1 #include <libubox/avl-cmp.h>
6 struct avl_tree services
;
7 static struct blob_buf b
;
10 service_instance_add(struct service
*s
, struct blob_attr
*attr
)
12 struct service_instance
*in
;
14 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
17 in
= calloc(1, sizeof(*in
));
21 instance_init(in
, s
, attr
);
22 vlist_add(&s
->instances
, &in
->node
, (void *) in
->name
);
26 service_instance_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
27 struct vlist_node
*node_old
)
29 struct service_instance
*in_o
= NULL
, *in_n
= NULL
;
32 in_o
= container_of(node_old
, struct service_instance
, node
);
35 in_n
= container_of(node_new
, struct service_instance
, node
);
38 LOG("Update instance %s::%s\n", in_o
->srv
->name
, in_o
->name
);
39 instance_update(in_o
, in_n
);
42 LOG("Free instance %s::%s\n", in_o
->srv
->name
, in_o
->name
);
43 instance_stop(in_o
, false);
46 LOG("Create instance %s::%s\n", in_n
->srv
->name
, in_n
->name
);
51 static struct service
*
52 service_alloc(const char *name
)
57 s
= calloc_a(sizeof(*s
), &new_name
, strlen(name
) + 1);
58 strcpy(new_name
, name
);
60 vlist_init(&s
->instances
, avl_strcmp
, service_instance_update
);
61 s
->instances
.keep_old
= true;
71 SERVICE_SET_INSTANCES
,
75 static const struct blobmsg_policy service_set_attrs
[__SERVICE_SET_MAX
] = {
76 [SERVICE_SET_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
77 [SERVICE_SET_SCRIPT
] = { "script", BLOBMSG_TYPE_STRING
},
78 [SERVICE_SET_INSTANCES
] = { "instances", BLOBMSG_TYPE_TABLE
},
83 service_update(struct service
*s
, struct blob_attr
*config
, struct blob_attr
**tb
, bool add
)
85 struct blob_attr
*cur
;
88 if (tb
[SERVICE_SET_INSTANCES
]) {
90 vlist_update(&s
->instances
);
91 blobmsg_for_each_attr(cur
, tb
[SERVICE_SET_INSTANCES
], rem
) {
92 service_instance_add(s
, cur
);
95 vlist_flush(&s
->instances
);
102 service_delete(struct service
*s
)
104 vlist_flush_all(&s
->instances
);
105 avl_delete(&services
, &s
->avl
);
115 static const struct blobmsg_policy service_attrs
[__SERVICE_ATTR_MAX
] = {
116 [SERVICE_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
120 SERVICE_DEL_ATTR_NAME
,
121 SERVICE_DEL_ATTR_INSTANCE
,
122 __SERVICE_DEL_ATTR_MAX
,
125 static const struct blobmsg_policy service_del_attrs
[__SERVICE_DEL_ATTR_MAX
] = {
126 [SERVICE_DEL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
127 [SERVICE_DEL_ATTR_INSTANCE
] = { "instance", BLOBMSG_TYPE_STRING
},
132 service_handle_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
133 struct ubus_request_data
*req
, const char *method
,
134 struct blob_attr
*msg
)
136 struct blob_attr
*tb
[__SERVICE_SET_MAX
], *cur
;
137 struct service
*s
= NULL
;
139 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
140 bool add
= !strcmp(method
, "add");
142 blobmsg_parse(service_set_attrs
, __SERVICE_SET_MAX
, tb
, blob_data(msg
), blob_len(msg
));
143 cur
= tb
[SERVICE_ATTR_NAME
];
147 name
= blobmsg_data(cur
);
149 s
= avl_find_element(&services
, name
, s
, avl
);
151 LOG("Update service %s\n", name
);
152 return service_update(s
, msg
, tb
, add
);
155 LOG("Create service %s\n", name
);
156 s
= service_alloc(name
);
158 return UBUS_STATUS_UNKNOWN_ERROR
;
160 ret
= service_update(s
, msg
, tb
, add
);
164 avl_insert(&services
, &s
->avl
);
174 service_dump(struct service
*s
)
176 struct service_instance
*in
;
179 c
= blobmsg_open_table(&b
, s
->name
);
180 i
= blobmsg_open_table(&b
, "instances");
181 vlist_for_each_element(&s
->instances
, in
, node
)
182 instance_dump(&b
, in
);
183 blobmsg_close_table(&b
, i
);
184 blobmsg_close_table(&b
, c
);
188 service_handle_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
189 struct ubus_request_data
*req
, const char *method
,
190 struct blob_attr
*msg
)
194 blob_buf_init(&b
, 0);
195 avl_for_each_element(&services
, s
, avl
)
198 ubus_send_reply(ctx
, req
, b
.head
);
204 service_handle_delete(struct ubus_context
*ctx
, struct ubus_object
*obj
,
205 struct ubus_request_data
*req
, const char *method
,
206 struct blob_attr
*msg
)
208 struct blob_attr
*tb
[__SERVICE_DEL_ATTR_MAX
], *cur
;
209 struct service
*s
, *tmp
;
210 struct service_instance
*in
;
212 blobmsg_parse(service_del_attrs
, __SERVICE_DEL_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
214 cur
= tb
[SERVICE_DEL_ATTR_NAME
];
216 avl_for_each_element_safe(&services
, s
, avl
, tmp
)
221 s
= avl_find_element(&services
, blobmsg_data(cur
), s
, avl
);
223 return UBUS_STATUS_NOT_FOUND
;
225 cur
= tb
[SERVICE_DEL_ATTR_INSTANCE
];
231 in
= vlist_find(&s
->instances
, blobmsg_data(cur
), in
, node
);
233 ERROR("instance %s not found\n", (char *) blobmsg_data(cur
));
234 return UBUS_STATUS_NOT_FOUND
;
237 vlist_delete(&s
->instances
, &in
->node
);
243 service_handle_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
244 struct ubus_request_data
*req
, const char *method
,
245 struct blob_attr
*msg
)
247 struct blob_attr
*tb
[__SERVICE_ATTR_MAX
], *cur
;
250 blobmsg_parse(service_attrs
, __SERVICE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
252 cur
= tb
[SERVICE_ATTR_NAME
];
254 return UBUS_STATUS_INVALID_ARGUMENT
;
256 s
= avl_find_element(&services
, blobmsg_data(cur
), s
, avl
);
258 return UBUS_STATUS_NOT_FOUND
;
260 if (!strcmp(method
, "update_start"))
261 vlist_update(&s
->instances
);
263 vlist_flush(&s
->instances
);
268 static struct ubus_method main_object_methods
[] = {
269 UBUS_METHOD("set", service_handle_set
, service_set_attrs
),
270 UBUS_METHOD("add", service_handle_set
, service_set_attrs
),
271 UBUS_METHOD("list", service_handle_list
, service_attrs
),
272 UBUS_METHOD("delete", service_handle_delete
, service_del_attrs
),
273 UBUS_METHOD("update_start", service_handle_update
, service_attrs
),
274 UBUS_METHOD("update_complete", service_handle_update
, service_attrs
),
277 static struct ubus_object_type main_object_type
=
278 UBUS_OBJECT_TYPE("service", main_object_methods
);
280 static struct ubus_object main_object
= {
282 .type
= &main_object_type
,
283 .methods
= main_object_methods
,
284 .n_methods
= ARRAY_SIZE(main_object_methods
),
287 void ubus_init_service(struct ubus_context
*ctx
)
289 avl_init(&services
, avl_strcmp
, false, NULL
);
290 ubus_add_object(ctx
, &main_object
);