liunsysc.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  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. Function Fork:longint;
  13. {
  14. This function issues the 'fork' System call. the program is duplicated in memory
  15. and Execution continues in parent and child process.
  16. In the parent process, fork returns the PID of the child. In the child process,
  17. zero is returned.
  18. A negative value indicates that an error has occurred, the error is returned in
  19. LinuxError.
  20. }
  21. begin
  22. Fork:=fpfork;
  23. LinuxError:=fpgetErrno;
  24. End;
  25. Procedure Execve(path:pathstr;args:ppchar;ep:ppchar);
  26. {
  27. Replaces the current program by the program specified in path,
  28. arguments in args are passed to Execve.
  29. environment specified in ep is passed on.
  30. }
  31. begin
  32. fpexecve(path,args,ep);
  33. Linuxerror:=fpgeterrno;
  34. end;
  35. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  36. {
  37. Replaces the current program by the program specified in path,
  38. arguments in args are passed to Execve.
  39. environment specified in ep is passed on.
  40. }
  41. begin
  42. fpexecve(path,args,ep);
  43. Linuxerror:=fpgeterrno;
  44. end;
  45. Procedure ExitProcess(val:longint);
  46. begin
  47. fpexit(val);
  48. end;
  49. Function WaitPid(Pid:longint;Status:pointer;Options:Longint):Longint;
  50. {
  51. Waits until a child with PID Pid exits, or returns if it is exited already.
  52. Any resources used by the child are freed.
  53. The exit status is reported in the adress referred to by Status. It should
  54. be a longint.
  55. }
  56. begin
  57. WaitPid:=fpWaitPid(pid,pcint(status),options);
  58. LinuxError:=fpgeterrno;
  59. end;
  60. Procedure GetTimeOfDay(var tv:timeval);
  61. {
  62. Get the number of seconds since 00:00, January 1 1970, GMT
  63. the time NOT corrected any way
  64. }
  65. begin
  66. gettimeofday(tv);
  67. LinuxError:=fpgetErrno;
  68. end;
  69. Function GetPriority(Which,Who:longint):longint;
  70. {
  71. Get Priority of process, process group, or user.
  72. Which : selects what kind of priority is used.
  73. can be one of the following predefined Constants :
  74. Prio_User.
  75. Prio_PGrp.
  76. Prio_Process.
  77. Who : depending on which, this is , respectively :
  78. Uid
  79. Pid
  80. Process Group id
  81. Errors are reported in linuxerror _only_. (priority can be negative)
  82. }
  83. begin
  84. fpseterrno(0);
  85. if (which<prio_process) or (which>prio_user) then
  86. begin
  87. { We can save an interrupt here }
  88. getpriority:=0;
  89. linuxerror:=ESyseinval;
  90. end
  91. else
  92. begin
  93. getpriority:=fpgetpriority(which,who);
  94. linuxerror:=fpgeterrno;
  95. end;
  96. end;
  97. Procedure SetPriority(Which,Who,What:Longint);
  98. {
  99. Set Priority of process, process group, or user.
  100. Which : selects what kind of priority is used.
  101. can be one of the following predefined Constants :
  102. Prio_User.
  103. Prio_PGrp.
  104. Prio_Process.
  105. Who : depending on value of which, this is, respectively :
  106. Uid
  107. Pid
  108. Process Group id
  109. what : A number between -20 and 20. -20 is most favorable, 20 least.
  110. 0 is the default.
  111. }
  112. begin
  113. fpseterrno(0);
  114. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  115. linuxerror:=ESyseinval { We can save an interrupt here }
  116. else
  117. begin
  118. fpsetpriority(which,who,what);
  119. linuxerror:=fpgeterrno;
  120. end;
  121. end;
  122. Procedure Nice(N:integer);
  123. {
  124. Set process priority. A positive N means a lower priority.
  125. A negative N decreases priority.
  126. }
  127. begin
  128. fpnice(n);
  129. end;
  130. Function GetPid:LongInt;
  131. {
  132. Get Process ID.
  133. }
  134. begin
  135. GetPid:=fpgetpid;
  136. linuxerror:=fpgeterrno;
  137. end;
  138. Function GetPPid:LongInt;
  139. {
  140. Get Process ID of parent process.
  141. }
  142. begin
  143. GetPpid:=fpgetppid;
  144. linuxerror:=fpgeterrno;
  145. end;
  146. Function GetUid:Longint;
  147. {
  148. Get User ID.
  149. }
  150. begin
  151. GetUid:=fpgetuid;
  152. Linuxerror:=fpgeterrno;
  153. end;
  154. Function GetEUid:Longint;
  155. {
  156. Get _effective_ User ID.
  157. }
  158. begin
  159. GetEuid:=fpgeteuid;
  160. Linuxerror:=fpgeterrno;
  161. end;
  162. Function GetGid:Longint;
  163. {
  164. Get Group ID.
  165. }
  166. begin
  167. Getgid:=fpgetgid;
  168. Linuxerror:=fpgeterrno;
  169. end;
  170. Function GetEGid:Longint;
  171. {
  172. Get _effective_ Group ID.
  173. }
  174. begin
  175. GetEgid:=fpgetegid;
  176. Linuxerror:=fpgeterrno;
  177. end;
  178. Function GetTimeOfDay: longint;
  179. {
  180. Get the number of seconds since 00:00, January 1 1970, GMT
  181. the time NOT corrected any way
  182. }
  183. //var
  184. // tv : timeval;
  185. begin
  186. gettimeofday:=fptime;
  187. // regs.reg2:=longint(@tv);
  188. // regs.reg3:=0;
  189. // SysCall(SysCall_nr_gettimeofday,regs);
  190. LinuxError:=fpgetErrno;
  191. // GetTimeOfDay:=tv.tv_sec;
  192. end;
  193. Function fdTruncate(fd,size:longint):boolean;
  194. begin
  195. // Regs.reg2:=fd;
  196. // Regs.reg3:=size;
  197. fdTruncate:=fpftruncate(fd,size)=0;
  198. //(SysCall(Syscall_nr_ftruncate,regs)=0);
  199. LinuxError:=fpgetErrno;
  200. end;
  201. Function fdFlush (fd : Longint) : Boolean;
  202. begin
  203. fdFlush :=unix.fdflush(fd);
  204. LinuxError:=fpgetErrno;
  205. end;
  206. Function Fcntl(Fd:longint;Cmd:longint):longint;
  207. {
  208. Read or manipulate a file.(See also fcntl (2) )
  209. Possible values for Cmd are :
  210. F_GetFd,F_GetFl,F_GetOwn
  211. Errors are reported in Linuxerror;
  212. If Cmd is different from the allowed values, linuxerror=ESyseninval.
  213. }
  214. begin
  215. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  216. begin
  217. Linuxerror:=fpfcntl(fd,cmd);
  218. if linuxerror=-1 then
  219. begin
  220. linuxerror:=fpgeterrno;
  221. fcntl:=0;
  222. end
  223. else
  224. begin
  225. fcntl:=linuxerror;
  226. linuxerror:=0;
  227. end;
  228. end
  229. else
  230. begin
  231. linuxerror:=ESyseinval;
  232. Fcntl:=0;
  233. end;
  234. end;
  235. Procedure Fcntl(Fd:longint;Cmd : longint;Arg:Longint);
  236. {
  237. Read or manipulate a file. (See also fcntl (2) )
  238. Possible values for Cmd are :
  239. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  240. Errors are reported in Linuxerror;
  241. If Cmd is different from the allowed values, linuxerror=ESyseninval.
  242. F_DupFD is not allowed, due to the structure of Files in Pascal.
  243. }
  244. begin
  245. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  246. begin
  247. fpfcntl(fd,cmd,arg);
  248. linuxerror:=fpgeterrno;
  249. end
  250. else
  251. linuxerror:=ESyseinval;
  252. end;
  253. Function Chmod(path:pathstr;Newmode:longint):Boolean;
  254. {
  255. Changes the permissions of a file.
  256. }
  257. begin
  258. Chmod:=fpchmod(path,newmode)=0;
  259. linuxerror:=fpgeterrno;
  260. end;
  261. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  262. {
  263. Change the owner and group of a file.
  264. A user can only change the group to a group of which he is a member.
  265. The super-user can change uid and gid of any file.
  266. }
  267. begin
  268. ChOwn:=fpChOwn(path,newuid,newgid)=0;
  269. linuxerror:=fpgeterrno;
  270. end;
  271. Function Utime(path:pathstr;utim:utimebuf):boolean;
  272. begin
  273. utime:=fputime(path,@utim)=0;
  274. linuxerror:=fpgeterrno;
  275. end;
  276. Function Flock (fd,mode : longint) : boolean;
  277. begin
  278. flock:=unix.flock(fd,mode);
  279. LinuxError:=fpgeterrno;
  280. end;
  281. Function Fstat(Fd:Longint;var Info:stat):Boolean;
  282. {
  283. Get all information on a file descriptor, and return it in info.
  284. }
  285. begin
  286. FStat:=fpfstat(fd,{$ifdef oldlinuxstat}baseunix.stat(info){$else}info{$endif})=0;
  287. LinuxError:=fpgetErrno;
  288. end;
  289. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  290. {
  291. Get all information on a link (the link itself), and return it in info.
  292. }
  293. begin
  294. LStat:=fplstat(filename,@Info)=0;
  295. LinuxError:=fpgetErrno;
  296. end;
  297. Function StatFS(Path:Pathstr;Var Info:tstatfs):Boolean;
  298. {
  299. Get all information on a fileSystem, and return it in Info.
  300. Path is the name of a file/directory on the fileSystem you wish to
  301. investigate.
  302. }
  303. begin
  304. statfs:=unix.statfs(path,info);
  305. // path:=path+#0;
  306. // regs.reg2:=longint(@path[1]);
  307. // regs.reg3:=longint(@Info);
  308. // StatFS:=(SysCall(SysCall_nr_statfs,regs)=0);
  309. LinuxError:=fpgeterrno;
  310. end;
  311. Function StatFS(Fd:Longint;Var Info:tstatfs):Boolean;
  312. {
  313. Get all information on a fileSystem, and return it in Info.
  314. Fd is the file descriptor of a file/directory on the fileSystem
  315. you wish to investigate.
  316. }
  317. begin
  318. statfs:=unix.statfs(fd,info);
  319. // regs.reg2:=Fd;
  320. // regs.reg3:=longint(@Info);
  321. // StatFS:=(SysCall(SysCall_nr_fstatfs,regs)=0);
  322. LinuxError:=fpgeterrno;
  323. end;
  324. Function Link(OldPath,NewPath:pathstr):boolean;
  325. {
  326. Proceduces a hard link from new to old.
  327. In effect, new will be the same file as old.
  328. }
  329. begin
  330. { oldpath:=oldpath+#0;
  331. newpath:=newpath+#0;
  332. regs.reg2:=longint(@oldpath[1]);
  333. regs.reg3:=longint(@newpath[1]);
  334. Link:=SysCall(SysCall_nr_link,regs)=0;}
  335. Link:=Fplink(oldpath,newpath)=0;
  336. linuxerror:=fpgeterrno;
  337. end;
  338. Function Umask(Mask:Integer):integer;
  339. {
  340. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  341. previous value.
  342. }
  343. begin
  344. Umask:=fpumask(mask);
  345. linuxerror:=0;
  346. end;
  347. Function Access(Path:Pathstr ;mode:longint):boolean;
  348. {
  349. Test users access rights on the specified file.
  350. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  351. R,W,X stand for read,write and Execute access, simultaneously.
  352. F_OK checks whether the test would be allowed on the file.
  353. i.e. It checks the search permissions in all directory components
  354. of the path.
  355. The test is done with the real user-ID, instead of the effective.
  356. If access is denied, or an error occurred, false is returned.
  357. If access is granted, true is returned.
  358. Errors other than no access,are reported in linuxerror.
  359. }
  360. begin
  361. access:=fpaccess(path,mode)=0;
  362. { path:=path+#0;
  363. sr.reg2:=longint(@(path[1]));
  364. sr.reg3:=mode;
  365. access:=(SysCall(Syscall_nr_access,sr)=0);
  366. }
  367. linuxerror:=fpgeterrno;
  368. end;
  369. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  370. {
  371. Copies the filedescriptor oldfile to newfile
  372. }
  373. begin
  374. newfile:=fpdup(oldfile);
  375. linuxerror:=fpgeterrno;
  376. Dup:=(LinuxError=0);
  377. end;
  378. Function Dup2(oldfile,newfile:longint):Boolean;
  379. {
  380. Copies the filedescriptor oldfile to newfile
  381. }
  382. begin
  383. fpdup2(oldfile,newfile);
  384. linuxerror:=fpgeterrno;
  385. Dup2:=(LinuxError=0);
  386. end;
  387. Function AssignPipe(var pipe_in,pipe_out:longint):boolean; external name 'FPC_SYSC_ASSIGNPIPE';
  388. Function PClose(Var F:text) :longint;
  389. var
  390. pl : ^longint;
  391. res : longint;
  392. begin
  393. pclose:=fpclose(Textrec(F).Handle);
  394. //sr.reg2:=Textrec(F).Handle;
  395. //SysCall (syscall_nr_close,sr);
  396. { closed our side, Now wait for the other - this appears to be needed ?? }
  397. pl:=@(textrec(f).userdata[2]);
  398. fpwaitpid(pl^,@res,0);
  399. pclose:=res shr 8;
  400. end;
  401. Function PClose(Var F:file) : longint;
  402. var
  403. pl : ^longint;
  404. res : longint;
  405. begin
  406. // sr.reg2:=FileRec(F).Handle;
  407. // SysCall (Syscall_nr_close,sr);
  408. fpclose(FileRec(F).Handle);
  409. { closed our side, Now wait for the other - this appears to be needed ?? }
  410. pl:=@(filerec(f).userdata[2]);
  411. fpwaitpid(pl^,@res,0);
  412. pclose:=res shr 8;
  413. end;
  414. Function mkFifo(pathname:string;mode:longint):boolean;
  415. begin
  416. mkfifo:=fpmkfifo(pathname,mode)=0;
  417. // pathname:=pathname+#0;
  418. // regs.reg2:=longint(@pathname[1]);
  419. // regs.reg3:=mode or STAT_IFIFO;
  420. // regs.reg4:=0;
  421. // mkFifo:=(SysCall(syscall_nr_mknod,regs)=0);
  422. end;
  423. Function Uname(var unamerec:utsname):Boolean;
  424. {
  425. Get machine's names
  426. }
  427. Begin
  428. Uname:=fpuname(unamerec)=0;
  429. LinuxError:=fpgetErrno;
  430. End;
  431. Function Kill(Pid:longint;Sig:longint):integer;
  432. {
  433. Send signal 'sig' to a process, or a group of processes.
  434. If Pid > 0 then the signal is sent to pid
  435. pid=-1 to all processes except process 1
  436. pid < -1 to process group -pid
  437. Return value is zero, except for case three, where the return value
  438. is the number of processes to which the signal was sent.
  439. }
  440. begin
  441. kill:=fpkill(pid,sig);
  442. if kill<0 then
  443. Kill:=0;
  444. linuxerror:=fpgeterrno;
  445. end;
  446. Procedure SigProcMask(How:longint;SSet,OldSSet:PSigSet);
  447. {
  448. Change the list of currently blocked signals.
  449. How determines which signals will be blocked :
  450. SigBlock : Add SSet to the current list of blocked signals
  451. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  452. SigSetMask : Set the list of blocked signals to SSet
  453. if OldSSet is non-null, the old set will be saved there.
  454. }
  455. begin
  456. fpsigprocmask(how,sset^,oldsset^);
  457. linuxerror:=fpgeterrno;
  458. end;
  459. Function SigPending:SigSet;
  460. {
  461. Allows examination of pending signals. The signal mask of pending
  462. signals is set in SSet
  463. }
  464. var
  465. dummy : Sigset;
  466. begin
  467. fpsigpending(dummy);
  468. linuxerror:=fpgeterrno;
  469. Sigpending:=dummy;
  470. end;
  471. Procedure SigSuspend(Mask:Sigset);
  472. {
  473. Set the signal mask with Mask, and suspend the program until a signal
  474. is received.
  475. }
  476. begin
  477. fpsigsuspend(mask);
  478. linuxerror:=fpgeterrno;
  479. end;
  480. Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
  481. {
  482. Install a new handler for signal Signum.
  483. The old signal handler is returned.
  484. This call does, in fact, the same as SigAction.
  485. }
  486. var ret:signalhandler;
  487. begin
  488. ret:=fpsignal(signum,handler);
  489. Linuxerror:=fpgeterrno;
  490. If longint(ret)=Sig_Err then
  491. begin
  492. Signal:=nil;
  493. Linuxerror:=fpgeterrno;
  494. end
  495. else
  496. begin
  497. Signal:=signalhandler(ret);
  498. linuxerror:=0;
  499. end;
  500. end;
  501. Function Alarm(Sec : Longint) : longint;
  502. begin
  503. Alarm:=FPAlarm(sec);
  504. end;
  505. Procedure Pause;
  506. begin
  507. fppause;
  508. end;
  509. {
  510. Function NanoSleep(const req : timespec;var rem : timespec) : longint;
  511. begin
  512. sr.reg2:=longint(@req);
  513. sr.reg3:=longint(@rem);
  514. NanoSleep:=Syscall(syscall_nr_nanosleep,sr);
  515. LinuxError:=Errno;
  516. end;
  517. }
  518. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  519. {
  520. Interface to Unix ioctl call.
  521. Performs various operations on the filedescriptor Handle.
  522. Ndx describes the operation to perform.
  523. Data points to data needed for the Ndx function. The structure of this
  524. data is function-dependent.
  525. }
  526. begin
  527. ioctl:=fpioctl(handle,ndx,data)=0;
  528. LinuxError:=fpgetErrno;
  529. end;
  530. function MMap(const m:tmmapargs):pointer;
  531. begin
  532. mmap:=fpmmap(pointer(m.address),m.size,m.prot, m.flags,m.fd, m.offset);
  533. LinuxError:=fpgetErrno;
  534. end;
  535. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  536. begin
  537. MUnMap:=fpmunmap(p,size)=0;
  538. LinuxError:=fpgetErrno;
  539. end;
  540. {--------------------------------
  541. Port IO functions
  542. --------------------------------}
  543. {$ifdef cpui386}
  544. {
  545. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  546. {
  547. Set permissions on NUM ports starting with port FROM to VALUE
  548. this works ONLY as root.
  549. }
  550. begin
  551. Sr.Reg2:=From;
  552. Sr.Reg3:=Num;
  553. Sr.Reg4:=Value;
  554. IOPerm:=Syscall(Syscall_nr_ioperm,sr)=0;
  555. LinuxError:=fpgetErrno;
  556. end;
  557. Function IoPL(Level : longint) : Boolean;
  558. begin
  559. Sr.Reg2:=Level;
  560. IOPL:=Syscall(Syscall_nr_iopl,sr)=0;
  561. LinuxError:=fpgetErrno;
  562. end;
  563. }
  564. {$endif cpui386}
  565. {
  566. $Log$
  567. Revision 1.6 2003-10-17 22:13:30 olle
  568. * changed i386 to cpui386
  569. Revision 1.5 2003/09/20 17:27:05 marco
  570. * mmap fix.
  571. Revision 1.4 2003/09/16 20:52:24 marco
  572. * small cleanups. Mostly killing of already commented code in unix etc
  573. Revision 1.3 2003/09/16 16:06:02 peter
  574. * add typecasts for oldlinuxstat
  575. Revision 1.2 2003/09/15 21:07:41 marco
  576. * second round of linux fixes. oldlinux now works
  577. Revision 1.1 2003/09/15 20:30:49 marco
  578. * syscalls
  579. Revision 1.10 2003/09/14 20:15:01 marco
  580. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  581. Revision 1.9 2003/07/08 21:23:24 peter
  582. * sparc fixes
  583. Revision 1.8 2002/12/18 16:43:26 marco
  584. * new unix rtl, linux part.....
  585. Revision 1.7 2002/09/07 16:01:20 peter
  586. * old logs removed and tabs fixed
  587. Revision 1.6 2002/03/05 20:04:25 michael
  588. + Patch from Sebastian for FCNTL call
  589. }