2
0

unixsysc.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY;without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  13. {NOT IMPLEMENTED YET UNDER BSD}
  14. begin // perhaps it is better to implement the hack from solaris then this msg
  15. HALT;
  16. END;
  17. if (pointer(func)=nil) or (sp=nil) then
  18. begin
  19. Lfpseterrno(EsysEInval);
  20. exit(-1);
  21. end;
  22. asm
  23. { Insert the argument onto the new stack. }
  24. movl sp,%ecx
  25. subl $8,%ecx
  26. movl args,%eax
  27. movl %eax,4(%ecx)
  28. { Save the function pointer as the zeroth argument.
  29. It will be popped off in the child in the ebx frobbing below. }
  30. movl func,%eax
  31. movl %eax,0(%ecx)
  32. { Do the system call }
  33. pushl %ebx
  34. pushl %ebx
  35. // movl flags,%ebx
  36. movl $251,%eax
  37. int $0x80
  38. popl %ebx
  39. popl %ebx
  40. test %eax,%eax
  41. jnz .Lclone_end
  42. { We're in the new thread }
  43. subl %ebp,%ebp { terminate the stack frame }
  44. call *%ebx
  45. { exit process }
  46. movl %eax,%ebx
  47. movl $1,%eax
  48. int $0x80
  49. .Lclone_end:
  50. movl %eax,__RESULT
  51. end;
  52. end;
  53. }
  54. {$ifndef FPC_USE_LIBC}
  55. Function fpFlock (fd,mode : longint) : cint;
  56. begin
  57. Flock:=do_syscall(syscall_nr_flock,fd,mode);
  58. end;
  59. Function fpfStatFS (Fd: cint; Info:pstatfs):cint;
  60. begin
  61. fpfstatfs:=do_SysCall(SysCall_nr_fstatfs,fd,TSysParam(info))
  62. end;
  63. Function fpStatFS (Path:pchar; Info:pstatfs):cint;
  64. begin
  65. fpstatfs:=do_SysCall(SysCall_nr_statfs,TSysParam(path),TSysParam(Info))
  66. end;
  67. Function fpfsync (fd : cint) : cint;
  68. begin
  69. fpfsync:=do_SysCall(syscall_nr_fsync, fd);
  70. end;
  71. // needs oldfpccall;
  72. Function intAssignPipe(var pipe_in,pipe_out:longint;var errn:cint):cint; oldfpccall;
  73. {
  74. Sets up a pair of file variables, which act as a pipe. The first one can
  75. be read from, the second one can be written to.
  76. If the operation was unsuccesful, linuxerror is set.
  77. }
  78. begin
  79. {$ifdef cpui386}
  80. asm
  81. mov $42,%eax
  82. int $0x80
  83. jb .Lerror
  84. mov pipe_in,%ebx
  85. mov %eax,(%ebx)
  86. mov pipe_out,%ebx
  87. mov $0,%eax
  88. mov %edx,(%ebx)
  89. mov %eax,%ebx
  90. jmp .Lexit
  91. .Lerror:
  92. mov %eax,%ebx
  93. mov $-1,%eax
  94. .Lexit:
  95. mov Errn,%edx
  96. mov %ebx,(%edx)
  97. end;
  98. {$endif}
  99. end;
  100. Function PClose(Var F:text) :cint;
  101. var
  102. pl : ^longint;
  103. res : longint;
  104. begin
  105. do_syscall(syscall_nr_close,Textrec(F).Handle);
  106. { closed our side, Now wait for the other - this appears to be needed ?? }
  107. pl:=@(textrec(f).userdata[2]);
  108. fpwaitpid(pl^,@res,0);
  109. pclose:=res shr 8;
  110. end;
  111. Function PClose(Var F:file) : cint;
  112. var
  113. pl : ^cint;
  114. res : cint;
  115. begin
  116. do_syscall(syscall_nr_close,filerec(F).Handle);
  117. { closed our side, Now wait for the other - this appears to be needed ?? }
  118. pl:=@(filerec(f).userdata[2]);
  119. fpwaitpid(pl^,@res,0);
  120. pclose:=res shr 8;
  121. end;
  122. function MUnMap (P : Pointer; Size : size_t) : cint;
  123. begin
  124. MUnMap:=do_syscall(syscall_nr_munmap,longint(P),Size);
  125. end;
  126. {$else}
  127. Function PClose(Var F:file) : cint;
  128. var
  129. pl : ^cint;
  130. res : cint;
  131. begin
  132. fpclose(filerec(F).Handle);
  133. { closed our side, Now wait for the other - this appears to be needed ?? }
  134. pl:=@(filerec(f).userdata[2]);
  135. fpwaitpid(pl^,@res,0);
  136. pclose:=res shr 8;
  137. end;
  138. Function PClose(Var F:text) :cint;
  139. var
  140. pl : ^longint;
  141. res : longint;
  142. begin
  143. fpclose(Textrec(F).Handle);
  144. { closed our side, Now wait for the other - this appears to be needed ?? }
  145. pl:=@(textrec(f).userdata[2]);
  146. fpwaitpid(pl^,@res,0);
  147. pclose:=res shr 8;
  148. end;
  149. {$endif}
  150. // can't have oldfpccall here, linux doesn't need it.
  151. Function AssignPipe(var pipe_in,pipe_out:cint):cint; [public, alias : 'FPC_SYSC_ASSIGNPIPE'];
  152. {
  153. Sets up a pair of file variables, which act as a pipe. The first one can
  154. be read from, the second one can be written to.
  155. If the operation was unsuccesful, linuxerror is set.
  156. }
  157. var
  158. ret : longint;
  159. errn : cint;
  160. {$ifdef FPC_USE_LIBC}
  161. fdis : array[0..1] of cint;
  162. {$endif}
  163. begin
  164. {$ifndef FPC_USE_LIBC}
  165. ret:=intAssignPipe(pipe_in,pipe_out,errn);
  166. if ret=-1 Then
  167. fpseterrno(errn);
  168. {$ELSE}
  169. fdis[0]:=pipe_in;
  170. fdis[1]:=pipe_out;
  171. ret:=pipe(fdis);
  172. pipe_in:=fdis[0];
  173. pipe_out:=fdis[1];
  174. {$ENDIF}
  175. AssignPipe:=ret;
  176. end;
  177. {
  178. function intClone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; oldfpccall;
  179. var lerrno : Longint;
  180. errset : Boolean;
  181. Res : Longint;
  182. begin
  183. errset:=false;
  184. Res:=0;
  185. asm
  186. pushl %esi
  187. movl 12(%ebp), %esi // get stack addr
  188. subl $4, %esi
  189. movl 20(%ebp), %eax // get __arg
  190. movl %eax, (%esi)
  191. subl $4, %esi
  192. movl 8(%ebp), %eax // get __fn
  193. movl %eax, (%esi)
  194. pushl 16(%ebp)
  195. pushl %esi
  196. mov syscall_nr_rfork, %eax
  197. int $0x80 // call actualsyscall
  198. jb .L2
  199. test %edx, %edx
  200. jz .L1
  201. movl %esi,%esp
  202. popl %eax
  203. call %eax
  204. addl $8, %esp
  205. call halt // Does not return
  206. .L2:
  207. mov %eax,LErrNo
  208. mov $true,Errset
  209. mov $-1,%eax
  210. // jmp .L1
  211. .L1:
  212. addl $8, %esp
  213. popl %esi
  214. mov %eax,Res
  215. end;
  216. If ErrSet Then
  217. fpSetErrno(LErrno);
  218. intClone:=Res;
  219. end;
  220. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  221. begin
  222. Clone:=
  223. intclone(tclonefunc(func),sp,flags,args);
  224. end;
  225. }