summaryrefslogtreecommitdiffstats
path: root/src/machine/operation.c
blob: 4d3939496fbdd27b64a95b89cc155050f78d80e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <sys/wait.h>
#include <unistd.h>

#include "alloc-util.h"
#include "fd-util.h"
#include "operation.h"
#include "process-util.h"

static int operation_done_internal(const siginfo_t *si, Operation *o, sd_bus_error *error) {
        int r;

        assert(si);
        assert(o);

        if (si->si_code != CLD_EXITED)
                return log_debug_errno(SYNTHETIC_ERRNO(ESHUTDOWN), "Child died abnormally");

        if (si->si_status == EXIT_SUCCESS)
                r = 0;
        else {
                ssize_t n = read(o->errno_fd, &r, sizeof(r));
                if (n < 0)
                        return log_debug_errno(errno, "Failed to read operation's errno: %m");
                if (n != sizeof(r))
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpectedly short message when reading operation's errno");
        }

        if (o->done)
                /* A completion routine is set for this operation, call it. */
                return o->done(o, r, error);

        /* The default operation when done is to simply return an error on failure or an empty success
         * message on success. */
        if (r < 0)
                log_debug_errno(r, "Operation failed: %m");

        return r;
}

static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdata) {
        Operation *o = ASSERT_PTR(userdata);
        int r;

        assert(si);

        log_debug("Operation " PID_FMT " is now complete with code=%s status=%i",
                  o->pid,
                  sigchld_code_to_string(si->si_code), si->si_status);

        o->pid = 0;

        if (o->message) {
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;

                r = operation_done_internal(si, o, &error);
                if (r < 0) {
                        if (!sd_bus_error_is_set(&error))
                                sd_bus_error_set_errno(&error, r);

                        r = sd_bus_reply_method_error(o->message, &error);
                        if (r < 0)
                                log_error_errno(r, "Failed to reply to dbus message: %m");
                } else {
                        r = sd_bus_reply_method_return(o->message, NULL);
                        if (r < 0)
                                log_error_errno(r, "Failed to reply to dbus message: %m");
                }
        } else if (o->link) {
                r = operation_done_internal(si, o, /* error = */ NULL);
                if (r < 0)
                        (void) sd_varlink_error_errno(o->link, r);
                else
                        (void) sd_varlink_reply(o->link, NULL);
        } else
                assert_not_reached();

        operation_free(o);
        return 0;
}

int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, sd_varlink *link, int errno_fd, Operation **ret) {
        Operation *o;
        int r;

        assert(manager);
        assert(child > 1);
        assert(errno_fd >= 0);
        assert(message || link);
        assert(!(message && link));

        o = new0(Operation, 1);
        if (!o)
                return -ENOMEM;

        o->extra_fd = -EBADF;

        r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
        if (r < 0) {
                free(o);
                return r;
        }

        o->pid = child;
        o->message = sd_bus_message_ref(message);
        o->link = sd_varlink_ref(link);
        o->errno_fd = errno_fd;

        LIST_PREPEND(operations, manager->operations, o);
        manager->n_operations++;
        o->manager = manager;

        if (machine) {
                LIST_PREPEND(operations_by_machine, machine->operations, o);
                o->machine = machine;
        }

        log_debug("Started new operation " PID_FMT ".", child);

        /* At this point we took ownership of both the child and the errno file descriptor! */

        if (ret)
                *ret = o;

        return 0;
}

Operation *operation_free(Operation *o) {
        if (!o)
                return NULL;

        sd_event_source_unref(o->event_source);

        safe_close(o->errno_fd);
        safe_close(o->extra_fd);

        if (o->pid > 1)
                (void) sigkill_wait(o->pid);

        sd_bus_message_unref(o->message);
        sd_varlink_unref(o->link);

        if (o->manager) {
                LIST_REMOVE(operations, o->manager->operations, o);
                o->manager->n_operations--;
        }

        if (o->machine)
                LIST_REMOVE(operations_by_machine, o->machine->operations, o);

        return mfree(o);
}