ipc.pp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. ipc_GETPID = 11;
  146. ipc_GETVAL = 12;
  147. ipc_GETALL = 13;
  148. ipc_GETNCNT = 14;
  149. ipc_GETZCNT = 15;
  150. ipc_SETVAL = 16;
  151. ipc_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 BaseUnix,Syscall;
  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. begin
  218. {$IFNDEF bsd}
  219. ipccall:=do_syscall(syscall_nr_ipc,call,first,second,third,longint(P));
  220. {$Endif}
  221. ipcerror:=fpgetErrno;
  222. end;
  223. Function ftok (Path : String; ID : char) : TKey;
  224. Var Info : TStat;
  225. begin
  226. If fpstat(path,info)<0 then
  227. ftok:=-1
  228. else
  229. begin
  230. ftok:= (info.st_ino and $FFFF) or ((info.st_dev and $ff) shl 16) or (byte(ID) shl 24)
  231. end;
  232. end;
  233. function shmget(key: Tkey; size:longint; flag:longint):longint;
  234. begin
  235. shmget:=ipccall (CALL_SHMGET,key,size,flag,nil);
  236. end;
  237. function shmat (shmid:longint; shmaddr:pchar; shmflg:longint): pchar;
  238. Var raddr : pchar;
  239. error : longint;
  240. begin
  241. error:=ipccall(CALL_SHMAT,shmid,shmflg,longint(@raddr),shmaddr);
  242. If Error<0 then
  243. shmat:=pchar(error)
  244. else
  245. shmat:=raddr;
  246. end;
  247. function shmdt (shmaddr:pchar): boolean;
  248. begin
  249. shmdt:=ipccall(CALL_SHMDT,0,0,0,shmaddr)<>-1;
  250. end;
  251. function shmctl(shmid:longint; cmd:longint; buf: pshmid_ds): Boolean;
  252. begin
  253. shmctl:=ipccall(CALL_SHMCTL,shmid,cmd,0,buf)=0;
  254. end;
  255. Function msgget(key:Tkey; msgflg:longint):longint;
  256. begin
  257. msgget:=ipccall(CALL_MSGGET,key,msgflg,0,Nil);
  258. end;
  259. Function msgsnd(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgflg:longint):Boolean;
  260. begin
  261. msgsnd:=ipccall(Call_MSGSND,msqid,msgsz,msgflg,msgp)=0;
  262. end;
  263. Function msgrcv(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgtyp:longint; msgflg:longint):Boolean;
  264. Type
  265. TIPC_Kludge = Record
  266. msgp : pmsgbuf;
  267. msgtyp : longint;
  268. end;
  269. Var
  270. tmp : TIPC_Kludge;
  271. begin
  272. tmp.msgp := msgp;
  273. tmp.msgtyp := msgtyp;
  274. msgrcv:=ipccall(CALL_MSGRCV,msqid,msgsz,msgflg,@tmp)>=0;
  275. end;
  276. Function msgctl(msqid:longint; cmd: longint; buf: PMSQid_ds): Boolean;
  277. begin
  278. msgctl:=ipccall(CALL_MSGCTL,msqid,cmd,0,buf)=0;
  279. end;
  280. Function semget(key:Tkey; nsems:longint; semflg:longint): longint;
  281. begin
  282. semget:=ipccall (CALL_SEMGET,key,nsems,semflg,Nil);
  283. end;
  284. Function semop(semid:longint; sops: pointer; nsops:cardinal): Boolean;
  285. begin
  286. semop:=ipccall (CALL_SEMOP,semid,Longint(nsops),0,Pointer(sops))=0;
  287. end;
  288. Function semctl(semid:longint; semnum:longint; cmd:longint; var arg: tsemun): longint;
  289. begin
  290. semctl:=ipccall(CALL_SEMCTL,semid,semnum,cmd,@arg);
  291. end;
  292. end.
  293. {
  294. $Log$
  295. Revision 1.7 2004-02-06 23:06:16 florian
  296. - killed tsyscallregs
  297. Revision 1.6 2003/11/16 14:09:25 marco
  298. * few things renamed
  299. Revision 1.5 2003/09/14 20:15:01 marco
  300. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  301. Revision 1.4 2002/09/07 16:01:27 peter
  302. * old logs removed and tabs fixed
  303. }