bsdfuncs.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. {$i syscallh.inc} // do_syscall declarations themselves
  15. {$i sysnr.inc} // syscall numbers.
  16. {$i bsdsysch.inc} // external interface to syscalls in system unit.
  17. {$i posixunx.inc} // generic calls. (like getenv)
  18. Const // OS specific parameters for general sigset behaviour
  19. SIG_MAXSIG = 128; // highest signal version
  20. wordsinsigset = 4; // words in sigset_t
  21. ln2bitsinword = 5; { 32bit : ln(32)/ln(2)=5 }
  22. ln2bitmask = 2 shl ln2bitsinword - 1;
  23. {$I gensigset.inc} // general sigset funcs implementation.
  24. Function sys_Kill(Pid:pid_t;Sig:cint):cint;
  25. {
  26. Send signal 'sig' to a process, or a group of processes.
  27. If Pid > 0 then the signal is sent to pid
  28. pid=-1 to all processes except process 1
  29. pid < -1 to process group -pid
  30. Return value is zero, except for case three, where the return value
  31. is the number of processes to which the signal was sent.
  32. }
  33. begin
  34. sys_kill:=do_syscall(syscall_nr_kill,pid,sig);
  35. // if kill<0 THEN
  36. // Kill:=0;
  37. end;
  38. function sys_SigProcMask(how:cint;var _set : sigset_t; var _oset : sigset_t):cint;
  39. {
  40. Change the list of currently blocked signals.
  41. How determines which signals will be blocked :
  42. SigBlock : Add SSet to the current list of blocked signals
  43. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  44. SigSetMask : Set the list of blocked signals to SSet
  45. if OldSSet is non-null, the old set will be saved there.
  46. }
  47. begin
  48. sys_sigprocmask:=do_syscall(syscall_nr_sigprocmask,longint(how),longint(@_set),longint(@_oset));
  49. end;
  50. Function sys_SigPending(var _set: sigset_t):cint;
  51. {
  52. Allows examination of pending signals. The signal mask of pending
  53. signals is set in SSet
  54. }
  55. begin
  56. sys_sigpending:=do_syscall(syscall_nr_sigpending,longint(@_set));
  57. end;
  58. function sys_sigsuspend(const sigmask:sigset_t):cint;
  59. {
  60. Set the signal mask with Mask, and suspend the program until a signal
  61. is received.
  62. }
  63. begin
  64. sys_sigsuspend:= do_syscall(syscall_nr_sigsuspend,longint(@sigmask));
  65. end;
  66. Type // implementation side for now. Should move to BSD unit.
  67. ITimerVal= Record
  68. It_Interval,
  69. It_Value : TimeVal;
  70. end;
  71. Const ITimer_Real =0;
  72. ITimer_Virtual =1;
  73. ITimer_Prof =2;
  74. Function SetITimer(Which : Longint;Const value : ItimerVal; var VarOValue:ItimerVal):Longint;
  75. Begin
  76. SetItimer:=Do_Syscall(syscall_nr_setitimer,Which,Longint(@Value),longint(@varovalue));
  77. End;
  78. Function GetITimer(Which : Longint;Var value : ItimerVal):Longint;
  79. Begin
  80. GetItimer:=Do_Syscall(syscall_nr_getItimer,Which,Longint(@value));
  81. End;
  82. Function sys_alarm(Seconds: cuint):cuint;
  83. Var it,oitv : Itimerval;
  84. Begin
  85. // register struct itimerval *itp = &it;
  86. it.it_interval.tv_sec:=0;
  87. it.it_interval.tv_usec:=0;
  88. it.it_value.tv_sec:=seconds;
  89. it.it_value.tv_usec:=0;
  90. If SetITimer(ITIMER_REAL,it,oitv)<0 Then
  91. Exit(-1);
  92. if oitv.it_value.tv_usec<>0 Then
  93. Inc(oitv.it_value.tv_sec);
  94. sys_Alarm:=oitv.it_value.tv_sec;
  95. End;
  96. function sigblock(mask:cuint):cint;
  97. {Depreciated, but used by pause.}
  98. var nset,oset: sigset_t;
  99. begin
  100. sys_sigemptyset(nset);
  101. nset[0]:=mask;
  102. sigblock:= sys_sigprocmask(SIG_BLOCK,nset,oset); // SIG_BLOCK=1
  103. if sigblock=0 Then
  104. sigblock:=oset[0];
  105. el
  106. end;
  107. function sigpause(sigmask:cint):cint;
  108. {Depreciated, but used by pause.}
  109. var nset: sigset_t;
  110. begin
  111. sys_sigemptyset(nset);
  112. nset[0]:=sigmask;
  113. sigpause:= sys_sigsuspend(nset);
  114. end;
  115. function sys_pause:cint;
  116. begin
  117. sys_pause:=sigpause(sigblock(cuint(0)));
  118. end;
  119. function sys_sleep(seconds:cuint):cuint;
  120. var time_to_sleep,time_remaining : timespec;
  121. begin
  122. {
  123. * Avoid overflow when `seconds' is huge. This assumes that
  124. * the maximum value for a time_t is >= INT_MAX.
  125. }
  126. if seconds > high(cint) Then
  127. sys_sleep:= (seconds - high(cint)) + sys_sleep(HIGH(cint));
  128. time_to_sleep.tv_sec := seconds;
  129. time_to_sleep.tv_nsec := 0;
  130. if (sys_nanosleep(time_to_sleep, time_remaining) <> -1) Then
  131. Exit(0);
  132. if (geterrno <> sys_EINTR) Then
  133. Exit (seconds); { best guess }
  134. sys_sleep:= time_remaining.tv_sec;
  135. if (time_remaining.tv_nsec <> 0) Then
  136. inc(sys_sleep);
  137. End;
  138. function sys_uname(var name:utsname):cint; [public,alias:'FPC_SYSC_UNAME'];
  139. Var
  140. mib : array[0..1] of cint;
  141. rval : cint;
  142. len : size_t;
  143. i : longint;
  144. oerrno : cint;
  145. procedure Doone(pz:pchar;pzsize:cint;val1,val2:cint);
  146. Begin
  147. mib[0] := val1;
  148. mib[1] := val2;
  149. len := pzsize;
  150. oerrno := geterrno;
  151. if (sys_sysctl(@mib, 2, pz, @len, NIL, 0) = -1) Then
  152. Begin
  153. if (geterrno = sys_ENOMEM) Then
  154. seterrno(oerrno)
  155. else
  156. rval := -1;
  157. End;
  158. pz[pzsize- 1] := #0;
  159. End;
  160. Begin
  161. rval := 0;
  162. DoOne(@name.sysname,sizeof(name.sysname),CTL_KERN,KERN_OSTYPE);
  163. DoOne(@name.nodename,sizeof(name.nodename),CTL_KERN,KERN_HOSTNAME);
  164. DoOne(@name.release,sizeof(name.release),CTL_KERN,KERN_OSRELEASE);
  165. { The version may have newlines in it, turn them into spaces. }
  166. DoOne(@name.version,sizeof(name.version),CTL_KERN,KERN_VERSION);
  167. For I:=0 to sizeof(name.sysname)-2 Do
  168. If (name.version[i]=#13) or (name.version[i]=#9) Then
  169. name.version[i]:=' ';
  170. DoOne(@name.machine,sizeof(name.machine),CTL_HW,HW_MACHINE);
  171. sys_Uname:=rval;
  172. end;
  173. function GetDomainName(Name:PChar; NameLen:Cint):cint; [public,alias:'FPC_SYSC_GETDOMAINNAME'];
  174. Const Mib_GetDomainName : array[0..1] of cint=(CTL_KERN,KERN_NISDOMAINNAME);
  175. VAR
  176. tsize : size_t;
  177. begin
  178. tsize := namelen;
  179. if (sys_sysctl(@Mib_GetDomainname, 2, name, @tsize, NIL, 0) = -1) Then
  180. GetDomainName:=-1
  181. Else
  182. GetDomainName:=0;
  183. end;
  184. function GetHostName(Name:PChar; NameLen:Cint):cint;[public,alias:'FPC_SYSC_GETHOSTNAME'];
  185. Const Mib_GetHostName : array[0..1] of cint=(CTL_KERN,KERN_HOSTNAME);
  186. Var
  187. tsize : size_t;
  188. begin
  189. tsize := namelen;
  190. if (sys_sysctl(@Mib_GetHostName, 2, name, @tsize, NIL, 0) = -1) Then
  191. GetHostName:=-1
  192. Else
  193. GetHostName:=0;
  194. End;
  195. const WAIT_ANY = -1;
  196. function sys_wait(var stat_loc:cint): pid_t;
  197. {
  198. Waits until a child with PID Pid exits, or returns if it is exited already.
  199. Any resources used by the child are freed.
  200. The exit status is reported in the adress referred to by Status. It should
  201. be a longint.
  202. }
  203. begin // actually a wait4() call with 4th arg 0.
  204. sys_Wait:=do_syscall(syscall_nr_WaitPID,WAIT_ANY,longint(@Stat_loc),0,0);
  205. end;
  206. //function sys_getpid : pid_t;
  207. // begin
  208. // sys_getpid:=do_syscall(syscall_nr_getpid);
  209. // end;
  210. function sys_getppid : pid_t;
  211. begin
  212. sys_getppid:=do_syscall(syscall_nr_getppid);
  213. end;
  214. function sys_getuid : uid_t;
  215. begin
  216. sys_getuid:=do_syscall(syscall_nr_getuid);
  217. end;
  218. function sys_geteuid : uid_t;
  219. begin
  220. sys_geteuid:=do_syscall(syscall_nr_geteuid);
  221. end;
  222. function sys_getgid : gid_t;
  223. begin
  224. sys_getgid:=do_syscall(syscall_nr_getgid);
  225. end;
  226. function sys_getegid : gid_t;
  227. begin
  228. sys_getegid:=do_syscall(syscall_nr_getegid);
  229. end;
  230. function sys_setuid(uid : uid_t): cint;
  231. begin
  232. sys_setuid:=do_syscall(syscall_nr_setuid,uid);
  233. end;
  234. function sys_setgid(gid : gid_t): cint;
  235. begin
  236. sys_setgid:=do_syscall(syscall_nr_setgid,gid);
  237. end;
  238. // type tgrparr=array[0..0] of gid_t;
  239. function sys_getgroups(gidsetsize : cint; var grouplist:tgrparr): cint;
  240. begin
  241. sys_getgroups:=do_syscall(syscall_nr_getgroups,gidsetsize,longint(@grouplist));
  242. end;
  243. function sys_getpgrp : pid_t;
  244. begin
  245. sys_getpgrp:=do_syscall(syscall_nr_getpgrp);
  246. end;
  247. function sys_setsid : pid_t;
  248. begin
  249. sys_setsid:=do_syscall(syscall_nr_setsid);
  250. end;
  251. Function sys_umask(cmask:mode_t):mode_t;
  252. {
  253. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  254. previous value.
  255. }
  256. begin
  257. sys_umask:=Do_syscall(syscall_nr_umask,cmask);
  258. end;
  259. Function sys_link(existing:pchar;_new:pchar):cint;
  260. {
  261. Proceduces a hard link from new to old.
  262. In effect, new will be the same file as old.
  263. }
  264. begin
  265. sys_Link:=Do_Syscall(syscall_nr_link,longint(existing),longint(_new));
  266. end;
  267. Function sys_mkfifo(path:pchar;mode:mode_t):cint;
  268. begin
  269. sys_mkfifo:=do_syscall(syscall_nr_mkfifo,longint(path),longint(mode));
  270. end;
  271. Function sys_chmod(path:pchar;mode:mode_t):cint;
  272. begin
  273. sys_chmod:=do_syscall(syscall_nr_chmod,longint(path),longint(mode));
  274. end;
  275. Function sys_chown(path:pchar;owner:uid_t;group:gid_t):cint;
  276. begin
  277. sys_ChOwn:=do_syscall(syscall_nr_chown,longint(path),longint(owner),longint(group));
  278. end;
  279. Function sys_Utime(path:pchar;times:putimbuf):cint;
  280. var tv : array[0..1] of timeval;
  281. tvp : ^timeval;
  282. begin
  283. if times=nil Then
  284. tvp:=nil
  285. else
  286. begin
  287. tv[0].tv_sec :=times^.actime;
  288. tv[1].tv_sec :=times^.modtime;
  289. tv[0].tv_usec:=0;
  290. tv[1].tv_usec:=0;
  291. tvp:=@tv;
  292. end;
  293. sys_utime:=do_syscall(syscall_nr_utimes,longint(path),longint(tvp));
  294. end;
  295. Function sys_pipe(var fildes : tfildes):cint;
  296. begin
  297. sys_pipe:=do_syscall(syscall_nr_pipe,longint(@fildes));
  298. end;
  299. function sys_fcntl(fildes:cint;Cmd:cint;Arg:cint):cint;
  300. begin
  301. sys_fcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,arg);
  302. end;
  303. function sys_fcntl(fildes:cint;Cmd:cint;var Arg:flock):cint;
  304. begin
  305. sys_fcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd,longint(@arg));
  306. end;
  307. function sys_fcntl(fildes:cint;Cmd:cint):cint;
  308. begin
  309. sys_fcntl:=do_syscall(syscall_nr_fcntl,fildes,cmd);
  310. end;
  311. function sys_execve(path:pchar;argv:ppchar;envp:ppchar):cint;
  312. Begin
  313. sys_execve:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  314. End;
  315. function sys_execv(path:pchar;argv:ppchar):cint;
  316. Begin
  317. sys_execv:=do_syscall(syscall_nr_Execve,longint(path),longint(argv),longint(envp));
  318. End;
  319. CONST RUSAGE_SELF = 0;
  320. RUSAGE_CHILDREN = -1;
  321. function sys_getrusage(who:cint;var ru : rusage):cint;
  322. begin
  323. sys_getrusage:=do_syscall(syscall_nr_getrusage,longint(who),longint(@ru));
  324. end;
  325. function sys_times(var buffer : tms):clock_t;
  326. var ru : rusage;
  327. t : timeval;
  328. CONST CLK_TCK=128;
  329. function CONVTCK(r:timeval):clock_t;
  330. {
  331. * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
  332. * but this would overflow if we switch to nanosec.
  333. }
  334. begin
  335. CONVTCK:=(r.tv_sec * CLK_TCK + r.tv_usec DIV (1000000 DIV CLK_TCK));
  336. end;
  337. begin
  338. if (sys_getrusage(RUSAGE_SELF, ru) < 0) Then
  339. exit(clock_t(-1));
  340. buffer.tms_utime := CONVTCK(ru.ru_utime);
  341. buffer.tms_stime := CONVTCK(ru.ru_stime);
  342. if (sys_getrusage(RUSAGE_CHILDREN, ru) < 0) Then
  343. exit(clock_t(-1));
  344. buffer.tms_cutime := CONVTCK(ru.ru_utime);
  345. buffer.tms_cstime := CONVTCK(ru.ru_stime);
  346. if do_syscall(syscall_nr_gettimeofday,longint(@t),0)<>0 Then
  347. exit(clock_t(-1));
  348. sys_times:=clock_t(CONVTCK(t));
  349. end;
  350. {
  351. $Log$
  352. Revision 1.11 2002-11-14 13:25:27 marco
  353. * Fix setitimer.
  354. Revision 1.10 2002/11/14 12:34:20 marco
  355. * took out the generic sethandling.
  356. Revision 1.9 2002/11/13 18:15:08 marco
  357. * sigset functions more flexible, small changes to sys_time
  358. Revision 1.8 2002/10/27 17:21:29 marco
  359. * Only "difficult" functions + execvp + termios + rewinddir left to do
  360. Revision 1.7 2002/10/27 11:58:29 marco
  361. * Modifications from Saturday.
  362. Revision 1.6 2002/10/26 18:27:51 marco
  363. * First series POSIX calls commits. Including getcwd.
  364. Revision 1.5 2002/10/25 15:46:48 marco
  365. * Should be alias.
  366. Revision 1.4 2002/09/08 16:20:27 marco
  367. * Forgot external name's
  368. Revision 1.3 2002/09/08 16:11:59 marco
  369. * Added GetDomainName and that other one ..
  370. Revision 1.2 2002/09/07 16:01:17 peter
  371. * old logs removed and tabs fixed
  372. Revision 1.1 2002/08/21 07:03:16 marco
  373. * Fixes from Tuesday.
  374. Revision 1.1 2002/08/08 11:39:30 marco
  375. * Initial versions, to allow support for uname in posix.pp
  376. }