Mon Mar 20 03:19:23 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / hurd / hurdinit.c
1 /* Copyright (C) 1992, 1993, 1994, 1995 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 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.
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 Library General Public License for more details.
13
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.  */
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
34 void _hurd_proc_init (char **argv);
35
36 DEFINE_HOOK (_hurd_subinit, (void));
37
38 /* Initialize the library data structures from the
39    ints and ports passed to us by the exec server.
40
41    PORTARRAY and INTARRAY are vm_deallocate'd.  */
42
43 void
44 _hurd_init (int flags, char **argv,
45             mach_port_t *portarray, size_t portarraysize,
46             int *intarray, size_t intarraysize)
47 {
48   size_t i;
49
50   _hurd_exec_flags = flags;
51
52   _hurd_ports = malloc (portarraysize * sizeof (*_hurd_ports));
53   if (_hurd_ports == NULL)
54     __libc_fatal ("Can't allocate _hurd_ports\n");
55   _hurd_nports = portarraysize;
56
57   /* See what ports we were passed.  */
58   for (i = 0; i < portarraysize; ++i)
59     _hurd_port_init (&_hurd_ports[i], portarray[i]);
60
61   /* When the user asks for the bootstrap port,
62      he will get the one the exec server passed us.  */
63   __task_set_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
64                            portarray[INIT_PORT_BOOTSTRAP]);
65
66   /* Tell the proc server we exist, if it does.  */
67   if (portarray[INIT_PORT_PROC] != MACH_PORT_NULL)
68     _hurd_proc_init (argv);
69
70   if (intarraysize > INIT_UMASK)
71     _hurd_umask = intarray[INIT_UMASK] & 0777;
72   else
73     _hurd_umask = CMASK;
74
75   /* All done with init ints and ports.  */
76   __vm_deallocate (__mach_task_self (),
77                    (vm_address_t) intarray,
78                    intarraysize * sizeof (int));
79   __vm_deallocate (__mach_task_self (),
80                    (vm_address_t) portarray,
81                    portarraysize * sizeof (mach_port_t));
82
83   if (flags & EXEC_SECURE)
84     /* XXX if secure exec, elide environment variables
85        which the library uses and could be security holes.
86        CORESERVER, COREFILE
87        */ ;
88
89   /* Call other things which want to do some initialization.  These are not
90      on the __libc_subinit hook because things there like to be able to
91      assume the availability of the POSIX.1 services we provide.  */
92   RUN_HOOK (_hurd_subinit, ());
93 }
94 \f
95 #include <hurd/signal.h>
96
97 /* The user can do "int _hide_arguments = 1;" to make
98    sure the arguments are never visible with `ps'.  */
99 int _hide_arguments, _hide_environment;
100
101 /* Hook for things which should be initialized as soon as the proc
102    server is available.  */
103 DEFINE_HOOK (_hurd_proc_subinit, (void));
104
105 /* Do startup handshaking with the proc server just installed in _hurd_ports.
106    Call _hurdsig_init to set up signal processing.  */
107
108 void
109 _hurd_proc_init (char **argv)
110 {
111   mach_port_t oldmsg;
112   struct hurd_userlink ulink;
113   process_t procserver;
114
115   /* Initialize the signal code; Mach exceptions will become signals.  */
116   _hurdsig_init ();
117
118   /* The signal thread is now prepared to receive messages.
119      It is safe to give the port to the proc server.  */
120
121   procserver = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
122
123   /* Give the proc server our message port.  */
124   __proc_setmsgport (procserver, _hurd_msgport, &oldmsg);
125   if (oldmsg != MACH_PORT_NULL)
126     /* Deallocate the old msg port we replaced.  */
127     __mach_port_deallocate (__mach_task_self (), oldmsg);
128
129   /* Tell the proc server where our args and environment are.  */
130   __proc_set_arg_locations (procserver,
131                             _hide_arguments ? 0 : (vm_address_t) argv,
132                             _hide_environment ? 0 : (vm_address_t) __environ);
133
134   _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, procserver);
135
136   /* Initialize proc server-assisted fault recovery for the signal thread.  */
137   _hurdsig_fault_init ();
138
139   /* Call other things which want to do some initialization.  These are not
140      on the _hurd_subinit hook because things there assume that things done
141      here, like _hurd_pid, are already initialized.  */
142   RUN_HOOK (_hurd_proc_subinit, ());
143
144   if (_hurd_exec_flags & EXEC_TRACED)
145     /* This process is "traced", meaning it should stop on signals or exec.
146        We are all set up now to handle signals.  Stop ourselves, to inform
147        our parent (presumably a debugger) that the exec has completed.  */
148     _hurd_raise_signal (NULL, SIGTRAP, 0, 0);
149 }
150 \f
151 /* Called when we get a message telling us to change our proc server port.  */
152
153 error_t
154 _hurd_setproc (process_t procserver)
155 {
156   error_t err;
157   mach_port_t oldmsg;
158
159   /* Give the proc server our message port.  */
160   if (err = __proc_setmsgport (procserver, _hurd_msgport, &oldmsg))
161     return err;
162   if (oldmsg != MACH_PORT_NULL)
163     /* Deallocate the old msg port we replaced.  */
164     __mach_port_deallocate (__mach_task_self (), oldmsg);
165
166   /* Tell the proc server where our args and environment are.  */
167   if (err = __proc_set_arg_locations (procserver,
168                                       /* We don't know the ARGV location.  */
169                                       (vm_address_t) 0,
170                                       _hide_environment ? 0 :
171                                       (vm_address_t) __environ))
172     return err;
173
174   /* Those calls worked, so the port looks good.  */
175   _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], procserver);
176
177   {
178     pid_t oldpgrp = _hurd_pgrp;
179
180     /* Call these functions again so they can fetch the
181        new information from the new proc server.  */
182     RUN_HOOK (_hurd_proc_subinit, ());
183
184     if (_hurd_pgrp != oldpgrp)
185       {
186         /* Run things that want notification of a pgrp change.  */
187         DECLARE_HOOK (_hurd_pgrp_changed_hook, (pid_t));
188         RUN_HOOK (_hurd_pgrp_changed_hook, (_hurd_pgrp));
189       }
190   }
191
192   return 0;
193 }