This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Replace our assert-which-can-be-caught-by-eval with the real deal from
[perl5.git] / ext / IPC / SysV / SysV.xs
CommitLineData
0ade1984
JH
1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5#include <sys/types.h>
0ade1984 6#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
aec308ec 7#ifndef HAS_SEM
45615152 8# include <sys/ipc.h>
aec308ec 9#endif
45615152
DN
10# ifdef HAS_MSG
11# include <sys/msg.h>
12# endif
45615152 13# ifdef HAS_SHM
e8523c57 14# if defined(PERL_SCO) || defined(PERL_ISC)
45615152
DN
15# include <sys/sysmacros.h> /* SHMLBA */
16# endif
17# include <sys/shm.h>
18# ifndef HAS_SHMAT_PROTOTYPE
20ce7b12 19 extern Shmat_t shmat (int, char *, int);
45615152 20# endif
b57863ed
MHM
21# if defined(HAS_SYSCONF) && defined(_SC_PAGESIZE)
22# undef SHMLBA /* not static: determined at boot time */
23# define SHMLBA sysconf(_SC_PAGESIZE)
24# elif defined(HAS_GETPAGESIZE)
45615152
DN
25# undef SHMLBA /* not static: determined at boot time */
26# define SHMLBA getpagesize()
b57863ed
MHM
27# elif defined(__linux__)
28# include <asm/page.h>
45615152
DN
29# endif
30# endif
0ade1984
JH
31#endif
32
aec308ec
SB
33/* Required to get 'struct pte' for SHMLBA on ULTRIX. */
34#if defined(__ultrix) || defined(__ultrix__) || defined(ultrix)
35#include <machine/pte.h>
36#endif
37
1e509ade
JH
38/* Required in BSDI to get PAGE_SIZE definition for SHMLBA.
39 * Ugly. More beautiful solutions welcome.
40 * Shouting at BSDI sounds quite beautiful. */
41#ifdef __bsdi__
45615152 42# include <vm/vm_param.h> /* move upwards under HAS_SHM? */
1e509ade
JH
43#endif
44
85ab1d1d
JH
45#ifndef S_IRWXU
46# ifdef S_IRUSR
d46b2a5c
MHM
47# define S_IRWXU (S_IRUSR|S_IWUSR|S_IXUSR)
48# define S_IRWXG (S_IRGRP|S_IWGRP|S_IXGRP)
49# define S_IRWXO (S_IROTH|S_IWOTH|S_IXOTH)
85ab1d1d
JH
50# else
51# define S_IRWXU 0700
52# define S_IRWXG 0070
53# define S_IRWXO 0007
54# endif
55#endif
56
0ade1984
JH
57MODULE=IPC::SysV PACKAGE=IPC::Msg::stat
58
59PROTOTYPES: ENABLE
60
61void
62pack(obj)
63 SV * obj
64PPCODE:
65{
6087ac44 66#ifdef HAS_MSG
0ade1984
JH
67 SV *sv;
68 struct msqid_ds ds;
69 AV *list = (AV*)SvRV(obj);
70 sv = *av_fetch(list,0,TRUE); ds.msg_perm.uid = SvIV(sv);
71 sv = *av_fetch(list,1,TRUE); ds.msg_perm.gid = SvIV(sv);
72 sv = *av_fetch(list,4,TRUE); ds.msg_perm.mode = SvIV(sv);
73 sv = *av_fetch(list,6,TRUE); ds.msg_qbytes = SvIV(sv);
79cb57f6 74 ST(0) = sv_2mortal(newSVpvn((char *)&ds,sizeof(ds)));
0ade1984 75 XSRETURN(1);
6087ac44
JH
76#else
77 croak("System V msgxxx is not implemented on this machine");
78#endif
0ade1984
JH
79}
80
81void
82unpack(obj,buf)
83 SV * obj
84 SV * buf
85PPCODE:
86{
6087ac44 87#ifdef HAS_MSG
0ade1984
JH
88 STRLEN len;
89 SV **sv_ptr;
90 struct msqid_ds *ds = (struct msqid_ds *)SvPV(buf,len);
91 AV *list = (AV*)SvRV(obj);
92 if (len != sizeof(*ds)) {
93 croak("Bad arg length for %s, length is %d, should be %d",
94 "IPC::Msg::stat",
95 len, sizeof(*ds));
96 }
97 sv_ptr = av_fetch(list,0,TRUE);
98 sv_setiv(*sv_ptr, ds->msg_perm.uid);
99 sv_ptr = av_fetch(list,1,TRUE);
100 sv_setiv(*sv_ptr, ds->msg_perm.gid);
101 sv_ptr = av_fetch(list,2,TRUE);
102 sv_setiv(*sv_ptr, ds->msg_perm.cuid);
103 sv_ptr = av_fetch(list,3,TRUE);
104 sv_setiv(*sv_ptr, ds->msg_perm.cgid);
105 sv_ptr = av_fetch(list,4,TRUE);
106 sv_setiv(*sv_ptr, ds->msg_perm.mode);
107 sv_ptr = av_fetch(list,5,TRUE);
108 sv_setiv(*sv_ptr, ds->msg_qnum);
109 sv_ptr = av_fetch(list,6,TRUE);
110 sv_setiv(*sv_ptr, ds->msg_qbytes);
111 sv_ptr = av_fetch(list,7,TRUE);
112 sv_setiv(*sv_ptr, ds->msg_lspid);
113 sv_ptr = av_fetch(list,8,TRUE);
114 sv_setiv(*sv_ptr, ds->msg_lrpid);
115 sv_ptr = av_fetch(list,9,TRUE);
116 sv_setiv(*sv_ptr, ds->msg_stime);
117 sv_ptr = av_fetch(list,10,TRUE);
118 sv_setiv(*sv_ptr, ds->msg_rtime);
119 sv_ptr = av_fetch(list,11,TRUE);
120 sv_setiv(*sv_ptr, ds->msg_ctime);
121 XSRETURN(1);
6087ac44
JH
122#else
123 croak("System V msgxxx is not implemented on this machine");
124#endif
0ade1984
JH
125}
126
127MODULE=IPC::SysV PACKAGE=IPC::Semaphore::stat
128
129void
130unpack(obj,ds)
131 SV * obj
132 SV * ds
133PPCODE:
134{
6087ac44 135#ifdef HAS_SEM
0ade1984
JH
136 STRLEN len;
137 AV *list = (AV*)SvRV(obj);
138 struct semid_ds *data = (struct semid_ds *)SvPV(ds,len);
139 if(!sv_isa(obj, "IPC::Semaphore::stat"))
140 croak("method %s not called a %s object",
141 "unpack","IPC::Semaphore::stat");
142 if (len != sizeof(*data)) {
143 croak("Bad arg length for %s, length is %d, should be %d",
144 "IPC::Semaphore::stat",
145 len, sizeof(*data));
146 }
147 sv_setiv(*av_fetch(list,0,TRUE), data[0].sem_perm.uid);
148 sv_setiv(*av_fetch(list,1,TRUE), data[0].sem_perm.gid);
149 sv_setiv(*av_fetch(list,2,TRUE), data[0].sem_perm.cuid);
150 sv_setiv(*av_fetch(list,3,TRUE), data[0].sem_perm.cgid);
151 sv_setiv(*av_fetch(list,4,TRUE), data[0].sem_perm.mode);
152 sv_setiv(*av_fetch(list,5,TRUE), data[0].sem_ctime);
153 sv_setiv(*av_fetch(list,6,TRUE), data[0].sem_otime);
154 sv_setiv(*av_fetch(list,7,TRUE), data[0].sem_nsems);
155 XSRETURN(1);
6087ac44
JH
156#else
157 croak("System V semxxx is not implemented on this machine");
158#endif
0ade1984
JH
159}
160
161void
162pack(obj)
163 SV * obj
164PPCODE:
165{
6087ac44 166#ifdef HAS_SEM
0ade1984 167 SV **sv_ptr;
0ade1984
JH
168 struct semid_ds ds;
169 AV *list = (AV*)SvRV(obj);
170 if(!sv_isa(obj, "IPC::Semaphore::stat"))
171 croak("method %s not called a %s object",
172 "pack","IPC::Semaphore::stat");
497b47a8 173 if((sv_ptr = av_fetch(list,0,TRUE)) && *sv_ptr)
0ade1984 174 ds.sem_perm.uid = SvIV(*sv_ptr);
497b47a8 175 if((sv_ptr = av_fetch(list,1,TRUE)) && *sv_ptr)
0ade1984 176 ds.sem_perm.gid = SvIV(*sv_ptr);
497b47a8 177 if((sv_ptr = av_fetch(list,2,TRUE)) && *sv_ptr)
0ade1984 178 ds.sem_perm.cuid = SvIV(*sv_ptr);
497b47a8 179 if((sv_ptr = av_fetch(list,3,TRUE)) && *sv_ptr)
0ade1984 180 ds.sem_perm.cgid = SvIV(*sv_ptr);
497b47a8 181 if((sv_ptr = av_fetch(list,4,TRUE)) && *sv_ptr)
0ade1984 182 ds.sem_perm.mode = SvIV(*sv_ptr);
497b47a8 183 if((sv_ptr = av_fetch(list,5,TRUE)) && *sv_ptr)
0ade1984 184 ds.sem_ctime = SvIV(*sv_ptr);
497b47a8 185 if((sv_ptr = av_fetch(list,6,TRUE)) && *sv_ptr)
0ade1984 186 ds.sem_otime = SvIV(*sv_ptr);
497b47a8 187 if((sv_ptr = av_fetch(list,7,TRUE)) && *sv_ptr)
0ade1984 188 ds.sem_nsems = SvIV(*sv_ptr);
79cb57f6 189 ST(0) = sv_2mortal(newSVpvn((char *)&ds,sizeof(ds)));
0ade1984 190 XSRETURN(1);
6087ac44
JH
191#else
192 croak("System V semxxx is not implemented on this machine");
193#endif
0ade1984
JH
194}
195
196MODULE=IPC::SysV PACKAGE=IPC::SysV
197
8063af02 198void
0ade1984
JH
199ftok(path, id)
200 char * path
201 int id
202 CODE:
203#if defined(HAS_SEM) || defined(HAS_SHM)
204 key_t k = ftok(path, id);
6b88bc9c 205 ST(0) = k == (key_t) -1 ? &PL_sv_undef : sv_2mortal(newSViv(k));
0ade1984 206#else
7d5888f8 207 Perl_die(aTHX_ PL_no_func, "ftok"); return;
0ade1984
JH
208#endif
209
8063af02 210void
0ade1984
JH
211SHMLBA()
212 CODE:
213#ifdef SHMLBA
214 ST(0) = sv_2mortal(newSViv(SHMLBA));
215#else
216 croak("SHMLBA is not defined on this architecture");
217#endif
218
219BOOT:
220{
da51bb9b 221 HV *stash = gv_stashpvn("IPC::SysV", 9, GV_ADD);
0ade1984
JH
222 /*
223 * constant subs for IPC::SysV
224 */
d3f5e399 225 struct { const char *n; I32 v; } IPC__SysV__const[] = {
0ade1984
JH
226#ifdef GETVAL
227 {"GETVAL", GETVAL},
228#endif
229#ifdef GETPID
230 {"GETPID", GETPID},
231#endif
232#ifdef GETNCNT
233 {"GETNCNT", GETNCNT},
234#endif
235#ifdef GETZCNT
236 {"GETZCNT", GETZCNT},
237#endif
238#ifdef GETALL
239 {"GETALL", GETALL},
240#endif
241#ifdef IPC_ALLOC
242 {"IPC_ALLOC", IPC_ALLOC},
243#endif
244#ifdef IPC_CREAT
245 {"IPC_CREAT", IPC_CREAT},
246#endif
247#ifdef IPC_EXCL
248 {"IPC_EXCL", IPC_EXCL},
249#endif
250#ifdef IPC_GETACL
d46b2a5c 251 {"IPC_GETACL", IPC_GETACL},
0ade1984
JH
252#endif
253#ifdef IPC_LOCKED
254 {"IPC_LOCKED", IPC_LOCKED},
255#endif
256#ifdef IPC_M
257 {"IPC_M", IPC_M},
258#endif
259#ifdef IPC_NOERROR
260 {"IPC_NOERROR", IPC_NOERROR},
261#endif
262#ifdef IPC_NOWAIT
263 {"IPC_NOWAIT", IPC_NOWAIT},
264#endif
265#ifdef IPC_PRIVATE
266 {"IPC_PRIVATE", IPC_PRIVATE},
267#endif
268#ifdef IPC_R
269 {"IPC_R", IPC_R},
270#endif
271#ifdef IPC_RMID
272 {"IPC_RMID", IPC_RMID},
273#endif
274#ifdef IPC_SET
275 {"IPC_SET", IPC_SET},
276#endif
277#ifdef IPC_SETACL
278 {"IPC_SETACL", IPC_SETACL},
279#endif
280#ifdef IPC_SETLABEL
281 {"IPC_SETLABEL", IPC_SETLABEL},
282#endif
283#ifdef IPC_STAT
284 {"IPC_STAT", IPC_STAT},
285#endif
286#ifdef IPC_W
287 {"IPC_W", IPC_W},
288#endif
289#ifdef IPC_WANTED
290 {"IPC_WANTED", IPC_WANTED},
291#endif
292#ifdef MSG_NOERROR
293 {"MSG_NOERROR", MSG_NOERROR},
294#endif
295#ifdef MSG_FWAIT
296 {"MSG_FWAIT", MSG_FWAIT},
297#endif
298#ifdef MSG_LOCKED
299 {"MSG_LOCKED", MSG_LOCKED},
300#endif
301#ifdef MSG_MWAIT
302 {"MSG_MWAIT", MSG_MWAIT},
303#endif
304#ifdef MSG_WAIT
305 {"MSG_WAIT", MSG_WAIT},
306#endif
307#ifdef MSG_R
308 {"MSG_R", MSG_R},
309#endif
310#ifdef MSG_RWAIT
311 {"MSG_RWAIT", MSG_RWAIT},
312#endif
313#ifdef MSG_STAT
314 {"MSG_STAT", MSG_STAT},
315#endif
316#ifdef MSG_W
317 {"MSG_W", MSG_W},
318#endif
319#ifdef MSG_WWAIT
320 {"MSG_WWAIT", MSG_WWAIT},
321#endif
322#ifdef SEM_A
323 {"SEM_A", SEM_A},
324#endif
325#ifdef SEM_ALLOC
326 {"SEM_ALLOC", SEM_ALLOC},
327#endif
328#ifdef SEM_DEST
329 {"SEM_DEST", SEM_DEST},
330#endif
331#ifdef SEM_ERR
332 {"SEM_ERR", SEM_ERR},
333#endif
334#ifdef SEM_R
335 {"SEM_R", SEM_R},
336#endif
337#ifdef SEM_ORDER
338 {"SEM_ORDER", SEM_ORDER},
339#endif
340#ifdef SEM_UNDO
341 {"SEM_UNDO", SEM_UNDO},
342#endif
343#ifdef SETVAL
344 {"SETVAL", SETVAL},
345#endif
346#ifdef SETALL
347 {"SETALL", SETALL},
348#endif
349#ifdef SHM_CLEAR
350 {"SHM_CLEAR", SHM_CLEAR},
351#endif
352#ifdef SHM_COPY
353 {"SHM_COPY", SHM_COPY},
354#endif
355#ifdef SHM_DCACHE
356 {"SHM_DCACHE", SHM_DCACHE},
357#endif
358#ifdef SHM_DEST
359 {"SHM_DEST", SHM_DEST},
360#endif
361#ifdef SHM_ECACHE
362 {"SHM_ECACHE", SHM_ECACHE},
363#endif
364#ifdef SHM_FMAP
365 {"SHM_FMAP", SHM_FMAP},
366#endif
367#ifdef SHM_ICACHE
368 {"SHM_ICACHE", SHM_ICACHE},
369#endif
370#ifdef SHM_INIT
371 {"SHM_INIT", SHM_INIT},
372#endif
373#ifdef SHM_LOCK
374 {"SHM_LOCK", SHM_LOCK},
375#endif
376#ifdef SHM_LOCKED
377 {"SHM_LOCKED", SHM_LOCKED},
378#endif
379#ifdef SHM_MAP
380 {"SHM_MAP", SHM_MAP},
381#endif
382#ifdef SHM_NOSWAP
383 {"SHM_NOSWAP", SHM_NOSWAP},
384#endif
385#ifdef SHM_RDONLY
386 {"SHM_RDONLY", SHM_RDONLY},
387#endif
388#ifdef SHM_REMOVED
389 {"SHM_REMOVED", SHM_REMOVED},
390#endif
391#ifdef SHM_RND
392 {"SHM_RND", SHM_RND},
393#endif
394#ifdef SHM_SHARE_MMU
395 {"SHM_SHARE_MMU", SHM_SHARE_MMU},
396#endif
397#ifdef SHM_SHATTR
398 {"SHM_SHATTR", SHM_SHATTR},
399#endif
400#ifdef SHM_SIZE
401 {"SHM_SIZE", SHM_SIZE},
402#endif
403#ifdef SHM_UNLOCK
404 {"SHM_UNLOCK", SHM_UNLOCK},
405#endif
406#ifdef SHM_W
407 {"SHM_W", SHM_W},
408#endif
409#ifdef S_IRUSR
410 {"S_IRUSR", S_IRUSR},
411#endif
412#ifdef S_IWUSR
413 {"S_IWUSR", S_IWUSR},
414#endif
415#ifdef S_IRWXU
416 {"S_IRWXU", S_IRWXU},
417#endif
418#ifdef S_IRGRP
419 {"S_IRGRP", S_IRGRP},
420#endif
421#ifdef S_IWGRP
422 {"S_IWGRP", S_IWGRP},
423#endif
424#ifdef S_IRWXG
425 {"S_IRWXG", S_IRWXG},
426#endif
427#ifdef S_IROTH
428 {"S_IROTH", S_IROTH},
429#endif
430#ifdef S_IWOTH
431 {"S_IWOTH", S_IWOTH},
432#endif
433#ifdef S_IRWXO
434 {"S_IRWXO", S_IRWXO},
435#endif
436 {Nullch,0}};
d3f5e399 437 const char *name;
0ade1984
JH
438 int i;
439
8063af02 440 for(i = 0 ; (name = IPC__SysV__const[i].n) ; i++) {
0ade1984
JH
441 newCONSTSUB(stash,name, newSViv(IPC__SysV__const[i].v));
442 }
443}
444