source: trunk/libs/newlib/src/newlib/libc/sys/linux/mq_receive.c

Last change on this file was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.5 KB
Line 
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#define _LIBC 1
10#include <sys/lock.h>
11#undef _LIBC
12
13#include "mqlocal.h"
14
15__LOCK_INIT(static, mq_rdbuf_lock);
16
17ssize_t
18mq_receive (mqd_t msgid, char *msg, size_t msg_len, unsigned int *msg_prio)
19{
20  struct libc_mq *info;
21  struct sembuf sb2 = {2, 1, 0};
22  struct sembuf sb3 = {3, -1, IPC_NOWAIT};
23  struct sembuf sb5 = {5, 1, IPC_NOWAIT};
24  ssize_t num_bytes;
25  int ipcflag;
26
27  info = __find_mq (msgid);
28
29  if (info == NULL || (info->oflag & O_ACCMODE) == O_WRONLY)
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  __lock_acquire (mq_rdbuf_lock);
42
43  ipcflag = (info->attr->mq_flags & O_NONBLOCK) ? IPC_NOWAIT : 0;
44
45  semop (info->semid, &sb5, 1); /* increase number of readers */
46  num_bytes = msgrcv (info->msgqid, info->rdbuf, msg_len, -MQ_PRIO_MAX, ipcflag);
47  sb5.sem_op = -1;
48  semop (info->semid, &sb5, 1); /* decrease number of readers */
49
50  if (num_bytes != (ssize_t)-1)
51    {
52      semop (info->semid, &sb2, 1); /* add one to messages left to write */
53      semop (info->semid, &sb3, 1); /* subtract one from messages to read */
54      memcpy (msg, info->rdbuf->text, num_bytes);
55      if (msg_prio != NULL)
56        *msg_prio = MQ_PRIO_MAX - info->rdbuf->type;
57    }
58 
59  __lock_release (mq_rdbuf_lock);
60  return num_bytes;
61}
62     
63
64
65
66
67
Note: See TracBrowser for help on using the repository browser.