コード例 #1
0
void stop_jffs2(int stop)
{
	struct statfs sf;
#if defined(RTCONFIG_PSISTLOG) || defined(RTCONFIG_JFFS2LOG)
	int restart_syslogd = 0;
#endif

	if (!wait_action_idle(10)) return;

	if ((statfs("/jffs", &sf) == 0) && (sf.f_type != 0x73717368) && (sf.f_type != 0x71736873)) {
		// is mounted
		run_userfile("/jffs", ".autostop", "/jffs", 5);
		run_nvscript("script_autostop", "/jffs", 5);
	}

#if defined(RTCONFIG_PSISTLOG) || defined(RTCONFIG_JFFS2LOG)
	if (!stop && !strncmp(get_syslog_fname(0), "/jffs/", 6)) {
		restart_syslogd = 1;
		stop_syslogd();
		eval("cp", "/jffs/syslog.log", "/jffs/syslog.log-1", "/tmp");
	}
#endif

	notice_set("jffs", "Stopped");
	if (umount("/jffs"))
		umount2("/jffs", MNT_DETACH);
	else
		modprobe_r(JFFS_NAME);

#if defined(RTCONFIG_PSISTLOG) || defined(RTCONFIG_JFFS2LOG)
	if (restart_syslogd)
		start_syslogd();
#endif
}
コード例 #2
0
void format_mount_2nd_jffs2(void)
{
        int format = 0;
        char s[256];
        int size;
        int part;
        const char *p;

        if (!wait_action_idle(10)) return;

        if (!mtd_getinfo(SECOND_JFFS2_PARTITION, &part, &size)) return;
	_dprintf("Format 2nd jffs2: %d, %d\n", part, size);

	if(!check_in_rootfs(SECOND_JFFS2_PATH, "2nd_jffs", format))
		return;

        if (!mtd_unlock(SECOND_JFFS2_PARTITION)) {
                error("unlocking");
                return;
        }

        modprobe(JFFS_NAME);
        sprintf(s, MTD_BLKDEV(%d), part);
        if (mount(s, SECOND_JFFS2_PATH, JFFS_NAME, MS_NOATIME, "") != 0) {
		if( (get_model()==MODEL_RTAC56U || get_model()==MODEL_RTAC56S || get_model()==MODEL_RTAC3200 || get_model()==MODEL_RTAC68U || get_model()==MODEL_RPAC68U || get_model()==MODEL_DSLAC68U || get_model()==MODEL_RTAC87U || get_model()==MODEL_RTAC88U || get_model()==MODEL_RTAC3100 || get_model()==MODEL_RTAC5300 || get_model==MODEL_RTAC5300R || get_model()==MODEL_RTN18U || get_model()==MODEL_RTAC1200G || get_model()==MODEL_RTAC1200GP) ^ (mtd_erase(SECOND_JFFS2_PARTITION)) ){
                        error("formatting");
                        return;
                }

                format = 1;
                if (mount(s, SECOND_JFFS2_PATH, JFFS_NAME, MS_NOATIME, "") != 0) {
                        _dprintf("*** jffs2 2-nd mount error\n");
                        //modprobe_r(JFFS_NAME);
                        error("mounting");
                        return;
                }
        }

	sprintf(s, "rm -rf %s/*", SECOND_JFFS2_PATH);
        system(s);

        notice_set("2nd_jffs", format ? "Formatted" : "Loaded");

        if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
                chdir(SECOND_JFFS2_PATH);
                system(p);
                chdir("/");
        }
        run_userfile(SECOND_JFFS2_PATH, ".asusrouter", SECOND_JFFS2_PATH, 3);

}
コード例 #3
0
ファイル: jffs2.c プロジェクト: StephenMacras/dsl-n55u-bender
void stop_jffs2(void)
{
	struct statfs sf;

	if (!wait_action_idle(10)) return;

	if ((statfs("/jffs", &sf) == 0) && (sf.f_type != 0x71736873)) {
		// is mounted
		run_userfile("/jffs", ".autostop", "/jffs", 5);
		run_nvscript("script_autostop", "/jffs", 5);
	}

	notice_set("jffs", "Stopped");
	umount2("/jffs", MNT_DETACH);
	modprobe_r(JFFS_NAME);
}
コード例 #4
0
ファイル: nvram_linux.c プロジェクト: gygy/asuswrt
int nvram_commit(void)
{
	int r = 0;
	FILE *fp;

	if (nvram_get(ASUS_STOP_COMMIT) != NULL)
	{
		cprintf("# skip nvram commit #\n");
		return r;
	}

	fp = fopen("/var/log/commit_ret", "w");

	if (wait_action_idle(10)) {
		if (nvram_fd < 0) {
			if ((r = nvram_init(NULL)) != 0) goto finish;
		}
		set_action(ACT_NVRAM_COMMIT);
//		nvram_unset("dirty");
		r = ioctl(nvram_fd, NVRAM_MAGIC, NULL);
		set_action(ACT_IDLE);

		if (r < 0) {
			perror(PATH_DEV_NVRAM);
			cprintf("commit: error\n");
			if(fp!=NULL)
				fprintf(fp,"commit: error\n");
		}
		else {
			if(fp!=NULL)
                                fprintf(fp,"commit: OK\n");
		}
	}
	else {
		cprintf("commit: system busy\n");
		if(fp!=NULL)
                        fprintf(fp,"commit: system busy\n");
	}

finish:
	if(fp!=NULL) fclose(fp);

	return r;
}
コード例 #5
0
void mount_2nd_jffs2(void)
{
        int format = 0;
        char s[256];
        int size;
        int part;
	int i = 0;

	_dprintf("Mount 2nd jffs2...\n");
	while(1) {
        	if (wait_action_idle(10)) break;
		else i++;

		if(i>=10) {
			_dprintf("Mount 2nd jffs2 failed!");
			return;
		}
	}

        if (!mtd_getinfo(SECOND_JFFS2_PARTITION, &part, &size)) {
		_dprintf("Can not get 2nd jffs2 information!");
		return;
	}
	_dprintf("2nd jffs2: %d, %d\n", part, size);

	if(!check_in_rootfs(SECOND_JFFS2_PATH, "2nd_jffs", format))
		return;

        modprobe(JFFS_NAME);
        sprintf(s, MTD_BLKDEV(%d), part);

	i = 0;
        while(mount(s, SECOND_JFFS2_PATH, JFFS_NAME, MS_RDONLY, "") != 0) {
		_dprintf("Mount 2nd jffs failed! Try again...\n");
		if(i >= 10) {
			_dprintf("Mount 2nd jffs 10 times failed, stop mount!");
			break;
		}
		i++;
        }

	return;
}
コード例 #6
0
ファイル: dhcp.c プロジェクト: twtomato/twtomato
int dhcpc_event_main(int argc, char **argv)
{
	char *ifname;

	if (!wait_action_idle(10)) return 1;

#if 0
	if (nvram_match("debug_dhcpcenv", "1")) {
		system("( date; env ) >> /tmp/dhcpc_event.env");
	}
#endif

	if ((argc == 2) && (ifname = getenv("interface")) != NULL) {
		if (strcmp(argv[1], "deconfig") == 0) return deconfig(ifname);
		if (strcmp(argv[1], "bound") == 0) return bound(ifname);
		if ((strcmp(argv[1], "renew") == 0) || (strcmp(argv[1], "update") == 0)) return renew(ifname);
	}
	
	_dprintf("%s: unknown event %s\n", __FUNCTION__, argv[1]);
	return 1;
}
コード例 #7
0
int ipdown_main(int argc, char **argv)
{
	int proto;
	
	TRACE_PT("begin\n");

	if (!wait_action_idle(10)) return -1;

	stop_ddns();	// avoid to trigger DOD
	stop_ntpc();

	unlink("/tmp/ppp/link");

	proto = get_wan_proto();
	if (proto == WP_L2TP || proto == WP_PPTP) {
		/* clear dns from the resolv.conf */
		nvram_set("wan_get_dns","");
		dns_to_resolv();

		if (proto == WP_L2TP) {
			route_del(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"),
				nvram_safe_get("wan_gateway"), "255.255.255.255"); // fixed routing problem in Israel by kanki
		}

		// Restore the default gateway for WAN interface
		nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));

		// Set default route to gateway if specified
		route_del(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0");
		route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0");
	}

	if (nvram_get_int("ppp_demand")) {
		killall("listen", SIGKILL);
		eval("listen", nvram_safe_get("lan_ifname"));
	}

	TRACE_PT("end\n");
	return 1;
}
コード例 #8
0
void mount_2nd_jffs2(void)
{
        int format = 0;
        char s[256];
        int size;
        int part;
        struct statfs sf;
        int model = 0;
	int i = 0;

	_dprintf("Mount 2nd jffs2...\n");
	while(1) {
        	if (wait_action_idle(10)) break;
		else i++;

		if(i>=10) {
			_dprintf("Mount 2nd jffs2 failed!");
			return;
		}
	}

        if (!mtd_getinfo(SECOND_JFFS2_PARTITION, &part, &size)) {
		_dprintf("Can not get 2nd jffs2 information!");
		return;
	}
	_dprintf("2nd jffs2: %d, %d\n", part, size);

        model = get_model();

        if (statfs(SECOND_JFFS2_PATH, &sf) == 0) {
                switch(model) {
                        case MODEL_RTAC56S:
                        case MODEL_RTAC56U:
			case MODEL_RTAC3200:
                        case MODEL_DSLAC68U:
                        case MODEL_RPAC68U:
                        case MODEL_RTAC68U:
                        case MODEL_RTAC87U:
			case MODEL_RTN18U:
                        case MODEL_RTN65U:
                        case MODEL_RTN14U: // it should be better to use LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,6,36)
                        {
                                if (sf.f_type != 0x73717368 /* squashfs */) {
                                        // already mounted
                                        notice_set("2nd_jffs", format ? "Formatted" : "Loaded");
                                        return;
                                }
                                break;
                        }
                        default:
                        {
                                if (sf.f_type != 0x71736873 /* squashfs */) {
                                        // already mounted
                                        notice_set("2nd_jffs", format ? "Formatted" : "Loaded");
                                        return;
                                }
                                break;
                        }
                }
        }

        modprobe(JFFS_NAME);
        sprintf(s, MTD_BLKDEV(%d), part);

	i = 0;
        while(mount(s, SECOND_JFFS2_PATH, JFFS_NAME, MS_RDONLY, "") != 0) {
		_dprintf("Mount 2nd jffs failed! Try again...\n");
		if(i >= 10) {
			_dprintf("Mount 2nd jffs 10 times failed, stop mount!");
			break;
		}
		i++;
        }

	return;
}
コード例 #9
0
void start_jffs2(void)
{
	if (!nvram_match("jffs2_on", "1")) {
		notice_set("jffs", "");
		return;
	}

	int format = 0;
	char s[256];
	int size;
	int part;
	const char *p;
	struct statfs sf;
	int model = 0;
	int i = 0;

        while(1) {
		if (wait_action_idle(10)) break;
		else i++;

		if(i>=10) {
			_dprintf("Mount jffs2 failed!");
			return;
		}
	}

	if (!mtd_getinfo(JFFS2_PARTITION, &part, &size)) return;

	model = get_model();
	_dprintf("start jffs2: %d, %d\n", part, size);
	if (nvram_match("jffs2_format", "1")) {
		nvram_set("jffs2_format", "0");
		nvram_commit_x();
		if (!mtd_erase(JFFS_NAME)) {
			error("formatting");
			return;
		}
		format = 1;
	}

	sprintf(s, "%d", size);
	p = nvram_get("jffs2_size");
	if ((p == NULL) || (strcmp(p, s) != 0)) {
		if (format) {
			nvram_set("jffs2_size", s);
			nvram_commit_x();
		}
		else if ((p != NULL) && (*p != 0)) {
			error("verifying known size of");
			return;
		}
	}

	if (statfs("/jffs", &sf) == 0) { 
		switch(model) {
			case MODEL_RTAC56S: 
			case MODEL_RTAC56U: 
			case MODEL_RTAC3200:
			case MODEL_DSLAC68U:
			case MODEL_RPAC68U: 
			case MODEL_RTAC68U: 
			case MODEL_RTAC87U:
			case MODEL_RTN18U: 
			case MODEL_RTN65U:
			case MODEL_RTN14U: // it should be better to use LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,6,36)
			{
				if (sf.f_type != 0x73717368 /* squashfs */) {
					// already mounted
					notice_set("jffs", format ? "Formatted" : "Loaded");
					return;
				}
				break;
			}
			default:
			{
	                        if (sf.f_type != 0x71736873 /* squashfs */) {
        	                        // already mounted
                	                notice_set("jffs", format ? "Formatted" : "Loaded");
                        	        return;
				}
				break;
			}
		}
	}

	if (nvram_get_int("jffs2_clean_fs")) {
		if (!mtd_unlock(JFFS2_PARTITION)) {
			error("unlocking");
			return;
		}
	}
	modprobe(JFFS_NAME);
	sprintf(s, MTD_BLKDEV(%d), part);

	if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
		if (!mtd_erase(JFFS_NAME)) {
                        error("formatting");
                        return;
                }

		format = 1;
		if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
			_dprintf("*** jffs2 2-nd mount error\n");
			//modprobe_r(JFFS_NAME);
			error("mounting");
			return;
		}
	}

