File: runalarm.c

package info (click to toggle)
cronutils 1.2-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 140 kB
  • sloc: ansic: 648; makefile: 32; sh: 1
file content (118 lines) | stat: -rw-r--r-- 3,075 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright 2000-2010 Google, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
qyou may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sysexits.h>
#include <unistd.h>

#include "subprocess.h"

int timeout = 60 * 60 * 24; /* 1 day */
volatile sig_atomic_t alarm_triggered = 0;

static void usage(char * prog) {
  fprintf(stderr,
          "Usage: %s [options] command [arg [arg...]]\n\n"
          "This program tries to run a command and, if the timeout is\n"
          "reached before the command exits, kills that process.\n"
          "Otherwise the errorcode of the command is returned.\n"
          "\noptions:\n"
          " -t timeout  time in seconds to wait before process is killed\n"
          " -d   send log messages to stderr as well as syslog.\n"
          " -h   print this help\n", prog);
 }

static void alarm_handler(int signum) {
  int old_errno;

  (void) signum; /* suppress unused parameter warnings */
  old_errno = errno;
  alarm_triggered = 1;
  kill_process_group();
  errno = old_errno;
}

static void set_timeout_alarm(void) {
  alarm(timeout);
}

int main(int argc, char ** argv) {
  int arg;
  char * progname;
  int status = -1;
  char * command;
  char ** command_args;
  char * endptr;
  struct sigaction sa, old_sa;
  int debug = 0;

  progname = argv[0];

  while ((arg = getopt(argc, argv, "+t:hd")) > 0) {
    switch (arg) {
    case 'h':
      usage(progname);
      exit(EXIT_SUCCESS);
      break;
    case 't':
      timeout = strtol(optarg, &endptr, 10);
      if (*endptr || !optarg) {
        fprintf(stderr, "invalid timeout specified: %s\n", optarg);
        exit(EX_DATAERR);
      }
      break;
    case 'd':
      debug = LOG_PERROR;
      break;
    default:
      break;
    }
  }
  if (optind >= argc) {
    usage(progname);
    exit(EXIT_FAILURE);
  } else {
    command = strdup(argv[optind]);
    command_args = &argv[optind];
  }

  openlog(progname, debug|LOG_ODELAY|LOG_PID|LOG_NOWAIT, LOG_CRON);
  if (debug)
    setlogmask(LOG_UPTO(LOG_DEBUG));
  else
    setlogmask(LOG_UPTO(LOG_INFO));

  /* set up the alarm handler */
  sigemptyset(&sa.sa_mask);
  sa.sa_handler = alarm_handler;
  sa.sa_flags = 0;
  sigaction(SIGALRM, &sa, &old_sa);
  /* exec the command */
  status = run_subprocess(command, command_args, &set_timeout_alarm);
  alarm(0); /* shutdown the alarm */
  if (alarm_triggered) {
    syslog(LOG_INFO, "command '%s' timed out after %d seconds", command,
           timeout);
    status = 128 + SIGALRM;
  }
  closelog();
  exit(status);
}