| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * mq_open.c - open a message queue. | 
|  | 3 | */ | 
|  | 4 |  | 
|  | 5 | #include <errno.h> | 
|  | 6 | #include <stdarg.h> | 
|  | 7 | #include <stddef.h> | 
|  | 8 | #include <sys/syscall.h> | 
|  | 9 |  | 
|  | 10 | #include <mqueue.h> | 
|  | 11 |  | 
|  | 12 | #ifdef __NR_mq_open | 
|  | 13 |  | 
|  | 14 | #define __NR___syscall_mq_open __NR_mq_open | 
|  | 15 | static __inline__ _syscall4(int, __syscall_mq_open, const char *, name, | 
|  | 16 | int, oflag, __kernel_mode_t, mode, void *, attr); | 
|  | 17 | /* | 
|  | 18 | * Establish connection between a process and a message queue and | 
|  | 19 | * return message queue descriptor or (mqd_t) -1 on error. | 
|  | 20 | * oflag determines the type of access used. If O_CREAT is on oflag, the | 
|  | 21 | * third argument is taken as a `mode_t', the mode of the created | 
|  | 22 | * message queue, and the fourth argument is taken as `struct mq_attr *', | 
|  | 23 | * pointer to message queue attributes. | 
|  | 24 | * If the fourth argument is NULL, default attributes are used. | 
|  | 25 | */ | 
|  | 26 | mqd_t mq_open(const char *name, int oflag, ...) | 
|  | 27 | { | 
|  | 28 | mode_t mode; | 
|  | 29 | struct mq_attr *attr; | 
|  | 30 |  | 
|  | 31 | if (name[0] != '/') { | 
|  | 32 | __set_errno(EINVAL); | 
|  | 33 | return -1; | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | mode = 0; | 
|  | 37 | attr = NULL; | 
|  | 38 |  | 
|  | 39 | if (oflag & O_CREAT) { | 
|  | 40 | va_list ap; | 
|  | 41 |  | 
|  | 42 | va_start(ap, oflag); | 
|  | 43 | mode = va_arg(ap, mode_t); | 
|  | 44 | attr = va_arg(ap, struct mq_attr *); | 
|  | 45 |  | 
|  | 46 | va_end(ap); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | return __syscall_mq_open(name + 1, oflag, mode, attr); | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | #endif |