- return 0;
-}
-
-static void follow_log(struct ubus_context *ctx, int id)
-{
- FILE *fp;
- int ret;
-
- signal(SIGPIPE, SIG_IGN);
-
- if (pid_file) {
- fp = fopen(pid_file, "w+");
- if (fp) {
- fprintf(fp, "%d", getpid());
- fclose(fp);
- }
- }
-
- uloop_init();
- ubus_add_uloop(ctx);
-
- log_event.remove_cb = log_handle_remove;
- log_event.cb = log_notify;
- ret = ubus_register_subscriber(ctx, &log_event);
- if (ret)
- fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
-
- ret = ubus_subscribe(ctx, &log_event, id);
- if (ret)
- fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
-
- if (log_ip && log_port) {
- openlog("logread", LOG_PID, LOG_DAEMON);
- log_type = LOG_NET;
- sender.cb = log_handle_fd;
- retry.cb = log_handle_reconnect;
- uloop_timeout_set(&retry, 1000);
- } else if (log_file) {
- log_type = LOG_FILE;
- sender.fd = open(log_file, O_CREAT | O_WRONLY| O_APPEND, 0600);
- if (sender.fd < 0) {
- fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
- exit(-1);
- }
- } else {
- sender.fd = STDOUT_FILENO;
- }
-
- uloop_run();
- ubus_free(ctx);
- uloop_done();
-}
-
-enum {
- READ_LINE,
- __READ_MAX
-};
-
-
-
-static const struct blobmsg_policy read_policy[] = {
- [READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY },
-};
-
-static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg)
-{
- struct blob_attr *cur;
- struct blob_attr *_tb[__READ_MAX];
- time_t t;
- int rem;
-
- if (!msg)
- return;
-
- blobmsg_parse(read_policy, ARRAY_SIZE(read_policy), _tb, blob_data(msg), blob_len(msg));
- if (!_tb[READ_LINE])
- return;
- blobmsg_for_each_attr(cur, _tb[READ_LINE], rem) {
- struct blob_attr *tb[__LOG_MAX];
- uint32_t p;
- char *c;
-
- if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
- continue;
-
- blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
- if (!tb[LOG_MSG] || !tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME])
- continue;
-
- t = blobmsg_get_u64(tb[LOG_TIME]);
- p = blobmsg_get_u32(tb[LOG_PRIO]);
- c = ctime(&t);
- c[strlen(c) - 1] = '\0';
-
- printf("%s %s.%s%s %s\n",
- c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
- (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
- blobmsg_get_string(tb[LOG_MSG]));
- }