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 <sys/types.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
28 #include <libubox/blobmsg_json.h>
29 #include <libubox/usock.h>
30 #include <libubox/uloop.h>
48 static const struct blobmsg_policy log_policy
[] = {
49 [LOG_MSG
] = { .name
= "msg", .type
= BLOBMSG_TYPE_STRING
},
50 [LOG_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
51 [LOG_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
52 [LOG_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_INT32
},
53 [LOG_TIME
] = { .name
= "time", .type
= BLOBMSG_TYPE_INT64
},
56 static struct ubus_subscriber log_event
;
57 static struct uloop_timeout retry
;
58 static struct uloop_fd sender
;
59 static const char *log_file
, *log_ip
, *log_port
, *pid_file
;
60 static int log_type
= LOG_STDOUT
;
61 static int log_size
, log_udp
;
63 static void log_handle_reconnect(struct uloop_timeout
*timeout
)
65 sender
.fd
= usock((log_udp
) ? (USOCK_UDP
) : (USOCK_TCP
), log_ip
, log_port
);
67 fprintf(stderr
, "failed to connect: %s\n", strerror(errno
));
68 uloop_timeout_set(&retry
, 1000);
70 uloop_fd_add(&sender
, ULOOP_READ
);
71 syslog(0, "Logread connected to %s:%s\n", log_ip
, log_port
);
75 static void log_handle_remove(struct ubus_context
*ctx
, struct ubus_subscriber
*s
,
78 fprintf(stderr
, "Object %08x went away\n", id
);
81 static void log_handle_fd(struct uloop_fd
*u
, unsigned int events
)
87 uloop_timeout_set(&retry
, 1000);
91 static int log_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
92 struct ubus_request_data
*req
, const char *method
,
93 struct blob_attr
*msg
)
95 struct blob_attr
*tb
[__LOG_MAX
];
106 blobmsg_parse(log_policy
, ARRAY_SIZE(log_policy
), tb
, blob_data(msg
), blob_len(msg
));
107 if (!tb
[LOG_ID
] || !tb
[LOG_PRIO
] || !tb
[LOG_SOURCE
] || !tb
[LOG_TIME
])
110 if ((log_type
== LOG_FILE
) && log_size
&& (!stat(log_file
, &s
)) && (s
.st_size
> log_size
)) {
111 char *old
= malloc(strlen(log_file
) + 5);
115 sprintf(old
, "%s.old", log_file
);
116 rename(log_file
, old
);
119 sender
.fd
= open(log_file
, O_CREAT
| O_WRONLY
| O_TRUNC
);
121 // fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
126 t
= blobmsg_get_u64(tb
[LOG_TIME
]) / 1000;
128 p
= blobmsg_get_u32(tb
[LOG_PRIO
]);
129 c
[strlen(c
) - 1] = '\0';
130 str
= blobmsg_format_json(msg
, true);
131 if (log_type
== LOG_NET
) {
132 snprintf(buf
, sizeof(buf
), "%s%s\n",
133 (blobmsg_get_u32(tb
[LOG_SOURCE
])) ? ("") : ("kernel: "),
135 send(sender
.fd
, buf
, strlen(buf
), 0);
137 snprintf(buf
, sizeof(buf
), "%s %s.%s%s %s\n",
138 c
, facilitynames
[LOG_FAC(p
)].c_name
, prioritynames
[LOG_PRI(p
)].c_name
,
139 (blobmsg_get_u32(tb
[LOG_SOURCE
])) ? ("") : (" kernel:"),
141 write(sender
.fd
, buf
, strlen(buf
));
145 if (log_type
== LOG_FILE
)
151 static void follow_log(struct ubus_context
*ctx
, int id
)
156 signal(SIGPIPE
, SIG_IGN
);
159 fp
= fopen(pid_file
, "w+");
161 fprintf(fp
, "%d", getpid());
169 log_event
.remove_cb
= log_handle_remove
;
170 log_event
.cb
= log_notify
;
171 ret
= ubus_register_subscriber(ctx
, &log_event
);
173 fprintf(stderr
, "Failed to add watch handler: %s\n", ubus_strerror(ret
));
175 ret
= ubus_subscribe(ctx
, &log_event
, id
);
177 fprintf(stderr
, "Failed to add watch handler: %s\n", ubus_strerror(ret
));
179 if (log_ip
&& log_port
) {
180 openlog("logread", LOG_PID
, LOG_DAEMON
);
182 sender
.cb
= log_handle_fd
;
183 retry
.cb
= log_handle_reconnect
;
184 uloop_timeout_set(&retry
, 1000);
185 } else if (log_file
) {
187 sender
.fd
= open(log_file
, O_CREAT
| O_WRONLY
);
189 fprintf(stderr
, "failed to open %s: %s\n", log_file
, strerror(errno
));
193 sender
.fd
= STDOUT_FILENO
;
206 static const struct blobmsg_policy read_policy
[] = {
207 [READ_LINE
] = { .name
= "lines", .type
= BLOBMSG_TYPE_ARRAY
},
210 static void read_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
212 struct blob_attr
*cur
;
213 struct blob_attr
*_tb
[__READ_MAX
];
220 blobmsg_parse(read_policy
, ARRAY_SIZE(read_policy
), _tb
, blob_data(msg
), blob_len(msg
));
223 blobmsg_for_each_attr(cur
, _tb
[READ_LINE
], rem
) {
224 struct blob_attr
*tb
[__LOG_MAX
];
228 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
)
231 blobmsg_parse(log_policy
, ARRAY_SIZE(log_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
232 if (!tb
[LOG_MSG
] || !tb
[LOG_ID
] || !tb
[LOG_PRIO
] || !tb
[LOG_SOURCE
] || !tb
[LOG_TIME
])
235 t
= blobmsg_get_u64(tb
[LOG_TIME
]);
236 p
= blobmsg_get_u32(tb
[LOG_PRIO
]);
238 c
[strlen(c
) - 1] = '\0';
240 printf("%s %s.%s%s %s\n",
241 c
, facilitynames
[LOG_FAC(p
)].c_name
, prioritynames
[LOG_PRI(p
)].c_name
,
242 (blobmsg_get_u32(tb
[LOG_SOURCE
])) ? ("") : (" kernel:"),
243 blobmsg_get_string(tb
[LOG_MSG
]));
247 static int usage(const char *prog
)
249 fprintf(stderr
, "Usage: %s [options]\n"
251 " -s <path> Path to ubus socket\n"
252 " -l <count> Got only the last 'count' messages\n"
253 " -r <server> <port> Stream message to a server\n"
254 " -F <file> Log file\n"
255 " -S <bytes> Log size\n"
256 " -p <file> PID file\n"
257 " -f Follow log messages\n"
258 " -u Use UDP as the protocol\n"
263 int main(int argc
, char **argv
)
265 struct ubus_context
*ctx
;
267 const char *ubus_socket
= NULL
;
268 int ch
, ret
, subscribe
= 0, lines
= 0;
269 static struct blob_buf b
;
271 while ((ch
= getopt(argc
, argv
, "ufcs:l:r:F:p:S:")) != -1) {
277 ubus_socket
= optarg
;
281 log_port
= argv
[optind
++];
293 lines
= atoi(optarg
);
296 log_size
= atoi(optarg
);
306 ctx
= ubus_connect(ubus_socket
);
308 fprintf(stderr
, "Failed to connect to ubus\n");
312 ret
= ubus_lookup_id(ctx
, "log", &id
);
314 fprintf(stderr
, "Failed to find log object: %s\n", ubus_strerror(ret
));
316 if (!subscribe
|| lines
) {
317 blob_buf_init(&b
, 0);
319 blobmsg_add_u32(&b
, "lines", lines
);
320 ubus_invoke(ctx
, id
, "read", b
.head
, read_cb
, 0, 3000);