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
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]
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.
49 (void) fprintf(stderr
, gettext("usage: runat filename [command]\n"));
53 main(int argc
, char *argv
[])
68 if ((fd
= open64(argv
[1], O_RDONLY
)) == -1) {
69 (void) fprintf(stderr
,
70 gettext("runat: cannot open %s: %s\n"), argv
[1],
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
));
84 if (fchdir(dirfd
) == -1) {
85 (void) fprintf(stderr
,
86 gettext("runat: cannot fchdir to attribute"
87 " directory: %s\n"), strerror(errno
));
92 shell
= getenv("SHELL");
94 (void) fprintf(stderr
,
96 "runat: shell not found, using /bin/sh\n"));
100 (void) execl(shell
, shell
, NULL
);
101 (void) fprintf(stderr
,
102 gettext("runat: Failed to exec %s: %s\n"), shell
,
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
));
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"));
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"));
155 (void) execvp(args
[0], args
);
156 (void) fprintf(stderr
, gettext("runat: Failed to exec %s: %s\n"),
157 argv
[0], strerror(errno
));