1 /**
2 * D header file for POSIX.
3 *
4 * Authors: Neven Miculinić
5 */
6 
7 module core.sys.posix.sys.msg;
8 
9 import core.sys.posix.config;
10 import core.sys.posix.sys.ipc;
11 import core.sys.posix.sys.types;
12 import core.stdc.config;
13 
14 version (CRuntime_Glibc):
15 // Some of these may be from linux kernel headers.
16 extern (C):
17 
18 version (ARM)     version = ARM_Any;
19 version (AArch64) version = ARM_Any;
20 version (HPPA)    version = HPPA_Any;
21 version (HPPA64)  version = HPPA_Any;
22 version (MIPS32)  version = MIPS_Any;
23 version (MIPS64)  version = MIPS_Any;
24 version (PPC)     version = PPC_Any;
25 version (PPC64)   version = PPC_Any;
26 version (RISCV32) version = RISCV_Any;
27 version (RISCV64) version = RISCV_Any;
28 version (S390)    version = IBMZ_Any;
29 version (SPARC)   version = SPARC_Any;
30 version (SPARC64) version = SPARC_Any;
31 version (SystemZ) version = IBMZ_Any;
32 
33 version (linux)
34 {
35     enum MSG_STAT = 11;
36     enum MSG_INFO = 12;
37 
38     enum MSG_NOERROR = 1 << 12; // octal!10000
39     enum  MSG_EXCEPT = 2 << 12; // octal!20000
40     enum    MSG_COPY = 4 << 12; // octal!40000
41 
42     struct msginfo
43     {
44         int msgpool;
45         int msgmap;
46         int msgmax;
47         int msgmnb;
48         int msgmni;
49         int msgssz;
50         int msgtql;
51         ushort msgseg;
52     }
53 
54     version (Alpha)     version = GENERICMSQ;
55     version (ARM_Any)   version = GENERICMSQ;
56     version (IA64)      version = GENERICMSQ;
57     version (IBMZ_Any)  version = GENERICMSQ;
58     version (RISCV_Any) version = GENERICMSQ;
59 
60     version (GENERICMSQ)
61     {
62         // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/bits/msq-pad.h
63         private enum MSQ_PAD_AFTER_TIME = (__WORDSIZE == 32);
64         private enum MSQ_PAD_BEFORE_TIME = false;
65     }
66     else version (HPPA_Any)
67     {
68         // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/hppa/bits/msq-pad.h
69         private enum MSQ_PAD_AFTER_TIME = false;
70         private enum MSQ_PAD_BEFORE_TIME = (__WORDSIZE == 32);
71     }
72     else version (MIPS_Any)
73     {
74         // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/mips/bits/msq-pad.h
75         version (LittleEndian)
76         {
77             private enum MSQ_PAD_AFTER_TIME = (__WORDSIZE == 32);
78             private enum MSQ_PAD_BEFORE_TIME = false;
79         }
80         else
81         {
82             private enum MSQ_PAD_AFTER_TIME = false;
83             private enum MSQ_PAD_BEFORE_TIME = (__WORDSIZE == 32);
84         }
85     }
86     else version (PPC_Any)
87     {
88         //  https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/powerpc/bits/msq-pad.h
89         private enum MSQ_PAD_AFTER_TIME = false;
90         private enum MSQ_PAD_BEFORE_TIME = (__WORDSIZE == 32);
91     }
92     else version (SPARC_Any)
93     {
94         // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/sparc/bits/msq-pad.h
95         private enum MSQ_PAD_AFTER_TIME = false;
96         private enum MSQ_PAD_BEFORE_TIME = (__WORDSIZE == 32);
97     }
98     else version (X86)
99     {
100         // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86/bits/msq-pad.h
101         private enum MSQ_PAD_AFTER_TIME = true;
102         private enum MSQ_PAD_BEFORE_TIME = false;
103     }
104     else version (X86_64)
105     {
106         // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86/bits/msq-pad.h
107         private enum MSQ_PAD_AFTER_TIME = false;
108         private enum MSQ_PAD_BEFORE_TIME = false;
109     }
110     else
111         static assert(0, "unimplemented");
112 
113     // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/bits/msq.h
114     alias msgqnum_t = ulong_t;
115     alias msglen_t = ulong_t;
116 
117     static if (MSQ_PAD_BEFORE_TIME)
118     {
119         struct msqid_ds
120         {
121             ipc_perm        msg_perm;
122             c_ulong __glibc_reserved1;
123             time_t          msg_stime;
124             c_ulong __glibc_reserved2;
125             time_t          msg_rtime;
126             c_ulong __glibc_reserved3;
127             time_t          msg_ctime;
128             ulong_t         __msg_cbytes;
129             msgqnum_t       msg_qnum;
130             msglen_t        msg_qbytes;
131             pid_t           msg_lspid;
132             pid_t           msg_lrpid;
133             ulong_t __glibc_reserved4;
134             ulong_t __glibc_reserved5;
135         }
136     }
137     else static if (MSQ_PAD_AFTER_TIME)
138     {
139         struct msqid_ds
140         {
141             ipc_perm        msg_perm;
142             time_t          msg_stime;
143             c_ulong __glibc_reserved1;
144             time_t          msg_rtime;
145             c_ulong __glibc_reserved2;
146             time_t          msg_ctime;
147             c_ulong __glibc_reserved3;
148             ulong_t         __msg_cbytes;
149             msgqnum_t       msg_qnum;
150             msglen_t        msg_qbytes;
151             pid_t           msg_lspid;
152             pid_t           msg_lrpid;
153             ulong_t __glibc_reserved4;
154             ulong_t __glibc_reserved5;
155         }
156     }
157     else
158     {
159         struct msqid_ds
160         {
161             ipc_perm        msg_perm;
162             time_t          msg_stime;
163             time_t          msg_rtime;
164             time_t          msg_ctime;
165             ulong_t         __msg_cbytes;
166             msgqnum_t       msg_qnum;
167             msglen_t        msg_qbytes;
168             pid_t           msg_lspid;
169             pid_t           msg_lrpid;
170             ulong_t __glibc_reserved4;
171             ulong_t __glibc_reserved5;
172         }
173     }
174 }
175 else
176 {
177     // https://sourceware.org/git/?p=glibc.git;a=blob;f=bits/msq.h
178     enum MSG_NOERROR = 1 << 12; // octal!10000
179 
180     alias msgqnum_t = ushort;
181     alias msglen_t = ushort;
182 
183     struct msqid_ds
184     {
185         ipc_perm msg_perm;
186         time_t          msg_stime;
187         time_t          msg_rtime;
188         time_t          msg_ctime;
189         msgqnum_t       msg_qnum;
190         msglen_t        msg_qbytes;
191         pid_t           msg_lspid;
192         pid_t           msg_lrpid;
193     }
194 }
195 
196 struct msgbuf
197 {
198     c_long mtype;
199     char[1] mtext = 0;
200 }
201 
202 int msgctl(int msqid, int cmd, msqid_ds* __buf);
203 int msgget(key_t key, int msgflg);
204 ssize_t msgrcv(int msqid, void* msgp, size_t msgsz, c_long msgtyp, int msgflg);
205 int msgsnd(int msqid, msgbuf* msgp, int msgsz, int msgflg);