bunxfunc.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. {$ifndef FPC_USE_LIBC}
  15. {$i syscallh.inc} // do_syscall declarations themselves
  16. {$i sysnr.inc} // syscall numbers.
  17. {$i ossysch.inc} // external interface to syscalls in system unit.
  18. {$endif}
  19. {$i genfuncs.inc} // generic calls. (like getenv)
  20. {$I gensigset.inc} // general sigset funcs implementation.
  21. {$I genfdset.inc} // general fdset funcs.
  22. {$ifndef FPC_USE_LIBC}
  23. { $ ifndef ver1_0}
  24. // Function FpSigProcMask(how : cInt; Const nset : TSigSet; var oset : TSigSet): cInt; external name 'FPC_SYSC_SIGPROCMASK';
  25. { $ endif}
  26. {$USER BLA!}
  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 FPSigPending(var nset: sigset_t):cint;
  42. {
  43. Allows examination of pending signals. The signal mask of pending
  44. signals is set in SSet
  45. }
  46. begin
  47. FPsigpending:=do_syscall(syscall_nr_sigpending,longint(@nset));
  48. end;
  49. function FPsigsuspend(const sigmask:sigset_t):cint;
  50. {
  51. Set the signal mask with Mask, and suspend the program until a signal
  52. is received.
  53. }
  54. begin
  55. FPsigsuspend:= do_syscall(syscall_nr_sigsuspend,longint(@sigmask));
  56. end;
  57. Type // implementation side for now. Should move to BSD unit.
  58. ITimerVal= Record
  59. It_Interval,
  60. It_Value : TimeVal;
  61. end;
  62. Const ITimer_Real =0;
  63. ITimer_Virtual =1;
  64. ITimer_Prof =2;
  65. Function SetITimer(Which : Longint;Const value : ItimerVal; var VarOValue:ItimerVal):Longint;
  66. Begin
  67. SetItimer:=Do_Syscall(syscall_nr_setitimer,Which,Longint(@Value),longint(@varovalue));
  68. End;
  69. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  70. Begin
  71. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,Longint(@value));
  72. End;
  73. Function FPalarm(Seconds: cuint):cuint;
  74. Var it,oitv : Itimerval;
  75. Begin
  76. // register struct itimerval *itp = &it;
  77. it.it_interval.tv_sec:=0;
  78. it.it_interval.tv_usec:=0;
  79. it.it_value.tv_sec:=seconds;
  80. it.it_value.tv_usec:=0;
  81. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  82. Exit(-1);
  83. if oitv.it_value.tv_usec<>0 Then
  84. Inc(oitv.it_value.tv_sec);
  85. FPAlarm:=oitv.it_value.tv_sec;
  86. End;
  87. function sigblock(mask:cuint):cint;
  88. {Depreciated, but used by pause.}
  89. var nset,oset: sigset_t;
  90. begin
  91. FPsigemptyset(nset);
  92. nset[0]:=mask;
  93. sigblock:= FPsigprocmask(SIG_BLOCK,@nset,@oset); // SIG_BLOCK=1
  94. if sigblock=0 Then
  95. sigblock:=oset[0];
  96. end;
  97. function sigpause(sigmask:cint):cint;
  98. {Depreciated, but used by pause.}
  99. var nset: sigset_t;
  100. begin
  101. FPsigemptyset(nset);
  102. nset[0]:=sigmask;
  103. sigpause:= FPsigsuspend(nset);
  104. end;
  105. function FPpause:cint;
  106. begin
  107. FPpause:=sigpause(sigblock(cuint(0)));
  108. end;
  109. function FPsleep(seconds:cuint):cuint;
  110. var time_to_sleep,time_remaining : timespec;
  111. begin
  112. {
  113. * Avoid overflow when `seconds' is huge. This assumes that
  114. * the maximum value for a time_t is >= INT_MAX.
  115. }
  116. if seconds > high(cint) Then
  117. FPsleep:= (seconds - high(cint)) + FPsleep(HIGH(cint));
  118. time_to_sleep.tv_sec := seconds;
  119. time_to_sleep.tv_nsec := 0;
  120. if (FPnanosleep(@time_to_sleep, @time_remaining) <> -1) Then
  121. Exit(0);
  122. if (fpgeterrno <> ESysEINTR) Then // EAGAIN?
  123. Exit (seconds); { best guess }
  124. FPsleep:= time_remaining.tv_sec;
  125. if (time_remaining.tv_nsec <> 0) Then
  126. inc(FPsleep);
  127. End;
  128. function FPuname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  129. Var
  130. mib : array[0..1] of cint;
  131. rval : cint;
  132. len : size_t;
  133. i : longint;
  134. oerrno : cint;
  135. procedure Doone(pz:pchar;pzsize:cint;val1,val2:cint);
  136. Begin
  137. mib[0] := val1;
  138. mib[1] := val2;
  139. len := pzsize;
  140. oerrno := fpgeterrno;
  141. if (FPsysctl(@mib, 2, pz, @len, NIL, 0) = -1) Then
  142. Begin
  143. if (fpgeterrno = ESysENOMEM) Then
  144. fpseterrno(oerrno)
  145. else
  146. rval := -1;
  147. End;
  148. pz[pzsize- 1] := #0;
  149. End;
  150. Begin
  151. rval := 0;
  152. DoOne(@name.sysname,sizeof(name.sysname),CTL_KERN,KERN_OSTYPE);
  153. DoOne(@name.nodename,sizeof(name.nodename),CTL_KERN,KERN_HOSTNAME);
  154. DoOne(@name.release,sizeof(name.release),CTL_KERN,KERN_OSRELEASE);
  155. { The version may have newlines in it, turn them into spaces. }
  156. DoOne(@name.version,sizeof(name.version),CTL_KERN,KERN_VERSION);
  157. For I:=0 to sizeof(name.sysname)-2 Do
  158. If (name.version[i]=#13) or (name.version[i]=#9) Then
  159. name.version[i]:=' ';
  160. DoOne(@name.machine,sizeof(name.machine),CTL_HW,HW_MACHINE);
  161. FPUname:=rval;
  162. end;
  163. function GetDomainName(Name:PChar; NameLen:Cint):cint; [public,alias:'FPC_SYSC_GETDOMAINNAME'];
  164. Const Mib_GetDomainName : array[0..1] of cint=(CTL_KERN,{$ifdef OpenBSD}KERN_DOMAINNAME{$ELSE}KERN_NISDOMAINNAME{$endif});
  165. VAR
  166. tsize : size_t;
  167. begin
  168. tsize := namelen;
  169. if (FPsysctl(@Mib_GetDomainname, 2, name, @tsize, NIL, 0) = -1) Then
  170. GetDomainName:=-1
  171. Else
  172. GetDomainName:=0;
  173. end;
  174. function GetHostName(Name:PChar; NameLen:Cint):cint;[public,alias:'FPC_SYSC_GETHOSTNAME'];
  175. Const Mib_GetHostName : array[0..1] of cint=(CTL_KERN,KERN_HOSTNAME);
  176. Var
  177. tsize : size_t;
  178. begin
  179. tsize := namelen;
  180. if (FPsysctl(@Mib_GetHostName, 2, name, @tsize, NIL, 0) = -1) Then
  181. GetHostName:=-1
  182. Else
  183. GetHostName:=0;
  184. End;
  185. const WAIT_ANY = -1;
  186. function FPwait(var stat_loc:cint): pid_t;
  187. {
  188. Waits until a child with PID Pid exits, or returns if it is exited already.
  189. Any resources used by the child are freed.
  190. The exit status is reported in the adress referred to by Status. It should
  191. be a longint.
  192. }
  193. begin // actually a wait4() call with 4th arg 0.
  194. FPWait:=do_syscall(syscall_nr_WaitPID,WAIT_ANY,longint(@Stat_loc),0,0);
  195. end;
  196. //function FPgetpid : pid_t;
  197. // begin
  198. // FPgetpid:=do_syscall(syscall_nr_getpid);
  199. // end;
  200. function FPgetppid : pid_t;
  201. begin
  202. FPgetppid:=do_syscall(syscall_nr_getppid);
  203. end;
  204. function FPgetuid : uid_t;
  205. begin
  206. FPgetuid:=do_syscall(syscall_nr_getuid);
  207. end;
  208. function FPgeteuid : uid_t;
  209. begin
  210. FPgeteuid:=do_syscall(syscall_nr_geteuid);
  211. end;
  212. function FPgetgid : gid_t;
  213. begin
  214. FPgetgid:=do_syscall(syscall_nr_getgid);
  215. end;
  216. function FPgetegid : gid_t;
  217. begin
  218. FPgetegid:=do_syscall(syscall_nr_getegid);
  219. end;
  220. function FPsetuid(uid : uid_t): cint;
  221. begin
  222. FPsetuid:=do_syscall(syscall_nr_setuid,uid);
  223. end;
  224. function FPsetgid(gid : gid_t): cint;
  225. begin
  226. FPsetgid:=do_syscall(syscall_nr_setgid,gid);
  227. end;
  228. // type tgrparr=array[0..0] of gid_t;
  229. function FPgetgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  230. begin
  231. FPgetgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,longint(@grouplist));
  232. end;
  233. function FPgetpgrp : pid_t;
  234. begin
  235. FPgetpgrp:=do_syscall(syscall_nr_getpgrp);
  236. end;
  237. function FPsetsid : pid_t;
  238. begin
  239. FPsetsid:=do_syscall(syscall_nr_setsid);
  240. end;
  241. Function FPumask(cmask:mode_t):mode_t;
  242. {
  243. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  244. previous value.
  245. }
  246. begin
  247. FPumask:=Do_syscall(syscall_nr_umask,cmask);
  248. end;
  249. Function FPlink(existing:pchar;newone:pchar):cint;
  250. {
  251. Proceduces a hard link from new to old.
  252. In effect, new will be the same file as old.
  253. }
  254. begin
  255. FPLink:=Do_Syscall(syscall_nr_link,longint(existing),longint(newone));
  256. end;
  257. Function FPmkfifo(path:pchar;mode:mode_t):cint;
  258. begin
  259. FPmkfifo:=do_syscall(syscall_nr_mkfifo,longint(path),longint(mode));
  260. end;
  261. Function FPchmod(path:pchar;mode:mode_t):cint;
  262. begin
  263. FPchmod:=do_syscall(syscall_nr_chmod,longint(path),longint(mode));
  264. end;
  265. Function FPchown(path:pchar;owner:uid_t;group:gid_t):cint;
  266. begin
  267. FPChOwn:=do_syscall(syscall_nr_chown,longint(path),longint(owner),longint(group));
  268. end;
  269. Function FPUtime(path:pchar;times:putimbuf):cint;
  270. var tv : array[0..1] of timeval;
  271. tvp : ^timeval;
  272. begin
  273. if times=nil Then
  274. tvp:=nil
  275. else
  276. begin
  277. tv[0].tv_sec :=times^.actime;
  278. tv[1].tv_sec :=times^.modtime;
  279. tv[0].tv_usec:=0;
  280. tv[1].tv_usec:=0;
  281. tvp:=@tv;
  282. end;
  283. FPutime:=do_syscall(syscall_nr_utimes,longint(path),longint(tvp));
  284. end;
  285. Function FPpipe(var fildes : tfildes):cint;
  286. begin
  287. FPpipe:=do_syscall(syscall_nr_pipe,longint(@fildes));
  288. end;
  289. function FPfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  290. begin
  291. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  292. end;
  293. function FPfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  294. begin
  295. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,longint(@arg));
  296. end;
  297. function FPfcntl(fildes:cint;Cmd:cint):cint;
  298. begin
  299. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  300. end;
  301. function FPexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  302. Begin
  303. FPexecve:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  304. End;
  305. function FPexecv(path:pchar;argv:ppchar):cint;
  306. Begin
  307. FPexecv:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  308. End;
  309. CONST RUSAGE_SELF = 0;
  310. RUSAGE_CHILDREN = -1;
  311. function FPgetrusage(who:cint;var ru : rusage):cint;
  312. begin
  313. FPgetrusage:=do_syscall(syscall_nr_getrusage,longint(who),longint(@ru));
  314. end;
  315. function FPtimes(var buffer : tms):clock_t;
  316. var ru : rusage;
  317. t : timeval;
  318. CONST CLK_TCK=128;
  319. function CONVTCK(r:timeval):clock_t;
  320. {
  321. * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
  322. * but this would overflow if we switch to nanosec.
  323. }
  324. begin
  325. CONVTCK:=(r.tv_sec * CLK_TCK + r.tv_usec DIV (1000000 DIV CLK_TCK));
  326. end;
  327. begin
  328. if (FPgetrusage(RUSAGE_SELF, ru) < 0) Then
  329. exit(clock_t(-1));
  330. buffer.tms_utime := CONVTCK(ru.ru_utime);
  331. buffer.tms_stime := CONVTCK(ru.ru_stime);
  332. if (FPgetrusage(RUSAGE_CHILDREN, ru) < 0) Then
  333. exit(clock_t(-1));
  334. buffer.tms_cutime := CONVTCK(ru.ru_utime);
  335. buffer.tms_cstime := CONVTCK(ru.ru_stime);
  336. if do_syscall(syscall_nr_gettimeofday,longint(@t),0)<>0 Then
  337. exit(clock_t(-1));
  338. FPtimes:=clock_t(CONVTCK(t));
  339. end;
  340. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  341. {
  342. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  343. have changed.
  344. }
  345. begin
  346. fpSelect:=do_syscall(syscall_nr_select,n,longint(readfds),longint(writefds),longint(exceptfds),longint(timeout));
  347. end;
  348. Function fpLstat(path:pchar;Info:pstat):cint;
  349. {
  350. Get all information on a link (the link itself), and return it in info.
  351. }
  352. begin
  353. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(path),TSysParam(info));
  354. end;
  355. Function fpLstat(Filename: ansistring;Info:pstat):cint;
  356. {
  357. Get all information on a link (the link itself), and return it in info.
  358. }
  359. begin
  360. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(pchar(filename)),TSysParam(info));
  361. end;
  362. function fpNice(N:cint):cint;
  363. {
  364. Set process priority. A positive N means a lower priority.
  365. A negative N decreases priority.
  366. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  367. }
  368. var prio : cint;
  369. begin
  370. fpseterrno(0);
  371. prio:=fpgetpriority(PRIO_PROCESS,0);
  372. if (prio=-1) and (fpgeterrno<>0) then
  373. exit(-1);
  374. fpNice:=fpSetPriority(Prio_Process,0,prio+N);
  375. end;
  376. Function fpGetPriority(Which,Who:cint):cint;
  377. {
  378. Get Priority of process, process group, or user.
  379. Which : selects what kind of priority is used.
  380. can be one of the following predefined Constants :
  381. Prio_User.
  382. Prio_PGrp.
  383. Prio_Process.
  384. Who : depending on which, this is , respectively :
  385. Uid
  386. Pid
  387. Process Group id
  388. Errors are reported in linuxerror _only_. (priority can be negative)
  389. }
  390. begin
  391. if (which<prio_process) or (which>prio_user) then
  392. begin
  393. { We can save an interrupt here }
  394. fpgetpriority:=0;
  395. fpseterrno(ESysEinval);
  396. end
  397. else
  398. begin
  399. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  400. end;
  401. end;
  402. Function fpSetPriority(Which,Who,What:cint):cint;
  403. {
  404. Set Priority of process, process group, or user.
  405. Which : selects what kind of priority is used.
  406. can be one of the following predefined Constants :
  407. Prio_User.
  408. Prio_PGrp.
  409. Prio_Process.
  410. Who : depending on value of which, this is, respectively :
  411. Uid
  412. Pid
  413. Process Group id
  414. what : A number between -20 and 20. -20 is most favorable, 20 least.
  415. 0 is the default.
  416. }
  417. begin
  418. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  419. fpseterrno(ESyseinval) { We can save an interrupt here }
  420. else
  421. begin
  422. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  423. end;
  424. end;
  425. Function fpSymlink(oldname,newname:pchar):cint;
  426. {
  427. We need this for erase
  428. }
  429. begin
  430. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  431. end;
  432. {$endif}
  433. {
  434. $Log$
  435. Revision 1.10 2004-11-19 13:15:14 marco
  436. * external rework. Mostly done.
  437. Revision 1.9 2004/11/14 12:21:08 marco
  438. * moved some calls from unix to baseunix. Darwin untested.
  439. Revision 1.8 2004/01/22 13:46:14 marco
  440. bsd
  441. Revision 1.7 2003/12/30 12:26:21 marco
  442. * FPC_USE_LIBC
  443. Revision 1.6 2003/11/18 10:12:25 marco
  444. * Small fixes for EAGAIN. bunxfunc only has comments added.
  445. Revision 1.5 2003/10/26 17:01:04 marco
  446. * moved sigprocmask to system
  447. Revision 1.4 2003/09/27 13:45:58 peter
  448. * fpnanosleep exported in baseunix
  449. * fpnanosleep has pointer arguments to be C compliant
  450. Revision 1.3 2003/09/14 20:15:01 marco
  451. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  452. Revision 1.2 2003/01/05 19:10:05 marco
  453. * Small sigprocmask fix
  454. Revision 1.1 2003/01/05 19:01:28 marco
  455. * FreeBSD compiles now with baseunix mods.
  456. Revision 1.11 2002/11/14 13:25:27 marco
  457. * Fix setitimer.
  458. Revision 1.10 2002/11/14 12:34:20 marco
  459. * took out the generic sethandling.
  460. Revision 1.9 2002/11/13 18:15:08 marco
  461. * sigset functions more flexible, small changes to FPtime
  462. Revision 1.8 2002/10/27 17:21:29 marco
  463. * Only "difficult" functions + execvp + termios + rewinddir left to do
  464. Revision 1.7 2002/10/27 11:58:29 marco
  465. * Modifications from Saturday.
  466. Revision 1.6 2002/10/26 18:27:51 marco
  467. * First series POSIX calls commits. Including getcwd.
  468. Revision 1.5 2002/10/25 15:46:48 marco
  469. * Should be alias.
  470. Revision 1.4 2002/09/08 16:20:27 marco
  471. * Forgot external name's
  472. Revision 1.3 2002/09/08 16:11:59 marco
  473. * Added GetDomainName and that other one ..
  474. Revision 1.2 2002/09/07 16:01:17 peter
  475. * old logs removed and tabs fixed
  476. Revision 1.1 2002/08/21 07:03:16 marco
  477. * Fixes from Tuesday.
  478. Revision 1.1 2002/08/08 11:39:30 marco
  479. * Initial versions, to allow support for uname in posix.pp
  480. }