X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fprocevent.c;h=850f7742bc956b31d2027b820a7b2c84b7f08755;hb=34714af37d83d62189cda12ff336acbaf7f6a812;hp=7fab6552be79638d3ce63e42ab71a6f622f6f6eb;hpb=6ce2cd186e3a543f75b8006c1b873f1e3653cdb2;p=collectd.git diff --git a/src/procevent.c b/src/procevent.c index 7fab6552..850f7742 100644 --- a/src/procevent.c +++ b/src/procevent.c @@ -112,7 +112,8 @@ typedef struct { struct processlist_s { char *process; - uint32_t pid; + long pid; + int32_t last_status; struct processlist_s *next; }; @@ -123,26 +124,36 @@ typedef struct processlist_s processlist_t; */ static ignorelist_t *ignorelist = NULL; -static int procevent_thread_loop = 0; -static int procevent_thread_error = 0; -static pthread_t procevent_thread_id; -static pthread_mutex_t procevent_lock = PTHREAD_MUTEX_INITIALIZER; +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 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_cond_t procevent_cond = PTHREAD_COND_INITIALIZER; -static pthread_mutex_t procevent_list_lock = PTHREAD_MUTEX_INITIALIZER; static int nl_sock = -1; static int buffer_length; static circbuf_t ring; static processlist_t *processlist_head = NULL; static int event_id = 0; -static const char *config_keys[] = {"BufferLength", "Process", "RegexProcess"}; +static const char *config_keys[] = {"BufferLength", "Process", "ProcessRegex"}; static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); /* + * Prototypes + */ + +static void procevent_dispatch_notification(long pid, const char *type, + gauge_t value, char *process, + long long unsigned int timestamp); + +/* * Private functions */ -static int gen_message_payload(int state, int pid, char *process, +static int gen_message_payload(int state, long pid, char *process, long long unsigned int timestamp, char **buf) { const unsigned char *buf2; yajl_gen g; @@ -205,7 +216,7 @@ static int gen_message_payload(int state, int pid, char *process, event_name_len = event_name_len + 13; // "process", 3 spaces, 2 parentheses and null-terminator memset(json_str, '\0', DATA_MAX_NAME_LEN); - snprintf(json_str, event_name_len, "process %s (%d) %s", process, pid, + snprintf(json_str, event_name_len, "process %s (%ld) %s", process, pid, (state == 0 ? PROCEVENT_EVENT_NAME_DOWN_VALUE : PROCEVENT_EVENT_NAME_UP_VALUE)); @@ -320,7 +331,7 @@ static int gen_message_payload(int state, int pid, char *process, alarm_condition_len + 25; // "process", "state", "change", 4 spaces, 2 // parentheses and null-terminator memset(json_str, '\0', DATA_MAX_NAME_LEN); - snprintf(json_str, alarm_condition_len, "process %s (%d) state change", + snprintf(json_str, alarm_condition_len, "process %s (%ld) state change", process, pid); if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) != @@ -390,7 +401,7 @@ static int gen_message_payload(int state, int pid, char *process, specific_problem_len + 13; // "process", 3 spaces, 2 parentheses and null-terminator memset(json_str, '\0', DATA_MAX_NAME_LEN); - snprintf(json_str, specific_problem_len, "process %s (%d) %s", process, pid, + snprintf(json_str, specific_problem_len, "process %s (%ld) %s", process, pid, (state == 0 ? PROCEVENT_SPECIFIC_PROBLEM_DOWN_VALUE : PROCEVENT_SPECIFIC_PROBLEM_UP_VALUE)); @@ -423,17 +434,14 @@ static int gen_message_payload(int state, int pid, char *process, if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok) goto err; - *buf = malloc(strlen((char *)buf2) + 1); + *buf = strdup((char *)buf2); if (*buf == NULL) { - char errbuf[1024]; - ERROR("procevent plugin: malloc failed during gen_message_payload: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("procevent plugin: strdup failed during gen_message_payload: %s", + STRERRNO); goto err; } - sstrncpy(*buf, (char *)buf2, strlen((char *)buf2) + 1); - yajl_gen_free(g); return 0; @@ -445,29 +453,30 @@ err: } // Does /proc//comm contain a process name we are interested in? -static processlist_t *process_check(int pid) { - int len, is_match, retval; +// 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]; - len = snprintf(file, sizeof(file), PROCDIR "/%d/comm", pid); + int len = snprintf(file, sizeof(file), PROCDIR "/%ld/comm", pid); if ((len < 0) || (len >= BUFSIZE)) { WARNING("procevent process_check: process name too large"); return NULL; } + FILE *fh; + if (NULL == (fh = fopen(file, "r"))) { // No /proc//comm for this pid, just ignore - DEBUG("procevent plugin: no comm file available for pid %d", pid); + DEBUG("procevent plugin: no comm file available for pid %ld", pid); return NULL; } - retval = fscanf(fh, "%[^\n]", buffer); + char buffer[BUFSIZE]; + int retval = fscanf(fh, "%[^\n]", buffer); if (retval < 0) { - WARNING("procevent process_check: unable to read comm file for pid %d", + WARNING("procevent process_check: unable to read comm file for pid %ld", pid); fclose(fh); return NULL; @@ -476,7 +485,7 @@ static processlist_t *process_check(int pid) { // Now that we have the process name in the buffer, check if we are // even interested in it if (ignorelist_match(ignorelist, buffer) != 0) { - DEBUG("procevent process_check: ignoring process %s (%d)", buffer, pid); + DEBUG("procevent process_check: ignoring process %s (%ld)", buffer, pid); fclose(fh); return NULL; } @@ -496,25 +505,28 @@ static processlist_t *process_check(int pid) { // associate with it (with the same process name as the existing). // - pthread_mutex_lock(&procevent_list_lock); - - processlist_t *pl; processlist_t *match = NULL; - for (pl = processlist_head; pl != NULL; pl = pl->next) { + for (processlist_t *pl = processlist_head; pl != NULL; pl = pl->next) { - is_match = (strcmp(buffer, pl->process) == 0 ? 1 : 0); + int is_match = (strcmp(buffer, pl->process) == 0 ? 1 : 0); if (is_match == 1) { - DEBUG("procevent plugin: process %d name match for %s", pid, buffer); + DEBUG("procevent plugin: process %ld name match for %s", pid, buffer); if (pl->pid == pid) { // this is a match, and we've already stored the exact pid/name combo + DEBUG("procevent plugin: found exact match with name %s, PID %ld for " + "incoming PID %ld", + pl->process, pl->pid, pid); match = pl; break; } else if (pl->pid == -1) { // this is a match, and we've found a candidate processlist_t to store // this new pid/name combo + DEBUG("procevent plugin: reusing pl object with PID %ld for incoming " + "PID %ld", + pl->pid, pid); pl->pid = pid; match = pl; break; @@ -522,6 +534,9 @@ static processlist_t *process_check(int pid) { // this is a match, but another instance of this process has already // claimed this pid/name combo, // so keep looking + DEBUG("procevent plugin: found pl object with matching name for " + "incoming PID %ld, but object is in use by PID %ld", + pid, pl->pid); match = pl; continue; } @@ -535,29 +550,22 @@ static processlist_t *process_check(int pid) { // contained a pid/name combo, // then make a new one and add it to the linked list - DEBUG( - "procevent plugin: allocating new processlist_t object for PID %d (%s)", - pid, buffer); + DEBUG("procevent plugin: allocating new processlist_t object for PID %ld " + "(%s)", + pid, buffer); - processlist_t *pl2; - char *process; - - pl2 = malloc(sizeof(*pl2)); + processlist_t *pl2 = calloc(1, sizeof(*pl2)); if (pl2 == NULL) { - char errbuf[1024]; - ERROR("procevent plugin: malloc failed during process_check: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - pthread_mutex_unlock(&procevent_list_lock); + ERROR("procevent plugin: calloc failed during process_check: %s", + STRERRNO); return NULL; } - process = strdup(buffer); + char *process = strdup(buffer); if (process == NULL) { - char errbuf[1024]; sfree(pl2); ERROR("procevent plugin: strdup failed during process_check: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - pthread_mutex_unlock(&procevent_list_lock); + STRERRNO); return NULL; } @@ -569,32 +577,29 @@ static processlist_t *process_check(int pid) { match = pl2; } - pthread_mutex_unlock(&procevent_list_lock); - return match; } // Does our map have this PID or name? -static processlist_t *process_map_check(int pid, char *process) { - processlist_t *pl; - - pthread_mutex_lock(&procevent_list_lock); - - for (pl = processlist_head; pl != NULL; pl = pl->next) { +// NOTE: Caller MUST hold procevent_data_lock when calling this function +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; } + int match_process = 0; + 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) @@ -603,47 +608,31 @@ static processlist_t *process_map_check(int pid, char *process) { match = 1; if (match == 1) { - pthread_mutex_unlock(&procevent_list_lock); return pl; } } - pthread_mutex_unlock(&procevent_list_lock); - return NULL; } static int process_map_refresh(void) { - DIR *proc; - errno = 0; - proc = opendir(PROCDIR); + DIR *proc = opendir(PROCDIR); + if (proc == NULL) { - char errbuf[1024]; - ERROR("procevent plugin: fopen (%s): %s", PROCDIR, - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("procevent plugin: fopen (%s): %s", PROCDIR, STRERRNO); return -1; } while (42) { - struct dirent *dent; - int len; - char file[BUFSIZE]; - - struct stat statbuf; - - int status; - errno = 0; - dent = readdir(proc); + struct dirent *dent = readdir(proc); if (dent == NULL) { - char errbuf[4096]; - if (errno == 0) /* end of directory */ break; ERROR("procevent plugin: failed to read directory %s: %s", PROCDIR, - sstrerror(errno, errbuf, sizeof(errbuf))); + STRERRNO); closedir(proc); return -1; } @@ -651,15 +640,17 @@ static int process_map_refresh(void) { if (dent->d_name[0] == '.') continue; - len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name); + char file[BUFSIZE]; + + int len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name); if ((len < 0) || (len >= BUFSIZE)) continue; - status = stat(file, &statbuf); + struct stat statbuf; + + int status = stat(file, &statbuf); if (status != 0) { - char errbuf[4096]; - WARNING("procevent plugin: stat (%s) failed: %s", file, - sstrerror(errno, errbuf, sizeof(errbuf))); + WARNING("procevent plugin: stat (%s) failed: %s", file, STRERRNO); continue; } @@ -685,7 +676,9 @@ static int process_map_refresh(void) { // Check if we need to store this pid/name combo in our processlist_t linked // list int this_pid = atoi(dent->d_name); + pthread_mutex_lock(&procevent_data_lock); processlist_t *pl = process_check(this_pid); + pthread_mutex_unlock(&procevent_data_lock); if (pl != NULL) DEBUG("procevent plugin: process map refreshed for PID %d and name %s", @@ -698,22 +691,19 @@ static int process_map_refresh(void) { } static int nl_connect() { - int rc; - struct sockaddr_nl sa_nl; + struct sockaddr_nl sa_nl = { + .nl_family = AF_NETLINK, .nl_groups = CN_IDX_PROC, .nl_pid = getpid(), + }; nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); if (nl_sock == -1) { - ERROR("procevent plugin: socket open failed."); + ERROR("procevent plugin: socket open failed: %d", errno); return -1; } - sa_nl.nl_family = AF_NETLINK; - sa_nl.nl_groups = CN_IDX_PROC; - sa_nl.nl_pid = getpid(); - - rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl)); + int rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl)); if (rc == -1) { - ERROR("procevent plugin: socket bind failed."); + ERROR("procevent plugin: socket bind failed: %d", errno); close(nl_sock); return -1; } @@ -722,7 +712,6 @@ static int nl_connect() { } static int set_proc_ev_listen(bool enable) { - int rc; struct __attribute__((aligned(NLMSG_ALIGNTO))) { struct nlmsghdr nl_hdr; struct __attribute__((__packed__)) { @@ -742,21 +731,19 @@ static int set_proc_ev_listen(bool enable) { nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE; - rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0); + int rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0); if (rc == -1) { - ERROR("procevent plugin: subscribing to netlink process events failed."); + ERROR("procevent plugin: subscribing to netlink process events failed: %d", + errno); return -1; } return 0; } +// Read from netlink socket and write to ring buffer static int read_event() { - int status; - int ret = 0; - int proc_id = -1; - int proc_status = -1; - int proc_extra = -1; + int recv_flags = MSG_DONTWAIT; struct __attribute__((aligned(NLMSG_ALIGNTO))) { struct nlmsghdr nl_hdr; struct __attribute__((__packed__)) { @@ -766,239 +753,478 @@ static int read_event() { } nlcn_msg; if (nl_sock == -1) - return ret; + return 0; - status = recv(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0); + while (42) { + pthread_mutex_lock(&procevent_thread_lock); - if (status == 0) { - return 0; - } else if (status == -1) { - if (errno != EINTR) { - ERROR("procevent plugin: socket receive error: %d", errno); - return -1; + if (procevent_netlink_thread_loop <= 0) { + pthread_mutex_unlock(&procevent_thread_lock); + return 0; + } + + pthread_mutex_unlock(&procevent_thread_lock); + + int status = recv(nl_sock, &nlcn_msg, sizeof(nlcn_msg), recv_flags); + + if (status == 0) { + return 0; + } else if (status < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + pthread_mutex_lock(&procevent_data_lock); + + // There was nothing more to receive for now, so... + // If ring head does not equal ring tail, there is data + // in the ring buffer for the dequeue thread to read, so + // signal it + if (ring.head != ring.tail) + pthread_cond_signal(&procevent_cond); + + pthread_mutex_unlock(&procevent_data_lock); + + // Since there was nothing to receive, set recv to block and + // try again + recv_flags = 0; + continue; + } else if (errno != EINTR) { + ERROR("procevent plugin: socket receive error: %d", errno); + return -1; + } else { + // Interrupt, so just return + return 0; + } + } + + // We successfully received a message, so don't block on the next + // read in case there are more (and if there aren't, it will be + // 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: + case PROC_EVENT_UID: + case PROC_EVENT_GID: + // Not of interest in current version + break; + case PROC_EVENT_EXEC: + proc_status = PROCEVENT_STARTED; + proc_id = nlcn_msg.proc_ev.event_data.exec.process_pid; + break; + case PROC_EVENT_EXIT: + proc_id = nlcn_msg.proc_ev.event_data.exit.process_pid; + proc_status = PROCEVENT_EXITED; + proc_extra = nlcn_msg.proc_ev.event_data.exit.exit_code; + break; + default: + break; } - } - switch (nlcn_msg.proc_ev.what) { - case PROC_EVENT_NONE: - case PROC_EVENT_FORK: - case PROC_EVENT_UID: - case PROC_EVENT_GID: - // Not of interest in current version - break; - case PROC_EVENT_EXEC: - proc_status = PROCEVENT_STARTED; - proc_id = nlcn_msg.proc_ev.event_data.exec.process_pid; - break; - case PROC_EVENT_EXIT: - proc_id = nlcn_msg.proc_ev.event_data.exit.process_pid; - proc_status = PROCEVENT_EXITED; - proc_extra = nlcn_msg.proc_ev.event_data.exit.exit_code; - break; - default: - break; + // If we're interested in this process status event, place the event + // in the ring buffer for consumption by the main polling thread. + + if (proc_status != -1) { + pthread_mutex_lock(&procevent_data_lock); + + int next = ring.head + 1; + if (next >= ring.maxLen) + next = 0; + + if (next == ring.tail) { + // Buffer is full, signal the dequeue thread to process the buffer + // and clean it out, and then sleep + WARNING("procevent plugin: ring buffer full"); + + pthread_cond_signal(&procevent_cond); + pthread_mutex_unlock(&procevent_data_lock); + + usleep(1000); + continue; + } else { + DEBUG("procevent plugin: Process %d status is now %s at %llu", proc_id, + (proc_status == PROCEVENT_EXITED ? "EXITED" : "STARTED"), + (long long unsigned int)CDTIME_T_TO_US(cdtime())); + + if (proc_status == PROCEVENT_EXITED) { + ring.buffer[ring.head][0] = proc_id; + ring.buffer[ring.head][1] = proc_status; + ring.buffer[ring.head][2] = proc_extra; + ring.buffer[ring.head][3] = + (long long unsigned int)CDTIME_T_TO_US(cdtime()); + } else { + ring.buffer[ring.head][0] = proc_id; + ring.buffer[ring.head][1] = proc_status; + ring.buffer[ring.head][2] = 0; + ring.buffer[ring.head][3] = + (long long unsigned int)CDTIME_T_TO_US(cdtime()); + } + + ring.head = next; + } + + pthread_mutex_unlock(&procevent_data_lock); + } } - // If we're interested in this process status event, place the event - // in the ring buffer for consumption by the main polling thread. + return 0; +} - if (proc_status != -1) { - pthread_mutex_unlock(&procevent_lock); +// Read from ring buffer and dispatch to write plugins +static void read_ring_buffer() { + pthread_mutex_lock(&procevent_data_lock); + + // If there's currently nothing to read from the buffer, + // then wait + if (ring.head == ring.tail) + pthread_cond_wait(&procevent_cond, &procevent_data_lock); + + while (ring.head != ring.tail) { + int next = ring.tail + 1; - int next = ring.head + 1; if (next >= ring.maxLen) next = 0; - if (next == ring.tail) { - WARNING("procevent plugin: ring buffer full"); - } else { - DEBUG("procevent plugin: Process %d status is now %s at %llu", proc_id, - (proc_status == PROCEVENT_EXITED ? "EXITED" : "STARTED"), - (long long unsigned int)CDTIME_T_TO_US(cdtime())); - - if (proc_status == PROCEVENT_EXITED) { - ring.buffer[ring.head][0] = proc_id; - ring.buffer[ring.head][1] = proc_status; - ring.buffer[ring.head][2] = proc_extra; - ring.buffer[ring.head][3] = - (long long unsigned int)CDTIME_T_TO_US(cdtime()); - } else { - ring.buffer[ring.head][0] = proc_id; - ring.buffer[ring.head][1] = proc_status; - ring.buffer[ring.head][2] = 0; - ring.buffer[ring.head][3] = - (long long unsigned int)CDTIME_T_TO_US(cdtime()); + if (ring.buffer[ring.tail][1] == PROCEVENT_EXITED) { + processlist_t *pl = process_map_check(ring.buffer[ring.tail][0], NULL); + + if (pl != NULL) { + // This process is of interest to us, so publish its EXITED status + procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge", + ring.buffer[ring.tail][1], pl->process, + ring.buffer[ring.tail][3]); + DEBUG( + "procevent plugin: PID %ld (%s) EXITED, removing PID from process " + "list", + pl->pid, pl->process); + pl->pid = -1; + pl->last_status = -1; } + } else if (ring.buffer[ring.tail][1] == PROCEVENT_STARTED) { + // a new process has started, so check if we should monitor it + processlist_t *pl = process_check(ring.buffer[ring.tail][0]); + + // If we had already seen this process name and pid combo before, + // and the last message was a "process started" message, don't send + // the notfication again + + if (pl != NULL && pl->last_status != PROCEVENT_STARTED) { + // This process is of interest to us, so publish its STARTED status + procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge", + ring.buffer[ring.tail][1], pl->process, + ring.buffer[ring.tail][3]); + + pl->last_status = PROCEVENT_STARTED; - ring.head = next; + DEBUG("procevent plugin: PID %ld (%s) STARTED, adding PID to process " + "list", + pl->pid, pl->process); + } } - pthread_mutex_unlock(&procevent_lock); + ring.tail = next; } - return ret; + pthread_mutex_unlock(&procevent_data_lock); } -static void *procevent_thread(void *arg) /* {{{ */ +// Entry point for thread responsible for listening +// to netlink socket and writing data to ring buffer +static void *procevent_netlink_thread(void *arg) /* {{{ */ { - pthread_mutex_lock(&procevent_lock); - - while (procevent_thread_loop > 0) { - int status; + pthread_mutex_lock(&procevent_thread_lock); - pthread_mutex_unlock(&procevent_lock); + while (procevent_netlink_thread_loop > 0) { + pthread_mutex_unlock(&procevent_thread_lock); - usleep(1000); + int status = read_event(); - status = read_event(); - - pthread_mutex_lock(&procevent_lock); + pthread_mutex_lock(&procevent_thread_lock); if (status < 0) { - procevent_thread_error = 1; + procevent_netlink_thread_error = 1; break; } + } /* while (procevent_netlink_thread_loop > 0) */ - if (procevent_thread_loop <= 0) - break; - } /* while (procevent_thread_loop > 0) */ - - pthread_mutex_unlock(&procevent_lock); + pthread_mutex_unlock(&procevent_thread_lock); - return ((void *)0); -} /* }}} void *procevent_thread */ + return (void *)0; +} /* }}} void *procevent_netlink_thread */ -static int start_thread(void) /* {{{ */ +// Entry point for thread responsible for reading from +// ring buffer and dispatching notifications +static void *procevent_dequeue_thread(void *arg) /* {{{ */ { - int status; + pthread_mutex_lock(&procevent_thread_lock); + + while (procevent_dequeue_thread_loop > 0) { + pthread_mutex_unlock(&procevent_thread_lock); + + read_ring_buffer(); + + pthread_mutex_lock(&procevent_thread_lock); + } /* while (procevent_dequeue_thread_loop > 0) */ - pthread_mutex_lock(&procevent_lock); + pthread_mutex_unlock(&procevent_thread_lock); - if (procevent_thread_loop != 0) { - pthread_mutex_unlock(&procevent_lock); - return (0); + return (void *)0; +} /* }}} void *procevent_dequeue_thread */ + +static int start_netlink_thread(void) /* {{{ */ +{ + pthread_mutex_lock(&procevent_thread_lock); + + if (procevent_netlink_thread_loop != 0) { + pthread_mutex_unlock(&procevent_thread_lock); + return 0; } + int status; + if (nl_sock == -1) { status = nl_connect(); - if (status != 0) + if (status != 0) { + pthread_mutex_unlock(&procevent_thread_lock); return status; + } status = set_proc_ev_listen(true); - if (status == -1) + if (status == -1) { + pthread_mutex_unlock(&procevent_thread_lock); return status; + } } DEBUG("procevent plugin: socket created and bound"); - procevent_thread_loop = 1; - procevent_thread_error = 0; + procevent_netlink_thread_loop = 1; + procevent_netlink_thread_error = 0; - status = plugin_thread_create(&procevent_thread_id, /* attr = */ NULL, - procevent_thread, + status = plugin_thread_create(&procevent_netlink_thread_id, /* attr = */ NULL, + procevent_netlink_thread, /* arg = */ (void *)0, "procevent"); if (status != 0) { - procevent_thread_loop = 0; - ERROR("procevent plugin: Starting thread failed."); - pthread_mutex_unlock(&procevent_lock); - return (-1); + procevent_netlink_thread_loop = 0; + ERROR("procevent plugin: Starting netlink thread failed."); + pthread_mutex_unlock(&procevent_thread_lock); + + int status2 = close(nl_sock); + + if (status2 != 0) { + ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock, + status2, STRERRNO); + } else + nl_sock = -1; + + return -1; } - pthread_mutex_unlock(&procevent_lock); - return (0); -} /* }}} int start_thread */ + pthread_mutex_unlock(&procevent_thread_lock); + + return status; +} /* }}} int start_netlink_thread */ -static int stop_thread(int shutdown) /* {{{ */ +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); + return 0; + } + + procevent_dequeue_thread_loop = 1; + + 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); + return -1; + } + + pthread_mutex_unlock(&procevent_thread_lock); + + return status; +} /* }}} int start_dequeue_thread */ + +static int start_threads(void) /* {{{ */ +{ + int status = start_netlink_thread(); + int status2 = start_dequeue_thread(); + + if (status != 0) + return status; + else + return status2; +} /* }}} int start_threads */ + +static int stop_netlink_thread(int shutdown) /* {{{ */ +{ + int socket_status; if (nl_sock != -1) { - status = close(nl_sock); - if (status != 0) { + socket_status = close(nl_sock); + if (socket_status != 0) { ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock, - status, strerror(errno)); - return (-1); + socket_status, strerror(errno)); + return -1; } else nl_sock = -1; + } else + socket_status = 0; + + pthread_mutex_lock(&procevent_thread_lock); + + if (procevent_netlink_thread_loop == 0) { + pthread_mutex_unlock(&procevent_thread_lock); + return -1; + } + + // Set thread termination status + 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); + + int thread_status; + + if (shutdown == 1) { + // Calling pthread_cancel here in + // the case of a shutdown just assures that the thread is + // gone and that the process has been fully terminated. + + DEBUG("procevent plugin: Canceling netlink thread for process shutdown"); + + thread_status = pthread_cancel(procevent_netlink_thread_id); + + if (thread_status != 0 && thread_status != ESRCH) { + ERROR("procevent plugin: Unable to cancel netlink thread: %d", + thread_status); + thread_status = -1; + } else + thread_status = 0; + } else { + thread_status = + pthread_join(procevent_netlink_thread_id, /* return = */ NULL); + if (thread_status != 0 && thread_status != ESRCH) { + ERROR("procevent plugin: Stopping netlink thread failed."); + thread_status = -1; + } else + thread_status = 0; } - pthread_mutex_lock(&procevent_lock); + pthread_mutex_lock(&procevent_thread_lock); + memset(&procevent_netlink_thread_id, 0, sizeof(procevent_netlink_thread_id)); + procevent_netlink_thread_error = 0; + pthread_mutex_unlock(&procevent_thread_lock); + + DEBUG("procevent plugin: Finished requesting stop of netlink thread"); + + if (socket_status != 0) + return socket_status; + else + return thread_status; +} /* }}} int stop_netlink_thread */ + +static int stop_dequeue_thread(int shutdown) /* {{{ */ +{ + int status; - if (procevent_thread_loop == 0) { - pthread_mutex_unlock(&procevent_lock); - return (-1); + pthread_mutex_lock(&procevent_thread_lock); + + if (procevent_dequeue_thread_loop == 0) { + pthread_mutex_unlock(&procevent_thread_lock); + return -1; } - procevent_thread_loop = 0; + procevent_dequeue_thread_loop = 0; + pthread_mutex_unlock(&procevent_thread_lock); + pthread_cond_broadcast(&procevent_cond); - pthread_mutex_unlock(&procevent_lock); if (shutdown == 1) { // Calling pthread_cancel here in // the case of a shutdown just assures that the thread is // gone and that the process has been fully terminated. - DEBUG("procevent plugin: Canceling thread for process shutdown"); + DEBUG("procevent plugin: Canceling dequeue thread for process shutdown"); - status = pthread_cancel(procevent_thread_id); + status = pthread_cancel(procevent_dequeue_thread_id); - if (status != 0) { - ERROR("procevent plugin: Unable to cancel thread: %d", status); + if (status != 0 && status != ESRCH) { + ERROR("procevent plugin: Unable to cancel dequeue thread: %d", status); status = -1; - } + } else + status = 0; } else { - status = pthread_join(procevent_thread_id, /* return = */ NULL); - if (status != 0) { - ERROR("procevent plugin: Stopping thread failed."); + status = pthread_join(procevent_dequeue_thread_id, /* return = */ NULL); + if (status != 0 && status != ESRCH) { + ERROR("procevent plugin: Stopping dequeue thread failed."); status = -1; - } + } else + status = 0; } - pthread_mutex_lock(&procevent_lock); - memset(&procevent_thread_id, 0, sizeof(procevent_thread_id)); - procevent_thread_error = 0; - pthread_mutex_unlock(&procevent_lock); + pthread_mutex_lock(&procevent_thread_lock); + memset(&procevent_dequeue_thread_id, 0, sizeof(procevent_dequeue_thread_id)); + pthread_mutex_unlock(&procevent_thread_lock); - DEBUG("procevent plugin: Finished requesting stop of thread"); + DEBUG("procevent plugin: Finished requesting stop of dequeue thread"); - return (status); -} /* }}} int stop_thread */ + return status; +} /* }}} int stop_dequeue_thread */ -static int procevent_init(void) /* {{{ */ +static int stop_threads(int shutdown) /* {{{ */ { - int status; + int status = stop_netlink_thread(shutdown); + int status2 = stop_dequeue_thread(shutdown); + if (status != 0) + return status; + else + return status2; +} /* }}} int stop_threads */ + +static int procevent_init(void) /* {{{ */ +{ ring.head = 0; ring.tail = 0; ring.maxLen = buffer_length; - ring.buffer = (long long unsigned int **)malloc( - buffer_length * sizeof(long long unsigned int *)); + ring.buffer = (long long unsigned int **)calloc( + buffer_length, sizeof(long long unsigned int *)); for (int i = 0; i < buffer_length; i++) { - ring.buffer[i] = (long long unsigned int *)malloc( - PROCEVENT_FIELDS * sizeof(long long unsigned int)); + ring.buffer[i] = (long long unsigned int *)calloc( + PROCEVENT_FIELDS, sizeof(long long unsigned int)); } - status = process_map_refresh(); + int status = process_map_refresh(); if (status == -1) { ERROR("procevent plugin: Initial process mapping failed."); - return (-1); + return -1; } - if (processlist_head == NULL) { + if (ignorelist == NULL) { NOTICE("procevent plugin: No processes have been configured."); - return (-1); + return -1; } - return (start_thread()); + return start_threads(); } /* }}} int procevent_init */ static int procevent_config(const char *key, const char *value) /* {{{ */ { - int status; - if (ignorelist == NULL) ignorelist = ignorelist_create(/* invert = */ 1); @@ -1006,49 +1232,52 @@ static int procevent_config(const char *key, const char *value) /* {{{ */ buffer_length = atoi(value); } else if (strcasecmp(key, "Process") == 0) { ignorelist_add(ignorelist, value); - } else if (strcasecmp(key, "RegexProcess") == 0) { + } else if (strcasecmp(key, "ProcessRegex") == 0) { #if HAVE_REGEX_H - status = ignorelist_add(ignorelist, value); + int status = ignorelist_add(ignorelist, 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 \"RegexProcess\" option."); + "for the \"ProcessRegex\" option."); #endif } else { - return (-1); + return -1; } - return (0); + return 0; } /* }}} int procevent_config */ -static void procevent_dispatch_notification(int pid, const char *type, /* {{{ */ +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)); + char *buf = NULL; gen_message_payload(value, pid, process, timestamp, &buf); notification_meta_t *m = calloc(1, sizeof(*m)); if (m == NULL) { - char errbuf[1024]; sfree(buf); - ERROR("procevent plugin: unable to allocate metadata: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("procevent plugin: unable to allocate metadata: %s", STRERRNO); return; } @@ -1060,82 +1289,44 @@ static void procevent_dispatch_notification(int pid, const char *type, /* {{{ */ DEBUG("procevent plugin: notification message: %s", n.meta->nm_value.nm_string); - DEBUG("procevent plugin: dispatching state %d for PID %d (%s)", (int)value, + DEBUG("procevent plugin: dispatching state %d for PID %ld (%s)", (int)value, pid, process); plugin_dispatch_notification(&n); plugin_notification_meta_free(n.meta); - // malloc'd in gen_message_payload + // strdup'd in gen_message_payload if (buf != NULL) sfree(buf); } static int procevent_read(void) /* {{{ */ { - if (procevent_thread_error != 0) { - ERROR( - "procevent plugin: The interface thread had a problem. Restarting it."); - - stop_thread(0); - - start_thread(); + pthread_mutex_lock(&procevent_thread_lock); - return (-1); - } /* if (procevent_thread_error != 0) */ + if (procevent_netlink_thread_error != 0) { - pthread_mutex_lock(&procevent_lock); + pthread_mutex_unlock(&procevent_thread_lock); - while (ring.head != ring.tail) { - int next = ring.tail + 1; + ERROR("procevent plugin: The netlink thread had a problem. Restarting it."); - if (next >= ring.maxLen) - next = 0; + stop_netlink_thread(0); - if (ring.buffer[ring.tail][1] == PROCEVENT_EXITED) { - processlist_t *pl = process_map_check(ring.buffer[ring.tail][0], NULL); + start_netlink_thread(); - if (pl != NULL) { - // This process is of interest to us, so publish its EXITED status - procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge", - ring.buffer[ring.tail][1], pl->process, - ring.buffer[ring.tail][3]); - DEBUG("procevent plugin: PID %d (%s) EXITED, removing PID from process " - "list", - pl->pid, pl->process); - pl->pid = -1; - } - } else if (ring.buffer[ring.tail][1] == PROCEVENT_STARTED) { - // a new process has started, so check if we should monitor it - processlist_t *pl = process_check(ring.buffer[ring.tail][0]); - - if (pl != NULL) { - // This process is of interest to us, so publish its STARTED status - procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge", - ring.buffer[ring.tail][1], pl->process, - ring.buffer[ring.tail][3]); - DEBUG( - "procevent plugin: PID %d (%s) STARTED, adding PID to process list", - pl->pid, pl->process); - } - } - - ring.tail = next; - } + return -1; + } /* if (procevent_netlink_thread_error != 0) */ - pthread_mutex_unlock(&procevent_lock); + pthread_mutex_unlock(&procevent_thread_lock); - return (0); + return 0; } /* }}} int procevent_read */ static int procevent_shutdown(void) /* {{{ */ { - processlist_t *pl; - - DEBUG("procevent plugin: Shutting down thread."); + DEBUG("procevent plugin: Shutting down threads."); - if (stop_thread(1) < 0) - return (-1); + int status = stop_threads(1); for (int i = 0; i < buffer_length; i++) { free(ring.buffer[i]); @@ -1143,7 +1334,7 @@ static int procevent_shutdown(void) /* {{{ */ free(ring.buffer); - pl = processlist_head; + processlist_t *pl = processlist_head; while (pl != NULL) { processlist_t *pl_next; @@ -1155,7 +1346,9 @@ static int procevent_shutdown(void) /* {{{ */ pl = pl_next; } - return (0); + ignorelist_free(ignorelist); + + return status; } /* }}} int procevent_shutdown */ void module_register(void) {