#ifdef TEST_INTEGRITY
	int test;

	if (format) {
		if (f_write("/jffs/.tomato_do_not_erase", &size, sizeof(size), 0, 0) != sizeof(size)) {
			stop_jffs2(0);
			error("setting integrity test for");
			return;
		}
	}
	if ((f_read("/jffs/.tomato_do_not_erase", &test, sizeof(test)) != sizeof(test)) || (test != size)) {
		stop_jffs2(0);
		error("testing integrity of");
		return;
	}
#endif

	if (nvram_get_int("jffs2_clean_fs")) {
		_dprintf("Clean /jffs/*\n");
		system("rm -fr /jffs/*");
		nvram_unset("jffs2_clean_fs");
		nvram_commit_x();
	}

	notice_set("jffs", format ? "Formatted" : "Loaded");

	if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
		chdir("/jffs");
		system(p);
		chdir("/");
	}
	run_userfile("/jffs", ".asusrouter", "/jffs", 3);

	if (!check_if_dir_exist("/jffs/scripts/")) mkdir("/jffs/scripts/", 0755);
	if (!check_if_dir_exist("/jffs/configs/")) mkdir("/jffs/configs/", 0755);
}
コード例 #10
0
void format_mount_2nd_jffs2(void)
{
        int format = 0;
        char s[256];
        int size;
        int part;
        const char *p;
        struct statfs sf;
        int model = 0;

        if (!wait_action_idle(10)) return;

        if (!mtd_getinfo(SECOND_JFFS2_PARTITION, &part, &size)) return;
	_dprintf("Format 2nd jffs2: %d, %d\n", part, size);

        model = get_model();

        sprintf(s, "%d", size);
        if (statfs(SECOND_JFFS2_PATH, &sf) == 0) {
                switch(model) {
                        case MODEL_RTAC56S:
                        case MODEL_RTAC56U:
			case MODEL_RTAC3200:
                        case MODEL_DSLAC68U:
                        case MODEL_RPAC68U:
                        case MODEL_RTAC68U:
                        case MODEL_RTAC87U:
			case MODEL_RTN18U:
                        case MODEL_RTN65U:
                        case MODEL_RTN14U: // it should be better to use LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,6,36)
                        {
                                if (sf.f_type != 0x73717368 /* squashfs */) {
                                        // already mounted
                                        notice_set("2nd_jffs", format ? "Formatted" : "Loaded");
                                        return;
                                }
                                break;
                        }
                        default:
                        {
                                if (sf.f_type != 0x71736873 /* squashfs */) {
                                        // already mounted
                                        notice_set("2nd_jffs", format ? "Formatted" : "Loaded");
                                        return;
                                }
                                break;
                        }
                }
        }

        if (!mtd_unlock(SECOND_JFFS2_PARTITION)) {
                error("unlocking");
                return;
        }

        modprobe(JFFS_NAME);
        sprintf(s, MTD_BLKDEV(%d), part);
        if (mount(s, SECOND_JFFS2_PATH, JFFS_NAME, MS_NOATIME, "") != 0) {
		if( (get_model()==MODEL_RTAC56U || get_model()==MODEL_RTAC56S || get_model()==MODEL_RTAC3200 || get_model()==MODEL_RTAC68U || get_model()==MODEL_RPAC68U || get_model()==MODEL_DSLAC68U || get_model()==MODEL_RTAC87U || get_model()==MODEL_RTN18U) ^ (!mtd_erase(JFFS_NAME)) ){
                        error("formatting");
                        return;
                }

                format = 1;
                if (mount(s, SECOND_JFFS2_PATH, JFFS_NAME, MS_NOATIME, "") != 0) {
                        _dprintf("*** jffs2 2-nd mount error\n");
                        //modprobe_r(JFFS_NAME);
                        error("mounting");
                        return;
                }
        }

	sprintf(s, "rm -rf %s/*", SECOND_JFFS2_PATH);
        system(s);

        notice_set("2nd_jffs", format ? "Formatted" : "Loaded");

        if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
                chdir(SECOND_JFFS2_PATH);
                system(p);
                chdir("/");
        }
        run_userfile(SECOND_JFFS2_PATH, ".asusrouter", SECOND_JFFS2_PATH, 3);

}
コード例 #11
0
ファイル: jffs2.c プロジェクト: StephenMacras/dsl-n55u-bender
void start_jffs2(void)
{
	if (!nvram_match("jffs2_on", "1")) {
		notice_set("jffs", "");
		return;
	}

	int format = 0;
	char s[256];
	int size;
	int part;
	const char *p;
	struct statfs sf;

	if (!wait_action_idle(10)) return;

	if (!mtd_getinfo("jffs2", &part, &size)) return;

_dprintf("*** jffs2: %d, %d\n", part, size);
	if (nvram_match("jffs2_format", "1")) {
		nvram_set("jffs2_format", "0");

		if (!mtd_erase("jffs2")) {
			error("formatting");
			return;
		}

		format = 1;
	}

	sprintf(s, "%d", size);
	p = nvram_get("jffs2_size");
	if ((p == NULL) || (strcmp(p, s) != 0)) {
		if (format) {
			nvram_set("jffs2_size", s);
			nvram_commit_x();
		}
		else if ((p != NULL) && (*p != 0)) {
			error("verifying known size of");
			return;
		}
	}

	if ((statfs("/jffs", &sf) == 0) && (sf.f_type != 0x71736873 /* squashfs */)) {
		// already mounted
		notice_set("jffs", format ? "Formatted" : "Loaded");
		return;
	}
	if (!mtd_unlock("jffs2")) {
		error("unlocking");
		return;
	}
	modprobe(JFFS_NAME);
	sprintf(s, MTD_BLKDEV(%d), part);
	if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
_dprintf("*** jffs2 mount error\n");
		//modprobe_r(JFFS_NAME);
		error("mounting");
		return;
	}
