McdeSys gets the backup (lib/McdeSys/backup.c): a partition becomes the backup disk with an encrypted repository (borg init -e repokey-blake2); every hour root's crontab runs dtbackup run: borg create of / (and /home) on its own file systems, lz4, then prune -- 24 hourly, 30 daily, 52 weekly, every monthly unless set otherwise (keep_* in /etc/mcde-backup.conf, -1 keeps every one) -- and compact. The watcher: before and after each run, while the backup disk has less than min_free percent free (10 unless set), the oldest backup is deleted and the space given back; the newest is never deleted. Listing (borg list --json) and restoring a path into a folder (borg extract). The passphrase lives in /etc/mcde-backup.pass, root's alone, handed over by BORG_PASSCOMMAND. docs/reference/borg.txt from borg 1.4.5's own documentation. dtbackup run | status | list: the same from a shell and for cron, no X. Measured: builds on the Linux host and compiles in the build VM; as a user dtbackup status says no disk is set up and dtbackup run refuses. Not measured: a real backup -- borg is not on the host and goes into the set with the next userland. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
352 lines
13 KiB
C
352 lines
13 KiB
C
/*
|
|
* McdeSys.h -- MCDE's layer between the desktop and the operating system
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* Programs of MCDE (the System Preferences first) change the system only
|
|
* through these functions; each operating system has one backend in
|
|
* lib/McdeSys/<system>.c that does it the system's own way (on NetBSD:
|
|
* useradd(8), /etc/rc.conf, ifconfig(8), wpa_supplicant(8) ...). See
|
|
* ext/src/mcde/AGENTS.md, "The philosophy".
|
|
*
|
|
* C99 and POSIX only. Every function returns MCDE_SYS_OK or a negative
|
|
* McdeSysStatus; on failure, mcde_sys_error() gives a sentence for the user.
|
|
* Functions that change the system need the privileges the backend documents
|
|
* (today: root); they do not raise privileges themselves.
|
|
*
|
|
* Status: lib/McdeSys implements mcde_sys_backend, mcde_sys_error,
|
|
* mcde_sys_info, mcde_sys_power, mcde_sys_interfaces, the sound, Bluetooth,
|
|
* battery, time zone, sharing, the network browser, disks, backup,
|
|
* user and software update functions (common.c, net.c, sound.c, users.c,
|
|
* netbsd.c, linux.c); the network setup and the host name setter are the
|
|
* contract still.
|
|
*/
|
|
|
|
#ifndef _Mcde_McdeSys_h
|
|
#define _Mcde_McdeSys_h
|
|
|
|
#include <stddef.h>
|
|
#include <sys/types.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef enum {
|
|
MCDE_SYS_OK = 0,
|
|
MCDE_SYS_ENOTSUP = -1, /* this backend cannot do it */
|
|
MCDE_SYS_EPERM = -2, /* not allowed (privileges) */
|
|
MCDE_SYS_EINVAL = -3, /* a bad argument (name, address ...) */
|
|
MCDE_SYS_EEXIST = -4, /* it exists already */
|
|
MCDE_SYS_ENOENT = -5, /* it does not exist */
|
|
MCDE_SYS_EFAIL = -6 /* the system tool failed; see mcde_sys_error() */
|
|
} McdeSysStatus;
|
|
|
|
/* the name of the backend ("netbsd", "linux"), for the About box and logs */
|
|
const char *mcde_sys_backend(void);
|
|
/* the last failure as a sentence for the user; never NULL */
|
|
const char *mcde_sys_error(void);
|
|
|
|
/*
|
|
* Users and groups
|
|
*/
|
|
typedef struct {
|
|
char login[32]; /* [a-z_][a-z0-9_-]*, as useradd(8) takes it */
|
|
char fullname[128];
|
|
uid_t uid;
|
|
int admin; /* may administer the computer (wheel/sudo) */
|
|
} McdeSysUser;
|
|
|
|
/* the people who use this computer (not the system accounts); returns count */
|
|
int mcde_sys_users(McdeSysUser *users, size_t max);
|
|
int mcde_sys_user_add(const McdeSysUser *user, const char *password);
|
|
int mcde_sys_user_delete(const char *login, int keep_home);
|
|
int mcde_sys_user_set_password(const char *login, const char *password);
|
|
int mcde_sys_user_set_admin(const char *login, int admin);
|
|
|
|
/*
|
|
* Network
|
|
*/
|
|
typedef enum {
|
|
MCDE_NET_WIRED,
|
|
MCDE_NET_WIFI,
|
|
MCDE_NET_OTHER
|
|
} McdeSysNetKind;
|
|
|
|
typedef struct {
|
|
char name[32]; /* the system's name: wm0, urtwn0, eth0 */
|
|
McdeSysNetKind kind;
|
|
int up; /* has an address */
|
|
char address[64]; /* the IPv4 or IPv6 address, or "" */
|
|
char ssid[33]; /* Wi-Fi: the network joined, or "" */
|
|
} McdeSysInterface;
|
|
|
|
typedef struct {
|
|
char ssid[33];
|
|
int signal; /* 0..100 */
|
|
int secured;
|
|
} McdeSysWifi;
|
|
|
|
/* the network interfaces; returns count */
|
|
int mcde_sys_interfaces(McdeSysInterface *ifs, size_t max);
|
|
/* address by DHCP (the default), now and at every boot */
|
|
int mcde_sys_net_dhcp(const char *ifname);
|
|
/* the Wi-Fi networks in reach; returns count */
|
|
int mcde_sys_wifi_scan(const char *ifname, McdeSysWifi *nets, size_t max);
|
|
/* join a Wi-Fi network, now and at every boot; passphrase NULL if open */
|
|
int mcde_sys_wifi_join(const char *ifname, const char *ssid,
|
|
const char *passphrase);
|
|
|
|
/*
|
|
* The computer
|
|
*/
|
|
typedef struct {
|
|
char system[64]; /* "Makronix", "Debian GNU/Linux" */
|
|
char version[64]; /* "0.99.9", "14" */
|
|
char kernel[128]; /* "NetBSD 11.99.9", "Linux 7.1.3" */
|
|
char model[128]; /* the machine, as the firmware names it */
|
|
char cpu[128]; /* the processor, as it names itself */
|
|
int cpus; /* processors online */
|
|
unsigned long long memory; /* bytes */
|
|
char disk[128]; /* the startup disk: the device of / */
|
|
unsigned long long diskSize; /* bytes of /, and free of them */
|
|
unsigned long long diskFree;
|
|
char hostname[256];
|
|
} McdeSysInfo;
|
|
|
|
/* what the About box shows; fields the system cannot tell are "" or 0 */
|
|
int mcde_sys_info(McdeSysInfo *info);
|
|
|
|
typedef enum {
|
|
MCDE_POWER_SLEEP,
|
|
MCDE_POWER_RESTART,
|
|
MCDE_POWER_SHUTDOWN
|
|
} McdeSysPower;
|
|
|
|
/* sleep, restart or shut down the computer, the system's own way */
|
|
int mcde_sys_power(McdeSysPower what);
|
|
|
|
/*
|
|
* Sound: the volume of the default output, through the sound server where
|
|
* one runs (PulseAudio), else the system's mixer
|
|
*/
|
|
int mcde_sys_volume_get(int *percent, int *muted); /* 0..100 */
|
|
int mcde_sys_volume_set(int percent);
|
|
int mcde_sys_volume_mute(int muted);
|
|
|
|
/*
|
|
* Bluetooth: the first controller
|
|
*/
|
|
typedef struct {
|
|
int present; /* there is a controller */
|
|
int on; /* it is powered */
|
|
char name[32]; /* the system's name of it: ubt0, hci0 */
|
|
} McdeSysBluetooth;
|
|
|
|
int mcde_sys_bluetooth(McdeSysBluetooth *bt);
|
|
int mcde_sys_bluetooth_power(int on);
|
|
|
|
/*
|
|
* The battery: all batteries of the machine as one, and the mains
|
|
*/
|
|
typedef struct {
|
|
int present; /* there is a battery */
|
|
int percent; /* its charge, 0..100 */
|
|
int charging; /* it is being charged */
|
|
int ac; /* the machine runs on mains power */
|
|
int minutes; /* until it is empty, -1 when not known or
|
|
* not running on it */
|
|
} McdeSysBattery;
|
|
|
|
int mcde_sys_battery(McdeSysBattery *bat);
|
|
|
|
/*
|
|
* Software update: the system and the packages, from the update source the
|
|
* system names (NetBSD: the URL in /etc/makronix/update, a release directory
|
|
* as build.sh makes it)
|
|
*/
|
|
typedef struct {
|
|
char current[64]; /* the version running */
|
|
char available[64]; /* the version the source has, or "" */
|
|
int packages; /* packages with a newer version, or -1: unknown */
|
|
char source[256]; /* where it looks, for the user */
|
|
char note[256]; /* a sentence for the user, or "" */
|
|
} McdeSysUpdate;
|
|
|
|
int mcde_sys_update_check(McdeSysUpdate *u);
|
|
/* bring the system and the packages up to what the source has (root) */
|
|
int mcde_sys_update_apply(void);
|
|
|
|
int mcde_sys_hostname_get(char *name, size_t len);
|
|
int mcde_sys_hostname_set(const char *name); /* now and at boot */
|
|
/*
|
|
* Sharing: the services that let others in, and the folders they share.
|
|
* SMB (Samba), NFS and AFP (netatalk) share the same folders; AppleTalk is
|
|
* netatalk's atalkd, the old network itself (NetBSD's kernel has it).
|
|
*/
|
|
typedef enum {
|
|
MCDE_SHARE_SSH, /* remote login */
|
|
MCDE_SHARE_SMB, /* file sharing for Windows and others */
|
|
MCDE_SHARE_NFS, /* file sharing for Unix */
|
|
MCDE_SHARE_AFP, /* file sharing by AFP over TCP */
|
|
MCDE_SHARE_APPLETALK, /* the AppleTalk network (atalkd, papd) */
|
|
MCDE_SHARE_COUNT
|
|
} McdeSysShareService;
|
|
|
|
typedef struct {
|
|
int installed; /* the server is there */
|
|
int enabled; /* it starts at boot */
|
|
int running; /* it runs now */
|
|
} McdeSysServiceState;
|
|
|
|
int mcde_sys_service_state(McdeSysShareService s, McdeSysServiceState *st);
|
|
/* on or off, now and at every boot (root) */
|
|
int mcde_sys_service_set(McdeSysShareService s, int on);
|
|
|
|
#define MCDE_SYS_SHARE_LEN 256
|
|
/* the shared folders (/etc/mcde-shares); how many */
|
|
int mcde_sys_shares(char (*paths)[MCDE_SYS_SHARE_LEN], size_t max);
|
|
/* share these folders by SMB, NFS and AFP: the list and the servers'
|
|
* configuration, which running servers read again (root) */
|
|
int mcde_sys_shares_set(char (*paths)[MCDE_SYS_SHARE_LEN], size_t n);
|
|
|
|
/*
|
|
* The network: the computers that announce what they offer (DNS-SD,
|
|
* multicast DNS), and connecting to what they share. A computer's kinds
|
|
* are bits of McdeSysNetKinds.
|
|
*/
|
|
enum {
|
|
MCDE_NET_HAS_SMB = 1 << 0, /* _smb._tcp */
|
|
MCDE_NET_HAS_AFP = 1 << 1, /* _afpovertcp._tcp */
|
|
MCDE_NET_HAS_NFS = 1 << 2, /* _nfs._tcp */
|
|
MCDE_NET_HAS_SSH = 1 << 3, /* _ssh._tcp or _sftp-ssh._tcp */
|
|
MCDE_NET_HAS_ANY = 1 << 4 /* _workstation._tcp, nothing shared */
|
|
};
|
|
|
|
typedef struct {
|
|
char name[128]; /* as it calls itself: "kitt" */
|
|
char host[256]; /* its address: "kitt.local" */
|
|
int kinds; /* MCDE_NET_HAS_* */
|
|
} McdeSysNetHost;
|
|
|
|
/* the computers found within about TIMEOUT_MS; how many */
|
|
int mcde_sys_net_browse(McdeSysNetHost *hosts, size_t max, int timeout_ms);
|
|
/* what HOST shares: NFS exports (showmount) or SMB shares (smbclient -L);
|
|
* KIND is MCDE_NET_HAS_NFS or _SMB; how many */
|
|
int mcde_sys_net_shares(const char *host, int kind, char (*names)[256],
|
|
size_t max);
|
|
/* connect URL -- sftp://[user@]host[/path], nfs://host/path,
|
|
* afp://[user@]host/volume -- to the folder MOUNTPOINT, which must exist;
|
|
* PASSWORD may be NULL */
|
|
int mcde_sys_net_mount(const char *url, const char *mountpoint,
|
|
const char *password);
|
|
/* disconnect what is mounted at MOUNTPOINT */
|
|
int mcde_sys_net_unmount(const char *mountpoint);
|
|
|
|
/*
|
|
* Disks: the drives and their partitions (NetBSD: wedges), where they are
|
|
* mounted, and what can be done to them. NAME is the system's: wd0, dk1,
|
|
* sd0 / nvme0n1, sda1. Changing a disk needs root.
|
|
*/
|
|
typedef struct {
|
|
char name[32];
|
|
char parent[32]; /* the drive of a partition; "" for a drive */
|
|
char model[96]; /* a drive's model, or "" */
|
|
char fstype[32]; /* ffs, msdos, ext2fs, swap, cd9660 ...; "" */
|
|
char label[96]; /* the partition's or volume's name, or "" */
|
|
unsigned long long bytes;
|
|
int removable; /* a stick, a card, a CD */
|
|
int system; /* holds / or swap: never erased */
|
|
char mountpoint[256]; /* where mounted, or "" */
|
|
unsigned long long used, avail; /* when mounted */
|
|
} McdeSysDisk;
|
|
|
|
/* the drives, each followed by its partitions; how many */
|
|
int mcde_sys_disks(McdeSysDisk *d, size_t max);
|
|
/* mount partition NAME on /media/LABEL (made); where, into MOUNTPOINT */
|
|
int mcde_sys_disk_mount(const char *name, char *mountpoint, size_t len);
|
|
int mcde_sys_disk_unmount(const char *name);
|
|
/* check partition NAME without changing it (fsck -n); its report into LOG */
|
|
int mcde_sys_disk_check(const char *name, char *log, size_t len);
|
|
/* erase the whole drive NAME: one partition, FSTYPE ("ffs", "msdos";
|
|
* Linux also "ext4") named LABEL; the new partition's name into PART.
|
|
* Refused for a drive with a system partition or one that is mounted. */
|
|
int mcde_sys_disk_erase(const char *name, const char *fstype,
|
|
const char *label, char *part, size_t len);
|
|
|
|
/*
|
|
* The permissions of the system's files: compared with the list the system
|
|
* keeps of how they should be, and set back to it (REPAIR, root). What
|
|
* differs -- or what was set back -- goes into LOG, a line each; how many
|
|
* lines. NetBSD: mtree(8) against /etc/mtree/set.* (every installed set).
|
|
*/
|
|
int mcde_sys_permissions(int repair, char *log, size_t len);
|
|
|
|
/*
|
|
* Backup: the whole system, every hour, to a disk of its own, by BorgBackup
|
|
* -- encrypted (repokey-blake2), deduplicated; kept are, unless set
|
|
* otherwise, 24 hourly, 30 daily, 52 weekly and every monthly state, and
|
|
* when the disk's free space falls below a share (10 %), the oldest are
|
|
* deleted until it is back. Setting up, running, listing and restoring
|
|
* need root.
|
|
*/
|
|
typedef struct {
|
|
int configured; /* a disk and a repository are set up */
|
|
int enabled; /* it runs every hour */
|
|
char disk[32]; /* the partition it backs up to */
|
|
char repo[512]; /* the repository on it */
|
|
long long last; /* the last backup's time (time_t), or 0 */
|
|
char result[256]; /* how the last one ended */
|
|
/* how many are kept (-1: every one); and the free space below which
|
|
* the oldest backups are deleted, in percent of the disk */
|
|
int keepHourly, keepDaily, keepWeekly, keepMonthly;
|
|
int minFree;
|
|
} McdeSysBackup;
|
|
|
|
typedef struct {
|
|
char name[128];
|
|
char time[32]; /* "2026-11-11T14:00:03" */
|
|
} McdeSysBackupArchive;
|
|
|
|
int mcde_sys_backup_get(McdeSysBackup *b);
|
|
/* make PARTITION (mounted, or mountable) the backup disk: a repository on
|
|
* it, encrypted with PASSPHRASE, which is kept for the hourly runs */
|
|
int mcde_sys_backup_setup(const char *partition, const char *passphrase);
|
|
int mcde_sys_backup_enable(int on);
|
|
/* how many are kept and the free space to hold, from B (root) */
|
|
int mcde_sys_backup_policy(const McdeSysBackup *b);
|
|
/* one backup now (the hourly run does this); borg's report into LOG */
|
|
int mcde_sys_backup_run(char *log, size_t len);
|
|
/* the backups there are, oldest first; how many */
|
|
int mcde_sys_backup_archives(McdeSysBackupArchive *a, size_t max);
|
|
/* PATH (absolute, as it was) of backup ARCHIVE, into the folder DEST */
|
|
int mcde_sys_backup_restore(const char *archive, const char *path,
|
|
const char *dest);
|
|
|
|
/*
|
|
* A process: the command line it was started with, as one line a shell
|
|
* reads back (each argument in single quotes). For the front panel, which
|
|
* keeps a running program to start it again.
|
|
*/
|
|
int mcde_sys_process_command(pid_t pid, char *buf, size_t len);
|
|
|
|
/*
|
|
* The time zone: names as the tz database has them ("Europe/Lisbon").
|
|
* /etc/localtime is a symbolic link into /usr/share/zoneinfo on NetBSD and
|
|
* on Linux alike (sysinst sets it so: src/usr.sbin/sysinst/util.c); the
|
|
* zones offered are the ones of zone1970.tab.
|
|
*/
|
|
#define MCDE_SYS_ZONE_LEN 64
|
|
/* the zone in use, "UTC" when none is set */
|
|
int mcde_sys_timezone_get(char *zone, size_t len);
|
|
/* up to MAX zones, sorted; how many */
|
|
int mcde_sys_timezones(char (*zones)[MCDE_SYS_ZONE_LEN], size_t max);
|
|
/* now and at boot (root); a running program sees it at its next tzset(3) */
|
|
int mcde_sys_timezone_set(const char *zone);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* _Mcde_McdeSys_h */
|