X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fmcelog.c;h=1a92a065eeda129ebbe55e2d6224bdcf31e28d52;hp=e5764e43d1be3326acb4cf3045937928a445c0ec;hb=a9e50e9e30ecde17e167e271060c8183bfcbf407;hpb=cf500b62343bf8d0c56220c95091a7def6e09f95 diff --git a/src/mcelog.c b/src/mcelog.c index e5764e43..1a92a065 100644 --- a/src/mcelog.c +++ b/src/mcelog.c @@ -52,12 +52,11 @@ #define MCELOG_UNCORRECTED_ERR_TYPE_INS "uncorrected_memory_errors" typedef struct mcelog_config_s { - char logfile[PATH_MAX]; /* mcelog logfile */ - pthread_t tid; /* poll thread id */ - llist_t *dimms_list; /* DIMMs list */ - /* lock for dimms cache */ + char logfile[PATH_MAX]; /* mcelog logfile */ + pthread_t tid; /* poll thread id */ + llist_t *dimms_list; /* DIMMs list */ + pthread_mutex_t dimms_lock; /* lock for dimms cache */ _Bool persist; - pthread_mutex_t dimms_lock; } mcelog_config_t; typedef struct socket_adapter_s socket_adapter_t; @@ -76,12 +75,12 @@ struct socket_adapter_s { typedef struct mcelog_memory_rec_s { int corrected_err_total; /* x total*/ int corrected_err_timed; /* x in 24h*/ - char corrected_err_timed_period[DATA_MAX_NAME_LEN]; + char corrected_err_timed_period[DATA_MAX_NAME_LEN / 2]; int uncorrected_err_total; /* x total*/ int uncorrected_err_timed; /* x in 24h*/ - char uncorrected_err_timed_period[DATA_MAX_NAME_LEN]; - char location[DATA_MAX_NAME_LEN]; /* SOCKET x CHANNEL x DIMM x*/ - char dimm_name[DATA_MAX_NAME_LEN]; /* DMI_NAME "DIMM_F1" */ + char uncorrected_err_timed_period[DATA_MAX_NAME_LEN / 2]; + char location[DATA_MAX_NAME_LEN / 2]; /* SOCKET x CHANNEL x DIMM x*/ + char dimm_name[DATA_MAX_NAME_LEN / 2]; /* DMI_NAME "DIMM_F1" */ } mcelog_memory_rec_t; static int socket_close(socket_adapter_t *self); @@ -125,39 +124,41 @@ static llentry_t *mcelog_dimm(const mcelog_memory_rec_t *rec, char dimm_name[DATA_MAX_NAME_LEN]; if (strlen(rec->dimm_name) > 0) { - ssnprintf(dimm_name, sizeof(dimm_name), "%s_%s", rec->location, - rec->dimm_name); + snprintf(dimm_name, sizeof(dimm_name), "%s_%s", rec->location, + rec->dimm_name); } else sstrncpy(dimm_name, rec->location, sizeof(dimm_name)); llentry_t *dimm_le = llist_search(g_mcelog_config.dimms_list, dimm_name); - if (dimm_le == NULL) { - mcelog_memory_rec_t *dimm_mr = calloc(1, sizeof(*dimm_mr)); - if (dimm_mr == NULL) { - ERROR(MCELOG_PLUGIN ": Error allocating dimm memory item"); - return NULL; - } - char *p_name = strdup(dimm_name); - if (p_name == NULL) { - ERROR(MCELOG_PLUGIN ": strdup: error"); - free(dimm_mr); - return NULL; - } + if (dimm_le != NULL) + return dimm_le; - /* add new dimm */ - dimm_le = llentry_create(p_name, dimm_mr); - if (dimm_le == NULL) { - ERROR(MCELOG_PLUGIN ": llentry_create(): error"); - free(dimm_mr); - free(p_name); - return NULL; - } - pthread_mutex_lock(&g_mcelog_config.dimms_lock); - llist_append(g_mcelog_config.dimms_list, dimm_le); - pthread_mutex_unlock(&g_mcelog_config.dimms_lock); + /* allocate new linked list entry */ + mcelog_memory_rec_t *dimm_mr = calloc(1, sizeof(*dimm_mr)); + if (dimm_mr == NULL) { + ERROR(MCELOG_PLUGIN ": Error allocating dimm memory item"); + return NULL; + } + char *p_name = strdup(dimm_name); + if (p_name == NULL) { + ERROR(MCELOG_PLUGIN ": strdup: error"); + free(dimm_mr); + return NULL; } + /* add new dimm */ + dimm_le = llentry_create(p_name, dimm_mr); + if (dimm_le == NULL) { + ERROR(MCELOG_PLUGIN ": llentry_create(): error"); + free(dimm_mr); + free(p_name); + return NULL; + } + pthread_mutex_lock(&g_mcelog_config.dimms_lock); + llist_append(g_mcelog_config.dimms_list, dimm_le); + pthread_mutex_unlock(&g_mcelog_config.dimms_lock); + return dimm_le; } @@ -178,13 +179,13 @@ static int mcelog_config(oconfig_item_t *ci) { ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\", Memory " "option is already configured.", child->key); - return (-1); + return -1; } if (cf_util_get_string_buffer(child, g_mcelog_config.logfile, sizeof(g_mcelog_config.logfile)) < 0) { ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\".", child->key); - return (-1); + return -1; } memset(socket_adapter.unix_sock.sun_path, 0, sizeof(socket_adapter.unix_sock.sun_path)); @@ -193,74 +194,70 @@ static int mcelog_config(oconfig_item_t *ci) { ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\", Logfile " "option is already configured.", child->key); - return (-1); + return -1; } use_memory = 1; - oconfig_item_t *mem_child = child->children; for (int j = 0; j < child->children_num; j++) { - mem_child += j; + oconfig_item_t *mem_child = child->children + j; if (strcasecmp("McelogClientSocket", mem_child->key) == 0) { if (cf_util_get_string_buffer( mem_child, socket_adapter.unix_sock.sun_path, sizeof(socket_adapter.unix_sock.sun_path)) < 0) { ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\".", mem_child->key); - return (-1); + return -1; } } else if (strcasecmp("PersistentNotification", mem_child->key) == 0) { if (cf_util_get_boolean(mem_child, &g_mcelog_config.persist) < 0) { ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\".", mem_child->key); - return (-1); + return -1; } } else { ERROR(MCELOG_PLUGIN ": Invalid Memory configuration option: \"%s\".", mem_child->key); - return (-1); + return -1; } } memset(g_mcelog_config.logfile, 0, sizeof(g_mcelog_config.logfile)); } else { ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\".", child->key); - return (-1); + return -1; } } if (!use_logfile && !use_memory) mcelog_apply_defaults = 1; - return (0); + return 0; } static int socket_close(socket_adapter_t *self) { int ret = 0; pthread_rwlock_rdlock(&self->lock); if (fcntl(self->sock_fd, F_GETFL) != -1) { - char errbuf[MCELOG_BUFF_SIZE]; if (shutdown(self->sock_fd, SHUT_RDWR) != 0) { - ERROR(MCELOG_PLUGIN ": Socket shutdown failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR(MCELOG_PLUGIN ": Socket shutdown failed: %s", STRERRNO); ret = -1; } if (close(self->sock_fd) != 0) { - ERROR(MCELOG_PLUGIN ": Socket close failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR(MCELOG_PLUGIN ": Socket close failed: %s", STRERRNO); ret = -1; } } pthread_rwlock_unlock(&self->lock); - return (ret); + return ret; } static int socket_write(socket_adapter_t *self, const char *msg, const size_t len) { int ret = 0; pthread_rwlock_rdlock(&self->lock); - if (swrite(self->sock_fd, msg, len) < 0) + if (swrite(self->sock_fd, msg, len) != 0) ret = -1; pthread_rwlock_unlock(&self->lock); - return (ret); + return ret; } static void mcelog_dispatch_notification(notification_t *n) { @@ -277,7 +274,6 @@ static void mcelog_dispatch_notification(notification_t *n) { } static int socket_reinit(socket_adapter_t *self) { - char errbuff[MCELOG_BUFF_SIZE]; int ret = -1; cdtime_t interval = plugin_get_interval(); struct timeval socket_timeout = CDTIME_T_TO_TIMEVAL(interval); @@ -287,10 +283,9 @@ static int socket_reinit(socket_adapter_t *self) { self->sock_fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); if (self->sock_fd < 0) { - ERROR(MCELOG_PLUGIN ": Could not create a socket. %s", - sstrerror(errno, errbuff, sizeof(errbuff))); + ERROR(MCELOG_PLUGIN ": Could not create a socket. %s", STRERRNO); pthread_rwlock_unlock(&self->lock); - return (ret); + return ret; } /* Set socket timeout option */ @@ -304,8 +299,7 @@ static int socket_reinit(socket_adapter_t *self) { pthread_rwlock_rdlock(&self->lock); if (connect(self->sock_fd, (struct sockaddr *)&(self->unix_sock), sizeof(self->unix_sock)) < 0) { - ERROR(MCELOG_PLUGIN ": Failed to connect to mcelog server. %s", - sstrerror(errno, errbuff, sizeof(errbuff))); + ERROR(MCELOG_PLUGIN ": Failed to connect to mcelog server. %s", STRERRNO); self->close(self); ret = -1; } else { @@ -318,7 +312,7 @@ static int socket_reinit(socket_adapter_t *self) { .type_instance = "mcelog_status"}); } pthread_rwlock_unlock(&self->lock); - return (ret); + return ret; } static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) { @@ -330,13 +324,13 @@ static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) { int dispatch_corrected_notifs = 0, dispatch_uncorrected_notifs = 0; if (mr == NULL) - return (-1); + return -1; llentry_t *dimm = mcelog_dimm(mr, g_mcelog_config.dimms_list); if (dimm == NULL) { ERROR(MCELOG_PLUGIN ": Error adding/getting dimm memory item to/from cache"); - return (-1); + return -1; } mcelog_memory_rec_t *mr_old = dimm->value; if (!g_mcelog_config.persist) { @@ -351,7 +345,7 @@ static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) { if (!dispatch_corrected_notifs && !dispatch_uncorrected_notifs) { DEBUG("%s: No new notifications to dispatch", MCELOG_PLUGIN); - return (0); + return 0; } } else { dispatch_corrected_notifs = 1; @@ -361,8 +355,8 @@ static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) { sstrncpy(n.host, hostname_g, sizeof(n.host)); if (mr->dimm_name[0] != '\0') - ssnprintf(n.plugin_instance, sizeof(n.plugin_instance), "%s_%s", - mr->location, mr->dimm_name); + snprintf(n.plugin_instance, sizeof(n.plugin_instance), "%s_%s", + mr->location, mr->dimm_name); else sstrncpy(n.plugin_instance, mr->location, sizeof(n.plugin_instance)); @@ -373,7 +367,7 @@ static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) { mr->corrected_err_total); plugin_notification_meta_add_signed_int(&n, MCELOG_CORRECTED_ERR_TIMED, mr->corrected_err_timed); - ssnprintf(n.message, sizeof(n.message), MCELOG_CORRECTED_ERR); + snprintf(n.message, sizeof(n.message), MCELOG_CORRECTED_ERR); sstrncpy(n.type_instance, MCELOG_CORRECTED_ERR_TYPE_INS, sizeof(n.type_instance)); plugin_dispatch_notification(&n); @@ -389,7 +383,7 @@ static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) { mr->uncorrected_err_total); plugin_notification_meta_add_signed_int(&n, MCELOG_UNCORRECTED_ERR_TIMED, mr->uncorrected_err_timed); - ssnprintf(n.message, sizeof(n.message), MCELOG_UNCORRECTED_ERR); + snprintf(n.message, sizeof(n.message), MCELOG_UNCORRECTED_ERR); sstrncpy(n.type_instance, MCELOG_UNCORRECTED_ERR_TYPE_INS, sizeof(n.type_instance)); n.severity = NOTIF_FAILURE; @@ -399,21 +393,21 @@ static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) { n.meta = NULL; } - return (0); + return 0; } static int mcelog_submit(const mcelog_memory_rec_t *mr) { if (!mr) { ERROR(MCELOG_PLUGIN ": %s: NULL pointer", __FUNCTION__); - return (-1); + return -1; } llentry_t *dimm = mcelog_dimm(mr, g_mcelog_config.dimms_list); if (dimm == NULL) { ERROR(MCELOG_PLUGIN ": Error adding/getting dimm memory item to/from cache"); - return (-1); + return -1; } value_list_t vl = { @@ -427,15 +421,15 @@ static int mcelog_submit(const mcelog_memory_rec_t *mr) { mcelog_update_dimm_stats(dimm, mr); if (mr->dimm_name[0] != '\0') - ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s_%s", - mr->location, mr->dimm_name); + snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s_%s", + mr->location, mr->dimm_name); else sstrncpy(vl.plugin_instance, mr->location, sizeof(vl.plugin_instance)); plugin_dispatch_values(&vl); - ssnprintf(vl.type_instance, sizeof(vl.type_instance), - "corrected_memory_errors_in_%s", mr->corrected_err_timed_period); + snprintf(vl.type_instance, sizeof(vl.type_instance), + "corrected_memory_errors_in_%s", mr->corrected_err_timed_period); vl.values = &(value_t){.derive = (derive_t)mr->corrected_err_timed}; plugin_dispatch_values(&vl); @@ -444,13 +438,12 @@ static int mcelog_submit(const mcelog_memory_rec_t *mr) { vl.values = &(value_t){.derive = (derive_t)mr->uncorrected_err_total}; plugin_dispatch_values(&vl); - ssnprintf(vl.type_instance, sizeof(vl.type_instance), - "uncorrected_memory_errors_in_%s", - mr->uncorrected_err_timed_period); + snprintf(vl.type_instance, sizeof(vl.type_instance), + "uncorrected_memory_errors_in_%s", mr->uncorrected_err_timed_period); vl.values = &(value_t){.derive = (derive_t)mr->uncorrected_err_timed}; plugin_dispatch_values(&vl); - return (0); + return 0; } static int parse_memory_info(FILE *p_file, mcelog_memory_rec_t *memory_record) { @@ -459,7 +452,7 @@ static int parse_memory_info(FILE *p_file, mcelog_memory_rec_t *memory_record) { /* Got empty line or "done" */ if ((!strncmp("\n", buf, strlen(buf))) || (!strncmp(buf, "done\n", strlen(buf)))) - return (1); + return 1; if (strlen(buf) < 5) continue; if (!strncmp(buf, MCELOG_SOCKET_STR, strlen(MCELOG_SOCKET_STR))) { @@ -515,7 +508,7 @@ static int parse_memory_info(FILE *p_file, mcelog_memory_rec_t *memory_record) { memset(buf, 0, sizeof(buf)); } /* parsing definitely finished */ - return (0); + return 0; } static void poll_worker_cleanup(void *arg) { @@ -535,12 +528,10 @@ static int socket_receive(socket_adapter_t *self, FILE **pp_file) { if ((res = poll(&poll_fd, 1, MCELOG_POLL_TIMEOUT)) <= 0) { if (res != 0 && errno != EINTR) { - char errbuf[MCELOG_BUFF_SIZE]; - ERROR("mcelog: poll failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("mcelog: poll failed: %s", STRERRNO); } pthread_rwlock_unlock(&self->lock); - return (res); + return res; } if (poll_fd.revents & (POLLERR | POLLHUP | POLLNVAL)) { @@ -555,29 +546,27 @@ static int socket_receive(socket_adapter_t *self, FILE **pp_file) { .type_instance = "mcelog_status"}); } pthread_rwlock_unlock(&self->lock); - return (-1); + return -1; } if (!(poll_fd.revents & (POLLIN | POLLPRI))) { INFO(MCELOG_PLUGIN ": No data to read"); pthread_rwlock_unlock(&self->lock); - return (0); + return 0; } if ((*pp_file = fdopen(dup(self->sock_fd), "r")) == NULL) res = -1; pthread_rwlock_unlock(&self->lock); - return (res); + return res; } static void *poll_worker(__attribute__((unused)) void *arg) { - char errbuf[MCELOG_BUFF_SIZE]; mcelog_thread_running = 1; FILE **pp_file = calloc(1, sizeof(*pp_file)); if (pp_file == NULL) { - ERROR("mcelog: memory allocation failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("mcelog: memory allocation failed: %s", STRERRNO); pthread_exit((void *)1); } @@ -622,7 +611,7 @@ static void *poll_worker(__attribute__((unused)) void *arg) { mcelog_thread_running = 0; pthread_cleanup_pop(1); - return (NULL); + return NULL; } static int mcelog_init(void) { @@ -635,22 +624,22 @@ static int mcelog_init(void) { int err = pthread_mutex_init(&g_mcelog_config.dimms_lock, NULL); if (err < 0) { ERROR(MCELOG_PLUGIN ": plugin: failed to initialize cache lock"); - return (-1); + return -1; } if (socket_adapter.reinit(&socket_adapter) != 0) { ERROR(MCELOG_PLUGIN ": Cannot connect to client socket"); - return (-1); + return -1; } if (strlen(socket_adapter.unix_sock.sun_path)) { if (plugin_thread_create(&g_mcelog_config.tid, NULL, poll_worker, NULL, NULL) != 0) { ERROR(MCELOG_PLUGIN ": Error creating poll thread."); - return (-1); + return -1; } } - return (0); + return 0; } static int get_memory_machine_checks(void) { @@ -660,7 +649,7 @@ static int get_memory_machine_checks(void) { ERROR(MCELOG_PLUGIN ": SENT DUMP REQUEST FAILED"); else DEBUG(MCELOG_PLUGIN ": SENT DUMP REQUEST OK"); - return (ret); + return ret; } static int mcelog_read(__attribute__((unused)) user_data_t *ud) { @@ -669,7 +658,7 @@ static int mcelog_read(__attribute__((unused)) user_data_t *ud) { if (get_memory_machine_checks() != 0) ERROR(MCELOG_PLUGIN ": MACHINE CHECK INFO NOT AVAILABLE"); - return (0); + return 0; } static int mcelog_shutdown(void) { @@ -689,7 +678,7 @@ static int mcelog_shutdown(void) { pthread_mutex_destroy(&g_mcelog_config.dimms_lock); ret = socket_adapter.close(&socket_adapter) || ret; pthread_rwlock_destroy(&(socket_adapter.lock)); - return (-ret); + return -ret; } void module_register(void) {