4 Copyright (C) 2006-2009 Jonathan Zarate
12 // #define DLOG(args...) syslog(LOG_DEBUG, args)
13 #define DLOG(args...) do { } while(0)
16 static void unsched(const char *key
)
20 DLOG("%s: %s", __FUNCTION__
, key
);
22 sprintf(s
, "cru d %s", key
);
26 static void sched(const char *key
, int resched
)
38 if ((sscanf(nvram_safe_get(key
), "%d,%d,%d", &en
, &t
, &dow
) != 3) || (!en
)) {
43 DLOG("%s: %s", __FUNCTION__
, key
);
46 sprintf(s
, "%s_last", key
);
47 sprintf(w
, "%ld", time(0));
50 DLOG("%s: set %s=%s", __FUNCTION__
, s
, w
);
55 if ((dow
& 0x7F) == 0) dow
= 0x7F;
58 for (i
= 0; i
< 7; ++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
);
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
);
74 DLOG("%s: t=%d", __FUNCTION__
, t
);
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
;
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;
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
);
104 static inline int is_sched(const char *key
)
106 return *nvram_safe_get(key
) == '1';
109 int sched_main(int argc
, char *argv
[])
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");
129 else if (strcmp(argv
[1], "sch_rcon") == 0) {
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)) {
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);
156 else if (strcmp(argv
[1], "start") == 0) {
157 while (time(0) < Y2K
) {
161 sched("sch_rboot", 0);
162 sched("sch_rcon", 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");