bunxsysc.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. {$ifdef OPENBSD}
  42. { OpenBSD sigsuspend syscall wants a simple int as arg }
  43. FPsigsuspend:= do_syscall(syscall_nr_sigsuspend,TSysParam(sigmask[0]));
  44. {$else}
  45. FPsigsuspend:= do_syscall(syscall_nr_sigsuspend,TSysParam(@sigmask));
  46. {$endif}
  47. end;
  48. {$ifndef FPC_SYS_SIGTIMEDWAIT_UNAVAILABLE}
  49. function fpsigtimedwait(const sigset:TSigSet;info:Psiginfo;timeout:Ptimespec):cint;
  50. begin
  51. FpSigTimedWait:=do_syscall(syscall_nr_sigtimedwait,
  52. Tsysparam(@sigset),
  53. Tsysparam(info),
  54. Tsysparam(timeout));
  55. end;
  56. {$endif ndef FPC_SYS_SIGTIMEDWAIT_UNAVAILABLE}
  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,TSysParam(@Value),TSysParam(@varovalue));
  68. End;
  69. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  70. Begin
  71. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,TSysParam(@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(cuint(-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,TSysParam(@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,TSysParam(@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 fpgetsid (pid:TPid): pid_t;
  242. begin
  243. fpgetsid:=do_syscall(syscall_nr_getsid,TSysParam(pid));
  244. end;
  245. Function FPumask(cmask:mode_t):mode_t;
  246. {
  247. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  248. previous value.
  249. }
  250. begin
  251. FPumask:=Do_syscall(syscall_nr_umask,cmask);
  252. end;
  253. Function FPlink(existing:pchar;newone:pchar):cint;
  254. {
  255. Proceduces a hard link from new to old.
  256. In effect, new will be the same file as old.
  257. }
  258. begin
  259. FPLink:=Do_Syscall(syscall_nr_link,TSysParam(existing),TSysParam(newone));
  260. end;
  261. Function FPmkfifo(path:pchar;mode:mode_t):cint;
  262. begin
  263. FPmkfifo:=do_syscall(syscall_nr_mkfifo,TSysParam(path),TSysParam(mode));
  264. end;
  265. Function FPchmod(path:pchar;mode:mode_t):cint;
  266. begin
  267. FPchmod:=do_syscall(syscall_nr_chmod,TSysParam(path),TSysParam(mode));
  268. end;
  269. Function FPchown(path:pchar;owner:uid_t;group:gid_t):cint;
  270. begin
  271. FPChOwn:=do_syscall(syscall_nr_chown,TSysParam(path),TSysParam(owner),TSysParam(group));
  272. end;
  273. Function FPUtime(path:pchar;times:putimbuf):cint;
  274. var tv : array[0..1] of timeval;
  275. tvp : ^timeval;
  276. begin
  277. if times=nil Then
  278. tvp:=nil
  279. else
  280. begin
  281. tv[0].tv_sec :=times^.actime;
  282. tv[1].tv_sec :=times^.modtime;
  283. tv[0].tv_usec:=0;
  284. tv[1].tv_usec:=0;
  285. tvp:=@tv;
  286. end;
  287. FPutime:=do_syscall(syscall_nr_utimes,TSysParam(path),TSysParam(tvp));
  288. end;
  289. function __pipe_call(sysnr:TSysParam):TSysResult; {$ifdef cpui386}oldfpccall{$endif} external name 'FPC_DOSYS0';
  290. {$if defined(freebsd) or defined (dragonfly)}
  291. {$define PIPE_RESULT_IN_EAX_AND_EDX}
  292. {$endif}
  293. Function FPpipe(var fildes : tfildes):cint;
  294. {$ifndef PIPE_RESULT_IN_EAX_AND_EDX}
  295. begin
  296. fppipe:=do_syscall(syscall_nr_pipe,TSysParam(@fildes));
  297. end;
  298. {$else}
  299. var
  300. a, b: cInt;
  301. begin
  302. asm
  303. {$ifdef CPUi386}
  304. pushl syscall_nr_pipe
  305. call __pipe_call
  306. movl %eax, a
  307. movl %edx, b
  308. {$else}
  309. movq syscall_nr_pipe, %rdi
  310. call __pipe_call
  311. movl %eax, a
  312. movl %edx, b
  313. {$endif}
  314. end;
  315. fpPipe := a; // eax is in a, no matter if it worked or not
  316. fildes[0] := a;
  317. fildes[1] := b;
  318. end;
  319. {$endif}
  320. function FPfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  321. begin
  322. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  323. end;
  324. function FPfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  325. begin
  326. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,TSysParam(@arg));
  327. end;
  328. function FPfcntl(fildes:cint;Cmd:cint):cint;
  329. begin
  330. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  331. end;
  332. function FPexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  333. Begin
  334. FPexecve:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  335. End;
  336. function FPexecv(path:pchar;argv:ppchar):cint;
  337. Begin
  338. FPexecv:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  339. End;
  340. CONST RUSAGE_SELF = 0;
  341. RUSAGE_CHILDREN = -1;
  342. function FPgetrusage(who:cint;var ru : rusage):cint;
  343. begin
  344. FPgetrusage:=do_syscall(syscall_nr_getrusage,longint(who),TSysParam(@ru));
  345. end;
  346. function FPtimes(var buffer : tms):clock_t;
  347. var ru : rusage;
  348. t : timeval;
  349. CONST CLK_TCK=128;
  350. function CONVTCK(r:timeval):clock_t;
  351. {
  352. * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
  353. * but this would overflow if we switch to nanosec.
  354. }
  355. begin
  356. CONVTCK:=(r.tv_sec * CLK_TCK + r.tv_usec DIV (1000000 DIV CLK_TCK));
  357. end;
  358. begin
  359. if (FPgetrusage(RUSAGE_SELF, ru) < 0) Then
  360. exit(clock_t(-1));
  361. buffer.tms_utime := CONVTCK(ru.ru_utime);
  362. buffer.tms_stime := CONVTCK(ru.ru_stime);
  363. if (FPgetrusage(RUSAGE_CHILDREN, ru) < 0) Then
  364. exit(clock_t(-1));
  365. buffer.tms_cutime := CONVTCK(ru.ru_utime);
  366. buffer.tms_cstime := CONVTCK(ru.ru_stime);
  367. if do_syscall(syscall_nr_gettimeofday,TSysParam(@t),0)<>0 Then
  368. exit(clock_t(-1));
  369. FPtimes:=clock_t(CONVTCK(t));
  370. end;
  371. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  372. {
  373. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  374. have changed.
  375. }
  376. begin
  377. fpSelect:=do_syscall(syscall_nr_select,n,TSysParam(readfds),TSysParam(writefds),TSysParam(exceptfds),TSysParam(timeout));
  378. end;
  379. function fpPoll(fds: ppollfd; nfds: cuint; timeout: clong): cint;
  380. begin
  381. fpPoll:=do_syscall(syscall_nr_poll,tsysparam(fds),tsysparam(nfds),tsysparam(timeout));
  382. end;
  383. Function fpLstat(path:pchar;Info:pstat):cint;
  384. {
  385. Get all information on a link (the link itself), and return it in info.
  386. }
  387. begin
  388. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(path),TSysParam(info));
  389. end;
  390. function fpNice(N:cint):cint;
  391. {
  392. Set process priority. A positive N means a lower priority.
  393. A negative N decreases priority.
  394. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  395. }
  396. var prio : cint;
  397. begin
  398. fpseterrno(0);
  399. prio:=fpgetpriority(PRIO_PROCESS,0);
  400. if (prio=-1) and (fpgeterrno<>0) then
  401. exit(-1);
  402. fpNice:=fpSetPriority(Prio_Process,0,prio+N);
  403. end;
  404. Function fpGetPriority(Which,Who:cint):cint;
  405. {
  406. Get Priority of process, process group, or user.
  407. Which : selects what kind of priority is used.
  408. can be one of the following predefined Constants :
  409. Prio_User.
  410. Prio_PGrp.
  411. Prio_Process.
  412. Who : depending on which, this is , respectively :
  413. Uid
  414. Pid
  415. Process Group id
  416. Errors are reported in linuxerror _only_. (priority can be negative)
  417. }
  418. begin
  419. if (which<prio_process) or (which>prio_user) then
  420. begin
  421. { We can save an interrupt here }
  422. fpgetpriority:=0;
  423. fpseterrno(ESysEinval);
  424. end
  425. else
  426. begin
  427. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  428. end;
  429. end;
  430. Function fpSetPriority(Which,Who,What:cint):cint;
  431. {
  432. Set Priority of process, process group, or user.
  433. Which : selects what kind of priority is used.
  434. can be one of the following predefined Constants :
  435. Prio_User.
  436. Prio_PGrp.
  437. Prio_Process.
  438. Who : depending on value of which, this is, respectively :
  439. Uid
  440. Pid
  441. Process Group id
  442. what : A number between -20 and 20. -20 is most favorable, 20 least.
  443. 0 is the default.
  444. }
  445. begin
  446. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  447. fpseterrno(ESyseinval) { We can save an interrupt here }
  448. else
  449. begin
  450. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  451. end;
  452. end;
  453. Function fpSymlink(oldname,newname:pchar):cint;
  454. {
  455. We need this for erase
  456. }
  457. begin
  458. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  459. end;
  460. function Fppread(fd: cint; buf: pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PREAD'];
  461. begin
  462. {$ifdef CPU64}
  463. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  464. {$else}
  465. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,
  466. {$ifdef 64bitfs}
  467. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  468. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  469. {$else}
  470. {$ifdef FPC_BIG_ENDIAN} 0,lo(offset){$endif}
  471. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),0{$endif}
  472. {$endif}
  473. );
  474. {$endif}
  475. end;
  476. function Fppwrite(fd: cint;buf:pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PWRITE'];
  477. begin
  478. {$ifdef CPU64}
  479. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  480. {$else}
  481. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,
  482. // ,0 = possible alignment here.
  483. {$ifdef 64bitfs}
  484. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  485. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  486. {$else}
  487. {$ifdef FPC_BIG_ENDIAN} 0,lo(offset){$endif}
  488. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),0{$endif}
  489. {$endif}
  490. );
  491. {$endif}
  492. end;
  493. function Fpreadv(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_READV'];
  494. begin
  495. Fpreadv:=do_syscall(syscall_nr_readv,Fd,TSysParam(iov),iovcnt);
  496. end;
  497. function Fpwritev(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_WRITEV'];
  498. begin
  499. Fpwritev:=do_syscall(syscall_nr_writev,Fd,TSysParam(iov),iovcnt);
  500. end;