]> git.ipfire.org Git - thirdparty/glibc.git/blob - hurd/hurdinit.c
y2038: Introduce struct __timespec64 - new internal glibc type
[thirdparty/glibc.git] / hurd / hurdinit.c
1 /* Copyright (C) 1992-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18 #include <sys/stat.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <hurd.h>
23 #include <hurd/port.h>
24 #include "set-hooks.h"
25 #include "hurdmalloc.h" /* XXX */
26
27
28 int _hurd_exec_flags;
29 struct hurd_port *_hurd_ports;
30 unsigned int _hurd_nports;
31 mode_t _hurd_umask;
32 sigset_t _hurdsig_traced;
33
34 char **__libc_argv;
35 int __libc_argc;
36
37
38 error_t
39 _hurd_ports_use (int which, error_t (*operate) (mach_port_t))
40 {
41 if (__glibc_unlikely (_hurd_ports == NULL))
42 /* This means that _hurd_init has not been called yet, which is
43 normally only the case in the bootstrap filesystem, and there
44 only in the early phases of booting. */
45 return EGRATUITOUS;
46
47 return HURD_PORT_USE (&_hurd_ports[which], (*operate) (port));
48 }
49
50 DEFINE_HOOK (_hurd_subinit, (void));
51
52 __typeof (_hurd_proc_init) _hurd_new_proc_init; /* below */
53
54 /* Initialize the library data structures from the
55 ints and ports passed to us by the exec server.
56
57 PORTARRAY and INTARRAY are vm_deallocate'd. */
58
59 void
60 _hurd_init (int flags, char **argv,
61 mach_port_t *portarray, size_t portarraysize,
62 int *intarray, size_t intarraysize)
63 {
64 size_t i;
65
66 _hurd_exec_flags = flags;
67
68 _hurd_ports = malloc (portarraysize * sizeof (*_hurd_ports));
69 if (_hurd_ports == NULL)
70 __libc_fatal ("Can't allocate _hurd_ports\n");
71 _hurd_nports = portarraysize;
72
73 /* See what ports we were passed. */
74 for (i = 0; i < portarraysize; ++i)
75 _hurd_port_init (&_hurd_ports[i], portarray[i]);
76
77 /* When the user asks for the bootstrap port,
78 he will get the one the exec server passed us. */
79 __task_set_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
80 portarray[INIT_PORT_BOOTSTRAP]);
81
82 if (intarraysize > INIT_UMASK)
83 _hurd_umask = intarray[INIT_UMASK] & 0777;
84 else
85 _hurd_umask = CMASK;
86
87 if (intarraysize > INIT_TRACEMASK)
88 _hurdsig_traced = intarray[INIT_TRACEMASK];
89
90 /* Tell the proc server we exist, if it does. */
91 if (portarray[INIT_PORT_PROC] != MACH_PORT_NULL)
92 _hurd_new_proc_init (argv, intarray, intarraysize);
93
94 /* All done with init ints and ports. */
95 __vm_deallocate (__mach_task_self (),
96 (vm_address_t) intarray,
97 intarraysize * sizeof (int));
98 __vm_deallocate (__mach_task_self (),
99 (vm_address_t) portarray,
100 portarraysize * sizeof (mach_port_t));
101
102 if (flags & EXEC_SECURE)
103 {
104 /* XXX if secure exec, elide environment variables
105 which the library uses and could be security holes.
106 CORESERVER, COREFILE
107 */
108 }
109
110 /* Call other things which want to do some initialization. These are not
111 on the __libc_subinit hook because things there like to be able to
112 assume the availability of the POSIX.1 services we provide. */
113 RUN_HOOK (_hurd_subinit, ());
114 }
115 libc_hidden_def (_hurd_init)
116 \f
117 #include <hurd/signal.h>
118
119 /* The user can do "int _hide_arguments = 1;" to make
120 sure the arguments are never visible with `ps'. */
121 int _hide_arguments, _hide_environment;
122
123 /* Hook for things which should be initialized as soon as the proc
124 server is available. */
125 DEFINE_HOOK (_hurd_proc_subinit, (void));
126
127 /* Do startup handshaking with the proc server just installed in _hurd_ports.
128 Call _hurdsig_init to set up signal processing. */
129
130 void
131 _hurd_new_proc_init (char **argv,
132 const int *intarray, size_t intarraysize)
133 {
134 mach_port_t oldmsg;
135 struct hurd_userlink ulink;
136 process_t procserver;
137
138 /* Initialize the signal code; Mach exceptions will become signals. */
139 _hurdsig_init (intarray, intarraysize);
140
141 /* The signal thread is now prepared to receive messages.
142 It is safe to give the port to the proc server. */
143
144 procserver = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
145
146 /* Give the proc server our message port. */
147 __proc_setmsgport (procserver, _hurd_msgport, &oldmsg);
148 if (oldmsg != MACH_PORT_NULL)
149 /* Deallocate the old msg port we replaced. */
150 __mach_port_deallocate (__mach_task_self (), oldmsg);
151
152 /* Tell the proc server where our args and environment are. */
153 __proc_set_arg_locations (procserver,
154 _hide_arguments ? 0 : (vm_address_t) argv,
155 _hide_environment ? 0 : (vm_address_t) __environ);
156
157 _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, procserver);
158
159 /* Initialize proc server-assisted fault recovery for the signal thread. */
160 _hurdsig_fault_init ();
161
162 /* Call other things which want to do some initialization. These are not
163 on the _hurd_subinit hook because things there assume that things done
164 here, like _hurd_pid, are already initialized. */
165 RUN_HOOK (_hurd_proc_subinit, ());
166
167 /* XXX This code should probably be removed entirely at some point. This
168 conditional should make it reasonably usable with old gdb's for a
169 while. Eventually it probably makes most sense for the exec server to
170 mask out EXEC_SIGTRAP so the debugged program is closer to not being
171 able to tell it's being debugged. */
172 if (!__sigisemptyset (&_hurdsig_traced)
173 #ifdef EXEC_SIGTRAP
174 && !(_hurd_exec_flags & EXEC_SIGTRAP)
175 #endif
176 )
177 /* This process is "traced", meaning it should stop on signals or exec.
178 We are all set up now to handle signals. Stop ourselves, to inform
179 our parent (presumably a debugger) that the exec has completed. */
180 __msg_sig_post (_hurd_msgport, SIGTRAP, 0, __mach_task_self ());
181 }
182
183 #include <shlib-compat.h>
184 versioned_symbol (libc, _hurd_new_proc_init, _hurd_proc_init, GLIBC_2_1);
185 \f
186 /* Called when we get a message telling us to change our proc server port. */
187
188 error_t
189 _hurd_setproc (process_t procserver)
190 {
191 error_t err;
192 mach_port_t oldmsg;
193
194 /* Give the proc server our message port. */
195 if (err = __proc_setmsgport (procserver, _hurd_msgport, &oldmsg))
196 return err;
197 if (oldmsg != MACH_PORT_NULL)
198 /* Deallocate the old msg port we replaced. */
199 __mach_port_deallocate (__mach_task_self (), oldmsg);
200
201 /* Tell the proc server where our args and environment are. */
202 if (err = __proc_set_arg_locations (procserver,
203 _hide_arguments ? 0
204 : (vm_address_t) __libc_argv,
205 _hide_environment ? 0
206 : (vm_address_t) __environ))
207 return err;
208
209 /* Those calls worked, so the port looks good. */
210 _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], procserver);
211
212 {
213 pid_t oldpgrp = _hurd_pgrp;
214
215 /* Call these functions again so they can fetch the
216 new information from the new proc server. */
217 RUN_HOOK (_hurd_proc_subinit, ());
218
219 if (_hurd_pgrp != oldpgrp)
220 {
221 /* Run things that want notification of a pgrp change. */
222 DECLARE_HOOK (_hurd_pgrp_changed_hook, (pid_t));
223 RUN_HOOK (_hurd_pgrp_changed_hook, (_hurd_pgrp));
224 }
225 }
226
227 return 0;
228 }