ossysc.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Marco van de Voort
  4. The base Linux 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. {$ifdef uselibc}
  15. {$else}
  16. {*****************************************************************************
  17. --- Main:The System Call Self ---
  18. *****************************************************************************}
  19. {$I ostypes.inc}
  20. {$I syscallh.inc}
  21. {$I syscall.inc}
  22. {$I sysnr.inc}
  23. {$I bunxmacr.inc}
  24. function Fptime(tloc:pTime): TTime; [public, alias : 'FPC_SYSC_TIME'];
  25. begin
  26. Fptime:=do_syscall(syscall_nr_time,TSysParam(tloc));
  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. function Fplseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  40. {
  41. Must be adapted/overloaded for 64-bit support, but that is a different call under
  42. Linux?
  43. }
  44. begin
  45. Fplseek:=do_syscall(syscall_nr_lseek,tsysparam(fd),tsysparam(offset),tsysparam(whence));
  46. end;
  47. function Fpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  48. begin
  49. Fpread:=do_syscall(syscall_nr_read,Fd,TSysParam(buf),nbytes);
  50. end;
  51. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  52. begin
  53. Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  54. end;
  55. function Fpunlink(path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  56. begin
  57. Fpunlink:=do_syscall(syscall_nr_unlink,TSysParam(path));
  58. end;
  59. function Fprename(old : pchar; newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  60. begin
  61. Fprename:=do_syscall(syscall_nr_rename,TSysParam(old),TSysParam(newpath));
  62. end;
  63. function Fpstat(path: pchar; var buf : stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  64. begin
  65. Fpstat:=do_syscall(syscall_nr_stat,TSysParam(path),TSysParam(@buf));
  66. end;
  67. {*****************************************************************************
  68. --- Directory:Directory related calls ---
  69. *****************************************************************************}
  70. function Fpchdir(path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  71. begin
  72. Fpchdir:=do_syscall(syscall_nr_chdir,TSysParam(path));
  73. end;
  74. function Fpmkdir(path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  75. begin
  76. Fpmkdir:=do_syscall(syscall_nr_mkdir,TSysParam(path),TSysParam(mode));
  77. end;
  78. function Fprmdir(path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  79. begin
  80. Fprmdir:=do_syscall(syscall_nr_rmdir,TSysParam(path));
  81. end;
  82. function Fpopendir(dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  83. var
  84. fd:integer;
  85. st:stat;
  86. ptr:pdir;
  87. begin
  88. Fpopendir:=nil;
  89. if Fpstat(dirname,st)<0 then
  90. exit;
  91. { Is it a dir ? }
  92. if not((st.st_mode and $f000)=$4000)then
  93. begin
  94. errno:=ESysENOTDIR;
  95. exit
  96. end;
  97. { Open it}
  98. fd:=Fpopen(dirname,O_RDONLY,438);
  99. if fd<0 then
  100. exit;
  101. new(ptr);
  102. if ptr=nil then
  103. exit;
  104. getmem(ptr^.dd_buf,sizeof(dirent));
  105. if ptr^.dd_buf=nil then
  106. exit;
  107. ptr^.dd_fd:=fd;
  108. ptr^.dd_loc:=0;
  109. ptr^.dd_size:=0;
  110. ptr^.dd_max:=sizeof(ptr^.dd_buf^);
  111. Fpopendir:=ptr;
  112. end;
  113. function Fpclosedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  114. begin
  115. Fpclosedir:=Fpclose(dirp^.dd_fd);
  116. freemem(dirp^.dd_buf,sizeof(dirent));
  117. dispose(dirp);
  118. end;
  119. function Fpreaddir(dirp : pdir) : pdirent; [public, alias : 'FPC_SYSC_READDIR'];
  120. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  121. missing of the readdir syscall.
  122. Getdents requires the buffer to be larger than the blocksize.
  123. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  124. with blockmode have this higher?}
  125. begin
  126. if do_SysCall(SysCall_nr_readdir,TSysParam(dirp^.dd_fd),TSysParam(dirp^.dd_buf),TSysParam(1))=0 Then
  127. { the readdir system call returns the number of bytes written }
  128. Fpreaddir:=nil
  129. else
  130. Fpreaddir:=dirp^.dd_buf
  131. end;
  132. {*****************************************************************************
  133. --- Process:Process & program handling - related calls ---
  134. *****************************************************************************}
  135. procedure Fpexit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  136. begin
  137. do_syscall(syscall_nr_exit,status);
  138. end;
  139. {
  140. Change action of process upon receipt of a signal.
  141. Signum specifies the signal (all except SigKill and SigStop).
  142. If Act is non-nil, it is used to specify the new action.
  143. If OldAct is non-nil the previous action is saved there.
  144. }
  145. function Fpsigaction(sig: cint; act : psigactionrec; oact : psigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  146. {
  147. Change action of process upon receipt of a signal.
  148. Signum specifies the signal (all except SigKill and SigStop).
  149. If Act is non-nil, it is used to specify the new action.
  150. If OldAct is non-nil the previous action is saved there.
  151. }
  152. begin
  153. do_syscall(syscall_nr_sigaction,TSysParam(sig),TSysParam(act),TSysParam(oact));
  154. end;
  155. function Fpftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  156. { See notes lseek. This one is completely similar for the parameter (but
  157. doesn't have the returnvalue 64-bit problem)}
  158. begin
  159. Fpftruncate:=Do_syscall(syscall_nr_ftruncate,TSysParam(fd),TSysParam(flength));
  160. end;
  161. function Fpfstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  162. begin
  163. FpFStat:=do_SysCall(syscall_nr_fstat,TSysParam(fd),TSysParam(@sb));
  164. end;
  165. function Fpfork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  166. {
  167. This function issues the 'fork' System call. the program is duplicated in memory
  168. and Execution continues in parent and child process.
  169. In the parent process, fork returns the PID of the child. In the child process,
  170. zero is returned.
  171. A negative value indicates that an error has occurred, the error is returned in
  172. LinuxError.
  173. }
  174. Begin
  175. Fpfork:=Do_syscall(SysCall_nr_fork);
  176. End;
  177. // Look at execve variants later, when overloaded is determined.
  178. {
  179. function Fpexecve(path : pathstr; argv : ppchar; envp: ppchar): cint;
  180. }
  181. {
  182. Replaces the current program by the program specified in path,
  183. arguments in args are passed to Execve.
  184. environment specified in ep is passed on.
  185. }
  186. {
  187. Begin
  188. path:=path+#0;
  189. do_syscall(syscall_nr_Execve,TSysParam(@path[1]),TSysParam(Argv),TSysParam(envp));
  190. End;
  191. }
  192. {
  193. function Fpexecve(path : pchar; argv : ppchar; envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  194. }
  195. {
  196. Replaces the current program by the program specified in path,
  197. arguments in args are passed to Execve.
  198. environment specified in ep is passed on.
  199. }
  200. {
  201. Begin
  202. do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(Argv),TSysParam(envp));
  203. End;
  204. }
  205. function Fpwaitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  206. {
  207. Waits until a child with PID Pid exits, or returns if it is exited already.
  208. Any resources used by the child are freed.
  209. The exit status is reported in the adress referred to by Status. It should
  210. be a longint.
  211. }
  212. begin
  213. FpWaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options);
  214. end;
  215. function Fpaccess(pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  216. {
  217. Test users access rights on the specified file.
  218. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  219. R,W,X stand for read,write and Execute access, simultaneously.
  220. F_OK checks whether the test would be allowed on the file.
  221. i.e. It checks the search permissions in all directory components
  222. of the path.
  223. The test is done with the real user-ID, instead of the effective.
  224. If access is denied, or an error occurred, false is returned.
  225. If access is granted, true is returned.
  226. Errors other than no access,are reported in unixerror.
  227. }
  228. begin
  229. FpAccess:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  230. end;
  231. { overloaded
  232. function Fpaccess(pathname : pathstr; amode : cint): cint;
  233. {
  234. Test users access rights on the specified file.
  235. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  236. R,W,X stand for read,write and Execute access, simultaneously.
  237. F_OK checks whether the test would be allowed on the file.
  238. i.e. It checks the search permissions in all directory components
  239. of the path.
  240. The test is done with the real user-ID, instead of the effective.
  241. If access is denied, or an error occurred, false is returned.
  242. If access is granted, true is returned.
  243. Errors other than no access,are reported in unixerror.
  244. }
  245. begin
  246. pathname:=pathname+#0;
  247. Access:=do_syscall(syscall_nr_access, TSysParam(@pathname[1]),mode)=0;
  248. end;
  249. }
  250. Function FpDup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  251. begin
  252. Fpdup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  253. end;
  254. Function FpDup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  255. begin
  256. Fpdup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  257. end;
  258. CONST
  259. { Constansts for MMAP }
  260. MAP_PRIVATE =2;
  261. MAP_ANONYMOUS =$20;
  262. {Constansts Termios/Ioctl (used in Do_IsDevice) }
  263. IOCtl_TCGETS=$5401; // TCGETS is also in termios.inc, but the sysunix needs only this
  264. type
  265. tmmapargs=packed record
  266. address : longint;
  267. size : longint;
  268. prot : longint;
  269. flags : longint;
  270. fd : longint;
  271. offset : longint;
  272. end;
  273. {$ifdef cpui386}
  274. {$define OLDMMAP}
  275. {$endif cpui386}
  276. {$ifdef cpum68k}
  277. {$define OLDMMAP}
  278. {$endif cpum68k}
  279. Function Fpmmap(adr:pointer;len:size_t;prot:cint;flags:cint;fd:cint;off:off_t):pointer; [public, alias : 'FPC_SYSC_MMAP'];
  280. // OFF_T procedure, and returns a pointer, NOT cint.
  281. {$ifdef OLDMMAP}
  282. var
  283. mmapargs : tmmapargs;
  284. begin
  285. mmapargs.address:=TSysParam(adr);
  286. mmapargs.size:=TSysParam(len);
  287. mmapargs.prot:=TSysParam(prot);
  288. mmapargs.flags:=TSysParam(flags);
  289. mmapargs.fd:=TSysParam(fd);
  290. mmapargs.offset:=TSysParam(off);
  291. Fpmmap:=pointer(do_syscall(syscall_nr_mmap,TSysParam(@MMapArgs)));
  292. end;
  293. {$else OLDMMAP}
  294. begin
  295. Fpmmap:= pointer(do_syscall(syscall_nr_mmap,TSysParam(adr),TSysParam(len),
  296. TSysParam(prot),TSysParam(flags),TSysParam(fd),TSysParam(off)));
  297. end;
  298. {$endif OLDMMAP}
  299. Function Fpmunmap(adr:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  300. begin
  301. Fpmunmap:=do_syscall(syscall_nr_munmap,TSysParam(Adr),TSysParam(Len));
  302. end;
  303. Function sbrk(size : longint) : pointer;
  304. begin
  305. sbrk:=Fpmmap(0,Size,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0);
  306. if sbrk=pointer(-1) then
  307. sbrk:=nil
  308. else
  309. errno:=0;
  310. end;
  311. {
  312. Interface to Unix ioctl call.
  313. Performs various operations on the filedescriptor Handle.
  314. Ndx describes the operation to perform.
  315. Data points to data needed for the Ndx function. The structure of this
  316. data is function-dependent.
  317. }
  318. // prototype is cint __P(cint,culong,....)
  319. // actual meaning of return value depends on request.
  320. Function FpIOCtl(fd:cint;request:culong;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  321. // This was missing here, instead hardcoded in Do_IsDevice
  322. begin
  323. FpIOCtl:=do_SysCall(syscall_nr_ioctl,tsysparam(fd),tsysparam(Request),TSysParam(data));
  324. end;
  325. Function Do_IsDevice(Handle:cint):boolean;
  326. {
  327. Interface to Unix ioctl call.
  328. Performs various operations on the filedescriptor Handle.
  329. Ndx describes the operation to perform.
  330. Data points to data needed for the Ndx function. The structure of this
  331. data is function-dependent.
  332. }
  333. var
  334. Data : array[0..255] of byte; {Large enough for termios info}
  335. begin
  336. Do_IsDevice:=(Fpioctl(handle,IOCTL_TCGETS,@data)<>-1);
  337. end;
  338. Function FpGetPid:pid_t; [public, alias : 'FPC_SYSC_GETPID'];
  339. {
  340. Get Process ID.
  341. }
  342. begin
  343. FpGetPID:=do_syscall(syscall_nr_getpid);
  344. end;
  345. Function FpReadLink(name,linkname:pchar;maxlen:size_t):cint; [public, alias : 'FPC_SYSC_READLINK'];
  346. begin
  347. Fpreadlink:=do_syscall(syscall_nr_readlink, TSysParam(name),TSysParam(linkname),maxlen);
  348. end;
  349. Function FpNanoSleep(req : ptimespec;rem : ptimespec):cint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  350. begin
  351. FpNanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(req),TSysParam(rem));
  352. end;
  353. // The following belongs here, but this should be researched more.
  354. // function Fpgetcwd(pt:pchar; _size:size_t):pchar;[public, alias :'FPC_SYSC_GETCWD'];
  355. function fpgettimeofday(tp: ptimeval;tzp:ptimezone):cint; [public, alias: 'FPC_SYSC_GETTIMEOFDAY'];
  356. begin
  357. fpgettimeofday:=do_syscall(syscall_nr_gettimeofday,TSysParam(tp),TSysParam(tzp));
  358. end;
  359. {$ENDIF}
  360. {
  361. $Log$
  362. Revision 1.9 2003-10-17 20:56:24 olle
  363. * Changed m68k to cpum68k, i386 to cpui386
  364. Revision 1.8 2003/09/27 13:45:58 peter
  365. * fpnanosleep exported in baseunix
  366. * fpnanosleep has pointer arguments to be C compliant
  367. Revision 1.7 2003/09/27 12:58:23 peter
  368. * mmap returns -1 on error
  369. Revision 1.6 2003/09/27 11:52:35 peter
  370. * sbrk returns pointer
  371. Revision 1.5 2003/09/15 20:29:50 marco
  372. * small fix
  373. Revision 1.4 2003/09/14 20:15:01 marco
  374. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  375. Revision 1.3 2003/05/11 16:07:55 jonas
  376. * fixed mmap for non-i386 non-m68k architectures (not sure about
  377. x86-64)
  378. Revision 1.2 2002/12/18 17:38:01 marco
  379. * small fix, new rtl now cycles
  380. Revision 1.1 2002/12/18 16:43:26 marco
  381. * new unix rtl, linux part.....
  382. Revision 1.1 2002/11/12 14:40:18 marco
  383. * The syscall core of the new system unit.
  384. }