summaryrefslogtreecommitdiffstats
path: root/src/plugins/supervisor.c
blob: e3c71c065ef1d36f3b90419fc99199e5a5807ca0 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2018 "IoT.bzh"
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "ctl-plugin.h"
#include "supervisor-api.h"
#include "supervisor.h"
#include "wrap-json.h"

struct afb_cred {
    int refcount;
    uid_t uid;
    gid_t gid;
    pid_t pid;
    const char* user;
    const char* label;
    const char* id;
};

static const char* null_str = "null";

struct decode_daemon_str {
    DAEMONS_T* daemons;
    AFB_ApiT api;
    const char* ignored_daemon;
    int* ret_code;
};

static void decode_daemons_cb(void* closure, json_object* obj, const char* fName)
{
    int rc;
    struct afb_cred cred;
    json_object *j_response, *j_query, *j_config, *j_ws_servers, *j_ws_clients;
    json_object *j_name, *j_apis;
    struct decode_daemon_str* clStr = (struct decode_daemon_str*)closure;
    DAEMON_T* daemon = calloc(sizeof(DAEMON_T), 1);

    if (!clStr->daemons)
        return;

    if ((rc = wrap_json_unpack(obj, "{si si si ss ss ss}", "pid", &cred.pid,
             "uid", &cred.uid, "gid", &cred.gid, "id", &cred.id,
             "label", &cred.label, "user", &cred.user))
        < 0) {
        // TODO
        return;
    }

    AFB_ApiInfo(clStr->api, "Get supervisor/config - pid %d", cred.pid);
    daemon->pid = cred.pid;

    // Get config
    wrap_json_pack(&j_query, "{s:i}", "pid", cred.pid);
    rc = AFB_ServiceSync(clStr->api, SRV_SUPERVISOR_NAME, "config", j_query, &j_response);
    if (rc < 0) {
        AFB_ApiError(clStr->api, "Cannot get config of pid %d", cred.pid);
        *clStr->ret_code = rc;
        return;
    }

    AFB_ApiDebug(clStr->api, "%s config result, res=%s", SRV_SUPERVISOR_NAME,
        json_object_to_json_string(j_response));

    if (json_object_object_get_ex(j_response, "response", &j_config)) {
        // FIXME : implement free
        daemon->config = j_config;

        rc = wrap_json_unpack(j_config, "{s:o s:o s:o}",
            "name", &j_name,
            "ws_servers", &j_ws_servers,
            "ws_clients", &j_ws_clients);
        if (rc < 0) {
            AFB_ApiError(clStr->api, "Error decoding config response %s", wrap_json_get_error_string(rc));
            return;
        }

        daemon->name = json_object_is_type(j_name, json_type_null) ? null_str : json_object_get_string(j_name);

        // ignored some daemon
        if (clStr->ignored_daemon != NULL && strstr(daemon->name, clStr->ignored_daemon) != NULL) {
            free(daemon);
            return;
        }

        daemon->ws_servers = j_ws_servers;
        daemon->isServer = (json_object_array_length(j_ws_servers) > 0);
        daemon->ws_clients = j_ws_clients;
        daemon->isClient = (json_object_array_length(j_ws_clients) > 0);
    }

    // Get apis
    AFB_ApiInfo(clStr->api, "Get supervisor/do monitor get apis - pid %d", cred.pid);

    // '{"pid":6262,"api":"monitor","verb":"get","args":{"apis":true}}
    wrap_json_pack(&j_query, "{si ss ss s {sb}}",
        "pid", cred.pid,
        "api", "monitor",
        "verb", "get",
        "args", "apis", true);
    rc = AFB_ServiceSync(clStr->api, SRV_SUPERVISOR_NAME, "do", j_query, &j_response);
    if (rc < 0) {
        AFB_ApiError(clStr->api, "Cannot get apis of pid %d", cred.pid);
        return;
    } else {
        AFB_ApiDebug(clStr->api, "%s do ...get apis result, res=%s", SRV_SUPERVISOR_NAME, json_object_to_json_string(j_response));

        if (json_object_object_get_ex(j_response, "response", &j_config) && json_object_object_get_ex(j_config, "apis", &j_apis)) {
            // Don't forward monitor config details
            json_object_object_del(j_apis, "monitor");
            daemon->apis = j_apis;
        }
    }
    clStr->daemons->daemons[clStr->daemons->count] = daemon;
    clStr->daemons->count++;
}

