ossysc.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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_t): Time_t; [public, alias : 'FPC_SYSC_TIME'];
  17. {$if defined(generic_linux_syscalls) or defined(FPC_USEGETTIMEOFDAY)}
  18. VAR tv : timeval;
  19. tz : timezone;
  20. retval : longint;
  21. begin
  22. Retval:=do_syscall(syscall_nr_gettimeofday,TSysParam(@tv),TSysParam(@tz));
  23. If retval=-1 then
  24. Fptime:=-1
  25. else
  26. Begin
  27. If Assigned(tloc) Then
  28. TLoc^:=tv.tv_sec;
  29. Fptime:=tv.tv_sec;
  30. End;
  31. End;
  32. {$else FPC_USEGETTIMEOFDAY}
  33. begin
  34. Fptime:=do_syscall(syscall_nr_time,TSysParam(tloc));
  35. end;
  36. {$endif FPC_USEGETTIMEOFDAY}
  37. {*****************************************************************************
  38. --- File:File handling related calls ---
  39. *****************************************************************************}
  40. function Fpopen(path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  41. Begin
  42. {$if defined(generic_linux_syscalls)}
  43. Fpopen:=do_syscall(syscall_nr_openat,AT_FDCWD,TSysParam(path),TSysParam(flags or O_LARGEFILE),TSysParam(mode));
  44. {$else}
  45. Fpopen:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags or O_LARGEFILE),TSysParam(mode));
  46. {$endif}
  47. End;
  48. function Fpclose(fd : cint): cint; [public, alias : 'FPC_SYSC_CLOSE'];
  49. begin
  50. Fpclose:=do_syscall(syscall_nr_close,fd);
  51. end;
  52. function Fplseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  53. begin
  54. {$if defined(cpu64)}
  55. result:=do_syscall(syscall_nr_lseek,tsysparam(fd),tsysparam(offset),tsysparam(whence));
  56. {$else}
  57. if do_syscall(syscall_nr__llseek,tsysparam(fd),
  58. tsysparam(hi(offset)),tsysparam(lo(offset)),
  59. tsysparam(@result), tsysparam(whence)) = -1 then
  60. result:=off_t(-1);
  61. {$endif}
  62. end;
  63. function Fpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  64. begin
  65. Fpread:=do_syscall(syscall_nr_read,Fd,TSysParam(buf),nbytes);
  66. end;
  67. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  68. begin
  69. Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  70. end;
  71. function Fpunlink(path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  72. begin
  73. {$if defined(generic_linux_syscalls)}
  74. Fpunlink:=do_syscall(syscall_nr_unlinkat,AT_FDCWD,TSysParam(path),0);
  75. {$else}
  76. Fpunlink:=do_syscall(syscall_nr_unlink,TSysParam(path));
  77. {$endif}
  78. end;
  79. function Fprename(old : pchar; newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  80. begin
  81. {$if defined(generic_linux_syscalls)}
  82. {$if defined(userenameat)}
  83. Fprename:=do_syscall(syscall_nr_renameat,AT_FDCWD,TSysParam(old),AT_FDCWD,TSysParam(newpath));
  84. {$else}
  85. Fprename:=do_syscall(syscall_nr_renameat2,AT_FDCWD,TSysParam(old),AT_FDCWD,TSysParam(newpath),0);
  86. {$endif}
  87. {$else}
  88. Fprename:=do_syscall(syscall_nr_rename,TSysParam(old),TSysParam(newpath));
  89. {$endif}
  90. end;
  91. function Fpstat(path: pchar; var buf: stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  92. begin
  93. { standard stat call is 32 bit on sparc64, so explicitly force a stat64 call }
  94. {$if (defined(cpu64) or defined(riscv32)) and not(defined(cpusparc64))}
  95. {$if defined(generic_linux_syscalls)}
  96. Fpstat:=do_syscall(syscall_nr_fstatat,AT_FDCWD,TSysParam(path),TSysParam(@buf),0);
  97. {$else}
  98. Fpstat:=do_syscall(syscall_nr_stat,TSysParam(path),TSysParam(@buf));
  99. {$endif}
  100. {$else}
  101. Fpstat:=do_syscall(syscall_nr_stat64,TSysParam(path),TSysParam(@buf));
  102. {$endif}
  103. end;
  104. {*****************************************************************************
  105. --- Directory:Directory related calls ---
  106. *****************************************************************************}
  107. function Fpchdir(path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  108. begin
  109. Fpchdir:=do_syscall(syscall_nr_chdir,TSysParam(path));
  110. end;
  111. function Fpmkdir(path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  112. begin
  113. {$if defined(generic_linux_syscalls)}
  114. Fpmkdir:=do_syscall(syscall_nr_mkdirat,AT_FDCWD,TSysParam(path),TSysParam(mode));
  115. {$else}
  116. Fpmkdir:=do_syscall(syscall_nr_mkdir,TSysParam(path),TSysParam(mode));
  117. {$endif}
  118. end;
  119. function Fprmdir(path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  120. begin
  121. {$if defined(generic_linux_syscalls)}
  122. Fprmdir:=do_syscall(syscall_nr_unlinkat,AT_FDCWD,TSysParam(path),AT_REMOVEDIR);
  123. {$else}
  124. Fprmdir:=do_syscall(syscall_nr_rmdir,TSysParam(path));
  125. {$endif}
  126. end;
  127. function Fpopendir(dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  128. var
  129. fd:cint;
  130. st:stat;
  131. ptr:pdir;
  132. begin
  133. Fpopendir:=nil;
  134. if Fpstat(dirname,st)<0 then
  135. exit;
  136. { Is it a dir ? }
  137. if not((st.st_mode and $f000)=$4000)then
  138. begin
  139. errno:=ESysENOTDIR;
  140. exit
  141. end;
  142. { Open it}
  143. fd:=Fpopen(dirname,O_RDONLY,438);
  144. if fd<0 then
  145. exit;
  146. new(ptr);
  147. if ptr=nil then
  148. exit;
  149. new(ptr^.dd_buf);
  150. if ptr^.dd_buf=nil then
  151. exit;
  152. ptr^.dd_fd:=fd;
  153. ptr^.dd_loc:=0;
  154. ptr^.dd_size:=0;
  155. ptr^.dd_nextoff:=0;
  156. ptr^.dd_max:=sizeof(ptr^.dd_buf^);
  157. Fpopendir:=ptr;
  158. end;
  159. function Fpclosedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  160. begin
  161. repeat
  162. Fpclosedir:=Fpclose(dirp^.dd_fd);
  163. until (Fpclosedir=0) or (errno<>ESysEINTR);
  164. dispose(dirp^.dd_buf);
  165. dispose(dirp);
  166. end;
  167. Function Fpreaddir(dirp : pdir) : pdirent; [public, alias: 'FPC_SYSC_READDIR'];
  168. var bytes : longint;
  169. dp : pdirent;
  170. begin
  171. repeat
  172. if dirp^.dd_nextoff >= dirp^.dd_size then
  173. begin
  174. bytes := do_SysCall(SysCall_nr_getdents64,TSysParam(dirp^.dd_fd),TSysParam(dirp^.dd_buf),TSysParam(dirp^.dd_max));
  175. if bytes <= 0 then
  176. begin
  177. fpreaddir := nil;
  178. exit;
  179. end;
  180. dirp^.dd_size := bytes;
  181. dirp^.dd_nextoff := 0;
  182. end;
  183. dp := Pdirent(ptruint(dirp^.dd_buf)+dirp^.dd_nextoff);
  184. inc(dirp^.dd_nextoff,dp^.d_reclen);
  185. inc(dirp^.dd_loc,dp^.d_reclen);
  186. until dp^.d_fileno <> 0; // Don't show deleted files
  187. Fpreaddir := dp;
  188. end;
  189. {*****************************************************************************
  190. --- Process:Process & program handling - related calls ---
  191. *****************************************************************************}
  192. procedure Fpexit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  193. begin
  194. do_syscall(syscall_nr_exit_group,status);
  195. end;
  196. {
  197. Change action of process upon receipt of a signal.
  198. Signum specifies the signal (all except SigKill and SigStop).
  199. If Act is non-nil, it is used to specify the new action.
  200. If OldAct is non-nil the previous action is saved there.
  201. }
  202. {$if defined(cpusparc) or defined(cpusparc64)}
  203. procedure Fprt_sigreturn_stub;assembler;nostackframe;
  204. asm
  205. mov syscall_nr_rt_sigreturn,%g1
  206. {$ifdef cpusparc}
  207. ta 0x10
  208. {$else}
  209. ta 0x6d
  210. {$endif}
  211. end;
  212. {$endif cpusparc or cpusparc64}
  213. {$if defined(android)}
  214. {$if defined(cpux86_64)}
  215. {$define NEED_USER_TRAMPOLINE}
  216. {$endif}
  217. {$else not android}
  218. {$if defined(cpui386) or defined(cpuarm) or defined(cpux86_64)}
  219. {$define NEED_USER_TRAMPOLINE}
  220. {$endif}
  221. {$if defined(cpui386) or defined(cpuarm)}
  222. {$define NEED_USER_TRAMPOLINE_RT_DIFFERENT}
  223. {$endif}
  224. {$endif android}
  225. {$ifdef NEED_USER_TRAMPOLINE}
  226. procedure linux_restore; cdecl; nostackframe; assembler;
  227. {$ifdef cpuarm}
  228. asm
  229. {$ifdef FPC_ABI_EABI}
  230. mov r7, syscall_nr_sigreturn
  231. {$ifdef CPUTHUMB}
  232. // svc #0x0
  233. // GNU 2.22 does not like svc #0x0
  234. .byte 0,0xdf
  235. {$else CPUTHUMB}
  236. swi #0x0
  237. {$endif CPUTHUMB}
  238. {$else}
  239. swi syscall_nr_sigreturn
  240. {$endif}
  241. end;
  242. {$endif}
  243. {$ifdef cpui386}
  244. asm
  245. popl %eax
  246. movl $syscall_nr_sigreturn, %eax
  247. int $0x80
  248. end;
  249. {$endif}
  250. {$ifdef cpux86_64}
  251. asm
  252. movq $syscall_nr_rt_sigreturn, %rax
  253. syscall
  254. end;
  255. {$endif}
  256. {$endif}
  257. {$ifdef NEED_USER_TRAMPOLINE_RT_DIFFERENT}
  258. procedure linux_restore_rt; cdecl; nostackframe; assembler;
  259. {$ifdef cpuarm}
  260. asm
  261. {$ifdef FPC_ABI_EABI}
  262. mov r7, syscall_nr_rt_sigreturn
  263. {$ifdef CPUTHUMB}
  264. // svc #0x0
  265. // GNU 2.22 does not like svc #0x0
  266. .byte 0,0xdf
  267. {$else CPUTHUMB}
  268. swi #0x0
  269. {$endif CPUTHUMB}
  270. {$else}
  271. swi syscall_nr_rt_sigreturn
  272. {$endif}
  273. end;
  274. {$endif}
  275. {$ifdef cpui386}
  276. asm
  277. movl $syscall_nr_rt_sigreturn, %eax
  278. int $0x80
  279. end;
  280. {$endif}
  281. {$endif}
  282. function Fpsigaction(sig: cint; new_action, old_action: psigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  283. {
  284. Change action of process upon receipt of a signal.
  285. Signum specifies the signal (all except SigKill and SigStop).
  286. If Act is non-nil, it is used to specify the new action.
  287. If OldAct is non-nil the previous action is saved there.
  288. }
  289. begin
  290. {$if defined(cpusparc) or defined(cpusparc64)}
  291. { Sparc has an extra stub parameter }
  292. {$ifdef cpusparc}
  293. { it seems that rt_sigreturn only works correctly for 32-bit if SA_SIGINFO is set }
  294. if new_action <> nil then
  295. new_action^.sa_flags:=new_action^.sa_flags or SA_SIGINFO;
  296. {$endif}
  297. { the -8 stems from the fact that the adress is supposed to be a value from a sub-routine
  298. call stored into register %i7 to which +8 is added upon return }
  299. Fpsigaction:=do_syscall(syscall_nr_rt_sigaction,TSysParam(sig),TSysParam(new_action),TSysParam(old_action),TSysParam(ptruint(@Fprt_sigreturn_stub)-8),TSysParam(8));
  300. {$else not (defined(cpusparc) or defined(cpusparc64))}
  301. {$ifdef NEED_USER_TRAMPOLINE}
  302. if new_action <> nil then
  303. begin
  304. // a different stack (SA_ONSTACK) requires sa_restorer field
  305. if (new_action^.sa_flags and (SA_RESTORER or SA_ONSTACK)) = 0 then
  306. begin
  307. new_action^.sa_flags := new_action^.sa_flags or SA_RESTORER;
  308. {$ifdef NEED_USER_TRAMPOLINE_RT_DIFFERENT}
  309. if (new_action^.sa_flags and SA_SIGINFO) <> 0 then
  310. new_action^.sa_restorer := @linux_restore_rt
  311. else
  312. {$endif}
  313. new_action^.sa_restorer := @linux_restore;
  314. end;
  315. end;
  316. {$endif}
  317. Fpsigaction:=do_syscall(syscall_nr_rt_sigaction,TSysParam(sig),
  318. TSysParam(new_action),TSysParam(old_action),
  319. {$ifdef cpumips}
  320. TSysParam(16{should be wordsinsigset})
  321. {$else not cpumips}
  322. TSysParam(8)
  323. {$endif not cpumips}
  324. );
  325. {$endif cpusparc}
  326. end;
  327. { this is used by the FpFtruncate below. for more information,
  328. check the syscall() Linux man page (KB) }
  329. {$if defined(FPC_ABI_EABI) or defined(CPUMIPS32) or defined(CPUMIPSEL32) or defined(CPUPOWERPC32)}
  330. {$define FPC_ALIGN_DUMMY}
  331. {$endif}
  332. function Fpftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  333. { See notes lseek. This one is completely similar for the parameter (but
  334. doesn't have the returnvalue 64-bit problem)}
  335. begin
  336. {$if defined(cpu64)}
  337. Fpftruncate:=Do_syscall(syscall_nr_ftruncate,TSysParam(fd),TSysParam(flength));
  338. {$else}
  339. Fpftruncate:=Do_syscall(syscall_nr_ftruncate64,TSysParam(fd),
  340. {$ifdef FPC_ALIGN_DUMMY} 0, {$endif FPC_ALIGN_DUMMY} { align parameters as required with dummy }
  341. {$ifdef FPC_BIG_ENDIAN} tsysparam(hi(flength)),tsysparam(lo(flength)){$endif}
  342. {$ifdef FPC_LITTLE_ENDIAN} tsysparam(lo(flength)),tsysparam(hi(flength)){$endif}
  343. );
  344. {$endif}
  345. end;
  346. {$undef FPC_ALIGN_DUMMY}
  347. function Fpfstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  348. begin
  349. {$if defined(cpu64) and not(defined(cpusparc64))}
  350. {$if defined(generic_linux_syscalls)}
  351. FpFStat:=do_SysCall(syscall_nr_fstat,TSysParam(fd),TSysParam(@sb));
  352. {$else}
  353. FpFStat:=do_SysCall(syscall_nr_fstat,TSysParam(fd),TSysParam(@sb));
  354. {$endif}
  355. {$else}
  356. FpFStat:=do_SysCall(syscall_nr_fstat64,TSysParam(fd),TSysParam(@sb));
  357. {$endif}
  358. end;
  359. {$ifndef FPC_SYSTEM_HAS_FPFORK}
  360. function Fpfork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  361. {
  362. This function issues the 'fork' System call. the program is duplicated in memory
  363. and Execution continues in parent and child process.
  364. In the parent process, fork returns the PID of the child. In the child process,
  365. zero is returned.
  366. A negative value indicates that an error has occurred, the error is returned in
  367. LinuxError.
  368. }
  369. {$if defined(generic_linux_syscalls) or defined(cpuxtensa)}
  370. var
  371. pid : Int64;
  372. {$endif}
  373. Begin
  374. {$if defined(generic_linux_syscalls) or defined(cpuxtensa)}
  375. Fpfork:=Do_syscall(syscall_nr_clone,clone_flags_fork,0,0,0,TSysParam(@pid));
  376. {$else}
  377. Fpfork:=Do_syscall(SysCall_nr_fork);
  378. {$endif}
  379. End;
  380. {$endif FPC_SYSTEM_HAS_FPFORK}
  381. // Look at execve variants later, when overloaded is determined.
  382. {
  383. function Fpexecve(path : pathstr; argv : ppchar; envp: ppchar): cint;
  384. }
  385. {
  386. Replaces the current program by the program specified in path,
  387. arguments in args are passed to Execve.
  388. environment specified in ep is passed on.
  389. }
  390. {
  391. Begin
  392. path:=path+#0;
  393. do_syscall(syscall_nr_Execve,TSysParam(@path[1]),TSysParam(Argv),TSysParam(envp));
  394. End;
  395. }
  396. {
  397. function Fpexecve(path : pchar; argv : ppchar; envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  398. }
  399. {
  400. Replaces the current program by the program specified in path,
  401. arguments in args are passed to Execve.
  402. environment specified in ep is passed on.
  403. }
  404. {
  405. Begin
  406. do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(Argv),TSysParam(envp));
  407. End;
  408. }
  409. function Fpwaitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  410. {
  411. Waits until a child with PID Pid exits, or returns if it is exited already.
  412. Any resources used by the child are freed.
  413. The exit status is reported in the adress referred to by Status. It should
  414. be a longint.
  415. }
  416. begin
  417. {$if defined(generic_linux_syscalls) or defined(WAIT4)}
  418. FpWaitPID:=do_syscall(syscall_nr_Wait4,PID,TSysParam(Stat_loc),options,0);
  419. {$else WAIT4}
  420. FpWaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options);
  421. {$endif WAIT4}
  422. end;
  423. function Fpaccess(pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  424. {
  425. Test users access rights on the specified file.
  426. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  427. R,W,X stand for read,write and Execute access, simultaneously.
  428. F_OK checks whether the test would be allowed on the file.
  429. i.e. It checks the search permissions in all directory components
  430. of the path.
  431. The test is done with the real user-ID, instead of the effective.
  432. If access is denied, or an error occurred, false is returned.
  433. If access is granted, true is returned.
  434. Errors other than no access,are reported in unixerror.
  435. }
  436. begin
  437. {$if defined(generic_linux_syscalls)}
  438. FpAccess:=do_syscall(syscall_nr_faccessat,AT_FDCWD,TSysParam(pathname),amode,0);
  439. {$else}
  440. FpAccess:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  441. {$endif}
  442. end;
  443. (* overloaded
  444. function Fpaccess(pathname : pathstr; amode : cint): cint;
  445. {
  446. Test users access rights on the specified file.
  447. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  448. R,W,X stand for read,write and Execute access, simultaneously.
  449. F_OK checks whether the test would be allowed on the file.
  450. i.e. It checks the search permissions in all directory components
  451. of the path.
  452. The test is done with the real user-ID, instead of the effective.
  453. If access is denied, or an error occurred, false is returned.
  454. If access is granted, true is returned.
  455. Errors other than no access,are reported in unixerror.
  456. }
  457. begin
  458. pathname:=pathname+#0;
  459. Access:=do_syscall(syscall_nr_access, TSysParam(@pathname[1]),mode)=0;
  460. end;
  461. *)
  462. Function FpDup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  463. begin
  464. Fpdup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  465. end;
  466. Function FpDup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  467. begin
  468. {$if defined(generic_linux_syscalls)}
  469. if fildes<>fildes2 then
  470. Fpdup2:=do_syscall(syscall_nr_dup3,TSysParam(fildes),TSysParam(fildes2),0)
  471. else if do_syscall(syscall_nr_fcntl,TSysParam(fildes),1)<>-1 then
  472. Fpdup2:=fildes2
  473. else
  474. Fpdup2:=-1;
  475. {$else}
  476. Fpdup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  477. {$endif}
  478. end;
  479. type
  480. tmmapargs = record
  481. address : TSysParam;
  482. size : TSysParam;
  483. prot : TSysParam;
  484. flags : TSysParam;
  485. fd : TSysParam;
  486. offset : TSysParam;
  487. end;
  488. Function Fpmmap(adr:pointer;len:size_t;prot:cint;flags:cint;fd:cint;off:off_t):pointer; [public, alias : 'FPC_SYSC_MMAP'];
  489. // OFF_T procedure, and returns a pointer, NOT cint.
  490. {$ifdef OLDMMAP}
  491. var
  492. mmapargs : tmmapargs;
  493. begin
  494. mmapargs.address:=TSysParam(adr);
  495. mmapargs.size:=TSysParam(len);
  496. mmapargs.prot:=TSysParam(prot);
  497. mmapargs.flags:=TSysParam(flags);
  498. mmapargs.fd:=TSysParam(fd);
  499. mmapargs.offset:=TSysParam(off);
  500. Fpmmap:=pointer(do_syscall(syscall_nr_mmap,TSysParam(@MMapArgs)));
  501. end;
  502. {$else OLDMMAP}
  503. {$ifdef MMAP2}
  504. begin
  505. {$if sizeof(TSysParam)>=sizeof(off_t)}
  506. Fpmmap:= pointer(do_syscall(syscall_nr_mmap2,TSysParam(adr),TSysParam(len),
  507. TSysParam(prot),TSysParam(flags),TSysParam(fd),TSysParam(off div 4096)));
  508. {$else off_t is larger than TsysParam }
  509. {$message warning need mmap64 syscall, hi(off) not used}
  510. Fpmmap:= pointer(do_syscall(syscall_nr_mmap2,TSysParam(adr),TSysParam(len),
  511. TSysParam(prot),TSysParam(flags),TSysParam(fd),TSysParam(lo(off div 4096))));
  512. {$endif}
  513. end;
  514. {$else MMAP2}
  515. begin
  516. {$if sizeof(TSysParam)>=sizeof(off_t)}
  517. Fpmmap:= pointer(do_syscall(syscall_nr_mmap,TSysParam(adr),TSysParam(len),
  518. TSysParam(prot),TSysParam(flags),TSysParam(fd),TSysParam(off)));
  519. {$else off_t is larger than TsysParam }
  520. {$message warning need mmap64 syscall, hi(off) not used}
  521. Fpmmap:= pointer(do_syscall(syscall_nr_mmap,TSysParam(adr),TSysParam(len),
  522. TSysParam(prot),TSysParam(flags),TSysParam(fd),TSysParam(lo(off))));
  523. {$endif}
  524. end;
  525. {$endif MMAP2}
  526. {$endif OLDMMAP}
  527. Function Fpmunmap(adr:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  528. begin
  529. Fpmunmap:=do_syscall(syscall_nr_munmap,TSysParam(Adr),TSysParam(Len));
  530. end;
  531. {$define FPC_SYSTEM_HAS_MREMAP}
  532. Function Fpmremap(adr:pointer;old_len,new_len:size_t;flags:cint;new_adr:pointer):pointer; [public, alias :'FPC_SYSC_MREMAP'];
  533. begin
  534. Fpmremap:=pointer(do_syscall(syscall_nr_mremap,TSysParam(Adr),TSysParam(old_len),TSysParam(new_len),TSysParam(flags),TSysParam(new_adr)));
  535. end;
  536. Function Fpmprotect(adr:pointer;len:size_t;prot:cint):cint; [public, alias : 'FPC_SYSC_MPROTECT'];
  537. begin
  538. Fpmprotect:=do_syscall(syscall_nr_mprotect,TSysParam(adr),TSysParam(len),TSysParam(prot));
  539. end;
  540. {
  541. Interface to Unix ioctl call.
  542. Performs various operations on the filedescriptor Handle.
  543. Ndx describes the operation to perform.
  544. Data points to data needed for the Ndx function. The structure of this
  545. data is function-dependent.
  546. }
  547. // prototype is cint __P(cint,culong,....)
  548. // actual meaning of return value depends on request.
  549. Function FpIOCtl(fd:cint;request:TIOCtlRequest;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  550. // This was missing here, instead hardcoded in Do_IsDevice
  551. begin
  552. FpIOCtl:=do_SysCall(syscall_nr_ioctl,tsysparam(fd),tsysparam(Request),TSysParam(data));
  553. end;
  554. Function FpGetPid:pid_t; [public, alias : 'FPC_SYSC_GETPID'];
  555. {
  556. Get Process ID.
  557. }
  558. begin
  559. FpGetPID:=do_syscall(syscall_nr_getpid);
  560. end;
  561. Function FpReadLink(name,linkname:pchar;maxlen:size_t):cint; [public, alias : 'FPC_SYSC_READLINK'];
  562. begin
  563. {$if defined(generic_linux_syscalls)}
  564. Fpreadlink:=do_syscall(syscall_nr_readlinkat,AT_FDCWD,TSysParam(name),TSysParam(linkname),maxlen);
  565. {$else}
  566. Fpreadlink:=do_syscall(syscall_nr_readlink, TSysParam(name),TSysParam(linkname),maxlen);
  567. {$endif}
  568. end;
  569. function FPSigProcMask(how:cint;nset : psigset;oset : psigset):cint; [public, alias : 'FPC_SYSC_SIGPROCMASK'];
  570. {
  571. Change the list of currently blocked signals.
  572. How determines which signals will be blocked :
  573. SigBlock : Add SSet to the current list of blocked signals
  574. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  575. SigSetMask : Set the list of blocked signals to SSet
  576. if OldSSet is non-null, the old set will be saved there.
  577. }
  578. begin
  579. FPsigprocmask:=do_syscall(syscall_nr_rt_sigprocmask,TSysParam(how),
  580. TSysParam(nset),TSysParam(oset),
  581. {$ifdef CPUMIPS}
  582. TSysParam(16)
  583. {$else not CPUMIPS}
  584. TSysParam(8)
  585. {$endif not CPUMIPS}
  586. );
  587. end;
  588. Function FpNanoSleep(req : ptimespec;rem : ptimespec):cint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  589. begin
  590. FpNanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(req),TSysParam(rem));
  591. end;
  592. function fpgetcwd(path : pchar; siz:tsize):pchar; [public, alias : 'FPC_SYSC_GETCWD'];
  593. begin
  594. fpgetcwd:=pchar(Do_Syscall(Syscall_nr_getcwd,TSysParam(Path),TSysParam(siz)));
  595. end;
  596. function fpgettimeofday(tp: ptimeval;tzp:ptimezone):cint; [public, alias: 'FPC_SYSC_GETTIMEOFDAY'];
  597. begin
  598. fpgettimeofday:=do_syscall(syscall_nr_gettimeofday,TSysParam(tp),TSysParam(tzp));
  599. end;
  600. function FpGetRLimit(resource : cInt; rlim : PRLimit) : cInt; [public, alias : 'FPC_SYSC_GETRLIMIT'];
  601. begin
  602. {$ifndef NO_SYSCALL_GETRLIMIT}
  603. FpGetRLimit := do_syscall(syscall_nr_getrlimit,
  604. TSysParam(resource), TSysParam(rlim));
  605. {$else}
  606. FpGetRLimit := do_syscall(syscall_nr_ugetrlimit,
  607. TSysParam(resource), TSysParam(rlim));
  608. {$endif}
  609. end;
  610. {$ifdef HAS_UGETRLIMIT}
  611. function fpugetrlimit(resource : cInt; rlim : PRLimit) : cInt;
  612. begin
  613. FpUGetRLimit := do_syscall(syscall_nr_ugetrlimit,
  614. TSysParam(resource), TSysParam(rlim));
  615. end;
  616. {$endif}
  617. {$if defined(cpupowerpc)}
  618. { fpprctl() call }
  619. function fpprctl(option : cint; const arg : ptruint) : cint;
  620. begin
  621. fpprctl := do_syscall(syscall_nr_prctl, option, arg);
  622. end;
  623. {$endif}
  624. function FpSetRLimit(Resource:cint;rlim:PRLimit):cint; [public, alias : 'FPC_SYSC_SETRLIMIT'];
  625. begin
  626. fpsetrlimit:=do_syscall(syscall_nr_setrlimit,TSysParam(Resource),TSysParam(rlim));
  627. end;
  628. function FpSchedGetAffinity(pid : pid_t;cpusetsize : size_t;mask : pcpu_set_t) : cint;
  629. begin
  630. FpSchedGetAffinity := do_syscall(syscall_nr_sched_getaffinity,TSysParam(pid),TSysParam(cpusetsize),TSysParam(mask));
  631. end;