diff --git a/AGENTS.md b/AGENTS.md index dbe81494..0cc5f6df 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,6 +155,19 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt toggle and the text field exist unmanaged (`G_rename_toggle`, `G_rename_text`). The multi-collision and the folder-merge dialog are unchanged (follow-up). +- **dtconsole** (`programs/dtconsole`): the log viewer -- the session's own + (`~/.dt/errorlog`, `~/.dt/startlog`) and the files under `/var/log` on the + left, the tail of the chosen one on the right, a filter field, Reload and + a line count. Values come from McdeSys (`mcde_sys_logs`, + `mcde_sys_log_tail`); a log the user may not read is listed as such + (`boot.log (not readable)`, measured). A selected log is watched with + `mcde_sys_log_watch` (inotify on Linux) and `XtAppAddInput` -- the view + follows the file without a timer (measured: an appended line appears and + the count grows while dtconsole runs). NetBSD's kqueue branch is the + follow-up. A new program is wired in three places: its directory with a + Makefile.am, `programs/Makefile.am`'s SUBDIRS and `configure.ac`'s + AC_CONFIG_FILES (the build regenerates configure and config.status by + itself, measured). - **System Information**: `dtsystemsettings/sysinfo.c` -- the sections Hardware, Network and Software in a list on the left, the values on the right from McdeSys (`mcde_sys_info`: model, processor, memory, startup diff --git a/configure.ac b/configure.ac index a66bef79..399800c3 100644 --- a/configure.ac +++ b/configure.ac @@ -898,6 +898,7 @@ programs/dtsystemsettings/Makefile programs/dtdiskutility/Makefile programs/dtbackup/Makefile programs/dtphoto/Makefile +programs/dtconsole/Makefile programs/dtpaint/Makefile programs/dttoolbox/Makefile programs/mcdesoundd/Makefile diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index ada4f632..69146367 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -417,6 +417,33 @@ int mcde_sys_ntp_get(int *on, char *server, size_t len); int mcde_sys_ntp_set(int on, const char *server); int mcde_sys_time_set(time_t when); +/* + * Logs + * + * The logs a person looks at without a terminal: the session's own + * (CDE's ~/.dt/errorlog and ~/.dt/startlog) and the system's files under + * /var/log. A log that cannot be read is listed as such (root would be + * needed); watching a file gives a descriptor that becomes readable when the + * file grows -- inotify on Linux, kqueue on NetBSD (the latter is the + * follow-up), never a timer. + */ +#define MCDE_SYS_LOG_NAME 96 +#define MCDE_SYS_LOG_PATH 256 + +typedef struct { + char name[MCDE_SYS_LOG_NAME]; + char path[MCDE_SYS_LOG_PATH]; + int readable; /* the user may read it */ +} McdeSysLog; + +/* the logs on this computer; the count */ +int mcde_sys_logs(McdeSysLog *list, size_t max); +/* a descriptor that is readable when PATH grows (MCDE_SYS_ENOTSUP where + * the system has no way to watch a file) */ +int mcde_sys_log_watch(const char *path); +/* the last LINE lines of PATH (at most LEN-1 bytes) into BUF; 0 or -1 */ +int mcde_sys_log_tail(const char *path, int lines, char *buf, size_t len); + /* * Printers (CUPS) * diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index 18c3e21f..d4f295e4 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -5,7 +5,7 @@ MAINTAINERCLEANFILES = Makefile.in lib_LTLIBRARIES = libMcdeSys.la libMcdeSys_la_CPPFLAGS = -I$(top_srcdir)/include -libMcdeSys_la_SOURCES = common.c net.c sound.c users.c share.c netmount.c backup.c printers.c private.h +libMcdeSys_la_SOURCES = common.c net.c sound.c users.c share.c netmount.c backup.c printers.c logs.c private.h if NETBSD libMcdeSys_la_SOURCES += netbsd.c disk_netbsd.c # crypt(3), for the password hash; proplib(3), for the battery; libdns_sd diff --git a/lib/McdeSys/logs.c b/lib/McdeSys/logs.c new file mode 100644 index 00000000..56f21e1f --- /dev/null +++ b/lib/McdeSys/logs.c @@ -0,0 +1,183 @@ +/* + * logs.c -- MCDE: the logs a person looks at (McdeSys.h) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * The list is assembled from what the session and the system keep: CDE's + * ~/.dt/errorlog and ~/.dt/startlog, and the regular files under /var/log + * that end in a log name (the newest first, at most 24). A file the user + * may not read stays in the list with readable = 0 -- the admin path of + * t0079 opens it. + * + * The watch: inotify on Linux; NetBSD's kqueue (EVFILT_VNODE) is the + * follow-up, and on a system without a way to watch, the call says so. + * + * C99 and POSIX only (ext/src/mcde/AGENTS.md). + */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ +#include +#endif + +#include "private.h" + +#define MAX_SYS_LOGS 24 + +static int +Add(McdeSysLog *list, size_t max, int n, const char *path, + const char *name) +{ + struct stat st; + + if (list == NULL || (size_t) n >= max) + return n; + if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) + return n; + (void)snprintf(list[n].name, sizeof list[n].name, "%s", name); + (void)snprintf(list[n].path, sizeof list[n].path, "%s", path); + list[n].readable = access(path, R_OK) == 0; + return n + 1; +} + +int +mcde_sys_logs(McdeSysLog *list, size_t max) +{ + const char *home = getenv("HOME"); + char path[512], name[128]; + struct dirent *de; + DIR *d; + int n = 0; + + if (list == NULL) + return mcde_sys_fail(MCDE_SYS_EINVAL, "There is no place for " + "the logs."); + if (home != NULL) { + (void)snprintf(path, sizeof path, "%s/.dt/errorlog", home); + n = Add(list, max, n, path, "Session errors"); + (void)snprintf(path, sizeof path, "%s/.dt/startlog", home); + n = Add(list, max, n, path, "Session start"); + } + (void)snprintf(path, sizeof path, "/var/log/messages"); + n = Add(list, max, n, path, "System messages"); + (void)snprintf(path, sizeof path, "/var/log/syslog"); + n = Add(list, max, n, path, "System log (syslog)"); + (void)snprintf(path, sizeof path, "/var/log/auth.log"); + n = Add(list, max, n, path, "Authentication"); + if ((d = opendir("/var/log")) != NULL) { + while ((de = readdir(d)) != NULL && n < (int) max && + n < MAX_SYS_LOGS) { + size_t l = strlen(de->d_name); + + if (de->d_name[0] == '.' || l < 5) + continue; + if (strcmp(de->d_name + l - 4, ".log") != 0 && + strcmp(de->d_name, "lastlog") != 0) + continue; + (void)snprintf(path, sizeof path, "/var/log/%s", + de->d_name); + (void)snprintf(name, sizeof name, "%s", de->d_name); + n = Add(list, max, n, path, name); + } + closedir(d); + } + return n; +} + +int +mcde_sys_log_watch(const char *path) +{ +#ifdef __linux__ + int fd; + + if (path == NULL || path[0] == '\0') + return mcde_sys_fail(MCDE_SYS_EINVAL, "No log was named."); + fd = inotify_init1(IN_NONBLOCK); + if (fd < 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "inotify is not there."); + if (inotify_add_watch(fd, path, IN_MODIFY | IN_ATTRIB) < 0) { + (void)close(fd); + return mcde_sys_fail(MCDE_SYS_EFAIL, "This log cannot be " + "watched."); + } + return fd; +#else + (void)path; + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "Watching a log file is " + "inotify's on this system; kqueue on NetBSD is a follow-up."); +#endif +} + +int +mcde_sys_log_tail(const char *path, int lines, char *buf, size_t len) +{ + FILE *f; + long size, from; + char line[2048]; + int keep = lines > 0 ? lines : 100; + int seen = 0, i; + char **ring; + + if (path == NULL || buf == NULL || len == 0) + return mcde_sys_fail(MCDE_SYS_EINVAL, "No log or buffer was " + "named."); + if ((f = fopen(path, "r")) == NULL) + return mcde_sys_fail(MCDE_SYS_EPERM, "This log cannot be read."); + /* the tail: seek near the end and drop the first part-line */ + if (fseek(f, 0, SEEK_END) != 0 || (size = ftell(f)) < 0) { + fclose(f); + return mcde_sys_fail(MCDE_SYS_EFAIL, "The log cannot be " + "read."); + } + from = size > 65536 ? size - 65536 : 0; + if (from > 0) + from += 0; /* the first line is dropped below */ + (void)fseek(f, from, SEEK_SET); + buf[0] = '\0'; + { + size_t at = 0; + + if (from > 0 && fgets(line, sizeof line, f) == NULL) { + fclose(f); + return MCDE_SYS_OK; + } + ring = calloc((size_t) keep, sizeof *ring); + if (ring == NULL) { + fclose(f); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory."); + } + while (fgets(line, sizeof line, f) != NULL) { + free(ring[seen % keep]); + ring[seen % keep] = strdup(line); + seen++; + } + fclose(f); + for (i = seen > keep ? seen - keep : 0; i < seen; i++) { + const char *l = ring[i % keep]; + size_t n; + + if (l == NULL) + continue; + n = strlen(l); + if (at + n >= len) + break; + memcpy(buf + at, l, n); + at += n; + } + buf[at] = '\0'; + for (i = 0; i < keep; i++) + free(ring[i]); + free(ring); + } + return MCDE_SYS_OK; +} diff --git a/programs/Makefile.am b/programs/Makefile.am index afa65853..3cbca239 100644 --- a/programs/Makefile.am +++ b/programs/Makefile.am @@ -6,7 +6,7 @@ SUBDIRS = backdrops icons dsdm dtmail dtpad dtfile dterror dtwm dtlogin \ palettes dtappintegrate dtprintegrate dtconfig \ dtcreate dtprintinfo fontaliases dtdspmsg dtimsstart \ dtpdm dtsr dtpdmd dtinfo types tttypes util dtopen localized \ - dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility dtbackup dtappscan dtkeys dtthemesync dtphoto dtpaint dttoolbox mcdesoundd + dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility dtbackup dtappscan dtkeys dtthemesync dtphoto dtpaint dttoolbox mcdesoundd dtconsole # The application builder and ttsnoop, its demonstration, need dtcodegen, a # program the build runs: not in a cross build (--disable-appbuilder; the native diff --git a/programs/dtconsole/Makefile.am b/programs/dtconsole/Makefile.am new file mode 100644 index 00000000..b6062f93 --- /dev/null +++ b/programs/dtconsole/Makefile.am @@ -0,0 +1,14 @@ +MAINTAINERCLEANFILES = Makefile.in + +bin_PROGRAMS = dtconsole + +dtconsole_CPPFLAGS = -I$(top_srcdir)/include \ + -DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" + +dtconsole_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XTOOLLIB) + +dtconsole_SOURCES = dtconsole.c + +if SOLARIS +dtconsole_LDADD += -lintl +endif diff --git a/programs/dtconsole/dtconsole.c b/programs/dtconsole/dtconsole.c new file mode 100644 index 00000000..91975f44 --- /dev/null +++ b/programs/dtconsole/dtconsole.c @@ -0,0 +1,306 @@ +/* + * dtconsole.c -- MCDE: the log viewer (t0123) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * The reference's window: the logs on the left (the session's own, the + * system's under /var/log), the messages on the right with a filter above + * them and Reload. The tail of a log is read through McdeSys + * (mcde_sys_log_tail) and a selected log is watched through + * mcde_sys_log_watch: the descriptor becomes readable when the file grows, + * XtAppAddInput hands that to this program -- no timer, no polling. A log + * the user may not read is listed and says so; the administrator's path + * (t0079) opens it. + * + * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). + */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static XtAppContext app; + +/* ---- the small helpers ---- */ + +static void +SetText(Widget w, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + + XtVaSetValues(w, XmNlabelString, s, NULL); + XmStringFree(s); +} + +static Widget +Text(Widget parent, const char *name, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + Widget w = XtVaCreateManagedWidget((char *) name, + xmLabelWidgetClass, parent, XmNlabelString, s, + XmNalignment, XmALIGNMENT_BEGINNING, NULL); + + XmStringFree(s); + return w; +} + +static Widget +Button(Widget parent, const char *label, XtCallbackProc cb, XtPointer data) +{ + XmString s = XmStringCreateLocalized((char *) label); + Widget b = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass, + parent, XmNlabelString, s, NULL); + + XmStringFree(s); + if (cb != NULL) + XtAddCallback(b, XmNactivateCallback, cb, data); + return b; +} + +#define MAX_LOGS 32 + +static Widget shell, logList, text, filterField, status; +static int watchFd = -1; +static int chosen = -1; +static char currentPath[512]; + +static void +Show(int at) +{ + static char buf[200000]; + int n; + + if (at < 0) + return; + if (mcde_sys_log_tail(currentPath, 500, buf, sizeof buf) != + MCDE_SYS_OK) { + XmTextSetString(text, (char *) mcde_sys_error()); + SetText(status, mcde_sys_error()); + return; + } + n = (int) strlen(buf); + (void) n; + XmTextSetString(text, buf); + { + char *f = XmTextFieldGetString(filterField); + + if (f != NULL && f[0] != '\0') { + /* a filter shows the lines that carry the text */ + char *copy = strdup(buf), *save = NULL, *line, + *out = calloc(1, strlen(buf) + 64); + char low[256]; + size_t k; + + (void)snprintf(low, sizeof low, "%s", f); + for (k = 0; low[k] != '\0'; k++) + low[k] = (char) (low[k] >= 'A' && low[k] <= 'Z' + ? low[k] - 'A' + 'a' : low[k]); + if (copy != NULL && out != NULL) { + for (line = strtok_r(copy, "\n", &save); + line != NULL; + line = strtok_r(NULL, "\n", &save)) { + char l2[2048]; + size_t j; + + (void)snprintf(l2, sizeof l2, "%s", + line); + for (j = 0; l2[j] != '\0'; j++) + l2[j] = (char) (l2[j] >= 'A' && + l2[j] <= 'Z' ? + l2[j] - 'A' + 'a' : l2[j]); + if (strstr(l2, low) != NULL) { + strcat(out, line); + strcat(out, "\n"); + } + } + XmTextSetString(text, out); + } + free(copy); + free(out); + } + if (f != NULL) + XtFree(f); + } + XmTextShowPosition(text, XmTextGetLastPosition(text)); + { + char *shown = XmTextGetString(text); + char s[128]; + int lines = 0; + const char *p; + + for (p = shown; p != NULL && *p != '\0'; p++) + if (*p == '\n') + lines++; + if (shown != NULL) + XtFree(shown); + (void)snprintf(s, sizeof s, "%d lines", lines); + SetText(status, s); + } +} + +static void OnGrown(XtPointer data, int *src, XtInputId *id); + +/* the watched log grew: drain what the kernel queued, show the new tail */ +static void +OnGrown(XtPointer data, int *src, XtInputId *id) +{ + char drain[4096]; + + (void) data; + (void) id; + while (read(*src, drain, sizeof drain) > 0) + ; + Show(chosen); +} + +static void +Watch(void) +{ + if (watchFd >= 0) { + XtRemoveInput(watchFd); + (void) close(watchFd); + watchFd = -1; + } + watchFd = mcde_sys_log_watch(currentPath); + if (watchFd >= 0) + XtAppAddInput(app, watchFd, (XtPointer) XtInputReadMask, + OnGrown, NULL); +} + +static void +Chosen(Widget w, XtPointer data, XtPointer call) +{ + XmListCallbackStruct *c = (XmListCallbackStruct *) call; + McdeSysLog logs[MAX_LOGS]; + int n; + + (void) w; + (void) data; + (void) call; + n = mcde_sys_logs(logs, MAX_LOGS); + if (c->item_position < 1 || c->item_position > n) + return; + chosen = c->item_position - 1; + (void)snprintf(currentPath, sizeof currentPath, "%s", + logs[chosen].path); + Watch(); + Show(chosen); +} + +static void +Reload(Widget w, XtPointer data, XtPointer call) +{ + (void) w; + (void) data; + (void) call; + Show(chosen); +} + +static void +Fill(Widget w) +{ + McdeSysLog logs[MAX_LOGS]; + int n, i; + + (void) w; + XmListDeleteAllItems(logList); + n = mcde_sys_logs(logs, MAX_LOGS); + for (i = 0; i < n; i++) { + char row[256]; + XmString s; + + (void)snprintf(row, sizeof row, "%s%s", logs[i].name, + logs[i].readable ? "" : " (not readable)"); + s = XmStringCreateLocalized(row); + XmListAddItemUnselected(logList, s, 0); + XmStringFree(s); + } + if (n > 0) { + XmListSelectPos(logList, 1, False); + chosen = 0; + (void)snprintf(currentPath, sizeof currentPath, "%s", + logs[0].path); + Watch(); + Show(0); + } else + SetText(status, mcde_sys_error()); +} + +int +main(int argc, char **argv) +{ + Widget form, bar, sw, pane; + static String fallback[] = { + "*renderTable: mcde", + "*XmText*fontList: fixed", + "*text.columns: 90", + "*text.rows: 24", + NULL + }; + XmString s; + + XtSetLanguageProc(NULL, NULL, NULL); + shell = XtVaAppInitialize(&app, "Dtconsole", NULL, 0, &argc, argv, + (String *) fallback, XmNtitle, "All Messages", NULL); + form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell, + XmNwidth, 820, XmNheight, 500, NULL); + bar = XtVaCreateManagedWidget("toolbar", xmRowColumnWidgetClass, form, + XmNorientation, XmHORIZONTAL, XmNspacing, 8, XmNmarginHeight, 4, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, NULL); + (void) Button(bar, "Reload", Reload, NULL); + s = XmStringCreateLocalized((char *) "Filter:"); + XtVaCreateManagedWidget("filterLabel", xmLabelWidgetClass, bar, + XmNlabelString, s, NULL); + XmStringFree(s); + filterField = XtVaCreateManagedWidget("filter", + xmTextFieldWidgetClass, bar, XmNcolumns, 24, NULL); + XtAddCallback(filterField, XmNvalueChangedCallback, Reload, NULL); + + pane = XtVaCreateManagedWidget("pane", xmRowColumnWidgetClass, form, + XmNorientation, XmHORIZONTAL, XmNspacing, 10, XmNmarginWidth, 8, + XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, bar, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, + status = Text(form, "status", ""), XmNbottomOffset, 8, NULL); + logList = XmCreateScrolledList(pane, "logs", NULL, 0); + XtVaSetValues(logList, XmNvisibleItemCount, 16, NULL); + XtVaSetValues(XtParent(logList), XmNwidth, 240, XmNheight, 400, + NULL); + XtManageChild(logList); + XtAddCallback(logList, XmNbrowseSelectionCallback, Chosen, NULL); + + sw = XtVaCreateManagedWidget("textBox", xmScrolledWindowWidgetClass, + pane, XmNscrollingPolicy, XmAUTOMATIC, XmNwidth, 540, + XmNheight, 400, NULL); + text = XtVaCreateManagedWidget("text", xmTextWidgetClass, sw, + XmNeditable, False, XmNcursorPositionVisible, False, + XmNwordWrap, False, NULL); + + XtVaSetValues(status, XmNbottomAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, + XmATTACH_FORM, NULL); + XtManageChild(form); + Fill(shell); + XtRealizeWidget(shell); + XtAppMainLoop(app); + return 0; +}