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