bunxsysc.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 POSIX 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,pid,sig);
  25. // if kill<0 THEN
  26. // Kill:=0;
  27. end;
  28. Function FPSigPending(var nset: sigset_t):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_sigpending,longint(@nset));
  35. end;
  36. function FPsigsuspend(const sigmask:sigset_t):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_sigsuspend,longint(@sigmask));
  43. end;
  44. Type // implementation side for now. Should move to BSD unit.
  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,Longint(@Value),longint(@varovalue));
  55. End;
  56. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  57. Begin
  58. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,Longint(@value));
  59. End;
  60. Function FPalarm(Seconds: cuint):cuint;
  61. Var it,oitv : Itimerval;
  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_sec:=seconds;
  67. it.it_value.tv_usec:=0;
  68. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  69. Exit(-1);
  70. if oitv.it_value.tv_usec<>0 Then
  71. Inc(oitv.it_value.tv_sec);
  72. FPAlarm:=oitv.it_value.tv_sec;
  73. End;
  74. function sigblock(mask:cuint):cint;
  75. {Depreciated, but used by pause.}
  76. var nset,oset: sigset_t;
  77. begin
  78. FPsigemptyset(nset);
  79. nset[0]:=mask;
  80. sigblock:= FPsigprocmask(SIG_BLOCK,@nset,@oset); // SIG_BLOCK=1
  81. if sigblock=0 Then
  82. sigblock:=oset[0];
  83. end;
  84. function sigpause(sigmask:cint):cint;
  85. {Depreciated, but used by pause.}
  86. var nset: sigset_t;
  87. begin
  88. FPsigemptyset(nset);
  89. nset[0]:=sigmask;
  90. sigpause:= FPsigsuspend(nset);
  91. end;
  92. function FPpause:cint;
  93. begin
  94. FPpause:=sigpause(sigblock(cuint(0)));
  95. end;
  96. function FPsleep(seconds:cuint):cuint;
  97. var time_to_sleep,time_remaining : timespec;
  98. begin
  99. {
  100. * Avoid overflow when `seconds' is huge. This assumes that
  101. * the maximum value for a time_t is >= INT_MAX.
  102. }
  103. if seconds > high(cint) Then
  104. FPsleep:= (seconds - high(cint)) + FPsleep(HIGH(cint));
  105. time_to_sleep.tv_sec := seconds;
  106. time_to_sleep.tv_nsec := 0;
  107. if (FPnanosleep(@time_to_sleep, @time_remaining) <> -1) Then
  108. Exit(0);
  109. if (fpgeterrno <> ESysEINTR) Then // EAGAIN?
  110. Exit (seconds); { best guess }
  111. FPsleep:= time_remaining.tv_sec;
  112. if (time_remaining.tv_nsec <> 0) Then
  113. inc(FPsleep);
  114. End;
  115. function FPuname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  116. Var
  117. mib : array[0..1] of cint;
  118. rval : cint;
  119. len : size_t;
  120. i : longint;
  121. oerrno : cint;
  122. procedure Doone(pz:pchar;pzsize:cint;val1,val2:cint);
  123. Begin
  124. mib[0] := val1;
  125. mib[1] := val2;
  126. len := pzsize;
  127. oerrno := fpgeterrno;
  128. if (FPsysctl(@mib, 2, pz, @len, NIL, 0) = -1) Then
  129. Begin
  130. if (fpgeterrno = ESysENOMEM) Then
  131. fpseterrno(oerrno)
  132. else
  133. rval := -1;
  134. End;
  135. pz[pzsize- 1] := #0;
  136. End;
  137. Begin
  138. rval := 0;
  139. DoOne(@name.sysname,sizeof(name.sysname),CTL_KERN,KERN_OSTYPE);
  140. DoOne(@name.nodename,sizeof(name.nodename),CTL_KERN,KERN_HOSTNAME);
  141. DoOne(@name.release,sizeof(name.release),CTL_KERN,KERN_OSRELEASE);
  142. { The version may have newlines in it, turn them into spaces. }
  143. DoOne(@name.version,sizeof(name.version),CTL_KERN,KERN_VERSION);
  144. For I:=0 to sizeof(name.sysname)-2 Do
  145. If (name.version[i]=#13) or (name.version[i]=#9) Then
  146. name.version[i]:=' ';
  147. DoOne(@name.machine,sizeof(name.machine),CTL_HW,HW_MACHINE);
  148. FPUname:=rval;
  149. end;
  150. function GetDomainName(Name:PChar; NameLen:Cint):cint; [public,alias:'FPC_SYSC_GETDOMAINNAME'];
  151. Const Mib_GetDomainName : array[0..1] of cint=(CTL_KERN,{$ifdef OpenBSD}KERN_DOMAINNAME{$ELSE}KERN_NISDOMAINNAME{$endif});
  152. VAR
  153. tsize : size_t;
  154. begin
  155. tsize := namelen;
  156. if (FPsysctl(@Mib_GetDomainname, 2, name, @tsize, NIL, 0) = -1) Then
  157. GetDomainName:=-1
  158. Else
  159. GetDomainName:=0;
  160. end;
  161. function GetHostName(Name:PChar; NameLen:Cint):cint;[public,alias:'FPC_SYSC_GETHOSTNAME'];
  162. Const Mib_GetHostName : array[0..1] of cint=(CTL_KERN,KERN_HOSTNAME);
  163. Var
  164. tsize : size_t;
  165. begin
  166. tsize := namelen;
  167. if (FPsysctl(@Mib_GetHostName, 2, name, @tsize, NIL, 0) = -1) Then
  168. GetHostName:=-1
  169. Else
  170. GetHostName:=0;
  171. End;
  172. const WAIT_ANY = -1;
  173. function FPwait(var stat_loc:cint): pid_t;
  174. {
  175. Waits until a child with PID Pid exits, or returns if it is exited already.
  176. Any resources used by the child are freed.
  177. The exit status is reported in the adress referred to by Status. It should
  178. be a longint.
  179. }
  180. begin // actually a wait4() call with 4th arg 0.
  181. FPWait:=do_syscall(syscall_nr_WaitPID,WAIT_ANY,longint(@Stat_loc),0,0);
  182. end;
  183. //function FPgetpid : pid_t;
  184. // begin
  185. // FPgetpid:=do_syscall(syscall_nr_getpid);
  186. // end;
  187. function FPgetppid : pid_t;
  188. begin
  189. FPgetppid:=do_syscall(syscall_nr_getppid);
  190. end;
  191. function FPgetuid : uid_t;
  192. begin
  193. FPgetuid:=do_syscall(syscall_nr_getuid);
  194. end;
  195. function FPgeteuid : uid_t;
  196. begin
  197. FPgeteuid:=do_syscall(syscall_nr_geteuid);
  198. end;
  199. function FPgetgid : gid_t;
  200. begin
  201. FPgetgid:=do_syscall(syscall_nr_getgid);
  202. end;
  203. function FPgetegid : gid_t;
  204. begin
  205. FPgetegid:=do_syscall(syscall_nr_getegid);
  206. end;
  207. function FPsetuid(uid : uid_t): cint;
  208. begin
  209. FPsetuid:=do_syscall(syscall_nr_setuid,uid);
  210. end;
  211. function FPsetgid(gid : gid_t): cint;
  212. begin
  213. FPsetgid:=do_syscall(syscall_nr_setgid,gid);
  214. end;
  215. // type tgrparr=array[0..0] of gid_t;
  216. function FPgetgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  217. begin
  218. FPgetgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,longint(@grouplist));
  219. end;
  220. function FPgetpgrp : pid_t;
  221. begin
  222. FPgetpgrp:=do_syscall(syscall_nr_getpgrp);
  223. end;
  224. function FPsetsid : pid_t;
  225. begin
  226. FPsetsid:=do_syscall(syscall_nr_setsid);
  227. end;
  228. Function FPumask(cmask:mode_t):mode_t;
  229. {
  230. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  231. previous value.
  232. }
  233. begin
  234. FPumask:=Do_syscall(syscall_nr_umask,cmask);
  235. end;
  236. Function FPlink(existing:pchar;newone:pchar):cint;
  237. {
  238. Proceduces a hard link from new to old.
  239. In effect, new will be the same file as old.
  240. }
  241. begin
  242. FPLink:=Do_Syscall(syscall_nr_link,longint(existing),longint(newone));
  243. end;
  244. Function FPmkfifo(path:pchar;mode:mode_t):cint;
  245. begin
  246. FPmkfifo:=do_syscall(syscall_nr_mkfifo,longint(path),longint(mode));
  247. end;
  248. Function FPchmod(path:pchar;mode:mode_t):cint;
  249. begin
  250. FPchmod:=do_syscall(syscall_nr_chmod,longint(path),longint(mode));
  251. end;
  252. Function FPchown(path:pchar;owner:uid_t;group:gid_t):cint;
  253. begin
  254. FPChOwn:=do_syscall(syscall_nr_chown,longint(path),longint(owner),longint(group));
  255. end;
  256. Function FPUtime(path:pchar;times:putimbuf):cint;
  257. var tv : array[0..1] of timeval;
  258. tvp : ^timeval;
  259. begin
  260. if times=nil Then
  261. tvp:=nil
  262. else
  263. begin
  264. tv[0].tv_sec :=times^.actime;
  265. tv[1].tv_sec :=times^.modtime;
  266. tv[0].tv_usec:=0;
  267. tv[1].tv_usec:=0;
  268. tvp:=@tv;
  269. end;
  270. FPutime:=do_syscall(syscall_nr_utimes,longint(path),longint(tvp));
  271. end;
  272. Function FPpipe(var fildes : tfildes):cint;
  273. begin
  274. FPpipe:=do_syscall(syscall_nr_pipe,longint(@fildes));
  275. end;
  276. function FPfcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  277. begin
  278. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  279. end;
  280. function FPfcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  281. begin
  282. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,longint(@arg));
  283. end;
  284. function FPfcntl(fildes:cint;Cmd:cint):cint;
  285. begin
  286. FPfcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  287. end;
  288. function FPexecve(path:pchar;argv:ppchar;envp:ppchar):cint;
  289. Begin
  290. FPexecve:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  291. End;
  292. function FPexecv(path:pchar;argv:ppchar):cint;
  293. Begin
  294. FPexecv:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  295. End;
  296. CONST RUSAGE_SELF = 0;
  297. RUSAGE_CHILDREN = -1;
  298. function FPgetrusage(who:cint;var ru : rusage):cint;
  299. begin
  300. FPgetrusage:=do_syscall(syscall_nr_getrusage,longint(who),longint(@ru));
  301. end;
  302. function FPtimes(var buffer : tms):clock_t;
  303. var ru : rusage;
  304. t : timeval;
  305. CONST CLK_TCK=128;
  306. function CONVTCK(r:timeval):clock_t;
  307. {
  308. * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
  309. * but this would overflow if we switch to nanosec.
  310. }
  311. begin
  312. CONVTCK:=(r.tv_sec * CLK_TCK + r.tv_usec DIV (1000000 DIV CLK_TCK));
  313. end;
  314. begin
  315. if (FPgetrusage(RUSAGE_SELF, ru) < 0) Then
  316. exit(clock_t(-1));
  317. buffer.tms_utime := CONVTCK(ru.ru_utime);
  318. buffer.tms_stime := CONVTCK(ru.ru_stime);
  319. if (FPgetrusage(RUSAGE_CHILDREN, ru) < 0) Then
  320. exit(clock_t(-1));
  321. buffer.tms_cutime := CONVTCK(ru.ru_utime);
  322. buffer.tms_cstime := CONVTCK(ru.ru_stime);
  323. if do_syscall(syscall_nr_gettimeofday,longint(@t),0)<>0 Then
  324. exit(clock_t(-1));
  325. FPtimes:=clock_t(CONVTCK(t));
  326. end;
  327. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
  328. {
  329. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  330. have changed.
  331. }
  332. begin
  333. fpSelect:=do_syscall(syscall_nr_select,n,longint(readfds),longint(writefds),longint(exceptfds),longint(timeout));
  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 decreases priority.
  353. Doesn't exist in BSD. Linux emu uses setpriority in a construct as below:
  354. }
  355. var prio : cint;
  356. begin
  357. fpseterrno(0);
  358. prio:=fpgetpriority(PRIO_PROCESS,0);
  359. if (prio=-1) and (fpgeterrno<>0) then
  360. exit(-1);
  361. fpNice:=fpSetPriority(Prio_Process,0,prio+N);
  362. end;
  363. Function fpGetPriority(Which,Who:cint):cint;
  364. {
  365. Get Priority of process, process group, or user.
  366. Which : selects what kind of priority is used.
  367. can be one of the following predefined Constants :
  368. Prio_User.
  369. Prio_PGrp.
  370. Prio_Process.
  371. Who : depending on which, this is , respectively :
  372. Uid
  373. Pid
  374. Process Group id
  375. Errors are reported in linuxerror _only_. (priority can be negative)
  376. }
  377. begin
  378. if (which<prio_process) or (which>prio_user) then
  379. begin
  380. { We can save an interrupt here }
  381. fpgetpriority:=0;
  382. fpseterrno(ESysEinval);
  383. end
  384. else
  385. begin
  386. fpGetPriority:=do_syscall(syscall_nr_GetPriority,which,who);
  387. end;
  388. end;
  389. Function fpSetPriority(Which,Who,What:cint):cint;
  390. {
  391. Set Priority of process, process group, or user.
  392. Which : selects what kind of priority is used.
  393. can be one of the following predefined Constants :
  394. Prio_User.
  395. Prio_PGrp.
  396. Prio_Process.
  397. Who : depending on value of which, this is, respectively :
  398. Uid
  399. Pid
  400. Process Group id
  401. what : A number between -20 and 20. -20 is most favorable, 20 least.
  402. 0 is the default.
  403. }
  404. begin
  405. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  406. fpseterrno(ESyseinval) { We can save an interrupt here }
  407. else
  408. begin
  409. fpSetPriority:=do_syscall(Syscall_nr_Setpriority,which,who,what);
  410. end;
  411. end;
  412. Function fpSymlink(oldname,newname:pchar):cint;
  413. {
  414. We need this for erase
  415. }
  416. begin
  417. fpsymlink:=do_syscall(syscall_nr_symlink,TSysParam(oldname),TSysParam(newname));
  418. end;
  419. {
  420. $Log$
  421. Revision 1.2 2005-02-14 17:13:21 peter
  422. * truncate log
  423. Revision 1.1 2005/02/13 20:01:37 peter
  424. * include file cleanup
  425. }