summaryrefslogtreecommitdiff
path: root/jobmon/netmon.c
blob: b571f1723ef62d31b02a3b709b0eb82ca7a6e7dc (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * netmon --
 *    Monitor a network interface.  If the IP address disappears, run "ifconfig <interface> inet autoconf"
 */

#include <arpa/inet.h>
#include <errno.h>
#include <getopt.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <syslog.h>
#include <unistd.h>

#define DEFAULT_INTERVAL 10
#define MAX_INTERVAL 3600

bool foreground;
bool use_syslog;
#define PID_DIR "/var/run"
#define PID_FILE "netmon.pid"
const char *pid_file = PID_DIR"/"PID_FILE;

void
do_log (int priority, const char *msg, va_list ap) {
   if (use_syslog) {
      vsyslog (priority, msg, ap);
   } else {
      FILE *out = (priority == LOG_NOTICE ||
                   priority == LOG_INFO ||
                   priority == LOG_DEBUG) ? stdout : stderr;
      vfprintf (out, msg, ap);
      fputc ('\n', out);
   }
}

void
debug (const char *msg, ...) {
   if (foreground) {
      va_list ap;
      va_start (ap, msg);
      do_log (LOG_DEBUG, msg, ap);
      va_end (ap);
   }
}

void
info (const char *msg, ...) {
   va_list ap;
   va_start (ap, msg);
   do_log (LOG_INFO, msg, ap);
   va_end (ap);
}

void
error (const char *msg, ...) {
   va_list ap;
   va_start (ap, msg);
   do_log (LOG_ERR, msg, ap);
   va_end (ap);
}

void
die (const char *msg, ...) {
   va_list ap;
   va_start (ap, msg);
   do_log (LOG_ERR, msg, ap);
   va_end (ap);
   unlink (pid_file);
   exit (EXIT_FAILURE);
}


void
sigexit (int sig) {
   unlink (pid_file);
   exit (EXIT_SUCCESS);
}


bool
is_up (int af, char *interface) {
   struct ifaddrs *ifap, *ifa = NULL;
   bool found = false;

   if (getifaddrs (&ifa) == -1) die ("getifaddrs: %s", strerror (errno));

   for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
      struct sockaddr *sa = ifap->ifa_addr;

      if (sa->sa_family == af && ! strcmp (interface, ifap->ifa_name)) {
         if (foreground) {
            if (sa->sa_family == AF_INET) {
               char addr[INET_ADDRSTRLEN] = {0};
               debug ("%s up: %s", ifap->ifa_name, inet_ntop (AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), addr, sizeof (addr)));
            } else if (sa->sa_family == AF_INET6) {
               char addr[INET6_ADDRSTRLEN] = {0};
               debug ("%s up: %s", ifap->ifa_name, inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), addr, sizeof (addr)));
            }
         }

         found = true;
         goto out;
      }
   }

  out:
   freeifaddrs (ifa);

   return found;
}

void
configure (int af, char *interface) {
   pid_t p, wp;
   int status;
   unsigned int iters = 0;
#define MAX_ITERS 30

   info ("reconfiguring %s", interface);

   if ((p = fork ()) == -1) die ("fork failure: %s", strerror (errno));

   if (p == 0) {
      execl ("/sbin/ifconfig", "ifconfig", interface, (af == AF_INET) ? "inet" : "inet6", "autoconf", NULL);
      die ("exec failure: %s", strerror (errno));
   }

   while (1) {
      wp = waitpid (p, &status, WNOHANG);

      if (wp == -1) {
         if (errno == ECHILD) { break; }
         die ("waitpid failure: %s", strerror (errno));
      } else if (wp == 0) {
         if (iters++ > MAX_ITERS) {
            error ("child overdue, killing and bailing");
            kill (p, SIGTERM);
            sleep (1);
            kill (p, SIGKILL);
            unlink (pid_file);
            exit (EXIT_FAILURE);
         }
      } else {
         if (WIFEXITED (status)) {
            debug ("child exited with exit code %d", WEXITSTATUS (status));
         } else if (WIFSIGNALED (status)) {
            debug ("child exited with signal %d", WTERMSIG (status));
         } else {
            debug ("child exited with status %d", status);
         }
      }

      sleep (1);
   }
}

void
help (int ec) {
   printf ("netmon [--interval|-i <seconds>] [-4|-6] <interface>\n"
           "\n"
           "    Monitor a network interface and reconfigure it if it goes down\n"
           "\n"
           "Options:\n"
           "    --debug|-d      run in the foreground and send output to the console\n"
           "    --interval|-i   how frequently to check in seconds (default %u)\n"
           "    --ipv4|-4       check for configured IPv4 address (default)\n"
           "    --ipv6|-6       check for a configured IPv6 address instead of IPv4\n",
           DEFAULT_INTERVAL);
   exit (ec);
}

int
main (int argc, char *argv[]) {
   struct option options[] = {
      { "interval", required_argument, NULL, 'i' },
      { "debug",          no_argument, NULL, 'd' },
      { "ipv4",           no_argument, NULL, '4' },
      { "ipv6",           no_argument, NULL, '6' },
      { "help",           no_argument, NULL, 'h' },
      { 0, 0, 0, 0 }
   };
   int opt;
   int af = AF_INET;
   unsigned long interval = DEFAULT_INTERVAL;
   char *interface = NULL;

   while ((opt = getopt_long (argc, argv, "i:46dh", options, NULL)) != -1) {
      switch (opt) {
      case 'i':
         errno = 0;
         interval = strtoul (optarg, NULL, 10);
         if (interval < 1 || interval > MAX_INTERVAL || errno) {
            die ("invalid interval '%s', must be in the range [1,%u]", optarg, MAX_INTERVAL);
         }
         break;
      case '4':
         af = AF_INET;
         break;
      case '6':
         af = AF_INET6;
         break;
      case 'd':
         foreground = true;
         break;
      case 'h':
         help (EXIT_SUCCESS);
         break;
      default:
         help (EXIT_FAILURE);
      }
   }

   if (optind >= argc) {
      help (EXIT_FAILURE);
   }

   interface = argv[optind];

   if (! foreground) {
      if (access (PID_DIR, W_OK) == -1) die ("unable to write pid file '%s'", pid_file);

      use_syslog = true;
      if (daemon (0, 0) == -1) die ("daemon failed: %s", strerror (errno));

      FILE *fp = fopen (pid_file, "w");
      if (fp == NULL) die ("error writing pid file '%s': %s", pid_file, strerror (errno));
      fprintf (fp, "%d\n", getpid ());
      fclose (fp);

      if (pledge ("cpath exec inet proc stdio unveil", NULL) == -1) die ("pledge: %s", strerror (errno));

      if (unveil (pid_file, "c") == -1) die ("unveil %s: %s", pid_file, strerror (errno));
      if (unveil ("/sbin", "rx") == -1) die ("unveil /sbin: %s", strerror (errno));
      if (unveil (NULL, NULL) == -1) die ("unveil: %s", strerror (errno));
   }

   signal (SIGINT, sigexit);
   signal (SIGTERM, sigexit);
   signal (SIGQUIT, sigexit);

   while (1) {
      if (! is_up (af, interface)) configure (af, interface);
      sleep ((unsigned int)interval);
   }

   return 0;
}