int getDaemons(AFB_ApiT apiHandle, DAEMONS_T** daemons)
{
    int rc;
    json_object *j_response, *j_daemons = NULL;

    *daemons = calloc(sizeof(DAEMONS_T), 1);

    AFB_ApiInfo(apiHandle, "Call supervisor/discover");
    if ((rc = AFB_ServiceSync(apiHandle, SRV_SUPERVISOR_NAME, "discover", NULL,
             &j_response))
        < 0) {
        return rc;
    }

    AFB_ApiInfo(apiHandle, "Call supervisor/list");
    if ((rc = AFB_ServiceSync(apiHandle, SRV_SUPERVISOR_NAME, "list", NULL,
             &j_response))
        < 0) {
        return rc;
    }

    AFB_ApiInfo(apiHandle, "Get details info for each daemon");
    AFB_ApiDebug(apiHandle, "%s list result, res=%s", SRV_SUPERVISOR_NAME, json_object_to_json_string(j_response));

    if (!json_object_object_get_ex(j_response, "response", &j_daemons)) {
    }
    struct decode_daemon_str cls = {
        *daemons,
        apiHandle,
        apiHandle->apiname,
        &rc
    };
    wrap_json_object_for_all(j_daemons, decode_daemons_cb, &cls);

    return rc;
}

#define XDS_TAG_REQUEST "xds:trace/request"
#define XDS_TAG_EVENT "xds:trace/event"
#define XDS_TRACE_NAME "xds-trace"

int trace_exchange(AFB_ApiT apiHandle, DAEMON_T* svr, DAEMON_T* cli, const char* level)
{
    int rc;
    json_object *j_response, *j_query, *j_tracereq, *j_traceevt;

    if (svr == NULL || cli == NULL) {
        return -1;
    }

    // First drop previous traces
    // monitor/trace({ "drop": { "tag": "trace/request" } })
    // Note: ignored error (expected 1st time/when no trace exist)
    wrap_json_pack(&j_query, "{s:i s:{s:[s s]}}", "pid", svr->pid,
        "drop", "tag", XDS_TAG_REQUEST, XDS_TAG_EVENT);
    AFB_ServiceSync(apiHandle, SRV_SUPERVISOR_NAME, "trace", j_query, &j_response);

    wrap_json_pack(&j_query, "{s:i s:{s:[s s]}}", "pid", cli->pid,
        "drop", "tag", XDS_TAG_REQUEST, XDS_TAG_EVENT);
    AFB_ServiceSync(apiHandle, SRV_SUPERVISOR_NAME, "trace", j_query, &j_response);

    j_tracereq = json_object_new_array();
    if (level && !strncmp(level, "all", 3)) {
        json_object_array_add(j_tracereq, json_object_new_string("all"));
    } else {
        json_object_array_add(j_tracereq, json_object_new_string("life"));
        json_object_array_add(j_tracereq, json_object_new_string("result"));
    }
    json_object_get(j_tracereq); // because use 2 times to configure both server and client

    j_traceevt = json_object_new_array();
    if (level && !strncmp(level, "all", 3)) {
        json_object_array_add(j_traceevt, json_object_new_string("all"));
    } else {
        json_object_array_add(j_traceevt, json_object_new_string("common"));
    }
    json_object_get(j_traceevt); // because use 2 times to configure both server and client

    // Configure trace for server daemon
    // request: monitor/trace({ "add": { "tag": "xds:trace/request", "name": "trace", "request": "all" } })
    wrap_json_pack(&j_query, "{s:i, s: [{s:s s:s s:o}, {s:s s:s s:o}] }",
        "pid", svr->pid, "add",
        "tag", XDS_TAG_REQUEST, "name", XDS_TRACE_NAME, "request", j_tracereq,
        "tag", XDS_TAG_EVENT, "name", XDS_TRACE_NAME, "event", j_traceevt);

    if ((rc = AFB_ServiceSync(apiHandle, SRV_SUPERVISOR_NAME, "trace", j_query, &j_response)) < 0) {
        AFB_ApiError(apiHandle, "ERROR trace %d result: %s", svr->pid,
            json_object_to_json_string(j_response));
        return rc;
    }

    // Configure trace for client daemon(s)
    // request: monitor/trace({ "pid": 1234, "add": { "event": "all" } })
    wrap_json_pack(&j_query, "{s:i, s: [{s:s s:s s:o}, {s:s s:s s:o}] }",
        "pid", cli->pid, "add",
        "tag", XDS_TAG_REQUEST, "name", XDS_TRACE_NAME, "request", j_tracereq,
        "tag", XDS_TAG_EVENT, "name", XDS_TRACE_NAME, "event", j_traceevt);

    if ((rc = AFB_ServiceSync(apiHandle, SRV_SUPERVISOR_NAME, "trace", j_query, &j_response)) < 0) {
        AFB_ApiError(apiHandle, "ERROR trace %d result: %s", cli->pid,
            json_object_to_json_string(j_response));
        return rc;
    }

    return 0;
}

int supervisor_init(void)
{
    return 0;
}