]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/operation.c
757052d4ebad2ee942f6819611eb6f1b6b3e203a
[thirdparty/systemd.git] / src / machine / operation.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2016 Lennart Poettering
4 ***/
5
6 #include <sys/wait.h>
7
8 #include "alloc-util.h"
9 #include "fd-util.h"
10 #include "operation.h"
11 #include "process-util.h"
12
13 static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdata) {
14 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
15 Operation *o = userdata;
16 int r;
17
18 assert(o);
19 assert(si);
20
21 log_debug("Operating " PID_FMT " is now complete with code=%s status=%i",
22 o->pid,
23 sigchld_code_to_string(si->si_code), si->si_status);
24
25 o->pid = 0;
26
27 if (si->si_code != CLD_EXITED) {
28 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
29 goto fail;
30 }
31
32 if (si->si_status == EXIT_SUCCESS)
33 r = 0;
34 else if (read(o->errno_fd, &r, sizeof(r)) != sizeof(r)) { /* Try to acquire error code for failed operation */
35 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child failed.");
36 goto fail;
37 }
38
39 if (o->done) {
40 /* A completion routine is set for this operation, call it. */
41 r = o->done(o, r, &error);
42 if (r < 0) {
43 if (!sd_bus_error_is_set(&error))
44 sd_bus_error_set_errno(&error, r);
45
46 goto fail;
47 }
48
49 } else {
50 /* The default operation when done is to simply return an error on failure or an empty success
51 * message on success. */
52 if (r < 0) {
53 sd_bus_error_set_errno(&error, r);
54 goto fail;
55 }
56
57 r = sd_bus_reply_method_return(o->message, NULL);
58 if (r < 0)
59 log_error_errno(r, "Failed to reply to message: %m");
60 }
61
62 operation_free(o);
63 return 0;
64
65 fail:
66 r = sd_bus_reply_method_error(o->message, &error);
67 if (r < 0)
68 log_error_errno(r, "Failed to reply to message: %m");
69
70 operation_free(o);
71 return 0;
72 }
73
74 int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, int errno_fd, Operation **ret) {
75 Operation *o;
76 int r;
77
78 assert(manager);
79 assert(child > 1);
80 assert(message);
81 assert(errno_fd >= 0);
82
83 o = new0(Operation, 1);
84 if (!o)
85 return -ENOMEM;
86
87 o->extra_fd = -1;
88
89 r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
90 if (r < 0) {
91 free(o);
92 return r;
93 }
94
95 o->pid = child;
96 o->message = sd_bus_message_ref(message);
97 o->errno_fd = errno_fd;
98
99 LIST_PREPEND(operations, manager->operations, o);
100 manager->n_operations++;
101 o->manager = manager;
102
103 if (machine) {
104 LIST_PREPEND(operations_by_machine, machine->operations, o);
105 o->machine = machine;
106 }
107
108 log_debug("Started new operation " PID_FMT ".", child);
109
110 /* At this point we took ownership of both the child and the errno file descriptor! */
111
112 if (ret)
113 *ret = o;
114
115 return 0;
116 }
117
118 Operation *operation_free(Operation *o) {
119 if (!o)
120 return NULL;
121
122 sd_event_source_unref(o->event_source);
123
124 safe_close(o->errno_fd);
125 safe_close(o->extra_fd);
126
127 if (o->pid > 1)
128 (void) sigkill_wait(o->pid);
129
130 sd_bus_message_unref(o->message);
131
132 if (o->manager) {
133 LIST_REMOVE(operations, o->manager->operations, o);
134 o->manager->n_operations--;
135 }
136
137 if (o->machine)
138 LIST_REMOVE(operations_by_machine, o->machine->operations, o);
139
140 return mfree(o);
141 }