ipc.pp 8.1 KB

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