Home | History | Annotate | Line # | Download | only in gen
      1 /*	$NetBSD: posix_spawnp.c,v 1.5 2024/11/11 06:49:31 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Martin Husemann <martin (at) NetBSD.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 __RCSID("$NetBSD: posix_spawnp.c,v 1.5 2024/11/11 06:49:31 martin Exp $");
     35 #endif /* LIBC_SCCS and not lint */
     36 
     37 #include "namespace.h"
     38 
     39 #include <assert.h>
     40 #include <errno.h>
     41 #include <paths.h>
     42 #include <spawn.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 
     49 int posix_spawnp(pid_t * __restrict pid, const char * __restrict file,
     50     const posix_spawn_file_actions_t *fa,
     51     const posix_spawnattr_t * __restrict sa,
     52     char * const *__restrict cav, char * const *__restrict env)
     53 {
     54 	char fpath[FILENAME_MAX];
     55 	const char *path, *p;
     56 	size_t lp, ln;
     57 	int err;
     58 
     59 	_DIAGASSERT(file != NULL);
     60 
     61 	/* "" is not a valid filename; check this before traversing PATH. */
     62 	if (file[0] == '\0')
     63 		return ENOENT;
     64 
     65 	/*
     66 	 * If there is a / in the name, fall straight through to posix_spawn().
     67 	 */
     68 	if (strchr(file, '/') != NULL)
     69 		return posix_spawn(pid, file, fa, sa, cav, env);
     70 
     71 	/* Get the path we're searching. */
     72 	if ((path = getenv("PATH")) == NULL)
     73 		path = _PATH_DEFPATH;
     74 
     75 	/*
     76 	 * Find an executable image with the given name in the PATH
     77 	 */
     78 
     79 	ln = strlen(file);
     80 	err = 0;
     81 	do {
     82 		/* Find the end of this path element. */
     83 		for (p = path; *path != 0 && *path != ':'; path++)
     84 			continue;
     85 		/*
     86 		 * It's a SHELL path -- double, leading and trailing colons
     87 		 * mean the current directory.
     88 		 */
     89 		if (p == path) {
     90 			p = ".";
     91 			lp = 1;
     92 		} else
     93 			lp = (size_t)(path - p);
     94 
     95 		/*
     96 		 * Once we gain chdir/fchdir file actions, this will need
     97 		 * serious work, as we must treat "." relative to the
     98 		 * target of the (final) chdir performed.
     99 		 *
    100 		 * Fortunately, that day is yet to come.
    101 		 */
    102 
    103 		/*
    104 		 * If the path is too long complain.  This is a possible
    105 		 * security issue; given a way to make the path too long
    106 		 * the user may execute the wrong program.
    107 		 */
    108 		if (lp + ln + 2 > sizeof(fpath)) {
    109 			(void)write(STDERR_FILENO, "posix_spawnp: ", 14);
    110 			(void)write(STDERR_FILENO, p, lp);
    111 			(void)write(STDERR_FILENO, ": path too long\n", 16);
    112 			continue;
    113 		}
    114 		memcpy(fpath, p, lp);
    115 		fpath[lp] = '/';
    116 		memcpy(fpath + lp + 1, file, ln);
    117 		fpath[lp + ln + 1] = '\0';
    118 
    119 		/*
    120 		 * It would be nice (much better) to try posix_spawn()
    121 		 * here, using the current fpath as the filename, but
    122 		 * there's no guarantee that it is safe to execute it
    123 		 * twice (the file actions may screw us) so that we
    124 		 * cannot do.   This test is weak, barely even adequate.
    125 		 * but unless we are forced into making posix_spawmp()
    126 		 * become a system call (with PATH as an arg, or an array
    127 		 * of possible paths to try, based upon PATH and file)
    128 		 * we really have no better method.
    129 		 */
    130 		if (access(fpath, X_OK) == 0)
    131 			break;
    132 
    133 		if (err == 0)
    134 			err = errno;
    135 
    136 		fpath[0] = '\0';
    137 
    138 
    139 	} while (*path++ == ':');	/* Otherwise, *path was NUL */
    140 
    141 	if (fpath[0] == '\0')
    142 		return err;
    143 
    144 	/*
    145 	 * Use posix_spawn() with the found binary
    146 	 */
    147 	return posix_spawn(pid, fpath, fa, sa, cav, env);
    148 }
    149