linux.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. pepoll_event = ^epoll_event;
  60. epoll_event = record
  61. events: cuint32;
  62. data: epoll_data;
  63. end;
  64. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  65. { open an epoll file descriptor }
  66. function epoll_create(size: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_create'; {$endif}
  67. { control interface for an epoll descriptor }
  68. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_ctl'; {$endif}
  69. { wait for an I/O event on an epoll file descriptor }
  70. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_wait'; {$endif}
  71. implementation
  72. {$ifndef FPC_USE_LIBC}
  73. Uses Syscall;
  74. Function Sysinfo(var Info:TSysinfo):Boolean;
  75. {
  76. Get system info
  77. }
  78. Begin
  79. Sysinfo:=do_SysCall(SysCall_nr_Sysinfo,TSysParam(@info))=0;
  80. End;
  81. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  82. begin
  83. if (pointer(func)=nil) or (sp=nil) then
  84. exit(-1); // give an error result
  85. {$ifdef cpui386}
  86. {$ASMMODE ATT}
  87. asm
  88. { Insert the argument onto the new stack. }
  89. movl sp,%ecx
  90. subl $8,%ecx
  91. movl args,%eax
  92. movl %eax,4(%ecx)
  93. { Save the function pointer as the zeroth argument.
  94. It will be popped off in the child in the ebx frobbing below. }
  95. movl func,%eax
  96. movl %eax,0(%ecx)
  97. { Do the system call }
  98. pushl %ebx
  99. movl flags,%ebx
  100. movl SysCall_nr_clone,%eax
  101. int $0x80
  102. popl %ebx
  103. test %eax,%eax
  104. jnz .Lclone_end
  105. { We're in the new thread }
  106. subl %ebp,%ebp { terminate the stack frame }
  107. call *%ebx
  108. { exit process }
  109. movl %eax,%ebx
  110. movl $1,%eax
  111. int $0x80
  112. .Lclone_end:
  113. movl %eax,__RESULT
  114. end;
  115. {$endif cpui386}
  116. {$ifdef cpum68k}
  117. { No yet translated, my m68k assembler is too weak for such things PM }
  118. (*
  119. asm
  120. { Insert the argument onto the new stack. }
  121. movl sp,%ecx
  122. subl $8,%ecx
  123. movl args,%eax
  124. movl %eax,4(%ecx)
  125. { Save the function pointer as the zeroth argument.
  126. It will be popped off in the child in the ebx frobbing below. }
  127. movl func,%eax
  128. movl %eax,0(%ecx)
  129. { Do the system call }
  130. pushl %ebx
  131. movl flags,%ebx
  132. movl SysCall_nr_clone,%eax
  133. int $0x80
  134. popl %ebx
  135. test %eax,%eax
  136. jnz .Lclone_end
  137. { We're in the new thread }
  138. subl %ebp,%ebp { terminate the stack frame }
  139. call *%ebx
  140. { exit process }
  141. movl %eax,%ebx
  142. movl $1,%eax
  143. int $0x80
  144. .Lclone_end:
  145. movl %eax,__RESULT
  146. end;
  147. *)
  148. {$endif cpum68k}
  149. end;
  150. function epoll_create(size: cint): cint;
  151. begin
  152. epoll_create := do_syscall(syscall_nr_epoll_create);
  153. end;
  154. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint;
  155. begin
  156. epoll_ctl := do_syscall(syscall_nr_epoll_ctl, tsysparam(epfd),
  157. tsysparam(op), tsysparam(fd), tsysparam(event));
  158. end;
  159. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint;
  160. begin
  161. epoll_wait := do_syscall(syscall_nr_epoll_wait, tsysparam(epfd),
  162. tsysparam(events), tsysparam(maxevents), tsysparam(timeout));
  163. end;
  164. {$endif}
  165. end.