summaryrefslogtreecommitdiffstats
path: root/src/lib/sifp/sss_sifp_dbus.c
blob: d9813718b2201b37b0eb8a1e23c4773e4cdd50a4 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2014 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <dbus/dbus.h>
#include <stdlib.h>
#include <string.h>

#include "lib/sifp/sss_sifp.h"
#include "lib/sifp/sss_sifp_dbus.h"
#include "lib/sifp/sss_sifp_private.h"

static sss_sifp_error sss_sifp_ifp_call(sss_sifp_ctx *ctx,
                                        const char *method,
                                        int first_arg_type,
                                        va_list ap,
                                        DBusMessage **_reply)
{
   DBusMessage *msg = NULL;
   sss_sifp_error ret;

   msg = sss_sifp_create_message(SSS_SIFP_PATH_IFP, SSS_SIFP_IFACE_IFP, method);
   if (msg == NULL) {
       ret = SSS_SIFP_OUT_OF_MEMORY;
       goto done;
   }

   if (first_arg_type != DBUS_TYPE_INVALID) {
       dbus_message_append_args_valist(msg, first_arg_type, ap);
   }

   ret = sss_sifp_send_message(ctx, msg, _reply);

done:
   if (msg != NULL) {
       dbus_message_unref(msg);
   }

   return ret;
}

DBusMessage *
sss_sifp_create_message(const char *object_path,
                        const char *interface,
                        const char *method)
{
    return dbus_message_new_method_call(SSS_SIFP_IFP, object_path,
                                        interface, method);
}

sss_sifp_error
sss_sifp_send_message(sss_sifp_ctx *ctx,
                      DBusMessage *msg,
                      DBusMessage **_reply)
{
    return sss_sifp_send_message_ex(ctx, msg, 5000, _reply);
}

sss_sifp_error
sss_sifp_send_message_ex(sss_sifp_ctx *ctx,
                         DBusMessage *msg,
                         int timeout,
                         DBusMessage **_reply)
{
    DBusMessage *reply = NULL;
    DBusError dbus_error;
    sss_sifp_error ret;

    if (ctx == NULL || msg == NULL) {
        return SSS_SIFP_INVALID_ARGUMENT;
    }

    dbus_error_init(&dbus_error);

    reply = dbus_connection_send_with_reply_and_block(ctx->conn, msg,
                                                      timeout, &dbus_error);
    if (dbus_error_is_set(&dbus_error)) {
        sss_sifp_set_io_error(ctx, &dbus_error);
        ret = SSS_SIFP_IO_ERROR;
        goto done;
    }

    if (_reply == NULL) {
        dbus_message_unref(reply);
    } else {
        *_reply = reply;
    }

    ret = SSS_SIFP_OK;

done:
    dbus_error_free(&dbus_error);
    return ret;
}

sss_sifp_error
sss_sifp_invoke_list(sss_sifp_ctx *ctx,
                     const char *method,
                     char ***_object_paths,
                     int first_arg_type,
                     ...)
{
    DBusMessage *reply = NULL;
    char *dbus_method = NULL;
    sss_sifp_error ret;
    va_list ap;

    if (ctx == NULL || method == NULL || _object_paths == NULL) {
        return SSS_SIFP_INVALID_ARGUMENT;
    }

    dbus_method = sss_sifp_strcat(ctx, "List", method);
    if (dbus_method == NULL) {
        ret = SSS_SIFP_OUT_OF_MEMORY;
        goto done;
    }

    va_start(ap, first_arg_type);
    ret = sss_sifp_ifp_call(ctx, dbus_method, first_arg_type, ap, &reply);
    va_end(ap);
    if (ret != SSS_SIFP_OK) {
        goto done;
    }

    ret = sss_sifp_parse_object_path_list(ctx, reply, _object_paths);

done:
    sss_sifp_free_string(ctx, &dbus_method);

    if (reply != NULL) {
        dbus_message_unref(reply);
    }

    return ret;
}

sss_sifp_error
sss_sifp_invoke_find(sss_sifp_ctx *ctx,
                     const char *method,
                     char **_object_path,
                     int first_arg_type,
                     ...)
{
   DBusMessage *reply = NULL;
   char *dbus_method = NULL;
   sss_sifp_error ret;
   va_list ap;

   if (ctx == NULL || method == NULL || _object_path == NULL) {
       return SSS_SIFP_INVALID_ARGUMENT;
   }

   dbus_method = sss_sifp_strcat(ctx, "Find", method);
   if (dbus_method == NULL) {
       ret = SSS_SIFP_OUT_OF_MEMORY;
       goto done;
   }

   va_start(ap, first_arg_type);
   ret = sss_sifp_ifp_call(ctx, dbus_method, first_arg_type, ap, &reply);
   va_end(ap);
   if (ret != SSS_SIFP_OK) {
       goto done;
   }

   ret = sss_sifp_parse_object_path(ctx, reply, _object_path);

done:
    sss_sifp_free_string(ctx, &dbus_method);

   if (reply != NULL) {
       dbus_message_unref(reply);
   }

   return ret;
}