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
157
158
159
160
161
162
163
164
165
|
/* 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 <libhw/generic/alarmclock.h>
#include <libmisc/alloc.h> /* for heap_alloc(), free() */
#include <libmisc/fmt.h> /* for fmt_snprint() */
#include <util9p/static.h>
#include "fs_harness_uptime_txt.h"
LO_IMPLEMENTATION_C(lib9p_srv_file, struct uptime_file, uptime_file);
struct uptime_fio {
struct uptime_file *parent;
size_t buf_len;
/* The maximum length (UINT64_MAX) string is 52 bytes, not
* including a nul-terminator:
*
* "18446744073709551615ns\n" # 22+1
* "584y343d 23h34m33.709551615s\n" # 28+1
*/
char buf[52];
};
LO_IMPLEMENTATION_STATIC(lib9p_srv_fio, struct uptime_fio, uptime_fio);
/* srv_file *******************************************************************/
void uptime_file_free(struct uptime_file *self) {
assert(self);
}
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,
};
}
lib9p_srv_stat_or_error 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 ERROR_NEW_VAL(lib9p_srv_stat, ((struct lib9p_srv_stat){
.qid = uptime_file_qid(self),
.mode = 0444,
.atime_sec = UTIL9P_ATIME,
.mtime_sec = UTIL9P_MTIME,
.size = size,
.name = lib9p_str(self->name),
.owner_uid = { .name = lib9p_str("root"), .num = 0 },
.owner_gid = { .name = lib9p_str("root"), .num = 0 },
.last_modifier_uid = { .name = lib9p_str("root"), .num = 0 },
.extension = lib9p_str(NULL),
}));
}
error uptime_file_wstat(struct uptime_file *self, struct lib9p_srv_ctx *ctx, struct lib9p_srv_stat) {
assert(self);
assert(ctx);
return error_new(E_POSIX_EROFS, "read-only part of filesystem");
}
error uptime_file_remove(struct uptime_file *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
return error_new(E_POSIX_EROFS, "read-only part of filesystem");
}
LIB9P_SRV_NOTDIR(, struct uptime_file, uptime_file);
lib9p_srv_fio_or_error 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 = heap_alloc(1, struct uptime_fio);
ret->parent = self;
ret->buf_len = 0;
return ERROR_NEW_VAL(lib9p_srv_fio, LO_BOX(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_ioqid(struct uptime_fio *self) {
assert(self);
assert(self->parent);
return uptime_file_qid(self->parent);
}
#define NS_PER_M (NS_PER_S*60)
#define NS_PER_H (NS_PER_S*60*60)
#define NS_PER_D (NS_PER_S*60*60*24)
#define NS_PER_Y (NS_PER_S*60*60*24*365)
static error uptime_fio_pread(struct uptime_fio *self, struct lib9p_srv_ctx *ctx,
lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) {
assert(self);
assert(ctx);
if (byte_offset == 0 || self->buf_len == 0) {
uint64_t now = LO_CALL(bootclock, get_time_ns);
self->buf_len = fmt_snprint(self->buf, sizeof(self->buf), now, "ns\n");
uint64_t ns = now;
uint64_t y = ns/NS_PER_Y; ns -= y*NS_PER_Y;
uint64_t d = ns/NS_PER_D; ns -= d*NS_PER_D;
uint64_t h = ns/NS_PER_H; ns -= h*NS_PER_H;
uint64_t m = ns/NS_PER_M; ns -= m*NS_PER_M;
uint64_t s = ns/NS_PER_S; ns -= s*NS_PER_S;
if (y)
self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, y, "y");
if (y || d)
self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, d, "d ");
if (y || d || h)
self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, h, "h");
if (y || d || h || m)
self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, m, "m");
self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, s, ".", (rjust, 9, '0', ns), "s\n");
}
if (byte_offset > (uint64_t)self->buf_len)
return error_new(E_POSIX_EINVAL, "offset is past end-of-file length");
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;
return io_write(dst, &self->buf[beg_off], end_off-beg_off).err;
}
static uint32_t_or_error 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);
return ERROR_NEW_ERR(uint32_t, error_new(E_POSIX_EROFS, "read-only part of filesystem"));
}
|