ipc.pp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by the Free Pascal development team
  5. This file implements IPC calls calls for Linux
  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. { ----------------------------------------------------------------------
  15. General IPC stuff
  16. ----------------------------------------------------------------------}
  17. Var
  18. IPCError : longint;
  19. Type
  20. TKey = Longint;
  21. PULong = ^Cardinal;
  22. Const
  23. { IPC flags for get calls }
  24. IPC_CREAT = 1 shl 9; { create if key is nonexistent }
  25. IPC_EXCL = 2 shl 9; { fail if key exists }
  26. IPC_NOWAIT = 4 shl 9; { return error on wait }
  27. { Actions for ctl calls }
  28. IPC_RMID = 0; { remove resource }
  29. IPC_SET = 1; { set ipc_perm options }
  30. IPC_STAT = 2; { get ipc_perm options }
  31. IPC_INFO = 3; { see ipcs }
  32. type
  33. PIPC_Perm = ^TIPC_Perm;
  34. TIPC_Perm = record
  35. key : TKey;
  36. uid,
  37. gid,
  38. cuid,
  39. cgid,
  40. mode,
  41. seq : Word;
  42. end;
  43. { Function to generate a IPC key. }
  44. Function ftok (Path : String; ID : char) : TKey;
  45. { ----------------------------------------------------------------------
  46. Shared memory stuff
  47. ----------------------------------------------------------------------}
  48. Type
  49. PShmid_DS = ^TShmid_ds;
  50. TShmid_ds = record
  51. shm_perm : TIPC_Perm;
  52. shm_segsz : longint;
  53. shm_atime : longint;
  54. shm_dtime : longint;
  55. shm_ctime : longint;
  56. shm_cpid : word;
  57. shm_lpid : word;
  58. shm_nattch : integer;
  59. shm_npages : word;
  60. shm_pages : Pointer;
  61. attaches : pointer;
  62. end;
  63. const
  64. SHM_R = 4 shl 6;
  65. SHM_W = 2 shl 6;
  66. SHM_RDONLY = 1 shl 12;
  67. SHM_RND = 2 shl 12;
  68. SHM_REMAP = 4 shl 12;
  69. SHM_LOCK = 11;
  70. SHM_UNLOCK = 12;
  71. type
  72. PSHMinfo = ^TSHMinfo;
  73. TSHMinfo = record
  74. shmmax : longint;
  75. shmmin : longint;
  76. shmmni : longint;
  77. shmseg : longint;
  78. shmall : longint;
  79. end;
  80. Function shmget(key: Tkey; size:longint; flag:longint):longint;
  81. Function shmat (shmid:longint; shmaddr:pchar; shmflg:longint):pchar;
  82. Function shmdt (shmaddr:pchar):boolean;
  83. Function shmctl(shmid:longint; cmd:longint; buf: pshmid_ds): Boolean;
  84. { ----------------------------------------------------------------------
  85. Message queue stuff
  86. ----------------------------------------------------------------------}
  87. const
  88. MSG_NOERROR = 1 shl 12;
  89. MSG_EXCEPT = 2 shl 12;
  90. MSGMNI = 128;
  91. MSGMAX = 4056;
  92. MSGMNB = 16384;
  93. type
  94. PMSG = ^TMSG;
  95. TMSG = record
  96. msg_next : PMSG;
  97. msg_type : Longint;
  98. msg_spot : PChar;
  99. msg_stime : Longint;
  100. msg_ts : Integer;
  101. end;
  102. type
  103. PMSQid_ds = ^TMSQid_ds;
  104. TMSQid_ds = record
  105. msg_perm : TIPC_perm;
  106. msg_first : PMsg;
  107. msg_last : PMsg;
  108. msg_stime : Longint;
  109. msg_rtime : Longint;
  110. msg_ctime : Longint;
  111. wwait : Pointer;
  112. rwait : pointer;
  113. msg_cbytes : word;
  114. msg_qnum : word;
  115. msg_qbytes : word;
  116. msg_lspid : word;
  117. msg_lrpid : word;
  118. end;
  119. PMSGbuf = ^TMSGbuf;
  120. TMSGbuf = record
  121. mtype : longint;
  122. mtext : array[0..0] of char;
  123. end;
  124. PMSGinfo = ^TMSGinfo;
  125. TMSGinfo = record
  126. msgpool : Longint;
  127. msgmap : Longint;
  128. msgmax : Longint;
  129. msgmnb : Longint;
  130. msgmni : Longint;
  131. msgssz : Longint;
  132. msgtql : Longint;
  133. msgseg : Word;
  134. end;
  135. Function msgget(key: TKey; msgflg:longint):longint;
  136. Function msgsnd(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgflg:longint): Boolean;
  137. Function msgrcv(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgtyp:longint; msgflg:longint): Boolean;
  138. Function msgctl(msqid:longint; cmd: longint; buf: PMSQid_ds): Boolean;
  139. { ----------------------------------------------------------------------
  140. Semaphores stuff
  141. ----------------------------------------------------------------------}
  142. const
  143. SEM_UNDO = $1000;
  144. GETPID = 11;
  145. GETVAL = 12;
  146. GETALL = 13;
  147. GETNCNT = 14;
  148. GETZCNT = 15;
  149. SETVAL = 16;
  150. SETALL = 17;
  151. SEMMNI = 128;
  152. SEMMSL = 32;
  153. SEMMNS = (SEMMNI * SEMMSL);
  154. SEMOPM = 32;
  155. SEMVMX = 32767;
  156. type
  157. PSEMid_ds = ^PSEMid_ds;
  158. TSEMid_ds = record
  159. sem_perm : tipc_perm;
  160. sem_otime : longint;
  161. sem_ctime : longint;
  162. sem_base : pointer;
  163. sem_pending : pointer;
  164. sem_pending_last : pointer;
  165. undo : pointer;
  166. sem_nsems : word;
  167. end;
  168. PSEMbuf = ^TSEMbuf;
  169. TSEMbuf = record
  170. sem_num : word;
  171. sem_op : integer;
  172. sem_flg : integer;
  173. end;
  174. PSEMinfo = ^TSEMinfo;
  175. TSEMinfo = record
  176. semmap : longint;
  177. semmni : longint;
  178. semmns : longint;
  179. semmnu : longint;
  180. semmsl : longint;
  181. semopm : longint;
  182. semume : longint;
  183. semusz : longint;
  184. semvmx : longint;
  185. semaem : longint;
  186. end;
  187. PSEMun = ^TSEMun;
  188. TSEMun = record
  189. case longint of
  190. 0 : ( val : longint );
  191. 1 : ( buf : PSEMid_ds );
  192. 2 : ( arr : Pointer );
  193. 3 : ( padbuf : PSeminfo );
  194. 4 : ( padpad : pointer );
  195. end;
  196. Function semget(key:Tkey; nsems:longint; semflg:longint): longint;
  197. Function semop(semid:longint; sops: pointer; nsops: cardinal): Boolean;
  198. Function semctl(semid:longint; semnum:longint; cmd:longint; arg: tsemun): longint;
  199. implementation
  200. uses Linux;
  201. { The following definitions come from linux/ipc.h }
  202. Const
  203. CALL_SEMOP = 1;
  204. CALL_SEMGET = 2;
  205. CALL_SEMCTL = 3;
  206. CALL_MSGSND = 11;
  207. CALL_MSGRCV = 12;
  208. CALL_MSGGET = 13;
  209. CALL_MSGCTL = 14;
  210. CALL_SHMAT = 21;
  211. CALL_SHMDT = 22;
  212. CALL_SHMGET = 23;
  213. CALL_SHMCTL = 24;
  214. { generic call that handles all IPC calls }
  215. function ipccall(Call,First,Second,Third : Longint; P : Pointer) : longint;
  216. Var SR : SysCallRegs;
  217. begin
  218. SR.Reg2:=Call;
  219. SR.reg3:=first;
  220. SR.reg4:=second;
  221. SR.Reg5:=third;
  222. SR.Reg6:=Longint(P);
  223. ipccall:=syscall(syscall_nr_ipc,sr);
  224. ipcerror:=Errno;
  225. end;
  226. Function ftok (Path : String; ID : char) : TKey;
  227. Var Info : Stat;
  228. begin
  229. If not fstat(path,info) then
  230. ftok:=-1
  231. else
  232. begin
  233. ftok:= (info.ino and $FFFF) or ((info.dev and $ff) shl 16) or (byte(ID) shl 24)
  234. end;
  235. end;
  236. function shmget(key: Tkey; size:longint; flag:longint):longint;
  237. begin
  238. shmget:=ipccall (CALL_SHMGET,key,size,flag,nil);
  239. end;
  240. function shmat (shmid:longint; shmaddr:pchar; shmflg:longint): pchar;
  241. Var raddr : pchar;
  242. error : longint;
  243. begin
  244. error:=ipccall(CALL_SHMAT,shmid,shmflg,longint(@raddr),shmaddr);
  245. If Error<0 then
  246. shmat:=pchar(error)
  247. else
  248. shmat:=raddr;
  249. end;
  250. function shmdt (shmaddr:pchar): boolean;
  251. begin
  252. shmdt:=ipccall(CALL_SHMDT,0,0,0,shmaddr)<>-1;
  253. end;
  254. function shmctl(shmid:longint; cmd:longint; buf: pshmid_ds): Boolean;
  255. begin
  256. shmctl:=ipccall(CALL_SHMCTL,shmid,cmd,0,buf)=0;
  257. end;
  258. Function msgget(key:Tkey; msgflg:longint):longint;
  259. begin
  260. msgget:=ipccall(CALL_MSGGET,key,msgflg,0,Nil);
  261. end;
  262. Function msgsnd(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgflg:longint):Boolean;
  263. begin
  264. msgsnd:=ipccall(Call_MSGSND,msqid,msgsz,msgflg,msgp)=0;
  265. end;
  266. Function msgrcv(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgtyp:longint; msgflg:longint):Boolean;
  267. Type
  268. TIPC_Kludge = Record
  269. msgp : pmsgbuf;
  270. msgtyp : longint;
  271. end;
  272. Var
  273. tmp : TIPC_Kludge;
  274. begin
  275. tmp.msgp := msgp;
  276. tmp.msgtyp := msgtyp;
  277. msgrcv:=ipccall(CALL_MSGRCV,msqid,msgsz,msgflg,@tmp)>=0;
  278. end;
  279. Function msgctl(msqid:longint; cmd: longint; buf: PMSQid_ds): Boolean;
  280. begin
  281. msgctl:=ipccall(CALL_MSGCTL,msqid,cmd,0,buf)=0;
  282. end;
  283. Function semget(key:Tkey; nsems:longint; semflg:longint): longint;
  284. begin
  285. semget:=ipccall (CALL_SEMGET,key,nsems,semflg,Nil);
  286. end;
  287. Function semop(semid:longint; sops: pointer; nsops:cardinal): Boolean;
  288. begin
  289. semop:=ipccall (CALL_SEMOP,semid,Longint(nsops),0,Pointer(sops))=0;
  290. end;
  291. Function semctl(semid:longint; semnum:longint; cmd:longint; arg: tsemun): longint;
  292. begin
  293. semctl:=ipccall(CALL_SEMCTL,semid,semnum,cmd,@arg);
  294. end;
  295. end.