ohmu
·
2026-10-15
1#include "common/duration.h"
2#include "common/printing.h"
3#include "common/jsonconfig.h"
4#include "common/time.h"
5#include "common/strutil.h"
6#include "detection/uptime/uptime.h"
7#include "modules/uptime/uptime.h"
8
9bool ffPrintUptime(FFUptimeOptions* options) {
10 FFUptimeResult result = {};
11
12 const char* error = ffDetectUptime(&result);
13
14 if (error) {
15 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Uptime), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
16 return false;
17 }
18
19 uint64_t uptime = result.uptime;
20 FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
21 ffDurationAppendNum((uptime + 500) / 1000, &buffer);
22
23 if (options->moduleArgs.outputFormat.length == 0) {
24 ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Uptime), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
25 ffStrbufPutTo(&buffer, stdout);
26 } else {
27 uint32_t milliseconds = (uint32_t) (uptime % 1000);
28 uptime /= 1000;
29 uint32_t seconds = (uint32_t) (uptime % 60);
30 uptime /= 60;
31 uint32_t minutes = (uint32_t) (uptime % 60);
32 uptime /= 60;
33 uint32_t hours = (uint32_t) (uptime % 24);
34 uptime /= 24;
35 uint32_t days = (uint32_t) uptime;
36
37 FFTimeGetAgeResult age = ffTimeGetAge(result.bootTime, ffTimeGetNow());
38
39 FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Uptime), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { FF_ARG(days, "days"), FF_ARG(hours, "hours"), FF_ARG(minutes, "minutes"), FF_ARG(seconds, "seconds"), FF_ARG(milliseconds, "milliseconds"), { FF_ARG_TYPE_STRING, ffTimeToShortStr(result.bootTime), "boot-time" }, FF_ARG(age.years, "years"), FF_ARG(age.daysOfYear, "days-of-year"), FF_ARG(age.yearsFraction, "years-fraction"), FF_ARG(buffer, "formatted") }));
40 }
41
42 return true;
43}
44
45void ffParseUptimeJsonObject(FFUptimeOptions* options, yyjson_val* module) {
46 yyjson_val *key, *val;
47 size_t idx, max;
48 yyjson_obj_foreach (module, idx, max, key, val) {
49 if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
50 continue;
51 }
52
53 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Uptime), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
54 }
55}
56
57[[gnu::cold]]
58void ffGenerateUptimeJsonConfig(FFUptimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
59 ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
60}
61
62bool ffGenerateUptimeJsonResult([[maybe_unused]] FFUptimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
63 FFUptimeResult result;
64 const char* error = ffDetectUptime(&result);
65
66 if (error) {
67 yyjson_mut_obj_add_str(doc, module, "error", error);
68 return false;
69 }
70
71 yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
72 yyjson_mut_obj_add_uint(doc, obj, "uptime", result.uptime);
73 yyjson_mut_obj_add_strcpy(doc, obj, "bootTime", ffTimeToFullStr(result.bootTime));
74
75 return true;
76}
77
78void ffInitUptimeOptions(FFUptimeOptions* options) {
79 ffOptionInitModuleArg(&options->moduleArgs, "");
80}
81
82void ffDestroyUptimeOptions(FFUptimeOptions* options) {
83 ffOptionDestroyModuleArg(&options->moduleArgs);
84}
85
86FFModuleBaseInfo ffUptimeModuleInfo = {
87 .name = "Uptime",
88 .description = "Print how long the system has been running",
89 .displayName = {
90 .en = "Uptime",
91 .ar = "مدة التشغيل",
92 .cs = "Doba běhu",
93 .de = "Betriebszeit",
94 .es = "Tiempo de actividad",
95 .fi = "Käynnissäoloaika",
96 .fr = "Temps de fonctionnement",
97 .gl = "Tempo de actividade",
98 .he = "זמן פעילות",
99 .id = "Waktu Aktif",
100 .it = "Tempo di attività",
101 .ja = "稼働時間",
102 .ko = "업타임",
103 .pl = "Czas pracy",
104 .pt = "Tempo de atividade",
105 .ru = "Время работы",
106 .tr = "Çalışma Süresi",
107 .uk = "Час роботи",
108 .vi = "Thời gian hoạt động",
109 .zh_CN = "运行时间",
110 .zh_TW = "運行時間",
111 },
112 .initOptions = (void*) ffInitUptimeOptions,
113 .destroyOptions = (void*) ffDestroyUptimeOptions,
114 .parseJsonObject = (void*) ffParseUptimeJsonObject,
115 .printModule = (void*) ffPrintUptime,
116 .generateJsonResult = (void*) ffGenerateUptimeJsonResult,
117 .generateJsonConfig = (void*) ffGenerateUptimeJsonConfig,
118 .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
119 { "Days after boot", "days" },
120 { "Hours after boot", "hours" },
121 { "Minutes after boot", "minutes" },
122 { "Seconds after boot", "seconds" },
123 { "Milliseconds after boot", "milliseconds" },
124 { "Boot time in local timezone", "boot-time" },
125 { "Years integer after boot", "years" },
126 { "Days of year after boot", "days-of-year" },
127 { "Years fraction after boot", "years-fraction" },
128 { "Formatted uptime", "formatted" },
129 })),
130 .defaultOrder = 11,
131};