chiark / gitweb /
Make tmpdir removal asynchronous
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 16 Sep 2013 20:50:38 +0000 (15:50 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 17 Sep 2013 15:26:30 +0000 (10:26 -0500)
https://bugs.freedesktop.org/show_bug.cgi?id=68232

Makefile.am
src/core/async.c [moved from src/core/sync.c with 87% similarity]
src/core/async.h [moved from src/core/sync.h with 93% similarity]
src/core/execute.c
src/core/job.c

index 4db064e7c242e68e2644911d87c4092b6f34b656..b69d66da1843b828d543dcd68bf765d352991142 100644 (file)
@@ -967,8 +967,8 @@ libsystemd_core_la_SOURCES = \
        src/core/syscall-list.h \
        src/core/audit-fd.c \
        src/core/audit-fd.h \
-       src/core/sync.c \
-       src/core/sync.h
+       src/core/async.c \
+       src/core/async.h
 
 if HAVE_KMOD
 libsystemd_core_la_SOURCES += \
similarity index 87%
rename from src/core/sync.c
rename to src/core/async.c
index 7e74b63071e4477fd6bcfb4bfcc892d1da3e002a..af527bea4ec62ff46c92ec0f0b547618f737cc09 100644 (file)
 #include <pthread.h>
 #include <unistd.h>
 
-#include "sync.h"
+#include "async.h"
+#include "log.h"
 
-static void *sync_thread(void *p) {
-        sync();
-        return NULL;
-}
-
-int asynchronous_sync(void) {
+int asynchronous_job(void* (*func)(void *p), void *arg) {
         pthread_attr_t a;
         pthread_t t;
         int r;
@@ -53,7 +49,7 @@ int asynchronous_sync(void) {
                 goto finish;
         }
 
-        r = pthread_create(&t, &a, sync_thread, NULL);
+        r = pthread_create(&t, &a, func, arg);
         if (r != 0) {
                 r = -r;
                 goto finish;
@@ -63,3 +59,14 @@ finish:
         pthread_attr_destroy(&a);
         return r;
 }
+
+static void *sync_thread(void *p) {
+        sync();
+        return NULL;
+}
+
+int asynchronous_sync(void) {
+        log_debug("Spawning new thread for sync");
+
+        return asynchronous_job(sync_thread, NULL);
+}
similarity index 93%
rename from src/core/sync.h
rename to src/core/async.h
index eb26c88deb6158e71d85876417c35c69a3b1d215..6601b4dc4b313c741b91b9ac8a838a83520cfb09 100644 (file)
@@ -21,4 +21,5 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+int asynchronous_job(void* (*func)(void *p), void *arg);
 int asynchronous_sync(void);
index 43b571e043ad0b7231311e748e68b9875e8c353e..f840642d14141a4afdfad9394545a0a41a351ab9 100644 (file)
@@ -67,6 +67,7 @@
 #include "env-util.h"
 #include "fileio.h"
 #include "unit.h"
+#include "async.h"
 
 #define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC)
 #define IDLE_TIMEOUT2_USEC (1*USEC_PER_SEC)
@@ -1581,6 +1582,28 @@ void exec_context_init(ExecContext *c) {
         c->timer_slack_nsec = (nsec_t) -1;
 }
 
+static void *remove_tmpdir_thread(void *p) {
+        int r;
+        _cleanup_free_ char *dirp = p;
+        char *dir;
+
+        assert(dirp);
+
+        r = rm_rf_dangerous(dirp, false, true, false);
+        dir = dirname(dirp);
+        if (r < 0)
+                log_warning("Failed to remove content of temporary directory %s: %s",
+                            dir, strerror(-r));
+        else {
+                r = rmdir(dir);
+                if (r < 0)
+                        log_warning("Failed to remove temporary directory %s: %s",
+                                    dir, strerror(-r));
+        }
+
+        return NULL;
+}
+
 void exec_context_tmp_dirs_done(ExecContext *c) {
         char* dirs[] = {c->tmp_dir ? c->tmp_dir : c->var_tmp_dir,
                         c->tmp_dir ? c->var_tmp_dir : NULL,
@@ -1588,22 +1611,8 @@ void exec_context_tmp_dirs_done(ExecContext *c) {
         char **dirp;
 
         for(dirp = dirs; *dirp; dirp++) {
-                char *dir;
-                int r;
-
-                r = rm_rf_dangerous(*dirp, false, true, false);
-                dir = dirname(*dirp);
-                if (r < 0)
-                        log_warning("Failed to remove content of temporary directory %s: %s",
-                                    dir, strerror(-r));
-                else {
-                        r = rmdir(dir);
-                        if (r < 0)
-                                log_warning("Failed to remove  temporary directory %s: %s",
-                                            dir, strerror(-r));
-                }
-
-                free(*dirp);
+                log_debug("Spawning thread to nuke %s", *dirp);
+                asynchronous_job(remove_tmpdir_thread, *dirp);
         }
 
         c->tmp_dir = c->var_tmp_dir = NULL;
index 85f77e8f0d8335959cbb26a23648f2bf237313b2..bf1d95690824818c593ff900a55ab479d40746d2 100644 (file)
@@ -35,7 +35,7 @@
 #include "log.h"
 #include "dbus-job.h"
 #include "special.h"
-#include "sync.h"
+#include "async.h"
 #include "virt.h"
 
 JobBusClient* job_bus_client_new(DBusConnection *connection, const char *name) {