ossysc.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. {
  2. Copyright (c) 2002 by Marco van de Voort
  3. The base *BSD syscalls required to implement the system unit. These
  4. are aliased for use in other units (to avoid poluting the system units
  5. interface)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. ****************************************************************************
  12. }
  13. function Fptime( tloc:ptime): time_t; [public, alias : 'FPC_SYSC_TIME'];
  14. VAR tv : timeval;
  15. tz : timezone;
  16. retval : longint;
  17. begin
  18. Retval:=do_syscall(syscall_nr_gettimeofday,TSysParam(@tv),TSysParam(@tz));
  19. If retval=-1 then
  20. Fptime:=-1
  21. else
  22. Begin
  23. If Assigned(tloc) Then
  24. TLoc^:=tv.tv_sec;
  25. Fptime:=tv.tv_sec;
  26. End;
  27. End;
  28. {*****************************************************************************
  29. --- File:File handling related calls ---
  30. *****************************************************************************}
  31. function Fpopen(path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  32. Begin
  33. Fpopen:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags),TSysParam(mode));
  34. End;
  35. function Fpclose(fd : cint): cint; [public, alias : 'FPC_SYSC_CLOSE'];
  36. begin
  37. Fpclose:=do_syscall(syscall_nr_close,fd);
  38. end;
  39. {$ifdef netbsd}
  40. {$ifdef cpupowerpc}
  41. {$define netbsdmacppc}
  42. {$endif}
  43. {$endif}
  44. {$ifdef netbsdmacppc}
  45. {$i sysofft.inc} // odd ball calling convention.
  46. {$else}
  47. // generic versions.
  48. function Fplseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  49. {
  50. this one is special for the return value being 64-bit..
  51. hi/lo offset not yet tested.
  52. NetBSD: ok, but implicit return value in edx:eax
  53. FreeBSD: same implementation as NetBSD.
  54. }
  55. begin
  56. Fplseek:=do_syscall(syscall_nr___syscall,syscall_nr_lseek,0,TSysParam(fd),0,lo(Offset),{0} hi(offset),Whence);
  57. end;
  58. function Fpftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  59. begin
  60. Fpftruncate:=Do_syscall(syscall_nr___syscall,syscall_nr_ftruncate,0,fd,0,lo(flength),hi(flength));
  61. end;
  62. Function Fpmmap(start:pointer;len:size_t;prot:cint;flags:cint;fd:cint;offst:off_t):pointer; [public, alias: 'FPC_SYSC_MMAP'];
  63. begin
  64. Fpmmap:=pointer(longint(do_syscall(syscall_nr_mmap,TSysParam(Start),Len,Prot,Flags,fd,
  65. {$ifdef FPC_BIG_ENDIAN} hi(offst),lo(offst){$endif}
  66. {$ifdef FPC_LITTLE_ENDIAN} lo(offst),hi(offst){$endif},0
  67. )));
  68. end;
  69. {$endif}
  70. function Fpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  71. begin
  72. Fpread:=do_syscall(syscall_nr_read,Fd,TSysParam(buf),nbytes);
  73. end;
  74. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  75. begin
  76. Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  77. end;
  78. function Fpunlink(const path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  79. begin
  80. Fpunlink:=do_syscall(syscall_nr_unlink,TSysParam(path));
  81. end;
  82. function Fprename(old : pchar; newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  83. begin
  84. Fprename:=do_syscall(syscall_nr_rename,TSysParam(old),TSysParam(newpath));
  85. end;
  86. function Fpstat(const path: pchar; var buf : stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  87. begin
  88. Fpstat:=do_syscall(syscall_nr_stat,TSysParam(path),TSysParam(@buf));
  89. end;
  90. {*****************************************************************************
  91. --- Directory:Directory related calls ---
  92. *****************************************************************************}
  93. function Fpchdir(path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  94. begin
  95. Fpchdir:=do_syscall(syscall_nr_chdir,TSysParam(path));
  96. end;
  97. function Fpmkdir(path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  98. begin {Mode is 16-bit on F-BSD 4!}
  99. Fpmkdir:=do_syscall(syscall_nr_mkdir,TSysParam(path),mode);
  100. end;
  101. function Fprmdir(path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  102. begin
  103. Fprmdir:=do_syscall(syscall_nr_rmdir,TSysParam(path));
  104. end;
  105. {$ifndef NewReaddir}
  106. const DIRBLKSIZ=1024;
  107. function Fpopendir(dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  108. var
  109. fd:longint;
  110. st:stat;
  111. ptr:pdir;
  112. begin
  113. Fpopendir:=nil;
  114. if Fpstat(dirname,st)<0 then
  115. exit;
  116. { Is it a dir ? }
  117. if not((st.st_mode and $f000)=$4000)then
  118. begin
  119. errno:=ESysENOTDIR;
  120. exit
  121. end;
  122. { Open it}
  123. fd:=Fpopen(dirname,O_RDONLY,438);
  124. if fd<0 then
  125. Begin
  126. Errno:=-1;
  127. exit;
  128. End;
  129. new(ptr);
  130. if ptr=nil then
  131. Begin
  132. Errno:=1;
  133. exit;
  134. End;
  135. Getmem(ptr^.dd_buf,2*DIRBLKSIZ);
  136. if ptr^.dd_buf=nil then
  137. exit;
  138. ptr^.dd_fd:=fd;
  139. ptr^.dd_loc:=-1;
  140. ptr^.dd_rewind:=longint(ptr^.dd_buf);
  141. ptr^.dd_size:=0;
  142. // ptr^.dd_max:=sizeof(ptr^.dd_buf^);
  143. Fpopendir:=ptr;
  144. end;
  145. function Fpclosedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  146. begin
  147. Fpclosedir:=Fpclose(dirp^.dd_fd);
  148. Freemem(dirp^.dd_buf);
  149. dispose(dirp);
  150. end;
  151. function Fpreaddir(dirp : pdir) : pdirent; [public, alias : 'FPC_SYSC_READDIR'];
  152. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  153. missing of the readdir syscall.
  154. Getdents requires the buffer to be larger than the blocksize.
  155. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  156. with blockmode have this higher?}
  157. function readbuffer:longint;
  158. var retval :longint;
  159. begin
  160. Retval:=do_syscall(syscall_nr_getdents,TSysParam(dirp^.dd_fd),TSysParam(@dirp^.dd_buf^),DIRBLKSIZ {sizeof(getdentsbuffer)});
  161. dirp^.dd_rewind:=TSysParam(dirp^.dd_buf);
  162. if retval=0 then
  163. begin
  164. dirp^.dd_rewind:=0;
  165. dirp^.dd_loc:=0;
  166. end
  167. else
  168. dirP^.dd_loc:=retval;
  169. dirP^.dd_size:=retval;
  170. readbuffer:=retval;
  171. end;
  172. var
  173. FinalEntry : pdirent;
  174. novalid : boolean;
  175. Reclen : Longint;
  176. CurEntry : PDirent;
  177. begin
  178. if (dirp^.dd_buf=nil) or (dirp^.dd_loc=0) THEN
  179. exit(nil);
  180. if (dirp^.dd_loc=-1) OR {First readdir on this pdir. Initial fill of buffer}
  181. (dirp^.dd_rewind>=(longint(dirp^.dd_buf)+dirp^.dd_size)) then {no more entries left?}
  182. Begin
  183. if readbuffer=0 then {succesful read?}
  184. Exit(NIL); {No more data}
  185. End;
  186. FinalEntry:=NIL;
  187. CurEntry:=nil;
  188. repeat
  189. novalid:=false;
  190. CurEntry:=pdirent(dirp^.dd_rewind);
  191. RecLen:=CurEntry^.d_reclen;
  192. if RecLen<>0 Then
  193. begin {valid direntry?}
  194. if CurEntry^.d_fileno<>0 then
  195. FinalEntry:=CurEntry;
  196. inc(dirp^.dd_rewind,Reclen);
  197. end
  198. else
  199. begin {block entirely searched or reclen=0}
  200. Novalid:=True;
  201. if dirp^.dd_loc<>0 THEN {blocks left?}
  202. if readbuffer()<>0 then {succesful read?}
  203. novalid:=false;
  204. end;
  205. until (FinalEntry<>nil) or novalid;
  206. If novalid then
  207. FinalEntry:=nil;
  208. FpReadDir:=FinalEntry;
  209. end;
  210. {$endif}
  211. {*****************************************************************************
  212. --- Process:Process & program handling - related calls ---
  213. *****************************************************************************}
  214. procedure Fpexit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  215. begin
  216. do_syscall(syscall_nr_exit,status);
  217. end;
  218. {
  219. Change action of process upon receipt of a signal.
  220. Signum specifies the signal (all except SigKill and SigStop).
  221. If Act is non-nil, it is used to specify the new action.
  222. If OldAct is non-nil the previous action is saved there.
  223. }
  224. function Fpsigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  225. {
  226. Change action of process upon receipt of a signal.
  227. Signum specifies the signal (all except SigKill and SigStop).
  228. If Act is non-nil, it is used to specify the new action.
  229. If OldAct is non-nil the previous action is saved there.
  230. }
  231. begin
  232. do_syscall(syscall_nr_sigaction,TSysParam(sig),TSysParam(@act),TSysParam(@oact));
  233. end;
  234. (*=================== MOVED from sysunix.inc ========================*)
  235. function Fpfstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  236. begin
  237. fpFStat:=do_SysCall(syscall_nr_fstat,fd,TSysParam(@sb));
  238. end;
  239. {$ifdef NewReaddir}
  240. {$I readdir.inc}
  241. {$endif}
  242. function Fpfork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  243. {
  244. This function issues the 'fork' System call. the program is duplicated in memory
  245. and Execution continues in parent and child process.
  246. In the parent process, fork returns the PID of the child. In the child process,
  247. zero is returned.
  248. A negative value indicates that an error has occurred, the error is returned in
  249. LinuxError.
  250. }
  251. Begin
  252. Fpfork:=Do_syscall(SysCall_nr_fork);
  253. End;
  254. {
  255. function Fpexecve(const path : pathstr; const argv : ppchar; const envp: ppchar): cint;
  256. }
  257. {
  258. Replaces the current program by the program specified in path,
  259. arguments in args are passed to Execve.
  260. environment specified in ep is passed on.
  261. }
  262. {
  263. Begin
  264. path:=path+#0;
  265. do_syscall(syscall_nr_Execve,TSysParam(@path[1]),TSysParam(Argv),TSysParam(envp));
  266. End;
  267. }
  268. {
  269. function Fpexecve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  270. }
  271. {
  272. Replaces the current program by the program specified in path,
  273. arguments in args are passed to Execve.
  274. environment specified in ep is passed on.
  275. }
  276. {
  277. Begin
  278. do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(Argv),TSysParam(envp));
  279. End;
  280. }
  281. function Fpwaitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  282. {
  283. Waits until a child with PID Pid exits, or returns if it is exited already.
  284. Any resources used by the child are freed.
  285. The exit status is reported in the adress referred to by Status. It should
  286. be a longint.
  287. }
  288. begin // actually a wait4() call with 4th arg 0.
  289. FpWaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options,0);
  290. end;
  291. function Fpaccess(const pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  292. {
  293. Test users access rights on the specified file.
  294. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  295. R,W,X stand for read,write and Execute access, simultaneously.
  296. F_OK checks whether the test would be allowed on the file.
  297. i.e. It checks the search permissions in all directory components
  298. of the path.
  299. The test is done with the real user-ID, instead of the effective.
  300. If access is denied, or an error occurred, false is returned.
  301. If access is granted, true is returned.
  302. Errors other than no access,are reported in unixerror.
  303. }
  304. begin
  305. FpAccess:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  306. end;
  307. {
  308. function Fpaccess(const pathname : pathstr; amode : cint): cint;
  309. {
  310. Test users access rights on the specified file.
  311. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  312. R,W,X stand for read,write and Execute access, simultaneously.
  313. F_OK checks whether the test would be allowed on the file.
  314. i.e. It checks the search permissions in all directory components
  315. of the path.
  316. The test is done with the real user-ID, instead of the effective.
  317. If access is denied, or an error occurred, false is returned.
  318. If access is granted, true is returned.
  319. Errors other than no access,are reported in unixerror.
  320. }
  321. begin
  322. pathname:=pathname+#0;
  323. Access:=do_syscall(syscall_nr_access, TSysParam(@pathname[1]),mode)=0;
  324. end;
  325. }
  326. Function FpDup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  327. begin
  328. Fpdup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  329. end;
  330. Function FpDup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  331. begin
  332. Fpdup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  333. end;
  334. Function Fpmunmap(start:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  335. begin
  336. Fpmunmap:=do_syscall(syscall_nr_munmap,TSysParam(start),Len);
  337. end;
  338. {
  339. Interface to Unix ioctl call.
  340. Performs various operations on the filedescriptor Handle.
  341. Ndx describes the operation to perform.
  342. Data points to data needed for the Ndx function. The structure of this
  343. data is function-dependent.
  344. }
  345. Function FpIOCtl(Handle:cint;Ndx: culong;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  346. // This was missing here, instead hardcoded in Do_IsDevice
  347. begin
  348. FpIOCtl:=do_SysCall(syscall_nr_ioctl,handle,Ndx,TSysParam(data));
  349. end;
  350. Function FpGetPid:LongInt; [public, alias : 'FPC_SYSC_GETPID'];
  351. {
  352. Get Process ID.
  353. }
  354. begin
  355. FpGetPID:=do_syscall(syscall_nr_getpid);
  356. end;
  357. function fpgettimeofday(tp: ptimeval;tzp:ptimezone):cint; [public, alias: 'FPC_SYSC_GETTIMEOFDAY'];
  358. begin
  359. fpgettimeofday:=do_syscall(syscall_nr_gettimeofday,TSysParam(tp),TSysParam(tzp));
  360. end;
  361. function FPSigProcMask(how:cint;nset : psigset;oset : psigset):cint; [public, alias : 'FPC_SYSC_SIGPROCMASK'];
  362. {
  363. Change the list of currently blocked signals.
  364. How determines which signals will be blocked :
  365. SigBlock : Add SSet to the current list of blocked signals
  366. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  367. SigSetMask : Set the list of blocked signals to SSet
  368. if OldSSet is non-null, the old set will be saved there.
  369. }
  370. begin
  371. FPsigprocmask:=do_syscall(syscall_nr_sigprocmask,longint(how),longint(nset),longint(oset));
  372. end;
  373. {$user BLA!}
  374. Function FpNanoSleep(req : ptimespec;rem : ptimespec) : cint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  375. begin
  376. FpNanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(req),TSysParam(rem));
  377. end;
  378. function Fpgetcwd(pt:pchar; _size:size_t):pchar;[public, alias :'FPC_SYSC_GETCWD'];
  379. const intpathmax = 1024-4; // didn't use POSIX data in libc
  380. // implementation.
  381. var ept,bpt : pchar;
  382. c : char;
  383. ret : cint;
  384. begin
  385. if pt=NIL Then
  386. begin
  387. // POSIX: undefined. (exit(nil) ?)
  388. // BSD : allocate mem for path.
  389. getmem(pt,intpathmax);
  390. if pt=nil Then
  391. exit(nil);
  392. ept:=pt+intpathmax;
  393. end
  394. else
  395. Begin
  396. if (_size=0) Then
  397. Begin
  398. seterrno(ESysEINVAL);
  399. exit(nil);
  400. End;
  401. if (_size=1) Then
  402. Begin
  403. seterrno(ESysERANGE);
  404. exit(nil);
  405. End;
  406. ept:=pt+_size;
  407. end;
  408. ret := do_syscall(syscall_nr___getcwd,TSysParam(pt),TSysParam( ept - pt));
  409. If (ret = 0) Then
  410. If (pt[0] <> '/') Then
  411. Begin
  412. bpt := pt;
  413. ept := pt + strlen(pt) - 1;
  414. While (bpt < ept) Do
  415. Begin
  416. c := bpt^;
  417. bpt^:=ept^;
  418. inc(bpt);
  419. ept^:=c;
  420. dec(ept);
  421. End;
  422. End;
  423. Fpgetcwd:=pt;
  424. end;
  425. Function fpReadLink(name,linkname:pchar;maxlen:size_t):cint; [public, alias : 'FPC_SYSC_READLINK'];
  426. begin
  427. fpreadlink:=do_syscall(syscall_nr_readlink, TSysParam(name),TSysParam(linkname),maxlen);
  428. end;