msgq.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <errno.h>
  2. #include <sys/msg.h>
  3. #include "ipc.h"
  4. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  5. #include "sysdep-cancel.h"
  6. #else
  7. #define SINGLE_THREAD_P 1
  8. #endif
  9. #ifdef L_msgctl
  10. #ifdef __NR_msgctl
  11. #define __NR___libc_msgctl __NR_msgctl
  12. static __inline__ _syscall3(int, __libc_msgctl, int, msqid, int, cmd, struct msqid_ds *, buf);
  13. #endif
  14. /* Message queue control operation. */
  15. int msgctl(int msqid, int cmd, struct msqid_ds *buf)
  16. {
  17. #ifdef __NR_msgctl
  18. return __libc_msgctl(msqid, cmd | __IPC_64, buf);
  19. #else
  20. return __syscall_ipc(IPCOP_msgctl, msqid, cmd | __IPC_64, 0, buf, 0);
  21. #endif
  22. }
  23. #endif
  24. #ifdef L_msgget
  25. #ifdef __NR_msgget
  26. _syscall2(int, msgget, key_t, key, int, msgflg)
  27. #else
  28. /* Get messages queue. */
  29. int msgget (key_t key, int msgflg)
  30. {
  31. return __syscall_ipc(IPCOP_msgget ,key ,msgflg ,0 ,0, 0);
  32. }
  33. #endif
  34. #endif
  35. struct new_msg_buf{
  36. struct msgbuf * oldmsg;
  37. long int r_msgtyp; /* the fifth arg of __syscall_ipc */
  38. };
  39. /* Receive message from message queue. */
  40. #ifdef L_msgrcv
  41. #ifdef __NR_msgrcv
  42. #define __NR___syscall_msgrcv __NR_msgrcv
  43. static inline _syscall5(ssize_t, __syscall_msgrcv, int, msqid, void *, msgp,
  44. size_t, msgsz, long int, msgtyp, int, msgflg)
  45. #endif
  46. static inline ssize_t do_msgrcv (int msqid, void *msgp, size_t msgsz,
  47. long int msgtyp, int msgflg)
  48. {
  49. #ifdef __NR_msgrcv
  50. return __syscall_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
  51. #else
  52. struct new_msg_buf temp;
  53. temp.r_msgtyp = msgtyp;
  54. temp.oldmsg = msgp;
  55. return __syscall_ipc(IPCOP_msgrcv ,msqid ,msgsz ,msgflg ,&temp, 0);
  56. #endif
  57. }
  58. ssize_t msgrcv (int msqid, void *msgp, size_t msgsz,
  59. long int msgtyp, int msgflg)
  60. {
  61. if (SINGLE_THREAD_P)
  62. return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
  63. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  64. int oldtype = LIBC_CANCEL_ASYNC ();
  65. int result = do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
  66. LIBC_CANCEL_RESET (oldtype);
  67. return result;
  68. #endif
  69. }
  70. #endif
  71. #ifdef L_msgsnd
  72. #ifdef __NR_msgsnd
  73. #define __NR___syscall_msgsnd __NR_msgsnd
  74. static inline _syscall4(int, __syscall_msgsnd, int, msqid, const void *, msgp,
  75. size_t, msgsz, int, msgflg)
  76. #endif
  77. /* Send message to message queue. */
  78. static inline int do_msgsnd (int msqid, const void *msgp, size_t msgsz,
  79. int msgflg)
  80. {
  81. #ifdef __NR_msgsnd
  82. return __syscall_msgsnd(msqid, msgp, msgsz, msgflg);
  83. #else
  84. return __syscall_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg, (void *)msgp, 0);
  85. #endif
  86. }
  87. int msgsnd (int msqid, const void *msgp, size_t msgsz, int msgflg)
  88. {
  89. if (SINGLE_THREAD_P)
  90. return do_msgsnd(msqid, msgp, msgsz, msgflg);
  91. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  92. int oldtype = LIBC_CANCEL_ASYNC ();
  93. int result = do_msgsnd(msqid, msgp, msgsz, msgflg);
  94. LIBC_CANCEL_RESET (oldtype);
  95. return result;
  96. #endif
  97. }
  98. #endif