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