4 Copyright (C) 2006-2009 Jonathan Zarate
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
)
34 if ((sscanf(nvram_safe_get(key
), "%d,%d,%d", &en
, &t
, &dow
) != 3) || (!en
)) {
39 DLOG("%s: %s", __FUNCTION__
, key
);
42 sprintf(s
, "%s_last", key
);
43 sprintf(w
, "%ld", time(0));
46 DLOG("%s: set %s=%s", __FUNCTION__
, s
, w
);
51 if ((dow
& 0x7F) == 0) dow
= 0x7F;
54 for (i
= 0; i
< 7; ++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
);
65 if (t
<= 5) { // 1 to 5m = a simple cron job
66 sprintf(s
, "*/%d * * * %s sched %s", t
, w
+ 1, key
);
70 DLOG("%s: t=%d", __FUNCTION__
, t
);
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
;
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;
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
[])
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");
124 else if (strcmp(argv
[1], "sch_rcon") == 0) {
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)) {
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);
151 else if (strcmp(argv
[1], "start") == 0) {
152 while (time(0) < Y2K
) {
156 sched("sch_rboot", 0);
157 sched("sch_rcon", 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");