ipc.pp 9.2 KB

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