Fixed USB LED GPIO Writes
[tomato.git] / release / src / router / rc / sched.c
blob8107abbba0e9ea8d25182dbb65d47134e10c1e3c
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(fmt, args...) _dprintf(fmt"\n", args)
15 static inline void unsched(const char *key)
17 DLOG("%s: %s", __FUNCTION__, key);
19 eval("cru", "d", (char *) key);
22 static void sched(const char *key, int resched)
24 int en;
25 int t;
26 int dow;
27 char s[64];
28 char w[32];
29 int i;
30 struct tm tm;
31 long tt, qq;
33 // en,time,days
34 if ((sscanf(nvram_safe_get(key), "%d,%d,%d", &en, &t, &dow) != 3) || (!en)) {
35 unsched(key);
36 return;
39 DLOG("%s: %s", __FUNCTION__, key);
41 if (resched) {
42 sprintf(s, "%s_last", key);
43 sprintf(w, "%ld", time(0));
44 nvram_set(s, w);
46 DLOG("%s: set %s=%s", __FUNCTION__, s, w);
48 if (t >= -5) return;
51 if ((dow & 0x7F) == 0) dow = 0x7F;
52 w[0] = 0;
53 w[1] = 0;
54 for (i = 0; i < 7; ++i) {
55 if (dow & (1 << i)) {
56 sprintf(w + strlen(w), ",%d", i);
60 if (t >= 0) { // specific time
61 sprintf(s, "%d %d * * %s sched %s", t % 60, t / 60, w + 1, key);
63 else { // every ...
64 t = -t;
65 if (t <= 5) { // 1 to 5m = a simple cron job
66 sprintf(s, "*/%d * * * %s sched %s", t, w + 1, key);
68 else {
69 t *= 60;
70 DLOG("%s: t=%d", __FUNCTION__, t);
72 tt = time(0) + 59;
73 tm = *localtime(&tt);
74 DLOG("%s: now=%d:%02d %02d/%02d", __FUNCTION__, tm.tm_hour, tm.tm_min, tm.tm_mon + 1, tm.tm_mday);
76 sprintf(s, "%s_last", key);
77 DLOG("%s: %s_last=%s, tt=%ld", __FUNCTION__, key, nvram_safe_get(s), tt);
78 qq = strtoul(nvram_safe_get(s), NULL, 10);
79 if ((qq + t) > tt) tt = qq;
81 tt += t;
82 while (1) {
83 tm = *localtime(&tt); // copy struct, otherwise we get weird stuff (!?)
84 DLOG("%s: %d:%02d %02d/%02d, tt=%ld", __FUNCTION__, tm.tm_hour, tm.tm_min, tm.tm_mon + 1, tm.tm_mday, tt);
85 // 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,
86 // dow, (dow & (1 << tm.tm_wday)), tt);
87 if (dow & (1 << tm.tm_wday)) break;
88 tt += 60;
91 sprintf(s, "%d %d %d %d * sched %s", tm.tm_min, tm.tm_hour, tm.tm_mday, tm.tm_mon + 1, key);
95 DLOG("%s: cru a %s %s", __FUNCTION__, key, s);
96 eval("cru", "a", (char *) key, s);
99 static inline int is_sched(const char *key)
101 return *nvram_safe_get(key) == '1';
104 int sched_main(int argc, char *argv[])
106 int n;
107 char s[64];
108 int log;
110 if (argc == 2) {
111 DLOG("%s: %s", __FUNCTION__, argv[1]);
113 log = nvram_contains_word("log_events", "sched");
115 if (strncmp(argv[1], "sch_", 4) == 0) {
116 wait_action_idle(5 * 60);
118 if (is_sched(argv[1])) {
119 if (strcmp(argv[1], "sch_rboot") == 0) {
120 if (log) syslog(LOG_INFO, "Performing scheduled %s...", "reboot");
121 eval("reboot");
122 return 0;
124 else if (strcmp(argv[1], "sch_rcon") == 0) {
125 sched(argv[1], 1);
126 if (log) syslog(LOG_INFO, "Performing scheduled %s...", "reconnect");
127 eval("service", "wan", "restart");
129 else if (strncmp(argv[1], "sch_c", 5) == 0) {
130 n = atoi(argv[1] + 5);
131 if ((n >= 1) && (n <= 5)) {
132 sched(argv[1], 1);
134 if (log) {
135 sprintf(s, "custom #%d", n);
136 syslog(LOG_INFO, "Performing scheduled %s...", s);
139 signal(SIGCHLD, chld_reap);
141 sprintf(s, "%s_cmd", argv[1]);
142 DLOG("%s: run=%s", __FUNCTION__, nvram_safe_get(s));
143 run_nvscript(s, "", 60);
147 else {
148 unsched(argv[1]);
151 else if (strcmp(argv[1], "start") == 0) {
152 while (time(0) < Y2K) {
153 sleep(1);
156 sched("sch_rboot", 0);
157 sched("sch_rcon", 0);
158 sched("sch_c1", 0);
159 sched("sch_c2", 0);
160 sched("sch_c3", 0);
161 sched("sch_c4", 0);
162 sched("sch_c5", 0);
166 return 0;
169 void start_sched(void)
171 DLOG("%s", __FUNCTION__);
173 killall("sched", SIGTERM);
174 xstart("sched", "start");
177 void stop_sched(void)
179 DLOG("%s", __FUNCTION__);
181 killall("sched", SIGTERM);
182 unsched("sch_rboot");
183 unsched("sch_rcon");
184 unsched("sch_sc1");
185 unsched("sch_sc2");
186 unsched("sch_sc3");
187 unsched("sch_sc4");
188 unsched("sch_sc5");