ipc.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2004 by the Free Pascal development team
  4. This file implements IPC calls calls for Linu/FreeBSD
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit ipc;
  12. interface
  13. Uses
  14. {$ifdef FPC_USE_LIBCX}
  15. initc,
  16. {$endif}
  17. BaseUnix,UnixType;
  18. {$i osdefs.inc} { Compile time defines }
  19. { ----------------------------------------------------------------------
  20. General IPC stuff
  21. ----------------------------------------------------------------------}
  22. //Var
  23. // IPCError : longint;
  24. {$packrecords c}
  25. Type
  26. {$IFDEF FreeBSD}
  27. TKey = clong;
  28. {$ELSE}
  29. TKey = cint;
  30. {$ENDIF}
  31. key_t = TKey;
  32. Const
  33. { IPC flags for get calls }
  34. {$if defined(FreeBSD) or defined(NetBSD) or defined(OpenBSD)} // BSD_VISIBLE
  35. IPC_R = 4 shl 6;
  36. IPC_W = 2 shl 6;
  37. IPC_M = 2 shl 12;
  38. {$endif}
  39. {$ifdef Darwin}
  40. IPC_R = 4 shl 6;
  41. IPC_W = 2 shl 6;
  42. IPC_M = 1 shl 12;
  43. {$endif}
  44. {$ifdef aix}
  45. IPC_R = 4 shl 6;
  46. IPC_W = 2 shl 6;
  47. { no IPC_M }
  48. {$endif}
  49. {$ifndef aix}
  50. IPC_CREAT = 1 shl 9; { create if key is non-existent }
  51. {$else aix}
  52. IPC_CREAT = 2 shl 12;
  53. {$endif aix}
  54. IPC_EXCL = 2 shl 9; { fail if key exists }
  55. IPC_NOWAIT = 4 shl 9; { return error on wait }
  56. {$if defined(FreeBSD) or defined(Darwin) or defined(Linux) or defined(OpenBSD)}
  57. IPC_PRIVATE = TKey(0);
  58. {$elseif defined(aix)}
  59. IPC_PRIVATE = TKey(-1)
  60. {$endif}
  61. { Actions for ctl calls }
  62. IPC_RMID = 0; { remove resource }
  63. {$ifndef aix}
  64. IPC_SET = 1; { set ipc_perm options }
  65. IPC_STAT = 2; { get ipc_perm options }
  66. {$else aix}
  67. IPC_SET = 101; { set ipc_perm options }
  68. IPC_STAT = 102; { get ipc_perm options }
  69. {$endif aix}
  70. {$ifndef Darwin}
  71. IPC_INFO = 3; { see ipcs }
  72. {$endif}
  73. type
  74. PIPC_Perm = ^TIPC_Perm;
  75. {$if defined(darwin) }
  76. {$packrecords 4}
  77. { This is also the strcut for FreeBSD up to version 7
  78. renamed ipc_perm_old in /usr/include/sys/ipc.h in version 8 and after }
  79. TIPC_Perm = record
  80. cuid : cushort; { creator user id }
  81. cgid : cushort; { creator group id }
  82. uid : cushort; { user id }
  83. gid : cushort; { group id }
  84. mode : cushort; { r/w permission }
  85. seq : cushort; { sequence # (to generate unique msg/sem/shm id) }
  86. key : key_t; { user specified msg/sem/shm key }
  87. End;
  88. {$packrecords c}
  89. {$elseif defined(NetBSD) or defined(OpenBSD) or defined(FreeBSD) }
  90. TIPC_Perm = record
  91. cuid : uid_t; { creator user id }
  92. cgid : gid_t; { creator group id }
  93. uid : uid_t; { user id }
  94. gid : gid_t; { group id }
  95. mode : mode_t; { r/w permission }
  96. seq : cushort; { sequence # (to generate unique msg/sem/shm id) }
  97. key : key_t; { user specified msg/sem/shm key }
  98. End;
  99. {$elseif defined(aix)}
  100. TIPC_Perm = record
  101. uid : uid_t;
  102. git : gid_t;
  103. cuid : uid_t;
  104. cgit : git_t;
  105. mode : mode_t;
  106. seq : cushort;
  107. key : key_t;
  108. end;
  109. {$else } // linux
  110. {$ifdef cpu32}
  111. {$ifndef linux_ipc64}
  112. {$define linux_ipc32}
  113. {$endif}
  114. {$endif}
  115. {$if not defined(linux_ipc32) and not defined(FPC_USE_LIBC)}
  116. TIPC_Perm = record
  117. key : TKey;
  118. uid : kernel_uid_t;
  119. gid : kernel_gid_t;
  120. cuid : kernel_uid_t;
  121. cgid : kernel_gid_t;
  122. mode : kernel_mode_t;
  123. {$if sizeof(kernel_mode_t) < 4}
  124. __pad1 : array[1..4-sizeof(mode_t)];
  125. {$endif}
  126. {$ifdef cpupowerpc}
  127. seq : cuint;
  128. {$else}
  129. seq : cushort;
  130. {$endif}
  131. __pad2 : cushort;
  132. __unused1 : culong;
  133. __unused2 : culong;
  134. End;
  135. {$else not(linux_ipc32) and not(FPC_USE_LIBC)}
  136. TIPC_Perm = record
  137. key : TKey;
  138. uid : kernel_uid_t;
  139. gid : kernel_gid_t;
  140. cuid : kernel_uid_t;
  141. cgid : kernel_gid_t;
  142. mode : kernel_mode_t;
  143. seq : cushort;
  144. End;
  145. {$endif not(linux_ipc32) and not(FPC_USE_LIBC)}
  146. {$endif}
  147. { Function to generate a IPC key. }
  148. Function ftok (Path : pchar; ID : cint) : TKey; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'ftok'; {$endif}
  149. { ----------------------------------------------------------------------
  150. Sys V Shared memory stuff
  151. ----------------------------------------------------------------------}
  152. Type
  153. PShmid_DS = ^TShmid_ds;
  154. {$if defined(FreeBSD) or defined(OpenBSD) or defined (NetBSD) }
  155. TShmid_ds = record
  156. shm_perm : TIPC_Perm;
  157. shm_segsz : cint;
  158. shm_lpid : pid_t;
  159. shm_cpid : pid_t;
  160. shm_nattch : cshort;
  161. shm_atime : time_t;
  162. shm_dtime : time_t;
  163. shm_ctime : time_t;
  164. shm_internal : pointer;
  165. end;
  166. {$endif}
  167. {$ifdef Darwin}
  168. {$packrecords 4}
  169. TShmid_ds = record
  170. shm_perm : TIPC_Perm;
  171. shm_segsz : size_t;
  172. shm_lpid : pid_t;
  173. shm_cpid : pid_t;
  174. shm_nattch : cushort; // typedef unsigned short shmatt_t
  175. shm_atime : time_t;
  176. shm_dtime : time_t;
  177. shm_ctime : time_t;
  178. shm_internal : pointer;
  179. end;
  180. {$packrecords c}
  181. {$endif}
  182. {$ifdef Linux}
  183. {$ifdef cpux86_64}
  184. TShmid_ds = record
  185. shm_perm : TIPC_Perm;
  186. shm_segsz : size_t;
  187. shm_atime : time_t;
  188. shm_dtime : time_t;
  189. shm_ctime : time_t;
  190. shm_cpid : pid_t;
  191. shm_lpid : pid_t;
  192. shm_nattch : culong;
  193. __unused4 : culong;
  194. __unused5 : culong;
  195. end;
  196. {$else cpux86_64}
  197. TShmid_ds = record
  198. shm_perm : TIPC_Perm;
  199. shm_segsz : cint;
  200. shm_atime : time_t;
  201. shm_dtime : time_t;
  202. shm_ctime : time_t;
  203. shm_cpid : ipc_pid_t;
  204. shm_lpid : ipc_pid_t;
  205. shm_nattch : word;
  206. shm_npages : word;
  207. shm_pages : pointer;
  208. attaches : pointer;
  209. end;
  210. {$endif cpux86_64}
  211. {$endif}
  212. {$ifdef aix}
  213. shmatt_t = {$ifdef cpu64}culong{$else}cushort{$endif};
  214. TShmid_ds = record
  215. shm_perm : TIPC_Perm;
  216. shm_segsz : size_t;
  217. shm_lpid : pid_t;
  218. shm_cpid : pid_t;
  219. shm_nattch : shmatt_t;
  220. shm_cnattch : shmatt_t;
  221. shm_atime : time_t;
  222. shm_dtime : time_t;
  223. shm_ctime : time_t;
  224. shm_handle : {$ifdef cpu64}cuint{$else}culong{$endif};
  225. shm_extshm : cint;
  226. shm_pagesize : cint64;
  227. shm_lba : cuint64;
  228. shm_reserved0 : cint64;
  229. shm_reserved1 : cint64;
  230. end;
  231. {$endif aix}
  232. const
  233. {$ifdef Linux}
  234. SHM_R = 4 shl 6;
  235. SHM_W = 2 shl 6;
  236. {$else}
  237. SHM_R = IPC_R;
  238. SHM_W = IPC_W;
  239. {$endif}
  240. SHM_RDONLY = 1 shl 12;
  241. SHM_RND = 2 shl 12;
  242. {$ifdef Linux}
  243. SHM_REMAP = 4 shl 12;
  244. {$endif}
  245. {$ifdef Darwin}
  246. SHMLBA = 4096;
  247. {$endif}
  248. {$ifdef aix}
  249. SHM_PIN = 4 shl 9;
  250. SHM_LGPAGE = 2 shl 30; { only available with SHM_PIN }
  251. {$endif}
  252. {$ifndef aix}
  253. SHM_LOCK = 11;
  254. SHM_UNLOCK = 12;
  255. {$else not aix}
  256. SHM_SIZE = 6;
  257. SHM_PAGESIZE = 200;
  258. SHM_LOCK = 201;
  259. SHM_UNLOCK = 202;
  260. SHM_GETLBA = 203;
  261. {$endif not aix}
  262. {$ifdef FreeBSD} // ipcs shmctl commands
  263. SHM_STAT = 13;
  264. SHM_INFO = 14;
  265. {$endif}
  266. type // the shm*info kind is "kernel" only.
  267. PSHMinfo = ^TSHMinfo;
  268. {$ifndef aix}
  269. TSHMinfo = record // comment under FreeBSD/Darwin: do we really need this?
  270. shmmax : cint;
  271. shmmin : cint;
  272. shmmni : cint;
  273. shmseg : cint;
  274. shmall : cint;
  275. end;
  276. {$else not aix }
  277. TSHMinfo = record
  278. shmmax : culonglong;
  279. shmmin : cint;
  280. shmmni : cint;
  281. end;
  282. {$endif not aix}
  283. {$if defined(FreeBSD) or defined(OpenBSD) or defined(Linux)}
  284. PSHM_info = ^TSHM_info;
  285. TSHM_info = record
  286. used_ids : cint;
  287. shm_tot,
  288. shm_rss,
  289. shm_swp,
  290. swap_attempts,
  291. swap_successes : culong;
  292. end;
  293. {$endif}
  294. Function shmget(key: Tkey; size:size_t; flag:cint):cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmget'; {$endif}
  295. Function shmat (shmid:cint; shmaddr:pointer; shmflg:cint):pointer; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmat'; {$endif}
  296. Function shmdt (shmaddr:pointer):cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmdt'; {$endif}
  297. Function shmctl(shmid:cint; cmd:cint; buf: pshmid_ds): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmctl'; {$endif}
  298. { ----------------------------------------------------------------------
  299. Message queue stuff
  300. ----------------------------------------------------------------------}
  301. const
  302. MSG_NOERROR = 1 shl 12;
  303. {$ifdef Linux}
  304. MSG_EXCEPT = 2 shl 12;
  305. MSGMNI = 128;
  306. MSGMAX = 4056;
  307. MSGMNB = 16384;
  308. {$endif}
  309. type
  310. msglen_t = culong;
  311. {$ifndef aix}
  312. msgqnum_t= culong;
  313. {$else not aix}
  314. msgqnum_t= cuint;
  315. {$endif not aix}
  316. {$ifdef Darwin}
  317. user_msglen_t = culonglong;
  318. user_msgqnum_t= culonglong;
  319. {$endif}
  320. {$ifdef aix}
  321. TMsg_Hdr = record
  322. mtime : time_t;
  323. muid : uid_t;
  324. mgid : git_t;
  325. mpid : pid_t;
  326. mtype : clong;
  327. end;
  328. {$endif aix}
  329. PMSG = ^TMSG;
  330. TMSG = record
  331. {$ifndef FreeBSD} // opaque in FreeBSD
  332. {$if defined(Darwin)}
  333. msg_next : PMSG;
  334. msg_type : clong;
  335. msg_ts : cushort;
  336. mac_label : pointer;
  337. {$elseif defined(aix)}
  338. msg_next : PMSG;
  339. msg_attr : TMsg_Hdr;
  340. msg_ts : cuint;
  341. msg_spot : pchar;
  342. {$else}
  343. msg_next : PMSG;
  344. msg_type : Longint;
  345. msg_spot : PChar;
  346. msg_stime : Longint;
  347. msg_ts : Integer;
  348. {$endif}
  349. {$endif}
  350. end;
  351. type
  352. {$ifdef Linux}
  353. PMSQid_ds = ^TMSQid_ds;
  354. TMSQid_ds = record
  355. msg_perm : TIPC_perm;
  356. msg_first : PMsg;
  357. msg_last : PMsg;
  358. msg_stime : time_t;
  359. msg_rtime : time_t;
  360. msg_ctime : time_t;
  361. msg_cbytes : word;
  362. msg_qnum : word;
  363. msg_qbytes : word;
  364. msg_lspid : ipc_pid_t;
  365. msg_lrpid : ipc_pid_t;
  366. end;
  367. {$else}
  368. {$if defined(Darwin)}
  369. {$packrecords 4}
  370. PMSQid_ds = ^TMSQid_ds;
  371. TMSQid_ds = record
  372. msg_perm : TIPC_perm;
  373. msg_first : cint32;
  374. msg_last : cint32;
  375. msg_cbytes : msglen_t;
  376. msg_qnum : msgqnum_t;
  377. msg_qbytes : msglen_t;
  378. msg_lspid : pid_t;
  379. msg_lrpid : pid_t;
  380. msg_stime : time_t;
  381. msg_pad1 : cint32;
  382. msg_rtime : time_t;
  383. msg_pad2 : cint32;
  384. msg_ctime : time_t;
  385. msg_pad3 : cint32;
  386. msg_pad4 : array [0..3] of cint32;
  387. end;
  388. {$packrecords c}
  389. {$elseif defined(aix)}
  390. PMSQid_ds = ^TMSQid_ds;
  391. TMSQid_ds = record
  392. msg_perm : TIPC_perm;
  393. {$ifdef cpu64}
  394. msg_first : cuint;
  395. msg_last : cuint;
  396. {$else cpu64}
  397. msg_first : PMsg;
  398. msg_last : PMsg;
  399. {$endif}
  400. msg_cbytes : cuint;
  401. msg_qnum : msgqnum_t;
  402. msg_qbytes : msglen_t;
  403. msg_lspid : pid_t;
  404. msg_lrpid : pid_t;
  405. msg_stime : time_t;
  406. msg_rtime : time_t;
  407. msg_ctime : time_t;
  408. msg_rwait : cint;
  409. msg_wwait : cint;
  410. msg_reqevents : cushort;
  411. end;
  412. {$else}
  413. PMSQid_ds = ^TMSQid_ds;
  414. TMSQid_ds = record
  415. msg_perm : TIPC_perm;
  416. msg_first : PMsg;
  417. msg_last : PMsg;
  418. msg_cbytes : msglen_t;
  419. msg_qnum : msgqnum_t;
  420. msg_qbytes : msglen_t;
  421. msg_lspid : pid_t;
  422. msg_lrpid : pid_t;
  423. msg_stime : time_t;
  424. msg_pad1 : clong;
  425. msg_rtime : time_t;
  426. msg_pad2 : clong;
  427. msg_ctime : time_t;
  428. msg_pad3 : clong;
  429. msg_pad4 : array [0..3] of clong;
  430. end;
  431. {$endif}
  432. {$endif}
  433. PMSGbuf = ^TMSGbuf;
  434. TMSGbuf = record // called mymsg on freebsd and SVID manual
  435. mtype : clong;
  436. mtext : array[0..0] of char;
  437. end;
  438. {$if defined(linux)}
  439. PMSGinfo = ^TMSGinfo;
  440. TMSGinfo = record
  441. msgpool : cint;
  442. msgmap : cint;
  443. msgmax : cint;
  444. msgmnb : cint;
  445. msgmni : cint;
  446. msgssz : cint;
  447. msgtql : cint;
  448. msgseg : cushort;
  449. end;
  450. {$elseif defined(aix)}
  451. PMSGinfo = ^TMSGinfo;
  452. TMSGinfo = record
  453. msgmax,
  454. msgmnb,
  455. msgmni,
  456. msgmnm : cint;
  457. end;
  458. {$else}
  459. PMSGinfo = ^TMSGinfo;
  460. TMSGinfo = record
  461. msgmax,
  462. msgmni,
  463. msgmnb,
  464. msgtql,
  465. msgssz,
  466. msgseg : cint;
  467. end;
  468. {$endif}
  469. Function msgget(key: TKey; msgflg:cint):cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgget'; {$endif}
  470. Function msgsnd(msqid:cint; msgp: PMSGBuf; msgsz: size_t; msgflg:cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgsnd'; {$endif}
  471. Function msgrcv(msqid:cint; msgp: PMSGBuf; msgsz: size_t; msgtyp:clong; msgflg:cint): {$if defined(Darwin) or defined(aix)}ssize_t;{$else}cint;{$endif} {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgrcv'; {$endif}
  472. Function msgctl(msqid:cint; cmd: cint; buf: PMSQid_ds): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgctl'; {$endif}
  473. { ----------------------------------------------------------------------
  474. Semaphores stuff
  475. ----------------------------------------------------------------------}
  476. const
  477. {$ifdef Linux} // renamed to many name clashes
  478. SEM_UNDO = $1000;
  479. SEM_GETPID = 11;
  480. SEM_GETVAL = 12;
  481. SEM_GETALL = 13;
  482. SEM_GETNCNT = 14;
  483. SEM_GETZCNT = 15;
  484. SEM_SETVAL = 16;
  485. SEM_SETALL = 17;
  486. SEM_SEMMNI = 128;
  487. SEM_SEMMSL = 32;
  488. SEM_SEMMNS = (SEM_SEMMNI * SEM_SEMMSL);
  489. SEM_SEMOPM = 32;
  490. SEM_SEMVMX = 32767;
  491. {$else}
  492. SEM_UNDO = 1 shl 12;
  493. MAX_SOPS = 5;
  494. {$if not defined(aix) and not defined(darwin)}
  495. SEM_GETNCNT = 3; { Return the value of sempid {READ} }
  496. SEM_GETPID = 4; { Return the value of semval {READ} }
  497. SEM_GETVAL = 5; { Return semvals into arg.array {READ} }
  498. SEM_GETALL = 6; { Return the value of semzcnt {READ} }
  499. SEM_GETZCNT = 7; { Set the value of semval to arg.val {ALTER} }
  500. SEM_SETVAL = 8; { Set semvals from arg.array {ALTER} }
  501. SEM_SETALL = 9;
  502. {$endif}
  503. { Permissions }
  504. SEM_A = 2 shl 6; { alter permission }
  505. SEM_R = 4 shl 6; { read permission }
  506. {$endif}
  507. type
  508. {$ifdef Linux}
  509. {$ifndef linux_ipc32}
  510. PSEMid_ds = ^TSEMid_ds;
  511. TSEMid_ds = record
  512. sem_perm : tipc_perm;
  513. sem_otime : time_t; // kernel
  514. unused1 : culong;
  515. sem_ctime : time_t;
  516. unused2 : culong;
  517. sem_nsems : culong;
  518. unused3 : culong;
  519. unused4 : culong;
  520. end;
  521. {$else not linux_ipc32}
  522. PSEMid_ds = ^TSEMid_ds;
  523. TSEMid_ds = record
  524. sem_perm : tipc_perm;
  525. sem_otime : time_t; // kernel
  526. sem_ctime : time_t;
  527. sem_base : pointer;
  528. sem_pending : pointer;
  529. sem_pending_last : pointer;
  530. undo : pointer;
  531. sem_nsems : cushort;
  532. end;
  533. {$endif not linux_ipc32}
  534. {$else Linux}
  535. {$if defined(Darwin)}
  536. PSEM = ^TSEM;
  537. TSEM = record
  538. semval : cushort;
  539. sempid : pid_t;
  540. semncnt : cushort;
  541. semzcnt : cushort;
  542. end;
  543. {$packrecords 4}
  544. PSEMid_ds = ^TSEMid_ds;
  545. TSEMid_ds = record
  546. sem_perm : tipc_perm;
  547. sem_base : cint32;
  548. sem_nsems : cushort;
  549. sem_otime : time_t;
  550. sem_pad1 : cint32;
  551. sem_ctime : time_t;
  552. sem_pad2 : cint32;
  553. sem_pad3 : array[0..3] of cint32;
  554. end;
  555. {$packrecords c}
  556. {$elseif defined(aix)}
  557. tid_t = {$ifdef cpu64}clong{$else}cint{$endif};
  558. PSEM = ^TSEM;
  559. TSEM = record
  560. semval : cushort;
  561. flags : cushort;
  562. sempid : pid_t;
  563. semncnt : cushort;
  564. semzcnt : cushort;
  565. semnwait : tid_t;
  566. semzwait : tid_t;
  567. end;
  568. PSEMid_ds = ^TSEMid_ds;
  569. TSEMid_ds = record
  570. sem_perm: tipc_perm;
  571. {$ifdef cpu64}
  572. sem_base: cuint;
  573. {$else}
  574. sem_base: PSEM;
  575. {$endif}
  576. sem_nsems: cushort;
  577. sem_otime: time_t;
  578. sem_ctime: time_t;
  579. end;
  580. {$else}
  581. PSEM = ^TSEM;
  582. TSEM = record end; // opaque
  583. PSEMid_ds = ^TSEMid_ds;
  584. TSEMid_ds = record
  585. sem_perm : tipc_perm;
  586. sem_base : PSEM;
  587. sem_nsems : cushort;
  588. sem_otime : time_t;
  589. sem_pad1 : cint;
  590. sem_ctime : time_t;
  591. sem_pad2 : cint;
  592. sem_pad3 : array[0..3] of cint;
  593. end;
  594. {$endif}
  595. {$endif}
  596. PSEMbuf = ^TSEMbuf;
  597. TSEMbuf = record
  598. sem_num : cushort;
  599. sem_op : cshort;
  600. sem_flg : cshort;
  601. end;
  602. PSEMinfo = ^TSEMinfo;
  603. {$ifndef aix}
  604. TSEMinfo = record
  605. semmap : cint;
  606. semmni : cint;
  607. semmns : cint;
  608. semmnu : cint;
  609. semmsl : cint;
  610. semopm : cint;
  611. semume : cint;
  612. semusz : cint;
  613. semvmx : cint;
  614. semaem : cint;
  615. end;
  616. {$else not aix}
  617. TSEMinfo = record
  618. semmni : cint;
  619. semmsl : cint;
  620. semopm : cint;
  621. semume : cint;
  622. semusz : cint;
  623. semvmx : cint;
  624. semaem : cint;
  625. end;
  626. {$endif not aix}
  627. { internal mode bits}
  628. {$ifdef FreeBSD}
  629. Const
  630. SEM_ALLOC = 1 shl 9;
  631. SEM_DEST = 2 shl 9;
  632. {$endif}
  633. Type
  634. PSEMun = ^TSEMun;
  635. TSEMun = record
  636. case cint of
  637. 0 : ( val : cint );
  638. 1 : ( buf : PSEMid_ds );
  639. 2 : ( arr : PWord ); // ^ushort
  640. {$ifdef linux}
  641. 3 : ( padbuf : PSeminfo );
  642. 4 : ( padpad : pointer );
  643. {$endif}
  644. end;
  645. Function semget(key:Tkey; nsems:cint; semflg:cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'semget'; {$endif}
  646. Function semop(semid:cint; sops: psembuf; nsops: cuint): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'semop'; {$endif}
  647. Function semctl(semid:cint; semnum:cint; cmd:cint; var arg: tsemun): cint;
  648. {$ifdef linux}
  649. Function semtimedop(semid:cint; sops: psembuf; nsops: cuint; timeOut: ptimespec): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'semtimedop'; platform; {$else} platform; {$endif}
  650. {$endif}
  651. {$ifdef aix}
  652. { only available as of AIX 6.1 }
  653. Function semtimedop(semid:cint; sops: psembuf; nsops: size_t; timeOut: ptimespec): cint; {$ifdef FPC_USE_LIBC} cdecl; weakexternal name 'semtimedop'; platform; {$else} platform; {$endif}
  654. {$endif}
  655. implementation
  656. {$ifndef FPC_USE_LIBC}
  657. uses Syscall;
  658. {$endif ndef FPC_USE_LIBC}
  659. {$ifndef FPC_USE_LIBC}
  660. {$ifdef Linux}
  661. {$if defined(cpux86_64) or defined(NO_SYSCALL_IPC)}
  662. {$i ipcsys.inc}
  663. {$else}
  664. {$i ipccall.inc}
  665. {$endif}
  666. {$endif}
  667. {$ifdef BSD}
  668. {$i ipcbsd.inc}
  669. {$endif}
  670. {$else ndef FPC_USE_LIBC}
  671. Function real_semctl(semid:cint; semnum:cint; cmd:cint): cint; {$ifdef FPC_USE_LIBC} cdecl; varargs; external clib name 'semctl'; {$endif}
  672. Function semctl(semid:cint; semnum:cint; cmd:cint; var arg: tsemun): cint;
  673. begin
  674. semctl := real_semctl(semid,semnum,cmd,pointer(arg));
  675. end;
  676. {$endif}
  677. end.