]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/portable/portabled-operation.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
5 #include "portabled-operation.h"
6 #include "process-util.h"
8 static int operation_done(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
9 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
10 Operation
*o
= userdata
;
16 log_debug("Operating " PID_FMT
" is now complete with code=%s status=%i",
18 sigchld_code_to_string(si
->si_code
), si
->si_status
);
22 if (si
->si_code
!= CLD_EXITED
) {
23 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
27 if (si
->si_status
== EXIT_SUCCESS
)
29 else if (read(o
->errno_fd
, &r
, sizeof(r
)) != sizeof(r
)) { /* Try to acquire error code for failed operation */
30 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child failed.");
35 /* A completion routine is set for this operation, call it. */
36 r
= o
->done(o
, r
, &error
);
38 if (!sd_bus_error_is_set(&error
))
39 sd_bus_error_set_errno(&error
, r
);
45 /* The default operation when done is to simply return an error on failure or an empty success
46 * message on success. */
48 sd_bus_error_set_errno(&error
, r
);
52 r
= sd_bus_reply_method_return(o
->message
, NULL
);
54 log_error_errno(r
, "Failed to reply to message: %m");
61 r
= sd_bus_reply_method_error(o
->message
, &error
);
63 log_error_errno(r
, "Failed to reply to message: %m");
69 int operation_new(Manager
*manager
, pid_t child
, sd_bus_message
*message
, int errno_fd
, Operation
**ret
) {
76 assert(errno_fd
>= 0);
78 o
= new0(Operation
, 1);
84 r
= sd_event_add_child(manager
->event
, &o
->event_source
, child
, WEXITED
, operation_done
, o
);
91 o
->message
= sd_bus_message_ref(message
);
92 o
->errno_fd
= errno_fd
;
94 LIST_PREPEND(operations
, manager
->operations
, o
);
95 manager
->n_operations
++;
98 log_debug("Started new operation " PID_FMT
".", child
);
100 /* At this point we took ownership of both the child and the errno file descriptor! */
108 Operation
*operation_free(Operation
*o
) {
112 sd_event_source_unref(o
->event_source
);
114 safe_close(o
->errno_fd
);
115 safe_close(o
->extra_fd
);
118 (void) sigkill_wait(o
->pid
);
120 sd_bus_message_unref(o
->message
);
123 LIST_REMOVE(operations
, o
->manager
->operations
, o
);
124 o
->manager
->n_operations
--;