ossysc.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. {$if defined(cpui386) or defined(cpuarm) or defined(cpux86_64)}
  165. {$define NEED_USER_TRAMPOLINE}
  166. {$endif}
  167. {$if defined(cpui386) or defined(cpuarm)}
  168. {$define NEED_USER_TRAMPOLINE_RT_DIFFERENT}
  169. {$endif}
  170. {$ifdef NEED_USER_TRAMPOLINE}
  171. procedure linux_restore; cdecl; nostackframe; assembler;
  172. asm
  173. {$ifdef cpuarm}
  174. swi __NR_sigreturn;
  175. {$endif}
  176. {$ifdef cpui386}
  177. popl %eax
  178. movl $syscall_nr_sigreturn, %eax
  179. int $0x80
  180. {$endif}
  181. {$ifdef cpux86_64}
  182. movq $syscall_nr_rt_sigreturn, %rax
  183. syscall
  184. {$endif}
  185. end;
  186. {$endif}
  187. {$ifdef NEED_USER_TRAMPOLINE_RT_DIFFERENT}
  188. procedure linux_restore_rt; cdecl; nostackframe; assembler;
  189. asm
  190. {$ifdef cpuarm}
  191. swi syscall_nr_rt_sigreturn
  192. {$endif}
  193. {$ifdef cpui386}
  194. movl $syscall_nr_rt_sigreturn, %eax
  195. int $0x80
  196. {$endif}
  197. end;
  198. {$endif}
  199. function Fpsigaction(sig: cint; new_action, old_action: psigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  200. {
  201. Change action of process upon receipt of a signal.
  202. Signum specifies the signal (all except SigKill and SigStop).
  203. If Act is non-nil, it is used to specify the new action.
  204. If OldAct is non-nil the previous action is saved there.
  205. }
  206. begin
  207. {$ifdef cpusparc}
  208. { Sparc has an extra stub parameter }
  209. Fpsigaction:=do_syscall(syscall_nr_rt_sigaction,TSysParam(sig),TSysParam(new_action),TSysParam(old_action),TSysParam(PtrInt(@Fprt_sigreturn_stub)-8),TSysParam(8));
  210. {$else cpusparc}
  211. {$ifdef NEED_USER_TRAMPOLINE}
  212. if new_action <> nil then
  213. begin
  214. // a different stack (SA_ONSTACK) requires sa_restorer field
  215. if (new_action^.sa_flags and (SA_RESTORER or SA_ONSTACK)) = 0 then
  216. begin
  217. new_action^.sa_flags := new_action^.sa_flags or SA_RESTORER;
  218. {$ifdef NEED_USER_TRAMPOLINE_RT_DIFFERENT}
  219. if (new_action^.sa_flags and SA_SIGINFO) <> 0 then
  220. new_action^.sa_restorer := @linux_restore_rt
  221. else
  222. {$endif}
  223. new_action^.sa_restorer := @linux_restore;
  224. end;
  225. end;
  226. {$endif}
  227. Fpsigaction:=do_syscall(syscall_nr_rt_sigaction,TSysParam(sig),TSysParam(new_action),TSysParam(old_action),TSysParam(8));
  228. {$endif cpusparc}
  229. end;
  230. function Fpftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  231. { See notes lseek. This one is completely similar for the parameter (but
  232. doesn't have the returnvalue 64-bit problem)}
  233. begin
  234. Fpftruncate:=Do_syscall(syscall_nr_ftruncate,TSysParam(fd),TSysParam(flength));
  235. end;
  236. function Fpfstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  237. begin
  238. FpFStat:=do_SysCall(syscall_nr_fstat,TSysParam(fd),TSysParam(@sb));
  239. end;
  240. {$ifndef FPC_SYSTEM_HAS_FPFORK}
  241. function Fpfork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  242. {
  243. This function issues the 'fork' System call. the program is duplicated in memory
  244. and Execution continues in parent and child process.
  245. In the parent process, fork returns the PID of the child. In the child process,
  246. zero is returned.
  247. A negative value indicates that an error has occurred, the error is returned in
  248. LinuxError.
  249. }
  250. Begin
  251. Fpfork:=Do_syscall(SysCall_nr_fork);
  252. End;
  253. {$endif FPC_SYSTEM_HAS_FPFORK}
  254. // Look at execve variants later, when overloaded is determined.
  255. {
  256. function Fpexecve(path : pathstr; argv : ppchar; envp: ppchar): cint;
  257. }
  258. {
  259. Replaces the current program by the program specified in path,
  260. arguments in args are passed to Execve.
  261. environment specified in ep is passed on.
  262. }
  263. {
  264. Begin
  265. path:=path+#0;
  266. do_syscall(syscall_nr_Execve,TSysParam(@path[1]),TSysParam(Argv),TSysParam(envp));
  267. End;
  268. }
  269. {
  270. function Fpexecve(path : pchar; argv : ppchar; envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  271. }
  272. {
  273. Replaces the current program by the program specified in path,
  274. arguments in args are passed to Execve.
  275. environment specified in ep is passed on.
  276. }
  277. {
  278. Begin
  279. do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(Argv),TSysParam(envp));
  280. End;
  281. }
  282. {$ifdef CPUARM}
  283. {$define WAIT4}
  284. {$endif CPUARM}
  285. {$ifdef CPUx86_64}
  286. {$define WAIT4}
  287. {$endif CPUx86_64}
  288. {$ifdef CPUSPARC}
  289. {$define WAIT4}
  290. {$endif CPUSPARC}
  291. function Fpwaitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  292. {
  293. Waits until a child with PID Pid exits, or returns if it is exited already.
  294. Any resources used by the child are freed.
  295. The exit status is reported in the adress referred to by Status. It should
  296. be a longint.
  297. }
  298. begin
  299. {$ifdef WAIT4}
  300. FpWaitPID:=do_syscall(syscall_nr_Wait4,PID,TSysParam(Stat_loc),options,0);
  301. {$else WAIT4}
  302. FpWaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options);
  303. {$endif WAIT4}
  304. end;
  305. function Fpaccess(pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  306. {
  307. Test users access rights on the specified file.
  308. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  309. R,W,X stand for read,write and Execute access, simultaneously.
  310. F_OK checks whether the test would be allowed on the file.
  311. i.e. It checks the search permissions in all directory components
  312. of the path.
  313. The test is done with the real user-ID, instead of the effective.
  314. If access is denied, or an error occurred, false is returned.
  315. If access is granted, true is returned.
  316. Errors other than no access,are reported in unixerror.
  317. }
  318. begin
  319. FpAccess:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  320. end;
  321. (* overloaded
  322. function Fpaccess(pathname : pathstr; amode : cint): cint;
  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. pathname:=pathname+#0;
  337. Access:=do_syscall(syscall_nr_access, TSysParam(@pathname[1]),mode)=0;
  338. end;
  339. *)
  340. Function FpDup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  341. begin
  342. Fpdup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  343. end;
  344. Function FpDup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  345. begin
  346. Fpdup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  347. end;
  348. type
  349. tmmapargs = packed record
  350. address : TSysParam;
  351. size : TSysParam;
  352. prot : TSysParam;
  353. flags : TSysParam;
  354. fd : TSysParam;
  355. offset : TSysParam;
  356. end;
  357. {$ifdef cpui386}
  358. {$define OLDMMAP}
  359. {$endif cpui386}
  360. {$ifdef cpum68k}
  361. {$define OLDMMAP}
  362. {$endif cpum68k}
  363. {$ifdef cpuarm}
  364. {$define OLDMMAP}
  365. {$endif cpuarm}
  366. Function Fpmmap(adr:pointer;len:size_t;prot:cint;flags:cint;fd:cint;off:off_t):pointer; [public, alias : 'FPC_SYSC_MMAP'];
  367. // OFF_T procedure, and returns a pointer, NOT cint.
  368. {$ifdef OLDMMAP}
  369. var
  370. mmapargs : tmmapargs;
  371. begin
  372. mmapargs.address:=TSysParam(adr);
  373. mmapargs.size:=TSysParam(len);
  374. mmapargs.prot:=TSysParam(prot);
  375. mmapargs.flags:=TSysParam(flags);
  376. mmapargs.fd:=TSysParam(fd);
  377. mmapargs.offset:=TSysParam(off);
  378. Fpmmap:=pointer(do_syscall(syscall_nr_mmap,TSysParam(@MMapArgs)));
  379. end;
  380. {$else OLDMMAP}
  381. begin
  382. Fpmmap:= pointer(do_syscall(syscall_nr_mmap,TSysParam(adr),TSysParam(len),
  383. TSysParam(prot),TSysParam(flags),TSysParam(fd),TSysParam(off)));
  384. end;
  385. {$endif OLDMMAP}
  386. Function Fpmunmap(adr:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  387. begin
  388. Fpmunmap:=do_syscall(syscall_nr_munmap,TSysParam(Adr),TSysParam(Len));
  389. end;
  390. {
  391. Interface to Unix ioctl call.
  392. Performs various operations on the filedescriptor Handle.
  393. Ndx describes the operation to perform.
  394. Data points to data needed for the Ndx function. The structure of this
  395. data is function-dependent.
  396. }
  397. // prototype is cint __P(cint,culong,....)
  398. // actual meaning of return value depends on request.
  399. Function FpIOCtl(fd:cint;request:culong;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  400. // This was missing here, instead hardcoded in Do_IsDevice
  401. begin
  402. FpIOCtl:=do_SysCall(syscall_nr_ioctl,tsysparam(fd),tsysparam(Request),TSysParam(data));
  403. end;
  404. Function FpGetPid:pid_t; [public, alias : 'FPC_SYSC_GETPID'];
  405. {
  406. Get Process ID.
  407. }
  408. begin
  409. FpGetPID:=do_syscall(syscall_nr_getpid);
  410. end;
  411. Function FpReadLink(name,linkname:pchar;maxlen:size_t):cint; [public, alias : 'FPC_SYSC_READLINK'];
  412. begin
  413. Fpreadlink:=do_syscall(syscall_nr_readlink, TSysParam(name),TSysParam(linkname),maxlen);
  414. end;
  415. function FPSigProcMask(how:cint;nset : psigset;oset : psigset):cint; [public, alias : 'FPC_SYSC_SIGPROCMASK'];
  416. {
  417. Change the list of currently blocked signals.
  418. How determines which signals will be blocked :
  419. SigBlock : Add SSet to the current list of blocked signals
  420. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  421. SigSetMask : Set the list of blocked signals to SSet
  422. if OldSSet is non-null, the old set will be saved there.
  423. }
  424. begin
  425. FPsigprocmask:=do_syscall(syscall_nr_rt_sigprocmask,TSysParam(how),TSysParam(nset),TSysParam(oset),TSysParam(8));
  426. end;
  427. Function FpNanoSleep(req : ptimespec;rem : ptimespec):cint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  428. begin
  429. FpNanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(req),TSysParam(rem));
  430. end;
  431. function fpgetcwd(path : pchar; siz:tsize):pchar; [public, alias : 'FPC_SYSC_GETCWD'];
  432. begin
  433. fpgetcwd:=pchar(Do_Syscall(Syscall_nr_getcwd,TSysParam(Path),TSysParam(siz)));
  434. end;
  435. function fpgettimeofday(tp: ptimeval;tzp:ptimezone):cint; [public, alias: 'FPC_SYSC_GETTIMEOFDAY'];
  436. begin
  437. fpgettimeofday:=do_syscall(syscall_nr_gettimeofday,TSysParam(tp),TSysParam(tzp));
  438. end;
  439. function FpGetRLimit(resource : cInt; rlim : PRLimit) : cInt;
  440. begin
  441. FpGetRLimit := do_syscall(syscall_nr_getrlimit,
  442. TSysParam(resource), TSysParam(rlim));
  443. end;
  444. {$ifdef HAS_UGETRLIMIT}
  445. function fpugetrlimit(resource : cInt; rlim : PRLimit) : cInt;
  446. begin
  447. FpUGetRLimit := do_syscall(syscall_nr_ugetrlimit,
  448. TSysParam(resource), TSysParam(rlim));
  449. end;
  450. {$endif}