ossysc.inc 16 KB

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