summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-mgr/app-manager/app_manager.c
blob: b27ee96eb12c69457e0d7107ccf2170f891c4f24 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "app_manager.h"
#include "app_manager_host.h"
#include "bh_platform.h"
#include "bi-inc/attr_container.h"
#include "event.h"
#include "watchdog.h"
#include "coap_ext.h"

/* Queue of app manager */
static bh_queue *g_app_mgr_queue;
static bool g_app_mgr_started;

void *
get_app_manager_queue()
{
    return g_app_mgr_queue;
}

void
app_manager_post_applets_update_event()
{
    module_data *m_data;
    attr_container_t *attr_cont;
    request_t msg;
    int num = 0, i = 0;
    char *url = "/applets";

    if (!event_is_registered(url))
        return;

    if (!(attr_cont = attr_container_create("All Applets"))) {
        app_manager_printf("Post applets update event failed: "
                           "allocate memory failed.");
        return;
    }

    os_mutex_lock(&module_data_list_lock);

    m_data = module_data_list;
    while (m_data) {
        num++;
        m_data = m_data->next;
    }

    if (!(attr_container_set_int(&attr_cont, "num", num))) {
        app_manager_printf("Post applets update event failed: "
                           "set attr container key failed.");
        goto fail;
    }

    m_data = module_data_list;
    while (m_data) {
        char buf[32];
        i++;
        snprintf(buf, sizeof(buf), "%s%d", "applet", i);
        if (!(attr_container_set_string(&attr_cont, buf,
                                        m_data->module_name))) {
            app_manager_printf("Post applets update event failed: "
                               "set attr applet name key failed.");
            goto fail;
        }
        snprintf(buf, sizeof(buf), "%s%d", "heap", i);
        if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
            app_manager_printf("Post applets update event failed: "
                               "set attr heap key failed.");
            goto fail;
        }
        m_data = m_data->next;
    }

    memset(&msg, 0, sizeof(msg));
    msg.url = url;
    msg.action = COAP_EVENT;
    msg.payload = (char *)attr_cont;
    send_request_to_host(&msg);

    app_manager_printf("Post applets update event success!\n");
    attr_container_dump(attr_cont);

fail:
    os_mutex_unlock(&module_data_list_lock);
    attr_container_destroy(attr_cont);
}

static int
get_applets_count()
{
    module_data *m_data;
    int num = 0;

    os_mutex_lock(&module_data_list_lock);

    m_data = module_data_list;
    while (m_data) {
        num++;
        m_data = m_data->next;
    }

    os_mutex_unlock(&module_data_list_lock);

    return num;
}

