Пример #1
0
int main(int argc, char **argv)
{
	char *base;
	int f;

	/*
		Make sure std* are valid since several functions attempt to close these
		handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
	*/

	if ((f = open("/dev/null", O_RDWR)) < 0) {
	}
	else if(f < 3) {
		dup(f);
		dup(f);
	}
	else {
		close(f);
	}

	base = strrchr(argv[0], '/');
	base = base ? base + 1 : argv[0];

#if 0
	if (strcmp(base, "rc") == 0) {
		if (argc < 2) return 1;
		if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
		if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
		if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
		++argv;
		--argc;
		base = argv[0];
	}
#endif

#ifdef RTCONFIG_RALINK
    if(getpid() != 1)
    {
#endif

#if defined(DEBUG_NOISY)
	if (nvram_match("debug_logrc", "1")) {
		int i;

		cprintf("[rc %d] ", getpid());
		for (i = 0; i < argc; ++i) {
			cprintf("%s ", argv[i]);
		}
		cprintf("\n");

	}
#endif

#if defined(DEBUG_NOISY)
	if (nvram_match("debug_ovrc", "1")) {
		char tmp[256];
		char *a[32];

		realpath(argv[0], tmp);
		if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
			sprintf(tmp, "%s%s", "/tmp/", base);
			if (f_exists(tmp)) {
				cprintf("[rc] override: %s\n", tmp);
				memcpy(a, argv, argc * sizeof(a[0]));
				a[argc] = 0;
				a[0] = tmp;
				execvp(tmp, a);
				exit(0);
			}
		}
	}
#endif
#ifdef RTCONFIG_RALINK
    }
#endif
	const applets_t *a;
	for (a = applets; a->name; ++a) {
		if (strcmp(base, a->name) == 0) {
			openlog(base, LOG_PID, LOG_USER);
			return a->main(argc, argv);
		}
	}


	if(!strcmp(base, "restart_wireless")){
		printf("restart wireless...\n");
		restart_wireless();
		return 0;
	}
        else if(!strcmp(base, "nvram_erase")){
                erase_nvram();
                return 0;
        }
#ifdef RTCONFIG_USB
	else if(!strcmp(base, "get_apps_name")){
		if(argc != 2){
			printf("Usage: get_apps_name [File name]\n");
			return 0;
		}

		return get_apps_name(argv[1]);
	}
	else if(!strcmp(base, "asus_sd")){
		if(argc != 3){
			printf("Usage: asus_sd [device_name] [action]\n");
			return 0;
		}

		return asus_sd(argv[1], argv[2]);
	}
	else if(!strcmp(base, "asus_lp")){
		if(argc != 3){
			printf("Usage: asus_lp [device_name] [action]\n");
			return 0;
		}

		return asus_lp(argv[1], argv[2]);
	}
	else if(!strcmp(base, "asus_sg")){
		if(argc != 3){
			printf("Usage: asus_sg [device_name] [action]\n");
			return 0;
		}

		return asus_sg(argv[1], argv[2]);
	}
	else if(!strcmp(base, "asus_sr")){
		if(argc != 3){
			printf("Usage: asus_sr [device_name] [action]\n");
			return 0;
		}

		return asus_sr(argv[1], argv[2]);
	}
	else if(!strcmp(base, "asus_tty")){
		if(argc != 3){
			printf("Usage: asus_tty [device_name] [action]\n");
			return 0;
		}

		return asus_tty(argv[1], argv[2]);
	}
	else if(!strcmp(base, "asus_usbbcm")){
		if(argc != 3){
			printf("Usage: asus_usbbcm [device_name] [action]\n");
			return 0;
		}

		return asus_usbbcm(argv[1], argv[2]);
	}
	else if(!strcmp(base, "asus_usb_interface")){
		if(argc != 3){
			printf("Usage: asus_usb_interface [device_name] [action]\n");
			return 0;
		}

		return asus_usb_interface(argv[1], argv[2]);
	}
	else if (!strcmp(base, "usb_notify")) {
#if defined(RTCONFIG_APP_PREINSTALLED) || defined(RTCONFIG_APP_NETINSTALLED)
		usb_notify();
#endif

		return 0;
	}
#if defined(RTCONFIG_APP_PREINSTALLED) || defined(RTCONFIG_APP_NETINSTALLED)
	else if(!strcmp(base, "run_app_script")){
		if(argc != 3){
			printf("Usage: run_app_script [Package name | allpkg] [APP action]\n");
			return 0;
		}

		if(!strcmp(argv[1], "allpkg"))
			return run_app_script(NULL, argv[2]);
		else
			return run_app_script(argv[1], argv[2]);
	}
	else if (!strcmp(base, "chk_app_state")) {
#define PID_FILE "/var/run/chk_app_state.pid"
		FILE *fp;
		char chk_value[4];

		if(f_read_string(PID_FILE, chk_value, 4) > 0
				&& atoi(chk_value) != getpid()){
			_dprintf("Already running!\n");
			return 0;
		}

		if((fp = fopen(PID_FILE, "w")) == NULL){
			_dprintf("Can't open the pid file!\n");
			return 0;
		}

		fprintf(fp, "%d", getpid());
		fclose(fp);

		memset(chk_value, 0, 4);
		strncpy(chk_value, nvram_safe_get("apps_state_switch"), 4);
		if(strcmp(chk_value, "")){
			if(atoi(chk_value) != APPS_SWITCH_FINISHED && !pids("app_switch.sh")){
				_dprintf("Don't have the switch script.\n");
				nvram_set("apps_state_switch", "");
			}

			unlink(PID_FILE);
			return 0;
		}

		memset(chk_value, 0, 4);
		strncpy(chk_value, nvram_safe_get("apps_state_install"), 4);
		if(strcmp(chk_value, "")){
			if(atoi(chk_value) != APPS_INSTALL_FINISHED && !pids("app_install.sh")){
				_dprintf("Don't have the install script.\n");
				nvram_set("apps_state_install", "");
			}

			unlink(PID_FILE);
			return 0;
		}

		memset(chk_value, 0, 4);
		strncpy(chk_value, nvram_safe_get("apps_state_upgrade"), 4);
		if(strcmp(chk_value, "")){
			if(atoi(chk_value) != APPS_UPGRADE_FINISHED && !pids("app_upgrade.sh")){
				_dprintf("Don't have the upgrade script.\n");
				nvram_set("apps_state_upgrade", "");
			}

			unlink(PID_FILE);
			return 0;
		}

		memset(chk_value, 0, 4);
		strncpy(chk_value, nvram_safe_get("apps_state_enable"), 4);
		if(strcmp(chk_value, "")){
			if(atoi(chk_value) != APPS_ENABLE_FINISHED && !pids("app_set_enabled.sh")){
				_dprintf("Don't have the enable script.\n");
				nvram_set("apps_state_enable", "");
			}

			unlink(PID_FILE);
			return 0;
		}

		unlink(PID_FILE);
		return 0;
	}
#endif
#endif
	else if(!strcmp(base, "ATE")) {
		if( argc == 2 || argc == 3 || argc == 4) {
			asus_ate_command(argv[1], argv[2], argv[3]);
		}
		else
			printf("ATE_ERROR\n");
                return 0;
	}
#if defined(RTCONFIG_RALINK)
	else if (!strcmp(base, "FWRITE")) {
		if (argc == 3)
			return FWRITE(argv[1], argv[2]);
		else
		return 0;
	}
	else if (!strcmp(base, "FREAD")) {
		if (argc == 3)
		{
			unsigned int addr;
			int len;
			addr = strtoul(argv[1], NULL, 16);
			if(argv[2][0] == '0' && argv[2][1] == 'x')
				len  = (int) strtoul(argv[2], NULL, 16);
			else
				len  = (int) strtoul(argv[2], NULL, 10);

			if(len > 0)
				return FREAD(addr, len);
		}
		printf("ATE_ERROR\n");
		return 0;
	}
	else if (!strcmp(base, "asuscfe_5g")) {
		if (argc == 2)
			return asuscfe(argv[1], WIF_5G);
		else
			return EINVAL;
	}
	else if (!strcmp(base, "asuscfe_2g")) {
		if (argc == 2)
			return asuscfe(argv[1], WIF_2G);
		else
			return EINVAL;
	}
	else if (!strcmp(base, "stainfo_2g")) {
		return stainfo(0);
	}
	else if (!strcmp(base, "stainfo_5g")) {
		return stainfo(1);
	}
#ifdef RTCONFIG_DSL
	else if(!strcmp(base, "gen_ralink_config")){
		if(argc != 3){
			printf("Usage: gen_ralink_config [band] [is_iNIC]\n");
			return 0;
		}
		return gen_ralink_config(atoi(argv[1]), atoi(argv[2]));
	}
#endif
#endif
	else if(!strcmp(base, "run_telnetd")) {
		run_telnetd();
		return 0;
	}
#if defined(RTCONFIG_PPTPD) || defined(RTCONFIG_ACCEL_PPTPD)
	else if(!strcmp(base, "run_pptpd")) {
		start_pptpd();
		return 0;
	}
#endif
#ifdef RTCONFIG_PARENTALCTRL
	else if(!strcmp(base, "pc")) {
		pc_main(argc, argv);
		return 0;
	}
#endif
#ifdef CONFIG_BCMWL5
	else if (!strcmp(base, "wlcscan")) {
		return wlcscan_main();
	}
