bunxfunc.inc 12 KB

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