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