From 39d723f4cb1d50b7ebb90c3251f0ebabad576412 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 26 Mar 2007 10:15:04 +0200 Subject: [PATCH] Replace all calls to `strerror' with `sstrerror' .. which internally uses the thread-safe function `strerror_r'. --- src/apcups.c | 10 ++- src/battery.c | 9 ++- src/collectd.c | 39 ++++++--- src/common.c | 19 ++++- src/common.h | 1 + src/cpu.c | 10 ++- src/cpufreq.c | 18 ++++- src/csv.c | 20 +++-- src/df.c | 5 +- src/dns.c | 3 +- src/email.c | 62 ++++++++++---- src/exec.c | 21 +++-- src/hddtemp.c | 22 +++-- src/iptables.c | 16 ++-- src/irq.c | 3 +- src/load.c | 20 ++++- src/mbmon.c | 18 ++++- src/memory.c | 15 +++- src/multimeter.c | 11 ++- src/network.c | 69 ++++++++++++---- src/nfs.c | 6 +- src/ntpd.c | 27 ++++--- src/ping.c | 8 +- src/plugin.c | 35 +++++++- src/plugin.h | 2 + src/processes.c | 4 +- src/rrdtool.c | 24 ++++-- src/sensors.c | 16 ++-- src/serial.c | 4 +- src/swap.c | 13 ++- src/traffic.c | 4 +- src/unixsock.c | 237 +++++++++++++++++++++++++++++++++++++++++++++--------- src/utils_mount.c | 20 +++-- src/vserver.c | 27 ++++--- src/wireless.c | 4 +- 35 files changed, 636 insertions(+), 186 deletions(-) diff --git a/src/apcups.c b/src/apcups.c index b5b939a1..fc94e7d7 100644 --- a/src/apcups.c +++ b/src/apcups.c @@ -186,7 +186,11 @@ static int net_open (char *host, char *service, int port) status = getaddrinfo (host, port_str, &ai_hints, &ai_return); if (status != 0) { - DEBUG ("getaddrinfo failed: %s", status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status)); + char errbuf[1024]; + DEBUG ("getaddrinfo failed: %s", + (status == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) + : gai_strerror (status)); return (-1); } @@ -213,7 +217,9 @@ static int net_open (char *host, char *service, int port) if (status != 0) /* `connect(2)' failed */ { - DEBUG ("connect failed: %s", strerror (errno)); + char errbuf[1024]; + DEBUG ("connect failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); close (sd); return (-1); } diff --git a/src/battery.c b/src/battery.c index 85bfc00a..53442ce6 100644 --- a/src/battery.c +++ b/src/battery.c @@ -457,7 +457,9 @@ static int battery_read (void) if ((dh = opendir ("/proc/acpi/battery")) == NULL) { - ERROR ("Cannot open `/proc/acpi/battery': %s", strerror (errno)); + char errbuf[1024]; + ERROR ("Cannot open `/proc/acpi/battery': %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -474,7 +476,10 @@ static int battery_read (void) if ((fh = fopen (filename, "r")) == NULL) { - ERROR ("Cannot open `%s': %s", filename, strerror (errno)); + char errbuf[1024]; + ERROR ("Cannot open `%s': %s", filename, + sstrerror (errno, errbuf, + sizeof (errbuf))); continue; } diff --git a/src/collectd.c b/src/collectd.c index e873adc2..9f3c62b7 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -91,7 +91,9 @@ static int change_basedir (const char *orig_dir) if (dir == NULL) { - ERROR ("strdup failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("strdup failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -111,21 +113,27 @@ static int change_basedir (const char *orig_dir) { if (mkdir (orig_dir, 0755) == -1) { + char errbuf[1024]; ERROR ("mkdir (%s): %s", orig_dir, - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } else if (chdir (orig_dir) == -1) { + char errbuf[1024]; ERROR ("chdir (%s): %s", orig_dir, - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } else { + char errbuf[1024]; ERROR ("chdir (%s): %s", orig_dir, - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } @@ -227,7 +235,10 @@ static int do_loop (void) { if (gettimeofday (&tv_next, NULL) < 0) { - ERROR ("gettimeofday failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("gettimeofday failed: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } tv_next.tv_sec += interval_g; @@ -241,8 +252,10 @@ static int do_loop (void) if (gettimeofday (&tv_now, NULL) < 0) { + char errbuf[1024]; ERROR ("gettimeofday failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } @@ -258,7 +271,10 @@ static int do_loop (void) { if (errno != EINTR) { - ERROR ("nanosleep failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("nanosleep failed: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } @@ -282,7 +298,9 @@ static int pidfile_create (void) if ((fh = fopen (file, "w")) == NULL) { - ERROR ("fopen (%s): %s", file, strerror (errno)); + char errbuf[1024]; + ERROR ("fopen (%s): %s", file, + sstrerror (errno, errbuf, sizeof (errbuf))); return (1); } @@ -396,7 +414,10 @@ int main (int argc, char **argv) if ((pid = fork ()) == -1) { /* error */ - fprintf (stderr, "fork: %s", strerror (errno)); + char errbuf[1024]; + fprintf (stderr, "fork: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (1); } else if (pid != 0) diff --git a/src/common.c b/src/common.c index ede2ea5c..967a82af 100644 --- a/src/common.c +++ b/src/common.c @@ -58,6 +58,15 @@ char *sstrdup (const char *s) return (r); } +/* Don't use the return value of `strerror_r', because the GNU-people got + * inventive there.. -octo */ +char *sstrerror (int errnum, char *buf, size_t buflen) +{ + buf[0] = '\0'; + strerror_r (errnum, buf, buflen); + return (buf); +} /* char *sstrerror */ + void *smalloc (size_t size) { void *r; @@ -372,13 +381,19 @@ int check_create_dir (const char *file_orig) { if (mkdir (dir, 0755) == -1) { - ERROR ("mkdir (%s): %s", dir, strerror (errno)); + char errbuf[1024]; + ERROR ("mkdir (%s): %s", dir, + sstrerror (errno, + errbuf, sizeof (errbuf))); return (-1); } } else { - ERROR ("stat (%s): %s", dir, strerror (errno)); + char errbuf[1024]; + ERROR ("stat (%s): %s", dir, + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } diff --git a/src/common.h b/src/common.h index 9164cc2d..310aa8a5 100644 --- a/src/common.h +++ b/src/common.h @@ -36,6 +36,7 @@ void sstrncpy(char *d, const char *s, int len); char *sstrdup(const char *s); void *smalloc(size_t size); +char *sstrerror (int errnum, char *buf, size_t buflen); /* * NAME diff --git a/src/cpu.c b/src/cpu.c index 6f18b4f4..1dcbdcd2 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -161,7 +161,9 @@ static int init (void) if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) { - WARNING ("cpu: sysctlbyname: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("cpu plugin: sysctlbyname: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -296,9 +298,10 @@ static int cpu_read (void) if ((fh = fopen ("/proc/stat", "r")) == NULL) { + char errbuf[1024]; plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: " "fopen (/proc/stat) failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -377,9 +380,10 @@ static int cpu_read (void) if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) { + char errbuf[1024]; plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: " "sysctlbyname failed: %s.", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return; } diff --git a/src/cpufreq.c b/src/cpufreq.c index 46e86598..5ceaacc8 100644 --- a/src/cpufreq.c +++ b/src/cpufreq.c @@ -117,19 +117,31 @@ static int cpufreq_read (void) if ((fp = fopen (filename, "r")) == NULL) { - WARNING ("cpufreq: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("cpufreq: fopen (%s): %s", filename, + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } if (fgets (buffer, 16, fp) == NULL) { - WARNING ("cpufreq: fgets: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("cpufreq: fgets: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); fclose (fp); return (-1); } if (fclose (fp)) - WARNING ("cpufreq: fclose: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("cpufreq: fclose: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); + } + /* You're seeing correctly: The file is reporting kHz values.. */ val = atoll (buffer) * 1000; diff --git a/src/csv.c b/src/csv.c index 62b6b2c7..4379fe5f 100644 --- a/src/csv.c +++ b/src/csv.c @@ -142,8 +142,10 @@ static int csv_create_file (const char *filename, const data_set_t *ds) csv = fopen (filename, "w"); if (csv == NULL) { + char errbuf[1024]; ERROR ("csv plugin: fopen (%s) failed: %s", - filename, strerror(errno)); + filename, + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -213,8 +215,10 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl) } else { - ERROR ("stat(%s) failed: %s", - filename, strerror (errno)); + char errbuf[1024]; + ERROR ("stat(%s) failed: %s", filename, + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } @@ -228,8 +232,9 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl) csv = fopen (filename, "a"); if (csv == NULL) { - ERROR ("csv plugin: fopen (%s) failed: %s", - filename, strerror (errno)); + char errbuf[1024]; + ERROR ("csv plugin: fopen (%s) failed: %s", filename, + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } csv_fd = fileno (csv); @@ -244,8 +249,9 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl) status = fcntl (csv_fd, F_SETLK, &fl); if (status != 0) { - ERROR ("csv plugin: flock (%s) failed: %s", - filename, strerror (errno)); + char errbuf[1024]; + ERROR ("csv plugin: flock (%s) failed: %s", filename, + sstrerror (errno, errbuf, sizeof (errbuf))); fclose (csv); return (-1); } diff --git a/src/df.c b/src/df.c index 6ec51fe3..48d58c3f 100644 --- a/src/df.c +++ b/src/df.c @@ -174,7 +174,10 @@ static int df_read (void) { if (STATANYFS (mnt_ptr->dir, &statbuf) < 0) { - ERROR ("statv?fs failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("statv?fs failed: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); continue; } diff --git a/src/dns.c b/src/dns.c index 11dc2874..255d1760 100644 --- a/src/dns.c +++ b/src/dns.c @@ -320,8 +320,9 @@ static int dns_init (void) (void *) 0); if (status != 0) { + char errbuf[1024]; ERROR ("dns plugin: pthread_create failed: %s", - strerror (status)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } diff --git a/src/email.c b/src/email.c index a0278336..c8c3982b 100644 --- a/src/email.c +++ b/src/email.c @@ -292,7 +292,9 @@ static char read_char (conn_t *src) FD_SET (src->socket, &fdset); if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) { - log_err ("select() failed: %s", strerror (errno)); + char errbuf[1024]; + log_err ("select() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return '\0'; } @@ -304,7 +306,9 @@ static char read_char (conn_t *src) errno = 0; if (0 > (len = read (src->socket, (void *)&ret, 1))) { if (EINTR != errno) { - log_err ("read() failed: %s", strerror (errno)); + char errbuf[1024]; + log_err ("read() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return '\0'; } } @@ -353,7 +357,9 @@ static char *read_line (conn_t *src) FD_SET (src->socket, &fdset); if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) { - log_err ("select() failed: %s", strerror (errno)); + char errbuf[1024]; + log_err ("select() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return NULL; } @@ -365,7 +371,9 @@ static char *read_line (conn_t *src) (void *)(src->buffer + src->idx), BUFSIZE - src->idx))) { if (EINTR != errno) { - log_err ("read() failed: %s", strerror (errno)); + char errbuf[1024]; + log_err ("read() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return NULL; } } @@ -437,13 +445,17 @@ static void *collect (void *arg) errno = 0; if (-1 == fcntl (connection->socket, F_GETFL, &flags)) { - log_err ("fcntl() failed: %s", strerror (errno)); + char errbuf[1024]; + log_err ("fcntl() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); loop = 0; } errno = 0; if (-1 == fcntl (connection->socket, F_SETFL, flags | O_NONBLOCK)) { - log_err ("fcntl() failed: %s", strerror (errno)); + char errbuf[1024]; + log_err ("fcntl() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); loop = 0; } } @@ -528,8 +540,10 @@ static void *open_connection (void *arg) /* create UNIX socket */ errno = 0; if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) { + char errbuf[1024]; disabled = 1; - log_err ("socket() failed: %s", strerror (errno)); + log_err ("socket() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } @@ -543,15 +557,19 @@ static void *open_connection (void *arg) if (-1 == bind (connector_socket, (struct sockaddr *)&addr, offsetof (struct sockaddr_un, sun_path) + strlen(addr.sun_path))) { + char errbuf[1024]; disabled = 1; - log_err ("bind() failed: %s", strerror (errno)); + log_err ("bind() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } errno = 0; if (-1 == listen (connector_socket, 5)) { + char errbuf[1024]; disabled = 1; - log_err ("listen() failed: %s", strerror (errno)); + log_err ("listen() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } @@ -566,7 +584,9 @@ static void *open_connection (void *arg) status = getgrnam_r (sock_group, &sg, grbuf, sizeof (grbuf), &grp); if (status != 0) { - log_warn ("getgrnam_r (%s) failed: %s", sock_group, strerror (status)); + char errbuf[1024]; + log_warn ("getgrnam_r (%s) failed: %s", sock_group, + sstrerror (errno, errbuf, sizeof (errbuf))); } else if (grp == NULL) { @@ -576,8 +596,12 @@ static void *open_connection (void *arg) { status = chown (SOCK_PATH, (uid_t) -1, grp->gr_gid); if (status != 0) + { + char errbuf[1024]; log_warn ("chown (%s, -1, %i) failed: %s", - SOCK_PATH, (int) grp->gr_gid, strerror (errno)); + SOCK_PATH, (int) grp->gr_gid, + sstrerror (errno, errbuf, sizeof (errbuf))); + } } } else /* geteuid != 0 */ @@ -587,7 +611,9 @@ static void *open_connection (void *arg) errno = 0; if (0 != chmod (SOCK_PATH, sock_perms)) { - log_warn ("chmod() failed: %s", strerror (errno)); + char errbuf[1024]; + log_warn ("chmod() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); } { /* initialize collector threads */ @@ -613,7 +639,9 @@ static void *open_connection (void *arg) if (0 != (err = pthread_create (&collectors[i]->thread, &ptattr, collect, collectors[i]))) { - log_err ("pthread_create() failed: %s", strerror (err)); + char errbuf[1024]; + log_err ("pthread_create() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); } } @@ -639,8 +667,10 @@ static void *open_connection (void *arg) errno = 0; if (-1 == (remote = accept (connector_socket, NULL, NULL))) { if (EINTR != errno) { + char errbuf[1024]; disabled = 1; - log_err ("accept() failed: %s", strerror (errno)); + log_err ("accept() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } } @@ -675,8 +705,10 @@ static int email_init (void) if (0 != (err = pthread_create (&connector, NULL, open_connection, NULL))) { + char errbuf[1024]; disabled = 1; - log_err ("pthread_create() failed: %s", strerror (err)); + log_err ("pthread_create() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } diff --git a/src/exec.c b/src/exec.c index fa0196b3..c7c72029 100644 --- a/src/exec.c +++ b/src/exec.c @@ -172,12 +172,14 @@ static void exec_child (program_list_t *pl) struct passwd *sp_ptr; struct passwd sp; char pwnambuf[2048]; + char errbuf[1024]; sp_ptr = NULL; status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr); if (status != 0) { - ERROR ("exec plugin: getpwnam_r failed: %s", strerror (status)); + ERROR ("exec plugin: getpwnam_r failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); exit (-1); } if (sp_ptr == NULL) @@ -196,7 +198,8 @@ static void exec_child (program_list_t *pl) status = setuid (uid); if (status != 0) { - ERROR ("exec plugin: setuid failed: %s", strerror (errno)); + ERROR ("exec plugin: setuid failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); exit (-1); } @@ -208,7 +211,8 @@ static void exec_child (program_list_t *pl) status = execlp (pl->exec, arg0, (char *) 0); - ERROR ("exec plugin: exec failed: %s", strerror (errno)); + ERROR ("exec plugin: exec failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); exit (-1); } /* void exec_child */ @@ -223,14 +227,18 @@ static int fork_child (program_list_t *pl) status = pipe (fd_pipe); if (status != 0) { - ERROR ("exec plugin: pipe failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("exec plugin: pipe failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } pl->pid = fork (); if (pl->pid < 0) { - ERROR ("exec plugin: fork failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("exec plugin: fork failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } else if (pl->pid == 0) @@ -269,8 +277,9 @@ static void *exec_read_one (void *arg) fh = fdopen (fd, "r"); if (fh == NULL) { + char errbuf[1024]; ERROR ("exec plugin: fdopen (%i) failed: %s", fd, - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); kill (pl->pid, SIGTERM); close (fd); pthread_exit ((void *) 1); diff --git a/src/hddtemp.c b/src/hddtemp.c index c2ff19e5..a7758613 100644 --- a/src/hddtemp.c +++ b/src/hddtemp.c @@ -133,9 +133,12 @@ static int hddtemp_query_daemon (char *buffer, int buffer_size) if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) { + char errbuf[1024]; ERROR ("hddtemp: getaddrinfo (%s, %s): %s", host, port, - ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return)); + (ai_return == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) + : gai_strerror (ai_return)); return (-1); } @@ -145,16 +148,18 @@ static int hddtemp_query_daemon (char *buffer, int buffer_size) /* create our socket descriptor */ if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0) { + char errbuf[1024]; ERROR ("hddtemp: socket: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); continue; } /* connect to the hddtemp daemon */ if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen)) { + char errbuf[1024]; DEBUG ("hddtemp: connect (%s, %s): %s", host, port, - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); close (fd); fd = -1; continue; @@ -181,11 +186,13 @@ static int hddtemp_query_daemon (char *buffer, int buffer_size) { if (status == -1) { + char errbuf[1024]; + if ((errno == EAGAIN) || (errno == EINTR)) continue; ERROR ("hddtemp: Error reading from socket: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); close (fd); return (-1); } @@ -377,9 +384,14 @@ static int hddtemp_init (void) } fclose (fh); } +#if COLLECT_DEBUG else + { + char errbuf[1024]; DEBUG ("Could not open /proc/partitions: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); + } +#endif /* COLLECT_DEBUG */ #endif /* KERNEL_LINUX */ return (0); diff --git a/src/iptables.c b/src/iptables.c index bc155593..47f0e550 100644 --- a/src/iptables.c +++ b/src/iptables.c @@ -119,7 +119,9 @@ static int iptables_config (const char *key, const char *value) value_copy = strdup (value); if (value_copy == NULL) { - ERROR ("strdup failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("strdup failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (1); } @@ -187,16 +189,20 @@ static int iptables_config (const char *key, const char *value) list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *)); if (list == NULL) { - ERROR ("realloc failed: %s", strerror (errno)); - return (1); + char errbuf[1024]; + ERROR ("realloc failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); } chain_list = list; final = (ip_chain_t *) malloc( sizeof(temp) ); if (final == NULL) { - ERROR ("malloc failed: %s", strerror (errno)); - return (1); + char errbuf[1024]; + ERROR ("malloc failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); } memcpy (final, &temp, sizeof (temp)); chain_list[chain_num] = final; diff --git a/src/irq.c b/src/irq.c index d44ac0b3..e39d0a57 100644 --- a/src/irq.c +++ b/src/irq.c @@ -178,8 +178,9 @@ static int irq_read (void) if ((fh = fopen ("/proc/interrupts", "r")) == NULL) { + char errbuf[1024]; WARNING ("irq plugin: fopen (/proc/interrupts): %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buffer, BUFSIZE, fh) != NULL) diff --git a/src/load.c b/src/load.c index aaba3a5f..b92e54f5 100644 --- a/src/load.c +++ b/src/load.c @@ -82,7 +82,11 @@ static int load_read (void) if (getloadavg (load, 3) == 3) load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]); else - WARNING ("load: getloadavg failed: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("load: getloadavg failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } /* #endif HAVE_GETLOADAVG */ #elif defined(KERNEL_LINUX) @@ -95,19 +99,27 @@ static int load_read (void) if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL) { - WARNING ("load: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("load: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return; } if (fgets (buffer, 16, loadavg) == NULL) { - WARNING ("load: fgets: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("load: fgets: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); fclose (loadavg); return; } if (fclose (loadavg)) - WARNING ("load: fclose: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("load: fclose: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } numfields = strsplit (buffer, fields, 8); diff --git a/src/mbmon.c b/src/mbmon.c index 241d7d04..fcb4cc74 100644 --- a/src/mbmon.c +++ b/src/mbmon.c @@ -139,9 +139,12 @@ static int mbmon_query_daemon (char *buffer, int buffer_size) if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) { + char errbuf[1024]; ERROR ("mbmon: getaddrinfo (%s, %s): %s", host, port, - ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return)); + (ai_return == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) + : gai_strerror (ai_return)); return (-1); } @@ -151,16 +154,20 @@ static int mbmon_query_daemon (char *buffer, int buffer_size) /* create our socket descriptor */ if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0) { + char errbuf[1024]; ERROR ("mbmon: socket: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); continue; } /* connect to the mbmon daemon */ if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen)) { + char errbuf[1024]; DEBUG ("mbmon: connect (%s, %s): %s", host, port, - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); close (fd); fd = -1; continue; @@ -187,11 +194,14 @@ static int mbmon_query_daemon (char *buffer, int buffer_size) { if (status == -1) { + char errbuf[1024]; + if ((errno == EAGAIN) || (errno == EINTR)) continue; ERROR ("mbmon: Error reading from socket: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); close (fd); return (-1); } diff --git a/src/memory.c b/src/memory.c index 91680279..66bcefae 100644 --- a/src/memory.c +++ b/src/memory.c @@ -214,8 +214,11 @@ static int memory_read (void) (void *) &sysctl_vals[i], &len, NULL, 0)) < 0) { + char errbuf[1024]; ERROR ("memory plugin: sysctlbyname (%s): %s", - sysctl_keys[i], strerror (errno)); + sysctl_keys[i], + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } DEBUG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]); @@ -245,7 +248,9 @@ static int memory_read (void) if ((fh = fopen ("/proc/meminfo", "r")) == NULL) { - WARNING ("memory: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("memory: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -273,7 +278,11 @@ static int memory_read (void) } if (fclose (fh)) - WARNING ("memory: fclose: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("memory: fclose: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } if (mem_used >= (mem_free + mem_buffered + mem_cached)) { diff --git a/src/multimeter.c b/src/multimeter.c index 04196bbe..d80c990c 100644 --- a/src/multimeter.c +++ b/src/multimeter.c @@ -81,8 +81,10 @@ static int multimeter_read_value(double *value) if (gettimeofday (&time_end, NULL) < 0) { + char errbuf[1024]; ERROR ("multimeter plugin: gettimeofday failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } time_end.tv_sec++; @@ -103,9 +105,11 @@ static int multimeter_read_value(double *value) if (gettimeofday (&time_now, NULL) < 0) { + char errbuf[1024]; ERROR ("multimeter plugin: " "gettimeofday failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1) @@ -157,9 +161,10 @@ static int multimeter_read_value(double *value) } else /* status == -1 */ { + char errbuf[1024]; ERROR ("multimeter plugin: " "select failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); break; } } diff --git a/src/network.c b/src/network.c index 6c6e0f60..b59b16d2 100644 --- a/src/network.c +++ b/src/network.c @@ -501,7 +501,9 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai) &network_config_ttl, sizeof (network_config_ttl)) == -1) { - ERROR ("setsockopt: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("setsockopt: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } } @@ -520,7 +522,10 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai) &network_config_ttl, sizeof (network_config_ttl)) == -1) { - ERROR ("setsockopt: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("setsockopt: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } @@ -536,7 +541,9 @@ static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai) if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1) { - ERROR ("bind: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("bind: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -555,14 +562,20 @@ static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai) if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof (loop)) == -1) { - ERROR ("setsockopt: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("setsockopt: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)) == -1) { - ERROR ("setsockopt: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("setsockopt: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } @@ -595,14 +608,20 @@ static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai) if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof (loop)) == -1) { - ERROR ("setsockopt: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("setsockopt: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof (mreq)) == -1) { - ERROR ("setsockopt: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("setsockopt: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } @@ -639,11 +658,12 @@ static sockent_t *network_create_socket (const char *node, ai_return = getaddrinfo (node, service, &ai_hints, &ai_list); if (ai_return != 0) { + char errbuf[1024]; ERROR ("getaddrinfo (%s, %s): %s", (node == NULL) ? "(null)" : node, (service == NULL) ? "(null)" : service, (ai_return == EAI_SYSTEM) - ? strerror (errno) + ? sstrerror (errno, errbuf, sizeof (errbuf)) : gai_strerror (ai_return)); return (NULL); } @@ -654,13 +674,19 @@ static sockent_t *network_create_socket (const char *node, if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL) { - ERROR ("malloc: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("malloc: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); continue; } if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL) { - ERROR ("malloc: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("malloc: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); free (se); continue; } @@ -677,7 +703,10 @@ static sockent_t *network_create_socket (const char *node, if (se->fd == -1) { - ERROR ("socket: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("socket: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); free (se->addr); free (se); continue; @@ -837,10 +866,11 @@ int network_receive (void) if (status <= 0) { + char errbuf[1024]; if (errno == EINTR) continue; ERROR ("poll failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -855,7 +885,10 @@ int network_receive (void) 0 /* no flags */); if (buffer_len < 0) { - ERROR ("recv failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("recv failed: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } @@ -886,10 +919,12 @@ static void network_send_buffer (const char *buffer, int buffer_len) (struct sockaddr *) se->addr, se->addrlen); if (status < 0) { + char errbuf[1024]; if (errno == EINTR) continue; ERROR ("network plugin: sendto failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); break; } @@ -1095,8 +1130,12 @@ static int network_init (void) receive_thread, NULL /* no argument */); if (status != 0) + { + char errbuf[1024]; ERROR ("network: pthread_create failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); + } } return (0); } /* int network_init */ diff --git a/src/nfs.c b/src/nfs.c index 81f07937..d54c8388 100644 --- a/src/nfs.c +++ b/src/nfs.c @@ -264,9 +264,10 @@ static void nfs_read_stats_file (FILE *fh, char *inst) values = (unsigned long long *) malloc (nfs2_procedures_names_num * sizeof (unsigned long long)); if (values == NULL) { + char errbuf[1024]; ERROR ("nfs plugin: malloc " "failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); continue; } @@ -300,9 +301,10 @@ static void nfs_read_stats_file (FILE *fh, char *inst) values = (unsigned long long *) malloc (nfs3_procedures_names_num * sizeof (unsigned long long)); if (values == NULL) { + char errbuf[1024]; ERROR ("nfs plugin: malloc " "failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); continue; } diff --git a/src/ntpd.c b/src/ntpd.c index 668e302d..dded360c 100644 --- a/src/ntpd.c +++ b/src/ntpd.c @@ -389,12 +389,12 @@ static int ntpd_connect (void) if ((status = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) { - DEBUG ("getaddrinfo (%s, %s): %s", - host, port, - status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status)); + char errbuf[1024]; ERROR ("ntpd plugin: getaddrinfo (%s, %s): %s", host, port, - status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status)); + (status == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) + : gai_strerror (status)); return (-1); } @@ -470,8 +470,9 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size, if (gettimeofday (&time_end, NULL) < 0) { + char errbuf[1024]; ERROR ("ntpd plugin: gettimeofday failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } time_end.tv_sec++; /* wait for a most one second */ @@ -481,8 +482,9 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size, { if (gettimeofday (&time_now, NULL) < 0) { + char errbuf[1024]; ERROR ("ntpd plugin: gettimeofday failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -502,9 +504,9 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size, if (status < 0) { - DEBUG ("poll failed: %s", strerror (errno)); + char errbuf[1024]; ERROR ("ntpd plugin: poll failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -522,7 +524,9 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size, if (status < 0) { - DEBUG ("recv(2) failed: %s", strerror (errno)); + char errbuf[1024]; + DEBUG ("recv(2) failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); DEBUG ("Closing socket #%i", sd); close (sd); sock_descr = sd = -1; @@ -874,9 +878,10 @@ static int ntpd_read (void) NULL, 0, 0 /* no flags */); if (status != 0) { + char errbuf[1024]; ERROR ("ntpd plugin: getnameinfo failed: %s", - status == EAI_SYSTEM - ? strerror (errno) + (status == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) : gai_strerror (status)); continue; } diff --git a/src/ping.c b/src/ping.c index 48a0ac87..3ac7aaba 100644 --- a/src/ping.c +++ b/src/ping.c @@ -143,15 +143,19 @@ static int ping_config (const char *key, const char *value) if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL) { + char errbuf[1024]; ERROR ("ping plugin: malloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (1); } if ((host = strdup (value)) == NULL) { + char errbuf[1024]; free (hl); ERROR ("ping plugin: strdup failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); return (1); } diff --git a/src/plugin.c b/src/plugin.c index 87fdcce6..e49963a6 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -23,6 +23,7 @@ #include +#include "common.h" #include "plugin.h" #include "configfile.h" #include "utils_llist.h" @@ -149,7 +150,11 @@ void plugin_set_dir (const char *dir) if (dir == NULL) plugindir = NULL; else if ((plugindir = strdup (dir)) == NULL) - ERROR ("strdup failed: %s", strerror (errno)); + { + char errbuf[1024]; + ERROR ("strdup failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } } #define BUFSIZE 512 @@ -180,7 +185,9 @@ int plugin_load (const char *type) if ((dh = opendir (dir)) == NULL) { - ERROR ("opendir (%s): %s", dir, strerror (errno)); + char errbuf[1024]; + ERROR ("opendir (%s): %s", dir, + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -197,7 +204,9 @@ int plugin_load (const char *type) if (lstat (filename, &statbuf) == -1) { - WARNING ("stat %s: %s", filename, strerror (errno)); + char errbuf[1024]; + WARNING ("stat %s: %s", filename, + sstrerror (errno, errbuf, sizeof (errbuf))); continue; } else if (!S_ISREG (statbuf.st_mode)) @@ -244,8 +253,9 @@ int plugin_register_read (const char *name, rf = (read_func_t *) malloc (sizeof (read_func_t)); if (rf == NULL) { + char errbuf[1024]; ERROR ("plugin_register_read: malloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -530,3 +540,20 @@ void plugin_relief (int level, complain_t *c, const char *format, ...) plugin_log (level, message); } + +const data_set_t *plugin_get_ds (const char *name) +{ + data_set_t *ds; + llentry_t *le; + + le = llist_search (list_data_set, name); + if (le == NULL) + { + DEBUG ("No such dataset registered: %s", name); + return (NULL); + } + + ds = (data_set_t *) le->value; + + return (ds); +} /* data_set_t *plugin_get_ds */ diff --git a/src/plugin.h b/src/plugin.h index cf2c3f77..f88bc48a 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -189,4 +189,6 @@ void plugin_log (int level, const char *format, ...); void plugin_complain (int level, complain_t *c, const char *format, ...); void plugin_relief (int level, complain_t *c, const char *format, ...); +const data_set_t *plugin_get_ds (const char *name); + #endif /* PLUGIN_H */ diff --git a/src/processes.c b/src/processes.c index e5c25126..7d918d4f 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1010,7 +1010,9 @@ static int ps_read (void) if ((proc = opendir ("/proc")) == NULL) { - ERROR ("Cannot open `/proc': %s", strerror (errno)); + char errbuf[1024]; + ERROR ("Cannot open `/proc': %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } diff --git a/src/rrdtool.c b/src/rrdtool.c index ec04642c..3bb8e075 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -181,8 +181,9 @@ static int ds_get (char ***ret, const data_set_t *ds) ds_def = (char **) malloc (ds->ds_num * sizeof (char *)); if (ds_def == NULL) { + char errbuf[1024]; ERROR ("rrdtool plugin: malloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } memset (ds_def, '\0', ds->ds_num * sizeof (char *)); @@ -286,7 +287,9 @@ static int rrd_create_file (char *filename, const data_set_t *ds) if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL) { - ERROR ("rrd_create failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("rrd_create failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -428,8 +431,9 @@ static rrd_cache_t *rrd_cache_insert (const char *filename, (rc->values_num + 1) * sizeof (char *)); if (rc->values == NULL) { + char errbuf[1024]; ERROR ("rrdtool plugin: realloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); if (cache != NULL) { void *cache_key = NULL; @@ -454,8 +458,9 @@ static rrd_cache_t *rrd_cache_insert (const char *filename, if (cache_key == NULL) { + char errbuf[1024]; ERROR ("rrdtool plugin: strdup failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); sfree (rc->values[0]); sfree (rc->values); sfree (rc); @@ -553,10 +558,11 @@ static void rrd_cache_flush (int timeout) (keys_num + 1) * sizeof (char *)); if (keys == NULL) { - DEBUG ("realloc failed: %s", strerror (errno)); + char errbuf[1024]; ERROR ("rrdtool plugin: " "realloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, + sizeof (errbuf))); avl_iterator_destroy (iter); return; } @@ -610,8 +616,10 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl) } else { - ERROR ("stat(%s) failed: %s", - filename, strerror (errno)); + char errbuf[1024]; + ERROR ("stat(%s) failed: %s", filename, + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } diff --git a/src/sensors.c b/src/sensors.c index 4bb544d5..544afd1b 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -256,8 +256,9 @@ static void sensors_load_conf (void) status = stat (conffile, &statbuf); if (status != 0) { - ERROR ("sensors plugin: stat (%s) failed: %s", - conffile, strerror (errno)); + char errbuf[1024]; + ERROR ("sensors plugin: stat (%s) failed: %s", conffile, + sstrerror (errno, errbuf, sizeof (errbuf))); sensors_config_mtime = 0; } @@ -276,8 +277,9 @@ static void sensors_load_conf (void) fh = fopen (conffile, "r"); if (fh == NULL) { - ERROR ("sensors plugin: fopen(%s) failed: %s", - conffile, strerror(errno)); + char errbuf[1024]; + ERROR ("sensors plugin: fopen(%s) failed: %s", conffile, + sstrerror (errno, errbuf, sizeof (errbuf))); return; } @@ -324,9 +326,9 @@ static void sensors_load_conf (void) if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL) { - DEBUG ("malloc: %s", strerror (errno)); - ERROR ("sensors plugin: malloc: %s", - strerror (errno)); + char errbuf[1024]; + ERROR ("sensors plugin: malloc: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); break; } diff --git a/src/serial.c b/src/serial.c index 0cfa3823..9fb26df1 100644 --- a/src/serial.c +++ b/src/serial.c @@ -80,7 +80,9 @@ static int serial_read (void) if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL && (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL) { - WARNING ("serial: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("serial: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } diff --git a/src/swap.c b/src/swap.c index 945c7580..6b0620cd 100644 --- a/src/swap.c +++ b/src/swap.c @@ -156,7 +156,9 @@ static int swap_read (void) if ((fh = fopen ("/proc/meminfo", "r")) == NULL) { - WARNING ("memory: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("memory: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -182,7 +184,11 @@ static int swap_read (void) } if (fclose (fh)) - WARNING ("memory: fclose: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("memory: fclose: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total)) return (-1); @@ -203,8 +209,9 @@ static int swap_read (void) if (swapctl (SC_AINFO, &ai) == -1) { + char errbuf[1024]; ERROR ("swap plugin: swapctl failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } diff --git a/src/traffic.c b/src/traffic.c index 7f624502..dc5daccd 100644 --- a/src/traffic.c +++ b/src/traffic.c @@ -304,7 +304,9 @@ static int traffic_read (void) if ((fh = fopen ("/proc/net/dev", "r")) == NULL) { - WARNING ("traffic: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("traffic: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } diff --git a/src/unixsock.c b/src/unixsock.c index 17956a17..82cfc604 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -83,6 +83,52 @@ static unsigned int cache_oldest = UINT_MAX; /* * Functions */ +static int parse_identifier (char *str, char **ret_host, + char **ret_plugin, char **ret_plugin_instance, + char **ret_type, char **ret_type_instance) +{ + char *hostname = NULL; + char *plugin = NULL; + char *plugin_instance = NULL; + char *type = NULL; + char *type_instance = NULL; + + hostname = str; + if (hostname == NULL) + return (-1); + + plugin = strchr (hostname, '/'); + if (plugin == NULL) + return (-1); + *plugin = '\0'; plugin++; + + type = strchr (plugin, '/'); + if (type == NULL) + return (-1); + *type = '\0'; type++; + + plugin_instance = strchr (plugin, '-'); + if (plugin_instance != NULL) + { + *plugin_instance = '\0'; + plugin_instance++; + } + + type_instance = strchr (type, '-'); + if (type_instance != NULL) + { + *type_instance = '\0'; + type_instance++; + } + + *ret_host = hostname; + *ret_plugin = plugin; + *ret_plugin_instance = plugin_instance; + *ret_type = type; + *ret_type_instance = type_instance; + return (0); +} /* int parse_identifier */ + static value_cache_t *cache_search (const char *name) { value_cache_t *vc; @@ -143,18 +189,20 @@ static int cache_insert (const data_set_t *ds, const value_list_t *vl) vc = (value_cache_t *) malloc (sizeof (value_cache_t)); if (vc == NULL) { + char errbuf[1024]; pthread_mutex_unlock (&cache_lock); ERROR ("unixsock plugin: malloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len); if (vc->gauge == NULL) { + char errbuf[1024]; pthread_mutex_unlock (&cache_lock); ERROR ("unixsock plugin: malloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); free (vc); return (-1); } @@ -162,9 +210,10 @@ static int cache_insert (const data_set_t *ds, const value_list_t *vl) vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len); if (vc->counter == NULL) { + char errbuf[1024]; pthread_mutex_unlock (&cache_lock); ERROR ("unixsock plugin: malloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); free (vc->gauge); free (vc); return (-1); @@ -351,8 +400,9 @@ static int us_open_socket (void) sock_fd = socket (PF_UNIX, SOCK_STREAM, 0); if (sock_fd < 0) { + char errbuf[1024]; ERROR ("unixsock plugin: socket failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -365,10 +415,10 @@ static int us_open_socket (void) status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa)); if (status != 0) { - DEBUG ("bind failed: %s; sa.sun_path = %s", - strerror (errno), sa.sun_path); - ERROR ("unixsock plugin: bind failed: %s", - strerror (errno)); + char errbuf[1024]; + sstrerror (errno, errbuf, sizeof (errbuf)); + DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path); + ERROR ("unixsock plugin: bind failed: %s", errbuf); close (sock_fd); sock_fd = -1; return (-1); @@ -377,8 +427,9 @@ static int us_open_socket (void) status = listen (sock_fd, 8); if (status != 0) { + char errbuf[1024]; ERROR ("unixsock plugin: listen failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); close (sock_fd); sock_fd = -1; return (-1); @@ -397,8 +448,9 @@ static int us_open_socket (void) status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g); if (status != 0) { - WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", - grpname, strerror (status)); + char errbuf[1024]; + WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname, + sstrerror (errno, errbuf, sizeof (errbuf))); break; } if (g == NULL) @@ -411,10 +463,11 @@ static int us_open_socket (void) if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (uid_t) -1, g->gr_gid) != 0) { + char errbuf[1024]; WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s", (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, (int) g->gr_gid, - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); } } while (0); @@ -423,7 +476,7 @@ static int us_open_socket (void) static int us_handle_getval (FILE *fh, char **fields, int fields_num) { - char *hostname = fields[1]; + char *hostname; char *plugin; char *plugin_instance; char *type; @@ -436,29 +489,11 @@ static int us_handle_getval (FILE *fh, char **fields, int fields_num) if (fields_num != 2) return (-1); - plugin = strchr (hostname, '/'); - if (plugin == NULL) - return (-1); - *plugin = '\0'; plugin++; - - type = strchr (plugin, '/'); - if (type == NULL) + status = parse_identifier (fields[1], &hostname, + &plugin, &plugin_instance, + &type, &type_instance); + if (status != 0) return (-1); - *type = '\0'; type++; - - plugin_instance = strchr (plugin, '-'); - if (plugin_instance != NULL) - { - *plugin_instance = '\0'; - plugin_instance++; - } - - type_instance = strchr (type, '-'); - if (type_instance != NULL) - { - *type_instance = '\0'; - type_instance++; - } status = cache_alloc_name (name, sizeof (name), hostname, plugin, plugin_instance, type, type_instance); @@ -498,6 +533,118 @@ static int us_handle_getval (FILE *fh, char **fields, int fields_num) return (0); } /* int us_handle_getval */ +static int us_handle_putval (FILE *fh, char **fields, int fields_num) +{ + char *hostname; + char *plugin; + char *plugin_instance; + char *type; + char *type_instance; + int status; + int i; + + const data_set_t *ds; + value_list_t vl = VALUE_LIST_INIT; + + char **value_ptr; + + if (fields_num != 3) + return (-1); + + status = parse_identifier (fields[1], &hostname, + &plugin, &plugin_instance, + &type, &type_instance); + if (status != 0) + return (-1); + + if ((strlen (hostname) > sizeof (vl.host)) + || (strlen (plugin) > sizeof (vl.plugin)) + || (strlen (plugin_instance) > sizeof (vl.plugin_instance)) + || (strlen (type_instance) > sizeof (vl.type_instance))) + return (-1); + + strcpy (vl.host, hostname); + strcpy (vl.plugin, plugin); + strcpy (vl.plugin_instance, plugin_instance); + strcpy (vl.type_instance, type_instance); + + { /* parse the time */ + char *t = fields[2]; + char *v = strchr (t, ':'); + if (v == NULL) + return (-1); + *v = '\0'; v++; + + vl.time = (time_t) atoi (t); + if (vl.time == 0) + vl.time = time (NULL); + + fields[2] = v; + } + + ds = plugin_get_ds (type); + if (ds == NULL) + return (-1); + + value_ptr = (char **) calloc (ds->ds_num, sizeof (char *)); + if (value_ptr == NULL) + return (-1); + + + { /* parse the value-list. It's colon-separated. */ + char *dummy; + char *ptr; + char *saveptr; + + i = 0; + dummy = fields[2]; + saveptr = NULL; + while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL) + { + dummy = NULL; + if (i >= ds->ds_num) + { + i = ds->ds_num + 1; + break; + } + value_ptr[i] = ptr; + i++; + } + + if (i != ds->ds_num) + { + free (value_ptr); + return (-1); + } + } /* done parsing the value-list */ + + vl.values_len = fields_num - 2; + vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t)); + if (vl.values == NULL) + { + free (value_ptr); + return (-1); + } + vl.values_len = ds->ds_num; + + for (i = 0; i < ds->ds_num; i++) + { + if (strcmp (value_ptr[i], "U") == 0) + vl.values[i].gauge = NAN; + else if (ds->ds[i].type == DS_TYPE_COUNTER) + vl.values[i].counter = atoll (value_ptr[i]); + else if (ds->ds[i].type == DS_TYPE_GAUGE) + vl.values[i].gauge = atof (value_ptr[i]); + } /* for (i = 2 .. fields_num) */ + sfree (value_ptr); + + plugin_dispatch_values (type, &vl); + + sfree (vl.values); + + return (0); +} /* int us_handle_putval */ + static void *us_handle_client (void *arg) { int fd; @@ -515,8 +662,9 @@ static void *us_handle_client (void *arg) fh = fdopen (fd, "r+"); if (fh == NULL) { + char errbuf[1024]; ERROR ("unixsock plugin: fdopen failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); close (fd); pthread_exit ((void *) 1); } @@ -548,6 +696,10 @@ static void *us_handle_client (void *arg) { us_handle_getval (fh, fields, fields_num); } + else if (strcasecmp (fields[0], "putval") == 0) + { + us_handle_putval (fh, fields, fields_num); + } else { fprintf (fh, "Unknown command: %s\n", fields[0]); @@ -577,11 +729,13 @@ static void *us_server_thread (void *arg) status = accept (sock_fd, NULL, NULL); if (status < 0) { + char errbuf[1024]; + if (errno == EINTR) continue; ERROR ("unixsock plugin: accept failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); close (sock_fd); sock_fd = -1; pthread_exit ((void *) 1); @@ -590,8 +744,9 @@ static void *us_server_thread (void *arg) remote_fd = (int *) malloc (sizeof (int)); if (remote_fd == NULL) { + char errbuf[1024]; WARNING ("unixsock plugin: malloc failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); close (status); continue; } @@ -605,8 +760,9 @@ static void *us_server_thread (void *arg) status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd); if (status != 0) { + char errbuf[1024]; WARNING ("unixsock plugin: pthread_create failed: %s", - strerror (status)); + sstrerror (errno, errbuf, sizeof (errbuf))); close (*remote_fd); free (remote_fd); continue; @@ -647,8 +803,9 @@ static int us_init (void) status = pthread_create (&listen_thread, NULL, us_server_thread, NULL); if (status != 0) { + char errbuf[1024]; ERROR ("unixsock plugin: pthread_create failed: %s", - strerror (status)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } diff --git a/src/utils_mount.c b/src/utils_mount.c index 61eb6d1c..eee2eb6c 100644 --- a/src/utils_mount.c +++ b/src/utils_mount.c @@ -381,7 +381,9 @@ static cu_mount_t *cu_mount_listmntent (void) struct tabmntent *mntlist; if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) { - DEBUG("calling listmntent() failed: %s", strerror(errno)); + char errbuf[1024]; + DEBUG("calling listmntent() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); } for(p = mntlist; p; p = p->next) { @@ -450,7 +452,9 @@ static cu_mount_t *cu_mount_getfsstat (void) /* Get the number of mounted file systems */ if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1) { - DEBUG ("getv?fsstat failed: %s", strerror (errno)); + char errbuf[1024]; + DEBUG ("getv?fsstat failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (NULL); } @@ -463,7 +467,9 @@ static cu_mount_t *cu_mount_getfsstat (void) * manpage.. -octo */ if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1) { - DEBUG ("getv?fsstat failed: %s", strerror (errno)); + char errbuf[1024]; + DEBUG ("getv?fsstat failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); free (buf); return (NULL); } @@ -516,7 +522,9 @@ static cu_mount_t *cu_mount_gen_getmntent (void) if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL) { - ERROR ("fopen (%s): %s", COLLECTD_MNTTAB, strerror (errno)); + char errbuf[1024]; + ERROR ("fopen (%s): %s", COLLECTD_MNTTAB, + sstrerror (errno, errbuf, sizeof (errbuf))); return (NULL); } @@ -571,7 +579,9 @@ static cu_mount_t *cu_mount_getmntent (void) if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL) { - ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB, strerror (errno)); + char errbuf[1024]; + ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB, + sstrerror (errno, errbuf, sizeof (errbuf))); return (NULL); } diff --git a/src/vserver.c b/src/vserver.c index fef45587..e4742e91 100644 --- a/src/vserver.c +++ b/src/vserver.c @@ -178,17 +178,14 @@ static int vserver_read (void) DIR *proc; struct dirent *dent; /* 42 */ - static complain_t complain_obj; - errno = 0; if (NULL == (proc = opendir (PROCDIR))) { - plugin_complain (LOG_ERR, &complain_obj, "vserver plugin: " - "fopen (%s) failed: %s", PROCDIR, strerror (errno)); + char errbuf[1024]; + ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } - plugin_relief (LOG_NOTICE, &complain_obj, "vserver plugin: " - "fopen (%s) succeeded.", PROCDIR); while (NULL != (dent = readdir (proc))) { @@ -213,7 +210,11 @@ static int vserver_read (void) continue; if (NULL == (fh = fopen (file, "r"))) - ERROR ("Cannot open '%s': %s", file, strerror (errno)); + { + char errbuf[1024]; + ERROR ("Cannot open '%s': %s", file, + sstrerror (errno, errbuf, sizeof (errbuf))); + } while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh))) { @@ -256,7 +257,11 @@ static int vserver_read (void) continue; if (NULL == (fh = fopen (file, "r"))) - ERROR ("Cannot open '%s': %s", file, strerror (errno)); + { + char errbuf[1024]; + ERROR ("Cannot open '%s': %s", file, + sstrerror (errno, errbuf, sizeof (errbuf))); + } while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh))) { @@ -304,7 +309,11 @@ static int vserver_read (void) continue; if (NULL == (fh = fopen (file, "r"))) - ERROR ("Cannot open '%s': %s", file, strerror (errno)); + { + char errbuf[1024]; + ERROR ("Cannot open '%s': %s", file, + sstrerror (errno, errbuf, sizeof (errbuf))); + } while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh))) { diff --git a/src/wireless.c b/src/wireless.c index a7040475..8ce7854c 100644 --- a/src/wireless.c +++ b/src/wireless.c @@ -112,7 +112,9 @@ static int wireless_read (void) /* there are a variety of names for the wireless device */ if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL) { - WARNING ("wireless: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("wireless: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } -- 2.11.0