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/ustream.h>
29 #include <libubox/blobmsg_json.h>
30 #include <libubox/usock.h>
31 #include <libubox/uloop.h>
50 static const struct blobmsg_policy log_policy
[] = {
51 [LOG_MSG
] = { .name
= "msg", .type
= BLOBMSG_TYPE_STRING
},
52 [LOG_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
53 [LOG_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
54 [LOG_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_INT32
},
55 [LOG_TIME
] = { .name
= "time", .type
= BLOBMSG_TYPE_INT64
},
58 static struct uloop_timeout retry
;
59 static struct uloop_fd sender
;
60 static const char *log_file
, *log_ip
, *log_port
, *log_prefix
, *pid_file
, *hostname
;
61 static int log_type
= LOG_STDOUT
;
62 static int log_size
, log_udp
, log_follow
= 0;
64 static const char* getcodetext(int value
, CODE
*codetable
) {
68 for (i
= codetable
; i
->c_val
!= -1; i
++)
69 if (i
->c_val
== value
)
74 static void log_handle_reconnect(struct uloop_timeout
*timeout
)
76 sender
.fd
= usock((log_udp
) ? (USOCK_UDP
) : (USOCK_TCP
), log_ip
, log_port
);
78 fprintf(stderr
, "failed to connect: %s\n", strerror(errno
));
79 uloop_timeout_set(&retry
, 1000);
81 uloop_fd_add(&sender
, ULOOP_READ
);
82 syslog(0, "Logread connected to %s:%s\n", log_ip
, log_port
);
86 static void log_handle_fd(struct uloop_fd
*u
, unsigned int events
)
92 uloop_timeout_set(&retry
, 1000);
96 static int log_notify(struct blob_attr
*msg
)
98 struct blob_attr
*tb
[__LOG_MAX
];
109 blobmsg_parse(log_policy
, ARRAY_SIZE(log_policy
), tb
, blob_data(msg
), blob_len(msg
));
110 if (!tb
[LOG_ID
] || !tb
[LOG_PRIO
] || !tb
[LOG_SOURCE
] || !tb
[LOG_TIME
] || !tb
[LOG_MSG
])
113 if ((log_type
== LOG_FILE
) && log_size
&& (!stat(log_file
, &s
)) && (s
.st_size
> log_size
)) {
114 char *old
= malloc(strlen(log_file
) + 5);
118 sprintf(old
, "%s.old", log_file
);
119 rename(log_file
, old
);
122 sender
.fd
= open(log_file
, O_CREAT
| O_WRONLY
| O_APPEND
, 0600);
124 fprintf(stderr
, "failed to open %s: %s\n", log_file
, strerror(errno
));
129 m
= blobmsg_get_string(tb
[LOG_MSG
]);
130 t
= blobmsg_get_u64(tb
[LOG_TIME
]) / 1000;
132 p
= blobmsg_get_u32(tb
[LOG_PRIO
]);
133 c
[strlen(c
) - 1] = '\0';
134 str
= blobmsg_format_json(msg
, true);
135 if (log_type
== LOG_NET
) {
140 snprintf(buf
, sizeof(buf
), "%s ", hostname
);
142 strncat(buf
, log_prefix
, sizeof(buf
));
143 strncat(buf
, ": ", sizeof(buf
));
145 if (blobmsg_get_u32(tb
[LOG_SOURCE
]) == SOURCE_KLOG
)
146 strncat(buf
, "kernel: ", sizeof(buf
));
147 strncat(buf
, m
, sizeof(buf
));
149 err
= write(sender
.fd
, buf
, strlen(buf
));
151 err
= send(sender
.fd
, buf
, strlen(buf
), 0);
154 syslog(0, "failed to send log data to %s:%s via %s\n",
155 log_ip
, log_port
, (log_udp
) ? ("udp") : ("tcp"));
156 uloop_fd_delete(&sender
);
159 uloop_timeout_set(&retry
, 1000);
162 snprintf(buf
, sizeof(buf
), "%s %s.%s%s %s\n",
163 c
, getcodetext(LOG_FAC(p
) << 3, facilitynames
), getcodetext(LOG_PRI(p
), prioritynames
),
164 (blobmsg_get_u32(tb
[LOG_SOURCE
])) ? ("") : (" kernel:"), m
);
165 write(sender
.fd
, buf
, strlen(buf
));
169 if (log_type
== LOG_FILE
)
175 static int usage(const char *prog
)
177 fprintf(stderr
, "Usage: %s [options]\n"
179 " -s <path> Path to ubus socket\n"
180 " -l <count> Got only the last 'count' messages\n"
181 " -r <server> <port> Stream message to a server\n"
182 " -F <file> Log file\n"
183 " -S <bytes> Log size\n"
184 " -p <file> PID file\n"
185 " -h <hostname> Add hostname to the message\n"
186 " -P <prefix> Prefix custom text to streamed messages\n"
187 " -f Follow log messages\n"
188 " -u Use UDP as the protocol\n"
193 static void logread_fd_data_cb(struct ustream
*s
, int bytes
)
199 a
= (void*) ustream_get_read_buf(s
, &len
);
200 if (len
< sizeof(*a
) || len
< blob_len(a
) + sizeof(*a
))
203 ustream_consume(s
, blob_len(a
) + sizeof(*a
));
209 static void logread_fd_cb(struct ubus_request
*req
, int fd
)
211 static struct ustream_fd test_fd
;
213 test_fd
.stream
.notify_read
= logread_fd_data_cb
;
214 ustream_fd_init(&test_fd
, fd
);
217 static void logread_complete_cb(struct ubus_request
*req
, int ret
)
221 int main(int argc
, char **argv
)
223 static struct ubus_request req
;
224 struct ubus_context
*ctx
;
226 const char *ubus_socket
= NULL
;
227 int ch
, ret
, lines
= 0;
228 static struct blob_buf b
;
231 signal(SIGPIPE
, SIG_IGN
);
233 while ((ch
= getopt(argc
, argv
, "ufcs:l:r:F:p:S:P:h:")) != -1) {
239 ubus_socket
= optarg
;
243 log_port
= argv
[optind
++];
258 lines
= atoi(optarg
);
261 log_size
= atoi(optarg
);
275 ctx
= ubus_connect(ubus_socket
);
277 fprintf(stderr
, "Failed to connect to ubus\n");
282 /* ugly ugly ugly ... we need a real reconnect logic */
284 ret
= ubus_lookup_id(ctx
, "log", &id
);
286 fprintf(stderr
, "Failed to find log object: %s\n", ubus_strerror(ret
));
291 blob_buf_init(&b
, 0);
293 blobmsg_add_u32(&b
, "lines", lines
);
295 blobmsg_add_u32(&b
, "lines", 0);
298 FILE *fp
= fopen(pid_file
, "w+");
300 fprintf(fp
, "%d", getpid());
306 if (log_ip
&& log_port
) {
307 openlog("logread", LOG_PID
, LOG_DAEMON
);
309 sender
.cb
= log_handle_fd
;
310 retry
.cb
= log_handle_reconnect
;
311 uloop_timeout_set(&retry
, 1000);
312 } else if (log_file
) {
314 sender
.fd
= open(log_file
, O_CREAT
| O_WRONLY
| O_APPEND
, 0600);
316 fprintf(stderr
, "failed to open %s: %s\n", log_file
, strerror(errno
));
320 sender
.fd
= STDOUT_FILENO
;
323 ubus_invoke_async(ctx
, id
, "read", b
.head
, &req
);
324 req
.fd_cb
= logread_fd_cb
;
325 req
.complete_cb
= logread_complete_cb
;
326 ubus_complete_request_async(ctx
, &req
);
332 } while (ret
&& tries
--);