hurd.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* Copyright (C) 1993-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef _HURD_H
  15. #define _HURD_H 1
  16. #include <features.h>
  17. /* Get types, macros, constants and function declarations
  18. for all Mach microkernel interaction. */
  19. #include <mach.h>
  20. #include <mach/mig_errors.h>
  21. /* Get types and constants necessary for Hurd interfaces. */
  22. #include <hurd/hurd_types.h>
  23. /* Get MiG stub declarations for commonly used Hurd interfaces. */
  24. #include <hurd/auth.h>
  25. #include <hurd/process.h>
  26. #include <hurd/fs.h>
  27. #include <hurd/io.h>
  28. /* Get `struct hurd_port' and related definitions implementing lightweight
  29. user references for ports. These are used pervasively throughout the C
  30. library; this is here to avoid putting it in nearly every source file. */
  31. #include <hurd/port.h>
  32. #include <errno.h>
  33. #include <bits/types/error_t.h>
  34. #include <bits/types/sigset_t.h>
  35. #ifndef _HURD_H_EXTERN_INLINE
  36. #define _HURD_H_EXTERN_INLINE __extern_inline
  37. #endif
  38. extern int __hurd_fail (error_t err) __COLD;
  39. #ifdef __USE_EXTERN_INLINES
  40. _HURD_H_EXTERN_INLINE int
  41. __hurd_fail (error_t err)
  42. {
  43. switch (err)
  44. {
  45. case EMACH_SEND_INVALID_DEST:
  46. case EMIG_SERVER_DIED:
  47. /* The server has disappeared! */
  48. err = (error_t) EIEIO;
  49. break;
  50. case KERN_NO_SPACE:
  51. err = (error_t) ENOMEM;
  52. break;
  53. case KERN_INVALID_ADDRESS:
  54. case KERN_INVALID_ARGUMENT:
  55. err = (error_t) EINVAL;
  56. break;
  57. case 0:
  58. return 0;
  59. default:
  60. break;
  61. }
  62. errno = err;
  63. return -1;
  64. }
  65. #endif
  66. /* Basic ports and info, initialized by startup. */
  67. extern int _hurd_exec_flags; /* Flags word passed in exec_startup. */
  68. extern struct hurd_port *_hurd_ports;
  69. extern unsigned int _hurd_nports;
  70. extern mode_t _hurd_umask;
  71. extern sigset_t _hurdsig_traced;
  72. /* Shorthand macro for internal library code referencing _hurd_ports (see
  73. <hurd/port.h>). */
  74. /* Also see __USEPORT_CANCEL. */
  75. #define __USEPORT(which, expr) \
  76. HURD_PORT_USE (&_hurd_ports[INIT_PORT_##which], (expr))
  77. /* Function version of __USEPORT: calls OPERATE with a send right. */
  78. extern error_t _hurd_ports_use (int which, error_t (*operate) (mach_port_t));
  79. /* Base address and size of the initial stack set up by the exec server.
  80. Not locked. */
  81. extern vm_address_t _hurd_stack_base;
  82. extern vm_size_t _hurd_stack_size;
  83. /* Initial file descriptor table we were passed at startup. If we are
  84. using a real dtable, these are turned into that and then cleared at
  85. startup. If not, these are never changed after startup. Not locked. */
  86. extern mach_port_t *_hurd_init_dtable;
  87. extern mach_msg_type_number_t _hurd_init_dtablesize;
  88. /* Current process IDs. */
  89. extern pid_t _hurd_pid, _hurd_ppid, _hurd_pgrp;
  90. extern int _hurd_orphaned;
  91. /* This variable is incremented every time the process IDs change. */
  92. extern unsigned int _hurd_pids_changed_stamp;
  93. /* Unix `data break', for brk and sbrk.
  94. If brk and sbrk are not used, this info will not be initialized or used. */
  95. /* Data break. This is what `sbrk (0)' returns. */
  96. extern vm_address_t _hurd_brk;
  97. /* End of allocated space. This is generally `round_page (_hurd_brk)'. */
  98. extern vm_address_t _hurd_data_end;
  99. /* This mutex locks _hurd_brk and _hurd_data_end. */
  100. extern struct mutex _hurd_brk_lock;
  101. /* Set the data break to NEWBRK; _hurd_brk_lock must
  102. be held, and is released on return. */
  103. extern int _hurd_set_brk (vm_address_t newbrk);
  104. #include <bits/types/FILE.h>
  105. /* Calls to get and set basic ports. */
  106. extern error_t _hurd_ports_get (unsigned int which, mach_port_t *result);
  107. extern error_t _hurd_ports_set (unsigned int which, mach_port_t newport);
  108. extern process_t getproc (void);
  109. extern file_t getcwdir (void), getcrdir (void);
  110. extern auth_t getauth (void);
  111. extern mach_port_t getcttyid (void);
  112. extern int setproc (process_t);
  113. extern int setcwdir (file_t), setcrdir (file_t);
  114. extern int setcttyid (mach_port_t);
  115. /* Does reauth with the proc server and fd io servers. */
  116. extern int __setauth (auth_t), setauth (auth_t);
  117. /* Modify a port cell by looking up a directory name.
  118. This verifies that it is a directory and that we have search permission. */
  119. extern int _hurd_change_directory_port_from_name (struct hurd_port *portcell,
  120. const char *name);
  121. /* Same thing, but using an open file descriptor.
  122. Also verifies that it is a directory and that we have search permission. */
  123. extern int _hurd_change_directory_port_from_fd (struct hurd_port *portcell,
  124. int fd);
  125. /* Get and set the effective UID set. */
  126. extern int geteuids (int __n, uid_t *__uidset);
  127. extern int seteuids (int __n, const uid_t *__uidset);
  128. /* Split FILE into a directory and a name within the directory. The
  129. directory lookup uses the current root and working directory. If
  130. successful, stores in *NAME a pointer into FILE where the name
  131. within directory begins and returns a port to the directory;
  132. otherwise sets `errno' and returns MACH_PORT_NULL. */
  133. extern file_t __file_name_split (const char *file, char **name);
  134. extern file_t file_name_split (const char *file, char **name);
  135. /* Split DIRECTORY into a parent directory and a name within the directory.
  136. This is the same as file_name_split, but ignores trailing slashes. */
  137. extern file_t __directory_name_split (const char *file, char **name);
  138. extern file_t directory_name_split (const char *file, char **name);
  139. /* Open a port to FILE with the given FLAGS and MODE (see <fcntl.h>).
  140. The file lookup uses the current root and working directory.
  141. Returns a port to the file if successful; otherwise sets `errno'
  142. and returns MACH_PORT_NULL. */
  143. extern file_t __file_name_lookup (const char *file, int flags, mode_t mode);
  144. extern file_t file_name_lookup (const char *file, int flags, mode_t mode);
  145. /* Open a port to FILE with the given FLAGS and MODE (see <fcntl.h>). The
  146. file lookup uses the current root directory, but uses STARTDIR as the
  147. "working directory" for file relative names. Returns a port to the file
  148. if successful; otherwise sets `errno' and returns MACH_PORT_NULL. */
  149. extern file_t __file_name_lookup_under (file_t startdir, const char *file,
  150. int flags, mode_t mode);
  151. extern file_t file_name_lookup_under (file_t startdir, const char *file,
  152. int flags, mode_t mode);
  153. /* Lookup FILE_NAME and return the node opened with FLAGS & MODE
  154. (see hurd_file_name_lookup for details), but a simple file name (without
  155. any directory prefixes) will be consecutively prefixed with the pathnames
  156. in the `:' separated list PATH until one succeeds in a successful lookup.
  157. If none succeed, then the first error that wasn't ENOENT is returned, or
  158. ENOENT if no other errors were returned. If PREFIXED_NAME is non-NULL,
  159. then if the result is looked up directly, *PREFIXED_NAME is set to NULL, and
  160. if it is looked up using a prefix from PATH, *PREFIXED_NAME is set to
  161. malloc'd storage containing the prefixed name. */
  162. extern file_t file_name_path_lookup (const char *file_name, const char *path,
  163. int flags, mode_t mode,
  164. char **prefixed_name);
  165. /* Open a file descriptor on a port. FLAGS are as for `open'; flags
  166. affected by io_set_openmodes are not changed by this. If successful,
  167. this consumes a user reference for PORT (which will be deallocated on
  168. close). */
  169. extern int openport (io_t port, int flags);
  170. /* Open a stream on a port. MODE is as for `fopen'.
  171. If successful, this consumes a user reference for PORT
  172. (which will be deallocated on fclose). */
  173. extern FILE *fopenport (io_t port, const char *mode);
  174. extern FILE *__fopenport (io_t port, const char *mode);
  175. /* Deprecated: use _hurd_exec_paths instead. */
  176. extern error_t _hurd_exec (task_t task,
  177. file_t file,
  178. char *const argv[],
  179. char *const envp[]) __attribute_deprecated__;
  180. /* Execute a file, replacing TASK's current program image. */
  181. extern error_t _hurd_exec_paths (task_t task,
  182. file_t file,
  183. const char *path,
  184. const char *abspath,
  185. char *const argv[],
  186. char *const envp[]);
  187. /* Inform the proc server we have exited with STATUS, and kill the
  188. task thoroughly. This function never returns, no matter what. */
  189. extern void _hurd_exit (int status) __attribute__ ((noreturn));
  190. /* Initialize the library data structures from the
  191. ints and ports passed to us by the exec server.
  192. Then vm_deallocate PORTARRAY and INTARRAY. */
  193. extern void _hurd_init (int flags, char **argv,
  194. mach_port_t *portarray, size_t portarraysize,
  195. int *intarray, size_t intarraysize);
  196. /* Register the process to the proc server. */
  197. extern void _hurd_libc_proc_init (char **argv);
  198. /* Do startup handshaking with the proc server, and initialize library data
  199. structures that require proc server interaction. This includes
  200. initializing signals; see _hurdsig_init in <hurd/signal.h>. */
  201. extern void _hurd_proc_init (char **argv,
  202. const int *intarray, size_t intarraysize);
  203. /* Return the socket server for sockaddr domain DOMAIN. If DEAD is
  204. nonzero, remove the old cached port and always do a fresh lookup.
  205. It is assumed that a socket server will stay alive during a complex socket
  206. operation involving several RPCs. But a socket server may die during
  207. long idle periods between socket operations. Callers should first pass
  208. zero for DEAD; if the first socket RPC tried on the returned port fails
  209. with MACH_SEND_INVALID_DEST or MIG_SERVER_DIED (indicating the server
  210. went away), the caller should call _hurd_socket_server again with DEAD
  211. nonzero and retry the RPC on the new socket server port. */
  212. extern socket_t _hurd_socket_server (int domain, int dead);
  213. /* Send a `sig_post' RPC to process number PID. If PID is zero,
  214. send the message to all processes in the current process's process group.
  215. If PID is < -1, send SIG to all processes in process group - PID.
  216. SIG and REFPORT are passed along in the request message. */
  217. extern error_t _hurd_sig_post (pid_t pid, int sig, mach_port_t refport);
  218. extern error_t hurd_sig_post (pid_t pid, int sig, mach_port_t refport);
  219. /* Fetch the host privileged port and device master port from the proc
  220. server. They are fetched only once and then cached in the
  221. variables below. A special program that gets them from somewhere
  222. other than the proc server (such as a bootstrap filesystem) can set
  223. these variables to install the ports. */
  224. extern kern_return_t __get_privileged_ports (mach_port_t *host_priv_ptr,
  225. device_t *device_master_ptr);
  226. extern kern_return_t get_privileged_ports (mach_port_t *host_priv_ptr,
  227. device_t *device_master_ptr);
  228. extern mach_port_t _hurd_host_priv, _hurd_device_master;
  229. /* Return the PID of the task whose control port is TASK.
  230. On error, sets `errno' and returns -1. */
  231. extern pid_t __task2pid (task_t task), task2pid (task_t task);
  232. /* Return the task control port of process PID.
  233. On error, sets `errno' and returns MACH_PORT_NULL. */
  234. extern task_t __pid2task (pid_t pid), pid2task (pid_t pid);
  235. /* Return the current thread's thread port. This is a cheap operation (no
  236. system call), but it relies on Hurd signal state being set up. */
  237. extern thread_t hurd_thread_self (void);
  238. /* Cancel pending operations on THREAD. If it is doing an interruptible RPC,
  239. that RPC will now return EINTR; otherwise, the "cancelled" flag will be
  240. set, causing the next `hurd_check_cancel' call to return nonzero or the
  241. next interruptible RPC to return EINTR (whichever is called first). */
  242. extern error_t hurd_thread_cancel (thread_t thread);
  243. /* Test and clear the calling thread's "cancelled" flag. */
  244. extern int hurd_check_cancel (void);
  245. /* Return the io server port for file descriptor FD.
  246. This adds a Mach user reference to the returned port.
  247. On error, sets `errno' and returns MACH_PORT_NULL. */
  248. extern io_t __getdport (int fd), getdport (int fd);
  249. #define __need___va_list
  250. #include <stdarg.h>
  251. /* Write formatted output to PORT, a Mach port supporting the i/o protocol,
  252. according to the format string FORMAT, using the argument list in ARG. */
  253. int vpprintf (io_t port, const char *format, __gnuc_va_list arg);
  254. #endif /* hurd.h */