aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lock.c39
-rwxr-xr-xlock.sh2
2 files changed, 30 insertions, 11 deletions
diff --git a/lock.c b/lock.c
index aaac35a..68245ac 100644
--- a/lock.c
+++ b/lock.c
@@ -1,5 +1,4 @@
/* See LICENSE for licence details. */
-#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
@@ -15,19 +14,39 @@ int main(int argc, char **argv){
return EXIT_FAILURE;
}
- /* create lock */
- if(mkdir(argv[1], S_IRUSR | S_IXUSR)){
- if(errno && errno != EEXIST) {
+ if(access(argv[1], F_OK)){
+ /* create lock */
+ if(mkdir(argv[1], S_IRUSR | S_IXUSR)){
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;
}
diff --git a/lock.sh b/lock.sh
index 9a9cdd6..22191fe 100755
--- a/lock.sh
+++ b/lock.sh
@@ -11,7 +11,7 @@ lock() {
echo "lock $1 found, waiting for unlock" >&2
while true; do
sleep 2;
- if mkdir "$1" &>/dev/null; then
+ if [[ ! -d "$1" ]]; then
return
fi
done