/* Query fw apps info if name = NULL, otherwise query specify app */
static bool
app_manager_query_applets(request_t *msg, const char *name)
{
    module_data *m_data;
    attr_container_t *attr_cont;
    int num = 0, i = 0, len;
    bool ret = false, found = false;
    response_t response[1] = { 0 };

    attr_cont = attr_container_create("Applets Info");
    if (!attr_cont) {
        SEND_ERR_RESPONSE(msg->mid,
                          "Query Applets failed: allocate memory failed.");
        return false;
    }

    os_mutex_lock(&module_data_list_lock);

    m_data = module_data_list;
    while (m_data) {
        num++;
        m_data = m_data->next;
    }

    if (name == NULL && !(attr_container_set_int(&attr_cont, "num", num))) {
        SEND_ERR_RESPONSE(
            msg->mid, "Query Applets failed: set attr container key failed.");
        goto fail;
    }

    m_data = module_data_list;
    while (m_data) {
        char buf[32];

        if (name == NULL) {
            i++;
            snprintf(buf, sizeof(buf), "%s%d", "applet", i);
            if (!(attr_container_set_string(&attr_cont, buf,
                                            m_data->module_name))) {
                SEND_ERR_RESPONSE(msg->mid, "Query Applets failed: "
                                            "set attr container key failed.");
                goto fail;
            }
            snprintf(buf, sizeof(buf), "%s%d", "heap", i);
            if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
                SEND_ERR_RESPONSE(msg->mid,
                                  "Query Applets failed: "
                                  "set attr container heap key failed.");
                goto fail;
            }
        }
        else if (!strcmp(name, m_data->module_name)) {
            found = true;
            if (!(attr_container_set_string(&attr_cont, "name",
                                            m_data->module_name))) {
                SEND_ERR_RESPONSE(msg->mid, "Query Applet failed: "
                                            "set attr container key failed.");
                goto fail;
            }
            if (!(attr_container_set_int(&attr_cont, "heap",
                                         m_data->heap_size))) {
                SEND_ERR_RESPONSE(msg->mid,
                                  "Query Applet failed: "
                                  "set attr container heap key failed.");
                goto fail;
            }
        }

        m_data = m_data->next;
    }

    if (name != NULL && !found) {
        SEND_ERR_RESPONSE(msg->mid,
                          "Query Applet failed: the app is not found.");
        goto fail;
    }

    len = attr_container_get_serialize_length(attr_cont);

    make_response_for_request(msg, response);
    set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, (char *)attr_cont,
                 len);
    send_response_to_host(response);

    ret = true;
    app_manager_printf("Query Applets success!\n");
    attr_container_dump(attr_cont);

fail:
    os_mutex_unlock(&module_data_list_lock);
    attr_container_destroy(attr_cont);
    return ret;
}

void
applet_mgt_reqeust_handler(request_t *request, void *unused)
{
    bh_message_t msg;
    /* deep copy, but not use app self heap, but use global heap */
    request_t *req = clone_request(request);

    if (!req)
        return;

    msg = bh_new_msg(RESTFUL_REQUEST, req, sizeof(*req), request_cleaner);
    if (!msg) {
        request_cleaner(req);
        return;
    }

    bh_post_msg2(get_app_manager_queue(), msg);
}

/* return -1 for error */
static int
get_module_type(char *kv_str)
{
    int module_type = -1;
    char type_str[16] = { 0 };

    find_key_value(kv_str, strlen(kv_str), "type", type_str,
                   sizeof(type_str) - 1, '&');

    if (strlen(type_str) == 0)
        module_type = Module_WASM_App;
    else if (strcmp(type_str, "jeff") == 0)
        module_type = Module_Jeff;
    else if (strcmp(type_str, "wasm") == 0)
        module_type = Module_WASM_App;
    else if (strcmp(type_str, "wasmlib") == 0)
        module_type = Module_WASM_Lib;

    return module_type;
}

#define APP_NAME_MAX_LEN 128

/* Queue callback of App Manager */

