]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/operation.c
Merge pull request #3183 from crawford/preset-array
[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 "alloc-util.h"
21 #include "fd-util.h"
22 #include "operation.h"
23 #include "process-util.h"
24
25 static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdata) {
26 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
27 Operation *o = userdata;
28 int r;
29
30 assert(o);
31 assert(si);
32
33 log_debug("Operating " PID_FMT " is now complete with with code=%s status=%i",
34 o->pid,
35 sigchld_code_to_string(si->si_code), si->si_status);
36
37 o->pid = 0;
38
39 if (si->si_code != CLD_EXITED) {
40 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
41 goto fail;
42 }
43
44 if (si->si_status != EXIT_SUCCESS) {
45 if (read(o->errno_fd, &r, sizeof(r)) == sizeof(r))
46 r = sd_bus_error_set_errnof(&error, r, "%m");
47 else
48 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child failed.");
49
50 goto fail;
51 }
52
53 r = sd_bus_reply_method_return(o->message, NULL);
54 if (r < 0)
55 log_error_errno(r, "Failed to reply to message: %m");
56
57 operation_free(o);
58 return 0;
59
60 fail:
61 r = sd_bus_reply_method_error(o->message, &error);
62 if (r < 0)
63 log_error_errno(r, "Failed to reply to message: %m");
64
65 operation_free(o);
66 return 0;
67 }
68
69 int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, int errno_fd) {
70 Operation *o;
71 int r;
72
73 assert(manager);
74 assert(child > 1);
75 assert(message);
76 assert(errno_fd >= 0);
77
78 o = new0(Operation, 1);
79 if (!o)
80 return -ENOMEM;
81
82 r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
83 if (r < 0) {
84 free(o);
85 return r;
86 }
87
88 o->pid = child;
89 o->message = sd_bus_message_ref(message);
90 o->errno_fd = errno_fd;
91
92 LIST_PREPEND(operations, manager->operations, o);
93 manager->n_operations++;
94 o->manager = manager;
95
96 if (machine) {
97 LIST_PREPEND(operations_by_machine, machine->operations, o);
98 o->machine = machine;
99 }
100
101 log_debug("Started new operation " PID_FMT ".", child);
102
103 /* At this point we took ownership of both the child and the errno file descriptor! */
104
105 return 0;
106 }
107
108 Operation *operation_free(Operation *o) {
109 if (!o)
110 return NULL;
111
112 sd_event_source_unref(o->event_source);
113
114 safe_close(o->errno_fd);
115
116 if (o->pid > 1)
117 (void) sigkill_wait(o->pid);
118
119 sd_bus_message_unref(o->message);
120
121 if (o->manager) {
122 LIST_REMOVE(operations, o->manager->operations, o);
123 o->manager->n_operations--;
124 }
125
126 if (o->machine)
127 LIST_REMOVE(operations_by_machine, o->machine->operations, o);
128
129 free(o);
130 return NULL;
131 }