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