1 /* Copyright (C) 1993-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
24 /* Get types, macros, constants and function declarations
25 for all Mach microkernel interaction. */
27 #include <mach/mig_errors.h>
29 /* Get types and constants necessary for Hurd interfaces. */
30 #include <hurd/hurd_types.h>
32 /* Get MiG stub declarations for commonly used Hurd interfaces. */
33 #include <hurd/auth.h>
34 #include <hurd/process.h>
38 /* Get `struct hurd_port' and related definitions implementing lightweight
39 user references for ports. These are used pervasively throughout the C
40 library; this is here to avoid putting it in nearly every source file. */
41 #include <hurd/port.h>
44 #include <bits/types/error_t.h>
45 #include <bits/types/sigset_t.h>
47 #ifndef _HURD_H_EXTERN_INLINE
48 #define _HURD_H_EXTERN_INLINE __extern_inline
51 extern int __hurd_fail (error_t err
);
53 #ifdef __USE_EXTERN_INLINES
54 _HURD_H_EXTERN_INLINE
int
55 __hurd_fail (error_t err
)
59 case EMACH_SEND_INVALID_DEST
:
60 case EMIG_SERVER_DIED
:
61 /* The server has disappeared! */
62 err
= (error_t
) EIEIO
;
66 err
= (error_t
) ENOMEM
;
69 case KERN_INVALID_ARGUMENT
:
70 err
= (error_t
) EINVAL
;
85 /* Basic ports and info, initialized by startup. */
87 extern int _hurd_exec_flags
; /* Flags word passed in exec_startup. */
88 extern struct hurd_port
*_hurd_ports
;
89 extern unsigned int _hurd_nports
;
90 extern mode_t _hurd_umask
;
91 extern sigset_t _hurdsig_traced
;
93 /* Shorthand macro for internal library code referencing _hurd_ports (see
95 /* Also see __USEPORT_CANCEL. */
97 #define __USEPORT(which, expr) \
98 HURD_PORT_USE (&_hurd_ports[INIT_PORT_##which], (expr))
100 /* Function version of __USEPORT: calls OPERATE with a send right. */
102 extern error_t
_hurd_ports_use (int which
, error_t (*operate
) (mach_port_t
));
105 /* Base address and size of the initial stack set up by the exec server.
106 If using cthreads, this stack is deallocated in startup.
109 extern vm_address_t _hurd_stack_base
;
110 extern vm_size_t _hurd_stack_size
;
112 /* Initial file descriptor table we were passed at startup. If we are
113 using a real dtable, these are turned into that and then cleared at
114 startup. If not, these are never changed after startup. Not locked. */
116 extern mach_port_t
*_hurd_init_dtable
;
117 extern mach_msg_type_number_t _hurd_init_dtablesize
;
119 /* Current process IDs. */
121 extern pid_t _hurd_pid
, _hurd_ppid
, _hurd_pgrp
;
122 extern int _hurd_orphaned
;
124 /* This variable is incremented every time the process IDs change. */
125 extern unsigned int _hurd_pids_changed_stamp
;
127 /* This condition is broadcast every time the process IDs change. */
128 extern struct condition _hurd_pids_changed_sync
;
130 /* Unix `data break', for brk and sbrk.
131 If brk and sbrk are not used, this info will not be initialized or used. */
134 /* Data break. This is what `sbrk (0)' returns. */
136 extern vm_address_t _hurd_brk
;
138 /* End of allocated space. This is generally `round_page (_hurd_brk)'. */
140 extern vm_address_t _hurd_data_end
;
142 /* This mutex locks _hurd_brk and _hurd_data_end. */
144 extern struct mutex _hurd_brk_lock
;
146 /* Set the data break to NEWBRK; _hurd_brk_lock must
147 be held, and is released on return. */
149 extern int _hurd_set_brk (vm_address_t newbrk
);
151 #include <bits/types/FILE.h>
153 /* Calls to get and set basic ports. */
155 extern error_t
_hurd_ports_get (unsigned int which
, mach_port_t
*result
);
156 extern error_t
_hurd_ports_set (unsigned int which
, mach_port_t newport
);
158 extern process_t
getproc (void);
159 extern file_t
getcwdir (void), getcrdir (void);
160 extern auth_t
getauth (void);
161 extern mach_port_t
getcttyid (void);
162 extern int setproc (process_t
);
163 extern int setcwdir (file_t
), setcrdir (file_t
);
164 extern int setcttyid (mach_port_t
);
166 /* Does reauth with the proc server and fd io servers. */
167 extern int __setauth (auth_t
), setauth (auth_t
);
170 /* Modify a port cell by looking up a directory name.
171 This verifies that it is a directory and that we have search permission. */
172 extern int _hurd_change_directory_port_from_name (struct hurd_port
*portcell
,
174 /* Same thing, but using an open file descriptor.
175 Also verifies that it is a directory and that we have search permission. */
176 extern int _hurd_change_directory_port_from_fd (struct hurd_port
*portcell
,
181 /* Get and set the effective UID set. */
182 extern int geteuids (int __n
, uid_t
*__uidset
);
183 extern int seteuids (int __n
, const uid_t
*__uidset
);
186 /* Split FILE into a directory and a name within the directory. The
187 directory lookup uses the current root and working directory. If
188 successful, stores in *NAME a pointer into FILE where the name
189 within directory begins and returns a port to the directory;
190 otherwise sets `errno' and returns MACH_PORT_NULL. */
192 extern file_t
__file_name_split (const char *file
, char **name
);
193 extern file_t
file_name_split (const char *file
, char **name
);
195 /* Split DIRECTORY into a parent directory and a name within the directory.
196 This is the same as file_name_split, but ignores trailing slashes. */
198 extern file_t
__directory_name_split (const char *file
, char **name
);
199 extern file_t
directory_name_split (const char *file
, char **name
);
201 /* Open a port to FILE with the given FLAGS and MODE (see <fcntl.h>).
202 The file lookup uses the current root and working directory.
203 Returns a port to the file if successful; otherwise sets `errno'
204 and returns MACH_PORT_NULL. */
206 extern file_t
__file_name_lookup (const char *file
, int flags
, mode_t mode
);
207 extern file_t
file_name_lookup (const char *file
, int flags
, mode_t mode
);
209 /* Open a port to FILE with the given FLAGS and MODE (see <fcntl.h>). The
210 file lookup uses the current root directory, but uses STARTDIR as the
211 "working directory" for file relative names. Returns a port to the file
212 if successful; otherwise sets `errno' and returns MACH_PORT_NULL. */
214 extern file_t
__file_name_lookup_under (file_t startdir
, const char *file
,
215 int flags
, mode_t mode
);
216 extern file_t
file_name_lookup_under (file_t startdir
, const char *file
,
217 int flags
, mode_t mode
);
220 /* Lookup FILE_NAME and return the node opened with FLAGS & MODE
221 (see hurd_file_name_lookup for details), but a simple file name (without
222 any directory prefixes) will be consecutively prefixed with the pathnames
223 in the `:' separated list PATH until one succeeds in a successful lookup.
224 If none succeed, then the first error that wasn't ENOENT is returned, or
225 ENOENT if no other errors were returned. If PREFIXED_NAME is non-NULL,
226 then if the result is looked up directly, *PREFIXED_NAME is set to NULL, and
227 if it is looked up using a prefix from PATH, *PREFIXED_NAME is set to
228 malloc'd storage containing the prefixed name. */
229 extern file_t
file_name_path_lookup (const char *file_name
, const char *path
,
230 int flags
, mode_t mode
,
231 char **prefixed_name
);
235 /* Open a file descriptor on a port. FLAGS are as for `open'; flags
236 affected by io_set_openmodes are not changed by this. If successful,
237 this consumes a user reference for PORT (which will be deallocated on
240 extern int openport (io_t port
, int flags
);
242 /* Open a stream on a port. MODE is as for `fopen'.
243 If successful, this consumes a user reference for PORT
244 (which will be deallocated on fclose). */
246 extern FILE *fopenport (io_t port
, const char *mode
);
247 extern FILE *__fopenport (io_t port
, const char *mode
);
250 /* Deprecated: use _hurd_exec_paths instead. */
252 extern error_t
_hurd_exec (task_t task
,
255 char *const envp
[]) __attribute_deprecated__
;
257 /* Execute a file, replacing TASK's current program image. */
259 extern error_t
_hurd_exec_paths (task_t task
,
267 /* Inform the proc server we have exited with STATUS, and kill the
268 task thoroughly. This function never returns, no matter what. */
270 extern void _hurd_exit (int status
) __attribute__ ((noreturn
));
273 /* Initialize the library data structures from the
274 ints and ports passed to us by the exec server.
275 Then vm_deallocate PORTARRAY and INTARRAY. */
277 extern void _hurd_init (int flags
, char **argv
,
278 mach_port_t
*portarray
, size_t portarraysize
,
279 int *intarray
, size_t intarraysize
);
281 /* Do startup handshaking with the proc server, and initialize library data
282 structures that require proc server interaction. This includes
283 initializing signals; see _hurdsig_init in <hurd/signal.h>. */
285 extern void _hurd_proc_init (char **argv
,
286 const int *intarray
, size_t intarraysize
);
289 /* Return the socket server for sockaddr domain DOMAIN. If DEAD is
290 nonzero, remove the old cached port and always do a fresh lookup.
292 It is assumed that a socket server will stay alive during a complex socket
293 operation involving several RPCs. But a socket server may die during
294 long idle periods between socket operations. Callers should first pass
295 zero for DEAD; if the first socket RPC tried on the returned port fails
296 with MACH_SEND_INVALID_DEST or MIG_SERVER_DIED (indicating the server
297 went away), the caller should call _hurd_socket_server again with DEAD
298 nonzero and retry the RPC on the new socket server port. */
300 extern socket_t
_hurd_socket_server (int domain
, int dead
);
302 /* Send a `sig_post' RPC to process number PID. If PID is zero,
303 send the message to all processes in the current process's process group.
304 If PID is < -1, send SIG to all processes in process group - PID.
305 SIG and REFPORT are passed along in the request message. */
307 extern error_t
_hurd_sig_post (pid_t pid
, int sig
, mach_port_t refport
);
308 extern error_t
hurd_sig_post (pid_t pid
, int sig
, mach_port_t refport
);
310 /* Fetch the host privileged port and device master port from the proc
311 server. They are fetched only once and then cached in the
312 variables below. A special program that gets them from somewhere
313 other than the proc server (such as a bootstrap filesystem) can set
314 these variables to install the ports. */
316 extern kern_return_t
__get_privileged_ports (mach_port_t
*host_priv_ptr
,
317 device_t
*device_master_ptr
);
318 extern kern_return_t
get_privileged_ports (mach_port_t
*host_priv_ptr
,
319 device_t
*device_master_ptr
);
320 extern mach_port_t _hurd_host_priv
, _hurd_device_master
;
322 /* Return the PID of the task whose control port is TASK.
323 On error, sets `errno' and returns -1. */
325 extern pid_t
__task2pid (task_t task
), task2pid (task_t task
);
327 /* Return the task control port of process PID.
328 On error, sets `errno' and returns MACH_PORT_NULL. */
330 extern task_t
__pid2task (pid_t pid
), pid2task (pid_t pid
);
332 /* Return the current thread's thread port. This is a cheap operation (no
333 system call), but it relies on Hurd signal state being set up. */
334 extern thread_t
hurd_thread_self (void);
337 /* Cancel pending operations on THREAD. If it is doing an interruptible RPC,
338 that RPC will now return EINTR; otherwise, the "cancelled" flag will be
339 set, causing the next `hurd_check_cancel' call to return nonzero or the
340 next interruptible RPC to return EINTR (whichever is called first). */
341 extern error_t
hurd_thread_cancel (thread_t thread
);
343 /* Test and clear the calling thread's "cancelled" flag. */
344 extern int hurd_check_cancel (void);
347 /* Return the io server port for file descriptor FD.
348 This adds a Mach user reference to the returned port.
349 On error, sets `errno' and returns MACH_PORT_NULL. */
351 extern io_t
__getdport (int fd
), getdport (int fd
);
356 /* Write formatted output to PORT, a Mach port supporting the i/o protocol,
357 according to the format string FORMAT, using the argument list in ARG. */
358 int vpprintf (io_t port
, const char *format
, va_list arg
);