unixsysc.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. Lfpseterrno(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. {$ifndef FPC_USE_LIBC}
  56. Function fsync (fd : cint) : cint;
  57. begin
  58. fsync:=do_syscall(syscall_nr_fsync,fd);
  59. end;
  60. Function Flock (fd,mode : longint) : cint;
  61. begin
  62. Flock:=do_syscall(syscall_nr_flock,fd,mode);
  63. end;
  64. Function fStatFS(Fd:Longint;Var Info:tstatfs):cint;
  65. {
  66. Get all information on a fileSystem, and return it in Info.
  67. Fd is the file descriptor of a file/directory on the fileSystem
  68. you wish to investigate.
  69. }
  70. begin
  71. fStatFS:=do_syscall(syscall_nr_fstatfs,fd,longint(@info));
  72. end;
  73. Function StatFS(path:pchar;Var Info:tstatfs):cint;
  74. {
  75. Get all information on a fileSystem, and return it in Info.
  76. Fd is the file descriptor of a file/directory on the fileSystem
  77. you wish to investigate.
  78. }
  79. begin
  80. StatFS:=do_syscall(syscall_nr_statfs,longint(path),longint(@info));
  81. end;
  82. // needs oldfpccall;
  83. Function intAssignPipe(var pipe_in,pipe_out:longint;var errn:cint):cint; {$ifndef ver1_0} oldfpccall;{$endif}
  84. {
  85. Sets up a pair of file variables, which act as a pipe. The first one can
  86. be read from, the second one can be written to.
  87. If the operation was unsuccesful, linuxerror is set.
  88. }
  89. begin
  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. end;
  109. Function PClose(Var F:text) :cint;
  110. var
  111. pl : ^longint;
  112. res : longint;
  113. begin
  114. do_syscall(syscall_nr_close,Textrec(F).Handle);
  115. { closed our side, Now wait for the other - this appears to be needed ?? }
  116. pl:=@(textrec(f).userdata[2]);
  117. fpwaitpid(pl^,@res,0);
  118. pclose:=res shr 8;
  119. end;
  120. Function PClose(Var F:file) : cint;
  121. var
  122. pl : ^cint;
  123. res : cint;
  124. begin
  125. do_syscall(syscall_nr_close,filerec(F).Handle);
  126. { closed our side, Now wait for the other - this appears to be needed ?? }
  127. pl:=@(filerec(f).userdata[2]);
  128. fpwaitpid(pl^,@res,0);
  129. pclose:=res shr 8;
  130. end;
  131. function MUnMap (P : Pointer; Size : size_t) : cint;
  132. begin
  133. MUnMap:=do_syscall(syscall_nr_munmap,longint(P),Size);
  134. end;
  135. {$else}
  136. Function PClose(Var F:file) : cint;
  137. var
  138. pl : ^cint;
  139. res : cint;
  140. begin
  141. fpclose(filerec(F).Handle);
  142. { closed our side, Now wait for the other - this appears to be needed ?? }
  143. pl:=@(filerec(f).userdata[2]);
  144. fpwaitpid(pl^,@res,0);
  145. pclose:=res shr 8;
  146. end;
  147. Function PClose(Var F:text) :cint;
  148. var
  149. pl : ^longint;
  150. res : longint;
  151. begin
  152. fpclose(Textrec(F).Handle);
  153. { closed our side, Now wait for the other - this appears to be needed ?? }
  154. pl:=@(textrec(f).userdata[2]);
  155. fpwaitpid(pl^,@res,0);
  156. pclose:=res shr 8;
  157. end;
  158. {$endif}
  159. // can't have oldfpccall here, linux doesn't need it.
  160. Function AssignPipe(var pipe_in,pipe_out:cint):cint; [public, alias : 'FPC_SYSC_ASSIGNPIPE'];
  161. {
  162. Sets up a pair of file variables, which act as a pipe. The first one can
  163. be read from, the second one can be written to.
  164. If the operation was unsuccesful, linuxerror is set.
  165. }
  166. var
  167. ret : longint;
  168. errn : cint;
  169. {$ifdef FPC_USE_LIBC}
  170. fdis : array[0..1] of cint;
  171. {$endif}
  172. begin
  173. {$ifndef FPC_USE_LIBC}
  174. ret:=intAssignPipe(pipe_in,pipe_out,errn);
  175. if ret=-1 Then
  176. fpseterrno(errn);
  177. {$ELSE}
  178. fdis[0]:=pipe_in;
  179. fdis[1]:=pipe_out;
  180. ret:=pipe(fdis);
  181. pipe_in:=fdis[0];
  182. pipe_out:=fdis[1];
  183. {$ENDIF}
  184. AssignPipe:=ret;
  185. end;
  186. {
  187. function intClone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifndef ver1_0} oldfpccall; {$endif}
  188. var lerrno : Longint;
  189. errset : Boolean;
  190. Res : Longint;
  191. begin
  192. errset:=false;
  193. Res:=0;
  194. asm
  195. pushl %esi
  196. movl 12(%ebp), %esi // get stack addr
  197. subl $4, %esi
  198. movl 20(%ebp), %eax // get __arg
  199. movl %eax, (%esi)
  200. subl $4, %esi
  201. movl 8(%ebp), %eax // get __fn
  202. movl %eax, (%esi)
  203. pushl 16(%ebp)
  204. pushl %esi
  205. mov syscall_nr_rfork, %eax
  206. int $0x80 // call actualsyscall
  207. jb .L2
  208. test %edx, %edx
  209. jz .L1
  210. movl %esi,%esp
  211. popl %eax
  212. call %eax
  213. addl $8, %esp
  214. call halt // Does not return
  215. .L2:
  216. mov %eax,LErrNo
  217. mov $true,Errset
  218. mov $-1,%eax
  219. // jmp .L1
  220. .L1:
  221. addl $8, %esp
  222. popl %esi
  223. mov %eax,Res
  224. end;
  225. If ErrSet Then
  226. fpSetErrno(LErrno);
  227. intClone:=Res;
  228. end;
  229. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  230. begin
  231. Clone:=
  232. intclone(tclonefunc(func),sp,flags,args);
  233. end;
  234. }
  235. {
  236. $Log$
  237. Revision 1.5 2004-01-04 01:11:28 marco
  238. * a new qod port of the freebsd rtl. To be refined in the coming days.
  239. Revision 1.18 2004/01/01 17:07:21 marco
  240. * few small freebsd fixes backported from debugging linux
  241. Revision 1.17 2003/12/30 12:32:30 marco
  242. *** empty log message ***
  243. Revision 1.16 2003/11/19 17:11:40 marco
  244. * termio unit
  245. Revision 1.15 2003/11/19 10:12:02 marco
  246. * more cleanups
  247. Revision 1.14 2003/11/17 10:05:51 marco
  248. * threads for FreeBSD. Not working tho
  249. Revision 1.13 2003/11/14 16:21:59 marco
  250. * linuxerror elimination
  251. Revision 1.12 2003/11/09 12:00:16 marco
  252. * pipe fix
  253. Revision 1.11 2003/09/20 12:38:29 marco
  254. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  255. Revision 1.10 2003/09/15 20:08:49 marco
  256. * small fixes. FreeBSD now cycles
  257. Revision 1.9 2003/09/15 07:09:58 marco
  258. * small fixes, round 1
  259. Revision 1.8 2003/09/14 20:15:01 marco
  260. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  261. Revision 1.7 2003/01/05 19:02:29 marco
  262. * Should now work with baseunx. (gmake all works)
  263. Revision 1.6 2002/10/18 12:19:59 marco
  264. * Fixes to get the generic *BSD RTL compiling again + fixes for thread
  265. support. Still problems left in fexpand. (inoutres?) Therefore fixed
  266. sysposix not yet commited
  267. Revision 1.5 2002/09/07 16:01:18 peter
  268. * old logs removed and tabs fixed
  269. Revision 1.4 2002/05/06 09:35:09 marco
  270. * Some stuff from 1.0.x ported
  271. }