From af129392bdb77b3e37afa69d4ea4e0fa58ff7500 Mon Sep 17 00:00:00 2001 From: Fotios Valasiadis Date: Mon, 26 Aug 2024 04:03:29 +0300 Subject: [PATCH] Replace GNU error(3) with POSIX perror and fprintf to add support for non-gnu operating systems. Signed-off-by: Fotios Valasiadis --- src/hash.c | 20 +++++++----- src/main.c | 7 +++-- src/record.c | 10 +++--- src/tracer.c | 87 ++++++++++++++++++++++++++++++++++------------------ 4 files changed, 81 insertions(+), 43 deletions(-) diff --git a/src/hash.c b/src/hash.c index fe6e6e9..43c7f48 100644 --- a/src/hash.c +++ b/src/hash.c @@ -7,7 +7,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later #include "config.h" #include // errno(3) -#include // error(3) #include // open(2) #include // PATH_MAX #include // uint8_t @@ -59,19 +58,22 @@ hash_file_contents(char *name, size_t sz) int fd = open(name, O_RDONLY); if (fd < 0) { - error(0, errno, "open `%s'", name); + fprintf(stderr, "hash.c:hash_file_contents():open(%s): %s", name, + strerror(errno)); return NULL; } char *buf = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0); if (buf == MAP_FAILED) { - error(0, errno, "mmaping `%s'", name); + fprintf(stderr, "hash.c:hash_file_contents():mmap(%s): %s", name, + strerror(errno)); return NULL; } int ret = madvise(buf, sz, MADV_SEQUENTIAL); if (ret) { - error(0, errno, "madvise `%s'", name); + fprintf(stderr, "hash.c:hash_file_contents():madvise(%s): %s", name, + strerror(errno)); } char pre[32]; @@ -83,7 +85,8 @@ hash_file_contents(char *name, size_t sz) hash = malloc(SHA1_OUTPUT_LEN); if (hash == NULL) { - error(0, errno, "malloc output on `%s'", name); + fprintf(stderr, "hash.c:hash_file_contents():malloc(%s): %s", name, + strerror(errno)); return NULL; } @@ -104,7 +107,9 @@ hash_file_contents(char *name, size_t sz) close(fd); if (munmap(buf, sz) < 0) { - error(EXIT_FAILURE, errno, "unmapping `%s'", name); + fprintf(stderr, "hash.c:hash_file_contents():munmap(%s): %s", name, + strerror(errno)); + exit(EXIT_FAILURE); } return hash; @@ -116,7 +121,8 @@ get_file_hash(char *fname) struct stat fstat; if (stat(fname, &fstat)) { - error(0, errno, "getting info on `%s'", fname); + fprintf(stderr, "hash.c:get_file_hash():stat(%s): %s", fname, + strerror(errno)); return NULL; } if (S_ISREG(fstat.st_mode) || S_ISLNK(fstat.st_mode)) { diff --git a/src/main.c b/src/main.c index 1cddfa3..72e69fe 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later #include "config.h" -#include #include #include #include @@ -21,8 +20,10 @@ void run_and_record_fnames(char **av, char **envp); int main(int argc, char **argv, char **envp) { - if (argc < 2) - error(EX_USAGE, 0, "missing command to record"); + if (argc < 2) { + fprintf(stderr, "main.c:main(): Missing command to record"); + exit(EX_USAGE); + } char *output_fname = "build-recorder.out"; diff --git a/src/record.c b/src/record.c index 1434d19..4821ded 100644 --- a/src/record.c +++ b/src/record.c @@ -17,7 +17,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later #include #include #include -#include #include FILE *fout; @@ -63,7 +62,8 @@ get_cmdline(pid_t pid) int fd = open(cmd_fname, O_RDONLY); if (fd < 0) { - error(EXIT_FAILURE, errno, "on get_cmdline open"); + perror("record.c:get_cmdline():open(cmd_fname)"); + exit(EXIT_FAILURE); } #define CMD_LINE_SIZE 1023 char data[CMD_LINE_SIZE + 1]; @@ -72,7 +72,8 @@ get_cmdline(pid_t pid) close(fd); if (n < 0) { - error(EXIT_FAILURE, errno, "on get_cmdline read"); + perror("record.c:get_cmdline:read(cmd_fname)"); + exit(EXIT_FAILURE); } else if (n == 0) { return NULL; } @@ -97,7 +98,8 @@ get_cmdline(pid_t pid) char *ret = malloc(sz); if (ret == NULL) { - error(EXIT_FAILURE, errno, "on get_cmdline malloc"); + perror("record.c:get_cmdline:malloc(cmd_fname)"); + exit(EXIT_FAILURE); } *ret = '\0'; diff --git a/src/tracer.c b/src/tracer.c index 3309a09..3855f43 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -8,7 +8,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later #include "config.h" #include -#include #include #include #include @@ -70,12 +69,16 @@ next_pinfo(pid_t pid) if (numpinfo == pinfo_size - 1) { pinfo_size *= 2; pinfo = reallocarray(pinfo, pinfo_size, sizeof (PROCESS_INFO)); - if (pinfo == NULL) - error(EXIT_FAILURE, errno, "reallocating process info array"); + if (pinfo == NULL) { + perror("tracer.c:next_pinfo:reallocarray(pinfo)"); + exit(EXIT_FAILURE); + } pids = reallocarray(pids, pinfo_size, sizeof (int)); - if (pids == NULL) - error(EXIT_FAILURE, errno, "reallocating pids array"); + if (pids == NULL) { + perror("tracer.c:next_pinfo:reallocarray(pids)"); + exit(EXIT_FAILURE); + } } pids[numpinfo + 1] = pid; @@ -88,8 +91,10 @@ next_finfo(void) if (numfinfo == finfo_size - 1) { finfo_size *= 2; finfo = reallocarray(finfo, finfo_size, sizeof (FILE_INFO)); - if (finfo == NULL) - error(EXIT_FAILURE, errno, "reallocating file info array"); + if (finfo == NULL) { + perror("tracer.c:next_finfo:reallocarray(finfo)"); + exit(EXIT_FAILURE); + } } return finfo + (++numfinfo); @@ -184,8 +189,10 @@ pinfo_next_finfo(PROCESS_INFO *self, int fd) reallocarray(self->finfo, self->finfo_size, sizeof (FILE_INFO)); self->fds = reallocarray(self->fds, self->finfo_size, sizeof (FILE_INFO)); - if (self->finfo == NULL) - error(EXIT_FAILURE, errno, "reallocating file info array"); + if (self->finfo == NULL) { + perror("tracer.c:pinfo_next_finfo:reallocarray(pinfo->finfo)"); + exit(EXIT_FAILURE); + } } self->fds[self->numfinfo + 1] = fd; @@ -254,7 +261,9 @@ find_in_path(char *path) sprintf(buf, "%s/%s", it, path); ret = realpath(buf, NULL); if (!ret && (errno != 0 && errno != ENOENT)) { - error(EXIT_FAILURE, errno, "on find_in_path realpath"); + fprintf(stderr, "tracer.c:find_in_path:realpath(%s): %s\n", path, + strerror(errno)); + exit(EXIT_FAILURE); } it = last + 1; } while (last != NULL && ret == NULL); @@ -271,8 +280,11 @@ handle_open(pid_t pid, PROCESS_INFO *pi, int fd, int dirfd, void *path, path = get_str_from_process(pid, path); char *abspath = absolutepath(pid, dirfd, path); - if (abspath == NULL) - error(EXIT_FAILURE, errno, "on handle_open absolutepath"); + if (abspath == NULL) { + fprintf(stderr, "tracer.c:handle_open:absolutepath(%s): %s\n", path, + strerror(errno)); + exit(EXIT_FAILURE); + } FILE_INFO *f = NULL; @@ -308,13 +320,17 @@ handle_execve(pid_t pid, PROCESS_INFO *pi, int dirfd, char *path) if (!abspath) { if (errno != ENOENT) { - error(EXIT_FAILURE, errno, "on handle_execve absolutepath"); + fprintf(stderr, "tracer.c:handle_execve:absolutepath(%s): %s\n", + path, strerror(errno)); + exit(EXIT_FAILURE); } abspath = find_in_path(path); if (!abspath) { - error(EXIT_FAILURE, errno, "on handle_execve find_in_path"); + fprintf(stderr, "tracer.c:handle_execve:find_in_path(%s): %s\n", + path, strerror(errno)); + exit(EXIT_FAILURE); } } @@ -357,8 +373,10 @@ handle_rename_entry(pid_t pid, PROCESS_INFO *pi, int olddirfd, char *oldpath) } pi->entry_info = (void *) (f - finfo); - if (pi->entry_info == NULL) - error(EXIT_FAILURE, errno, "on handle_rename_entry absolutepath"); + if (pi->entry_info == NULL) { + fprintf(stderr, "tracer.c:handle_rename_entry(%s)", f->path); + exit(EXIT_FAILURE); + } } static void @@ -603,14 +621,16 @@ tracer_main(pid_t pid, PROCESS_INFO *pi, char *path, char **envp) // Starting tracee if (ptrace(PTRACE_SYSCALL, pid, NULL, NULL) < 0) { - error(EXIT_FAILURE, errno, "tracee PTRACE_SYSCALL failed"); + perror("tracer.c:tracer_main():ptrace(tracee PTRACE_SYSCALL)"); + exit(EXIT_FAILURE); } while (running) { pid = wait(&status); if (pid < 0) { - error(EXIT_FAILURE, errno, "wait failed"); + perror("tracer.c:tracer_main():wait()"); + exit(EXIT_FAILURE); } unsigned int restart_sig = 0; @@ -620,14 +640,16 @@ tracer_main(pid_t pid, PROCESS_INFO *pi, char *path, char **envp) case SIGTRAP | 0x80: process_state = find_pinfo(pid); if (!process_state) { - error(EXIT_FAILURE, 0, "find_pinfo on syscall sigtrap"); + fprintf(stderr, + "tracer.c:tracer_main():find_pinfo() on syscall sigtrap\n"); + exit(EXIT_FAILURE); } if (ptrace (PTRACE_GET_SYSCALL_INFO, pid, (void *) sizeof (info), &info) < 0) { - error(EXIT_FAILURE, errno, - "tracee PTRACE_GET_SYSCALL_INFO failed"); + perror("tracer.c:tracer_main():ptrace(PTRACE_GET_SYSCALL_INFO)"); + exit(EXIT_FAILURE); } switch (info.op) { @@ -640,8 +662,10 @@ tracer_main(pid_t pid, PROCESS_INFO *pi, char *path, char **envp) &process_state->state, &info); break; default: - error(EXIT_FAILURE, errno, - "expected PTRACE_SYSCALL_INFO_ENTRY or PTRACE_SYSCALL_INFO_EXIT\n"); + fprintf(stderr, + "tracer.c:tracer_main():WSTOPSIG(%d) expected PTRACE_SYSCALL_INFO_ENTRY or PTRACE_SYSCALL_INFOO_EXIT: %s", + WSTOPSIG(status), strerror(errno)); + exit(EXIT_FAILURE); } break; @@ -672,14 +696,17 @@ tracer_main(pid_t pid, PROCESS_INFO *pi, char *path, char **envp) // Restarting process if (ptrace(PTRACE_SYSCALL, pid, NULL, restart_sig) < 0) { - error(EXIT_FAILURE, errno, "failed restarting process"); + perror("tracer.c:tracer_main():ptrace(): failed restarting process"); + exit(EXIT_FAILURE); } } else if (WIFEXITED(status)) { // child process exited --running; process_state = find_pinfo(pid); if (!process_state) { - error(EXIT_FAILURE, 0, "find_pinfo on WIFEXITED"); + fprintf(stderr, + "tracer.c:tracer_main():find_pinfo on WIFEXITED\n"); + exit(EXIT_FAILURE); } record_process_end(process_state->outname); @@ -716,7 +743,8 @@ run_tracee(char **av) { ptrace(PTRACE_TRACEME, NULL, NULL, NULL); execvp(*av, av); - error(EXIT_FAILURE, errno, "after child exec()"); + perror("tracer.c:run_tracee():execvp(): after child exec()"); + exit(EXIT_FAILURE); } void @@ -725,9 +753,10 @@ run_and_record_fnames(char **av, char **envp) pid_t pid; pid = fork(); - if (pid < 0) - error(EXIT_FAILURE, errno, "in original fork()"); - else if (pid == 0) + if (pid < 0) { + perror("in original fork()"); + exit(EXIT_FAILURE); + } else if (pid == 0) run_tracee(av); init();