summaryrefslogtreecommitdiffstats
path: root/src/home/homed-operation.c
blob: 156a25ec816138908ce57bea6fc7825034eb758e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* SPDX-License-Identifier: LGPL-2.1+ */

#include "fd-util.h"
#include "homed-operation.h"

Operation *operation_new(OperationType type, sd_bus_message *m) {
        Operation *o;

        assert(type >= 0);
        assert(type < _OPERATION_MAX);

        o = new(Operation, 1);
        if (!o)
                return NULL;

        *o = (Operation) {
                .type = type,
                .n_ref = 1,
                .message = sd_bus_message_ref(m),
                .send_fd = -1,
                .result = -1,
        };

        return o;
}

static Operation *operation_free(Operation *o) {
        int r;

        if (!o)
                return NULL;

        if (o->message && o->result >= 0) {

                if (o->result) {
                        /* Propagate success */
                        if (o->send_fd < 0)
                                r = sd_bus_reply_method_return(o->message, NULL);
                        else
                                r = sd_bus_reply_method_return(o->message, "h", o->send_fd);

                } else {
                        /* Propagate failure */
                        if (sd_bus_error_is_set(&o->error))
                                r = sd_bus_reply_method_error(o->message, &o->error);
                        else
                                r = sd_bus_reply_method_errnof(o->message, o->ret, "Failed to execute operation: %m");
                }
                if (r < 0)
                        log_warning_errno(r, "Failed to reply to %s method call, ignoring: %m", sd_bus_message_get_member(o->message));
        }

        sd_bus_message_unref(o->message);
        user_record_unref(o->secret);
        safe_close(o->send_fd);
        sd_bus_error_free(&o->error);

        return mfree(o);
}

DEFINE_TRIVIAL_REF_UNREF_FUNC(Operation, operation, operation_free);

void operation_result(Operation *o, int ret, const sd_bus_error *error) {
        assert(o);

        if (ret >= 0)
                o->result = true;
        else {
                o->ret = ret;

                sd_bus_error_free(&o->error);
                sd_bus_error_copy(&o->error, error);

                o->result = false;
        }
}