2
0

linuxnew.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. New linux unit. Linux only calls only. Will be renamed to linux.pp
  8. when 1.0.x support is killed off.
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY;without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. unit Linux;
  16. interface
  17. Type
  18. TSysinfo = packed record
  19. uptime : longint;
  20. loads : array[1..3] of longint;
  21. totalram,
  22. freeram,
  23. sharedram,
  24. bufferram,
  25. totalswap,
  26. freeswap : longint;
  27. procs : integer;
  28. s : string[18];
  29. end;
  30. PSysInfo = ^TSysInfo;
  31. Function Sysinfo(var Info:TSysinfo):Boolean; {$ifdef FPC_USE_LIBC} cdecl; external name 'sysinfo'; {$endif}
  32. Const
  33. CSIGNAL = $000000ff; // signal mask to be sent at exit
  34. CLONE_VM = $00000100; // set if VM shared between processes
  35. CLONE_FS = $00000200; // set if fs info shared between processes
  36. CLONE_FILES = $00000400; // set if open files shared between processes
  37. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  38. CLONE_PID = $00001000; // set if pid shared
  39. type
  40. TCloneFunc=function(args:pointer):longint;cdecl;
  41. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  42. implementation
  43. {$ifndef FPC_USE_LIBC}
  44. Uses Syscall;
  45. Function Sysinfo(var Info:TSysinfo):Boolean;
  46. {
  47. Get system info
  48. }
  49. Begin
  50. Sysinfo:=do_SysCall(SysCall_nr_Sysinfo,TSysParam(@info))=0;
  51. End;
  52. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  53. begin
  54. if (pointer(func)=nil) or (sp=nil) then
  55. exit(-1); // give an error result
  56. {$ifdef cpui386}
  57. {$ASMMODE ATT}
  58. asm
  59. { Insert the argument onto the new stack. }
  60. movl sp,%ecx
  61. subl $8,%ecx
  62. movl args,%eax
  63. movl %eax,4(%ecx)
  64. { Save the function pointer as the zeroth argument.
  65. It will be popped off in the child in the ebx frobbing below. }
  66. movl func,%eax
  67. movl %eax,0(%ecx)
  68. { Do the system call }
  69. pushl %ebx
  70. movl flags,%ebx
  71. movl SysCall_nr_clone,%eax
  72. int $0x80
  73. popl %ebx
  74. test %eax,%eax
  75. jnz .Lclone_end
  76. { We're in the new thread }
  77. subl %ebp,%ebp { terminate the stack frame }
  78. call *%ebx
  79. { exit process }
  80. movl %eax,%ebx
  81. movl $1,%eax
  82. int $0x80
  83. .Lclone_end:
  84. movl %eax,__RESULT
  85. end;
  86. {$endif cpui386}
  87. {$ifdef cpum68k}
  88. { No yet translated, my m68k assembler is too weak for such things PM }
  89. (*
  90. asm
  91. { Insert the argument onto the new stack. }
  92. movl sp,%ecx
  93. subl $8,%ecx
  94. movl args,%eax
  95. movl %eax,4(%ecx)
  96. { Save the function pointer as the zeroth argument.
  97. It will be popped off in the child in the ebx frobbing below. }
  98. movl func,%eax
  99. movl %eax,0(%ecx)
  100. { Do the system call }
  101. pushl %ebx
  102. movl flags,%ebx
  103. movl SysCall_nr_clone,%eax
  104. int $0x80
  105. popl %ebx
  106. test %eax,%eax
  107. jnz .Lclone_end
  108. { We're in the new thread }
  109. subl %ebp,%ebp { terminate the stack frame }
  110. call *%ebx
  111. { exit process }
  112. movl %eax,%ebx
  113. movl $1,%eax
  114. int $0x80
  115. .Lclone_end:
  116. movl %eax,__RESULT
  117. end;
  118. *)
  119. {$endif cpum68k}
  120. end;
  121. {$endif}
  122. end.
  123. {
  124. $Log$
  125. Revision 1.5 2004-05-01 16:35:13 peter
  126. * ptrint
  127. Revision 1.4 2003/12/30 12:24:01 marco
  128. * FPC_USE_LIBC
  129. Revision 1.3 2003/11/17 11:28:08 marco
  130. * Clone moved to linux, + few small unit unix changes
  131. Revision 1.2 2003/09/15 20:08:49 marco
  132. * small fixes. FreeBSD now cycles
  133. Revision 1.1 2003/09/15 14:12:17 marco
  134. * moved linux.pp contents to linuxold etc
  135. }