bunxsysc.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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));
  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. Function fpUtime(path:pchar;times:putimbuf):cint;
  260. begin
  261. fputime:=do_syscall(syscall_nr_utime,TSysParam(path),TSysParam(times));
  262. end;
  263. {$ifndef FPC_BASEUNIX_HAS_FPPIPE}
  264. Function fppipe(var fildes : tfildes):cint;
  265. begin
  266. fppipe:=do_syscall(syscall_nr_pipe,TSysParam(@fildes));
  267. end;
  268. {$endif FPC_BASEUNIX_HAS_FPPIPE}
  269. function fpfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  270. begin
  271. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  272. end;
  273. function fpfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  274. begin
  275. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,TSysParam(@arg));
  276. end;
  277. function fpfcntl(fildes:cint;Cmd:cint):cint;
  278. begin
  279. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  280. end;
  281. function fpexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  282. Begin
  283. fpexecve:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  284. End;
  285. function fpexecv(path:pchar;argv:ppchar):cint;
  286. Begin
  287. fpexecv:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  288. End;
  289. function fptimes(var buffer : tms):clock_t;
  290. begin
  291. fptimes:=Do_syscall(syscall_nr_times,TSysParam(@buffer));
  292. end;
  293. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  294. {
  295. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  296. have changed.
  297. }
  298. begin
  299. {$ifdef cpux86_64}
  300. {$define bunxfunc_fpselect_implemented}
  301. fpSelect:=do_syscall(syscall_nr_select,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  302. {$else}
  303. {$define bunxfunc_fpselect_implemented}
  304. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  305. {$endif}
  306. {$ifndef bunxfunc_fpselect_implemented}
  307. {$error Implement fpselect}
  308. {$endif bunxfunc_fpselect_implemented}
  309. end;
  310. Function fpLstat(path:pchar;Info:pstat):cint;
  311. {
  312. Get all information on a link (the link itself), and return it in info.
  313. }
  314. begin
  315. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(path),TSysParam(info));
  316. end;
  317. Function fpLstat(Filename: ansistring;Info:pstat):cint;
  318. {
  319. Get all information on a link (the link itself), and return it in info.
  320. }
  321. begin
  322. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(pchar(filename)),TSysParam(info));
  323. end;
  324. function fpNice(N:cint):cint;
  325. {
  326. Set process priority. A positive N means a lower priority.
  327. A negative N increases priority.
  328. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  329. }
  330. {$ifdef cpux86_64}
  331. var
  332. oldprio : cint;
  333. {$endif}
  334. begin
  335. {$ifdef cpux86_64}
  336. oldprio:=fpGetPriority(Prio_Process,0);
  337. fpNice:=fpSetPriority(Prio_Process,0,oldprio+N);
  338. if fpNice=0 then
  339. fpNice:=fpGetPriority(Prio_Process,0);
  340. {$else}
  341. fpNice:=do_syscall(Syscall_nr_nice,N);
  342. {$endif}
  343. end;
  344. Function fpGetPriority(Which,Who:cint):cint;
  345. {
  346. Get Priority of process, process group, or user.
  347. Which : selects what kind of priority is used.
  348. can be one of the following predefined Constants :
  349. Prio_User.
  350. Prio_PGrp.
  351. Prio_Process.
  352. Who : depending on which, this is , respectively :
  353. Uid
  354. Pid
  355. Process Group id
  356. Errors are reported in linuxerror _only_. (priority can be negative)
  357. }
  358. begin
  359. if (which<prio_process) or (which>prio_user) then
  360. begin
  361. { We can save an interrupt here }
  362. fpgetpriority:=-1;
  363. fpsetErrno(ESysEinval);
  364. end
  365. else
  366. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  367. end;
  368. Function fpSetPriority(Which,Who,What:cint):cint;
  369. {
  370. Set Priority of process, process group, or user.
  371. Which : selects what kind of priority is used.
  372. can be one of the following predefined Constants :
  373. Prio_User.
  374. Prio_PGrp.
  375. Prio_Process.
  376. Who : depending on value of which, this is, respectively :
  377. Uid
  378. Pid
  379. Process Group id
  380. what : A number between -20 and 20. -20 is most favorable, 20 least.
  381. 0 is the default.
  382. }
  383. begin
  384. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  385. fpseterrno(ESyseinval) { We can save an interrupt here }
  386. else
  387. begin
  388. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  389. end;
  390. end;
  391. Function fpSymlink(oldname,newname:pchar):cint;
  392. {
  393. We need this for erase
  394. }
  395. begin
  396. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  397. end;
  398. function Fppread(fd: cint; buf: pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PREAD'];
  399. begin
  400. {$ifdef CPU64}
  401. Fppread:=do_syscall(syscall_nr_pread64,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  402. {$else}
  403. Fppread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,
  404. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  405. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  406. );
  407. {$endif}
  408. end;
  409. function Fppwrite(fd: cint;buf:pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PWRITE'];
  410. begin
  411. {$ifdef CPU64}
  412. Fppwrite:=do_syscall(syscall_nr_pwrite64,Fd,TSysParam(buf),nbytes,TSysParam(OffSet));
  413. {$else}
  414. Fppwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,
  415. {$ifdef FPC_BIG_ENDIAN} hi(offset),lo(offset){$endif}
  416. {$ifdef FPC_LITTLE_ENDIAN} lo(offset),hi(offset){$endif}
  417. );
  418. {$endif}
  419. end;
  420. function Fpreadv(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_READV'];
  421. begin
  422. Fpreadv:=do_syscall(syscall_nr_readv,Fd,TSysParam(iov),iovcnt);
  423. end;
  424. function Fpwritev(fd: cint; const iov : piovec; iovcnt : cint):ssize_t; [public, alias : 'FPC_SYSC_WRITEV'];
  425. begin
  426. Fpwritev:=do_syscall(syscall_nr_writev,Fd,TSysParam(iov),iovcnt);
  427. end;