1 | /* Copyright 2002, Red Hat Inc. */ |
---|
2 | |
---|
3 | #include <mqueue.h> |
---|
4 | #include <fcntl.h> |
---|
5 | #include <errno.h> |
---|
6 | #include <sys/ipc.h> |
---|
7 | #include <sys/sem.h> |
---|
8 | #include <string.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #define _LIBC 1 |
---|
11 | #include <sys/lock.h> |
---|
12 | #undef _LIBC |
---|
13 | |
---|
14 | #include "mqlocal.h" |
---|
15 | |
---|
16 | __LOCK_INIT(static, mq_wrbuf_lock); |
---|
17 | |
---|
18 | int |
---|
19 | mq_send (mqd_t msgid, const char *msg, size_t msg_len, unsigned int msg_prio) |
---|
20 | { |
---|
21 | struct libc_mq *info; |
---|
22 | struct sembuf sb2 = {2, -1, 0}; |
---|
23 | struct sembuf sb3 = {3, 1, 0}; |
---|
24 | int rc; |
---|
25 | int ipcflag; |
---|
26 | |
---|
27 | info = __find_mq (msgid); |
---|
28 | |
---|
29 | if (info == NULL || (info->oflag & O_ACCMODE) == O_RDONLY) |
---|
30 | { |
---|
31 | errno = EBADF; |
---|
32 | return -1; |
---|
33 | } |
---|
34 | |
---|
35 | if (msg_len > info->attr->mq_msgsize) |
---|
36 | { |
---|
37 | errno = EMSGSIZE; |
---|
38 | return -1; |
---|
39 | } |
---|
40 | |
---|
41 | if (msg_prio > MQ_PRIO_MAX) |
---|
42 | { |
---|
43 | errno = EINVAL; |
---|
44 | return -1; |
---|
45 | } |
---|
46 | |
---|
47 | __lock_acquire (mq_wrbuf_lock); |
---|
48 | |
---|
49 | memcpy (info->wrbuf->text, msg, msg_len); |
---|
50 | info->wrbuf->type = (MQ_PRIO_MAX - msg_prio); |
---|
51 | |
---|
52 | ipcflag = (info->attr->mq_flags & O_NONBLOCK) ? IPC_NOWAIT : 0; |
---|
53 | sb2.sem_flg = ipcflag; |
---|
54 | |
---|
55 | /* check to see if max msgs are on queue */ |
---|
56 | rc = semop (info->semid, &sb2, 1); |
---|
57 | |
---|
58 | if (rc == 0) |
---|
59 | rc = msgsnd (info->msgqid, info->wrbuf, msg_len, ipcflag); |
---|
60 | |
---|
61 | if (rc == 0) |
---|
62 | semop (info->semid, &sb3, 1); /* increment number of reads */ |
---|
63 | |
---|
64 | __lock_release (mq_wrbuf_lock); |
---|
65 | return rc; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | |
---|