17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1b4Sraf * Common Development and Distribution License (the "License").
67257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*a89c0811SRobert Mustacchi * Copyright 2024 Oxide Computer Company
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
29*a89c0811SRobert Mustacchi /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * execlp(name, arg,...,0) (like execl, but does path search)
337c478bd9Sstevel@tonic-gate * execvp(name, argv) (like execv, but does path search)
34*a89c0811SRobert Mustacchi * execvpe(name, argv, envp) (like execve, but does path search)
357c478bd9Sstevel@tonic-gate */
367257d1b4Sraf
377257d1b4Sraf #pragma weak _execlp = execlp
387257d1b4Sraf #pragma weak _execvp = execvp
397257d1b4Sraf
407257d1b4Sraf #include "lint.h"
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <alloca.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <stdarg.h>
487c478bd9Sstevel@tonic-gate #include <stdlib.h>
496a5408e6SRichard Lowe #include <paths.h>
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static const char *execat(const char *, const char *, char *);
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate extern int __xpg4; /* defined in xpg4.c; 0 if not xpg4-compiled program */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /*VARARGS1*/
567c478bd9Sstevel@tonic-gate int
execlp(const char * name,const char * arg0,...)577c478bd9Sstevel@tonic-gate execlp(const char *name, const char *arg0, ...)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate char **argp;
607c478bd9Sstevel@tonic-gate va_list args;
617c478bd9Sstevel@tonic-gate char **argvec;
627c478bd9Sstevel@tonic-gate int err;
637c478bd9Sstevel@tonic-gate int nargs = 0;
647c478bd9Sstevel@tonic-gate char *nextarg;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * count the number of arguments in the variable argument list
687c478bd9Sstevel@tonic-gate * and allocate an argument vector for them on the stack,
697c478bd9Sstevel@tonic-gate * adding space for a terminating null pointer at the end
707c478bd9Sstevel@tonic-gate * and one additional space for argv[0] which is no longer
717c478bd9Sstevel@tonic-gate * counted by the varargs loop.
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate va_start(args, arg0);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate while (va_arg(args, char *) != (char *)0)
777c478bd9Sstevel@tonic-gate nargs++;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate va_end(args);
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate * load the arguments in the variable argument list
837c478bd9Sstevel@tonic-gate * into the argument vector and add the terminating null pointer
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate va_start(args, arg0);
877c478bd9Sstevel@tonic-gate /* workaround for bugid 1242839 */
887c478bd9Sstevel@tonic-gate argvec = alloca((size_t)((nargs + 2) * sizeof (char *)));
897c478bd9Sstevel@tonic-gate nextarg = va_arg(args, char *);
907c478bd9Sstevel@tonic-gate argp = argvec;
917c478bd9Sstevel@tonic-gate *argp++ = (char *)arg0;
927c478bd9Sstevel@tonic-gate while (nargs-- && nextarg != (char *)0) {
937c478bd9Sstevel@tonic-gate *argp = nextarg;
947c478bd9Sstevel@tonic-gate argp++;
957c478bd9Sstevel@tonic-gate nextarg = va_arg(args, char *);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate va_end(args);
987c478bd9Sstevel@tonic-gate *argp = (char *)0;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * call execvp()
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate err = execvp(name, argvec);
1057c478bd9Sstevel@tonic-gate return (err);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
108*a89c0811SRobert Mustacchi static int
execvpe_int(const char * name,char * const * argv,char * const * envp,boolean_t use_env)109*a89c0811SRobert Mustacchi execvpe_int(const char *name, char *const *argv, char *const *envp,
110*a89c0811SRobert Mustacchi boolean_t use_env)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate const char *pathstr;
1137c478bd9Sstevel@tonic-gate char fname[PATH_MAX+2];
1147c478bd9Sstevel@tonic-gate char *newargs[256];
1157c478bd9Sstevel@tonic-gate int i;
1167c478bd9Sstevel@tonic-gate const char *cp;
1177c478bd9Sstevel@tonic-gate unsigned etxtbsy = 1;
1187c478bd9Sstevel@tonic-gate int eacces = 0;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate if (*name == '\0') {
1217c478bd9Sstevel@tonic-gate errno = ENOENT;
1227c478bd9Sstevel@tonic-gate return (-1);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate if ((pathstr = getenv("PATH")) == NULL) {
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * XPG4: pathstr is equivalent to CSPATH, except that
1277c478bd9Sstevel@tonic-gate * :/usr/sbin is appended when root, and pathstr must end
1287c478bd9Sstevel@tonic-gate * with a colon when not root. Keep these paths in sync
1297c478bd9Sstevel@tonic-gate * with CSPATH in confstr.c. Note that pathstr must end
1307c478bd9Sstevel@tonic-gate * with a colon when not root so that when name doesn't
1317c478bd9Sstevel@tonic-gate * contain '/', the last call to execat() will result in an
1327c478bd9Sstevel@tonic-gate * attempt to execv name from the current directory.
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate if (geteuid() == 0 || getuid() == 0) {
1357c478bd9Sstevel@tonic-gate if (__xpg4 == 0) { /* not XPG4 */
1367c478bd9Sstevel@tonic-gate pathstr = "/usr/sbin:/usr/ccs/bin:/usr/bin";
1377c478bd9Sstevel@tonic-gate } else { /* XPG4 (CSPATH + /usr/sbin) */
1386a5408e6SRichard Lowe pathstr = "/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:"
1396a5408e6SRichard Lowe "/opt/SUNWspro/bin:/usr/sbin";
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate } else {
1427c478bd9Sstevel@tonic-gate if (__xpg4 == 0) { /* not XPG4 */
1437c478bd9Sstevel@tonic-gate pathstr = "/usr/ccs/bin:/usr/bin:";
1447c478bd9Sstevel@tonic-gate } else { /* XPG4 (CSPATH) */
1457c478bd9Sstevel@tonic-gate pathstr = "/usr/xpg4/bin:/usr/ccs/bin:"
1467c478bd9Sstevel@tonic-gate "/usr/bin:/opt/SUNWspro/bin:";
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate cp = strchr(name, '/')? (const char *)"": pathstr;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate do {
1537c478bd9Sstevel@tonic-gate cp = execat(cp, name, fname);
1547c478bd9Sstevel@tonic-gate retry:
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate * 4025035 and 4038378
1577c478bd9Sstevel@tonic-gate * if a filename begins with a "-" prepend "./" so that
1587c478bd9Sstevel@tonic-gate * the shell can't interpret it as an option
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate if (*fname == '-') {
1617c478bd9Sstevel@tonic-gate size_t size = strlen(fname) + 1;
1627c478bd9Sstevel@tonic-gate if ((size + 2) > sizeof (fname)) {
1637c478bd9Sstevel@tonic-gate errno = E2BIG;
1647c478bd9Sstevel@tonic-gate return (-1);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate (void) memmove(fname + 2, fname, size);
1677c478bd9Sstevel@tonic-gate fname[0] = '.';
1687c478bd9Sstevel@tonic-gate fname[1] = '/';
1697c478bd9Sstevel@tonic-gate }
170*a89c0811SRobert Mustacchi if (use_env) {
171*a89c0811SRobert Mustacchi (void) execve(fname, argv, envp);
172*a89c0811SRobert Mustacchi } else {
173*a89c0811SRobert Mustacchi (void) execv(fname, argv);
174*a89c0811SRobert Mustacchi }
1757c478bd9Sstevel@tonic-gate switch (errno) {
1767c478bd9Sstevel@tonic-gate case ENOEXEC:
1776a5408e6SRichard Lowe newargs[0] = "sh";
1787c478bd9Sstevel@tonic-gate newargs[1] = fname;
1797c478bd9Sstevel@tonic-gate for (i = 1; (newargs[i + 1] = argv[i]) != NULL; ++i) {
1807c478bd9Sstevel@tonic-gate if (i >= 254) {
1817c478bd9Sstevel@tonic-gate errno = E2BIG;
1827c478bd9Sstevel@tonic-gate return (-1);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate }
185*a89c0811SRobert Mustacchi if (use_env) {
186*a89c0811SRobert Mustacchi (void) execve(_PATH_BSHELL, newargs, envp);
187*a89c0811SRobert Mustacchi } else {
188*a89c0811SRobert Mustacchi (void) execv(_PATH_BSHELL, newargs);
189*a89c0811SRobert Mustacchi }
1907c478bd9Sstevel@tonic-gate return (-1);
1917c478bd9Sstevel@tonic-gate case ETXTBSY:
1927c478bd9Sstevel@tonic-gate if (++etxtbsy > 5)
1937c478bd9Sstevel@tonic-gate return (-1);
1947c478bd9Sstevel@tonic-gate (void) sleep(etxtbsy);
1957c478bd9Sstevel@tonic-gate goto retry;
1967c478bd9Sstevel@tonic-gate case EACCES:
1977c478bd9Sstevel@tonic-gate ++eacces;
1987c478bd9Sstevel@tonic-gate break;
1997c478bd9Sstevel@tonic-gate case ENOMEM:
2007c478bd9Sstevel@tonic-gate case E2BIG:
2017c478bd9Sstevel@tonic-gate case EFAULT:
2027c478bd9Sstevel@tonic-gate return (-1);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate } while (cp);
2057c478bd9Sstevel@tonic-gate if (eacces)
2067c478bd9Sstevel@tonic-gate errno = EACCES;
2077c478bd9Sstevel@tonic-gate return (-1);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
210*a89c0811SRobert Mustacchi int
execvp(const char * file,char * const * argv)211*a89c0811SRobert Mustacchi execvp(const char *file, char *const *argv)
212*a89c0811SRobert Mustacchi {
213*a89c0811SRobert Mustacchi return (execvpe_int(file, argv, NULL, B_FALSE));
214*a89c0811SRobert Mustacchi }
215*a89c0811SRobert Mustacchi
216*a89c0811SRobert Mustacchi int
execvpe(const char * file,char * const * argv,char * const * envp)217*a89c0811SRobert Mustacchi execvpe(const char *file, char *const *argv, char *const *envp)
218*a89c0811SRobert Mustacchi {
219*a89c0811SRobert Mustacchi return (execvpe_int(file, argv, envp, B_TRUE));
220*a89c0811SRobert Mustacchi }
221*a89c0811SRobert Mustacchi
2227c478bd9Sstevel@tonic-gate static const char *
execat(const char * s1,const char * s2,char * si)2237c478bd9Sstevel@tonic-gate execat(const char *s1, const char *s2, char *si)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate char *s;
2267c478bd9Sstevel@tonic-gate int cnt = PATH_MAX + 1; /* number of characters in s2 */
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate s = si;
2297c478bd9Sstevel@tonic-gate while (*s1 && *s1 != ':') {
2307c478bd9Sstevel@tonic-gate if (cnt > 0) {
2317c478bd9Sstevel@tonic-gate *s++ = *s1++;
2327c478bd9Sstevel@tonic-gate cnt--;
2337c478bd9Sstevel@tonic-gate } else
2347c478bd9Sstevel@tonic-gate s1++;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate if (si != s && cnt > 0) {
2377c478bd9Sstevel@tonic-gate *s++ = '/';
2387c478bd9Sstevel@tonic-gate cnt--;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate while (*s2 && cnt > 0) {
2417c478bd9Sstevel@tonic-gate *s++ = *s2++;
2427c478bd9Sstevel@tonic-gate cnt--;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate *s = '\0';
2457c478bd9Sstevel@tonic-gate return (*s1 ? ++s1: 0);
2467c478bd9Sstevel@tonic-gate }
247