ipc.pp 8.3 KB

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