linux.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. BSD parts (c) 2000 by Marco van de Voort
  5. members of the Free Pascal development team.
  6. New linux unit. Linux only calls only. Will be renamed to linux.pp
  7. when 1.0.x support is killed off.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY;without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit Linux;
  15. {$packrecords c}
  16. interface
  17. uses
  18. ctypes;
  19. Type
  20. TSysinfo = packed record
  21. uptime : longint;
  22. loads : array[1..3] of longint;
  23. totalram,
  24. freeram,
  25. sharedram,
  26. bufferram,
  27. totalswap,
  28. freeswap : longint;
  29. procs : integer;
  30. s : string[18];
  31. end;
  32. PSysInfo = ^TSysInfo;
  33. Function Sysinfo(var Info:TSysinfo):Boolean; {$ifdef FPC_USE_LIBC} cdecl; external name 'sysinfo'; {$endif}
  34. Const
  35. CSIGNAL = $000000ff; // signal mask to be sent at exit
  36. CLONE_VM = $00000100; // set if VM shared between processes
  37. CLONE_FS = $00000200; // set if fs info shared between processes
  38. CLONE_FILES = $00000400; // set if open files shared between processes
  39. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  40. CLONE_PID = $00001000; // set if pid shared
  41. EPOLLIN = $01; { The associated file is available for read(2) operations. }
  42. EPOLLOUT = $02; { The associated file is available for write(2) operations. }
  43. EPOLLPRI = $04; { There is urgent data available for read(2) operations. }
  44. EPOLLERR = $08; { Error condition happened on the associated file descriptor. }
  45. EPOLLHUP = $10; { Hang up happened on the associated file descriptor. }
  46. EPOLLET = $80000000; { Sets the Edge Triggered behaviour for the associated file descriptor. }
  47. { Valid opcodes ( "op" parameter ) to issue to epoll_ctl }
  48. EPOLL_CTL_ADD = 1;
  49. EPOLL_CTL_MOD = 2;
  50. EPOLL_CTL_DEL = 3;
  51. type
  52. TCloneFunc=function(args:pointer):longint;cdecl;
  53. EPoll_Data = record
  54. case integer of
  55. 0: (ptr: pointer);
  56. 1: (fd: cint);
  57. 2: (u32: cuint);
  58. 3: (u64: cuint64);
  59. end;
  60. TEPoll_Data = Epoll_Data;
  61. PEPoll_Data = ^Epoll_Data;
  62. EPoll_Event = record
  63. Events: cuint32;
  64. Data : TEpoll_Data;
  65. end;
  66. TEPoll_Event = Epoll_Event;
  67. PEpoll_Event = ^Epoll_Event;
  68. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  69. { open an epoll file descriptor }
  70. function epoll_create(size: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_create'; {$endif}
  71. { control interface for an epoll descriptor }
  72. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_ctl'; {$endif}
  73. { wait for an I/O event on an epoll file descriptor }
  74. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_wait'; {$endif}
  75. implementation
  76. {$ifndef FPC_USE_LIBC}
  77. Uses Syscall;
  78. Function Sysinfo(var Info:TSysinfo):Boolean;
  79. {
  80. Get system info
  81. }
  82. Begin
  83. Sysinfo:=do_SysCall(SysCall_nr_Sysinfo,TSysParam(@info))=0;
  84. End;
  85. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  86. begin
  87. if (pointer(func)=nil) or (sp=nil) then
  88. exit(-1); // give an error result
  89. {$ifdef cpui386}
  90. {$ASMMODE ATT}
  91. asm
  92. { Insert the argument onto the new stack. }
  93. movl sp,%ecx
  94. subl $8,%ecx
  95. movl args,%eax
  96. movl %eax,4(%ecx)
  97. { Save the function pointer as the zeroth argument.
  98. It will be popped off in the child in the ebx frobbing below. }
  99. movl func,%eax
  100. movl %eax,0(%ecx)
  101. { Do the system call }
  102. pushl %ebx
  103. movl flags,%ebx
  104. movl SysCall_nr_clone,%eax
  105. int $0x80
  106. popl %ebx
  107. test %eax,%eax
  108. jnz .Lclone_end
  109. { We're in the new thread }
  110. subl %ebp,%ebp { terminate the stack frame }
  111. call *%ebx
  112. { exit process }
  113. movl %eax,%ebx
  114. movl $1,%eax
  115. int $0x80
  116. .Lclone_end:
  117. movl %eax,__RESULT
  118. end;
  119. {$endif cpui386}
  120. {$ifdef cpum68k}
  121. { No yet translated, my m68k assembler is too weak for such things PM }
  122. (*
  123. asm
  124. { Insert the argument onto the new stack. }
  125. movl sp,%ecx
  126. subl $8,%ecx
  127. movl args,%eax
  128. movl %eax,4(%ecx)
  129. { Save the function pointer as the zeroth argument.
  130. It will be popped off in the child in the ebx frobbing below. }
  131. movl func,%eax
  132. movl %eax,0(%ecx)
  133. { Do the system call }
  134. pushl %ebx
  135. movl flags,%ebx
  136. movl SysCall_nr_clone,%eax
  137. int $0x80
  138. popl %ebx
  139. test %eax,%eax
  140. jnz .Lclone_end
  141. { We're in the new thread }
  142. subl %ebp,%ebp { terminate the stack frame }
  143. call *%ebx
  144. { exit process }
  145. movl %eax,%ebx
  146. movl $1,%eax
  147. int $0x80
  148. .Lclone_end:
  149. movl %eax,__RESULT
  150. end;
  151. *)
  152. {$endif cpum68k}
  153. end;
  154. function epoll_create(size: cint): cint;
  155. begin
  156. epoll_create := do_syscall(syscall_nr_epoll_create);
  157. end;
  158. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint;
  159. begin
  160. epoll_ctl := do_syscall(syscall_nr_epoll_ctl, tsysparam(epfd),
  161. tsysparam(op), tsysparam(fd), tsysparam(event));
  162. end;
  163. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint;
  164. begin
  165. epoll_wait := do_syscall(syscall_nr_epoll_wait, tsysparam(epfd),
  166. tsysparam(events), tsysparam(maxevents), tsysparam(timeout));
  167. end;
  168. {$endif}
  169. end.