linsysc.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. {$ifdef uselibc}
  15. {$Linklib c}
  16. // Out of date atm.
  17. { var
  18. Errno : cint; external name 'errno';}
  19. function sys_time(tloc:ptime_t): time_t; cdecl; external name 'time';
  20. function sys_open(const path: pchar; flags : cint; mode: mode_t):cint; cdecl; external name 'open';
  21. function sys_close(fd : cint): cint; cdecl; external name 'close';
  22. function sys_lseek(fd : cint; offset : off_t; whence : cint): off_t; cdecl; external name 'lseek';
  23. function sys_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external name 'read';
  24. function sys_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; cdecl; external name 'write';
  25. function sys_unlink(const path: pchar): cint; cdecl; external name 'unlink';
  26. function sys_rename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename';
  27. function sys_stat(const path: pchar; var buf : stat): cint; cdecl; external name 'stat';
  28. function sys_chdir(const path : pchar): cint; cdecl; external name 'chdir';
  29. function sys_mkdir(const path : pchar; mode: mode_t):cint; cdecl; external name 'mkdir';
  30. function sys_rmdir(const path : pchar): cint; cdecl; external name 'rmdir';
  31. function sys_opendir(const dirname : pchar): pdir; cdecl; external name 'opendir';
  32. function sys_readdir(var dirp : dir) : pdirent;cdecl; external name 'readdir';
  33. function sys_closedir(var dirp : dir): cint; cdecl; external name 'closedir';
  34. procedure sys_exit(status : cint); cdecl; external name '_exit';
  35. function sys_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; cdecl; external name 'sigaction';
  36. function sys_ftruncate(fd : cint; flength : off_t): cint; cdecl; external name 'ftruncate';
  37. function sys_rename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename';
  38. function sys_fstat(fd : cint; var sb : stat): cint; cdecl; external name 'fstat';
  39. function sys_fork : pid_t; cdecl; external name 'fork';
  40. function sys_execve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; cdecl; external name 'execve';
  41. function sys_waitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; cdecl; external name 'waitpid';
  42. function sys_access(const pathname : pchar; amode : cint): cint; cdecl; external name 'access';
  43. function sys_uname(var name: utsname): cint; cdecl; external name 'uname';
  44. function sys_Dup(oldd:cint):cint; cdecl; external name 'dup';
  45. function sys_Dup2(oldd:cint;newd:cint):cint; cdecl; external name 'dup2';
  46. {$else}
  47. {*****************************************************************************
  48. --- Main:The System Call Self ---
  49. *****************************************************************************}
  50. {$I ostypes.inc}
  51. {$I syscall.inc}
  52. {$I sysnr.inc}
  53. {$I posmacro.inc}
  54. function sys_time(tloc:ptime_t): time_t; [public, alias : 'FPC_SYSC_TIME'];
  55. begin
  56. sys_time:=do_syscall(syscall_nr_time,TSysParam(tloc));
  57. End;
  58. {*****************************************************************************
  59. --- File:File handling related calls ---
  60. *****************************************************************************}
  61. function sys_open(const path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  62. Begin
  63. sys_open:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags),TSysParam(mode));
  64. End;
  65. function sys_close(fd : cint): cint;
  66. begin
  67. sys_close:=do_syscall(syscall_nr_close,fd);
  68. end;
  69. function sys_lseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  70. {
  71. Must be adapted/overloaded for 64-bit support, but that is a different call under
  72. Linux?
  73. }
  74. begin
  75. sys_lseek:=do_syscall(syscall_nr_lseek,tsysparam(fd),tsysparam(offset),tsysparam(whence));
  76. end;
  77. function sys_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  78. begin
  79. sys_read:=do_syscall(syscall_nr_read,Fd,TSysParam(buf),nbytes);
  80. end;
  81. function sys_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  82. begin
  83. sys_write:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  84. end;
  85. function sys_unlink(const path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  86. begin
  87. sys_unlink:=do_syscall(syscall_nr_unlink,TSysParam(path));
  88. end;
  89. function sys_rename(const old : pchar; const newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  90. begin
  91. sys_rename:=do_syscall(syscall_nr_rename,TSysParam(old),TSysParam(newpath));
  92. end;
  93. function sys_stat(const path: pchar; var buf : stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  94. begin
  95. sys_stat:=do_syscall(syscall_nr_stat,TSysParam(path),TSysParam(@buf));
  96. end;
  97. {*****************************************************************************
  98. --- Directory:Directory related calls ---
  99. *****************************************************************************}
  100. function sys_chdir(const path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  101. begin
  102. sys_chdir:=do_syscall(syscall_nr_chdir,TSysParam(path));
  103. end;
  104. function sys_mkdir(const path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  105. begin
  106. sys_mkdir:=do_syscall(syscall_nr_mkdir,TSysParam(path),TSysParam(mode));
  107. end;
  108. function sys_rmdir(const path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  109. begin
  110. sys_rmdir:=do_syscall(syscall_nr_rmdir,TSysParam(path));
  111. end;
  112. function sys_opendir(const dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  113. var
  114. fd:integer;
  115. st:stat;
  116. ptr:pdir;
  117. begin
  118. sys_opendir:=nil;
  119. if sys_stat(dirname,st)<0 then
  120. exit;
  121. { Is it a dir ? }
  122. if not((st.st_mode and $f000)=$4000)then
  123. begin
  124. errno:=sys_enotdir;
  125. exit
  126. end;
  127. { Open it}
  128. fd:=sys_open(dirname,O_RDONLY,438);
  129. if fd<0 then
  130. exit;
  131. new(ptr);
  132. if ptr=nil then
  133. exit;
  134. getmem(ptr^.buf,sizeof(dirent));
  135. if ptr^.buf=nil then
  136. exit;
  137. ptr^.fd:=fd;
  138. ptr^.loc:=0;
  139. ptr^.size:=0;
  140. ptr^.dd_max:=sizeof(ptr^.buf^);
  141. sys_opendir:=ptr;
  142. end;
  143. function sys_closedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  144. begin
  145. sys_closedir:=sys_close(dirp^.fd);
  146. freemem(dirp^.buf,sizeof(dirent));
  147. dispose(dirp);
  148. end;
  149. function sys_readdir(dirp : pdir) : pdirent; [public, alias : 'FPC_SYSC_READDIR'];
  150. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  151. missing of the readdir syscall.
  152. Getdents requires the buffer to be larger than the blocksize.
  153. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  154. with blockmode have this higher?}
  155. begin
  156. if do_SysCall(SysCall_nr_readdir,TSysParam(dirp^.fd),TSysParam(dirp^.buf),TSysParam(1))=0 Then
  157. { the readdir system call returns the number of bytes written }
  158. sys_readdir:=nil
  159. else
  160. sys_readdir:=dirp^.buf
  161. end;
  162. {*****************************************************************************
  163. --- Process:Process & program handling - related calls ---
  164. *****************************************************************************}
  165. procedure sys_exit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  166. begin
  167. do_syscall(syscall_nr_exit,status);
  168. end;
  169. {
  170. Change action of process upon receipt of a signal.
  171. Signum specifies the signal (all except SigKill and SigStop).
  172. If Act is non-nil, it is used to specify the new action.
  173. If OldAct is non-nil the previous action is saved there.
  174. }
  175. function sys_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  176. {
  177. Change action of process upon receipt of a signal.
  178. Signum specifies the signal (all except SigKill and SigStop).
  179. If Act is non-nil, it is used to specify the new action.
  180. If OldAct is non-nil the previous action is saved there.
  181. }
  182. begin
  183. do_syscall(syscall_nr_sigaction,TSysParam(sig),TSysParam(@act),TSysParam(@oact));
  184. end;
  185. function sys_ftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  186. { See notes lseek. This one is completely similar for the parameter (but
  187. doesn't have the returnvalue 64-bit problem)}
  188. begin
  189. sys_ftruncate:=Do_syscall(syscall_nr_ftruncate,TSysParam(fd),TSysParam(flength));
  190. end;
  191. function sys_fstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  192. begin
  193. Sys_FStat:=do_SysCall(syscall_nr_fstat,TSysParam(fd),TSysParam(@sb));
  194. end;
  195. function sys_fork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  196. {
  197. This function issues the 'fork' System call. the program is duplicated in memory
  198. and Execution continues in parent and child process.
  199. In the parent process, fork returns the PID of the child. In the child process,
  200. zero is returned.
  201. A negative value indicates that an error has occurred, the error is returned in
  202. LinuxError.
  203. }
  204. Begin
  205. sys_fork:=Do_syscall(SysCall_nr_fork);
  206. End;
  207. // Look at execve variants later, when overloaded is determined.
  208. {
  209. function sys_execve(const path : pathstr; const argv : ppchar; const envp: ppchar): cint;
  210. }
  211. {
  212. Replaces the current program by the program specified in path,
  213. arguments in args are passed to Execve.
  214. environment specified in ep is passed on.
  215. }
  216. {
  217. Begin
  218. path:=path+#0;
  219. do_syscall(syscall_nr_Execve,TSysParam(@path[1]),TSysParam(Argv),TSysParam(envp));
  220. End;
  221. }
  222. {
  223. function sys_execve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  224. }
  225. {
  226. Replaces the current program by the program specified in path,
  227. arguments in args are passed to Execve.
  228. environment specified in ep is passed on.
  229. }
  230. {
  231. Begin
  232. do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(Argv),TSysParam(envp));
  233. End;
  234. }
  235. function sys_waitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  236. {
  237. Waits until a child with PID Pid exits, or returns if it is exited already.
  238. Any resources used by the child are freed.
  239. The exit status is reported in the adress referred to by Status. It should
  240. be a longint.
  241. }
  242. begin
  243. sys_WaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options);
  244. end;
  245. function sys_access(const 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. sys_Access:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  260. end;
  261. { overloaded
  262. function sys_access(const 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 sys_Dup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  281. begin
  282. sys_dup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  283. end;
  284. Function sys_Dup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  285. begin
  286. sys_dup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  287. end;
  288. CONST
  289. { Constansts for MMAP }
  290. MAP_PRIVATE =2;
  291. MAP_ANONYMOUS =$20;
  292. {Constansts Termios/Ioctl (used in Do_IsDevice) }
  293. IOCtl_TCGETS=$5401; // TCGETS is also in termios.inc, but the sysunix needs only this
  294. type
  295. tmmapargs=packed record
  296. address : longint;
  297. size : longint;
  298. prot : longint;
  299. flags : longint;
  300. fd : longint;
  301. offset : longint;
  302. end;
  303. Function Sys_mmap(adr:pointer;len:size_t;prot:cint;flags:cint;fd:cint;off:off_t):pointer; [public, alias : 'FPC_SYSC_MMAP'];
  304. // OFF_T procedure, and returns a pointer, NOT cint.
  305. var
  306. mmapargs : tmmapargs;
  307. begin
  308. mmapargs.address:=TSysParam(adr);
  309. mmapargs.size:=TSysParam(len);
  310. mmapargs.prot:=TSysParam(prot);
  311. mmapargs.flags:=TSysParam(flags);
  312. mmapargs.fd:=TSysParam(fd);
  313. mmapargs.offset:=TSysParam(off);
  314. Sys_mmap:=pointer(do_syscall(syscall_nr_mmap,TSysParam(@MMapArgs)));
  315. end;
  316. Function Sys_munmap(adr:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  317. begin
  318. Sys_munmap:=do_syscall(syscall_nr_munmap,TSysParam(Adr),TSysParam(Len));
  319. end;
  320. Function sbrk(size : longint) : longint;
  321. begin
  322. sbrk:=longint(Sys_mmap(0,Size,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0));
  323. if sbrk<>-1 then
  324. errno:=0;
  325. {! It must be -1, not 0 as before, see heap.inc. Should be in sysmmap?}
  326. end;
  327. {
  328. Interface to Unix ioctl call.
  329. Performs various operations on the filedescriptor Handle.
  330. Ndx describes the operation to perform.
  331. Data points to data needed for the Ndx function. The structure of this
  332. data is function-dependent.
  333. }
  334. // prototype is cint __P(cint,culong,....)
  335. // actual meaning of return value depends on request.
  336. Function Sys_IOCtl(fd:cint;request:culong;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  337. // This was missing here, instead hardcoded in Do_IsDevice
  338. begin
  339. Sys_IOCtl:=do_SysCall(syscall_nr_ioctl,tsysparam(fd),tsysparam(Request),TSysParam(data));
  340. end;
  341. Function Do_IsDevice(Handle:cint):boolean;
  342. {
  343. Interface to Unix ioctl call.
  344. Performs various operations on the filedescriptor Handle.
  345. Ndx describes the operation to perform.
  346. Data points to data needed for the Ndx function. The structure of this
  347. data is function-dependent.
  348. }
  349. var
  350. Data : array[0..255] of byte; {Large enough for termios info}
  351. begin
  352. Do_IsDevice:=(sys_ioctl(handle,IOCTL_TCGETS,@data)<>-1);
  353. end;
  354. Function sys_GetPid:pid_t; [public, alias : 'FPC_SYSC_GETPID'];
  355. {
  356. Get Process ID.
  357. }
  358. begin
  359. sys_GetPID:=do_syscall(syscall_nr_getpid);
  360. end;
  361. Function Sys_ReadLink(name,linkname:pchar;maxlen:size_t):cint; [public, alias : 'FPC_SYSC_READLINK'];
  362. begin
  363. sys_readlink:=do_syscall(syscall_nr_readlink, TSysParam(name),TSysParam(linkname),maxlen);
  364. end;
  365. Function sys_NanoSleep(const req : timespec;rem : ptimespec) : longint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  366. begin
  367. sys_NanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(@req),TSysParam(rem));
  368. end;
  369. // The following belongs here, but this should be researched more.
  370. // function sys_getcwd(pt:pchar; _size:size_t):pchar;[public, alias :'FPC_SYSC_GETCWD'];
  371. {$endif}
  372. {
  373. $Log$
  374. Revision 1.1 2002-11-12 14:40:18 marco
  375. * The syscall core of the new system unit.
  376. }