ossysc.inc 14 KB

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