[LEDE-DEV] [PATCH 2/2] [ubox] logd: add ubus reload method

John Crispin john at phrozen.org
Sat May 14 12:33:05 PDT 2016



On 12/05/2016 14:09, Dan Bugnar wrote:
> From: Dan Bugnar <danutbug at gmail.com>
> 
> Add logd link to uci library, to read the system config file and get the buffer size.
> Remove the -S option support and use just the option from the config file.
> 
> Signed-off-by: Dan Bugnar <danutbug at gmail.com>

NAK, logd should not get a dependency on uci. if you want to change the
buffer size there are 2 options

1) restart the log daemon with a bigger default buffer
2) add a ubus call that will realloc the buffer and copy the existing data

	John

> ---
>  CMakeLists.txt |  2 +-
>  log/logd.c     | 60 ++++++++++++++++++++++++++++++++++++++++++++--------------
>  log/syslog.c   |  8 +-------
>  log/syslog.h   |  2 +-
>  4 files changed, 49 insertions(+), 23 deletions(-)
> 
> diff --git a/CMakeLists.txt b/CMakeLists.txt
> index 834b5b6..b635c4a 100644
> --- a/CMakeLists.txt
> +++ b/CMakeLists.txt
> @@ -42,7 +42,7 @@ INSTALL(TARGETS validate_data
>  )
>  
>  ADD_EXECUTABLE(logd log/logd.c log/syslog.c)
> -TARGET_LINK_LIBRARIES(logd ubox ubus)
> +TARGET_LINK_LIBRARIES(logd ubox ubus uci)
>  INSTALL(TARGETS logd
>  	RUNTIME DESTINATION sbin
>  )
> diff --git a/log/logd.c b/log/logd.c
> index 27d3cac..6e493d0 100644
> --- a/log/logd.c
> +++ b/log/logd.c
> @@ -23,9 +23,14 @@
>  #include <libubox/list.h>
>  #include <libubox/ustream.h>
>  #include <libubus.h>
> +#include <uci.h>
>  
>  #include "syslog.h"
>  
> +#define SYSTEM_CONFIG_PATH "/etc/config"
> +#define SYSTEM_CONFIG "system"
> +#define LOG_DEFAULT_SIZE 16
> +
>  int debug = 0;
>  static struct blob_buf b;
>  static struct ubus_auto_conn conn;
> @@ -124,9 +129,48 @@ write_log(struct ubus_context *ctx, struct ubus_object *obj,
>  	return 0;
>  }
>  
> +static void
> +config_reload()
> +{
> +	struct uci_context *ctx;
> +	struct uci_package *p;
> +	struct uci_element *e;
> +	int size = LOG_DEFAULT_SIZE;
> +	
> +	ctx = uci_alloc_context();
> +	if (!ctx) {
> +		fprintf(stderr, "Could not allocate memory for config\n");
> +		exit(-1);
> +	}
> +	ctx->flags &= ~UCI_FLAG_STRICT;
> +	uci_set_confdir(ctx, SYSTEM_CONFIG_PATH);
> +	if (uci_load(ctx, SYSTEM_CONFIG, &p) == 0){
> +		uci_foreach_element(&p->sections, e){
> +			struct uci_section *s = uci_to_section(e);
> +			if (strcmp(s->type, "system") == 0){
> +				const char *buffer_size = uci_lookup_option_string(ctx, s, "log_buffer_size");
> +				if (buffer_size != NULL)
> +					size = atoi(buffer_size);
> +				break;
> +			}
> +		}
> +	}
> +	uci_free_context(ctx);
> +	log_buffer_reinit(size * 1024);
> +}
> +
> +static int
> +reload_log(struct ubus_context *ctx, struct ubus_object *obj,
> +		struct ubus_request_data *req, const char *method,
> +		struct blob_attr *msg)
> +{
> +	config_reload();
> +	return 0;
> +}
>  static const struct ubus_method log_methods[] = {
>  	{ .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
>  	{ .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
> +	{ .name = "reload", .handler = reload_log },
>  };
>  
>  static struct ubus_object_type log_object_type =
> @@ -176,22 +220,10 @@ ubus_connect_handler(struct ubus_context *ctx)
>  int
>  main(int argc, char **argv)
>  {
> -	int ch, log_size = 16;
> -
>  	signal(SIGPIPE, SIG_IGN);
> -	while ((ch = getopt(argc, argv, "S:")) != -1) {
> -		switch (ch) {
> -		case 'S':
> -			log_size = atoi(optarg);
> -			if (log_size < 1)
> -				log_size = 16;
> -			break;
> -		}
> -	}
> -	log_size *= 1024;
> -
>  	uloop_init();
> -	log_init(log_size);
> +	config_reload();
> +	log_init();
>  	conn.cb = ubus_connect_handler;
>  	ubus_auto_connect(&conn);
>  	uloop_run();
> diff --git a/log/syslog.c b/log/syslog.c
> index d6cb868..683eeb8 100644
> --- a/log/syslog.c
> +++ b/log/syslog.c
> @@ -270,16 +270,10 @@ log_buffer_reinit(int size)
>  }
>  
>  void
> -log_init(int _log_size)
> +log_init()
>  {
>  	regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED);
>  	regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED);
> -
> -	if (log_buffer_reinit(_log_size)) {
> -		fprintf(stderr, "Failed to allocate log memory\n");
> -		exit(-1);
> -	}
> -
>  	syslog_open();
>  	klog_open();
>  	openlog("sysinit", LOG_CONS, LOG_DAEMON);
> diff --git a/log/syslog.h b/log/syslog.h
> index ed5a41b..fe815b9 100644
> --- a/log/syslog.h
> +++ b/log/syslog.h
> @@ -30,7 +30,7 @@ struct log_head {
>  	char data[];
>  };
>  
> -void log_init(int log_size);
> +void log_init();
>  void log_shutdown(void);
>  
>  typedef void (*log_list_cb)(struct log_head *h);
> 



More information about the Lede-dev mailing list