├── .gitignore ├── man ├── meson.build ├── usysconf.1.md ├── usysconf.1 └── usysconf.1.html ├── .gitmodules ├── update_format.sh ├── gen_docs.sh ├── mkrelease.sh ├── src ├── cli │ ├── cli.h │ ├── version.c │ ├── main.c │ └── triggers.c ├── files.h ├── handlers │ ├── mandb.c │ ├── xdg │ │ ├── gtk3_immodules.c │ │ ├── glib2.c │ │ ├── dconf.c │ │ ├── gtk2_immodules.c │ │ ├── desktop-files.c │ │ ├── fonts.c │ │ ├── mime.c │ │ ├── icon-cache.c │ │ └── gconf.c │ ├── systemd │ │ ├── sysusers.c │ │ ├── tmpfiles.c │ │ ├── reload.c │ │ ├── reexec.c │ │ └── vbox-restart.c │ ├── ssl.c │ ├── ldconfig.c │ ├── mono-certs.c │ ├── hwdb.c │ ├── openssh.c │ ├── kernel │ │ ├── apparmor.c │ │ ├── clr-boot-manager.c │ │ └── depmod.c │ ├── qol-assist.c │ ├── linux-driver-management.c │ └── udev-rules.c ├── handlers.h ├── util.h ├── state.h ├── files.c ├── meson.build ├── util.c ├── context.h ├── state.c └── context.c ├── meson_options.txt ├── .clang-format ├── README.md ├── meson.build └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /state 3 | -------------------------------------------------------------------------------- /man/meson.build: -------------------------------------------------------------------------------- 1 | install_data( 2 | 'usysconf.1', 3 | install_dir: join_paths(path_mandir, 'man1') 4 | ) 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "subprojects/libuf"] 2 | path = subprojects/libuf 3 | url = https://github.com/ikeydoherty/libuf.git 4 | -------------------------------------------------------------------------------- /update_format.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | clang-format -i $(find . -name '*.[ch]') 3 | 4 | # Check we have no typos. 5 | which misspell 2>/dev/null >/dev/null 6 | if [[ $? -eq 0 ]]; then 7 | misspell -error `find src -name '*.c' -or -name '*.h'` 8 | fi 9 | -------------------------------------------------------------------------------- /gen_docs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Credit to swupd developers: https://github.com/clearlinux/swupd-client 4 | 5 | MANPAGES="man/usysconf.1" 6 | 7 | for MANPAGE in ${MANPAGES}; do \ 8 | ronn --roff < ${MANPAGE}.md > ${MANPAGE}; \ 9 | ronn --html < ${MANPAGE}.md > ${MANPAGE}.html; \ 10 | done 11 | -------------------------------------------------------------------------------- /mkrelease.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | git submodule init 5 | git submodule update 6 | 7 | VERSION="0.5.2" 8 | NAME="usysconf" 9 | git-archive-all.sh --format tar --prefix ${NAME}-${VERSION}/ --verbose -t HEAD ${NAME}-${VERSION}.tar 10 | xz -9 "${NAME}-${VERSION}.tar" 11 | 12 | gpg --armor --detach-sign "${NAME}-${VERSION}.tar.xz" 13 | gpg --verify "${NAME}-${VERSION}.tar.xz.asc" 14 | -------------------------------------------------------------------------------- /src/cli/cli.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #pragma once 13 | 14 | /** 15 | * Simple helper for defining CLI commands and such 16 | */ 17 | typedef struct UscSubCommand { 18 | const char *name; /** 13 | #include 14 | 15 | #include "cli.h" 16 | #include "config.h" 17 | #include "util.h" 18 | 19 | int usc_cli_version(__usc_unused__ int argc, __usc_unused__ char **argv) 20 | { 21 | fputs(PACKAGE_NAME " version " PACKAGE_VERSION "\n\n", stdout); 22 | fputs("Copyright © 2017-2018 Solus Project\n\n", stdout); 23 | fputs(PACKAGE_NAME 24 | " " 25 | "is free software; you can redistribute it and/or modify\n\ 26 | it under the terms of the GNU General Public License as published by\n\ 27 | the Free Software Foundation; either version 2 of the License, or\n\ 28 | (at your option) any later version.\n", 29 | stdout); 30 | 31 | return EXIT_SUCCESS; 32 | } 33 | 34 | /* 35 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 36 | * 37 | * Local variables: 38 | * c-basic-offset: 8 39 | * tab-width: 8 40 | * indent-tabs-mode: nil 41 | * End: 42 | * 43 | * vi: set shiftwidth=8 tabstop=8 expandtab: 44 | * :indentSize=8:tabSize=8:noTabs=true: 45 | */ 46 | -------------------------------------------------------------------------------- /meson_options.txt: -------------------------------------------------------------------------------- 1 | option('with-static-binary', type: 'boolean', value: 'false', description: 'Build a static binary') 2 | 3 | # Control what modules usysconf knows about 4 | option('with-linux-driver-management', type: 'boolean', value: 'true', description: 'Enable linux-driver-management support') 5 | option('with-clr-boot-manager', type: 'boolean', value: 'true', description: 'Enable clr-boot-manager support') 6 | option('with-qol-assist', type: 'boolean', value: 'true', description: 'Enable qol-assist support') 7 | 8 | option('with-systemd', type: 'boolean', value: 'true', description: 'Enable systemd support') 9 | option('with-systemd-reexec', type: 'boolean', value: 'true', description: 'Enable systemd re-exec support') 10 | 11 | # Used for boot management bits (like depmod) 12 | option('with-kernel-modules-dir', type: 'string', value: '/lib/modules', description: 'Path to the kernel module base tree') 13 | # clr-boot-manager specific currently 14 | option('with-kernel-dir', type: 'string', value: '/usr/lib/kernel', description: 'Path to the vendor kernel tree') 15 | 16 | option('with-log-dir', type: 'string', value: '/var/log', description: 'Logging directory for usysconf') 17 | 18 | # This is useful for virtualbox upgrades 19 | option('with-vbox-restart', type: 'boolean', value: 'false', description: 'Automatically restart vboxdrv service on update') 20 | 21 | option('with-apparmor', type: 'boolean', value: 'true', description: 'Enable AppArmor integration via aa-lsm-hook') 22 | 23 | option('with-mono-certs', type: 'boolean', value: 'true', description: 'Enable Mono certificate population via cert-sync') 24 | -------------------------------------------------------------------------------- /src/files.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #pragma once 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | /** 19 | * Determine if the system is operating within a chroot or not. 20 | * 21 | * @returns True if we've detected a chroot environment 22 | */ 23 | bool usc_is_chrooted(void); 24 | 25 | /** 26 | * Sanity test, determine if the /proc filesystem is mounted and available. 27 | * To be useful and function correctly, usysconf absolutely requires that 28 | * the procfs is mounted. 29 | * 30 | * @returns True if proc was successfully detected as mounted. 31 | */ 32 | bool usc_is_proc_mounted(void); 33 | 34 | /** 35 | * Grab the mtime for a given path 36 | * 37 | * @param path Path that exists.. 38 | * @param time Pointer to store the time in 39 | * 40 | * @return True if we were able to grab the mtime 41 | */ 42 | bool usc_file_mtime(const char *path, time_t *time); 43 | 44 | /** 45 | * Determine if the given path actually exists 46 | * 47 | * @param path Path to test 48 | * @returns True if it exists 49 | */ 50 | bool usc_file_exists(const char *path); 51 | 52 | /** 53 | * Determine if the given path is a directory 54 | * 55 | * @param path The path to test 56 | * @returns True if the given path is a directory 57 | */ 58 | bool usc_file_is_dir(const char *path); 59 | 60 | /* 61 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 62 | * 63 | * Local variables: 64 | * c-basic-offset: 8 65 | * tab-width: 8 66 | * indent-tabs-mode: nil 67 | * End: 68 | * 69 | * vi: set shiftwidth=8 tabstop=8 expandtab: 70 | * :indentSize=8:tabSize=8:noTabs=true: 71 | */ 72 | -------------------------------------------------------------------------------- /src/handlers/mandb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *manpage_paths[] = { 19 | /* Match all manpage pages*/ 20 | "/usr/share/man/man*", 21 | }; 22 | 23 | /** 24 | * Update the man database when manpage directories change 25 | */ 26 | static UscHandlerStatus usc_handler_mandb_exec(UscContext *ctx, const char *path) 27 | { 28 | char *command[] = { 29 | "/usr/bin/mandb", 30 | "-q", /* keep it quiet */ 31 | NULL, /* Terminator */ 32 | }; 33 | 34 | if (!usc_file_is_dir(path)) { 35 | return USC_HANDLER_SKIP; 36 | } 37 | 38 | usc_context_emit_task_start(ctx, "Updating manpages database"); 39 | int ret = usc_exec_command(command); 40 | if (ret != 0) { 41 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 42 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 43 | } 44 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 45 | /* Only want to run once for all of our globs */ 46 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 47 | } 48 | 49 | const UscHandler usc_handler_mandb = { 50 | .name = "mandb", 51 | .description = "Updating manpages database", 52 | .required_bin = "/usr/bin/mandb", 53 | .exec = usc_handler_mandb_exec, 54 | .paths = manpage_paths, 55 | .n_paths = ARRAY_SIZE(manpage_paths), 56 | }; 57 | 58 | /* 59 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 60 | * 61 | * Local variables: 62 | * c-basic-offset: 8 63 | * tab-width: 8 64 | * indent-tabs-mode: nil 65 | * End: 66 | * 67 | * vi: set shiftwidth=8 tabstop=8 expandtab: 68 | * :indentSize=8:tabSize=8:noTabs=true: 69 | */ 70 | -------------------------------------------------------------------------------- /src/handlers/xdg/gtk3_immodules.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *module_paths[] = { 19 | "/usr/lib/gtk-3.0/3.*/immodules", 20 | }; 21 | 22 | /** 23 | * Update immodules cache for GTK3 24 | */ 25 | static UscHandlerStatus usc_handler_gtk3_immodules_exec(UscContext *ctx, const char *path) 26 | { 27 | char *command[] = { 28 | "/usr/bin/gtk-query-immodules-3.0", 29 | "--update-cache", /* Update cache directly */ 30 | NULL, /* Terminator */ 31 | }; 32 | 33 | if (!usc_file_is_dir(path)) { 34 | return USC_HANDLER_SKIP; 35 | } 36 | 37 | usc_context_emit_task_start(ctx, "Updating GTK3 input module cache"); 38 | int ret = usc_exec_command(command); 39 | if (ret != 0) { 40 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 41 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 42 | } 43 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 44 | /* Only want to run once for all of our globs */ 45 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 46 | } 47 | 48 | const UscHandler usc_handler_gtk3_immodules = { 49 | .name = "gtk3-immodules", 50 | .description = "Update GTK3 input module cache", 51 | .required_bin = "/usr/bin/gtk-query-immodules-3.0", 52 | .exec = usc_handler_gtk3_immodules_exec, 53 | .paths = module_paths, 54 | .n_paths = ARRAY_SIZE(module_paths), 55 | }; 56 | 57 | /* 58 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 59 | * 60 | * Local variables: 61 | * c-basic-offset: 8 62 | * tab-width: 8 63 | * indent-tabs-mode: nil 64 | * End: 65 | * 66 | * vi: set shiftwidth=8 tabstop=8 expandtab: 67 | * :indentSize=8:tabSize=8:noTabs=true: 68 | */ 69 | -------------------------------------------------------------------------------- /src/handlers/xdg/glib2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *schema_paths[] = { 19 | "/usr/share/glib-2.0/schemas", 20 | }; 21 | 22 | /** 23 | * Compile glib schemas whenever the directory is updated 24 | */ 25 | static UscHandlerStatus usc_handler_glib2_exec(__usc_unused__ UscContext *ctx, const char *path) 26 | { 27 | autofree(char) *fp = NULL; 28 | char *command[] = { 29 | "/usr/bin/glib-compile-schemas", 30 | NULL, /* /usr/share/glib-2.0/schemas */ 31 | NULL, /* Terminator */ 32 | }; 33 | 34 | if (!usc_file_is_dir(path)) { 35 | return USC_HANDLER_SKIP; 36 | } 37 | 38 | command[1] = (char *)path, 39 | 40 | usc_context_emit_task_start(ctx, "Compiling glib-schemas"); 41 | int ret = usc_exec_command(command); 42 | if (ret != 0) { 43 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 44 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 45 | } 46 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 47 | /* Only want to run once for all of our globs */ 48 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 49 | } 50 | 51 | const UscHandler usc_handler_glib2 = { 52 | .name = "glib2", 53 | .description = "Compile glib-schemas", 54 | .required_bin = "/usr/bin/glib-compile-schemas", 55 | .exec = usc_handler_glib2_exec, 56 | .paths = schema_paths, 57 | .n_paths = ARRAY_SIZE(schema_paths), 58 | }; 59 | 60 | /* 61 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 62 | * 63 | * Local variables: 64 | * c-basic-offset: 8 65 | * tab-width: 8 66 | * indent-tabs-mode: nil 67 | * End: 68 | * 69 | * vi: set shiftwidth=8 tabstop=8 expandtab: 70 | * :indentSize=8:tabSize=8:noTabs=true: 71 | */ 72 | -------------------------------------------------------------------------------- /src/handlers/xdg/dconf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *dconf_paths[] = { "/etc/dconf/db/*", 19 | "/etc/dconf/profile/*", 20 | "/usr/share/dconf/db/*" 21 | "/usr/share/dconf/profile/*" }; 22 | 23 | /** 24 | * Update dconf database 25 | */ 26 | static UscHandlerStatus usc_handler_dconf_exec(UscContext *ctx, const char *path) 27 | { 28 | char *command[] = { 29 | "/usr/bin/dconf", 30 | "update", /* Update cache directly */ 31 | NULL, /* Terminator */ 32 | }; 33 | 34 | if (!usc_file_is_dir(path)) { 35 | return USC_HANDLER_SKIP; 36 | } 37 | 38 | usc_context_emit_task_start(ctx, "Rebuilding dconf database"); 39 | int ret = usc_exec_command(command); 40 | if (ret != 0) { 41 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 42 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 43 | } 44 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 45 | /* Only want to run once for all of our globs */ 46 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 47 | } 48 | 49 | const UscHandler usc_handler_dconf = { 50 | .name = "dconf", 51 | .description = "Update dconf database", 52 | .required_bin = "/usr/bin/dconf", 53 | .exec = usc_handler_dconf_exec, 54 | .paths = dconf_paths, 55 | .n_paths = ARRAY_SIZE(dconf_paths), 56 | }; 57 | 58 | /* 59 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 60 | * 61 | * Local variables: 62 | * c-basic-offset: 8 63 | * tab-width: 8 64 | * indent-tabs-mode: nil 65 | * End: 66 | * 67 | * vi: set shiftwidth=8 tabstop=8 expandtab: 68 | * :indentSize=8:tabSize=8:noTabs=true: 69 | */ 70 | -------------------------------------------------------------------------------- /src/handlers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #pragma once 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | 17 | /* Implemented elsewhere in the codebase */ 18 | extern UscHandler usc_handler_ldconfig; 19 | 20 | #ifdef HAVE_CBM 21 | extern UscHandler usc_handler_cbm; 22 | #endif 23 | 24 | #ifdef HAVE_QOL_ASSIST 25 | extern UscHandler usc_handler_qol_assist; 26 | #endif 27 | 28 | extern UscHandler usc_handler_depmod; 29 | 30 | extern UscHandler usc_handler_hwdb; 31 | 32 | #ifdef HAVE_LDM 33 | extern UscHandler usc_handler_ldm; 34 | #endif 35 | 36 | #ifdef HAVE_SYSTEMD 37 | extern UscHandler usc_handler_sysusers; 38 | extern UscHandler usc_handler_tmpfiles; 39 | extern UscHandler usc_handler_systemd_reload; 40 | #ifdef HAVE_SYSTEMD_REEXEC 41 | extern UscHandler usc_handler_systemd_reexec; 42 | #endif 43 | #ifdef HAVE_VBOX_RESTART 44 | extern UscHandler usc_handler_vbox_restart; 45 | #endif 46 | #endif 47 | 48 | #ifdef HAVE_APPARMOR 49 | extern UscHandler usc_handler_apparmor; 50 | #endif 51 | 52 | extern UscHandler usc_handler_glib2; 53 | extern UscHandler usc_handler_fonts; 54 | extern UscHandler usc_handler_mime; 55 | extern UscHandler usc_handler_icon_cache; 56 | extern UscHandler usc_handler_desktop_files; 57 | extern UscHandler usc_handler_gconf; 58 | extern UscHandler usc_handler_dconf; 59 | 60 | extern UscHandler usc_handler_gtk2_immodules; 61 | extern UscHandler usc_handler_gtk3_immodules; 62 | 63 | extern UscHandler usc_handler_mandb; 64 | extern UscHandler usc_handler_ssl_certs; 65 | 66 | #ifdef HAVE_MONO_CERTS 67 | extern UscHandler usc_handler_mono_certs; 68 | #endif 69 | 70 | extern UscHandler usc_handler_sshd; 71 | 72 | extern UscHandler usc_handler_udev_rules; 73 | 74 | /* 75 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 76 | * 77 | * Local variables: 78 | * c-basic-offset: 8 79 | * tab-width: 8 80 | * indent-tabs-mode: nil 81 | * End: 82 | * 83 | * vi: set shiftwidth=8 tabstop=8 expandtab: 84 | * :indentSize=8:tabSize=8:noTabs=true: 85 | */ 86 | -------------------------------------------------------------------------------- /src/handlers/xdg/gtk2_immodules.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *module_paths[] = { 19 | "/usr/lib/gtk-2.0/2.*/immodules", 20 | }; 21 | 22 | /** 23 | * Update immodules cache for GTK2 24 | */ 25 | static UscHandlerStatus usc_handler_gtk2_immodules_exec(__usc_unused__ UscContext *ctx, 26 | const char *path) 27 | { 28 | char *command[] = { 29 | "/usr/bin/gtk-query-immodules-2.0", 30 | "--update-cache", /* Update cache directly */ 31 | NULL, /* Terminator */ 32 | }; 33 | 34 | if (!usc_file_is_dir(path)) { 35 | return USC_HANDLER_SKIP; 36 | } 37 | 38 | usc_context_emit_task_start(ctx, "Updating GTK2 input module cache"); 39 | int ret = usc_exec_command(command); 40 | if (ret != 0) { 41 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 42 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 43 | } 44 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 45 | /* Only want to run once for all of our globs */ 46 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 47 | } 48 | 49 | const UscHandler usc_handler_gtk2_immodules = { 50 | .name = "gtk2-immodules", 51 | .description = "Update GTK2 input module cache", 52 | .required_bin = "/usr/bin/gtk-query-immodules-3.0", 53 | .exec = usc_handler_gtk2_immodules_exec, 54 | .paths = module_paths, 55 | .n_paths = ARRAY_SIZE(module_paths), 56 | }; 57 | 58 | /* 59 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 60 | * 61 | * Local variables: 62 | * c-basic-offset: 8 63 | * tab-width: 8 64 | * indent-tabs-mode: nil 65 | * End: 66 | * 67 | * vi: set shiftwidth=8 tabstop=8 expandtab: 68 | * :indentSize=8:tabSize=8:noTabs=true: 69 | */ 70 | -------------------------------------------------------------------------------- /src/handlers/systemd/sysusers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *sysuser_paths[] = { 20 | SYSTEMD_SYSUSERS_DIR, 21 | }; 22 | 23 | /** 24 | * Create systemd sysusers 25 | * 26 | * If an update delivers changes to /usr/lib/sysusers.d, tell 27 | * systemd-sysusers to go do something with that. 28 | */ 29 | static UscHandlerStatus usc_handler_sysusers_exec(UscContext *ctx, const char *path) 30 | { 31 | const char *command[] = { 32 | "/usr/bin/systemd-sysusers", 33 | "--root=/", /* Ensure no tom-foolery with dbus */ 34 | NULL, /* Terminator */ 35 | }; 36 | 37 | if (!usc_file_is_dir(path)) { 38 | return USC_HANDLER_SKIP; 39 | } 40 | 41 | usc_context_emit_task_start(ctx, "Updating system users"); 42 | int ret = usc_exec_command((char **)command); 43 | if (ret != 0) { 44 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 45 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 46 | } 47 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 48 | /* Only want to run once for all of our globs */ 49 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 50 | } 51 | 52 | const UscHandler usc_handler_sysusers = { 53 | .name = "sysusers", 54 | .description = "Update systemd sysusers", 55 | .required_bin = "/usr/bin/systemd-sysusers", 56 | .exec = usc_handler_sysusers_exec, 57 | .paths = sysuser_paths, 58 | .n_paths = ARRAY_SIZE(sysuser_paths), 59 | }; 60 | 61 | /* 62 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 63 | * 64 | * Local variables: 65 | * c-basic-offset: 8 66 | * tab-width: 8 67 | * indent-tabs-mode: nil 68 | * End: 69 | * 70 | * vi: set shiftwidth=8 tabstop=8 expandtab: 71 | * :indentSize=8:tabSize=8:noTabs=true: 72 | */ 73 | -------------------------------------------------------------------------------- /src/handlers/ssl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *cert_paths[] = { 19 | "/etc/ssl/certs", 20 | }; 21 | 22 | /** 23 | * Update the SSL certificates. Currently Solus uses the old cranky scheme 24 | * of c_rehash invocation on /etc/ssl. We know this is super janky and we 25 | * do intend to port both Solus and usysconf to support the `trust` p11-kit 26 | * mechanism in line with other distros. 27 | * 28 | * Basically, stay tuned, this part will suck less soon ™ 29 | */ 30 | static UscHandlerStatus usc_handler_ssl_certs_exec(__usc_unused__ UscContext *ctx, const char *path) 31 | { 32 | char *command[] = { 33 | "/usr/bin/c_rehash", NULL, /* Terminator */ 34 | }; 35 | 36 | if (!usc_file_is_dir(path)) { 37 | return USC_HANDLER_SKIP; 38 | } 39 | 40 | usc_context_emit_task_start(ctx, "Updating SSL certificates"); 41 | int ret = usc_exec_command(command); 42 | if (ret != 0) { 43 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 44 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 45 | } 46 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 47 | /* Only want to run once for all of our globs */ 48 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 49 | } 50 | 51 | const UscHandler usc_handler_ssl_certs = { 52 | .name = "ssl-certs", 53 | .description = "Update SSL certificate configuration", 54 | .required_bin = "/usr/bin/c_rehash", 55 | .exec = usc_handler_ssl_certs_exec, 56 | .paths = cert_paths, 57 | .n_paths = ARRAY_SIZE(cert_paths), 58 | }; 59 | 60 | /* 61 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 62 | * 63 | * Local variables: 64 | * c-basic-offset: 8 65 | * tab-width: 8 66 | * indent-tabs-mode: nil 67 | * End: 68 | * 69 | * vi: set shiftwidth=8 tabstop=8 expandtab: 70 | * :indentSize=8:tabSize=8:noTabs=true: 71 | */ 72 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #pragma once 13 | 14 | #define _GNU_SOURCE 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | /** 21 | * Fork and execute a command, waiting for it to complete. 22 | * 23 | * @returns Status code of the command, if it executed. 24 | */ 25 | int usc_exec_command(char **command); 26 | 27 | /** 28 | * Grab the (0-based) indexed path component from the string 29 | * and return a copy of it 30 | * 31 | * @note Will return NULL if the component is out of range 32 | * 33 | * @returns Duplicated string with the indexed path component 34 | */ 35 | char *usc_get_strn_component(const char *inp_path, ssize_t whence); 36 | 37 | /** 38 | * Taken out of libnica and various other Solus projects 39 | */ 40 | #define DEF_AUTOFREE(N, C) \ 41 | static inline void _autofree_func_##N(void *p) \ 42 | { \ 43 | if (p && *(N **)p) { \ 44 | C(*(N **)p); \ 45 | (*(void **)p) = NULL; \ 46 | } \ 47 | } 48 | 49 | #define autofree(N) __attribute__((cleanup(_autofree_func_##N))) N 50 | 51 | #define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0]) 52 | 53 | #define __usc_unused__ __attribute__((unused)) 54 | 55 | DEF_AUTOFREE(char, free) 56 | DEF_AUTOFREE(DIR, closedir) 57 | 58 | /* 59 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 60 | * 61 | * Local variables: 62 | * c-basic-offset: 8 63 | * tab-width: 8 64 | * indent-tabs-mode: nil 65 | * End: 66 | * 67 | * vi: set shiftwidth=8 tabstop=8 expandtab: 68 | * :indentSize=8:tabSize=8:noTabs=true: 69 | */ 70 | -------------------------------------------------------------------------------- /src/handlers/systemd/tmpfiles.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *sysuser_paths[] = { 20 | SYSTEMD_TMPFILES_DIR, 21 | }; 22 | 23 | /** 24 | * Create systemd tmpfiles 25 | * 26 | * If an update delivers changes to /usr/lib/tmpfiles.d, tell 27 | * systemd-tmpfiles to go do something with that. 28 | */ 29 | static UscHandlerStatus usc_handler_tmpfiles_exec(UscContext *ctx, const char *path) 30 | { 31 | const char *command[] = { 32 | "/usr/bin/systemd-tmpfiles", 33 | "--root=/", /* Ensure no tom-foolery with dbus */ 34 | "--create", /* Create tmpfiles */ 35 | NULL, /* Terminator */ 36 | }; 37 | 38 | if (!usc_file_is_dir(path)) { 39 | return USC_HANDLER_SKIP; 40 | } 41 | 42 | usc_context_emit_task_start(ctx, "Updating systemd tmpfiles"); 43 | int ret = usc_exec_command((char **)command); 44 | if (ret != 0) { 45 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 46 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 47 | } 48 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 49 | /* Only want to run once for all of our globs */ 50 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 51 | } 52 | 53 | const UscHandler usc_handler_tmpfiles = { 54 | .name = "tmpfiles", 55 | .description = "Update systemd tmpfiles", 56 | .required_bin = "/usr/bin/systemd-tmpfiles", 57 | .exec = usc_handler_tmpfiles_exec, 58 | .paths = sysuser_paths, 59 | .n_paths = ARRAY_SIZE(sysuser_paths), 60 | }; 61 | 62 | /* 63 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 64 | * 65 | * Local variables: 66 | * c-basic-offset: 8 67 | * tab-width: 8 68 | * indent-tabs-mode: nil 69 | * End: 70 | * 71 | * vi: set shiftwidth=8 tabstop=8 expandtab: 72 | * :indentSize=8:tabSize=8:noTabs=true: 73 | */ 74 | -------------------------------------------------------------------------------- /src/handlers/systemd/reload.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *unit_paths[] = { 20 | SYSTEMD_UNIT_DIR, 21 | }; 22 | 23 | /** 24 | * Reload systemd if the units change on disk 25 | * 26 | */ 27 | static UscHandlerStatus usc_handler_systemd_reload_exec(UscContext *ctx, const char *path) 28 | { 29 | const char *command[] = { 30 | "/usr/bin/systemctl", "daemon-reload", NULL, /* Terminator */ 31 | }; 32 | 33 | if (!usc_file_is_dir(path)) { 34 | return USC_HANDLER_SKIP; 35 | } 36 | 37 | usc_context_emit_task_start(ctx, "Reloading systemd configuration"); 38 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED)) { 39 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 40 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 41 | } 42 | 43 | int ret = usc_exec_command((char **)command); 44 | if (ret != 0) { 45 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 46 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 47 | } 48 | 49 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 50 | 51 | /* Only want to run once for all of our globs */ 52 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 53 | } 54 | 55 | const UscHandler usc_handler_systemd_reload = { 56 | .name = "systemd-reload", 57 | .description = "Reload systemd configuration", 58 | .required_bin = "/usr/bin/systemctl", 59 | .exec = usc_handler_systemd_reload_exec, 60 | .paths = unit_paths, 61 | .n_paths = ARRAY_SIZE(unit_paths), 62 | }; 63 | 64 | /* 65 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 66 | * 67 | * Local variables: 68 | * c-basic-offset: 8 69 | * tab-width: 8 70 | * indent-tabs-mode: nil 71 | * End: 72 | * 73 | * vi: set shiftwidth=8 tabstop=8 expandtab: 74 | * :indentSize=8:tabSize=8:noTabs=true: 75 | */ 76 | -------------------------------------------------------------------------------- /src/handlers/ldconfig.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *library_paths[] = { 19 | /* Match all library paths directories and run ldconfig for them */ 20 | "/usr/lib64", 21 | "/usr/lib32", 22 | "/usr/lib", 23 | "/lib32", 24 | "/lib64", 25 | "/lib", 26 | "/usr/share/ld.so.conf.d", 27 | }; 28 | 29 | /** 30 | * Update the linker cache (`/sbin/ldconfig`) when library directories update 31 | */ 32 | static UscHandlerStatus usc_handler_ldconfig_exec(UscContext *ctx, const char *path) 33 | { 34 | char *command[] = { 35 | "/sbin/ldconfig", 36 | "-X", /* don't update symlinks */ 37 | NULL, /* Terminator */ 38 | }; 39 | 40 | if (!usc_file_is_dir(path)) { 41 | return USC_HANDLER_SKIP; 42 | } 43 | 44 | usc_context_emit_task_start(ctx, "Updating dynamic library cache"); 45 | int ret = usc_exec_command(command); 46 | if (ret != 0) { 47 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 48 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 49 | } 50 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 51 | /* Only want to run once for all of our globs */ 52 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 53 | } 54 | 55 | const UscHandler usc_handler_ldconfig = { 56 | .name = "ldconfig", 57 | .description = "Update dynamic library cache", 58 | .required_bin = "/sbin/ldconfig", 59 | .exec = usc_handler_ldconfig_exec, 60 | .paths = library_paths, 61 | .n_paths = ARRAY_SIZE(library_paths), 62 | }; 63 | 64 | /* 65 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 66 | * 67 | * Local variables: 68 | * c-basic-offset: 8 69 | * tab-width: 8 70 | * indent-tabs-mode: nil 71 | * End: 72 | * 73 | * vi: set shiftwidth=8 tabstop=8 expandtab: 74 | * :indentSize=8:tabSize=8:noTabs=true: 75 | */ 76 | -------------------------------------------------------------------------------- /src/handlers/xdg/desktop-files.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *app_paths[] = { 19 | "/usr/share/applications", 20 | }; 21 | 22 | /** 23 | * Update the desktop database 24 | * 25 | * This simply runs when /usr/share/applications is modified, building the 26 | * new "mimeinfo.cache" file. Note this is anti-statelesss. 27 | */ 28 | static UscHandlerStatus usc_handler_desktop_files_exec(UscContext *ctx, const char *path) 29 | { 30 | char *command[] = { 31 | "/usr/bin/update-desktop-database", 32 | "-q", /* Quiet */ 33 | NULL, /* /usr/share/applications */ 34 | NULL, /* Terminator */ 35 | }; 36 | 37 | if (!usc_file_is_dir(path)) { 38 | return USC_HANDLER_SKIP; 39 | } 40 | 41 | command[2] = (char *)path; 42 | 43 | usc_context_emit_task_start(ctx, "Updating desktop database"); 44 | int ret = usc_exec_command((char **)command); 45 | if (ret != 0) { 46 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 47 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 48 | } 49 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 50 | /* Only want to run once for all of our globs */ 51 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 52 | } 53 | 54 | const UscHandler usc_handler_desktop_files = { 55 | .name = "desktop-files", 56 | .description = "Update desktop database", 57 | .required_bin = "/usr/bin/update-desktop-database", 58 | .exec = usc_handler_desktop_files_exec, 59 | .paths = app_paths, 60 | .n_paths = ARRAY_SIZE(app_paths), 61 | }; 62 | 63 | /* 64 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 65 | * 66 | * Local variables: 67 | * c-basic-offset: 8 68 | * tab-width: 8 69 | * indent-tabs-mode: nil 70 | * End: 71 | * 72 | * vi: set shiftwidth=8 tabstop=8 expandtab: 73 | * :indentSize=8:tabSize=8:noTabs=true: 74 | */ 75 | -------------------------------------------------------------------------------- /src/handlers/mono-certs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *mono_certs_paths[] = { 20 | "/etc/ssl/certs/ca-certificates.crt", 21 | }; 22 | 23 | /** 24 | * Populate the Mono certificates systemwide so Mono applications are ready to 25 | * be used out of the box. 26 | */ 27 | static UscHandlerStatus usc_handler_mono_certs_exec(UscContext *ctx, 28 | __usc_unused__ const char *path) 29 | { 30 | char *command[] = { 31 | "/usr/bin/cert-sync", 32 | "--quiet", /* keep it quiet */ 33 | "/etc/ssl/certs/ca-certificates.crt", 34 | NULL, /* Terminator */ 35 | }; 36 | 37 | usc_context_emit_task_start(ctx, "Populating Mono certificates"); 38 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED)) { 39 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 40 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 41 | } 42 | 43 | int ret = usc_exec_command(command); 44 | if (ret != 0) { 45 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 46 | return USC_HANDLER_FAIL; 47 | } 48 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 49 | 50 | /* Only run once */ 51 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 52 | } 53 | 54 | const UscHandler usc_handler_mono_certs = { 55 | .name = "mono-certs", 56 | .description = "Populate Mono certificates", 57 | .required_bin = "/usr/bin/cert-sync", 58 | .exec = usc_handler_mono_certs_exec, 59 | .paths = mono_certs_paths, 60 | .n_paths = ARRAY_SIZE(mono_certs_paths), 61 | }; 62 | 63 | /* 64 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 65 | * 66 | * Local variables: 67 | * c-basic-offset: 8 68 | * tab-width: 8 69 | * indent-tabs-mode: nil 70 | * End: 71 | * 72 | * vi: set shiftwidth=8 tabstop=8 expandtab: 73 | * :indentSize=8:tabSize=8:noTabs=true: 74 | */ 75 | -------------------------------------------------------------------------------- /src/handlers/xdg/fonts.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *font_paths[] = { 19 | "/usr/share/fonts/*/*", 20 | "/usr/share/fonts/*", 21 | }; 22 | 23 | /** 24 | * Update the font cache on disk. 25 | * 26 | * TODO: Track cache invalidations whereby an existing glob that we once 27 | * tracked has disappeared so we can rebuild the whole cache, then we'll 28 | * be able to do per directory fc-cache updates to make updates quicker. 29 | */ 30 | static UscHandlerStatus usc_handler_fonts_exec(UscContext *ctx, const char *path) 31 | { 32 | /* TODO: Maybe try again with '-r' if this fails */ 33 | const char *command[] = { 34 | "/usr/bin/fc-cache", 35 | "-f", /* Force */ 36 | "-s", /* System only */ 37 | NULL, /* Terminator */ 38 | }; 39 | 40 | if (!usc_file_is_dir(path)) { 41 | return USC_HANDLER_SKIP; 42 | } 43 | 44 | usc_context_emit_task_start(ctx, "Rebuilding font cache"); 45 | int ret = usc_exec_command((char **)command); 46 | if (ret != 0) { 47 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 48 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 49 | } 50 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 51 | /* Only want to run once for all of our globs */ 52 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 53 | } 54 | 55 | const UscHandler usc_handler_fonts = { 56 | .name = "fonts", 57 | .description = "Rebuild font cache", 58 | .required_bin = "/usr/bin/fc-cache", 59 | .exec = usc_handler_fonts_exec, 60 | .paths = font_paths, 61 | .n_paths = ARRAY_SIZE(font_paths), 62 | }; 63 | 64 | /* 65 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 66 | * 67 | * Local variables: 68 | * c-basic-offset: 8 69 | * tab-width: 8 70 | * indent-tabs-mode: nil 71 | * End: 72 | * 73 | * vi: set shiftwidth=8 tabstop=8 expandtab: 74 | * :indentSize=8:tabSize=8:noTabs=true: 75 | */ 76 | -------------------------------------------------------------------------------- /src/handlers/systemd/reexec.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *unit_paths[] = { 20 | SYSTEMD_UTIL_DIR "/systemd" /* /usr/lib/systemd/systemd */ 21 | }; 22 | 23 | /** 24 | * Ask systemd to reexec when the binary has been updated 25 | * 26 | */ 27 | static UscHandlerStatus usc_handler_systemd_reexec_exec(UscContext *ctx, const char *path) 28 | { 29 | const char *command[] = { 30 | "/usr/bin/systemctl", "daemon-reexec", NULL, /* Terminator */ 31 | }; 32 | 33 | if (access(path, X_OK) != 0) { 34 | return USC_HANDLER_SKIP; 35 | } 36 | 37 | usc_context_emit_task_start(ctx, "Re-executing systemd"); 38 | 39 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED) || 40 | usc_context_has_flag(ctx, USC_FLAGS_LIVE_MEDIUM)) { 41 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 42 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 43 | } 44 | 45 | int ret = usc_exec_command((char **)command); 46 | if (ret != 0) { 47 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 48 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 49 | } 50 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 51 | /* Only want to run once for all of our globs */ 52 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 53 | } 54 | 55 | const UscHandler usc_handler_systemd_reexec = { 56 | .name = "systemd-reexec", 57 | .description = "Re-execute systemd", 58 | .required_bin = "/usr/bin/systemctl", 59 | .exec = usc_handler_systemd_reexec_exec, 60 | .paths = unit_paths, 61 | .n_paths = ARRAY_SIZE(unit_paths), 62 | }; 63 | 64 | /* 65 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 66 | * 67 | * Local variables: 68 | * c-basic-offset: 8 69 | * tab-width: 8 70 | * indent-tabs-mode: nil 71 | * End: 72 | * 73 | * vi: set shiftwidth=8 tabstop=8 expandtab: 74 | * :indentSize=8:tabSize=8:noTabs=true: 75 | */ 76 | -------------------------------------------------------------------------------- /src/handlers/systemd/vbox-restart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *unit_paths[] = { 20 | SYSTEMD_UNIT_DIR "/vboxdrv.service", /* Main systemd unit */ 21 | "/usr/bin/VirtualBox", /* VBox.sh */ 22 | }; 23 | 24 | /** 25 | * Reload systemd if the units change on disk 26 | * 27 | */ 28 | static UscHandlerStatus usc_handler_vbox_restart_exec(UscContext *ctx, 29 | __usc_unused__ const char *path) 30 | { 31 | const char *command[] = { 32 | "/usr/bin/systemctl", "restart", "vboxdrv.service", NULL, /* Terminator */ 33 | }; 34 | 35 | usc_context_emit_task_start(ctx, "Restarting VirtualBox services"); 36 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED) || 37 | usc_context_has_flag(ctx, USC_FLAGS_LIVE_MEDIUM)) { 38 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 39 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 40 | } 41 | 42 | int ret = usc_exec_command((char **)command); 43 | if (ret != 0) { 44 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 45 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 46 | } 47 | 48 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 49 | 50 | /* Only want to run once for all of our globs */ 51 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 52 | } 53 | 54 | const UscHandler usc_handler_vbox_restart = { 55 | .name = "vbox-restart", 56 | .description = "Restart VirtualBox services", 57 | .required_bin = "/usr/bin/VBox.sh", 58 | .exec = usc_handler_vbox_restart_exec, 59 | .paths = unit_paths, 60 | .n_paths = ARRAY_SIZE(unit_paths), 61 | }; 62 | 63 | /* 64 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 65 | * 66 | * Local variables: 67 | * c-basic-offset: 8 68 | * tab-width: 8 69 | * indent-tabs-mode: nil 70 | * End: 71 | * 72 | * vi: set shiftwidth=8 tabstop=8 expandtab: 73 | * :indentSize=8:tabSize=8:noTabs=true: 74 | */ 75 | -------------------------------------------------------------------------------- /src/state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #pragma once 13 | 14 | #include "util.h" 15 | 16 | /** 17 | * A UscStateTracker is an opaque type that is used to track the state 18 | * of various registered trigger directories on the system 19 | */ 20 | typedef struct UscStateTracker UscStateTracker; 21 | 22 | /** 23 | * Create a new UscStateTracker for the given context 24 | * 25 | * @returns A newly allocated UscStateTracker 26 | */ 27 | UscStateTracker *usc_state_tracker_new(void); 28 | 29 | /** 30 | * Free a previously allocated UscStateTracker 31 | */ 32 | void usc_state_tracker_free(UscStateTracker *tracker); 33 | 34 | /** 35 | * Attempt to push the path into the tracker with the current mtime 36 | * 37 | * Any existing entries will be overwritten by matching their collapsed 38 | * path first. As such, all entries are required to actually exist on disk. 39 | */ 40 | bool usc_state_tracker_push_path(UscStateTracker *tracker, const char *path); 41 | 42 | /** 43 | * Check our internal state db to decide if this path is due an update 44 | * 45 | * @param tracker Pointer to an allocated UscStateTracker 46 | * @param path Path to query (will be resolved) 47 | * @param force If the path exists but has no mtime update, force it to update 48 | * 49 | * @returns True if the path needs updating 50 | */ 51 | bool usc_state_tracker_needs_update(UscStateTracker *tracker, const char *path, bool force); 52 | 53 | /** 54 | * Attempt to load the existing state from disk 55 | * 56 | * @param tracker Pointer to an existing tracker 57 | * 58 | * @returns true if the load succeeded (or file was absent). 59 | */ 60 | bool usc_state_tracker_load(UscStateTracker *tracker); 61 | 62 | /** 63 | * Request that the state tracker actually dumps itself to disk so that it 64 | * can be reloaded at a later point 65 | */ 66 | bool usc_state_tracker_write(UscStateTracker *tracker); 67 | 68 | DEF_AUTOFREE(UscStateTracker, usc_state_tracker_free) 69 | 70 | /* 71 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 72 | * 73 | * Local variables: 74 | * c-basic-offset: 8 75 | * tab-width: 8 76 | * indent-tabs-mode: nil 77 | * End: 78 | * 79 | * vi: set shiftwidth=8 tabstop=8 expandtab: 80 | * :indentSize=8:tabSize=8:noTabs=true: 81 | */ 82 | -------------------------------------------------------------------------------- /src/handlers/hwdb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *hwdb_paths[] = { 20 | "/usr/lib/udev/hwdb.d", 21 | "/etc/udev/hwdb.d", 22 | }; 23 | 24 | /** 25 | * Update the hwdb caches 26 | * 27 | * This generates the .bin cache file from the hwdb.d directories 28 | * and effectively compiles to a single cache 29 | */ 30 | static UscHandlerStatus usc_handler_hwdb_exec(UscContext *ctx, const char *path) 31 | { 32 | const char *command[] = { 33 | #ifdef HAVE_SYSTEMD 34 | "/usr/bin/systemd-hwdb", 35 | "update", /* Update hwdb cache */ 36 | #else 37 | "/usr/bin/udevadm", 38 | "hwdb", 39 | "--update", /* Legacy invocation */ 40 | #endif 41 | NULL, /* Terminator */ 42 | }; 43 | 44 | if (!usc_file_is_dir(path)) { 45 | return USC_HANDLER_SKIP; 46 | } 47 | 48 | usc_context_emit_task_start(ctx, "Updating hwdb"); 49 | int ret = usc_exec_command((char **)command); 50 | if (ret != 0) { 51 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 52 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 53 | } 54 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 55 | /* Only want to run once for all of our globs */ 56 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 57 | } 58 | 59 | const UscHandler usc_handler_hwdb = { 60 | .name = "hwdb", 61 | .description = "Update hardware database", 62 | #ifdef HAVE_SYSTEMD 63 | .required_bin = "/usr/bin/systemd-hwdb", 64 | #else 65 | .required_bin = "/usr/bin/udevadm", 66 | #endif 67 | .exec = usc_handler_hwdb_exec, 68 | .paths = hwdb_paths, 69 | .n_paths = ARRAY_SIZE(hwdb_paths), 70 | }; 71 | 72 | /* 73 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 74 | * 75 | * Local variables: 76 | * c-basic-offset: 8 77 | * tab-width: 8 78 | * indent-tabs-mode: nil 79 | * End: 80 | * 81 | * vi: set shiftwidth=8 tabstop=8 expandtab: 82 | * :indentSize=8:tabSize=8:noTabs=true: 83 | */ 84 | -------------------------------------------------------------------------------- /src/handlers/xdg/mime.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *mime_paths[] = { 19 | "/usr/share/mime/*", 20 | }; 21 | 22 | /** 23 | * Update the mime database. 24 | * 25 | * Due to the way in which we have to match anything under the mime tree, 26 | * i.e. because other things own the directories, we may end up with a 27 | * case of trying to re-update the mime due to our own timestamps being 28 | * slightly off. 29 | * 30 | * This is OK however as we pass -n which will bypass unnecessary updates 31 | * to the cache. 32 | */ 33 | static UscHandlerStatus usc_handler_mime_exec(UscContext *ctx, const char *path) 34 | { 35 | char *command[] = { 36 | "/usr/bin/update-mime-database", 37 | "-n", 38 | NULL, /* /usr/share/mime */ 39 | NULL, /* Terminator */ 40 | }; 41 | 42 | if (!usc_file_is_dir(path)) { 43 | return USC_HANDLER_SKIP; 44 | } 45 | 46 | /* Locked for now due to only using our own root */ 47 | command[2] = "/usr/share/mime"; 48 | 49 | usc_context_emit_task_start(ctx, "Updating mimetype database"); 50 | int ret = usc_exec_command((char **)command); 51 | if (ret != 0) { 52 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 53 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 54 | } 55 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 56 | /* Only want to run once for all of our globs */ 57 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 58 | } 59 | 60 | const UscHandler usc_handler_mime = { 61 | .name = "mime", 62 | .description = "Update mimetype database", 63 | .required_bin = "/usr/bin/update-mime-database", 64 | .exec = usc_handler_mime_exec, 65 | .paths = mime_paths, 66 | .n_paths = ARRAY_SIZE(mime_paths), 67 | }; 68 | 69 | /* 70 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 71 | * 72 | * Local variables: 73 | * c-basic-offset: 8 74 | * tab-width: 8 75 | * indent-tabs-mode: nil 76 | * End: 77 | * 78 | * vi: set shiftwidth=8 tabstop=8 expandtab: 79 | * :indentSize=8:tabSize=8:noTabs=true: 80 | */ 81 | -------------------------------------------------------------------------------- /src/cli/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "cli.h" 19 | #include "util.h" 20 | 21 | static UscSubCommand subcommands[] = { 22 | { "run", "run triggers", usc_cli_run_triggers }, 23 | { "version", "print the program version", usc_cli_version }, 24 | }; 25 | 26 | static size_t n_subcommands = ARRAY_SIZE(subcommands); 27 | 28 | static const char *prog_name = NULL; 29 | 30 | static void print_usage(void) 31 | { 32 | fprintf(stderr, "Usage: %s [command]\n\n", prog_name); 33 | for (size_t i = 0; i < n_subcommands; i++) { 34 | UscSubCommand *command = &subcommands[i]; 35 | fprintf(stderr, "%*s - %s\n", 20, command->name, command->summary); 36 | } 37 | } 38 | 39 | int main(int argc, char **argv) 40 | { 41 | UscSubCommand *command = NULL; 42 | const char *command_arg = NULL; 43 | prog_name = argv[0]; 44 | ++argv; 45 | --argc; 46 | 47 | if (argc < 1) { 48 | print_usage(); 49 | return EXIT_SUCCESS; 50 | } 51 | 52 | command_arg = argv[0]; 53 | ++argv; 54 | --argc; 55 | 56 | /* Find the root subcommand from the passed argument */ 57 | for (size_t i = 0; i < n_subcommands; i++) { 58 | UscSubCommand *c = &subcommands[i]; 59 | if (c->name && strcmp(c->name, command_arg) == 0) { 60 | command = c; 61 | break; 62 | } 63 | } 64 | 65 | if (!command) { 66 | fprintf(stderr, "Unknown command '%s'\n", command_arg); 67 | print_usage(); 68 | return EXIT_FAILURE; 69 | } 70 | 71 | if (!command->exec) { 72 | fprintf(stderr, "Not yet implemented: '%s'\n", command_arg); 73 | return EXIT_FAILURE; 74 | } 75 | 76 | return command->exec(argc, argv); 77 | } 78 | 79 | /* 80 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 81 | * 82 | * Local variables: 83 | * c-basic-offset: 8 84 | * tab-width: 8 85 | * indent-tabs-mode: nil 86 | * End: 87 | * 88 | * vi: set shiftwidth=8 tabstop=8 expandtab: 89 | * :indentSize=8:tabSize=8:noTabs=true: 90 | */ 91 | -------------------------------------------------------------------------------- /src/cli/triggers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "cli.h" 18 | #include "context.h" 19 | 20 | int usc_cli_run_triggers(int argc, char **argv) 21 | { 22 | autofree(UscContext) *context = NULL; 23 | const char *trigger = NULL; 24 | bool force_run = false; 25 | bool list = false; 26 | 27 | for (int i = 0; i < argc; i++) { 28 | if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--force") == 0) { 29 | force_run = true; 30 | break; 31 | } 32 | if (strcmp(argv[i], "list") == 0) { 33 | list = true; 34 | break; 35 | } 36 | } 37 | 38 | /* Don't require euid 0 to list triggers. */ 39 | if (list) { 40 | usc_context_list_triggers(); 41 | return EXIT_SUCCESS; 42 | } 43 | 44 | if (geteuid() != 0) { 45 | fprintf(stderr, "You must be root to run triggers\n"); 46 | return EXIT_FAILURE; 47 | } 48 | 49 | context = usc_context_new(); 50 | if (!context) { 51 | fputs("Cannot create context!\n", stderr); 52 | return EXIT_FAILURE; 53 | } 54 | 55 | /* No args, run all triggers */ 56 | if (argc == 0 || (argc == 1 && force_run)) { 57 | return usc_context_run_triggers(context, trigger, force_run) ? EXIT_SUCCESS 58 | : EXIT_FAILURE; 59 | } 60 | 61 | /* Run all specified triggers by name */ 62 | for (int i = 0; i < argc; i++) { 63 | if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--force") == 0) { 64 | continue; 65 | } 66 | if (!usc_context_run_triggers(context, argv[i], force_run)) { 67 | return EXIT_FAILURE; 68 | } 69 | } 70 | 71 | return EXIT_SUCCESS; 72 | } 73 | 74 | /* 75 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 76 | * 77 | * Local variables: 78 | * c-basic-offset: 8 79 | * tab-width: 8 80 | * indent-tabs-mode: nil 81 | * End: 82 | * 83 | * vi: set shiftwidth=8 tabstop=8 expandtab: 84 | * :indentSize=8:tabSize=8:noTabs=true: 85 | */ 86 | -------------------------------------------------------------------------------- /src/handlers/openssh.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | static const char *sshd_paths[] = { 19 | "/usr/sbin/sshd", 20 | "/sbin/sshd", 21 | }; 22 | 23 | #define RSA_HOST_KEY "/etc/ssh/ssh_host_rsa_key" 24 | #define DSA_HOST_KEY "/etc/ssh/ssh_host_dsa_key" 25 | 26 | /** 27 | * Handle correct bootstrap of sshd 28 | */ 29 | static UscHandlerStatus usc_handler_sshd_exec(UscContext *ctx, const char *path) 30 | { 31 | const char *command[] = { 32 | "/usr/bin/ssh-keygen", 33 | "-q", 34 | "-t", 35 | "rsa", 36 | "-f", 37 | RSA_HOST_KEY, /* Build the RSA host key. */ 38 | "-C", 39 | "", 40 | "-N", 41 | "", 42 | NULL, /* Terminator */ 43 | }; 44 | 45 | /* We expect to be looking at the executable sshd here. */ 46 | if (access(path, X_OK) != 0) { 47 | return USC_HANDLER_SKIP; 48 | } 49 | 50 | /* Host key is configured, let's bail. */ 51 | if (usc_file_exists(RSA_HOST_KEY) || usc_file_exists(DSA_HOST_KEY)) { 52 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 53 | } 54 | 55 | usc_context_emit_task_start(ctx, "Creating OpenSSH host key"); 56 | int ret = usc_exec_command((char **)command); 57 | if (ret != 0) { 58 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 59 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 60 | } 61 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 62 | /* Only want to run once for all of our globs */ 63 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 64 | } 65 | 66 | const UscHandler usc_handler_sshd = { 67 | .name = "openssh", 68 | .description = "Create OpenSSH host key", 69 | .required_bin = "/usr/bin/ssh-keygen", 70 | .exec = usc_handler_sshd_exec, 71 | .paths = sshd_paths, 72 | .n_paths = ARRAY_SIZE(sshd_paths), 73 | }; 74 | 75 | /* 76 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 77 | * 78 | * Local variables: 79 | * c-basic-offset: 8 80 | * tab-width: 8 81 | * indent-tabs-mode: nil 82 | * End: 83 | * 84 | * vi: set shiftwidth=8 tabstop=8 expandtab: 85 | * :indentSize=8:tabSize=8:noTabs=true: 86 | */ 87 | -------------------------------------------------------------------------------- /src/handlers/kernel/apparmor.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *apparmor_paths[] = { 20 | "/etc/apparmor.d", 21 | }; 22 | 23 | /** 24 | * Update the apparmor cache within the kernel directory to ensure new apparmors 25 | * are readily available. 26 | */ 27 | static UscHandlerStatus usc_handler_apparmor_exec(UscContext *ctx, __usc_unused__ const char *path) 28 | { 29 | char *compile_command[] = { 30 | "/usr/sbin/aa-lsm-hook-compile", NULL, /* Terminator */ 31 | }; 32 | char *load_command[] = { 33 | "/usr/sbin/aa-lsm-hook-load", NULL, /* Terminator */ 34 | }; 35 | 36 | usc_context_emit_task_start(ctx, "Compiling AppArmor profiles"); 37 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED)) { 38 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 39 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 40 | } 41 | 42 | /* Compile */ 43 | int ret = usc_exec_command(compile_command); 44 | if (ret != 0) { 45 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 46 | return USC_HANDLER_FAIL; 47 | } 48 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 49 | 50 | /* Load */ 51 | usc_context_emit_task_start(ctx, "Reloading AppArmor profiles"); 52 | ret = usc_exec_command(load_command); 53 | if (ret != 0) { 54 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 55 | return USC_HANDLER_FAIL; 56 | } 57 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 58 | 59 | /* Only run once */ 60 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 61 | } 62 | 63 | const UscHandler usc_handler_apparmor = { 64 | .name = "apparmor", 65 | .description = "Compile AppArmor profiles", 66 | .required_bin = "/usr/sbin/aa-lsm-hook-compile", 67 | .exec = usc_handler_apparmor_exec, 68 | .paths = apparmor_paths, 69 | .n_paths = ARRAY_SIZE(apparmor_paths), 70 | }; 71 | 72 | /* 73 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 74 | * 75 | * Local variables: 76 | * c-basic-offset: 8 77 | * tab-width: 8 78 | * indent-tabs-mode: nil 79 | * End: 80 | * 81 | * vi: set shiftwidth=8 tabstop=8 expandtab: 82 | * :indentSize=8:tabSize=8:noTabs=true: 83 | */ 84 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | AccessModifierOffset: 0 3 | AlignAfterOpenBracket: true 4 | AlignConsecutiveAssignments: false 5 | #uncomment for clang 3.9 6 | #AlignConsecutiveDeclarations: false 7 | AlignEscapedNewlinesLeft: false 8 | AlignOperands: true 9 | AlignTrailingComments: true 10 | AllowAllParametersOfDeclarationOnNextLine: true 11 | AllowShortBlocksOnASingleLine: false 12 | AllowShortCaseLabelsOnASingleLine: false 13 | AllowShortFunctionsOnASingleLine: None 14 | AllowShortIfStatementsOnASingleLine: false 15 | AllowShortLoopsOnASingleLine: false 16 | # AlwaysBreakAfterDefinitionReturnType: None 17 | #uncomment for clang 3.9 18 | #AlwaysBreakAfterReturnType: None 19 | AlwaysBreakBeforeMultilineStrings: true 20 | AlwaysBreakTemplateDeclarations: false 21 | BinPackArguments: false 22 | BinPackParameters: true 23 | # BraceWrapping: (not set since BreakBeforeBraces is not Custom) 24 | BreakBeforeBinaryOperators: None 25 | # BreakAfterJavaFieldAnnotations: (not java) 26 | BreakBeforeBinaryOperators: None 27 | BreakBeforeBraces: Linux 28 | BreakBeforeTernaryOperators: true 29 | BreakConstructorInitializersBeforeComma: false 30 | #uncomment for clang 3.9 31 | #BreakStringLiterals: false 32 | ColumnLimit: 100 33 | CommentPragmas: '\*\<' 34 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 35 | ConstructorInitializerIndentWidth: 4 36 | ContinuationIndentWidth: 4 37 | Cpp11BracedListStyle: false 38 | DerivePointerAlignment: false 39 | DisableFormat: false 40 | ExperimentalAutoDetectBinPacking: false 41 | ForEachMacros: [ ] 42 | #Uncomment for clang 3.9 43 | #IncludeCategories: 44 | # - Regex: '^"' 45 | # Priority: 1 46 | # IncludeIsMainRegex: (project doesn't use a main includes that can add other includes via regex) 47 | IndentCaseLabels: false 48 | IndentWidth: 8 49 | IndentWrappedFunctionNames: false 50 | # JavaScriptQuotes: (not javascript) 51 | KeepEmptyLinesAtTheStartOfBlocks: false 52 | Language: Cpp 53 | MacroBlockBegin: '' 54 | MacroBlockEnd: '' 55 | MaxEmptyLinesToKeep: 1 56 | NamespaceIndentation: None 57 | # ObjCBlockIndentWidth: (not objc) 58 | # ObjCSpaceAfterProperty: (not objc) 59 | # ObjCSpaceBeforeProtocolList: (not objc) 60 | PenaltyBreakBeforeFirstCallParameter: 400 61 | PenaltyBreakComment: 0 62 | # PenaltyBreakFirstLessLess: (not cpp) 63 | PenaltyBreakString: 500 64 | PenaltyExcessCharacter: 10000 65 | PenaltyReturnTypeOnItsOwnLine: 600 66 | PointerAlignment: Right 67 | #uncomment for clang 3.9 68 | #ReflowComments: true 69 | #uncomment for clang 3.9 70 | #SortIncludes: true 71 | SpaceAfterCStyleCast: false 72 | SpaceBeforeAssignmentOperators: true 73 | SpaceBeforeParens: ControlStatements 74 | SpaceInEmptyParentheses: false 75 | SpacesBeforeTrailingComments: 1 76 | SpacesInAngles: false 77 | SpacesInCStyleCastParentheses: false 78 | # SpacesInContainerLiterals: (not objc or javascript) 79 | SpacesInParentheses: false 80 | SpacesInSquareBrackets: false 81 | Standard: Cpp11 82 | TabWidth: 8 83 | UseTab: Never 84 | ... 85 | -------------------------------------------------------------------------------- /src/handlers/kernel/clr-boot-manager.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | /** 20 | * Paths that trigger a clr-boot-manager update 21 | */ 22 | static const char *boot_paths[] = { 23 | KERNEL_DIR, 24 | "/usr/lib/goofiboot", 25 | "/usr/lib/systemd/boot/efi", 26 | }; 27 | 28 | /** 29 | * Trigger `clr-boot-manager update` when one of the boot-relevant components 30 | * has been altered within the upstream delivery mechanism. 31 | */ 32 | static UscHandlerStatus usc_handler_cbm_exec(UscContext *ctx, const char *path) 33 | { 34 | char *command[] = { 35 | "/usr/bin/clr-boot-manager", "update", NULL, /* Terminator */ 36 | }; 37 | 38 | /* TODO: Support --path updates */ 39 | if (!usc_file_is_dir(path) && access(path, X_OK) != 0) { 40 | return USC_HANDLER_SKIP; 41 | } 42 | 43 | /* Only support this in non chroot environments. Users will need to 44 | * perform CBM recovery in chroots using CBM directly to ensure that 45 | * CBM is never accidentally invoked 46 | */ 47 | usc_context_emit_task_start(ctx, "Updating clr-boot-manager"); 48 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED) || 49 | usc_context_has_flag(ctx, USC_FLAGS_LIVE_MEDIUM)) { 50 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 51 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 52 | } 53 | 54 | int ret = usc_exec_command(command); 55 | if (ret != 0) { 56 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 57 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 58 | } 59 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 60 | /* Only want to run once for all of our globs */ 61 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 62 | } 63 | 64 | const UscHandler usc_handler_cbm = { 65 | .name = "boot", 66 | .description = "Update boot configuration + kernels", 67 | .required_bin = "/usr/bin/clr-boot-manager", 68 | .exec = usc_handler_cbm_exec, 69 | .paths = boot_paths, 70 | .n_paths = ARRAY_SIZE(boot_paths), 71 | }; 72 | 73 | /* 74 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 75 | * 76 | * Local variables: 77 | * c-basic-offset: 8 78 | * tab-width: 8 79 | * indent-tabs-mode: nil 80 | * End: 81 | * 82 | * vi: set shiftwidth=8 tabstop=8 expandtab: 83 | * :indentSize=8:tabSize=8:noTabs=true: 84 | */ 85 | -------------------------------------------------------------------------------- /src/handlers/qol-assist.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | /** 19 | * Paths that trigger a qol-assist update 20 | */ 21 | static const char *qol_paths[] = { 22 | "/usr/sbin/qol-assist", 23 | "/usr/lib/systemd/system/qol-assist-migration.service", 24 | }; 25 | 26 | /** 27 | * Trigger 'qol-assist' to run on the next boot. 28 | * 29 | * qol-assist will be systemd started when the trigger file exists. Running 30 | * the trigger subcommand will enforce creation of the trigger to ensure 31 | * qol-assist runs. 32 | * 33 | * This will follow a versioned migration, and will happily ignore any 34 | * migrations that already happened. 35 | */ 36 | static UscHandlerStatus usc_handler_qol_assist_exec(UscContext *ctx, const char *path) 37 | { 38 | char *command[] = { 39 | "/usr/sbin/qol-assist", 40 | "trigger", /* Trigger an update on boot */ 41 | NULL, /* Terminator */ 42 | }; 43 | 44 | if (access(path, X_OK) != 0) { 45 | return USC_HANDLER_SKIP; 46 | } 47 | 48 | /* QoL migrations only make sense for real live systems */ 49 | usc_context_emit_task_start(ctx, "Registering QoL migration on next boot"); 50 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED) || 51 | usc_context_has_flag(ctx, USC_FLAGS_LIVE_MEDIUM)) { 52 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 53 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 54 | } 55 | 56 | int ret = usc_exec_command(command); 57 | if (ret != 0) { 58 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 59 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 60 | } 61 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 62 | /* Only want to run once for all of our globs */ 63 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 64 | } 65 | 66 | const UscHandler usc_handler_qol_assist = { 67 | .name = "qol-assist", 68 | .description = "Register QoL migration", 69 | .required_bin = "/usr/sbin/qol-assist", 70 | .exec = usc_handler_qol_assist_exec, 71 | .paths = qol_paths, 72 | .n_paths = ARRAY_SIZE(qol_paths), 73 | }; 74 | 75 | /* 76 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 77 | * 78 | * Local variables: 79 | * c-basic-offset: 8 80 | * tab-width: 8 81 | * indent-tabs-mode: nil 82 | * End: 83 | * 84 | * vi: set shiftwidth=8 tabstop=8 expandtab: 85 | * :indentSize=8:tabSize=8:noTabs=true: 86 | */ 87 | -------------------------------------------------------------------------------- /src/files.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "files.h" 21 | #include "util.h" 22 | 23 | bool usc_is_chrooted() 24 | { 25 | struct stat root_stat = { 0 }; 26 | struct stat proc_stat = { 0 }; 27 | 28 | if (stat("/", &root_stat) != 0) { 29 | fprintf(stderr, "Unable to probe '/', assuming chroot\n"); 30 | return true; 31 | } 32 | if (stat("/proc/1/root/.", &proc_stat) != 0) { 33 | fprintf(stderr, "Unable to probe '/proc/1/root/.', assuming chroot\n"); 34 | return true; 35 | } 36 | 37 | if (root_stat.st_dev != proc_stat.st_dev) { 38 | return true; 39 | } 40 | if (root_stat.st_ino != proc_stat.st_ino) { 41 | return true; 42 | } 43 | return false; 44 | } 45 | 46 | bool usc_is_proc_mounted() 47 | { 48 | struct mntent *ent = NULL; 49 | struct mntent mnt = { 0 }; 50 | FILE *mnts = NULL; 51 | char buf[PATH_MAX]; 52 | bool ret = false; 53 | 54 | mnts = setmntent("/proc/self/mounts", "r"); 55 | if (!mnts) { 56 | return false; 57 | } 58 | 59 | while ((ent = getmntent_r(mnts, &mnt, buf, sizeof(buf)))) { 60 | if (mnt.mnt_dir && strcmp("/proc", mnt.mnt_dir) == 0) { 61 | ret = true; 62 | break; 63 | } 64 | } 65 | 66 | endmntent(mnts); 67 | return ret; 68 | } 69 | 70 | bool usc_file_mtime(const char *path, time_t *time) 71 | { 72 | struct stat st = { 0 }; 73 | if (stat(path, &st) != 0) { 74 | return false; 75 | } 76 | *time = st.st_mtime; 77 | return true; 78 | } 79 | 80 | bool usc_file_exists(const char *path) 81 | { 82 | __usc_unused__ struct stat st = { 0 }; 83 | if (lstat(path, &st) != 0) { 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | bool usc_file_is_dir(const char *path) 90 | { 91 | struct stat st = { 0 }; 92 | if (stat(path, &st) != 0) { 93 | return false; 94 | } 95 | return S_ISDIR(st.st_mode); 96 | } 97 | 98 | /* 99 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 100 | * 101 | * Local variables: 102 | * c-basic-offset: 8 103 | * tab-width: 8 104 | * indent-tabs-mode: nil 105 | * End: 106 | * 107 | * vi: set shiftwidth=8 tabstop=8 expandtab: 108 | * :indentSize=8:tabSize=8:noTabs=true: 109 | */ 110 | -------------------------------------------------------------------------------- /src/meson.build: -------------------------------------------------------------------------------- 1 | xdg_handlers = [ 2 | 'dconf', 3 | 'gconf', 4 | 'glib2', 5 | 'mime', 6 | 'icon-cache', 7 | 'fonts', 8 | 'desktop-files', 9 | 'gtk2_immodules', 10 | 'gtk3_immodules', 11 | ] 12 | 13 | handlers = [ 14 | 'ldconfig', 15 | 'hwdb', 16 | 'mandb', 17 | 'ssl', 18 | 'openssh', 19 | 'udev-rules', 20 | ] 21 | 22 | kernel_handlers = [ 23 | 'depmod', 24 | ] 25 | 26 | if with_cbm == true 27 | kernel_handlers += 'clr-boot-manager' 28 | endif 29 | 30 | if with_qol == true 31 | handlers += 'qol-assist' 32 | endif 33 | 34 | if with_ldm == true 35 | handlers += 'linux-driver-management' 36 | endif 37 | 38 | if with_apparmor == true 39 | kernel_handlers += 'apparmor' 40 | endif 41 | 42 | if with_mono_certs == true 43 | handlers += 'mono-certs' 44 | endif 45 | 46 | # We'll still enable hwdb but alter for udev path if not using systemd 47 | if with_systemd == true 48 | handlers += [ 49 | 'systemd/tmpfiles', 50 | 'systemd/sysusers', 51 | 'systemd/reload', 52 | ] 53 | if with_systemd_reexec == true 54 | handlers += 'systemd/reexec' 55 | endif 56 | if with_vbox_restart == true 57 | handlers += 'systemd/vbox-restart' 58 | endif 59 | endif 60 | 61 | libusysconf_sources = [ 62 | 'context.c', 63 | 'files.c', 64 | 'state.c', 65 | 'util.c', 66 | ] 67 | 68 | # Push all known handlers into the build 69 | foreach handler : handlers 70 | libusysconf_sources += 'handlers/@0@.c'.format(handler) 71 | endforeach 72 | 73 | # Stick all the xdg guys in 74 | foreach xdg_handler : xdg_handlers 75 | libusysconf_sources += 'handlers/xdg/@0@.c'.format(xdg_handler) 76 | endforeach 77 | 78 | # Stick all the kernel guys in 79 | foreach kernel_handler : kernel_handlers 80 | libusysconf_sources += 'handlers/kernel/@0@.c'.format(kernel_handler) 81 | endforeach 82 | 83 | usysconf_link_args = [] 84 | 85 | if with_static_binary == true 86 | usysconf_link_args += '-static' 87 | endif 88 | 89 | usysconf_sources = [ 90 | 'cli/main.c', 91 | 'cli/triggers.c', 92 | 'cli/version.c', 93 | ] 94 | 95 | libusysconf_includes = [ 96 | config_h_dir, 97 | include_directories('.'), 98 | ] 99 | 100 | libusysconf = static_library( 101 | 'usysconf', 102 | sources: libusysconf_sources, 103 | install: false, 104 | c_args: [ 105 | '-fvisibility=default', 106 | ], 107 | dependencies: [ 108 | libuf.get_variable('link_libuf'), 109 | ], 110 | include_directories: libusysconf_includes, 111 | ) 112 | 113 | link_libusysconf = declare_dependency( 114 | link_with: libusysconf, 115 | include_directories: libusysconf_includes, 116 | ) 117 | 118 | 119 | # Build main qol-assist binary 120 | usysconf = executable( 121 | 'usysconf', 122 | sources: usysconf_sources, 123 | install: true, 124 | install_dir: path_sbindir, 125 | link_args: usysconf_link_args, 126 | c_args: [ 127 | '-fvisibility=default', 128 | ], 129 | dependencies: [ 130 | link_libusysconf, 131 | ], 132 | ) 133 | -------------------------------------------------------------------------------- /src/handlers/linux-driver-management.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "context.h" 15 | #include "files.h" 16 | #include "util.h" 17 | 18 | /** 19 | * Paths that trigger an LDM update 20 | */ 21 | static const char *driver_paths[] = { 22 | "/usr/lib/glx-provider", "/usr/lib/glx-provider/*", "/usr/lib/nvidia", 23 | "/usr/lib32/glx-provider", "/usr/lib32/glx-provider/*", "/usr/lib32/nvidia", 24 | "/usr/lib/nvidia/modules", "/usr/lib/xorg/modules/drivers", "/usr/share/glvnd/egl_vendor.d", 25 | }; 26 | 27 | /** 28 | * Trigger `linux-driver-management configure gpu` when one of the driver relevant 29 | * components has been altered within the upstream delivery mechanism. 30 | * 31 | * This allows automatic configuration of newly installed drivers through the 32 | * linux-driver-management utility. 33 | * 34 | * In the absence of usable hardware configuration data and driver packages, 35 | * LDM will always fall back to configuring Mesa as the default provider. 36 | * This ensures that even if /dev & /sys are mounted into a chroot, LDM can 37 | * still safely be executed as long as the NVIDIA drivers aren't also installed 38 | * into the chroot (which you should never do from a distribution perspective) 39 | */ 40 | static UscHandlerStatus usc_handler_ldm_exec(UscContext *ctx, const char *path) 41 | { 42 | char *command[] = { 43 | "/usr/bin/linux-driver-management", 44 | "configure", /* Want to configure the drivers */ 45 | "gpu", /* Specifically the GPU drivers */ 46 | NULL, /* Terminator */ 47 | }; 48 | 49 | if (!usc_file_is_dir(path)) { 50 | return USC_HANDLER_SKIP; 51 | } 52 | 53 | usc_context_emit_task_start(ctx, "Updating graphical driver configuration"); 54 | int ret = usc_exec_command(command); 55 | if (ret != 0) { 56 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 57 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 58 | } 59 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 60 | /* Only want to run once for all of our globs */ 61 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 62 | } 63 | 64 | const UscHandler usc_handler_ldm = { 65 | .name = "drivers", 66 | .description = "Update graphical driver configuration", 67 | .required_bin = "/usr/bin/linux-driver-management", 68 | .exec = usc_handler_ldm_exec, 69 | .paths = driver_paths, 70 | .n_paths = ARRAY_SIZE(driver_paths), 71 | }; 72 | 73 | /* 74 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 75 | * 76 | * Local variables: 77 | * c-basic-offset: 8 78 | * tab-width: 8 79 | * indent-tabs-mode: nil 80 | * End: 81 | * 82 | * vi: set shiftwidth=8 tabstop=8 expandtab: 83 | * :indentSize=8:tabSize=8:noTabs=true: 84 | */ 85 | -------------------------------------------------------------------------------- /man/usysconf.1.md: -------------------------------------------------------------------------------- 1 | usysconf(1) -- Universal system configuration 2 | ============================================= 3 | 4 | 5 | ## SYNOPSIS 6 | 7 | `usysconf run [triggers]` 8 | 9 | 10 | ## DESCRIPTION 11 | 12 | `usysconf` is a system configuration agent designed for integration within the 13 | software update process. The main job for `usysconf` is to run OS configuration 14 | tasks within a well defined centralised location. 15 | 16 | `usysconf` will run a variety of triggers to ensure system consistency and 17 | health, detecting changes within the filesystem and taking any appropriate 18 | actions to configure components such as kernels or the dynamic linker cache. 19 | 20 | ## OPTIONS 21 | 22 | The following options are applicable to `usysconf(1)`. 23 | 24 | 25 | * `-f`, `--force` 26 | 27 | Force a trigger to run, even if the local file doesn't seem to have been 28 | updated. 29 | 30 | ## SUBCOMMANDS 31 | 32 | `run [triggers]` 33 | 34 | When called without any arguments, all system triggers will be executed. 35 | Alternatively, you may provide a list of triggers to run directly. 36 | 37 | The special value "list" will cause all known triggers to be listed 38 | instead of executing them. 39 | 40 | `help` 41 | 42 | Print the supported command set for the `usysconf(1)` binary. 43 | 44 | `version` 45 | 46 | Print the version and license information, before quitting. 47 | 48 | ## FILES 49 | 50 | `usysconf` tracks state through some special files, and will recover in their 51 | absence. 52 | 53 | `/var/log/usysconf.log` 54 | 55 | This file will contain the output from the last run of usysconf, if the 56 | tool was run automatically without a working `stdout`. This is typical 57 | for situations where `usysconf` is masked behind a long running daemon. 58 | 59 | `/var/log/usysconf.rewind.log` 60 | 61 | This is a temporary file used by `usysconf` to store the stdout/stderr 62 | of the currently executing command. Under normal situations, this file 63 | will be replayed into the main log file if there is an error, and this 64 | rewind log will be deleted. 65 | 66 | `/var/lib/usysconf/status` 67 | 68 | This file is used to track the current state of `usysconf` handlers. 69 | Upon completion, `usysconf` will write the state file with the mtime 70 | for each known asset for the triggers. This file is read by `usysconf` 71 | on startup to allow unmodified assets to be skipped, making `usysconf` 72 | incremental in nature. 73 | 74 | If this file is removed, `usysconf` will run all triggers again and 75 | write the state file once more - this is non fatal. In fact, this 76 | mechanism can be used to reset `usysconf` state to ensure that all 77 | triggers do run again. 78 | 79 | 80 | ## EXIT STATUS 81 | 82 | On success, 0 is returned. A non-zero return code signals a failure. 83 | 84 | 85 | ## COPYRIGHT 86 | 87 | * Copyright © 2017-2018 Ikey Doherty, License: CC-BY-SA-3.0 88 | 89 | 90 | ## SEE ALSO 91 | 92 | `clr-boot-manager(1)`, `qol-assist(1)` 93 | 94 | * https://github.com/solus-project/usysconf 95 | 96 | ## NOTES 97 | 98 | Creative Commons Attribution-ShareAlike 3.0 Unported 99 | 100 | * http://creativecommons.org/licenses/by-sa/3.0/ 101 | -------------------------------------------------------------------------------- /src/handlers/kernel/depmod.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | 16 | #include "config.h" 17 | #include "context.h" 18 | #include "files.h" 19 | #include "util.h" 20 | 21 | static const char *module_paths[] = { 22 | /* Glob all module directories and track individually */ 23 | KERNEL_MODULES_DIR "/*", 24 | KERNEL_MODULES_DIR "/*/*", /* ./extra/ */ 25 | KERNEL_MODULES_DIR "/*/kernel/drivers/*", /* i.e. nvidia in video dir */ 26 | }; 27 | 28 | /** 29 | * Update the module cache within the kernel directory to ensure new modules 30 | * are readily available. 31 | */ 32 | static UscHandlerStatus usc_handler_depmod_exec(UscContext *ctx, const char *path) 33 | { 34 | autofree(char) *kern_dir = NULL; 35 | autofree(char) *kernel_nom = NULL; 36 | char *command[] = { 37 | "/sbin/depmod", 38 | "-a", /* probe all fellers */ 39 | NULL, /* The path we're depmodding */ 40 | NULL, /* Terminator */ 41 | }; 42 | 43 | /* Grab the kernel name for this set */ 44 | kernel_nom = usc_get_strn_component(path, 2); 45 | if (!kernel_nom) { 46 | return USC_HANDLER_FAIL; 47 | } 48 | 49 | if (asprintf(&kern_dir, "/lib/modules/%s/kernel", kernel_nom) < 0) { 50 | fputs("OOM\n", stderr); 51 | return USC_HANDLER_FAIL; 52 | } 53 | 54 | if (!usc_file_is_dir(kern_dir)) { 55 | return USC_HANDLER_SKIP; 56 | } 57 | 58 | /* We'll only record our skip for valid paths */ 59 | if (usc_context_should_skip(ctx, kern_dir)) { 60 | return USC_HANDLER_SUCCESS; 61 | } 62 | 63 | /* Assign path argument. */ 64 | command[2] = (char *)kernel_nom; 65 | 66 | if (!usc_context_push_skip(ctx, kern_dir)) { 67 | return USC_HANDLER_FAIL; 68 | } 69 | 70 | usc_context_emit_task_start(ctx, "Running depmod on kernel %s", kernel_nom); 71 | int ret = usc_exec_command(command); 72 | if (ret != 0) { 73 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 74 | return USC_HANDLER_FAIL; 75 | } 76 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 77 | 78 | /* Need to run for all of our globs */ 79 | return USC_HANDLER_SUCCESS; 80 | } 81 | 82 | const UscHandler usc_handler_depmod = { 83 | .name = "depmod", 84 | .description = "Run depmod for each kernel", 85 | .required_bin = "/sbin/depmod", 86 | .exec = usc_handler_depmod_exec, 87 | .paths = module_paths, 88 | .n_paths = ARRAY_SIZE(module_paths), 89 | }; 90 | 91 | /* 92 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 93 | * 94 | * Local variables: 95 | * c-basic-offset: 8 96 | * tab-width: 8 97 | * indent-tabs-mode: nil 98 | * End: 99 | * 100 | * vi: set shiftwidth=8 tabstop=8 expandtab: 101 | * :indentSize=8:tabSize=8:noTabs=true: 102 | */ 103 | -------------------------------------------------------------------------------- /src/handlers/udev-rules.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include "config.h" 15 | #include "context.h" 16 | #include "files.h" 17 | #include "util.h" 18 | 19 | static const char *udev_rules_paths[] = { 20 | "/usr/lib/udev/rules.d", 21 | "/lib/udev/rules.d", 22 | "/etc/udev/rules.d", 23 | }; 24 | 25 | /** 26 | * Reload the udev rules and then trigger rebuild of udev devices 27 | * 28 | * Some of the udev rules being loaded may be composite in nature with 29 | * include directories, so we cannot rely on inotify alone. Instead we'll 30 | * manually apply the rules when one of the source directories change, 31 | * and then ask udev to rebuild and apply the rules to existing udev devices 32 | * so that updates can automatically "fix" udev rule issues. 33 | */ 34 | static UscHandlerStatus usc_handler_udev_rules_exec(UscContext *ctx, const char *path) 35 | { 36 | const char *command_reload[] = { 37 | "/usr/bin/udevadm", 38 | "control", 39 | "--reload-rules", /* Reload rules */ 40 | NULL, /* Terminator */ 41 | }; 42 | const char *command_trigger[] = { 43 | "/usr/bin/udevadm", 44 | "trigger", /* Rebuild rules and apply them */ 45 | NULL, /* Terminator */ 46 | }; 47 | 48 | if (!usc_file_is_dir(path)) { 49 | return USC_HANDLER_SKIP; 50 | } 51 | 52 | /* First up, reload the rules */ 53 | usc_context_emit_task_start(ctx, "Reloading udev rules"); 54 | if (usc_context_has_flag(ctx, USC_FLAGS_CHROOTED)) { 55 | usc_context_emit_task_finish(ctx, USC_HANDLER_SKIP); 56 | return USC_HANDLER_SKIP | USC_HANDLER_BREAK; 57 | } 58 | 59 | int ret = usc_exec_command((char **)command_reload); 60 | if (ret != 0) { 61 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 62 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 63 | } 64 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 65 | 66 | /* Now trigger the rules */ 67 | usc_context_emit_task_start(ctx, "Applying udev rules"); 68 | ret = usc_exec_command((char **)command_trigger); 69 | if (ret != 0) { 70 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 71 | return USC_HANDLER_FAIL | USC_HANDLER_BREAK; 72 | } 73 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 74 | 75 | /* Only want to run once for all of our globs */ 76 | return USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 77 | } 78 | 79 | const UscHandler usc_handler_udev_rules = { 80 | .name = "udev-rules", 81 | .description = "Reload udev rules", 82 | .required_bin = "/usr/bin/udevadm", 83 | .exec = usc_handler_udev_rules_exec, 84 | .paths = udev_rules_paths, 85 | .n_paths = ARRAY_SIZE(udev_rules_paths), 86 | }; 87 | 88 | /* 89 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 90 | * 91 | * Local variables: 92 | * c-basic-offset: 8 93 | * tab-width: 8 94 | * indent-tabs-mode: nil 95 | * End: 96 | * 97 | * vi: set shiftwidth=8 tabstop=8 expandtab: 98 | * :indentSize=8:tabSize=8:noTabs=true: 99 | */ 100 | -------------------------------------------------------------------------------- /man/usysconf.1: -------------------------------------------------------------------------------- 1 | .\" generated with Ronn/v0.7.3 2 | .\" http://github.com/rtomayko/ronn/tree/0.7.3 3 | . 4 | .TH "USYSCONF" "1" "May 2018" "" "" 5 | . 6 | .SH "NAME" 7 | \fBusysconf\fR \- Universal system configuration 8 | . 9 | .SH "SYNOPSIS" 10 | \fBusysconf run [triggers]\fR 11 | . 12 | .SH "DESCRIPTION" 13 | \fBusysconf\fR is a system configuration agent designed for integration within the software update process\. The main job for \fBusysconf\fR is to run OS configuration tasks within a well defined centralised location\. 14 | . 15 | .P 16 | \fBusysconf\fR will run a variety of triggers to ensure system consistency and health, detecting changes within the filesystem and taking any appropriate actions to configure components such as kernels or the dynamic linker cache\. 17 | . 18 | .SH "OPTIONS" 19 | The following options are applicable to \fBusysconf(1)\fR\. 20 | . 21 | .IP "\(bu" 4 22 | \fB\-f\fR, \fB\-\-force\fR 23 | . 24 | .IP 25 | Force a trigger to run, even if the local file doesn\'t seem to have been updated\. 26 | . 27 | .IP "" 0 28 | . 29 | .SH "SUBCOMMANDS" 30 | \fBrun [triggers]\fR 31 | . 32 | .IP "" 4 33 | . 34 | .nf 35 | 36 | When called without any arguments, all system triggers will be executed\. 37 | Alternatively, you may provide a list of triggers to run directly\. 38 | 39 | The special value "list" will cause all known triggers to be listed 40 | instead of executing them\. 41 | . 42 | .fi 43 | . 44 | .IP "" 0 45 | . 46 | .P 47 | \fBhelp\fR 48 | . 49 | .IP "" 4 50 | . 51 | .nf 52 | 53 | Print the supported command set for the `usysconf(1)` binary\. 54 | . 55 | .fi 56 | . 57 | .IP "" 0 58 | . 59 | .P 60 | \fBversion\fR 61 | . 62 | .IP "" 4 63 | . 64 | .nf 65 | 66 | Print the version and license information, before quitting\. 67 | . 68 | .fi 69 | . 70 | .IP "" 0 71 | . 72 | .SH "FILES" 73 | \fBusysconf\fR tracks state through some special files, and will recover in their absence\. 74 | . 75 | .P 76 | \fB/var/log/usysconf\.log\fR 77 | . 78 | .IP "" 4 79 | . 80 | .nf 81 | 82 | This file will contain the output from the last run of usysconf, if the 83 | tool was run automatically without a working `stdout`\. This is typical 84 | for situations where `usysconf` is masked behind a long running daemon\. 85 | . 86 | .fi 87 | . 88 | .IP "" 0 89 | . 90 | .P 91 | \fB/var/log/usysconf\.rewind\.log\fR 92 | . 93 | .IP "" 4 94 | . 95 | .nf 96 | 97 | This is a temporary file used by `usysconf` to store the stdout/stderr 98 | of the currently executing command\. Under normal situations, this file 99 | will be replayed into the main log file if there is an error, and this 100 | rewind log will be deleted\. 101 | . 102 | .fi 103 | . 104 | .IP "" 0 105 | . 106 | .P 107 | \fB/var/lib/usysconf/status\fR 108 | . 109 | .IP "" 4 110 | . 111 | .nf 112 | 113 | This file is used to track the current state of `usysconf` handlers\. 114 | Upon completion, `usysconf` will write the state file with the mtime 115 | for each known asset for the triggers\. This file is read by `usysconf` 116 | on startup to allow unmodified assets to be skipped, making `usysconf` 117 | incremental in nature\. 118 | 119 | If this file is removed, `usysconf` will run all triggers again and 120 | write the state file once more \- this is non fatal\. In fact, this 121 | mechanism can be used to reset `usysconf` state to ensure that all 122 | triggers do run again\. 123 | . 124 | .fi 125 | . 126 | .IP "" 0 127 | . 128 | .SH "EXIT STATUS" 129 | On success, 0 is returned\. A non\-zero return code signals a failure\. 130 | . 131 | .SH "COPYRIGHT" 132 | . 133 | .IP "\(bu" 4 134 | Copyright © 2017\-2018 Ikey Doherty, License: CC\-BY\-SA\-3\.0 135 | . 136 | .IP "" 0 137 | . 138 | .SH "SEE ALSO" 139 | \fBclr\-boot\-manager(1)\fR, \fBqol\-assist(1)\fR 140 | . 141 | .IP "\(bu" 4 142 | https://github\.com/solus\-project/usysconf 143 | . 144 | .IP "" 0 145 | . 146 | .SH "NOTES" 147 | Creative Commons Attribution\-ShareAlike 3\.0 Unported 148 | . 149 | .IP "\(bu" 4 150 | http://creativecommons\.org/licenses/by\-sa/3\.0/ 151 | . 152 | .IP "" 0 153 | 154 | -------------------------------------------------------------------------------- /src/util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "config.h" 26 | #include "util.h" 27 | 28 | /** 29 | * Ensure that /dev/null is actually a character device and exists, 30 | * so that we don't try to redirect stdout to the disk. 31 | static bool dev_null_not_cranky(void) 32 | { 33 | struct stat st = { 0 }; 34 | if (stat("/dev/null", &st) != 0) { 35 | return false; 36 | } 37 | if (!S_ISCHR(st.st_mode)) { 38 | return false; 39 | } 40 | return true; 41 | }*/ 42 | 43 | /** 44 | * Redirect the file descriptor to the named file 45 | */ 46 | static void redirect_fileno_named(int file_no, const char *file, int mode) 47 | { 48 | int fd = -1; 49 | fd = open(file, mode, 00644); 50 | dup2(fd, file_no); 51 | close(fd); 52 | } 53 | 54 | /** 55 | * Redirect the file descriptor to /dev/null 56 | static void redirect_fileno_devnull(int file_no) 57 | { 58 | redirect_fileno_named(file_no, "/dev/null", O_WRONLY); 59 | }*/ 60 | 61 | int usc_exec_command(char **command) 62 | { 63 | pid_t p; 64 | int status = 0; 65 | int ret = 0; 66 | int r = -1; 67 | 68 | if ((p = fork()) < 0) { 69 | fprintf(stderr, "Failed to fork(): %s\n", strerror(errno)); 70 | return -1; 71 | } else if (p == 0) { 72 | /* Force all output to a temporary log file */ 73 | redirect_fileno_named(STDOUT_FILENO, 74 | USYSCONF_REWIND_LOG_FILE, 75 | O_WRONLY | O_CREAT | O_APPEND); 76 | redirect_fileno_named(STDERR_FILENO, 77 | USYSCONF_REWIND_LOG_FILE, 78 | O_WRONLY | O_CREAT | O_APPEND); 79 | 80 | /* Execute the command */ 81 | if ((r = execv(command[0], command)) != 0) { 82 | fprintf(stderr, "Failed to execve(%s): %s\n", command[0], strerror(errno)); 83 | exit(EXIT_FAILURE); 84 | } 85 | _exit(EXIT_SUCCESS); 86 | /* We'd never actually get here. */ 87 | } else { 88 | if (waitpid(p, &status, 0) < 0) { 89 | fprintf(stderr, "Failed to waitpid(%d): %s\n", (int)p, strerror(errno)); 90 | return -1; 91 | } 92 | /* i.e. sigsev */ 93 | if (!WIFEXITED(status)) { 94 | fprintf(stderr, "Child process '%s' exited abnormally\n", command[0]); 95 | } 96 | } 97 | 98 | /* At this point just make sure the return code was 0 */ 99 | ret = WEXITSTATUS(status); 100 | return ret; 101 | } 102 | 103 | char *usc_get_strn_component(const char *inp_path, ssize_t whence) 104 | { 105 | char *c = NULL; 106 | char *snd = NULL; 107 | ssize_t i; 108 | 109 | c = (char *)inp_path; 110 | 111 | for (i = 0; i <= whence; i++) { 112 | c = strchr(c, '/'); 113 | if (!c) { 114 | return NULL; 115 | } 116 | c++; 117 | } 118 | 119 | if (i != whence + 1) { 120 | return NULL; 121 | } 122 | 123 | snd = strchr(c, '/'); 124 | if (snd) { 125 | return strndup(c, (size_t)(snd - c)); 126 | } 127 | return strdup(c); 128 | } 129 | 130 | /* 131 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 132 | * 133 | * Local variables: 134 | * c-basic-offset: 8 135 | * tab-width: 8 136 | * indent-tabs-mode: nil 137 | * End: 138 | * 139 | * vi: set shiftwidth=8 tabstop=8 expandtab: 140 | * :indentSize=8:tabSize=8:noTabs=true: 141 | */ 142 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # usysconf 2 | 3 | [![License](https://img.shields.io/badge/License-GPL%202.0-blue.svg)](https://opensource.org/licenses/GPL-2.0) 4 | 5 | Universal system configuration interface for operating systems (i.e. Solus) 6 | 7 | `usysconf` provides a centralised configuration system to replace "package hooks" and post-installation triggers with a reentrant binary, suitable for use in recovery situations. It requires a certain development approach, by throwing away traditional postinstalls, and centralising/minimising the amount of system delta generated during these steps. 8 | 9 | The binary replaces the legacy [COMAR system](https://solus-project.com/2017/11/12/this-week-in-solus-install-48/) in Solus with a single point for all configuration to take place. A simple state file tracks the `mtime` for interesting file paths, and when those are invalidated triggers are run in response. This allows `usysconf` to be the final execution step in any package/update operation, and incremently apply trigger steps to the system. 10 | 11 | Lastly, a CLI interface is provided allowing users to forcibly run well known triggers by name (or all) to assist in recovery. This allows users to not worry about the inaccessible triggers of the past, and instead request usysconf run all triggers that need running. Optionally, all triggers can be run, bypassing update time checks: 12 | 13 | ```bash 14 | $ sudo usysconf run -f 15 | ``` 16 | 17 | `usysconf` is a [Solus project](https://solus-project.com/) 18 | 19 | ![logo](https://build.solus-project.com/logo.png) 20 | 21 | ## Supported triggers 22 | 23 | | Trigger | Description | Notes | 24 | |----------------|-----------------------------------------|-----------------------------------------------| 25 | | ldconfig | Update dynamic library cache | | 26 | | boot | Update boot configuration + kernels | Uses `clr-boot-manager` | 27 | | qol-assist | Register QoL migration | Uses `qol-assist` | 28 | | depmod | Run depmod for each kernel | | 29 | | hwdb | Update hardware database | | 30 | | drivers | Update graphical driver configuration | Uses `linux-driver-management` | 31 | | sysusers | Update systemd sysusers | | 32 | | tmpfiles | Update systemd tmpfiles | | 33 | | systemd-reload | Reload systemd configuration | | 34 | | systemd-reexec | Re-execute systemd | | 35 | | vbox-restart | Restart VirtualBox services | Will be replaced with generic service handler | 36 | | apparmor | Compile AppArmor profiles | Uses `aa-lsm-hook` | 37 | | glib2 | Compile glib-schemas | | 38 | | fonts | Rebuild font cache | | 39 | | mime | Update mimetype database | | 40 | | icon-caches | Update icon theme caches | | 41 | | desktop-files | Update desktop database | | 42 | | gconf | Update GConf schemas | | 43 | | dconf | Update dconf database | | 44 | | gtk2-immodules | Update GTK2 input module cache | | 45 | | gtk3-immodules | Update GTK3 input module cache | | 46 | | mandb | Updating manpages database | | 47 | | ssl-certs | Update SSL certificate configuration | Only uses `c_rehash` right now | 48 | | mono-certs | Populate Mono certificates | Uses `cert-sync` | 49 | | openssh | Create OpenSSH host key | | 50 | | udev-rules | Reload and apply new udev rules | Always last to facilitate userspace responses | 51 | 52 | ### Special note 53 | 54 | The two cert handlers will eventually merge to use a full system trust store that serves normal applications and Mono. 55 | 56 | 57 | ## Authors 58 | 59 | Copyright © 2017-2018 Solus Project 60 | 61 | `usysconf` is available under the terms of the `GPL-2.0` license. 62 | -------------------------------------------------------------------------------- /src/handlers/xdg/icon-cache.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "context.h" 21 | #include "files.h" 22 | #include "util.h" 23 | 24 | static const char *icon_cache_paths[] = { 25 | "/usr/share/icons/hicolor/*/*", /* per app */ 26 | "/usr/share/icons/gnome/*/*", /* per app */ 27 | "/usr/share/icons/*", 28 | }; 29 | 30 | static bool is_orphan_icon_dir(const char *directory) 31 | { 32 | int n_items = 0; 33 | autofree(DIR) *dir = NULL; 34 | struct dirent *ent = NULL; 35 | 36 | dir = opendir(directory); 37 | if (!dir) { 38 | return false; 39 | } 40 | while ((ent = readdir(dir)) != NULL) { 41 | if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { 42 | continue; 43 | } 44 | ++n_items; 45 | if (strcmp(ent->d_name, "icon-theme.cache") != 0) { 46 | return false; 47 | } 48 | } 49 | /* Must have had exactly one file, the icon-theme.cache */ 50 | return n_items <= 1; 51 | } 52 | 53 | /** 54 | * Helper to nuke the one file and it's parent directory. 55 | * 56 | * TODO: Just add a new rm_rf style command. 57 | */ 58 | static int rmdir_and(const char *dir, const char *file) 59 | { 60 | autofree(char) *fp = NULL; 61 | int ret = 0; 62 | 63 | ret = asprintf(&fp, "%s/%s", dir, file); 64 | if (ret < 0) { 65 | return ret; 66 | } 67 | if (usc_file_exists(fp)) { 68 | ret = unlink(fp); 69 | if (ret < 0) { 70 | return ret; 71 | } 72 | } 73 | return rmdir(dir); 74 | } 75 | 76 | /** 77 | * Update the icon cache on disk for every icon them found in the given directory 78 | */ 79 | static UscHandlerStatus usc_handler_icon_cache_exec(UscContext *ctx, const char *path) 80 | { 81 | autofree(char) *theme_name = NULL; 82 | autofree(char) *theme_index = NULL; 83 | autofree(char) *theme_dir = NULL; 84 | char *command[] = { 85 | "/usr/bin/gtk-update-icon-cache", 86 | "-ftq", 87 | NULL, /* Path */ 88 | NULL, /* Terminator */ 89 | }; 90 | 91 | if (!usc_file_is_dir(path)) { 92 | return USC_HANDLER_SKIP; 93 | } 94 | 95 | /* We're not interested in subdirs in our execution call */ 96 | theme_name = usc_get_strn_component(path, 3); 97 | if (!theme_name) { 98 | fputs("OOM\n", stderr); 99 | return USC_HANDLER_FAIL; 100 | } 101 | 102 | /* Grab the theme dir now */ 103 | if (asprintf(&theme_dir, "/usr/share/icons/%s", theme_name) < 0) { 104 | fputs("OOM\n", stderr); 105 | return USC_HANDLER_FAIL; 106 | } 107 | 108 | /* Index theme too */ 109 | if (asprintf(&theme_index, "%s/index.theme", theme_dir) < 0) { 110 | fputs("OOM\n", stderr); 111 | return USC_HANDLER_FAIL; 112 | } 113 | 114 | /* We'll only record our skip for valid paths */ 115 | if (usc_context_should_skip(ctx, theme_dir)) { 116 | return USC_HANDLER_SUCCESS; 117 | } 118 | 119 | if (!usc_context_push_skip(ctx, theme_dir)) { 120 | return USC_HANDLER_FAIL; 121 | } 122 | 123 | /* Is this a stray icon theme dir? */ 124 | if (is_orphan_icon_dir(theme_dir)) { 125 | usc_context_emit_task_start(ctx, "Removing empty icon directory: %s", theme_dir); 126 | if (rmdir_and(theme_dir, "icon-theme.cache") != 0) { 127 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 128 | usc_context_printf(ctx, 129 | "Failed to remove '%s': %s\n", 130 | theme_dir, 131 | strerror(errno)); 132 | return USC_HANDLER_FAIL; 133 | } 134 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 135 | return USC_HANDLER_SUCCESS | USC_HANDLER_DROP; 136 | } 137 | 138 | /* Require an index theme. */ 139 | if (!usc_file_exists(theme_index)) { 140 | return USC_HANDLER_SKIP; 141 | } 142 | 143 | command[2] = (char *)theme_dir; 144 | usc_context_emit_task_start(ctx, "Updating icon theme cache: %s", theme_name); 145 | int ret = usc_exec_command(command); 146 | if (ret != 0) { 147 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 148 | return USC_HANDLER_FAIL; 149 | } 150 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 151 | return USC_HANDLER_SUCCESS; 152 | } 153 | 154 | const UscHandler usc_handler_icon_cache = { 155 | .name = "icon-caches", 156 | .description = "Update icon theme caches", 157 | .required_bin = "/usr/bin/gtk-update-icon-cache", 158 | .exec = usc_handler_icon_cache_exec, 159 | .paths = icon_cache_paths, 160 | .n_paths = ARRAY_SIZE(icon_cache_paths), 161 | }; 162 | 163 | /* 164 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 165 | * 166 | * Local variables: 167 | * c-basic-offset: 8 168 | * tab-width: 8 169 | * indent-tabs-mode: nil 170 | * End: 171 | * 172 | * vi: set shiftwidth=8 tabstop=8 expandtab: 173 | * :indentSize=8:tabSize=8:noTabs=true: 174 | */ 175 | -------------------------------------------------------------------------------- /src/context.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #pragma once 13 | 14 | #include 15 | 16 | #include "util.h" 17 | 18 | /** 19 | * Bitwise flags for tracking the supported operations and state of a UscContext 20 | */ 21 | typedef enum { 22 | USC_FLAGS_MIN = 1 << 0, 23 | USC_FLAGS_CHROOTED = 1 << 1, 24 | USC_FLAGS_FORCE_CHROOT = 1 << 2, 25 | USC_FLAGS_LIVE_MEDIUM = 1 << 3, 26 | USC_FLAGS_MAX, 27 | } UscFlags; 28 | 29 | /** 30 | * A UscContext is an opaque object passed around to our trigger functions 31 | * to organise state. 32 | */ 33 | typedef struct UscContext UscContext; 34 | 35 | /** 36 | * Each execution handler can return one of 3 return codes only. 37 | */ 38 | typedef enum { 39 | USC_HANDLER_MIN = 1 << 0, 40 | USC_HANDLER_SUCCESS = 1 << 1, /** 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "context.h" 25 | #include "files.h" 26 | #include "util.h" 27 | 28 | static const char *gconf_paths[] = { "/etc/gconf/schemas", "/usr/share/gconf/schemas" }; 29 | 30 | #define GCONF_PRIMARY_TREE "/etc/gconf/gconf.xml.defaults" 31 | #define MERGE_PTR "xml:merged:" GCONF_PRIMARY_TREE 32 | 33 | /** 34 | * Helper method to nuke the existing tree quickly 35 | */ 36 | static int rm_dir_kids(const char *dir) 37 | { 38 | autofree(DIR) *d = NULL; 39 | struct dirent *ent = NULL; 40 | int fd = 0; 41 | int r = 0; 42 | 43 | d = opendir(dir); 44 | if (!d) { 45 | return -errno; 46 | } 47 | 48 | fd = dirfd(d); 49 | 50 | while ((ent = readdir(d)) != NULL) { 51 | if (strcmp(ent->d_name, ".") == 0) { 52 | continue; 53 | } 54 | if (strcmp(ent->d_name, "..") == 0) { 55 | continue; 56 | } 57 | if (!strstr(ent->d_name, ".xml")) { 58 | continue; 59 | } 60 | if ((r = unlinkat(fd, ent->d_name, 0)) != 0) { 61 | return r; 62 | } 63 | } 64 | return 0; 65 | } 66 | 67 | /** 68 | * Rebuild the gconf2 merged database 69 | */ 70 | static UscHandlerStatus usc_handler_gconf_exec_internal(UscContext *ctx, const char *path) 71 | { 72 | glob_t schemas = { 0 }; 73 | autofree(char) *schema_glob = NULL; 74 | UscHandlerStatus ret = USC_HANDLER_SKIP; 75 | 76 | if (!usc_file_is_dir(path)) { 77 | return USC_HANDLER_SKIP; 78 | } 79 | 80 | /* Build the glob pattern */ 81 | if (asprintf(&schema_glob, "%s/*.schemas", path) < 0) { 82 | fputs("OOM\n", stderr); 83 | return USC_HANDLER_FAIL; 84 | } 85 | 86 | schemas.gl_offs = 2; 87 | if (glob(schema_glob, GLOB_DOOFFS | GLOB_NOSORT, NULL, &schemas) != 0) { 88 | ret = USC_HANDLER_SKIP; 89 | goto done; 90 | } 91 | 92 | /* Set args */ 93 | schemas.gl_pathv[0] = "/usr/bin/gconftool-2"; 94 | schemas.gl_pathv[1] = "--makefile-install-rule"; 95 | 96 | setenv("GCONF_CONFIG_SOURCE", MERGE_PTR, 1); 97 | usc_context_emit_task_start(ctx, "Registering gconf schemas in: %s", path); 98 | int r = usc_exec_command(schemas.gl_pathv); 99 | if (r != 0) { 100 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 101 | ret = USC_HANDLER_FAIL | USC_HANDLER_BREAK; 102 | } else { 103 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 104 | ret = USC_HANDLER_SUCCESS | USC_HANDLER_BREAK; 105 | } 106 | 107 | unsetenv("GCONF_CONFIG_SOURCE"); 108 | done: 109 | globfree(&schemas); 110 | return ret; 111 | } 112 | 113 | static UscHandlerStatus usc_handler_gconf_exec(UscContext *ctx, const char *path) 114 | { 115 | UscHandlerStatus ret = 0; 116 | 117 | if (!usc_file_is_dir(path)) { 118 | return USC_HANDLER_SKIP; 119 | } 120 | 121 | /* Even if we re-enter, ensure we only nuke the first time. */ 122 | if (!usc_context_should_skip(ctx, GCONF_PRIMARY_TREE)) { 123 | if (usc_file_is_dir(GCONF_PRIMARY_TREE)) { 124 | usc_context_emit_task_start(ctx, "Removing old gconf tree"); 125 | if (rm_dir_kids(GCONF_PRIMARY_TREE) < 0) { 126 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 127 | usc_context_printf(ctx, 128 | "Cannot remove stale gconf tree: %s\n", 129 | strerror(errno)); 130 | return USC_HANDLER_FAIL; 131 | } 132 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 133 | } 134 | 135 | if (!usc_context_push_skip(ctx, GCONF_PRIMARY_TREE)) { 136 | fputs("OOM\n", stderr); 137 | return USC_HANDLER_FAIL; 138 | } 139 | } 140 | 141 | if (!usc_file_exists(GCONF_PRIMARY_TREE)) { 142 | usc_context_emit_task_start(ctx, "Preparing gconf tree"); 143 | if (mkdir(GCONF_PRIMARY_TREE, 00755) != 0) { 144 | usc_context_emit_task_finish(ctx, USC_HANDLER_FAIL); 145 | fprintf(stderr, "Cannot construct gconf dir: %s\n", strerror(errno)); 146 | return USC_HANDLER_FAIL; 147 | } 148 | usc_context_emit_task_finish(ctx, USC_HANDLER_SUCCESS); 149 | } 150 | 151 | /* Make sure we update all the trees now */ 152 | for (size_t i = 0; i < ARRAY_SIZE(gconf_paths); i++) { 153 | UscHandlerStatus lret = usc_handler_gconf_exec_internal(ctx, gconf_paths[i]); 154 | if ((lret & USC_HANDLER_FAIL) == USC_HANDLER_FAIL) { 155 | ret = lret; 156 | break; 157 | } 158 | if ((ret & USC_HANDLER_SUCCESS) != USC_HANDLER_SUCCESS) { 159 | ret = lret; 160 | } 161 | } 162 | 163 | return ret; 164 | } 165 | 166 | const UscHandler usc_handler_gconf = { 167 | .name = "gconf", 168 | .description = "Update GConf schemas", 169 | .required_bin = "/usr/bin/gconftool-2", 170 | .exec = usc_handler_gconf_exec, 171 | .paths = gconf_paths, 172 | .n_paths = ARRAY_SIZE(gconf_paths), 173 | }; 174 | 175 | /* 176 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 177 | * 178 | * Local variables: 179 | * c-basic-offset: 8 180 | * tab-width: 8 181 | * indent-tabs-mode: nil 182 | * End: 183 | * 184 | * vi: set shiftwidth=8 tabstop=8 expandtab: 185 | * :indentSize=8:tabSize=8:noTabs=true: 186 | */ 187 | -------------------------------------------------------------------------------- /man/usysconf.1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | usysconf(1) - Universal system configuration 7 | 44 | 45 | 52 | 53 |
54 | 55 | 67 | 68 |
    69 |
  1. usysconf(1)
  2. 70 |
  3. 71 |
  4. usysconf(1)
  5. 72 |
