unixsysc.inc 7.8 KB

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