bunxsysc.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. Function fppipe(var fildes : tfildes):cint;
  265. begin
  266. fppipe:=do_syscall(syscall_nr_pipe,TSysParam(@fildes));
  267. end;
  268. function fpfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  269. begin
  270. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  271. end;
  272. function fpfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  273. begin
  274. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,TSysParam(@arg));
  275. end;
  276. function fpfcntl(fildes:cint;Cmd:cint):cint;
  277. begin
  278. fpfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  279. end;
  280. function fpexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  281. Begin
  282. fpexecve:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  283. End;
  284. function fpexecv(path:pchar;argv:ppchar):cint;
  285. Begin
  286. fpexecv:=do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(argv),TSysParam(envp));
  287. End;
  288. function fptimes(var buffer : tms):clock_t;
  289. begin
  290. fptimes:=Do_syscall(syscall_nr_times,TSysParam(@buffer));
  291. end;
  292. function pfpgetcwd(path : pchar; siz:tsize):pchar; [public, alias : 'FPC_SYSC_GETCWD'];
  293. begin
  294. pfpgetcwd:=pchar(Do_Syscall(Syscall_nr_getcwd,TSysParam(Path),TSysParam(siz)));
  295. end;
  296. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  297. {
  298. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  299. have changed.
  300. }
  301. {$ifdef cpui386}
  302. Var
  303. SelectArray : Array[1..5] of TSysParam;
  304. {$endif}
  305. begin
  306. {$ifdef cpui386}
  307. {$define bunxfunc_fpselect_implemented}
  308. SelectArray[1]:=n;
  309. SelectArray[2]:=TSysParam(Readfds);
  310. Selectarray[3]:=TSysParam(Writefds);
  311. selectarray[4]:=TSysParam(exceptfds);
  312. Selectarray[5]:=TSysParam(TimeOut);
  313. fpSelect:=do_syscall(syscall_nr_select,TSysParam(@selectarray));
  314. {$endif cpui386}
  315. {$ifdef cpux86_64}
  316. {$define bunxfunc_fpselect_implemented}
  317. fpSelect:=do_syscall(syscall_nr_select,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  318. {$endif cpux86_64}
  319. {$ifdef cpuarm}
  320. {$define bunxfunc_fpselect_implemented}
  321. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  322. {$endif cpuarm}
  323. {$ifdef cpupowerpc}
  324. {$define bunxfunc_fpselect_implemented}
  325. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  326. {$endif cpupowerpc}
  327. {$ifdef cpusparc}
  328. {$define bunxfunc_fpselect_implemented}
  329. fpSelect:=do_syscall(syscall_nr__newselect,n,tsysparam(readfds),tsysparam(writefds),tsysparam(exceptfds),tsysparam(timeout));
  330. {$endif cpusparc}
  331. {$ifndef bunxfunc_fpselect_implemented}
  332. {$error Implement fpselect}
  333. {$endif bunxfunc_fpselect_implemented}
  334. end;
  335. Function fpLstat(path:pchar;Info:pstat):cint;
  336. {
  337. Get all information on a link (the link itself), and return it in info.
  338. }
  339. begin
  340. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(path),TSysParam(info));
  341. end;
  342. Function fpLstat(Filename: ansistring;Info:pstat):cint;
  343. {
  344. Get all information on a link (the link itself), and return it in info.
  345. }
  346. begin
  347. fpLStat:=do_syscall(syscall_nr_lstat,TSysParam(pchar(filename)),TSysParam(info));
  348. end;
  349. function fpNice(N:cint):cint;
  350. {
  351. Set process priority. A positive N means a lower priority.
  352. A negative N increases priority.
  353. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  354. }
  355. {$ifdef cpux86_64}
  356. var
  357. oldprio : cint;
  358. {$endif}
  359. begin
  360. {$ifdef cpux86_64}
  361. oldprio:=fpGetPriority(Prio_Process,0);
  362. fpNice:=fpSetPriority(Prio_Process,0,oldprio+N);
  363. if fpNice=0 then
  364. fpNice:=fpGetPriority(Prio_Process,0);
  365. {$else}
  366. fpNice:=do_syscall(Syscall_nr_nice,N);
  367. {$endif}
  368. end;
  369. Function fpGetPriority(Which,Who:cint):cint;
  370. {
  371. Get Priority of process, process group, or user.
  372. Which : selects what kind of priority is used.
  373. can be one of the following predefined Constants :
  374. Prio_User.
  375. Prio_PGrp.
  376. Prio_Process.
  377. Who : depending on which, this is , respectively :
  378. Uid
  379. Pid
  380. Process Group id
  381. Errors are reported in linuxerror _only_. (priority can be negative)
  382. }
  383. begin
  384. if (which<prio_process) or (which>prio_user) then
  385. begin
  386. { We can save an interrupt here }
  387. fpgetpriority:=-1;
  388. fpsetErrno(ESysEinval);
  389. end
  390. else
  391. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  392. end;
  393. Function fpSetPriority(Which,Who,What:cint):cint;
  394. {
  395. Set Priority of process, process group, or user.
  396. Which : selects what kind of priority is used.
  397. can be one of the following predefined Constants :
  398. Prio_User.
  399. Prio_PGrp.
  400. Prio_Process.
  401. Who : depending on value of which, this is, respectively :
  402. Uid
  403. Pid
  404. Process Group id
  405. what : A number between -20 and 20. -20 is most favorable, 20 least.
  406. 0 is the default.
  407. }
  408. begin
  409. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  410. fpseterrno(ESyseinval) { We can save an interrupt here }
  411. else
  412. begin
  413. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  414. end;
  415. end;
  416. Function fpSymlink(oldname,newname:pchar):cint;
  417. {
  418. We need this for erase
  419. }
  420. begin
  421. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  422. end;
  423. {
  424. $Log$
  425. Revision 1.1 2005-02-13 20:01:38 peter
  426. * include file cleanup
  427. Revision 1.15 2005/01/30 18:01:15 peter
  428. * signal cleanup for linux
  429. * sigactionhandler instead of tsigaction for bsds
  430. * sigcontext moved to cpu dir
  431. Revision 1.14 2004/11/19 13:15:14 marco
  432. * external rework. Mostly done.
  433. Revision 1.13 2004/11/14 12:21:08 marco
  434. * moved some calls from unix to baseunix. Darwin untested.
  435. Revision 1.12 2004/10/24 13:55:52 peter
  436. * fpselect for amd64,arm
  437. Revision 1.11 2004/10/13 20:47:12 florian
  438. + implemented fpselect for sparc
  439. Revision 1.10 2004/04/28 20:48:20 peter
  440. * ordinal-pointer conversions fixed
  441. Revision 1.9 2004/04/22 17:17:23 peter
  442. * x86-64 fixes
  443. Revision 1.8 2004/02/21 23:18:50 marco
  444. * powerpc select fix
  445. Revision 1.7 2003/12/30 15:43:20 marco
  446. * linux now compiles with FPC_USE_LIBC
  447. Revision 1.6 2003/11/19 14:04:34 marco
  448. * fix for sleep from johill
  449. Revision 1.5 2003/09/27 13:45:58 peter
  450. * fpnanosleep exported in baseunix
  451. * fpnanosleep has pointer arguments to be C compliant
  452. Revision 1.4 2003/09/17 11:24:46 marco
  453. * fixes for new macro's
  454. Revision 1.3 2003/09/14 20:15:01 marco
  455. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  456. Revision 1.2 2003/01/05 19:16:45 marco
  457. * small fix
  458. Revision 1.1 2002/12/18 16:43:26 marco
  459. * new unix rtl, linux part.....
  460. Revision 1.1 2002/11/14 16:48:39 marco
  461. * Initial version
  462. Revision 1.10 2002/11/14 12:34:20 marco
  463. * took out the generic sethandling.
  464. Revision 1.9 2002/11/13 18:15:08 marco
  465. * sigset functions more flexible, small changes to sys_time
  466. Revision 1.8 2002/10/27 17:21:29 marco
  467. * Only "difficult" functions + execvp + termios + rewinddir left to do
  468. Revision 1.7 2002/10/27 11:58:29 marco
  469. * Modifications from Saturday.
  470. Revision 1.6 2002/10/26 18:27:51 marco
  471. * First series POSIX calls commits. Including getcwd.
  472. Revision 1.5 2002/10/25 15:46:48 marco
  473. * Should be alias.
  474. Revision 1.4 2002/09/08 16:20:27 marco
  475. * Forgot external name's
  476. Revision 1.3 2002/09/08 16:11:59 marco
  477. * Added GetDomainName and that other one ..
  478. Revision 1.2 2002/09/07 16:01:17 peter
  479. * old logs removed and tabs fixed
  480. Revision 1.1 2002/08/21 07:03:16 marco
  481. * Fixes from Tuesday.
  482. Revision 1.1 2002/08/08 11:39:30 marco
  483. * Initial versions, to allow support for uname in posix.pp
  484. }