summaryrefslogtreecommitdiff
path: root/src/kernel/handle.c
blob: 6e25b747ecd7dec0ba8b1addcfc31b73db25a394 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <kernel/handle.h>
#include <kernel/mem.h>
#include <kernel/panic.h>
#include <kernel/proc.h>

static int handleop_special_tty(struct handleop_args *args);

int handleop_dispatch(struct handleop_args args) {
	switch(args.handle->type) {
		case HANDLE_EMPTY: {
			if (args.type == HANDLEOP_MOUNT) // mounting an empty handle is allowed
				return 0;
			return -1;
		}
		case HANDLE_SPECIAL_TTY:
			return handleop_special_tty(&args);
		default:
			panic();
	}
}

static int handleop_special_tty(struct handleop_args *args) {
	switch(args->type) {
		case HANDLEOP_MOUNT:
			return 0; // no special action needed

		case HANDLEOP_OPEN:
			/* don't allow anything after the mount point
			 * this is a file, not a directory
			 * for example: open("/tty") is allowed. open("/tty/smth") isn't */
			if (args->open.len == 0) {
				args->open.target->type = HANDLE_SPECIAL_TTY;
				return 0;
			}
			return -1;

		case HANDLEOP_READ:
			return -1; // input not implemented yet

		case HANDLEOP_WRITE: {
			 struct virt_iter iter;
			 virt_iter_new(&iter, args->rw.ptr, args->rw.len,
					 process_current->pages, true, false);
			 while (virt_iter_next(&iter))
				 tty_write(iter.frag, iter.frag_len);
			 return iter.prior;
		}

		case HANDLEOP_CLOSE:
			args->handle->type = HANDLE_EMPTY;
			return 0;

		default: panic();
	}
}