linux.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. interface
  16. uses
  17. ctypes;
  18. Type
  19. TSysinfo = packed record
  20. uptime : longint;
  21. loads : array[1..3] of longint;
  22. totalram,
  23. freeram,
  24. sharedram,
  25. bufferram,
  26. totalswap,
  27. freeswap : longint;
  28. procs : integer;
  29. s : string[18];
  30. end;
  31. PSysInfo = ^TSysInfo;
  32. Function Sysinfo(var Info:TSysinfo):Boolean; {$ifdef FPC_USE_LIBC} cdecl; external name 'sysinfo'; {$endif}
  33. Const
  34. CSIGNAL = $000000ff; // signal mask to be sent at exit
  35. CLONE_VM = $00000100; // set if VM shared between processes
  36. CLONE_FS = $00000200; // set if fs info shared between processes
  37. CLONE_FILES = $00000400; // set if open files shared between processes
  38. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  39. CLONE_PID = $00001000; // set if pid shared
  40. EPOLLIN = $01; { The associated file is available for read(2) operations. }
  41. EPOLLOUT = $02; { The associated file is available for write(2) operations. }
  42. EPOLLPRI = $04; { There is urgent data available for read(2) operations. }
  43. EPOLLERR = $08; { Error condition happened on the associated file descriptor. }
  44. EPOLLHUP = $10; { Hang up happened on the associated file descriptor. }
  45. EPOLLET = $80000000; { Sets the Edge Triggered behaviour for the associated file descriptor. }
  46. { Valid opcodes ( "op" parameter ) to issue to epoll_ctl }
  47. EPOLL_CTL_ADD = 1;
  48. EPOLL_CTL_MOD = 2;
  49. EPOLL_CTL_DEL = 3;
  50. type
  51. TCloneFunc=function(args:pointer):longint;cdecl;
  52. EPoll_Data = Record
  53. case integer of
  54. 0: (ptr: pointer);
  55. 1: (fd: cint);
  56. 2: (u32: cuint);
  57. 3: (u64: cuint64);
  58. end;
  59. TEPoll_Data = Epoll_Data;
  60. PEPoll_Data = ^Epoll_Data;
  61. EPoll_Event = Record
  62. Events: cuint32;
  63. Data : TEpoll_Data;
  64. end;
  65. TEPoll_Event = Epoll_Event;
  66. PEpoll_Event = ^Epoll_Event;
  67. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  68. { open an epoll file descriptor }
  69. function epoll_create(size: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_create'; {$endif}
  70. { control interface for an epoll descriptor }
  71. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_ctl'; {$endif}
  72. { wait for an I/O event on an epoll file descriptor }
  73. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_wait'; {$endif}
  74. implementation
  75. {$ifndef FPC_USE_LIBC}
  76. Uses Syscall;
  77. Function Sysinfo(var Info:TSysinfo):Boolean;
  78. {
  79. Get system info
  80. }
  81. Begin
  82. Sysinfo:=do_SysCall(SysCall_nr_Sysinfo,TSysParam(@info))=0;
  83. End;
  84. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  85. begin
  86. if (pointer(func)=nil) or (sp=nil) then
  87. exit(-1); // give an error result
  88. {$ifdef cpui386}
  89. {$ASMMODE ATT}
  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. {$endif cpui386}
  119. {$ifdef cpum68k}
  120. { No yet translated, my m68k assembler is too weak for such things PM }
  121. (*
  122. asm
  123. { Insert the argument onto the new stack. }
  124. movl sp,%ecx
  125. subl $8,%ecx
  126. movl args,%eax
  127. movl %eax,4(%ecx)
  128. { Save the function pointer as the zeroth argument.
  129. It will be popped off in the child in the ebx frobbing below. }
  130. movl func,%eax
  131. movl %eax,0(%ecx)
  132. { Do the system call }
  133. pushl %ebx
  134. movl flags,%ebx
  135. movl SysCall_nr_clone,%eax
  136. int $0x80
  137. popl %ebx
  138. test %eax,%eax
  139. jnz .Lclone_end
  140. { We're in the new thread }
  141. subl %ebp,%ebp { terminate the stack frame }
  142. call *%ebx
  143. { exit process }
  144. movl %eax,%ebx
  145. movl $1,%eax
  146. int $0x80
  147. .Lclone_end:
  148. movl %eax,__RESULT
  149. end;
  150. *)
  151. {$endif cpum68k}
  152. end;
  153. function epoll_create(size: cint): cint;
  154. begin
  155. epoll_create := do_syscall(syscall_nr_epoll_create);
  156. end;
  157. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint;
  158. begin
  159. epoll_ctl := do_syscall(syscall_nr_epoll_ctl, tsysparam(epfd),
  160. tsysparam(op), tsysparam(fd), tsysparam(event));
  161. end;
  162. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint;
  163. begin
  164. epoll_wait := do_syscall(syscall_nr_epoll_wait, tsysparam(epfd),
  165. tsysparam(events), tsysparam(maxevents), tsysparam(timeout));
  166. end;
  167. {$endif}
  168. end.