]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/portable/portabled-operation.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / portable / portabled-operation.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "fd-util.h"
7 #include "portabled-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 = ASSERT_PTR(userdata);
13 int r;
14
15 assert(si);
16
17 log_debug("Operating " PID_FMT " is now complete with code=%s status=%i",
18 o->pid,
19 sigchld_code_to_string(si->si_code), si->si_status);
20
21 o->pid = 0;
22
23 if (si->si_code != CLD_EXITED) {
24 r = sd_bus_error_set(&error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
25 goto fail;
26 }
27
28 if (si->si_status == EXIT_SUCCESS)
29 r = 0;
30 else if (read(o->errno_fd, &r, sizeof(r)) != sizeof(r)) { /* Try to acquire error code for failed operation */
31 r = sd_bus_error_set(&error, SD_BUS_ERROR_FAILED, "Child failed.");
32 goto fail;
33 }
34
35 if (o->done) {
36 /* A completion routine is set for this operation, call it. */
37 r = o->done(o, r, &error);
38 if (r < 0) {
39 if (!sd_bus_error_is_set(&error))
40 sd_bus_error_set_errno(&error, r);
41
42 goto fail;
43 }
44
45 } else {
46 /* The default operation when done is to simply return an error on failure or an empty success
47 * message on success. */
48 if (r < 0) {
49 sd_bus_error_set_errno(&error, r);
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
58 operation_free(o);
59 return 0;
60
61 fail:
62 r = sd_bus_reply_method_error(o->message, &error);
63 if (r < 0)
64 log_error_errno(r, "Failed to reply to message: %m");
65
66 operation_free(o);
67 return 0;
68 }
69
70 int operation_new(Manager *manager, pid_t child, sd_bus_message *message, int errno_fd, Operation **ret) {
71 Operation *o;
72 int r;
73
74 assert(manager);
75 assert(child > 1);
76 assert(message);
77 assert(errno_fd >= 0);
78
79 o = new0(Operation, 1);
80 if (!o)
81 return -ENOMEM;
82
83 o->extra_fd = -1;
84
85 r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
86 if (r < 0) {
87 free(o);
88 return r;
89 }
90
91 o->pid = child;
92 o->message = sd_bus_message_ref(message);
93 o->errno_fd = errno_fd;
94
95 LIST_PREPEND(operations, manager->operations, o);
96 manager->n_operations++;
97 o->manager = manager;
98
99 log_debug("Started new operation " PID_FMT ".", child);
100
101 /* At this point we took ownership of both the child and the errno file descriptor! */
102
103 if (ret)
104 *ret = o;
105
106 return 0;
107 }
108
109 Operation *operation_free(Operation *o) {
110 if (!o)
111 return NULL;
112
113 sd_event_source_unref(o->event_source);
114
115 safe_close(o->errno_fd);
116 safe_close(o->extra_fd);
117
118 if (o->pid > 1)
119 (void) sigkill_wait(o->pid);
120
121 sd_bus_message_unref(o->message);
122
123 if (o->manager) {
124 LIST_REMOVE(operations, o->manager->operations, o);
125 o->manager->n_operations--;
126 }
127
128 return mfree(o);
129 }