]> git.netwichtig.de Git - user/henk/code/snooze.git/blob - snooze.c
unset tm_isdst before doing date operations
[user/henk/code/snooze.git] / snooze.c
1 /*
2  * snooze - run a command at a particular time
3  *
4  * To the extent possible under law, Leah Neukirchen <leah@vuxu.org>
5  * has waived all copyright and related or neighboring rights to this work.
6  * http://creativecommons.org/publicdomain/zero/1.0/
7  */
8
9 #include <sys/stat.h>
10 #include <sys/types.h>
11
12 #include <ctype.h>
13 #include <errno.h>
14 #include <signal.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #include <unistd.h>
20
21 #ifdef __linux__
22 #include <sys/auxv.h>
23 #endif
24
25 static long slack = 60;
26 #define SLEEP_PHASE 300
27 static int nflag, vflag;
28
29 static int timewait = -1;
30 static int randdelay = 0;
31 static char *timefile;
32
33 static sig_atomic_t alarm_rang = 0;
34
35 static void
36 wakeup(int sig)
37 {
38         (void)sig;
39         alarm_rang = 1;
40 }
41
42 static long
43 parse_int(char **s, size_t minn, size_t maxn)
44 {
45         long n;
46         char *end;
47
48         errno = 0;
49         n = strtol(*s, &end, 10);
50         if (errno) {
51                 perror("strtol");
52                 exit(1);
53         }
54         if (n < (long)minn || n >= (long)maxn) {
55                 fprintf(stderr, "number outside %zd <= n < %zd\n", minn, maxn);
56                 exit(1);
57         }
58         *s = end;
59         return n;
60 }
61
62 static long
63 parse_dur(char *s)
64 {
65         long n;
66         char *end;
67
68         errno = 0;
69         n = strtol(s, &end, 10);
70         if (errno) {
71                 perror("strtol");
72                 exit(1);
73         }
74         if (n < 0) {
75                 fprintf(stderr, "negative duration\n");
76                 exit(1);
77         }
78         switch (*end) {
79         case 'm': n *= 60; break;
80         case 'h': n *= 60*60; break;
81         case 'd': n *= 24*60*60; break;
82         case 0: break;
83         default:
84                 fprintf(stderr, "junk after duration: %s\n", end);
85                 exit(1);
86         }
87         return n;
88 }
89
90 static int
91 parse(char *expr, char *buf, long bufsiz, int offset)
92 {
93         char *s;
94         long i, n = 0, n0 = 0;
95
96         memset(buf, ' ', bufsiz);
97
98         s = expr;
99         while (*s) {
100                 switch (*s) {
101                 case '0': case '1': case '2': case '3': case '4':
102                 case '5': case '6': case '7': case '8': case '9':
103                         n = parse_int(&s, -offset, bufsiz);
104                         buf[n+offset] = '*';
105                         break;
106                 case '-':
107                         n0 = n;
108                         s++;
109                         n = parse_int(&s, -offset, bufsiz);
110                         for (i = n0; i <= n; i++)
111                                 buf[i+offset] = '*';
112                         break;
113                 case '/':
114                         s++;
115                         n0 = n;
116                         if (*s)
117                                 n = parse_int(&s, -offset, bufsiz);
118                         if (n == 0)  // / = *
119                                 n = 1;
120                         for (i = n0; i < bufsiz; i += n)
121                                 buf[i+offset] = '*';
122                         break;
123                 case ',':
124                         s++;
125                         n = 0;
126                         break;
127                 case '*':
128                         s++;
129                         n = 0;
130                         for (i = 0; i < bufsiz; i++)
131                                 buf[i+offset] = '*';
132                         break;
133                 default:
134                         fprintf(stderr, "can't parse: %s %s\n", expr, s);
135                         exit(1);
136                 }
137         }
138
139         return 0;
140 }
141
142 char weekday[8] = {0};
143 char dayofmonth[32] = {0};
144 char month[13] = {0};
145 char dayofyear[367] = {0};
146 char weekofyear[54] = {0};
147 char hour[24] = {0};
148 char minute[60] = {0};
149 char second[61] = {0};
150
151 int
152 isoweek(struct tm *tm)
153 {
154         /* ugh, but easier than the correct formula... */
155         char weekstr[3];
156         char *w = weekstr;
157         strftime(weekstr, sizeof weekstr, "%V", tm);
158         return parse_int(&w, 1, 54);
159 }
160
161 time_t
162 find_next(time_t from)
163 {
164         time_t t;
165         struct tm *tm;
166
167         t = from;
168         tm = localtime(&t);
169
170 next_day:
171         while (!(
172             weekday[tm->tm_wday] == '*' &&
173             dayofmonth[tm->tm_mday-1] == '*' &&
174             month[tm->tm_mon] == '*' &&
175             weekofyear[isoweek(tm)-1] == '*' &&
176             dayofyear[tm->tm_yday] == '*')) {
177                 if (month[tm->tm_mon] != '*') {
178                         // if month is not good, step month
179                         tm->tm_mon++;
180                         tm->tm_mday = 1;
181                 } else {
182                         tm->tm_mday++;
183                 }
184
185                 tm->tm_isdst = -1;
186                 tm->tm_sec = 0;
187                 tm->tm_min = 0;
188                 tm->tm_hour = 0;
189
190                 t = mktime(tm);
191                 tm->tm_isdst = -1;
192
193                 if (t > from+(366*24*60*60))  // no result within a year
194                         return -1;
195         }
196
197         int y = tm->tm_yday;  // save yday
198
199         while (!(
200             hour[tm->tm_hour] == '*' &&
201             minute[tm->tm_min] == '*' &&
202             second[tm->tm_sec] == '*')) {
203                 if (hour[tm->tm_hour] != '*') {
204                         tm->tm_hour++;
205                         tm->tm_min = 0;
206                         tm->tm_sec = 0;
207                 } else if (minute[tm->tm_min] != '*') {
208                         tm->tm_min++;
209                         tm->tm_sec = 0;
210                 } else {
211                         tm->tm_sec++;
212                 }
213                 t = mktime(tm);
214                 if (tm->tm_yday != y)  // hit a different day, retry...
215                         goto next_day;
216         }
217
218         return t;
219 }
220
221 static char isobuf[25];
222 char *
223 isotime(const struct tm *tm)
224 {
225         strftime(isobuf, sizeof isobuf, "%FT%T%z", tm);
226         return isobuf;
227 }
228
229 int
230 main(int argc, char *argv[])
231 {
232         int c;
233         time_t t;
234         time_t now = time(0);
235         time_t last = 0;
236
237         /* default: every day at 00:00:00 */
238         memset(weekday, '*', sizeof weekday);
239         memset(dayofmonth, '*', sizeof dayofmonth);
240         memset(month, '*', sizeof month);
241         memset(dayofyear, '*', sizeof dayofyear);
242         memset(weekofyear, '*', sizeof weekofyear);
243         hour[0] = '*';
244         minute[0] = '*';
245         second[0] = '*';
246
247         while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
248                 switch (c) {
249                 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
250                 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
251                 case 'H': parse(optarg, hour, sizeof hour, 0); break;
252                 case 'M': parse(optarg, minute, sizeof minute, 0); break;
253                 case 'S': parse(optarg, second, sizeof second, 0); break;
254                 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
255                 case 'm': parse(optarg, month, sizeof month, -1); break;
256                 case 'w': parse(optarg, weekday, sizeof weekday, 0);
257                         // special case: sunday is both 0 and 7.
258                         if (weekday[7] == '*')
259                                 weekday[0] = '*';
260                         break;
261                 case 'n': nflag++; break;
262                 case 'v': vflag++; break;
263                 case 's': slack = parse_dur(optarg); break;
264                 case 'T': timewait = parse_dur(optarg); break;
265                 case 't': timefile = optarg; break;
266                 case 'R': randdelay = parse_dur(optarg); break;
267                 default:
268                         fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
269                             "  [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
270                             "Timespec: exact: 1,3,5\n"
271                             "          range: 1-7\n"
272                             "          every n-th: /10\n", argv[0]);
273                         exit(2);
274                 }
275
276         time_t start = now + 1;
277
278         if (timefile) {
279                 struct stat st;
280                 if (stat(timefile, &st) < 0) {
281                         if (errno != ENOENT)
282                                 perror("stat");
283                         t = start - slack - 1 - timewait;
284                 } else {
285                         t = st.st_mtime + 1;
286                 }
287                 if (timewait == -1) {
288                         while (t < start - slack)
289                                 t = find_next(t + 1);
290                         start = t;
291                 } else {
292                         if (t + timewait > start)
293                                 start = st.st_mtime + timewait;
294                 }
295         }
296
297         if (randdelay) {
298                 long delay;
299 #ifdef __linux__
300                 long rnd = getauxval(AT_RANDOM);
301                 if (rnd > 0)
302                         delay = rnd % randdelay;
303                 else
304 #endif
305                 {
306                         srand48(getpid() ^ start);
307                         delay = lrand48() % randdelay;
308                 }
309                 if (vflag)
310                         printf("randomly delaying by %lds.\n", delay);
311                 start += delay;
312         }
313
314         t = find_next(start);
315         if (t < 0) {
316                 fprintf(stderr, "no satisfying date found within a year.\n");
317                 exit(2);
318         }
319
320         if (nflag) {
321                 /* dry-run, just output the next 5 dates. */
322                 int i;
323                 for (i = 0; i < 5; i++) {
324                         char weekstr[4];
325                         struct tm *tm = localtime(&t);
326                         strftime(weekstr, sizeof weekstr, "%a", tm);
327                         printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
328                             isotime(tm),
329                             weekstr,
330                             ((t - now) / (60*60*24)),
331                             ((t - now) / (60*60)) % 24,
332                             ((t - now) / 60) % 60,
333                             (t - now) % 60);
334                         t = find_next(t + 1);
335                         if (t < 0) {
336                                 fprintf(stderr,
337                                     "no satisfying date found within a year.\n");
338                                 exit(2);
339                         }
340                 }
341                 exit(0);
342         }
343
344         struct tm *tm = localtime(&t);
345         if (vflag)
346                 printf("Snoozing until %s\n", isotime(tm));
347
348         // setup SIGALRM handler to force early execution
349         struct sigaction sa;
350         sa.sa_handler = &wakeup;
351         sa.sa_flags = SA_RESTART;
352         sigfillset(&sa.sa_mask);
353         sigaction(SIGALRM, &sa, NULL);  // XXX error handling
354
355         while (!alarm_rang) {
356                 now = time(0);
357                 if (now < last) {
358                         t = find_next(now);
359                         if (vflag)
360                                 printf("Time moved backwards, rescheduled for %s\n", isotime(tm));
361                 }
362                 t = mktime(tm);
363                 if (t <= now) {
364                         if (now - t <= slack)  // still about time
365                                 break;
366                         else {  // reschedule to next event
367                                 if (vflag)
368                                         printf("Missed execution at %s\n", isobuf);
369                                 t = find_next(now + 1);
370                                 tm = localtime(&t);
371                                 if (vflag)
372                                         printf("Snoozing until %s\n", isotime(tm));
373                         }
374                 } else {
375                         // do some sleeping, but not more than SLEEP_PHASE
376                         struct timespec ts;
377                         ts.tv_nsec = 0;
378                         ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
379                         last = now;
380                         nanosleep(&ts, 0);
381                         // we just iterate again when this exits early
382                 }
383         }
384
385         // no command to run, the outside script can go on
386         if (argc == optind)
387                 return 0;
388
389         if (vflag) {
390                 now = time(0);
391                 tm = localtime(&now);
392                 printf("Starting execution at %s\n", isotime(tm));
393         }
394
395         execvp(argv[optind], argv+optind);
396         perror("execvp");
397         return 255;
398 }