From ed2589219056458a20aa399d128e06394f632e15 Mon Sep 17 00:00:00 2001 From: mar77i Date: Fri, 11 Apr 2014 11:15:04 +0200 Subject: lock.c: Truls Beckens was right #2 also, what the hell? Fixed the obvious stuff and returned atomicity to the locking - no, access() isn't safe when you want that. --- lock.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) (limited to 'lock.c') diff --git a/lock.c b/lock.c index 68245ac..aaac35a 100644 --- a/lock.c +++ b/lock.c @@ -1,4 +1,5 @@ /* See LICENSE for licence details. */ +#include #include #include #include @@ -14,39 +15,19 @@ int main(int argc, char **argv){ return EXIT_FAILURE; } - if(access(argv[1], F_OK)){ - /* create lock */ - if(mkdir(argv[1], S_IRUSR | S_IXUSR)){ + /* create lock */ + if(mkdir(argv[1], S_IRUSR | S_IXUSR)){ + if(errno && errno != EEXIST) { fprintf(stderr, "Failed to create lock %s.\n", argv[1]); return EXIT_FAILURE; } + /* lock exists */ + for(; mkdir(argv[1], S_IRUSR | S_IXUSR);){ + sleep(1); + } + return EXIT_SUCCESS; + } else return EXIT_SUCCESS; } - /* prepare path */ - argvlen = strlen(argv[1]); - pathlen = argvlen + sizeof("/..") + 1; - path = malloc(pathlen); - if(path == NULL) { - fprintf(stderr, "malloc() failed\n"); - return EXIT_FAILURE; - } - - memcpy(path, argv[1], argvlen); - memcpy(path + argvlen, "/..\0", sizeof("/..") + 1); - - if(access(path, F_OK)){ - /* invalid lock */ - fprintf(stderr, "%s is not a lock directory.\n", argv[1]); - free(path); - return EXIT_FAILURE; - } - - /* lock exists */ - for(; !access(path, F_OK) ;){ - sleep(1); - } - - free(path); - return EXIT_SUCCESS; } -- cgit v1.2.3