bunxsysc.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 baseunix 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,TSysParam(pid),TSysParam(sig));
  24. // if kill<0 THEN
  25. // Kill:=0;
  26. end;
  27. Function fpSigPending(var nset: TSigSet):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_rt_sigpending,TSysParam(@nset));
  34. end;
  35. function fpsigsuspend(const sigmask:TSigSet):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_rt_sigsuspend,TSysParam(@sigmask),TSysParam(8));
  42. end;
  43. Type
  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. retval : cuint;
  62. Begin
  63. // register struct itimerval *itp = &it;
  64. it.it_interval.tv_sec:=0;
  65. it.it_interval.tv_usec:=0;
  66. it.it_value.tv_usec:=0;
  67. it.it_value.tv_sec:=seconds;
  68. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  69. Exit(0); // different from *BSD!
  70. retval:= oitv.it_value.tv_usec;
  71. if retval<>0 Then
  72. inc(retval);
  73. fpAlarm:=retval;
  74. End;
  75. // The following versions are for internal use _ONLY_
  76. // This because it works for the first 32 signals _ONLY_, but that
  77. // is enough since they are depreciated, and for legacy applications
  78. // anyway.
  79. function sigblock(mask:cuint):cint;
  80. var nset,oset: TSigSet;
  81. begin
  82. fpsigemptyset(nset);
  83. // fpsigaddset(nset,mask); needs _mask_
  84. nset[0]:=mask;
  85. sigblock:= fpsigprocmask(SIG_BLOCK,@nset,@oset); // SIG_BLOCK=1
  86. if sigblock=0 Then
  87. sigblock:=oset[0];
  88. end;
  89. function sigpause(sigmask:cint):cint;
  90. var nset: TSigSet;
  91. begin
  92. fpsigemptyset(nset);
  93. nset[0]:=sigmask;
  94. sigpause:= fpsigsuspend(nset);
  95. end;
  96. function fppause:cint;
  97. begin
  98. fppause:=sigpause(sigblock(cuint(0)));
  99. end;
  100. function fpsleep(seconds:cuint):cuint;
  101. {see comments in libc}
  102. var time_to_sleep,time_remaining : timespec;
  103. nset,oset : TSigSet;
  104. oerrno : cint;
  105. oact : sigactionrec;
  106. begin
  107. time_to_sleep.tv_sec := seconds;
  108. time_to_sleep.tv_nsec := 0;
  109. fpsigemptyset(nset);
  110. fpsigaddset (nset,SIGCHLD);
  111. if fpsigprocmask(SIG_BLOCK,@nset,@oset)=-1 Then
  112. exit(cuint(-1));
  113. if fpsigismember(oset,SIGCHLD)<>0 Then
  114. Begin
  115. fpsigemptyset(nset);
  116. fpsigaddset (nset,SIGCHLD);
  117. if fpsigaction(SIGCHLD,NIL,@oact)<0 Then
  118. begin
  119. oerrno:=fpgeterrno;
  120. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  121. fpseterrno(oerrno);
  122. exit(cuint(-1));
  123. End;
  124. if oact.sa_handler=SigActionhandler(SIG_IGN) Then
  125. Begin
  126. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining);
  127. oerrno:=fpgeterrno;
  128. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  129. fpseterrno(oerrno);
  130. End
  131. Else
  132. Begin
  133. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  134. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining)
  135. End;
  136. end
  137. else
  138. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining);
  139. if fpsleep<>0 Then
  140. if time_remaining.tv_nsec>=500000000 Then
  141. inc(fpsleep);
  142. End;
  143. function fpuname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  144. begin
  145. fpuname:=Do_Syscall(syscall_nr_uname,TSysParam(@name));
  146. end;
  147. Function fpGetDomainName(Name:PChar; NameLen:size_t):cint;
  148. Var
  149. srec : utsname;
  150. tsize : size_t;
  151. Begin
  152. if fpuname(srec)<0 Then
  153. exit(-1);
  154. tsize:=strlen(@srec.domain[0]);
  155. if tsize>(namelen-1) Then
  156. tsize:=namelen-1;
  157. move(srec.domain[0],name[0],tsize);
  158. name[namelen-1]:=#0;
  159. fpgetDomainName:=0;
  160. End;
  161. function fpGetHostName(Name:PChar; NameLen:size_t):cint;
  162. Var
  163. srec : utsname;
  164. tsize : size_t;
  165. begin
  166. if fpuname(srec)<0 Then
  167. exit(-1);
  168. tsize:=strlen(@srec.nodename[0]);
  169. if tsize>(namelen-1) Then
  170. tsize:=namelen-1;
  171. move(srec.nodename[0],name[0],tsize);
  172. name[namelen-1]:=#0;
  173. fpgethostName:=0;
  174. End;
  175. const WAIT_ANY = -1;
  176. function fpwait(var stat_loc:cint): pid_t;
  177. {
  178. Waits until a child with PID Pid exits, or returns if it is exited already.
  179. Any resources used by the child are freed.
  180. The exit status is reported in the adress referred to by Status. It should
  181. be a longint.
  182. }
  183. begin // actually a wait4() call with 4th arg 0.
  184. fpWait:=do_syscall(syscall_nr_Wait4,WAIT_ANY,TSysParam(@Stat_loc),0,0);
  185. end;
  186. //function fpgetpid : pid_t;
  187. // begin
  188. // fpgetpid:=do_syscall(syscall_nr_getpid);
  189. // end;
  190. function fpgetppid : pid_t;
  191. begin
  192. fpgetppid:=do_syscall(syscall_nr_getppid);
  193. end;
  194. function fpgetuid : uid_t;
  195. begin
  196. fpgetuid:=do_syscall(syscall_nr_getuid);
  197. end;
  198. function fpgeteuid : uid_t;
  199. begin
  200. fpgeteuid:=do_syscall(syscall_nr_geteuid);
  201. end;
  202. function fpgetgid : gid_t;
  203. begin
  204. fpgetgid:=do_syscall(syscall_nr_getgid);
  205. end;
  206. function fpgetegid : gid_t;
  207. begin
  208. fpgetegid:=do_syscall(syscall_nr_getegid);
  209. end;
  210. function fpsetuid(uid : uid_t): cint;
  211. begin
  212. fpsetuid:=do_syscall(syscall_nr_setuid,uid);
  213. end;
  214. function fpsetgid(gid : gid_t): cint;
  215. begin
  216. fpsetgid:=do_syscall(syscall_nr_setgid,gid);
  217. end;
  218. // type tgrparr=array[0..0] of gid_t;
  219. function fpgetgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  220. begin
  221. fpgetgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,TSysParam(@grouplist));
  222. end;
  223. function fpgetpgrp : pid_t;
  224. begin
  225. fpgetpgrp:=do_syscall(syscall_nr_getpgrp);
  226. end;
  227. function fpsetsid : pid_t;
  228. begin
  229. fpsetsid:=do_syscall(syscall_nr_setsid);
  230. end;
  231. Function fpumask(cmask:mode_t):mode_t;
  232. {
  233. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  234. previous value.
  235. }
  236. begin
  237. fpumask:=Do_syscall(syscall_nr_umask,cmask);
  238. end;
  239. Function fplink(existing:pchar;newone:pchar):cint;
  240. {
  241. Proceduces a hard link from new to old.
  242. In effect, new will be the same file as old.
  243. }
  244. begin
  245. fpLink:=Do_Syscall(syscall_nr_link,TSysParam(existing),TSysParam(newone));
  246. end;
  247. Function fpmkfifo(path:pchar;mode:mode_t):cint;
  248. begin
  249. fpmkfifo:=do_syscall(syscall_nr_mknod,TSysParam(path),TSysParam(mode or S_IFIFO),TSysParam(0));
  250. end;
  251. Function fpchmod(path:pchar;mode:mode_t):cint;
  252. begin
  253. fpchmod:=do_syscall(syscall_nr_chmod,TSysParam(path),TSysParam(mode));
  254. end;
  255. Function fpchown(path:pchar;owner:uid_t;group:gid_t):cint;
  256. begin
  257. fpChOwn:=do_syscall(syscall_nr_chown,TSysParam(path),TSysParam(owner),TSysParam(group));
  258. end;
  259. {$ifndef NO_SYSCALL_UTIME}
  260. Function fpUtime(path:pchar;times:putimbuf):cint;
  261. begin
  262. fputime:=do_syscall(syscall_nr_utime,TSysParam(path),TSysParam(times));
  263. end;
  264. {$else}
  265. Function fpUtime(path:pchar;times:putimbuf):cint;
  266. var
  267. tva: Array[0..1] of timeval;
  268. begin
  269. tva[0].tv_sec := times^.actime;
  270. tva[0].tv_usec := 0;
  271. tva[1].tv_sec := times^.modtime;
  272. tva[1].tv_usec := 0;
  273. fputime:=do_syscall(syscall_nr_utimes,TSysParam(path),TSysParam(@tva));
  274. end;
  275. {$endif}
  276. {$ifndef FPC_BASEUNIX_HAS_FPPIPE}
  277. Function fppipe(var fildes : tfildes):cint;
  278. begin
  279. fppipe:=do_syscall(syscall_nr_pipe,TSysParam(@fildes));
  280. end;
  281. {$endif FPC_BASEUNIX_HAS_FPPIPE}
  282. function fpfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  283. begin
  284. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  285. end;
  286. function fpfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  287. begin
  288. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,TSysParam(@arg));
  289. end;
  290. function fpfcntl(fildes:cint;Cmd:cint):cint;
  291. begin
  292. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  293. end;
  294. function fpexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  295. Begin
  296. fpexecve:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  297. End;
  298. function fpexecv(path:pchar;argv:ppchar):cint;
  299. Begin
  300. fpexecv:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  301. End;
  302. function fptimes(var buffer : tms):clock_t;
  303. begin
  304. fptimes:=Do_syscall(syscall_nr_times,TSysParam(@buffer));
  305. end;
  306. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  307. {
  308. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  309. have changed.
  310. }
  311. begin
  312. {$ifdef cpux86_64}
  313. {$define bunxfunc_fpselect_implemented}
  314. fpSelect:=do_syscall(syscall_nr_select,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  315. {$else}
  316. {$define bunxfunc_fpselect_implemented}
  317. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  318. {$endif}
  319. {$ifndef bunxfunc_fpselect_implemented}
  320. {$error Implement fpselect}
  321. {$endif bunxfunc_fpselect_implemented}
  322. end;
  323. function fpPoll(fds: ppollfd; nfds: cuint; timeout: clong): cint;
  324. begin
  325. fpPoll:=do_syscall(syscall_nr_poll,tsysparam(fds),tsysparam(nfds),tsysparam(timeout));
  326. end;
  327. Function fpLstat(path:pchar;Info:pstat):cint;
  328. {
  329. Get all information on a link (the link itself), and return it in info.
  330. }
  331. begin
  332. fpLStat:=do_syscall(
  333. {$ifdef cpu64}
  334. syscall_nr_lstat,
  335. {$else}
  336. syscall_nr_lstat64,
  337. {$endif}
  338. TSysParam(path),TSysParam(info));
  339. end;
  340. function fpNice(N:cint):cint;
  341. {
  342. Set process priority. A positive N means a lower priority.
  343. A negative N increases priority.
  344. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  345. }
  346. {$ifdef cpux86_64}
  347. var
  348. oldprio : cint;
  349. {$endif}
  350. begin
  351. {$ifdef cpux86_64}
  352. oldprio:=fpGetPriority(Prio_Process,0);
  353. fpNice:=fpSetPriority(Prio_Process,0,oldprio+N);
  354. if fpNice=0 then
  355. fpNice:=fpGetPriority(Prio_Process,0);
  356. {$else}
  357. fpNice:=do_syscall(Syscall_nr_nice,N);
  358. {$endif}
  359. end;
  360. Function fpGetPriority(Which,Who:cint):cint;
  361. {
  362. Get Priority of process, process group, or user.
  363. Which : selects what kind of priority is used.
  364. can be one of the following predefined Constants :
  365. Prio_User.
  366. Prio_PGrp.
  367. Prio_Process.
  368. Who : depending on which, this is , respectively :
  369. Uid
  370. Pid
  371. Process Group id
  372. Errors are reported in linuxerror _only_. (priority can be negative)
  373. }
  374. begin
  375. if (which<prio_process) or (which>prio_user) then
  376. begin
  377. { We can save an interrupt here }
  378. fpgetpriority:=-1;
  379. fpsetErrno(ESysEinval);
  380. end
  381. else
  382. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  383. end;
  384. Function fpSetPriority(Which,Who,What:cint):cint;
  385. {
  386. Set 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 value of which, this is, respectively :
  393. Uid
  394. Pid
  395. Process Group id
  396. what : A number between -20 and 20. -20 is most favorable, 20 least.
  397. 0 is the default.
  398. }
  399. begin
  400. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  401. fpseterrno(ESyseinval) { We can save an interrupt here }
  402. else
  403. begin
  404. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  405. end;
  406. end;
  407. Function fpSymlink(oldname,newname:pchar):cint;
  408. {
  409. We need this for erase
  410. }
  411. begin
  412. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  413. end;
  414. function Fppread(fd: cint; buf: pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PREAD'];
  415. begin
  416. {$ifdef CPU64}
  417. Fppread:=do_syscall(syscall_nr_pread64,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  418. {$else}
  419. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,
  420. {$ifdef FPC_ABI_EABI} 0, { align parameters as required with dummy } {$endif FPC_ABI_EABI}
  421. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  422. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  423. );
  424. {$endif}
  425. end;
  426. function Fppwrite(fd: cint;buf:pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PWRITE'];
  427. begin
  428. {$ifdef CPU64}
  429. Fppwrite:=do_syscall(syscall_nr_pwrite64,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  430. {$else}
  431. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,
  432. {$ifdef FPC_ABI_EABI} 0, { align parameters as required with dummy } {$endif FPC_ABI_EABI}
  433. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  434. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  435. );
  436. {$endif}
  437. end;
  438. function Fpreadv(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_READV'];
  439. begin
  440. Fpreadv:=do_syscall(syscall_nr_readv,Fd,TSysParam(iov),iovcnt);
  441. end;
  442. function Fpwritev(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_WRITEV'];
  443. begin
  444. Fpwritev:=do_syscall(syscall_nr_writev,Fd,TSysParam(iov),iovcnt);
  445. end;