#endif
#ifdef RTCONFIG_WIRELESSREPEATER
	else if (!strcmp(base, "wlcconnect")) {
		return wlcconnect_main();
	}
	else if (!strcmp(base, "setup_dnsmq")) {
		if(argc != 2)
			return 0;

		return setup_dnsmq(atoi(argv[1]));
	}
#endif
	else if (!strcmp(base, "add_multi_routes")) {
		return add_multi_routes();
	}
	else if (!strcmp(base, "led_ctrl")) {
		if (argc != 3)
			return 0;

		return(led_control(atoi(argv[1]), atoi(argv[2])));
	}
#ifdef RTCONFIG_BCMARM
        /* mtd-erase2 [device] */
        else if (!strcmp(base, "mtd-erase2")) {
                if (argv[1] && ((!strcmp(argv[1], "boot")) ||
                        (!strcmp(argv[1], "linux")) ||
                        (!strcmp(argv[1], "linux2")) ||
                        (!strcmp(argv[1], "rootfs")) ||
                        (!strcmp(argv[1], "rootfs2")) ||
                        (!strcmp(argv[1], "nvram")))) {

                        return mtd_erase(argv[1]);
                } else {
                        fprintf(stderr, "usage: mtd-erase2 [device]\n");
                        return EINVAL;
                }
        }
        /* mtd-write2 [path] [device] */
        else if (!strcmp(base, "mtd-write2")) {
                if (argc >= 3)
                        return mtd_write(argv[1], argv[2]);
                else {
                        fprintf(stderr, "usage: mtd-write2 [path] [device]\n");
                        return EINVAL;
                }
        }
#endif
	else if (!strcmp(base, "free_caches")) {
		int c;
		unsigned int test_num;
		char *set_value = NULL;
		int clean_time = 1;
		int threshold = 0;

		if(argc){
			while((c = getopt(argc, argv, "c:w:t:")) != -1){
				switch(c){
					case 'c': // set the clean-cache mode: 0~3.
						test_num = strtol(optarg, NULL, 10);
        		if(test_num == LONG_MIN || test_num == LONG_MAX){
        			_dprintf("ERROR: unknown value %s...\n", optarg);
							return 0;
						}

						if(test_num < 0 || test_num > 3){
							_dprintf("ERROR: the value %s was over the range...\n", optarg);
							return 0;
						}

						set_value = optarg;

						break;
					case 'w': // set the waited time for cleaning.
						test_num = strtol(optarg, NULL, 10);
        		if(test_num < 0 || test_num == LONG_MIN || test_num == LONG_MAX){
        			_dprintf("ERROR: unknown value %s...\n", optarg);
							return 0;
						}

						clean_time = test_num;

						break;
					case 't': // set the waited time for cleaning.
						test_num = strtol(optarg, NULL, 10);
        		if(test_num < 0 || test_num == LONG_MIN || test_num == LONG_MAX){
        			_dprintf("ERROR: unknown value %s...\n", optarg);
							return 0;
						}

						threshold = test_num;

						break;
					default:
						fprintf(stderr, "Usage: free_caches [ -c clean_mode ] [ -w clean_time ] [ -t threshold ]\n");
						break;
				}
			}
		}

		if(!set_value)
			set_value = FREE_MEM_PAGE;

		free_caches(set_value, clean_time, threshold);

		return 0;
	}
	printf("Unknown applet: %s\n", base);
	return 0;
}
Пример #2
0
static int
start_wpa_supplicant(int unit, int restart)
{
	FILE *fp;
	char tmp[100];
	char prefix[] = "wanXXXXXXXXXX_";
	char options[sizeof("/etc/wpa_supplicantXXXXXXXXXX.conf")];
	char pidfile[sizeof("/var/run/wpa_supplicantXXXXXXXXXX.pid")];
	char *wpa_argv[] = {"/usr/sbin/wpa_supplicant",
		"-B", "-W",
		"-i", NULL,	/* interface */
		"-D", "wired",
		"-c", options,
		"-P", pidfile,
		NULL
	};
	char *cli_argv[] = {"/usr/sbin/wpa_cli",
		"-B",
		"-i", NULL,	/* interface */
		"-a", "/tmp/wpa_cli",
		NULL};
	int ret;

	snprintf(prefix, sizeof(prefix), "wan%d_", unit);
	snprintf(options, sizeof(options), "/etc/wpa_supplicant%d.conf", unit);
	snprintf(pidfile, sizeof(pidfile), "/var/run/wpa_supplicant%d.pid", unit);

	if (restart && pids("wpa_supplicant"))
		return kill_pidfile_s(pidfile, SIGUSR2);

	/* Get interface */
	wpa_argv[4] = nvram_safe_get(strcat_r(prefix, "ifname", tmp));
	cli_argv[3] = wpa_argv[4];

	/* Get driver, wired default */
#ifndef RTCONFIG_RALINK /* Both BCM 5.x and 6.x */
	if (get_switch() == SWITCH_BCM5325)
		wpa_argv[6] = "roboswitch";
#endif

	/* Generate options file */
	if ((fp = fopen(options, "w")) == NULL) {
		perror(options);
		return -1;
	}
	fprintf(fp,
		"ctrl_interface=/var/run/wpa_supplicant\n"
		"ap_scan=0\n"
		"fast_reauth=1\n"
		"network={\n"
		"	key_mgmt=IEEE8021X\n"
		"	eap=MD5\n"
		"	identity=\"%s\"\n"
		"	password=\"%s\"\n"
		"	eapol_flags=0\n"
		"}\n",
		nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp)),
		nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp)));
	fclose(fp);

	/* Start supplicant & monitor */
	ret = _eval(wpa_argv, NULL, 0, NULL);
	if (ret == 0)
		_eval(cli_argv, NULL, 0, NULL);

	return 0;
}
Пример #3
0
// Create list of processes
static void update_processes(Widget processes, bool keep_selection)
{
    StatusDelay delay("Getting list of processes");

    string cmd = sh_command(app_data.ps_command) + " 2>&1";
    FILE *fp = popen(cmd.chars(), "r");
    if (fp == 0)
    {
	delay.outcome = strerror(errno);
	return;
    }

    StringArray all_process_list;
    int c;
    string line = "";
    bool first_line = true;

    while ((c = getc(fp)) != EOF)
    {
	if (c == '\n')
	{
	    if (first_line || valid_ps_line(line, app_data.ps_command))
		all_process_list += line;
#if 0
	    else
		std::clog << "Excluded: " << line << "\n";
#endif

	    if (first_line)
	    {
		// Find first occurrence of `PID' title
		ps_pid_index = line.index(" PID ");
		if (ps_pid_index < 0)
		    ps_pid_index = 0;
	    }

	    line = "";
	    first_line = false;
	}
	else
	{
	    line += c;
	}
    }

    pclose(fp);
    sortProcesses(all_process_list);
    DynIntArray pids(all_process_list.size());

    // If GDB cannot send a signal to the process, we cannot debug it.
    // Try a `kill -0' (via GDB, as it may be setuid) and filter out
    // all processes in the `kill' diagnostic -- that is, all
    // processes that `kill' could not send a signal.
    string kill = "kill -0";

    if (gdb->has_handler_command())
	kill = "/usr/bin/kill -0"; // Bypass built-in SUN DBX command

    int i;
    for (i = 0; i < all_process_list.size(); i++)
    {
	pids[i] = ps_pid(all_process_list[i]);
	if (pids[i])
	    kill += string(" ") + itostring(pids[i]);
    }
#if defined(__sun) 
    // bypass underlying debugger
    // Fix for Sun: use /usr/bin/kill
    string kill_result;
    {
      std::ostringstream os;
      kill += " 2>&1";
      FILE *fp = popen(kill.chars(), "r");
      if (fp != 0)
      {
	  int c;
	  while ((c = getc(fp)) != EOF)
	  {
	      os << (char)c;
	  }
	  pclose(fp);
      }
      kill_result = os;
    }
#else
    string kill_result = gdb_question(gdb->shell_command(kill));
#endif
    i = 0;
    while (i >= 0)
    {
	i = kill_result.index(rxint, i);
	if (i >= 0)
	{
	    int bad_pid = atoi(kill_result.chars() + i);
	    for (int k = 0; k < all_process_list.size(); k++)
	    {
		if (pids[k] != 0 && pids[k] == bad_pid)
		{
#if 0
		    std::clog << "Excluded: " << all_process_list[k] << "\n";
#endif
		    all_process_list[k] = NO_GDB_ANSWER;
		}
	    }
	    i++;
	}
    }

    StringArray process_list;
    for (i = 0; i < all_process_list.size(); i++)
	if (all_process_list[i] != NO_GDB_ANSWER)
	    process_list += all_process_list[i];

    // Now set the selection.
    bool *selected = new bool[process_list.size()];
    for (i = 0; i < process_list.size(); i++)
	selected[i] = false;

    int pos = -1;
    if (keep_selection)
    {
	// Preserve old selection: each PID selected before will also be
	// selected after.
	IntArray selection;
	getPIDs(processes, selection);

	for (i = 0; i < selection.size(); i++)
	{
	    for (int j = 0; j < process_list.size(); j++)
		if (selection[i] == ps_pid(process_list[j]))
		{
		    if (pos < 0)
			pos = j;
		    selected[j] = true;
		}
	}
    }

    if (pos < 0)
    {
	// Create new selection from current file and current pid.
	ProgramInfo info;

	// Check for current pid; if found, highlight it.
	for (i = 0; pos < 0 && i < process_list.size(); i++)
	{
	    if (info.pid != 0 && ps_pid(process_list[i]) == info.pid)
		pos = i;
	}

	if (pos < 0)
	{
	    // Not found? Try leftmost occurrence of process base name.
	    string current_base = basename(info.file.chars());
	    int leftmost = INT_MAX;
	    for (i = 0; i < process_list.size(); i++)
	    {
		int occurrence = process_list[i].index(current_base);
		if (occurrence >= 0 && occurrence < leftmost 
		    && ps_pid(process_list[i]) > 0)
		{
		    leftmost = occurrence;
		    pos = i;
		}
	    }
	}
    }

    if (pos >= 0)
	selected[pos] = true;

    setLabelList(processes, process_list.values(),
		 selected, process_list.size(), true, false);

    if (pos >= 0)
	ListSetAndSelectPos(processes, pos + 1);

    delete[] selected;
}
Пример #4
0
static int
httpd_check_v2()
{
    FILE *fp = NULL;
    int i, httpd_live, http_port;
    char line[80], *login_timestamp;
    long now;
    static int check_count_down = 3;
    static int httpd_timer = 0;

    /* skip 30 seconds after start watchdog */
    if (check_count_down)
    {
        check_count_down--;
        return 1;
    }

    /* check every 30 seconds */
    httpd_timer = (httpd_timer + 1) % 3;
    if (httpd_timer)
        return 1;

    /* check last http login */
    login_timestamp = nvram_safe_get("login_timestamp");
    if (strlen(login_timestamp) < 1)
        return 1;

    now = uptime();
    if (((unsigned long)(now - strtoul(login_timestamp, NULL, 10)) < 60))
        return 1;

#if defined (SUPPORT_HTTPS)
    /* check HTTPS only */
    if (nvram_get_int("http_proto") == 1)
        return 1;
#endif
    remove(DETECT_HTTPD_FILE);

    http_port = nvram_get_int("http_lanport");

    /* httpd will not count 127.0.0.1 */
    doSystem("wget -q http://127.0.0.1:%d/httpd_check.htm -O %s &", http_port, DETECT_HTTPD_FILE);

    httpd_live = 0;
    for (i=0; i < 3; i++)
    {
        if ((fp = fopen(DETECT_HTTPD_FILE, "r")) != NULL)
        {
            if ( fgets(line, sizeof(line), fp) != NULL )
            {
                if (strstr(line, "ASUSTeK"))
                {
                    httpd_live = 1;
                }
            }

            fclose(fp);
        }

        if (httpd_live)
            break;

        /* check port changed */
        if (nvram_get_int("http_lanport") != http_port)
        {
            if (pids("wget"))
                system("killall wget");
            return 1;
        }

        sleep(1);
    }

    if (!httpd_live)
    {
        if (pids("wget"))
            system("killall wget");

        dbg("httpd is so dead!!!\n");

        return 0;
    }

    return 1;
}
Пример #5
0
    bool isProcessRunning(int pid, const std::string &fileName)
    {
      if (pid <= 0)
        return false;

      qiLogDebug() << "Checking if process #" << pid << " is running";
      std::string commandLine;

#if BOOST_OS_WINDOWS
      HANDLE processHandle = OpenProcess(
            PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, static_cast<DWORD>(pid));
      if (!processHandle)
        return false;
      qiLogDebug() << "Got handle for process #" << pid;

      DWORD exitCode = 0xFFFFFF;
      if (!GetExitCodeProcess(processHandle, &exitCode))
        return false;

      if (exitCode != STILL_ACTIVE)
        return false;

      qiLogDebug() << "Process #" << pid << " is running";
      if (fileName.empty())
        return true;

      qiLogDebug() << "Checking process name for #" << pid;
      WCHAR winCommandLine[MAX_PATH];
      DWORD clSize = GetProcessImageFileNameW(
            processHandle, winCommandLine,
            sizeof(winCommandLine) / sizeof(*winCommandLine));
      CloseHandle(processHandle);

      if(clSize <= 0)
        return false;

      commandLine = qi::Path::fromNative(winCommandLine).str();

#elif BOOST_OS_MACOS
      int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
      if (numberOfProcesses == 0)
      {
        qiLogError() << "Cannot get number of processes";
        return false;
      }

      std::vector<int> pids(numberOfProcesses, 0);
      numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, pids.data(), sizeof(int) * pids.size());
      if (numberOfProcesses == 0)
      {
        qiLogError() << "Cannot get list of processes";
        return false;
      }

      if (std::find(pids.begin(), pids.end(), pid) == pids.end())
        return false;

      if (fileName.empty())
        return true;

      char procPidPath[PROC_PIDPATHINFO_MAXSIZE];
      int res = proc_pidpath(pid, procPidPath, sizeof(char) * PROC_PIDPATHINFO_MAXSIZE);
      if (!res)
      {
        qiLogDebug() << "Failed to get process info: " << strerror(errno);
        return false;
      }

      commandLine = procPidPath;

