summaryrefslogtreecommitdiff
path: root/cmd/srv9p/main.c
blob: d30c4f9e3e811961981673a97d8301e6906da143 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* srv9p/main.c - Main entry point for test 9P server
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <error.h>
#include <stdio.h>

#include <lib9p/srv.h>
#include <libcr/coroutine.h>
#include <libhw/generic/net.h>
#include <libhw/host_net.h>

#include "static9p.h"
#include "static.h"

/* configuration **************************************************************/

#include "config.h"

#ifndef CONFIG_NETIO_NUM_CONNS
	#error config.h must define CONFIG_NETIO_NUM_CONNS
#endif

/* implementation *************************************************************/

#define UNUSED(name) /* name __attribute__((unused)) */

/* file tree ******************************************************************/

#define FILE_COMMON(NAME) {                                             \
		.vtable = &static_file_vtable,                            \
		                                                          \
		.u_name = "root", .u_num = 0, /* owner user */            \
		.g_name = "root", .g_num = 0, /* owner group */           \
		.m_name = "root", .m_num = 0, /* last-modified-by user */ \
		                                                          \
		.pathnum = __COUNTER__,                                   \
		.name    = NAME,                                          \
		.perm    = 0444,                                          \
		.atime   = 1728337905,                                    \
		.mtime   = 1728337904,                                    \
	}

#define DIR_COMMON(NAME) {                                              \
		.vtable = &static_dir_vtable,                             \
		                                                          \
		.u_name = "root", .u_num = 0, /* owner user */            \
		.g_name = "root", .g_num = 0, /* owner group */           \
		.m_name = "root", .m_num = 0, /* last-modified-by user */ \
		                                                          \
		.pathnum = __COUNTER__,                                   \
		.name    = NAME,                                          \
		.perm    = 0555,                                          \
		.atime   = 1728337905,                                    \
		.mtime   = 1728337904,                                    \
	}

#define STATIC_FILE(STRNAME, SYMNAME)                           \
	&((static struct static_file){                          \
		._static_common = FILE_COMMON(STRNAME),           \
		.data_start     = _binary_static_##SYMNAME##_start, \
		.data_end       = _binary_static_##SYMNAME##_end,   \
	})

static struct static_dir root = {
	._static_common = DIR_COMMON(""),
	.members = {
		&((static struct static_dir){
			._static_common = DIR_COMMON("Documentation"),
			.members = {
				STATIC_FILE("x", Documentation_x),
				NULL
			},
		}),
		STATIC_FILE("README.md", README_md),
		NULL,
	},
};

static implements_lib9p_srv_file *get_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) {
	return &root;
}

/* main ***********************************************************************/

static COROUTINE read_cr(void *_srv) {
	struct lib9p_srv *srv = _srv;
	assert(srv);

	cr_begin();

	struct hostnet_tcp_listener listener;
	hostnet_tcp_listener_init(&listener, 9000);

	lib9p_srv_read_cr(srv, &listener);

	cr_end();
}

int main() {
	struct lib9p_srv srv = {
		.rootdir = get_root,
	};

	for (int i = 0; i < CONFIG_NETIO_NUM_CONNS; i++)
		if (!coroutine_add(read_cr, &srv))
			error(1, 0, "coroutine_add(read_cr, &srv)");
	for (int i = 0; i < 2*CONFIG_NETIO_NUM_CONNS; i++)
		if (!coroutine_add(lib9p_srv_write_cr, &srv))
			error(1, 0, "coroutine_add(lib9p_srv_write_cr, &srv)");

	coroutine_main();
	return 1;
}