Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / mach / hurd / spawni.c
blobffc1cf68cfc7b186843e574baab6b1108bec44f4
1 /* spawn a new process running an executable. Hurd version.
2 Copyright (C) 2001-2015 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
17 not, see <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <paths.h>
22 #include <spawn.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <hurd.h>
27 #include <hurd/signal.h>
28 #include <hurd/fd.h>
29 #include <hurd/id.h>
30 #include <hurd/lookup.h>
31 #include <hurd/resource.h>
32 #include <assert.h>
33 #include <argz.h>
34 #include "spawn_int.h"
36 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
37 Before running the process perform the actions described in FILE-ACTIONS. */
38 int
39 __spawni (pid_t *pid, const char *file,
40 const posix_spawn_file_actions_t *file_actions,
41 const posix_spawnattr_t *attrp,
42 char *const argv[], char *const envp[],
43 int xflags)
45 pid_t new_pid;
46 char *path, *p, *name;
47 size_t len;
48 size_t pathlen;
49 short int flags;
51 /* The generic POSIX.1 implementation of posix_spawn uses fork and exec.
52 In traditional POSIX systems (Unix, Linux, etc), the only way to
53 create a new process is by fork, which also copies all the things from
54 the parent process that will be immediately wiped and replaced by the
55 exec.
57 This Hurd implementation works by doing an exec on a fresh task,
58 without ever doing all the work of fork. The only work done by fork
59 that remains visible after an exec is registration with the proc
60 server, and the inheritance of various values and ports. All those
61 inherited values and ports are what get collected up and passed in the
62 file_exec RPC by an exec call. So we do the proc server registration
63 here, following the model of fork (see fork.c). We then collect up
64 the inherited values and ports from this (parent) process following
65 the model of exec (see hurd/hurdexec.c), modify or replace each value
66 that fork would (plus the specific changes demanded by ATTRP and
67 FILE_ACTIONS), and make the file_exec RPC on the requested executable
68 file with the child process's task port rather than our own. This
69 should be indistinguishable from the fork + exec implementation,
70 except that all errors will be detected here (in the parent process)
71 and return proper errno codes rather than the child dying with 127.
73 XXX The one exception to this supposed indistinguishableness is that
74 when posix_spawn_file_actions_addopen has been used, the parent
75 process can do various filesystem RPCs on the child's behalf, rather
76 than the child process doing it. If these block due to a broken or
77 malicious filesystem server or just a blocked network fs or a serial
78 port waiting for carrier detect (!!), the parent's posix_spawn call
79 can block arbitrarily rather than just the child blocking. Possible
80 solutions include:
81 * punt to plain fork + exec implementation if addopen was used
82 ** easy to do
83 ** gives up all benefits of this implementation in that case
84 * if addopen was used, don't do any file actions at all here;
85 instead, exec an installed helper program e.g.:
86 /libexec/spawn-helper close 3 dup2 1 2 open 0 /file 0x123 0666 exec /bin/foo foo a1 a2
87 ** extra exec might be more or less overhead than fork
88 * could do some weird half-fork thing where the child would inherit
89 our vm and run some code here, but not do the full work of fork
91 XXX Actually, the parent opens the executable file on behalf of
92 the child, and that has all the same issues.
94 I am favoring the half-fork solution. That is, we do task_create with
95 vm inheritance, and we setjmp/longjmp the child like fork does. But
96 rather than all the fork hair, the parent just packs up init/dtable
97 ports and does a single IPC to a receive right inserted in the child. */
99 error_t err;
100 task_t task;
101 file_t execfile;
102 process_t proc;
103 auth_t auth;
104 int ints[INIT_INT_MAX];
105 file_t *dtable;
106 unsigned int dtablesize, orig_dtablesize, i;
107 struct hurd_port **dtable_cells;
108 char *dtable_cloexec;
109 struct hurd_userlink *ulink_dtable = NULL;
110 struct hurd_sigstate *ss;
112 /* For POSIX_SPAWN_RESETIDS, this reauthenticates our root/current
113 directory ports with the new AUTH port. */
114 file_t rcrdir = MACH_PORT_NULL, rcwdir = MACH_PORT_NULL;
115 error_t reauthenticate (int which, file_t *result)
117 error_t err;
118 mach_port_t ref;
119 if (*result != MACH_PORT_NULL)
120 return 0;
121 ref = __mach_reply_port ();
122 err = HURD_PORT_USE
123 (&_hurd_ports[which],
125 err = __io_reauthenticate (port, ref, MACH_MSG_TYPE_MAKE_SEND);
126 if (!err)
127 err = __auth_user_authenticate (auth,
128 ref, MACH_MSG_TYPE_MAKE_SEND,
129 result);
130 err;
131 }));
132 __mach_port_destroy (__mach_task_self (), ref);
133 return err;
136 /* Reauthenticate one of our file descriptors for the child. A null
137 element of DTABLE_CELLS indicates a descriptor that was already
138 reauthenticated, or was newly opened on behalf of the child. */
139 error_t reauthenticate_fd (int fd)
141 if (dtable_cells[fd] != NULL)
143 file_t newfile;
144 mach_port_t ref = __mach_reply_port ();
145 error_t err = __io_reauthenticate (dtable[fd],
146 ref, MACH_MSG_TYPE_MAKE_SEND);
147 if (!err)
148 err = __auth_user_authenticate (auth,
149 ref, MACH_MSG_TYPE_MAKE_SEND,
150 &newfile);
151 __mach_port_destroy (__mach_task_self (), ref);
152 if (err)
153 return err;
154 _hurd_port_free (dtable_cells[fd], &ulink_dtable[fd], dtable[fd]);
155 dtable_cells[fd] = NULL;
156 dtable[fd] = newfile;
158 return 0;
161 /* These callbacks are for looking up file names on behalf of the child. */
162 error_t child_init_port (int which, error_t (*operate) (mach_port_t))
164 if (flags & POSIX_SPAWN_RESETIDS)
165 switch (which)
167 case INIT_PORT_AUTH:
168 return (*operate) (auth);
169 case INIT_PORT_CRDIR:
170 return (reauthenticate (INIT_PORT_CRDIR, &rcrdir)
171 ?: (*operate) (rcrdir));
172 case INIT_PORT_CWDIR:
173 return (reauthenticate (INIT_PORT_CWDIR, &rcwdir)
174 ?: (*operate) (rcwdir));
176 assert (which != INIT_PORT_PROC);
177 return _hurd_ports_use (which, operate);
179 file_t child_fd (int fd)
181 if ((unsigned int) fd < dtablesize && dtable[fd] != MACH_PORT_NULL)
183 if (flags & POSIX_SPAWN_RESETIDS)
185 /* Reauthenticate this descriptor right now,
186 since it is going to be used on behalf of the child. */
187 errno = reauthenticate_fd (fd);
188 if (errno)
189 return MACH_PORT_NULL;
191 __mach_port_mod_refs (__mach_task_self (), dtable[fd],
192 MACH_PORT_RIGHT_SEND, +1);
193 return dtable[fd];
195 errno = EBADF;
196 return MACH_PORT_NULL;
198 inline error_t child_lookup (const char *file, int oflag, mode_t mode,
199 file_t *result)
201 return __hurd_file_name_lookup (&child_init_port, &child_fd, 0,
202 file, oflag, mode, result);
206 /* Do this once. */
207 flags = attrp == NULL ? 0 : attrp->__flags;
209 /* Generate the new process. We create a task that does not inherit our
210 memory, and then register it as our child like fork does. See fork.c
211 for comments about the sequencing of these proc operations. */
213 err = __task_create (__mach_task_self (),
214 #ifdef KERN_INVALID_LEDGER
215 NULL, 0, /* OSF Mach */
216 #endif
217 0, &task);
218 if (err)
219 return __hurd_fail (err);
220 // From here down we must deallocate TASK and PROC before returning.
221 proc = MACH_PORT_NULL;
222 auth = MACH_PORT_NULL;
223 err = __USEPORT (PROC, __proc_task2pid (port, task, &new_pid));
224 if (!err)
225 err = __USEPORT (PROC, __proc_task2proc (port, task, &proc));
226 if (!err)
227 err = __USEPORT (PROC, __proc_child (port, task));
228 if (err)
229 goto out;
231 /* Load up the ints to give the new program. */
232 memset (ints, 0, sizeof ints);
233 ints[INIT_UMASK] = _hurd_umask;
234 ints[INIT_TRACEMASK] = _hurdsig_traced;
236 ss = _hurd_self_sigstate ();
238 assert (! __spin_lock_locked (&ss->critical_section_lock));
239 __spin_lock (&ss->critical_section_lock);
241 __spin_lock (&ss->lock);
242 ints[INIT_SIGMASK] = ss->blocked;
243 ints[INIT_SIGPENDING] = ss->pending;
244 ints[INIT_SIGIGN] = 0;
245 /* Unless we were asked to reset all handlers to SIG_DFL,
246 pass down the set of signals that were set to SIG_IGN. */
247 if ((flags & POSIX_SPAWN_SETSIGDEF) == 0)
248 for (i = 1; i < NSIG; ++i)
249 if (ss->actions[i].sa_handler == SIG_IGN)
250 ints[INIT_SIGIGN] |= __sigmask (i);
252 /* We hold the sigstate lock until the exec has failed so that no signal
253 can arrive between when we pack the blocked and ignored signals, and
254 when the exec actually happens. A signal handler could change what
255 signals are blocked and ignored. Either the change will be reflected
256 in the exec, or the signal will never be delivered. Setting the
257 critical section flag avoids anything we call trying to acquire the
258 sigstate lock. */
260 __spin_unlock (&ss->lock);
262 /* Set signal mask. */
263 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0)
264 ints[INIT_SIGMASK] = attrp->__ss;
266 #ifdef _POSIX_PRIORITY_SCHEDULING
267 /* Set the scheduling algorithm and parameters. */
268 # error implement me
269 if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
270 == POSIX_SPAWN_SETSCHEDPARAM)
272 if (__sched_setparam (0, &attrp->__sp) == -1)
273 _exit (SPAWN_ERROR);
275 else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0)
277 if (__sched_setscheduler (0, attrp->__policy,
278 (flags & POSIX_SPAWN_SETSCHEDPARAM) != 0
279 ? &attrp->__sp : NULL) == -1)
280 _exit (SPAWN_ERROR);
282 #endif
284 /* Set the process group ID. */
285 if (!err && (flags & POSIX_SPAWN_SETPGROUP) != 0)
286 err = __proc_setpgrp (proc, new_pid, attrp->__pgrp);
288 /* Set the effective user and group IDs. */
289 if (!err && (flags & POSIX_SPAWN_RESETIDS) != 0)
291 /* We need a different auth port for the child. */
293 __mutex_lock (&_hurd_id.lock);
294 err = _hurd_check_ids (); /* Get _hurd_id up to date. */
295 if (!err && _hurd_id.rid_auth == MACH_PORT_NULL)
297 /* Set up _hurd_id.rid_auth. This is a special auth server port
298 which uses the real uid and gid (the first aux uid and gid) as
299 the only effective uid and gid. */
301 if (_hurd_id.aux.nuids < 1 || _hurd_id.aux.ngids < 1)
302 /* We do not have a real UID and GID. Lose, lose, lose! */
303 err = EGRATUITOUS;
305 /* Create a new auth port using our real UID and GID (the first
306 auxiliary UID and GID) as the only effective IDs. */
307 if (!err)
308 err = __USEPORT (AUTH,
309 __auth_makeauth (port,
310 NULL, MACH_MSG_TYPE_COPY_SEND, 0,
311 _hurd_id.aux.uids, 1,
312 _hurd_id.aux.uids,
313 _hurd_id.aux.nuids,
314 _hurd_id.aux.gids, 1,
315 _hurd_id.aux.gids,
316 _hurd_id.aux.ngids,
317 &_hurd_id.rid_auth));
319 if (!err)
321 /* Use the real-ID auth port in place of the normal one. */
322 assert (_hurd_id.rid_auth != MACH_PORT_NULL);
323 auth = _hurd_id.rid_auth;
324 __mach_port_mod_refs (__mach_task_self (), auth,
325 MACH_PORT_RIGHT_SEND, +1);
327 __mutex_unlock (&_hurd_id.lock);
329 else
330 /* Copy our existing auth port. */
331 err = __USEPORT (AUTH, __mach_port_mod_refs (__mach_task_self (),
332 (auth = port),
333 MACH_PORT_RIGHT_SEND, +1));
335 if (err)
336 goto out;
338 /* Pack up the descriptor table to give the new program.
339 These descriptors will need to be reauthenticated below
340 if POSIX_SPAWN_RESETIDS is set. */
341 __mutex_lock (&_hurd_dtable_lock);
342 dtablesize = _hurd_dtablesize;
343 orig_dtablesize = _hurd_dtablesize;
344 dtable = __alloca (dtablesize * sizeof (dtable[0]));
345 ulink_dtable = __alloca (dtablesize * sizeof (ulink_dtable[0]));
346 dtable_cells = __alloca (dtablesize * sizeof (dtable_cells[0]));
347 dtable_cloexec = __alloca (dtablesize);
348 for (i = 0; i < dtablesize; ++i)
350 struct hurd_fd *const d = _hurd_dtable[i];
351 if (d == NULL)
353 dtable[i] = MACH_PORT_NULL;
354 dtable_cells[i] = NULL;
355 continue;
357 /* Note that this might return MACH_PORT_NULL. */
358 dtable[i] = _hurd_port_get (&d->port, &ulink_dtable[i]);
359 dtable_cells[i] = &d->port;
360 dtable_cloexec[i] = (d->flags & FD_CLOEXEC) != 0;
362 __mutex_unlock (&_hurd_dtable_lock);
364 /* Safe to let signals happen now. */
365 _hurd_critical_section_unlock (ss);
367 /* Execute the file actions. */
368 if (file_actions != NULL)
369 for (i = 0; i < file_actions->__used; ++i)
371 /* Close a file descriptor in the child. */
372 error_t do_close (int fd)
374 if ((unsigned int)fd < dtablesize
375 && dtable[fd] != MACH_PORT_NULL)
377 if (dtable_cells[fd] == NULL)
378 __mach_port_deallocate (__mach_task_self (), dtable[fd]);
379 else
381 _hurd_port_free (dtable_cells[fd],
382 &ulink_dtable[fd], dtable[fd]);
384 dtable_cells[fd] = NULL;
385 dtable[fd] = MACH_PORT_NULL;
386 return 0;
388 return EBADF;
391 /* Make sure the dtable can hold NEWFD. */
392 #define EXPAND_DTABLE(newfd) \
393 ({ \
394 if ((unsigned int)newfd >= dtablesize \
395 && newfd < _hurd_rlimits[RLIMIT_OFILE].rlim_cur) \
397 /* We need to expand the dtable for the child. */ \
398 NEW_TABLE (dtable, newfd); \
399 NEW_TABLE (ulink_dtable, newfd); \
400 NEW_TABLE (dtable_cells, newfd); \
401 dtablesize = newfd + 1; \
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 ((xflags & SPAWN_XFLAGS_USE_PATH) == 0 || 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 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;