hammer util: avoid running concurrent cleanups
authorSimon Schubert <simon.schubert@epfl.ch>
Tue, 4 Aug 2009 14:23:57 +0000 (16:23 +0200)
committerSimon Schubert <corecode@dragonflybsd.org>
Sun, 9 Aug 2009 08:32:25 +0000 (10:32 +0200)
If hammer cleanup gets called more frequently than the cleanup runs
take, they build up, all trying to run in parallel.  Prevent this by
locking the config file while cleanup is running.  If cleanup finds the
config file locked for a PFS, it will skip processing.

sbin/hammer/cmd_cleanup.c

index 0cd1f09..f1ffd9c 100644 (file)
@@ -249,6 +249,15 @@ do_cleanup(const char *path)
                return;
        }
 
+       if (flock(fileno(fp), LOCK_EX|LOCK_NB) == -1) {
+               if (errno == EWOULDBLOCK)
+                       printf(" PFS #%d locked by other process\n", pfs.pfs_id);
+               else
+                       printf(" can not lock %s: %s\n", config_path, strerror(errno));
+               fclose(fp);
+               return;
+       }
+
        printf(" handle PFS #%d using %s\n", pfs.pfs_id, snapshots_path);
 
        /*