unixsysc.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2000 by Marco van de Voort
  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. {
  13. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  14. {NOT IMPLEMENTED YET UNDER BSD}
  15. begin // perhaps it is better to implement the hack from solaris then this msg
  16. HALT;
  17. END;
  18. if (pointer(func)=nil) or (sp=nil) then
  19. begin
  20. LinuxError:=EsysEInval;
  21. exit(-1);
  22. end;
  23. asm
  24. { Insert the argument onto the new stack. }
  25. movl sp,%ecx
  26. subl $8,%ecx
  27. movl args,%eax
  28. movl %eax,4(%ecx)
  29. { Save the function pointer as the zeroth argument.
  30. It will be popped off in the child in the ebx frobbing below. }
  31. movl func,%eax
  32. movl %eax,0(%ecx)
  33. { Do the system call }
  34. pushl %ebx
  35. pushl %ebx
  36. // movl flags,%ebx
  37. movl $251,%eax
  38. int $0x80
  39. popl %ebx
  40. popl %ebx
  41. test %eax,%eax
  42. jnz .Lclone_end
  43. { We're in the new thread }
  44. subl %ebp,%ebp { terminate the stack frame }
  45. call *%ebx
  46. { exit process }
  47. movl %eax,%ebx
  48. movl $1,%eax
  49. int $0x80
  50. .Lclone_end:
  51. movl %eax,__RESULT
  52. end;
  53. end;
  54. }
  55. {
  56. Procedure GetTimeOfDay(var tv:timeval);
  57. {
  58. Get the number of seconds since 00:00, January 1 1970, GMT
  59. the time NOT corrected any way
  60. }
  61. var tz : timezone;
  62. begin
  63. do_syscall(syscall_nr_gettimeofday,longint(@tv),longint(@tz));
  64. LinuxError:=FpGetErrno;
  65. end;
  66. }
  67. Function fdFlush (fd : Longint) : Boolean;
  68. begin
  69. fdflush:=do_syscall(syscall_nr_fsync,fd)=0;
  70. LinuxError:=FpGetErrno;
  71. end;
  72. Function Flock (fd,mode : longint) : boolean;
  73. begin
  74. Flock:=do_syscall(syscall_nr_flock,fd,mode)=0;
  75. LinuxError:=FpGetErrno;
  76. end;
  77. Function StatFS(Path:Pathstr;Var Info:Tstatfs):Boolean;
  78. {
  79. Get all information on a fileSystem, and return it in Info.
  80. Path is the name of a file/directory on the fileSystem you wish to
  81. investigate.
  82. }
  83. begin
  84. path:=path+#0;
  85. StatFS:=Do_Syscall(syscall_nr_statfs,longint(@path[1]),longint(@info))=0;
  86. LinuxError:=FpGetErrno;
  87. end;
  88. Function StatFS(Fd:Longint;Var Info:tstatfs):Boolean;
  89. {
  90. Get all information on a fileSystem, and return it in Info.
  91. Fd is the file descriptor of a file/directory on the fileSystem
  92. you wish to investigate.
  93. }
  94. begin
  95. StatFS:=do_syscall(syscall_nr_fstatfs,fd,longint(@info))=0;
  96. LinuxError:=FpGetErrno;
  97. end;
  98. Function AssignPipe(var pipe_in,pipe_out:longint):boolean; [public, alias : 'FPC_SYSC_ASSIGNPIPE'];
  99. {
  100. Sets up a pair of file variables, which act as a pipe. The first one can
  101. be read from, the second one can be written to.
  102. If the operation was unsuccesful, linuxerror is set.
  103. }
  104. var
  105. pip : tpipe;
  106. begin
  107. do_syscall(syscall_nr_pipe,longint(@pip));
  108. LinuxError:=FpGetErrno;
  109. pipe_in:=pip[1];
  110. pipe_out:=pip[2];
  111. AssignPipe:=(LinuxError=0);
  112. end;
  113. Function PClose(Var F:text) :longint;
  114. var
  115. pl : ^longint;
  116. res : longint;
  117. begin
  118. do_syscall(syscall_nr_close,Textrec(F).Handle);
  119. { closed our side, Now wait for the other - this appears to be needed ?? }
  120. pl:=@(textrec(f).userdata[2]);
  121. fpwaitpid(pl^,@res,0);
  122. pclose:=res shr 8;
  123. end;
  124. Function PClose(Var F:file) : longint;
  125. var
  126. pl : ^longint;
  127. res : longint;
  128. begin
  129. do_syscall(syscall_nr_close,filerec(F).Handle);
  130. { closed our side, Now wait for the other - this appears to be needed ?? }
  131. pl:=@(filerec(f).userdata[2]);
  132. fpwaitpid(pl^,@res,0);
  133. pclose:=res shr 8;
  134. end;
  135. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  136. begin
  137. MUnMap:=do_syscall(syscall_nr_munmap,longint(P),Size)=0;
  138. LinuxError:=FpGetErrno;
  139. end;
  140. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  141. var lerrno : Longint;
  142. errset : Boolean;
  143. Res : Longint;
  144. begin
  145. errset:=false;
  146. Res:=0;
  147. asm
  148. pushl %esi
  149. movl 12(%ebp), %esi // get stack addr
  150. subl $4, %esi
  151. movl 20(%ebp), %eax // get __arg
  152. movl %eax, (%esi)
  153. subl $4, %esi
  154. movl 8(%ebp), %eax // get __fn
  155. movl %eax, (%esi)
  156. pushl 16(%ebp)
  157. pushl %esi
  158. mov syscall_nr_rfork, %eax
  159. int $0x80 // call actualsyscall
  160. jb .L2
  161. test %edx, %edx
  162. jz .L1
  163. movl %esi,%esp
  164. popl %eax
  165. call %eax
  166. addl $8, %esp
  167. call halt // Does not return
  168. .L2:
  169. mov %eax,LErrNo
  170. mov $true,Errset
  171. mov $-1,%eax
  172. // jmp .L1
  173. .L1:
  174. addl $8, %esp
  175. popl %esi
  176. mov %eax,Res
  177. end;
  178. If ErrSet Then
  179. fpSetErrno(LErrno);
  180. Clone:=Res;
  181. end;
  182. {
  183. $Log$
  184. Revision 1.11 2003-09-20 12:38:29 marco
  185. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  186. Revision 1.10 2003/09/15 20:08:49 marco
  187. * small fixes. FreeBSD now cycles
  188. Revision 1.9 2003/09/15 07:09:58 marco
  189. * small fixes, round 1
  190. Revision 1.8 2003/09/14 20:15:01 marco
  191. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  192. Revision 1.7 2003/01/05 19:02:29 marco
  193. * Should now work with baseunx. (gmake all works)
  194. Revision 1.6 2002/10/18 12:19:59 marco
  195. * Fixes to get the generic *BSD RTL compiling again + fixes for thread
  196. support. Still problems left in fexpand. (inoutres?) Therefore fixed
  197. sysposix not yet commited
  198. Revision 1.5 2002/09/07 16:01:18 peter
  199. * old logs removed and tabs fixed
  200. Revision 1.4 2002/05/06 09:35:09 marco
  201. * Some stuff from 1.0.x ported
  202. }