1*f14fb602SLionel Sambuc /* $NetBSD: posix_spawnp.c,v 1.2 2012/02/22 17:51:01 martin Exp $ */
2*f14fb602SLionel Sambuc
3*f14fb602SLionel Sambuc /*-
4*f14fb602SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*f14fb602SLionel Sambuc * All rights reserved.
6*f14fb602SLionel Sambuc *
7*f14fb602SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*f14fb602SLionel Sambuc * by Martin Husemann <martin@NetBSD.org>.
9*f14fb602SLionel Sambuc *
10*f14fb602SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*f14fb602SLionel Sambuc * modification, are permitted provided that the following conditions
12*f14fb602SLionel Sambuc * are met:
13*f14fb602SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*f14fb602SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*f14fb602SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*f14fb602SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*f14fb602SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*f14fb602SLionel Sambuc *
19*f14fb602SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*f14fb602SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*f14fb602SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*f14fb602SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*f14fb602SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*f14fb602SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*f14fb602SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*f14fb602SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*f14fb602SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*f14fb602SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*f14fb602SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*f14fb602SLionel Sambuc */
31*f14fb602SLionel Sambuc
32*f14fb602SLionel Sambuc #include <sys/cdefs.h>
33*f14fb602SLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
34*f14fb602SLionel Sambuc __RCSID("$NetBSD: posix_spawnp.c,v 1.2 2012/02/22 17:51:01 martin Exp $");
35*f14fb602SLionel Sambuc #endif /* LIBC_SCCS and not lint */
36*f14fb602SLionel Sambuc
37*f14fb602SLionel Sambuc #include <stdio.h>
38*f14fb602SLionel Sambuc #include <string.h>
39*f14fb602SLionel Sambuc #include <stdlib.h>
40*f14fb602SLionel Sambuc #include <errno.h>
41*f14fb602SLionel Sambuc #include <unistd.h>
42*f14fb602SLionel Sambuc #include <spawn.h>
43*f14fb602SLionel Sambuc
44*f14fb602SLionel Sambuc
posix_spawnp(pid_t * __restrict pid,const char * __restrict file,const posix_spawn_file_actions_t * fa,const posix_spawnattr_t * __restrict sa,char * const * __restrict cav,char * const * __restrict env)45*f14fb602SLionel Sambuc int posix_spawnp(pid_t * __restrict pid, const char * __restrict file,
46*f14fb602SLionel Sambuc const posix_spawn_file_actions_t *fa,
47*f14fb602SLionel Sambuc const posix_spawnattr_t * __restrict sa,
48*f14fb602SLionel Sambuc char * const *__restrict cav, char * const *__restrict env)
49*f14fb602SLionel Sambuc {
50*f14fb602SLionel Sambuc char fpath[FILENAME_MAX], *last, *p;
51*f14fb602SLionel Sambuc char *path;
52*f14fb602SLionel Sambuc
53*f14fb602SLionel Sambuc /*
54*f14fb602SLionel Sambuc * If there is a / in the filename, or no PATH environment variable
55*f14fb602SLionel Sambuc * set, fall straight through to posix_spawn().
56*f14fb602SLionel Sambuc */
57*f14fb602SLionel Sambuc if (strchr(file, '/') != NULL || (path = getenv("PATH")) == NULL)
58*f14fb602SLionel Sambuc return posix_spawn(pid, file, fa, sa, cav, env);
59*f14fb602SLionel Sambuc
60*f14fb602SLionel Sambuc path = strdup(path);
61*f14fb602SLionel Sambuc if (path == NULL)
62*f14fb602SLionel Sambuc return ENOMEM;
63*f14fb602SLionel Sambuc
64*f14fb602SLionel Sambuc /*
65*f14fb602SLionel Sambuc * Find an executable image with the given name in the PATH
66*f14fb602SLionel Sambuc */
67*f14fb602SLionel Sambuc for (p = strtok_r(path, ":", &last); p;
68*f14fb602SLionel Sambuc p = strtok_r(NULL, ":", &last)) {
69*f14fb602SLionel Sambuc snprintf(fpath, sizeof fpath, "%s/%s", p, file);
70*f14fb602SLionel Sambuc fpath[FILENAME_MAX-1] = 0;
71*f14fb602SLionel Sambuc if (access(fpath, X_OK) == 0)
72*f14fb602SLionel Sambuc break;
73*f14fb602SLionel Sambuc }
74*f14fb602SLionel Sambuc free(path);
75*f14fb602SLionel Sambuc
76*f14fb602SLionel Sambuc /*
77*f14fb602SLionel Sambuc * Use posix_spawn() with the found binary
78*f14fb602SLionel Sambuc */
79*f14fb602SLionel Sambuc return posix_spawn(pid, fpath, fa, sa, cav, env);
80*f14fb602SLionel Sambuc }
81*f14fb602SLionel Sambuc
82