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 <julius@plenz.com>
This commit is contained in:
Julius Plenz
2012-08-11 01:50:37 +02:00
committed by Michael Stapelberg
parent da924aae6f
commit 70ec3867fe
3 changed files with 37 additions and 2 deletions

View File

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