1 /* spawn a new process running an executable. Hurd version.
2 Copyright (C) 2001-2019 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/>. */
28 #include <hurd/signal.h>
31 #include <hurd/lookup.h>
32 #include <hurd/resource.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. */
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
[],
47 char *path
, *p
, *name
;
48 char *concat_name
= NULL
;
49 const char *relpath
, *abspath
;
55 /* The generic POSIX.1 implementation of posix_spawn uses fork and exec.
56 In traditional POSIX systems (Unix, Linux, etc), the only way to
57 create a new process is by fork, which also copies all the things from
58 the parent process that will be immediately wiped and replaced by the
61 This Hurd implementation works by doing an exec on a fresh task,
62 without ever doing all the work of fork. The only work done by fork
63 that remains visible after an exec is registration with the proc
64 server, and the inheritance of various values and ports. All those
65 inherited values and ports are what get collected up and passed in the
66 file_exec_paths RPC by an exec call. So we do the proc server
67 registration here, following the model of fork (see fork.c). We then
68 collect up the inherited values and ports from this (parent) process
69 following the model of exec (see hurd/hurdexec.c), modify or replace each
70 value that fork would (plus the specific changes demanded by ATTRP and
71 FILE_ACTIONS), and make the file_exec_paths RPC on the requested
72 executable file with the child process's task port rather than our own.
73 This should be indistinguishable from the fork + exec implementation,
74 except that all errors will be detected here (in the parent process)
75 and return proper errno codes rather than the child dying with 127.
77 XXX The one exception to this supposed indistinguishableness is that
78 when posix_spawn_file_actions_addopen has been used, the parent
79 process can do various filesystem RPCs on the child's behalf, rather
80 than the child process doing it. If these block due to a broken or
81 malicious filesystem server or just a blocked network fs or a serial
82 port waiting for carrier detect (!!), the parent's posix_spawn call
83 can block arbitrarily rather than just the child blocking. Possible
85 * punt to plain fork + exec implementation if addopen was used
87 ** gives up all benefits of this implementation in that case
88 * if addopen was used, don't do any file actions at all here;
89 instead, exec an installed helper program e.g.:
90 /libexec/spawn-helper close 3 dup2 1 2 open 0 /file 0x123 0666 exec /bin/foo foo a1 a2
91 ** extra exec might be more or less overhead than fork
92 * could do some weird half-fork thing where the child would inherit
93 our vm and run some code here, but not do the full work of fork
95 XXX Actually, the parent opens the executable file on behalf of
96 the child, and that has all the same issues.
98 I am favoring the half-fork solution. That is, we do task_create with
99 vm inheritance, and we setjmp/longjmp the child like fork does. But
100 rather than all the fork hair, the parent just packs up init/dtable
101 ports and does a single IPC to a receive right inserted in the child. */
108 int ints
[INIT_INT_MAX
];
110 unsigned int dtablesize
, orig_dtablesize
, i
;
111 struct hurd_port
**dtable_cells
;
112 char *dtable_cloexec
;
113 struct hurd_userlink
*ulink_dtable
= NULL
;
114 struct hurd_sigstate
*ss
;
116 /* Child current working dir */
117 file_t ccwdir
= MACH_PORT_NULL
;
119 /* For POSIX_SPAWN_RESETIDS, this reauthenticates our root/current
120 directory ports with the new AUTH port. */
121 file_t rcrdir
= MACH_PORT_NULL
, rcwdir
= MACH_PORT_NULL
;
122 error_t
reauthenticate (int which
, file_t
*result
)
126 if (*result
!= MACH_PORT_NULL
)
128 ref
= __mach_reply_port ();
129 if (which
== INIT_PORT_CWDIR
&& ccwdir
!= MACH_PORT_NULL
)
131 err
= __io_reauthenticate (ccwdir
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
133 err
= __auth_user_authenticate (auth
,
134 ref
, MACH_MSG_TYPE_MAKE_SEND
,
139 (&_hurd_ports
[which
],
141 err
= __io_reauthenticate (port
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
143 err
= __auth_user_authenticate (auth
,
144 ref
, MACH_MSG_TYPE_MAKE_SEND
,
148 __mach_port_destroy (__mach_task_self (), ref
);
152 /* Reauthenticate one of our file descriptors for the child. A null
153 element of DTABLE_CELLS indicates a descriptor that was already
154 reauthenticated, or was newly opened on behalf of the child. */
155 error_t
reauthenticate_fd (int fd
)
157 if (dtable_cells
[fd
] != NULL
)
160 mach_port_t ref
= __mach_reply_port ();
161 error_t err
= __io_reauthenticate (dtable
[fd
],
162 ref
, MACH_MSG_TYPE_MAKE_SEND
);
164 err
= __auth_user_authenticate (auth
,
165 ref
, MACH_MSG_TYPE_MAKE_SEND
,
167 __mach_port_destroy (__mach_task_self (), ref
);
170 _hurd_port_free (dtable_cells
[fd
], &ulink_dtable
[fd
], dtable
[fd
]);
171 dtable_cells
[fd
] = NULL
;
172 dtable
[fd
] = newfile
;
177 /* These callbacks are for looking up file names on behalf of the child. */
178 error_t
child_init_port (int which
, error_t (*operate
) (mach_port_t
))
180 if (flags
& POSIX_SPAWN_RESETIDS
)
184 return (*operate
) (auth
);
185 case INIT_PORT_CRDIR
:
186 return (reauthenticate (INIT_PORT_CRDIR
, &rcrdir
)
187 ?: (*operate
) (rcrdir
));
188 case INIT_PORT_CWDIR
:
189 return (reauthenticate (INIT_PORT_CWDIR
, &rcwdir
)
190 ?: (*operate
) (rcwdir
));
195 case INIT_PORT_CWDIR
:
196 if (ccwdir
!= MACH_PORT_NULL
)
197 return (*operate
) (ccwdir
);
200 assert (which
!= INIT_PORT_PROC
);
201 return _hurd_ports_use (which
, operate
);
203 file_t
child_fd (int fd
)
205 if ((unsigned int) fd
< dtablesize
&& dtable
[fd
] != MACH_PORT_NULL
)
207 if (flags
& POSIX_SPAWN_RESETIDS
)
209 /* Reauthenticate this descriptor right now,
210 since it is going to be used on behalf of the child. */
211 errno
= reauthenticate_fd (fd
);
213 return MACH_PORT_NULL
;
215 __mach_port_mod_refs (__mach_task_self (), dtable
[fd
],
216 MACH_PORT_RIGHT_SEND
, +1);
220 return MACH_PORT_NULL
;
222 inline error_t
child_lookup (const char *file
, int oflag
, mode_t mode
,
225 return __hurd_file_name_lookup (&child_init_port
, &child_fd
, 0,
226 file
, oflag
, mode
, result
);
228 auto error_t
child_chdir (const char *name
)
232 /* Append trailing "/." to directory name to force ENOTDIR if
233 it's not a directory and EACCES if we don't have search
236 const char *lookup
= name
;
237 if (len
>= 2 && name
[len
- 2] == '/' && name
[len
- 1] == '.')
240 /* Special-case empty file name according to POSIX. */
241 return __hurd_fail (ENOENT
);
244 char *n
= alloca (len
+ 3);
245 memcpy (n
, name
, len
);
252 error_t err
= child_lookup (lookup
, 0, 0, &new_ccwdir
);
255 if (ccwdir
!= MACH_PORT_NULL
)
256 __mach_port_deallocate (__mach_task_self (), ccwdir
);
262 inline error_t
child_lookup_under (file_t startdir
, const char *file
,
263 int oflag
, mode_t mode
, file_t
*result
)
265 error_t
use_init_port (int which
, error_t (*operate
) (mach_port_t
))
267 return (which
== INIT_PORT_CWDIR
? (*operate
) (startdir
) :
268 child_init_port (which
, operate
));
271 return __hurd_file_name_lookup (&use_init_port
, &child_fd
, 0,
272 file
, oflag
, mode
, result
);
274 auto error_t
child_fchdir (int fd
)
279 if ((unsigned int)fd
>= dtablesize
280 || dtable
[fd
] == MACH_PORT_NULL
)
283 /* We look up "." to force ENOTDIR if it's not a directory and EACCES if
284 we don't have search permission. */
285 if (dtable_cells
[fd
] != NULL
)
286 err
= HURD_PORT_USE (dtable_cells
[fd
],
288 child_lookup_under (port
, ".", O_NOTRANS
, 0, &new_ccwdir
);
291 err
= child_lookup_under (dtable
[fd
], ".", O_NOTRANS
, 0, &new_ccwdir
);
295 if (ccwdir
!= MACH_PORT_NULL
)
296 __mach_port_deallocate (__mach_task_self (), ccwdir
);
305 flags
= attrp
== NULL
? 0 : attrp
->__flags
;
307 /* Generate the new process. We create a task that does not inherit our
308 memory, and then register it as our child like fork does. See fork.c
309 for comments about the sequencing of these proc operations. */
311 err
= __task_create (__mach_task_self (),
312 #ifdef KERN_INVALID_LEDGER
313 NULL
, 0, /* OSF Mach */
317 return __hurd_fail (err
);
318 // From here down we must deallocate TASK and PROC before returning.
319 proc
= MACH_PORT_NULL
;
320 auth
= MACH_PORT_NULL
;
321 err
= __USEPORT (PROC
, __proc_task2pid (port
, task
, &new_pid
));
323 err
= __USEPORT (PROC
, __proc_task2proc (port
, task
, &proc
));
325 err
= __USEPORT (PROC
, __proc_child (port
, task
));
329 /* Load up the ints to give the new program. */
330 memset (ints
, 0, sizeof ints
);
331 ints
[INIT_UMASK
] = _hurd_umask
;
332 ints
[INIT_TRACEMASK
] = _hurdsig_traced
;
334 ss
= _hurd_self_sigstate ();
336 assert (! __spin_lock_locked (&ss
->critical_section_lock
));
337 __spin_lock (&ss
->critical_section_lock
);
339 __spin_lock (&ss
->lock
);
340 ints
[INIT_SIGMASK
] = ss
->blocked
;
341 ints
[INIT_SIGPENDING
] = ss
->pending
;
342 ints
[INIT_SIGIGN
] = 0;
343 /* Unless we were asked to reset all handlers to SIG_DFL,
344 pass down the set of signals that were set to SIG_IGN. */
345 if ((flags
& POSIX_SPAWN_SETSIGDEF
) == 0)
346 for (i
= 1; i
< NSIG
; ++i
)
347 if (ss
->actions
[i
].sa_handler
== SIG_IGN
)
348 ints
[INIT_SIGIGN
] |= __sigmask (i
);
350 /* We hold the sigstate lock until the exec has failed so that no signal
351 can arrive between when we pack the blocked and ignored signals, and
352 when the exec actually happens. A signal handler could change what
353 signals are blocked and ignored. Either the change will be reflected
354 in the exec, or the signal will never be delivered. Setting the
355 critical section flag avoids anything we call trying to acquire the
358 __spin_unlock (&ss
->lock
);
360 /* Set signal mask. */
361 if ((flags
& POSIX_SPAWN_SETSIGMASK
) != 0)
362 ints
[INIT_SIGMASK
] = attrp
->__ss
;
364 #ifdef _POSIX_PRIORITY_SCHEDULING
365 /* Set the scheduling algorithm and parameters. */
367 if ((flags
& (POSIX_SPAWN_SETSCHEDPARAM
| POSIX_SPAWN_SETSCHEDULER
))
368 == POSIX_SPAWN_SETSCHEDPARAM
)
370 if (__sched_setparam (0, &attrp
->__sp
) == -1)
373 else if ((flags
& POSIX_SPAWN_SETSCHEDULER
) != 0)
375 if (__sched_setscheduler (0, attrp
->__policy
,
376 (flags
& POSIX_SPAWN_SETSCHEDPARAM
) != 0
377 ? &attrp
->__sp
: NULL
) == -1)
382 if (!err
&& (flags
& POSIX_SPAWN_SETSID
) != 0)
383 err
= __proc_setsid (proc
);
385 /* Set the process group ID. */
386 if (!err
&& (flags
& POSIX_SPAWN_SETPGROUP
) != 0)
387 err
= __proc_setpgrp (proc
, new_pid
, attrp
->__pgrp
);
389 /* Set the effective user and group IDs. */
390 if (!err
&& (flags
& POSIX_SPAWN_RESETIDS
) != 0)
392 /* We need a different auth port for the child. */
394 __mutex_lock (&_hurd_id
.lock
);
395 err
= _hurd_check_ids (); /* Get _hurd_id up to date. */
396 if (!err
&& _hurd_id
.rid_auth
== MACH_PORT_NULL
)
398 /* Set up _hurd_id.rid_auth. This is a special auth server port
399 which uses the real uid and gid (the first aux uid and gid) as
400 the only effective uid and gid. */
402 if (_hurd_id
.aux
.nuids
< 1 || _hurd_id
.aux
.ngids
< 1)
403 /* We do not have a real UID and GID. Lose, lose, lose! */
406 /* Create a new auth port using our real UID and GID (the first
407 auxiliary UID and GID) as the only effective IDs. */
409 err
= __USEPORT (AUTH
,
410 __auth_makeauth (port
,
411 NULL
, MACH_MSG_TYPE_COPY_SEND
, 0,
412 _hurd_id
.aux
.uids
, 1,
415 _hurd_id
.aux
.gids
, 1,
418 &_hurd_id
.rid_auth
));
422 /* Use the real-ID auth port in place of the normal one. */
423 assert (_hurd_id
.rid_auth
!= MACH_PORT_NULL
);
424 auth
= _hurd_id
.rid_auth
;
425 __mach_port_mod_refs (__mach_task_self (), auth
,
426 MACH_PORT_RIGHT_SEND
, +1);
428 __mutex_unlock (&_hurd_id
.lock
);
431 /* Copy our existing auth port. */
432 err
= __USEPORT (AUTH
, __mach_port_mod_refs (__mach_task_self (),
434 MACH_PORT_RIGHT_SEND
, +1));
439 /* Pack up the descriptor table to give the new program.
440 These descriptors will need to be reauthenticated below
441 if POSIX_SPAWN_RESETIDS is set. */
442 __mutex_lock (&_hurd_dtable_lock
);
443 dtablesize
= _hurd_dtablesize
;
444 orig_dtablesize
= _hurd_dtablesize
;
445 dtable
= __alloca (dtablesize
* sizeof (dtable
[0]));
446 ulink_dtable
= __alloca (dtablesize
* sizeof (ulink_dtable
[0]));
447 dtable_cells
= __alloca (dtablesize
* sizeof (dtable_cells
[0]));
448 dtable_cloexec
= __alloca (orig_dtablesize
);
449 for (i
= 0; i
< dtablesize
; ++i
)
451 struct hurd_fd
*const d
= _hurd_dtable
[i
];
454 dtable
[i
] = MACH_PORT_NULL
;
455 dtable_cells
[i
] = NULL
;
458 /* Note that this might return MACH_PORT_NULL. */
459 dtable
[i
] = _hurd_port_get (&d
->port
, &ulink_dtable
[i
]);
460 dtable_cells
[i
] = &d
->port
;
461 dtable_cloexec
[i
] = (d
->flags
& FD_CLOEXEC
) != 0;
463 __mutex_unlock (&_hurd_dtable_lock
);
465 /* Safe to let signals happen now. */
466 _hurd_critical_section_unlock (ss
);
468 /* Execute the file actions. */
469 if (file_actions
!= NULL
)
470 for (i
= 0; i
< file_actions
->__used
; ++i
)
472 /* Close a file descriptor in the child. */
473 error_t
do_close (int fd
)
475 if ((unsigned int)fd
< dtablesize
476 && dtable
[fd
] != MACH_PORT_NULL
)
478 if (dtable_cells
[fd
] == NULL
)
479 __mach_port_deallocate (__mach_task_self (), dtable
[fd
]);
482 _hurd_port_free (dtable_cells
[fd
],
483 &ulink_dtable
[fd
], dtable
[fd
]);
485 dtable_cells
[fd
] = NULL
;
486 dtable
[fd
] = MACH_PORT_NULL
;
492 /* Make sure the dtable can hold NEWFD. */
493 #define EXPAND_DTABLE(newfd) \
495 if ((unsigned int)newfd >= dtablesize \
496 && newfd < _hurd_rlimits[RLIMIT_OFILE].rlim_cur) \
498 /* We need to expand the dtable for the child. */ \
499 NEW_TABLE (dtable, newfd); \
500 NEW_ULINK_TABLE (ulink_dtable, newfd); \
501 NEW_TABLE (dtable_cells, newfd); \
502 dtablesize = newfd + 1; \
504 ((unsigned int)newfd < dtablesize ? 0 : EMFILE); \
506 #define NEW_TABLE(x, newfd) \
507 do { __typeof (x) new_##x = __alloca ((newfd + 1) * sizeof (x[0])); \
508 memcpy (new_##x, x, dtablesize * sizeof (x[0])); \
509 memset (&new_##x[dtablesize], 0, (newfd + 1 - dtablesize) * sizeof (x[0])); \
510 x = new_##x; } while (0)
511 #define NEW_ULINK_TABLE(x, newfd) \
512 do { __typeof (x) new_##x = __alloca ((newfd + 1) * sizeof (x[0])); \
514 for (i = 0; i < dtablesize; i++) \
515 if (dtable_cells[i] != NULL) \
516 _hurd_port_move (dtable_cells[i], &new_##x[i], &x[i]); \
518 memset(&new_##x[i], 0, sizeof(new_##x[i])); \
519 memset (&new_##x[dtablesize], 0, (newfd + 1 - dtablesize) * sizeof (x[0])); \
520 x = new_##x; } while (0)
522 struct __spawn_action
*action
= &file_actions
->__actions
[i
];
527 err
= do_close (action
->action
.close_action
.fd
);
531 if ((unsigned int)action
->action
.dup2_action
.fd
< dtablesize
532 && dtable
[action
->action
.dup2_action
.fd
] != MACH_PORT_NULL
)
534 const int fd
= action
->action
.dup2_action
.fd
;
535 const int newfd
= action
->action
.dup2_action
.newfd
;
536 // dup2 always clears any old FD_CLOEXEC flag on the new fd.
537 if (newfd
< orig_dtablesize
)
538 dtable_cloexec
[newfd
] = 0;
540 // Same is same as same was.
542 err
= EXPAND_DTABLE (newfd
);
545 /* Close the old NEWFD and replace it with FD's
546 contents, which can be either an original
547 descriptor (DTABLE_CELLS[FD] != 0) or a new
548 right that we acquired in this function. */
550 dtable_cells
[newfd
] = dtable_cells
[fd
];
551 if (dtable_cells
[newfd
] != NULL
)
552 dtable
[newfd
] = _hurd_port_get (dtable_cells
[newfd
],
553 &ulink_dtable
[newfd
]);
556 dtable
[newfd
] = dtable
[fd
];
557 err
= __mach_port_mod_refs (__mach_task_self (),
559 MACH_PORT_RIGHT_SEND
, +1);
564 // The old FD specified was bogus.
569 /* Open a file on behalf of the child.
571 XXX note that this can subject the parent to arbitrary
572 delays waiting for the files to open. I don't know what the
573 spec says about this. If it's not permissible, then this
574 whole forkless implementation is probably untenable. */
576 const int fd
= action
->action
.open_action
.fd
;
579 if (fd
< orig_dtablesize
)
580 dtable_cloexec
[fd
] = 0;
581 err
= EXPAND_DTABLE (fd
);
585 err
= child_lookup (action
->action
.open_action
.path
,
586 action
->action
.open_action
.oflag
,
587 action
->action
.open_action
.mode
,
589 dtable_cells
[fd
] = NULL
;
594 err
= child_chdir (action
->action
.chdir_action
.path
);
597 case spawn_do_fchdir
:
598 err
= child_fchdir (action
->action
.fchdir_action
.fd
);
606 /* Only now can we perform FD_CLOEXEC. We had to leave the descriptors
607 unmolested for the file actions to use. Note that the DTABLE_CLOEXEC
608 array is never expanded by file actions, so it might now have fewer
609 than DTABLESIZE elements. */
610 for (i
= 0; i
< orig_dtablesize
; ++i
)
611 if (dtable
[i
] != MACH_PORT_NULL
&& dtable_cloexec
[i
])
613 assert (dtable_cells
[i
] != NULL
);
614 _hurd_port_free (dtable_cells
[i
], &ulink_dtable
[i
], dtable
[i
]);
615 dtable
[i
] = MACH_PORT_NULL
;
618 /* Prune trailing null ports from the descriptor table. */
619 while (dtablesize
> 0 && dtable
[dtablesize
- 1] == MACH_PORT_NULL
)
622 if (flags
& POSIX_SPAWN_RESETIDS
)
624 /* Reauthenticate all the child's ports with its new auth handle. */
629 /* Reauthenticate with the proc server. */
630 ref
= __mach_reply_port ();
631 err
= __proc_reauthenticate (proc
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
633 err
= __auth_user_authenticate (auth
,
634 ref
, MACH_MSG_TYPE_MAKE_SEND
,
636 __mach_port_destroy (__mach_task_self (), ref
);
639 __mach_port_deallocate (__mach_task_self (), proc
);
644 err
= reauthenticate (INIT_PORT_CRDIR
, &rcrdir
);
646 err
= reauthenticate (INIT_PORT_CWDIR
, &rcwdir
);
648 /* We must reauthenticate all the fds except those that came from
649 `spawn_do_open' file actions, which were opened using the child's
650 auth port to begin with. */
651 for (i
= 0; !err
&& i
< dtablesize
; ++i
)
652 err
= reauthenticate_fd (i
);
657 /* Now we are ready to open the executable file using the child's ports.
658 We do this after performing all the file actions so the order of
659 events is the same as for a fork, exec sequence. This affects things
660 like the meaning of a /dev/fd file name, as well as which error
661 conditions are diagnosed first and what side effects (file creation,
662 etc) can be observed before what errors. */
664 if ((xflags
& SPAWN_XFLAGS_USE_PATH
) == 0 || strchr (file
, '/') != NULL
)
665 /* The FILE parameter is actually a path. */
666 err
= child_lookup (relpath
= file
, O_EXEC
, 0, &execfile
);
669 /* We have to search for FILE on the path. */
670 path
= getenv ("PATH");
673 /* There is no `PATH' in the environment.
674 The default search path is the current directory
675 followed by the path `confstr' returns for `_CS_PATH'. */
676 len
= __confstr (_CS_PATH
, (char *) NULL
, 0);
677 path
= (char *) __alloca (1 + len
);
679 (void) __confstr (_CS_PATH
, path
+ 1, len
);
682 len
= strlen (file
) + 1;
683 pathlen
= strlen (path
);
684 name
= __alloca (pathlen
+ len
+ 1);
685 /* Copy the file name at the top. */
686 name
= (char *) memcpy (name
+ pathlen
+ 1, file
, len
);
687 /* And add the slash. */
696 p
= __strchrnul (path
, ':');
699 /* Two adjacent colons, or a colon at the beginning or the end
700 of `PATH' means to search the current directory. */
703 startp
= (char *) memcpy (name
- (p
- path
), path
, p
- path
);
705 /* Try to open this file name. */
706 err
= child_lookup (startp
, O_EXEC
, 0, &execfile
);
713 /* Those errors indicate the file is missing or not executable
714 by us, in which case we want to just try the next path
718 case 0: /* Success! */
720 /* Some other error means we found an executable file, but
721 something went wrong executing it; return the error to our
726 // We only get here when we are done looking for the file.
730 while (*p
++ != '\0');
735 if (relpath
[0] == '/')
737 /* Already an absolute path */
743 char *cwd
= __getcwd (NULL
, 0);
747 res
= __asprintf (&concat_name
, "%s/%s", cwd
, relpath
);
752 abspath
= concat_name
;
757 mach_port_t ports
[_hurd_nports
];
758 struct hurd_userlink ulink_ports
[_hurd_nports
];
759 char *args
= NULL
, *env
= NULL
;
760 size_t argslen
= 0, envlen
= 0;
762 inline error_t
exec (file_t file
)
764 error_t err
= __file_exec_paths
766 __sigismember (&_hurdsig_traced
, SIGKILL
) ? EXEC_SIGTRAP
: 0,
767 relpath
, abspath
, args
, argslen
, env
, envlen
,
768 dtable
, MACH_MSG_TYPE_COPY_SEND
, dtablesize
,
769 ports
, MACH_MSG_TYPE_COPY_SEND
, _hurd_nports
,
773 /* Fallback for backwards compatibility. This can just be removed
774 when __file_exec goes away. */
775 if (err
== MIG_BAD_ID
)
776 return __file_exec (file
, task
,
777 (__sigismember (&_hurdsig_traced
, SIGKILL
)
779 args
, argslen
, env
, envlen
,
780 dtable
, MACH_MSG_TYPE_COPY_SEND
, dtablesize
,
781 ports
, MACH_MSG_TYPE_COPY_SEND
, _hurd_nports
,
788 /* Now we are out of things that can fail before the file_exec RPC,
789 for which everything else must be prepared. The only thing left
790 to do is packing up the argument and environment strings,
791 and the array of init ports. */
794 err
= __argz_create (argv
, &args
, &argslen
);
795 if (!err
&& envp
!= NULL
)
796 err
= __argz_create (envp
, &env
, &envlen
);
798 /* Load up the ports to give to the new program.
799 Note the loop/switch below must parallel exactly to release refs. */
800 for (i
= 0; i
< _hurd_nports
; ++i
)
810 case INIT_PORT_CRDIR
:
811 if (flags
& POSIX_SPAWN_RESETIDS
)
817 case INIT_PORT_CWDIR
:
818 if (flags
& POSIX_SPAWN_RESETIDS
)
823 if (ccwdir
!= MACH_PORT_NULL
)
830 ports
[i
] = _hurd_port_get (&_hurd_ports
[i
], &ulink_ports
[i
]);
833 /* Finally, try executing the file we opened. */
835 err
= exec (execfile
);
836 __mach_port_deallocate (__mach_task_self (), execfile
);
840 /* The file is accessible but it is not an executable file.
841 Invoke the shell to interpret it as a script. */
842 err
= __argz_insert (&args
, &argslen
, args
, _PATH_BSHELL
);
844 err
= child_lookup (_PATH_BSHELL
, O_EXEC
, 0, &execfile
);
847 err
= exec (execfile
);
848 __mach_port_deallocate (__mach_task_self (), execfile
);
852 /* Release the references just packed up in PORTS.
853 This switch must always parallel the one above that fills PORTS. */
854 for (i
= 0; i
< _hurd_nports
; ++i
)
861 case INIT_PORT_CRDIR
:
862 if (flags
& POSIX_SPAWN_RESETIDS
)
865 case INIT_PORT_CWDIR
:
866 if (flags
& POSIX_SPAWN_RESETIDS
)
868 if (ccwdir
!= MACH_PORT_NULL
)
872 _hurd_port_free (&_hurd_ports
[i
], &ulink_ports
[i
], ports
[i
]);
879 /* We did it! We have a child! */
884 /* Clean up all the references we are now holding. */
886 if (task
!= MACH_PORT_NULL
)
889 /* We failed after creating the task, so kill it. */
890 __task_terminate (task
);
891 __mach_port_deallocate (__mach_task_self (), task
);
893 __mach_port_deallocate (__mach_task_self (), auth
);
894 __mach_port_deallocate (__mach_task_self (), proc
);
895 if (ccwdir
!= MACH_PORT_NULL
)
896 __mach_port_deallocate (__mach_task_self (), ccwdir
);
897 if (rcrdir
!= MACH_PORT_NULL
)
898 __mach_port_deallocate (__mach_task_self (), rcrdir
);
899 if (rcwdir
!= MACH_PORT_NULL
)
900 __mach_port_deallocate (__mach_task_self (), rcwdir
);
903 /* Release references to the file descriptor ports. */
904 for (i
= 0; i
< dtablesize
; ++i
)
905 if (dtable
[i
] != MACH_PORT_NULL
)
907 if (dtable_cells
[i
] == NULL
)
908 __mach_port_deallocate (__mach_task_self (), dtable
[i
]);
910 _hurd_port_free (dtable_cells
[i
], &ulink_dtable
[i
], dtable
[i
]);
916 /* This hack canonicalizes the error code that we return. */
917 err
= (__hurd_fail (err
), errno
);