ipc.pp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. implementation
  140. uses Linux;
  141. { The following definitions come from linux/ipc.h }
  142. Const
  143. CALL_SEMOP = 1;
  144. CALL_SEMGET = 2;
  145. CALL_SEMCTL = 3;
  146. CALL_MSGSND = 11;
  147. CALL_MSGRCV = 12;
  148. CALL_MSGGET = 13;
  149. CALL_MSGCTL = 14;
  150. CALL_SHMAT = 21;
  151. CALL_SHMDT = 22;
  152. CALL_SHMGET = 23;
  153. CALL_SHMCTL = 24;
  154. { generic call that handles all IPC calls }
  155. function ipccall(Call,First,Second,Third : Longint; P : Pointer) : longint;
  156. Var SR : SysCallRegs;
  157. begin
  158. SR.Reg2:=Call;
  159. SR.reg3:=first;
  160. SR.reg4:=second;
  161. SR.Reg5:=third;
  162. SR.Reg6:=Longint(P);
  163. ipccall:=syscall(syscall_nr_ipc,sr);
  164. ipcerror:=Errno;
  165. end;
  166. Function ftok (Path : String; ID : char) : TKey;
  167. Var Info : Stat;
  168. begin
  169. If not fstat(path,info) then
  170. ftok:=-1
  171. else
  172. begin
  173. ftok:= (info.ino and $FFFF) or ((info.dev and $ff) shl 16) or (byte(ID) shl 24)
  174. end;
  175. end;
  176. function shmget(key: Tkey; size:longint; flag:longint):longint;
  177. begin
  178. shmget:=ipccall (CALL_SHMGET,key,size,flag,nil);
  179. end;
  180. function shmat (shmid:longint; shmaddr:pchar; shmflg:longint): pchar;
  181. Var raddr : pchar;
  182. error : longint;
  183. begin
  184. error:=ipccall(CALL_SHMAT,shmid,shmflg,longint(@raddr),shmaddr);
  185. If Error<0 then
  186. shmat:=pchar(error)
  187. else
  188. shmat:=raddr;
  189. end;
  190. function shmdt (shmaddr:pchar): boolean;
  191. begin
  192. shmdt:=ipccall(CALL_SHMDT,0,0,0,shmaddr)<>-1;
  193. end;
  194. function shmctl(shmid:longint; cmd:longint; buf: pshmid_ds): Boolean;
  195. begin
  196. shmctl:=ipccall(CALL_SHMCTL,shmid,cmd,0,buf)=0;
  197. end;
  198. Function msgget(key:Tkey; msgflg:longint):longint;
  199. begin
  200. msgget:=ipccall(CALL_MSGGET,key,msgflg,0,Nil);
  201. end;
  202. Function msgsnd(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgflg:longint):Boolean;
  203. begin
  204. msgsnd:=ipccall(Call_MSGSND,msqid,msgsz,msgflg,msgp)=0;
  205. end;
  206. Function msgrcv(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgtyp:longint; msgflg:longint):Boolean;
  207. Type
  208. TIPC_Kludge = Record
  209. msgp : pmsgbuf;
  210. msgtyp : longint;
  211. end;
  212. Var
  213. tmp : TIPC_Kludge;
  214. begin
  215. tmp.msgp := msgp;
  216. tmp.msgtyp := msgtyp;
  217. msgrcv:=ipccall(CALL_MSGRCV,msqid,msgsz,msgflg,@tmp)>=0;
  218. end;
  219. Function msgctl(msqid:longint; cmd: longint; buf: PMSQid_ds): Boolean;
  220. begin
  221. msgctl:=ipccall(CALL_MSGCTL,msqid,cmd,0,buf)=0;
  222. end;
  223. end.