diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-01 09:59:01 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-01 09:59:01 +0000 |
commit | a0898170d876aeeea19eb144556afd05819db450 (patch) | |
tree | 89ec46edf8f92856277859b9ea7e0b5336fa962d | |
parent | 96e1b38586e80a0f014038bf4fdf4689c668fbd6 (diff) | |
download | busybox-w32-a0898170d876aeeea19eb144556afd05819db450.tar.gz busybox-w32-a0898170d876aeeea19eb144556afd05819db450.tar.bz2 busybox-w32-a0898170d876aeeea19eb144556afd05819db450.zip |
hush: stop doing manual acounting of open fd's, kernel can do it for us
-rw-r--r-- | shell/hush.c | 48 |
1 files changed, 3 insertions, 45 deletions
diff --git a/shell/hush.c b/shell/hush.c index 9c705568d..9181641b9 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -277,11 +277,6 @@ struct pipe { | |||
277 | smallint res_word; /* needed for if, for, while, until... */ | 277 | smallint res_word; /* needed for if, for, while, until... */ |
278 | }; | 278 | }; |
279 | 279 | ||
280 | struct close_me { | ||
281 | struct close_me *next; | ||
282 | int fd; | ||
283 | }; | ||
284 | |||
285 | /* On program start, environ points to initial environment. | 280 | /* On program start, environ points to initial environment. |
286 | * putenv adds new pointers into it, unsetenv removes them. | 281 | * putenv adds new pointers into it, unsetenv removes them. |
287 | * Neither of these (de)allocates the strings. | 282 | * Neither of these (de)allocates the strings. |
@@ -362,7 +357,6 @@ struct globals { | |||
362 | int global_argc; | 357 | int global_argc; |
363 | int last_return_code; | 358 | int last_return_code; |
364 | const char *ifs; | 359 | const char *ifs; |
365 | struct close_me *close_me_head; | ||
366 | const char *cwd; | 360 | const char *cwd; |
367 | unsigned last_bg_pid; | 361 | unsigned last_bg_pid; |
368 | struct variable *top_var; /* = &shell_ver (set in main()) */ | 362 | struct variable *top_var; /* = &shell_ver (set in main()) */ |
@@ -409,7 +403,6 @@ enum { run_list_level = 0 }; | |||
409 | #define last_return_code (G.last_return_code) | 403 | #define last_return_code (G.last_return_code) |
410 | #define ifs (G.ifs ) | 404 | #define ifs (G.ifs ) |
411 | #define fake_mode (G.fake_mode ) | 405 | #define fake_mode (G.fake_mode ) |
412 | #define close_me_head (G.close_me_head ) | ||
413 | #define cwd (G.cwd ) | 406 | #define cwd (G.cwd ) |
414 | #define last_bg_pid (G.last_bg_pid ) | 407 | #define last_bg_pid (G.last_bg_pid ) |
415 | #define top_var (G.top_var ) | 408 | #define top_var (G.top_var ) |
@@ -487,10 +480,6 @@ static int file_get(struct in_str *i); | |||
487 | static int file_peek(struct in_str *i); | 480 | static int file_peek(struct in_str *i); |
488 | static void setup_file_in_str(struct in_str *i, FILE *f); | 481 | static void setup_file_in_str(struct in_str *i, FILE *f); |
489 | static void setup_string_in_str(struct in_str *i, const char *s); | 482 | static void setup_string_in_str(struct in_str *i, const char *s); |
490 | /* close_me manipulations: */ | ||
491 | static void mark_open(int fd); | ||
492 | static void mark_closed(int fd); | ||
493 | static void close_all(void); | ||
494 | /* "run" the final data structures: */ | 483 | /* "run" the final data structures: */ |
495 | #if !defined(DEBUG_CLEAN) | 484 | #if !defined(DEBUG_CLEAN) |
496 | #define free_pipe_list(head, indent) free_pipe_list(head) | 485 | #define free_pipe_list(head, indent) free_pipe_list(head) |
@@ -999,14 +988,13 @@ static int builtin_source(char **argv) | |||
999 | bb_error_msg("cannot open '%s'", argv[1]); | 988 | bb_error_msg("cannot open '%s'", argv[1]); |
1000 | return EXIT_FAILURE; | 989 | return EXIT_FAILURE; |
1001 | } | 990 | } |
991 | close_on_exec_on(fileno(input)); | ||
1002 | 992 | ||
1003 | /* Now run the file */ | 993 | /* Now run the file */ |
1004 | /* XXX argv and argc are broken; need to save old global_argv | 994 | /* XXX argv and argc are broken; need to save old global_argv |
1005 | * (pointer only is OK!) on this stack frame, | 995 | * (pointer only is OK!) on this stack frame, |
1006 | * set global_argv=argv+1, recurse, and restore. */ | 996 | * set global_argv=argv+1, recurse, and restore. */ |
1007 | mark_open(fileno(input)); | ||
1008 | status = parse_and_run_file(input); | 997 | status = parse_and_run_file(input); |
1009 | mark_closed(fileno(input)); | ||
1010 | fclose(input); | 998 | fclose(input); |
1011 | return status; | 999 | return status; |
1012 | } | 1000 | } |
@@ -1251,33 +1239,6 @@ static void setup_string_in_str(struct in_str *i, const char *s) | |||
1251 | i->eof_flag = 0; | 1239 | i->eof_flag = 0; |
1252 | } | 1240 | } |
1253 | 1241 | ||
1254 | static void mark_open(int fd) | ||
1255 | { | ||
1256 | struct close_me *new = xmalloc(sizeof(struct close_me)); | ||
1257 | new->fd = fd; | ||
1258 | new->next = close_me_head; | ||
1259 | close_me_head = new; | ||
1260 | } | ||
1261 | |||
1262 | static void mark_closed(int fd) | ||
1263 | { | ||
1264 | struct close_me *tmp; | ||
1265 | if (close_me_head == NULL || close_me_head->fd != fd) | ||
1266 | bb_error_msg_and_die("corrupt close_me"); | ||
1267 | tmp = close_me_head; | ||
1268 | close_me_head = close_me_head->next; | ||
1269 | free(tmp); | ||
1270 | } | ||
1271 | |||
1272 | static void close_all(void) | ||
1273 | { | ||
1274 | struct close_me *c; | ||
1275 | for (c = close_me_head; c; c = c->next) { | ||
1276 | close(c->fd); | ||
1277 | } | ||
1278 | close_me_head = NULL; | ||
1279 | } | ||
1280 | |||
1281 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, | 1242 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
1282 | * and stderr if they are redirected. */ | 1243 | * and stderr if they are redirected. */ |
1283 | static int setup_redirects(struct child_prog *prog, int squirrel[]) | 1244 | static int setup_redirects(struct child_prog *prog, int squirrel[]) |
@@ -1826,7 +1787,6 @@ static int run_pipe_real(struct pipe *pi) | |||
1826 | } | 1787 | } |
1827 | #endif | 1788 | #endif |
1828 | /* in non-interactive case fatal sigs are already SIG_DFL */ | 1789 | /* in non-interactive case fatal sigs are already SIG_DFL */ |
1829 | close_all(); | ||
1830 | if (nextin != 0) { | 1790 | if (nextin != 0) { |
1831 | dup2(nextin, 0); | 1791 | dup2(nextin, 0); |
1832 | close(nextin); | 1792 | close(nextin); |
@@ -3183,7 +3143,7 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, | |||
3183 | 3143 | ||
3184 | p = generate_stream_from_list(inner.list_head); | 3144 | p = generate_stream_from_list(inner.list_head); |
3185 | if (p == NULL) return 1; | 3145 | if (p == NULL) return 1; |
3186 | mark_open(fileno(p)); | 3146 | close_on_exec_on(fileno(p)); |
3187 | setup_file_in_str(&pipe_str, p); | 3147 | setup_file_in_str(&pipe_str, p); |
3188 | 3148 | ||
3189 | /* now send results of command back into original context */ | 3149 | /* now send results of command back into original context */ |
@@ -3206,7 +3166,6 @@ static int process_command_subs(o_string *dest, struct p_context *ctx, | |||
3206 | * to do better, by using wait(), and keeping track of background jobs | 3166 | * to do better, by using wait(), and keeping track of background jobs |
3207 | * at the same time. That would be a lot of work, and contrary | 3167 | * at the same time. That would be a lot of work, and contrary |
3208 | * to the KISS philosophy of this program. */ | 3168 | * to the KISS philosophy of this program. */ |
3209 | mark_closed(fileno(p)); | ||
3210 | retcode = fclose(p); | 3169 | retcode = fclose(p); |
3211 | free_pipe_list(inner.list_head, 0); | 3170 | free_pipe_list(inner.list_head, 0); |
3212 | debug_printf("closed FILE from child, retcode=%d\n", retcode); | 3171 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
@@ -3718,9 +3677,8 @@ int hush_main(int argc, char **argv) | |||
3718 | debug_printf("sourcing /etc/profile\n"); | 3677 | debug_printf("sourcing /etc/profile\n"); |
3719 | input = fopen("/etc/profile", "r"); | 3678 | input = fopen("/etc/profile", "r"); |
3720 | if (input != NULL) { | 3679 | if (input != NULL) { |
3721 | mark_open(fileno(input)); | 3680 | close_on_exec_on(fileno(input)); |
3722 | parse_and_run_file(input); | 3681 | parse_and_run_file(input); |
3723 | mark_closed(fileno(input)); | ||
3724 | fclose(input); | 3682 | fclose(input); |
3725 | } | 3683 | } |
3726 | } | 3684 | } |