73 | 74 |

NAME

75 |

76 | usysconf - Universal system configuration 77 |

78 | 79 |

SYNOPSIS

80 | 81 |

usysconf run [triggers]

82 | 83 |

DESCRIPTION

84 | 85 |

usysconf is a system configuration agent designed for integration within the 86 | software update process. The main job for usysconf is to run OS configuration 87 | tasks within a well defined centralised location.

88 | 89 |

usysconf will run a variety of triggers to ensure system consistency and 90 | health, detecting changes within the filesystem and taking any appropriate 91 | actions to configure components such as kernels or the dynamic linker cache.

92 | 93 |

OPTIONS

94 | 95 |

The following options are applicable to usysconf(1).

96 | 97 |
    98 |
  • -f, --force

    99 | 100 |

    Force a trigger to run, even if the local file doesn't seem to have been 101 | updated.

  • 102 |
103 | 104 | 105 |

SUBCOMMANDS

106 | 107 |

run [triggers]

108 | 109 |
When called without any arguments, all system triggers will be executed.
110 | Alternatively, you may provide a list of triggers to run directly.
111 | 
112 | The special value "list" will cause all known triggers to be listed
113 | instead of executing them.
114 | 
115 | 116 |

help

117 | 118 |
Print the supported command set for the `usysconf(1)` binary.
119 | 
120 | 121 |

