ipc.pp 10 KB

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