]> mj.ucw.cz Git - umpf.git/blobdiff - lock.c
fix many little bugs, release 0.1
[umpf.git] / lock.c
diff --git a/lock.c b/lock.c
index 0b04ef29a10946d479fe6447940f06c6bd31f1e6..12f40830b4e4f4bee8b4846556b986e8cb6d221c 100644 (file)
--- a/lock.c
+++ b/lock.c
@@ -1,17 +1,18 @@
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#define _GNU_SOURCE
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/stat.h>
+#include <time.h>
 
-#include "brum.h"
-
-#define LOCK_MAX_TRIES 30
+#include "umpf.h"
 
+#define LOCK_MAX_TRIES 3
 gid_t rgid, egid, sgid;
 
 void
@@ -45,17 +46,16 @@ random_sleep(unsigned int about, unsigned int offset)
        usleep(about * 1000000 + myrand * 500000);
 }
 
-static char*
+char*
 cat(char* l, char* r)
 {
        char* res = xmalloc(strlen(l) + strlen (r) + 1);
        strcpy(res, l);
        strcat(res, r);
-
+       
        return res;     
 }
 
-/* FIXME: what about privileges? */
 static int
 dot_lock(char* path)
 {
@@ -64,7 +64,10 @@ dot_lock(char* path)
        int res = -1;
        char* lockfile = cat(path, ".lock");
 
+       raise_gid();
        for (i = 0; i < LOCK_MAX_TRIES; i++){
+               struct stat buf;
+
                if ((fd = open(lockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) 
                        >= 0) {
                        close(fd);
@@ -74,11 +77,13 @@ dot_lock(char* path)
 
                if (errno != EEXIST)
                        break;
-
-               /* FIXME: deal with old locks */
-
+               /* deal with old locks */
+               stat(lockfile, &buf);
+               if ((time(NULL) - buf.st_mtime) > 3600)
+                       unlink(lockfile);               
                random_sleep(1, 1);
        }
+       drop_gid();
 
        free(lockfile);
        return res;
@@ -169,24 +174,3 @@ open_mailbox(char* path, int is_default_mailbox)
 
        return fd;
 }
-
-int
-main(int argc, char** argv)
-{
-       /* FIXME: move somewhere */
-       int fd;
-       if (argc < 3)
-               return 1;
-       char* mb = argv[1];
-       int is_default_mailbox = atoi(argv[2]);
-       save_gids();
-       drop_gid(); 
-       fd = open_mailbox(mb, is_default_mailbox);
-       printf("%d\n", fd); 
-       if (fd < 0)
-               return 1;       
-
-       close_mailbox(fd, mb, is_default_mailbox);
-
-       return 0;
-}