version

122 | 123 |
Print the version and license information, before quitting.
124 | 
125 | 126 |

FILES

127 | 128 |

usysconf tracks state through some special files, and will recover in their 129 | absence.

130 | 131 |

/var/log/usysconf.log

132 | 133 |
This file will contain the output from the last run of usysconf, if the
134 | tool was run automatically without a working `stdout`. This is typical
135 | for situations where `usysconf` is masked behind a long running daemon.
136 | 
137 | 138 |

/var/log/usysconf.rewind.log

139 | 140 |
This is a temporary file used by `usysconf` to store the stdout/stderr
141 | of the currently executing command. Under normal situations, this file
142 | will be replayed into the main log file if there is an error, and this
143 | rewind log will be deleted.
144 | 
145 | 146 |

/var/lib/usysconf/status

147 | 148 |
This file is used to track the current state of `usysconf` handlers.
149 | Upon completion, `usysconf` will write the state file with the mtime
150 | for each known asset for the triggers. This file is read by `usysconf`
151 | on startup to allow unmodified assets to be skipped, making `usysconf`
152 | incremental in nature.
153 | 
154 | If this file is removed, `usysconf` will run all triggers again and
155 | write the state file once more - this is non fatal. In fact, this
156 | mechanism can be used to reset `usysconf` state to ensure that all
157 | triggers do run again.
158 | 
159 | 160 |

