Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace GNU error(3) with POSIX perror and fprintf to add support for non-gnu operating systems. #223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later
#include "config.h"

#include <errno.h> // errno(3)
#include <error.h> // error(3)
#include <fcntl.h> // open(2)
#include <limits.h> // PATH_MAX
#include <stdint.h> // uint8_t
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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)) {
Expand Down
7 changes: 4 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later

#include "config.h"

#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -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";

Expand Down
10 changes: 6 additions & 4 deletions src/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>

FILE *fout;
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
Expand All @@ -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';
Expand Down
87 changes: 58 additions & 29 deletions src/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ SPDX-License-Identifier: LGPL-2.1-or-later
#include "config.h"

#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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("tracer.c:run_and_record_fnames(): in original fork()");
exit(EXIT_FAILURE);
} else if (pid == 0)
run_tracee(av);

init();
Expand Down