aboutsummaryrefslogtreecommitdiff
path: root/lock.c
blob: 871425dbe3042ad92178e8deec08aa527913e9bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* See LICENSE for licence details. */
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>

int try_mkdir(const char *dn) {
	int ret;

	errno = 0;
	ret = 0;

	ret = mkdir(dn, S_IRUSR);
	if(errno && errno != EEXIST) {
		fprintf(stderr, "Failed to create lock %s.\n", dn);
		exit(EXIT_FAILURE);
	}
		return ret;

}

int main(int argc, char **argv){
	if(argc != 2){
		fprintf(stderr, "Please supply one argument: the lock directory.\n");
		return EXIT_FAILURE;
	}

	/* create lock */
	if(try_mkdir(argv[1])){
		/* lock exists */
		for(; try_mkdir(argv[1]); ){
			sleep(1);
		}
	}
	return EXIT_SUCCESS;
}