#ifdef TEST_INTEGRITY
	int test;

	if (format) {
		if (f_write("/jffs/.tomato_do_not_erase", &size, sizeof(size), 0, 0) != sizeof(size)) {
			stop_jffs2();
			error("setting integrity test for");
			return;
		}
	}
	if ((f_read("/jffs/.tomato_do_not_erase", &test, sizeof(test)) != sizeof(test)) || (test != size)) {
		stop_jffs2();
		error("testing integrity of");
		return;
	}
#endif

	notice_set("jffs", format ? "Formatted" : "Loaded");

	if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
		chdir("/jffs");
		system(p);
		chdir("/");
	}
	run_userfile("/jffs", ".asusrouter", "/jffs", 3);

	if (!check_if_dir_exist("/jffs/scripts/")) mkdir("/jffs/scripts/", 0755);
	if (!check_if_dir_exist("/jffs/configs/")) mkdir("/jffs/configs/", 0755);

}
コード例 #12
0
ファイル: cstats.c プロジェクト: themiron/asuswrt-merlin
static void save(int quick) {
	int i;
	int n;
	int b;
	char hgz[256];
	char tmp[256];
	char bak[256];
	char bkp[256];
	time_t now;
	struct tm *tms;
	static int lastbak = -1;

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

	f_write("/var/lib/misc/cstats-stime", &save_utime, sizeof(save_utime), 0, 0);

	n = save_history_from_tree(history_fn);
	_dprintf("%s: saved %d records from tree on file %s\n", __FUNCTION__, n, history_fn);

	_dprintf("%s: write source=%s\n", __FUNCTION__, save_path);
	f_write_string(source_fn, save_path, 0, 0);

	if (quick) {
		return;
	}

	sprintf(hgz, "%s.gz", history_fn);

	if (save_path[0] != 0) {
		strcpy(tmp, save_path);
		strcat(tmp, ".tmp");

		for (i = 15; i > 0; --i) {
			if (!wait_action_idle(10)) {
				_dprintf("%s: busy, not saving\n", __FUNCTION__);
			}
			else {
				_dprintf("%s: cp %s %s\n", __FUNCTION__, hgz, tmp);
				if (eval("cp", hgz, tmp) == 0) {
					_dprintf("%s: copy ok\n", __FUNCTION__);

					if (!nvram_match("rstats_bak", "0")) {
						now = time(0);
						tms = localtime(&now);
						if (lastbak != tms->tm_yday) {
							strcpy(bak, save_path);
							n = strlen(bak);
							if ((n > 3) && (strcmp(bak + (n - 3), ".gz") == 0)) n -= 3;
//							sprintf(bak + n, "_%d.bak", ((tms->tm_yday / 7) % 3) + 1);
//							if (eval("cp", save_path, bak) == 0) lastbak = tms->tm_yday;
							strcpy(bkp, bak);
							for (b = HI_BACK-1; b > 0; --b) {
								sprintf(bkp + n, "_%d.bak", b + 1);
								sprintf(bak + n, "_%d.bak", b);
								rename(bak, bkp);
							}
							if (eval("cp", "-p", save_path, bak) == 0) lastbak = tms->tm_yday;
						}
					}

					_dprintf("%s: rename %s %s\n", __FUNCTION__, tmp, save_path);
					if (rename(tmp, save_path) == 0) {
						_dprintf("%s: rename ok\n", __FUNCTION__);
						break;
					}
				}
			}

			// might not be ready
			sleep(3);
			if (gotterm) break;
		}
	}
}
コード例 #13
0
void start_jffs2(void)
{
	if (!nvram_match("jffs2_enable", "1")) {
		notice_set("jffs", "");
		return;
	}

	int format = 0;
	char s[256];
	int size;
	int part;
	const char *p;
	int model = 0;
	int i = 0;

        while(1) {
		if (wait_action_idle(10)) break;
		else i++;

		if(i>=10) {
			_dprintf("Mount jffs2 failed!");
			return;
		}
	}

	if (!mtd_getinfo(JFFS2_PARTITION, &part, &size)) return;

	model = get_model();
	_dprintf("start jffs2: %d, %d\n", part, size);
	if (nvram_match("jffs2_format", "1")) {
		nvram_set("jffs2_format", "0");
		nvram_commit_x();
		if (mtd_erase(JFFS2_MTD_NAME)) {
			error("formatting");
			return;
		}
		format = 1;
	}

	sprintf(s, "%d", size);
	p = nvram_get("jffs2_size");
	if ((p == NULL) || (strcmp(p, s) != 0)) {
		if (format) {
			nvram_set("jffs2_size", s);
			nvram_commit_x();
		}
		else if ((p != NULL) && (*p != 0)) {
			error("verifying known size of");
			return;
		}
	}

	if(!check_in_rootfs("/jffs", "jffs", format))
		return;

	if (nvram_get_int("jffs2_clean_fs")) {
		if (!mtd_unlock(JFFS2_PARTITION)) {
			error("unlocking");
			return;
		}
	}
	modprobe(JFFS_NAME);
	sprintf(s, MTD_BLKDEV(%d), part);

	if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
		if (mtd_erase(JFFS2_MTD_NAME)) {
			jffs2_fail = 1;
                        error("formatting");
                        return;
                }

		format = 1;
		if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
			_dprintf("*** jffs2 2-nd mount error\n");
			//modprobe_r(JFFS_NAME);
			error("mounting");
			jffs2_fail = 1;
			return;
		}
	}

	if(nvram_match("force_erase_jffs2", "1")) {
		_dprintf("\n*** force erase jffs2 ***\n");
		mtd_erase(JFFS2_MTD_NAME);
		nvram_set("jffs2_clean_fs", "1");
		nvram_commit();
		reboot(RB_AUTOBOOT);
	}
