ossysc.inc 17 KB

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