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