bunxfunc.inc 12 KB

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