ipc.pp 9.9 KB

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