bunxsysc.inc 14 KB

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