]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/hurdexec.c
Tue Apr 30 17:30:46 1996 Miles Bader <miles@gnu.ai.mit.edu>
[thirdparty/glibc.git] / hurd / hurdexec.c
CommitLineData
28f540f4
RM
1/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19#include <errno.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <limits.h>
23#include <stdlib.h>
24#include <string.h>
25#include <hurd.h>
26#include <hurd/fd.h>
27#include <hurd/signal.h>
28
29/* Overlay TASK, executing FILE with arguments ARGV and environment ENVP.
30 If TASK == mach_task_self (), some ports are dealloc'd by the exec server.
31 ARGV and ENVP are terminated by NULL pointers. */
32error_t
75914335 33_hurd_exec (task_t task, file_t file,
28f540f4
RM
34 char *const argv[], char *const envp[])
35{
36 error_t err;
37 char *args, *env, *ap;
38 size_t argslen, envlen;
39 int ints[INIT_INT_MAX];
40 mach_port_t ports[_hurd_nports];
41 struct hurd_userlink ulink_ports[_hurd_nports];
42 file_t *dtable;
1e9dc039 43 unsigned int dtablesize, i;
28f540f4
RM
44 struct hurd_port **dtable_cells;
45 struct hurd_userlink *ulink_dtable;
28f540f4
RM
46 char *const *p;
47 struct hurd_sigstate *ss;
48 mach_port_t *please_dealloc, *pdp;
49
50
51 /* Pack the arguments into an array with nulls separating the elements. */
52 argslen = 0;
53 if (argv != NULL)
54 {
55 p = argv;
56 while (*p != NULL)
57 argslen += strlen (*p++) + 1;
58 args = __alloca (argslen);
59 ap = args;
60 for (p = argv; *p != NULL; ++p)
61 ap = __memccpy (ap, *p, '\0', ULONG_MAX);
62 }
63 else
64 args = NULL;
65
66 /* Pack the environment into an array with nulls separating elements. */
67 envlen = 0;
68 if (envp != NULL)
69 {
70 p = envp;
71 while (*p != NULL)
72 envlen += strlen (*p++) + 1;
73 env = __alloca (envlen);
74 ap = env;
75 for (p = envp; *p != NULL; ++p)
76 ap = __memccpy (ap, *p, '\0', ULONG_MAX);
77 }
78 else
79 env = NULL;
80
81 /* Load up the ports to give to the new program. */
82 for (i = 0; i < _hurd_nports; ++i)
83 if (i == INIT_PORT_PROC && task != __mach_task_self ())
84 {
85 /* This is another task, so we need to ask the proc server
86 for the right proc server port for it. */
87 if (err = __USEPORT (PROC, __proc_task2proc (port, task, &ports[i])))
88 {
89 while (--i > 0)
90 _hurd_port_free (&_hurd_ports[i], &ulink_ports[i], ports[i]);
91 return err;
92 }
93 }
94 else
95 ports[i] = _hurd_port_get (&_hurd_ports[i], &ulink_ports[i]);
96
97
98 /* Load up the ints to give the new program. */
99 for (i = 0; i < INIT_INT_MAX; ++i)
100 switch (i)
101 {
102 case INIT_UMASK:
103 ints[i] = _hurd_umask;
104 break;
105
106 case INIT_SIGMASK:
107 case INIT_SIGIGN:
108 case INIT_SIGPENDING:
109 /* We will set these all below. */
110 break;
111
112 default:
113 ints[i] = 0;
114 }
115
116 ss = _hurd_self_sigstate ();
117 __spin_lock (&ss->lock);
118 ints[INIT_SIGMASK] = ss->blocked;
119 ints[INIT_SIGPENDING] = ss->pending;
120 ints[INIT_SIGIGN] = 0;
121 for (i = 1; i < NSIG; ++i)
122 if (ss->actions[i].sa_handler == SIG_IGN)
123 ints[INIT_SIGIGN] |= __sigmask (i);
124
125 /* We hold the sigstate lock until the exec has failed so that no signal
126 can arrive between when we pack the blocked and ignored signals, and
127 when the exec actually happens. A signal handler could change what
128 signals are blocked and ignored. Either the change will be reflected
129 in the exec, or the signal will never be delivered. Setting the
130 critical section flag avoids anything we call trying to acquire the
131 sigstate lock. */
75914335 132
28f540f4
RM
133 ss->critical_section = 1;
134 __spin_unlock (&ss->lock);
135
136 /* Pack up the descriptor table to give the new program. */
137 __mutex_lock (&_hurd_dtable_lock);
138
139 dtablesize = _hurd_dtable ? _hurd_dtablesize : _hurd_init_dtablesize;
140
141 if (task == __mach_task_self ())
142 /* Request the exec server to deallocate some ports from us if the exec
143 succeeds. The init ports and descriptor ports will arrive in the
144 new program's exec_startup message. If we failed to deallocate
145 them, the new program would have duplicate user references for them.
146 But we cannot deallocate them ourselves, because we must still have
147 them after a failed exec call. */
148 please_dealloc = __alloca ((_hurd_nports + (2 * dtablesize))
149 * sizeof (mach_port_t));
150 else
151 please_dealloc = NULL;
152 pdp = please_dealloc;
153
154 if (_hurd_dtable != NULL)
155 {
156 dtable = __alloca (dtablesize * sizeof (dtable[0]));
157 ulink_dtable = __alloca (dtablesize * sizeof (ulink_dtable[0]));
158 dtable_cells = __alloca (dtablesize * sizeof (dtable_cells[0]));
159 for (i = 0; i < dtablesize; ++i)
160 {
161 struct hurd_fd *const d = _hurd_dtable[i];
162 if (d == NULL)
163 {
164 dtable[i] = MACH_PORT_NULL;
165 continue;
166 }
167 __spin_lock (&d->port.lock);
168 if (d->flags & FD_CLOEXEC)
169 {
170 /* This descriptor is marked to be closed on exec.
171 So don't pass it to the new program. */
172 dtable[i] = MACH_PORT_NULL;
173 if (pdp && d->port.port != MACH_PORT_NULL)
174 {
175 /* We still need to deallocate the ports. */
176 *pdp++ = d->port.port;
177 if (d->ctty.port != MACH_PORT_NULL)
178 *pdp++ = d->ctty.port;
179 }
180 __spin_unlock (&d->port.lock);
181 }
182 else
183 {
184 if (pdp && d->ctty.port != MACH_PORT_NULL)
185 /* All the elements of DTABLE are added to PLEASE_DEALLOC
186 below, so we needn't add the port itself.
187 But we must deallocate the ctty port as well as
188 the normal port that got installed in DTABLE[I]. */
189 *pdp++ = d->ctty.port;
190 dtable[i] = _hurd_port_locked_get (&d->port, &ulink_dtable[i]);
191 dtable_cells[i] = &d->port;
192 }
193 }
194 }
195 else
196 {
197 dtable = _hurd_init_dtable;
198 ulink_dtable = NULL;
199 dtable_cells = NULL;
200 }
201
202 /* The information is all set up now. Try to exec the file. */
203
204 {
205 if (pdp)
206 {
207 /* Request the exec server to deallocate some ports from us if the exec
208 succeeds. The init ports and descriptor ports will arrive in the
209 new program's exec_startup message. If we failed to deallocate
210 them, the new program would have duplicate user references for them.
211 But we cannot deallocate them ourselves, because we must still have
212 them after a failed exec call. */
213
214 for (i = 0; i < _hurd_nports; ++i)
215 *pdp++ = ports[i];
216 for (i = 0; i < dtablesize; ++i)
217 *pdp++ = dtable[i];
218 }
219
924b9ff6 220 err = __file_exec (file, task,
28f540f4 221 _hurd_exec_flags & EXEC_INHERITED,
924b9ff6
RM
222 args, argslen, env, envlen,
223 dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize,
224 ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports,
225 ints, INIT_INT_MAX,
226 please_dealloc, pdp - please_dealloc,
75914335 227 &_hurd_msgport, task == __mach_task_self () ? 1 : 0);
28f540f4
RM
228 }
229
230 /* Release references to the standard ports. */
231 for (i = 0; i < _hurd_nports; ++i)
232 if (i == INIT_PORT_PROC && task != __mach_task_self ())
233 __mach_port_deallocate (__mach_task_self (), ports[i]);
234 else
235 _hurd_port_free (&_hurd_ports[i], &ulink_ports[i], ports[i]);
236
237 if (ulink_dtable != NULL)
238 /* Release references to the file descriptor ports. */
239 for (i = 0; i < dtablesize; ++i)
240 if (dtable[i] != MACH_PORT_NULL)
241 _hurd_port_free (dtable_cells[i], &ulink_dtable[i], dtable[i]);
242
243 /* Release lock on the file descriptor table. */
244 __mutex_unlock (&_hurd_dtable_lock);
245
246 /* Safe to let signals happen now. */
247 {
248 sigset_t pending;
249 __spin_lock (&ss->lock);
250 ss->critical_section = 0;
251 pending = ss->pending & ~ss->blocked;
252 __spin_unlock (&ss->lock);
253 if (pending)
254 __msg_sig_post (_hurd_msgport, 0, __mach_task_self ());
255 }
256
257 return err;
258}