/* sbc_harness/fs_harness_uptime_txt.c - 9P access to harness uptime * * Copyright (C) 2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ #include #include /* for heap_alloc(), free() */ #include /* for fmt_snprint() */ #include #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")); }