*** empty log message ***
[glibc.git] / hurd / hurdexec.c
blobe18107b9a75d083288b291780a6a462aec5a5385
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 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 <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>
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. */
32 error_t
33 _hurd_exec (task_t task, file_t file,
34 char *const argv[], char *const envp[])
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;
43 unsigned int dtablesize, i;
44 struct hurd_port **dtable_cells;
45 struct hurd_userlink *ulink_dtable;
46 char *const *p;
47 struct hurd_sigstate *ss;
48 mach_port_t *please_dealloc, *pdp;
51 /* Pack the arguments into an array with nulls separating the elements. */
52 argslen = 0;
53 if (argv != NULL)
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);
63 else
64 args = NULL;
66 /* Pack the environment into an array with nulls separating elements. */
67 envlen = 0;
68 if (envp != NULL)
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);
78 else
79 env = NULL;
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 ())
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])))
89 while (--i > 0)
90 _hurd_port_free (&_hurd_ports[i], &ulink_ports[i], ports[i]);
91 return err;
94 else
95 ports[i] = _hurd_port_get (&_hurd_ports[i], &ulink_ports[i]);
98 /* Load up the ints to give the new program. */
99 for (i = 0; i < INIT_INT_MAX; ++i)
100 switch (i)
102 case INIT_UMASK:
103 ints[i] = _hurd_umask;
104 break;
106 case INIT_SIGMASK:
107 case INIT_SIGIGN:
108 case INIT_SIGPENDING:
109 /* We will set these all below. */
110 break;
112 default:
113 ints[i] = 0;
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);
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. */
133 ss->critical_section = 1;
134 __spin_unlock (&ss->lock);
136 /* Pack up the descriptor table to give the new program. */
137 __mutex_lock (&_hurd_dtable_lock);
139 dtablesize = _hurd_dtable ? _hurd_dtablesize : _hurd_init_dtablesize;
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;
154 if (_hurd_dtable != NULL)
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)
161 struct hurd_fd *const d = _hurd_dtable[i];
162 if (d == NULL)
164 dtable[i] = MACH_PORT_NULL;
165 continue;
167 __spin_lock (&d->port.lock);
168 if (d->flags & FD_CLOEXEC)
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)
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;
180 __spin_unlock (&d->port.lock);
182 else
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;
195 else
197 dtable = _hurd_init_dtable;
198 ulink_dtable = NULL;
199 dtable_cells = NULL;
202 /* The information is all set up now. Try to exec the file. */
205 if (pdp)
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. */
214 for (i = 0; i < _hurd_nports; ++i)
215 *pdp++ = ports[i];
216 for (i = 0; i < dtablesize; ++i)
217 *pdp++ = dtable[i];
220 err = __file_exec (file, task,
221 _hurd_exec_flags & EXEC_INHERITED,
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,
227 NULL, 0);
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]);
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]);
243 /* Release lock on the file descriptor table. */
244 __mutex_unlock (&_hurd_dtable_lock);
246 /* Safe to let signals happen now. */
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 ());
257 return err;