diff options
| author | mar77i <mar77i@users.noreply.github.com> | 2014-04-11 11:15:04 +0200 | 
|---|---|---|
| committer | mar77i <mar77i@users.noreply.github.com> | 2014-04-11 11:15:04 +0200 | 
| commit | ed2589219056458a20aa399d128e06394f632e15 (patch) | |
| tree | 5beaf3d3281d4c57388b58a9d9d8a14225e8f76d /lock.c | |
| parent | 4f4bd122b940d19d90e23f9a012c51097d42dfbc (diff) | |
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.
Diffstat (limited to 'lock.c')
| -rw-r--r-- | lock.c | 39 | 
1 files changed, 10 insertions, 29 deletions
| @@ -1,4 +1,5 @@  /* See LICENSE for licence details. */
 +#include <errno.h>
  #include <string.h>
  #include <stdio.h>
  #include <unistd.h>
 @@ -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;
  }
 | 
