add systemd support
[project/ubus.git] / lua / ubus.c
1 /*
2  * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
3  * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <unistd.h>
16 #include <libubus.h>
17 #include <libubox/blobmsg.h>
18 #include <libubox/blobmsg_json.h>
19 #include <lauxlib.h>
20
21
22 #define MODNAME        "ubus"
23 #define METANAME       MODNAME ".meta"
24
25 static lua_State *state;
26
27 struct ubus_lua_connection {
28         int timeout;
29         struct blob_buf buf;
30         struct ubus_context *ctx;
31 };
32
33 struct ubus_lua_object {
34         struct ubus_object o;
35         int r;
36 };
37
38 static int
39 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
40
41 static int
42 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, int len, bool table)
43 {
44         int rv;
45         int idx = 1;
46         int rem = len;
47         struct blob_attr *pos;
48
49         lua_newtable(L);
50
51         __blob_for_each_attr(pos, attr, rem)
52         {
53                 rv = ubus_lua_parse_blob(L, pos, table);
54
55                 if (rv > 1)
56                         lua_rawset(L, -3);
57                 else if (rv > 0)
58                         lua_rawseti(L, -2, idx++);
59         }
60
61         return 1;
62 }
63
64 static int
65 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
66 {
67         int len;
68         int off = 0;
69         void *data;
70
71         if (!blobmsg_check_attr(attr, false))
72                 return 0;
73
74         if (table && blobmsg_name(attr)[0])
75         {
76                 lua_pushstring(L, blobmsg_name(attr));
77                 off++;
78         }
79
80         data = blobmsg_data(attr);
81         len = blobmsg_data_len(attr);
82
83         switch (blob_id(attr))
84         {
85         case BLOBMSG_TYPE_BOOL:
86                 lua_pushboolean(L, *(uint8_t *)data);
87                 break;
88
89         case BLOBMSG_TYPE_INT16:
90                 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
91                 break;
92
93         case BLOBMSG_TYPE_INT32:
94                 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
95                 break;
96
97         case BLOBMSG_TYPE_INT64:
98                 lua_pushnumber(L, (double) be64_to_cpu(*(uint64_t *)data));
99                 break;
100
101         case BLOBMSG_TYPE_STRING:
102                 lua_pushstring(L, data);
103                 break;
104
105         case BLOBMSG_TYPE_ARRAY:
106                 ubus_lua_parse_blob_array(L, data, len, false);
107                 break;
108
109         case BLOBMSG_TYPE_TABLE:
110                 ubus_lua_parse_blob_array(L, data, len, true);
111                 break;
112
113         default:
114                 lua_pushnil(L);
115                 break;
116         }
117
118         return off + 1;
119 }
120
121
122 static bool
123 ubus_lua_format_blob_is_array(lua_State *L)
124 {
125         lua_Integer prv = 0;
126         lua_Integer cur = 0;
127
128         /* Find out whether table is array-like */
129         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
130         {
131 #ifdef LUA_TINT
132                 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
133 #else
134                 if (lua_type(L, -2) != LUA_TNUMBER)
135 #endif
136                 {
137                         lua_pop(L, 2);
138                         return false;
139                 }
140
141                 cur = lua_tointeger(L, -2);
142
143                 if ((cur - 1) != prv)
144                 {
145                         lua_pop(L, 2);
146                         return false;
147                 }
148
149                 prv = cur;
150         }
151
152         return true;
153 }
154
155 static int
156 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
157
158 static int
159 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
160 {
161         void *c;
162         bool rv = true;
163         const char *key = table ? lua_tostring(L, -2) : NULL;
164
165         switch (lua_type(L, -1))
166         {
167         case LUA_TBOOLEAN:
168                 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
169                 break;
170
171 #ifdef LUA_TINT
172         case LUA_TINT:
173 #endif
174         case LUA_TNUMBER:
175                 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
176                 break;
177
178         case LUA_TSTRING:
179         case LUA_TUSERDATA:
180         case LUA_TLIGHTUSERDATA:
181                 blobmsg_add_string(b, key, lua_tostring(L, -1));
182                 break;
183
184         case LUA_TTABLE:
185                 if (ubus_lua_format_blob_is_array(L))
186                 {
187                         c = blobmsg_open_array(b, key);
188                         rv = ubus_lua_format_blob_array(L, b, false);
189                         blobmsg_close_array(b, c);
190                 }
191                 else
192                 {
193                         c = blobmsg_open_table(b, key);
194                         rv = ubus_lua_format_blob_array(L, b, true);
195                         blobmsg_close_table(b, c);
196                 }
197                 break;
198
199         default:
200                 rv = false;
201                 break;
202         }
203
204         return rv;
205 }
206
207 static int
208 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
209 {
210         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
211         {
212                 if (!ubus_lua_format_blob(L, b, table))
213                 {
214                         lua_pop(L, 1);
215                         return false;
216                 }
217         }
218
219         return true;
220 }
221
222
223 static int
224 ubus_lua_connect(lua_State *L)
225 {
226         struct ubus_lua_connection *c;
227         const char *sockpath = luaL_optstring(L, 1, NULL);
228         int timeout = luaL_optint(L, 2, 30);
229
230         if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
231                 (c->ctx = ubus_connect(sockpath)) != NULL)
232         {
233                 ubus_add_uloop(c->ctx);
234                 c->timeout = timeout;
235                 memset(&c->buf, 0, sizeof(c->buf));
236                 luaL_getmetatable(L, METANAME);
237                 lua_setmetatable(L, -2);
238                 return 1;
239         }
240
241         /* NB: no errors from ubus_connect() yet */
242         lua_pushnil(L);
243         lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
244         return 2;
245 }
246
247
248 static void
249 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
250 {
251         lua_State *L = (lua_State *)p;
252
253         lua_pushstring(L, o->path);
254         lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
255 }
256
257 static int
258 ubus_lua_objects(lua_State *L)
259 {
260         int rv;
261         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
262
263         lua_newtable(L);
264         rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
265
266         if (rv != UBUS_STATUS_OK)
267         {
268                 lua_pop(L, 1);
269                 lua_pushnil(L);
270                 lua_pushinteger(L, rv);
271                 return 2;
272         }
273
274         return 1;
275 }
276
277 static int
278 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
279                 struct ubus_request_data *req, const char *method,
280                 struct blob_attr *msg)
281 {
282         struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
283
284         lua_getglobal(state, "__ubus_cb");
285         lua_rawgeti(state, -1, o->r);
286         lua_getfield(state, -1, method);
287
288         if (lua_isfunction(state, -1)) {
289                 lua_pushlightuserdata(state, req);
290                 if (!msg)
291                         lua_pushnil(state);
292                 else
293                         ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
294                 lua_call(state, 2, 0);
295         }
296         return 0;
297 }
298
299 static int lua_gettablelen(lua_State *L, int index)
300 {
301         int cnt = 0;
302
303         lua_pushnil(L);
304         index -= 1;
305         while (lua_next(L, index) != 0) {
306                 cnt++;
307                 lua_pop(L, 1);
308         }
309
310         return cnt;
311 }
312
313 static int ubus_lua_reply(lua_State *L)
314 {
315         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
316         struct ubus_request_data *req;
317
318         luaL_checktype(L, 3, LUA_TTABLE);
319         blob_buf_init(&c->buf, 0);
320
321         if (!ubus_lua_format_blob_array(L, &c->buf, true))
322         {
323                 lua_pushnil(L);
324                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
325                 return 2;
326         }
327
328         req = lua_touserdata(L, 2);
329         ubus_send_reply(c->ctx, req, c->buf.head);
330
331         return 0;
332 }
333
334 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
335 {
336         struct blobmsg_policy *p;
337         int plen;
338         int pidx = 0;
339
340         /* get the function pointer */
341         lua_pushinteger(L, 1);
342         lua_gettable(L, -2);
343
344         /* get the policy table */
345         lua_pushinteger(L, 2);
346         lua_gettable(L, -3);
347         plen = lua_gettablelen(L, -1);
348
349         /* check if the method table is valid */
350         if ((lua_type(L, -2) != LUA_TFUNCTION) ||
351                         (lua_type(L, -1) != LUA_TTABLE) ||
352                         lua_objlen(L, -1) || !plen) {
353                 lua_pop(L, 2);
354                 return 1;
355         }
356
357         /* store function pointer */
358         lua_pushvalue(L, -2);
359         lua_setfield(L, -6, lua_tostring(L, -5));
360
361         /* setup the policy pointers */
362         p = malloc(sizeof(struct blobmsg_policy) * plen);
363         memset(p, 0, sizeof(struct blobmsg_policy) * plen);
364         m->policy = p;
365         lua_pushnil(L);
366         while (lua_next(L, -2) != 0) {
367                 int val = lua_tointeger(L, -1);
368
369                 /* check if the policy is valid */
370                 if ((lua_type(L, -2) != LUA_TSTRING) ||
371                                 (lua_type(L, -1) != LUA_TNUMBER) ||
372                                 (val < 0) ||
373                                 (val > BLOBMSG_TYPE_LAST)) {
374                         lua_pop(L, 1);
375                         continue;
376                 }
377                 p[pidx].name = lua_tostring(L, -2);
378                 p[pidx].type = val;
379                 lua_pop(L, 1);
380                 pidx++;
381         }
382
383         m->n_policy = pidx;
384         m->name = lua_tostring(L, -4);
385         m->handler = ubus_method_handler;
386         lua_pop(L, 2);
387
388         return 0;
389 }
390
391 static struct ubus_object* ubus_lua_load_object(lua_State *L)
392 {
393         struct ubus_lua_object *obj = NULL;
394         int mlen = lua_gettablelen(L, -1);
395         struct ubus_method *m;
396         int midx = 0;
397
398         /* setup object pointers */
399         obj = malloc(sizeof(struct ubus_lua_object));
400         memset(obj, 0, sizeof(struct ubus_lua_object));
401         obj->o.name = lua_tostring(L, -2);
402
403         /* setup method pointers */
404         m = malloc(sizeof(struct ubus_method) * mlen);
405         memset(m, 0, sizeof(struct ubus_method) * mlen);
406         obj->o.methods = m;
407
408         /* setup type pointers */
409         obj->o.type = malloc(sizeof(struct ubus_object_type));
410         memset(obj->o.type, 0, sizeof(struct ubus_object_type));
411         obj->o.type->name = lua_tostring(L, -2);
412         obj->o.type->id = 0;
413         obj->o.type->methods = obj->o.methods;
414
415         /* create the he callback lookup table */
416         lua_createtable(L, 1, 0);
417         lua_getglobal(L, "__ubus_cb");
418         lua_pushvalue(L, -2);
419         obj->r = luaL_ref(L, -2);
420         lua_pop(L, 1);
421
422         /* scan each method */
423         lua_pushnil(L);
424         while (lua_next(L, -3) != 0) {
425                 /* check if it looks like a method */
426                 if ((lua_type(L, -2) != LUA_TSTRING) ||
427                                 (lua_type(L, -1) != LUA_TTABLE) ||
428                                 !lua_objlen(L, -1)) {
429                         lua_pop(L, 1);
430                         continue;
431                 }
432
433                 if (!ubus_lua_load_methods(L, &m[midx]))
434                         midx++;
435                 lua_pop(L, 1);
436         }
437
438         obj->o.type->n_methods = obj->o.n_methods = midx;
439
440         /* pop the callback table */
441         lua_pop(L, 1);
442
443         return &obj->o;
444 }
445
446 static int ubus_lua_add(lua_State *L)
447 {
448         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
449
450         /* verify top level object */
451         if (lua_istable(L, 1)) {
452                 lua_pushstring(L, "you need to pass a table");
453                 lua_error(L);
454                 return 0;
455         }
456
457         /* scan each object */
458         lua_pushnil(L);
459         while (lua_next(L, -2) != 0) {
460                 struct ubus_object *obj = NULL;
461
462                 /* check if the object has a table of methods */
463                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
464                         obj = ubus_lua_load_object(L);
465
466                         if (obj)
467                                 ubus_add_object(c->ctx, obj);
468                 }
469                 lua_pop(L, 1);
470         }
471
472         return 0;
473 }
474
475 static void
476 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
477 {
478         lua_State *L = (lua_State *)p;
479
480         if (!o->signature)
481                 return;
482
483         ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
484 }
485
486 static int
487 ubus_lua_signatures(lua_State *L)
488 {
489         int rv;
490         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
491         const char *path = luaL_checkstring(L, 2);
492
493         rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
494
495         if (rv != UBUS_STATUS_OK)
496         {
497                 lua_pop(L, 1);
498                 lua_pushnil(L);
499                 lua_pushinteger(L, rv);
500                 return 2;
501         }
502
503         return 1;
504 }
505
506
507 static void
508 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
509 {
510         lua_State *L = (lua_State *)req->priv;
511
512         if (!msg)
513                 lua_pushnil(L);
514
515         ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
516 }
517
518 static int
519 ubus_lua_call(lua_State *L)
520 {
521         int rv, top;
522         uint32_t id;
523         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
524         const char *path = luaL_checkstring(L, 2);
525         const char *func = luaL_checkstring(L, 3);
526
527         luaL_checktype(L, 4, LUA_TTABLE);
528         blob_buf_init(&c->buf, 0);
529
530         if (!ubus_lua_format_blob_array(L, &c->buf, true))
531         {
532                 lua_pushnil(L);
533                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
534                 return 2;
535         }
536
537         rv = ubus_lookup_id(c->ctx, path, &id);
538
539         if (rv)
540         {
541                 lua_pushnil(L);
542                 lua_pushinteger(L, rv);
543                 return 2;
544         }
545
546         top = lua_gettop(L);
547         rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
548
549         if (rv != UBUS_STATUS_OK)
550         {
551                 lua_pop(L, 1);
552                 lua_pushnil(L);
553                 lua_pushinteger(L, rv);
554                 return 2;
555         }
556
557         return lua_gettop(L) - top;
558 }
559
560
561 static int
562 ubus_lua__gc(lua_State *L)
563 {
564         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
565
566         if (c->ctx != NULL)
567         {
568                 ubus_free(c->ctx);
569                 memset(c, 0, sizeof(*c));
570         }
571
572         return 0;
573 }
574
575 static const luaL_Reg ubus[] = {
576         { "connect", ubus_lua_connect },
577         { "objects", ubus_lua_objects },
578         { "add", ubus_lua_add },
579         { "reply", ubus_lua_reply },
580         { "signatures", ubus_lua_signatures },
581         { "call", ubus_lua_call },
582         { "close", ubus_lua__gc },
583         { "__gc", ubus_lua__gc },
584         { NULL, NULL },
585 };
586
587 /* avoid missing prototype warning */
588 int luaopen_ubus(lua_State *L);
589
590 int
591 luaopen_ubus(lua_State *L)
592 {
593         /* create metatable */
594         luaL_newmetatable(L, METANAME);
595
596         /* metatable.__index = metatable */
597         lua_pushvalue(L, -1);
598         lua_setfield(L, -2, "__index");
599
600         /* fill metatable */
601         luaL_register(L, NULL, ubus);
602         lua_pop(L, 1);
603
604         /* create module */
605         luaL_register(L, MODNAME, ubus);
606
607         /* set some enum defines */
608         lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
609         lua_setfield(L, -2, "ARRAY");
610         lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
611         lua_setfield(L, -2, "TABLE");
612         lua_pushinteger(L, BLOBMSG_TYPE_STRING);
613         lua_setfield(L, -2, "STRING");
614         lua_pushinteger(L, BLOBMSG_TYPE_INT64);
615         lua_setfield(L, -2, "INT64");
616         lua_pushinteger(L, BLOBMSG_TYPE_INT32);
617         lua_setfield(L, -2, "INT32");
618         lua_pushinteger(L, BLOBMSG_TYPE_INT16);
619         lua_setfield(L, -2, "INT16");
620         lua_pushinteger(L, BLOBMSG_TYPE_INT8);
621         lua_setfield(L, -2, "INT8");
622         lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
623         lua_setfield(L, -2, "BOOLEAN");
624
625         /* used in our callbacks */
626         state = L;
627
628         /* create the callback table */
629         lua_createtable(L, 1, 0);
630         lua_setglobal(L, "__ubus_cb");
631
632         return 0;
633 }