net.c (11 calls), logs.c (9), disk_linux.c (13) and mount_linux.c (1) are messages 187-217 of mcdesys.cat now: no interfaces, no card of that name, the network-settings administrator sentence, the address checks (IPv4, subnet mask, router, IPv6 with prefix, DNS, domain), the DHCP client ID rule; the log sentences (no room, no name, inotify missing, cannot be watched/read, kqueue is a follow-up on this system, no buffer); the disk sentence set (lsblk/udisksctl missing, no such disk, not mounted, the system's, cannot be checked, erasing needs the administrator, the name rule, no drive, holds the system, mounted on, no permissions list); "No /proc/mounts." German copy with the same numbers in programs/localized/de_DE.UTF-8/msg/mcdesys.msg. Measured with a runner (setlocale + the public calls): LANG=C "Only the administrator can change the network settings: click the lock first." and "This log cannot be read."; LANG=de_DE.UTF-8 "Nur der Administrator kann die Netzwerkeinstellungen ändern: zuerst entsperren." and "Dieses Log kann nicht gelesen werden." make mcde-linux MCDE_DIRS="install lib/McdeSys" and "install programs/localized/C/msg" exit=0; "check lib/McdeSys": # FAIL: 0, exit=0; MCDE_DIRS=plist: ok.
179 lines
5.0 KiB
C
179 lines
5.0 KiB
C
/*
|
|
* 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 <dirent.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#ifdef __linux__
|
|
#include <sys/inotify.h>
|
|
#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, mcde_sys_msg(MSG_LOG_NO_ROOM, "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, mcde_sys_msg(MSG_LOG_NO_NAME, "No log was named."));
|
|
fd = inotify_init1(IN_NONBLOCK);
|
|
if (fd < 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_LOG_NO_INOTIFY, "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, mcde_sys_msg(MSG_LOG_WATCH_FAIL, "This log cannot be watched."));
|
|
}
|
|
return fd;
|
|
#else
|
|
(void)path;
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_LOG_WATCH_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, mcde_sys_msg(MSG_LOG_NO_BUFFER, "No log or buffer was named."));
|
|
if ((f = fopen(path, "r")) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_LOG_READ_FAIL, "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, mcde_sys_msg(MSG_LOG_READ_FAIL2, "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, mcde_sys_msg(MSG_NO_MEMORY, "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;
|
|
}
|