From 94574db6f6d329797d7fa4e19f75645cd6cbae86 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 11 Jun 2014 09:17:43 +0200 Subject: [PATCH] cleanup temporary directory when restarting and not using XDG_RUNTIME_DIR fixes #1253 --- src/commands.c | 8 +++++ src/main.c | 8 ++++- testcases/lib/i3test.pm | 2 +- testcases/t/229-cleanup-tmpdir.t | 50 ++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/commands.c b/src/commands.c index 81358274..d6c6fc41 100644 --- a/src/commands.c +++ b/src/commands.c @@ -1682,6 +1682,14 @@ void cmd_reload(I3_CMD) { */ void cmd_restart(I3_CMD) { LOG("restarting i3\n"); + ipc_shutdown(); + /* We need to call this manually since atexit handlers don’t get called + * when exec()ing */ + purge_zerobyte_logfile(); + /* The unlink call is intentionally after the purge_zerobyte_logfile() so + * that the latter does not remove the directory yet. We need to store the + * restart layout state in there. */ + unlink(config.ipc_socket_path); i3_restart(false); // XXX: default reply for now, make this a better reply diff --git a/src/main.c b/src/main.c index 4081c234..9a306388 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "all.h" #include "shmlog.h" @@ -670,8 +671,13 @@ int main(int argc, char *argv[]) { if (layout_path) { LOG("Trying to restore the layout from %s...", layout_path); needs_tree_init = !tree_restore(layout_path, greply); - if (delete_layout_path) + if (delete_layout_path) { unlink(layout_path); + const char *dir = dirname(layout_path); + /* possibly fails with ENOTEMPTY if there are files (or + * sockets) left. */ + rmdir(dir); + } free(layout_path); } if (needs_tree_init) diff --git a/testcases/lib/i3test.pm b/testcases/lib/i3test.pm index 5dd96acd..a6b982ba 100644 --- a/testcases/lib/i3test.pm +++ b/testcases/lib/i3test.pm @@ -783,7 +783,7 @@ To avoid caching: =cut sub get_socket_path { my ($cache) = @_; - $cache ||= 1; + $cache //= 1; if ($cache && defined($_cached_socket_path)) { return $_cached_socket_path; diff --git a/testcases/t/229-cleanup-tmpdir.t b/testcases/t/229-cleanup-tmpdir.t index 6448da00..eff39b08 100644 --- a/testcases/t/229-cleanup-tmpdir.t +++ b/testcases/t/229-cleanup-tmpdir.t @@ -50,4 +50,54 @@ if (-d $tmpdir) { diag('contents = ' . Dumper(<$tmpdir/*>)); } +$pid = launch_with_config($config, dont_add_socket_path => 1, dont_create_temp_dir => 1); +$socketpath = get_socket_path(0); +$tmpdir = dirname($socketpath); + +ok(-d $tmpdir, "tmpdir $tmpdir exists"); + +# Clear the error logfile. The testsuite runs in an environment where RandR is +# not supported, so there always is a message about xinerama in the error +# logfile. +@errorlogfiles = <$tmpdir/errorlog.*>; +for my $fn (@errorlogfiles) { + open(my $fh, '>', $fn); + close($fh); +} + +diag('socket path before restarting is ' . $socketpath); + +cmd 'restart'; + +# The socket path will be different, and we use that for checking whether i3 has restarted yet. +while (get_socket_path(0) eq $socketpath) { + sleep 0.1; +} + +my $new_tmpdir = dirname(get_socket_path()); + +does_i3_live; + +# Clear the error logfile. The testsuite runs in an environment where RandR is +# not supported, so there always is a message about xinerama in the error +# logfile. +@errorlogfiles = <$new_tmpdir/errorlog.*>; +for my $fn (@errorlogfiles) { + open(my $fh, '>', $fn); + close($fh); +} + +exit_gracefully($pid); + +ok(! -d $tmpdir, "old tmpdir $tmpdir was cleaned up"); +if (-d $tmpdir) { + diag('contents = ' . Dumper(<$tmpdir/*>)); +} + +ok(! -d $new_tmpdir, "new tmpdir $new_tmpdir was cleaned up"); +if (-d $new_tmpdir) { + diag('contents = ' . Dumper(<$new_tmpdir/*>)); +} + + done_testing;