From b7a60c5bed989a52a53b8b697203f55367f55a89 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 11 May 2026 15:24:53 -0400 Subject: Replace msg, perror, and fprintf with SYS message macros --- src/lib/core/artifactory.c | 28 ++++++++++++------------ src/lib/core/conda.c | 34 +++++++++++++++--------------- src/lib/core/copy.c | 16 +++++++------- src/lib/core/download.c | 2 +- src/lib/core/envctl.c | 10 ++++----- src/lib/core/environment.c | 2 +- src/lib/core/github.c | 13 ++++++------ src/lib/core/ini.c | 3 ++- src/lib/core/multiprocessing.c | 48 +++++++++++++++++++++++++----------------- src/lib/core/recipe.c | 4 ++-- src/lib/core/relocation.c | 2 +- src/lib/core/str.c | 5 +++-- src/lib/core/strlist.c | 6 +++--- src/lib/core/system.c | 16 +++++++------- src/lib/core/template.c | 12 +++++------ src/lib/core/utils.c | 36 ++++++++++++++++++++----------- src/lib/core/wheel.c | 12 +++++------ 17 files changed, 136 insertions(+), 113 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/artifactory.c b/src/lib/core/artifactory.c index 1e9de84..669bfff 100644 --- a/src/lib/core/artifactory.c +++ b/src/lib/core/artifactory.c @@ -38,7 +38,7 @@ int artifactory_download_cli(char *dest, strncpy(os_ident, "linux", sizeof(os_ident) - 1); os_ident[sizeof(os_ident) - 1] = '\0'; } else { - fprintf(stderr, "%s: unknown operating system: %s\n", __FUNCTION__, os_ident); + SYSERROR("%s: unknown operating system: %s", __FUNCTION__, os_ident); return -1; } @@ -59,7 +59,7 @@ int artifactory_download_cli(char *dest, } else if (!strcmp(arch_ident, "arm64") || !strcmp(arch_ident, "aarch64")) { strncpy(arch_ident, "arm64", sizeof(arch_ident) - 1); } else { - fprintf(stderr, "%s: unknown architecture: %s\n", __FUNCTION__, arch_ident); + SYSERROR("%s: unknown architecture: %s", __FUNCTION__, arch_ident); return -1; } arch_ident[sizeof(arch_ident) - 1] = '\0'; @@ -79,7 +79,7 @@ int artifactory_download_cli(char *dest, path[sizeof(path) - 1] = '\0'; if (mkdirs(path, 0755)) { - fprintf(stderr, "%s: %s: %s", __FUNCTION__, path, strerror(errno)); + SYSERROR("%s: %s: %s", __FUNCTION__, path, strerror(errno)); return -1; } @@ -176,8 +176,8 @@ int jfrt_auth_init(struct JFRT_Auth *auth_ctx) { char *client_cert_path = getenv("STASIS_JF_CLIENT_CERT_PATH"); if (!url) { - fprintf(stderr, "Artifactory URL is not configured:\n"); - fprintf(stderr, "please set STASIS_JF_ARTIFACTORY_URL\n"); + SYSERROR("Artifactory URL is not configured:"); + SYSERROR("please set STASIS_JF_ARTIFACTORY_URL"); return -1; } auth_ctx->url = url; @@ -208,11 +208,11 @@ int jfrt_auth_init(struct JFRT_Auth *auth_ctx) { auth_ctx->client_cert_key_path = client_cert_key_path; auth_ctx->client_cert_path = client_cert_path; } else { - fprintf(stderr, "Artifactory authentication is not configured:\n"); - fprintf(stderr, "set STASIS_JF_USER and STASIS_JF_PASSWORD\n"); - fprintf(stderr, "or, set STASIS_JF_ACCESS_TOKEN\n"); - fprintf(stderr, "or, set STASIS_JF_SSH_KEY_PATH and STASIS_JF_SSH_KEY_PASSPHRASE\n"); - fprintf(stderr, "or, set STASIS_JF_CLIENT_CERT_KEY_PATH and STASIS_JF_CLIENT_CERT_PATH\n"); + SYSERROR("Artifactory authentication is not configured:"); + SYSERROR("set STASIS_JF_USER and STASIS_JF_PASSWORD"); + SYSERROR("or, set STASIS_JF_ACCESS_TOKEN"); + SYSERROR("or, set STASIS_JF_SSH_KEY_PATH and STASIS_JF_SSH_KEY_PASSPHRASE"); + SYSERROR("or, set STASIS_JF_CLIENT_CERT_KEY_PATH and STASIS_JF_CLIENT_CERT_PATH"); return -1; } return 0; @@ -305,7 +305,7 @@ int jfrog_cli_rt_download(struct JFRT_Auth *auth, struct JFRT_Download *ctx, cha char cmd[STASIS_BUFSIZ] = {0}; if (isempty(repo_path)) { - fprintf(stderr, "repo_path argument must be a valid artifactory repository path\n"); + SYSERROR("repo_path argument must be a valid artifactory repository path"); return -1; } @@ -371,12 +371,12 @@ int jfrog_cli_rt_upload(struct JFRT_Auth *auth, struct JFRT_Upload *ctx, char *s char cmd[STASIS_BUFSIZ] = {0}; if (isempty(src)) { - fprintf(stderr, "src argument must be a valid file system path\n"); + SYSERROR("src argument must be a valid file system path"); return -1; } if (isempty(repo_path)) { - fprintf(stderr, "repo_path argument must be a valid artifactory repository path\n"); + SYSERROR("repo_path argument must be a valid artifactory repository path"); return -1; } @@ -469,7 +469,7 @@ int jfrog_cli_rt_search(struct JFRT_Auth *auth, struct JFRT_Search *ctx, char *r char cmd[STASIS_BUFSIZ] = {0}; if (isempty(repo_path)) { - fprintf(stderr, "repo_path argument must be a valid artifactory repository path\n"); + SYSERROR("repo_path argument must be a valid artifactory repository path"); return -1; } diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c index 0259e82..f9e2bd2 100644 --- a/src/lib/core/conda.c +++ b/src/lib/core/conda.c @@ -40,7 +40,7 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { char *errmsg = NULL; const long http_code = download(url, installer_path, &errmsg); if (HTTP_ERROR(http_code)) { - fprintf(stderr, "download failed: %ld: %s\n", http_code, errmsg); + SYSERROR("download failed: %ld: %s", http_code, errmsg); guard_free(errmsg); return -1; } @@ -176,7 +176,7 @@ int pkg_index_provides(int mode, const char *index, const char *spec, const char int logfd = mkstemp(logfile); if (logfd < 0) { - perror(logfile); + SYSERROR("unable to create log file: %s", logfile); remove(logfile); // fail harmlessly if not present return PKG_INDEX_PROVIDES_E_INTERNAL_LOG_HANDLE; } @@ -330,7 +330,7 @@ static int conda_prepend_condabin(const char *root) { static int env0_to_runtime(const char *logfile) { FILE *fp = fopen(logfile, "r"); if (!fp) { - perror(logfile); + SYSERROR("unable to open log file for reading: %s, %s", logfile, strerror(errno)); return -1; } @@ -355,14 +355,14 @@ static int env0_to_runtime(const char *logfile) { char **part = split(buf, "=", 1); if (!part) { - perror("unable to split environment variable buffer"); + SYSERROR("unable to split environment variable buffer: %s", strerror(errno)); fclose(fp); return -1; } if (!part[0]) { - msg(STASIS_MSG_WARN | STASIS_MSG_L1, "Invalid environment variable key ignored: '%s'\n", buf); + SYSWARN("Invalid environment variable key ignored: '%s'", buf); } else if (!part[1]) { - msg(STASIS_MSG_WARN | STASIS_MSG_L1, "Invalid environment variable value ignored: '%s'\n", buf); + SYSWARN("Invalid environment variable value ignored: '%s'", buf); } else { setenv(part[0], part[1], 1); } @@ -391,7 +391,7 @@ int conda_activate(const char *root, const char *env_name) { int fd = mkstemp(logfile); if (fd < 0) { - perror(logfile); + SYSERROR("log file creation failed: %s, %s", logfile, strerror(errno)); return -1; } close(fd); @@ -402,13 +402,13 @@ int conda_activate(const char *root, const char *env_name) { // Verify conda's init scripts are available if (access(path_conda, F_OK) < 0) { - perror(path_conda); + SYSERROR("conda is missing: %s, %s", path_conda, strerror(errno)); remove(logfile); return -1; } if (access(path_mamba, F_OK) < 0) { - perror(path_mamba); + SYSERROR("mamba is missing: %s, %s", path_mamba, strerror(errno)); remove(logfile); return -1; } @@ -514,7 +514,7 @@ int conda_check_required() { guard_free(cmd_out); guard_strlist_free(&result); } else { - msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "The base package requirement check could not be performed\n"); + SYSERROR("The base package requirement check could not be performed"); return 2; } return 0; @@ -559,7 +559,7 @@ int conda_setup_headless() { } if (conda_exec(cmd)) { - msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "Unable to install user-defined base packages (conda)\n"); + SYSERROR("Unable to install user-defined base packages (conda)"); return 1; } } @@ -582,21 +582,21 @@ int conda_setup_headless() { } if (pip_exec(cmd)) { - msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "Unable to install user-defined base packages (pip)\n"); + SYSERROR("Unable to install user-defined base packages (pip)"); return 1; } } if (conda_check_required()) { - msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "Your STASIS configuration lacks the bare" - " minimum software required to build conda packages." - " Please fix it.\n"); + SYSERROR("Your STASIS configuration lacks the bare" + " minimum software required to build conda packages." + " Please fix it."); return 1; } if (globals.always_update_base_environment) { if (conda_exec("update --all")) { - fprintf(stderr, "conda update was unsuccessful\n"); + SYSERROR("conda update was unsuccessful"); return 1; } } @@ -630,7 +630,7 @@ int conda_env_create_from_uri(char *name, char *uri, char *python_version) { const long http_code = download(uri_fs ? uri_fs : uri, tempfile, &errmsg); if (HTTP_ERROR(http_code)) { if (errmsg) { - fprintf(stderr, "download failed: %ld: %s\n", http_code, errmsg); + SYSERROR("download failed: %ld: %s", http_code, errmsg); guard_free(errmsg); } guard_free(uri_fs); diff --git a/src/lib/core/copy.c b/src/lib/core/copy.c index ed545cc..7666e67 100644 --- a/src/lib/core/copy.c +++ b/src/lib/core/copy.c @@ -5,7 +5,7 @@ int copy2(const char *src, const char *dest, unsigned int op) { SYSDEBUG("Stat source file: %s", src); if (lstat(src, &src_stat) < 0) { - perror(src); + SYSERROR("unable to stat source file: %s, %s", src, strerror(errno)); return -1; } @@ -37,12 +37,12 @@ int copy2(const char *src, const char *dest, unsigned int op) { } } else if (S_ISREG(src_stat.st_mode) && src_stat.st_nlink > 2 && src_stat.st_dev == dnamest.st_dev) { if (link(src, dest) < 0) { - perror(src); + SYSERROR("unable to link: %s, %s", src, strerror(errno)); return -1; } } else if (S_ISFIFO(src_stat.st_mode) || S_ISBLK(src_stat.st_mode) || S_ISCHR(src_stat.st_mode) || S_ISSOCK(src_stat.st_mode)) { if (mknod(dest, src_stat.st_mode, src_stat.st_rdev) < 0) { - perror(src); + SYSERROR("unable to mknod: %s, %s", dest, strerror(errno)); return -1; } } else if (S_ISREG(src_stat.st_mode)) { @@ -51,14 +51,14 @@ int copy2(const char *src, const char *dest, unsigned int op) { SYSDEBUG("Opening source file for reading"); FILE *fp1 = fopen(src, "rb"); if (!fp1) { - perror(src); + SYSERROR("unable to open source file for reading: %s, %s", src, strerror(errno)); return -1; } SYSDEBUG("Opening destination file for writing"); FILE *fp2 = fopen(dest, "w+b"); if (!fp2) { - perror(dest); + SYSERROR("unable to open destination file for writing: %s, %s", dest, strerror(errno)); fclose(fp1); return -1; } @@ -71,16 +71,16 @@ int copy2(const char *src, const char *dest, unsigned int op) { fclose(fp2); if (bytes_written != (size_t) src_stat.st_size) { - fprintf(stderr, "%s: SHORT WRITE (expected %zu bytes, but wrote %zu bytes)\n", dest, src_stat.st_size, bytes_written); + SYSDEBUG("%s: SHORT WRITE (expected %zu bytes, but wrote %zu bytes)", dest, src_stat.st_size, bytes_written); return -1; } if (op & CT_OWNER && chown(dest, src_stat.st_uid, src_stat.st_gid) < 0) { - perror(dest); + SYSERROR("unable to change owner: %s, %s", dest, strerror(errno)); } if (op & CT_PERM && chmod(dest, src_stat.st_mode) < 0) { - perror(dest); + SYSERROR("unable to chmod: %s, %s", dest, strerror(errno)); } } else { errno = EOPNOTSUPP; diff --git a/src/lib/core/download.c b/src/lib/core/download.c index ef1474f..28c5a6f 100644 --- a/src/lib/core/download.c +++ b/src/lib/core/download.c @@ -61,7 +61,7 @@ long download(char *url, const char *filename, char **errmsg) { CURL *c = curl_easy_init(); for (size_t retry = 0; retry < max_retries; retry++) { if (retry) { - fprintf(stderr, "[RETRY %zu/%zu] %s: %s\n", retry + 1, max_retries, *errmsg, url); + SYSWARN("[RETRY %zu/%zu] %s: %s", retry + 1, max_retries, *errmsg, url); } SYSDEBUG("Configuring curl"); diff --git a/src/lib/core/envctl.c b/src/lib/core/envctl.c index 5987616..9069a7f 100644 --- a/src/lib/core/envctl.c +++ b/src/lib/core/envctl.c @@ -85,9 +85,8 @@ unsigned envctl_get_flags(const struct EnvCtl *envctl, const char *name) { envctl_decode_index(poll_index, &state, &id, &name_id); if (!state) { return 0; - } else { - fprintf(stderr, "managed environment variable: %s\n", name); } + SYSINFO("managed environment variable: %s", name); return envctl->item[id]->flags; } @@ -106,14 +105,15 @@ void envctl_do_required(const struct EnvCtl *envctl, int verbose) { continue; } if (code == STASIS_ENVCTL_RET_FAIL) { - msg(STASIS_MSG_ERROR, "\n%s%s must be defined.\n", name, STASIS_COLOR_RESET); + SYSERROR("%s%s must be defined.", name, STASIS_COLOR_RESET); failed++; + continue; } - msg(STASIS_MSG_ERROR, "\nan unknown envctl callback code occurred: %d\n", code); + SYSWARN("an unknown envctl callback code occurred: %d", code); } if (failed) { - msg(STASIS_MSG_ERROR, "Environment check failed with %d error(s)\n", failed); + SYSERROR("Environment check failed with %d error(s)", failed); exit(1); } } diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index d81c3d1..4258004 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -74,7 +74,7 @@ void runtime_export(RuntimeEnv *env, char **keys) { char *_sh = getenv("SHELL"); char *sh = path_basename(_sh); if (sh == NULL) { - fprintf(stderr, "echo SHELL environment variable is not defined"); + SYSERROR("echo SHELL environment variable is not defined"); exit(1); } diff --git a/src/lib/core/github.c b/src/lib/core/github.c index 7c20fa4..992ab32 100644 --- a/src/lib/core/github.c +++ b/src/lib/core/github.c @@ -15,7 +15,7 @@ static size_t writer(const void *contents, size_t size, size_t nmemb, void *resu char *ptr = realloc(content->data, content->len + newlen + 1); if (!ptr) { - perror("realloc failed"); + SYSERROR("realloc failed"); return 0; } @@ -89,8 +89,7 @@ int get_github_release_notes(const char *api_token, const char *repo, const char curl_easy_cleanup(curl); if(res != CURLE_OK) { - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); + SYSERROR("curl_easy_perform() failed: %s", curl_easy_strerror(res)); return -1; } @@ -122,14 +121,14 @@ int get_github_release_notes(const char *api_token, const char *repo, const char if (data_mark) { *data_mark = '\0'; } - fprintf(stderr, "GitHub API Error: '%s'\n", data_offset); - fprintf(stderr, "URL: %s\n", endpoint_url); - fprintf(stderr, "POST: %s\n", endpoint_post_fields); + SYSERROR("GitHub API Error: '%s'", data_offset); + SYSERROR("URL: %s", endpoint_url); + SYSERROR("POST: %s", endpoint_post_fields); guard_free(content.data); return -1; } } else { - fprintf(stderr, "Unknown error\n"); + SYSERROR("Unknown error"); guard_free(content.data); return -1; } diff --git a/src/lib/core/ini.c b/src/lib/core/ini.c index 183aa6b..81c75ac 100644 --- a/src/lib/core/ini.c +++ b/src/lib/core/ini.c @@ -601,7 +601,8 @@ struct INIFILE *ini_open(const char *filename) { char *section_name = substring_between(line, "[]"); if (!section_name) { - fprintf(stderr, "error: invalid section syntax, line %zu: '%s'\n", i + 1, line); + SYSERROR("invalid section syntax, line %zu: '%s'", i + 1, line); + ini_free(&ini); return NULL; } diff --git a/src/lib/core/multiprocessing.c b/src/lib/core/multiprocessing.c index ffc6b95..f431dd3 100644 --- a/src/lib/core/multiprocessing.c +++ b/src/lib/core/multiprocessing.c @@ -24,7 +24,7 @@ static double get_task_interval_duration(const struct MultiProcessingTask *task) static void update_task_interval_start(struct MultiProcessingTask *task) { // Record the task stop time if (clock_gettime(CLOCK_REALTIME, &task->interval_data.t_start) < 0) { - perror("clock_gettime"); + SYSERROR("realtime clock unavailable"); exit(1); } } @@ -32,7 +32,7 @@ static void update_task_interval_start(struct MultiProcessingTask *task) { static void update_task_interval_elapsed(struct MultiProcessingTask *task) { // Record the interval stop time if (clock_gettime(CLOCK_REALTIME, &task->interval_data.t_stop) < 0) { - perror("clock_gettime"); + SYSERROR("realtime clock unavailable"); exit(1); } task->interval_data.duration = get_task_interval_duration(task); @@ -41,14 +41,14 @@ static void update_task_interval_elapsed(struct MultiProcessingTask *task) { static void update_task_start(struct MultiProcessingTask *task) { // Record the task start time if (clock_gettime(CLOCK_REALTIME, &task->time_data.t_start) < 0) { - perror("clock_gettime"); + SYSERROR("realtime clock unavailable"); exit(1); } } static void update_task_elapsed(struct MultiProcessingTask *task) { // Record the task stop time if (clock_gettime(CLOCK_REALTIME, &task->time_data.t_stop) < 0) { - perror("clock_gettime"); + SYSERROR("realtime clock unavailable"); exit(1); } task->time_data.duration = get_task_duration(task); @@ -76,22 +76,25 @@ int child(struct MultiProcessingPool *pool, struct MultiProcessingTask *task) { if (globals.enable_task_logging) { snprintf(task->log_file + strlen(task->log_file), sizeof(task->log_file) - strlen(task->log_file), "task-%zu-%d.log", mp_global_task_count, task->parent_pid); + SYSDEBUG("using log file: %s", task->log_file); } fp_log = freopen(task->log_file, "w+", stdout); if (!fp_log) { fprintf(stderr, "unable to open '%s' for writing: %s\n", task->log_file, strerror(errno)); + SYSERROR("unable to open '%s' for writing: %s", task->log_file, strerror(errno)); return -1; } int fd = -1; if ((fd = dup2(STDOUT_FILENO, STDERR_FILENO)) < 0) { SYSERROR("%s", "Unable to redirect stderr to stdout"); + SYSERROR("Unable to redirect stderr to stdout"); fclose(fp_log); return -1; } // Generate timestamp for log header - time_t t = time(NULL); + const time_t t = time(NULL); char *timebuf = ctime(&t); if (timebuf) { // strip line feed from timestamp @@ -131,7 +134,7 @@ int parent(struct MultiProcessingPool *pool, struct MultiProcessingTask *task, p // Check child's status pid_t code = waitpid(pid, child_status, WUNTRACED | WCONTINUED | WNOHANG); if (code < 0) { - perror("waitpid failed"); + SYSERROR("waitpid failed"); return -1; } return 0; @@ -144,6 +147,7 @@ static int mp_task_fork(struct MultiProcessingPool *pool, struct MultiProcessing int parent_status = 0; int child_status = 0; if (pid == -1) { + SYSERROR("fork failed"); return -1; } if (pid == 0) { @@ -164,7 +168,7 @@ struct MultiProcessingTask *mp_pool_task(struct MultiProcessingPool *pool, const SYSDEBUG("Using slot %zu of %zu", pool->num_used, pool->num_alloc); pool->num_used++; } else { - fprintf(stderr, "Maximum number of tasks reached\n"); + SYSERROR("Maximum number of tasks reached"); return NULL; } @@ -268,6 +272,7 @@ void mp_pool_show_summary(struct MultiProcessingPool *pool) { static int show_log_contents(FILE *stream, struct MultiProcessingTask *task) { FILE *fp = fopen(task->log_file, "r"); if (!fp) { + SYSERROR("Failed to open log file for reading: %s, %s", task->log_file, strerror(errno)); return -1; } char buf[STASIS_BUFSIZ] = {0}; @@ -296,7 +301,7 @@ int mp_pool_kill(struct MultiProcessingPool *pool, int signum) { status = kill(slot->pid, signum); semaphore_post(&pool->semaphore); if (status && errno != ESRCH) { - fprintf(stderr, "Task '%s' (pid: %d) did not respond: %s\n", slot->ident, slot->pid, strerror(errno)); + SYSERROR("Task '%s' (pid: %d) did not respond: %s", slot->ident, slot->pid, strerror(errno)); } else { // Wait for process to handle the signal, then set the status accordingly if (waitpid(slot->pid, &status, 0) >= 0) { @@ -349,7 +354,7 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { if (slot->status == MP_POOL_TASK_STATUS_INITIAL) { slot->_startup = time(NULL); if (mp_task_fork(pool, slot)) { - fprintf(stderr, "%s: mp_task_fork failed\n", slot->ident); + SYSERROR("%s: mp_task_fork failed", slot->ident); kill(0, SIGTERM); } } @@ -363,7 +368,7 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { // forever. This protects the program from entering an // infinite loop. SYSDEBUG("slot %zu: hang_check=%zu >= pool->num_used=%zu", i, hang_check, pool->num_used); - SYSERROR("%s is deadlocked\n", pool->ident); + SYSERROR("%s is deadlocked", pool->ident); failures++; goto pool_deadlocked; } @@ -422,13 +427,13 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { printf("%s Task ended (status: %d)\n", progress, status_exit); tasks_complete++; } else { - fprintf(stderr, "%s Task state is unknown (0x%04X)\n", progress, status); + SYSWARN("%s Task state is unknown (0x%04X)", progress, status); } if (globals.enable_task_logging) { // Show the log (always) if (show_log_contents(stdout, slot)) { - perror(slot->log_file); + SYSWARN("%s Task has no log file", slot->ident); } } @@ -452,17 +457,17 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { // Clean up logs and scripts left behind by the task if (globals.enable_task_logging) { if (remove(slot->log_file)) { - fprintf(stderr, "%s Unable to remove log file: '%s': %s\n", progress, slot->parent_script, strerror(errno)); + SYSWARN("%s Unable to remove log file: '%s': %s", progress, slot->parent_script, strerror(errno)); } } if (remove(slot->parent_script)) { - fprintf(stderr, "%s Unable to remove temporary script '%s': %s\n", progress, slot->parent_script, strerror(errno)); + SYSWARN("%s Unable to remove temporary script '%s': %s", progress, slot->parent_script, strerror(errno)); } // Update progress and tell the poller to ignore the PID. The process is gone. slot->pid = MP_POOL_PID_UNUSED; } else if (pid < 0) { - fprintf(stderr, "waitpid failed: %s\n", strerror(errno)); + SYSERROR("waitpid failed: %s", strerror(errno)); return -1; } else { // Track the number of seconds elapsed for each task. @@ -533,26 +538,29 @@ struct MultiProcessingPool *mp_pool_init(const char *ident, const char *log_root pool->num_alloc = MP_POOL_TASK_MAX; // Create the log directory + SYSDEBUG("Creating log directory: %s", pool->log_root); if (mkdirs(log_root, 0700) < 0) { if (errno != EEXIST) { - perror(log_root); + SYSERROR("%s: unable to create directory", log_root); mp_pool_free(&pool); return NULL; } } // Task array is shared with children + SYSDEBUG("Memory mapping pool task array"); pool->task = mmap(NULL, (pool->num_alloc + 1) * sizeof(*pool->task), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (pool->task == MAP_FAILED) { - perror("mmap"); + SYSERROR("unable to memory map pool task"); mp_pool_free(&pool); return NULL; } + SYSDEBUG("initializing pool semaphore"); char semaphore_name[255] = {0}; snprintf(semaphore_name, sizeof(semaphore_name), "stasis_mp_%s", ident); if (semaphore_init(&pool->semaphore, semaphore_name, 1) != 0) { - fprintf(stderr, "unable to initialize semaphore\n"); + SYSERROR("unable to initialize pool semaphore"); mp_pool_free(&pool); return NULL; } @@ -563,6 +571,7 @@ struct MultiProcessingPool *mp_pool_init(const char *ident, const char *log_root } void mp_pool_free(struct MultiProcessingPool **pool) { + SYSDEBUG("freeing pool"); if (!isempty((*pool)->semaphore.name)) { semaphore_destroy(&(*pool)->semaphore); } @@ -581,8 +590,9 @@ void mp_pool_free(struct MultiProcessingPool **pool) { // Unmap the pool if ((*pool)) { if (munmap((*pool), sizeof(*(*pool))) < 0) { - perror("munmap"); + SYSWARN("munmap failed: %s", strerror(errno)); } (*pool) = NULL; } + SYSDEBUG("pool freed"); } \ No newline at end of file diff --git a/src/lib/core/recipe.c b/src/lib/core/recipe.c index e5769bb..0ee1ef8 100644 --- a/src/lib/core/recipe.c +++ b/src/lib/core/recipe.c @@ -21,8 +21,8 @@ int recipe_clone(char *recipe_dir, char *url, char *gitref, char **result) { if (!access(destdir, F_OK)) { if (!strcmp(destdir, "/")) { - fprintf(stderr, "STASIS is misconfigured. Please check your output path(s) immediately.\n"); - fprintf(stderr, "recipe_dir = '%s'\nreponame = '%s'\ndestdir = '%s'\n", + SYSERROR("STASIS is misconfigured. Please check your output path(s) immediately."); + SYSERROR("recipe_dir = '%s'\nreponame = '%s'\ndestdir = '%s'", recipe_dir, reponame, destdir); exit(1); } diff --git a/src/lib/core/relocation.c b/src/lib/core/relocation.c index 348bad1..8204101 100644 --- a/src/lib/core/relocation.c +++ b/src/lib/core/relocation.c @@ -112,7 +112,7 @@ int file_replace_text(const char* filename, const char* target, const char* repl FILE *fp = fopen(filename, "r"); if (!fp) { - fprintf(stderr, "unable to open for reading: %s\n", filename); + SYSERROR("unable to open for reading: %s", filename); return -1; } diff --git a/src/lib/core/str.c b/src/lib/core/str.c index 5df7372..501c232 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -212,7 +212,8 @@ char *join_ex(char *separator, ...) { // Initialize array argv = calloc(argc + 1, sizeof(char **)); if (argv == NULL) { - perror("join_ex calloc failed"); + SYSERROR("calloc failed: %s", strerror(errno)); + va_end(ap); return NULL; } @@ -546,7 +547,7 @@ char *normalize_space(char *s) { } if (!(tmp = strdup(s))) { - perror("could not allocate memory for temporary string"); + SYSERROR("could not allocate memory for temporary string"); return NULL; } char *tmp_orig = tmp; diff --git a/src/lib/core/strlist.c b/src/lib/core/strlist.c index 0d25a66..526a1c9 100644 --- a/src/lib/core/strlist.c +++ b/src/lib/core/strlist.c @@ -45,7 +45,7 @@ void strlist_append(struct StrList **pStrList, char *str) { tmp = realloc((*pStrList)->data, ((*pStrList)->num_alloc + 1) * sizeof(char *)); if (tmp == NULL) { guard_strlist_free(pStrList); - perror("failed to append to array"); + SYSERROR("failed to append to array: %s", strerror(errno)); exit(1); } (*pStrList)->data = tmp; @@ -437,7 +437,7 @@ void strlist_set(struct StrList **pStrList, size_t index, char *value) { } else { tmp = realloc((*pStrList)->data[index], (strlen(value) + 1) * sizeof(char *)); if (!tmp) { - perror("realloc strlist_set replacement value"); + SYSERROR("strlist_set replacement realloc failed: %s", strerror(errno)); return; } else if (tmp != (*pStrList)->data[index]) { (*pStrList)->data[index] = tmp; @@ -750,7 +750,7 @@ long double strlist_item_as_long_double(struct StrList *pStrList, size_t index) struct StrList *strlist_init() { struct StrList *pStrList = calloc(1, sizeof(struct StrList)); if (pStrList == NULL) { - perror("failed to allocate array"); + SYSERROR("failed to allocate array: %s", strerror(errno)); return NULL; } pStrList->num_inuse = 0; diff --git a/src/lib/core/system.c b/src/lib/core/system.c index 06ca7bf..f0fd38f 100644 --- a/src/lib/core/system.c +++ b/src/lib/core/system.c @@ -41,7 +41,7 @@ int shell(struct Process *proc, char *args) { pid_t pid = fork(); if (pid == -1) { - fprintf(stderr, "fork failed\n"); + SYSERROR("fork failed"); guard_free(t_name); fclose(tp); exit(1); @@ -52,7 +52,7 @@ int shell(struct Process *proc, char *args) { if (strlen(proc->f_stdout)) { fp_out = freopen(proc->f_stdout, "w+", stdout); if (!fp_out) { - fprintf(stderr, "Unable to redirect stdout to %s: %s\n", proc->f_stdout, strerror(errno)); + SYSERROR("Unable to redirect stdout to %s: %s", proc->f_stdout, strerror(errno)); exit(1); } } @@ -61,7 +61,7 @@ int shell(struct Process *proc, char *args) { if (!proc->redirect_stderr) { fp_err = freopen(proc->f_stderr, "w+", stderr); if (!fp_err) { - fprintf(stderr, "Unable to redirect stderr to %s: %s\n", proc->f_stdout, strerror(errno)); + SYSERROR("Unable to redirect stderr to %s: %s", proc->f_stdout, strerror(errno)); if (fp_out) { fclose(fp_out); } @@ -73,7 +73,7 @@ int shell(struct Process *proc, char *args) { if (proc->redirect_stderr) { if (fp_err) { if (dup2(fileno(fp_err), STDERR_FILENO) < 0) { - fprintf(stderr, "Unable to redirect stderr to %s: %s\n", proc->f_stderr, strerror(errno)); + SYSERROR("Unable to redirect stderr to %s: %s", proc->f_stderr, strerror(errno)); if (fp_out) { fclose(fp_out); } @@ -83,7 +83,7 @@ int shell(struct Process *proc, char *args) { } else { // redirect stderr to stdout if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) { - fprintf(stderr, "Unable to redirect stderr to stdout: %s\n", strerror(errno)); + SYSERROR("Unable to redirect stderr to stdout: %s", strerror(errno)); if (fp_out) { fclose(fp_out); } @@ -97,13 +97,13 @@ int shell(struct Process *proc, char *args) { if (waitpid(pid, &status, WUNTRACED) > 0) { if (WIFEXITED(status) && WEXITSTATUS(status)) { if (WEXITSTATUS(status) == 127) { - fprintf(stderr, "execv failed\n"); + SYSERROR("execv failed"); } } else if (WIFSIGNALED(status)) { - fprintf(stderr, "signal received: %d\n", WIFSIGNALED(status)); + SYSWARN("signal received: %d", WIFSIGNALED(status)); } } else { - fprintf(stderr, "waitpid() failed\n"); + SYSERROR("waitpid() failed"); } } diff --git a/src/lib/core/template.c b/src/lib/core/template.c index cdf8e58..00e0058 100644 --- a/src/lib/core/template.c +++ b/src/lib/core/template.c @@ -168,7 +168,7 @@ char *tpl_render(char *str) { output = calloc(output_bytes, sizeof(*output)); if (!output) { - perror("unable to allocate output buffer"); + SYSERROR("unable to allocate output buffer: %s", strerror(errno)); return NULL; } @@ -218,7 +218,7 @@ char *tpl_render(char *str) { // Find closing brace b_close = strstr(&pos[off], "}}"); if (!b_close) { - fprintf(stderr, "error while templating '%s'\n\nunbalanced brace at position %zu\n", str, z); + SYSERROR("while templating '%s'\n\nunbalanced brace at position %zu", str, z); guard_free(output); return NULL; } else { @@ -240,7 +240,7 @@ char *tpl_render(char *str) { char *param_begin = strchr(func_name_temp, '('); if (!param_begin) { - fprintf(stderr, "At position %zu in %s\nfunction name must be followed by a '('\n", off, key); + SYSERROR("At position %zu in %s\nfunction name must be followed by a '('", off, key); guard_free(output); return NULL; } @@ -248,7 +248,7 @@ char *tpl_render(char *str) { param_begin++; char *param_end = strrchr(param_begin, ')'); if (!param_end) { - fprintf(stderr, "At position %zu in %s\nfunction arguments must be closed with a ')'\n", off, key); + SYSERROR("At position %zu in %s\nfunction arguments must be closed with a ')'", off, key); guard_free(output); return NULL; } @@ -265,7 +265,7 @@ char *tpl_render(char *str) { return NULL; } if (params_count > frame->argc || params_count < frame->argc) { - fprintf(stderr, "At position %zu in %s\nIncorrect number of arguments for function: %s (expected %d, got %d)\n", off, key, frame->key, frame->argc, params_count); + SYSERROR("At position %zu in %s\nIncorrect number of arguments for function: %s (expected %d, got %d)", off, key, frame->key, frame->argc, params_count); value = strdup(""); } else { for (size_t p = 0; p < sizeof(frame->argv) / sizeof(*frame->argv) && params[p] != NULL; p++) { @@ -276,7 +276,7 @@ char *tpl_render(char *str) { char *func_result = NULL; int func_status = 0; if ((func_status = frame->func(frame, &func_result))) { - fprintf(stderr, "%s returned non-zero status: %d\n", frame->key, func_status); + SYSERROR("%s returned non-zero status: %d", frame->key, func_status); } value = strdup(func_result ? func_result : ""); SYSDEBUG("Returned from function: %s (status: %d)\nData OUT\n--------\n'%s'", k, func_status, value); diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index 264f23d..54c3dce 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -182,7 +182,6 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn } if (fp == NULL) { - perror(filename); SYSERROR("failed to open %s for reading", filename); return NULL; } @@ -303,7 +302,7 @@ int touch(const char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) { - perror(filename); + SYSERROR("unable to open %s for writing", filename); return 1; } fclose(fp); @@ -385,7 +384,11 @@ char *git_describe(const char *path) { if (!pp) { return NULL; } - fgets(version, sizeof(version) - 1, pp); + if (fgets(version, sizeof(version) - 1, pp) == NULL) { + pclose(pp); + popd(); + return NULL; + } strip(version); pclose(pp); popd(); @@ -398,7 +401,7 @@ char *git_rev_parse(const char *path, char *args) { memset(version, 0, sizeof(version)); if (isempty(args)) { - fprintf(stderr, "git_rev_parse args cannot be empty\n"); + SYSERROR("git_rev_parse args cannot be empty"); return NULL; } @@ -514,6 +517,10 @@ char *xmkstemp(FILE **fp, const char *mode) { snprintf(t_name, sizeof(t_name), "%s/%s", tmpdir, "STASIS.XXXXXX"); fd = mkstemp(t_name); + if (fd < 0) { + SYSERROR("unable to create temporary file: %s", t_name); + return NULL; + } *fp = fdopen(fd, mode); if (!*fp) { // unable to open, die @@ -632,11 +639,12 @@ int xml_pretty_print_in_place(const char *filename, const char *pretty_print_pro } if (copy2(tempfile, filename, CT_PERM)) { - goto pretty_print_failed; + SYSWARN("unable to copy: '%s' -> '%s'", tempfile, filename); } if (remove(tempfile)) { goto pretty_print_failed; + SYSWARN("unable to remove temporary file: %s", tempfile); } fclose(fp); @@ -891,9 +899,15 @@ int env_manipulate_pathstr(const char *key, char *path, int mode) { char *system_path_new = NULL; if (mode & PM_APPEND) { - asprintf(&system_path_new, "%s%s%s", system_path_old, PATH_SEP, path); + if (asprintf(&system_path_new, "%s%s%s", system_path_old, PATH_SEP, path) < 0 || !system_path_new) { + SYSERROR("%s", "Unable to allocate memory to update PATH"); + return -1; + } } else if (mode & PM_PREPEND) { - asprintf(&system_path_new, "%s%s%s", path, PATH_SEP, system_path_old); + if (asprintf(&system_path_new, "%s%s%s", path, PATH_SEP, system_path_old) < 0 || !system_path_new) { + SYSERROR("%s", "Unable to allocate memory to update PATH"); + return -1; + } } if (!system_path_new) { @@ -990,12 +1004,10 @@ int grow(const size_t size_new, size_t *size_orig, char **data) { char *tmp = realloc(*data, new_size); if (!tmp) { - perror("realloc failed"); + SYSERROR("realloc failed"); return -1; } - if (tmp != *data) { - *data = tmp; - } + *data = tmp; *size_orig = new_size; } return 0; @@ -1041,7 +1053,7 @@ static int read_vcs_records(const size_t line, char **data) { const char *vcs = vcs_name[i]; char *data_local = strdup(*data); if (!data_local) { - fprintf(stderr, "Out of memory\n"); + SYSERROR("out of memory"); return -1; } diff --git a/src/lib/core/wheel.c b/src/lib/core/wheel.c index ef491c9..ea2089a 100644 --- a/src/lib/core/wheel.c +++ b/src/lib/core/wheel.c @@ -139,7 +139,7 @@ static ssize_t wheel_parse_wheel(struct Wheel * pkg, const char * data) { break; } default: - fprintf(stderr, "warning: unhandled wheel key on line %zu:\nbuffer contents: '%s'\n", i, value); + SYSWARN("unhandled wheel key on line %zu:\nbuffer contents: '%s'", i, value); break; } guard_free(key); @@ -618,7 +618,7 @@ static ssize_t wheel_parse_metadata(struct WheelMetadata * const pkg, const char } case WHEEL_KEY_UNKNOWN: default: - fprintf(stderr, "warning: unhandled metadata key on line %zu:\nbuffer contents: '%s'\n", i, value); + SYSWARN("unhandled metadata key on line %zu:\nbuffer contents: '%s'", i, value); break; } guard_free(key); @@ -1205,7 +1205,7 @@ int wheel_show_info(const struct Wheel *wheel) { for (ssize_t i = 0; i < WHEEL_DIST_END_ENUM; i++) { const char *key = wheel_get_key_by_id(WHEEL_FROM_DIST, i); if (!key) { - fprintf(stderr, "wheel_get_key_by_id(%zi) failed\n", i); + SYSERROR("wheel_get_key_by_id(%zi) failed", i); return -1; } @@ -1213,7 +1213,7 @@ int wheel_show_info(const struct Wheel *wheel) { fflush(stdout); const struct WheelValue dist = wheel_get_value_by_id(wheel, WHEEL_FROM_DIST, i); if (wheel_value_error(&dist)) { - fprintf(stderr, "wheel_get_value_by_id(%zi) failed\n", i); + SYSERROR("wheel_get_value_by_id(%zi) failed", i); return -1; } switch (dist.type) { @@ -1274,7 +1274,7 @@ int wheel_show_info(const struct Wheel *wheel) { for (ssize_t i = 0; i < WHEEL_META_END_ENUM; i++) { const char *key = wheel_get_key_by_id(WHEEL_FROM_METADATA, i); if (!key) { - fprintf(stderr, "wheel_get_key_by_id(%zi) failed\n", i); + SYSERROR("wheel_get_key_by_id(%zi) failed", i); return -1; } printf("%s: ", key); @@ -1282,7 +1282,7 @@ int wheel_show_info(const struct Wheel *wheel) { const struct WheelValue pkg = wheel_get_value_by_id(wheel, WHEEL_FROM_METADATA, i); if (wheel_value_error(&pkg)) { - fprintf(stderr, "wheel_get_value_by_id(%zi) failed\n", i); + SYSERROR("wheel_get_value_by_id(%zi) failed", i); return -1; } switch (pkg.type) { -- cgit