bunxfunc.inc 11 KB

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