aboutsummaryrefslogtreecommitdiff
path: root/contrib/sleeprun.c
blob: 01ea5c9e2784a126ad69be9dc9db6709549cb16e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <signal.h>
#include "../ninitfeatures.h"
#include "../error_table.h"

int main(int argc, char **argv) {
  int fd;
  unsigned long now, interval, last, ul[2] = { 0, 0 };
  char tmp[32],*p;
  if (argc<3) 
    die(1, "usage: sleeprun SleepFile interval [program args...]\n"
	    "usage: sleeprun -aNumber program [args...]");
  errmsg_iam(argv[0]);

  p = argv[1];
  if (p[0] == '-' && p[1] == 'a') {
    scan_ulong(p+2, &interval);
    if (interval) alarm(interval);
    argv += 2;
    goto do_it;
  }

  if (read_ulongs(argv[1], ul, 2) > 0) {
    errno=0;
    if (kill(ul[0],0)==0 || errno != ESRCH) {
      tmp[fmt_ulong(tmp, ul[0])] = 0;
      carp("WARNING: a program with PID ",tmp," is running now");
    }
  }

  last=ul[1];
  scan_ulong(argv[2], &interval);
  now = (unsigned long)time(0);

  last += interval;
  if (last > now) {
    nano_sleep(last-now, 0);
    now = (unsigned long)time(0);
  }

  if ((fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644)) >=0) {
    p = tmp;
    p += fmt_ulong(tmp, getpid()); *p++ = ':';
    p += fmt_ulong(p, now); *p++ = 0;
    write(fd,tmp,p-tmp);
    close(fd);
  }

  argv+=3;
 do_it:
  if (argv[0]) {
    char *argv_0 = argv[0];
    argv[0] = argv_0 + str_rchr(argv_0,'/');
    if (argv[0][0]) argv[0]++;
    else argv[0]=argv_0;

    pathexec_run(argv_0,argv,environ);
    carp("unable to run: ",argv_0,": ",error_string(table, errno));
    return 127;
  }
  return 0;
}