bsdsysc.inc 17 KB

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