diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-25 06:03:19 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-25 06:03:19 +0000 |
commit | 0c3b0d501e7f42f679add3ad28a20e1281c77475 (patch) | |
tree | 7a74099e6f379d6fa0bb0bc1b10894c72b630fb3 | |
parent | f7d56659693c78c26b25037c7f74101a4aede381 (diff) | |
download | busybox-w32-0c3b0d501e7f42f679add3ad28a20e1281c77475.tar.gz busybox-w32-0c3b0d501e7f42f679add3ad28a20e1281c77475.tar.bz2 busybox-w32-0c3b0d501e7f42f679add3ad28a20e1281c77475.zip |
lpd: small shrink
lpd_main 761 747 -14
-rw-r--r-- | printutils/lpd.c | 85 |
1 files changed, 35 insertions, 50 deletions
diff --git a/printutils/lpd.c b/printutils/lpd.c index f4c902c79..953324452 100644 --- a/printutils/lpd.c +++ b/printutils/lpd.c | |||
@@ -87,43 +87,6 @@ static char *sane(char *str) | |||
87 | return str; | 87 | return str; |
88 | } | 88 | } |
89 | 89 | ||
90 | // we can use leaky setenv since we are about to exec or exit | ||
91 | static void exec_helper(char **filenames, char **argv) ATTRIBUTE_NORETURN; | ||
92 | static void exec_helper(char **filenames, char **argv) | ||
93 | { | ||
94 | char *p, *q; | ||
95 | char var[2]; | ||
96 | |||
97 | var[1] = '\0'; | ||
98 | |||
99 | // read and delete ctrlfile | ||
100 | q = xmalloc_open_read_close(filenames[0], NULL); | ||
101 | unlink(filenames[0]); | ||
102 | // provide datafile name | ||
103 | xsetenv("DATAFILE", filenames[1]); | ||
104 | // parse control file by "\n" | ||
105 | while ((p = strchr(q, '\n')) != NULL | ||
106 | && isalpha(*q) | ||
107 | ) { | ||
108 | *p++ = '\0'; | ||
109 | // q is a line of <SYM><VALUE>, | ||
110 | // we are setting environment string <SYM>=<VALUE>. | ||
111 | // Ignoring "l<datafile>", exporting others: | ||
112 | if (*q != 'l') { | ||
113 | var[0] = *q++; | ||
114 | xsetenv(var, q); | ||
115 | } | ||
116 | // next line, plz! | ||
117 | q = p; | ||
118 | } | ||
119 | // we are the helper, we wanna be silent. | ||
120 | // this call reopens stdio fds to "/dev/null" | ||
121 | // (no daemonization is done) | ||
122 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO | DAEMON_ONLY_SANITIZE, NULL); | ||
123 | BB_EXECVP(*argv, argv); | ||
124 | exit(127); // it IS error if helper cannot be executed! | ||
125 | } | ||
126 | |||
127 | static char *xmalloc_read_stdin(void) | 90 | static char *xmalloc_read_stdin(void) |
128 | { | 91 | { |
129 | // SECURITY: | 92 | // SECURITY: |
@@ -184,12 +147,42 @@ int lpd_main(int argc ATTRIBUTE_UNUSED, char *argv[]) | |||
184 | // N.B. we bail out on any error | 147 | // N.B. we bail out on any error |
185 | s = xmalloc_read_stdin(); | 148 | s = xmalloc_read_stdin(); |
186 | if (!s) { // (probably) EOF | 149 | if (!s) { // (probably) EOF |
187 | if (spooling /* && 7 != spooling - always true */) { | 150 | char *p, *q, var[2]; |
188 | // we didn't see both ctrlfile & datafile! | 151 | |
189 | goto err_exit; | 152 | // non-spooling mode or no spool helper specified |
153 | if (!spooling || !*argv) | ||
154 | return EXIT_SUCCESS; // the only non-error exit | ||
155 | // spooling mode but we didn't see both ctrlfile & datafile | ||
156 | if (spooling != 7) | ||
157 | goto err_exit; // reject job | ||
158 | |||
159 | // spooling mode and spool helper specified -> exec spool helper | ||
160 | // (we exit 127 if helper cannot be executed) | ||
161 | var[1] = '\0'; | ||
162 | // read and delete ctrlfile | ||
163 | q = xmalloc_open_read_close(filenames[0], NULL); | ||
164 | unlink(filenames[0]); | ||
165 | // provide datafile name | ||
166 | // we can use leaky setenv since we are about to exec or exit | ||
167 | xsetenv("DATAFILE", filenames[1]); | ||
168 | // parse control file by "\n" | ||
169 | while ((p = strchr(q, '\n')) != NULL && isalpha(*q)) { | ||
170 | *p++ = '\0'; | ||
171 | // q is a line of <SYM><VALUE>, | ||
172 | // we are setting environment string <SYM>=<VALUE>. | ||
173 | // Ignoring "l<datafile>", exporting others: | ||
174 | if (*q != 'l') { | ||
175 | var[0] = *q++; | ||
176 | xsetenv(var, q); | ||
177 | } | ||
178 | q = p; // next line | ||
190 | } | 179 | } |
191 | // one of only two non-error exits | 180 | // helper should not talk over network. |
192 | return EXIT_SUCCESS; | 181 | // this call reopens stdio fds to "/dev/null" |
182 | // (no daemonization is done) | ||
183 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO | DAEMON_ONLY_SANITIZE, NULL); | ||
184 | BB_EXECVP(*argv, argv); | ||
185 | exit(127); | ||
193 | } | 186 | } |
194 | 187 | ||
195 | // validate input. | 188 | // validate input. |
@@ -269,14 +262,6 @@ int lpd_main(int argc ATTRIBUTE_UNUSED, char *argv[]) | |||
269 | 262 | ||
270 | free(s); | 263 | free(s); |
271 | close(fd); // NB: can do close(-1). Who cares? | 264 | close(fd); // NB: can do close(-1). Who cares? |
272 | |||
273 | // spawn spool helper and exit if all files are dumped | ||
274 | if (7 == spooling && *argv) { | ||
275 | // signal OK | ||
276 | safe_write(STDOUT_FILENO, "", 1); | ||
277 | // does not return (exits 0) | ||
278 | exec_helper(filenames, argv); | ||
279 | } | ||
280 | } // while (1) | 265 | } // while (1) |
281 | 266 | ||
282 | err_exit: | 267 | err_exit: |