unixsysc.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // needs oldfpccall;
  99. Function intAssignPipe(var pipe_in,pipe_out:longint;var errn:cint):cint; {$ifndef ver1_0} oldfpccall;{$endif}
  100. {
  101. Sets up a pair of file variables, which act as a pipe. The first one can
  102. be read from, the second one can be written to.
  103. If the operation was unsuccesful, linuxerror is set.
  104. }
  105. begin
  106. asm
  107. mov $42,%eax
  108. int $0x80
  109. jb .Lerror
  110. mov pipe_in,%ebx
  111. mov %eax,(%ebx)
  112. mov pipe_out,%ebx
  113. mov $0,%eax
  114. mov %edx,(%ebx)
  115. mov %eax,%ebx
  116. jmp .Lexit
  117. .Lerror:
  118. mov %eax,%ebx
  119. mov $-1,%eax
  120. .Lexit:
  121. mov Errn,%edx
  122. mov %ebx,(%edx)
  123. end;
  124. end;
  125. // can't have oldfpccall here, linux doesn't need it.
  126. Function AssignPipe(var pipe_in,pipe_out:longint):boolean; [public, alias : 'FPC_SYSC_ASSIGNPIPE'];
  127. {
  128. Sets up a pair of file variables, which act as a pipe. The first one can
  129. be read from, the second one can be written to.
  130. If the operation was unsuccesful, linuxerror is set.
  131. }
  132. var
  133. ret : longint;
  134. errn : cint;
  135. begin
  136. ret:=intAssignPipe(pipe_in,pipe_out,errn);
  137. fpseterrno(errn);
  138. LinuxError:=Errn;
  139. AssignPipe:=(LinuxError=0);
  140. end;
  141. Function PClose(Var F:text) :longint;
  142. var
  143. pl : ^longint;
  144. res : longint;
  145. begin
  146. do_syscall(syscall_nr_close,Textrec(F).Handle);
  147. { closed our side, Now wait for the other - this appears to be needed ?? }
  148. pl:=@(textrec(f).userdata[2]);
  149. fpwaitpid(pl^,@res,0);
  150. pclose:=res shr 8;
  151. end;
  152. Function PClose(Var F:file) : longint;
  153. var
  154. pl : ^longint;
  155. res : longint;
  156. begin
  157. do_syscall(syscall_nr_close,filerec(F).Handle);
  158. { closed our side, Now wait for the other - this appears to be needed ?? }
  159. pl:=@(filerec(f).userdata[2]);
  160. fpwaitpid(pl^,@res,0);
  161. pclose:=res shr 8;
  162. end;
  163. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  164. begin
  165. MUnMap:=do_syscall(syscall_nr_munmap,longint(P),Size)=0;
  166. LinuxError:=FpGetErrno;
  167. end;
  168. function intClone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifndef ver1_0} oldfpccall; {$endif}
  169. var lerrno : Longint;
  170. errset : Boolean;
  171. Res : Longint;
  172. begin
  173. errset:=false;
  174. Res:=0;
  175. asm
  176. pushl %esi
  177. movl 12(%ebp), %esi // get stack addr
  178. subl $4, %esi
  179. movl 20(%ebp), %eax // get __arg
  180. movl %eax, (%esi)
  181. subl $4, %esi
  182. movl 8(%ebp), %eax // get __fn
  183. movl %eax, (%esi)
  184. pushl 16(%ebp)
  185. pushl %esi
  186. mov syscall_nr_rfork, %eax
  187. int $0x80 // call actualsyscall
  188. jb .L2
  189. test %edx, %edx
  190. jz .L1
  191. movl %esi,%esp
  192. popl %eax
  193. call %eax
  194. addl $8, %esp
  195. call halt // Does not return
  196. .L2:
  197. mov %eax,LErrNo
  198. mov $true,Errset
  199. mov $-1,%eax
  200. // jmp .L1
  201. .L1:
  202. addl $8, %esp
  203. popl %esi
  204. mov %eax,Res
  205. end;
  206. If ErrSet Then
  207. fpSetErrno(LErrno);
  208. intClone:=Res;
  209. end;
  210. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  211. begin
  212. Clone:=
  213. intclone(tclonefunc(func),sp,flags,args);
  214. end;
  215. {
  216. $Log$
  217. Revision 1.12 2003-11-09 12:00:16 marco
  218. * pipe fix
  219. Revision 1.11 2003/09/20 12:38:29 marco
  220. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  221. Revision 1.10 2003/09/15 20:08:49 marco
  222. * small fixes. FreeBSD now cycles
  223. Revision 1.9 2003/09/15 07:09:58 marco
  224. * small fixes, round 1
  225. Revision 1.8 2003/09/14 20:15:01 marco
  226. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  227. Revision 1.7 2003/01/05 19:02:29 marco
  228. * Should now work with baseunx. (gmake all works)
  229. Revision 1.6 2002/10/18 12:19:59 marco
  230. * Fixes to get the generic *BSD RTL compiling again + fixes for thread
  231. support. Still problems left in fexpand. (inoutres?) Therefore fixed
  232. sysposix not yet commited
  233. Revision 1.5 2002/09/07 16:01:18 peter
  234. * old logs removed and tabs fixed
  235. Revision 1.4 2002/05/06 09:35:09 marco
  236. * Some stuff from 1.0.x ported
  237. }