bunxsysc.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Marco van de Voort
  4. Calls needed for the POSIX unit, but not for system.
  5. Some calls that can be used for both Linux and *BSD will be
  6. moved to a /unix/ includedfile later.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Function FPKill(Pid:pid_t;Sig:cint):cint;
  14. {
  15. Send signal 'sig' to a process, or a group of processes.
  16. If Pid > 0 then the signal is sent to pid
  17. pid=-1 to all processes except process 1
  18. pid < -1 to process group -pid
  19. Return value is zero, except for case three, where the return value
  20. is the number of processes to which the signal was sent.
  21. }
  22. begin
  23. FPkill:=do_syscall(syscall_nr_kill,pid,sig);
  24. // if kill<0 THEN
  25. // Kill:=0;
  26. end;
  27. Function FPSigPending(var nset: sigset_t):cint;
  28. {
  29. Allows examination of pending signals. The signal mask of pending
  30. signals is set in SSet
  31. }
  32. begin
  33. FPsigpending:=do_syscall(syscall_nr_sigpending,TSysParam(@nset));
  34. end;
  35. function FPsigsuspend(const sigmask:sigset_t):cint;
  36. {
  37. Set the signal mask with Mask, and suspend the program until a signal
  38. is received.
  39. }
  40. begin
  41. FPsigsuspend:= do_syscall(syscall_nr_sigsuspend,TSysParam(@sigmask));
  42. end;
  43. {$ifndef FPC_SYS_SIGTIMEDWAIT_UNAVAILABLE}
  44. function fpsigtimedwait(const sigset:TSigSet;info:Psiginfo;timeout:Ptimespec):cint;
  45. begin
  46. FpSigTimedWait:=do_syscall(syscall_nr_sigtimedwait,
  47. Tsysparam(@sigset),
  48. Tsysparam(info),
  49. Tsysparam(timeout));
  50. end;
  51. {$endif ndef FPC_SYS_SIGTIMEDWAIT_UNAVAILABLE}
  52. Type // implementation side for now. Should move to BSD unit.
  53. ITimerVal= Record
  54. It_Interval,
  55. It_Value : TimeVal;
  56. end;
  57. Const ITimer_Real =0;
  58. ITimer_Virtual =1;
  59. ITimer_Prof =2;
  60. Function SetITimer(Which : Longint;Const value : ItimerVal; var VarOValue:ItimerVal):Longint;
  61. Begin
  62. SetItimer:=Do_Syscall(syscall_nr_setitimer,Which,TSysParam(@Value),TSysParam(@varovalue));
  63. End;
  64. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  65. Begin
  66. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,TSysParam(@value));
  67. End;
  68. Function FPalarm(Seconds: cuint):cuint;
  69. Var it,oitv : Itimerval;
  70. Begin
  71. // register struct itimerval *itp = &it;
  72. it.it_interval.tv_sec:=0;
  73. it.it_interval.tv_usec:=0;
  74. it.it_value.tv_sec:=seconds;
  75. it.it_value.tv_usec:=0;
  76. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  77. Exit(cuint(-1));
  78. if oitv.it_value.tv_usec<>0 Then
  79. Inc(oitv.it_value.tv_sec);
  80. FPAlarm:=oitv.it_value.tv_sec;
  81. End;
  82. function sigblock(mask:cuint):cint;
  83. {Depreciated, but used by pause.}
  84. var nset,oset: sigset_t;
  85. begin
  86. FPsigemptyset(nset);
  87. nset[0]:=mask;
  88. sigblock:= FPsigprocmask(SIG_BLOCK,@nset,@oset); // SIG_BLOCK=1
  89. if sigblock=0 Then
  90. sigblock:=oset[0];
  91. end;
  92. function sigpause(sigmask:cint):cint;
  93. {Depreciated, but used by pause.}
  94. var nset: sigset_t;
  95. begin
  96. FPsigemptyset(nset);
  97. nset[0]:=sigmask;
  98. sigpause:= FPsigsuspend(nset);
  99. end;
  100. function FPpause:cint;
  101. begin
  102. FPpause:=sigpause(sigblock(cuint(0)));
  103. end;
  104. function FPsleep(seconds:cuint):cuint;
  105. var time_to_sleep,time_remaining : timespec;
  106. begin
  107. {
  108. * Avoid overflow when `seconds' is huge. This assumes that
  109. * the maximum value for a time_t is >= INT_MAX.
  110. }
  111. if seconds > high(cint) Then
  112. FPsleep:= (seconds - high(cint)) + FPsleep(HIGH(cint));
  113. time_to_sleep.tv_sec := seconds;
  114. time_to_sleep.tv_nsec := 0;
  115. if (FPnanosleep(@time_to_sleep, @time_remaining) <> -1) Then
  116. Exit(0);
  117. if (fpgeterrno <> ESysEINTR) Then // EAGAIN?
  118. Exit (seconds); { best guess }
  119. FPsleep:= time_remaining.tv_sec;
  120. if (time_remaining.tv_nsec <> 0) Then
  121. inc(FPsleep);
  122. End;
  123. function FPuname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  124. Var
  125. mib : array[0..1] of cint;
  126. rval : cint;
  127. len : size_t;
  128. i : longint;
  129. oerrno : cint;
  130. procedure Doone(pz:pchar;pzsize:cint;val1,val2:cint);
  131. Begin
  132. mib[0] := val1;
  133. mib[1] := val2;
  134. len := pzsize;
  135. oerrno := fpgeterrno;
  136. if (FPsysctl(@mib, 2, pz, @len, NIL, 0) = -1) Then
  137. Begin
  138. if (fpgeterrno = ESysENOMEM) Then
  139. fpseterrno(oerrno)
  140. else
  141. rval := -1;
  142. End;
  143. pz[pzsize- 1] := #0;
  144. End;
  145. Begin
  146. rval := 0;
  147. DoOne(@name.sysname,sizeof(name.sysname),CTL_KERN,KERN_OSTYPE);
  148. DoOne(@name.nodename,sizeof(name.nodename),CTL_KERN,KERN_HOSTNAME);
  149. DoOne(@name.release,sizeof(name.release),CTL_KERN,KERN_OSRELEASE);
  150. { The version may have newlines in it, turn them into spaces. }
  151. DoOne(@name.version,sizeof(name.version),CTL_KERN,KERN_VERSION);
  152. For I:=0 to sizeof(name.sysname)-2 Do
  153. If (name.version[i]=#13) or (name.version[i]=#9) Then
  154. name.version[i]:=' ';
  155. DoOne(@name.machine,sizeof(name.machine),CTL_HW,HW_MACHINE);
  156. FPUname:=rval;
  157. end;
  158. function GetDomainName(Name:PChar; NameLen:Cint):cint; [public,alias:'FPC_SYSC_GETDOMAINNAME'];
  159. Const Mib_GetDomainName : array[0..1] of cint=(CTL_KERN,{$ifdef OpenBSD}KERN_DOMAINNAME{$ELSE}KERN_NISDOMAINNAME{$endif});
  160. VAR
  161. tsize : size_t;
  162. begin
  163. tsize := namelen;
  164. if (FPsysctl(@Mib_GetDomainname, 2, name, @tsize, NIL, 0) = -1) Then
  165. GetDomainName:=-1
  166. Else
  167. GetDomainName:=0;
  168. end;
  169. function GetHostName(Name:PChar; NameLen:Cint):cint;[public,alias:'FPC_SYSC_GETHOSTNAME'];
  170. Const Mib_GetHostName : array[0..1] of cint=(CTL_KERN,KERN_HOSTNAME);
  171. Var
  172. tsize : size_t;
  173. begin
  174. tsize := namelen;
  175. if (FPsysctl(@Mib_GetHostName, 2, name, @tsize, NIL, 0) = -1) Then
  176. GetHostName:=-1
  177. Else
  178. GetHostName:=0;
  179. End;
  180. const WAIT_ANY = -1;
  181. function FPwait(var stat_loc:cint): pid_t;
  182. {
  183. Waits until a child with PID Pid exits, or returns if it is exited already.
  184. Any resources used by the child are freed.
  185. The exit status is reported in the adress referred to by Status. It should
  186. be a longint.
  187. }
  188. begin // actually a wait4() call with 4th arg 0.
  189. FPWait:=do_syscall(syscall_nr_WaitPID,WAIT_ANY,TSysParam(@Stat_loc),0,0);
  190. end;
  191. //function FPgetpid : pid_t;
  192. // begin
  193. // FPgetpid:=do_syscall(syscall_nr_getpid);
  194. // end;
  195. function FPgetppid : pid_t;
  196. begin
  197. FPgetppid:=do_syscall(syscall_nr_getppid);
  198. end;
  199. function FPgetuid : uid_t;
  200. begin
  201. FPgetuid:=do_syscall(syscall_nr_getuid);
  202. end;
  203. function FPgeteuid : uid_t;
  204. begin
  205. FPgeteuid:=do_syscall(syscall_nr_geteuid);
  206. end;
  207. function FPgetgid : gid_t;
  208. begin
  209. FPgetgid:=do_syscall(syscall_nr_getgid);
  210. end;
  211. function FPgetegid : gid_t;
  212. begin
  213. FPgetegid:=do_syscall(syscall_nr_getegid);
  214. end;
  215. function FPsetuid(uid : uid_t): cint;
  216. begin
  217. FPsetuid:=do_syscall(syscall_nr_setuid,uid);
  218. end;
  219. function FPsetgid(gid : gid_t): cint;
  220. begin
  221. FPsetgid:=do_syscall(syscall_nr_setgid,gid);
  222. end;
  223. // type tgrparr=array[0..0] of gid_t;
  224. function FPgetgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  225. begin
  226. FPgetgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,TSysParam(@grouplist));
  227. end;
  228. function FPgetpgrp : pid_t;
  229. begin
  230. FPgetpgrp:=do_syscall(syscall_nr_getpgrp);
  231. end;
  232. function FPsetsid : pid_t;
  233. begin
  234. FPsetsid:=do_syscall(syscall_nr_setsid);
  235. end;
  236. function fpgetsid (pid:TPid): pid_t;
  237. begin
  238. fpgetsid:=do_syscall(syscall_nr_getsid,TSysParam(pid));
  239. end;
  240. Function FPumask(cmask:mode_t):mode_t;
  241. {
  242. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  243. previous value.
  244. }
  245. begin
  246. FPumask:=Do_syscall(syscall_nr_umask,cmask);
  247. end;
  248. Function FPlink(existing:pchar;newone:pchar):cint;
  249. {
  250. Proceduces a hard link from new to old.
  251. In effect, new will be the same file as old.
  252. }
  253. begin
  254. FPLink:=Do_Syscall(syscall_nr_link,TSysParam(existing),TSysParam(newone));
  255. end;
  256. Function FPmkfifo(path:pchar;mode:mode_t):cint;
  257. begin
  258. FPmkfifo:=do_syscall(syscall_nr_mkfifo,TSysParam(path),TSysParam(mode));
  259. end;
  260. Function FPchmod(path:pchar;mode:mode_t):cint;
  261. begin
  262. FPchmod:=do_syscall(syscall_nr_chmod,TSysParam(path),TSysParam(mode));
  263. end;
  264. Function FPchown(path:pchar;owner:uid_t;group:gid_t):cint;
  265. begin
  266. FPChOwn:=do_syscall(syscall_nr_chown,TSysParam(path),TSysParam(owner),TSysParam(group));
  267. end;
  268. Function FPUtime(path:pchar;times:putimbuf):cint;
  269. var tv : array[0..1] of timeval;
  270. tvp : ^timeval;
  271. begin
  272. if times=nil Then
  273. tvp:=nil
  274. else
  275. begin
  276. tv[0].tv_sec :=times^.actime;
  277. tv[1].tv_sec :=times^.modtime;
  278. tv[0].tv_usec:=0;
  279. tv[1].tv_usec:=0;
  280. tvp:=@tv;
  281. end;
  282. FPutime:=do_syscall(syscall_nr_utimes,TSysParam(path),TSysParam(tvp));
  283. end;
  284. function __pipe_call(sysnr:TSysParam):TSysResult; {$ifdef cpui386}oldfpccall{$endif} external name 'FPC_DOSYS0';
  285. Function FPpipe(var fildes : tfildes):cint;
  286. {$ifndef freebsd}
  287. begin
  288. fppipe:=do_syscall(syscall_nr_pipe,TSysParam(@fildes));
  289. end;
  290. {$else}
  291. var
  292. a, b: cInt;
  293. begin
  294. asm
  295. {$ifdef CPUi386}
  296. pushl syscall_nr_pipe
  297. call __pipe_call
  298. movl %eax, a
  299. movl %edx, b
  300. {$else}
  301. movq syscall_nr_pipe, %rdi
  302. call __pipe_call
  303. movl %eax, a
  304. movl %edx, b
  305. {$endif}
  306. end;
  307. fpPipe := a; // eax is in a, no matter if it worked or not
  308. fildes[0] := a;
  309. fildes[1] := b;
  310. end;
  311. {$endif}
  312. function FPfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  313. begin
  314. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  315. end;
  316. function FPfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  317. begin
  318. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,TSysParam(@arg));
  319. end;
  320. function FPfcntl(fildes:cint;Cmd:cint):cint;
  321. begin
  322. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  323. end;
  324. function FPexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  325. Begin
  326. FPexecve:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  327. End;
  328. function FPexecv(path:pchar;argv:ppchar):cint;
  329. Begin
  330. FPexecv:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  331. End;
  332. CONST RUSAGE_SELF = 0;
  333. RUSAGE_CHILDREN = -1;
  334. function FPgetrusage(who:cint;var ru : rusage):cint;
  335. begin
  336. FPgetrusage:=do_syscall(syscall_nr_getrusage,longint(who),TSysParam(@ru));
  337. end;
  338. function FPtimes(var buffer : tms):clock_t;
  339. var ru : rusage;
  340. t : timeval;
  341. CONST CLK_TCK=128;
  342. function CONVTCK(r:timeval):clock_t;
  343. {
  344. * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
  345. * but this would overflow if we switch to nanosec.
  346. }
  347. begin
  348. CONVTCK:=(r.tv_sec * CLK_TCK + r.tv_usec DIV (1000000 DIV CLK_TCK));
  349. end;
  350. begin
  351. if (FPgetrusage(RUSAGE_SELF, ru) < 0) Then
  352. exit(clock_t(-1));
  353. buffer.tms_utime := CONVTCK(ru.ru_utime);
  354. buffer.tms_stime := CONVTCK(ru.ru_stime);
  355. if (FPgetrusage(RUSAGE_CHILDREN, ru) < 0) Then
  356. exit(clock_t(-1));
  357. buffer.tms_cutime := CONVTCK(ru.ru_utime);
  358. buffer.tms_cstime := CONVTCK(ru.ru_stime);
  359. if do_syscall(syscall_nr_gettimeofday,TSysParam(@t),0)<>0 Then
  360. exit(clock_t(-1));
  361. FPtimes:=clock_t(CONVTCK(t));
  362. end;
  363. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  364. {
  365. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  366. have changed.
  367. }
  368. begin
  369. fpSelect:=do_syscall(syscall_nr_select,n,TSysParam(readfds),TSysParam(writefds),TSysParam(exceptfds),TSysParam(timeout));
  370. end;
  371. function fpPoll(fds: ppollfd; nfds: cuint; timeout: clong): cint;
  372. begin
  373. fpPoll:=do_syscall(syscall_nr_poll,tsysparam(fds),tsysparam(nfds),tsysparam(timeout));
  374. end;
  375. Function fpLstat(path:pchar;Info:pstat):cint;
  376. {
  377. Get all information on a link (the link itself), and return it in info.
  378. }
  379. begin
  380. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(path),TSysParam(info));
  381. end;
  382. function fpNice(N:cint):cint;
  383. {
  384. Set process priority. A positive N means a lower priority.
  385. A negative N decreases priority.
  386. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  387. }
  388. var prio : cint;
  389. begin
  390. fpseterrno(0);
  391. prio:=fpgetpriority(PRIO_PROCESS,0);
  392. if (prio=-1) and (fpgeterrno<>0) then
  393. exit(-1);
  394. fpNice:=fpSetPriority(Prio_Process,0,prio+N);
  395. end;
  396. Function fpGetPriority(Which,Who:cint):cint;
  397. {
  398. Get Priority of process, process group, or user.
  399. Which : selects what kind of priority is used.
  400. can be one of the following predefined Constants :
  401. Prio_User.
  402. Prio_PGrp.
  403. Prio_Process.
  404. Who : depending on which, this is , respectively :
  405. Uid
  406. Pid
  407. Process Group id
  408. Errors are reported in linuxerror _only_. (priority can be negative)
  409. }
  410. begin
  411. if (which<prio_process) or (which>prio_user) then
  412. begin
  413. { We can save an interrupt here }
  414. fpgetpriority:=0;
  415. fpseterrno(ESysEinval);
  416. end
  417. else
  418. begin
  419. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  420. end;
  421. end;
  422. Function fpSetPriority(Which,Who,What:cint):cint;
  423. {
  424. Set Priority of process, process group, or user.
  425. Which : selects what kind of priority is used.
  426. can be one of the following predefined Constants :
  427. Prio_User.
  428. Prio_PGrp.
  429. Prio_Process.
  430. Who : depending on value of which, this is, respectively :
  431. Uid
  432. Pid
  433. Process Group id
  434. what : A number between -20 and 20. -20 is most favorable, 20 least.
  435. 0 is the default.
  436. }
  437. begin
  438. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  439. fpseterrno(ESyseinval) { We can save an interrupt here }
  440. else
  441. begin
  442. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  443. end;
  444. end;
  445. Function fpSymlink(oldname,newname:pchar):cint;
  446. {
  447. We need this for erase
  448. }
  449. begin
  450. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  451. end;
  452. function Fppread(fd: cint; buf: pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PREAD'];
  453. begin
  454. {$ifdef CPU64}
  455. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  456. {$else}
  457. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,
  458. {$ifdef 64bitfs}
  459. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  460. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  461. {$else}
  462. {$ifdef FPC_BIG_ENDIAN} 0,lo(offset){$endif}
  463. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),0{$endif}
  464. {$endif}
  465. );
  466. {$endif}
  467. end;
  468. function Fppwrite(fd: cint;buf:pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PWRITE'];
  469. begin
  470. {$ifdef CPU64}
  471. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  472. {$else}
  473. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,
  474. // ,0 = possible alignment here.
  475. {$ifdef 64bitfs}
  476. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  477. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  478. {$else}
  479. {$ifdef FPC_BIG_ENDIAN} 0,lo(offset){$endif}
  480. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),0{$endif}
  481. {$endif}
  482. );
  483. {$endif}
  484. end;
  485. function Fpreadv(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_READV'];
  486. begin
  487. Fpreadv:=do_syscall(syscall_nr_readv,Fd,TSysParam(iov),iovcnt);
  488. end;
  489. function Fpwritev(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_WRITEV'];
  490. begin
  491. Fpwritev:=do_syscall(syscall_nr_writev,Fd,TSysParam(iov),iovcnt);
  492. end;