]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2002 Michal Moskal <malekith@pld-linux.org>. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by Michal Moskal. | |
16 | * 4. Neither the name of the author nor the names of any co-contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY MICHAL MOSKAL AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | /* | |
34 | * Usage | |
35 | * ~~~~~ | |
36 | * | |
37 | * This simple program is aimed to be used as script interpreter in rpm | |
38 | * %post scriptlets and the like. It opens file passed as first argument | |
39 | * and executes commands found there. Only linear execution is supported. | |
40 | * For example, in glibc.spec: | |
41 | * | |
42 | * %post -p /sbin/postshell | |
43 | * ldconfig | |
44 | * -telinit q | |
45 | * | |
46 | * (full patch like /sbin/ldconfig or -/sbin/tellinit will also work). | |
47 | * | |
48 | * If command starts with - its exit status is ignored. Otherwise last | |
49 | * non-zero exit status is returned. | |
50 | * | |
51 | * There are no builtin commands (yet :). | |
52 | * | |
53 | * Following commands *will* work as expected (as in Bourne shell): | |
54 | * | |
55 | * /bin/echo "Foo bar baz" | |
56 | * insmod foobar options="foo bar 'qux'" | |
57 | * false | |
58 | * | |
59 | * Following *won't*: | |
60 | * | |
61 | * exit 1 | |
62 | * echo foo || echo baz | |
63 | * set -x | |
64 | * | |
65 | * Patches and bugreports are welcome, direct them to Michal Moskal | |
66 | * <malekith@pld-linux.org>. | |
67 | */ | |
68 | ||
69 | #define _GNU_SOURCE | |
70 | #include <sys/types.h> | |
71 | #include <sys/wait.h> | |
72 | #include <unistd.h> | |
73 | #include <stdio.h> | |
74 | #include <stdlib.h> | |
75 | #include <string.h> | |
76 | #include <fcntl.h> | |
77 | #include <sys/mman.h> | |
78 | #include <sys/stat.h> | |
79 | ||
80 | #define MAX_LINE 1024 | |
81 | #define MAX_ARGS 32 | |
82 | #define SEARCH_PATH { "/sbin/", "/bin/", "/usr/sbin/", "/usr/bin/", NULL } | |
83 | ||
84 | int exit_status; | |
85 | int ignore_status; | |
86 | ||
87 | void do_execve(char **argv) | |
88 | { | |
89 | char *path[] = SEARCH_PATH; | |
90 | char file[MAX_LINE + 100]; | |
91 | int i; | |
92 | ||
93 | if (**argv == '.' || **argv == '/') | |
94 | execve(argv[0], argv, environ); | |
95 | else | |
96 | for (i = 0; path[i]; i++) { | |
97 | strcpy(file, path[i]); | |
98 | strcat(file, argv[0]); | |
99 | execve(file, argv, environ); | |
100 | } | |
101 | } | |
102 | ||
103 | int exec_and_wait(char **argv) | |
104 | { | |
105 | pid_t pid; | |
106 | ||
107 | pid = fork(); | |
108 | ||
109 | if (pid == -1) { | |
110 | perror("fork"); | |
111 | return -1; | |
112 | } else if (pid == 0) { | |
113 | /* child. */ | |
114 | do_execve(argv); | |
115 | if (!ignore_status) | |
116 | perror(argv[0]); | |
117 | exit(127); | |
118 | } else { | |
119 | int status, err; | |
120 | ||
121 | err = waitpid(pid, &status, 0); | |
122 | if (err < 0) { | |
123 | perror("waitpid"); | |
124 | return -1; | |
125 | } else | |
126 | return WEXITSTATUS(status); | |
127 | } | |
128 | } | |
129 | ||
130 | void split_argv(char **argv, char *s) | |
131 | { | |
132 | char *dst; | |
133 | int argc, delim; | |
134 | ||
135 | for (argc = 0; argc < MAX_ARGS; argc++) { | |
136 | while (*s == ' ' || *s == '\t') | |
137 | s++; | |
138 | ||
139 | if (*s == 0) | |
140 | break; | |
141 | ||
142 | argv[argc] = s; | |
143 | dst = s; | |
144 | ||
145 | while (*s && *s != ' ' && *s != '\t') { | |
146 | if (*s == '\'' || *s == '"') { | |
147 | delim = *s++; | |
148 | while (*s && *s != delim) | |
149 | *dst++ = *s++; | |
150 | if (*s) | |
151 | s++; | |
152 | } else { | |
153 | *dst++ = *s++; | |
154 | } | |
155 | } | |
156 | ||
157 | if (*dst) { | |
158 | if (s == dst) | |
159 | s++; | |
160 | *dst++ = 0; | |
161 | } | |
162 | } | |
163 | ||
164 | argv[argc] = NULL; | |
165 | } | |
166 | ||
167 | void exec_line(char *s) | |
168 | { | |
169 | char *argv[MAX_ARGS + 1]; | |
170 | int ret; | |
171 | ||
172 | split_argv(argv, s); | |
173 | ||
174 | ignore_status = 0; | |
175 | ||
176 | if (**argv == '-') { | |
177 | ignore_status++; | |
178 | (*argv)++; | |
179 | } | |
180 | ||
181 | ret = exec_and_wait(argv); | |
182 | ||
183 | if (ret && !ignore_status) | |
184 | exit_status = ret; | |
185 | } | |
186 | ||
187 | void exec_file(int fd) | |
188 | { | |
189 | char line[MAX_LINE]; | |
190 | struct stat sbuf; | |
191 | char *p, *s, *a; | |
192 | ||
193 | if (fstat(fd, &sbuf) < 0) { | |
194 | perror("fstat()"); | |
195 | exit(1); | |
196 | } | |
197 | ||
198 | if ((p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) < 0) { | |
199 | perror("mmap()"); | |
200 | exit(1); | |
201 | } | |
202 | ||
203 | for (a = s = p; s < p + sbuf.st_size; s++) { | |
204 | if (*s == '\n') { | |
205 | memcpy(line, a, s - a); | |
206 | line[s - a] = '\0'; | |
207 | exec_line(line); | |
208 | a = ++s; | |
209 | } | |
210 | } | |
211 | ||
212 | // last line was not terminated. | |
213 | if (s == p + sbuf.st_size) { | |
214 | memcpy(line, a, s - a); | |
215 | line[s - a] = '\0'; | |
216 | exec_line(line); | |
217 | } | |
218 | ||
219 | if (munmap(p, sbuf.st_size) < 0) { | |
220 | perror("munmap()"); | |
221 | exit(1); | |
222 | } | |
223 | } | |
224 | ||
225 | #define error(msg) write(2, msg, strlen(msg)) | |
226 | int main(int argc, char **argv) | |
227 | { | |
228 | int fd; | |
229 | ||
230 | if (argc < 2) { | |
231 | error("USAGE: "); | |
232 | error(argv[0]); | |
233 | error(" filename\n"); | |
234 | exit(1); | |
235 | } | |
236 | ||
237 | fd = open(argv[1], O_RDONLY); | |
238 | ||
239 | if (fd == -1) { | |
240 | perror(argv[1]); | |
241 | exit(1); | |
242 | } | |
243 | ||
244 | exec_file(fd); | |
245 | close(fd); | |
246 | exit(exit_status); | |
247 | } |