#else // Linux
      std::string pathInProc = "/proc/" + to_string(pid) + "/cmdline";

      bool cmdLineFilePresentButEmpty = false;
      do
      {
        boost::filesystem::ifstream file(pathInProc);
        qiLogDebug() << "process #" << pid << " " << (file.is_open() ? "exists" : "does not exist");
        if (!file)
          return false;

        if (fileName.empty())
        {
          file.close();
          return true;
        }

        std::string buff;
        file >> buff;
        file.close();
        commandLine.assign(buff.data(), std::strlen(buff.c_str()));

        cmdLineFilePresentButEmpty = commandLine.empty();
      }
      while(cmdLineFilePresentButEmpty);
#endif

      qiLogDebug() << "process #" << pid << " full command was: " << commandLine;
      const std::string actualProcessName = qi::Path(commandLine).filename();
      qiLogDebug() << "process #" << pid << " executable was: "
                   << actualProcessName << ", expected: "
                   << (fileName + qi::path::detail::binSuffix());
      return actualProcessName == (fileName + qi::path::detail::binSuffix());
    }
Пример #6
0
void 
stop_telnetd()
{
	if (pids("telnetd"))
		system("killall telnetd");
}
Пример #7
0
int
httpd_check_v2()
{
#if (!defined(W7_LOGO) && !defined(WIFI_LOGO))
	time_t now = uptime();

//	if (!nvram_match("wan_route_x", "IP_Routed"))
//		return 1;

	if (check_count_down)
	{
		check_count_down--;
		httpd_error_count = 0;
		return 1;
	}

        httpd_timer = (httpd_timer + 1) % 2;
        if (httpd_timer) return 1;

	if (nvram_match("v2_debug", "1"))
		dbg("uptime: %d\n", now);

	if (nvram_get("login_timestamp") && ((unsigned long)(now - strtoul(nvram_safe_get("login_timestamp"), NULL, 10)) < 60))
	{
		if (nvram_match("v2_debug", "1"))
			dbg("user login within 1 minutu: %d\n", (unsigned long)(now - strtoul(nvram_safe_get("login_timestamp"), NULL, 10)));

		httpd_error_count = 0;
		return 1;
	}

	int ret = 0;
	FILE *fp = NULL;
	char line[80], cmd[128], url[80];

	/* httpd will not count 127.0.0.1 */
	//sprintf(url, "http://%s/httpd_check.htm", get_lan_ipaddr());
	sprintf(url, "http://%s/httpd_check.htm", "127.0.0.1");
	remove(DETECT_HTTPD_FILE);

	wget_timestamp = uptime();
	memset(wget_timestampstr, 0, 32);
	sprintf(wget_timestampstr, "%lu", wget_timestamp);
	nvram_set("wget_timestamp", wget_timestampstr);
/*
	if (nvram_get("login_timestamp") && !nvram_match("login_timestamp", ""))
	{
		httpd_error_count = 0;
		return 1;
	}
	else
*/
		eval("wget", "-q", url, "-O", DETECT_HTTPD_FILE, "&");

	if ((fp = fopen(DETECT_HTTPD_FILE, "r")) != NULL)
	{
		if ( fgets(line, sizeof(line), fp) != NULL )
		{
			if (strstr(line, "ASUSTeK"))
			{
				if (nvram_match("v2_debug", "1"))
					dbg("httpd is alive!\n");

				ret = 1;
			}
		}

		fclose(fp);
	}
	else
	{
		if (nvram_match("v2_debug", "1"))
			dbg("fopen %s error!\n", DETECT_HTTPD_FILE);

		if (pids("wget"))
			system("killall wget");
	}

	nvram_unset("wget_timestamp");

	if (!ret)
	{
		if (nvram_match("v2_debug", "1"))
			dbg("httpd no response!\n");

		httpd_error_count++;
	}
	else
		httpd_error_count = 0;

	if (nvram_match("v2_debug", "1"))
		dbg("httpd_error_count: %d\n", httpd_error_count);

	if (httpd_error_count > 2)
	{
		dbg("httpd is so dead!!!\n");
		httpd_error_count = 0;
		return 0;
	}
	else
		return 1;
#else
	return 1;
#endif
}
Пример #8
0
	std::unique_ptr<Role> assign_roles(unsigned nprocs) {

		char* hostname = new char[MAX_HOSTNAME_LENGTH+1];
		char other_hostname[(MAX_HOSTNAME_LENGTH+1)*nprocs];

		// LOG(DEBUG) << "Detecting hostname for this process:";

		/* invoke the 'hostname' command and read the value */
		gethostname(hostname, MAX_HOSTNAME_LENGTH);
		
		std::string host(hostname); 
		// LOG(DEBUG) << "\tHostname: '" << hostname << "'";

		/* Collect all the hostnames from all the processes */
		MPI_Allgather( hostname, MAX_HOSTNAME_LENGTH+1, MPI_CHAR, other_hostname, 
					   MAX_HOSTNAME_LENGTH+1, MPI_CHAR, MPI_COMM_WORLD );

		/* free memory */
		delete[] hostname;

		std::set<std::string> hosts;
		for(size_t p=0; p<static_cast<size_t>(nprocs); ++p) {
			hosts.insert( std::string(&other_hostname[p*(MAX_HOSTNAME_LENGTH+1)]) );
		}
	
		// declare communicators
		MPI_Comm node_comm, sched_comm;

		auto fit = std::find(hosts.begin(), hosts.end(), host);
		assert(fit != hosts.end() && "Current host name not found!");

		MPI_Comm_split(MPI_COMM_WORLD, std::distance(hosts.begin(), fit), 0, &node_comm);

		int node_comm_size;
		int node_comm_rank;
		MPI_Comm_size(node_comm, &node_comm_size);
		MPI_Comm_rank(node_comm, &node_comm_rank);
		if (node_comm_rank==0) {
			LOG(DEBUG) << "Node communicator has '" << node_comm_size << "' nodes";
			LOG(DEBUG) << "Number of nodes detected is '" << hosts.size() << "'";
			LOG(DEBUG) << utils::join(hosts.begin(), hosts.end(), ",");
		}

		/* 
		 * If the rank in the new communicator is 0 then this process will act as 
		 * a node scheduler, therefore a communicator between scheduler is create
		 * to connect them 
		 */
		MPI_Comm_split(MPI_COMM_WORLD, node_comm_rank==0, 0, &sched_comm);

		LOG(DEBUG) << "\\@ Initialization completed!";

		int mypid = getpid();
		if (node_comm_rank==0) {
			std::vector<int> node_pids(node_comm_size);

			MPI_Gather(&mypid, 1, MPI_INT, 
					   &node_pids.front(), 1, MPI_INT, 
					   0, node_comm);
			
			Scheduler::Pids pids(node_comm_size);
			for(int i=1;i<node_comm_size;++i) 
				pids[i-1] = { i, node_pids[i] };

			return std::move( std::unique_ptr<Scheduler>( 
						new Scheduler(node_comm, sched_comm, std::move(pids)) ) 
					);
		}

		MPI_Gather(&mypid, 1, MPI_INT, NULL, 0, MPI_INT, 0, node_comm);
		return std::move( std::unique_ptr<Worker>( new Worker(node_comm) ) );
	}
