From 1989c3688631c8f82455fb51b6f185063e17b659 Mon Sep 17 00:00:00 2001 From: Aaron Cook Date: Mon, 8 May 2023 10:00:37 -0500 Subject: [PATCH] #10 Process specific backup log files A process will open outbuf/inbuf.pid.log and move it to the outbuf/inbuf.log upon close, allowing for multiple processes to be open at once --- src/bytenuts.c | 6 +++++- src/cheerios.c | 53 ++++++++++++++++++++++++++++++++++++++++++-------- src/cheerios.h | 5 +++-- src/ingest.c | 52 +++++++++++++++++++++++++++++++++++++++---------- src/ingest.h | 1 + 5 files changed, 96 insertions(+), 21 deletions(-) diff --git a/src/bytenuts.c b/src/bytenuts.c index f8978f3..b4b158d 100644 --- a/src/bytenuts.c +++ b/src/bytenuts.c @@ -414,10 +414,14 @@ read_state() { struct stat st; char *buf; - char *cwd = getcwd(NULL, 0); + char *cwd; char *home = getenv("HOME"); FILE *fd; + if (!home) + return 0; + + cwd = getcwd(NULL, 0); chdir(home); fd = fopen(".config/bytenuts/inbuf.log", "r"); diff --git a/src/cheerios.c b/src/cheerios.c index 1cff23b..6d27b0e 100644 --- a/src/cheerios.c +++ b/src/cheerios.c @@ -25,7 +25,6 @@ int cheerios_start(bytenuts_t *bytenuts) { char *home = getenv("HOME"); - char *cwd; memset(&cheerios, 0, sizeof(cheerios_t)); @@ -43,11 +42,25 @@ cheerios_start(bytenuts_t *bytenuts) } } - cwd = getcwd(NULL, 0); - chdir(home); - cheerios.backup = fopen(".config/bytenuts/outbuf.log", "w"); - chdir(cwd); - free(cwd); + if (home) { + pid_t pid; + int name_len; + + /* ensure that a process has a unique log */ + pid = getpid(); + name_len = snprintf( + NULL, 0, + "%s/.config/bytenuts/outbuf.%d.log", + home, pid + ); + cheerios.backup_filename = calloc(1, name_len + 1); + snprintf( + cheerios.backup_filename, name_len + 1, + "%s/.config/bytenuts/outbuf.%d.log", + home, pid + ); + cheerios.backup = fopen(cheerios.backup_filename, "w"); + } pthread_cond_init(&cheerios.cond, NULL); pthread_mutex_init(&cheerios.lock, NULL); @@ -138,7 +151,6 @@ cheerios_stop() { cheerios.running = 0; pthread_join(cheerios.thr, NULL); - return 0; } @@ -338,8 +350,33 @@ cheerios_thread(void *arg) if (cheerios.log) fclose(cheerios.log); - if (cheerios.backup) + + if (cheerios.backup) { + char *out_filename; + int out_filename_len; + char *home = getenv("HOME"); + + /* do not chdir as other threads can still be running */ + out_filename_len = snprintf( + NULL, 0, + "%s/.config/bytenuts/outbuf.log", + home + ); + out_filename = calloc(1, out_filename_len + 1); + snprintf( + out_filename, out_filename_len + 1, + "%s/.config/bytenuts/outbuf.log", + home + ); + + /* move this processes log to the path that can be loaded on resumption */ fclose(cheerios.backup); + rename(cheerios.backup_filename, out_filename); + + free(out_filename); + free(cheerios.backup_filename); + } + pthread_exit(NULL); return NULL; } diff --git a/src/cheerios.h b/src/cheerios.h index e83dee2..97e2a53 100644 --- a/src/cheerios.h +++ b/src/cheerios.h @@ -35,8 +35,9 @@ typedef struct cheerios_struct { pthread_mutex_t *term_lock; int ser_fd; line_buffer_t lines; - FILE *log; - FILE *backup; + FILE *log; /* log file which was opened with -l */ + char *backup_filename; /* realpath to the backup outbuf.pid.log */ + FILE *backup; /* backup log file object */ bytenuts_config_t *config; volatile int mode; pthread_cond_t cond; diff --git a/src/ingest.c b/src/ingest.c index 1c491f5..1d043ac 100644 --- a/src/ingest.c +++ b/src/ingest.c @@ -40,19 +40,29 @@ ingest_start(bytenuts_t *bytenuts) HOME = getenv("HOME"); if (HOME) { - char *cwd = getcwd(NULL, 0); int idx = 0; - - chdir(HOME); + int name_len; + pid_t pid; while (!read_cmd_page(idx)) { idx++; } - ingest.history_fd = fopen(".config/bytenuts/inbuf.log", "w"); + /* ensure that a process has a unique log */ + pid = getpid(); + name_len = snprintf( + NULL, 0, + "%s/.config/bytenuts/inbuf.%d.log", + HOME, pid + ); + ingest.history_filename = calloc(1, name_len + 1); + snprintf( + ingest.history_filename, name_len + 1, + "%s/.config/bytenuts/inbuf.%d.log", + HOME, pid + ); - chdir(cwd); - free(cwd); + ingest.history_fd = fopen(ingest.history_filename, "w"); } if (ingest.cmd_pgs_n > 0) { @@ -73,10 +83,6 @@ ingest_stop() { ingest.running = 0; pthread_join(ingest.thr, NULL); - - if (ingest.history_fd) - fclose(ingest.history_fd); - return 0; } @@ -419,6 +425,32 @@ ingest_thread(void *arg) sched_yield(); } + if (ingest.history_fd) { + char *in_filename; + int in_filename_len; + char *home = getenv("HOME"); + + /* do not chdir as other threads can still be running */ + in_filename_len = snprintf( + NULL, 0, + "%s/.config/bytenuts/inbuf.log", + home + ); + in_filename = calloc(1, in_filename_len + 1); + snprintf( + in_filename, in_filename_len + 1, + "%s/.config/bytenuts/inbuf.log", + home + ); + + /* move this processes history to the path that can be loaded on resumption */ + fclose(ingest.history_fd); + rename(ingest.history_filename, in_filename); + + free(in_filename); + free(ingest.history_filename); + } + pthread_exit(NULL); return NULL; } diff --git a/src/ingest.h b/src/ingest.h index 06d1fec..5f39b06 100644 --- a/src/ingest.h +++ b/src/ingest.h @@ -24,6 +24,7 @@ typedef struct ingest_struct { int inpos; /* cursor position within the inbuf itself */ int inlen; /* length of the input buffer */ char **history; /* array of strings representing the command history */ + char *history_filename; /* realpath of inbuf.pid.log */ FILE *history_fd; char tmp_history[4096]; /* copy of inbuf before we load in history */ int history_len;