static void
app_manager_queue_callback(void *message, void *arg)
{
    request_t *request = (request_t *)bh_message_payload((bh_message_t)message);
    int mid = request->mid, module_type, offset;

    (void)arg;

    if ((offset =
             check_url_start(request->url, strlen(request->url), "/applet"))
        > 0) {
        module_type = get_module_type(request->url + offset);

        if (module_type == -1) {
            SEND_ERR_RESPONSE(mid,
                              "Applet Management failed: invalid module type.");
            goto fail;
        }

        /* Install Applet */
        if (request->action == COAP_PUT) {
            if (get_applets_count() >= MAX_APP_INSTALLATIONS) {
                SEND_ERR_RESPONSE(
                    mid,
                    "Install Applet failed: exceed max app installations.");
                goto fail;
            }

            if (!request->payload) {
                SEND_ERR_RESPONSE(mid,
                                  "Install Applet failed: invalid payload.");
                goto fail;
            }
            if (g_module_interfaces[module_type]
                && g_module_interfaces[module_type]->module_install) {
                if (!g_module_interfaces[module_type]->module_install(request))
                    goto fail;
            }
        }
        /* Uninstall Applet */
        else if (request->action == COAP_DELETE) {
            module_type = get_module_type(request->url + offset);
            if (module_type == -1) {
                SEND_ERR_RESPONSE(
                    mid, "Uninstall Applet failed: invalid module type.");
                goto fail;
            }

            if (g_module_interfaces[module_type]
                && g_module_interfaces[module_type]->module_uninstall) {
                if (!g_module_interfaces[module_type]->module_uninstall(
                        request))
                    goto fail;
            }
        }
        /* Query Applets installed */
        else if (request->action == COAP_GET) {
            char name[APP_NAME_MAX_LEN] = { 0 };
            char *properties = request->url + offset;
            find_key_value(properties, strlen(properties), "name", name,
                           sizeof(name) - 1, '&');
            if (strlen(name) > 0)
                app_manager_query_applets(request, name);
            else
                app_manager_query_applets(request, NULL);
        }
        else {
            SEND_ERR_RESPONSE(mid, "Invalid request of applet: invalid action");
        }
    }
    /* Event Register/Unregister */
    else if ((offset = check_url_start(request->url, strlen(request->url),
                                       "/event/"))
             > 0) {
        char url_buf[256] = { 0 };

        strncpy(url_buf, request->url + offset, sizeof(url_buf) - 1);

        if (!event_handle_event_request(request->action, url_buf, ID_HOST)) {
            SEND_ERR_RESPONSE(mid, "Handle event request failed.");
            goto fail;
        }
        send_error_response_to_host(mid, CONTENT_2_05, NULL); /* OK */
    }
    else {
        int i;
        for (i = 0; i < Module_Max; i++) {
            if (g_module_interfaces[i]
                && g_module_interfaces[i]->module_handle_host_url) {
                if (g_module_interfaces[i]->module_handle_host_url(request))
                    break;
            }
        }
    }

fail:
    return;
}

static void
module_interfaces_init()
{
    int i;
    for (i = 0; i < Module_Max; i++) {
        if (g_module_interfaces[i] && g_module_interfaces[i]->module_init)
            g_module_interfaces[i]->module_init();
    }
}

void
app_manager_startup(host_interface *interface)
{
    module_interfaces_init();

    /* Create queue of App Manager */
    g_app_mgr_queue = bh_queue_create();
    if (!g_app_mgr_queue)
        return;

    if (!module_data_list_init())
        goto fail1;

    if (!watchdog_startup())
        goto fail2;

    /* Initialize Host */
    app_manager_host_init(interface);

    am_register_resource("/app/", targeted_app_request_handler, ID_APP_MGR);

    /* /app/ and /event/ are both processed by applet_mgt_reqeust_handler */
    am_register_resource("/applet", applet_mgt_reqeust_handler, ID_APP_MGR);
    am_register_resource("/event/", applet_mgt_reqeust_handler, ID_APP_MGR);

    app_manager_printf("App Manager started.\n");

    g_app_mgr_started = true;

    /* Enter loop run */
    bh_queue_enter_loop_run(g_app_mgr_queue, app_manager_queue_callback, NULL);

    g_app_mgr_started = false;

    /* Destroy registered resources */
    am_cleanup_registeration(ID_APP_MGR);

    /* Destroy watchdog */
    watchdog_destroy();

fail2:
    module_data_list_destroy();

fail1:
    bh_queue_destroy(g_app_mgr_queue);
}

bool
app_manager_is_started(void)
{
    return g_app_mgr_started;
}

#include "module_config.h"

module_interface *g_module_interfaces[Module_Max] = {
#if ENABLE_MODULE_JEFF != 0
    &jeff_module_interface,
#else
    NULL,
#endif

#if ENABLE_MODULE_WASM_APP != 0
    &wasm_app_module_interface,
#else
    NULL,
#endif

#if ENABLE_MODULE_WASM_LIB != 0
    &wasm_lib_module_interface
#else
    NULL
#endif
};