Пример #9
0
int
is_ftp_run(void)
{
	return (pids("vsftpd")) ? 1 : 0;
}
Пример #10
0
static int
httpd_check_v2()
{
#if (!defined(W7_LOGO) && !defined(WIFI_LOGO))
	int i, httpd_live, http_port;
	FILE *fp = NULL;
	char line[80];
	time_t now;
	static int check_count_down = 3;
	static int httpd_timer = 0;
	
	// skip 30 seconds after start watchdog
	if (check_count_down)
	{
		check_count_down--;
		return 1;
	}
	
	// check every 30 seconds
	httpd_timer = (httpd_timer + 1) % 3;
	if (httpd_timer) return 1;
	
	now = uptime();
	if (nvram_get("login_timestamp") && ((unsigned long)(now - strtoul(nvram_safe_get("login_timestamp"), NULL, 10)) < 60))
	{
		return 1;
	}
	
	remove(DETECT_HTTPD_FILE);
	
	http_port = nvram_get_int("http_lanport");
	
	/* httpd will not count 127.0.0.1 */
	doSystem("wget -q http://127.0.0.1:%d/httpd_check.htm -O %s &", http_port, DETECT_HTTPD_FILE);
	
	httpd_live = 0;
	for (i=0; i < 3; i++)
	{
		if ((fp = fopen(DETECT_HTTPD_FILE, "r")) != NULL)
		{
			if ( fgets(line, sizeof(line), fp) != NULL )
			{
				if (strstr(line, "ASUSTeK"))
				{
					httpd_live = 1;
				}
			}
			
			fclose(fp);
		}
		
		if (httpd_live)
			break;
		
		/* check port changed */
		if (nvram_get_int("http_lanport") != http_port) 
		{
			if (pids("wget"))
				system("killall wget");
			return 1;
		}
		
		sleep(1);
	}
	
	if (!httpd_live)
	{
		if (pids("wget"))
			system("killall wget");
		
		dbg("httpd is so dead!!!\n");
		
		return 0;
	}
	
	return 1;
#else
	return 1;
#endif
}
Пример #11
0
int
start_vpnc(void)
{
	FILE *fp;
	char options[80];
	char *pppd_argv[] = { "/usr/sbin/pppd", "file", options, NULL};
	char tmp[100], prefix[] = "vpnc_", wan_prefix[] = "wanXXXXXXXXXX_";
	char buf[256];	/* although maximum length of pppoe_username/pppoe_passwd is 64. pppd accepts up to 256 characters. */
	mode_t mask;
	int ret = 0;

	snprintf(wan_prefix, sizeof(wan_prefix), "wan%d_", wan_primary_ifunit());
#if 0
	if (nvram_match(strcat_r(wan_prefix, "proto", tmp), "pptp") || nvram_match(strcat_r(wan_prefix, "proto", tmp), "l2tp"))
		return 0;
#endif
	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp"))
		sprintf(options, "/tmp/ppp/vpnc_options.pptp");
	else if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp"))
		sprintf(options, "/tmp/ppp/vpnc_options.l2tp");
	else
		return 0;

	/* shut down previous instance if any */
	stop_vpnc();

	/* unset vpnc_dut_disc */
	nvram_unset(strcat_r(prefix, "dut_disc", tmp));

	update_vpnc_state(prefix, WAN_STATE_INITIALIZING, 0);

	mask = umask(0000);

	/* Generate options file */
	if (!(fp = fopen(options, "w"))) {
		perror(options);
		umask(mask);
		return -1;
	}

	umask(mask);

	/* route for pptp/l2tp's server */
	if (nvram_match(strcat_r(wan_prefix, "proto", tmp), "pptp") || nvram_match(strcat_r(wan_prefix, "proto", tmp), "l2tp")) {
		char *wan_ifname = nvram_safe_get(strcat_r(wan_prefix, "pppoe_ifname", tmp));
		route_add(wan_ifname, 0, nvram_safe_get(strcat_r(wan_prefix, "gateway", tmp)), "0.0.0.0", "255.255.255.255");
	}

	/* do not authenticate peer and do not use eap */
	fprintf(fp, "noauth\n");
	fprintf(fp, "refuse-eap\n");
	fprintf(fp, "user '%s'\n",
		ppp_safe_escape(nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp)), buf, sizeof(buf)));
	fprintf(fp, "password '%s'\n",
		ppp_safe_escape(nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp)), buf, sizeof(buf)));

	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp")) {
		fprintf(fp, "plugin pptp.so\n");
		fprintf(fp, "pptp_server '%s'\n",
			nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ?
			nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) :
			nvram_safe_get(strcat_r(prefix, "gateway_x", tmp)));
		fprintf(fp, "vpnc 1\n");
		/* see KB Q189595 -- historyless & mtu */
		if (nvram_match(strcat_r(wan_prefix, "proto", tmp), "pptp") || nvram_match(strcat_r(wan_prefix, "proto", tmp), "l2tp"))
			fprintf(fp, "nomppe-stateful mtu 1300\n");
		else
			fprintf(fp, "nomppe-stateful mtu 1400\n");

		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "-mppc")) {
			fprintf(fp, "nomppe nomppc\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-40")) {
			fprintf(fp, "require-mppe\n"
				    "require-mppe-40\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-56")) {
			fprintf(fp, "nomppe-40\n"
				    "nomppe-128\n"
				    "require-mppe\n"
				    "require-mppe-56\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-128")) {
			fprintf(fp, "nomppe-40\n"
				    "nomppe-56\n"
				    "require-mppe\n"
				    "require-mppe-128\n");
		}
	} else {
		fprintf(fp, "nomppe nomppc\n");

		if (nvram_match(strcat_r(wan_prefix, "proto", tmp), "pptp") || nvram_match(strcat_r(wan_prefix, "proto", tmp), "l2tp"))
			fprintf(fp, "mtu 1300\n");
		else
			fprintf(fp, "mtu 1400\n");			
	}

	if (nvram_invmatch(strcat_r(prefix, "proto", tmp), "l2tp")) {
		ret = nvram_get_int(strcat_r(prefix, "pppoe_idletime", tmp));
		if (ret && nvram_get_int(strcat_r(prefix, "pppoe_demand", tmp))) {
			fprintf(fp, "idle %d ", ret);
			if (nvram_invmatch(strcat_r(prefix, "pppoe_txonly_x", tmp), "0"))
				fprintf(fp, "tx_only ");
			fprintf(fp, "demand\n");
		}
		fprintf(fp, "persist\n");
	}

	fprintf(fp, "holdoff %d\n", nvram_get_int(strcat_r(prefix, "pppoe_holdoff", tmp)) ? : 10);
	fprintf(fp, "maxfail %d\n", nvram_get_int(strcat_r(prefix, "pppoe_maxfail", tmp)));

	if (nvram_invmatch(strcat_r(prefix, "dnsenable_x", tmp), "0"))
		fprintf(fp, "usepeerdns\n");

	fprintf(fp, "ipcp-accept-remote ipcp-accept-local noipdefault\n");
	fprintf(fp, "ktune\n");

	/* pppoe set these options automatically */
	/* looks like pptp also likes them */
	fprintf(fp, "default-asyncmap nopcomp noaccomp\n");

	/* pppoe disables "vj bsdcomp deflate" automagically */
	/* ccp should still be enabled - mppe/mppc requires this */
	fprintf(fp, "novj nobsdcomp nodeflate\n");

	/* echo failures */
	fprintf(fp, "lcp-echo-interval 6\n");
	fprintf(fp, "lcp-echo-failure 10\n");

	/* pptp has Echo Request/Reply, l2tp has Hello packets */
	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp") ||
	    nvram_match(strcat_r(prefix, "proto", tmp), "l2tp"))
		fprintf(fp, "lcp-echo-adaptive\n");

	fprintf(fp, "unit %d\n", vpnc_unit);
	fprintf(fp, "linkname vpn%d\n", vpnc_unit);
	fprintf(fp, "ip-up-script %s\n", "/tmp/ppp/vpnc-ip-up");
	fprintf(fp, "ip-down-script %s\n", "/tmp/ppp/vpnc-ip-down");
	fprintf(fp, "ip-pre-up-script %s\n", "/tmp/ppp/vpnc-ip-pre-up");
	fprintf(fp, "auth-fail-script %s\n", "/tmp/ppp/vpnc-auth-fail");

