diff options
author | Maksym Kryzhanovskyy <xmaks@email.cz> | 2010-07-08 02:47:25 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-07-08 02:47:25 +0200 |
commit | 9388b4e72051b031b84a1345cc763757dbd9a699 (patch) | |
tree | 506e15e37e463911fb01d3b7c620a6ff4786afe8 | |
parent | 1d36f24bcb22d06b007f162802cbc5a251f3e612 (diff) | |
download | busybox-w32-9388b4e72051b031b84a1345cc763757dbd9a699.tar.gz busybox-w32-9388b4e72051b031b84a1345cc763757dbd9a699.tar.bz2 busybox-w32-9388b4e72051b031b84a1345cc763757dbd9a699.zip |
ifplugd: code shrink
function old new delta
detect_link 122 221 +99
api_modes - 7 +7
maybe_up_new_iface 27 33 +6
ifplugd_main 1143 1089 -54
detect_link_auto 117 - -117
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 2/1 up/down: 112/-171) Total: -59 bytes
Signed-off-by: Maksym Kryzhanovskyy <xmaks@email.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/ifplugd.c | 95 |
1 files changed, 35 insertions, 60 deletions
diff --git a/networking/ifplugd.c b/networking/ifplugd.c index 8cb07db5d..eb7442881 100644 --- a/networking/ifplugd.c +++ b/networking/ifplugd.c | |||
@@ -79,6 +79,7 @@ enum { // api mode | |||
79 | API_WLAN = 'w', | 79 | API_WLAN = 'w', |
80 | API_IFF = 'i', | 80 | API_IFF = 'i', |
81 | }; | 81 | }; |
82 | static const char api_modes[] ALIGN1 = "aempwi"; | ||
82 | 83 | ||
83 | enum { // interface status | 84 | enum { // interface status |
84 | IFSTATUS_ERR = -1, | 85 | IFSTATUS_ERR = -1, |
@@ -95,6 +96,7 @@ struct globals { | |||
95 | smallint iface_last_status; | 96 | smallint iface_last_status; |
96 | smallint iface_prev_status; | 97 | smallint iface_prev_status; |
97 | smallint iface_exists; | 98 | smallint iface_exists; |
99 | smallint api_method_num; | ||
98 | 100 | ||
99 | /* Used in getopt32, must have sizeof == sizeof(int) */ | 101 | /* Used in getopt32, must have sizeof == sizeof(int) */ |
100 | unsigned poll_time; | 102 | unsigned poll_time; |
@@ -107,7 +109,6 @@ struct globals { | |||
107 | const char *extra_arg; | 109 | const char *extra_arg; |
108 | 110 | ||
109 | smallint (*detect_link_func)(void); | 111 | smallint (*detect_link_func)(void); |
110 | smallint (*cached_detect_link_func)(void); | ||
111 | }; | 112 | }; |
112 | #define G (*ptr_to_globals) | 113 | #define G (*ptr_to_globals) |
113 | #define INIT_G() do { \ | 114 | #define INIT_G() do { \ |
@@ -239,8 +240,8 @@ static void maybe_up_new_iface(void) | |||
239 | G.iface, buf, driver_info.driver, driver_info.version); | 240 | G.iface, buf, driver_info.driver, driver_info.version); |
240 | } | 241 | } |
241 | #endif | 242 | #endif |
242 | 243 | if (G.api_method_num == 0) | |
243 | G.cached_detect_link_func = NULL; | 244 | G.detect_link_func = NULL; |
244 | } | 245 | } |
245 | 246 | ||
246 | static smallint detect_link_mii(void) | 247 | static smallint detect_link_mii(void) |
@@ -348,7 +349,7 @@ static smallint detect_link_wlan(void) | |||
348 | return IFSTATUS_UP; | 349 | return IFSTATUS_UP; |
349 | } | 350 | } |
350 | 351 | ||
351 | static smallint detect_link_auto(void) | 352 | static smallint detect_link(void) |
352 | { | 353 | { |
353 | static const struct { | 354 | static const struct { |
354 | const char *name; | 355 | const char *name; |
@@ -360,32 +361,6 @@ static smallint detect_link_auto(void) | |||
360 | { "wireless extension", &detect_link_wlan }, | 361 | { "wireless extension", &detect_link_wlan }, |
361 | { "IFF_RUNNING" , &detect_link_iff }, | 362 | { "IFF_RUNNING" , &detect_link_iff }, |
362 | }; | 363 | }; |
363 | int i; | ||
364 | smallint iface_status; | ||
365 | smallint sv_logmode; | ||
366 | |||
367 | if (G.cached_detect_link_func) { | ||
368 | iface_status = G.cached_detect_link_func(); | ||
369 | if (iface_status != IFSTATUS_ERR) | ||
370 | return iface_status; | ||
371 | } | ||
372 | |||
373 | sv_logmode = logmode; | ||
374 | for (i = 0; i < ARRAY_SIZE(method); i++) { | ||
375 | logmode = LOGMODE_NONE; | ||
376 | iface_status = method[i].func(); | ||
377 | logmode = sv_logmode; | ||
378 | if (iface_status != IFSTATUS_ERR) { | ||
379 | G.cached_detect_link_func = method[i].func; | ||
380 | bb_error_msg("using %s detection mode", method[i].name); | ||
381 | break; | ||
382 | } | ||
383 | } | ||
384 | return iface_status; | ||
385 | } | ||
386 | |||
387 | static smallint detect_link(void) | ||
388 | { | ||
389 | smallint status; | 364 | smallint status; |
390 | 365 | ||
391 | if (!G.iface_exists) | 366 | if (!G.iface_exists) |
@@ -398,20 +373,38 @@ static smallint detect_link(void) | |||
398 | if (!(option_mask32 & FLAG_NO_AUTO)) | 373 | if (!(option_mask32 & FLAG_NO_AUTO)) |
399 | up_iface(); | 374 | up_iface(); |
400 | 375 | ||
376 | if (!G.detect_link_func) { | ||
377 | if (G.api_method_num == 0) { | ||
378 | int i; | ||
379 | smallint sv_logmode; | ||
380 | |||
381 | sv_logmode = logmode; | ||
382 | for (i = 0; i < ARRAY_SIZE(method); i++) { | ||
383 | logmode = LOGMODE_NONE; | ||
384 | status = method[i].func(); | ||
385 | logmode = sv_logmode; | ||
386 | if (status != IFSTATUS_ERR) { | ||
387 | G.detect_link_func = method[i].func; | ||
388 | bb_error_msg("using %s detection mode", method[i].name); | ||
389 | goto _2; | ||
390 | } | ||
391 | } | ||
392 | goto _1; | ||
393 | } | ||
394 | G.detect_link_func = method[G.api_method_num - 1].func; | ||
395 | } | ||
396 | |||
401 | status = G.detect_link_func(); | 397 | status = G.detect_link_func(); |
398 | _1: | ||
402 | if (status == IFSTATUS_ERR) { | 399 | if (status == IFSTATUS_ERR) { |
403 | if (option_mask32 & FLAG_IGNORE_FAIL) | 400 | if (option_mask32 & FLAG_IGNORE_FAIL) |
404 | status = IFSTATUS_DOWN; | 401 | status = IFSTATUS_DOWN; |
405 | if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE) | 402 | else if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE) |
406 | status = IFSTATUS_UP; | 403 | status = IFSTATUS_UP; |
404 | else if (G.api_method_num == 0) | ||
405 | bb_error_msg("can't detect link status"); | ||
407 | } | 406 | } |
408 | 407 | _2: | |
409 | if (status == IFSTATUS_ERR | ||
410 | && G.detect_link_func == detect_link_auto | ||
411 | ) { | ||
412 | bb_error_msg("can't detect link status"); | ||
413 | } | ||
414 | |||
415 | if (status != G.iface_last_status) { | 408 | if (status != G.iface_last_status) { |
416 | G.iface_prev_status = G.iface_last_status; | 409 | G.iface_prev_status = G.iface_last_status; |
417 | G.iface_last_status = status; | 410 | G.iface_last_status = status; |
@@ -523,6 +516,7 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv) | |||
523 | const char *iface_status_str; | 516 | const char *iface_status_str; |
524 | struct pollfd netlink_pollfd[1]; | 517 | struct pollfd netlink_pollfd[1]; |
525 | unsigned opts; | 518 | unsigned opts; |
519 | const char *api_mode_found; | ||
526 | #if ENABLE_FEATURE_PIDFILE | 520 | #if ENABLE_FEATURE_PIDFILE |
527 | char *pidfile_name; | 521 | char *pidfile_name; |
528 | pid_t pid_from_pidfile; | 522 | pid_t pid_from_pidfile; |
@@ -551,29 +545,10 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv) | |||
551 | if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0) | 545 | if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0) |
552 | bb_error_msg_and_die("daemon already running"); | 546 | bb_error_msg_and_die("daemon already running"); |
553 | #endif | 547 | #endif |
554 | 548 | api_mode_found = strchr(api_modes, G.api_mode[0]); | |
555 | switch (G.api_mode[0]) { | 549 | if (!api_mode_found) |
556 | case API_AUTO: | ||
557 | G.detect_link_func = detect_link_auto; | ||
558 | break; | ||
559 | case API_ETHTOOL: | ||
560 | G.detect_link_func = detect_link_ethtool; | ||
561 | break; | ||
562 | case API_MII: | ||
563 | G.detect_link_func = detect_link_mii; | ||
564 | break; | ||
565 | case API_PRIVATE: | ||
566 | G.detect_link_func = detect_link_priv; | ||
567 | break; | ||
568 | case API_WLAN: | ||
569 | G.detect_link_func = detect_link_wlan; | ||
570 | break; | ||
571 | case API_IFF: | ||
572 | G.detect_link_func = detect_link_iff; | ||
573 | break; | ||
574 | default: | ||
575 | bb_error_msg_and_die("unknown API mode '%s'", G.api_mode); | 550 | bb_error_msg_and_die("unknown API mode '%s'", G.api_mode); |
576 | } | 551 | G.api_method_num = api_mode_found - api_modes; |
577 | 552 | ||
578 | if (!(opts & FLAG_NO_DAEMON)) | 553 | if (!(opts & FLAG_NO_DAEMON)) |
579 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); | 554 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); |