From: Alberto Leiva Popper Date: Tue, 9 Nov 2021 19:08:54 +0000 (-0600) Subject: Log: Remove ##__VA_ARGS__ and -Wlogical-op X-Git-Tag: 1.5.4~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cb337229c89d1d809ddb88b127e341d7d933d212;p=thirdparty%2FFORT-validator.git Log: Remove ##__VA_ARGS__ and -Wlogical-op Improves portability. F1xes #64. --- diff --git a/src/asn1/signed_data.c b/src/asn1/signed_data.c index 16e36b00..41e4f816 100644 --- a/src/asn1/signed_data.c +++ b/src/asn1/signed_data.c @@ -279,8 +279,10 @@ validate(struct SignedData *sdata, ANY_t *sdata_encoded, /* rfc6488#section-3.1.b */ error = asn_INTEGER2ulong(&sdata->version, &version); if (error) { - if (errno) - pr_val_errno(errno, "Error converting SignedData version"); + if (errno) { + pr_val_err("Error converting SignedData version: %s", + strerror(errno)); + } return pr_val_err("The SignedData version isn't a valid unsigned long"); } if (version != 3) { @@ -329,8 +331,10 @@ validate(struct SignedData *sdata, ANY_t *sdata_encoded, error = asn_INTEGER2ulong(&sinfo->version, &version); if (error) { - if (errno) - pr_val_errno(errno, "Error converting SignerInfo version"); + if (errno) { + pr_val_err("Error converting SignerInfo version: %s", + strerror(errno)); + } return pr_val_err("The SignerInfo version isn't a valid unsigned long"); } if (version != 3) { diff --git a/src/common.c b/src/common.c index 342d0d8b..2384f3ea 100644 --- a/src/common.c +++ b/src/common.c @@ -86,20 +86,21 @@ process_file(char const *dir_name, char const *file_name, char const *file_ext, /* Get the full file path */ tmp = strdup(dir_name); if (tmp == NULL) - return -pr_op_errno(errno, "Couldn't create temporal char"); + return pr_enomem(); tmp = realloc(tmp, strlen(tmp) + 1 + strlen(file_name) + 1); if (tmp == NULL) - return -pr_op_errno(errno, "Couldn't reallocate temporal char"); + return pr_enomem(); strcat(tmp, "/"); strcat(tmp, file_name); fullpath = realpath(tmp, NULL); if (fullpath == NULL) { + error = errno; + pr_op_err("Error getting real path for file '%s' at directory '%s': %s", + dir_name, file_name, strerror(error)); free(tmp); - return -pr_op_errno(errno, - "Error getting real path for file '%s' at dir '%s'", - dir_name, file_name); + return -error; } error = cb(fullpath, arg); @@ -118,7 +119,9 @@ process_dir_files(char const *location, char const *file_ext, bool empty_err, dir_loc = opendir(location); if (dir_loc == NULL) { - error = -pr_op_errno(errno, "Couldn't open dir %s", location); + error = -errno; + pr_op_err("Couldn't open directory '%s': %s", location, + strerror(-error)); goto end; } @@ -158,8 +161,12 @@ process_file_or_dir(char const *location, char const *file_ext, bool empty_err, int error; error = stat(location, &attr); - if (error) - return pr_op_errno(errno, "Error reading path '%s'", location); + if (error) { + error = errno; + pr_op_err("Error reading path '%s': %s", location, + strerror(error)); + return error; + } if (S_ISDIR(attr.st_mode) == 0) return cb(location, arg); @@ -170,7 +177,7 @@ process_file_or_dir(char const *location, char const *file_ext, bool empty_err, bool valid_file_or_dir(char const *location, bool check_file, bool check_dir, - int (*error_fn)(int error, const char *format, ...)) + int (*error_fn)(const char *format, ...)) { struct stat attr; bool is_file, is_dir; @@ -181,7 +188,7 @@ valid_file_or_dir(char const *location, bool check_file, bool check_dir, if (stat(location, &attr) == -1) { if (error_fn != NULL) { - error_fn(errno, "stat(%s) failed: %s", location, + error_fn("stat(%s) failed: %s", location, strerror(errno)); } return false; @@ -204,6 +211,7 @@ dir_exists(char const *path, bool *result) { struct stat _stat; char *last_slash; + int error; last_slash = strrchr(path, '/'); if (last_slash == NULL) { @@ -226,7 +234,9 @@ dir_exists(char const *path, bool *result) } else if (errno == ENOENT) { *result = false; } else { - return pr_op_errno(errno, "stat() failed"); + error = errno; + pr_op_err("stat() failed: %s", strerror(error)); + return error; } *last_slash = '/'; @@ -238,11 +248,14 @@ create_dir(char *path) { int error; - error = mkdir(path, 0777); - - if (error && errno != EEXIST) - return pr_op_errno(errno, "Error while making directory '%s'", - path); + if (mkdir(path, 0777) != 0) { + error = errno; + if (error != EEXIST) { + pr_op_err("Error while making directory '%s': %s", + path, strerror(error)); + return error; + } + } return 0; } @@ -291,9 +304,12 @@ remove_file(char const *path) int error; errno = 0; - error = remove(path); - if (error) - return pr_val_errno(errno, "Couldn't delete %s", path); + if (remove(path) != 0) { + error = errno; + pr_val_err("Couldn't delete '%s': %s", path, + strerror(error)); + return errno; + } return 0; } @@ -353,10 +369,12 @@ delete_dir_recursive_bottom_up(char const *path) continue; /* Keep deleting up */ /* Stop if there's content in the dir */ - if (errno == ENOTEMPTY || errno == EEXIST) + error = errno; + if (error == ENOTEMPTY || error == EEXIST) break; - error = pr_op_errno(errno, "Couldn't delete dir %s", work_loc); + pr_op_err("Couldn't delete directory '%s': %s", work_loc, + strerror(error)); goto release_str; } while (true); @@ -371,10 +389,15 @@ int get_current_time(time_t *result) { time_t now; + int error; now = time(NULL); - if (now == ((time_t) -1)) - return pr_val_errno(errno, "Error getting the current time"); + if (now == ((time_t) -1)) { + error = errno; + pr_val_err("Error getting the current time: %s", + strerror(errno)); + return error; + } *result = now; return 0; diff --git a/src/common.h b/src/common.h index cf21ccc5..47551a48 100644 --- a/src/common.h +++ b/src/common.h @@ -50,7 +50,7 @@ typedef int (*process_file_cb)(char const *, void *); int process_file_or_dir(char const *, char const *, bool, process_file_cb, void *); -typedef int (*pr_errno_cb)(int, const char *, ...); +typedef int (*pr_errno_cb)(const char *, ...); bool valid_file_or_dir(char const *, bool, bool, pr_errno_cb); int create_dir_recursive(char const *); diff --git a/src/config.c b/src/config.c index a955c8d7..36ff9723 100644 --- a/src/config.c +++ b/src/config.c @@ -1144,7 +1144,7 @@ validate_config(void) /* A file location at --tal isn't valid when --init-tals is set */ if (!valid_file_or_dir(rpki_config.tal, !rpki_config.init_tals, true, - __pr_op_err)) + pr_op_err)) return pr_op_err("Invalid TAL(s) location."); /* Ignore the other checks */ @@ -1166,7 +1166,7 @@ validate_config(void) return pr_op_err("Invalid output.bgpsec file."); if (rpki_config.slurm != NULL && - !valid_file_or_dir(rpki_config.slurm, true, true, __pr_op_err)) + !valid_file_or_dir(rpki_config.slurm, true, true, pr_op_err)) return pr_op_err("Invalid slurm location."); /* TODO (later) Remove when sync-strategy is fully deprecated */ diff --git a/src/config/uint.c b/src/config/uint.c index 3534109a..6eba6524 100644 --- a/src/config/uint.c +++ b/src/config/uint.c @@ -18,6 +18,7 @@ parse_argv_uint(struct option_field const *field, char const *str, { char *tmp; unsigned long parsed; + int error; if (field->type->has_arg != required_argument || str == NULL || strlen(str) == 0) { @@ -27,12 +28,14 @@ parse_argv_uint(struct option_field const *field, char const *str, errno = 0; parsed = strtoul(str, &tmp, 10); - if (errno || *tmp != '\0') - return errno ? pr_op_errno(errno, - "Value '%s' at '%s' is not an unsigned integer", str, - field->name) : - pr_op_err("Value '%s' at '%s' is not an unsigned integer", - str, field->name); + error = errno; + if (error || *tmp != '\0') { + if (!error) + error = -EINVAL; + pr_op_err("Value '%s' at '%s' is not an unsigned integer: %s", + str, field->name, strerror(abs(error))); + return error; + } if (parsed < field->min || field->max < parsed) { return pr_op_err("Value of '%s' is out of range (%u-%u).", diff --git a/src/crypto/hash.c b/src/crypto/hash.c index 4b6de3c8..68b820f7 100644 --- a/src/crypto/hash.c +++ b/src/crypto/hash.c @@ -84,8 +84,8 @@ hash_local_file(char const *algorithm, char const *uri, unsigned char *result, consumed = fread(buffer, 1, buffer_len, file); error = ferror(file); if (error) { - pr_val_errno(error, - "File reading error. Error message (apparently)"); + pr_val_err("File reading error. Error message (apparently): %s", + strerror(error)); goto end3; } diff --git a/src/daemon.c b/src/daemon.c index 9688e103..3fadba68 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -23,6 +23,7 @@ daemonize(daemon_log_cb log_cb) char *pwd; pid_t pid; long int fds; + int error; /* Already a daemon, just return */ if (getppid() == 1) @@ -34,8 +35,11 @@ daemonize(daemon_log_cb log_cb) return pr_enomem(); pid = fork(); - if (pid < 0) - return pr_op_errno(errno, "Couldn't fork to daemonize"); + if (pid < 0) { + error = errno; + pr_op_err("Couldn't fork to daemonize: %s", strerror(error)); + return error; + } /* Terminate parent */ if (pid > 0) @@ -46,9 +50,10 @@ daemonize(daemon_log_cb log_cb) /* Child goes on from here */ if (setsid() < 0) { - pr_op_errno(errno, - "Couldn't create new session, ending execution"); - exit(errno); + error = errno; + pr_op_err("Couldn't create new session, ending execution: %s", + strerror(error)); + exit(error); } /* @@ -61,9 +66,10 @@ daemonize(daemon_log_cb log_cb) /* Assure this is not a session leader */ pid = fork(); if (pid < 0) { - pr_op_errno(errno, - "Couldn't fork again to daemonize, ending execution"); - exit(errno); + error = errno; + pr_op_err("Couldn't fork again to daemonize, ending execution: %s", + strerror(error)); + exit(error); } /* Terminate parent */ @@ -81,9 +87,10 @@ daemonize(daemon_log_cb log_cb) umask(0); if (chdir(pwd) < 0) { - pr_op_errno(errno, - "Couldn't chdir() of daemon, ending execution"); - exit(errno); + error = errno; + pr_op_err("Couldn't chdir() of daemon, ending execution: %s", + strerror(error)); + exit(error); } free(pwd); diff --git a/src/delete_dir_daemon.c b/src/delete_dir_daemon.c index 76478d3c..ea4a327d 100644 --- a/src/delete_dir_daemon.c +++ b/src/delete_dir_daemon.c @@ -24,6 +24,8 @@ struct rem_dirs { static int remove_file(char const *location) { + int error; + pr_op_debug("Trying to remove file '%s'.", location); #ifdef DEBUG_RRDP @@ -31,14 +33,21 @@ remove_file(char const *location) return 0; #endif - return remove(location) - ? pr_op_errno(errno, "Couldn't delete file '%s'", location) - : 0; + if (remove(location) != 0) { + error = errno; + pr_op_err("Couldn't delete file '%s': %s", location, + strerror(error)); + return error; + } + + return 0; } static int remove_dir(char const *location) { + int error; + pr_op_debug("Trying to remove dir '%s'.", location); #ifdef DEBUG_RRDP @@ -46,9 +55,14 @@ remove_dir(char const *location) return 0; #endif - return rmdir(location) - ? pr_op_errno(errno, "Couldn't delete directory '%s'", location) - : 0; + if (remove(location) != 0) { + error = errno; + pr_op_err("Couldn't delete directory '%s': %s", location, + strerror(error)); + return error; + } + + return 0; } static int diff --git a/src/file.c b/src/file.c index 89ae166f..c9552ad7 100644 --- a/src/file.c +++ b/src/file.c @@ -12,11 +12,16 @@ file_get(char const *file_name, FILE **result, struct stat *stat, int error; file = fopen(file_name, mode); - if (file == NULL) - return pr_val_errno(errno, "Could not open file '%s'", file_name); + if (file == NULL) { + error = errno; + pr_val_err("Could not open file '%s': %s", file_name, + strerror(error)); + return error; + } if (fstat(fileno(file), stat) == -1) { - error = pr_val_errno(errno, "fstat(%s) failed", file_name); + error = errno; + pr_val_err("fstat(%s) failed: %s", file_name, strerror(error)); goto fail; } if (!S_ISREG(stat->st_mode)) { @@ -49,7 +54,7 @@ void file_close(FILE *file) { if (fclose(file) == -1) - pr_val_errno(errno, "fclose() failed"); + pr_val_err("fclose() failed: %s", strerror(errno)); } int @@ -80,8 +85,8 @@ file_load(char const *file_name, struct file_contents *fc) * code. It literally doesn't say how to get an error * code. */ - pr_val_errno(error, - "File reading error. Error message (apparently)"); + pr_val_err("File reading error. The error message is (apparently) '%s'", + strerror(error)); free(fc->buffer); goto end; } diff --git a/src/http/http.c b/src/http/http.c index 147be88c..3aa82c96 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -413,8 +413,8 @@ __http_download_file(struct rpki_uri *uri, long *response_code, long ims_value, error = rename(tmp_file, original_file); if (error) { error = errno; - pr_val_errno(error, "Renaming temporal file from '%s' to '%s'", - tmp_file, original_file); + pr_val_err("Renaming temporal file from '%s' to '%s': %s", + tmp_file, original_file, strerror(error)); goto delete_dir; } @@ -549,8 +549,8 @@ http_direct_download(char const *remote, char const *dest) error = rename(tmp_file, dest); if (error) { error = errno; - pr_val_errno(error, "Renaming temporal file from '%s' to '%s'", - tmp_file, dest); + pr_val_err("Renaming temporal file from '%s' to '%s': %s", + tmp_file, dest, strerror(error)); goto release_tmp; } diff --git a/src/line_file.c b/src/line_file.c index b31034fc..ffce6c27 100644 --- a/src/line_file.c +++ b/src/line_file.c @@ -103,8 +103,11 @@ lfile_read(struct line_file *lfile, char **result) error = errno; free(string); *result = NULL; - if (ferror(lfile->file)) - return pr_op_errno(error, "Error while reading file"); + if (ferror(lfile->file)) { + pr_op_err("Error while reading file: %s", + strerror(error)); + return error; + } if (feof(lfile->file)) return 0; pr_crit("Supposedly unreachable code reached. ferror:%d feof:%d", diff --git a/src/log.c b/src/log.c index 166a2fc5..99091192 100644 --- a/src/log.c +++ b/src/log.c @@ -163,7 +163,8 @@ register_signal_handlers(void) * Not fatal; it just means we will not print stack traces on * Segmentation Faults. */ - pr_op_errno(errno, "SIGSEGV handler registration failure"); + pr_op_err("SIGSEGV handler registration failure: %s", + strerror(errno)); } /* @@ -190,7 +191,8 @@ register_signal_handlers(void) * will die silently. * But they're somewhat rare, so whatever. */ - pr_op_errno(errno, "SIGPIPE handler registration failure"); + pr_op_err("SIGPIPE handler registration failure: %s", + strerror(errno)); } } @@ -220,11 +222,11 @@ log_setup(bool unit_tests) error = pthread_mutex_init(&logck, NULL); if (error) { fprintf(ERR.stream, "pthread_mutex_init() returned %d: %s\n", - error, strerror(abs(error))); + error, strerror(error)); if (!unit_tests) syslog(LOG_ERR | op_config.facility, "pthread_mutex_init() returned %d: %s", - error, strerror(abs(error))); + error, strerror(error)); return error; } @@ -504,13 +506,13 @@ pr_op_warn(const char *format, ...) } int -__pr_op_err(int error, const char *format, ...) +pr_op_err(const char *format, ...) { PR_SIMPLE(LOG_ERR, op_config); lock_mutex(); print_stack_trace(NULL); unlock_mutex(); - return error; + return -EINVAL; } void @@ -537,15 +539,15 @@ pr_val_warn(const char *format, ...) } int -__pr_val_err(int error, const char *format, ...) +pr_val_err(const char *format, ...) { PR_SIMPLE(LOG_ERR, val_config); - return error; + return -EINVAL; } struct crypto_cb_arg { unsigned int stack_size; - int (*error_fn)(int, const char *, ...); + int (*error_fn)(const char *, ...); }; static int @@ -558,19 +560,19 @@ log_crypto_error(const char *str, size_t len, void *_arg) } static int -crypto_err(struct log_config *cfg, int (*error_fn)(int, const char *, ...)) +crypto_err(struct log_config *cfg, int (*error_fn)(const char *, ...)) { struct crypto_cb_arg arg; - error_fn(0, "libcrypto error stack:"); + error_fn("libcrypto error stack:"); arg.stack_size = 0; arg.error_fn = error_fn; ERR_print_errors_cb(log_crypto_error, &arg); if (arg.stack_size == 0) - error_fn(0, " "); + error_fn(" "); else - error_fn(0, "End of libcrypto stack."); + error_fn("End of libcrypto stack."); return -EINVAL; } @@ -591,7 +593,7 @@ int op_crypto_err(const char *format, ...) { PR_SIMPLE(LOG_ERR, op_config); - return crypto_err(&op_config, __pr_op_err); + return crypto_err(&op_config, pr_op_err); } /** @@ -610,7 +612,7 @@ int val_crypto_err(const char *format, ...) { PR_SIMPLE(LOG_ERR, val_config); - return crypto_err(&val_config, __pr_val_err); + return crypto_err(&val_config, pr_val_err); } int diff --git a/src/log.h b/src/log.h index 4155638d..c2b2cec7 100644 --- a/src/log.h +++ b/src/log.h @@ -70,19 +70,8 @@ void pr_op_debug(const char *, ...) CHECK_FORMAT(1, 2); void pr_op_info(const char *, ...) CHECK_FORMAT(1, 2); /* Non-errors that suggest a problem. */ int pr_op_warn(const char *, ...) CHECK_FORMAT(1, 2); -/* Do not use this; see pr_op_err() and pr_op_errno(). */ -int __pr_op_err(int, const char *, ...) CHECK_FORMAT(2, 3); -/* - * Problematic situations that prevent Fort from doing its job. - * (Always returns -EINVAL.) - */ -#define pr_op_err(fmt, ...) __pr_op_err(-EINVAL, fmt, ##__VA_ARGS__) -/* - * Like pr_op_err(), but also prints strerror(error). - * (Always returns error). - */ -#define pr_op_errno(error, fmt, ...) \ - __pr_op_err(error, fmt ": %s", ##__VA_ARGS__, strerror(abs(error))) +/* Problematic situations that prevent Fort from doing its job. */ +int pr_op_err(const char *, ...) CHECK_FORMAT(1, 2); /* Like pr_op_err(), except it prints libcrypto's error stack as well. */ int op_crypto_err(const char *, ...) CHECK_FORMAT(1, 2); @@ -95,16 +84,8 @@ void pr_val_debug(const char *, ...) CHECK_FORMAT(1, 2); void pr_val_info(const char *, ...) CHECK_FORMAT(1, 2); /* Issues that did not trigger RPKI object rejection. */ int pr_val_warn(const char *, ...) CHECK_FORMAT(1, 2); -/* Do not use this; see pr_val_err() and pr_val_errno(). */ -int __pr_val_err(int, const char *, ...) CHECK_FORMAT(2, 3); /* Problems that trigger RPKI object rejection. */ -#define pr_val_err(fmt, ...) __pr_val_err(-EINVAL, fmt, ##__VA_ARGS__) -/* - * Like pr_val_err(), but also prints strerror(error). - * (Always returns error). - */ -#define pr_val_errno(error, fmt, ...) \ - __pr_val_err(error, fmt ": %s", ##__VA_ARGS__, strerror(abs(error))) +int pr_val_err(const char *, ...) CHECK_FORMAT(1, 2); /* Like pr_val_err(), except it prints libcrypto's error stack as well. */ int val_crypto_err(const char *, ...) CHECK_FORMAT(1, 2); diff --git a/src/object/certificate.c b/src/object/certificate.c index 57d3022a..b281ffed 100644 --- a/src/object/certificate.c +++ b/src/object/certificate.c @@ -1975,8 +1975,7 @@ certificate_validate_aia(struct rpki_uri *caIssuers, X509 *cert) static int verify_mft_loc(struct rpki_uri *mft_uri) { - if (!valid_file_or_dir(uri_get_local(mft_uri), true, false, - __pr_val_err)) + if (!valid_file_or_dir(uri_get_local(mft_uri), true, false, pr_val_err)) return -EINVAL; /* Error already logged */ return 0; diff --git a/src/object/manifest.c b/src/object/manifest.c index 1c83cc85..68c2fb77 100644 --- a/src/object/manifest.c +++ b/src/object/manifest.c @@ -111,8 +111,10 @@ validate_manifest(struct Manifest *manifest) if (manifest->version != NULL) { error = asn_INTEGER2ulong(manifest->version, &version); if (error) { - if (errno) - pr_val_errno(errno, "Error casting manifest version"); + if (errno) { + pr_val_err("Error casting manifest version: %s", + strerror(errno)); + } return pr_val_err("The manifest version isn't a valid unsigned long"); } if (version != 0) diff --git a/src/object/roa.c b/src/object/roa.c index 77c1cab1..fc979e54 100644 --- a/src/object/roa.c +++ b/src/object/roa.c @@ -42,8 +42,10 @@ ____handle_roa_v4(struct resources *parent, unsigned long asn, if (roa_addr->maxLength != NULL) { error = asn_INTEGER2ulong(roa_addr->maxLength, &max_length); if (error) { - if (errno) - pr_val_errno(errno, "Error casting ROA's IPv4 maxLength"); + if (errno) { + pr_val_err("Error casting ROA's IPv4 maxLength: %s", + strerror(errno)); + } error = pr_val_err("The ROA's IPv4 maxLength isn't a valid unsigned long"); goto end_error; } @@ -96,8 +98,10 @@ ____handle_roa_v6(struct resources *parent, unsigned long asn, if (roa_addr->maxLength != NULL) { error = asn_INTEGER2ulong(roa_addr->maxLength, &max_length); if (error) { - if (errno) - pr_val_errno(errno, "Error casting ROA's IPv6 maxLength"); + if (errno) { + pr_val_err("Error casting ROA's IPv6 maxLength: %s", + strerror(errno)); + } error = pr_val_err("The ROA's IPv6 maxLength isn't a valid unsigned long"); goto end_error; } @@ -160,8 +164,10 @@ __handle_roa(struct RouteOriginAttestation *roa, struct resources *parent) if (roa->version != NULL) { error = asn_INTEGER2ulong(roa->version, &version); if (error) { - if (errno) - pr_val_errno(errno, "Error casting ROA's version"); + if (errno) { + pr_val_err("Error casting ROA's version: %s", + strerror(errno)); + } error = pr_val_err("The ROA's version isn't a valid long"); goto end_error; } @@ -175,8 +181,10 @@ __handle_roa(struct RouteOriginAttestation *roa, struct resources *parent) /* rfc6482#section-3.2 */ if (asn_INTEGER2ulong(&roa->asID, &asn) != 0) { - if (errno) - pr_val_errno(errno, "Error casting ROA's AS ID value"); + if (errno) { + pr_val_err("Error casting ROA's AS ID value: %s", + strerror(errno)); + } error = pr_val_err("ROA's AS ID couldn't be parsed as unsigned long"); goto end_error; } diff --git a/src/object/tal.c b/src/object/tal.c index 98d58900..b63ca3af 100644 --- a/src/object/tal.c +++ b/src/object/tal.c @@ -255,8 +255,8 @@ base64_sanitize(struct line_file *lfile, char **out) * code. It literally doesn't say how to get an error * code. */ - pr_op_errno(error, - "File reading error. Error message (apparently)"); + pr_op_err("File reading error. Presumably, the error message is '%s.'", + strerror(error)); goto free_result; } @@ -353,7 +353,8 @@ tal_load(char const *file_name, struct tal **result) error = lfile_open(file_name, &lfile); if (error) { - pr_op_errno(error, "Error opening file '%s'", file_name); + pr_op_err("Error opening file '%s': %s", file_name, + strerror(abs(error))); goto fail4; } @@ -555,7 +556,7 @@ handle_tal_uri(struct tal *tal, struct rpki_uri *uri, void *arg) } else { /* Look for local files */ if (!valid_file_or_dir(uri_get_local(uri), true, false, - __pr_val_err)) { + pr_val_err)) { validation_destroy(state); return 0; /* Error already logged */ } diff --git a/src/reqs_errors.c b/src/reqs_errors.c index 1501ce0b..951b93e0 100644 --- a/src/reqs_errors.c +++ b/src/reqs_errors.c @@ -94,8 +94,10 @@ reqs_errors_init(void) int error; error = pthread_rwlock_init(&db_lock, NULL); - if (error) - return pr_op_errno(error, "pthread_rwlock_init() errored"); + if (error) { + pr_op_err("pthread_rwlock_init() errored: %s", strerror(error)); + return error; + } err_uris_db = NULL; diff --git a/src/resource.c b/src/resource.c index 92c8f11f..232309be 100644 --- a/src/resource.c +++ b/src/resource.c @@ -421,8 +421,10 @@ ASId2ulong(ASId_t *as_id, unsigned long *result) error = asn_INTEGER2ulong(as_id, result); if (error) { - if (errno) - pr_val_errno(errno, "Error converting ASN value"); + if (errno) { + pr_val_err("Error converting ASN value: %s", + strerror(errno)); + } return pr_val_err("ASN value is not a valid unsigned long"); } diff --git a/src/rrdp/db/db_rrdp.c b/src/rrdp/db/db_rrdp.c index 7bf328a6..bdccbbe9 100644 --- a/src/rrdp/db/db_rrdp.c +++ b/src/rrdp/db/db_rrdp.c @@ -139,8 +139,11 @@ db_rrdp_init(void) int error; error = pthread_rwlock_init(&lock, NULL); - if (error) - return pr_op_errno(error, "DB RRDP pthread_rwlock_init() errored"); + if (error) { + pr_op_err("DB RRDP pthread_rwlock_init() errored: %s", + strerror(error)); + return error; + } SLIST_INIT(&db.tals); return 0; diff --git a/src/rrdp/rrdp_parser.c b/src/rrdp/rrdp_parser.c index 34d840ca..40216049 100644 --- a/src/rrdp/rrdp_parser.c +++ b/src/rrdp/rrdp_parser.c @@ -276,6 +276,7 @@ parse_long(xmlTextReaderPtr reader, char const *attr, unsigned long *result) { xmlChar *xml_value; unsigned long tmp; + int error; xml_value = xmlTextReaderGetAttribute(reader, BAD_CAST attr); if (xml_value == NULL) @@ -284,10 +285,11 @@ parse_long(xmlTextReaderPtr reader, char const *attr, unsigned long *result) errno = 0; tmp = strtoul((char *) xml_value, NULL, 10); - if (errno) { + error = errno; + if (error) { xmlFree(xml_value); - pr_val_errno(errno, "RRDP file: Invalid long value '%s'", - xml_value); + pr_val_err("RRDP file: Invalid long value '%s': %s", + xml_value, strerror(error)); return -EINVAL; } xmlFree(xml_value); diff --git a/src/rsync/rsync.c b/src/rsync/rsync.c index 97c35993..091025c7 100644 --- a/src/rsync/rsync.c +++ b/src/rsync/rsync.c @@ -266,14 +266,25 @@ handle_child_thread(char **args, int fds[2][2]) static int create_pipes(int fds[2][2]) { - if (pipe(fds[0]) == -1) - return -pr_op_errno(errno, "Piping rsync stderr"); + int error; + + if (pipe(fds[0]) == -1) { + error = errno; + pr_op_err("Piping rsync stderr: %s", strerror(error)); + return -error; + } + if (pipe(fds[1]) == -1) { + error = errno; + /* Close pipe previously created */ close(fds[0][0]); close(fds[0][1]); - return -pr_op_errno(errno, "Piping rsync stdout"); + + pr_op_err("Piping rsync stdout: %s", strerror(error)); + return -error; } + return 0; } @@ -322,10 +333,13 @@ read_pipe(int fd_pipe[2][2], int type, bool log_operation) while (1) { count = read(fd_pipe[type][0], buffer, sizeof(buffer)); if (count == -1) { - if (errno == EINTR) + error = errno; + if (error == EINTR) continue; close(fd_pipe[type][0]); /* Close read end */ - return -pr_val_errno(errno, "Reading rsync buffer"); + pr_val_err("rsync buffer read error: %s", + strerror(error)); + return -error; } if (count == 0) break; @@ -422,8 +436,9 @@ do_rsync(struct rpki_uri *uri, bool is_ta, bool log_operation) handle_child_thread(args, fork_fds); } if (child_pid < 0) { - pr_op_errno(errno, "Couldn't fork to execute rsync"); error = errno; + pr_op_err("Couldn't fork to execute rsync: %s", + strerror(error)); /* Close all ends from the created pipes */ close(fork_fds[0][0]); close(fork_fds[1][0]); diff --git a/src/rtr/db/db_table.c b/src/rtr/db/db_table.c index 21af1b3d..cf2e795d 100644 --- a/src/rtr/db/db_table.c +++ b/src/rtr/db/db_table.c @@ -108,11 +108,16 @@ static int add_roa(struct db_table *table, struct hashable_roa *new) { struct hashable_roa *old; + int error; errno = 0; HASH_REPLACE(hh, table->roas, data, sizeof(new->data), new, old); - if (errno) - return -pr_val_errno(errno, "ROA couldn't be added to hash table"); + error = errno; + if (error) { + pr_val_err("ROA couldn't be added to hash table: %s", + strerror(error)); + return -error; + } if (old != NULL) free(old); @@ -123,11 +128,16 @@ static int add_router_key(struct db_table *table, struct hashable_key *new) { struct hashable_key *old; + int error; errno = 0; HASH_REPLACE(hh, table->router_keys, data, sizeof(new->data), new, old); - if (errno) - return -pr_val_errno(errno, "Router Key couldn't be added to hash table"); + error = errno; + if (error) { + pr_val_err("Router Key couldn't be added to hash table: %s", + strerror(error)); + return -error; + } if (old != NULL) free(old); diff --git a/src/rtr/db/vrps.c b/src/rtr/db/vrps.c index f8ff3b7a..40a3468b 100644 --- a/src/rtr/db/vrps.c +++ b/src/rtr/db/vrps.c @@ -129,13 +129,15 @@ vrps_init(void) error = pthread_rwlock_init(&state_lock, NULL); if (error) { - error = pr_op_errno(error, "state pthread_rwlock_init() errored"); + pr_op_err("state pthread_rwlock_init() errored: %s", + strerror(error)); goto revert_deltas; } error = pthread_rwlock_init(&table_lock, NULL); if (error) { - error = pr_op_errno(error, "table pthread_rwlock_init() errored"); + pr_op_err("table pthread_rwlock_init() errored: %s", + strerror(error)); goto revert_state_lock; } diff --git a/src/rtr/pdu_sender.c b/src/rtr/pdu_sender.c index fc4857f2..9f997818 100644 --- a/src/rtr/pdu_sender.c +++ b/src/rtr/pdu_sender.c @@ -49,10 +49,12 @@ send_response(int fd, uint8_t pdu_type, unsigned char *data, size_t data_len) return pr_op_err("poll() returned revents %u.", pfd.revents); } while (!(pfd.revents & POLLOUT)); - error = write(fd, data, data_len); - if (error < 0) - return pr_op_errno(errno, "Error sending %s to client.", - pdutype2str(pdu_type)); + if (write(fd, data, data_len) < 0) { + error = errno; + pr_op_err("Error sending %s to client: %s", + pdutype2str(pdu_type), strerror(error)); + return error; + } return 0; } diff --git a/src/rtr/rtr.c b/src/rtr/rtr.c index 606f1db6..830ab011 100644 --- a/src/rtr/rtr.c +++ b/src/rtr/rtr.c @@ -215,7 +215,7 @@ set_nonblock(int fd) flags = fcntl(fd, F_GETFL); if (flags == -1) { error = errno; - pr_op_errno(error, "fcntl() to get flags failed"); + pr_op_err("fcntl() to get flags failed: %s", strerror(error)); return error; } @@ -223,7 +223,7 @@ set_nonblock(int fd) if (fcntl(fd, F_SETFL, flags) == -1) { error = errno; - pr_op_errno(error, "fcntl() to set flags failed"); + pr_op_err("fcntl() to set flags failed: %s", strerror(error)); return error; } @@ -261,7 +261,7 @@ create_server_socket(char const *input_addr, char const *hostname, for (addr = addrs; addr != NULL; addr = addr->ai_next) { fd = socket(addr->ai_family, SOCK_STREAM, 0); if (fd < 0) { - pr_op_errno(errno, "socket() failed"); + pr_op_err("socket() failed: %s", strerror(errno)); continue; } @@ -277,7 +277,8 @@ create_server_socket(char const *input_addr, char const *hostname, /* enable SO_REUSEADDR */ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0) { - pr_op_errno(errno, "setsockopt(SO_REUSEADDR) failed"); + pr_op_err("setsockopt(SO_REUSEADDR) failed: %s", + strerror(errno)); close(fd); continue; } @@ -285,22 +286,24 @@ create_server_socket(char const *input_addr, char const *hostname, /* enable SO_REUSEPORT */ if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(int)) < 0) { - pr_op_errno(errno, "setsockopt(SO_REUSEPORT) failed"); + pr_op_err("setsockopt(SO_REUSEPORT) failed: %s", + strerror(errno)); close(fd); continue; } if (bind(fd, addr->ai_addr, addr->ai_addrlen) < 0) { - pr_op_errno(errno, "bind() failed"); + pr_op_err("bind() failed: %s", strerror(errno)); close(fd); continue; } - error = getsockname(fd, addr->ai_addr, &addr->ai_addrlen); - if (error) { + if (getsockname(fd, addr->ai_addr, &addr->ai_addrlen) != 0) { + error = errno; close(fd); freeaddrinfo(addrs); - return pr_op_errno(errno, "getsockname() failed"); + pr_op_err("getsockname() failed: %s", strerror(error)); + return error; } port = (unsigned char)(addr->ai_addr->sa_data[0]) << 8; @@ -311,8 +314,10 @@ create_server_socket(char const *input_addr, char const *hostname, freeaddrinfo(addrs); if (listen(fd, config_get_server_queue()) != 0) { + error = errno; close(fd); - return pr_op_errno(errno, "listen() failure"); + pr_op_err("listen() failure: %s", strerror(error)); + return error; } server.fd = fd; @@ -438,11 +443,10 @@ handle_accept_result(int client_fd, int err) goto retry; #endif -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wlogical-op" - if (err == EAGAIN || err == EWOULDBLOCK) + if (err == EAGAIN) + goto retry; + if (err == EWOULDBLOCK) goto retry; -#pragma GCC diagnostic pop pr_op_info("Client connection attempt not accepted: %s. Quitting...", strerror(err)); @@ -497,6 +501,7 @@ read_until_block(int fd, struct client_request *request) { ssize_t read_result; size_t offset; + int error; request->nread = 0; @@ -504,10 +509,12 @@ read_until_block(int fd, struct client_request *request) read_result = read(fd, &request->buffer[offset], REQUEST_BUFFER_LEN - offset); if (read_result == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) + error = errno; + if (error == EAGAIN || error == EWOULDBLOCK) return true; /* Ok, we have the full packet. */ - pr_op_errno(errno, "Client socket read interrupted"); + pr_op_err("Client socket read interrupted: %s", + strerror(error)); return false; } @@ -786,8 +793,10 @@ void rtr_stop(void) stop_server_thread = true; error = pthread_join(server_thread, NULL); - if (error) - pr_op_errno(error, "pthread_join() returned %d", error); + if (error) { + pr_op_err("pthread_join() returned error %d: %s", error, + strerror(error)); + } thread_pool_destroy(request_handlers); diff --git a/src/slurm/slurm_parser.c b/src/slurm/slurm_parser.c index e5426d51..00c25b98 100644 --- a/src/slurm/slurm_parser.c +++ b/src/slurm/slurm_parser.c @@ -132,7 +132,7 @@ set_prefix(json_t *object, bool is_assertion, struct slurm_prefix *result, clone = strdup(str_prefix); if (clone == NULL) - return -pr_op_errno(errno, "Couldn't allocate string to parse prefix"); + return pr_enomem(); token = strtok(clone, "/"); isv4 = strchr(token, ':') == NULL; diff --git a/src/thread/thread_pool.c b/src/thread/thread_pool.c index a58c6798..97e6a22d 100644 --- a/src/thread/thread_pool.c +++ b/src/thread/thread_pool.c @@ -248,15 +248,19 @@ thread_pool_attr_create(pthread_attr_t *attr) int error; error = pthread_attr_init(attr); - if (error) - return pr_op_errno(error, "Calling pthread_attr_init()"); + if (error) { + pr_op_err("pthread_attr_init() returned error %d: %s", + error, strerror(error)); + return error; + } /* Use 2MB (default in most 64 bits systems) */ error = pthread_attr_setstacksize(attr, 1024 * 1024 * 2); if (error) { pthread_attr_destroy(attr); - return pr_op_errno(error, - "Calling pthread_attr_setstacksize()"); + pr_op_err("pthread_attr_setstacksize() returned error %d: %s", + error, strerror(error)); + return error; } /* @@ -293,7 +297,8 @@ spawn_threads(struct thread_pool *pool) error = pthread_create(&pool->thread_ids[i], &attr, tasks_poll, pool); if (error) { - error = pr_op_errno(error, "Spawning pool thread"); + pr_op_err("pthread_create() returned error %d: %s", + error, strerror(error)); goto end; } @@ -319,23 +324,24 @@ thread_pool_create(char const *name, unsigned int threads, /* Init locking */ error = pthread_mutex_init(&result->lock, NULL); if (error) { - error = pr_op_errno(error, "Calling pthread_mutex_init()"); + pr_op_err("pthread_mutex_init() returned error %d: %s", + error, strerror(error)); goto free_tmp; } /* Init conditional to signal pending work */ error = pthread_cond_init(&result->parent2worker, NULL); if (error) { - error = pr_op_errno(error, - "Calling pthread_cond_init() at working condition"); + pr_op_err("pthread_cond_init(p2w) returned error %d: %s", + error, strerror(error)); goto free_mutex; } /* Init conditional to signal no pending work */ error = pthread_cond_init(&result->worker2parent, NULL); if (error) { - error = pr_op_errno(error, - "Calling pthread_cond_init() at waiting condition"); + pr_op_err("pthread_cond_init(w2p) returned error %d: %s", + error, strerror(error)); goto free_working_cond; } diff --git a/src/types/address.c b/src/types/address.c index 4eeb2065..f918224c 100644 --- a/src/types/address.c +++ b/src/types/address.c @@ -424,15 +424,17 @@ int prefix_length_parse(const char *text, uint8_t *dst, uint8_t max_value) { unsigned long len; + int error; if (text == NULL) return pr_val_err("Can't decode NULL prefix length"); errno = 0; len = strtoul(text, NULL, 10); - if (errno) { - pr_val_errno(errno, "Invalid prefix length '%s'", text); - return -EINVAL; + error = errno; + if (error) { + return pr_val_err("Invalid prefix length '%s': %s", text, + strerror(error)); } /* An underflow or overflow will be considered here */ if (max_value < len)