bunxsysc.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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,longint(@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,longint(@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,Longint(@Value),longint(@varovalue));
  54. End;
  55. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  56. Begin
  57. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,Longint(@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,longint(@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,longint(@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,longint(existing),longint(newone));
  242. end;
  243. Function FPmkfifo(path:pchar;mode:mode_t):cint;
  244. begin
  245. FPmkfifo:=do_syscall(syscall_nr_mkfifo,longint(path),longint(mode));
  246. end;
  247. Function FPchmod(path:pchar;mode:mode_t):cint;
  248. begin
  249. FPchmod:=do_syscall(syscall_nr_chmod,longint(path),longint(mode));
  250. end;
  251. Function FPchown(path:pchar;owner:uid_t;group:gid_t):cint;
  252. begin
  253. FPChOwn:=do_syscall(syscall_nr_chown,longint(path),longint(owner),longint(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,longint(path),longint(tvp));
  270. end;
  271. Function FPpipe(var fildes : tfildes):cint;
  272. begin
  273. FPpipe:=do_syscall(syscall_nr_pipe,longint(@fildes));
  274. end;
  275. function FPfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  276. begin
  277. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  278. end;
  279. function FPfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  280. begin
  281. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,longint(@arg));
  282. end;
  283. function FPfcntl(fildes:cint;Cmd:cint):cint;
  284. begin
  285. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  286. end;
  287. function FPexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  288. Begin
  289. FPexecve:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  290. End;
  291. function FPexecv(path:pchar;argv:ppchar):cint;
  292. Begin
  293. FPexecv:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  294. End;
  295. CONST RUSAGE_SELF = 0;
  296. RUSAGE_CHILDREN = -1;
  297. function FPgetrusage(who:cint;var ru : rusage):cint;
  298. begin
  299. FPgetrusage:=do_syscall(syscall_nr_getrusage,longint(who),longint(@ru));
  300. end;
  301. function FPtimes(var buffer : tms):clock_t;
  302. var ru : rusage;
  303. t : timeval;
  304. CONST CLK_TCK=128;
  305. function CONVTCK(r:timeval):clock_t;
  306. {
  307. * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
  308. * but this would overflow if we switch to nanosec.
  309. }
  310. begin
  311. CONVTCK:=(r.tv_sec * CLK_TCK + r.tv_usec DIV (1000000 DIV CLK_TCK));
  312. end;
  313. begin
  314. if (FPgetrusage(RUSAGE_SELF, ru) < 0) Then
  315. exit(clock_t(-1));
  316. buffer.tms_utime := CONVTCK(ru.ru_utime);
  317. buffer.tms_stime := CONVTCK(ru.ru_stime);
  318. if (FPgetrusage(RUSAGE_CHILDREN, ru) < 0) Then
  319. exit(clock_t(-1));
  320. buffer.tms_cutime := CONVTCK(ru.ru_utime);
  321. buffer.tms_cstime := CONVTCK(ru.ru_stime);
  322. if do_syscall(syscall_nr_gettimeofday,longint(@t),0)<>0 Then
  323. exit(clock_t(-1));
  324. FPtimes:=clock_t(CONVTCK(t));
  325. end;
  326. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  327. {
  328. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  329. have changed.
  330. }
  331. begin
  332. fpSelect:=do_syscall(syscall_nr_select,n,longint(readfds),longint(writefds),longint(exceptfds),longint(timeout));
  333. end;
  334. Function fpLstat(path:pchar;Info:pstat):cint;
  335. {
  336. Get all information on a link (the link itself), and return it in info.
  337. }
  338. begin
  339. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(path),TSysParam(info));
  340. end;
  341. Function fpLstat(Filename: ansistring;Info:pstat):cint;
  342. {
  343. Get all information on a link (the link itself), and return it in info.
  344. }
  345. begin
  346. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(pchar(filename)),TSysParam(info));
  347. end;
  348. function fpNice(N:cint):cint;
  349. {
  350. Set process priority. A positive N means a lower priority.
  351. A negative N decreases priority.
  352. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  353. }
  354. var prio : cint;
  355. begin
  356. fpseterrno(0);
  357. prio:=fpgetpriority(PRIO_PROCESS,0);
  358. if (prio=-1) and (fpgeterrno<>0) then
  359. exit(-1);
  360. fpNice:=fpSetPriority(Prio_Process,0,prio+N);
  361. end;
  362. Function fpGetPriority(Which,Who:cint):cint;
  363. {
  364. Get Priority of process, process group, or user.
  365. Which : selects what kind of priority is used.
  366. can be one of the following predefined Constants :
  367. Prio_User.
  368. Prio_PGrp.
  369. Prio_Process.
  370. Who : depending on which, this is , respectively :
  371. Uid
  372. Pid
  373. Process Group id
  374. Errors are reported in linuxerror _only_. (priority can be negative)
  375. }
  376. begin
  377. if (which<prio_process) or (which>prio_user) then
  378. begin
  379. { We can save an interrupt here }
  380. fpgetpriority:=0;
  381. fpseterrno(ESysEinval);
  382. end
  383. else
  384. begin
  385. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  386. end;
  387. end;
  388. Function fpSetPriority(Which,Who,What:cint):cint;
  389. {
  390. Set Priority of process, process group, or user.
  391. Which : selects what kind of priority is used.
  392. can be one of the following predefined Constants :
  393. Prio_User.
  394. Prio_PGrp.
  395. Prio_Process.
  396. Who : depending on value of which, this is, respectively :
  397. Uid
  398. Pid
  399. Process Group id
  400. what : A number between -20 and 20. -20 is most favorable, 20 least.
  401. 0 is the default.
  402. }
  403. begin
  404. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  405. fpseterrno(ESyseinval) { We can save an interrupt here }
  406. else
  407. begin
  408. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  409. end;
  410. end;
  411. Function fpSymlink(oldname,newname:pchar):cint;
  412. {
  413. We need this for erase
  414. }
  415. begin
  416. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  417. end;
  418. function Fppread(fd: cint; buf: pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PREAD'];
  419. begin
  420. {$ifdef CPU64}
  421. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  422. {$else}
  423. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,
  424. {$ifdef 64bitfs}
  425. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  426. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  427. {$else}
  428. {$ifdef FPC_BIG_ENDIAN} 0,lo(offset){$endif}
  429. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),0{$endif}
  430. {$endif}
  431. );
  432. {$endif}
  433. end;
  434. function Fppwrite(fd: cint;buf:pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PWRITE'];
  435. begin
  436. {$ifdef CPU64}
  437. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  438. {$else}
  439. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,
  440. // ,0 = possible alignment here.
  441. {$ifdef 64bitfs}
  442. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  443. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  444. {$else}
  445. {$ifdef FPC_BIG_ENDIAN} 0,lo(offset){$endif}
  446. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),0{$endif}
  447. {$endif}
  448. );
  449. {$endif}
  450. end;
  451. function Fpreadv(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_READV'];
  452. begin
  453. Fpreadv:=do_syscall(syscall_nr_readv,Fd,TSysParam(iov),iovcnt);
  454. end;
  455. function Fpwritev(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_WRITEV'];
  456. begin
  457. Fpwritev:=do_syscall(syscall_nr_writev,Fd,TSysParam(iov),iovcnt);
  458. end;