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