int check_wanup(void) { int up = 0; int proto; char buf1[64]; char buf2[64]; const char *name; int f; struct ifreq ifr; proto = get_wan_proto(); if (proto == WP_DISABLED) return 0; if ((proto == WP_PPTP) || (proto == WP_L2TP) || (proto == WP_PPPOE) || (proto == WP_HEARTBEAT)) { if (f_read_string("/tmp/ppp/link", buf1, sizeof(buf1)) > 0) { if (proto == WP_HEARTBEAT) { // contains pid of bpalogin if (strcmp(psname(atoi(buf1), buf2, sizeof(buf2)), "bpalogin") == 0) { up = 1; } } else { // contains the base name of a file in /var/run/ containing pid of a daemon snprintf(buf2, sizeof(buf2), "/var/run/%s.pid", buf1); if (f_read_string(buf2, buf1, sizeof(buf1)) > 0) { name = psname(atoi(buf1), buf2, sizeof(buf2)); if (proto == WP_PPPOE) { if (strcmp(name, "pppoecd") == 0) up = 1; } else { if (strcmp(name, "pppd") == 0) up = 1; } } } if (!up) { unlink("/tmp/ppp/link"); cprintf("required daemon not found, assuming link is dead\n"); } } } else if (!nvram_match("wan_ipaddr", "0.0.0.0")) { up = 1; } if ((up) && ((f = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)) { strlcpy(ifr.ifr_name, nvram_safe_get("wan_iface"), sizeof(ifr.ifr_name)); if (ioctl(f, SIOCGIFFLAGS, &ifr) < 0) up = 0; close(f); if ((ifr.ifr_flags & IFF_UP) == 0) up = 0; } return up; }
/* @return: * 0: success * -1: invalid parameter * 1: wait pending rc_service timeout */ static int notify_rc_internal(const char *event_name, bool do_wait, int wait) { int i; char p1[16], p2[16]; if (!event_name || wait < 0) return -1; psname(nvram_get_int("rc_service_pid"), p1, sizeof(p1)); psname(getpid(), p2, sizeof(p2)); _dprintf("%s %d:notify_rc: %s\n", p2, getpid(), event_name); logmessage("rc_service", "%s %d:notify_rc %s", p2, getpid(), event_name); i=wait; int first_try = 1, got_right = 1; while ((!nvram_match("rc_service", "")) && (i-- > 0)) { if(first_try){ logmessage("rc_service", "%s is waitting %s...", event_name, nvram_safe_get("rc_service")); first_try = 0; } _dprintf("%d %s: wait for previous script(%d/%d): %s %d %s.\n", getpid(), p2, i, wait, nvram_safe_get("rc_service"), nvram_get_int("rc_service_pid"), p1); sleep(1); if(i <= 0) got_right = 0; } if(!got_right){ logmessage("rc_service", "skip the event: %s.", event_name); _dprintf("rc_service: skip the event: %s.\n", event_name); return 1; } nvram_set("rc_service", event_name); nvram_set_int("rc_service_pid", getpid()); kill(1, SIGUSR1); if(do_wait) { i = wait; while((nvram_match("rc_service", (char *)event_name))&&(i-- > 0)) { _dprintf("%s %d: waiting after %d/%d.\n", event_name, getpid(), i, wait); sleep(1); } } return 0; }
/* @return: * 0: success * -1: invalid parameter * 1: wait pending rc_service timeout */ static int notify_rc_internal(const char *event_name, bool do_wait, int wait) { int i; char p2[16]; if (!event_name || wait < 0) return -1; psname(getpid(), p2, sizeof(p2)); _dprintf("%s %d:notify_rc: %s\n", p2, getpid(), event_name); logmessage_normal("rc_service", "%s %d:notify_rc %s", p2, getpid(), event_name); if (!wait_rc_service(wait)) { logmessage_normal("rc_service", "skip the event: %s.", event_name); _dprintf("rc_service: skip the event: %s.\n", event_name); return 1; } nvram_set("rc_service", event_name); nvram_set_int("rc_service_pid", getpid()); kill(1, SIGUSR1); if(do_wait) { i = wait; while((nvram_match("rc_service", (char *)event_name))&&(i-- > 0)) { _dprintf("%s %d: waiting after %d/%d.\n", event_name, getpid(), i, wait); sleep(1); } if(i == 0 && nvram_match("rc_service", (char *)event_name)) return 2; } return 0; }
static int _pidof(const char *name, pid_t** pids) { const char *p; char *e; DIR *dir; struct dirent *de; pid_t i; int count; char buf[256]; count = 0; *pids = NULL; if ((p = strchr(name, '/')) != NULL) name = p + 1; if ((dir = opendir("/proc")) != NULL) { while ((de = readdir(dir)) != NULL) { i = strtol(de->d_name, &e, 10); if (*e != 0) continue; if (strcmp(name, psname(i, buf, sizeof(buf))) == 0) { if ((*pids = realloc(*pids, sizeof(pid_t) * (count + 1))) == NULL) { return -1; } (*pids)[count++] = i; } } } closedir(dir); return count; }
/* * int wait_rc_service(int wait) * wait: seconds to wait and check * * @return: * 0: no right * 1: get right */ int wait_rc_service(int wait) { int i=wait; int first_try = 1; char p1[16]; psname(nvram_get_int("rc_service_pid"), p1, sizeof(p1)); while (!nvram_match("rc_service", "")) { if(--i < 0) return 0; if(first_try){ logmessage_normal("rc_service", "waitting \"%s\" via %s ...", nvram_safe_get("rc_service"), p1); first_try = 0; } _dprintf("%d: wait for previous script(%d/%d): %s %d %s.\n", getpid(), i, wait, nvram_safe_get("rc_service"), nvram_get_int("rc_service_pid"), p1); sleep(1); } return 1; }
int check_wanup(void) { int up = 0; int proto; char buf1[64]; char buf2[64]; const char *name; int f; struct ifreq ifr; proto = get_wan_proto(); if (proto == WP_DISABLED) { if (nvram_match("boardrev", "0x11")) { // Ovislink 1600GL - led "connected" off led(LED_WHITE,LED_OFF); } if (nvram_match("boardtype", "0x052b") && nvram_match("boardrev", "0x1204")) { //rt-n15u wan led off led(LED_WHITE,LED_OFF); } return 0; } if ((proto == WP_PPTP) || (proto == WP_L2TP) || (proto == WP_PPPOE) || (proto == WP_PPP3G)) { if (f_read_string("/tmp/ppp/link", buf1, sizeof(buf1)) > 0) { // contains the base name of a file in /var/run/ containing pid of a daemon snprintf(buf2, sizeof(buf2), "/var/run/%s.pid", buf1); if (f_read_string(buf2, buf1, sizeof(buf1)) > 0) { name = psname(atoi(buf1), buf2, sizeof(buf2)); if (strcmp(name, "pppd") == 0) up = 1; } else { _dprintf("%s: error reading %s\n", __FUNCTION__, buf2); } if (!up) { unlink("/tmp/ppp/link"); _x_dprintf("required daemon not found, assuming link is dead\n"); } } else { _x_dprintf("%s: error reading %s\n", __FUNCTION__, "/tmp/ppp/link"); } } else if (!nvram_match("wan_ipaddr", "0.0.0.0")) { up = 1; } else { _x_dprintf("%s: default !up\n", __FUNCTION__); } if ((up) && ((f = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)) { strlcpy(ifr.ifr_name, nvram_safe_get("wan_iface"), sizeof(ifr.ifr_name)); if (ioctl(f, SIOCGIFFLAGS, &ifr) < 0) { up = 0; _x_dprintf("%s: SIOCGIFFLAGS\n", __FUNCTION__); } close(f); if ((ifr.ifr_flags & IFF_UP) == 0) { up = 0; _x_dprintf("%s: !IFF_UP\n", __FUNCTION__); } } if (nvram_match("boardrev", "0x11")) { // Ovislink 1600GL - led "connected" on led(LED_WHITE,up); } if (nvram_match("boardtype", "0x052b") && nvram_match("boardrev", "0x1204")) { //rt-n15u wan led on led(LED_WHITE,up); } return up; }