bunxfunc.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2002 by Marco van de Voort
  5. Calls needed for the baseunix unit, but not for system.
  6. Some calls that can be used for both Linux and *BSD will be
  7. moved to a /unix/ includedfile later.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. {$i syscallh.inc} // do_syscall declarations themselves
  15. {$i sysnr.inc} // syscall numbers.
  16. {$i ostypes.inc}
  17. {$i ossysch.inc} // external interface to syscalls in system unit.
  18. //{$i genfuncs.inc}
  19. {$i bunxmacr.inc} // macro's.
  20. {$I gensigset.inc} // general sigset funcs implementation.
  21. {$I genfdset.inc}
  22. Function fpKill(Pid:pid_t;Sig:cint):cint;
  23. {
  24. Send signal 'sig' to a process, or a group of processes.
  25. If Pid > 0 then the signal is sent to pid
  26. pid=-1 to all processes except process 1
  27. pid < -1 to process group -pid
  28. Return value is zero, except for case three, where the return value
  29. is the number of processes to which the signal was sent.
  30. }
  31. begin
  32. fpkill:=do_syscall(syscall_nr_kill,TSysParam(pid),TSysParam(sig));
  33. // if kill<0 THEN
  34. // Kill:=0;
  35. end;
  36. {overload}
  37. Function FpSigProcMask(how : cInt; Const nset : TSigSet; var oset : TSigSet): cInt; external name 'FPC_SYSC_SIGPROGMASK';
  38. Function fpSigProcMask(how:cint;nset : pSigSet; oset : pSigSet):cint; [public, alias : 'FPC_SYSC_SIGPROGMASK'];
  39. {
  40. Change the list of currently blocked signals.
  41. How determines which signals will be blocked :
  42. SigBlock : Add SSet to the current list of blocked signals
  43. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  44. SigSetMask : Set the list of blocked signals to SSet
  45. if OldSSet is non-null, the old set will be saved there.
  46. }
  47. begin
  48. fpsigprocmask:=do_syscall(syscall_nr_sigprocmask,longint(how),longint(nset),longint(oset));
  49. end;
  50. Function fpSigPending(var nset: TSigSet):cint;
  51. {
  52. Allows examination of pending signals. The signal mask of pending
  53. signals is set in SSet
  54. }
  55. begin
  56. fpsigpending:=do_syscall(syscall_nr_sigpending,longint(@nset));
  57. end;
  58. function fpsigsuspend(const sigmask:TSigSet):cint;
  59. {
  60. Set the signal mask with Mask, and suspend the program until a signal
  61. is received.
  62. }
  63. begin
  64. fpsigsuspend:= do_syscall(syscall_nr_sigsuspend,longint(@sigmask));
  65. end;
  66. Type
  67. ITimerVal= Record
  68. It_Interval,
  69. It_Value : TimeVal;
  70. end;
  71. Const ITimer_Real =0;
  72. ITimer_Virtual =1;
  73. ITimer_Prof =2;
  74. Function SetITimer(Which : Longint;Const value : ItimerVal; var VarOValue:ItimerVal):Longint;
  75. Begin
  76. SetItimer:=Do_Syscall(syscall_nr_setitimer,Which,Longint(@Value),longint(@varovalue));
  77. End;
  78. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  79. Begin
  80. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,Longint(@value));
  81. End;
  82. Function fpalarm(Seconds: cuint):cuint;
  83. Var it,oitv : Itimerval;
  84. retval : cuint;
  85. Begin
  86. // register struct itimerval *itp = &it;
  87. it.it_interval.tv_sec:=0;
  88. it.it_interval.tv_usec:=0;
  89. it.it_value.tv_usec:=0;
  90. it.it_value.tv_sec:=seconds;
  91. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  92. Exit(0); // different from *BSD!
  93. retval:= oitv.it_value.tv_usec;
  94. if retval<>0 Then
  95. inc(retval);
  96. fpAlarm:=retval;
  97. End;
  98. // The following versions are for internal use _ONLY_
  99. // This because it works for the first 32 signals _ONLY_, but that
  100. // is enough since they are depreciated, and for legacy applications
  101. // anyway.
  102. function sigblock(mask:cuint):cint;
  103. var nset,oset: TSigSet;
  104. begin
  105. fpsigemptyset(nset);
  106. // fpsigaddset(nset,mask); needs _mask_
  107. nset[0]:=mask;
  108. sigblock:= fpsigprocmask(SIG_BLOCK,@nset,@oset); // SIG_BLOCK=1
  109. if sigblock=0 Then
  110. sigblock:=oset[0];
  111. end;
  112. function sigpause(sigmask:cint):cint;
  113. var nset: TSigSet;
  114. begin
  115. fpsigemptyset(nset);
  116. nset[0]:=sigmask;
  117. sigpause:= fpsigsuspend(nset);
  118. end;
  119. function fppause:cint;
  120. begin
  121. fppause:=sigpause(sigblock(cuint(0)));
  122. end;
  123. function fpsleep(seconds:cuint):cuint;
  124. {see comments in libc}
  125. var time_to_sleep,time_remaining : timespec;
  126. nset,oset : TSigSet;
  127. oerrno : cint;
  128. oact : sigactionrec;
  129. begin
  130. time_to_sleep.tv_sec := seconds;
  131. time_to_sleep.tv_nsec := 0;
  132. fpsigemptyset(nset);
  133. fpsigaddset (nset,SIGCHLD);
  134. if fpsigprocmask(SIG_BLOCK,@nset,@oset)=0 Then
  135. exit(cuint(-1));
  136. if fpsigismember(oset,SIGCHLD)<>0 Then
  137. Begin
  138. fpsigemptyset(nset);
  139. fpsigaddset (nset,SIGCHLD);
  140. if fpsigaction(SIGCHLD,NIL,@oact)<0 Then
  141. begin
  142. oerrno:=geterrno;
  143. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  144. seterrno(oerrno);
  145. exit(cuint(-1));
  146. End;
  147. if oact.sa_handler=signalhandler(SIG_IGN) Then
  148. Begin
  149. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining);
  150. oerrno:=geterrno;
  151. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  152. seterrno(oerrno);
  153. End
  154. Else
  155. Begin
  156. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  157. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining)
  158. End;
  159. end
  160. else
  161. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining);
  162. if fpsleep<>0 Then
  163. if time_remaining.tv_nsec>=500000000 Then
  164. inc(fpsleep);
  165. End;
  166. function fpuname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  167. begin
  168. fpuname:=Do_Syscall(syscall_nr_uname,TSysParam(@name));
  169. end;
  170. Function fpGetDomainName(Name:PChar; NameLen:size_t):cint;
  171. Var
  172. srec : utsname;
  173. tsize : size_t;
  174. Begin
  175. if fpuname(srec)<0 Then
  176. exit(-1);
  177. tsize:=strlen(@srec.domain[0]);
  178. if tsize>(namelen-1) Then
  179. tsize:=namelen-1;
  180. move(srec.domain[0],name[0],tsize);
  181. name[namelen-1]:=#0;
  182. fpgetDomainName:=0;
  183. End;
  184. function fpGetHostName(Name:PChar; NameLen:size_t):cint;
  185. Var
  186. srec : utsname;
  187. tsize : size_t;
  188. begin
  189. if fpuname(srec)<0 Then
  190. exit(-1);
  191. tsize:=strlen(@srec.nodename[0]);
  192. if tsize>(namelen-1) Then
  193. tsize:=namelen-1;
  194. move(srec.nodename[0],name[0],tsize);
  195. name[namelen-1]:=#0;
  196. fpgethostName:=0;
  197. End;
  198. const WAIT_ANY = -1;
  199. function fpwait(var stat_loc:cint): pid_t;
  200. {
  201. Waits until a child with PID Pid exits, or returns if it is exited already.
  202. Any resources used by the child are freed.
  203. The exit status is reported in the adress referred to by Status. It should
  204. be a longint.
  205. }
  206. begin // actually a wait4() call with 4th arg 0.
  207. fpWait:=do_syscall(syscall_nr_Wait4,WAIT_ANY,longint(@Stat_loc),0,0);
  208. end;
  209. //function fpgetpid : pid_t;
  210. // begin
  211. // fpgetpid:=do_syscall(syscall_nr_getpid);
  212. // end;
  213. function fpgetppid : pid_t;
  214. begin
  215. fpgetppid:=do_syscall(syscall_nr_getppid);
  216. end;
  217. function fpgetuid : uid_t;
  218. begin
  219. fpgetuid:=do_syscall(syscall_nr_getuid);
  220. end;
  221. function fpgeteuid : uid_t;
  222. begin
  223. fpgeteuid:=do_syscall(syscall_nr_geteuid);
  224. end;
  225. function fpgetgid : gid_t;
  226. begin
  227. fpgetgid:=do_syscall(syscall_nr_getgid);
  228. end;
  229. function fpgetegid : gid_t;
  230. begin
  231. fpgetegid:=do_syscall(syscall_nr_getegid);
  232. end;
  233. function fpsetuid(uid : uid_t): cint;
  234. begin
  235. fpsetuid:=do_syscall(syscall_nr_setuid,uid);
  236. end;
  237. function fpsetgid(gid : gid_t): cint;
  238. begin
  239. fpsetgid:=do_syscall(syscall_nr_setgid,gid);
  240. end;
  241. // type tgrparr=array[0..0] of gid_t;
  242. function fpgetgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  243. begin
  244. fpgetgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,longint(@grouplist));
  245. end;
  246. function fpgetpgrp : pid_t;
  247. begin
  248. fpgetpgrp:=do_syscall(syscall_nr_getpgrp);
  249. end;
  250. function fpsetsid : pid_t;
  251. begin
  252. fpsetsid:=do_syscall(syscall_nr_setsid);
  253. end;
  254. Function fpumask(cmask:mode_t):mode_t;
  255. {
  256. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  257. previous value.
  258. }
  259. begin
  260. fpumask:=Do_syscall(syscall_nr_umask,cmask);
  261. end;
  262. Function fplink(existing:pchar;newone:pchar):cint;
  263. {
  264. Proceduces a hard link from new to old.
  265. In effect, new will be the same file as old.
  266. }
  267. begin
  268. fpLink:=Do_Syscall(syscall_nr_link,longint(existing),longint(newone));
  269. end;
  270. Function fpmkfifo(path:pchar;mode:mode_t):cint;
  271. begin
  272. fpmkfifo:=do_syscall(syscall_nr_mknod,TSysParam(path),TSysParam(mode or S_IFIFO),TSysParam(0));
  273. end;
  274. Function fpchmod(path:pchar;mode:mode_t):cint;
  275. begin
  276. fpchmod:=do_syscall(syscall_nr_chmod,longint(path),longint(mode));
  277. end;
  278. Function fpchown(path:pchar;owner:uid_t;group:gid_t):cint;
  279. begin
  280. fpChOwn:=do_syscall(syscall_nr_chown,longint(path),longint(owner),longint(group));
  281. end;
  282. Function fpUtime(path:pchar;times:putimbuf):cint;
  283. begin
  284. fputime:=do_syscall(syscall_nr_utime,TSysParam(path),TSysParam(times));
  285. end;
  286. Function fppipe(var fildes : tfildes):cint;
  287. begin
  288. fppipe:=do_syscall(syscall_nr_pipe,longint(@fildes));
  289. end;
  290. function fpfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  291. begin
  292. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  293. end;
  294. function fpfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  295. begin
  296. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,longint(@arg));
  297. end;
  298. function fpfcntl(fildes:cint;Cmd:cint):cint;
  299. begin
  300. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  301. end;
  302. function fpexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  303. Begin
  304. fpexecve:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  305. End;
  306. function fpexecv(path:pchar;argv:ppchar):cint;
  307. Begin
  308. fpexecv:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  309. End;
  310. function fptimes(var buffer : tms):clock_t;
  311. begin
  312. fptimes:=Do_syscall(syscall_nr_times,TSysParam(@buffer));
  313. end;
  314. function fpgetcwd(path : pchar; siz:size_t):pchar;
  315. begin
  316. fpgetcwd:=pchar(Do_Syscall(Syscall_nr_getcwd,TSysParam(Path),TSysParam(siz)));
  317. end;
  318. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  319. {
  320. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  321. have changed.
  322. }
  323. Var
  324. SelectArray : Array[1..5] of longint;
  325. begin
  326. SelectArray[1]:=n;
  327. SelectArray[2]:=longint(Readfds);
  328. Selectarray[3]:=longint(Writefds);
  329. selectarray[4]:=longint(exceptfds);
  330. Selectarray[5]:=longint(TimeOut);
  331. fpSelect:=do_syscall(syscall_nr_select,longint(@selectarray));
  332. end;
  333. {
  334. $Log$
  335. Revision 1.5 2003-09-27 13:45:58 peter
  336. * fpnanosleep exported in baseunix
  337. * fpnanosleep has pointer arguments to be C compliant
  338. Revision 1.4 2003/09/17 11:24:46 marco
  339. * fixes for new macro's
  340. Revision 1.3 2003/09/14 20:15:01 marco
  341. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  342. Revision 1.2 2003/01/05 19:16:45 marco
  343. * small fix
  344. Revision 1.1 2002/12/18 16:43:26 marco
  345. * new unix rtl, linux part.....
  346. Revision 1.1 2002/11/14 16:48:39 marco
  347. * Initial version
  348. Revision 1.10 2002/11/14 12:34:20 marco
  349. * took out the generic sethandling.
  350. Revision 1.9 2002/11/13 18:15:08 marco
  351. * sigset functions more flexible, small changes to sys_time
  352. Revision 1.8 2002/10/27 17:21:29 marco
  353. * Only "difficult" functions + execvp + termios + rewinddir left to do
  354. Revision 1.7 2002/10/27 11:58:29 marco
  355. * Modifications from Saturday.
  356. Revision 1.6 2002/10/26 18:27:51 marco
  357. * First series POSIX calls commits. Including getcwd.
  358. Revision 1.5 2002/10/25 15:46:48 marco
  359. * Should be alias.
  360. Revision 1.4 2002/09/08 16:20:27 marco
  361. * Forgot external name's
  362. Revision 1.3 2002/09/08 16:11:59 marco
  363. * Added GetDomainName and that other one ..
  364. Revision 1.2 2002/09/07 16:01:17 peter
  365. * old logs removed and tabs fixed
  366. Revision 1.1 2002/08/21 07:03:16 marco
  367. * Fixes from Tuesday.
  368. Revision 1.1 2002/08/08 11:39:30 marco
  369. * Initial versions, to allow support for uname in posix.pp
  370. }