ossysc.inc 15 KB

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