diff options
-rw-r--r-- | LICENSE | 32 | ||||
-rw-r--r-- | Makefile | 16 | ||||
-rw-r--r-- | lock.c | 54 | ||||
-rwxr-xr-x | lock.sh (renamed from lock) | 0 |
4 files changed, 102 insertions, 0 deletions
@@ -0,0 +1,32 @@ +Copyright (c) 2014, Calvin Morrison, Laslo Hunhold + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +* Neither the name of the <organization> nor the +names of its contributors may be used to endorse or promote products +derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. + +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0c3dbd8 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +VERSION=\"0.0.5\" +CC = gcc +CFLAGS = -Os -mtune=native -Wall -Wextra -pedantic +DESTDIR = /usr/local/ + +all: lock + +lock: + $(CC) $(CFLAGS) lock.c -o lock + + +install: all + @install lock $(DESTDIR)/bin/ + +clean: + rm lock @@ -0,0 +1,54 @@ +/* See LICENSE for licence details. */
+
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv){
+ size_t pathlen, argvlen;
+ char *path;
+
+ if(argc != 2){
+ fprintf(stderr, "Please supply one argument: the lock directory.\n");
+ return EXIT_FAILURE;
+ }
+
+ 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;
+ }
+ return EXIT_SUCCESS;
+ }
+
+ /* prepare path */
+ argvlen = strlen(argv[1]);
+ pathlen = argvlen + sizeof("/..") + 1;
+ path = malloc(pathlen);
+ if(path == NULL) {
+ fprintf(stderr, "Error: %s\n", strerror(errno));
+ 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;
+}
|