unixsysc.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 fsync (fd : cint) : cint;
  56. begin
  57. fsync:=do_syscall(syscall_nr_fsync,fd);
  58. end;
  59. Function Flock (fd,mode : longint) : cint;
  60. begin
  61. Flock:=do_syscall(syscall_nr_flock,fd,mode);
  62. end;
  63. Function fStatFS(Fd:Longint;Var Info:tstatfs):cint;
  64. {
  65. Get all information on a fileSystem, and return it in Info.
  66. Fd is the file descriptor of a file/directory on the fileSystem
  67. you wish to investigate.
  68. }
  69. begin
  70. fStatFS:=do_syscall(syscall_nr_fstatfs,fd,longint(@info));
  71. end;
  72. Function StatFS(path:pchar;Var Info:tstatfs):cint;
  73. {
  74. Get all information on a fileSystem, and return it in Info.
  75. Fd is the file descriptor of a file/directory on the fileSystem
  76. you wish to investigate.
  77. }
  78. begin
  79. StatFS:=do_syscall(syscall_nr_statfs,longint(path),longint(@info));
  80. end;
  81. // needs oldfpccall;
  82. Function intAssignPipe(var pipe_in,pipe_out:longint;var errn:cint):cint; oldfpccall;
  83. {
  84. Sets up a pair of file variables, which act as a pipe. The first one can
  85. be read from, the second one can be written to.
  86. If the operation was unsuccesful, linuxerror is set.
  87. }
  88. begin
  89. {$ifdef cpui386}
  90. asm
  91. mov $42,%eax
  92. int $0x80
  93. jb .Lerror
  94. mov pipe_in,%ebx
  95. mov %eax,(%ebx)
  96. mov pipe_out,%ebx
  97. mov $0,%eax
  98. mov %edx,(%ebx)
  99. mov %eax,%ebx
  100. jmp .Lexit
  101. .Lerror:
  102. mov %eax,%ebx
  103. mov $-1,%eax
  104. .Lexit:
  105. mov Errn,%edx
  106. mov %ebx,(%edx)
  107. end;
  108. {$endif}
  109. end;
  110. Function PClose(Var F:text) :cint;
  111. var
  112. pl : ^longint;
  113. res : longint;
  114. begin
  115. do_syscall(syscall_nr_close,Textrec(F).Handle);
  116. { closed our side, Now wait for the other - this appears to be needed ?? }
  117. pl:=@(textrec(f).userdata[2]);
  118. fpwaitpid(pl^,@res,0);
  119. pclose:=res shr 8;
  120. end;
  121. Function PClose(Var F:file) : cint;
  122. var
  123. pl : ^cint;
  124. res : cint;
  125. begin
  126. do_syscall(syscall_nr_close,filerec(F).Handle);
  127. { closed our side, Now wait for the other - this appears to be needed ?? }
  128. pl:=@(filerec(f).userdata[2]);
  129. fpwaitpid(pl^,@res,0);
  130. pclose:=res shr 8;
  131. end;
  132. function MUnMap (P : Pointer; Size : size_t) : cint;
  133. begin
  134. MUnMap:=do_syscall(syscall_nr_munmap,longint(P),Size);
  135. end;
  136. {$else}
  137. Function PClose(Var F:file) : cint;
  138. var
  139. pl : ^cint;
  140. res : cint;
  141. begin
  142. fpclose(filerec(F).Handle);
  143. { closed our side, Now wait for the other - this appears to be needed ?? }
  144. pl:=@(filerec(f).userdata[2]);
  145. fpwaitpid(pl^,@res,0);
  146. pclose:=res shr 8;
  147. end;
  148. Function PClose(Var F:text) :cint;
  149. var
  150. pl : ^longint;
  151. res : longint;
  152. begin
  153. fpclose(Textrec(F).Handle);
  154. { closed our side, Now wait for the other - this appears to be needed ?? }
  155. pl:=@(textrec(f).userdata[2]);
  156. fpwaitpid(pl^,@res,0);
  157. pclose:=res shr 8;
  158. end;
  159. {$endif}
  160. // can't have oldfpccall here, linux doesn't need it.
  161. Function AssignPipe(var pipe_in,pipe_out:cint):cint; [public, alias : 'FPC_SYSC_ASSIGNPIPE'];
  162. {
  163. Sets up a pair of file variables, which act as a pipe. The first one can
  164. be read from, the second one can be written to.
  165. If the operation was unsuccesful, linuxerror is set.
  166. }
  167. var
  168. ret : longint;
  169. errn : cint;
  170. {$ifdef FPC_USE_LIBC}
  171. fdis : array[0..1] of cint;
  172. {$endif}
  173. begin
  174. {$ifndef FPC_USE_LIBC}
  175. ret:=intAssignPipe(pipe_in,pipe_out,errn);
  176. if ret=-1 Then
  177. fpseterrno(errn);
  178. {$ELSE}
  179. fdis[0]:=pipe_in;
  180. fdis[1]:=pipe_out;
  181. ret:=pipe(fdis);
  182. pipe_in:=fdis[0];
  183. pipe_out:=fdis[1];
  184. {$ENDIF}
  185. AssignPipe:=ret;
  186. end;
  187. {
  188. function intClone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; oldfpccall;
  189. var lerrno : Longint;
  190. errset : Boolean;
  191. Res : Longint;
  192. begin
  193. errset:=false;
  194. Res:=0;
  195. asm
  196. pushl %esi
  197. movl 12(%ebp), %esi // get stack addr
  198. subl $4, %esi
  199. movl 20(%ebp), %eax // get __arg
  200. movl %eax, (%esi)
  201. subl $4, %esi
  202. movl 8(%ebp), %eax // get __fn
  203. movl %eax, (%esi)
  204. pushl 16(%ebp)
  205. pushl %esi
  206. mov syscall_nr_rfork, %eax
  207. int $0x80 // call actualsyscall
  208. jb .L2
  209. test %edx, %edx
  210. jz .L1
  211. movl %esi,%esp
  212. popl %eax
  213. call %eax
  214. addl $8, %esp
  215. call halt // Does not return
  216. .L2:
  217. mov %eax,LErrNo
  218. mov $true,Errset
  219. mov $-1,%eax
  220. // jmp .L1
  221. .L1:
  222. addl $8, %esp
  223. popl %esi
  224. mov %eax,Res
  225. end;
  226. If ErrSet Then
  227. fpSetErrno(LErrno);
  228. intClone:=Res;
  229. end;
  230. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  231. begin
  232. Clone:=
  233. intclone(tclonefunc(func),sp,flags,args);
  234. end;
  235. }