bunxfunc.inc 12 KB

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