#ifdef TEST_INTEGRITY
	int test;

	if (format) {
		if (f_write("/jffs/.tomato_do_not_erase", &size, sizeof(size), 0, 0) != sizeof(size)) {
			stop_jffs2(0);
			error("setting integrity test for");
			return;
		}
	}
	if ((f_read("/jffs/.tomato_do_not_erase", &test, sizeof(test)) != sizeof(test)) || (test != size)) {
		stop_jffs2(0);
		error("testing integrity of");
		return;
	}
#endif

	if (nvram_get_int("jffs2_clean_fs")) {
		_dprintf("Clean /jffs/*\n");
		system("rm -fr /jffs/*");
		nvram_unset("jffs2_clean_fs");
		nvram_commit_x();
	}

	notice_set("jffs", format ? "Formatted" : "Loaded");
	jffs2_fail = 0;

	if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
		chdir("/jffs");
		system(p);
		chdir("/");
	}
	run_userfile("/jffs", ".asusrouter", "/jffs", 3);

	if (!check_if_dir_exist("/jffs/scripts/")) mkdir("/jffs/scripts/", 0755);
	if (!check_if_dir_exist("/jffs/configs/")) mkdir("/jffs/configs/", 0755);
}
コード例 #14
0
static void load(long uptime)
{
	int i;
	long t;
	char *bi, *bo;
	int n;
	char hgz[256];
	unsigned char mac[6];

	strlcpy(save_path, nvram_safe_get("rstats_path"), sizeof(save_path) - 32);
	if (((n = strlen(save_path)) > 0) && (save_path[n - 1] == '/')) {
		ether_atoe(nvram_safe_get("et0macaddr"), mac);
		sprintf(save_path + n, "ddwrt_rstats_%02x%02x%02x%02x%02x%02x.gz",
			mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
	}

	if (f_read("/var/lib/misc/rstats-stime", &save_utime, sizeof(save_utime)) != sizeof(save_utime)) {
		save_utime = 0;
	}
	t = uptime + get_stime();
	if ((save_utime < uptime) || (save_utime > t)) save_utime = t;
	_dprintf("uptime = %dm, save_utime = %dm\n", uptime / 60, save_utime / 60);

	speed_count = decomp(speed_fn, speed, sizeof(speed[0]), MAX_SPEED_IF);
	_dprintf("load speed_count = %d\n", speed_count);
	
	if ((load_history()) || (save_path[0] == 0)) return;

	sprintf(hgz, "%s.gz", history_fn);

	if (strcmp(save_path, "*nvram") == 0) {
		if (!wait_action_idle(60)) exit(0);
		
		bi = nvram_safe_get("rstats_data");
		if ((n = strlen(bi)) > 0) {
			if ((bo = malloc(base64_decoded_len(n))) != NULL) {
				n = base64_decode(bi, bo, n);
				f_write(hgz, bo, n, 0, 0);
				free(bo);
				_dprintf("from nvram = %d\n", n);
			}
		}
	}
	else {
		for (i = 30; i > 0; --i) {
			if (!wait_action_idle(60)) exit(0);

			_dprintf("copy %s to %s\n", save_path, hgz);
			if (eval("cp", save_path, hgz) == 0) break;

			// not ready...
			sleep(3);
		}
		if (i == 0) {
			save_utime = uptime + 60;
			_dprintf("forcing early save on next\n");
		}
	}

	load_history();
}
コード例 #15
0
static void save(int exiting)
{
	int i;
	time_t now;
	char *bi, *bo;
	int n;
	char hgz[256];
	char tmp[128];

	_dprintf("save\n");

	f_write("/var/lib/misc/rstats-stime", &save_utime, sizeof(save_utime), 0, 0);

	comp(speed_fn, speed, sizeof(speed[0]) * speed_count);
	
	if ((now = time(0)) < Y2K) {
		_dprintf("time not set\n");
		return;
	}
	
	comp(history_fn, &history, sizeof(history));
	
	if (exiting) {
		_dprintf("exiting=1\n");
		return;
	}

	_dprintf("save ext\n");
	
	sprintf(hgz, "%s.gz", history_fn);
	
	if (strcmp(save_path, "*nvram") == 0) {
		if (!wait_action_idle(10)) {
			_dprintf("busy, not saving\n");
			return;
		}

		if ((n = f_read_alloc(hgz, &bi, 20 * 1024)) > 0) {
			if ((bo = malloc(base64_encoded_len(n) + 1)) != NULL) {
				n = base64_encode(bi, bo, n);
				bo[n] = 0;
				_dprintf("rstats_data=%s\n", bo);
				nvram_set("rstats_data", bo);
				if (!nvram_match("debug_nocommit", "1")) nvram_commit();
				free(bo);
			}
		}
		free(bi);
	}
	else if (save_path[0] != 0) {
		strcpy(tmp, save_path);
		strcat(tmp, "_tmp");

		for (i = 15; i > 0; --i) {
			if (!wait_action_idle(10)) {
				_dprintf("busy, not saving\n");
			}
			else {
				_dprintf("copy %s to %s\n", hgz, save_path);
				if (eval("cp", hgz, tmp) == 0) {
					_dprintf("copy ok\n");
					if (rename(tmp, save_path) == 0) {
						_dprintf("rename ok\n");
						break;
					}
				}
			}

			// might not be ready
			sleep(3);
		}
	}
}
コード例 #16
0
int ipup_main(int argc, char **argv)
{
	char *wan_ifname;
	char *value;
	char buf[256];
	const char *p;

	TRACE_PT("begin\n");

	killall("listen", SIGKILL);
	
	if (!wait_action_idle(10)) return -1;

	wan_ifname = safe_getenv("IFNAME");
	if ((!wan_ifname) || (!*wan_ifname)) return -1;
	nvram_set("wan_iface", wan_ifname);	// ppp#

	// ipup receives six arguments:
	//   <interface name>  <tty device>  <speed> <local IP address> <remote IP address> <ipparam>
	//   ppp1 vlan1 0 71.135.98.32 151.164.184.87 0

	f_write_string("/tmp/ppp/link", argv[1], 0, 0);
	
	if ((p = getenv("IPREMOTE"))) {
		nvram_set("wan_gateway_get", p);
		TRACE_PT("IPREMOTE=%s\n", p);
	}

	if ((value = getenv("IPLOCAL"))) {
		_dprintf("IPLOCAL=%s\n", value);

		switch (get_wan_proto()) {
		case WP_PPPOE:
		case WP_PPP3G:
			nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));		// store last ip address
			nvram_set("wan_ipaddr", value);
			nvram_set("wan_netmask", "255.255.255.255");
			break;
		case WP_PPTP:
		case WP_L2TP:
			nvram_set("wan_ipaddr_buf", nvram_safe_get("ppp_get_ip"));
			break;
		}

		if (!nvram_match("ppp_get_ip", value)) {
			ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
			nvram_set("ppp_get_ip", value);
		}

		_ifconfig(wan_ifname, IFUP, value, "255.255.255.255", (p && (*p)) ? p : NULL);
	}

	buf[0] = 0;
	if ((p = getenv("DNS1")) != NULL) strlcpy(buf, p, sizeof(buf));
	if ((p = getenv("DNS2")) != NULL) {
		if (buf[0]) strlcat(buf, " ", sizeof(buf));
		strlcat(buf, p, sizeof(buf));
	}
	nvram_set("wan_get_dns", buf);
	TRACE_PT("DNS=%s\n", buf);

	if ((value = getenv("AC_NAME"))) nvram_set("ppp_get_ac", value);
	if ((value = getenv("SRV_NAME"))) nvram_set("ppp_get_srv", value);
	if ((value = getenv("MTU"))) nvram_set("wan_run_mtu", value);

	start_wan_done(wan_ifname);

	TRACE_PT("end\n");
	return 0;
}
コード例 #17
0
ファイル: rstats.c プロジェクト: hebjt/asuswrt-merlin
static void save(int quick)
{
	int i;
	char *bi, *bo;
	int n;
	int b;
	char hgz[256];
	char tmp[256];
	char bak[256];
	char bkp[256];
	time_t now;
	struct tm *tms;
	static int lastbak = -1;

	//_dprintf("%s: quick=%d\n", __FUNCTION__, quick);

	f_write("/var/lib/misc/rstats-stime", &save_utime, sizeof(save_utime), 0, 0);

	comp(speed_fn, speed, sizeof(speed[0]) * speed_count);

/*
	if ((now = time(0)) < Y2K) {
		_dprintf("%s: time not set\n", __FUNCTION__);
		return;
	}
*/

	comp(history_fn, &history, sizeof(history));

	//_dprintf("%s: write source=%s\n", __FUNCTION__, save_path);
	f_write_string(source_fn, save_path, 0, 0);

	if (quick) {
		return;
	}

	sprintf(hgz, "%s.gz", history_fn);

	if (strcmp(save_path, "*nvram") == 0) {
		if (!wait_action_idle(10)) {
			_dprintf("%s: busy, not saving\n", __FUNCTION__);
			return;
		}

		if ((n = f_read_alloc(hgz, &bi, 20 * 1024)) > 0) {
			if ((bo = malloc(base64_encoded_len(n) + 1)) != NULL) {
				n = base64_encode(bi, bo, n);
				bo[n] = 0;
				nvram_set("rstats_data", bo);
				if (!nvram_match("debug_nocommit", "1")) nvram_commit();

				_dprintf("%s: nvram commit\n", __FUNCTION__);

				free(bo);
			}
		}
		free(bi);
	}
	else if (save_path[0] != 0) {
		strcpy(tmp, save_path);
		strcat(tmp, ".tmp");

		for (i = 15; i > 0; --i) {
			if (!wait_action_idle(10)) {
				_dprintf("%s: busy, not saving\n", __FUNCTION__);
			}
			else {
				_dprintf("%s: cp %s %s\n", __FUNCTION__, hgz, tmp);
				if (eval("cp", hgz, tmp) == 0) {
					_dprintf("%s: copy ok\n", __FUNCTION__);

					if (!nvram_match("rstats_bak", "0")) {
						now = time(0);
						tms = localtime(&now);
						if (lastbak != tms->tm_yday) {
							strcpy(bak, save_path);
							n = strlen(bak);
							if ((n > 3) && (strcmp(bak + (n - 3), ".gz") == 0)) n -= 3;
							strcpy(bkp, bak);
							for (b = HI_BACK-1; b > 0; --b) {
								sprintf(bkp + n, "_%d.bak", b + 1);
								sprintf(bak + n, "_%d.bak", b);
								rename(bak, bkp);
							}
							if (eval("cp", "-p", save_path, bak) == 0) lastbak = tms->tm_yday;
						}
					}

					_dprintf("%s: rename %s %s\n", __FUNCTION__, tmp, save_path);
					if (rename(tmp, save_path) == 0) {
						_dprintf("%s: rename ok\n", __FUNCTION__);
						break;
					}
				}
			}

			// might not be ready
			sleep(3);
			if (gotterm) break;
		}
	}
}
コード例 #18
0
static int restore_main(int argc, char **argv)
{
	char *name;
	int test;
	int force;
	int commit;
	backup_t data;
	unsigned int size;
	char s[512];
	char tmp[128];
	unsigned long hw;
	char current[NVRAM_SPACE];
	char *b, *bk, *bv;
	char *c, *ck, *cv;
	int nset;
	int nunset;
	int nsame;
	int cmp;
	int i;

	test = 0;
	force = 0;
	commit = 1;
	name = NULL;
	for (i = 1; i < argc; ++i) {
		if (argv[i][0] == '-') {
			if (strcmp(argv[i], "--test") == 0) {
				test = 1;
			}
			else if (strcmp(argv[i], "--force") == 0) {
				force = 1;
			}
			else if (strcmp(argv[i], "--forceall") == 0) {
				force = 2;
			}
			else if (strcmp(argv[i], "--nocommit") == 0) {
				commit = 0;
			}
			else {
				help();
			}
		}
		else {
			name = argv[i];
		}
	}
	if (!name) help();

	strcpy(tmp, "/tmp/nvramXXXXXX");
	mktemp(tmp);
	sprintf(s, "gzip -d < %s > %s", name, tmp);
	if (system(s) != 0) {
		unlink(tmp);
		printf("Error decompressing file.\n");
		return 1;
	}

	size = f_size(tmp);
	if ((size <= (sizeof(data) - sizeof(data.buffer))) || (size > sizeof(data)) ||
		(f_read(tmp, &data, sizeof(data)) != size)) {
		unlink(tmp);
		printf("Invalid data size or read error.\n");
		return 1;
	}

	unlink(tmp);

	if (data.sig != V1) {
		printf("Invalid signature: %08lX / %08lX\n", data.sig, V1);
		return 1;
	}

	hw = check_hw_type();
	if ((data.hwid != hw) && (!force)) {
		printf("Invalid hardware type: %08lX / %08lX\n", data.hwid, hw);
		return 1;
	}

	// 1 - check data

	size -= sizeof(data) - sizeof(data.buffer);
	if ((data.buffer[size - 1] != 0) || (data.buffer[size - 2] != 0)) {
CORRUPT:
		printf("Corrupted data area.\n");
		return 1;
	}

	b = data.buffer;
	while (*b) {
		bk = b;
		b += strlen(b) + 1;
		if ((bv = strchr(bk, '=')) == NULL) {
			goto CORRUPT;
		}
		*bv = 0;
		if (strcmp(bk, "et0macaddr") == 0) {
			if (!nvram_match(bk, bv + 1)) {
				if (!force) {
					printf("Cannot restore on a different router.\n");
					return 1;
				}
			}
		}
		*bv = '=';
	}
	if (((b - data.buffer) + 1) != size) {
		printf("Extra data found at the end.\n");
		return 1;
	}


	// 2 - set

	if (!test) {
		if (!wait_action_idle(10)) {
			printf("System busy.\n");
			return 1;
		}
		set_action(ACT_SW_RESTORE);
		led(LED_DIAG, 1);
	}

	nset = nunset = nsame = 0;

	b = data.buffer;
	while (*b) {
		bk = b;
		b += strlen(b) + 1;
		bv = strchr(bk, '=');
		*bv++ = 0;

		if ((force != 1) || (in_defaults(bk))) {
			if (!nvram_match(bk, bv)) {
				if (test) printf("nvram set \"%s=%s\"\n", bk, bv);
					else nvram_set(bk, bv);
				++nset;
			}
			else {
				++nsame;
			}
		}

		*(bv - 1) = '=';
	}


	// 3 - unset

	getall(current);
	c = current;
	while (*c) {
		ck = c;
		c += strlen(c) + 1;
		if ((cv = strchr(ck, '=')) == NULL) {
			printf("Invalid data in NVRAM: %s.", ck);
			continue;
		}
		*cv++ = 0;

		if ((force != 1) || (in_defaults(ck))) {
			cmp = 1;
			b = data.buffer;
			while (*b) {
				bk = b;
				b += strlen(b) + 1;
				bv = strchr(bk, '=');
				*bv++ = 0;
				cmp = strcmp(bk, ck);
				*(bv - 1) = '=';
				if (cmp == 0) break;
			}
			if (cmp != 0) {
				++nunset;
				if (test) printf("nvram unset \"%s\"\n", ck);
					else nvram_unset(ck);
			}
		}
	}
	

	if ((nset == 0) && (nunset == 0)) commit = 0;
	printf("\nPerformed %d set and %d unset operations. %d required no changes.\n%s\n",
		nset, nunset, nsame, commit ? "Committing..." : "Not commiting.");
	fflush(stdout);

	if (!test) {
		set_action(ACT_IDLE);
		if (commit) nvram_commit();
	}
	return 0;
}