ipc.pp 9.0 KB

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