Debian Codegraph @

src/portable/portabled-operation.c

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