Merge branch 'tomato-ND-usbmod-mixvpn' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / sched.c
blob09370780a6348649f923f442d54235a5e6b7a9a6
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include <time.h>
9 #include "rc.h"
12 // #define DLOG(args...) syslog(LOG_DEBUG, args)
13 #define DLOG(args...) do { } while(0)
16 static void unsched(const char *key)
18 char s[64];
20 DLOG("%s: %s", __FUNCTION__, key);
22 sprintf(s, "cru d %s", key);
23 system(s);
26 static void sched(const char *key, int resched)
28 int en;
29 int t;
30 int dow;
31 char s[64];
32 char w[32];
33 int i;
34 struct tm tm;
35 long tt, qq;
37 // en,time,days
38 if ((sscanf(nvram_safe_get(key), "%d,%d,%d", &en, &t, &dow) != 3) || (!en)) {
39 unsched(key);
40 return;
43 DLOG("%s: %s", __FUNCTION__, key);
45 if (resched) {
46 sprintf(s, "%s_last", key);
47 sprintf(w, "%ld", time(0));
48 nvram_set(s, w);
50 DLOG("%s: set %s=%s", __FUNCTION__, s, w);
52 if (t >= -5) return;
55 if ((dow & 0x7F) == 0) dow = 0x7F;
56 w[0] = 0;
57 w[1] = 0;
58 for (i = 0; i < 7; ++i) {
59 if (dow & (1 << i)) {
60 sprintf(w + strlen(w), ",%d", i);
64 if (t >= 0) { // specific time
65 sprintf(s, "cru a %s \"%d %d * * %s sched %s\"", key, t % 60, t / 60, w + 1, key);
67 else { // every ...
68 t = -t;
69 if (t <= 5) { // 1 to 5m = a simple cron job
70 sprintf(s, "cru a %s \"*/%d * * * %s sched %s\"", key, t, w + 1, key);
72 else {
73 t *= 60;
74 DLOG("%s: t=%d", __FUNCTION__, t);
76 tt = time(0) + 59;
77 tm = *localtime(&tt);
78 DLOG("%s: now=%d:%02d %02d/%02d", __FUNCTION__, tm.tm_hour, tm.tm_min, tm.tm_mon + 1, tm.tm_mday);
80 sprintf(s, "%s_last", key);
81 DLOG("%s: %s_last=%s, tt=%ld", __FUNCTION__, key, nvram_safe_get(s), tt);
82 qq = strtoul(nvram_safe_get(s), NULL, 10);
83 if ((qq + t) > tt) tt = qq;
85 tt += t;
86 while (1) {
87 tm = *localtime(&tt); // copy struct, otherwise we get weird stuff (!?)
88 DLOG("%s: %d:%02d %02d/%02d, tt=%ld", __FUNCTION__, tm.tm_hour, tm.tm_min, tm.tm_mon + 1, tm.tm_mday, tt);
89 // printf("%d:%02d %02d/%02d, w=%d, dow=%d, x=%d, tt=%ld\n", tm.tm_hour, tm.tm_min, tm.tm_mon + 1, tm.tm_mday, tm.tm_wday,
90 // dow, (dow & (1 << tm.tm_wday)), tt);
91 if (dow & (1 << tm.tm_wday)) break;
92 tt += 60;
95 sprintf(s, "cru a %s \"%d %d %d %d * sched %s\"", key, tm.tm_min, tm.tm_hour, tm.tm_mday, tm.tm_mon + 1, key);
99 DLOG("%s: %s", __FUNCTION__, s);
101 system(s);
104 static inline int is_sched(const char *key)
106 return *nvram_safe_get(key) == '1';
109 int sched_main(int argc, char *argv[])
111 int n;
112 char s[64];
113 int log;
115 if (argc == 2) {
116 DLOG("%s: %s", __FUNCTION__, argv[1]);
118 log = nvram_contains_word("log_events", "sched");
120 if (strncmp(argv[1], "sch_", 4) == 0) {
121 wait_action_idle(5 * 60);
123 if (is_sched(argv[1])) {
124 if (strcmp(argv[1], "sch_rboot") == 0) {
125 syslog(LOG_INFO, "Performing scheduled %s...", "reboot");
126 system("reboot");
127 return 0;
129 else if (strcmp(argv[1], "sch_rcon") == 0) {
130 sched(argv[1], 1);
131 if (log) syslog(LOG_INFO, "Performing scheduled %s...", "reconnect");
132 system("service wan restart");
134 else if (strncmp(argv[1], "sch_c", 5) == 0) {
135 n = atoi(argv[1] + 5);
136 if ((n >= 1) && (n <= 3)) {
137 sched(argv[1], 1);
139 if (log) {
140 sprintf(s, "custom #%d", n);
141 syslog(LOG_INFO, "Performing scheduled %s...", s);
144 signal(SIGCHLD, handle_reap);
146 sprintf(s, "%s_cmd", argv[1]);
147 DLOG("%s: run=%s", __FUNCTION__, nvram_safe_get(s));
148 run_nvscript(s, "", 60);
152 else {
153 unsched(argv[1]);
156 else if (strcmp(argv[1], "start") == 0) {
157 while (time(0) < Y2K) {
158 sleep(1);
161 sched("sch_rboot", 0);
162 sched("sch_rcon", 0);
163 sched("sch_c1", 0);
164 sched("sch_c2", 0);
165 sched("sch_c3", 0);
169 return 0;
172 void start_sched(void)
174 DLOG("%s", __FUNCTION__);
176 killall("sched", SIGTERM);
177 xstart("sched", "start");
180 void stop_sched(void)
182 DLOG("%s", __FUNCTION__);
184 killall("sched", SIGTERM);
185 unsched("sch_rboot");
186 unsched("sch_rcon");
187 unsched("sch_sc1");
188 unsched("sch_sc2");
189 unsched("sch_sc3");