2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <libubox/blobmsg_json.h>
16 #include <libubox/avl-cmp.h>
25 struct avl_tree services
;
26 static struct blob_buf b
;
27 static struct ubus_context
*ctx
;
30 service_instance_add(struct service
*s
, struct blob_attr
*attr
)
32 struct service_instance
*in
;
34 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
37 in
= calloc(1, sizeof(*in
));
41 instance_init(in
, s
, attr
);
42 vlist_add(&s
->instances
, &in
->node
, (void *) in
->name
);
46 service_instance_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
47 struct vlist_node
*node_old
)
49 struct service_instance
*in_o
= NULL
, *in_n
= NULL
;
52 in_o
= container_of(node_old
, struct service_instance
, node
);
55 in_n
= container_of(node_new
, struct service_instance
, node
);
58 DEBUG(2, "Update instance %s::%s\n", in_o
->srv
->name
, in_o
->name
);
59 instance_update(in_o
, in_n
);
62 DEBUG(2, "Free instance %s::%s\n", in_o
->srv
->name
, in_o
->name
);
66 DEBUG(2, "Create instance %s::%s\n", in_n
->srv
->name
, in_n
->name
);
70 trigger_event("instance.update", b
.head
);
73 static struct service
*
74 service_alloc(const char *name
)
79 s
= calloc_a(sizeof(*s
), &new_name
, strlen(name
) + 1);
80 strcpy(new_name
, name
);
82 vlist_init(&s
->instances
, avl_strcmp
, service_instance_update
);
83 s
->instances
.keep_old
= true;
86 INIT_LIST_HEAD(&s
->validators
);
94 SERVICE_SET_INSTANCES
,
100 static const struct blobmsg_policy service_set_attrs
[__SERVICE_SET_MAX
] = {
101 [SERVICE_SET_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
102 [SERVICE_SET_SCRIPT
] = { "script", BLOBMSG_TYPE_STRING
},
103 [SERVICE_SET_INSTANCES
] = { "instances", BLOBMSG_TYPE_TABLE
},
104 [SERVICE_SET_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
105 [SERVICE_SET_VALIDATE
] = { "validate", BLOBMSG_TYPE_ARRAY
},
109 service_update(struct service
*s
, struct blob_attr
**tb
, bool add
)
111 struct blob_attr
*cur
;
120 service_validate_del(s
);
122 if (tb
[SERVICE_SET_TRIGGER
] && blobmsg_data_len(tb
[SERVICE_SET_TRIGGER
])) {
123 s
->trigger
= blob_memdup(tb
[SERVICE_SET_TRIGGER
]);
126 trigger_add(s
->trigger
, s
);
129 if (tb
[SERVICE_SET_VALIDATE
] && blobmsg_data_len(tb
[SERVICE_SET_VALIDATE
])) {
130 blobmsg_for_each_attr(cur
, tb
[SERVICE_SET_VALIDATE
], rem
)
131 service_validate_add(s
, cur
);
134 if (tb
[SERVICE_SET_INSTANCES
]) {
136 vlist_update(&s
->instances
);
137 blobmsg_for_each_attr(cur
, tb
[SERVICE_SET_INSTANCES
], rem
) {
138 service_instance_add(s
, cur
);
141 vlist_flush(&s
->instances
);
144 rc(s
->name
, "running");
150 service_delete(struct service
*s
)
152 service_event("service.stop", s
->name
, NULL
);
153 vlist_flush_all(&s
->instances
);
154 avl_delete(&services
, &s
->avl
);
158 service_validate_del(s
);
166 static const struct blobmsg_policy service_attrs
[__SERVICE_ATTR_MAX
] = {
167 [SERVICE_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
171 SERVICE_DEL_ATTR_NAME
,
172 SERVICE_DEL_ATTR_INSTANCE
,
173 __SERVICE_DEL_ATTR_MAX
,
176 static const struct blobmsg_policy service_del_attrs
[__SERVICE_DEL_ATTR_MAX
] = {
177 [SERVICE_DEL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
178 [SERVICE_DEL_ATTR_INSTANCE
] = { "instance", BLOBMSG_TYPE_STRING
},
182 SERVICE_LIST_ATTR_NAME
,
183 SERVICE_LIST_ATTR_VERBOSE
,
184 __SERVICE_LIST_ATTR_MAX
,
187 static const struct blobmsg_policy service_list_attrs
[__SERVICE_LIST_ATTR_MAX
] = {
188 [SERVICE_LIST_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
189 [SERVICE_LIST_ATTR_VERBOSE
] = { "verbose", BLOBMSG_TYPE_BOOL
},
198 static const struct blobmsg_policy event_policy
[__EVENT_MAX
] = {
199 [EVENT_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
200 [EVENT_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
210 static const struct blobmsg_policy validate_policy
[__VALIDATE_MAX
] = {
211 [VALIDATE_PACKAGE
] = { .name
= "package", .type
= BLOBMSG_TYPE_STRING
},
212 [VALIDATE_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
213 [VALIDATE_SERVICE
] = { .name
= "service", .type
= BLOBMSG_TYPE_STRING
},
216 static const struct blobmsg_policy get_data_policy
[] = {
217 { "type", BLOBMSG_TYPE_STRING
}
221 service_handle_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
222 struct ubus_request_data
*req
, const char *method
,
223 struct blob_attr
*msg
)
225 struct blob_attr
*tb
[__SERVICE_SET_MAX
], *cur
;
226 struct service
*s
= NULL
;
228 bool add
= !strcmp(method
, "add");
231 blobmsg_parse(service_set_attrs
, __SERVICE_SET_MAX
, tb
, blob_data(msg
), blob_len(msg
));
232 cur
= tb
[SERVICE_ATTR_NAME
];
234 return UBUS_STATUS_INVALID_ARGUMENT
;
236 name
= blobmsg_data(cur
);
238 s
= avl_find_element(&services
, name
, s
, avl
);
240 DEBUG(2, "Update service %s\n", name
);
241 return service_update(s
, tb
, add
);
244 DEBUG(2, "Create service %s\n", name
);
245 s
= service_alloc(name
);
247 return UBUS_STATUS_UNKNOWN_ERROR
;
249 ret
= service_update(s
, tb
, add
);
253 avl_insert(&services
, &s
->avl
);
255 service_event("service.start", s
->name
, NULL
);
261 service_dump(struct service
*s
, bool verbose
)
263 struct service_instance
*in
;
266 c
= blobmsg_open_table(&b
, s
->name
);
268 if (!avl_is_empty(&s
->instances
.avl
)) {
269 i
= blobmsg_open_table(&b
, "instances");
270 vlist_for_each_element(&s
->instances
, in
, node
)
271 instance_dump(&b
, in
, verbose
);
272 blobmsg_close_table(&b
, i
);
274 if (verbose
&& s
->trigger
)
275 blobmsg_add_blob(&b
, s
->trigger
);
276 if (verbose
&& !list_empty(&s
->validators
))
277 service_validate_dump(&b
, s
);
278 blobmsg_close_table(&b
, c
);
282 service_handle_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
283 struct ubus_request_data
*req
, const char *method
,
284 struct blob_attr
*msg
)
286 struct blob_attr
*tb
[__SERVICE_LIST_ATTR_MAX
];
288 const char *name
= NULL
;
289 bool verbose
= false;
291 blobmsg_parse(service_list_attrs
, __SERVICE_LIST_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
293 if (tb
[SERVICE_LIST_ATTR_VERBOSE
])
294 verbose
= blobmsg_get_bool(tb
[SERVICE_LIST_ATTR_VERBOSE
]);
295 if (tb
[SERVICE_LIST_ATTR_NAME
])
296 name
= blobmsg_get_string(tb
[SERVICE_LIST_ATTR_NAME
]);
298 blob_buf_init(&b
, 0);
299 avl_for_each_element(&services
, s
, avl
) {
300 if (name
&& strcmp(s
->name
, name
) != 0)
303 service_dump(s
, verbose
);
306 ubus_send_reply(ctx
, req
, b
.head
);
312 service_handle_delete(struct ubus_context
*ctx
, struct ubus_object
*obj
,
313 struct ubus_request_data
*req
, const char *method
,
314 struct blob_attr
*msg
)
316 struct blob_attr
*tb
[__SERVICE_DEL_ATTR_MAX
], *cur
;
318 struct service_instance
*in
;
320 blobmsg_parse(service_del_attrs
, __SERVICE_DEL_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
322 cur
= tb
[SERVICE_DEL_ATTR_NAME
];
324 return UBUS_STATUS_NOT_FOUND
;
326 s
= avl_find_element(&services
, blobmsg_data(cur
), s
, avl
);
328 return UBUS_STATUS_NOT_FOUND
;
330 cur
= tb
[SERVICE_DEL_ATTR_INSTANCE
];
336 in
= vlist_find(&s
->instances
, blobmsg_data(cur
), in
, node
);
338 ERROR("instance %s not found\n", (char *) blobmsg_data(cur
));
339 return UBUS_STATUS_NOT_FOUND
;
342 vlist_delete(&s
->instances
, &in
->node
);
348 service_handle_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
349 struct ubus_request_data
*req
, const char *method
,
350 struct blob_attr
*msg
)
352 struct blob_attr
*tb
[__SERVICE_ATTR_MAX
], *cur
;
355 blobmsg_parse(service_attrs
, __SERVICE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
357 cur
= tb
[SERVICE_ATTR_NAME
];
359 return UBUS_STATUS_INVALID_ARGUMENT
;
361 s
= avl_find_element(&services
, blobmsg_data(cur
), s
, avl
);
363 return UBUS_STATUS_NOT_FOUND
;
365 if (!strcmp(method
, "update_start"))
366 vlist_update(&s
->instances
);
368 vlist_flush(&s
->instances
);
374 service_handle_event(struct ubus_context
*ctx
, struct ubus_object
*obj
,
375 struct ubus_request_data
*req
, const char *method
,
376 struct blob_attr
*msg
)
378 struct blob_attr
*tb
[__EVENT_MAX
];
381 return UBUS_STATUS_INVALID_ARGUMENT
;
383 blobmsg_parse(event_policy
, __EVENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
384 if (!tb
[EVENT_TYPE
] || !tb
[EVENT_DATA
])
385 return UBUS_STATUS_INVALID_ARGUMENT
;
387 trigger_event(blobmsg_get_string(tb
[EVENT_TYPE
]), tb
[EVENT_DATA
]);
393 service_handle_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
394 struct ubus_request_data
*req
, const char *method
,
395 struct blob_attr
*msg
)
397 struct blob_attr
*tb
[__VALIDATE_MAX
];
398 char *p
= NULL
, *t
= NULL
;
401 return UBUS_STATUS_INVALID_ARGUMENT
;
403 blobmsg_parse(validate_policy
, __VALIDATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
404 if (tb
[VALIDATE_SERVICE
]) {
407 if (tb
[VALIDATE_PACKAGE
])
408 p
= blobmsg_get_string(tb
[VALIDATE_PACKAGE
]);
410 if (tb
[VALIDATE_TYPE
])
411 t
= blobmsg_get_string(tb
[VALIDATE_TYPE
]);
413 blob_buf_init(&b
, 0);
414 service_validate_dump_all(&b
, p
, t
);
415 ubus_send_reply(ctx
, req
, b
.head
);
421 service_get_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
422 struct ubus_request_data
*req
, const char *method
,
423 struct blob_attr
*msg
)
425 struct service_instance
*in
;
427 struct blob_attr
*tb
;
428 const char *type
= NULL
;
430 blobmsg_parse(get_data_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
432 type
= blobmsg_data(tb
);
434 blob_buf_init(&b
, 0);
435 avl_for_each_element(&services
, s
, avl
) {
438 vlist_for_each_element(&s
->instances
, in
, node
) {
439 struct blobmsg_list_node
*var
;
442 blobmsg_list_for_each(&in
->data
, var
) {
444 strcmp(blobmsg_name(var
->data
), type
) != 0)
448 cs
= blobmsg_open_table(&b
, s
->name
);
450 ci
= blobmsg_open_table(&b
, in
->name
);
452 blobmsg_add_blob(&b
, var
->data
);
456 blobmsg_close_table(&b
, ci
);
460 blobmsg_close_table(&b
, cs
);
463 ubus_send_reply(ctx
, req
, b
.head
);
467 static struct ubus_method main_object_methods
[] = {
468 UBUS_METHOD("set", service_handle_set
, service_set_attrs
),
469 UBUS_METHOD("add", service_handle_set
, service_set_attrs
),
470 UBUS_METHOD("list", service_handle_list
, service_list_attrs
),
471 UBUS_METHOD("delete", service_handle_delete
, service_del_attrs
),
472 UBUS_METHOD("update_start", service_handle_update
, service_attrs
),
473 UBUS_METHOD("update_complete", service_handle_update
, service_attrs
),
474 UBUS_METHOD("event", service_handle_event
, event_policy
),
475 UBUS_METHOD("validate", service_handle_validate
, validate_policy
),
476 UBUS_METHOD("get_data", service_get_data
, get_data_policy
),
479 static struct ubus_object_type main_object_type
=
480 UBUS_OBJECT_TYPE("service", main_object_methods
);
482 static struct ubus_object main_object
= {
484 .type
= &main_object_type
,
485 .methods
= main_object_methods
,
486 .n_methods
= ARRAY_SIZE(main_object_methods
),
490 service_start_early(char *name
, char *cmdline
)
492 void *instances
, *instance
, *command
, *respawn
;
495 blob_buf_init(&b
, 0);
496 blobmsg_add_string(&b
, "name", name
);
497 instances
= blobmsg_open_table(&b
, "instances");
498 instance
= blobmsg_open_table(&b
, "instance1");
499 command
= blobmsg_open_array(&b
, "command");
500 t
= strtok(cmdline
, " ");
502 blobmsg_add_string(&b
, NULL
, t
);
503 t
= strtok(NULL
, " ");
505 blobmsg_close_array(&b
, command
);
506 respawn
= blobmsg_open_array(&b
, "respawn");
507 blobmsg_add_string(&b
, NULL
, "3600");
508 blobmsg_add_string(&b
, NULL
, "1");
509 blobmsg_add_string(&b
, NULL
, "0");
510 blobmsg_close_array(&b
, respawn
);
511 blobmsg_close_table(&b
, instance
);
512 blobmsg_close_table(&b
, instances
);
514 return service_handle_set(NULL
, NULL
, NULL
, "add", b
.head
);
517 void service_event(const char *type
, const char *service
, const char *instance
)
522 blob_buf_init(&b
, 0);
523 blobmsg_add_string(&b
, "service", service
);
525 blobmsg_add_string(&b
, "instance", instance
);
526 ubus_notify(ctx
, &main_object
, type
, b
.head
, -1);
529 void ubus_init_service(struct ubus_context
*_ctx
)
532 ubus_add_object(ctx
, &main_object
);
538 avl_init(&services
, avl_strcmp
, false, NULL
);
539 service_validate_init();