From: Julius Plenz Date: Fri, 10 Aug 2012 23:50:37 +0000 (+0200) Subject: clean up zero-byte logfile on immediate exit X-Git-Tag: 4.3~115 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=70ec3867fe0fdf5d97b5680fdb5f670474bceaa7;p=i3%2Fi3 clean up zero-byte logfile on immediate exit Otherwise, a zero-byte log file stays behind after every call to `i3 --get-socketpath`. Also, replace "return" calls with more explicit "exit" calls. Before: $ ls -ld /tmp/i3* | wc -l; \ repeat 10 i3 --get-socketpath >/dev/null; \ ls -ld /tmp/i3* | wc -l 1 11 Now: $ ls -ld /tmp/i3* | wc -l; \ repeat 10 i3 --get-socketpath >/dev/null; \ ls -ld /tmp/i3* | wc -l 1 1 Signed-off-by: Julius Plenz --- diff --git a/include/log.h b/include/log.h index 0ad0fb37..a8209cca 100644 --- a/include/log.h +++ b/include/log.h @@ -67,4 +67,11 @@ void errorlog(char *fmt, ...) void verboselog(char *fmt, ...) __attribute__ ((format (printf, 1, 2))); +/** + * Deletes the unused log files. Useful if i3 exits immediately, eg. + * because --get-socketpath was called. We don't care for syscall + * failures. This function is invoked automatically when exiting. + */ +void purge_zerobyte_logfile(void); + #endif diff --git a/src/log.c b/src/log.c index f7932926..944edb3f 100644 --- a/src/log.c +++ b/src/log.c @@ -131,6 +131,7 @@ void init_logging(void) { loglastwrap = logbuffer + logbuffer_size; store_log_markers(); } + atexit(purge_zerobyte_logfile); } /* @@ -268,3 +269,30 @@ void debuglog(char *fmt, ...) { vlog(debug_logging, fmt, args); va_end(args); } + +/* + * Deletes the unused log files. Useful if i3 exits immediately, eg. + * because --get-socketpath was called. We don't care for syscall + * failures. This function is invoked automatically when exiting. + */ +void purge_zerobyte_logfile(void) { + struct stat st; + char *slash; + + if (!errorfilename) + return; + + /* don't delete the log file if it contains something */ + if ((stat(errorfilename, &st)) == -1 || st.st_size > 0) + return; + + if (unlink(errorfilename) == -1) + return; + + if ((slash = strrchr(errorfilename, '/')) != NULL) { + *slash = '\0'; + /* possibly fails with ENOTEMPTY if there are files (or + * sockets) left. */ + rmdir(errorfilename); + } +} diff --git a/src/main.c b/src/main.c index e75e7fbf..d6a7f617 100644 --- a/src/main.c +++ b/src/main.c @@ -343,10 +343,10 @@ int main(int argc, char *argv[]) { char *socket_path = root_atom_contents("I3_SOCKET_PATH"); if (socket_path) { printf("%s\n", socket_path); - return 0; + exit(EXIT_SUCCESS); } - return 1; + exit(EXIT_FAILURE); } else if (strcmp(long_options[option_index].name, "shmlog-size") == 0 || strcmp(long_options[option_index].name, "shmlog_size") == 0) { shmlog_size = atoi(optarg);