]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/operation.c
Merge pull request #5960 from keszybz/journald-memleak
[thirdparty/systemd.git] / src / machine / operation.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2016 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/wait.h>
21
22 #include "alloc-util.h"
23 #include "fd-util.h"
24 #include "operation.h"
25 #include "process-util.h"
26
27 static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdata) {
28 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
29 Operation *o = userdata;
30 int r;
31
32 assert(o);
33 assert(si);
34
35 log_debug("Operating " PID_FMT " is now complete with code=%s status=%i",
36 o->pid,
37 sigchld_code_to_string(si->si_code), si->si_status);
38
39 o->pid = 0;
40
41 if (si->si_code != CLD_EXITED) {
42 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
43 goto fail;
44 }
45
46 if (si->si_status == EXIT_SUCCESS)
47 r = 0;
48 else if (read(o->errno_fd, &r, sizeof(r)) != sizeof(r)) { /* Try to acquire error code for failed operation */
49 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child failed.");
50 goto fail;
51 }
52
53 if (o->done) {
54 /* A completion routine is set for this operation, call it. */
55 r = o->done(o, r, &error);
56 if (r < 0) {
57 if (!sd_bus_error_is_set(&error))
58 sd_bus_error_set_errno(&error, r);
59
60 goto fail;
61 }
62
63 } else {
64 /* The default operation when done is to simply return an error on failure or an empty success
65 * message on success. */
66 if (r < 0) {
67 sd_bus_error_set_errno(&error, r);
68 goto fail;
69 }
70
71 r = sd_bus_reply_method_return(o->message, NULL);
72 if (r < 0)
73 log_error_errno(r, "Failed to reply to message: %m");
74 }
75
76 operation_free(o);
77 return 0;
78
79 fail:
80 r = sd_bus_reply_method_error(o->message, &error);
81 if (r < 0)
82 log_error_errno(r, "Failed to reply to message: %m");
83
84 operation_free(o);
85 return 0;
86 }
87
88 int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, int errno_fd, Operation **ret) {
89 Operation *o;
90 int r;
91
92 assert(manager);
93 assert(child > 1);
94 assert(message);
95 assert(errno_fd >= 0);
96
97 o = new0(Operation, 1);
98 if (!o)
99 return -ENOMEM;
100
101 o->extra_fd = -1;
102
103 r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
104 if (r < 0) {
105 free(o);
106 return r;
107 }
108
109 o->pid = child;
110 o->message = sd_bus_message_ref(message);
111 o->errno_fd = errno_fd;
112
113 LIST_PREPEND(operations, manager->operations, o);
114 manager->n_operations++;
115 o->manager = manager;
116
117 if (machine) {
118 LIST_PREPEND(operations_by_machine, machine->operations, o);
119 o->machine = machine;
120 }
121
122 log_debug("Started new operation " PID_FMT ".", child);
123
124 /* At this point we took ownership of both the child and the errno file descriptor! */
125
126 if (ret)
127 *ret = o;
128
129 return 0;
130 }
131
132 Operation *operation_free(Operation *o) {
133 if (!o)
134 return NULL;
135
136 sd_event_source_unref(o->event_source);
137
138 safe_close(o->errno_fd);
139 safe_close(o->extra_fd);
140
141 if (o->pid > 1)
142 (void) sigkill_wait(o->pid);
143
144 sd_bus_message_unref(o->message);
145
146 if (o->manager) {
147 LIST_REMOVE(operations, o->manager->operations, o);
148 o->manager->n_operations--;
149 }
150
151 if (o->machine)
152 LIST_REMOVE(operations_by_machine, o->machine->operations, o);
153
154 return mfree(o);
155 }