2 * collectd - src/connectivity.c
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 * Andrew Bays <abays at redhat.com>
25 * Aneesh Puttur <aputtur at redhat.com>
31 #include "utils/common/common.h"
32 #include "utils/ignorelist/ignorelist.h"
33 #include "utils_complain.h"
35 #include <asm/types.h>
38 #include <netinet/in.h>
42 #include <sys/socket.h>
45 #include <libmnl/libmnl.h>
46 #include <linux/netlink.h>
47 #include <linux/rtnetlink.h>
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>
54 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
55 #define HAVE_YAJL_V2 1
58 #define MYPROTO NETLINK_ROUTE
60 #define LINK_STATE_DOWN 0
61 #define LINK_STATE_UP 1
62 #define LINK_STATE_UNKNOWN 2
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"
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"
98 struct interface_list_s {
102 uint32_t prev_status;
106 struct interface_list_s *next;
108 typedef struct interface_list_s interface_list_t;
114 static ignorelist_t *ignorelist = NULL;
116 static interface_list_t *interface_list_head = NULL;
117 static int monitor_all_interfaces = 1;
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;
131 static const char *config_keys[] = {"Interface", "IgnoreSelected"};
132 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
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;
142 char json_str[DATA_MAX_NAME_LEN];
144 #if !defined(HAVE_YAJL_V2)
145 yajl_gen_config conf = {0};
150 g = yajl_gen_alloc(NULL);
151 yajl_gen_config(g, yajl_gen_beautify, 0);
154 g = yajl_gen_alloc(&conf, NULL);
159 // *** BEGIN common event header ***
161 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
165 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_DOMAIN_FIELD,
166 strlen(CONNECTIVITY_DOMAIN_FIELD)) != yajl_gen_status_ok)
169 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_DOMAIN_VALUE,
170 strlen(CONNECTIVITY_DOMAIN_VALUE)) != yajl_gen_status_ok)
174 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_EVENT_ID_FIELD,
175 strlen(CONNECTIVITY_EVENT_ID_FIELD)) !=
179 event_id = event_id + 1;
180 if (snprintf(json_str, sizeof(json_str), "%d", event_id) < 0) {
184 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
189 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_EVENT_NAME_FIELD,
190 strlen(CONNECTIVITY_EVENT_NAME_FIELD)) !=
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) {
200 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
201 yajl_gen_status_ok) {
206 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD,
207 strlen(CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD)) !=
211 if (snprintf(json_str, sizeof(json_str), "%" PRIu64,
212 CDTIME_T_TO_US(cdtime())) < 0) {
216 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
221 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_PRIORITY_FIELD,
222 strlen(CONNECTIVITY_PRIORITY_FIELD)) !=
226 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_PRIORITY_VALUE,
227 strlen(CONNECTIVITY_PRIORITY_VALUE)) !=
231 // reportingEntityName
232 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD,
233 strlen(CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD)) !=
237 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE,
238 strlen(CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE)) !=
243 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_SEQUENCE_FIELD,
244 strlen(CONNECTIVITY_SEQUENCE_FIELD)) !=
248 if (yajl_gen_number(g, CONNECTIVITY_SEQUENCE_VALUE,
249 strlen(CONNECTIVITY_SEQUENCE_VALUE)) !=
254 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_SOURCE_NAME_FIELD,
255 strlen(CONNECTIVITY_SOURCE_NAME_FIELD)) !=
259 if (yajl_gen_string(g, (u_char *)interface, strlen(interface)) !=
263 // startEpochMicrosec
264 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_START_EPOCH_MICROSEC_FIELD,
265 strlen(CONNECTIVITY_START_EPOCH_MICROSEC_FIELD)) !=
269 if (snprintf(json_str, sizeof(json_str), "%" PRIu64,
270 CDTIME_T_TO_US(timestamp)) < 0) {
274 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
279 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_VERSION_FIELD,
280 strlen(CONNECTIVITY_VERSION_FIELD)) != yajl_gen_status_ok)
283 if (yajl_gen_number(g, CONNECTIVITY_VERSION_VALUE,
284 strlen(CONNECTIVITY_VERSION_VALUE)) != yajl_gen_status_ok)
287 // *** END common event header ***
289 // *** BEGIN state change fields ***
291 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD,
292 strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD)) !=
296 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
300 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_NEW_STATE_FIELD,
301 strlen(CONNECTIVITY_NEW_STATE_FIELD)) !=
306 (state == 0 ? strlen(CONNECTIVITY_NEW_STATE_FIELD_DOWN_VALUE)
307 : strlen(CONNECTIVITY_NEW_STATE_FIELD_UP_VALUE));
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)
317 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_OLD_STATE_FIELD,
318 strlen(CONNECTIVITY_OLD_STATE_FIELD)) !=
323 (old_state == 0 ? strlen(CONNECTIVITY_OLD_STATE_FIELD_DOWN_VALUE)
324 : strlen(CONNECTIVITY_OLD_STATE_FIELD_UP_VALUE));
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)
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)) !=
340 if (yajl_gen_number(g, CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE,
341 strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE)) !=
346 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_STATE_INTERFACE_FIELD,
347 strlen(CONNECTIVITY_STATE_INTERFACE_FIELD)) !=
351 if (yajl_gen_string(g, (u_char *)interface, strlen(interface)) !=
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)
360 // *** END state change fields ***
362 if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok)
365 *buf = strdup((char *)buf2);
368 ERROR("connectivity plugin: strdup failed during gen_message_payload: %s",
379 ERROR("connectivity plugin: gen_message_payload failed to generate JSON");
383 static interface_list_t *add_interface(const char *interface, int status,
385 interface_list_t *il = calloc(1, sizeof(*il));
388 ERROR("connectivity plugin: calloc failed during add_interface: %s",
393 char *interface2 = strdup(interface);
394 if (interface2 == NULL) {
396 ERROR("connectivity plugin: strdup failed during add_interface: %s",
401 il->interface = interface2;
403 il->prev_status = prev_status;
404 il->timestamp = cdtime();
406 il->next = interface_list_head;
407 interface_list_head = il;
409 DEBUG("connectivity plugin: added interface %s", interface2);
414 static int connectivity_link_state(struct nlmsghdr *msg) {
415 pthread_mutex_lock(&connectivity_data_lock);
418 struct ifinfomsg *ifi = mnl_nlmsg_get_payload(msg);
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)
425 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
426 ERROR("connectivity plugin: connectivity_link_state: IFLA_IFNAME "
429 pthread_mutex_unlock(&connectivity_data_lock);
433 const char *dev = mnl_attr_get_str(attr);
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 "
444 interface_list_t *il = NULL;
446 for (il = interface_list_head; il != NULL; il = il->next)
447 if (strcmp(dev, il->interface) == 0)
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);
455 ERROR("connectivity plugin: unable to add interface %s during "
456 "connectivity_link_state",
462 uint32_t prev_status = il->status;
464 ((ifi->ifi_flags & IFF_RUNNING) ? LINK_STATE_UP : LINK_STATE_DOWN);
465 il->timestamp = cdtime();
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;
473 statuses_to_send = 1;
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"));
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')
486 pthread_mutex_unlock(&connectivity_data_lock);
491 static int msg_handler(struct nlmsghdr *msg) {
492 // We are only interested in RTM_NEWLINK messages
493 if (msg->nlmsg_type != RTM_NEWLINK) {
496 return connectivity_link_state(msg);
499 static int read_event(int (*msg_handler)(struct nlmsghdr *)) {
501 int recv_flags = MSG_DONTWAIT;
503 if (nl_sock == -1 || msg_handler == NULL)
507 pthread_mutex_lock(&connectivity_threads_lock);
509 if (connectivity_netlink_thread_loop <= 0) {
510 pthread_mutex_unlock(&connectivity_threads_lock);
514 pthread_mutex_unlock(&connectivity_threads_lock);
517 int status = recv(nl_sock, buf, sizeof(buf), recv_flags);
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);
532 if (errno == EINTR) {
533 // Interrupt, so just continue and try again
537 /* Anything else is an error */
538 ERROR("connectivity plugin: read_event: Error recv: %d", status);
542 // Message received successfully, so we'll stop blocking on the
543 // receive call for now (until we get a "would block" error, which
544 // will be handled above)
545 recv_flags = MSG_DONTWAIT;
548 DEBUG("connectivity plugin: read_event: EOF");
551 /* We need to handle more than one message per 'recvmsg' */
552 for (struct nlmsghdr *h = (struct nlmsghdr *)buf;
553 NLMSG_OK(h, (unsigned int)status); h = NLMSG_NEXT(h, status)) {
555 if (h->nlmsg_type == NLMSG_DONE)
558 /* Message is some kind of error */
559 if (h->nlmsg_type == NLMSG_ERROR) {
560 struct nlmsgerr *l_err = (struct nlmsgerr *)NLMSG_DATA(h);
561 ERROR("connectivity plugin: read_event: Message is an error: %d",
566 /* Call message handler */
568 ret = (*msg_handler)(h);
570 ERROR("connectivity plugin: read_event: Message handler error %d",
575 ERROR("connectivity plugin: read_event: Error NULL message handler");
584 static void connectivity_dispatch_notification(const char *interface,
585 gauge_t value, gauge_t old_value,
586 cdtime_t timestamp) {
589 .severity = (value == LINK_STATE_UP ? NOTIF_OKAY : NOTIF_FAILURE),
591 .plugin = "connectivity",
593 .type_instance = "interface_status",
596 sstrncpy(n.host, hostname_g, sizeof(n.host));
597 sstrncpy(n.plugin_instance, interface, sizeof(n.plugin_instance));
601 gen_message_payload(value, old_value, interface, timestamp, &buf);
603 int status = plugin_notification_meta_add_string(&n, "ves", buf);
607 ERROR("connectivity plugin: unable to set notification VES metadata: %s",
612 DEBUG("connectivity plugin: notification VES metadata: %s",
613 n.meta->nm_value.nm_string);
615 DEBUG("connectivity plugin: dispatching state %d for interface %s",
616 (int)value, interface);
618 plugin_dispatch_notification(&n);
619 plugin_notification_meta_free(n.meta);
621 // strdup'd in gen_message_payload
626 // NOTE: Caller MUST hold connectivity_data_lock when calling this function
627 static void send_interface_status() {
628 for (interface_list_t *il = interface_list_head; il != NULL;
629 il = il->next) /* {{{ */
631 uint32_t status = il->status;
632 uint32_t prev_status = il->prev_status;
633 uint32_t sent = il->sent;
635 if (status != prev_status && sent == 0) {
636 connectivity_dispatch_notification(il->interface, status, prev_status,
640 } /* }}} for (il = interface_list_head; il != NULL; il = il->next) */
642 statuses_to_send = 0;
645 static void read_interface_status() /* {{{ */
647 pthread_mutex_lock(&connectivity_data_lock);
649 // If we don't have any interface statuses to dispatch,
650 // then we wait until signalled
651 if (!statuses_to_send)
652 pthread_cond_wait(&connectivity_cond, &connectivity_data_lock);
654 send_interface_status();
656 pthread_mutex_unlock(&connectivity_data_lock);
657 } /* }}} int *read_interface_status */
659 static void *connectivity_netlink_thread(void *arg) /* {{{ */
661 pthread_mutex_lock(&connectivity_threads_lock);
663 while (connectivity_netlink_thread_loop > 0) {
664 pthread_mutex_unlock(&connectivity_threads_lock);
666 int status = read_event(msg_handler);
668 pthread_mutex_lock(&connectivity_threads_lock);
671 connectivity_netlink_thread_error = 1;
674 } /* while (connectivity_netlink_thread_loop > 0) */
676 pthread_mutex_unlock(&connectivity_threads_lock);
679 } /* }}} void *connectivity_netlink_thread */
681 static void *connectivity_dequeue_thread(void *arg) /* {{{ */
683 pthread_mutex_lock(&connectivity_threads_lock);
685 while (connectivity_dequeue_thread_loop > 0) {
686 pthread_mutex_unlock(&connectivity_threads_lock);
688 read_interface_status();
690 pthread_mutex_lock(&connectivity_threads_lock);
691 } /* while (connectivity_dequeue_thread_loop > 0) */
693 pthread_mutex_unlock(&connectivity_threads_lock);
696 } /* }}} void *connectivity_dequeue_thread */
698 static int nl_connect() {
699 struct sockaddr_nl sa_nl = {
700 .nl_family = AF_NETLINK,
701 .nl_groups = RTMGRP_LINK,
705 nl_sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
707 ERROR("connectivity plugin: socket open failed: %s", STRERRNO);
711 int rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
713 ERROR("connectivity plugin: socket bind failed: %s", STRERRNO);
722 static int start_netlink_thread(void) /* {{{ */
724 pthread_mutex_lock(&connectivity_threads_lock);
726 if (connectivity_netlink_thread_loop != 0) {
727 pthread_mutex_unlock(&connectivity_threads_lock);
731 connectivity_netlink_thread_loop = 1;
732 connectivity_netlink_thread_error = 0;
737 status = nl_connect();
740 pthread_mutex_unlock(&connectivity_threads_lock);
745 status = plugin_thread_create(&connectivity_netlink_thread_id,
746 /* attr = */ NULL, connectivity_netlink_thread,
747 /* arg = */ (void *)0, "connectivity");
749 connectivity_netlink_thread_loop = 0;
750 ERROR("connectivity plugin: Starting thread failed.");
751 pthread_mutex_unlock(&connectivity_threads_lock);
753 int status2 = close(nl_sock);
756 ERROR("connectivity plugin: failed to close socket %d: %d (%s)", nl_sock,
765 pthread_mutex_unlock(&connectivity_threads_lock);
770 static int start_dequeue_thread(void) /* {{{ */
772 pthread_mutex_lock(&connectivity_threads_lock);
774 if (connectivity_dequeue_thread_loop != 0) {
775 pthread_mutex_unlock(&connectivity_threads_lock);
779 connectivity_dequeue_thread_loop = 1;
782 plugin_thread_create(&connectivity_dequeue_thread_id,
783 /* attr = */ NULL, connectivity_dequeue_thread,
784 /* arg = */ (void *)0, "connectivity");
786 connectivity_dequeue_thread_loop = 0;
787 ERROR("connectivity plugin: Starting dequeue thread failed.");
788 pthread_mutex_unlock(&connectivity_threads_lock);
792 pthread_mutex_unlock(&connectivity_threads_lock);
795 } /* }}} int start_dequeue_thread */
797 static int start_threads(void) /* {{{ */
799 int status = start_netlink_thread();
800 int status2 = start_dequeue_thread();
806 } /* }}} int start_threads */
808 static int stop_netlink_thread(int shutdown) /* {{{ */
813 socket_status = close(nl_sock);
814 if (socket_status != 0) {
815 ERROR("connectivity plugin: failed to close socket %d: %d (%s)", nl_sock,
816 socket_status, STRERRNO);
823 pthread_mutex_lock(&connectivity_threads_lock);
825 if (connectivity_netlink_thread_loop == 0) {
826 pthread_mutex_unlock(&connectivity_threads_lock);
827 // Thread has already been terminated, nothing more to attempt
828 return socket_status;
831 // Set thread termination status
832 connectivity_netlink_thread_loop = 0;
833 pthread_mutex_unlock(&connectivity_threads_lock);
835 // Let threads waiting on access to the interface list know to move
836 // on such that they'll see the thread's termination status
837 pthread_cond_broadcast(&connectivity_cond);
842 // Since the thread is blocking, calling pthread_join
843 // doesn't actually succeed in stopping it. It will stick around
844 // until a NETLINK message is received on the socket (at which
845 // it will realize that "connectivity_netlink_thread_loop" is 0 and will
846 // break out of the read loop and be allowed to die). This is
847 // fine when the process isn't supposed to be exiting, but in
848 // the case of a process shutdown, we don't want to have an
849 // idle thread hanging around. Calling pthread_cancel here in
850 // the case of a shutdown is just assures that the thread is
851 // gone and that the process has been fully terminated.
853 DEBUG("connectivity plugin: Canceling netlink thread for process shutdown");
855 thread_status = pthread_cancel(connectivity_netlink_thread_id);
857 if (thread_status != 0 && thread_status != ESRCH) {
858 ERROR("connectivity plugin: Unable to cancel netlink thread: %d",
865 pthread_join(connectivity_netlink_thread_id, /* return = */ NULL);
866 if (thread_status != 0 && thread_status != ESRCH) {
867 ERROR("connectivity plugin: Stopping netlink thread failed: %d",
874 pthread_mutex_lock(&connectivity_threads_lock);
875 memset(&connectivity_netlink_thread_id, 0,
876 sizeof(connectivity_netlink_thread_id));
877 connectivity_netlink_thread_error = 0;
878 pthread_mutex_unlock(&connectivity_threads_lock);
880 DEBUG("connectivity plugin: Finished requesting stop of netlink thread");
882 if (socket_status != 0)
883 return socket_status;
885 return thread_status;
888 static int stop_dequeue_thread() /* {{{ */
890 pthread_mutex_lock(&connectivity_threads_lock);
892 if (connectivity_dequeue_thread_loop == 0) {
893 pthread_mutex_unlock(&connectivity_threads_lock);
897 // Set thread termination status
898 connectivity_dequeue_thread_loop = 0;
899 pthread_mutex_unlock(&connectivity_threads_lock);
901 // Let threads waiting on access to the interface list know to move
902 // on such that they'll see the threads termination status
903 pthread_cond_broadcast(&connectivity_cond);
905 // Calling pthread_cancel here just assures that the thread is
906 // gone and that the process has been fully terminated.
908 DEBUG("connectivity plugin: Canceling dequeue thread for process shutdown");
910 int status = pthread_cancel(connectivity_dequeue_thread_id);
912 if (status != 0 && status != ESRCH) {
913 ERROR("connectivity plugin: Unable to cancel dequeue thread: %d", status);
918 pthread_mutex_lock(&connectivity_threads_lock);
919 memset(&connectivity_dequeue_thread_id, 0,
920 sizeof(connectivity_dequeue_thread_id));
921 pthread_mutex_unlock(&connectivity_threads_lock);
923 DEBUG("connectivity plugin: Finished requesting stop of dequeue thread");
926 } /* }}} int stop_dequeue_thread */
928 static int stop_threads() /* {{{ */
930 int status = stop_netlink_thread(1);
931 int status2 = stop_dequeue_thread();
937 } /* }}} int stop_threads */
939 static int connectivity_init(void) /* {{{ */
941 if (monitor_all_interfaces) {
942 NOTICE("connectivity plugin: No interfaces have been selected, so all will "
946 return start_threads();
947 } /* }}} int connectivity_init */
949 static int connectivity_config(const char *key, const char *value) /* {{{ */
951 if (ignorelist == NULL) {
952 ignorelist = ignorelist_create(/* invert = */ 1);
954 if (ignorelist == NULL)
958 if (strcasecmp(key, "Interface") == 0) {
959 ignorelist_add(ignorelist, value);
960 monitor_all_interfaces = 0;
961 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
965 ignorelist_set_invert(ignorelist, invert);
971 } /* }}} int connectivity_config */
973 static int connectivity_read(void) /* {{{ */
975 pthread_mutex_lock(&connectivity_threads_lock);
977 if (connectivity_netlink_thread_error != 0) {
979 pthread_mutex_unlock(&connectivity_threads_lock);
981 ERROR("connectivity plugin: The netlink thread had a problem. Restarting "
984 stop_netlink_thread(0);
986 for (interface_list_t *il = interface_list_head; il != NULL;
988 il->status = LINK_STATE_UNKNOWN;
989 il->prev_status = LINK_STATE_UNKNOWN;
993 start_netlink_thread();
996 } /* if (connectivity_netlink_thread_error != 0) */
998 pthread_mutex_unlock(&connectivity_threads_lock);
1001 } /* }}} int connectivity_read */
1003 static int connectivity_shutdown(void) /* {{{ */
1005 DEBUG("connectivity plugin: Shutting down thread.");
1007 int status = stop_threads();
1009 interface_list_t *il = interface_list_head;
1010 while (il != NULL) {
1011 interface_list_t *il_next;
1015 sfree(il->interface);
1021 ignorelist_free(ignorelist);
1024 } /* }}} int connectivity_shutdown */
1026 void module_register(void) {
1027 plugin_register_config("connectivity", connectivity_config, config_keys,
1029 plugin_register_init("connectivity", connectivity_init);
1030 plugin_register_read("connectivity", connectivity_read);
1031 plugin_register_shutdown("connectivity", connectivity_shutdown);
1032 } /* void module_register */