bunxsysc.inc 15 KB

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