bunxsysc.inc 15 KB

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