xref: /illumos-gate/usr/src/cmd/runat/runat.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * runat: run a command in attribute directory.
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  * runat file [command]
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * when command is not specified an interactive shell is started
33*7c478bd9Sstevel@tonic-gate  * in the attribute directory.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
40*7c478bd9Sstevel@tonic-gate #include <libintl.h>
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate #include <strings.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static void
usage()45*7c478bd9Sstevel@tonic-gate usage()
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: runat filename [command]\n"));
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])51*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	int fd;
54*7c478bd9Sstevel@tonic-gate 	int dirfd;
55*7c478bd9Sstevel@tonic-gate 	int i;
56*7c478bd9Sstevel@tonic-gate 	int argslen;
57*7c478bd9Sstevel@tonic-gate 	char *shell;
58*7c478bd9Sstevel@tonic-gate 	char *args[4];
59*7c478bd9Sstevel@tonic-gate 	char *cmdargs;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	if (argc < 2) {
62*7c478bd9Sstevel@tonic-gate 		usage();
63*7c478bd9Sstevel@tonic-gate 		exit(127);
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	if ((fd = open64(argv[1], O_RDONLY)) == -1) {
67*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
68*7c478bd9Sstevel@tonic-gate 		    gettext("runat: cannot open %s: %s\n"), argv[1],
69*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
70*7c478bd9Sstevel@tonic-gate 		exit(125);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if ((dirfd = openat64(fd, ".", O_RDONLY|O_XATTR)) == -1) {
74*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
75*7c478bd9Sstevel@tonic-gate 		    gettext("runat: cannot open attribute"
76*7c478bd9Sstevel@tonic-gate 		    " directory for %s: %s\n"), argv[1], strerror(errno));
77*7c478bd9Sstevel@tonic-gate 		exit(125);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if (fchdir(dirfd) == -1) {
83*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
84*7c478bd9Sstevel@tonic-gate 		    gettext("runat: cannot fchdir to attribute"
85*7c478bd9Sstevel@tonic-gate 		    " directory: %s\n"), strerror(errno));
86*7c478bd9Sstevel@tonic-gate 		exit(125);
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if (argc < 3) {
90*7c478bd9Sstevel@tonic-gate 		shell = getenv("SHELL");
91*7c478bd9Sstevel@tonic-gate 		if (shell == NULL) {
92*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
93*7c478bd9Sstevel@tonic-gate 			    gettext(
94*7c478bd9Sstevel@tonic-gate 			    "runat: shell not found, using /bin/sh\n"));
95*7c478bd9Sstevel@tonic-gate 			shell = "/bin/sh";
96*7c478bd9Sstevel@tonic-gate 		}
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		(void) execl(shell, shell, NULL);
99*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
100*7c478bd9Sstevel@tonic-gate 		    gettext("runat: Failed to exec %s: %s\n"), shell,
101*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
102*7c478bd9Sstevel@tonic-gate 		return (126);
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	/*
106*7c478bd9Sstevel@tonic-gate 	 * Count up the size of all of the args
107*7c478bd9Sstevel@tonic-gate 	 */
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	for (i = 2, argslen = 0; i < argc; i++) {
110*7c478bd9Sstevel@tonic-gate 		argslen += strlen(argv[i]) + 1;
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	cmdargs = calloc(1, argslen);
114*7c478bd9Sstevel@tonic-gate 	if (cmdargs == NULL) {
115*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
116*7c478bd9Sstevel@tonic-gate 		    "runat: failed to allocate memory for"
117*7c478bd9Sstevel@tonic-gate 		    " command arguments: %s\n"), strerror(errno));
118*7c478bd9Sstevel@tonic-gate 		exit(126);
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	/*
123*7c478bd9Sstevel@tonic-gate 	 * create string with all of the args concatenated together
124*7c478bd9Sstevel@tonic-gate 	 * This is done so that the shell will interpret the args
125*7c478bd9Sstevel@tonic-gate 	 * and do globbing if necessary.
126*7c478bd9Sstevel@tonic-gate 	 */
127*7c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
128*7c478bd9Sstevel@tonic-gate 		if (strlcat(cmdargs, argv[i], argslen) >= argslen) {
129*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
130*7c478bd9Sstevel@tonic-gate 			    "runat: arguments won't fit in"
131*7c478bd9Sstevel@tonic-gate 			    " allocated buffer\n"));
132*7c478bd9Sstevel@tonic-gate 			exit(126);
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 		/*
136*7c478bd9Sstevel@tonic-gate 		 * tack on a space if there are more args
137*7c478bd9Sstevel@tonic-gate 		 */
138*7c478bd9Sstevel@tonic-gate 		if ((i + 1) < argc) {
139*7c478bd9Sstevel@tonic-gate 			if (strlcat(cmdargs, " ", argslen) >= argslen) {
140*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
141*7c478bd9Sstevel@tonic-gate 				    "runat: arguments won't fit in"
142*7c478bd9Sstevel@tonic-gate 				    " allocated buffer\n"));
143*7c478bd9Sstevel@tonic-gate 				exit(126);
144*7c478bd9Sstevel@tonic-gate 			}
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	args[0] = "/bin/sh";
150*7c478bd9Sstevel@tonic-gate 	args[1] = "-c";
151*7c478bd9Sstevel@tonic-gate 	args[2] = cmdargs;
152*7c478bd9Sstevel@tonic-gate 	args[3] = NULL;
153*7c478bd9Sstevel@tonic-gate 	(void) execvp(args[0], args);
154*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("runat: Failed to exec %s: %s\n"),
155*7c478bd9Sstevel@tonic-gate 	    argv[0], strerror(errno));
156*7c478bd9Sstevel@tonic-gate 	return (126);
157*7c478bd9Sstevel@tonic-gate }
158