#if 0 /* unsupported */
#ifdef RTCONFIG_IPV6
	switch (get_ipv6_service()) {
		case IPV6_NATIVE_DHCP:
		case IPV6_MANUAL:
			fprintf(fp, "+ipv6\n");
			break;
        }
#endif
#endif

	/* user specific options */
	fprintf(fp, "%s\n",
		nvram_safe_get(strcat_r(prefix, "pppoe_options_x", tmp)));

	fclose(fp);

#if 0
	/* shut down previous instance if any */
	stop_vpnc();

	nvram_unset(strcat_r(prefix, "dut_disc", tmp));
#endif

	if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp"))
	{
		if (!(fp = fopen(L2TP_VPNC_CONF, "w"))) {
			perror(options);
			return -1;
		}

		fprintf(fp, "# automagically generated\n"
			"global\n\n"
			"load-handler \"sync-pppd.so\"\n"
			"load-handler \"cmd.so\"\n\n"
			"section sync-pppd\n\n"
			"lac-pppd-opts \"file %s\"\n\n"
			"section peer\n"
			"port 1701\n"
			"peername %s\n"
			"vpnc 1\n"
			"hostname %s\n"
			"lac-handler sync-pppd\n"
			"persist yes\n"
			"maxfail %d\n"
			"holdoff %d\n"
			"hide-avps no\n"
			"section cmd\n"
			"socket-path " L2TP_VPNC_CTRL "\n\n",
			options,
                        nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ?
                                nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) :
                                nvram_safe_get(strcat_r(prefix, "gateway_x", tmp)),
			nvram_invmatch(strcat_r(prefix, "hostname", tmp), "") ?
				nvram_safe_get(strcat_r(prefix, "hostname", tmp)) : "localhost",
			nvram_get_int(strcat_r(prefix, "pppoe_maxfail", tmp))  ? : 32767,
			nvram_get_int(strcat_r(prefix, "pppoe_holdoff", tmp)) ? : 10);

		fclose(fp);

		/* launch l2tp */
		eval("/usr/sbin/l2tpd", "-c", L2TP_VPNC_CONF, "-p", L2TP_VPNC_PID);

		ret = 3;
		do {
			_dprintf("%s: wait l2tpd up at %d seconds...\n", __FUNCTION__, ret);
			usleep(1000*1000);
		} while (!pids("l2tpd") && ret--);

		/* start-session */
		ret = eval("/usr/sbin/l2tp-control", "-s", L2TP_VPNC_CTRL, "start-session 0.0.0.0");

		/* pppd sync nodetach noaccomp nobsdcomp nodeflate */
		/* nopcomp novj novjccomp file /tmp/ppp/options.l2tp */

	} else
