Browse Source

kernel: handle O_EXCL... correctly...

K. Lange 2 years ago
parent
commit
cc3aa63a7b
2 changed files with 35 additions and 5 deletions
  1. 30 0
      apps/test-lock.c
  2. 5 5
      kernel/sys/syscall.c

+ 30 - 0
apps/test-lock.c

@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+int main(int argc, char * argv[]) {
+	if (argc < 2 ){
+		fprintf(stderr, "usage: test-lock LOCKPATH\n");
+		return 1;
+	}
+	int fd = open(argv[1],O_RDWR|O_CREAT|O_EXCL);
+	if (fd < 0) {
+		if (errno == EEXIST) {
+			fprintf(stderr, "Lock is already held.\n");
+			return 0;
+		} else {
+			fprintf(stderr, "Some other error? %d = %s\n", errno, strerror(errno));
+			return 1;
+		}
+	} else {
+		fprintf(stderr, "I have the lock, the fd is %d.\n", fd);
+		fprintf(stderr, "Press Enter to release lock.\n");
+		while (!feof(stdin) && fgetc(stdin) != '\n') {
+			/* nothing */
+		}
+		close(fd);
+		unlink(argv[1]);
+		return 0;
+	}
+}

+ 5 - 5
kernel/sys/syscall.c

@@ -129,6 +129,11 @@ static int sys_open(const char * file, int flags, int mode) {
 
 	int access_bits = 0;
 
+	if (node && (flags & O_CREAT) && (flags & O_EXCL)) {
+		close_fs(node);
+		return -EEXIST;
+	}
+
 	if (!(flags & O_WRONLY) || (flags & O_RDWR)) {
 		if (node && !has_permission(node, 04)) {
 			debug_print(WARNING, "access denied (read, sys_open, file=%s)", file);
@@ -153,11 +158,6 @@ static int sys_open(const char * file, int flags, int mode) {
 		}
 	}
 
-	if (node && (flags & O_CREAT) && (flags & O_EXCL)) {
-		close_fs(node);
-		return -EEXIST;
-	}
-
 	if (!node && (flags & O_CREAT)) {
 		/* TODO check directory permissions */
 		debug_print(NOTICE, "- file does not exist and create was requested.");