]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homed-operation.c
homed: make it easier to run multiple instances of homed
[thirdparty/systemd.git] / src / home / homed-operation.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "fd-util.h"
4 #include "homed-operation.h"
5
6 Operation *operation_new(OperationType type, sd_bus_message *m) {
7 Operation *o;
8
9 assert(type >= 0);
10 assert(type < _OPERATION_MAX);
11
12 o = new(Operation, 1);
13 if (!o)
14 return NULL;
15
16 *o = (Operation) {
17 .type = type,
18 .n_ref = 1,
19 .message = sd_bus_message_ref(m),
20 .send_fd = -1,
21 .result = -1,
22 };
23
24 return o;
25 }
26
27 static Operation *operation_free(Operation *o) {
28 int r;
29
30 if (!o)
31 return NULL;
32
33 if (o->message && o->result >= 0) {
34
35 if (o->result) {
36 /* Propagate success */
37 if (o->send_fd < 0)
38 r = sd_bus_reply_method_return(o->message, NULL);
39 else
40 r = sd_bus_reply_method_return(o->message, "h", o->send_fd);
41
42 } else {
43 /* Propagate failure */
44 if (sd_bus_error_is_set(&o->error))
45 r = sd_bus_reply_method_error(o->message, &o->error);
46 else
47 r = sd_bus_reply_method_errnof(o->message, o->ret, "Failed to execute operation: %m");
48 }
49 if (r < 0)
50 log_warning_errno(r, "Failed to reply to %s method call, ignoring: %m", sd_bus_message_get_member(o->message));
51 }
52
53 sd_bus_message_unref(o->message);
54 user_record_unref(o->secret);
55 safe_close(o->send_fd);
56 sd_bus_error_free(&o->error);
57
58 return mfree(o);
59 }
60
61 DEFINE_TRIVIAL_REF_UNREF_FUNC(Operation, operation, operation_free);
62
63 void operation_result(Operation *o, int ret, const sd_bus_error *error) {
64 assert(o);
65
66 if (ret >= 0)
67 o->result = true;
68 else {
69 o->ret = ret;
70
71 sd_bus_error_free(&o->error);
72 sd_bus_error_copy(&o->error, error);
73
74 o->result = false;
75 }
76 }