posix_spawnp.c revision 1.4 1 1.4 kre /* $NetBSD: posix_spawnp.c,v 1.4 2020/05/11 14:54:34 kre 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.4 kre __RCSID("$NetBSD: posix_spawnp.c,v 1.4 2020/05/11 14:54:34 kre 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.1 martin /*
62 1.4 kre * If there is a / in the name, fall straight through to posix_spawn().
63 1.1 martin */
64 1.4 kre if (strchr(file, '/') != NULL)
65 1.1 martin return posix_spawn(pid, file, fa, sa, cav, env);
66 1.1 martin
67 1.4 kre /* Get the path we're searching. */
68 1.4 kre if ((path = getenv("PATH")) == NULL)
69 1.4 kre path = _PATH_DEFPATH;
70 1.1 martin
71 1.1 martin /*
72 1.1 martin * Find an executable image with the given name in the PATH
73 1.1 martin */
74 1.4 kre
75 1.4 kre ln = strlen(file);
76 1.4 kre err = 0;
77 1.4 kre do {
78 1.4 kre /* Find the end of this path element. */
79 1.4 kre for (p = path; *path != 0 && *path != ':'; path++)
80 1.4 kre continue;
81 1.4 kre /*
82 1.4 kre * It's a SHELL path -- double, leading and trailing colons
83 1.4 kre * mean the current directory.
84 1.4 kre */
85 1.4 kre if (p == path) {
86 1.4 kre p = ".";
87 1.4 kre lp = 1;
88 1.4 kre } else
89 1.4 kre lp = (size_t)(path - p);
90 1.4 kre
91 1.4 kre /*
92 1.4 kre * Once we gain chdir/fchdir file actions, this will need
93 1.4 kre * serious work, as we must treat "." relative to the
94 1.4 kre * target of the (final) chdir performed.
95 1.4 kre *
96 1.4 kre * Fortunately, that day is yet to come.
97 1.4 kre */
98 1.4 kre
99 1.4 kre /*
100 1.4 kre * If the path is too long complain. This is a possible
101 1.4 kre * security issue; given a way to make the path too long
102 1.4 kre * the user may execute the wrong program.
103 1.4 kre */
104 1.4 kre if (lp + ln + 2 > sizeof(fpath)) {
105 1.4 kre (void)write(STDERR_FILENO, "posix_spawnp: ", 14);
106 1.4 kre (void)write(STDERR_FILENO, p, lp);
107 1.4 kre (void)write(STDERR_FILENO, ": path too long\n", 16);
108 1.4 kre continue;
109 1.4 kre }
110 1.4 kre memcpy(fpath, p, lp);
111 1.4 kre fpath[lp] = '/';
112 1.4 kre memcpy(fpath + lp + 1, file, ln);
113 1.4 kre fpath[lp + ln + 1] = '\0';
114 1.4 kre
115 1.4 kre /*
116 1.4 kre * It would be nice (much better) to try posix_spawn()
117 1.4 kre * here, using the current fpath as the filename, but
118 1.4 kre * there's no guarantee that it is safe to execute it
119 1.4 kre * twice (the file actions may screw us) so that we
120 1.4 kre * cannot do. This test is weak, barely even adequate.
121 1.4 kre * but unless we are forced into making posix_spawmp()
122 1.4 kre * become a system call (with PATH as an arg, or an array
123 1.4 kre * of possible paths to try, based upon PATH and file)
124 1.4 kre * we really have no better method.
125 1.4 kre */
126 1.1 martin if (access(fpath, X_OK) == 0)
127 1.1 martin break;
128 1.4 kre
129 1.4 kre if (err == 0)
130 1.4 kre err = errno;
131 1.4 kre
132 1.4 kre fpath[0] = '\0';
133 1.4 kre
134 1.4 kre
135 1.4 kre } while (*path++ == ':'); /* Otherwise, *path was NUL */
136 1.4 kre
137 1.4 kre if (fpath[0] == '\0')
138 1.4 kre return err;
139 1.1 martin
140 1.1 martin /*
141 1.1 martin * Use posix_spawn() with the found binary
142 1.1 martin */
143 1.1 martin return posix_spawn(pid, fpath, fa, sa, cav, env);
144 1.1 martin }
145