ossysc.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. {
  2. Copyright (c) 2002 by Marco van de Voort
  3. The base Linux syscalls required to implement the system unit. These
  4. are aliased for use in other units (to avoid poluting the system units
  5. interface)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. ****************************************************************************
  12. }
  13. {*****************************************************************************
  14. --- Main:The System Call Self ---
  15. *****************************************************************************}
  16. function Fptime(tloc:pTime): TTime; [public, alias : 'FPC_SYSC_TIME'];
  17. begin
  18. Fptime:=do_syscall(syscall_nr_time,TSysParam(tloc));
  19. End;
  20. {*****************************************************************************
  21. --- File:File handling related calls ---
  22. *****************************************************************************}
  23. function Fpopen(path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  24. Begin
  25. Fpopen:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags),TSysParam(mode));
  26. End;
  27. function Fpclose(fd : cint): cint; [public, alias : 'FPC_SYSC_CLOSE'];
  28. begin
  29. Fpclose:=do_syscall(syscall_nr_close,fd);
  30. end;
  31. function Fplseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  32. {
  33. Must be adapted/overloaded for 64-bit support, but that is a different call under
  34. Linux?
  35. }
  36. begin
  37. Fplseek:=do_syscall(syscall_nr_lseek,tsysparam(fd),tsysparam(offset),tsysparam(whence));
  38. end;
  39. function Fpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  40. begin
  41. Fpread:=do_syscall(syscall_nr_read,Fd,TSysParam(buf),nbytes);
  42. end;
  43. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  44. begin
  45. Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  46. end;
  47. {
  48. function Fppread(fd: cint; buf: pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PREAD'];
  49. begin
  50. Fpread:=do_syscall(syscall_nr_pread,Fd,TSysParam(buf),nbytes,offset);
  51. end;
  52. function Fppwrite(fd: cint;buf:pchar; nbytes : size_t; offset:Toff): ssize_t; [public, alias : 'FPC_SYSC_PWRITE'];
  53. begin
  54. Fpwrite:=do_syscall(syscall_nr_pwrite,Fd,TSysParam(buf),nbytes,offset);
  55. end;
  56. }
  57. function Fpunlink(path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  58. begin
  59. Fpunlink:=do_syscall(syscall_nr_unlink,TSysParam(path));
  60. end;
  61. function Fprename(old : pchar; newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  62. begin
  63. Fprename:=do_syscall(syscall_nr_rename,TSysParam(old),TSysParam(newpath));
  64. end;
  65. function Fpstat(path: pchar; var buf : stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  66. begin
  67. Fpstat:=do_syscall(syscall_nr_stat,TSysParam(path),TSysParam(@buf));
  68. end;
  69. {*****************************************************************************
  70. --- Directory:Directory related calls ---
  71. *****************************************************************************}
  72. function Fpchdir(path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  73. begin
  74. Fpchdir:=do_syscall(syscall_nr_chdir,TSysParam(path));
  75. end;
  76. function Fpmkdir(path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  77. begin
  78. Fpmkdir:=do_syscall(syscall_nr_mkdir,TSysParam(path),TSysParam(mode));
  79. end;
  80. function Fprmdir(path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  81. begin
  82. Fprmdir:=do_syscall(syscall_nr_rmdir,TSysParam(path));
  83. end;
  84. function Fpopendir(dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  85. var
  86. fd:integer;
  87. st:stat;
  88. ptr:pdir;
  89. begin
  90. Fpopendir:=nil;
  91. if Fpstat(dirname,st)<0 then
  92. exit;
  93. { Is it a dir ? }
  94. if not((st.st_mode and $f000)=$4000)then
  95. begin
  96. errno:=ESysENOTDIR;
  97. exit
  98. end;
  99. { Open it}
  100. fd:=Fpopen(dirname,O_RDONLY,438);
  101. if fd<0 then
  102. exit;
  103. new(ptr);
  104. if ptr=nil then
  105. exit;
  106. getmem(ptr^.dd_buf,sizeof(dirent));
  107. if ptr^.dd_buf=nil then
  108. exit;
  109. ptr^.dd_fd:=fd;
  110. ptr^.dd_loc:=0;
  111. ptr^.dd_size:=0;
  112. ptr^.dd_nextoff:=0;
  113. ptr^.dd_max:=sizeof(ptr^.dd_buf^);
  114. Fpopendir:=ptr;
  115. end;
  116. function Fpclosedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  117. begin
  118. Fpclosedir:=Fpclose(dirp^.dd_fd);
  119. freemem(dirp^.dd_buf,sizeof(dirent));
  120. dispose(dirp);
  121. end;
  122. Function Fpreaddir(dirp : pdir) : pdirent; [public, alias: 'FPC_SYSC_READDIR'];
  123. var bytes : longint;
  124. dp : pdirent;
  125. begin
  126. repeat
  127. if dirp^.dd_nextoff >= dirp^.dd_size then
  128. begin
  129. bytes := do_SysCall(SysCall_nr_getdents,TSysParam(dirp^.dd_fd),TSysParam(dirp^.dd_buf),TSysParam(dirp^.dd_max));
  130. if bytes <= 0 then
  131. begin
  132. fpreaddir := nil;
  133. exit;
  134. end;
  135. dirp^.dd_size := bytes;
  136. dirp^.dd_nextoff := 0;
  137. end;
  138. dp := pdirent(ptrint(dirp^.dd_buf)+dirp^.dd_nextoff);
  139. inc(dirp^.dd_nextoff,dp^.d_reclen);
  140. inc(dirp^.dd_loc,dp^.d_reclen);
  141. until dp^.d_fileno <> 0; // Don't show deleted files
  142. Fpreaddir := dp;
  143. end;
  144. {*****************************************************************************
  145. --- Process:Process & program handling - related calls ---
  146. *****************************************************************************}
  147. procedure Fpexit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  148. begin
  149. do_syscall(syscall_nr_exit,status);
  150. end;
  151. {
  152. Change action of process upon receipt of a signal.
  153. Signum specifies the signal (all except SigKill and SigStop).
  154. If Act is non-nil, it is used to specify the new action.
  155. If OldAct is non-nil the previous action is saved there.
  156. }
  157. {$ifdef cpusparc}
  158. procedure Fprt_sigreturn_stub;assembler;nostackframe;
  159. asm
  160. mov syscall_nr_rt_sigreturn,%g1
  161. ta 0x10
  162. end;
  163. {$endif cpusparc}
  164. function Fpsigaction(sig: cint; act : psigactionrec; oact : psigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  165. {
  166. Change action of process upon receipt of a signal.
  167. Signum specifies the signal (all except SigKill and SigStop).
  168. If Act is non-nil, it is used to specify the new action.
  169. If OldAct is non-nil the previous action is saved there.
  170. }
  171. begin
  172. {$ifdef cpusparc}
  173. { Sparc has an extra stub parameter }
  174. Fpsigaction:=do_syscall(syscall_nr_rt_sigaction,TSysParam(sig),TSysParam(act),TSysParam(oact),TSysParam(PtrInt(@Fprt_sigreturn_stub)-8),TSysParam(8));
  175. {$else cpusparc}
  176. Fpsigaction:=do_syscall(syscall_nr_rt_sigaction,TSysParam(sig),TSysParam(act),TSysParam(oact),TSysParam(8));
  177. {$endif cpusparc}
  178. end;
  179. function Fpftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  180. { See notes lseek. This one is completely similar for the parameter (but
  181. doesn't have the returnvalue 64-bit problem)}
  182. begin
  183. Fpftruncate:=Do_syscall(syscall_nr_ftruncate,TSysParam(fd),TSysParam(flength));
  184. end;
  185. function Fpfstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  186. begin
  187. FpFStat:=do_SysCall(syscall_nr_fstat,TSysParam(fd),TSysParam(@sb));
  188. end;
  189. {$ifndef FPC_SYSTEM_HAS_FPFORK}
  190. function Fpfork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  191. {
  192. This function issues the 'fork' System call. the program is duplicated in memory
  193. and Execution continues in parent and child process.
  194. In the parent process, fork returns the PID of the child. In the child process,
  195. zero is returned.
  196. A negative value indicates that an error has occurred, the error is returned in
  197. LinuxError.
  198. }
  199. Begin
  200. Fpfork:=Do_syscall(SysCall_nr_fork);
  201. End;
  202. {$endif FPC_SYSTEM_HAS_FPFORK}
  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. {$ifdef CPUSPARC}
  238. {$define WAIT4}
  239. {$endif CPUSPARC}
  240. function Fpwaitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  241. {
  242. Waits until a child with PID Pid exits, or returns if it is exited already.
  243. Any resources used by the child are freed.
  244. The exit status is reported in the adress referred to by Status. It should
  245. be a longint.
  246. }
  247. begin
  248. {$ifdef WAIT4}
  249. FpWaitPID:=do_syscall(syscall_nr_Wait4,PID,TSysParam(Stat_loc),options,0);
  250. {$else WAIT4}
  251. FpWaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options);
  252. {$endif WAIT4}
  253. end;
  254. function Fpaccess(pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  255. {
  256. Test users access rights on the specified file.
  257. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  258. R,W,X stand for read,write and Execute access, simultaneously.
  259. F_OK checks whether the test would be allowed on the file.
  260. i.e. It checks the search permissions in all directory components
  261. of the path.
  262. The test is done with the real user-ID, instead of the effective.
  263. If access is denied, or an error occurred, false is returned.
  264. If access is granted, true is returned.
  265. Errors other than no access,are reported in unixerror.
  266. }
  267. begin
  268. FpAccess:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  269. end;
  270. (* overloaded
  271. function Fpaccess(pathname : pathstr; amode : cint): cint;
  272. {
  273. Test users access rights on the specified file.
  274. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  275. R,W,X stand for read,write and Execute access, simultaneously.
  276. F_OK checks whether the test would be allowed on the file.
  277. i.e. It checks the search permissions in all directory components
  278. of the path.
  279. The test is done with the real user-ID, instead of the effective.
  280. If access is denied, or an error occurred, false is returned.
  281. If access is granted, true is returned.
  282. Errors other than no access,are reported in unixerror.
  283. }
  284. begin
  285. pathname:=pathname+#0;
  286. Access:=do_syscall(syscall_nr_access, TSysParam(@pathname[1]),mode)=0;
  287. end;
  288. *)
  289. Function FpDup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  290. begin
  291. Fpdup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  292. end;
  293. Function FpDup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  294. begin
  295. Fpdup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  296. end;
  297. type
  298. tmmapargs = packed record
  299. address : TSysParam;
  300. size : TSysParam;
  301. prot : TSysParam;
  302. flags : TSysParam;
  303. fd : TSysParam;
  304. offset : TSysParam;
  305. end;
  306. {$ifdef cpui386}
  307. {$define OLDMMAP}
  308. {$endif cpui386}
  309. {$ifdef cpum68k}
  310. {$define OLDMMAP}
  311. {$endif cpum68k}
  312. {$ifdef cpuarm}
  313. {$define OLDMMAP}
  314. {$endif cpuarm}
  315. Function Fpmmap(adr:pointer;len:size_t;prot:cint;flags:cint;fd:cint;off:off_t):pointer; [public, alias : 'FPC_SYSC_MMAP'];
  316. // OFF_T procedure, and returns a pointer, NOT cint.
  317. {$ifdef OLDMMAP}
  318. var
  319. mmapargs : tmmapargs;
  320. begin
  321. mmapargs.address:=TSysParam(adr);
  322. mmapargs.size:=TSysParam(len);
  323. mmapargs.prot:=TSysParam(prot);
  324. mmapargs.flags:=TSysParam(flags);
  325. mmapargs.fd:=TSysParam(fd);
  326. mmapargs.offset:=TSysParam(off);
  327. Fpmmap:=pointer(do_syscall(syscall_nr_mmap,TSysParam(@MMapArgs)));
  328. end;
  329. {$else OLDMMAP}
  330. begin
  331. Fpmmap:= pointer(do_syscall(syscall_nr_mmap,TSysParam(adr),TSysParam(len),
  332. TSysParam(prot),TSysParam(flags),TSysParam(fd),TSysParam(off)));
  333. end;
  334. {$endif OLDMMAP}
  335. Function Fpmunmap(adr:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  336. begin
  337. Fpmunmap:=do_syscall(syscall_nr_munmap,TSysParam(Adr),TSysParam(Len));
  338. end;
  339. {
  340. Interface to Unix ioctl call.
  341. Performs various operations on the filedescriptor Handle.
  342. Ndx describes the operation to perform.
  343. Data points to data needed for the Ndx function. The structure of this
  344. data is function-dependent.
  345. }
  346. // prototype is cint __P(cint,culong,....)
  347. // actual meaning of return value depends on request.
  348. Function FpIOCtl(fd:cint;request:culong;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  349. // This was missing here, instead hardcoded in Do_IsDevice
  350. begin
  351. FpIOCtl:=do_SysCall(syscall_nr_ioctl,tsysparam(fd),tsysparam(Request),TSysParam(data));
  352. end;
  353. Function FpGetPid:pid_t; [public, alias : 'FPC_SYSC_GETPID'];
  354. {
  355. Get Process ID.
  356. }
  357. begin
  358. FpGetPID:=do_syscall(syscall_nr_getpid);
  359. end;
  360. Function FpReadLink(name,linkname:pchar;maxlen:size_t):cint; [public, alias : 'FPC_SYSC_READLINK'];
  361. begin
  362. Fpreadlink:=do_syscall(syscall_nr_readlink, TSysParam(name),TSysParam(linkname),maxlen);
  363. end;
  364. function FPSigProcMask(how:cint;nset : psigset;oset : psigset):cint; [public, alias : 'FPC_SYSC_SIGPROCMASK'];
  365. {
  366. Change the list of currently blocked signals.
  367. How determines which signals will be blocked :
  368. SigBlock : Add SSet to the current list of blocked signals
  369. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  370. SigSetMask : Set the list of blocked signals to SSet
  371. if OldSSet is non-null, the old set will be saved there.
  372. }
  373. begin
  374. FPsigprocmask:=do_syscall(syscall_nr_rt_sigprocmask,TSysParam(how),TSysParam(nset),TSysParam(oset),TSysParam(8));
  375. end;
  376. Function FpNanoSleep(req : ptimespec;rem : ptimespec):cint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  377. begin
  378. FpNanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(req),TSysParam(rem));
  379. end;
  380. function fpgetcwd(path : pchar; siz:tsize):pchar; [public, alias : 'FPC_SYSC_GETCWD'];
  381. begin
  382. fpgetcwd:=pchar(Do_Syscall(Syscall_nr_getcwd,TSysParam(Path),TSysParam(siz)));
  383. end;
  384. function fpgettimeofday(tp: ptimeval;tzp:ptimezone):cint; [public, alias: 'FPC_SYSC_GETTIMEOFDAY'];
  385. begin
  386. fpgettimeofday:=do_syscall(syscall_nr_gettimeofday,TSysParam(tp),TSysParam(tzp));
  387. end;
  388. function FpGetRLimit(resource : cInt; rlim : PRLimit) : cInt;
  389. begin
  390. FpGetRLimit := do_syscall(syscall_nr_getrlimit,
  391. TSysParam(resource), TSysParam(rlim));
  392. end;
  393. {$ifdef HAS_UGETRLIMIT}
  394. function fpugetrlimit(resource : cInt; rlim : PRLimit) : cInt;
  395. begin
  396. FpUGetRLimit := do_syscall(syscall_nr_ugetrlimit,
  397. TSysParam(resource), TSysParam(rlim));
  398. end;
  399. {$endif}