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