Пример #12
0
int
getStorageStatus(STORAGE_INFO_T *st)
{
//	char tmparray[MAXARRAY][MAXARRSIZE];
	char *username, *password;
	char *apps_ver, *apps_model;
	int port;

	st->Capability=0;

	unsigned long apps_status=0;

	// count apps_status
	if (swap_check())
	{
		apps_status|=APPS_STATUS_SWAP;
	}
	if (nvram_match("apps_comp", "1"))
	{
		apps_status|=APPS_STATUS_FILECOMPLETENESS;
	}
	if (nvram_match("apps_disk_free", "1"))
	{
		apps_status|=APPS_STATUS_FREESPACE;
	}
	if (pids("smbd"))
	{
		apps_status|=APPS_STATUS_SAMBAMODE;
	}
	if (is_apps_running() /*&& nvram_match("apps_dl_ex", "1")*/)
	{
		apps_status|=APPS_STATUS_RUNNING;
	}
	if (nvram_match("usb_path1", "storage") || nvram_match("usb_path2", "storage"))
	{
		apps_status|=APPS_STATUS_DISCONPORT;
	}
	if (nvram_match("apps_dl", "1"))
	{
		apps_status|=APPS_STATUS_DMMODE;
	}
	if (nvram_match("apps_status_checked", "1"))
	{
		apps_status|=APPS_STATUS_CHECKED;
	}
	if (nvram_match("usb_mnt_first_path_port", "1"))
	{
		apps_status|=APPS_STATUS_USBPORT1;
	}
	if (strcmp(nvram_safe_get("mnt_type"), "ntfs") == 0)
	{
		apps_status|=APPS_STATUS_FS_NTFS;
	}
	if (strcmp(nvram_safe_get("mnt_type"), "fat32") == 0)
	{
		apps_status|=APPS_STATUS_FS_FAT;
	}
	if (strcmp(nvram_safe_get("dmrd"), "1") == 0)     
	{
		apps_status|=APPS_STATUS_READONLY;
	}
	if (strcmp(nvram_safe_get("dm_block"), "1") == 0)
	{
		apps_status|=APPS_STATUS_BLOCKED;
	}
	if (!strcmp(nvram_safe_get("st_samba_mode"), "2") || !strcmp(nvram_safe_get("st_samba_mode"), "4"))
	{
		apps_status|=APPS_STATUS_SMBUSER;
	}
//	if (strcmp(nvram_safe_get("slow_disk"), "1") == 0)
	//if (swap_check() < 1024*1024*128)
	if (	(apps_status & APPS_STATUS_SWAP == APPS_STATUS_SWAP) &&
		(swap_check() < MINSWAPSIZE))
	{
		apps_status|=APPS_SLOW_DISK;
	}
	if (atoi(nvram_safe_get("usb_mnt_first_path_port")) > 0)
	{
		apps_status|=APPS_STATUS_DMPORT;
	}
	st->AppsStatus=apps_status;	// disable for tmp

	printf("\n[infosvr] apps status : %x\n", apps_status);	// tmp test
	if(nvram_invmatch("apps_pool", "") && nvram_invmatch("apps_share", ""))
	{
		st->Capability = (unsigned long) atoi(nvram_safe_get("apps_caps"));
		strcpy(st->AppsPool, nvram_safe_get("usb_mnt_first_path"));
		strcpy(st->AppsShare, nvram_safe_get("apps_share"));
	}
	else
	{
		st->Capability=0;
		st->AppsPool[0]=0;
		st->AppsShare[0]=0;
	}

	st->DiskSize=0;
	st->DiskStatus=DISK_STATUS_NONE;

	st->DiskModel[0]=0;	// disable for tmp

	memset(st->PrinterModel1, 0, 32);
	memset(st->PrinterModel2, 0, 32);
#if 0
	if (strncmp(nvram_safe_get("st_ftp_mode"), "2", 1) == 0)
	{
		username = nvram_safe_get("acc_username0");
		password = nvram_safe_get("acc_password0");
		strncpy(st->PrinterModel1, username, strlen(username));
		strncpy(st->PrinterModel2, password, strlen(password));
	}
	else
	{
		strncpy(st->PrinterModel1, "anonymous", 9);
	}
#endif
	// copy apps_version to DiskModel
	apps_ver = nvram_get("apps_version");
	if(!apps_ver)
	{
		printf("get empty apps ver!");	// tmp test
		apps_ver = "0.0.0.0";
	}
	strncpy(st->DiskModel, apps_ver, strlen(apps_ver));	// tmp test
	printf("set apps version as %s\n", apps_ver);	// tmp test
	// copy apps_model_name to Apps_Model_Name
	apps_model = nvram_safe_get("apps_model_name");
	if(!apps_model)
	{
		printf("get empty apps model\n");	// tmp test
		apps_model = "no_model";
	}
	strncpy(st->Apps_Model_Name, apps_model, strlen(apps_model));	// tmp test
	printf("set apps model as %s\n", apps_model);	// tmp test
#ifdef NOPARSER
	// get from page
	system("wget http://127.0.0.1/diskstatus.asp -O /tmp/diskstatus");
	// find claimed ide
	if ((st->DiskSize=isClaimedIDE(st->DiskModel)))
	{
		st->DiskStatus=DISK_STATUS_CLAIMED;
		printf("chk 14: is claimed IDE\n");	// tmp test
	}
	else if ((st->DiskSize=isBlankIDE(st->DiskModel)))
	{	
		st->DiskStatus=DISK_STATUS_BLANK;
	}
	isPrinter(st->PrinterModel1, st->PrinterModel2);
#endif

	printf("Storage: %d %s\n", st->DiskStatus, st->AppsShare);	// tmp test
	return 0;
}
Пример #13
0
//////////vvvvvvvvvvvvvvvvvvvvvjerry5 2009.07
void
stop_rstats(void)
{
	if (pids("rstats"))
		system("killall rstats");
}
Пример #14
0
void
stop_httpd(void)
{
	if (pids("httpd"))
		system("killall httpd");
}
Пример #15
0
int
start_pppd(int unit)
{
	FILE *fp;
	char options[80];
	char *pppd_argv[] = { "/usr/sbin/pppd", "file", options, NULL};
	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
	char buf[256];	/* although maximum length of pppoe_username/pppoe_passwd is 64. pppd accepts up to 256 characters. */
	mode_t mask;
	int ret = 0;

_dprintf("%s: unit=%d.\n", __FUNCTION__, unit);

	snprintf(prefix, sizeof(prefix), "wan%d_", unit);
	sprintf(options, "/tmp/ppp/options.wan%d", unit);

	mask = umask(0000);

	/* Generate options file */
	if (!(fp = fopen(options, "w"))) {
		perror(options);
		umask(mask);
		return -1;
	}

	umask(mask);

	/* do not authenticate peer and do not use eap */
	fprintf(fp, "noauth\n");
	fprintf(fp, "refuse-eap\n");
	handle_special_char_for_pppd(buf, sizeof(buf), nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp)));
	fprintf(fp, "user '%s'\n", buf);
	handle_special_char_for_pppd(buf, sizeof(buf), nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp)));
	fprintf(fp, "password '%s'\n", buf);

	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp")) {
		fprintf(fp, "plugin pptp.so\n");
		fprintf(fp, "pptp_server '%s'\n",
			nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ?
			nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) :
			nvram_safe_get(strcat_r(prefix, "gateway_x", tmp)));
		/* see KB Q189595 -- historyless & mtu */
		fprintf(fp, "nomppe-stateful mtu 1400\n");
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "-mppc")) {
			fprintf(fp, "nomppe nomppc\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-40")) {
			fprintf(fp, "require-mppe\n");
			fprintf(fp, "require-mppe-40\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-56")) {
			fprintf(fp, "nomppe-40\n"
				    "require-mppe\n"
				    "require-mppe-56\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-128")) {
			fprintf(fp, "nomppe-40\n"
				    "nomppe-56\n"
				    "require-mppe\n"
				    "require-mppe-128\n");
		}
	} else {
		fprintf(fp, "nomppe nomppc\n");
	}

	if (nvram_match(strcat_r(prefix, "proto", tmp), "pppoe")) {
		fprintf(fp, "plugin rp-pppoe.so nic-%s\n",
			nvram_safe_get(strcat_r(prefix, "ifname", tmp)));

		if (nvram_invmatch(strcat_r(prefix, "pppoe_service", tmp), "")) {
			fprintf(fp, "rp_pppoe_service '%s'\n",
				nvram_safe_get(strcat_r(prefix, "pppoe_service", tmp)));
		}

		if (nvram_invmatch(strcat_r(prefix, "pppoe_ac", tmp), "")) {
			fprintf(fp, "rp_pppoe_ac '%s'\n",
				nvram_safe_get(strcat_r(prefix, "pppoe_ac", tmp)));
		}

#ifdef RTCONFIG_DSL
		if (nvram_match("dsl0_proto", "pppoa")) {
			FILE *fp_dsl_mac;
			char *dsl_mac = NULL;
			int timeout = 10; /* wait up to 10 seconds */

			while (timeout--) {
				fp_dsl_mac = fopen("/tmp/adsl/tc_mac.txt","r");
				if (fp_dsl_mac != NULL) {
					dsl_mac = fgets(tmp, sizeof(tmp), fp_dsl_mac);
					dsl_mac = strsep(&dsl_mac, "\r\n");
					fclose(fp_dsl_mac);
					break;
				}
				usleep(1000*1000);
			}

			fprintf(fp, "rp_pppoe_sess %d:%s\n", 154,
				(dsl_mac && *dsl_mac) ? dsl_mac : "00:11:22:33:44:55");
		}
#endif

		fprintf(fp, "mru %s mtu %s\n",
			nvram_safe_get(strcat_r(prefix, "pppoe_mru", tmp)),
			nvram_safe_get(strcat_r(prefix, "pppoe_mtu", tmp)));
	}

	if (nvram_invmatch(strcat_r(prefix, "proto", tmp), "l2tp")) {
		ret = nvram_get_int(strcat_r(prefix, "pppoe_idletime", tmp));
		if (ret && nvram_get_int(strcat_r(prefix, "pppoe_demand", tmp))) {
			fprintf(fp, "idle %d ", ret);
			if (nvram_invmatch(strcat_r(prefix, "pppoe_txonly_x", tmp), "0"))
				fprintf(fp, "tx_only ");
			fprintf(fp, "demand\n");
		}
		fprintf(fp, "persist\n");
	}

	fprintf(fp, "holdoff %d\n", nvram_get_int(strcat_r(prefix, "pppoe_holdoff", tmp)) ? : 10);
	fprintf(fp, "maxfail %d\n", nvram_get_int(strcat_r(prefix, "pppoe_maxfail", tmp)));

	if (nvram_invmatch(strcat_r(prefix, "dnsenable_x", tmp), "0"))
		fprintf(fp, "usepeerdns\n");

	fprintf(fp, "ipcp-accept-remote ipcp-accept-local noipdefault\n");
	fprintf(fp, "ktune\n");

	/* pppoe set these options automatically */
	/* looks like pptp also likes them */
	fprintf(fp, "default-asyncmap nopcomp noaccomp\n");

	/* pppoe disables "vj bsdcomp deflate" automagically */
	/* ccp should still be enabled - mppe/mppc requires this */
	fprintf(fp, "novj nobsdcomp nodeflate\n");

	/* echo failures */
	fprintf(fp, "lcp-echo-interval 6\n");
	fprintf(fp, "lcp-echo-failure 10\n");

	/* pptp has Echo Request/Reply, l2tp has Hello packets */
	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp") ||
	    nvram_match(strcat_r(prefix, "proto", tmp), "l2tp"))
		fprintf(fp, "lcp-echo-adaptive\n");

	fprintf(fp, "unit %d\n", unit);
	fprintf(fp, "linkname wan%d\n", unit);

#ifdef RTCONFIG_IPV6
	switch (get_ipv6_service()) {
		case IPV6_NATIVE:
		case IPV6_NATIVE_DHCP:
		case IPV6_MANUAL:
			fprintf(fp, "+ipv6\n");
			break;
        }
#endif

	/* user specific options */
	fprintf(fp, "%s\n",
		nvram_safe_get(strcat_r(prefix, "pppoe_options_x", tmp)));

	fclose(fp);

	/* shut down previous instance if any */
	stop_pppd(unit);
	nvram_set(strcat_r(prefix, "pppoe_ifname", tmp), "");

	if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp"))
	{
		if (!(fp = fopen("/tmp/l2tp.conf", "w"))) {
			perror(options);
			return -1;
		}

		fprintf(fp, "# automagically generated\n"
			"global\n\n"
			"load-handler \"sync-pppd.so\"\n"
			"load-handler \"cmd.so\"\n\n"
			"section sync-pppd\n\n"
			"lac-pppd-opts \"file %s\"\n\n"
			"section peer\n"
			"port 1701\n"
			"peername %s\n"
			"hostname %s\n"
			"lac-handler sync-pppd\n"
			"persist yes\n"
			"maxfail %d\n"
			"holdoff %d\n"
			"hide-avps no\n"
			"section cmd\n\n",
			options,
                        nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ?
                                nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) :
                                nvram_safe_get(strcat_r(prefix, "gateway_x", tmp)),
			nvram_invmatch(strcat_r(prefix, "hostname", tmp), "") ?
				nvram_safe_get(strcat_r(prefix, "hostname", tmp)) : "localhost",
			nvram_get_int(strcat_r(prefix, "pppoe_maxfail", tmp))  ? : 32767,
			nvram_get_int(strcat_r(prefix, "pppoe_holdoff", tmp)) ? : 10);

		fclose(fp);

		/* launch l2tp */
		eval("/usr/sbin/l2tpd");

		ret = 3;
		do {
			_dprintf("%s: wait l2tpd up at %d seconds...\n", __FUNCTION__, ret);
			usleep(1000*1000);
		} while (!pids("l2tpd") && ret--);

		/* start-session */
		ret = eval("/usr/sbin/l2tp-control", "start-session 0.0.0.0");

		/* pppd sync nodetach noaccomp nobsdcomp nodeflate */
		/* nopcomp novj novjccomp file /tmp/ppp/options.l2tp */

	} else
Пример #16
0
/* wathchdog is runned in NORMAL_PERIOD, 1 seconds
 * check in each NORMAL_PERIOD
 *	1. button
 *
 * check in each NORAML_PERIOD*10
 *
 *      1. ntptime 
 *      2. time-dependent service
 *      3. http-process
 *      4. usb hotplug status
 */
