Example #1
0
int main(int argc, char*argv[]) {

        if (argc != 2) {
                log_error("This program requires one argument.");
                return EXIT_FAILURE;
        }

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        umask(0022);

        mac_selinux_init(NULL);

        if (streq(argv[1], "start")) {
                int r = 0;

                if (unlink("/run/nologin") < 0 && errno != ENOENT) {
                        log_error_errno(errno, "Failed to remove /run/nologin file: %m");
                        r = -errno;
                }

                if (unlink("/etc/nologin") < 0 && errno != ENOENT) {
                        /* If the file doesn't exist and /etc simply
                         * was read-only (in which case unlink()
                         * returns EROFS even if the file doesn't
                         * exist), don't complain */

                        if (errno != EROFS || access("/etc/nologin", F_OK) >= 0) {
                                log_error_errno(errno, "Failed to remove /etc/nologin file: %m");
                                return EXIT_FAILURE;
                        }
                }

                if (r < 0)
                        return EXIT_FAILURE;

        } else if (streq(argv[1], "stop")) {
                int r;

                r = write_string_file_atomic_label("/run/nologin", "System is going down.");
                if (r < 0) {
                        log_error_errno(r, "Failed to create /run/nologin: %m");
                        return EXIT_FAILURE;
                }

        } else {
                log_error("Unknown verb %s.", argv[1]);
                return EXIT_FAILURE;
        }

        mac_selinux_finish();

        return EXIT_SUCCESS;
}
Example #2
0
static int context_write_data_static_hostname(Context *c) {

    assert(c);

    if (isempty(c->data[PROP_STATIC_HOSTNAME])) {

        if (unlink("/etc/hostname") < 0)
            return errno == ENOENT ? 0 : -errno;

        return 0;
    }
    return write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]);
}
Example #3
0
int create_shutdown_run_nologin_or_warn(void) {
        int r;

        /* This is used twice: once in systemd-user-sessions.service, in order to block logins when we actually go
         * down, and once in systemd-logind.service when shutdowns are scheduled, and logins are to be turned off a bit
         * in advance. We use the same wording of the message in both cases. */

        r = write_string_file_atomic_label("/run/nologin",
                                           "System is going down. Unprivileged users are not permitted to log in anymore. "
                                           "For technical details, see pam_nologin(8).");
        if (r < 0)
                return log_error_errno(r, "Failed to create /run/nologin: %m");

        return 0;
}
Example #4
0
int write_drop_in(const char *dir, const char *unit, unsigned level,
                  const char *name, const char *data) {

        _cleanup_free_ char *p = NULL, *q = NULL;
        int r;

        assert(dir);
        assert(unit);
        assert(name);
        assert(data);

        r = drop_in_file(dir, unit, level, name, &p, &q);
        if (r < 0)
                return r;

        (void) mkdir_p(p, 0755);
        return write_string_file_atomic_label(q, data);
}