ossysc.inc 15 KB

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