More styling + remove superfluous dequeue thread error logic
authorAndrew Bays <abays@redhat.com>
Fri, 19 Oct 2018 12:11:10 +0000 (08:11 -0400)
committerAndrew Bays <andrew.bays@gmail.com>
Wed, 4 Sep 2019 19:50:44 +0000 (15:50 -0400)
src/procevent.c

index 4c8d024..38c3fd1 100644 (file)
@@ -128,7 +128,6 @@ static int procevent_netlink_thread_loop = 0;
 static int procevent_netlink_thread_error = 0;
 static pthread_t procevent_netlink_thread_id;
 static int procevent_dequeue_thread_loop = 0;
 static int procevent_netlink_thread_error = 0;
 static pthread_t procevent_netlink_thread_id;
 static int procevent_dequeue_thread_loop = 0;
-static int procevent_dequeue_thread_error = 0;
 static pthread_t procevent_dequeue_thread_id;
 static pthread_mutex_t procevent_thread_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t procevent_data_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_t procevent_dequeue_thread_id;
 static pthread_mutex_t procevent_thread_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t procevent_data_lock = PTHREAD_MUTEX_INITIALIZER;
@@ -457,8 +456,6 @@ err:
 // NOTE: Caller MUST hold procevent_data_lock when calling this function
 static processlist_t *process_check(long pid) {
   char file[BUFSIZE];
 // NOTE: Caller MUST hold procevent_data_lock when calling this function
 static processlist_t *process_check(long pid) {
   char file[BUFSIZE];
-  FILE *fh;
-  char buffer[BUFSIZE];
 
   int len = snprintf(file, sizeof(file), PROCDIR "/%ld/comm", pid);
 
 
   int len = snprintf(file, sizeof(file), PROCDIR "/%ld/comm", pid);
 
@@ -467,12 +464,15 @@ static processlist_t *process_check(long pid) {
     return NULL;
   }
 
     return NULL;
   }
 
+  FILE *fh;
+
   if (NULL == (fh = fopen(file, "r"))) {
     // No /proc/<pid>/comm for this pid, just ignore
     DEBUG("procevent plugin: no comm file available for pid %ld", pid);
     return NULL;
   }
 
   if (NULL == (fh = fopen(file, "r"))) {
     // No /proc/<pid>/comm for this pid, just ignore
     DEBUG("procevent plugin: no comm file available for pid %ld", pid);
     return NULL;
   }
 
+  char buffer[BUFSIZE];
   int retval = fscanf(fh, "%[^\n]", buffer);
 
   if (retval < 0) {
   int retval = fscanf(fh, "%[^\n]", buffer);
 
   if (retval < 0) {
@@ -585,19 +585,21 @@ static processlist_t *process_check(long pid) {
 static processlist_t *process_map_check(long pid, char *process) {
   for (processlist_t *pl = processlist_head; pl != NULL; pl = pl->next) {
     int match_pid = 0;
 static processlist_t *process_map_check(long pid, char *process) {
   for (processlist_t *pl = processlist_head; pl != NULL; pl = pl->next) {
     int match_pid = 0;
-    int match_process = 0;
-    int match = 0;
 
     if (pid > 0) {
       if (pl->pid == pid)
         match_pid = 1;
     }
 
 
     if (pid > 0) {
       if (pl->pid == pid)
         match_pid = 1;
     }
 
+    int match_process = 0;
+
     if (process != NULL) {
       if (strcmp(pl->process, process) == 0)
         match_process = 1;
     }
 
     if (process != NULL) {
       if (strcmp(pl->process, process) == 0)
         match_process = 1;
     }
 
+    int match = 0;
+
     if (pid > 0 && process == NULL && match_pid == 1)
       match = 1;
     else if (pid < 0 && process != NULL && match_process == 1)
     if (pid > 0 && process == NULL && match_pid == 1)
       match = 1;
     else if (pid < 0 && process != NULL && match_process == 1)
@@ -623,10 +625,6 @@ static int process_map_refresh(void) {
   }
 
   while (42) {
   }
 
   while (42) {
-    char file[BUFSIZE];
-
-    struct stat statbuf;
-
     errno = 0;
     struct dirent *dent = readdir(proc);
     if (dent == NULL) {
     errno = 0;
     struct dirent *dent = readdir(proc);
     if (dent == NULL) {
@@ -642,10 +640,14 @@ static int process_map_refresh(void) {
     if (dent->d_name[0] == '.')
       continue;
 
     if (dent->d_name[0] == '.')
       continue;
 
+    char file[BUFSIZE];
+
     int len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
     if ((len < 0) || (len >= BUFSIZE))
       continue;
 
     int len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
     if ((len < 0) || (len >= BUFSIZE))
       continue;
 
+    struct stat statbuf;
+
     int status = stat(file, &statbuf);
     if (status != 0) {
       WARNING("procevent plugin: stat (%s) failed: %s", file, STRERRNO);
     int status = stat(file, &statbuf);
     if (status != 0) {
       WARNING("procevent plugin: stat (%s) failed: %s", file, STRERRNO);
@@ -754,10 +756,6 @@ static int read_event() {
     return 0;
 
   while (42) {
     return 0;
 
   while (42) {
-    int proc_id = -1;
-    int proc_status = -1;
-    int proc_extra = -1;
-
     pthread_mutex_lock(&procevent_thread_lock);
 
     if (procevent_netlink_thread_loop <= 0) {
     pthread_mutex_lock(&procevent_thread_lock);
 
     if (procevent_netlink_thread_loop <= 0) {
@@ -799,6 +797,10 @@ static int read_event() {
     // handled above in the error-checking)
     recv_flags = MSG_DONTWAIT;
 
     // handled above in the error-checking)
     recv_flags = MSG_DONTWAIT;
 
+    int proc_id = -1;
+    int proc_status = -1;
+    int proc_extra = -1;
+
     switch (nlcn_msg.proc_ev.what) {
     case PROC_EVENT_NONE:
     case PROC_EVENT_FORK:
     switch (nlcn_msg.proc_ev.what) {
     case PROC_EVENT_NONE:
     case PROC_EVENT_FORK:
@@ -869,7 +871,7 @@ static int read_event() {
 }
 
 // Read from ring buffer and dispatch to write plugins
 }
 
 // Read from ring buffer and dispatch to write plugins
-static int read_ring_buffer() {
+static void read_ring_buffer() {
   pthread_mutex_lock(&procevent_data_lock);
 
   // If there's currently nothing to read from the buffer,
   pthread_mutex_lock(&procevent_data_lock);
 
   // If there's currently nothing to read from the buffer,
@@ -924,8 +926,6 @@ static int read_ring_buffer() {
   }
 
   pthread_mutex_unlock(&procevent_data_lock);
   }
 
   pthread_mutex_unlock(&procevent_data_lock);
-
-  return 0;
 }
 
 // Entry point for thread responsible for listening
 }
 
 // Entry point for thread responsible for listening
@@ -949,7 +949,7 @@ static void *procevent_netlink_thread(void *arg) /* {{{ */
 
   pthread_mutex_unlock(&procevent_thread_lock);
 
 
   pthread_mutex_unlock(&procevent_thread_lock);
 
-  return ((void *)0);
+  return (void *)0;
 } /* }}} void *procevent_netlink_thread */
 
 // Entry point for thread responsible for reading from
 } /* }}} void *procevent_netlink_thread */
 
 // Entry point for thread responsible for reading from
@@ -961,32 +961,27 @@ static void *procevent_dequeue_thread(void *arg) /* {{{ */
   while (procevent_dequeue_thread_loop > 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
 
   while (procevent_dequeue_thread_loop > 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
 
-    int status = read_ring_buffer();
+    read_ring_buffer();
 
     pthread_mutex_lock(&procevent_thread_lock);
 
     pthread_mutex_lock(&procevent_thread_lock);
-
-    if (status < 0) {
-      procevent_dequeue_thread_error = 1;
-      break;
-    }
   } /* while (procevent_dequeue_thread_loop > 0) */
 
   pthread_mutex_unlock(&procevent_thread_lock);
 
   } /* while (procevent_dequeue_thread_loop > 0) */
 
   pthread_mutex_unlock(&procevent_thread_lock);
 
-  return ((void *)0);
+  return (void *)0;
 } /* }}} void *procevent_dequeue_thread */
 
 static int start_netlink_thread(void) /* {{{ */
 {
 } /* }}} void *procevent_dequeue_thread */
 
 static int start_netlink_thread(void) /* {{{ */
 {
-  int status;
-
   pthread_mutex_lock(&procevent_thread_lock);
 
   if (procevent_netlink_thread_loop != 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
   pthread_mutex_lock(&procevent_thread_lock);
 
   if (procevent_netlink_thread_loop != 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
-    return (0);
+    return 0;
   }
 
   }
 
+  int status;
+
   if (nl_sock == -1) {
     status = nl_connect();
 
   if (nl_sock == -1) {
     status = nl_connect();
 
@@ -1023,7 +1018,7 @@ static int start_netlink_thread(void) /* {{{ */
     } else
       nl_sock = -1;
 
     } else
       nl_sock = -1;
 
-    return (-1);
+    return -1;
   }
 
   pthread_mutex_unlock(&procevent_thread_lock);
   }
 
   pthread_mutex_unlock(&procevent_thread_lock);
@@ -1033,26 +1028,23 @@ static int start_netlink_thread(void) /* {{{ */
 
 static int start_dequeue_thread(void) /* {{{ */
 {
 
 static int start_dequeue_thread(void) /* {{{ */
 {
-  int status;
-
   pthread_mutex_lock(&procevent_thread_lock);
 
   if (procevent_dequeue_thread_loop != 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
   pthread_mutex_lock(&procevent_thread_lock);
 
   if (procevent_dequeue_thread_loop != 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
-    return (0);
+    return 0;
   }
 
   procevent_dequeue_thread_loop = 1;
   }
 
   procevent_dequeue_thread_loop = 1;
-  procevent_dequeue_thread_error = 0;
 
 
-  status = plugin_thread_create(&procevent_dequeue_thread_id, /* attr = */ NULL,
-                                procevent_dequeue_thread,
-                                /* arg = */ (void *)0, "procevent");
+  int status = plugin_thread_create(&procevent_dequeue_thread_id,
+                                    /* attr = */ NULL, procevent_dequeue_thread,
+                                    /* arg = */ (void *)0, "procevent");
   if (status != 0) {
     procevent_dequeue_thread_loop = 0;
     ERROR("procevent plugin: Starting dequeue thread failed.");
     pthread_mutex_unlock(&procevent_thread_lock);
   if (status != 0) {
     procevent_dequeue_thread_loop = 0;
     ERROR("procevent plugin: Starting dequeue thread failed.");
     pthread_mutex_unlock(&procevent_thread_lock);
-    return (-1);
+    return -1;
   }
 
   pthread_mutex_unlock(&procevent_thread_lock);
   }
 
   pthread_mutex_unlock(&procevent_thread_lock);
@@ -1073,14 +1065,14 @@ static int start_threads(void) /* {{{ */
 
 static int stop_netlink_thread(int shutdown) /* {{{ */
 {
 
 static int stop_netlink_thread(int shutdown) /* {{{ */
 {
-  int socket_status, thread_status;
+  int socket_status;
 
   if (nl_sock != -1) {
     socket_status = close(nl_sock);
     if (socket_status != 0) {
       ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock,
             socket_status, strerror(errno));
 
   if (nl_sock != -1) {
     socket_status = close(nl_sock);
     if (socket_status != 0) {
       ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock,
             socket_status, strerror(errno));
-      return (-1);
+      return -1;
     } else
       nl_sock = -1;
   } else
     } else
       nl_sock = -1;
   } else
@@ -1090,14 +1082,19 @@ static int stop_netlink_thread(int shutdown) /* {{{ */
 
   if (procevent_netlink_thread_loop == 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
 
   if (procevent_netlink_thread_loop == 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
-    return (-1);
+    return -1;
   }
 
   }
 
+  // Set thread termination status
   procevent_netlink_thread_loop = 0;
   pthread_mutex_unlock(&procevent_thread_lock);
 
   procevent_netlink_thread_loop = 0;
   pthread_mutex_unlock(&procevent_thread_lock);
 
+  // Let threads waiting on access to the data know to move
+  // on such that they'll see the thread's termination status
   pthread_cond_broadcast(&procevent_cond);
 
   pthread_cond_broadcast(&procevent_cond);
 
+  int thread_status;
+
   if (shutdown == 1) {
     // Calling pthread_cancel here in
     // the case of a shutdown just assures that the thread is
   if (shutdown == 1) {
     // Calling pthread_cancel here in
     // the case of a shutdown just assures that the thread is
@@ -1144,7 +1141,7 @@ static int stop_dequeue_thread(int shutdown) /* {{{ */
 
   if (procevent_dequeue_thread_loop == 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
 
   if (procevent_dequeue_thread_loop == 0) {
     pthread_mutex_unlock(&procevent_thread_lock);
-    return (-1);
+    return -1;
   }
 
   procevent_dequeue_thread_loop = 0;
   }
 
   procevent_dequeue_thread_loop = 0;
@@ -1177,12 +1174,11 @@ static int stop_dequeue_thread(int shutdown) /* {{{ */
 
   pthread_mutex_lock(&procevent_thread_lock);
   memset(&procevent_dequeue_thread_id, 0, sizeof(procevent_dequeue_thread_id));
 
   pthread_mutex_lock(&procevent_thread_lock);
   memset(&procevent_dequeue_thread_id, 0, sizeof(procevent_dequeue_thread_id));
-  procevent_dequeue_thread_error = 0;
   pthread_mutex_unlock(&procevent_thread_lock);
 
   DEBUG("procevent plugin: Finished requesting stop of dequeue thread");
 
   pthread_mutex_unlock(&procevent_thread_lock);
 
   DEBUG("procevent plugin: Finished requesting stop of dequeue thread");
 
-  return (status);
+  return status;
 } /* }}} int stop_dequeue_thread */
 
 static int stop_threads(int shutdown) /* {{{ */
 } /* }}} int stop_dequeue_thread */
 
 static int stop_threads(int shutdown) /* {{{ */
@@ -1190,7 +1186,7 @@ static int stop_threads(int shutdown) /* {{{ */
   int status = stop_netlink_thread(shutdown);
   int status2 = stop_dequeue_thread(shutdown);
 
   int status = stop_netlink_thread(shutdown);
   int status2 = stop_dequeue_thread(shutdown);
 
-  if (status < 0)
+  if (status != 0)
     return status;
   else
     return status2;
     return status;
   else
     return status2;
@@ -1213,15 +1209,15 @@ static int procevent_init(void) /* {{{ */
 
   if (status == -1) {
     ERROR("procevent plugin: Initial process mapping failed.");
 
   if (status == -1) {
     ERROR("procevent plugin: Initial process mapping failed.");
-    return (-1);
+    return -1;
   }
 
   if (ignorelist == NULL) {
     NOTICE("procevent plugin: No processes have been configured.");
   }
 
   if (ignorelist == NULL) {
     NOTICE("procevent plugin: No processes have been configured.");
-    return (-1);
+    return -1;
   }
 
   }
 
-  return (start_threads());
+  return start_threads();
 } /* }}} int procevent_init */
 
 static int procevent_config(const char *key, const char *value) /* {{{ */
 } /* }}} int procevent_init */
 
 static int procevent_config(const char *key, const char *value) /* {{{ */
@@ -1239,35 +1235,39 @@ static int procevent_config(const char *key, const char *value) /* {{{ */
 
     if (status != 0) {
       ERROR("procevent plugin: invalid regular expression: %s", value);
 
     if (status != 0) {
       ERROR("procevent plugin: invalid regular expression: %s", value);
-      return (1);
+      return 1;
     }
 #else
     WARNING("procevent plugin: The plugin has been compiled without support "
             "for the \"ProcessRegex\" option.");
 #endif
   } else {
     }
 #else
     WARNING("procevent plugin: The plugin has been compiled without support "
             "for the \"ProcessRegex\" option.");
 #endif
   } else {
-    return (-1);
+    return -1;
   }
 
   }
 
-  return (0);
+  return 0;
 } /* }}} int procevent_config */
 
 static void procevent_dispatch_notification(long pid,
                                             const char *type, /* {{{ */
                                             gauge_t value, char *process,
                                             long long unsigned int timestamp) {
 } /* }}} int procevent_config */
 
 static void procevent_dispatch_notification(long pid,
                                             const char *type, /* {{{ */
                                             gauge_t value, char *process,
                                             long long unsigned int timestamp) {
-  char *buf = NULL;
-  notification_t n = {NOTIF_FAILURE, cdtime(), "", "", "procevent", "", "", "",
-                      NULL};
-
-  if (value == 1)
-    n.severity = NOTIF_OKAY;
 
 
+  notification_t n = {(value == 1 ? NOTIF_OKAY : NOTIF_FAILURE),
+                      cdtime(),
+                      "",
+                      "",
+                      "procevent",
+                      "",
+                      "",
+                      "",
+                      NULL};
   sstrncpy(n.host, hostname_g, sizeof(n.host));
   sstrncpy(n.plugin_instance, process, sizeof(n.plugin_instance));
   sstrncpy(n.type, "gauge", sizeof(n.type));
   sstrncpy(n.type_instance, "process_status", sizeof(n.type_instance));
 
   sstrncpy(n.host, hostname_g, sizeof(n.host));
   sstrncpy(n.plugin_instance, process, sizeof(n.plugin_instance));
   sstrncpy(n.type, "gauge", sizeof(n.type));
   sstrncpy(n.type_instance, "process_status", sizeof(n.type_instance));
 
+  char *buf = NULL;
   gen_message_payload(value, pid, process, timestamp, &buf);
 
   notification_meta_t *m = calloc(1, sizeof(*m));
   gen_message_payload(value, pid, process, timestamp, &buf);
 
   notification_meta_t *m = calloc(1, sizeof(*m));
@@ -1311,25 +1311,12 @@ static int procevent_read(void) /* {{{ */
 
     start_netlink_thread();
 
 
     start_netlink_thread();
 
-    return (-1);
+    return -1;
   } /* if (procevent_netlink_thread_error != 0) */
 
   } /* if (procevent_netlink_thread_error != 0) */
 
-  if (procevent_dequeue_thread_error != 0) {
-
-    pthread_mutex_unlock(&procevent_thread_lock);
-
-    ERROR("procevent plugin: The dequeue thread had a problem. Restarting it.");
-
-    stop_dequeue_thread(0);
-
-    start_dequeue_thread();
-
-    return (-1);
-  } /* if (procevent_dequeue_thread_error != 0) */
-
   pthread_mutex_unlock(&procevent_thread_lock);
 
   pthread_mutex_unlock(&procevent_thread_lock);
 
-  return (0);
+  return 0;
 } /* }}} int procevent_read */
 
 static int procevent_shutdown(void) /* {{{ */
 } /* }}} int procevent_read */
 
 static int procevent_shutdown(void) /* {{{ */
@@ -1356,6 +1343,8 @@ static int procevent_shutdown(void) /* {{{ */
     pl = pl_next;
   }
 
     pl = pl_next;
   }
 
+  ignorelist_free(ignorelist);
+
   return status;
 } /* }}} int procevent_shutdown */
 
   return status;
 } /* }}} int procevent_shutdown */