bool is_clean_exit_lsb(int code, int status, ExitStatusSet *success_status) {

        if (is_clean_exit(code, status, success_status))
                return true;

        return
                code == CLD_EXITED &&
                (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED);
}
예제 #2
0
int main(int argc, char *argv[]) {
    int ret = EXIT_FAILURE;
    _cleanup_endmntent_ FILE *f = NULL;
    struct mntent* me;
    Hashmap *pids = NULL;

    if (argc > 1) {
        log_error("This program takes no argument.");
        return EXIT_FAILURE;
    }

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

    umask(0022);

    f = setmntent("/etc/fstab", "r");
    if (!f) {
        if (errno == ENOENT)
            return EXIT_SUCCESS;

        log_error("Failed to open /etc/fstab: %m");
        return EXIT_FAILURE;
    }

    pids = hashmap_new(trivial_hash_func, trivial_compare_func);
    if (!pids) {
        log_error("Failed to allocate set");
        goto finish;
    }

    ret = EXIT_SUCCESS;

    while ((me = getmntent(f))) {
        pid_t pid;
        int k;
        char *s;

        /* Remount the root fs, /usr and all API VFS */
        if (!mount_point_is_api(me->mnt_dir) &&
                !path_equal(me->mnt_dir, "/") &&
                !path_equal(me->mnt_dir, "/usr"))
            continue;

        log_debug("Remounting %s", me->mnt_dir);

        pid = fork();
        if (pid < 0) {
            log_error("Failed to fork: %m");
            ret = EXIT_FAILURE;
            continue;
        }

        if (pid == 0) {
            const char *arguments[5];
            /* Child */

            arguments[0] = "/bin/mount";
            arguments[1] = me->mnt_dir;
            arguments[2] = "-o";
            arguments[3] = "remount";
            arguments[4] = NULL;

            execv("/bin/mount", (char **) arguments);

            log_error("Failed to execute /bin/mount: %m");
            _exit(EXIT_FAILURE);
        }

        /* Parent */

        s = strdup(me->mnt_dir);
        if (!s) {
            log_oom();
            ret = EXIT_FAILURE;
            continue;
        }


        k = hashmap_put(pids, UINT_TO_PTR(pid), s);
        if (k < 0) {
            log_error("Failed to add PID to set: %s", strerror(-k));
            ret = EXIT_FAILURE;
            continue;
        }
    }

    while (!hashmap_isempty(pids)) {
        siginfo_t si = {};
        char *s;

        if (waitid(P_ALL, 0, &si, WEXITED) < 0) {

            if (errno == EINTR)
                continue;

            log_error("waitid() failed: %m");
            ret = EXIT_FAILURE;
            break;
        }

        s = hashmap_remove(pids, UINT_TO_PTR(si.si_pid));
        if (s) {
            if (!is_clean_exit(si.si_code, si.si_status, NULL)) {
                if (si.si_code == CLD_EXITED)
                    log_error("/bin/mount for %s exited with exit status %i.", s, si.si_status);
                else
                    log_error("/bin/mount for %s terminated by signal %s.", s, signal_to_string(si.si_status));

                ret = EXIT_FAILURE;
            }

            free(s);
        }
    }

finish:

    if (pids)
        hashmap_free_free(pids);

    return ret;
}
static int busname_peek_message(BusName *n) {
        struct kdbus_cmd_recv cmd_recv = {
                .size = sizeof(cmd_recv),
                .flags = KDBUS_RECV_PEEK,
        };
        struct kdbus_cmd_free cmd_free = {
                .size = sizeof(cmd_free),
        };
        const char *comm = NULL;
        struct kdbus_item *d;
        struct kdbus_msg *k;
        size_t start, ps, sz, delta;
        void *p = NULL;
        pid_t pid = 0;
        int r;

        /* Generate a friendly debug log message about which process
         * caused triggering of this bus name. This simply peeks the
         * metadata of the first queued message and logs it. */

        assert(n);

        /* Let's shortcut things a bit, if debug logging is turned off
         * anyway. */

        if (log_get_max_level() < LOG_DEBUG)
                return 0;

        r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv);
        if (r < 0) {
                if (errno == EINTR || errno == EAGAIN)
                        return 0;

                log_unit_error(UNIT(n)->id, "%s: Failed to query activation message: %m", UNIT(n)->id);
                return -errno;
        }

        /* We map as late as possible, and unmap imemdiately after
         * use. On 32bit address space is scarce and we want to be
         * able to handle a lot of activator connections at the same
         * time, and hence shouldn't keep the mmap()s around for
         * longer than necessary. */

        ps = page_size();
        start = (cmd_recv.msg.offset / ps) * ps;
        delta = cmd_recv.msg.offset - start;
        sz = PAGE_ALIGN(delta + cmd_recv.msg.msg_size);

        p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
        if (p == MAP_FAILED) {
                log_unit_error(UNIT(n)->id, "%s: Failed to map activation message: %m", UNIT(n)->id);
                r = -errno;
                goto finish;
        }

        k = (struct kdbus_msg *) ((uint8_t *) p + delta);
        KDBUS_ITEM_FOREACH(d, k, items) {
                switch (d->type) {

                case KDBUS_ITEM_PIDS:
                        pid = d->pids.pid;
                        break;

                case KDBUS_ITEM_PID_COMM:
                        comm = d->str;
                        break;
                }
        }

        if (pid > 0)
                log_unit_debug(UNIT(n)->id, "%s: Activation triggered by process " PID_FMT " (%s)", UNIT(n)->id, pid, strna(comm));

        r = 0;

