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