From d1660672c0ba88e75b27af85449a0d39abed8408 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Thu, 9 Jan 2014 15:15:53 +0100 Subject: Use re-entrant version of strerror() for thread safety --- src/account/indication_common.c | 3 ++- src/fan/fan.c | 3 ++- src/globals.h | 1 + src/hardware/lsblk.c | 3 ++- src/hardware/smartctl.c | 3 ++- src/hardware/sysfs.c | 9 ++++--- src/hardware/utils.c | 5 ++-- src/journald/LMI_JournalLogRecordProvider.c | 22 +++++++++------- src/journald/LMI_JournalRecordInLogProvider.c | 5 ++-- src/journald/instutil.c | 36 ++++++++++++++++----------- src/logicalfile/LMI_UnixDirectoryProvider.c | 6 +++-- 11 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/account/indication_common.c b/src/account/indication_common.c index f5caa75..648a20f 100644 --- a/src/account/indication_common.c +++ b/src/account/indication_common.c @@ -105,6 +105,7 @@ bool watcher(AccountIndication *ind, void **data) { struct timespec curr_pwd, curr_grp; char buffer[BUF_LEN]; + char errbuf[STRERROR_BUF_LEN]; if (ind->inotify_fd < 0) return false; @@ -112,7 +113,7 @@ bool watcher(AccountIndication *ind, void **data) do { int len = 0, i = 0; if ((len = read(ind->inotify_fd, buffer, BUF_LEN)) < 0 || len > (int) BUF_LEN) { - warn("account watcher: error reading from inotify fd: %s", strerror(errno)); + warn("account watcher: error reading from inotify fd: %s", strerror_r(errno, errbuf, sizeof(errbuf))); watcher_destroy(ind); watcher_init(ind); return false; diff --git a/src/fan/fan.c b/src/fan/fan.c index 5e36cb0..8296d61 100644 --- a/src/fan/fan.c +++ b/src/fan/fan.c @@ -109,11 +109,12 @@ static cim_fan_error_t reload_config_file(char const * fp) { * @return 0 on success */ FILE *config_file = NULL; int err; + char errbuf[STRERROR_BUF_LEN]; if (fp) { if (!(config_file = fopen(fp, "r"))) { error("Cound not open config file \"%s\": %s\n", - fp, strerror(errno)); + fp, strerror_r(errno, errbuf, sizeof(errbuf))); } } err = sensors_init(config_file); diff --git a/src/globals.h b/src/globals.h index 094602f..5789aae 100644 --- a/src/globals.h +++ b/src/globals.h @@ -32,5 +32,6 @@ #define ORGID "LMI" +#define STRERROR_BUF_LEN 1024 #endif diff --git a/src/hardware/lsblk.c b/src/hardware/lsblk.c index 611ab5c..45ba051 100644 --- a/src/hardware/lsblk.c +++ b/src/hardware/lsblk.c @@ -96,6 +96,7 @@ short lsblk_get_hdds(LsblkHdd **hdds, unsigned *hdds_nb) unsigned i, curr_hdd = 0, buffer_size = 0; char **buffer = NULL, *path, *type; struct stat sb; + char errbuf[STRERROR_BUF_LEN]; lsblk_free_hdds(hdds, hdds_nb); @@ -130,7 +131,7 @@ short lsblk_get_hdds(LsblkHdd **hdds, unsigned *hdds_nb) } if (stat(path, &sb) != 0) { warn("Stat() call on file \"%s\" failed: %s", - path, strerror(errno)); + path, strerror_r(errno, errbuf, sizeof(errbuf))); free(path); continue; } diff --git a/src/hardware/smartctl.c b/src/hardware/smartctl.c index 349f198..874475a 100644 --- a/src/hardware/smartctl.c +++ b/src/hardware/smartctl.c @@ -112,6 +112,7 @@ short scan_smctlhdd_devices(SmartctlHdd **hdds, unsigned *hdds_nb) unsigned i, curr_hdd = 0, buffer_size = 0, sec_buffer_size = 0; char **buffer = NULL, **sec_buffer = NULL; struct stat sb; + char errbuf[STRERROR_BUF_LEN]; smartctl_free_hdds(hdds, hdds_nb); @@ -147,7 +148,7 @@ short scan_smctlhdd_devices(SmartctlHdd **hdds, unsigned *hdds_nb) if (stat(sec_buffer[0], &sb) != 0) { warn("Stat() call on file \"%s\" failed: %s", - sec_buffer[0], strerror(errno)); + sec_buffer[0], strerror_r(errno, errbuf, sizeof(errbuf))); free_2d_buffer(&sec_buffer, &sec_buffer_size); continue; } diff --git a/src/hardware/sysfs.c b/src/hardware/sysfs.c index aab2ba6..5ed34ad 100644 --- a/src/hardware/sysfs.c +++ b/src/hardware/sysfs.c @@ -26,13 +26,14 @@ short path_get_unsigned(const char *path, unsigned *result) short ret = -1; unsigned buffer_size = 0; char **buffer = NULL; + char errbuf[STRERROR_BUF_LEN]; if (read_file(path, &buffer, &buffer_size) != 0 || buffer_size < 1) { goto done; } if (sscanf(buffer[0], "%u", result) != 1) { warn("Failed to parse file: \"%s\"; Error: %s", - path, strerror(errno)); + path, strerror_r(errno, errbuf, sizeof(errbuf))); goto done; } @@ -160,6 +161,7 @@ short sysfs_get_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb) DmiProcessor *dmi_cpus = NULL; unsigned dmi_cpus_nb = 0, cpus_nb = 0; LscpuProcessor lscpu; + char errbuf[STRERROR_BUF_LEN]; *caches_nb = 0; @@ -186,7 +188,7 @@ short sysfs_get_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb) dir = opendir(cache_dir); if (!dir) { warn("Failed to read directory: \"%s\"; Error: %s", - cache_dir, strerror(errno)); + cache_dir, strerror_r(errno, errbuf, sizeof(errbuf))); goto done; } while (readdir(dir)) { @@ -344,6 +346,7 @@ short sysfs_get_sizes_of_hugepages(unsigned **sizes, unsigned *sizes_nb) { short ret = -1; DIR *dir = NULL; + char errbuf[STRERROR_BUF_LEN]; *sizes_nb = 0; *sizes = NULL; @@ -353,7 +356,7 @@ short sysfs_get_sizes_of_hugepages(unsigned **sizes, unsigned *sizes_nb) dir = opendir(sizes_dir); if (!dir) { warn("Failed to read directory: \"%s\"; Error: %s", - sizes_dir, strerror(errno)); + sizes_dir, strerror_r(errno, errbuf, sizeof(errbuf))); goto done; } while (readdir(dir)) { diff --git a/src/hardware/utils.c b/src/hardware/utils.c index 305cc1e..fdce75d 100644 --- a/src/hardware/utils.c +++ b/src/hardware/utils.c @@ -132,6 +132,7 @@ short run_command(const char *command, char ***buffer, unsigned *buffer_size) { FILE *fp = NULL; short ret = -1; + char errbuf[STRERROR_BUF_LEN]; /* if command is empty */ if (!command || strlen(command) < 1) { @@ -144,7 +145,7 @@ short run_command(const char *command, char ***buffer, unsigned *buffer_size) fp = popen(command, "r"); if (!fp) { warn("Failed to run command: \"%s\"; Error: %s", - command, strerror(errno)); + command, strerror_r(errno, errbuf, sizeof(errbuf))); goto done; } @@ -159,7 +160,7 @@ done: int ret_code = pclose(fp); if (ret_code == -1) { warn("Failed to run command: \"%s\"; Error: %s", - command, strerror(errno)); + command, strerror_r(errno, errbuf, sizeof(errbuf))); if (ret == 0) { ret = -1; } diff --git a/src/journald/LMI_JournalLogRecordProvider.c b/src/journald/LMI_JournalLogRecordProvider.c index e9a26dc..82a6fda 100644 --- a/src/journald/LMI_JournalLogRecordProvider.c +++ b/src/journald/LMI_JournalLogRecordProvider.c @@ -42,12 +42,13 @@ static void LMI_JournalLogRecordInitialize(const CMPIContext *ctx) { sd_journal *journal; int r; + char errbuf[STRERROR_BUF_LEN]; lmi_init(JOURNAL_CIM_LOG_NAME, _cb, ctx, provider_config_defaults); r = sd_journal_open(&journal, 0); if (r < 0) { - error("Error opening journal: %s\n", strerror(-r)); + error("Error opening journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return; } journal_iter = journal; @@ -76,16 +77,17 @@ static CMPIStatus LMI_JournalLogRecordEnumInstanceNames( int r; LMI_JournalLogRecordRef log_record_ref; unsigned long count = 0; + char errbuf[STRERROR_BUF_LEN]; /* Open our own journal instance to prevent losing cursor position in the global instance */ r = sd_journal_open(&journal, 0); if (r < 0) - KReturn2(_cb, ERR_FAILED, "Error opening journal: %s\n", strerror(-r)); + KReturn2(_cb, ERR_FAILED, "Error opening journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); r = sd_journal_seek_tail(journal); if (r < 0) { sd_journal_close(journal); - KReturn2(_cb, ERR_NOT_FOUND, "Failed to seek to the end of the journal: %s\n", strerror(-r)); + KReturn2(_cb, ERR_NOT_FOUND, "Failed to seek to the end of the journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); } SD_JOURNAL_FOREACH_BACKWARDS(journal) { @@ -143,6 +145,7 @@ static CMPIStatus LMI_JournalLogRecordGetInstance( { LMI_JournalLogRecord log_record; int r; + char errbuf[STRERROR_BUF_LEN]; LMI_JournalLogRecord_InitFromObjectPath(&log_record, _cb, cop); @@ -151,15 +154,15 @@ static CMPIStatus LMI_JournalLogRecordGetInstance( r = sd_journal_seek_cursor(journal_iter, log_record.RecordID.chars); if (r < 0) - KReturn2(_cb, ERR_NOT_FOUND, "Failed to seek to the requested position: %s\n", strerror(-r)); + KReturn2(_cb, ERR_NOT_FOUND, "Failed to seek to the requested position: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); r = sd_journal_next(journal_iter); if (r < 0) - KReturn2(_cb, ERR_FAILED, "Failed to seek next to the cursor: %s\n", strerror(-r)); + KReturn2(_cb, ERR_FAILED, "Failed to seek next to the cursor: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); r = create_LMI_JournalLogRecord(journal_iter, &log_record, _cb); if (r <= 0) - KReturn2(_cb, ERR_FAILED, "Failed to create instance: %s\n", strerror(-r)); + KReturn2(_cb, ERR_FAILED, "Failed to create instance: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); KReturnInstance(cr, log_record); KReturn(OK); @@ -181,7 +184,7 @@ get_string_property(const char *property_name, const CMPIInstance* ci) #define J_RESULT_CHECK(r,j,msg) \ if (r < 0) { \ sd_journal_close(j); \ - KReturn2(_cb, ERR_FAILED, msg ": %s\n", strerror(-r)); \ + KReturn2(_cb, ERR_FAILED, msg ": %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); \ } static CMPIStatus LMI_JournalLogRecordCreateInstance( @@ -199,6 +202,7 @@ static CMPIStatus LMI_JournalLogRecordCreateInstance( int r; CMPIrc rc; bool found; + char errbuf[STRERROR_BUF_LEN]; cr_cl_n = get_string_property("CreationClassName", ci); log_cr_cl_n = get_string_property("LogCreationClassName", ci); @@ -215,7 +219,7 @@ static CMPIStatus LMI_JournalLogRecordCreateInstance( r = sd_journal_open(&journal, 0); if (r < 0) { - KReturn2(_cb, ERR_FAILED, "Error opening journal: %s\n", strerror(-r)); + KReturn2(_cb, ERR_FAILED, "Error opening journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); } r = sd_journal_seek_tail(journal); @@ -260,7 +264,7 @@ static CMPIStatus LMI_JournalLogRecordCreateInstance( r = create_LMI_JournalLogRecord(journal, &log_record, _cb); if (r <= 0) { sd_journal_close(journal); - KReturn2(_cb, ERR_FAILED, "Failed to create instance: %s\n", strerror(-r)); + KReturn2(_cb, ERR_FAILED, "Failed to create instance: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); } CMReturnObjectPath(cr, LMI_JournalLogRecord_ToObjectPath(&log_record, NULL)); diff --git a/src/journald/LMI_JournalRecordInLogProvider.c b/src/journald/LMI_JournalRecordInLogProvider.c index 9be0f19..33a50e5 100644 --- a/src/journald/LMI_JournalRecordInLogProvider.c +++ b/src/journald/LMI_JournalRecordInLogProvider.c @@ -68,6 +68,7 @@ static CMPIStatus LMI_JournalRecordInLogEnumInstances( int r; CMPIStatus rc; unsigned long count = 0; + char errbuf[STRERROR_BUF_LEN]; LMI_JournalMessageLogRef_Init(&message_log_ref, _cb, ns); LMI_JournalMessageLogRef_Set_CreationClassName(&message_log_ref, LMI_JournalMessageLog_ClassName); @@ -78,12 +79,12 @@ static CMPIStatus LMI_JournalRecordInLogEnumInstances( r = sd_journal_open(&journal, 0); if (r < 0) - KReturn2(_cb, ERR_FAILED, "Error opening journal: %s\n", strerror(-r)); + KReturn2(_cb, ERR_FAILED, "Error opening journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); r = sd_journal_seek_tail(journal); if (r < 0) { sd_journal_close(journal); - KReturn2(_cb, ERR_NOT_FOUND, "Failed to seek to the end of the journal: %s\n", strerror(-r)); + KReturn2(_cb, ERR_NOT_FOUND, "Failed to seek to the end of the journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); } SD_JOURNAL_FOREACH_BACKWARDS(journal) { diff --git a/src/journald/instutil.c b/src/journald/instutil.c index 44b7ddb..480751a 100644 --- a/src/journald/instutil.c +++ b/src/journald/instutil.c @@ -275,19 +275,21 @@ int match_journal_record(sd_journal *j, const char *message, const char *code_fu void ind_init() { + char errbuf[STRERROR_BUF_LEN]; + if (ind_journal == NULL) { sd_journal *journal; int r; r = sd_journal_open(&journal, 0); if (r < 0) { - error("ind_init(): Error opening journal: %s\n", strerror(-r)); + error("ind_init(): Error opening journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return; } r = sd_journal_seek_tail(journal); if (r < 0) { - error("ind_init(): Error seeking to the end of the journal: %s\n", strerror(-r)); + error("ind_init(): Error seeking to the end of the journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); sd_journal_close(journal); return; } @@ -295,7 +297,7 @@ void ind_init() /* need to position the marker one step before EOF or otherwise the next sd_journal_next() call will overflow to the beginning */ r = sd_journal_previous(journal); if (r < 0) { - error("ind_init(): Error seeking to the end of the journal: %s\n", strerror(-r)); + error("ind_init(): Error seeking to the end of the journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); sd_journal_close(journal); return; } @@ -315,6 +317,7 @@ void ind_destroy() bool ind_watcher(void **data) { int r; + char errbuf[STRERROR_BUF_LEN]; if (ind_journal == NULL) { error("ind_watcher(): indications have not been initialized yet or error occurred previously\n"); @@ -332,7 +335,7 @@ bool ind_watcher(void **data) r = sd_journal_wait(ind_journal, (uint64_t) -1); } if (r < 0) { - warn("ind_watcher(): Error while waiting for new record: %s\n", strerror(-r)); + warn("ind_watcher(): Error while waiting for new record: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return false; } if (r == SD_JOURNAL_INVALIDATE) { @@ -352,13 +355,14 @@ bool ind_gather(const IMManager *manager, CMPIInstance **old, CMPIInstance **new int r; LMI_JournalLogRecord log_record; CMPIStatus st; + char errbuf[STRERROR_BUF_LEN]; g_return_val_if_fail(data != NULL, false); journal = data; r = sd_journal_next(journal); if (r < 0) { - error("ind_gather(): Failed to iterate to next entry: %s\n", strerror(-r)); + error("ind_gather(): Failed to iterate to next entry: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return false; } if (r == 0) { @@ -370,7 +374,7 @@ bool ind_gather(const IMManager *manager, CMPIInstance **old, CMPIInstance **new LMI_JournalLogRecord_Init(&log_record, manager->broker, "root/cimv2"); r = create_LMI_JournalLogRecord(journal, &log_record, manager->broker); if (r <= 0) { - error("ind_gather(): Failed to create instance: %s\n", strerror(-r)); + error("ind_gather(): Failed to create instance: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return false; } @@ -431,13 +435,14 @@ journal_iter_new(const gchar *req_cursor, sd_journal **journal_out) char *cursor; sd_journal *journal; int r; + char errbuf[STRERROR_BUF_LEN]; if (journal_out) *journal_out = NULL; r = sd_journal_open(&journal, 0); if (r < 0) { - error("Error opening journal: %s\n", strerror(-r)); + error("Error opening journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return NULL; } @@ -447,21 +452,21 @@ journal_iter_new(const gchar *req_cursor, sd_journal **journal_out) r = sd_journal_seek_head(journal); if (r < 0) { - error("Error seeking to the requested journal position: %s\n", strerror(-r)); + error("Error seeking to the requested journal position: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); sd_journal_close(journal); return NULL; } r = sd_journal_next(journal); if (r < 0) { - error("Error stepping next in the journal: %s\n", strerror(-r)); + error("Error stepping next in the journal: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); sd_journal_close(journal); return NULL; } r = sd_journal_get_cursor(journal, &cursor); if (r < 0) { - error("Error getting current cursor: %s\n", strerror(-r)); + error("Error getting current cursor: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); sd_journal_close(journal); return NULL; } @@ -563,10 +568,11 @@ update_iter(gchar **iter_id, sd_journal *journal) gchar *iter_id_short; char *cursor; int r; + char errbuf[STRERROR_BUF_LEN]; r = sd_journal_get_cursor(journal, &cursor); if (r < 0) { - error("Error getting current cursor: %s\n", strerror(-r)); + error("Error getting current cursor: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return false; } @@ -581,6 +587,7 @@ bool journal_iter_seek(gchar **iter_id, sd_journal *journal, gint64 position) { int r; + char errbuf[STRERROR_BUF_LEN]; g_return_val_if_fail(journal != NULL, false); @@ -595,7 +602,7 @@ journal_iter_seek(gchar **iter_id, sd_journal *journal, gint64 position) r = sd_journal_previous_skip(journal, -position); if (r < 0) { - error("Error seeking to the requested position: %s\n", strerror(-r)); + error("Error seeking to the requested position: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return false; } @@ -612,20 +619,21 @@ journal_iter_get_data(gchar **iter_id, sd_journal *journal, gboolean step_next) { gchar *d; int r; + char errbuf[STRERROR_BUF_LEN]; g_return_val_if_fail(journal != NULL, false); /* Construct the message */ r = get_record_message(journal, TRUE, &d); if (r < 0) { - error("Error getting record message: %s\n", strerror(-r)); + error("Error getting record message: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); return NULL; } if (step_next) { r = sd_journal_next(journal); if (r < 0) { - error("Error advancing to the next record: %s\n", strerror(-r)); + error("Error advancing to the next record: %s\n", strerror_r(-r, errbuf, sizeof(errbuf))); g_free(d); return NULL; } diff --git a/src/logicalfile/LMI_UnixDirectoryProvider.c b/src/logicalfile/LMI_UnixDirectoryProvider.c index af4be57..0f5c69b 100644 --- a/src/logicalfile/LMI_UnixDirectoryProvider.c +++ b/src/logicalfile/LMI_UnixDirectoryProvider.c @@ -93,7 +93,8 @@ static CMPIStatus LMI_UnixDirectoryCreateInstance( if (mkdir(path, 0777) < 0) { char errmsg[BUFLEN]; - snprintf(errmsg, BUFLEN, "Can't mkdir: %s (%s)", path, strerror(errno)); + char strerrmsg[STRERROR_BUF_LEN]; + snprintf(errmsg, BUFLEN, "Can't mkdir: %s (%s)", path, strerror_r(errno, strerrmsg, sizeof(strerrmsg))); CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg); } @@ -121,7 +122,8 @@ static CMPIStatus LMI_UnixDirectoryDeleteInstance( if (rmdir(path) < 0) { char errmsg[BUFLEN]; - snprintf(errmsg, BUFLEN, "Can't rmdir: %s (%s)", path, strerror(errno)); + char strerrmsg[STRERROR_BUF_LEN]; + snprintf(errmsg, BUFLEN, "Can't rmdir: %s (%s)", path, strerror_r(errno, strerrmsg, sizeof(strerrmsg))); CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg); } -- cgit