Update.
[glibc.git] / sysdeps / mach / hurd / spawni.c
blob73b4d3507c892638bbda59f6bf83917b8674dc8c
1 /* spawn a new process running an executable. Hurd version.
2 Copyright (C) 2001,02 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <paths.h>
23 #include <spawn.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <hurd.h>
28 #include <hurd/signal.h>
29 #include <hurd/fd.h>
30 #include <hurd/id.h>
31 #include <hurd/lookup.h>
32 #include <hurd/resource.h>
33 #include <assert.h>
34 #include <argz.h>
35 #include "spawn_int.h"
37 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
38 Before running the process perform the actions described in FILE-ACTIONS. */
39 int
40 __spawni (pid_t *pid, const char *file,
41 const posix_spawn_file_actions_t *file_actions,
42 const posix_spawnattr_t *attrp,
43 char *const argv[], char *const envp[],
44 int use_path)
46 pid_t new_pid;
47 char *path, *p, *name;
48 size_t len;
49 size_t pathlen;
50 short int flags;
52 /* The generic POSIX.1 implementation of posix_spawn uses fork and exec.
53 In traditional POSIX systems (Unix, Linux, etc), the only way to
54 create a new process is by fork, which also copies all the things from
55 the parent process that will be immediately wiped and replaced by the
56 exec.
58 This Hurd implementation works by doing an exec on a fresh task,
59 without ever doing all the work of fork. The only work done by fork
60 that remains visible after an exec is registration with the proc
61 server, and the inheritance of various values and ports. All those
62 inherited values and ports are what get collected up and passed in the
63 file_exec RPC by an exec call. So we do the proc server registration
64 here, following the model of fork (see fork.c). We then collect up
65 the inherited values and ports from this (parent) process following
66 the model of exec (see hurd/hurdexec.c), modify or replace each value
67 that fork would (plus the specific changes demanded by ATTRP and
68 FILE_ACTIONS), and make the file_exec RPC on the requested executable
69 file with the child process's task port rather than our own. This
70 should be indistinguishable from the fork + exec implementation,
71 except that all errors will be detected here (in the parent process)
72 and return proper errno codes rather than the child dying with 127.
74 XXX The one exception to this supposed indistinguishableness is that
75 when posix_spawn_file_actions_addopen has been used, the parent
76 process can do various filesystem RPCs on the child's behalf, rather
77 than the child process doing it. If these block due to a broken or
78 malicious filesystem server or just a blocked network fs or a serial
79 port waiting for carrier detect (!!), the parent's posix_spawn call
80 can block arbitrarily rather than just the child blocking. Possible
81 solutions include:
82 * punt to plain fork + exec implementation if addopen was used
83 ** easy to do
84 ** gives up all benefits of this implementation in that case
85 * if addopen was used, don't do any file actions at all here;
86 instead, exec an installed helper program e.g.:
87 /libexec/spawn-helper close 3 dup2 1 2 open 0 /file 0x123 0666 exec /bin/foo foo a1 a2
88 ** extra exec might be more or less overhead than fork
89 * could do some weird half-fork thing where the child would inherit
90 our vm and run some code here, but not do the full work of fork
92 XXX Actually, the parent opens the executable file on behalf of
93 the child, and that has all the same issues.
95 I am favoring the half-fork solution. That is, we do task_create with
96 vm inheritance, and we setjmp/longjmp the child like fork does. But
97 rather than all the fork hair, the parent just packs up init/dtable
98 ports and does a single IPC to a receive right inserted in the child. */
100 error_t err;
101 task_t task;
102 file_t execfile;
103 process_t proc;
104 auth_t auth;
105 int ints[INIT_INT_MAX];
106 file_t *dtable;
107 unsigned int dtablesize, orig_dtablesize, i;
108 struct hurd_port **dtable_cells;
109 char *dtable_cloexec;
110 struct hurd_userlink *ulink_dtable = NULL;
111 struct hurd_sigstate *ss;
113 /* For POSIX_SPAWN_RESETIDS, this reauthenticates our root/current
114 directory ports with the new AUTH port. */
115 file_t rcrdir = MACH_PORT_NULL, rcwdir = MACH_PORT_NULL;
116 error_t reauthenticate (int which, file_t *result)
118 error_t err;
119 mach_port_t ref;
120 if (*result != MACH_PORT_NULL)
121 return 0;
122 ref = __mach_reply_port ();
123 err = HURD_PORT_USE
124 (&_hurd_ports[which],
126 err = __io_reauthenticate (port, ref, MACH_MSG_TYPE_MAKE_SEND);
127 if (!err)
128 err = __auth_user_authenticate (auth,
129 ref, MACH_MSG_TYPE_MAKE_SEND,
130 result);
131 err;
132 }));
133 __mach_port_destroy (__mach_task_self (), ref);
134 return err;
137 /* Reauthenticate one of our file descriptors for the child. A null
138 element of DTABLE_CELLS indicates a descriptor that was already
139 reauthenticated, or was newly opened on behalf of the child. */
140 error_t reauthenticate_fd (int fd)
142 if (dtable_cells[fd] != NULL)
144 file_t newfile;
145 mach_port_t ref = __mach_reply_port ();
146 error_t err = __io_reauthenticate (dtable[fd],
147 ref, MACH_MSG_TYPE_MAKE_SEND);
148 if (!err)
149 err = __auth_user_authenticate (auth,
150 ref, MACH_MSG_TYPE_MAKE_SEND,
151 &newfile);
152 __mach_port_destroy (__mach_task_self (), ref);
153 if (err)
154 return err;
155 _hurd_port_free (dtable_cells[fd], &ulink_dtable[fd], dtable[fd]);
156 dtable_cells[fd] = NULL;
157 dtable[fd] = newfile;
159 return 0;
162 /* These callbacks are for looking up file names on behalf of the child. */
163 error_t child_init_port (int which, error_t (*operate) (mach_port_t))
165 if (flags & POSIX_SPAWN_RESETIDS)
166 switch (which)
168 case INIT_PORT_AUTH:
169 return (*operate) (auth);
170 case INIT_PORT_CRDIR:
171 return (reauthenticate (INIT_PORT_CRDIR, &rcrdir)
172 ?: (*operate) (rcrdir));
173 case INIT_PORT_CWDIR:
174 return (reauthenticate (INIT_PORT_CWDIR, &rcwdir)
175 ?: (*operate) (rcwdir));
177 assert (which != INIT_PORT_PROC);
178 return _hurd_ports_use (which, operate);
180 file_t child_fd (int fd)
182 if ((unsigned int) fd < dtablesize && dtable[fd] != MACH_PORT_NULL)
184 if (flags & POSIX_SPAWN_RESETIDS)
186 /* Reauthenticate this descriptor right now,
187 since it is going to be used on behalf of the child. */
188 errno = reauthenticate_fd (fd);
189 if (errno)
190 return MACH_PORT_NULL;
192 __mach_port_mod_refs (__mach_task_self (), dtable[fd],
193 MACH_PORT_RIGHT_SEND, +1);
194 return dtable[fd];
196 errno = EBADF;
197 return MACH_PORT_NULL;
199 inline error_t child_lookup (const char *file, int oflag, mode_t mode,
200 file_t *result)
202 return __hurd_file_name_lookup (&child_init_port, &child_fd, 0,
203 file, oflag, mode, result);
207 /* Do this once. */
208 flags = attrp == NULL ? 0 : attrp->__flags;
210 /* Generate the new process. We create a task that does not inherit our
211 memory, and then register it as our child like fork does. See fork.c
212 for comments about the sequencing of these proc operations. */
214 err = __task_create (__mach_task_self (),
215 #ifdef KERN_INVALID_LEDGER
216 NULL, 0, /* OSF Mach */
217 #endif
218 0, &task);
219 if (err)
220 return __hurd_fail (err);
221 // From here down we must deallocate TASK and PROC before returning.
222 proc = MACH_PORT_NULL;
223 auth = MACH_PORT_NULL;
224 err = __USEPORT (PROC, __proc_task2pid (port, task, &new_pid));
225 if (!err)
226 err = __USEPORT (PROC, __proc_task2proc (port, task, &proc));
227 if (!err)
228 err = __USEPORT (PROC, __proc_child (port, task));
229 if (err)
230 goto out;
232 /* Load up the ints to give the new program. */
233 memset (ints, 0, sizeof ints);
234 ints[INIT_UMASK] = _hurd_umask;
235 ints[INIT_TRACEMASK] = _hurdsig_traced;
237 ss = _hurd_self_sigstate ();
239 assert (! __spin_lock_locked (&ss->critical_section_lock));
240 __spin_lock (&ss->critical_section_lock);
242 __spin_lock (&ss->lock);
243 ints[INIT_SIGMASK] = ss->blocked;
244 ints[INIT_SIGPENDING] = ss->pending;
245 ints[INIT_SIGIGN] = 0;
246 /* Unless we were asked to reset all handlers to SIG_DFL,
247 pass down the set of signals that were set to SIG_IGN. */
248 if ((flags & POSIX_SPAWN_SETSIGDEF) == 0)
249 for (i = 1; i < NSIG; ++i)
250 if (ss->actions[i].sa_handler == SIG_IGN)
251 ints[INIT_SIGIGN] |= __sigmask (i);
253 /* We hold the sigstate lock until the exec has failed so that no signal
254 can arrive between when we pack the blocked and ignored signals, and
255 when the exec actually happens. A signal handler could change what
256 signals are blocked and ignored. Either the change will be reflected
257 in the exec, or the signal will never be delivered. Setting the
258 critical section flag avoids anything we call trying to acquire the
259 sigstate lock. */
261 __spin_unlock (&ss->lock);
263 /* Set signal mask. */
264 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0)
265 ints[INIT_SIGMASK] = attrp->__ss;
267 #ifdef _POSIX_PRIORITY_SCHEDULING
268 /* Set the scheduling algorithm and parameters. */
269 # error implement me
270 if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
271 == POSIX_SPAWN_SETSCHEDPARAM)
273 if (__sched_setparam (0, &attrp->__sp) == -1)
274 _exit (SPAWN_ERROR);
276 else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0)
278 if (__sched_setscheduler (0, attrp->__policy,
279 (flags & POSIX_SPAWN_SETSCHEDPARAM) != 0
280 ? &attrp->__sp : NULL) == -1)
281 _exit (SPAWN_ERROR);
283 #endif
285 /* Set the process group ID. */
286 if (!err && (flags & POSIX_SPAWN_SETPGROUP) != 0)
287 err = __proc_setpgrp (proc, new_pid, attrp->__pgrp);
289 /* Set the effective user and group IDs. */
290 if (!err && (flags & POSIX_SPAWN_RESETIDS) != 0)
292 /* We need a different auth port for the child. */
294 __mutex_lock (&_hurd_id.lock);
295 err = _hurd_check_ids (); /* Get _hurd_id up to date. */
296 if (!err && _hurd_id.rid_auth == MACH_PORT_NULL)
298 /* Set up _hurd_id.rid_auth. This is a special auth server port
299 which uses the real uid and gid (the first aux uid and gid) as
300 the only effective uid and gid. */
302 if (_hurd_id.aux.nuids < 1 || _hurd_id.aux.ngids < 1)
303 /* We do not have a real UID and GID. Lose, lose, lose! */
304 err = EGRATUITOUS;
306 /* Create a new auth port using our real UID and GID (the first
307 auxiliary UID and GID) as the only effective IDs. */
308 if (!err)
309 err = __USEPORT (AUTH,
310 __auth_makeauth (port,
311 NULL, MACH_MSG_TYPE_COPY_SEND, 0,
312 _hurd_id.aux.uids, 1,
313 _hurd_id.aux.uids,
314 _hurd_id.aux.nuids,
315 _hurd_id.aux.gids, 1,
316 _hurd_id.aux.gids,
317 _hurd_id.aux.ngids,
318 &_hurd_id.rid_auth));
320 if (!err)
322 /* Use the real-ID auth port in place of the normal one. */
323 assert (_hurd_id.rid_auth != MACH_PORT_NULL);
324 auth = _hurd_id.rid_auth;
325 __mach_port_mod_refs (__mach_task_self (), auth,
326 MACH_PORT_RIGHT_SEND, +1);
328 __mutex_unlock (&_hurd_id.lock);
330 else
331 /* Copy our existing auth port. */
332 err = __USEPORT (AUTH, __mach_port_mod_refs (__mach_task_self (),
333 (auth = port),
334 MACH_PORT_RIGHT_SEND, +1));
336 if (err)
337 goto out;
339 /* Pack up the descriptor table to give the new program.
340 These descriptors will need to be reauthenticated below
341 if POSIX_SPAWN_RESETIDS is set. */
342 __mutex_lock (&_hurd_dtable_lock);
343 dtablesize = _hurd_dtablesize;
344 orig_dtablesize = _hurd_dtablesize;
345 dtable = __alloca (dtablesize * sizeof (dtable[0]));
346 ulink_dtable = __alloca (dtablesize * sizeof (ulink_dtable[0]));
347 dtable_cells = __alloca (dtablesize * sizeof (dtable_cells[0]));
348 dtable_cloexec = __alloca (dtablesize);
349 for (i = 0; i < dtablesize; ++i)
351 struct hurd_fd *const d = _hurd_dtable[i];
352 if (d == NULL)
354 dtable[i] = MACH_PORT_NULL;
355 dtable_cells[i] = NULL;
356 continue;
358 /* Note that this might return MACH_PORT_NULL. */
359 dtable[i] = _hurd_port_get (&d->port, &ulink_dtable[i]);
360 dtable_cells[i] = &d->port;
361 dtable_cloexec[i] = (d->flags & FD_CLOEXEC) != 0;
363 __mutex_unlock (&_hurd_dtable_lock);
365 /* Safe to let signals happen now. */
366 _hurd_critical_section_unlock (ss);
368 /* Execute the file actions. */
369 if (file_actions != NULL)
370 for (i = 0; i < file_actions->__used; ++i)
372 /* Close a file descriptor in the child. */
373 error_t do_close (int fd)
375 if ((unsigned int)fd < dtablesize
376 && dtable[fd] != MACH_PORT_NULL)
378 if (dtable_cells[fd] == NULL)
379 __mach_port_deallocate (__mach_task_self (), dtable[fd]);
380 else
382 _hurd_port_free (dtable_cells[fd],
383 &ulink_dtable[fd], dtable[fd]);
385 dtable_cells[fd] = NULL;
386 dtable[fd] = MACH_PORT_NULL;
387 return 0;
389 return EBADF;
392 /* Make sure the dtable can hold NEWFD. */
393 #define EXPAND_DTABLE(newfd) \
394 ({ \
395 if ((unsigned int)newfd >= dtablesize \
396 && newfd < _hurd_rlimits[RLIMIT_OFILE].rlim_cur) \
398 /* We need to expand the dtable for the child. */ \
399 NEW_TABLE (dtable, newfd); \
400 NEW_TABLE (ulink_dtable, newfd); \
401 NEW_TABLE (dtable_cells, newfd); \
403 ((unsigned int)newfd < dtablesize ? 0 : EMFILE); \
405 #define NEW_TABLE(x, newfd) \
406 do { __typeof (x) new_##x = __alloca ((newfd + 1) * sizeof (x[0])); \
407 memcpy (new_##x, x, dtablesize * sizeof (x[0])); \
408 memset (&new_##x[dtablesize], 0, (newfd + 1 - dtablesize) * sizeof (x[0])); \
409 x = new_##x; } while (0)
411 struct __spawn_action *action = &file_actions->__actions[i];
413 switch (action->tag)
415 case spawn_do_close:
416 err = do_close (action->action.close_action.fd);
417 break;
419 case spawn_do_dup2:
420 if ((unsigned int)action->action.dup2_action.fd < dtablesize
421 && dtable[action->action.dup2_action.fd] != MACH_PORT_NULL)
423 const int fd = action->action.dup2_action.fd;
424 const int newfd = action->action.dup2_action.newfd;
425 // dup2 always clears any old FD_CLOEXEC flag on the new fd.
426 if (newfd < orig_dtablesize)
427 dtable_cloexec[newfd] = 0;
428 if (fd == newfd)
429 // Same is same as same was.
430 break;
431 err = EXPAND_DTABLE (newfd);
432 if (!err)
434 /* Close the old NEWFD and replace it with FD's
435 contents, which can be either an original
436 descriptor (DTABLE_CELLS[FD] != 0) or a new
437 right that we acquired in this function. */
438 do_close (newfd);
439 dtable_cells[newfd] = dtable_cells[fd];
440 if (dtable_cells[newfd] != NULL)
441 dtable[newfd] = _hurd_port_get (dtable_cells[newfd],
442 &ulink_dtable[newfd]);
443 else
445 dtable[newfd] = dtable[fd];
446 err = __mach_port_mod_refs (__mach_task_self (),
447 dtable[fd],
448 MACH_PORT_RIGHT_SEND, +1);
452 else
453 // The old FD specified was bogus.
454 err = EBADF;
455 break;
457 case spawn_do_open:
458 /* Open a file on behalf of the child.
460 XXX note that this can subject the parent to arbitrary
461 delays waiting for the files to open. I don't know what the
462 spec says about this. If it's not permissible, then this
463 whole forkless implementation is probably untenable. */
465 const int fd = action->action.open_action.fd;
467 do_close (fd);
468 if (fd < orig_dtablesize)
469 dtable_cloexec[fd] = 0;
470 err = EXPAND_DTABLE (fd);
471 if (err)
472 break;
474 err = child_lookup (action->action.open_action.path,
475 action->action.open_action.oflag,
476 action->action.open_action.mode,
477 &dtable[fd]);
478 dtable_cells[fd] = NULL;
479 break;
483 if (err)
484 goto out;
487 /* Only now can we perform FD_CLOEXEC. We had to leave the descriptors
488 unmolested for the file actions to use. Note that the DTABLE_CLOEXEC
489 array is never expanded by file actions, so it might now have fewer
490 than DTABLESIZE elements. */
491 for (i = 0; i < orig_dtablesize; ++i)
492 if (dtable[i] != MACH_PORT_NULL && dtable_cloexec[i])
494 assert (dtable_cells[i] != NULL);
495 _hurd_port_free (dtable_cells[i], &ulink_dtable[i], dtable[i]);
496 dtable[i] = MACH_PORT_NULL;
499 /* Prune trailing null ports from the descriptor table. */
500 while (dtablesize > 0 && dtable[dtablesize - 1] == MACH_PORT_NULL)
501 --dtablesize;
503 if (flags & POSIX_SPAWN_RESETIDS)
505 /* Reauthenticate all the child's ports with its new auth handle. */
507 mach_port_t ref;
508 process_t newproc;
510 /* Reauthenticate with the proc server. */
511 ref = __mach_reply_port ();
512 err = __proc_reauthenticate (proc, ref, MACH_MSG_TYPE_MAKE_SEND);
513 if (!err)
514 err = __auth_user_authenticate (auth,
515 ref, MACH_MSG_TYPE_MAKE_SEND,
516 &newproc);
517 __mach_port_destroy (__mach_task_self (), ref);
518 if (!err)
520 __mach_port_deallocate (__mach_task_self (), proc);
521 proc = newproc;
524 if (!err)
525 err = reauthenticate (INIT_PORT_CRDIR, &rcrdir);
526 if (!err)
527 err = reauthenticate (INIT_PORT_CWDIR, &rcwdir);
529 /* We must reauthenticate all the fds except those that came from
530 `spawn_do_open' file actions, which were opened using the child's
531 auth port to begin with. */
532 for (i = 0; !err && i < dtablesize; ++i)
533 err = reauthenticate_fd (i);
535 if (err)
536 goto out;
538 /* Now we are ready to open the executable file using the child's ports.
539 We do this after performing all the file actions so the order of
540 events is the same as for a fork, exec sequence. This affects things
541 like the meaning of a /dev/fd file name, as well as which error
542 conditions are diagnosed first and what side effects (file creation,
543 etc) can be observed before what errors. */
545 if (! use_path || strchr (file, '/') != NULL)
546 /* The FILE parameter is actually a path. */
547 err = child_lookup (file, O_EXEC, 0, &execfile);
548 else
550 /* We have to search for FILE on the path. */
551 path = getenv ("PATH");
552 if (path == NULL)
554 /* There is no `PATH' in the environment.
555 The default search path is the current directory
556 followed by the path `confstr' returns for `_CS_PATH'. */
557 len = confstr (_CS_PATH, (char *) NULL, 0);
558 path = (char *) __alloca (1 + len);
559 path[0] = ':';
560 (void) confstr (_CS_PATH, path + 1, len);
563 len = strlen (file) + 1;
564 pathlen = strlen (path);
565 name = __alloca (pathlen + len + 1);
566 /* Copy the file name at the top. */
567 name = (char *) memcpy (name + pathlen + 1, file, len);
568 /* And add the slash. */
569 *--name = '/';
571 p = path;
574 char *startp;
576 path = p;
577 p = __strchrnul (path, ':');
579 if (p == path)
580 /* Two adjacent colons, or a colon at the beginning or the end
581 of `PATH' means to search the current directory. */
582 startp = name + 1;
583 else
584 startp = (char *) memcpy (name - (p - path), path, p - path);
586 /* Try to open this file name. */
587 err = child_lookup (startp, O_EXEC, 0, &execfile);
588 switch (err)
590 case EACCES:
591 case ENOENT:
592 case ESTALE:
593 case ENOTDIR:
594 /* Those errors indicate the file is missing or not executable
595 v by us, in which case we want to just try the next path
596 directory. */
597 continue;
599 case 0: /* Success! */
600 default:
601 /* Some other error means we found an executable file, but
602 something went wrong executing it; return the error to our
603 caller. */
604 break;
607 // We only get here when we are done looking for the file.
608 break;
610 while (*p++ != '\0');
612 if (err)
613 goto out;
615 /* Almost there! */
617 mach_port_t ports[_hurd_nports];
618 struct hurd_userlink ulink_ports[_hurd_nports];
619 char *args = NULL, *env = NULL;
620 size_t argslen = 0, envlen = 0;
622 inline error_t exec (file_t file)
624 return __file_exec (file, task,
625 (__sigismember (&_hurdsig_traced, SIGKILL)
626 ? EXEC_SIGTRAP : 0),
627 args, argslen, env, envlen,
628 dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize,
629 ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports,
630 ints, INIT_INT_MAX,
631 NULL, 0, NULL, 0);
634 /* Now we are out of things that can fail before the file_exec RPC,
635 for which everything else must be prepared. The only thing left
636 to do is packing up the argument and environment strings,
637 and the array of init ports. */
639 if (argv != NULL)
640 err = __argz_create (argv, &args, &argslen);
641 if (!err && envp != NULL)
642 err = __argz_create (envp, &env, &envlen);
644 /* Load up the ports to give to the new program.
645 Note the loop/switch below must parallel exactly to release refs. */
646 for (i = 0; i < _hurd_nports; ++i)
648 switch (i)
650 case INIT_PORT_AUTH:
651 ports[i] = auth;
652 continue;
653 case INIT_PORT_PROC:
654 ports[i] = proc;
655 continue;
656 case INIT_PORT_CRDIR:
657 if (flags & POSIX_SPAWN_RESETIDS)
659 ports[i] = rcrdir;
660 continue;
662 break;
663 case INIT_PORT_CWDIR:
664 if (flags & POSIX_SPAWN_RESETIDS)
666 ports[i] = rcwdir;
667 continue;
669 break;
671 ports[i] = _hurd_port_get (&_hurd_ports[i], &ulink_ports[i]);
674 /* Finally, try executing the file we opened. */
675 if (!err)
676 err = exec (execfile);
677 __mach_port_deallocate (__mach_task_self (), execfile);
679 if (err == ENOEXEC)
681 /* The file is accessible but it is not an executable file.
682 Invoke the shell to interpret it as a script. */
683 err = __argz_insert (&args, &argslen, args, _PATH_BSHELL);
684 if (!err)
685 err = child_lookup (_PATH_BSHELL, O_EXEC, 0, &execfile);
686 if (!err)
688 err = exec (execfile);
689 __mach_port_deallocate (__mach_task_self (), execfile);
693 /* Release the references just packed up in PORTS.
694 This switch must always parallel the one above that fills PORTS. */
695 for (i = 0; i < _hurd_nports; ++i)
697 switch (i)
699 case INIT_PORT_AUTH:
700 case INIT_PORT_PROC:
701 continue;
702 case INIT_PORT_CRDIR:
703 if (flags & POSIX_SPAWN_RESETIDS)
704 continue;
705 break;
706 case INIT_PORT_CWDIR:
707 if (flags & POSIX_SPAWN_RESETIDS)
708 continue;
709 break;
711 _hurd_port_free (&_hurd_ports[i], &ulink_ports[i], ports[i]);
714 free (args);
715 free (env);
718 /* We did it! We have a child! */
719 if (pid != NULL)
720 *pid = new_pid;
722 out:
723 /* Clean up all the references we are now holding. */
725 if (task != MACH_PORT_NULL)
727 if (err)
728 /* We failed after creating the task, so kill it. */
729 __task_terminate (task);
730 __mach_port_deallocate (__mach_task_self (), task);
732 __mach_port_deallocate (__mach_task_self (), auth);
733 __mach_port_deallocate (__mach_task_self (), proc);
734 if (rcrdir != MACH_PORT_NULL)
735 __mach_port_deallocate (__mach_task_self (), rcrdir);
736 if (rcwdir != MACH_PORT_NULL)
737 __mach_port_deallocate (__mach_task_self (), rcwdir);
739 if (ulink_dtable)
740 /* Release references to the file descriptor ports. */
741 for (i = 0; i < dtablesize; ++i)
742 if (dtable[i] != MACH_PORT_NULL)
744 if (dtable_cells[i] == NULL)
745 __mach_port_deallocate (__mach_task_self (), dtable[i]);
746 else
747 _hurd_port_free (dtable_cells[i], &ulink_dtable[i], dtable[i]);
750 if (err)
751 /* This hack canonicalizes the error code that we return. */
752 err = (__hurd_fail (err), errno);
754 return err;