1 /* Copyright (C) 1992, 93, 94, 95, 96, 97 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
24 #include <hurd/port.h>
25 #include "set-hooks.h"
26 #include "hurdmalloc.h" /* XXX */
30 struct hurd_port
*_hurd_ports
;
31 unsigned int _hurd_nports
;
33 sigset_t _hurdsig_traced
;
40 _hurd_ports_use (int which
, error_t (*operate
) (mach_port_t
))
42 return HURD_PORT_USE (&_hurd_ports
[which
], (*operate
) (port
));
45 void _hurd_proc_init (char **argv
);
47 DEFINE_HOOK (_hurd_subinit
, (void));
49 /* Initialize the library data structures from the
50 ints and ports passed to us by the exec server.
52 PORTARRAY and INTARRAY are vm_deallocate'd. */
55 _hurd_init (int flags
, char **argv
,
56 mach_port_t
*portarray
, size_t portarraysize
,
57 int *intarray
, size_t intarraysize
)
61 _hurd_exec_flags
= flags
;
63 _hurd_ports
= malloc (portarraysize
* sizeof (*_hurd_ports
));
64 if (_hurd_ports
== NULL
)
65 __libc_fatal ("Can't allocate _hurd_ports\n");
66 _hurd_nports
= portarraysize
;
68 /* See what ports we were passed. */
69 for (i
= 0; i
< portarraysize
; ++i
)
70 _hurd_port_init (&_hurd_ports
[i
], portarray
[i
]);
72 /* When the user asks for the bootstrap port,
73 he will get the one the exec server passed us. */
74 __task_set_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT
,
75 portarray
[INIT_PORT_BOOTSTRAP
]);
77 if (intarraysize
> INIT_UMASK
)
78 _hurd_umask
= intarray
[INIT_UMASK
] & 0777;
82 if (intarraysize
> INIT_TRACEMASK
)
83 _hurdsig_traced
= intarray
[INIT_TRACEMASK
];
85 /* Tell the proc server we exist, if it does. */
86 if (portarray
[INIT_PORT_PROC
] != MACH_PORT_NULL
)
87 _hurd_proc_init (argv
);
89 /* All done with init ints and ports. */
90 __vm_deallocate (__mach_task_self (),
91 (vm_address_t
) intarray
,
92 intarraysize
* sizeof (int));
93 __vm_deallocate (__mach_task_self (),
94 (vm_address_t
) portarray
,
95 portarraysize
* sizeof (mach_port_t
));
97 if (flags
& EXEC_SECURE
)
98 /* XXX if secure exec, elide environment variables
99 which the library uses and could be security holes.
103 /* Call other things which want to do some initialization. These are not
104 on the __libc_subinit hook because things there like to be able to
105 assume the availability of the POSIX.1 services we provide. */
106 RUN_HOOK (_hurd_subinit
, ());
109 #include <hurd/signal.h>
111 /* The user can do "int _hide_arguments = 1;" to make
112 sure the arguments are never visible with `ps'. */
113 int _hide_arguments
, _hide_environment
;
115 /* Hook for things which should be initialized as soon as the proc
116 server is available. */
117 DEFINE_HOOK (_hurd_proc_subinit
, (void));
119 /* Do startup handshaking with the proc server just installed in _hurd_ports.
120 Call _hurdsig_init to set up signal processing. */
123 _hurd_proc_init (char **argv
)
126 struct hurd_userlink ulink
;
127 process_t procserver
;
129 /* Initialize the signal code; Mach exceptions will become signals. */
132 /* The signal thread is now prepared to receive messages.
133 It is safe to give the port to the proc server. */
135 procserver
= _hurd_port_get (&_hurd_ports
[INIT_PORT_PROC
], &ulink
);
137 /* Give the proc server our message port. */
138 __proc_setmsgport (procserver
, _hurd_msgport
, &oldmsg
);
139 if (oldmsg
!= MACH_PORT_NULL
)
140 /* Deallocate the old msg port we replaced. */
141 __mach_port_deallocate (__mach_task_self (), oldmsg
);
143 /* Tell the proc server where our args and environment are. */
144 __proc_set_arg_locations (procserver
,
145 _hide_arguments
? 0 : (vm_address_t
) argv
,
146 _hide_environment
? 0 : (vm_address_t
) __environ
);
148 _hurd_port_free (&_hurd_ports
[INIT_PORT_PROC
], &ulink
, procserver
);
150 /* Initialize proc server-assisted fault recovery for the signal thread. */
151 _hurdsig_fault_init ();
153 /* Call other things which want to do some initialization. These are not
154 on the _hurd_subinit hook because things there assume that things done
155 here, like _hurd_pid, are already initialized. */
156 RUN_HOOK (_hurd_proc_subinit
, ());
159 /* This process is "traced", meaning it should stop on signals or exec.
160 We are all set up now to handle signals. Stop ourselves, to inform
161 our parent (presumably a debugger) that the exec has completed. */
162 __msg_sig_post (_hurd_msgport
, SIGTRAP
, 0, __mach_task_self ());
165 /* Called when we get a message telling us to change our proc server port. */
168 _hurd_setproc (process_t procserver
)
173 /* Give the proc server our message port. */
174 if (err
= __proc_setmsgport (procserver
, _hurd_msgport
, &oldmsg
))
176 if (oldmsg
!= MACH_PORT_NULL
)
177 /* Deallocate the old msg port we replaced. */
178 __mach_port_deallocate (__mach_task_self (), oldmsg
);
180 /* Tell the proc server where our args and environment are. */
181 if (err
= __proc_set_arg_locations (procserver
,
182 _hide_arguments
? 0 :
183 (vm_address_t
) __libc_argv
,
184 _hide_environment
? 0 :
185 (vm_address_t
) __environ
))
188 /* Those calls worked, so the port looks good. */
189 _hurd_port_set (&_hurd_ports
[INIT_PORT_PROC
], procserver
);
192 pid_t oldpgrp
= _hurd_pgrp
;
194 /* Call these functions again so they can fetch the
195 new information from the new proc server. */
196 RUN_HOOK (_hurd_proc_subinit
, ());
198 if (_hurd_pgrp
!= oldpgrp
)
200 /* Run things that want notification of a pgrp change. */
201 DECLARE_HOOK (_hurd_pgrp_changed_hook
, (pid_t
));
202 RUN_HOOK (_hurd_pgrp_changed_hook
, (_hurd_pgrp
));