void watchdog(void)
{
	/* handle button */
	btn_check();

	/* if timer is set to less than 1 sec, then bypass the following */
	if (itv.it_value.tv_sec == 0) return;

	if (nvram_match("asus_mfg", "1"))
	{
		system("rmmod hw_nat");
		if (pids("ntp"))
			system("killall -SIGTERM ntp");
		if (pids("ntpclient"))
			system("killall ntpclient");
		if (pids("udhcpc"))
			system("killall -SIGTERM udhcpc");
#if (!defined(W7_LOGO) && !defined(WIFI_LOGO))
		if (pids("ots"))
			system("killall ots");
#endif
		stop_wanduck();
		stop_logger();
		stop_upnp();	// it may cause upnp cannot run
		stop_dhcpd();
		stop_dns();
#if (!defined(W7_LOGO) && !defined(WIFI_LOGO))
		stop_pspfix();
#endif
		stop_wsc();
		stop_wsc_2g();
		stop_lltd();
		stop_networkmap();
		stop_httpd();
		stop_lpd();
		stop_u2ec();
		kill_pidfile_s("/var/run/linkstatus_monitor.pid", SIGTERM);
		if (pids("detectWan"))
			system("killall detectWan");
#if (!defined(W7_LOGO) && !defined(WIFI_LOGO))
		stop_rstats();
		kill_pidfile_s("/var/run/detect_internet.pid", SIGTERM);
#endif
		if (pids("tcpcheck"))
			system("killall -SIGTERM tcpcheck");
#ifdef HTTPD_CHECK
		if (pids("httpdcheck"))
			system("killall -SIGTERM httpdcheck");
#endif
		if (pids("traceroute"))
			system("killall traceroute");
		if (pids("usbled"))
			system("killall -SIGTERM usbled");

		nvram_set("asus_mfg", "2");
	}

#if 0
	// reboot signal checking
	if (nvram_match("reboot", "1"))
	{
		printf("[watchdog] nvram match reboot\n");

		reboot_count++;
		if (reboot_count >= 2) 
		{
//			kill(1, SIGTERM);
			sys_exit();
		}

		return;
	}
#else
	if (nvram_match("reboot", "1")) return;
#endif

	if (stop_service_type_99) return;

	if (!nvram_match("asus_mfg", "0")) return;

	watchdog_period = (watchdog_period + 1) % 10;

	if (watchdog_period) return;

#ifdef BTN_SETUP
	if (btn_pressed_setup >= BTNSETUP_START) return;
#endif

#if (!defined(W7_LOGO) && !defined(WIFI_LOGO))
	if (count_to_stop_wps > 0)
	{
		count_to_stop_wps--;

		if (!count_to_stop_wps)
		{
//			if (nvram_match("wl_radio_x", "1"))
			stop_wsc();			// psp fix
//			if (nvram_match("rt_radio_x", "1"))
			stop_wsc_2g();			// psp fix
			nvram_set("wps_enable", "0");	// psp fix
		}
	}
#endif

	ddns_timer = (ddns_timer + 1) % 4320;

	if (nvram_match("asus_debug", "1"))
		mem_timer = (mem_timer + 1) % 60;

	if (!watchdog_count)
		watchdog_count++;
	else if (watchdog_count++ == 1)
	{
#if 0
		if (!strcmp(nvram_safe_get("rc_service"), "restart_upnp"))
			service_handle();
		else
#endif
		if (	nvram_match("router_disable", "0") &&
			nvram_match("upnp_enable", "1") &&
			nvram_match("upnp_started", "0")	)
		{
//			if (has_wan_ip())
			{
				dbg("[watchdog] starting upnp...\n");
				stop_upnp();
				start_upnp();
			}
		}
	}

	/* check for time-dependent services */
	svc_timecheck();

	/* http server check */
	httpd_processcheck();

	u2ec_processcheck();

	media_processcheck();
#if 0
	samba_processcheck();
#endif
	pppd_processcheck();

	if (nvram_match("wan_route_x", "IP_Routed"))
		nm_processcheck();

	cpu_usage_minotor();

	dm_block_chk();

	if (nvram_match("asus_debug", "1") && !mem_timer)
	{
		print_num_of_connections();
		dbg("Hardware NAT: %s\n", is_hwnat_loaded() ? "Enabled": "Disabled");
		dbg("Software QoS: %s\n", nvram_match("qos_enable", "1") ? "Enabled": "Disabled");
		dbg("pppd running: %s\n", pids("pppd") ? "Yes": "No");
#if 0
		dbg("CPU usage: %d%%\n", get_cpu_usage());
#else
		dbg("CPU usage: %d%%\n", cpu_main(0, NULL, 0));
#endif
		system("free");
		system("date");
		print_uptime();
	}

#ifdef CDMA
	/* CDMA_DOWN = 99, none
	 * CDMA_DOWN = 1, currently down
	 * CDMA_DOWN = 2, currently up
	 * CDMA_DOWN = 0, currently trying to connect
	 */
	if (nvram_match("cdma_down", "1"))
	{
		logmessage("CDMA client", "cdma is down(%d)!", cdma_down);

		cdma_down++;
		cdma_connec t = 0;

		if (cdma_down == 2)
		{
			printf("[watchdog] stop wan\n");
			stop_wan();
			start_wan();
		}
		else if (cdma_down >= 12) /* 2 minutes timeout for retry */
		{
			cdma_down = 0;
		}
	}
	else if (nvram_match("cdma_down", "0"))
	{
		logmessage("CDMA client", "cdma try connect(%d)!", cdma_connect);
		cdma_down = 0;
		cdma_connect++;

		if (cdma_connect > 12) /* 2 minitues timeout for connecting */
		{
			nvram_set("cdma_down", "1");
		}
	}
	else
	{
		cdma_down = 0;
		cdma_connect = 0;
	}
#endif

	if (nvram_match("wan_route_x", "IP_Routed"))
	{
		if (!is_phyconnected() || !has_wan_ip())
			return;

		/* sync time to ntp server if necessary */
		if (!nvram_match("wan_dns_t", "") && !nvram_match("wan_gateway_t", ""))
		{
			ntp_timesync();
		}

		if (	nvram_match("ddns_enable_x", "1") && 
			(!nvram_match("ddns_updated", "1") || !ddns_timer)
		)
		{
			logmessage("RT-N56U", "[start ddns] watchdog");
			start_ddns();
		}

		if (!ddns_timer)
		{
			stop_networkmap();
			start_networkmap();
		}
	}
	else
	{
		if (/*!nvram_match("lan_dns_t", "") && */!nvram_match("lan_gateway_t", ""))
			ntp_timesync();
	}
}
Пример #17
0
int start_pppd(int unit)
{
	int ret;
	
	FILE *fp;
	char options[80];
	char *pppd_argv[] = { "/usr/sbin/pppd", "file", options, NULL};
	char *l2tpd_argv[] = { "/usr/sbin/l2tpd", "-f", NULL};
	char tmp[100], tmp1[32], prefix[] = "wanXXXXXXXXXX_";
	mode_t mask;
	int pid;

_dprintf("%s: unit=%d.\n", __FUNCTION__, unit);

	snprintf(prefix, sizeof(prefix), "wan%d_", unit);
	sprintf(options, "/tmp/ppp/options.wan%d", unit);

	mask = umask(0000);

	/* Generate options file */
	if (!(fp = fopen(options, "w"))) {
		perror(options);
		umask(mask);
		return -1;
	}

	umask(mask);

	/* do not authenticate peer and do not use eap */
	fprintf(fp, "noauth\n");
	fprintf(fp, "refuse-eap\n");
	fprintf(fp, "user '%s'\n",
		nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp)));
	fprintf(fp, "password '%s'\n",
		nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp)));

	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp"))
	{
		fprintf(fp, "plugin pptp.so\n");
		fprintf(fp, "pptp_server '%s'\n",
			nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ?
			nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) :
			nvram_safe_get(strcat_r(prefix, "gateway_x", tmp)));
		/* see KB Q189595 -- historyless & mtu */
		fprintf(fp, "nomppe-stateful mtu 1400\n");
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "-mppc")) {
			fprintf(fp, "nomppe nomppc\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-40")) {
			fprintf(fp, "require-mppe-40\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-56")) {
			fprintf(fp, "nomppe-40\n"
				    "require-mppe-56\n");
		} else
		if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-128")) {
			fprintf(fp, "nomppe-40\n"
				    "nomppe-56\n"
				    "require-mppe-128\n");
		}
	} else {
		fprintf(fp, "nomppe nomppc\n");
	}

	if (nvram_match(strcat_r(prefix, "proto", tmp), "pppoe"))
	{
#ifdef RTCONFIG_DSL	
		FILE* fp_dsl_mac;
		char buf_mac[32];
		int trp_cnt;
		int rm_cnt;
#endif
		
		fprintf(fp, "plugin rp-pppoe.so");

		if (nvram_invmatch(strcat_r(prefix, "pppoe_service", tmp), "")) {
			fprintf(fp, " rp_pppoe_service '%s'",
				nvram_safe_get(strcat_r(prefix, "pppoe_service", tmp)));
		}

		if (nvram_invmatch(strcat_r(prefix, "pppoe_ac", tmp), "")) {
			fprintf(fp, " rp_pppoe_ac '%s'",
				nvram_safe_get(strcat_r(prefix, "pppoe_ac", tmp)));
		}

		fprintf(fp, " nic-%s\n", nvram_safe_get(strcat_r(prefix, "ifname", tmp)));

		fprintf(fp, "mru %s mtu %s\n",
			nvram_safe_get(strcat_r(prefix, "pppoe_mru", tmp)),
			nvram_safe_get(strcat_r(prefix, "pppoe_mtu", tmp)));


	// wait 10 seconds for DSL MAC address file ready
#ifdef RTCONFIG_DSL
		if (nvram_match("dsl0_proto", "pppoa"))
		{
			strcpy(buf_mac, "00:11:22:33:44:55");
			for (trp_cnt = 0; trp_cnt < 10; trp_cnt++)
			{
				fp_dsl_mac = fopen("/tmp/adsl/tc_mac.txt","r");
				if (fp_dsl_mac != NULL)
				{
					fgets(buf_mac,sizeof(buf_mac),fp_dsl_mac);
					fclose(fp_dsl_mac);					 
					break;
				}
				usleep(1000*1000);				
			}
			// remove cr lf in buf_mac
            for (rm_cnt = 0; rm_cnt < sizeof(buf_mac); rm_cnt++)
            {
            	if (buf_mac[rm_cnt] == 0) break;
            	if (buf_mac[rm_cnt] == 0x0a || buf_mac[rm_cnt] == 0x0d)
            	{
            		buf_mac[rm_cnt]=0;
            		break;
        		}
            }			
			fprintf(fp, "rp_pppoe_sess %d:%s\n", 154, buf_mac);									
		}
#endif		
			
	}

	if (nvram_invmatch(strcat_r(prefix, "proto", tmp), "l2tp")){
		ret = nvram_get_int(strcat_r(prefix, "pppoe_idletime", tmp));
		if (ret && nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1"))
		{
			fprintf(fp, "idle %d ", ret);
			if (nvram_invmatch(strcat_r(prefix, "pppoe_txonly_x", tmp), "0"))
				fprintf(fp, "tx_only ");
			fprintf(fp, "demand\n");
		}

		fprintf(fp, "persist\n");
	}

	fprintf(fp, "holdoff %s\n", nvram_invmatch(strcat_r(prefix, "pppoe_holdoff", tmp), "")?nvram_safe_get(tmp):"10");	// pppd re-call-time(s)
	fprintf(fp, "maxfail 0\n");

	if (nvram_invmatch(strcat_r(prefix, "dnsenable_x", tmp), "0"))
		fprintf(fp, "usepeerdns\n");

	fprintf(fp, "ipcp-accept-remote ipcp-accept-local noipdefault\n");
	fprintf(fp, "ktune\n");

	/* pppoe set these options automatically */
	/* looks like pptp also likes them */
	fprintf(fp, "default-asyncmap nopcomp noaccomp\n");

	/* pppoe disables "vj bsdcomp deflate" automagically */
	/* ccp should still be enabled - mppe/mppc requires this */
	fprintf(fp, "novj nobsdcomp nodeflate\n");

	/* echo failures */
	fprintf(fp, "lcp-echo-interval 6\n");
	fprintf(fp, "lcp-echo-failure 10\n");

	fprintf(fp, "unit %d\n", unit);
	fprintf(fp, "linkname wan%d\n", unit);

#ifdef RTCONFIG_IPV6
	switch (get_ipv6_service()) {
		case IPV6_NATIVE:
		case IPV6_NATIVE_DHCP:
		case IPV6_MANUAL:
			fprintf(fp, "+ipv6\n");
			break;
        }
#endif

	/* user specific options */
	fprintf(fp, "%s\n",
		nvram_safe_get(strcat_r(prefix, "pppoe_options_x", tmp)));

	fclose(fp);

	if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp"))
	{
		if (!(fp = fopen("/tmp/l2tp.conf", "w"))) {
			perror(options);
			return -1;
		}

		fprintf(fp, "# automagically generated\n"
			"global\n\n"
			"load-handler \"sync-pppd.so\"\n"
			"load-handler \"cmd.so\"\n\n"
			"section sync-pppd\n\n"
			"lac-pppd-opts \"file %s\"\n\n"
			"section peer\n"
			"port 1701\n"
			"peername %s\n"
			"hostname %s\n"
			"lac-handler sync-pppd\n"
			"persist yes\n"
			"maxfail %s\n"
			"holdoff %s\n"
			"hide-avps no\n"
			"section cmd\n\n",
			options,
                        nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ?
                                nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) :
                                nvram_safe_get(strcat_r(prefix, "gateway_x", tmp)),
			nvram_invmatch(strcat_r(prefix, "hostname", tmp), "") ?	// ham 0509
				nvram_safe_get(strcat_r(prefix, "hostname", tmp)) : "localhost",
			nvram_invmatch(strcat_r(prefix, "pppoe_maxfail", tmp), "") ?
				nvram_safe_get(strcat_r(prefix, "pppoe_maxfail", tmp)) : "32767",
			nvram_invmatch(strcat_r(prefix, "pppoe_holdoff", tmp), "") ?
				nvram_safe_get(strcat_r(prefix, "pppoe_holdoff", tmp)) : "10");

		fclose(fp);

		/* launch l2tp */
		ret = _eval(l2tpd_argv, NULL, 0, &pid);

		int retry = 3;
		while(!pids("l2tpd") && retry--){
			_dprintf("%s: wait l2tpd up at %d seconds...\n", __FUNCTION__, retry);
			sleep(1);
		}
		sleep(1); // when the pid of l2tpd is existed, also need to wait a more second.

		/* start-session */
		ret = eval("/usr/sbin/l2tp-control", "start-session 0.0.0.0");

		/* pppd sync nodetach noaccomp nobsdcomp nodeflate */
		/* nopcomp novj novjccomp file /tmp/ppp/options.l2tp */

	} else{
		char pid_file[256], *value;
		int orig_pid;
		int wait_time = 0;

		memset(pid_file, 0, 256);
		snprintf(pid_file, 256, "/var/run/ppp-wan%d.pid", unit);

		if((value = file2str(pid_file)) != NULL && (orig_pid = atoi(value)) > 1){
_dprintf("%s: kill pppd(%d).\n", __FUNCTION__, orig_pid);
			kill(orig_pid, SIGHUP);
			sleep(1);
			while(check_process_exist(orig_pid) && wait_time < MAX_WAIT_FILE){
_dprintf("%s: kill pppd(%d).\n", __FUNCTION__, orig_pid);
				++wait_time;
				kill(orig_pid, SIGTERM);
				sleep(1);
			}

			if(check_process_exist(orig_pid)){
				kill(orig_pid, SIGKILL);
				sleep(1);
			}
		}
		if(value != NULL)
			free(value);

		ret = _eval(pppd_argv, NULL, 0, NULL);
	}
	
	return 0;
}
Пример #18
0
int main(int argc, char **argv) {

    UNUSED(argc);

    //- Check if same process is running.
    FILE *fp = fopen(LIGHTTPD_MONITOR_PID_FILE_PATH, "r");
    if (fp) {
        fclose(fp);
        return 0;
    }

    //- Write PID file
    pid_t pid = getpid();
    fp = fopen(LIGHTTPD_MONITOR_PID_FILE_PATH, "w");
    if (!fp) {
        exit(EXIT_FAILURE);
    }
    fprintf(fp, "%d\n", pid);
    fclose(fp);

#if EMBEDDED_EANBLE
    sigset_t sigs_to_catch;

    /* set the signal handler */
    sigemptyset(&sigs_to_catch);
    sigaddset(&sigs_to_catch, SIGTERM);
    sigprocmask(SIG_UNBLOCK, &sigs_to_catch, NULL);

    signal(SIGTERM, sigaction_handler);
#else
    struct sigaction act;
    memset(&act, 0, sizeof(act));
    act.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &act, NULL);
    sigaction(SIGUSR1, &act, NULL);

    act.sa_sigaction = sigaction_handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;

    sigaction(SIGINT,  &act, NULL);
    sigaction(SIGTERM, &act, NULL);
    sigaction(SIGHUP,  &act, NULL);
    sigaction(SIGALRM, &act, NULL);
    sigaction(SIGCHLD, &act, NULL);
