diff mbox

[OpenWrt-Devel] logread: add support for non-transparent-framing for Syslog over TCP

Message ID 1427906006-25548-1-git-send-email-champetier.etienne@gmail.com
State Superseded
Headers show

Commit Message

Etienne Champetier April 1, 2015, 4:33 p.m. UTC
https://tools.ietf.org/html/rfc6587#section-3.4.2
the idea is to add \n or \0 as message trailer so it's easier
to find message end

Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
---
 log/logread.c | 38 +++++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

Comments

John Crispin April 1, 2015, 5:25 p.m. UTC | #1
Hi,

i have just cooked up an alternative patch to do this. i think we should
make \0 or \n a default and then add an option to switch to the other.
which one would make more sense as a default ?


	John

On 01/04/2015 18:33, Etienne CHAMPETIER wrote:
> https://tools.ietf.org/html/rfc6587#section-3.4.2
> the idea is to add \n or \0 as message trailer so it's easier
> to find message end
> 
> Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
> ---
>  log/logread.c | 38 +++++++++++++++++++++++++++++++-------
>  1 file changed, 31 insertions(+), 7 deletions(-)
> 
> diff --git a/log/logread.c b/log/logread.c
> index 06dda62..c791a85 100644
> --- a/log/logread.c
> +++ b/log/logread.c
> @@ -59,7 +59,7 @@ static struct uloop_timeout retry;
>  static struct uloop_fd sender;
>  static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
>  static int log_type = LOG_STDOUT;
> -static int log_size, log_udp, log_follow = 0;
> +static int log_size, log_udp, log_follow, log_trailer_null, log_trailer_lf = 0;
>  
>  static const char* getcodetext(int value, CODE *codetable) {
>  	CODE *i;
> @@ -149,10 +149,26 @@ static int log_notify(struct blob_attr *msg)
>  		if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
>  			strncat(buf, "kernel: ", sizeof(buf) - strlen(buf) - 1);
>  		strncat(buf, m, sizeof(buf) - strlen(buf) - 1);
> -		if (log_udp)
> +		if (log_udp) {
>  			err = write(sender.fd, buf, strlen(buf));
> -		else
> -			err = send(sender.fd, buf, strlen(buf), 0);
> +		} else {
> +			size_t buflen = strlen(buf);
> +			if (log_trailer_lf) {
> +				if (buflen == sizeof(buf)-1) {
> +					//buf is full, overwrite last char
> +					buf[buflen-1] = '\n';
> +				} else {
> +					buf[buflen] = '\n';
> +					buf[buflen+1] = '\0';
> +					buflen++;
> +				}
> +			}
> +			//buf is already null terminated
> +			if (log_trailer_null)
> +				buflen++;
> +
> +			err = send(sender.fd, buf, buflen, 0);
> +		}
>  
>  		if (err < 0) {
>  			syslog(LOG_INFO, "failed to send log data to %s:%s via %s\n",
> @@ -182,14 +198,16 @@ static int usage(const char *prog)
>  		"Options:\n"
>  		"    -s <path>		Path to ubus socket\n"
>  		"    -l	<count>		Got only the last 'count' messages\n"
> -		"    -r	<server> <port>	Stream message to a server\n"
>  		"    -F	<file>		Log file\n"
>  		"    -S	<bytes>		Log size\n"
>  		"    -p	<file>		PID file\n"
> +		"    -f			Follow log messages\n"
> +		"    -r	<server> <port>	Stream message to a server\n"
>  		"    -h	<hostname>	Add hostname to the message\n"
>  		"    -P	<prefix>	Prefix custom text to streamed messages\n"
> -		"    -f			Follow log messages\n"
>  		"    -u			Use UDP as the protocol\n"
> +		"    -t			Add null byte at the end of message when using TCP protocol\n"
> +		"    -T			Add LF at the end of message when using TCP protocol\n"
>  		"\n", prog);
>  	return 1;
>  }
> @@ -234,11 +252,17 @@ int main(int argc, char **argv)
>  
>  	signal(SIGPIPE, SIG_IGN);
>  
> -	while ((ch = getopt(argc, argv, "ufcs:l:r:F:p:S:P:h:")) != -1) {
> +	while ((ch = getopt(argc, argv, "utTfcs:l:r:F:p:S:P:h:")) != -1) {
>  		switch (ch) {
>  		case 'u':
>  			log_udp = 1;
>  			break;
> +		case 't':
> +			log_trailer_null = 1;
> +			break;
> +		case 'T':
> +			log_trailer_lf = 1;
> +			break;
>  		case 's':
>  			ubus_socket = optarg;
>  			break;
>
Etienne Champetier April 1, 2015, 5:40 p.m. UTC | #2
Hi,

Le 1 avr. 2015 19:25, "John Crispin" <blogic@openwrt.org> a écrit :
>
> Hi,
>
> i have just cooked up an alternative patch to do this. i think we should
> make \0 or \n a default and then add an option to switch to the other.
> which one would make more sense as a default ?
>

Thanks for your quick answer!
I think \n is more common and understoud by every syslog/TCP receiver (1
line = 1 log), so you should put this as default.
I prefer \0 because it's very unlikely to find it in original log.

Etienne
>
>         John
>
> On 01/04/2015 18:33, Etienne CHAMPETIER wrote:
> > https://tools.ietf.org/html/rfc6587#section-3.4.2
> > the idea is to add \n or \0 as message trailer so it's easier
> > to find message end
> >
> > Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
> > ---
diff mbox

Patch

diff --git a/log/logread.c b/log/logread.c
index 06dda62..c791a85 100644
--- a/log/logread.c
+++ b/log/logread.c
@@ -59,7 +59,7 @@  static struct uloop_timeout retry;
 static struct uloop_fd sender;
 static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
 static int log_type = LOG_STDOUT;
-static int log_size, log_udp, log_follow = 0;
+static int log_size, log_udp, log_follow, log_trailer_null, log_trailer_lf = 0;
 
 static const char* getcodetext(int value, CODE *codetable) {
 	CODE *i;
@@ -149,10 +149,26 @@  static int log_notify(struct blob_attr *msg)
 		if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
 			strncat(buf, "kernel: ", sizeof(buf) - strlen(buf) - 1);
 		strncat(buf, m, sizeof(buf) - strlen(buf) - 1);
-		if (log_udp)
+		if (log_udp) {
 			err = write(sender.fd, buf, strlen(buf));
-		else
-			err = send(sender.fd, buf, strlen(buf), 0);
+		} else {
+			size_t buflen = strlen(buf);
+			if (log_trailer_lf) {
+				if (buflen == sizeof(buf)-1) {
+					//buf is full, overwrite last char
+					buf[buflen-1] = '\n';
+				} else {
+					buf[buflen] = '\n';
+					buf[buflen+1] = '\0';
+					buflen++;
+				}
+			}
+			//buf is already null terminated
+			if (log_trailer_null)
+				buflen++;
+
+			err = send(sender.fd, buf, buflen, 0);
+		}
 
 		if (err < 0) {
 			syslog(LOG_INFO, "failed to send log data to %s:%s via %s\n",
@@ -182,14 +198,16 @@  static int usage(const char *prog)
 		"Options:\n"
 		"    -s <path>		Path to ubus socket\n"
 		"    -l	<count>		Got only the last 'count' messages\n"
-		"    -r	<server> <port>	Stream message to a server\n"
 		"    -F	<file>		Log file\n"
 		"    -S	<bytes>		Log size\n"
 		"    -p	<file>		PID file\n"
+		"    -f			Follow log messages\n"
+		"    -r	<server> <port>	Stream message to a server\n"
 		"    -h	<hostname>	Add hostname to the message\n"
 		"    -P	<prefix>	Prefix custom text to streamed messages\n"
-		"    -f			Follow log messages\n"
 		"    -u			Use UDP as the protocol\n"
+		"    -t			Add null byte at the end of message when using TCP protocol\n"
+		"    -T			Add LF at the end of message when using TCP protocol\n"
 		"\n", prog);
 	return 1;
 }
@@ -234,11 +252,17 @@  int main(int argc, char **argv)
 
 	signal(SIGPIPE, SIG_IGN);
 
-	while ((ch = getopt(argc, argv, "ufcs:l:r:F:p:S:P:h:")) != -1) {
+	while ((ch = getopt(argc, argv, "utTfcs:l:r:F:p:S:P:h:")) != -1) {
 		switch (ch) {
 		case 'u':
 			log_udp = 1;
 			break;
+		case 't':
+			log_trailer_null = 1;
+			break;
+		case 'T':
+			log_trailer_lf = 1;
+			break;
 		case 's':
 			ubus_socket = optarg;
 			break;