linux.pp 29 KB

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