bsdsysc.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. {
  2. $Id$
  3. Copyright (c) 2001 by Carl Eric Codere
  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. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. {$ifdef uselibc}
  21. {$Linklib c}
  22. { var
  23. Errno : cint; external name 'errno';}
  24. function sys_time(var tloc:time_t): time_t; cdecl; external name 'time';
  25. function sys_open(const path: pchar; flags : cint; mode: mode_t):cint; cdecl; external name 'open';
  26. function sys_close(fd : cint): cint; cdecl; external name 'close';
  27. function sys_lseek(fd : cint; offset : off_t; whence : cint): off_t; cdecl; external name 'lseek';
  28. function sys_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external name 'read';
  29. function sys_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; cdecl; external name 'write';
  30. function sys_unlink(const path: pchar): cint; cdecl; external name 'unlink';
  31. function sys_rename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename';
  32. function sys_stat(const path: pchar; var buf : stat): cint; cdecl; external name 'stat';
  33. function sys_chdir(const path : pchar): cint; cdecl; external name 'chdir';
  34. function sys_mkdir(const path : pchar; mode: mode_t):cint; cdecl; external name 'mkdir';
  35. function sys_rmdir(const path : pchar): cint; cdecl; external name 'rmdir';
  36. function sys_opendir(const dirname : pchar): pdir; cdecl; external name 'opendir';
  37. function sys_readdir(var dirp : dir) : pdirent;cdecl; external name 'readdir';
  38. function sys_closedir(var dirp : dir): cint; cdecl; external name 'closedir';
  39. procedure sys_exit(status : cint); cdecl; external name '_exit';
  40. function sys_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; cdecl; external name 'sigaction';
  41. function sys_ftruncate(fd : cint; flength : off_t): cint; cdecl; external name 'ftruncate';
  42. function sys_rename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename';
  43. function sys_fstat(fd : cint; var sb : stat): cint; cdecl; external name 'fstat';
  44. function sys_fork : pid_t; cdecl; external name 'fork';
  45. function sys_execve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; cdecl; external name 'execve';
  46. function sys_waitpid(pid : pid_t; var stat_loc : cint; options: cint): pid_t; cdecl; external name 'waitpid';
  47. function sys_access(const pathname : pchar; amode : cint): cint; cdecl; external name 'access';
  48. function sys_uname(var name: utsname): cint; cdecl; external name 'uname';
  49. function sys_Dup(oldd:cint):cint; cdecl; external name 'dup';
  50. function sys_Dup2(oldd:cint;newd:cint):cint; cdecl; external name 'dup2';
  51. {$else}
  52. {*****************************************************************************
  53. --- Main:The System Call Self ---
  54. *****************************************************************************}
  55. { The system designed for Linux can't be used for *BSD so easily, since
  56. *BSD pushes arguments, instead of loading them to registers.}
  57. // Var ErrNo : Longint;
  58. {$I syscall.inc}
  59. {$I sysnr.inc}
  60. {$I bsdmacro.inc}
  61. {$I bsdtypes.inc}
  62. // Should be moved to a FreeBSD specific unit in the future.
  63. function sys_time(var tloc:time_t): time_t; [public, alias : 'FPC_SYSC_TIME'];
  64. VAR tv : timeval;
  65. tz : timezone;
  66. retval : longint;
  67. begin
  68. Retval:=do_syscall(syscall_nr_gettimeofday,longint(@tv),longint(@tz));
  69. If retval=-1 then
  70. sys_time:=-1
  71. else
  72. Begin
  73. // If Assigned(tloc) Then
  74. TLoc:=tv.sec;
  75. sys_time:=tv.sec;
  76. End;
  77. End;
  78. {*****************************************************************************
  79. --- File:File handling related calls ---
  80. *****************************************************************************}
  81. function sys_open(const path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  82. Begin
  83. sys_open:=do_syscall(syscall_nr_open,longint(path),longint(flags),longint(mode));
  84. End;
  85. function sys_close(fd : cint): cint;
  86. begin
  87. sys_close:=do_syscall(syscall_nr_close,fd);
  88. end;
  89. function sys_lseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  90. {
  91. this one is special for the return value being 64-bit..
  92. hi/lo offset not yet tested.
  93. NetBSD: ok, but implicit return value in edx:eax
  94. FreeBSD: same implementation as NetBSD.
  95. }
  96. begin
  97. sys_lseek:=do_syscall(syscall_nr___syscall,syscall_nr_lseek,0,longint(fd),0,lo(Offset),{0} hi(offset),Whence);
  98. end;
  99. function sys_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  100. begin
  101. sys_read:=do_syscall(syscall_nr_read,Fd,longint(buf),nbytes);
  102. end;
  103. function sys_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  104. begin
  105. sys_write:=do_syscall(syscall_nr_write,Fd,longint(buf),nbytes);
  106. end;
  107. function sys_unlink(const path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  108. begin
  109. sys_unlink:=do_syscall(syscall_nr_unlink,longint(path));
  110. end;
  111. function sys_rename(const old : pchar; const newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  112. begin
  113. sys_rename:=do_syscall(syscall_nr_rename,longint(old),longint(newpath));
  114. end;
  115. function sys_stat(const path: pchar; var buf : stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  116. begin
  117. sys_stat:=do_syscall(syscall_nr_stat,longint(path),longint(@buf));
  118. end;
  119. {*****************************************************************************
  120. --- Directory:Directory related calls ---
  121. *****************************************************************************}
  122. function sys_chdir(const path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  123. begin
  124. sys_chdir:=do_syscall(syscall_nr_chdir,longint(path));
  125. end;
  126. function sys_mkdir(const path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  127. begin {Mode is 16-bit on F-BSD}
  128. sys_mkdir:=do_syscall(syscall_nr_mkdir,longint(path),mode);
  129. end;
  130. function sys_rmdir(const path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  131. begin
  132. sys_rmdir:=do_syscall(syscall_nr_rmdir,longint(path));
  133. end;
  134. {$ifndef NewReaddir}
  135. const DIRBLKSIZ=1024;
  136. function sys_opendir(const dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  137. var
  138. fd:longint;
  139. st:stat;
  140. ptr:pdir;
  141. begin
  142. sys_opendir:=nil;
  143. if sys_stat(dirname,st)<0 then
  144. exit;
  145. { Is it a dir ? }
  146. if not((st.st_mode and $f000)=$4000)then
  147. begin
  148. errno:=sys_enotdir;
  149. exit
  150. end;
  151. { Open it}
  152. fd:=sys_open(dirname,O_RDONLY,438);
  153. if fd<0 then
  154. Begin
  155. Errno:=-1;
  156. exit;
  157. End;
  158. new(ptr);
  159. if ptr=nil then
  160. Begin
  161. Errno:=1;
  162. exit;
  163. End;
  164. Getmem(ptr^.dd_buf,2*DIRBLKSIZ);
  165. if ptr^.dd_buf=nil then
  166. exit;
  167. ptr^.dd_fd:=fd;
  168. ptr^.dd_loc:=-1;
  169. ptr^.dd_rewind:=longint(ptr^.dd_buf);
  170. ptr^.dd_size:=0;
  171. // ptr^.dd_max:=sizeof(ptr^.dd_buf^);
  172. sys_opendir:=ptr;
  173. end;
  174. function sys_closedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  175. begin
  176. sys_closedir:=sys_close(dirp^.dd_fd);
  177. Freemem(dirp^.dd_buf);
  178. dispose(dirp);
  179. end;
  180. function sys_readdir(dirp : pdir) : pdirent; [public, alias : 'FPC_SYSC_READDIR'];
  181. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  182. missing of the readdir syscall.
  183. Getdents requires the buffer to be larger than the blocksize.
  184. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  185. with blockmode have this higher?}
  186. function readbuffer:longint;
  187. var retval :longint;
  188. begin
  189. Retval:=do_syscall(syscall_nr_getdents,longint(dirp^.dd_fd),longint(@dirp^.dd_buf^),DIRBLKSIZ {sizeof(getdentsbuffer)});
  190. dirp^.dd_rewind:=longint(dirp^.dd_buf);
  191. if retval=0 then
  192. begin
  193. dirp^.dd_rewind:=0;
  194. dirp^.dd_loc:=0;
  195. end
  196. else
  197. dirP^.dd_loc:=retval;
  198. readbuffer:=retval;
  199. end;
  200. var
  201. FinalEntry : pdirent;
  202. novalid : boolean;
  203. Reclen : Longint;
  204. CurEntry : PDirent;
  205. begin
  206. if (dirp^.dd_buf=nil) or (dirp^.dd_loc=0) THEN
  207. exit(nil);
  208. if (dirp^.dd_loc=-1) OR {First readdir on this pdir. Initial fill of buffer}
  209. (dirp^.dd_rewind>=(longint(dirp^.dd_buf)+dirblksiz)) then {no more entries left?}
  210. Begin
  211. if readbuffer=0 then {succesful read?}
  212. Exit(NIL); {No more data}
  213. End;
  214. FinalEntry:=NIL;
  215. CurEntry:=nil;
  216. repeat
  217. novalid:=false;
  218. CurEntry:=pdirent(dirp^.dd_rewind);
  219. RecLen:=CurEntry^.d_reclen;
  220. if RecLen<>0 Then
  221. begin {valid direntry?}
  222. if CurEntry^.d_fileno<>0 then
  223. FinalEntry:=CurEntry;
  224. inc(dirp^.dd_rewind,Reclen);
  225. end
  226. else
  227. begin {block entirely searched or reclen=0}
  228. Novalid:=True;
  229. if dirp^.dd_loc<>0 THEN {blocks left?}
  230. if readbuffer()<>0 then {succesful read?}
  231. novalid:=false;
  232. end;
  233. until (FinalEntry<>nil) or novalid;
  234. If novalid then
  235. FinalEntry:=nil;
  236. Sys_ReadDir:=FinalEntry;
  237. end;
  238. {$endif}
  239. {*****************************************************************************
  240. --- Process:Process & program handling - related calls ---
  241. *****************************************************************************}
  242. procedure sys_exit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  243. begin
  244. do_syscall(syscall_nr_exit,status);
  245. end;
  246. {
  247. Change action of process upon receipt of a signal.
  248. Signum specifies the signal (all except SigKill and SigStop).
  249. If Act is non-nil, it is used to specify the new action.
  250. If OldAct is non-nil the previous action is saved there.
  251. }
  252. function sys_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  253. {
  254. Change action of process upon receipt of a signal.
  255. Signum specifies the signal (all except SigKill and SigStop).
  256. If Act is non-nil, it is used to specify the new action.
  257. If OldAct is non-nil the previous action is saved there.
  258. }
  259. begin
  260. do_syscall(syscall_nr_sigaction,longint(sig),longint(@act),longint(@oact));
  261. end;
  262. (*=================== MOVED from sysunix.inc ========================*)
  263. function sys_ftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  264. { See notes lseek. This one is completely similar.
  265. }
  266. begin
  267. sys_ftruncate:=Do_syscall(syscall_nr___syscall,syscall_nr_ftruncate,0,fd,0,lo(flength),hi(flength));
  268. end;
  269. function sys_fstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  270. begin
  271. Sys_FStat:=do_SysCall(syscall_nr_fstat,fd,longint(@sb));
  272. end;
  273. {$ifdef NewReaddir}
  274. {$I readdir.inc}
  275. {$endif}
  276. function sys_fork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  277. {
  278. This function issues the 'fork' System call. the program is duplicated in memory
  279. and Execution continues in parent and child process.
  280. In the parent process, fork returns the PID of the child. In the child process,
  281. zero is returned.
  282. A negative value indicates that an error has occurred, the error is returned in
  283. LinuxError.
  284. }
  285. Begin
  286. sys_fork:=Do_syscall(SysCall_nr_fork);
  287. End;
  288. {
  289. function sys_execve(const path : pathstr; const argv : ppchar; const envp: ppchar): cint;
  290. }
  291. {
  292. Replaces the current program by the program specified in path,
  293. arguments in args are passed to Execve.
  294. environment specified in ep is passed on.
  295. }
  296. {
  297. Begin
  298. path:=path+#0;
  299. do_syscall(syscall_nr_Execve,longint(@path[1]),longint(Argv),longint(envp));
  300. End;
  301. }
  302. function sys_execve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  303. {
  304. Replaces the current program by the program specified in path,
  305. arguments in args are passed to Execve.
  306. environment specified in ep is passed on.
  307. }
  308. Begin
  309. do_syscall(syscall_nr_Execve,longint(path),longint(Argv),longint(envp));
  310. End;
  311. function sys_waitpid(pid : pid_t; var stat_loc : cint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  312. {
  313. Waits until a child with PID Pid exits, or returns if it is exited already.
  314. Any resources used by the child are freed.
  315. The exit status is reported in the adress referred to by Status. It should
  316. be a longint.
  317. }
  318. begin
  319. sys_WaitPID:=do_syscall(syscall_nr_WaitPID,PID,longint(Stat_loc),options,0);
  320. end;
  321. function sys_access(const pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  322. {
  323. Test users access rights on the specified file.
  324. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  325. R,W,X stand for read,write and Execute access, simultaneously.
  326. F_OK checks whether the test would be allowed on the file.
  327. i.e. It checks the search permissions in all directory components
  328. of the path.
  329. The test is done with the real user-ID, instead of the effective.
  330. If access is denied, or an error occurred, false is returned.
  331. If access is granted, true is returned.
  332. Errors other than no access,are reported in unixerror.
  333. }
  334. begin
  335. sys_Access:=do_syscall(syscall_nr_access,longint(pathname),amode);
  336. end;
  337. {
  338. function sys_access(const pathname : pathstr; amode : cint): cint;
  339. {
  340. Test users access rights on the specified file.
  341. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  342. R,W,X stand for read,write and Execute access, simultaneously.
  343. F_OK checks whether the test would be allowed on the file.
  344. i.e. It checks the search permissions in all directory components
  345. of the path.
  346. The test is done with the real user-ID, instead of the effective.
  347. If access is denied, or an error occurred, false is returned.
  348. If access is granted, true is returned.
  349. Errors other than no access,are reported in unixerror.
  350. }
  351. begin
  352. pathname:=pathname+#0;
  353. Access:=do_syscall(syscall_nr_access, longint(@pathname[1]),mode)=0;
  354. end;
  355. }
  356. function sys_Dup(oldd:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  357. {
  358. Copies the filedescriptor oldfile to newfile
  359. }
  360. begin
  361. sys_dup:=Do_syscall(syscall_nr_dup,oldd);
  362. end;
  363. function sys_Dup2(oldd:cint;newd:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  364. {
  365. Copies the filedescriptor oldfile to newfile
  366. }
  367. begin
  368. sys_dup2:=do_syscall(syscall_nr_dup2,oldd,newd);
  369. end;
  370. CONST
  371. { Constansts for MMAP }
  372. MAP_PRIVATE =2;
  373. MAP_ANONYMOUS =$1000;
  374. Function Sys_mmap(adr,len,prot,flags,fdes,off:longint):longint; [public, alias : 'FPC_SYSC_MMAP'];
  375. begin
  376. Sys_mmap:=do_syscall(syscall_nr_mmap,Adr,Len,Prot,Flags,fdes,off,0);
  377. end;
  378. Function sbrk(size : longint) : Longint;
  379. begin
  380. sbrk:=Sys_mmap(0,Size,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0);
  381. if sbrk<>-1 then
  382. errno:=0;
  383. {! It must be -1, not 0 as before, see heap.inc. Should be in sysmmap?}
  384. end;
  385. {
  386. Interface to Unix ioctl call.
  387. Performs various operations on the filedescriptor Handle.
  388. Ndx describes the operation to perform.
  389. Data points to data needed for the Ndx function. The structure of this
  390. data is function-dependent.
  391. }
  392. Function Sys_IOCtl(Handle,Ndx: Longint;Data: Pointer):LongInt; [public, alias : 'FPC_SYSC_IOCTL'];
  393. // This was missing here, instead hardcoded in Do_IsDevice
  394. begin
  395. Sys_IOCtl:=do_SysCall(syscall_nr_ioctl,handle,Ndx,longint(data));
  396. end;
  397. CONST
  398. IOCtl_TCGETS=$5401;
  399. Function Do_IsDevice(Handle:Longint):boolean;
  400. {
  401. Interface to Unix ioctl call.
  402. Performs various operations on the filedescriptor Handle.
  403. Ndx describes the operation to perform.
  404. Data points to data needed for the Ndx function. The structure of this
  405. data is function-dependent.
  406. }
  407. var
  408. Data : array[0..255] of byte; {Large enough for termios info}
  409. begin
  410. Do_IsDevice:=(sys_ioctl(handle,IOCTL_TCGETS,@data)<>-1);
  411. end;
  412. Function sys_GetPid:LongInt; [public, alias : 'FPC_SYSC_GETPID'];
  413. {
  414. Get Process ID.
  415. }
  416. begin
  417. sys_GetPID:=do_syscall(syscall_nr_getpid);
  418. end;
  419. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint; [public, alias : 'FPC_SYSC_READLINK'];
  420. begin
  421. sys_readlink:=do_syscall(syscall_nr_readlink, longint(name),longint(linkname),maxlen);
  422. end;
  423. {$endif}
  424. {
  425. $Log$
  426. Revision 1.4 2002-10-16 18:44:18 marco
  427. * Lseek and ftruncate 64-bit fix
  428. Revision 1.3 2002/09/07 16:01:17 peter
  429. * old logs removed and tabs fixed
  430. Revision 1.2 2002/08/21 07:03:16 marco
  431. * Fixes from Tuesday.
  432. Revision 1.1 2002/08/19 12:29:11 marco
  433. * First working POSIX *BSD system unit.
  434. Revision 1.2 2002/08/04 04:29:34 marco
  435. * More POSIX updates. Small changes to lseek and ftruncate in osposix.inc
  436. Initial versions of the type includefiles
  437. Revision 1.1 2002/08/03 19:34:19 marco
  438. * Initial *BSD versions. Seems that OpenBSD doesn't need much change,
  439. NetBSD may need some fixes to stat record and ftruncate and lseek.
  440. It is all close together, and it should be doable to have just one copy
  441. of these for *BSD.
  442. }