bunxfunc.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 POSIX 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 ossysch.inc} // external interface to syscalls in system unit.
  17. {$i genfuncs.inc} // generic calls. (like getenv)
  18. {$I gensigset.inc} // general sigset funcs implementation.
  19. {$I genfdset.inc} // general fdset funcs.
  20. { $ ifndef ver1_0}
  21. Function FpSigProcMask(how : cInt; Const nset : TSigSet; var oset : TSigSet): cInt; external name 'FPC_SYSC_SIGPROCMASK';
  22. { $ endif}
  23. Function FPKill(Pid:pid_t;Sig:cint):cint;
  24. {
  25. Send signal 'sig' to a process, or a group of processes.
  26. If Pid > 0 then the signal is sent to pid
  27. pid=-1 to all processes except process 1
  28. pid < -1 to process group -pid
  29. Return value is zero, except for case three, where the return value
  30. is the number of processes to which the signal was sent.
  31. }
  32. begin
  33. FPkill:=do_syscall(syscall_nr_kill,pid,sig);
  34. // if kill<0 THEN
  35. // Kill:=0;
  36. end;
  37. function FPSigProcMask(how:cint;nset : psigset;oset : psigset):cint; [public, alias : 'FPC_SYSC_SIGPROCMASK'];
  38. {
  39. Change the list of currently blocked signals.
  40. How determines which signals will be blocked :
  41. SigBlock : Add SSet to the current list of blocked signals
  42. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  43. SigSetMask : Set the list of blocked signals to SSet
  44. if OldSSet is non-null, the old set will be saved there.
  45. }
  46. begin
  47. FPsigprocmask:=do_syscall(syscall_nr_sigprocmask,longint(how),longint(nset),longint(oset));
  48. end;
  49. Function FPSigPending(var nset: sigset_t):cint;
  50. {
  51. Allows examination of pending signals. The signal mask of pending
  52. signals is set in SSet
  53. }
  54. begin
  55. FPsigpending:=do_syscall(syscall_nr_sigpending,longint(@nset));
  56. end;
  57. function FPsigsuspend(const sigmask:sigset_t):cint;
  58. {
  59. Set the signal mask with Mask, and suspend the program until a signal
  60. is received.
  61. }
  62. begin
  63. FPsigsuspend:= do_syscall(syscall_nr_sigsuspend,longint(@sigmask));
  64. end;
  65. Type // implementation side for now. Should move to BSD unit.
  66. ITimerVal= Record
  67. It_Interval,
  68. It_Value : TimeVal;
  69. end;
  70. Const ITimer_Real =0;
  71. ITimer_Virtual =1;
  72. ITimer_Prof =2;
  73. Function SetITimer(Which : Longint;Const value : ItimerVal; var VarOValue:ItimerVal):Longint;
  74. Begin
  75. SetItimer:=Do_Syscall(syscall_nr_setitimer,Which,Longint(@Value),longint(@varovalue));
  76. End;
  77. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  78. Begin
  79. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,Longint(@value));
  80. End;
  81. Function FPalarm(Seconds: cuint):cuint;
  82. Var it,oitv : Itimerval;
  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_sec:=seconds;
  88. it.it_value.tv_usec:=0;
  89. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  90. Exit(-1);
  91. if oitv.it_value.tv_usec<>0 Then
  92. Inc(oitv.it_value.tv_sec);
  93. FPAlarm:=oitv.it_value.tv_sec;
  94. End;
  95. function sigblock(mask:cuint):cint;
  96. {Depreciated, but used by pause.}
  97. var nset,oset: sigset_t;
  98. begin
  99. FPsigemptyset(nset);
  100. nset[0]:=mask;
  101. sigblock:= FPsigprocmask(SIG_BLOCK,@nset,@oset); // SIG_BLOCK=1
  102. if sigblock=0 Then
  103. sigblock:=oset[0];
  104. end;
  105. function sigpause(sigmask:cint):cint;
  106. {Depreciated, but used by pause.}
  107. var nset: sigset_t;
  108. begin
  109. FPsigemptyset(nset);
  110. nset[0]:=sigmask;
  111. sigpause:= FPsigsuspend(nset);
  112. end;
  113. function FPpause:cint;
  114. begin
  115. FPpause:=sigpause(sigblock(cuint(0)));
  116. end;
  117. function FPsleep(seconds:cuint):cuint;
  118. var time_to_sleep,time_remaining : timespec;
  119. begin
  120. {
  121. * Avoid overflow when `seconds' is huge. This assumes that
  122. * the maximum value for a time_t is >= INT_MAX.
  123. }
  124. if seconds > high(cint) Then
  125. FPsleep:= (seconds - high(cint)) + FPsleep(HIGH(cint));
  126. time_to_sleep.tv_sec := seconds;
  127. time_to_sleep.tv_nsec := 0;
  128. if (FPnanosleep(time_to_sleep, time_remaining) <> -1) Then
  129. Exit(0);
  130. if (geterrno <> ESysEINTR) Then
  131. Exit (seconds); { best guess }
  132. FPsleep:= time_remaining.tv_sec;
  133. if (time_remaining.tv_nsec <> 0) Then
  134. inc(FPsleep);
  135. End;
  136. function FPuname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  137. Var
  138. mib : array[0..1] of cint;
  139. rval : cint;
  140. len : size_t;
  141. i : longint;
  142. oerrno : cint;
  143. procedure Doone(pz:pchar;pzsize:cint;val1,val2:cint);
  144. Begin
  145. mib[0] := val1;
  146. mib[1] := val2;
  147. len := pzsize;
  148. oerrno := geterrno;
  149. if (FPsysctl(@mib, 2, pz, @len, NIL, 0) = -1) Then
  150. Begin
  151. if (geterrno = ESysENOMEM) Then
  152. seterrno(oerrno)
  153. else
  154. rval := -1;
  155. End;
  156. pz[pzsize- 1] := #0;
  157. End;
  158. Begin
  159. rval := 0;
  160. DoOne(@name.sysname,sizeof(name.sysname),CTL_KERN,KERN_OSTYPE);
  161. DoOne(@name.nodename,sizeof(name.nodename),CTL_KERN,KERN_HOSTNAME);
  162. DoOne(@name.release,sizeof(name.release),CTL_KERN,KERN_OSRELEASE);
  163. { The version may have newlines in it, turn them into spaces. }
  164. DoOne(@name.version,sizeof(name.version),CTL_KERN,KERN_VERSION);
  165. For I:=0 to sizeof(name.sysname)-2 Do
  166. If (name.version[i]=#13) or (name.version[i]=#9) Then
  167. name.version[i]:=' ';
  168. DoOne(@name.machine,sizeof(name.machine),CTL_HW,HW_MACHINE);
  169. FPUname:=rval;
  170. end;
  171. function GetDomainName(Name:PChar; NameLen:Cint):cint; [public,alias:'FPC_SYSC_GETDOMAINNAME'];
  172. Const Mib_GetDomainName : array[0..1] of cint=(CTL_KERN,KERN_NISDOMAINNAME);
  173. VAR
  174. tsize : size_t;
  175. begin
  176. tsize := namelen;
  177. if (FPsysctl(@Mib_GetDomainname, 2, name, @tsize, NIL, 0) = -1) Then
  178. GetDomainName:=-1
  179. Else
  180. GetDomainName:=0;
  181. end;
  182. function GetHostName(Name:PChar; NameLen:Cint):cint;[public,alias:'FPC_SYSC_GETHOSTNAME'];
  183. Const Mib_GetHostName : array[0..1] of cint=(CTL_KERN,KERN_HOSTNAME);
  184. Var
  185. tsize : size_t;
  186. begin
  187. tsize := namelen;
  188. if (FPsysctl(@Mib_GetHostName, 2, name, @tsize, NIL, 0) = -1) Then
  189. GetHostName:=-1
  190. Else
  191. GetHostName:=0;
  192. End;
  193. const WAIT_ANY = -1;
  194. function FPwait(var stat_loc:cint): pid_t;
  195. {
  196. Waits until a child with PID Pid exits, or returns if it is exited already.
  197. Any resources used by the child are freed.
  198. The exit status is reported in the adress referred to by Status. It should
  199. be a longint.
  200. }
  201. begin // actually a wait4() call with 4th arg 0.
  202. FPWait:=do_syscall(syscall_nr_WaitPID,WAIT_ANY,longint(@Stat_loc),0,0);
  203. end;
  204. //function FPgetpid : pid_t;
  205. // begin
  206. // FPgetpid:=do_syscall(syscall_nr_getpid);
  207. // end;
  208. function FPgetppid : pid_t;
  209. begin
  210. FPgetppid:=do_syscall(syscall_nr_getppid);
  211. end;
  212. function FPgetuid : uid_t;
  213. begin
  214. FPgetuid:=do_syscall(syscall_nr_getuid);
  215. end;
  216. function FPgeteuid : uid_t;
  217. begin
  218. FPgeteuid:=do_syscall(syscall_nr_geteuid);
  219. end;
  220. function FPgetgid : gid_t;
  221. begin
  222. FPgetgid:=do_syscall(syscall_nr_getgid);
  223. end;
  224. function FPgetegid : gid_t;
  225. begin
  226. FPgetegid:=do_syscall(syscall_nr_getegid);
  227. end;
  228. function FPsetuid(uid : uid_t): cint;
  229. begin
  230. FPsetuid:=do_syscall(syscall_nr_setuid,uid);
  231. end;
  232. function FPsetgid(gid : gid_t): cint;
  233. begin
  234. FPsetgid:=do_syscall(syscall_nr_setgid,gid);
  235. end;
  236. // type tgrparr=array[0..0] of gid_t;
  237. function FPgetgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  238. begin
  239. FPgetgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,longint(@grouplist));
  240. end;
  241. function FPgetpgrp : pid_t;
  242. begin
  243. FPgetpgrp:=do_syscall(syscall_nr_getpgrp);
  244. end;
  245. function FPsetsid : pid_t;
  246. begin
  247. FPsetsid:=do_syscall(syscall_nr_setsid);
  248. end;
  249. Function FPumask(cmask:mode_t):mode_t;
  250. {
  251. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  252. previous value.
  253. }
  254. begin
  255. FPumask:=Do_syscall(syscall_nr_umask,cmask);
  256. end;
  257. Function FPlink(existing:pchar;newone:pchar):cint;
  258. {
  259. Proceduces a hard link from new to old.
  260. In effect, new will be the same file as old.
  261. }
  262. begin
  263. FPLink:=Do_Syscall(syscall_nr_link,longint(existing),longint(newone));
  264. end;
  265. Function FPmkfifo(path:pchar;mode:mode_t):cint;
  266. begin
  267. FPmkfifo:=do_syscall(syscall_nr_mkfifo,longint(path),longint(mode));
  268. end;
  269. Function FPchmod(path:pchar;mode:mode_t):cint;
  270. begin
  271. FPchmod:=do_syscall(syscall_nr_chmod,longint(path),longint(mode));
  272. end;
  273. Function FPchown(path:pchar;owner:uid_t;group:gid_t):cint;
  274. begin
  275. FPChOwn:=do_syscall(syscall_nr_chown,longint(path),longint(owner),longint(group));
  276. end;
  277. Function FPUtime(path:pchar;times:putimbuf):cint;
  278. var tv : array[0..1] of timeval;
  279. tvp : ^timeval;
  280. begin
  281. if times=nil Then
  282. tvp:=nil
  283. else
  284. begin
  285. tv[0].tv_sec :=times^.actime;
  286. tv[1].tv_sec :=times^.modtime;
  287. tv[0].tv_usec:=0;
  288. tv[1].tv_usec:=0;
  289. tvp:=@tv;
  290. end;
  291. FPutime:=do_syscall(syscall_nr_utimes,longint(path),longint(tvp));
  292. end;
  293. Function FPpipe(var fildes : tfildes):cint;
  294. begin
  295. FPpipe:=do_syscall(syscall_nr_pipe,longint(@fildes));
  296. end;
  297. function FPfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  298. begin
  299. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  300. end;
  301. function FPfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  302. begin
  303. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,longint(@arg));
  304. end;
  305. function FPfcntl(fildes:cint;Cmd:cint):cint;
  306. begin
  307. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  308. end;
  309. function FPexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  310. Begin
  311. FPexecve:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  312. End;
  313. function FPexecv(path:pchar;argv:ppchar):cint;
  314. Begin
  315. FPexecv:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  316. End;
  317. CONST RUSAGE_SELF = 0;
  318. RUSAGE_CHILDREN = -1;
  319. function FPgetrusage(who:cint;var ru : rusage):cint;
  320. begin
  321. FPgetrusage:=do_syscall(syscall_nr_getrusage,longint(who),longint(@ru));
  322. end;
  323. function FPtimes(var buffer : tms):clock_t;
  324. var ru : rusage;
  325. t : timeval;
  326. CONST CLK_TCK=128;
  327. function CONVTCK(r:timeval):clock_t;
  328. {
  329. * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
  330. * but this would overflow if we switch to nanosec.
  331. }
  332. begin
  333. CONVTCK:=(r.tv_sec * CLK_TCK + r.tv_usec DIV (1000000 DIV CLK_TCK));
  334. end;
  335. begin
  336. if (FPgetrusage(RUSAGE_SELF, ru) < 0) Then
  337. exit(clock_t(-1));
  338. buffer.tms_utime := CONVTCK(ru.ru_utime);
  339. buffer.tms_stime := CONVTCK(ru.ru_stime);
  340. if (FPgetrusage(RUSAGE_CHILDREN, ru) < 0) Then
  341. exit(clock_t(-1));
  342. buffer.tms_cutime := CONVTCK(ru.ru_utime);
  343. buffer.tms_cstime := CONVTCK(ru.ru_stime);
  344. if do_syscall(syscall_nr_gettimeofday,longint(@t),0)<>0 Then
  345. exit(clock_t(-1));
  346. FPtimes:=clock_t(CONVTCK(t));
  347. end;
  348. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  349. {
  350. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  351. have changed.
  352. }
  353. begin
  354. fpSelect:=do_syscall(syscall_nr_select,n,longint(readfds),longint(writefds),longint(exceptfds),longint(timeout));
  355. end;
  356. {
  357. $Log$
  358. Revision 1.3 2003-09-14 20:15:01 marco
  359. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  360. Revision 1.2 2003/01/05 19:10:05 marco
  361. * Small sigprocmask fix
  362. Revision 1.1 2003/01/05 19:01:28 marco
  363. * FreeBSD compiles now with baseunix mods.
  364. Revision 1.11 2002/11/14 13:25:27 marco
  365. * Fix setitimer.
  366. Revision 1.10 2002/11/14 12:34:20 marco
  367. * took out the generic sethandling.
  368. Revision 1.9 2002/11/13 18:15:08 marco
  369. * sigset functions more flexible, small changes to FPtime
  370. Revision 1.8 2002/10/27 17:21:29 marco
  371. * Only "difficult" functions + execvp + termios + rewinddir left to do
  372. Revision 1.7 2002/10/27 11:58:29 marco
  373. * Modifications from Saturday.
  374. Revision 1.6 2002/10/26 18:27:51 marco
  375. * First series POSIX calls commits. Including getcwd.
  376. Revision 1.5 2002/10/25 15:46:48 marco
  377. * Should be alias.
  378. Revision 1.4 2002/09/08 16:20:27 marco
  379. * Forgot external name's
  380. Revision 1.3 2002/09/08 16:11:59 marco
  381. * Added GetDomainName and that other one ..
  382. Revision 1.2 2002/09/07 16:01:17 peter
  383. * old logs removed and tabs fixed
  384. Revision 1.1 2002/08/21 07:03:16 marco
  385. * Fixes from Tuesday.
  386. Revision 1.1 2002/08/08 11:39:30 marco
  387. * Initial versions, to allow support for uname in posix.pp
  388. }