Sun Jul 14 01:51:39 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / hurd / hurdinit.c
blob7d946b8bffd09c0811a1c12d794aaeb715e2eb9b
1 /* Copyright (C) 1992, 1993, 1994, 1995, 1996 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
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 */
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;
35 error_t
36 _hurd_ports_use (int which, error_t (*operate) (mach_port_t))
38 return HURD_PORT_USE (&_hurd_ports[which], (*operate) (port));
41 void _hurd_proc_init (char **argv);
43 DEFINE_HOOK (_hurd_subinit, (void));
45 /* Initialize the library data structures from the
46 ints and ports passed to us by the exec server.
48 PORTARRAY and INTARRAY are vm_deallocate'd. */
50 void
51 _hurd_init (int flags, char **argv,
52 mach_port_t *portarray, size_t portarraysize,
53 int *intarray, size_t intarraysize)
55 size_t i;
57 _hurd_exec_flags = flags;
59 _hurd_ports = malloc (portarraysize * sizeof (*_hurd_ports));
60 if (_hurd_ports == NULL)
61 __libc_fatal ("Can't allocate _hurd_ports\n");
62 _hurd_nports = portarraysize;
64 /* See what ports we were passed. */
65 for (i = 0; i < portarraysize; ++i)
66 _hurd_port_init (&_hurd_ports[i], portarray[i]);
68 /* When the user asks for the bootstrap port,
69 he will get the one the exec server passed us. */
70 __task_set_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
71 portarray[INIT_PORT_BOOTSTRAP]);
73 if (intarraysize > INIT_UMASK)
74 _hurd_umask = intarray[INIT_UMASK] & 0777;
75 else
76 _hurd_umask = CMASK;
78 if (intarraysize > INIT_TRACEMASK)
79 _hurdsig_traced = intarray[INIT_TRACEMASK];
81 /* Tell the proc server we exist, if it does. */
82 if (portarray[INIT_PORT_PROC] != MACH_PORT_NULL)
83 _hurd_proc_init (argv);
85 /* All done with init ints and ports. */
86 __vm_deallocate (__mach_task_self (),
87 (vm_address_t) intarray,
88 intarraysize * sizeof (int));
89 __vm_deallocate (__mach_task_self (),
90 (vm_address_t) portarray,
91 portarraysize * sizeof (mach_port_t));
93 if (flags & EXEC_SECURE)
94 /* XXX if secure exec, elide environment variables
95 which the library uses and could be security holes.
96 CORESERVER, COREFILE
97 */ ;
99 /* Call other things which want to do some initialization. These are not
100 on the __libc_subinit hook because things there like to be able to
101 assume the availability of the POSIX.1 services we provide. */
102 RUN_HOOK (_hurd_subinit, ());
105 #include <hurd/signal.h>
107 /* The user can do "int _hide_arguments = 1;" to make
108 sure the arguments are never visible with `ps'. */
109 int _hide_arguments, _hide_environment;
111 /* Hook for things which should be initialized as soon as the proc
112 server is available. */
113 DEFINE_HOOK (_hurd_proc_subinit, (void));
115 /* Do startup handshaking with the proc server just installed in _hurd_ports.
116 Call _hurdsig_init to set up signal processing. */
118 void
119 _hurd_proc_init (char **argv)
121 mach_port_t oldmsg;
122 struct hurd_userlink ulink;
123 process_t procserver;
125 /* Initialize the signal code; Mach exceptions will become signals. */
126 _hurdsig_init ();
128 /* The signal thread is now prepared to receive messages.
129 It is safe to give the port to the proc server. */
131 procserver = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
133 /* Give the proc server our message port. */
134 __proc_setmsgport (procserver, _hurd_msgport, &oldmsg);
135 if (oldmsg != MACH_PORT_NULL)
136 /* Deallocate the old msg port we replaced. */
137 __mach_port_deallocate (__mach_task_self (), oldmsg);
139 /* Tell the proc server where our args and environment are. */
140 __proc_set_arg_locations (procserver,
141 _hide_arguments ? 0 : (vm_address_t) argv,
142 _hide_environment ? 0 : (vm_address_t) __environ);
144 _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, procserver);
146 /* Initialize proc server-assisted fault recovery for the signal thread. */
147 _hurdsig_fault_init ();
149 /* Call other things which want to do some initialization. These are not
150 on the _hurd_subinit hook because things there assume that things done
151 here, like _hurd_pid, are already initialized. */
152 RUN_HOOK (_hurd_proc_subinit, ());
154 if (_hurdsig_traced)
155 /* This process is "traced", meaning it should stop on signals or exec.
156 We are all set up now to handle signals. Stop ourselves, to inform
157 our parent (presumably a debugger) that the exec has completed. */
158 __msg_sig_post (_hurd_msgport, SIGTRAP, 0, __mach_task_self ());
161 /* Called when we get a message telling us to change our proc server port. */
163 error_t
164 _hurd_setproc (process_t procserver)
166 error_t err;
167 mach_port_t oldmsg;
169 /* Give the proc server our message port. */
170 if (err = __proc_setmsgport (procserver, _hurd_msgport, &oldmsg))
171 return err;
172 if (oldmsg != MACH_PORT_NULL)
173 /* Deallocate the old msg port we replaced. */
174 __mach_port_deallocate (__mach_task_self (), oldmsg);
176 /* Tell the proc server where our args and environment are. */
177 if (err = __proc_set_arg_locations (procserver,
178 /* We don't know the ARGV location. */
179 (vm_address_t) 0,
180 _hide_environment ? 0 :
181 (vm_address_t) __environ))
182 return err;
184 /* Those calls worked, so the port looks good. */
185 _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], procserver);
188 pid_t oldpgrp = _hurd_pgrp;
190 /* Call these functions again so they can fetch the
191 new information from the new proc server. */
192 RUN_HOOK (_hurd_proc_subinit, ());
194 if (_hurd_pgrp != oldpgrp)
196 /* Run things that want notification of a pgrp change. */
197 DECLARE_HOOK (_hurd_pgrp_changed_hook, (pid_t));
198 RUN_HOOK (_hurd_pgrp_changed_hook, (_hurd_pgrp));
202 return 0;
205 /* Map the page at address zero with no access allowed, so
206 dereferencing NULL will fault and no "anywhere" allocations
207 (e.g. the out of line memory containing the argument strings)
208 can be assigned address zero, which C says is not a valid pointer.
210 When dynamically linked, this should be done by the dynamic linker
211 before we run, but failing is harmless and we ignore the error. */
213 static void map0 (void) __attribute__ ((unused));
214 text_set_element (_hurd_preinit_hook, map0);
216 static void
217 map0 (void)
219 vm_address_t addr = 0;
220 __vm_map (__mach_task_self (),
221 &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
222 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);