Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / runat / runat.c
blobcdb91e6363459b4d1490789d81da169e5201f9ad
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * runat: run a command in attribute directory.
32 * runat file [command]
34 * when command is not specified an interactive shell is started
35 * in the attribute directory.
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>
46 static void
47 usage()
49 (void) fprintf(stderr, gettext("usage: runat filename [command]\n"));
52 int
53 main(int argc, char *argv[])
55 int fd;
56 int dirfd;
57 int i;
58 int argslen;
59 char *shell;
60 char *args[4];
61 char *cmdargs;
63 if (argc < 2) {
64 usage();
65 exit(127);
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);
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);
82 (void) close(fd);
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);
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";
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);
108 * Count up the size of all of the args
111 for (i = 2, argslen = 0; i < argc; i++) {
112 argslen += strlen(argv[i]) + 1;
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);
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.
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);
138 * tack on a space if there are more args
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);
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);