]> git.pld-linux.org Git - packages/setup.git/blob - postshell.c
- shouldn't the trigger be static program, as setup is installed as very first thing...
[packages/setup.git] / postshell.c
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
79 #define MAX_LINE 1024
80 #define MAX_ARGS 32
81 #define SEARCH_PATH { "/sbin/", "/bin/", "/usr/sbin/", "/usr/bin/", NULL }
82
83 int exit_status;
84 int ignore_status;
85
86 void do_execve(char **argv)
87 {
88         char *path[] = SEARCH_PATH;
89         char file[MAX_LINE + 100];
90         int i; 
91
92         if (**argv == '.' || **argv == '/')
93                 execve(argv[0], argv, environ);
94         else 
95                 for (i = 0; path[i]; i++) {
96                         strcpy(file, path[i]);
97                         strcat(file, argv[0]);
98                         execve(file, argv, environ);
99                 }
100 }
101
102 int exec_and_wait(char **argv)
103 {
104         pid_t pid;
105
106         pid = fork();
107
108         if (pid == -1) {
109                 perror("fork");
110                 return -1;
111         } else if (pid == 0) {
112                 /* child. */
113                 do_execve(argv);
114                 if (!ignore_status) 
115                         perror(argv[0]); 
116                 exit(127);
117         } else {
118                 int status, err;
119                 
120                 err = waitpid(pid, &status, 0);
121                 if (err < 0) {
122                         perror("waitpid");
123                         return -1;
124                 } else
125                         return WEXITSTATUS(status);
126         }
127 }
128
129 void split_argv(char **argv, char *s)
130 {
131         char *dst;
132         int argc, delim;
133         
134         for (argc = 0; argc < MAX_ARGS; argc++) {
135                 while (*s == ' ' || *s == '\t')
136                         s++;
137
138                 if (*s == 0)
139                         break;
140
141                 argv[argc] = s;
142                 dst = s;
143                 
144                 while (*s && *s != ' ' && *s != '\t') {
145                         if (*s == '\'' || *s == '"') {
146                                 delim = *s++;
147                                 while (*s && *s != delim)
148                                         *dst++ = *s++;
149                                 if (*s)
150                                         s++;
151                         } else {
152                                 *dst++ = *s++;
153                         }
154                 }
155
156                 if (*dst) {
157                         if (s == dst)
158                                 s++;
159                         *dst++ = 0;
160                 }
161         }
162
163         argv[argc] = NULL;
164 }
165
166 void exec_line(char *s)
167 {
168         char *argv[MAX_ARGS + 1];
169         int ret;
170
171         split_argv(argv, s);
172         
173         ignore_status = 0;
174
175         if (**argv == '-') {
176                 ignore_status++;
177                 (*argv)++;
178         }
179
180         ret = exec_and_wait(argv);
181
182         if (ret && !ignore_status)
183                 exit_status = ret;
184 }
185
186 void exec_file(int fd)
187 {
188         char line[MAX_LINE];
189         struct stat sbuf;
190         char *p, *s, *a;
191
192         if (fstat(fd, &sbuf) < 0) {
193                 perror("fstat()");
194                 exit(1);
195         }
196
197         if ((p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) < 0) {
198                 perror("mmap()");
199                 exit(1);
200         }
201
202         for (a = s = p; s < p + sbuf.st_size; s++) {
203                 if (*s == '\n') {
204                         memcpy(line, a, s - a);
205                         line[s - a] = '\0';
206                         exec_line(line);
207                         a = ++s;
208                 }
209         }
210
211         // last line was not terminated.
212         if (s == p + sbuf.st_size) {
213                 memcpy(line, a, s - a);
214                 line[s - a] = '\0';
215                 exec_line(line);
216         }
217
218         if (munmap(p, sbuf.st_size) < 0) {
219                 perror("munmap()");
220                 exit(1);
221         }
222 }
223
224 #define error(msg) write(2, msg, strlen(msg))
225 int main(int argc, char **argv)
226 {
227         int fd;
228
229         if (argc < 2) {
230                 error("USAGE: ");
231                 error(argv[0]);
232                 error(" filename\n");
233                 exit(1);
234         }
235
236         fd = open(argv[1], O_RDONLY);
237         
238         if (fd == -1) {
239                 perror(argv[1]);
240                 exit(1);
241         }
242
243         exec_file(fd);
244         close(fd);
245         exit(exit_status);
246 }
This page took 0.045139 seconds and 3 git commands to generate.