2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 static struct blob_buf buf;
22 static struct uci_context *cursor;
33 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
34 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
35 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
36 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
37 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
38 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
49 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
50 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
51 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
52 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
53 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
65 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
66 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
67 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
68 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
69 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
70 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
83 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
84 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
85 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
86 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
87 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
88 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
89 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
100 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
101 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
102 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
103 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
104 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
113 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
114 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
115 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
123 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
124 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
128 * Turn uci error state into ubus return code
136 return UBUS_STATUS_OK;
139 return UBUS_STATUS_INVALID_ARGUMENT;
141 case UCI_ERR_NOTFOUND:
142 return UBUS_STATUS_NOT_FOUND;
145 return UBUS_STATUS_UNKNOWN_ERROR;
150 * Format applicable blob value as string and place a pointer to the string
151 * buffer in "p". Uses a static string buffer.
154 rpc_uci_format_blob(struct blob_attr *v, const char **p)
160 switch (blobmsg_type(v))
162 case BLOBMSG_TYPE_STRING:
163 if (blobmsg_data_len(v) > 1)
164 *p = blobmsg_data(v);
167 case BLOBMSG_TYPE_INT64:
168 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
172 case BLOBMSG_TYPE_INT32:
173 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
177 case BLOBMSG_TYPE_INT16:
178 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
182 case BLOBMSG_TYPE_INT8:
183 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
195 * Lookup the given uci_ptr and enable extended lookup format if the .section
196 * value of the uci_ptr looks like extended syntax. Uses an internal copy
197 * of the given uci_ptr to perform the lookup as failing extended section
198 * lookup operations in libuci will zero our the uci_ptr struct.
199 * Copies the internal uci_ptr back to given the uci_ptr on success.
202 rpc_uci_lookup(struct uci_ptr *ptr)
205 struct uci_ptr lookup = *ptr;
207 if (!lookup.s && lookup.section && *lookup.section == '@')
208 lookup.flags |= UCI_LOOKUP_EXTENDED;
210 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
219 * Checks whether the given uci_option object matches the given string value.
220 * 1) If the uci_option is of type list, check whether any of the list elements
221 * equals to the given string
222 * 2) If the uci_option is of type string, parse it into space separated tokens
223 * and check if any of the tokens equals to the given string.
224 * Returns true if a list element or token matched the given string.
227 rpc_uci_match_option(struct uci_option *o, const char *cmp)
229 struct uci_element *e;
232 if (o->type == UCI_TYPE_LIST)
234 uci_foreach_element(&o->v.list, e)
235 if (e->name && !strcmp(e->name, cmp))
244 s = strdup(o->v.string);
249 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
263 * Checks whether the given uci_section matches the type and value blob attrs.
264 * 1) Returns false if "type" is given and the section type does not match
265 * the value specified in the "type" string blob attribute, else continue.
266 * 2) Tests whether any key in the "matches" table blob attribute exists in
267 * the given uci_section and whether each value is contained in the
268 * corresponding uci option value (see rpc_uci_match_option()).
269 * 3) A missing or empty "matches" table blob attribute is always considered
271 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
274 rpc_uci_match_section(struct uci_section *s,
275 struct blob_attr *type, struct blob_attr *matches)
277 struct uci_element *e;
278 struct blob_attr *cur;
284 if (type && strcmp(s->type, blobmsg_data(type)))
290 blobmsg_for_each_attr(cur, matches, rem)
292 if (!rpc_uci_format_blob(cur, &cmp))
295 uci_foreach_element(&s->options, e)
297 if (strcmp(e->name, blobmsg_name(cur)))
300 if (!rpc_uci_match_option(uci_to_option(e), cmp))
309 return (empty || match);
313 * Dump the given uci_option value into the global blobmsg buffer and use
314 * given "name" as key.
315 * 1) If the uci_option is of type list, put a table into the blob buffer and
316 * add each list item as string to it.
317 * 2) If the uci_option is of type string, put its value directly into the blob
321 rpc_uci_dump_option(struct uci_option *o, const char *name)
324 struct uci_element *e;
328 case UCI_TYPE_STRING:
329 blobmsg_add_string(&buf, name, o->v.string);
333 c = blobmsg_open_array(&buf, name);
335 uci_foreach_element(&o->v.list, e)
336 blobmsg_add_string(&buf, NULL, e->name);
338 blobmsg_close_array(&buf, c);
347 * Dump the given uci_section object into the global blobmsg buffer and use
348 * given "name" as key.
349 * Puts a table into the blob buffer and puts each section option member value
350 * as value into the table using the option name as key.
351 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
352 * corresponding section properties.
355 rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
358 struct uci_option *o;
359 struct uci_element *e;
361 c = blobmsg_open_table(&buf, name);
363 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
364 blobmsg_add_string(&buf, ".type", s->type);
365 blobmsg_add_string(&buf, ".name", s->e.name);
368 blobmsg_add_u32(&buf, ".index", index);
370 uci_foreach_element(&s->options, e)
372 o = uci_to_option(e);
373 rpc_uci_dump_option(o, o->e.name);
376 blobmsg_close_table(&buf, c);
380 * Dump the given uci_package object into the global blobmsg buffer and use
381 * given "name" as key.
382 * Puts a table into the blob buffer and puts each package section member as
383 * value into the table using the section name as key.
384 * Only dumps sections matching the given "type" and "matches", see explaination
385 * of rpc_uci_match_section() for details.
388 rpc_uci_dump_package(struct uci_package *p, const char *name,
389 struct blob_attr *type, struct blob_attr *matches)
392 struct uci_element *e;
395 c = blobmsg_open_table(&buf, name);
397 uci_foreach_element(&p->sections, e)
401 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
404 rpc_uci_dump_section(uci_to_section(e), e->name, i);
407 blobmsg_close_table(&buf, c);
412 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
413 struct ubus_request_data *req, const char *method,
414 struct blob_attr *msg)
416 struct blob_attr *tb[__RPC_G_MAX];
417 struct uci_package *p = NULL;
418 struct uci_ptr ptr = { 0 };
420 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
421 blob_data(msg), blob_len(msg));
423 if (!tb[RPC_G_CONFIG])
424 return UBUS_STATUS_INVALID_ARGUMENT;
426 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
427 uci_load(cursor, ptr.package, &p);
432 if (tb[RPC_G_SECTION])
434 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
436 if (tb[RPC_G_OPTION])
437 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
440 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
443 blob_buf_init(&buf, 0);
445 switch (ptr.last->type)
447 case UCI_TYPE_PACKAGE:
448 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
451 case UCI_TYPE_SECTION:
452 rpc_uci_dump_section(ptr.s, "values", -1);
455 case UCI_TYPE_OPTION:
456 rpc_uci_dump_option(ptr.o, "value");
463 ubus_send_reply(ctx, req, buf.head);
467 uci_unload(cursor, p);
469 return rpc_uci_status();
473 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
474 struct ubus_request_data *req, const char *method,
475 struct blob_attr *msg)
477 struct blob_attr *tb[__RPC_A_MAX];
478 struct blob_attr *cur, *elem;
479 struct uci_package *p = NULL;
480 struct uci_section *s;
481 struct uci_ptr ptr = { 0 };
484 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
485 blob_data(msg), blob_len(msg));
487 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
488 return UBUS_STATUS_INVALID_ARGUMENT;
490 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
492 uci_load(cursor, ptr.package, &p);
497 /* add named section */
500 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
501 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
504 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
508 /* add anon section */
511 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
514 ptr.section = s->e.name;
517 if (tb[RPC_A_VALUES])
519 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
522 ptr.option = blobmsg_name(cur);
524 if (rpc_uci_lookup(&ptr) || !ptr.s)
527 switch (blobmsg_type(cur))
529 case BLOBMSG_TYPE_ARRAY:
530 blobmsg_for_each_attr(elem, cur, rem2)
531 if (rpc_uci_format_blob(elem, &ptr.value))
532 uci_add_list(cursor, &ptr);
536 if (rpc_uci_format_blob(cur, &ptr.value))
537 uci_set(cursor, &ptr);
545 blob_buf_init(&buf, 0);
546 blobmsg_add_string(&buf, "section", ptr.section);
547 ubus_send_reply(ctx, req, buf.head);
551 uci_unload(cursor, p);
553 return rpc_uci_status();
557 * Turn value from a blob attribute into uci set operation
558 * 1) if the blob is of type array, delete existing option (if any) and
559 * emit uci add_list operations for each element
560 * 2) if the blob is not an array but an option of type list exists,
561 * delete existing list and emit uci set operation for the blob value
562 * 3) in all other cases only emit a set operation if there is no existing
563 * option of if the existing options value differs from the blob value
566 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
568 struct blob_attr *cur;
572 ptr->option = blobmsg_name(opt);
574 if (rpc_uci_lookup(ptr) || !ptr->s)
577 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
580 uci_delete(cursor, ptr);
582 blobmsg_for_each_attr(cur, opt, rem)
583 if (rpc_uci_format_blob(cur, &ptr->value))
584 uci_add_list(cursor, ptr);
586 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
588 uci_delete(cursor, ptr);
590 if (rpc_uci_format_blob(opt, &ptr->value))
591 uci_set(cursor, ptr);
593 else if (rpc_uci_format_blob(opt, &ptr->value))
595 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
596 uci_set(cursor, ptr);
601 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
602 struct ubus_request_data *req, const char *method,
603 struct blob_attr *msg)
605 struct blob_attr *tb[__RPC_S_MAX];
606 struct blob_attr *cur;
607 struct uci_package *p = NULL;
608 struct uci_element *e;
609 struct uci_ptr ptr = { 0 };
612 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
613 blob_data(msg), blob_len(msg));
615 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
616 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
617 return UBUS_STATUS_INVALID_ARGUMENT;
619 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
620 uci_load(cursor, ptr.package, &p);
625 if (tb[RPC_S_SECTION])
627 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
628 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
629 rpc_uci_merge_set(cur, &ptr);
633 uci_foreach_element(&p->sections, e)
635 if (!rpc_uci_match_section(uci_to_section(e),
636 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
640 ptr.section = e->name;
642 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
643 rpc_uci_merge_set(cur, &ptr);
651 uci_unload(cursor, p);
653 return rpc_uci_status();
657 * Delete option or section from uci specified by given blob attribute pointer
658 * 1) if the blob is of type array, delete any option named after each element
659 * 2) if the blob is of type string, delete the option named after its value
660 * 3) if the blob is NULL, delete entire section
663 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
665 struct blob_attr *cur;
668 if (rpc_uci_lookup(ptr) || !ptr->s)
676 uci_delete(cursor, ptr);
678 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
680 blobmsg_for_each_attr(cur, opt, rem)
682 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
686 ptr->option = blobmsg_data(cur);
688 if (rpc_uci_lookup(ptr) || !ptr->o)
691 uci_delete(cursor, ptr);
694 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
697 ptr->option = blobmsg_data(opt);
699 if (rpc_uci_lookup(ptr) || !ptr->o)
702 uci_delete(cursor, ptr);
707 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
708 struct ubus_request_data *req, const char *method,
709 struct blob_attr *msg)
711 struct blob_attr *tb[__RPC_D_MAX];
712 struct uci_package *p = NULL;
713 struct uci_element *e, *tmp;
714 struct uci_ptr ptr = { 0 };
716 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
717 blob_data(msg), blob_len(msg));
719 if (!tb[RPC_D_CONFIG] ||
720 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
721 return UBUS_STATUS_INVALID_ARGUMENT;
723 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
724 uci_load(cursor, ptr.package, &p);
729 if (tb[RPC_D_SECTION])
731 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
733 if (tb[RPC_D_OPTIONS])
734 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
736 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
740 uci_foreach_element_safe(&p->sections, tmp, e)
742 if (!rpc_uci_match_section(uci_to_section(e),
743 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
747 ptr.section = e->name;
749 if (tb[RPC_D_OPTIONS])
750 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
752 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
760 uci_unload(cursor, p);
762 return rpc_uci_status();
766 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
767 struct ubus_request_data *req, const char *method,
768 struct blob_attr *msg)
770 struct blob_attr *tb[__RPC_R_MAX];
771 struct uci_package *p = NULL;
772 struct uci_ptr ptr = { 0 };
774 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
775 blob_data(msg), blob_len(msg));
777 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
778 return UBUS_STATUS_INVALID_ARGUMENT;
780 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
781 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
782 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
784 if (tb[RPC_R_OPTION])
785 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
787 uci_load(cursor, ptr.package, &p);
792 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
795 if ((ptr.option && !ptr.o) || !ptr.s)
797 cursor->err = UCI_ERR_NOTFOUND;
801 if (uci_rename(cursor, &ptr))
808 uci_unload(cursor, p);
810 return rpc_uci_status();
814 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
815 struct ubus_request_data *req, const char *method,
816 struct blob_attr *msg)
818 struct blob_attr *tb[__RPC_O_MAX];
819 struct blob_attr *cur;
820 struct uci_package *p = NULL;
821 struct uci_ptr ptr = { 0 };
824 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
825 blob_data(msg), blob_len(msg));
827 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
828 return UBUS_STATUS_INVALID_ARGUMENT;
830 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
832 uci_load(cursor, ptr.package, &p);
837 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
839 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
843 ptr.section = blobmsg_data(cur);
845 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
848 uci_reorder_section(cursor, ptr.s, i++);
855 uci_unload(cursor, p);
857 return rpc_uci_status();
861 rpc_uci_dump_change(struct uci_delta *d)
864 const char *types[] = {
865 [UCI_CMD_REORDER] = "order",
866 [UCI_CMD_REMOVE] = "remove",
867 [UCI_CMD_RENAME] = "rename",
868 [UCI_CMD_ADD] = "add",
869 [UCI_CMD_LIST_ADD] = "list-add",
870 [UCI_CMD_LIST_DEL] = "list-del",
871 [UCI_CMD_CHANGE] = "set",
877 c = blobmsg_open_array(&buf, NULL);
879 blobmsg_add_string(&buf, NULL, types[d->cmd]);
880 blobmsg_add_string(&buf, NULL, d->section);
883 blobmsg_add_string(&buf, NULL, d->e.name);
887 if (d->cmd == UCI_CMD_REORDER)
888 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
890 blobmsg_add_string(&buf, NULL, d->value);
893 blobmsg_close_array(&buf, c);
897 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
898 struct ubus_request_data *req, const char *method,
899 struct blob_attr *msg)
901 struct blob_attr *tb[__RPC_C_MAX];
902 struct uci_package *p = NULL;
903 struct uci_element *e;
906 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
907 blob_data(msg), blob_len(msg));
909 if (!tb[RPC_C_CONFIG])
910 return UBUS_STATUS_INVALID_ARGUMENT;
912 uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p);
917 blob_buf_init(&buf, 0);
918 c = blobmsg_open_array(&buf, "changes");
920 uci_foreach_element(&p->saved_delta, e)
921 rpc_uci_dump_change(uci_to_delta(e));
923 blobmsg_close_array(&buf, c);
925 ubus_send_reply(ctx, req, buf.head);
929 uci_unload(cursor, p);
931 return rpc_uci_status();
935 rpc_uci_revert_commit(struct blob_attr *msg, bool commit)
937 struct blob_attr *tb[__RPC_C_MAX];
938 struct uci_package *p = NULL;
939 struct uci_ptr ptr = { 0 };
941 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
942 blob_data(msg), blob_len(msg));
944 if (!tb[RPC_C_CONFIG])
945 return UBUS_STATUS_INVALID_ARGUMENT;
947 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
948 uci_load(cursor, ptr.package, &p);
950 if (!p || uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.p)
954 uci_commit(cursor, &p, false);
956 uci_revert(cursor, &ptr);
960 uci_unload(cursor, p);
962 return rpc_uci_status();
966 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
967 struct ubus_request_data *req, const char *method,
968 struct blob_attr *msg)
970 return rpc_uci_revert_commit(msg, false);
974 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
975 struct ubus_request_data *req, const char *method,
976 struct blob_attr *msg)
978 return rpc_uci_revert_commit(msg, true);
982 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
983 struct ubus_request_data *req, const char *method,
984 struct blob_attr *msg)
990 if (uci_list_configs(cursor, &configs))
993 blob_buf_init(&buf, 0);
995 c = blobmsg_open_array(&buf, "configs");
997 for (i = 0; configs[i]; i++)
998 blobmsg_add_string(&buf, NULL, configs[i]);
1000 blobmsg_close_array(&buf, c);
1002 ubus_send_reply(ctx, req, buf.head);
1005 return rpc_uci_status();
1009 int rpc_uci_api_init(struct ubus_context *ctx)
1011 static const struct ubus_method uci_methods[] = {
1012 { .name = "configs", .handler = rpc_uci_configs },
1013 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1014 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1015 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1016 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1017 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1018 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1019 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1020 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1021 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1024 static struct ubus_object_type uci_type =
1025 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1027 static struct ubus_object obj = {
1030 .methods = uci_methods,
1031 .n_methods = ARRAY_SIZE(uci_methods),
1034 cursor = uci_alloc_context();
1037 return UBUS_STATUS_UNKNOWN_ERROR;
1039 return ubus_add_object(ctx, &obj);