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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/* sbc_harness/fs_harness_uptime_txt.c - 9P access to harness uptime
*
* Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <stdio.h> /* for snprintf() */
#include <stdlib.h> /* for malloc(), free() */
#include <libhw/generic/alarmclock.h>
#include <util9p/static.h>
#include "fs_harness_uptime_txt.h"
LO_IMPLEMENTATION_C(lib9p_srv_file, struct uptime_file, uptime_file, static);
struct uptime_fio {
struct uptime_file *parent;
size_t buf_len;
char buf[24]; /* len(str(UINT64_MAX)+"ns\n\0") */
};
LO_IMPLEMENTATION_H(lib9p_srv_fio, struct uptime_fio, uptime_fio);
LO_IMPLEMENTATION_C(lib9p_srv_fio, struct uptime_fio, uptime_fio, static);
/* srv_file *******************************************************************/
static void uptime_file_free(struct uptime_file *self) {
assert(self);
}
static struct lib9p_qid uptime_file_qid(struct uptime_file *self) {
assert(self);
return (struct lib9p_qid){
.type = LIB9P_QT_FILE,
.vers = 1,
.path = self->pathnum,
};
}
static struct lib9p_stat uptime_file_stat(struct uptime_file *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
uint64_t now = LO_CALL(bootclock, get_time_ns);
uint64_t size = 0;
while (now) {
size++;
now /= 10;
}
if (!size)
size++;
size += 3;
return (struct lib9p_stat){
.kern_type = 0,
.kern_dev = 0,
.file_qid = uptime_file_qid(self),
.file_mode = 0444,
.file_atime = UTIL9P_ATIME,
.file_mtime = UTIL9P_MTIME,
.file_size = size,
.file_name = lib9p_str(self->name),
.file_owner_uid = lib9p_str("root"),
.file_owner_gid = lib9p_str("root"),
.file_last_modified_uid = lib9p_str("root"),
.file_extension = lib9p_str(NULL),
.file_owner_n_uid = 0,
.file_owner_n_gid = 0,
.file_last_modified_n_uid = 0,
};
}
static void uptime_file_wstat(struct uptime_file *self, struct lib9p_srv_ctx *ctx,
struct lib9p_stat) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
}
static void uptime_file_remove(struct uptime_file *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
}
LIB9P_SRV_NOTDIR(struct uptime_file, uptime_file);
static lo_interface lib9p_srv_fio uptime_file_fopen(struct uptime_file *self, struct lib9p_srv_ctx *ctx,
bool LM_UNUSED(rd), bool LM_UNUSED(wr), bool LM_UNUSED(trunc)) {
assert(self);
assert(ctx);
struct uptime_fio *ret = malloc(sizeof(struct uptime_fio));
ret->parent = self;
ret->buf_len = 0;
return lo_box_uptime_fio_as_lib9p_srv_fio(ret);
}
/* srv_fio ********************************************************************/
static uint32_t uptime_fio_iounit(struct uptime_fio *self) {
assert(self);
return sizeof(self->buf)-1;
}
static void uptime_fio_iofree(struct uptime_fio *self) {
assert(self);
free(self);
}
static struct lib9p_qid uptime_fio_qid(struct uptime_fio *self) {
assert(self);
assert(self->parent);
return uptime_file_qid(self->parent);
}
static void uptime_fio_pread(struct uptime_fio *self, struct lib9p_srv_ctx *ctx,
uint32_t byte_count, uint64_t byte_offset,
struct iovec *ret) {
assert(self);
assert(ctx);
assert(ret);
if (byte_offset == 0 || self->buf_len == 0) {
uint64_t now = LO_CALL(bootclock, get_time_ns);
self->buf_len = snprintf(self->buf, sizeof(self->buf), "%"PRIu64"ns\n", now);
}
if (byte_offset > (uint64_t)self->buf_len) {
lib9p_error(&ctx->basectx,
LIB9P_ERRNO_L_EINVAL, "offset is past end-of-file length");
return;
}
size_t beg_off = (size_t)byte_offset;
size_t end_off = beg_off + (size_t)byte_count;
if (end_off > self->buf_len)
end_off = self->buf_len;
*ret = (struct iovec){
.iov_base = &self->buf[beg_off],
.iov_len = end_off-beg_off,
};
}
static uint32_t uptime_fio_pwrite(struct uptime_fio *self, struct lib9p_srv_ctx *ctx,
void *LM_UNUSED(buf),
uint32_t LM_UNUSED(byte_count),
uint64_t LM_UNUSED(byte_offset)) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
return 0;
}
|