linux.pp 32 KB

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