]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
56599585 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2016 Lennart Poettering | |
56599585 LP |
6 | ***/ |
7 | ||
fe993888 MS |
8 | #include <sys/wait.h> |
9 | ||
56599585 LP |
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 | ||
61233823 | 23 | log_debug("Operating " PID_FMT " is now complete with code=%s status=%i", |
56599585 LP |
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 | ||
03c2b288 LP |
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."); | |
56599585 LP |
38 | goto fail; |
39 | } | |
40 | ||
03c2b288 LP |
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 { | |
61233823 | 52 | /* The default operation when done is to simply return an error on failure or an empty success |
03c2b288 | 53 | * message on success. */ |
3aca8326 LP |
54 | if (r < 0) { |
55 | sd_bus_error_set_errno(&error, r); | |
03c2b288 | 56 | goto fail; |
3aca8326 | 57 | } |
03c2b288 LP |
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 | } | |
56599585 LP |
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 | ||
03c2b288 | 76 | int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, int errno_fd, Operation **ret) { |
56599585 LP |
77 | Operation *o; |
78 | int r; | |
79 | ||
795c5d31 LP |
80 | assert(manager); |
81 | assert(child > 1); | |
82 | assert(message); | |
83 | assert(errno_fd >= 0); | |
84 | ||
56599585 LP |
85 | o = new0(Operation, 1); |
86 | if (!o) | |
87 | return -ENOMEM; | |
88 | ||
03c2b288 LP |
89 | o->extra_fd = -1; |
90 | ||
795c5d31 | 91 | r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o); |
56599585 LP |
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 | ||
795c5d31 LP |
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 | } | |
56599585 LP |
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 | ||
03c2b288 LP |
114 | if (ret) |
115 | *ret = o; | |
116 | ||
56599585 LP |
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); | |
03c2b288 | 127 | safe_close(o->extra_fd); |
56599585 LP |
128 | |
129 | if (o->pid > 1) | |
89c9030d | 130 | (void) sigkill_wait(o->pid); |
56599585 LP |
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 | ||
795c5d31 LP |
139 | if (o->machine) |
140 | LIST_REMOVE(operations_by_machine, o->machine->operations, o); | |
141 | ||
6b430fdb | 142 | return mfree(o); |
56599585 | 143 | } |