ipc.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. { ----------------------------------------------------------------------
  19. General IPC stuff
  20. ----------------------------------------------------------------------}
  21. //Var
  22. // IPCError : longint;
  23. Type
  24. {$IFDEF FreeBSD}
  25. TKey = clong;
  26. {$ELSE}
  27. TKey = cint;
  28. {$ENDIF}
  29. key_t = TKey;
  30. Const
  31. { IPC flags for get calls }
  32. {$if defined(FreeBSD) or defined(NetBSD)} // BSD_VISIBLE
  33. IPC_R = 4 shl 6;
  34. IPC_W = 2 shl 6;
  35. IPC_M = 2 shl 12;
  36. {$endif}
  37. {$ifdef Darwin}
  38. IPC_R = 4 shl 6;
  39. IPC_W = 2 shl 6;
  40. IPC_M = 1 shl 12;
  41. {$endif}
  42. IPC_CREAT = 1 shl 9; { create if key is nonexistent }
  43. IPC_EXCL = 2 shl 9; { fail if key exists }
  44. IPC_NOWAIT = 4 shl 9; { return error on wait }
  45. {$if defined(FreeBSD) or defined(Darwin) or defined(Linux)}
  46. IPC_PRIVATE : TKey = 0;
  47. {$endif}
  48. { Actions for ctl calls }
  49. IPC_RMID = 0; { remove resource }
  50. IPC_SET = 1; { set ipc_perm options }
  51. IPC_STAT = 2; { get ipc_perm options }
  52. {$ifndef Darwin}
  53. IPC_INFO = 3; { see ipcs }
  54. {$endif}
  55. type
  56. PIPC_Perm = ^TIPC_Perm;
  57. {$if defined(FreeBSD) or defined(Darwin)}
  58. TIPC_Perm = record
  59. cuid : cushort; { creator user id }
  60. cgid : cushort; { creator group id }
  61. uid : cushort; { user id }
  62. gid : cushort; { group id }
  63. mode : cushort; { r/w permission }
  64. seq : cushort; { sequence # (to generate unique msg/sem/shm id) }
  65. key : key_t; { user specified msg/sem/shm key }
  66. End;
  67. {$else} // linux
  68. {$ifdef cpux86_64}
  69. TIPC_Perm = record
  70. key : TKey;
  71. uid : uid_t;
  72. gid : gid_t;
  73. cuid : uid_t;
  74. cgid : gid_t;
  75. mode : mode_t;
  76. __pad1 : cushort;
  77. seq : cushort;
  78. __pad2 : cushort;
  79. __unused1 : culong;
  80. __unused2 : culong;
  81. End;
  82. {$else cpux86_64}
  83. TIPC_Perm = record
  84. key : TKey;
  85. uid : uid_t;
  86. gid : gid_t;
  87. cuid : uid_t;
  88. cgid : gid_t;
  89. mode : mode_t;
  90. seq : cushort;
  91. End;
  92. {$endif cpux86_64}
  93. {$endif}
  94. { Function to generate a IPC key. }
  95. Function ftok (Path : pchar; ID : cint) : TKey; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'ftok'; {$endif}
  96. { ----------------------------------------------------------------------
  97. Sys V Shared memory stuff
  98. ----------------------------------------------------------------------}
  99. Type
  100. PShmid_DS = ^TShmid_ds;
  101. {$ifdef FreeBSD}
  102. TShmid_ds = record
  103. shm_perm : TIPC_Perm;
  104. shm_segsz : cint;
  105. shm_lpid : pid_t;
  106. shm_cpid : pid_t;
  107. shm_nattch : cshort;
  108. shm_atime : time_t;
  109. shm_dtime : time_t;
  110. shm_ctime : time_t;
  111. shm_internal : pointer;
  112. end;
  113. {$endif}
  114. {$ifdef Darwin}
  115. TShmid_ds = record
  116. shm_perm : TIPC_Perm;
  117. shm_segsz : size_t;
  118. shm_lpid : pid_t;
  119. shm_cpid : pid_t;
  120. shm_nattch : cushort; // typedef unsigned short shmatt_t
  121. shm_atime : time_t;
  122. shm_dtime : time_t;
  123. shm_ctime : time_t;
  124. shm_internal : pointer;
  125. end;
  126. {$endif}
  127. {$ifdef Linux}
  128. {$ifdef cpux86_64}
  129. TShmid_ds = record
  130. shm_perm : TIPC_Perm;
  131. shm_segsz : size_t;
  132. shm_atime : time_t;
  133. shm_dtime : time_t;
  134. shm_ctime : time_t;
  135. shm_cpid : pid_t;
  136. shm_lpid : pid_t;
  137. shm_nattch : culong;
  138. __unused4 : culong;
  139. __unused5 : culong;
  140. end;
  141. {$else cpux86_64}
  142. TShmid_ds = record
  143. shm_perm : TIPC_Perm;
  144. shm_segsz : cint;
  145. shm_atime : time_t;
  146. shm_dtime : time_t;
  147. shm_ctime : time_t;
  148. shm_cpid : ipc_pid_t;
  149. shm_lpid : ipc_pid_t;
  150. shm_nattch : word;
  151. shm_npages : word;
  152. shm_pages : pointer;
  153. attaches : pointer;
  154. end;
  155. {$endif cpux86_64}
  156. {$endif}
  157. const
  158. {$ifdef Linux}
  159. SHM_R = 4 shl 6;
  160. SHM_W = 2 shl 6;
  161. {$else}
  162. SHM_R = IPC_R;
  163. SHM_W = IPC_W;
  164. {$endif}
  165. SHM_RDONLY = 1 shl 12;
  166. SHM_RND = 2 shl 12;
  167. {$ifdef Linux}
  168. SHM_REMAP = 4 shl 12;
  169. {$endif}
  170. {$ifdef Darwin}
  171. SHMLBA = 4096;
  172. {$endif}
  173. SHM_LOCK = 11;
  174. SHM_UNLOCK = 12;
  175. {$ifdef FreeBSD} // ipcs shmctl commands
  176. SHM_STAT = 13;
  177. SHM_INFO = 14;
  178. {$endif}
  179. type // the shm*info kind is "kernel" only.
  180. PSHMinfo = ^TSHMinfo;
  181. TSHMinfo = record // comment under FreeBSD/Darwin: do we really need this?
  182. shmmax : cint;
  183. shmmin : cint;
  184. shmmni : cint;
  185. shmseg : cint;
  186. shmall : cint;
  187. end;
  188. {$if defined(FreeBSD) or defined(Linux)}
  189. PSHM_info = ^TSHM_info;
  190. TSHM_info = record
  191. used_ids : cint;
  192. shm_tot,
  193. shm_rss,
  194. shm_swp,
  195. swap_attempts,
  196. swap_successes : culong;
  197. end;
  198. {$endif}
  199. Function shmget(key: Tkey; size:size_t; flag:cint):cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmget'; {$endif}
  200. Function shmat (shmid:cint; shmaddr:pointer; shmflg:cint):pointer; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmat'; {$endif}
  201. Function shmdt (shmaddr:pointer):cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmdt'; {$endif}
  202. Function shmctl(shmid:cint; cmd:cint; buf: pshmid_ds): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'shmctl'; {$endif}
  203. { ----------------------------------------------------------------------
  204. Message queue stuff
  205. ----------------------------------------------------------------------}
  206. const
  207. MSG_NOERROR = 1 shl 12;
  208. {$ifdef Linux}
  209. MSG_EXCEPT = 2 shl 12;
  210. MSGMNI = 128;
  211. MSGMAX = 4056;
  212. MSGMNB = 16384;
  213. {$endif}
  214. type
  215. msglen_t = culong;
  216. msgqnum_t= culong;
  217. {$ifdef Darwin}
  218. user_msglen_t = culonglong;
  219. user_msgqnum_t= culonglong;
  220. {$endif}
  221. PMSG = ^TMSG;
  222. TMSG = record
  223. {$ifndef FreeBSD} // opague in FreeBSD
  224. {$ifdef Darwin}
  225. msg_next : PMSG;
  226. msg_type : clong;
  227. msg_ts : cushort;
  228. mac_label : pointer;
  229. {$else}
  230. msg_next : PMSG;
  231. msg_type : Longint;
  232. msg_spot : PChar;
  233. msg_stime : Longint;
  234. msg_ts : Integer;
  235. {$endif}
  236. {$endif}
  237. end;
  238. type
  239. {$ifdef Linux}
  240. PMSQid_ds = ^TMSQid_ds;
  241. TMSQid_ds = record
  242. msg_perm : TIPC_perm;
  243. msg_first : PMsg;
  244. msg_last : PMsg;
  245. msg_stime : time_t;
  246. msg_rtime : time_t;
  247. msg_ctime : time_t;
  248. msg_cbytes : word;
  249. msg_qnum : word;
  250. msg_qbytes : word;
  251. msg_lspid : ipc_pid_t;
  252. msg_lrpid : ipc_pid_t;
  253. end;
  254. {$else}
  255. {$ifdef Darwin}
  256. PMSQid_ds = ^TMSQid_ds;
  257. TMSQid_ds = record
  258. msg_perm : TIPC_perm;
  259. msg_first : cint32;
  260. msg_last : cint32;
  261. msg_cbytes : msglen_t;
  262. msg_qnum : msgqnum_t;
  263. msg_qbytes : msglen_t;
  264. msg_lspid : pid_t;
  265. msg_lrpid : pid_t;
  266. msg_stime : time_t;
  267. msg_pad1 : cint32;
  268. msg_rtime : time_t;
  269. msg_pad2 : cint32;
  270. msg_ctime : time_t;
  271. msg_pad3 : cint32;
  272. msg_pad4 : array [0..3] of cint32;
  273. end;
  274. {$else}
  275. PMSQid_ds = ^TMSQid_ds;
  276. TMSQid_ds = record
  277. msg_perm : TIPC_perm;
  278. msg_first : PMsg;
  279. msg_last : PMsg;
  280. msg_cbytes : msglen_t;
  281. msg_qnum : msgqnum_t;
  282. msg_qbytes : msglen_t;
  283. msg_lspid : pid_t;
  284. msg_lrpid : pid_t;
  285. msg_stime : time_t;
  286. msg_pad1 : clong;
  287. msg_rtime : time_t;
  288. msg_pad2 : clong;
  289. msg_ctime : time_t;
  290. msg_pad3 : clong;
  291. msg_pad4 : array [0..3] of clong;
  292. end;
  293. {$endif}
  294. {$endif}
  295. PMSGbuf = ^TMSGbuf;
  296. TMSGbuf = record // called mymsg on freebsd and SVID manual
  297. mtype : clong;
  298. mtext : array[0..0] of char;
  299. end;
  300. {$ifdef linux}
  301. PMSGinfo = ^TMSGinfo;
  302. TMSGinfo = record
  303. msgpool : cint;
  304. msgmap : cint;
  305. msgmax : cint;
  306. msgmnb : cint;
  307. msgmni : cint;
  308. msgssz : cint;
  309. msgtql : cint;
  310. msgseg : cushort;
  311. end;
  312. {$else}
  313. PMSGinfo = ^TMSGinfo;
  314. TMSGinfo = record
  315. msgmax,
  316. msgmni,
  317. msgmnb,
  318. msgtql,
  319. msgssz,
  320. msgseg : cint;
  321. end;
  322. {$endif}
  323. Function msgget(key: TKey; msgflg:cint):cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgget'; {$endif}
  324. Function msgsnd(msqid:cint; msgp: PMSGBuf; msgsz: size_t; msgflg:cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgsnd'; {$endif}
  325. Function msgrcv(msqid:cint; msgp: PMSGBuf; msgsz: size_t; msgtyp:cint; msgflg:cint): {$ifdef Darwin}ssize_t;{$else}cint;{$endif} {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgrcv'; {$endif}
  326. Function msgctl(msqid:cint; cmd: cint; buf: PMSQid_ds): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'msgctl'; {$endif}
  327. { ----------------------------------------------------------------------
  328. Semaphores stuff
  329. ----------------------------------------------------------------------}
  330. const
  331. {$ifdef Linux} // renamed to many name clashes
  332. SEM_UNDO = $1000;
  333. SEM_GETPID = 11;
  334. SEM_GETVAL = 12;
  335. SEM_GETALL = 13;
  336. SEM_GETNCNT = 14;
  337. SEM_GETZCNT = 15;
  338. SEM_SETVAL = 16;
  339. SEM_SETALL = 17;
  340. SEM_SEMMNI = 128;
  341. SEM_SEMMSL = 32;
  342. SEM_SEMMNS = (SEM_SEMMNI * SEM_SEMMSL);
  343. SEM_SEMOPM = 32;
  344. SEM_SEMVMX = 32767;
  345. {$else}
  346. SEM_UNDO = 1 shl 12;
  347. MAX_SOPS = 5;
  348. SEM_GETNCNT = 3; { Return the value of sempid {READ} }
  349. SEM_GETPID = 4; { Return the value of semval {READ} }
  350. SEM_GETVAL = 5; { Return semvals into arg.array {READ} }
  351. SEM_GETALL = 6; { Return the value of semzcnt {READ} }
  352. SEM_GETZCNT = 7; { Set the value of semval to arg.val {ALTER} }
  353. SEM_SETVAL = 8; { Set semvals from arg.array {ALTER} }
  354. SEM_SETALL = 9;
  355. { Permissions }
  356. SEM_A = 2 shl 6; { alter permission }
  357. SEM_R = 4 shl 6; { read permission }
  358. {$endif}
  359. type
  360. {$ifdef Linux}
  361. PSEMid_ds = ^TSEMid_ds;
  362. TSEMid_ds = record
  363. sem_perm : tipc_perm;
  364. sem_otime : time_t;
  365. sem_ctime : time_t;
  366. sem_base : pointer;
  367. sem_pending : pointer;
  368. sem_pending_last : pointer;
  369. undo : pointer;
  370. sem_nsems : cushort;
  371. end;
  372. {$else}
  373. {$ifdef Darwin}
  374. PSEM = ^TSEM;
  375. TSEM = record
  376. semval : cushort;
  377. sempid : pid_t;
  378. semncnt : cushort;
  379. semzcnt : cushort;
  380. end;
  381. PSEMid_ds = ^TSEMid_ds;
  382. TSEMid_ds = record
  383. sem_perm : tipc_perm;
  384. sem_base : PSEM;
  385. sem_nsems : cushort;
  386. sem_otime : time_t;
  387. sem_pad1 : cint32;
  388. sem_ctime : time_t;
  389. sem_pad2 : cint32;
  390. sem_pad3 : array[0..3] of cint32;
  391. end;
  392. {$else}
  393. PSEM = ^TSEM;
  394. TSEM = record end; // opague
  395. PSEMid_ds = ^TSEMid_ds;
  396. TSEMid_ds = record
  397. sem_perm : tipc_perm;
  398. sem_base : PSEM;
  399. sem_nsems : cushort;
  400. sem_otime : time_t;
  401. sem_pad1 : cint;
  402. sem_ctime : time_t;
  403. sem_pad2 : cint;
  404. sem_pad3 : array[0..3] of cint;
  405. end;
  406. {$endif}
  407. {$endif}
  408. PSEMbuf = ^TSEMbuf;
  409. TSEMbuf = record
  410. sem_num : cushort;
  411. sem_op : cshort;
  412. sem_flg : cshort;
  413. end;
  414. PSEMinfo = ^TSEMinfo;
  415. TSEMinfo = record
  416. semmap : cint;
  417. semmni : cint;
  418. semmns : cint;
  419. semmnu : cint;
  420. semmsl : cint;
  421. semopm : cint;
  422. semume : cint;
  423. semusz : cint;
  424. semvmx : cint;
  425. semaem : cint;
  426. end;
  427. { internal mode bits}
  428. {$ifdef FreeBSD}
  429. Const
  430. SEM_ALLOC = 1 shl 9;
  431. SEM_DEST = 2 shl 9;
  432. {$endif}
  433. Type
  434. PSEMun = ^TSEMun;
  435. TSEMun = record
  436. case cint of
  437. 0 : ( val : cint );
  438. 1 : ( buf : PSEMid_ds );
  439. 2 : ( arr : PWord ); // ^ushort
  440. {$ifdef linux}
  441. 3 : ( padbuf : PSeminfo );
  442. 4 : ( padpad : pointer );
  443. {$endif}
  444. end;
  445. Function semget(key:Tkey; nsems:cint; semflg:cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'semget'; {$endif}
  446. Function semop(semid:cint; sops: psembuf; nsops: cuint): cint; {$ifdef FPC_USE_LIBC} cdecl; external clib name 'semop'; {$endif}
  447. Function semctl(semid:cint; semnum:cint; cmd:cint; var arg: tsemun): cint;
  448. implementation
  449. {$ifndef FPC_USE_LIBC}
  450. uses Syscall;
  451. {$endif ndef FPC_USE_LIBC}
  452. {$ifndef FPC_USE_LIBC}
  453. {$ifdef Linux}
  454. {$ifdef cpux86_64}
  455. {$i ipcsys.inc}
  456. {$else}
  457. {$i ipccall.inc}
  458. {$endif}
  459. {$endif}
  460. {$ifdef BSD}
  461. {$i ipcbsd.inc}
  462. {$endif}
  463. {$else ndef FPC_USE_LIBC}
  464. Function real_semctl(semid:cint; semnum:cint; cmd:cint): cint; {$ifdef FPC_USE_LIBC} cdecl; varargs; external clib name 'semctl'; {$endif}
  465. Function semctl(semid:cint; semnum:cint; cmd:cint; var arg: tsemun): cint;
  466. begin
  467. semctl := real_semctl(semid,semnum,cmd,pointer(@arg));
  468. end;
  469. {$endif}
  470. end.