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