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);