EXIT STATUS

161 | 162 |

On success, 0 is returned. A non-zero return code signals a failure.

163 | 164 | 165 | 166 |
    167 |
  • Copyright © 2017-2018 Ikey Doherty, License: CC-BY-SA-3.0
  • 168 |
169 | 170 | 171 |

SEE ALSO

172 | 173 |

clr-boot-manager(1), qol-assist(1)

174 | 175 |
    176 |
  • https://github.com/solus-project/usysconf
  • 177 |
178 | 179 | 180 |

NOTES

181 | 182 |

Creative Commons Attribution-ShareAlike 3.0 Unported

183 | 184 |
    185 |
  • http://creativecommons.org/licenses/by-sa/3.0/
  • 186 |
187 | 188 | 189 | 190 |
    191 |
  1. 192 |
  2. May 2018
  3. 193 |
  4. usysconf(1)
  5. 194 |
195 | 196 |
197 | 198 | 199 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project( 2 | 'usysconf', 3 | ['c'], 4 | version: '0.5.2', 5 | license: [ 6 | 'GPL-2.0', 7 | ], 8 | default_options: [ 9 | 'c_std=c11', 10 | 'prefix=/usr', 11 | 'sysconfdir=/etc', 12 | 'localstatedir=/var', 13 | 'default-library=static', 14 | ], 15 | ) 16 | 17 | am_cflags = [ 18 | '-fstack-protector', 19 | '-Wall', 20 | '-pedantic', 21 | '-Wstrict-prototypes', 22 | '-Wundef', 23 | '-fno-common', 24 | '-Werror-implicit-function-declaration', 25 | '-Wformat', 26 | '-Wformat-security', 27 | '-Werror=format-security', 28 | '-Wconversion', 29 | '-Wunused-variable', 30 | '-Wunreachable-code', 31 | '-W', 32 | ] 33 | 34 | # Add our main flags 35 | add_global_arguments(am_cflags, language: 'c') 36 | 37 | # Get configuration bits together 38 | path_prefix = get_option('prefix') 39 | path_sysconfdir = join_paths(path_prefix, get_option('sysconfdir')) 40 | path_mandir = join_paths(path_prefix, get_option('mandir')) 41 | path_datadir = join_paths(path_prefix, get_option('datadir')) 42 | path_sbindir = join_paths(path_prefix, get_option('sbindir')) 43 | path_vardir = join_paths(path_prefix, get_option('localstatedir'), 'lib', meson.project_name()) 44 | 45 | 46 | # Sort out config.h now 47 | cdata = configuration_data() 48 | 49 | # General options.. 50 | cdata.set_quoted('PACKAGE_NAME', meson.project_name()) 51 | cdata.set_quoted('PACKAGE_VERSION', meson.project_version()) 52 | cdata.set_quoted('PACKAGE_URL', 'https://solus-project.com') 53 | 54 | # Allow building a static binary. For now let's default to dynamic so we can 55 | # actually valgrind the thing. 56 | with_static_binary = get_option('with-static-binary') 57 | 58 | with_cbm = get_option('with-clr-boot-manager') 59 | if with_cbm == true 60 | cdata.set('HAVE_CBM', '1') 61 | endif 62 | with_ldm = get_option('with-linux-driver-management') 63 | if with_ldm == true 64 | cdata.set('HAVE_LDM', '1') 65 | endif 66 | with_qol = get_option('with-qol-assist') 67 | if with_qol == true 68 | cdata.set('HAVE_QOL_ASSIST', '1') 69 | endif 70 | with_apparmor = get_option('with-apparmor') 71 | if with_apparmor == true 72 | cdata.set('HAVE_APPARMOR', '1') 73 | endif 74 | with_mono_certs = get_option('with-mono-certs') 75 | if with_mono_certs == true 76 | cdata.set('HAVE_MONO_CERTS', '1') 77 | endif 78 | 79 | # Work out systemd support 80 | with_systemd = get_option('with-systemd') 81 | if with_systemd == true 82 | cdata.set('HAVE_SYSTEMD', '1') 83 | 84 | message('Asking pkg-config for systemd\'s directories') 85 | dep_systemd = dependency('systemd') 86 | systemd_unit_dir = dep_systemd.get_pkgconfig_variable('systemdsystemunitdir') 87 | systemd_tmpfiles_dir = dep_systemd.get_pkgconfig_variable('tmpfilesdir') 88 | systemd_sysusers_dir = dep_systemd.get_pkgconfig_variable('sysusersdir') 89 | systemd_util_dir = dep_systemd.get_pkgconfig_variable('systemdutildir') 90 | cdata.set_quoted('SYSTEMD_UNIT_DIR', systemd_unit_dir) 91 | cdata.set_quoted('SYSTEMD_TMPFILES_DIR', systemd_tmpfiles_dir) 92 | cdata.set_quoted('SYSTEMD_SYSUSERS_DIR', systemd_sysusers_dir) 93 | cdata.set_quoted('SYSTEMD_UTIL_DIR', systemd_util_dir) 94 | 95 | with_systemd_reexec = get_option('with-systemd-reexec') 96 | if with_systemd_reexec == true 97 | cdata.set_quoted('HAVE_SYSTEMD_REEXEC', '1') 98 | endif 99 | 100 | with_vbox_restart = get_option('with-vbox-restart') 101 | if with_vbox_restart == true 102 | cdata.set_quoted('HAVE_VBOX_RESTART', '1') 103 | endif 104 | endif 105 | 106 | with_kernel_modules_dir = get_option('with-kernel-modules-dir') 107 | with_kernel_dir = get_option('with-kernel-dir') 108 | cdata.set_quoted('KERNEL_MODULES_DIR', with_kernel_modules_dir) 109 | cdata.set_quoted('KERNEL_DIR', with_kernel_dir) 110 | 111 | # Tracking files 112 | cdata.set_quoted('USYSCONF_TRACK_DIR', path_vardir) 113 | with_status_file = join_paths(path_vardir, 'status') 114 | cdata.set_quoted('USYSCONF_STATUS_FILE', with_status_file) 115 | 116 | with_log_dir = get_option('with-log-dir') 117 | with_rewind_log_file = join_paths(with_log_dir, '@0@.rewind.log'.format(meson.project_name())) 118 | with_log_file = join_paths(with_log_dir, '@0@.log'.format(meson.project_name())) 119 | cdata.set_quoted('USYSCONF_LOG_DIR', with_log_dir) 120 | cdata.set_quoted('USYSCONF_REWIND_LOG_FILE', with_rewind_log_file) 121 | cdata.set_quoted('USYSCONF_LOG_FILE', with_log_file) 122 | 123 | # Write config.h now 124 | config_h = configure_file( 125 | configuration: cdata, 126 | output: 'config.h', 127 | ) 128 | config_h_dir = include_directories('.') 129 | 130 | # Get libuf built to provide convenience functions 131 | libuf = subproject('libuf', 132 | default_options: [ 133 | 'c_std=c11', 134 | 'with-tests=false', 135 | 'with-static=true', 136 | ] 137 | ) 138 | 139 | # Install manpage 140 | subdir('man') 141 | 142 | # Now go build the source 143 | subdir('src') 144 | 145 | report = [ 146 | ' Build configuration:', 147 | ' ====================', 148 | '', 149 | ' prefix: @0@'.format(path_prefix), 150 | ' datadir: @0@'.format(path_datadir), 151 | ' sysconfdir: @0@'.format(path_sysconfdir), 152 | ' mandir: @0@'.format(path_mandir), 153 | ' sbindir: @0@'.format(path_sbindir), 154 | ' static binary: @0@'.format(with_static_binary), 155 | ' tracking directory: @0@'.format(path_vardir), 156 | ' status file: @0@'.format(with_status_file), 157 | ' rewind log file: @0@'.format(with_rewind_log_file), 158 | ' main log file: @0@'.format(with_log_file), 159 | '', 160 | ' Extra modules:', 161 | ' ==============', 162 | '', 163 | ' clr-boot-manager: @0@'.format(with_cbm), 164 | ' linux-driver-management: @0@'.format(with_ldm), 165 | ' qol-assist: @0@'.format(with_qol), 166 | ' systemd support: @0@'.format(with_systemd), 167 | ' apparmor (aa-lsm-hook) @0@'.format(with_apparmor), 168 | ' mono-certs (cert-sync) @0@'.format(with_mono_certs), 169 | '', 170 | ' Kernel configuration:', 171 | ' =====================', 172 | '', 173 | ' modules directory: @0@'.format(with_kernel_dir), 174 | ' kernel directory: @0@'.format(with_kernel_modules_dir), 175 | ] 176 | 177 | if with_systemd == true 178 | report += [ 179 | '', 180 | ' systemd configuration:', 181 | ' ======================', 182 | '', 183 | ' systemd unit directory: @0@'.format(systemd_unit_dir), 184 | ' systemd tmpfiles directory: @0@'.format(systemd_tmpfiles_dir), 185 | ' systemd sysusers directory: @0@'.format(systemd_sysusers_dir), 186 | ' systemd binary directory: @0@'.format(systemd_util_dir), 187 | ' allow systemd reexec: @0@'.format(with_systemd_reexec), 188 | ' support vboxdrv.service: @0@'.format(with_vbox_restart), 189 | ] 190 | endif 191 | 192 | # Output some stuff to validate the build config 193 | message('\n\n\n' + '\n'.join(report) + '\n\n') 194 | -------------------------------------------------------------------------------- /src/state.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "config.h" 22 | #include "files.h" 23 | #include "state.h" 24 | 25 | DEF_AUTOFREE(FILE, fclose) 26 | 27 | /** 28 | * Each entry is just a list node with a ptr (registered interest) and 29 | * an mtime for when it was last modified on disk 30 | */ 31 | typedef struct UscStateEntry { 32 | struct UscStateEntry *next; /**state_file = USYSCONF_STATUS_FILE; 54 | return ret; 55 | } 56 | 57 | /** 58 | * Lookup the path within the internal list and find any matching node for it. 59 | * 60 | * This may return NULL. 61 | */ 62 | static UscStateEntry *usc_state_tracker_lookup(UscStateTracker *self, const char *path) 63 | { 64 | for (UscStateEntry *entry = self->entry; entry; entry = entry->next) { 65 | if (entry->ptr && strcmp(entry->ptr, path) == 0) { 66 | return entry; 67 | } 68 | } 69 | return NULL; 70 | } 71 | 72 | /** 73 | * Internal logic for pushing or updating an existing path. 74 | */ 75 | static bool usc_state_tracker_put_entry(UscStateTracker *self, char *path, time_t mtime) 76 | { 77 | UscStateEntry *entry = NULL; 78 | 79 | /* Does this entry already exist */ 80 | entry = usc_state_tracker_lookup(self, path); 81 | if (entry) { 82 | entry->mtime = mtime; 83 | return true; 84 | } 85 | 86 | /* Must insert a new entry now */ 87 | entry = calloc(1, sizeof(UscStateEntry)); 88 | if (!entry) { 89 | fputs("OOM\n", stderr); 90 | return false; 91 | } 92 | entry->ptr = strdup(path); 93 | if (!entry->ptr) { 94 | fputs("OOM\n", stderr); 95 | free(entry); 96 | return false; 97 | } 98 | 99 | /* Merge list reversed */ 100 | entry->mtime = mtime; 101 | entry->next = self->entry; 102 | self->entry = entry; 103 | return true; 104 | } 105 | 106 | bool usc_state_tracker_push_path(UscStateTracker *self, const char *path) 107 | { 108 | struct stat st = { 0 }; 109 | 110 | autofree(char) *dup = NULL; 111 | 112 | /* Make sure it actually exists */ 113 | dup = realpath(path, NULL); 114 | if (!dup) { 115 | return false; 116 | } 117 | 118 | if (stat(dup, &st) != 0) { 119 | return false; 120 | } 121 | 122 | return usc_state_tracker_put_entry(self, dup, st.st_mtime); 123 | } 124 | 125 | static void usc_state_entry_free(UscStateEntry *entry) 126 | { 127 | if (!entry) { 128 | return; 129 | } 130 | /* Free next dude in the chain */ 131 | usc_state_entry_free(entry->next); 132 | free(entry->ptr); 133 | free(entry); 134 | } 135 | 136 | void usc_state_tracker_free(UscStateTracker *self) 137 | { 138 | if (!self) { 139 | return; 140 | } 141 | usc_state_entry_free(self->entry); 142 | free(self); 143 | } 144 | 145 | bool usc_state_tracker_write(UscStateTracker *self) 146 | { 147 | autofree(FILE) *fp = NULL; 148 | 149 | if (!usc_file_exists(USYSCONF_TRACK_DIR) && mkdir(USYSCONF_TRACK_DIR, 00755) != 0) { 150 | fprintf(stderr, "mkdir(): %s %s\n", USYSCONF_TRACK_DIR, strerror(errno)); 151 | return false; 152 | } 153 | 154 | fp = fopen(self->state_file, "w"); 155 | if (!fp) { 156 | fprintf(stderr, "fopen(): %s %s\n", self->state_file, strerror(errno)); 157 | return false; 158 | } 159 | 160 | fprintf(fp, "# This file is automatically generated. DO NOT EDIT\n"); 161 | 162 | /* Walk nodes */ 163 | for (UscStateEntry *entry = self->entry; entry; entry = entry->next) { 164 | /* Drop stale entries here */ 165 | if (!entry->ptr || !usc_file_exists(entry->ptr)) { 166 | continue; 167 | } 168 | if (fprintf(fp, "%ld:%s\n", entry->mtime, entry->ptr) < 0) { 169 | fprintf(stderr, "fprintf(): %s %s\n", self->state_file, strerror(errno)); 170 | return false; 171 | } 172 | } 173 | 174 | return true; 175 | } 176 | 177 | bool usc_state_tracker_load(UscStateTracker *self) 178 | { 179 | autofree(FILE) *fp = NULL; 180 | char *bfr = NULL; 181 | size_t n = 0; 182 | ssize_t read = 0; 183 | 184 | fp = fopen(self->state_file, "r"); 185 | if (!fp) { 186 | /* If it doesn't exist, *meh* */ 187 | if (errno == ENOENT) { 188 | errno = 0; 189 | return true; 190 | } 191 | fprintf(stderr, 192 | "Failed to load state file %s: %s\n", 193 | self->state_file, 194 | strerror(errno)); 195 | return false; 196 | } 197 | 198 | errno = 0; 199 | 200 | /* Walk the line. */ 201 | while ((read = getline(&bfr, &n, fp)) > 0) { 202 | char *c = NULL; 203 | char *part = NULL; 204 | autofree(char) *snd = NULL; 205 | time_t t = 0; 206 | 207 | if (read < 1) { 208 | continue; 209 | } 210 | /* Strip the newline from it */ 211 | if (bfr[read - 1] == '\n') { 212 | bfr[read - 1] = '\0'; 213 | --read; 214 | } 215 | 216 | /* Skip comments */ 217 | if (*bfr == '#') { 218 | continue; 219 | } 220 | 221 | c = memchr(bfr, ':', (size_t)read); 222 | if (!c) { 223 | fprintf(stderr, "Erronous line in input misses colon: '%s'\n", bfr); 224 | errno = EINVAL; 225 | break; 226 | } 227 | 228 | if (c - bfr < 1) { 229 | fprintf(stderr, "Missing filename in line: '%s'\n", bfr); 230 | errno = EINVAL; 231 | break; 232 | } 233 | 234 | snd = strdup(c + 1); 235 | 236 | /* Now break the string here at the colon */ 237 | bfr[c - bfr] = '\0'; 238 | 239 | t = strtoll(bfr, &part, 10); 240 | if (!part) { 241 | fprintf(stderr, "Invalid timestamp '%s'\n", bfr); 242 | errno = EINVAL; 243 | break; 244 | } 245 | 246 | /* Drop old cache entries */ 247 | if (!usc_file_exists(snd)) { 248 | errno = 0; 249 | goto next; 250 | } 251 | 252 | /* Ensure we can actually push this guy. */ 253 | if (!usc_state_tracker_put_entry(self, snd, t)) { 254 | fprintf(stderr, "Failed to push entry: %s\n", snd); 255 | errno = EINVAL; 256 | break; 257 | } 258 | 259 | next: 260 | free(bfr); 261 | bfr = NULL; 262 | } 263 | 264 | if (bfr) { 265 | free(bfr); 266 | } 267 | 268 | /* Janky ready. */ 269 | if (errno != 0) { 270 | fprintf(stderr, 271 | "Failed to parse state: %s %s\n", 272 | self->state_file, 273 | strerror(errno)); 274 | usc_state_entry_free(self->entry); 275 | self->entry = NULL; 276 | return false; 277 | } 278 | 279 | /* Successfully loaded */ 280 | return true; 281 | } 282 | 283 | bool usc_state_tracker_needs_update(UscStateTracker *self, const char *path, bool force) 284 | { 285 | time_t mtime = 0; 286 | UscStateEntry *entry = NULL; 287 | autofree(char) *real = NULL; 288 | 289 | real = realpath(path, NULL); 290 | if (!real) { 291 | return false; 292 | } 293 | 294 | /* Don't know about this guy? Needs an update */ 295 | entry = usc_state_tracker_lookup(self, real); 296 | if (!entry) { 297 | return true; 298 | } 299 | 300 | /* Some fs bork, do it anyway */ 301 | if (!usc_file_mtime(real, &mtime)) { 302 | return true; 303 | } 304 | 305 | if (force) { 306 | return true; 307 | } 308 | 309 | /* If our record mtime is not the same as the current mtime, update it */ 310 | if (entry->mtime != mtime) { 311 | return true; 312 | } 313 | 314 | /* Nothing to be done here.. */ 315 | return false; 316 | } 317 | 318 | /* 319 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 320 | * 321 | * Local variables: 322 | * c-basic-offset: 8 323 | * tab-width: 8 324 | * indent-tabs-mode: nil 325 | * End: 326 | * 327 | * vi: set shiftwidth=8 tabstop=8 expandtab: 328 | * :indentSize=8:tabSize=8:noTabs=true: 329 | */ 330 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /src/context.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of usysconf. 3 | * 4 | * Copyright © 2017-2018 Solus Project 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | */ 11 | 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "config.h" 24 | #include "context.h" 25 | #include "files.h" 26 | #include "handlers.h" 27 | #include "state.h" 28 | 29 | /* libuf */ 30 | #include "map.h" 31 | 32 | /** 33 | * TODO: Expose this more generically through libuf API 34 | */ 35 | #ifndef UF_INT_TO_PTR 36 | #define UF_INT_TO_PTR(x) ((void *)((uintptr_t)x)) 37 | #endif 38 | 39 | /* Table of supported handlers */ 40 | static const UscHandler *usc_handlers[] = { 41 | &usc_handler_ldconfig, /**flags |= USC_FLAGS_CHROOTED; 132 | } 133 | ret->have_tty = isatty(STDOUT_FILENO) ? true : false; 134 | ret->failed = false; 135 | 136 | if (access("/run/initramfs/livedev", F_OK) == 0) { 137 | ret->flags |= USC_FLAGS_LIVE_MEDIUM; 138 | } 139 | 140 | /* Useful for build environments to forcibly bypass isatty detection */ 141 | if (getenv("USYSCONF_LOG_STDOUT")) { 142 | ret->have_tty = true; 143 | ret->logfh = stdout; 144 | } 145 | 146 | /* Ensure we have logging */ 147 | if (!ret->have_tty) { 148 | /* Before we go, make sure log directory exists */ 149 | if (!usc_file_exists(USYSCONF_LOG_DIR) && mkdir(USYSCONF_LOG_DIR, 00755) != 0) { 150 | fprintf(stderr, 151 | "Cannot construct log directory %s: %s\n", 152 | USYSCONF_LOG_DIR, 153 | strerror(errno)); 154 | usc_context_free(ret); 155 | return NULL; 156 | } 157 | /* We only want to write a fresh log for the current run. Failing items will 158 | * keep failing until fixed, such as clr-boot-manager */ 159 | ret->logfh = fopen(USYSCONF_LOG_FILE, "w"); 160 | if (!ret->logfh) { 161 | fprintf(stderr, 162 | "Cannot open log file %s: %s\n", 163 | USYSCONF_LOG_FILE, 164 | strerror(errno)); 165 | usc_context_free(ret); 166 | return NULL; 167 | } 168 | } else { 169 | ret->logfh = stdout; 170 | } 171 | 172 | /* Skip map only contains a 1 value */ 173 | ret->skip_map = 174 | uf_hashmap_new_full(uf_hashmap_string_hash, uf_hashmap_string_equal, free, NULL); 175 | if (!ret->skip_map) { 176 | usc_context_free(ret); 177 | return NULL; 178 | } 179 | 180 | return ret; 181 | } 182 | 183 | void usc_context_free(UscContext *self) 184 | { 185 | if (!self) { 186 | return; 187 | } 188 | if (!self->have_tty && self->logfh) { 189 | fclose(self->logfh); 190 | } 191 | if (self->task_string) { 192 | free(self->task_string); 193 | } 194 | uf_hashmap_free(self->skip_map); 195 | free(self); 196 | } 197 | 198 | bool usc_context_has_flag(UscContext *self, unsigned int flag) 199 | { 200 | if (!self) { 201 | return false; 202 | } 203 | if ((self->flags & flag) == flag) { 204 | return true; 205 | } 206 | return false; 207 | } 208 | 209 | /** 210 | * An item failed, so wind back our log and spit it back out to the tty 211 | */ 212 | static void usc_spit_fail_log(UscContext *self) 213 | { 214 | ssize_t nr; 215 | char buf[4096]; 216 | FILE *dst = NULL; 217 | int fd = -1; 218 | 219 | fd = open(USYSCONF_REWIND_LOG_FILE, O_RDONLY); 220 | if (fd < 0) { 221 | fprintf(stderr, 222 | "open(%s): failed to open log file: %s\n", 223 | USYSCONF_REWIND_LOG_FILE, 224 | strerror(errno)); 225 | return; 226 | } 227 | 228 | dst = self->have_tty ? stderr : self->logfh; 229 | fputs("\nA copy of the command output follows:\n\n", dst); 230 | while ((nr = read(fd, buf, sizeof(buf))) > 0) { 231 | if (fwrite(buf, (size_t)nr, 1, dst) != 1) { 232 | fprintf(stderr, "fwrite(): %s\n", strerror(errno)); 233 | return; 234 | } 235 | } 236 | fputs("\n\n", dst); 237 | } 238 | 239 | static void usc_nuke_rewind_log(void) 240 | { 241 | /* Nuke old playback log here */ 242 | if (usc_file_exists(USYSCONF_REWIND_LOG_FILE) && unlink(USYSCONF_REWIND_LOG_FILE) != 0) { 243 | fprintf(stderr, 244 | "unlink(%s): Cannot remove old log file: %s\n", 245 | USYSCONF_REWIND_LOG_FILE, 246 | strerror(errno)); 247 | } 248 | } 249 | 250 | static void usc_handle_one(const UscHandler *handler, UscContext *context, UscStateTracker *tracker, 251 | bool force_run) 252 | { 253 | UscHandlerStatus status = USC_HANDLER_MIN; 254 | bool record_remain = false; 255 | 256 | /* Make sure the binary is actually present */ 257 | if (handler->required_bin && access(handler->required_bin, X_OK) != 0) { 258 | if (!getenv("USYSCONFIG_DEBUG")) { 259 | return; 260 | } 261 | usc_context_emit_task_start(context, 262 | "skipping: %s (command not found)", 263 | handler->name); 264 | usc_context_emit_task_finish(context, USC_HANDLER_SKIP); 265 | return; 266 | } 267 | 268 | for (size_t i = 0; i < handler->n_paths; i++) { 269 | glob_t glo = { 0 }; 270 | const char *path = NULL; 271 | 272 | path = handler->paths[i]; 273 | 274 | if (glob(path, GLOB_NOSORT, NULL, &glo) != 0) { 275 | continue; 276 | } 277 | 278 | for (size_t i = 0; i < glo.gl_pathc; i++) { 279 | char *resolved = glo.gl_pathv[i]; 280 | bool record_path = false; 281 | 282 | /* Don't try to do anything for the remainder of this glob. */ 283 | if (record_remain) { 284 | goto push_entry; 285 | } 286 | 287 | /* Do we need to handle this dude ? */ 288 | if (!usc_state_tracker_needs_update(tracker, resolved, force_run)) { 289 | continue; 290 | } 291 | 292 | usc_nuke_rewind_log(); 293 | status = handler->exec(context, resolved); 294 | 295 | if ((status & USC_HANDLER_SUCCESS) == USC_HANDLER_SUCCESS) { 296 | record_path = true; 297 | } 298 | if ((status & USC_HANDLER_FAIL) == USC_HANDLER_FAIL) { 299 | context->failed = true; 300 | if (!context->have_tty) { 301 | fprintf(stderr, 302 | "Failed handler '%s', please consult the log file: " 303 | "%s\n", 304 | handler->name, 305 | USYSCONF_LOG_FILE); 306 | } 307 | usc_spit_fail_log(context); 308 | continue; 309 | } 310 | if ((status & USC_HANDLER_SKIP) == USC_HANDLER_SKIP) { 311 | continue; 312 | } 313 | 314 | if ((status & USC_HANDLER_BREAK) == USC_HANDLER_BREAK) { 315 | /* Record all paths as updated */ 316 | record_remain = true; 317 | } 318 | 319 | if (!record_path) { 320 | continue; 321 | } 322 | 323 | push_entry: 324 | /* We won't record the new entry here and we won't write it back out either 325 | */ 326 | if ((status & USC_HANDLER_DROP) == USC_HANDLER_DROP) { 327 | continue; 328 | } 329 | if (!usc_state_tracker_push_path(tracker, resolved)) { 330 | fprintf(stderr, "Failed to record path %s\n", resolved); 331 | } 332 | } 333 | globfree(&glo); 334 | } 335 | usc_nuke_rewind_log(); 336 | } 337 | 338 | /** 339 | * Quick wrapper to force usysconf to perform a filesystem sync right 340 | * before we go to run anything. 341 | * 342 | * Even if we no-op because no changes are needed, we will force syncing 343 | * to get around eopkg not always syncing properly. 344 | */ 345 | static void usc_context_sync(UscContext *self) 346 | { 347 | usc_context_emit_task_start(self, "Syncing filesystems"); 348 | sync(); 349 | usc_context_emit_task_finish(self, USC_HANDLER_SUCCESS); 350 | } 351 | 352 | void usc_context_list_triggers(void) 353 | { 354 | fputs("Currently known triggers:\n\n", stdout); 355 | 356 | for (size_t i = 0; i < ARRAY_SIZE(usc_handlers); i++) { 357 | fprintf(stdout, 358 | "%*s - %s\n", 359 | 30, 360 | usc_handlers[i]->name, 361 | usc_handlers[i]->description); 362 | } 363 | } 364 | 365 | static bool usc_context_ensure_sanity(void) 366 | { 367 | static const char *dirs[] = { 368 | "/root", 369 | "/home/root", 370 | "/", 371 | }; 372 | 373 | /* Ensure we have a home directory */ 374 | if (!getenv("HOME")) { 375 | for (size_t i = 0; i < ARRAY_SIZE(dirs); i++) { 376 | if (usc_file_exists(dirs[i])) { 377 | setenv("HOME", dirs[i], 1); 378 | break; 379 | } 380 | } 381 | } 382 | if (!getenv("PATH")) { 383 | setenv("PATH", "/bin:/usr/bin:/sbin:/usr/sbin", 1); 384 | } 385 | 386 | errno = 0; 387 | 388 | /* Make sure track dir exists */ 389 | if (!usc_file_exists(USYSCONF_TRACK_DIR) && mkdir(USYSCONF_TRACK_DIR, 00755) != 0) { 390 | fprintf(stderr, "Cannot create tracking dir %s\n", USYSCONF_TRACK_DIR); 391 | return false; 392 | } 393 | 394 | /* Move into the track dir for safety */ 395 | if (chdir(USYSCONF_TRACK_DIR) != 0) { 396 | fprintf(stderr, "Failed to chdir() into %s", USYSCONF_TRACK_DIR); 397 | return false; 398 | } 399 | 400 | return true; 401 | } 402 | 403 | bool usc_context_run_triggers(UscContext *context, const char *name, bool force_run) 404 | { 405 | autofree(UscStateTracker) *tracker = NULL; 406 | bool ran_trigger = false; 407 | bool did_sync = false; 408 | 409 | if (!usc_is_proc_mounted()) { 410 | fputs("Refusing to run without a mounted /proc filesystem\n", stderr); 411 | return false; 412 | } 413 | 414 | tracker = usc_state_tracker_new(); 415 | if (!tracker) { 416 | fputs("Cannot continue without valid UscStateTracker\n", stderr); 417 | return false; 418 | } 419 | 420 | /* Crack on regardless. */ 421 | if (!usc_state_tracker_load(tracker)) { 422 | fputs("Invalid state has been removed\n", stderr); 423 | } 424 | 425 | /* At this point ensure we have some sanity */ 426 | if (!usc_context_ensure_sanity()) { 427 | fprintf(stderr, "Failed to assert sane environment: %s\n", strerror(errno)); 428 | return false; 429 | } 430 | 431 | for (size_t i = 0; i < ARRAY_SIZE(usc_handlers); i++) { 432 | if (name && strcmp(usc_handlers[i]->name, name) != 0) { 433 | continue; 434 | } 435 | if (!did_sync) { 436 | usc_context_sync(context); 437 | did_sync = true; 438 | } 439 | usc_handle_one(usc_handlers[i], context, tracker, force_run); 440 | ran_trigger = true; 441 | } 442 | 443 | if (!ran_trigger) { 444 | fprintf(stderr, "Unknown trigger '%s'\n", name); 445 | return false; 446 | } 447 | 448 | /* Dump it back to disk */ 449 | if (!usc_state_tracker_write(tracker)) { 450 | fputs("Failed to write state file!\n", stderr); 451 | return false; 452 | } 453 | 454 | return !context->failed; 455 | } 456 | 457 | bool usc_context_push_skip(UscContext *self, char *skip_item) 458 | { 459 | if (!self) { 460 | return false; 461 | } 462 | return uf_hashmap_put(self->skip_map, strdup(skip_item), UF_INT_TO_PTR(1)); 463 | } 464 | 465 | bool usc_context_should_skip(UscContext *self, char *skip_item) 466 | { 467 | if (!self) { 468 | return false; 469 | } 470 | /* Only 0==NULL on glibc, 1 will be set and thus not NULL */ 471 | return uf_hashmap_get(self->skip_map, skip_item) != NULL; 472 | } 473 | 474 | void usc_context_printf(UscContext *self, const char *fmt, ...) 475 | { 476 | va_list va; 477 | va_start(va, fmt); 478 | /* For now just wrap the existing printf function */ 479 | vfprintf(self->logfh, fmt, va); 480 | va_end(va); 481 | } 482 | 483 | void usc_context_emit_task_start(UscContext *self, const char *fmt, ...) 484 | { 485 | va_list va; 486 | va_start(va, fmt); 487 | int local_offset = 5 + strlen("running"); 488 | static const char *col_reset = "\x1b[0m"; 489 | static const char *embold = "\x1b[1;37m"; 490 | 491 | if (self->task_string) { 492 | free(self->task_string); 493 | self->task_string = NULL; 494 | } 495 | 496 | self->task_print_offset = vasprintf(&self->task_string, fmt, va); 497 | if (self->task_print_offset < 0) { 498 | fputs("OOM\n", stderr); 499 | abort(); 500 | } 501 | 502 | if (self->have_tty) { 503 | fprintf(self->logfh, 504 | " ⌛ %s%*s%srunning%s", 505 | self->task_string, 506 | 79 - self->task_print_offset - local_offset, 507 | " ", 508 | embold, 509 | col_reset); 510 | } else { 511 | fprintf(self->logfh, "Run Task: %s", self->task_string); 512 | } 513 | 514 | fflush(self->logfh); 515 | 516 | va_end(va); 517 | } 518 | 519 | /** 520 | * Let the user know that the task finished 521 | * 522 | * @param context Pointer to an allocated UscContext instance 523 | * @param success True if we're registering success, otherwise it's a failure. 524 | */ 525 | void usc_context_emit_task_finish(UscContext *self, UscHandlerStatus status) 526 | { 527 | static const char *labels[] = { 528 | [USC_HANDLER_SUCCESS] = "success", 529 | [USC_HANDLER_FAIL] = "failed", 530 | [USC_HANDLER_SKIP] = "skipped", 531 | }; 532 | static const char *status_marks[] = { 533 | [USC_HANDLER_SUCCESS] = "✓", 534 | [USC_HANDLER_FAIL] = "✗", 535 | [USC_HANDLER_SKIP] = " ", 536 | }; 537 | static const char *colors[] = { 538 | [USC_HANDLER_SUCCESS] = "\x1b[0;36m", 539 | [USC_HANDLER_FAIL] = "\x1b[1;31m", 540 | [USC_HANDLER_SKIP] = "\x1b[1;37m", 541 | }; 542 | static const char *col_reset = "\x1b[0m"; 543 | int local_offset = 5; 544 | 545 | /* Just make sure the old string really gets wiped */ 546 | for (size_t i = 0; i < strlen("running"); i++) { 547 | fputc('\b', stdout); 548 | } 549 | 550 | local_offset += (int)strlen(labels[status]); 551 | 552 | /* Rewind the string and update the current status */ 553 | if (self->have_tty) { 554 | fprintf(self->logfh, 555 | "\r [%s%s%s] %s%*s%s%s%s\n", 556 | colors[status], 557 | status_marks[status], 558 | col_reset, 559 | self->task_string, 560 | 79 - self->task_print_offset - local_offset, 561 | " ", 562 | colors[status], 563 | labels[status], 564 | col_reset); 565 | } else { 566 | fprintf(self->logfh, 567 | "%*s%s\n", 568 | 79 - self->task_print_offset - local_offset, 569 | " ", 570 | labels[status]); 571 | fflush(self->logfh); 572 | } 573 | } 574 | 575 | /* 576 | * Editor modelines - https://www.wireshark.org/tools/modelines.html 577 | * 578 | * Local variables: 579 | * c-basic-offset: 8 580 | * tab-width: 8 581 | * indent-tabs-mode: nil 582 | * End: 583 | * 584 | * vi: set shiftwidth=8 tabstop=8 expandtab: 585 | * :indentSize=8:tabSize=8:noTabs=true: 586 | */ 587 | --------------------------------------------------------------------------------