linux.pp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. {$ifdef FPC_USE_LIBC}
  17. {$linklib rt} // for clock* functions
  18. {$endif}
  19. interface
  20. uses
  21. BaseUnix, unixtype;
  22. type
  23. TSysInfo = record
  24. uptime: clong; //* Seconds since boot */
  25. loads: array[0..2] of culong; //* 1, 5, and 15 minute load averages */
  26. totalram: culong; //* Total usable main memory size */
  27. freeram: culong; //* Available memory size */
  28. sharedram: culong; //* Amount of shared memory */
  29. bufferram: culong; //* Memory used by buffers */
  30. totalswap: culong; //* Total swap space size */
  31. freeswap: culong; //* swap space still available */
  32. procs: cushort; //* Number of current processes */
  33. pad: cushort; //* explicit padding for m68k */
  34. totalhigh: culong; //* Total high memory size */
  35. freehigh: culong; //* Available high memory size */
  36. mem_unit: cuint; //* Memory unit size in bytes */
  37. {$ifndef cpu64}
  38. { the upper bound of the array below is negative for 64 bit cpus }
  39. _f: array[0..19-2*sizeof(clong)-sizeof(cint)] of cChar; //* Padding: libc5 uses this.. */
  40. {$endif cpu64}
  41. end;
  42. PSysInfo = ^TSysInfo;
  43. function Sysinfo(Info: PSysinfo): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'sysinfo'; {$endif}
  44. const
  45. CSIGNAL = $000000ff; // signal mask to be sent at exit
  46. CLONE_VM = $00000100; // set if VM shared between processes
  47. CLONE_FS = $00000200; // set if fs info shared between processes
  48. CLONE_FILES = $00000400; // set if open files shared between processes
  49. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  50. CLONE_PID = $00001000; // set if pid shared
  51. CLONE_PTRACE = $00002000; // Set if tracing continues on the child.
  52. CLONE_VFORK = $00004000; // Set if the parent wants the child to wake it up on mm_release.
  53. CLONE_PARENT = $00008000; // Set if we want to have the same parent as the cloner.
  54. CLONE_THREAD = $00010000; // Set to add to same thread group.
  55. CLONE_NEWNS = $00020000; // Set to create new namespace.
  56. CLONE_SYSVSEM = $00040000; // Set to shared SVID SEM_UNDO semantics.
  57. CLONE_SETTLS = $00080000; // Set TLS info.
  58. CLONE_PARENT_SETTID = $00100000; // Store TID in userlevel buffer before MM copy.
  59. CLONE_CHILD_CLEARTID = $00200000; // Register exit futex and memory location to clear.
  60. CLONE_DETACHED = $00400000; // Create clone detached.
  61. CLONE_UNTRACED = $00800000; // Set if the tracing process can't force CLONE_PTRACE on this clone.
  62. CLONE_CHILD_SETTID = $01000000; // Store TID in userlevel buffer in the child.
  63. CLONE_STOPPED = $02000000; // Start in stopped state.
  64. FUTEX_WAIT = 0;
  65. FUTEX_WAKE = 1;
  66. FUTEX_FD = 2;
  67. FUTEX_REQUEUE = 3;
  68. FUTEX_CMP_REQUEUE = 4;
  69. FUTEX_WAKE_OP = 5;
  70. FUTEX_LOCK_PI = 6;
  71. FUTEX_UNLOCK_PI = 7;
  72. FUTEX_TRYLOCK_PI = 8;
  73. FUTEX_OP_SET = 0; // *(int *)UADDR2 = OPARG;
  74. FUTEX_OP_ADD = 1; // *(int *)UADDR2 += OPARG;
  75. FUTEX_OP_OR = 2; // *(int *)UADDR2 |= OPARG;
  76. FUTEX_OP_ANDN = 3; // *(int *)UADDR2 &= ~OPARG;
  77. FUTEX_OP_XOR = 4; // *(int *)UADDR2 ^= OPARG;
  78. FUTEX_OP_OPARG_SHIFT = 8; // Use (1 << OPARG) instead of OPARG.
  79. FUTEX_OP_CMP_EQ = 0; // if (oldval == CMPARG) wake
  80. FUTEX_OP_CMP_NE = 1; // if (oldval != CMPARG) wake
  81. FUTEX_OP_CMP_LT = 2; // if (oldval < CMPARG) wake
  82. FUTEX_OP_CMP_LE = 3; // if (oldval <= CMPARG) wake
  83. FUTEX_OP_CMP_GT = 4; // if (oldval > CMPARG) wake
  84. FUTEX_OP_CMP_GE = 5; // if (oldval >= CMPARG) wake
  85. { FUTEX_WAKE_OP will perform atomically
  86. int oldval = *(int *)UADDR2;
  87. *(int *)UADDR2 = oldval OP OPARG;
  88. if (oldval CMP CMPARG)
  89. wake UADDR2; }
  90. {$ifndef FPC_USE_LIBC}
  91. function futex(uaddr:Pcint;op,val:cint;timeout:Ptimespec;addr2:Pcint;val3:cint):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  92. function futex(var uaddr;op,val:cint;timeout:Ptimespec;var addr2;val3:cint):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  93. function futex(var uaddr;op,val:cint;var timeout:Ttimespec;var addr2;val3:cint):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  94. // general aliases:
  95. function futex(uaddr:Pcint;op,val:cint;timeout:Ptimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  96. function futex(var uaddr;op,val:cint;timeout:Ptimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  97. function futex(var uaddr;op,val:cint;var timeout:Ttimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  98. {$else}
  99. // futex is currently not exposed by glibc
  100. //function futex(uaddr:Pcint;op,val:cint;timeout:Ptimespec;addr2:Pcint;val3:cint):cint; cdecl; external name 'futex';
  101. //function futex(var uaddr;op,val:cint;timeout:Ptimespec;var addr2;val3:cint):cint; cdecl; external name 'futex';
  102. //function futex(var uaddr;op,val:cint;var timeout:Ttimespec;var addr2;val3:cint):cint; cdecl; external name 'futex';
  103. {$endif}
  104. {$ifndef FPC_USE_LIBC}
  105. function futex_op(op, oparg, cmp, cmparg: cint): cint; {$ifdef SYSTEMINLINE}inline;{$endif}
  106. {$endif}
  107. const
  108. POLLMSG = $0400;
  109. POLLREMOVE = $1000;
  110. POLLRDHUP = $2000;
  111. EPOLLIN = $01; { The associated file is available for read(2) operations. }
  112. EPOLLPRI = $02; { There is urgent data available for read(2) operations. }
  113. EPOLLOUT = $04; { The associated file is available for write(2) operations. }
  114. EPOLLERR = $08; { Error condition happened on the associated file descriptor. }
  115. EPOLLHUP = $10; { Hang up happened on the associated file descriptor. }
  116. EPOLLONESHOT = $40000000; { Sets the One-Shot behaviour for the associated file descriptor. }
  117. EPOLLET = $80000000; { Sets the Edge Triggered behaviour for the associated file descriptor. }
  118. { Valid opcodes ( "op" parameter ) to issue to epoll_ctl }
  119. EPOLL_CTL_ADD = 1;
  120. EPOLL_CTL_DEL = 2;
  121. EPOLL_CTL_MOD = 3;
  122. {Some console iotcl's.}
  123. GIO_FONT = $4B60; {gets font in expanded form}
  124. PIO_FONT = $4B61; {use font in expanded form}
  125. GIO_FONTX = $4B6B; {get font using struct consolefontdesc}
  126. PIO_FONTX = $4B6C; {set font using struct consolefontdesc}
  127. PIO_FONTRESET = $4B6D; {reset to default font}
  128. GIO_CMAP = $4B70; {gets colour palette on VGA+}
  129. PIO_CMAP = $4B71; {sets colour palette on VGA+}
  130. KIOCSOUND = $4B2F; {start sound generation (0 for off)}
  131. KDMKTONE = $4B30; {generate tone}
  132. KDGETLED = $4B31; {return current led state}
  133. KDSETLED = $4B32; {set led state [lights, not flags]}
  134. KDGKBTYPE = $4B33; {get keyboard type}
  135. KDADDIO = $4B34; {add i/o port as valid}
  136. KDDELIO = $4B35; {del i/o port as valid}
  137. KDENABIO = $4B36; {enable i/o to video board}
  138. KDDISABIO = $4B37; {disable i/o to video board}
  139. KDSETMODE = $4B3A; {set text/graphics mode}
  140. KDGETMODE = $4B3B; {get current mode}
  141. KDMAPDISP = $4B3C; {map display into address space}
  142. KDUNMAPDISP = $4B3D; {unmap display from address space}
  143. GIO_SCRNMAP = $4B40; {get screen mapping from kernel}
  144. PIO_SCRNMAP = $4B41; {put screen mapping table in kernel}
  145. GIO_UNISCRNMAP = $4B69; {get full Unicode screen mapping}
  146. PIO_UNISCRNMAP = $4B6A; {set full Unicode screen mapping}
  147. GIO_UNIMAP = $4B66; {get unicode-to-font mapping from kernel}
  148. PIO_UNIMAP = $4B67; {put unicode-to-font mapping in kernel}
  149. PIO_UNIMAPCLR = $4B68; {clear table, possibly advise hash algorithm}
  150. KDGKBDIACR = $4B4A; {read kernel accent table}
  151. KDSKBDIACR = $4B4B; {write kernel accent table}
  152. KDGETKEYCODE = $4B4C; {read kernel keycode table entry}
  153. KDSETKEYCODE = $4B4D; {write kernel keycode table entry}
  154. KDSIGACCEPT = $4B4E; {accept kbd generated signals}
  155. KDFONTOP = $4B72; {font operations}
  156. {Keyboard types (for KDGKBTYPE)}
  157. KB_84 = 1;
  158. KB_101 = 2; {Normal PC keyboard.}
  159. KB_OTHER = 3;
  160. {Keyboard LED constants.}
  161. LED_SCR = 1; {scroll lock led}
  162. LED_NUM = 2; {num lock led}
  163. LED_CAP = 4; {caps lock led}
  164. {Tty modes. (for KDSETMODE)}
  165. KD_TEXT = 0;
  166. KD_GRAPHICS = 1;
  167. KD_TEXT0 = 2; {obsolete}
  168. KD_TEXT1 = 3; {obsolete}
  169. {$if defined(cpumips) or defined(cpumipsel)}
  170. MAP_GROWSDOWN = $1000; { stack-like segment }
  171. MAP_DENYWRITE = $2000; { ETXTBSY }
  172. MAP_EXECUTABLE = $4000; { mark it as an executable }
  173. MAP_LOCKED = $8000; { pages are locked }
  174. MAP_NORESERVE = $4000; { don't check for reservations; not defined for linux/mips? }
  175. {$else cpumips}
  176. MAP_GROWSDOWN = $100; { stack-like segment }
  177. MAP_DENYWRITE = $800; { ETXTBSY }
  178. MAP_EXECUTABLE = $1000; { mark it as an executable }
  179. MAP_LOCKED = $2000; { pages are locked }
  180. MAP_NORESERVE = $4000; { don't check for reservations }
  181. {$endif cpumips}
  182. type
  183. TCloneFunc = function(args:pointer):longint;cdecl;
  184. {$ifdef cpui386}
  185. {$define clone_implemented}
  186. {$endif}
  187. {$ifdef clone_implemented}
  188. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  189. {$endif}
  190. {$ifndef FPC_USE_LIBC}
  191. {$if defined(cpui386) or defined(cpux86_64)}
  192. const
  193. MODIFY_LDT_CONTENTS_DATA = 0;
  194. MODIFY_LDT_CONTENTS_STACK = 1;
  195. MODIFY_LDT_CONTENTS_CODE = 2;
  196. { Flags for user_desc.flags }
  197. UD_SEG_32BIT = $01;
  198. UD_CONTENTS_DATA = MODIFY_LDT_CONTENTS_DATA shl 1;
  199. UD_CONTENTS_STACK = MODIFY_LDT_CONTENTS_STACK shl 1;
  200. UD_CONTENTS_CODE = MODIFY_LDT_CONTENTS_CODE shl 1;
  201. UD_READ_EXEC_ONLY = $08;
  202. UD_LIMIT_IN_PAGES = $10;
  203. UD_SEG_NOT_PRESENT = $20;
  204. UD_USEABLE = $40;
  205. UD_LM = $80;
  206. type
  207. user_desc = record
  208. entry_number : cuint;
  209. base_addr : cuint;
  210. limit : cuint;
  211. flags : cuint;
  212. end;
  213. TUser_Desc = user_desc;
  214. PUser_Desc = ^user_desc;
  215. function modify_ldt(func:cint;p:pointer;bytecount:culong):cint;
  216. {$endif cpui386 or cpux86_64}
  217. {$endif}
  218. procedure sched_yield; {$ifdef FPC_USE_LIBC} cdecl; external name 'sched_yield'; {$endif}
  219. type
  220. EPoll_Data = record
  221. case integer of
  222. 0: (ptr: pointer);
  223. 1: (fd: cint);
  224. 2: (u32: cuint);
  225. 3: (u64: cuint64);
  226. end;
  227. TEPoll_Data = Epoll_Data;
  228. PEPoll_Data = ^Epoll_Data;
  229. EPoll_Event = {$ifdef cpu64} packed {$endif} record
  230. Events: cuint32;
  231. Data : TEpoll_Data;
  232. end;
  233. TEPoll_Event = Epoll_Event;
  234. PEpoll_Event = ^Epoll_Event;
  235. { open an epoll file descriptor }
  236. function epoll_create(size: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_create'; {$endif}
  237. { control interface for an epoll descriptor }
  238. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_ctl'; {$endif}
  239. { wait for an I/O event on an epoll file descriptor }
  240. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_wait'; {$endif}
  241. type Puser_cap_header=^user_cap_header;
  242. user_cap_header=record
  243. version: cuint32;
  244. pid:cint;
  245. end;
  246. Puser_cap_data=^user_cap_data;
  247. user_cap_data=record
  248. effective,permitted,inheritable:cuint32;
  249. end;
  250. {Get a capability.}
  251. function capget(header:Puser_cap_header;data:Puser_cap_data):cint;{$ifdef FPC_USE_LIBC} cdecl; external name 'capget'; {$endif}
  252. {Set a capability.}
  253. function capset(header:Puser_cap_header;data:Puser_cap_data):cint;{$ifdef FPC_USE_LIBC} cdecl; external name 'capset'; {$endif}
  254. const CAP_CHOWN = 0;
  255. CAP_DAC_OVERRIDE = 1;
  256. CAP_DAC_READ_SEARCH = 2;
  257. CAP_FOWNER = 3;
  258. CAP_FSETID = 4;
  259. CAP_FS_MASK = $1f;
  260. CAP_KILL = 5;
  261. CAP_SETGID = 6;
  262. CAP_SETUID = 7;
  263. CAP_SETPCAP = 8;
  264. CAP_LINUX_IMMUTABLE = 9;
  265. CAP_NET_BIND_SERVICE = 10;
  266. CAP_NET_BROADCAST = 11;
  267. CAP_NET_ADMIN = 12;
  268. CAP_NET_RAW = 13;
  269. CAP_IPC_LOCK = 14;
  270. CAP_IPC_OWNER = 15;
  271. CAP_SYS_MODULE = 16;
  272. CAP_SYS_RAWIO = 17;
  273. CAP_SYS_CHROOT = 18;
  274. CAP_SYS_PTRACE = 19;
  275. CAP_SYS_PACCT = 20;
  276. CAP_SYS_ADMIN = 21;
  277. CAP_SYS_BOOT = 22;
  278. CAP_SYS_NICE = 23;
  279. CAP_SYS_RESOURCE = 24;
  280. CAP_SYS_TIME = 25;
  281. CAP_SYS_TTY_CONFIG = 26;
  282. CAP_MKNOD = 27;
  283. CAP_LEASE = 28;
  284. CAP_AUDIT_WRITE = 29;
  285. CAP_AUDIT_CONTROL = 30;
  286. LINUX_CAPABILITY_VERSION = $19980330;
  287. //***********************************************SPLICE from kernel 2.6.17+****************************************
  288. const
  289. {* Flags for SPLICE and VMSPLICE. *}
  290. SPLICE_F_MOVE = 1; { Move pages instead of copying. }
  291. SPLICE_F_NONBLOCK = 2; {* Don't block on the pipe splicing
  292. (but we may still block on the fd
  293. we splice from/to). *}
  294. SPLICE_F_MORE = 4; {* Expect more data. *}
  295. SPLICE_F_GIFT = 8; {* Pages passed in are a gift. *}
  296. {$ifdef cpu86}
  297. {* Splice address range into a pipe. *}
  298. function vmsplice (fdout: cInt; iov: PIOVec; count: size_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'vmsplice'; {$ENDIF}
  299. {* Splice two files together. *}
  300. function splice (fdin: cInt; offin: off64_t; fdout: cInt;
  301. offout: off64_t; len: size_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'splice'; {$ENDIF}
  302. function tee(fd_in: cInt; fd_out: cInt; len: size_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'tee'; {$ENDIF}
  303. {$endif} // x86
  304. const
  305. { flags for sync_file_range }
  306. SYNC_FILE_RANGE_WAIT_BEFORE = 1;
  307. SYNC_FILE_RANGE_WRITE = 2;
  308. SYNC_FILE_RANGE_WAIT_AFTER = 4;
  309. function sync_file_range(fd: cInt; offset, nbytes: off64_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'sync_file_range'; {$ENDIF}
  310. function fdatasync (fd: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'sync_file_range'; {$ENDIF}
  311. {$PACKRECORDS 1}
  312. { Flags for the parameter of inotify_init1. }
  313. Const
  314. IN_CLOEXEC = &02000000;
  315. IN_NONBLOCK = &00004000;
  316. Type
  317. inotify_event = record
  318. wd : cint;
  319. mask : cuint32;
  320. cookie : cuint32;
  321. len : cuint32;
  322. name : char;
  323. end;
  324. Pinotify_event = ^inotify_event;
  325. { Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH. }
  326. const
  327. IN_ACCESS = $00000001; { File was accessed. }
  328. IN_MODIFY = $00000002; { File was modified. }
  329. IN_ATTRIB = $00000004; { Metadata changed. }
  330. IN_CLOSE_WRITE = $00000008; { Writtable file was closed. }
  331. IN_CLOSE_NOWRITE = $00000010; { Unwrittable file closed. }
  332. IN_OPEN = $00000020; { File was opened. }
  333. IN_MOVED_FROM = $00000040; { File was moved from X. }
  334. IN_MOVED_TO = $00000080; { File was moved to Y. }
  335. IN_CLOSE = IN_CLOSE_WRITE or IN_CLOSE_NOWRITE; { Close. }
  336. IN_MOVE = IN_MOVED_FROM or IN_MOVED_TO; { Moves. }
  337. IN_CREATE = $00000100; { Subfile was created. }
  338. IN_DELETE = $00000200; { Subfile was deleted. }
  339. IN_DELETE_SELF = $00000400; { Self was deleted. }
  340. IN_MOVE_SELF = $00000800; { Self was moved. }
  341. { Events sent by the kernel. }
  342. IN_UNMOUNT = $00002000; { Backing fs was unmounted. }
  343. IN_Q_OVERFLOW = $00004000; { Event queued overflowed. }
  344. IN_IGNORED = $00008000; { File was ignored. }
  345. { Special flags. }
  346. IN_ONLYDIR = $01000000; { Only watch the path if it is a directory. }
  347. IN_DONT_FOLLOW = $02000000; { Do not follow a sym link. }
  348. IN_MASK_ADD = $20000000; { Add to the mask of an already existing watch. }
  349. IN_ISDIR = $40000000; { Event occurred against dir. }
  350. IN_ONESHOT = $80000000; { Only send event once. }
  351. { All events which a program can wait on. }
  352. IN_ALL_EVENTS = IN_ACCESS or IN_MODIFY or IN_ATTRIB or IN_CLOSE
  353. or IN_OPEN or IN_MOVE or IN_CREATE or IN_DELETE
  354. or IN_DELETE_SELF or IN_MOVE_SELF;
  355. // these have _THROW in the header.
  356. { Create and initialize inotify instance. }
  357. function inotify_init: cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'inotify_init'; {$ENDIF}
  358. { Create and initialize inotify instance. }
  359. function inotify_init1(flags:cint):cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'inotify_init1'; {$ENDIF}
  360. { Add watch of object NAME to inotify instance FD.
  361. Notify about events specified by MASK. }
  362. function inotify_add_watch(fd:cint; name:Pchar; mask:cuint32):cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'inotify_add_watch'; {$ENDIF}
  363. { Remove the watch specified by WD from the inotify instance FD. }
  364. function inotify_rm_watch(fd:cint; wd: cint):cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'inotify_rm_watch'; {$ENDIF}
  365. { clock_gettime, clock_settime, clock_getres }
  366. Const
  367. // Taken from linux/time.h
  368. // Posix timers
  369. CLOCK_REALTIME = 0;
  370. CLOCK_MONOTONIC = 1;
  371. CLOCK_PROCESS_CPUTIME_ID = 2;
  372. CLOCK_THREAD_CPUTIME_ID = 3;
  373. CLOCK_MONOTONIC_RAW = 4;
  374. CLOCK_REALTIME_COARSE = 5;
  375. CLOCK_MONOTONIC_COARSE = 6;
  376. // Linux specific
  377. CLOCK_SGI_CYCLE = 10;
  378. MAX_CLOCKS = 16;
  379. CLOCKS_MASK = CLOCK_REALTIME or CLOCK_MONOTONIC;
  380. CLOCKS_MONO = CLOCK_MONOTONIC;
  381. Type
  382. clockid_t = cint;
  383. // FPC_USE_LIBC unchecked, just to get it compiling again.
  384. function clock_getres(clk_id : clockid_t; res : ptimespec) : cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clock_getres'; {$ENDIF}
  385. function clock_gettime(clk_id : clockid_t; tp: ptimespec) : cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clock_gettime'; {$ENDIF}
  386. function clock_settime(clk_id : clockid_t; tp : ptimespec) : cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clock_settime'; {$ENDIF}
  387. function setregid(rgid,egid : uid_t): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'setregid'; {$ENDIF}
  388. function setreuid(ruid,euid : uid_t): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'setreuid'; {$ENDIF}
  389. implementation
  390. {$if not defined(FPC_USE_LIBC) or defined(cpui386) or defined(cpux86_64)}
  391. { needed for modify_ldt on x86 }
  392. Uses Syscall;
  393. {$endif not defined(FPC_USE_LIBC) or defined(cpui386) or defined(cpux86_64)}
  394. {$ifndef FPC_USE_LIBC}
  395. function Sysinfo(Info: PSysinfo): cInt;
  396. begin
  397. Sysinfo := do_SysCall(SysCall_nr_Sysinfo, TSysParam(info));
  398. end;
  399. {$ifdef clone_implemented}
  400. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  401. begin
  402. if (pointer(func)=nil) or (sp=nil) then
  403. exit(-1); // give an error result
  404. {$ifdef cpui386}
  405. {$ASMMODE ATT}
  406. asm
  407. { Insert the argument onto the new stack. }
  408. movl sp,%ecx
  409. subl $8,%ecx
  410. movl args,%eax
  411. movl %eax,4(%ecx)
  412. { Save the function pointer as the zeroth argument.
  413. It will be popped off in the child in the ebx frobbing below. }
  414. movl func,%eax
  415. movl %eax,0(%ecx)
  416. { Do the system call }
  417. pushl %ebx
  418. movl flags,%ebx
  419. movl syscall_nr_clone,%eax
  420. int $0x80
  421. popl %ebx
  422. test %eax,%eax
  423. jnz .Lclone_end
  424. { We're in the new thread }
  425. subl %ebp,%ebp { terminate the stack frame }
  426. call *%ebx
  427. { exit process }
  428. movl %eax,%ebx
  429. movl syscall_nr_exit,%eax
  430. int $0x80
  431. .Lclone_end:
  432. movl %eax,__RESULT
  433. end;
  434. {$endif cpui386}
  435. end;
  436. {$endif}
  437. procedure sched_yield;
  438. begin
  439. do_syscall(syscall_nr_sched_yield);
  440. end;
  441. function epoll_create(size: cint): cint;
  442. begin
  443. epoll_create := do_syscall(syscall_nr_epoll_create,tsysparam(size));
  444. end;
  445. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint;
  446. begin
  447. epoll_ctl := do_syscall(syscall_nr_epoll_ctl, tsysparam(epfd),
  448. tsysparam(op), tsysparam(fd), tsysparam(event));
  449. end;
  450. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint;
  451. begin
  452. epoll_wait := do_syscall(syscall_nr_epoll_wait, tsysparam(epfd),
  453. tsysparam(events), tsysparam(maxevents), tsysparam(timeout));
  454. end;
  455. function capget(header:Puser_cap_header;data:Puser_cap_data):cint;
  456. begin
  457. capget:=do_syscall(syscall_nr_capget,Tsysparam(header),Tsysparam(data));
  458. end;
  459. function capset(header:Puser_cap_header;data:Puser_cap_data):cint;
  460. begin
  461. capset:=do_syscall(syscall_nr_capset,Tsysparam(header),Tsysparam(data));
  462. end;
  463. // TODO: update also on non x86!
  464. {$ifdef cpu86} // didn't update syscall_nr on others yet
  465. function vmsplice (fdout: cInt; iov: PIOVec; count: size_t; flags: cuInt): cInt;
  466. begin
  467. vmsplice := do_syscall(syscall_nr_vmsplice, TSysParam(fdout), TSysParam(iov),
  468. TSysParam(count), TSysParam(flags));
  469. end;
  470. function splice (fdin: cInt; offin: off64_t; fdout: cInt; offout: off64_t; len: size_t; flags: cuInt): cInt;
  471. begin
  472. splice := do_syscall(syscall_nr_splice, TSysParam(fdin), TSysParam(@offin),
  473. TSysParam(fdout), TSysParam(@offout), TSysParam(len), TSysParam(flags));
  474. end;
  475. function tee(fd_in: cInt; fd_out: cInt; len: size_t; flags: cuInt): cInt;
  476. begin
  477. tee := do_syscall(syscall_nr_tee, TSysParam(fd_in), TSysParam(fd_out),
  478. TSysParam(len), TSysParam(flags));
  479. end;
  480. {$endif} // x86
  481. function sync_file_range(fd: cInt; offset: off64_t; nbytes: off64_t; flags: cuInt): cInt;
  482. begin
  483. {$if defined(cpupowerpc) or defined(cpuarm)}
  484. sync_file_range := do_syscall(syscall_nr_sync_file_range2, TSysParam(fd), TSysParam(flags),
  485. TSysParam(hi(offset)), TSysParam(lo(offset)), TSysParam(hi(nbytes)), TSysParam(lo(nbytes)));
  486. {$else}
  487. {$if defined(cpupowerpc64)}
  488. sync_file_range := do_syscall(syscall_nr_sync_file_range2, TSysParam(fd), TSysParam(flags),
  489. TSysParam(offset), TSysParam(nbytes));
  490. {$else}
  491. {$ifdef cpu64}
  492. sync_file_range := do_syscall(syscall_nr_sync_file_range, TSysParam(fd), TSysParam(offset),
  493. TSysParam(nbytes), TSysParam(flags));
  494. {$else}
  495. sync_file_range := do_syscall(syscall_nr_sync_file_range, TSysParam(fd), TSysParam(lo(offset)),
  496. TSysParam(hi(offset)), TSysParam(lo(nbytes)), TSysParam(hi(nbytes)), TSysParam(flags));
  497. {$endif}
  498. {$endif}
  499. {$endif}
  500. end;
  501. function fdatasync (fd: cint): cint;
  502. begin
  503. fdatasync:=do_SysCall(syscall_nr_fdatasync, fd);
  504. end;
  505. function futex(uaddr:Pcint;op,val:cint;timeout:Ptimespec;addr2:Pcint;val3:cint):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  506. begin
  507. futex:=do_syscall(syscall_nr_futex,Tsysparam(uaddr),Tsysparam(op),Tsysparam(val),Tsysparam(timeout),
  508. Tsysparam(addr2),Tsysparam(val3));
  509. end;
  510. function futex(var uaddr;op,val:cint;timeout:Ptimespec;var addr2;val3:cint):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  511. begin
  512. futex:=do_syscall(syscall_nr_futex,Tsysparam(@uaddr),Tsysparam(op),Tsysparam(val),Tsysparam(timeout),
  513. Tsysparam(@addr2),Tsysparam(val3));
  514. end;
  515. function futex(var uaddr;op,val:cint;var timeout:Ttimespec;var addr2;val3:cint):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  516. begin
  517. futex:=do_syscall(syscall_nr_futex,Tsysparam(@uaddr),Tsysparam(op),Tsysparam(val),Tsysparam(@timeout),
  518. Tsysparam(@addr2),Tsysparam(val3));
  519. end;
  520. function futex(uaddr:Pcint;op,val:cint;timeout:Ptimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  521. begin
  522. futex:=do_syscall(syscall_nr_futex,Tsysparam(uaddr),Tsysparam(op),Tsysparam(val),Tsysparam(timeout));
  523. end;
  524. function futex(var uaddr;op,val:cint;timeout:Ptimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  525. begin
  526. futex:=do_syscall(syscall_nr_futex,Tsysparam(@uaddr),Tsysparam(op),Tsysparam(val),Tsysparam(timeout));
  527. end;
  528. function futex(var uaddr;op,val:cint;var timeout:Ttimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  529. begin
  530. futex:=do_syscall(syscall_nr_futex,Tsysparam(@uaddr),Tsysparam(op),Tsysparam(val),Tsysparam(@timeout));
  531. end;
  532. {$else}
  533. {Libc case.}
  534. (*
  535. function futex(uaddr:Pcint;op,val:cint;timeout:Ptimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  536. begin
  537. futex:=futex(uaddr,op,val,nil,nil,0);
  538. end;
  539. function futex(var uaddr;op,val:cint;timeout:Ptimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  540. begin
  541. futex:=futex(@uaddr,op,val,nil,nil,0);
  542. end;
  543. function futex(var uaddr;op,val:cint;var timeout:Ttimespec):cint;{$ifdef SYSTEMINLINE}inline;{$endif}
  544. begin
  545. futex:=futex(@uaddr,op,val,@timeout,nil,0);
  546. end;
  547. *)
  548. {$endif} // non-libc
  549. {$ifndef FPC_USE_LIBC}
  550. {$if defined(cpui386) or defined(cpux86_64)}
  551. { does not exist as a wrapper in glibc, and exists only for x86 }
  552. function modify_ldt(func:cint;p:pointer;bytecount:culong):cint;
  553. begin
  554. modify_ldt:=do_syscall(syscall_nr_modify_ldt,Tsysparam(func),
  555. Tsysparam(p),
  556. Tsysparam(bytecount));
  557. end;
  558. {$endif}
  559. {$endif}
  560. { FUTEX_OP is a macro, doesn't exist in libC as function}
  561. function FUTEX_OP(op, oparg, cmp, cmparg: cint): cint; {$ifdef SYSTEMINLINE}inline;{$endif}
  562. begin
  563. FUTEX_OP := ((op and $F) shl 28) or ((cmp and $F) shl 24) or ((oparg and $FFF) shl 12) or (cmparg and $FFF);
  564. end;
  565. {$ifndef FPC_USE_LIBC}
  566. function inotify_init:cint;
  567. begin
  568. inotify_init:=inotify_init1(0);
  569. end;
  570. function inotify_init1(flags:cint):cint;
  571. begin
  572. inotify_init1:=do_SysCall(syscall_nr_inotify_init,tsysparam(flags));
  573. end;
  574. function inotify_add_watch(fd:cint; name:Pchar; mask:cuint32):cint;
  575. begin
  576. inotify_add_watch:=do_SysCall(syscall_nr_inotify_add_watch,tsysparam(fd),tsysparam(name),tsysparam(mask));
  577. end;
  578. function inotify_rm_watch(fd:cint; wd:cint):cint;
  579. begin
  580. inotify_rm_watch:=do_SysCall(syscall_nr_inotify_rm_watch,tsysparam(fd),tsysparam(wd));
  581. end;
  582. function clock_getres(clk_id : clockid_t; res : ptimespec) : cint;
  583. begin
  584. clock_getres:=do_SysCall(syscall_nr_clock_getres,tsysparam(clk_id),tsysparam(res));
  585. end;
  586. function clock_gettime(clk_id : clockid_t; tp: ptimespec) : cint;
  587. begin
  588. clock_gettime:=do_SysCall(syscall_nr_clock_gettime,tsysparam(clk_id),tsysparam(tp));
  589. end;
  590. function clock_settime(clk_id : clockid_t; tp : ptimespec) : cint;
  591. begin
  592. clock_settime:=do_SysCall(syscall_nr_clock_settime,tsysparam(clk_id),tsysparam(tp));
  593. end;
  594. function setregid(rgid,egid : uid_t): cint;
  595. begin
  596. setregid:=do_syscall(syscall_nr_setregid,rgid,egid);
  597. end;
  598. function setreuid(ruid,euid : uid_t): cint;
  599. begin
  600. setreuid:=do_syscall(syscall_nr_setreuid,ruid,euid);
  601. end;
  602. {$endif}
  603. end.