bunxsysc.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2002 by Marco van de Voort
  5. Calls needed for the baseunix unit, but not for system.
  6. Some calls that can be used for both Linux and *BSD will be
  7. moved to a /unix/ includedfile later.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. Function fpKill(Pid:pid_t;Sig:cint):cint;
  15. {
  16. Send signal 'sig' to a process, or a group of processes.
  17. If Pid > 0 then the signal is sent to pid
  18. pid=-1 to all processes except process 1
  19. pid < -1 to process group -pid
  20. Return value is zero, except for case three, where the return value
  21. is the number of processes to which the signal was sent.
  22. }
  23. begin
  24. fpkill:=do_syscall(syscall_nr_kill,TSysParam(pid),TSysParam(sig));
  25. // if kill<0 THEN
  26. // Kill:=0;
  27. end;
  28. Function fpSigPending(var nset: TSigSet):cint;
  29. {
  30. Allows examination of pending signals. The signal mask of pending
  31. signals is set in SSet
  32. }
  33. begin
  34. fpsigpending:=do_syscall(syscall_nr_rt_sigpending,TSysParam(@nset));
  35. end;
  36. function fpsigsuspend(const sigmask:TSigSet):cint;
  37. {
  38. Set the signal mask with Mask, and suspend the program until a signal
  39. is received.
  40. }
  41. begin
  42. fpsigsuspend:= do_syscall(syscall_nr_rt_sigsuspend,TSysParam(@sigmask));
  43. end;
  44. Type
  45. ITimerVal= Record
  46. It_Interval,
  47. It_Value : TimeVal;
  48. end;
  49. Const ITimer_Real =0;
  50. ITimer_Virtual =1;
  51. ITimer_Prof =2;
  52. Function SetITimer(Which : Longint;Const value : ItimerVal; var VarOValue:ItimerVal):Longint;
  53. Begin
  54. SetItimer:=Do_Syscall(syscall_nr_setitimer,Which,TSysParam(@Value),TSysParam(@varovalue));
  55. End;
  56. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  57. Begin
  58. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,TSysParam(@value));
  59. End;
  60. Function fpalarm(Seconds: cuint):cuint;
  61. Var it,oitv : Itimerval;
  62. retval : cuint;
  63. Begin
  64. // register struct itimerval *itp = &it;
  65. it.it_interval.tv_sec:=0;
  66. it.it_interval.tv_usec:=0;
  67. it.it_value.tv_usec:=0;
  68. it.it_value.tv_sec:=seconds;
  69. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  70. Exit(0); // different from *BSD!
  71. retval:= oitv.it_value.tv_usec;
  72. if retval<>0 Then
  73. inc(retval);
  74. fpAlarm:=retval;
  75. End;
  76. // The following versions are for internal use _ONLY_
  77. // This because it works for the first 32 signals _ONLY_, but that
  78. // is enough since they are depreciated, and for legacy applications
  79. // anyway.
  80. function sigblock(mask:cuint):cint;
  81. var nset,oset: TSigSet;
  82. begin
  83. fpsigemptyset(nset);
  84. // fpsigaddset(nset,mask); needs _mask_
  85. nset[0]:=mask;
  86. sigblock:= fpsigprocmask(SIG_BLOCK,@nset,@oset); // SIG_BLOCK=1
  87. if sigblock=0 Then
  88. sigblock:=oset[0];
  89. end;
  90. function sigpause(sigmask:cint):cint;
  91. var nset: TSigSet;
  92. begin
  93. fpsigemptyset(nset);
  94. nset[0]:=sigmask;
  95. sigpause:= fpsigsuspend(nset);
  96. end;
  97. function fppause:cint;
  98. begin
  99. fppause:=sigpause(sigblock(cuint(0)));
  100. end;
  101. function fpsleep(seconds:cuint):cuint;
  102. {see comments in libc}
  103. var time_to_sleep,time_remaining : timespec;
  104. nset,oset : TSigSet;
  105. oerrno : cint;
  106. oact : sigactionrec;
  107. begin
  108. time_to_sleep.tv_sec := seconds;
  109. time_to_sleep.tv_nsec := 0;
  110. fpsigemptyset(nset);
  111. fpsigaddset (nset,SIGCHLD);
  112. if fpsigprocmask(SIG_BLOCK,@nset,@oset)=-1 Then
  113. exit(cuint(-1));
  114. if fpsigismember(oset,SIGCHLD)<>0 Then
  115. Begin
  116. fpsigemptyset(nset);
  117. fpsigaddset (nset,SIGCHLD);
  118. if fpsigaction(SIGCHLD,NIL,@oact)<0 Then
  119. begin
  120. oerrno:=fpgeterrno;
  121. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  122. fpseterrno(oerrno);
  123. exit(cuint(-1));
  124. End;
  125. if oact.sa_handler=SigActionhandler(SIG_IGN) Then
  126. Begin
  127. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining);
  128. oerrno:=fpgeterrno;
  129. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  130. fpseterrno(oerrno);
  131. End
  132. Else
  133. Begin
  134. fpsigprocmask(SIG_SETMASK,@oset,NIL);
  135. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining)
  136. End;
  137. end
  138. else
  139. fpsleep:=fpnanosleep(@time_to_sleep, @time_remaining);
  140. if fpsleep<>0 Then
  141. if time_remaining.tv_nsec>=500000000 Then
  142. inc(fpsleep);
  143. End;
  144. function fpuname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  145. begin
  146. fpuname:=Do_Syscall(syscall_nr_uname,TSysParam(@name));
  147. end;
  148. Function fpGetDomainName(Name:PChar; NameLen:size_t):cint;
  149. Var
  150. srec : utsname;
  151. tsize : size_t;
  152. Begin
  153. if fpuname(srec)<0 Then
  154. exit(-1);
  155. tsize:=strlen(@srec.domain[0]);
  156. if tsize>(namelen-1) Then
  157. tsize:=namelen-1;
  158. move(srec.domain[0],name[0],tsize);
  159. name[namelen-1]:=#0;
  160. fpgetDomainName:=0;
  161. End;
  162. function fpGetHostName(Name:PChar; NameLen:size_t):cint;
  163. Var
  164. srec : utsname;
  165. tsize : size_t;
  166. begin
  167. if fpuname(srec)<0 Then
  168. exit(-1);
  169. tsize:=strlen(@srec.nodename[0]);
  170. if tsize>(namelen-1) Then
  171. tsize:=namelen-1;
  172. move(srec.nodename[0],name[0],tsize);
  173. name[namelen-1]:=#0;
  174. fpgethostName:=0;
  175. End;
  176. const WAIT_ANY = -1;
  177. function fpwait(var stat_loc:cint): pid_t;
  178. {
  179. Waits until a child with PID Pid exits, or returns if it is exited already.
  180. Any resources used by the child are freed.
  181. The exit status is reported in the adress referred to by Status. It should
  182. be a longint.
  183. }
  184. begin // actually a wait4() call with 4th arg 0.
  185. fpWait:=do_syscall(syscall_nr_Wait4,WAIT_ANY,TSysParam(@Stat_loc),0,0);
  186. end;
  187. //function fpgetpid : pid_t;
  188. // begin
  189. // fpgetpid:=do_syscall(syscall_nr_getpid);
  190. // end;
  191. function fpgetppid : pid_t;
  192. begin
  193. fpgetppid:=do_syscall(syscall_nr_getppid);
  194. end;
  195. function fpgetuid : uid_t;
  196. begin
  197. fpgetuid:=do_syscall(syscall_nr_getuid);
  198. end;
  199. function fpgeteuid : uid_t;
  200. begin
  201. fpgeteuid:=do_syscall(syscall_nr_geteuid);
  202. end;
  203. function fpgetgid : gid_t;
  204. begin
  205. fpgetgid:=do_syscall(syscall_nr_getgid);
  206. end;
  207. function fpgetegid : gid_t;
  208. begin
  209. fpgetegid:=do_syscall(syscall_nr_getegid);
  210. end;
  211. function fpsetuid(uid : uid_t): cint;
  212. begin
  213. fpsetuid:=do_syscall(syscall_nr_setuid,uid);
  214. end;
  215. function fpsetgid(gid : gid_t): cint;
  216. begin
  217. fpsetgid:=do_syscall(syscall_nr_setgid,gid);
  218. end;
  219. // type tgrparr=array[0..0] of gid_t;
  220. function fpgetgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  221. begin
  222. fpgetgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,TSysParam(@grouplist));
  223. end;
  224. function fpgetpgrp : pid_t;
  225. begin
  226. fpgetpgrp:=do_syscall(syscall_nr_getpgrp);
  227. end;
  228. function fpsetsid : pid_t;
  229. begin
  230. fpsetsid:=do_syscall(syscall_nr_setsid);
  231. end;
  232. Function fpumask(cmask:mode_t):mode_t;
  233. {
  234. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  235. previous value.
  236. }
  237. begin
  238. fpumask:=Do_syscall(syscall_nr_umask,cmask);
  239. end;
  240. Function fplink(existing:pchar;newone:pchar):cint;
  241. {
  242. Proceduces a hard link from new to old.
  243. In effect, new will be the same file as old.
  244. }
  245. begin
  246. fpLink:=Do_Syscall(syscall_nr_link,TSysParam(existing),TSysParam(newone));
  247. end;
  248. Function fpmkfifo(path:pchar;mode:mode_t):cint;
  249. begin
  250. fpmkfifo:=do_syscall(syscall_nr_mknod,TSysParam(path),TSysParam(mode or S_IFIFO),TSysParam(0));
  251. end;
  252. Function fpchmod(path:pchar;mode:mode_t):cint;
  253. begin
  254. fpchmod:=do_syscall(syscall_nr_chmod,TSysParam(path),TSysParam(mode));
  255. end;
  256. Function fpchown(path:pchar;owner:uid_t;group:gid_t):cint;
  257. begin
  258. fpChOwn:=do_syscall(syscall_nr_chown,TSysParam(path),TSysParam(owner),TSysParam(group));
  259. end;
  260. Function fpUtime(path:pchar;times:putimbuf):cint;
  261. begin
  262. fputime:=do_syscall(syscall_nr_utime,TSysParam(path),TSysParam(times));
  263. end;
  264. {$ifndef FPC_SYSTEM_HAS_FPPIPE}
  265. Function fppipe(var fildes : tfildes):cint;
  266. begin
  267. fppipe:=do_syscall(syscall_nr_pipe,TSysParam(@fildes));
  268. end;
  269. {$endif FPC_SYSTEM_HAS_FPPIPE}
  270. function fpfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  271. begin
  272. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  273. end;
  274. function fpfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  275. begin
  276. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,TSysParam(@arg));
  277. end;
  278. function fpfcntl(fildes:cint;Cmd:cint):cint;
  279. begin
  280. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  281. end;
  282. function fpexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  283. Begin
  284. fpexecve:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  285. End;
  286. function fpexecv(path:pchar;argv:ppchar):cint;
  287. Begin
  288. fpexecv:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  289. End;
  290. function fptimes(var buffer : tms):clock_t;
  291. begin
  292. fptimes:=Do_syscall(syscall_nr_times,TSysParam(@buffer));
  293. end;
  294. function pfpgetcwd(path : pchar; siz:tsize):pchar; [public, alias : 'FPC_SYSC_GETCWD'];
  295. begin
  296. pfpgetcwd:=pchar(Do_Syscall(Syscall_nr_getcwd,TSysParam(Path),TSysParam(siz)));
  297. end;
  298. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  299. {
  300. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  301. have changed.
  302. }
  303. {$ifdef cpui386}
  304. Var
  305. SelectArray : Array[1..5] of TSysParam;
  306. {$endif}
  307. begin
  308. {$ifdef cpui386}
  309. {$define bunxfunc_fpselect_implemented}
  310. SelectArray[1]:=n;
  311. SelectArray[2]:=TSysParam(Readfds);
  312. Selectarray[3]:=TSysParam(Writefds);
  313. selectarray[4]:=TSysParam(exceptfds);
  314. Selectarray[5]:=TSysParam(TimeOut);
  315. fpSelect:=do_syscall(syscall_nr_select,TSysParam(@selectarray));
  316. {$endif cpui386}
  317. {$ifdef cpux86_64}
  318. {$define bunxfunc_fpselect_implemented}
  319. fpSelect:=do_syscall(syscall_nr_select,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  320. {$endif cpux86_64}
  321. {$ifdef cpuarm}
  322. {$define bunxfunc_fpselect_implemented}
  323. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  324. {$endif cpuarm}
  325. {$ifdef cpupowerpc}
  326. {$define bunxfunc_fpselect_implemented}
  327. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  328. {$endif cpupowerpc}
  329. {$ifdef cpusparc}
  330. {$define bunxfunc_fpselect_implemented}
  331. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  332. {$endif cpusparc}
  333. {$ifndef bunxfunc_fpselect_implemented}
  334. {$error Implement fpselect}
  335. {$endif bunxfunc_fpselect_implemented}
  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(syscall_nr_lstat,TSysParam(path),TSysParam(info));
  343. end;
  344. Function fpLstat(Filename: ansistring;Info:pstat):cint;
  345. {
  346. Get all information on a link (the link itself), and return it in info.
  347. }
  348. begin
  349. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(pchar(filename)),TSysParam(info));
  350. end;
  351. function fpNice(N:cint):cint;
  352. {
  353. Set process priority. A positive N means a lower priority.
  354. A negative N increases priority.
  355. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  356. }
  357. {$ifdef cpux86_64}
  358. var
  359. oldprio : cint;
  360. {$endif}
  361. begin
  362. {$ifdef cpux86_64}
  363. oldprio:=fpGetPriority(Prio_Process,0);
  364. fpNice:=fpSetPriority(Prio_Process,0,oldprio+N);
  365. if fpNice=0 then
  366. fpNice:=fpGetPriority(Prio_Process,0);
  367. {$else}
  368. fpNice:=do_syscall(Syscall_nr_nice,N);
  369. {$endif}
  370. end;
  371. Function fpGetPriority(Which,Who:cint):cint;
  372. {
  373. Get Priority of process, process group, or user.
  374. Which : selects what kind of priority is used.
  375. can be one of the following predefined Constants :
  376. Prio_User.
  377. Prio_PGrp.
  378. Prio_Process.
  379. Who : depending on which, this is , respectively :
  380. Uid
  381. Pid
  382. Process Group id
  383. Errors are reported in linuxerror _only_. (priority can be negative)
  384. }
  385. begin
  386. if (which<prio_process) or (which>prio_user) then
  387. begin
  388. { We can save an interrupt here }
  389. fpgetpriority:=-1;
  390. fpsetErrno(ESysEinval);
  391. end
  392. else
  393. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  394. end;
  395. Function fpSetPriority(Which,Who,What:cint):cint;
  396. {
  397. Set Priority of process, process group, or user.
  398. Which : selects what kind of priority is used.
  399. can be one of the following predefined Constants :
  400. Prio_User.
  401. Prio_PGrp.
  402. Prio_Process.
  403. Who : depending on value of which, this is, respectively :
  404. Uid
  405. Pid
  406. Process Group id
  407. what : A number between -20 and 20. -20 is most favorable, 20 least.
  408. 0 is the default.
  409. }
  410. begin
  411. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  412. fpseterrno(ESyseinval) { We can save an interrupt here }
  413. else
  414. begin
  415. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  416. end;
  417. end;
  418. Function fpSymlink(oldname,newname:pchar):cint;
  419. {
  420. We need this for erase
  421. }
  422. begin
  423. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  424. end;
  425. {
  426. $Log$
  427. Revision 1.3 2005-03-03 20:13:44 florian
  428. + sparc specific pipe implementation
  429. Revision 1.2 2005/02/14 17:13:30 peter
  430. * truncate log
  431. Revision 1.1 2005/02/13 20:01:38 peter
  432. * include file cleanup
  433. Revision 1.15 2005/01/30 18:01:15 peter
  434. * signal cleanup for linux
  435. * sigactionhandler instead of tsigaction for bsds
  436. * sigcontext moved to cpu dir
  437. }