finish:
        if (p)
                (void) munmap(p, sz);

        cmd_free.offset = cmd_recv.msg.offset;
        if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
                log_unit_warning(UNIT(n)->id, "Failed to free peeked message, ignoring: %m");

        return r;
}

static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
        BusName *n = userdata;

        assert(n);
        assert(fd >= 0);

        if (n->state != BUSNAME_LISTENING)
                return 0;

        log_unit_debug(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);

        if (revents != EPOLLIN) {
                log_unit_error(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
                               UNIT(n)->id, revents);
                goto fail;
        }

        busname_peek_message(n);
        busname_enter_running(n);
        return 0;
fail:

        busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
        return 0;
}

static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
        BusName *n = BUSNAME(u);
        BusNameResult f;

        assert(n);
        assert(pid >= 0);

        if (pid != n->control_pid)
                return;

        n->control_pid = 0;

        if (is_clean_exit(code, status, NULL))
                f = BUSNAME_SUCCESS;
        else if (code == CLD_EXITED)
                f = BUSNAME_FAILURE_EXIT_CODE;
        else if (code == CLD_KILLED)
                f = BUSNAME_FAILURE_SIGNAL;
        else if (code == CLD_DUMPED)
                f = BUSNAME_FAILURE_CORE_DUMP;
        else
                assert_not_reached("Unknown sigchld code");

        log_unit_full(u->id,
                      f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
                      "%s control process exited, code=%s status=%i",
                      u->id, sigchld_code_to_string(code), status);

        if (f != BUSNAME_SUCCESS)
                n->result = f;

        switch (n->state) {

        case BUSNAME_MAKING:
                if (f == BUSNAME_SUCCESS)
                        busname_enter_listening(n);
                else
                        busname_enter_signal(n, BUSNAME_SIGTERM, f);
                break;

        case BUSNAME_SIGTERM:
        case BUSNAME_SIGKILL:
                busname_enter_dead(n, f);
                break;

        default:
                assert_not_reached("Uh, control process died at wrong time.");
        }

        /* Notify clients about changed exit status */
        unit_add_to_dbus_queue(u);
}
예제 #4
0
int main(int argc, char *argv[]) {
        _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
        _cleanup_endmntent_ FILE *f = NULL;
        struct mntent* me;
        int r;

        if (argc > 1) {
                log_error("This program takes no argument.");
                return EXIT_FAILURE;
        }

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

        umask(0022);

        f = setmntent("/etc/fstab", "r");
        if (!f) {
                if (errno == ENOENT) {
                        r = 0;
                        goto finish;
                }

                r = log_error_errno(errno, "Failed to open /etc/fstab: %m");
                goto finish;
        }

        pids = hashmap_new(NULL);
        if (!pids) {
                r = log_oom();
                goto finish;
        }

        while ((me = getmntent(f))) {
                pid_t pid;
                int k;
                char *s;

                /* Remount the root fs, /usr and all API VFS */
                if (!mount_point_is_api(me->mnt_dir) &&
                    !path_equal(me->mnt_dir, "/") &&
                    !path_equal(me->mnt_dir, "/usr"))
                        continue;

                log_debug("Remounting %s", me->mnt_dir);

                pid = fork();
                if (pid < 0) {
                        r = log_error_errno(errno, "Failed to fork: %m");
                        goto finish;
                }

                if (pid == 0) {
                        /* Child */

                        (void) reset_all_signal_handlers();
                        (void) reset_signal_mask();
                        (void) prctl(PR_SET_PDEATHSIG, SIGTERM);

                        execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));

                        log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
                        _exit(EXIT_FAILURE);
                }

                /* Parent */

                s = strdup(me->mnt_dir);
                if (!s) {
                        r = log_oom();
                        goto finish;
                }

                k = hashmap_put(pids, PID_TO_PTR(pid), s);
                if (k < 0) {
                        free(s);
                        r = log_oom();
                        goto finish;
                }
        }

        r = 0;
        while (!hashmap_isempty(pids)) {
                siginfo_t si = {};
                char *s;

                if (waitid(P_ALL, 0, &si, WEXITED) < 0) {

                        if (errno == EINTR)
                                continue;

                        r = log_error_errno(errno, "waitid() failed: %m");
                        goto finish;
                }

                s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
                if (s) {
                        if (!is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
                                if (si.si_code == CLD_EXITED)
                                        log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
                                else
                                        log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));

                                r = -ENOEXEC;
                        }

                        free(s);
                }
        }

finish:
        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}