5 struct ubus_auto_conn conn
;
7 static struct ubus_object udebug_object
;
8 static struct blob_attr
*service_config
;
18 static const struct blobmsg_policy list_policy
[__LIST_ATTR_MAX
] = {
19 [LIST_ATTR_PROCNAME
] = { "proc_name", BLOBMSG_TYPE_ARRAY
},
20 [LIST_ATTR_RINGNAME
] = { "ring_name", BLOBMSG_TYPE_ARRAY
},
21 [LIST_ATTR_PID
] = { "pid", BLOBMSG_TYPE_ARRAY
},
22 [LIST_ATTR_UID
] = { "uid", BLOBMSG_TYPE_ARRAY
},
26 string_array_match(const char *val
, struct blob_attr
*match
)
28 struct blob_attr
*cur
;
31 if (!match
|| !blobmsg_len(match
))
34 if (blobmsg_check_array(match
, BLOBMSG_TYPE_STRING
) < 0)
37 blobmsg_for_each_attr(cur
, match
, rem
) {
38 if (fnmatch(blobmsg_get_string(cur
), val
, 0) == 0)
46 int_array_match(unsigned int val
, struct blob_attr
*match
)
48 struct blob_attr
*cur
;
51 if (!match
|| !blobmsg_len(match
))
54 if (blobmsg_check_array(match
, BLOBMSG_TYPE_INT32
) < 0)
57 blobmsg_for_each_attr(cur
, match
, rem
) {
58 if (val
== blobmsg_get_u32(cur
))
66 udebug_list_match(struct client_ring
*r
, struct blob_attr
**tb
)
68 return string_array_match(r
->cl
->proc_name
, tb
[LIST_ATTR_PROCNAME
]) &&
69 string_array_match(r
->name
, tb
[LIST_ATTR_RINGNAME
]) &&
70 int_array_match(r
->cl
->pid
, tb
[LIST_ATTR_PID
]) &&
71 int_array_match(r
->cl
->uid
, tb
[LIST_ATTR_UID
]);
75 udebug_list_add_ring_data(struct client_ring
*r
)
77 blobmsg_add_u32(&b
, "id", ring_id(r
));
78 blobmsg_add_string(&b
, "proc_name", r
->cl
->proc_name
);
79 blobmsg_add_string(&b
, "ring_name", r
->name
);
80 blobmsg_add_u32(&b
, "pid", r
->cl
->pid
);
81 blobmsg_add_u32(&b
, "uid", r
->cl
->uid
);
82 blobmsg_add_u32(&b
, "ring_size", r
->ring_size
);
83 blobmsg_add_u32(&b
, "data_size", r
->data_size
);
85 blobmsg_add_blob(&b
, r
->flags
);
88 void udebug_ubus_ring_notify(struct client_ring
*r
, bool add
)
91 udebug_list_add_ring_data(r
);
92 ubus_notify(&conn
.ctx
, &udebug_object
, add
? "add" : "remove", b
.head
, -1);
96 udebug_list_add_ring(struct client_ring
*r
)
100 c
= blobmsg_open_table(&b
, NULL
);
101 udebug_list_add_ring_data(r
);
102 blobmsg_close_table(&b
, c
);
106 udebug_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
107 struct ubus_request_data
*req
, const char *method
,
108 struct blob_attr
*msg
)
110 struct blob_attr
*tb
[__LIST_ATTR_MAX
];
111 struct client_ring
*r
;
114 blobmsg_parse_attr(list_policy
, __LIST_ATTR_MAX
, tb
, msg
);
116 blob_buf_init(&b
, 0);
117 c
= blobmsg_open_array(&b
, "results");
118 avl_for_each_element(&rings
, r
, node
)
119 if (udebug_list_match(r
, tb
))
120 udebug_list_add_ring(r
);
121 blobmsg_close_array(&b
, c
);
122 ubus_send_reply(ctx
, req
, b
.head
);
131 static const struct blobmsg_policy config_policy
[__CFG_ATTR_MAX
] = {
132 [CFG_ATTR_SERVICE
] = { "service", BLOBMSG_TYPE_TABLE
},
135 static struct blob_attr
*
136 udebug_fill_config(void)
138 blob_buf_init(&b
, 0);
140 blobmsg_add_blob(&b
, service_config
);
142 blobmsg_close_table(&b
, blobmsg_open_table(&b
, "service"));
148 udebug_set_config(struct ubus_context
*ctx
, struct ubus_object
*obj
,
149 struct ubus_request_data
*req
, const char *method
,
150 struct blob_attr
*msg
)
152 struct blob_attr
*tb
[__CFG_ATTR_MAX
], *cur
;
154 blobmsg_parse_attr(config_policy
, __CFG_ATTR_MAX
, tb
, msg
);
155 if ((cur
= tb
[CFG_ATTR_SERVICE
]) != NULL
) {
156 free(service_config
);
157 service_config
= blob_memdup(cur
);
158 ubus_notify(ctx
, obj
, "config", udebug_fill_config(), -1);
165 udebug_get_config(struct ubus_context
*ctx
, struct ubus_object
*obj
,
166 struct ubus_request_data
*req
, const char *method
,
167 struct blob_attr
*msg
)
169 ubus_send_reply(ctx
, req
, udebug_fill_config());
173 static const struct ubus_method udebug_methods
[] = {
174 UBUS_METHOD("list", udebug_list
, list_policy
),
175 UBUS_METHOD("set_config", udebug_set_config
, config_policy
),
176 UBUS_METHOD_NOARG("get_config", udebug_get_config
),
179 static struct ubus_object_type udebug_object_type
=
180 UBUS_OBJECT_TYPE("udebug", udebug_methods
);
182 static struct ubus_object udebug_object
= {
184 .type
= &udebug_object_type
,
185 .methods
= udebug_methods
,
186 .n_methods
= ARRAY_SIZE(udebug_methods
),
189 static void ubus_connect_cb(struct ubus_context
*ctx
)
191 ubus_add_object(ctx
, &udebug_object
);
194 void udebug_ubus_init(void)
196 conn
.cb
= ubus_connect_cb
;
197 ubus_auto_connect(&conn
);
200 void udebug_ubus_free(void)
202 ubus_auto_shutdown(&conn
);