82fb8f87fdfa4e3182a0ad7d5ff20779c5713e2d
[project/procd.git] / log.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 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 <linux/types.h>
16
17 #include <libubox/uloop.h>
18 #include <libubox/blobmsg_json.h>
19
20 #include "procd.h"
21 #include "syslog.h"
22
23 static struct ubus_subscriber log_event;
24
25 static int notify;
26 struct ubus_context *_ctx;
27 static struct blob_buf b;
28
29 static const struct blobmsg_policy read_policy =
30         { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
31
32 static const struct blobmsg_policy write_policy =
33         { .name = "event", .type = BLOBMSG_TYPE_STRING };
34
35 static int read_log(struct ubus_context *ctx, struct ubus_object *obj,
36                 struct ubus_request_data *req, const char *method,
37                 struct blob_attr *msg)
38 {
39         struct blob_attr *tb;
40         struct log_head *l;
41         void *lines, *entry;
42         int count = 0;
43
44         if (msg) {
45                 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
46                 if (tb)
47                         count = blobmsg_get_u32(tb);
48         }
49
50         blob_buf_init(&b, 0);
51         lines = blobmsg_open_array(&b, "lines");
52
53         l = log_list(count, NULL);
54
55         while (l) {
56                 entry = blobmsg_open_table(&b, NULL);
57                 blobmsg_add_string(&b, "msg", l->data);
58                 blobmsg_add_u32(&b, "id", l->id);
59                 blobmsg_add_u32(&b, "priority", l->priority);
60                 blobmsg_add_u32(&b, "source", l->source);
61                 blobmsg_add_u64(&b, "time", l->ts.tv_sec);
62                 blobmsg_close_table(&b, entry);
63                 l = log_list(count, l);
64         }
65         blobmsg_close_table(&b, lines);
66         ubus_send_reply(ctx, req, b.head);
67
68         return 0;
69 }
70
71 static int write_log(struct ubus_context *ctx, struct ubus_object *obj,
72                 struct ubus_request_data *req, const char *method,
73                 struct blob_attr *msg)
74 {
75         struct blob_attr *tb;
76         char *event;
77
78         if (msg) {
79                 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
80                 if (tb) {
81                         event = blobmsg_get_string(tb);
82                         log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
83                 }
84         }
85
86         return 0;
87 }
88
89 static void log_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
90 {
91         notify = obj->has_subscribers;
92 }
93
94 static const struct ubus_method log_methods[] = {
95         { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
96         { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
97 };
98
99 static struct ubus_object_type log_object_type =
100         UBUS_OBJECT_TYPE("log", log_methods);
101
102 static struct ubus_object log_object = {
103         .name = "log",
104         .type = &log_object_type,
105         .methods = log_methods,
106         .n_methods = ARRAY_SIZE(log_methods),
107         .subscribe_cb = log_subscribe_cb,
108 };
109
110 void ubus_notify_log(struct log_head *l)
111 {
112         int ret;
113
114         if (!notify)
115                 return;
116
117         blob_buf_init(&b, 0);
118         blobmsg_add_u32(&b, "id", l->id);
119         blobmsg_add_u32(&b, "priority", l->priority);
120         blobmsg_add_u32(&b, "source", l->source);
121         blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
122
123         ret = ubus_notify(_ctx, &log_object, l->data, b.head, -1);
124         if (ret)
125                 ERROR("Failed to notify log: %s\n", ubus_strerror(ret));
126 }
127
128 void ubus_init_log(struct ubus_context *ctx)
129 {
130         int ret;
131
132         _ctx = ctx;
133
134         ret = ubus_add_object(ctx, &log_object);
135         if (ret)
136                 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
137
138         ret = ubus_register_subscriber(ctx, &log_event);
139         if (ret)
140                 ERROR("Failed to add watch handler: %s\n", ubus_strerror(ret));
141 }