timer

Wake up alarm system
git clone git://git.vgx.fr/timer
Log | Files | Refs

timer.c (1247B)


      1 #include <sys/timerfd.h> /* timerfd_create() timerfd_settime() */
      2 #include <unistd.h> /* read(), get/set(e)uid() execl()*/
      3 #include <stdlib.h> /* atol() */
      4 #include <stdint.h> /* uint64_t */
      5 #include <stdio.h>  /* perror() */
      6 
      7 int main(int argc, char* argv[]) {
      8     if (argc != 3) {
      9         fprintf(stderr, "Usage: %s time cmd\n", argv[0]);
     10         return 1;
     11     }
     12     
     13     int err = seteuid(0);
     14     
     15     if (err == -1) {
     16         perror("seteuid (get root rights)");
     17         return 1;
     18     }
     19     
     20     int fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_CLOEXEC);
     21 
     22     if (fd == -1) {
     23         perror("timerfd creation");
     24         return 1;
     25     }
     26 
     27     const struct itimerspec tspec = { .it_value.tv_sec = atol(argv[1])};
     28 
     29     err = timerfd_settime(fd, 0, &tspec, NULL);
     30     
     31     if (err == -1) {
     32         perror("timerfd arming");
     33         return 1;
     34     }
     35 
     36     err = seteuid(getuid());
     37     
     38     if (err == -1) {
     39         perror("seteuid (drop root right)");
     40         return 1;
     41     }
     42    
     43     uint64_t res = 0;
     44     ssize_t size = read(fd, &res, sizeof(res));
     45 
     46     if (size == -1) {
     47         perror("timerfd read");
     48         return 1;
     49     }
     50 
     51     //printf("dring\n");
     52 
     53     execl("/bin/sh", "sh", "-c", argv[2], (char*)NULL);
     54 
     55     perror("exec");
     56     return 1;
     57 }
     58