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