#endif

    time_t prv_ts = time(NULL);

    int stop_arp_count = 0;
    int commit_count = 0;

    while (!is_shutdown) {

        sleep(10);

        int start_lighttpd = 0;
        int start_lighttpd_arp = 0;

        time_t cur_ts = time(NULL);

#if EMBEDDED_EANBLE
        if (!pids("lighttpd")) {
            start_lighttpd = 1;
        }

        if (!pids("lighttpd-arpping")) {
            start_lighttpd_arp = 1;
        }
#else
        if (!system("pidof lighttpd")) {
            start_lighttpd = 1;
        }

        if (!system("pidof lighttpd-arpping")) {
            start_lighttpd_arp = 1;
        }
#endif

        //-every 30 sec
        if(cur_ts - prv_ts >= 30) {

            if(start_lighttpd) {
#if EMBEDDED_EANBLE
                system("/usr/sbin/lighttpd -f /tmp/lighttpd.conf -D &");
#else
                system("./_inst/sbin/lighttpd -f lighttpd.conf &");
#endif
            }

            if(start_lighttpd_arp) {
#if EMBEDDED_EANBLE
                system("/usr/sbin/lighttpd-arpping -f br0 &");
#else
                system("./_inst/sbin/lighttpd-arpping -f eth0 &");
#endif
            }

            //-every 2 hour
            if(stop_arp_count>=240) {
                stop_arpping_process();
                stop_arp_count=0;
            }

            //-every 12 hour
            if(commit_count>=1440) {

#if EMBEDDED_EANBLE
                int i, act;
                for (i = 30; i > 0; --i) {
                    if (((act = check_action()) == ACT_IDLE) || (act == ACT_REBOOT)) break;
                    fprintf(stderr, "Busy with %d. Waiting before shutdown... %d", act, i);
                    sleep(1);
                }

                nvram_do_commit();
#endif

                commit_count=0;
            }

            prv_ts = cur_ts;
            stop_arp_count++;
            commit_count++;
        }
    }

    Cdbg(DBE, "Success to terminate lighttpd-monitor.....");

    exit(EXIT_SUCCESS);

    return 0;
}