Browse Source

Various errno-related fixes

K. Lange 4 years ago
parent
commit
dace5d456b
9 changed files with 25 additions and 72 deletions
  1. 17 17
      kernel/fs/vfs.c
  2. 1 6
      libc/dirent/mkdir.c
  3. 1 8
      libc/sys/mount.c
  4. 1 6
      libc/sys/wait.c
  5. 1 6
      libc/unistd/chmod.c
  6. 1 6
      libc/unistd/pipe.c
  7. 1 8
      libc/unistd/readlink.c
  8. 1 8
      libc/unistd/symlink.c
  9. 1 7
      libc/unistd/unlink.c

+ 17 - 17
kernel/fs/vfs.c

@@ -29,7 +29,7 @@ hashmap_t * fs_types = NULL;
 int has_permission(fs_node_t * node, int permission_bit) {
 	if (!node) return 0;
 
-	if (current_process->user == 0) {
+	if (current_process->user == 0 && permission_bit != 01) { /* even root needs exec to exec */
 		return 1;
 	}
 
@@ -98,26 +98,26 @@ static fs_node_t * vfs_mapper(void) {
  * selectcheck_fs: Check if a read from this file would block.
  */
 int selectcheck_fs(fs_node_t * node) {
-	if (!node) return -1;
+	if (!node) return -ENOENT;
 
 	if (node->selectcheck) {
 		return node->selectcheck(node);
 	}
 
-	return -1;
+	return -EINVAL;
 }
 
 /**
  * selectwait_fs: Inform a node that it should alert the current_process.
  */
 int selectwait_fs(fs_node_t * node, void * process) {
-	if (!node) return -1;
+	if (!node) return -ENOENT;
 
 	if (node->selectwait) {
 		return node->selectwait(node, process);
 	}
 
-	return -1;
+	return -EINVAL;
 }
 
 /**
@@ -130,13 +130,13 @@ int selectwait_fs(fs_node_t * node, void * process) {
  * @returns Bytes read
  */
 uint32_t read_fs(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
-	if (!node) return -1;
+	if (!node) return -ENOENT;
 
 	if (node->read) {
 		uint32_t ret = node->read(node, offset, size, buffer);
 		return ret;
 	} else {
-		return -1;
+		return -EINVAL;
 	}
 }
 
@@ -150,13 +150,13 @@ uint32_t read_fs(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffe
  * @returns Bytes written
  */
 uint32_t write_fs(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
-	if (!node) return -1;
+	if (!node) return -ENOENT;
 
 	if (node->write) {
 		uint32_t ret = node->write(node, offset, size, buffer);
 		return ret;
 	} else {
-		return -1;
+		return -EINVAL;
 	}
 }
 
@@ -286,12 +286,12 @@ fs_node_t *finddir_fs(fs_node_t *node, char *name) {
  * @returns Depends on `request`
  */
 int ioctl_fs(fs_node_t *node, int request, void * argp) {
-	if (!node) return -1;
+	if (!node) return -ENOENT;
 
 	if (node->ioctl) {
 		return node->ioctl(node, request, argp);
 	} else {
-		return -1; /* TODO Should actually be ENOTTY, but we're bad at error numbers */
+		return -EINVAL;
 	}
 }
 
@@ -332,7 +332,7 @@ int create_file_fs(char *name, uint16_t permission) {
 	if (!parent) {
 		debug_print(WARNING, "failed to open parent");
 		free(path);
-		return -1;
+		return -ENOENT;
 	}
 
 	if (!has_permission(parent, 02)) {
@@ -378,7 +378,7 @@ int unlink_fs(char * name) {
 
 	if (!parent) {
 		free(path);
-		return -1;
+		return -ENOENT;
 	}
 
 	if (parent->unlink) {
@@ -429,7 +429,7 @@ int mkdir_fs(char *name, uint16_t permission) {
 		if (_exists) {
 			return -EEXIST;
 		}
-		return -1;
+		return -ENOENT;
 	}
 
 	if (parent->mkdir) {
@@ -481,7 +481,7 @@ int symlink_fs(char * target, char * name) {
 
 	if (!parent) {
 		free(path);
-		return -1;
+		return -ENOENT;
 	}
 
 	if (parent->symlink) {
@@ -495,12 +495,12 @@ int symlink_fs(char * target, char * name) {
 }
 
 int readlink_fs(fs_node_t *node, char * buf, uint32_t size) {
-	if (!node) return -1;
+	if (!node) return -ENOENT;
 
 	if (node->readlink) {
 		return node->readlink(node, buf, size);
 	} else {
-		return -1;
+		return -EINVAL;
 	}
 }
 

+ 1 - 6
libc/dirent/mkdir.c

@@ -3,10 +3,5 @@
 #include <sys/stat.h>
 
 int mkdir(const char *pathname, mode_t mode) {
-	int ret = syscall_mkdir((char *)pathname, mode);
-	if (ret < 0) {
-		errno = -ret;
-		return -1;
-	}
-	return ret;
+	__sets_errno(syscall_mkdir((char *)pathname, mode));
 }

+ 1 - 8
libc/sys/mount.c

@@ -2,13 +2,6 @@
 #include <errno.h>
 
 int mount(char * source, char * target, char * type, unsigned long flags, void * data) {
-	int r = syscall_mount(source, target, type, flags, data);
-
-	if (r < 0) {
-		errno = -r;
-		return -1;
-	}
-
-	return r;
+	__sets_errno(syscall_mount(source, target, type, flags, data));
 }
 

+ 1 - 6
libc/sys/wait.c

@@ -3,12 +3,7 @@
 
 int waitpid(int pid, int *status, int options) {
 	/* XXX: status, options? */
-	int i = syscall_waitpid(pid, status, options);
-	if (i < 0) {
-		errno = -i;
-		return -1;
-	}
-	return i;
+	__sets_errno(syscall_waitpid(pid, status, options));
 }
 
 int wait(int *status) {

+ 1 - 6
libc/unistd/chmod.c

@@ -5,11 +5,6 @@
 DEFN_SYSCALL2(chmod, 50, char *, int);
 
 int chmod(const char *path, mode_t mode) {
-	int result = syscall_chmod((char *)path, mode);
-	if (result < 0) {
-		errno = -result;
-		result = -1;
-	}
-	return result;
+	__sets_errno(syscall_chmod((char *)path, mode));
 }
 

+ 1 - 6
libc/unistd/pipe.c

@@ -5,10 +5,5 @@
 DEFN_SYSCALL1(pipe, 54, int *);
 
 int pipe(int fildes[2]) {
-	int ret = syscall_pipe((int *)fildes);
-	if (ret < 0) {
-		errno = -ret;
-		return -1;
-	}
-	return ret;
+	__sets_errno(syscall_pipe((int *)fildes));
 }

+ 1 - 8
libc/unistd/readlink.c

@@ -6,13 +6,6 @@
 DEFN_SYSCALL3(readlink, SYS_READLINK, char *, char *, int);
 
 ssize_t readlink(const char * name, char * buf, size_t len) {
-	int r = syscall_readlink((char*)name, buf, len);
-
-	if (r < 0) {
-		errno = -r;
-		return -1;
-	}
-
-	return r;
+	__sets_errno(syscall_readlink((char*)name, buf, len));
 }
 

+ 1 - 8
libc/unistd/symlink.c

@@ -7,13 +7,6 @@
 DEFN_SYSCALL2(symlink, SYS_SYMLINK, const char *, const char *);
 
 int symlink(const char *target, const char *name) {
-	int r = syscall_symlink(target, name);
-
-	if (r < 0) {
-		errno = -r;
-		return -1;
-	}
-
-	return r;
+	__sets_errno(syscall_symlink(target, name));
 }
 

+ 1 - 7
libc/unistd/unlink.c

@@ -6,11 +6,5 @@
 DEFN_SYSCALL1(unlink, SYS_UNLINK, char *);
 
 int unlink(const char * pathname) {
-	int result = syscall_unlink((char *)pathname);
-	if (result < 0) {
-		errno = -result;
-		return -1;
-	}
-
-	return 0;
+	__sets_errno(syscall_unlink((char *)pathname));
 }