diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2004-01-17 03:20:46 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2004-01-17 03:20:46 +0000 |
commit | a277e026635e110bdb3cede02b254019cc653918 (patch) | |
tree | df22e9167f11ab8c81995a92484ce5e44c4fabfb /networking/inetd.c | |
parent | ff6ec8a2ae2d45fbd70159b35695e4f3f1736c4d (diff) | |
download | busybox-w32-a277e026635e110bdb3cede02b254019cc653918.tar.gz busybox-w32-a277e026635e110bdb3cede02b254019cc653918.tar.bz2 busybox-w32-a277e026635e110bdb3cede02b254019cc653918.zip |
The functions setconfig, enter and bump_nofile were only called once, marge them into the calling
function.
Diffstat (limited to 'networking/inetd.c')
-rw-r--r-- | networking/inetd.c | 126 |
1 files changed, 50 insertions, 76 deletions
diff --git a/networking/inetd.c b/networking/inetd.c index db0ee814b..d225527a9 100644 --- a/networking/inetd.c +++ b/networking/inetd.c | |||
@@ -267,7 +267,6 @@ static int maxsock; | |||
267 | static int timingout; | 267 | static int timingout; |
268 | static int rlim_ofile_cur = OPEN_MAX; | 268 | static int rlim_ofile_cur = OPEN_MAX; |
269 | static const char *CONFIG = _PATH_INETDCONF; | 269 | static const char *CONFIG = _PATH_INETDCONF; |
270 | static FILE *fconfig; | ||
271 | 270 | ||
272 | static void | 271 | static void |
273 | syslog_err_and_discard_dg(int se_socktype, const char *msg, ...) | 272 | syslog_err_and_discard_dg(int se_socktype, const char *msg, ...) |
@@ -286,20 +285,6 @@ syslog_err_and_discard_dg(int se_socktype, const char *msg, ...) | |||
286 | _exit(1); | 285 | _exit(1); |
287 | } | 286 | } |
288 | 287 | ||
289 | static FILE *setconfig(void) | ||
290 | { | ||
291 | FILE *f = fconfig; | ||
292 | |||
293 | if (f != NULL) { | ||
294 | fseek(f, 0L, L_SET); | ||
295 | } else { | ||
296 | f = fconfig = fopen(CONFIG, "r"); | ||
297 | if(f == NULL) | ||
298 | syslog(LOG_ERR, "%s: %m", CONFIG); | ||
299 | } | ||
300 | return f; | ||
301 | } | ||
302 | |||
303 | static char *skip(char **cpp) | 288 | static char *skip(char **cpp) |
304 | { | 289 | { |
305 | char *cp = *cpp; | 290 | char *cp = *cpp; |
@@ -473,62 +458,6 @@ static void setproctitle(char *a, int s) | |||
473 | } | 458 | } |
474 | #endif /* INETD_FEATURE_ENABLED */ | 459 | #endif /* INETD_FEATURE_ENABLED */ |
475 | 460 | ||
476 | static struct servtab *enter(struct servtab *cp) | ||
477 | { | ||
478 | struct servtab *sep; | ||
479 | sigset_t oldmask; | ||
480 | |||
481 | sep = (struct servtab *)malloc(sizeof (*sep)); | ||
482 | if (sep == NULL) { | ||
483 | syslog_err_and_discard_dg(SOCK_STREAM, bb_msg_memory_exhausted); | ||
484 | } | ||
485 | *sep = *cp; | ||
486 | sep->se_fd = -1; | ||
487 | sigprocmask(SIG_BLOCK, &blockmask, &oldmask); | ||
488 | sep->se_next = servtab; | ||
489 | servtab = sep; | ||
490 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
491 | return (sep); | ||
492 | } | ||
493 | |||
494 | static int bump_nofile(void) | ||
495 | { | ||
496 | #ifdef RLIMIT_NOFILE | ||
497 | |||
498 | #define FD_CHUNK 32 | ||
499 | |||
500 | struct rlimit rl; | ||
501 | |||
502 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { | ||
503 | syslog(LOG_ERR, "getrlimit: %m"); | ||
504 | return -1; | ||
505 | } | ||
506 | rl.rlim_cur = rl.rlim_max < (rl.rlim_cur + FD_CHUNK) ? rl.rlim_max : (rl.rlim_cur + FD_CHUNK); | ||
507 | if (rl.rlim_cur <= rlim_ofile_cur) { | ||
508 | syslog(LOG_ERR, | ||
509 | #if _FILE_OFFSET_BITS == 64 | ||
510 | "bump_nofile: cannot extend file limit, max = %lld", | ||
511 | #else | ||
512 | "bump_nofile: cannot extend file limit, max = %ld", | ||
513 | #endif | ||
514 | rl.rlim_cur); | ||
515 | return -1; | ||
516 | } | ||
517 | |||
518 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { | ||
519 | syslog(LOG_ERR, "setrlimit: %m"); | ||
520 | return -1; | ||
521 | } | ||
522 | |||
523 | rlim_ofile_cur = rl.rlim_cur; | ||
524 | return 0; | ||
525 | |||
526 | #else | ||
527 | syslog(LOG_ERR, "bump_nofile: cannot extend file limit"); | ||
528 | return -1; | ||
529 | #endif | ||
530 | } | ||
531 | |||
532 | 461 | ||
533 | static void setup(struct servtab *sep) | 462 | static void setup(struct servtab *sep) |
534 | { | 463 | { |
@@ -560,8 +489,39 @@ static void setup(struct servtab *sep) | |||
560 | nsock++; | 489 | nsock++; |
561 | if (sep->se_fd > maxsock) { | 490 | if (sep->se_fd > maxsock) { |
562 | maxsock = sep->se_fd; | 491 | maxsock = sep->se_fd; |
563 | if (maxsock > rlim_ofile_cur - FD_MARGIN) | 492 | if (maxsock > rlim_ofile_cur - FD_MARGIN) { |
564 | bump_nofile(); | 493 | #ifdef RLIMIT_NOFILE |
494 | # define FD_CHUNK 32 | ||
495 | struct rlimit rl; | ||
496 | |||
497 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { | ||
498 | syslog(LOG_ERR, "getrlimit: %m"); | ||
499 | return; | ||
500 | } | ||
501 | rl.rlim_cur = rl.rlim_max < (rl.rlim_cur + FD_CHUNK) ? rl.rlim_max : (rl.rlim_cur + FD_CHUNK); | ||
502 | if (rl.rlim_cur <= rlim_ofile_cur) { | ||
503 | syslog(LOG_ERR, | ||
504 | # if _FILE_OFFSET_BITS == 64 | ||
505 | "bump_nofile: cannot extend file limit, max = %lld", | ||
506 | # else | ||
507 | "bump_nofile: cannot extend file limit, max = %ld", | ||
508 | # endif | ||
509 | rl.rlim_cur); | ||
510 | return; | ||
511 | } | ||
512 | |||
513 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { | ||
514 | syslog(LOG_ERR, "setrlimit: %m"); | ||
515 | return; | ||
516 | } | ||
517 | |||
518 | rlim_ofile_cur = rl.rlim_cur; | ||
519 | return; | ||
520 | #else | ||
521 | syslog(LOG_ERR, "bump_nofile: cannot extend file limit"); | ||
522 | return; | ||
523 | #endif /* RLIMIT_NOFILE */ | ||
524 | } | ||
565 | } | 525 | } |
566 | } | 526 | } |
567 | 527 | ||
@@ -572,8 +532,16 @@ static void config(int signum) | |||
572 | unsigned n; | 532 | unsigned n; |
573 | 533 | ||
574 | (void)signum; | 534 | (void)signum; |
575 | if (setconfig() == NULL) | 535 | |
576 | return; | 536 | if (fconfig != NULL) { |
537 | fseek(fconfig, 0L, L_SET); | ||
538 | } else { | ||
539 | fconfig = fopen(CONFIG, "r"); | ||
540 | if (fconfig == NULL) { | ||
541 | syslog(LOG_ERR, "%s: %m", CONFIG); | ||
542 | return; | ||
543 | } | ||
544 | } | ||
577 | 545 | ||
578 | for (sep = servtab; sep; sep = sep->se_next) | 546 | for (sep = servtab; sep; sep = sep->se_next) |
579 | sep->se_checked = 0; | 547 | sep->se_checked = 0; |
@@ -614,7 +582,13 @@ static void config(int signum) | |||
614 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | 582 | sigprocmask(SIG_SETMASK, &oldmask, NULL); |
615 | freeconfig(cp); | 583 | freeconfig(cp); |
616 | } else { | 584 | } else { |
617 | sep = enter(cp); | 585 | sep = (struct servtab *)xmalloc(sizeof (*sep)); |
586 | *sep = *cp; | ||
587 | sep->se_fd = -1; | ||
588 | sigprocmask(SIG_BLOCK, &blockmask, &oldmask); | ||
589 | sep->se_next = servtab; | ||
590 | servtab = sep; | ||
591 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
618 | } | 592 | } |
619 | sep->se_checked = 1; | 593 | sep->se_checked = 1; |
620 | 594 | ||