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