ossysc.inc 15 KB

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