Handled interrupted socket reads
[collectd.git] / src / connectivity.c
1 /**
2  * collectd - src/connectivity.c
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  * Authors:
23  *   Red Hat NFVPE
24  *     Andrew Bays <abays at redhat.com>
25  *     Aneesh Puttur <aputtur at redhat.com>
26  **/
27
28 #include "collectd.h"
29
30 #include "plugin.h"
31 #include "utils/common/common.h"
32 #include "utils/ignorelist/ignorelist.h"
33 #include "utils_complain.h"
34
35 #include <asm/types.h>
36 #include <errno.h>
37 #include <net/if.h>
38 #include <netinet/in.h>
39 #include <pthread.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/socket.h>
43 #include <unistd.h>
44
45 #include <libmnl/libmnl.h>
46 #include <linux/netlink.h>
47 #include <linux/rtnetlink.h>
48
49 #include <yajl/yajl_common.h>
50 #include <yajl/yajl_gen.h>
51 #if HAVE_YAJL_YAJL_VERSION_H
52 #include <yajl/yajl_version.h>
53 #endif
54 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
55 #define HAVE_YAJL_V2 1
56 #endif
57
58 #define MYPROTO NETLINK_ROUTE
59
60 #define LINK_STATE_DOWN 0
61 #define LINK_STATE_UP 1
62 #define LINK_STATE_UNKNOWN 2
63
64 #define CONNECTIVITY_DOMAIN_FIELD "domain"
65 #define CONNECTIVITY_DOMAIN_VALUE "stateChange"
66 #define CONNECTIVITY_EVENT_ID_FIELD "eventId"
67 #define CONNECTIVITY_EVENT_NAME_FIELD "eventName"
68 #define CONNECTIVITY_EVENT_NAME_DOWN_VALUE "down"
69 #define CONNECTIVITY_EVENT_NAME_UP_VALUE "up"
70 #define CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD "lastEpochMicrosec"
71 #define CONNECTIVITY_PRIORITY_FIELD "priority"
72 #define CONNECTIVITY_PRIORITY_VALUE "high"
73 #define CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD "reportingEntityName"
74 #define CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE "collectd connectivity plugin"
75 #define CONNECTIVITY_SEQUENCE_FIELD "sequence"
76 #define CONNECTIVITY_SEQUENCE_VALUE "0"
77 #define CONNECTIVITY_SOURCE_NAME_FIELD "sourceName"
78 #define CONNECTIVITY_START_EPOCH_MICROSEC_FIELD "startEpochMicrosec"
79 #define CONNECTIVITY_VERSION_FIELD "version"
80 #define CONNECTIVITY_VERSION_VALUE "1.0"
81
82 #define CONNECTIVITY_NEW_STATE_FIELD "newState"
83 #define CONNECTIVITY_NEW_STATE_FIELD_DOWN_VALUE "outOfService"
84 #define CONNECTIVITY_NEW_STATE_FIELD_UP_VALUE "inService"
85 #define CONNECTIVITY_OLD_STATE_FIELD "oldState"
86 #define CONNECTIVITY_OLD_STATE_FIELD_DOWN_VALUE "outOfService"
87 #define CONNECTIVITY_OLD_STATE_FIELD_UP_VALUE "inService"
88 #define CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD "stateChangeFields"
89 #define CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_FIELD                         \
90   "stateChangeFieldsVersion"
91 #define CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE "1.0"
92 #define CONNECTIVITY_STATE_INTERFACE_FIELD "stateInterface"
93
94 /*
95  * Private data types
96  */
97
98 struct interface_list_s {
99   char *interface;
100
101   uint32_t status;
102   uint32_t prev_status;
103   uint32_t sent;
104   cdtime_t timestamp;
105
106   struct interface_list_s *next;
107 };
108 typedef struct interface_list_s interface_list_t;
109
110 /*
111  * Private variables
112  */
113
114 static ignorelist_t *ignorelist = NULL;
115
116 static interface_list_t *interface_list_head = NULL;
117 static int monitor_all_interfaces = 1;
118
119 static int connectivity_netlink_thread_loop = 0;
120 static int connectivity_netlink_thread_error = 0;
121 static pthread_t connectivity_netlink_thread_id;
122 static int connectivity_dequeue_thread_loop = 0;
123 static pthread_t connectivity_dequeue_thread_id;
124 static pthread_mutex_t connectivity_threads_lock = PTHREAD_MUTEX_INITIALIZER;
125 static pthread_mutex_t connectivity_data_lock = PTHREAD_MUTEX_INITIALIZER;
126 static pthread_cond_t connectivity_cond = PTHREAD_COND_INITIALIZER;
127 static int nl_sock = -1;
128 static int event_id = 0;
129 static int statuses_to_send = 0;
130
131 static const char *config_keys[] = {"Interface", "IgnoreSelected"};
132 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
133
134 /*
135  * Private functions
136  */
137
138 static int gen_message_payload(int state, int old_state, const char *interface,
139                                cdtime_t timestamp, char **buf) {
140   const unsigned char *buf2;
141   yajl_gen g;
142   char json_str[DATA_MAX_NAME_LEN];
143
144 #if !defined(HAVE_YAJL_V2)
145   yajl_gen_config conf = {0};
146 #endif
147
148 #if HAVE_YAJL_V2
149   size_t len;
150   g = yajl_gen_alloc(NULL);
151   yajl_gen_config(g, yajl_gen_beautify, 0);
152 #else
153   unsigned int len;
154   g = yajl_gen_alloc(&conf, NULL);
155 #endif
156
157   yajl_gen_clear(g);
158
159   // *** BEGIN common event header ***
160
161   if (yajl_gen_map_open(g) != yajl_gen_status_ok)
162     goto err;
163
164   // domain
165   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_DOMAIN_FIELD,
166                       strlen(CONNECTIVITY_DOMAIN_FIELD)) != yajl_gen_status_ok)
167     goto err;
168
169   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_DOMAIN_VALUE,
170                       strlen(CONNECTIVITY_DOMAIN_VALUE)) != yajl_gen_status_ok)
171     goto err;
172
173   // eventId
174   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_EVENT_ID_FIELD,
175                       strlen(CONNECTIVITY_EVENT_ID_FIELD)) !=
176       yajl_gen_status_ok)
177     goto err;
178
179   event_id = event_id + 1;
180   if (snprintf(json_str, sizeof(json_str), "%d", event_id) < 0) {
181     goto err;
182   }
183
184   if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
185     goto err;
186   }
187
188   // eventName
189   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_EVENT_NAME_FIELD,
190                       strlen(CONNECTIVITY_EVENT_NAME_FIELD)) !=
191       yajl_gen_status_ok)
192     goto err;
193
194   if (snprintf(json_str, sizeof(json_str), "interface %s %s", interface,
195                (state == 0 ? CONNECTIVITY_EVENT_NAME_DOWN_VALUE
196                            : CONNECTIVITY_EVENT_NAME_UP_VALUE)) < 0) {
197     goto err;
198   }
199
200   if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
201       yajl_gen_status_ok) {
202     goto err;
203   }
204
205   // lastEpochMicrosec
206   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD,
207                       strlen(CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD)) !=
208       yajl_gen_status_ok)
209     goto err;
210
211   if (snprintf(json_str, sizeof(json_str), "%" PRIu64,
212                CDTIME_T_TO_US(cdtime())) < 0) {
213     goto err;
214   }
215
216   if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
217     goto err;
218   }
219
220   // priority
221   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_PRIORITY_FIELD,
222                       strlen(CONNECTIVITY_PRIORITY_FIELD)) !=
223       yajl_gen_status_ok)
224     goto err;
225
226   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_PRIORITY_VALUE,
227                       strlen(CONNECTIVITY_PRIORITY_VALUE)) !=
228       yajl_gen_status_ok)
229     goto err;
230
231   // reportingEntityName
232   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD,
233                       strlen(CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD)) !=
234       yajl_gen_status_ok)
235     goto err;
236
237   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE,
238                       strlen(CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE)) !=
239       yajl_gen_status_ok)
240     goto err;
241
242   // sequence
243   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_SEQUENCE_FIELD,
244                       strlen(CONNECTIVITY_SEQUENCE_FIELD)) !=
245       yajl_gen_status_ok)
246     goto err;
247
248   if (yajl_gen_number(g, CONNECTIVITY_SEQUENCE_VALUE,
249                       strlen(CONNECTIVITY_SEQUENCE_VALUE)) !=
250       yajl_gen_status_ok)
251     goto err;
252
253   // sourceName
254   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_SOURCE_NAME_FIELD,
255                       strlen(CONNECTIVITY_SOURCE_NAME_FIELD)) !=
256       yajl_gen_status_ok)
257     goto err;
258
259   if (yajl_gen_string(g, (u_char *)interface, strlen(interface)) !=
260       yajl_gen_status_ok)
261     goto err;
262
263   // startEpochMicrosec
264   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_START_EPOCH_MICROSEC_FIELD,
265                       strlen(CONNECTIVITY_START_EPOCH_MICROSEC_FIELD)) !=
266       yajl_gen_status_ok)
267     goto err;
268
269   if (snprintf(json_str, sizeof(json_str), "%" PRIu64,
270                CDTIME_T_TO_US(timestamp)) < 0) {
271     goto err;
272   }
273
274   if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
275     goto err;
276   }
277
278   // version
279   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_VERSION_FIELD,
280                       strlen(CONNECTIVITY_VERSION_FIELD)) != yajl_gen_status_ok)
281     goto err;
282
283   if (yajl_gen_number(g, CONNECTIVITY_VERSION_VALUE,
284                       strlen(CONNECTIVITY_VERSION_VALUE)) != yajl_gen_status_ok)
285     goto err;
286
287   // *** END common event header ***
288
289   // *** BEGIN state change fields ***
290
291   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD,
292                       strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD)) !=
293       yajl_gen_status_ok)
294     goto err;
295
296   if (yajl_gen_map_open(g) != yajl_gen_status_ok)
297     goto err;
298
299   // newState
300   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_NEW_STATE_FIELD,
301                       strlen(CONNECTIVITY_NEW_STATE_FIELD)) !=
302       yajl_gen_status_ok)
303     goto err;
304
305   int new_state_len =
306       (state == 0 ? strlen(CONNECTIVITY_NEW_STATE_FIELD_DOWN_VALUE)
307                   : strlen(CONNECTIVITY_NEW_STATE_FIELD_UP_VALUE));
308
309   if (yajl_gen_string(g,
310                       (u_char *)(state == 0
311                                      ? CONNECTIVITY_NEW_STATE_FIELD_DOWN_VALUE
312                                      : CONNECTIVITY_NEW_STATE_FIELD_UP_VALUE),
313                       new_state_len) != yajl_gen_status_ok)
314     goto err;
315
316   // oldState
317   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_OLD_STATE_FIELD,
318                       strlen(CONNECTIVITY_OLD_STATE_FIELD)) !=
319       yajl_gen_status_ok)
320     goto err;
321
322   int old_state_len =
323       (old_state == 0 ? strlen(CONNECTIVITY_OLD_STATE_FIELD_DOWN_VALUE)
324                       : strlen(CONNECTIVITY_OLD_STATE_FIELD_UP_VALUE));
325
326   if (yajl_gen_string(g,
327                       (u_char *)(old_state == 0
328                                      ? CONNECTIVITY_OLD_STATE_FIELD_DOWN_VALUE
329                                      : CONNECTIVITY_OLD_STATE_FIELD_UP_VALUE),
330                       old_state_len) != yajl_gen_status_ok)
331     goto err;
332
333   // stateChangeFieldsVersion
334   if (yajl_gen_string(g,
335                       (u_char *)CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_FIELD,
336                       strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_FIELD)) !=
337       yajl_gen_status_ok)
338     goto err;
339
340   if (yajl_gen_number(g, CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE,
341                       strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE)) !=
342       yajl_gen_status_ok)
343     goto err;
344
345   // stateInterface
346   if (yajl_gen_string(g, (u_char *)CONNECTIVITY_STATE_INTERFACE_FIELD,
347                       strlen(CONNECTIVITY_STATE_INTERFACE_FIELD)) !=
348       yajl_gen_status_ok)
349     goto err;
350
351   if (yajl_gen_string(g, (u_char *)interface, strlen(interface)) !=
352       yajl_gen_status_ok)
353     goto err;
354
355   // close state change and header fields
356   if (yajl_gen_map_close(g) != yajl_gen_status_ok ||
357       yajl_gen_map_close(g) != yajl_gen_status_ok)
358     goto err;
359
360   // *** END state change fields ***
361
362   if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok)
363     goto err;
364
365   *buf = strdup((char *)buf2);
366
367   if (*buf == NULL) {
368     ERROR("connectivity plugin: strdup failed during gen_message_payload: %s",
369           STRERRNO);
370     goto err;
371   }
372
373   yajl_gen_free(g);
374
375   return 0;
376
377 err:
378   yajl_gen_free(g);
379   ERROR("connectivity plugin: gen_message_payload failed to generate JSON");
380   return -1;
381 }
382
383 static interface_list_t *add_interface(const char *interface, int status,
384                                        int prev_status) {
385   interface_list_t *il = calloc(1, sizeof(*il));
386
387   if (il == NULL) {
388     ERROR("connectivity plugin: calloc failed during add_interface: %s",
389           STRERRNO);
390     return NULL;
391   }
392
393   char *interface2 = strdup(interface);
394   if (interface2 == NULL) {
395     sfree(il);
396     ERROR("connectivity plugin: strdup failed during add_interface: %s",
397           STRERRNO);
398     return NULL;
399   }
400
401   il->interface = interface2;
402   il->status = status;
403   il->prev_status = prev_status;
404   il->timestamp = cdtime();
405   il->sent = 0;
406   il->next = interface_list_head;
407   interface_list_head = il;
408
409   DEBUG("connectivity plugin: added interface %s", interface2);
410
411   return il;
412 }
413
414 static int connectivity_link_state(struct nlmsghdr *msg) {
415   pthread_mutex_lock(&connectivity_data_lock);
416
417   struct nlattr *attr;
418   struct ifinfomsg *ifi = mnl_nlmsg_get_payload(msg);
419
420   /* Scan attribute list for device name. */
421   mnl_attr_for_each(attr, msg, sizeof(*ifi)) {
422     if (mnl_attr_get_type(attr) != IFLA_IFNAME)
423       continue;
424
425     if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
426       ERROR("connectivity plugin: connectivity_link_state: IFLA_IFNAME "
427             "mnl_attr_validate "
428             "failed.");
429       pthread_mutex_unlock(&connectivity_data_lock);
430       return MNL_CB_ERROR;
431     }
432
433     const char *dev = mnl_attr_get_str(attr);
434
435     // Check the list of interfaces we should monitor, if we've chosen
436     // a subset.  If we don't care about this one, abort.
437     if (ignorelist_match(ignorelist, dev) != 0) {
438       DEBUG("connectivity plugin: Ignoring link state change for unmonitored "
439             "interface: %s",
440             dev);
441       break;
442     }
443
444     interface_list_t *il = NULL;
445
446     for (il = interface_list_head; il != NULL; il = il->next)
447       if (strcmp(dev, il->interface) == 0)
448         break;
449
450     if (il == NULL) {
451       // We haven't encountered this interface yet, so add it to the linked list
452       il = add_interface(dev, LINK_STATE_UNKNOWN, LINK_STATE_UNKNOWN);
453
454       if (il == NULL) {
455         ERROR("connectivity plugin: unable to add interface %s during "
456               "connectivity_link_state",
457               dev);
458         return MNL_CB_ERROR;
459       }
460     }
461
462     uint32_t prev_status = il->status;
463     il->status =
464         ((ifi->ifi_flags & IFF_RUNNING) ? LINK_STATE_UP : LINK_STATE_DOWN);
465     il->timestamp = cdtime();
466
467     // If the new status is different than the previous status,
468     // store the previous status and set sent to zero, and set the
469     // global flag to indicate there are statuses to dispatch
470     if (il->status != prev_status) {
471       il->prev_status = prev_status;
472       il->sent = 0;
473       statuses_to_send = 1;
474     }
475
476     DEBUG("connectivity plugin (%llu): Interface %s status is now %s",
477           (unsigned long long)il->timestamp, dev,
478           ((ifi->ifi_flags & IFF_RUNNING) ? "UP" : "DOWN"));
479
480     // no need to loop again, we found the interface name attr
481     // (otherwise the first if-statement in the loop would
482     // have moved us on with 'continue')
483     break;
484   }
485
486   pthread_mutex_unlock(&connectivity_data_lock);
487
488   return 0;
489 }
490
491 static int msg_handler(struct nlmsghdr *msg) {
492   // We are only interested in RTM_NEWLINK messages
493   if (msg->nlmsg_type != RTM_NEWLINK) {
494     return 0;
495   }
496   return connectivity_link_state(msg);
497 }
498
499 static int read_event(int (*msg_handler)(struct nlmsghdr *)) {
500   int ret = 0;
501   int recv_flags = MSG_DONTWAIT;
502
503   if (nl_sock == -1 || msg_handler == NULL)
504     return EINVAL;
505
506   while (42) {
507     pthread_mutex_lock(&connectivity_threads_lock);
508
509     if (connectivity_netlink_thread_loop <= 0) {
510       pthread_mutex_unlock(&connectivity_threads_lock);
511       return ret;
512     }
513
514     pthread_mutex_unlock(&connectivity_threads_lock);
515
516     char buf[4096];
517     int status = recv(nl_sock, buf, sizeof(buf), recv_flags);
518
519     if (status < 0) {
520
521       // If there were no more messages to drain from the socket,
522       // then signal the dequeue thread and allow it to dispatch
523       // any saved interface status changes.  Then continue, but
524       // block and wait for new messages
525       if (errno == EWOULDBLOCK || errno == EAGAIN) {
526         pthread_cond_signal(&connectivity_cond);
527
528         recv_flags = 0;
529         continue;
530       }
531
532       if (errno == EINTR) {
533         // Interrupt, so just continue and try again
534         continue;
535       }
536
537       if (errno == EINTR)
538       {
539         // Interrupt, so just return
540         return 0;
541       }
542
543       /* Anything else is an error */
544       ERROR("connectivity plugin: read_event: Error recv: %d", status);
545       return status;
546     }
547
548     // Message received successfully, so we'll stop blocking on the
549     // receive call for now (until we get a "would block" error, which
550     // will be handled above)
551     recv_flags = MSG_DONTWAIT;
552
553     if (status == 0) {
554       DEBUG("connectivity plugin: read_event: EOF");
555     }
556
557     /* We need to handle more than one message per 'recvmsg' */
558     for (struct nlmsghdr *h = (struct nlmsghdr *)buf;
559          NLMSG_OK(h, (unsigned int)status); h = NLMSG_NEXT(h, status)) {
560       /* Finish reading */
561       if (h->nlmsg_type == NLMSG_DONE)
562         return ret;
563
564       /* Message is some kind of error */
565       if (h->nlmsg_type == NLMSG_ERROR) {
566         struct nlmsgerr *l_err = (struct nlmsgerr *)NLMSG_DATA(h);
567         ERROR("connectivity plugin: read_event: Message is an error: %d",
568               l_err->error);
569         return -1; // Error
570       }
571
572       /* Call message handler */
573       if (msg_handler) {
574         ret = (*msg_handler)(h);
575         if (ret < 0) {
576           ERROR("connectivity plugin: read_event: Message handler error %d",
577                 ret);
578           return ret;
579         }
580       } else {
581         ERROR("connectivity plugin: read_event: Error NULL message handler");
582         return -1;
583       }
584     }
585   }
586
587   return ret;
588 }
589
590 static void connectivity_dispatch_notification(const char *interface,
591                                                gauge_t value, gauge_t old_value,
592                                                cdtime_t timestamp) {
593
594   notification_t n = {
595       .severity = (value == LINK_STATE_UP ? NOTIF_OKAY : NOTIF_FAILURE),
596       .time = cdtime(),
597       .plugin = "connectivity",
598       .type = "gauge",
599       .type_instance = "interface_status",
600   };
601
602   sstrncpy(n.host, hostname_g, sizeof(n.host));
603   sstrncpy(n.plugin_instance, interface, sizeof(n.plugin_instance));
604
605   char *buf = NULL;
606
607   gen_message_payload(value, old_value, interface, timestamp, &buf);
608
609   int status = plugin_notification_meta_add_string(&n, "ves", buf);
610
611   if (status < 0) {
612     sfree(buf);
613     ERROR("connectivity plugin: unable to set notification VES metadata: %s",
614           STRERRNO);
615     return;
616   }
617
618   DEBUG("connectivity plugin: notification VES metadata: %s",
619         n.meta->nm_value.nm_string);
620
621   DEBUG("connectivity plugin: dispatching state %d for interface %s",
622         (int)value, interface);
623
624   plugin_dispatch_notification(&n);
625   plugin_notification_meta_free(n.meta);
626
627   // strdup'd in gen_message_payload
628   if (buf != NULL)
629     sfree(buf);
630 }
631
632 // NOTE: Caller MUST hold connectivity_data_lock when calling this function
633 static void send_interface_status() {
634   for (interface_list_t *il = interface_list_head; il != NULL;
635        il = il->next) /* {{{ */
636   {
637     uint32_t status = il->status;
638     uint32_t prev_status = il->prev_status;
639     uint32_t sent = il->sent;
640
641     if (status != prev_status && sent == 0) {
642       connectivity_dispatch_notification(il->interface, status, prev_status,
643                                          il->timestamp);
644       il->sent = 1;
645     }
646   } /* }}} for (il = interface_list_head; il != NULL; il = il->next) */
647
648   statuses_to_send = 0;
649 }
650
651 static void read_interface_status() /* {{{ */
652 {
653   pthread_mutex_lock(&connectivity_data_lock);
654
655   // If we don't have any interface statuses to dispatch,
656   // then we wait until signalled
657   if (!statuses_to_send)
658     pthread_cond_wait(&connectivity_cond, &connectivity_data_lock);
659
660   send_interface_status();
661
662   pthread_mutex_unlock(&connectivity_data_lock);
663 } /* }}} int *read_interface_status */
664
665 static void *connectivity_netlink_thread(void *arg) /* {{{ */
666 {
667   pthread_mutex_lock(&connectivity_threads_lock);
668
669   while (connectivity_netlink_thread_loop > 0) {
670     pthread_mutex_unlock(&connectivity_threads_lock);
671
672     int status = read_event(msg_handler);
673
674     pthread_mutex_lock(&connectivity_threads_lock);
675
676     if (status < 0) {
677       connectivity_netlink_thread_error = 1;
678       break;
679     }
680   } /* while (connectivity_netlink_thread_loop > 0) */
681
682   pthread_mutex_unlock(&connectivity_threads_lock);
683
684   return (void *)0;
685 } /* }}} void *connectivity_netlink_thread */
686
687 static void *connectivity_dequeue_thread(void *arg) /* {{{ */
688 {
689   pthread_mutex_lock(&connectivity_threads_lock);
690
691   while (connectivity_dequeue_thread_loop > 0) {
692     pthread_mutex_unlock(&connectivity_threads_lock);
693
694     read_interface_status();
695
696     pthread_mutex_lock(&connectivity_threads_lock);
697   } /* while (connectivity_dequeue_thread_loop > 0) */
698
699   pthread_mutex_unlock(&connectivity_threads_lock);
700
701   return ((void *)0);
702 } /* }}} void *connectivity_dequeue_thread */
703
704 static int nl_connect() {
705   struct sockaddr_nl sa_nl = {
706       .nl_family = AF_NETLINK,
707       .nl_groups = RTMGRP_LINK,
708       .nl_pid = getpid(),
709   };
710
711   nl_sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
712   if (nl_sock == -1) {
713     ERROR("connectivity plugin: socket open failed: %s", STRERRNO);
714     return -1;
715   }
716
717   int rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
718   if (rc == -1) {
719     ERROR("connectivity plugin: socket bind failed: %s", STRERRNO);
720     close(nl_sock);
721     nl_sock = -1;
722     return -1;
723   }
724
725   return 0;
726 }
727
728 static int start_netlink_thread(void) /* {{{ */
729 {
730   pthread_mutex_lock(&connectivity_threads_lock);
731
732   if (connectivity_netlink_thread_loop != 0) {
733     pthread_mutex_unlock(&connectivity_threads_lock);
734     return 0;
735   }
736
737   connectivity_netlink_thread_loop = 1;
738   connectivity_netlink_thread_error = 0;
739
740   int status;
741
742   if (nl_sock == -1) {
743     status = nl_connect();
744
745     if (status != 0) {
746       pthread_mutex_unlock(&connectivity_threads_lock);
747       return status;
748     }
749   }
750
751   status = plugin_thread_create(&connectivity_netlink_thread_id,
752                                 /* attr = */ NULL, connectivity_netlink_thread,
753                                 /* arg = */ (void *)0, "connectivity");
754   if (status != 0) {
755     connectivity_netlink_thread_loop = 0;
756     ERROR("connectivity plugin: Starting thread failed.");
757     pthread_mutex_unlock(&connectivity_threads_lock);
758
759     int status2 = close(nl_sock);
760
761     if (status2 != 0) {
762       ERROR("connectivity plugin: failed to close socket %d: %d (%s)", nl_sock,
763             status2, STRERRNO);
764     }
765
766     nl_sock = -1;
767
768     return -1;
769   }
770
771   pthread_mutex_unlock(&connectivity_threads_lock);
772
773   return status;
774 }
775
776 static int start_dequeue_thread(void) /* {{{ */
777 {
778   pthread_mutex_lock(&connectivity_threads_lock);
779
780   if (connectivity_dequeue_thread_loop != 0) {
781     pthread_mutex_unlock(&connectivity_threads_lock);
782     return 0;
783   }
784
785   connectivity_dequeue_thread_loop = 1;
786
787   int status =
788       plugin_thread_create(&connectivity_dequeue_thread_id,
789                            /* attr = */ NULL, connectivity_dequeue_thread,
790                            /* arg = */ (void *)0, "connectivity");
791   if (status != 0) {
792     connectivity_dequeue_thread_loop = 0;
793     ERROR("connectivity plugin: Starting dequeue thread failed.");
794     pthread_mutex_unlock(&connectivity_threads_lock);
795     return -1;
796   }
797
798   pthread_mutex_unlock(&connectivity_threads_lock);
799
800   return status;
801 } /* }}} int start_dequeue_thread */
802
803 static int start_threads(void) /* {{{ */
804 {
805   int status = start_netlink_thread();
806   int status2 = start_dequeue_thread();
807
808   if (status != 0)
809     return status;
810   else
811     return status2;
812 } /* }}} int start_threads */
813
814 static int stop_netlink_thread(int shutdown) /* {{{ */
815 {
816   int socket_status;
817
818   if (nl_sock != -1) {
819     socket_status = close(nl_sock);
820     if (socket_status != 0) {
821       ERROR("connectivity plugin: failed to close socket %d: %d (%s)", nl_sock,
822             socket_status, STRERRNO);
823     }
824
825     nl_sock = -1;
826   } else
827     socket_status = 0;
828
829   pthread_mutex_lock(&connectivity_threads_lock);
830
831   if (connectivity_netlink_thread_loop == 0) {
832     pthread_mutex_unlock(&connectivity_threads_lock);
833     // Thread has already been terminated, nothing more to attempt
834     return socket_status;
835   }
836
837   // Set thread termination status
838   connectivity_netlink_thread_loop = 0;
839   pthread_mutex_unlock(&connectivity_threads_lock);
840
841   // Let threads waiting on access to the interface list know to move
842   // on such that they'll see the thread's termination status
843   pthread_cond_broadcast(&connectivity_cond);
844
845   int thread_status;
846
847   if (shutdown == 1) {
848     // Since the thread is blocking, calling pthread_join
849     // doesn't actually succeed in stopping it.  It will stick around
850     // until a NETLINK message is received on the socket (at which
851     // it will realize that "connectivity_netlink_thread_loop" is 0 and will
852     // break out of the read loop and be allowed to die).  This is
853     // fine when the process isn't supposed to be exiting, but in
854     // the case of a process shutdown, we don't want to have an
855     // idle thread hanging around.  Calling pthread_cancel here in
856     // the case of a shutdown is just assures that the thread is
857     // gone and that the process has been fully terminated.
858
859     DEBUG("connectivity plugin: Canceling netlink thread for process shutdown");
860
861     thread_status = pthread_cancel(connectivity_netlink_thread_id);
862
863     if (thread_status != 0 && thread_status != ESRCH) {
864       ERROR("connectivity plugin: Unable to cancel netlink thread: %d",
865             thread_status);
866       thread_status = -1;
867     } else
868       thread_status = 0;
869   } else {
870     thread_status =
871         pthread_join(connectivity_netlink_thread_id, /* return = */ NULL);
872     if (thread_status != 0 && thread_status != ESRCH) {
873       ERROR("connectivity plugin: Stopping netlink thread failed: %d",
874             thread_status);
875       thread_status = -1;
876     } else
877       thread_status = 0;
878   }
879
880   pthread_mutex_lock(&connectivity_threads_lock);
881   memset(&connectivity_netlink_thread_id, 0,
882          sizeof(connectivity_netlink_thread_id));
883   connectivity_netlink_thread_error = 0;
884   pthread_mutex_unlock(&connectivity_threads_lock);
885
886   DEBUG("connectivity plugin: Finished requesting stop of netlink thread");
887
888   if (socket_status != 0)
889     return socket_status;
890   else
891     return thread_status;
892 }
893
894 static int stop_dequeue_thread() /* {{{ */
895 {
896   pthread_mutex_lock(&connectivity_threads_lock);
897
898   if (connectivity_dequeue_thread_loop == 0) {
899     pthread_mutex_unlock(&connectivity_threads_lock);
900     return -1;
901   }
902
903   // Set thread termination status
904   connectivity_dequeue_thread_loop = 0;
905   pthread_mutex_unlock(&connectivity_threads_lock);
906
907   // Let threads waiting on access to the interface list know to move
908   // on such that they'll see the threads termination status
909   pthread_cond_broadcast(&connectivity_cond);
910
911   // Calling pthread_cancel here just assures that the thread is
912   // gone and that the process has been fully terminated.
913
914   DEBUG("connectivity plugin: Canceling dequeue thread for process shutdown");
915
916   int status = pthread_cancel(connectivity_dequeue_thread_id);
917
918   if (status != 0 && status != ESRCH) {
919     ERROR("connectivity plugin: Unable to cancel dequeue thread: %d", status);
920     status = -1;
921   } else
922     status = 0;
923
924   pthread_mutex_lock(&connectivity_threads_lock);
925   memset(&connectivity_dequeue_thread_id, 0,
926          sizeof(connectivity_dequeue_thread_id));
927   pthread_mutex_unlock(&connectivity_threads_lock);
928
929   DEBUG("connectivity plugin: Finished requesting stop of dequeue thread");
930
931   return status;
932 } /* }}} int stop_dequeue_thread */
933
934 static int stop_threads() /* {{{ */
935 {
936   int status = stop_netlink_thread(1);
937   int status2 = stop_dequeue_thread();
938
939   if (status != 0)
940     return status;
941   else
942     return status2;
943 } /* }}} int stop_threads */
944
945 static int connectivity_init(void) /* {{{ */
946 {
947   if (monitor_all_interfaces) {
948     NOTICE("connectivity plugin: No interfaces have been selected, so all will "
949            "be monitored");
950   }
951
952   return start_threads();
953 } /* }}} int connectivity_init */
954
955 static int connectivity_config(const char *key, const char *value) /* {{{ */
956 {
957   if (ignorelist == NULL) {
958     ignorelist = ignorelist_create(/* invert = */ 1);
959
960     if (ignorelist == NULL)
961       return -1;
962   }
963
964   if (strcasecmp(key, "Interface") == 0) {
965     ignorelist_add(ignorelist, value);
966     monitor_all_interfaces = 0;
967   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
968     int invert = 1;
969     if (IS_TRUE(value))
970       invert = 0;
971     ignorelist_set_invert(ignorelist, invert);
972   } else {
973     return -1;
974   }
975
976   return 0;
977 } /* }}} int connectivity_config */
978
979 static int connectivity_read(void) /* {{{ */
980 {
981   pthread_mutex_lock(&connectivity_threads_lock);
982
983   if (connectivity_netlink_thread_error != 0) {
984
985     pthread_mutex_unlock(&connectivity_threads_lock);
986
987     ERROR("connectivity plugin: The netlink thread had a problem. Restarting "
988           "it.");
989
990     stop_netlink_thread(0);
991
992     for (interface_list_t *il = interface_list_head; il != NULL;
993          il = il->next) {
994       il->status = LINK_STATE_UNKNOWN;
995       il->prev_status = LINK_STATE_UNKNOWN;
996       il->sent = 0;
997     }
998
999     start_netlink_thread();
1000
1001     return -1;
1002   } /* if (connectivity_netlink_thread_error != 0) */
1003
1004   pthread_mutex_unlock(&connectivity_threads_lock);
1005
1006   return 0;
1007 } /* }}} int connectivity_read */
1008
1009 static int connectivity_shutdown(void) /* {{{ */
1010 {
1011   DEBUG("connectivity plugin: Shutting down thread.");
1012
1013   int status = stop_threads();
1014
1015   interface_list_t *il = interface_list_head;
1016   while (il != NULL) {
1017     interface_list_t *il_next;
1018
1019     il_next = il->next;
1020
1021     sfree(il->interface);
1022     sfree(il);
1023
1024     il = il_next;
1025   }
1026
1027   ignorelist_free(ignorelist);
1028
1029   return status;
1030 } /* }}} int connectivity_shutdown */
1031
1032 void module_register(void) {
1033   plugin_register_config("connectivity", connectivity_config, config_keys,
1034                          config_keys_num);
1035   plugin_register_init("connectivity", connectivity_init);
1036   plugin_register_read("connectivity", connectivity_read);
1037   plugin_register_shutdown("connectivity", connectivity_shutdown);
1038 } /* void module_register */