Skip to content

Commit

Permalink
confd,statd: add support for enabling DEBUG log messages at runtime
Browse files Browse the repository at this point in the history
 - add debug flag to trigger DEBUG() statements
 - DEBUG() macro needs 'path' argument

For statd we can add support for an optional '-d' command line option to
trigger, and SIGUSR1 to toggle, debug mode.  TBD for confd which still
remains to be refactored into the same standalone daemon as statd.

Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Aug 2, 2023
1 parent 9295e51 commit bef2ee1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/confd/src/confd/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../lib/common.h"

static struct confd confd;
int debug;

uint32_t core_hook_prio(void)
{
Expand Down Expand Up @@ -66,9 +67,18 @@ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *modul

int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
{
int log_opts = LOG_USER;
int rc = SR_ERR_SYS;
char *env;

openlog("confd", LOG_USER, 0);
/* Convert into command line option+SIGUSR1 when converting to standalone confd */
env = getenv("DEBUG");
if (env) {
log_opts |= LOG_PERROR;
debug = 1;
}

openlog("confd", log_opts, 0);

/* Save context with default running config datastore for all our models */
*priv = (void *)&confd;
Expand Down
4 changes: 2 additions & 2 deletions src/confd/src/confd/ietf-interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ static int netdag_gen_vlan(struct dagger *net, struct lyd_node *dif,
const char *proto;
int err;

DEBUG("");
DEBUG("ifname %s parent %s", ifname, parent);

err = dagger_add_dep(net, ifname, parent);
if (err)
Expand Down Expand Up @@ -844,7 +844,7 @@ static int netdag_gen_vlan(struct dagger *net, struct lyd_node *dif,
fprintf(ip, " id %s", vidd.new);

fputc('\n', ip);
DEBUG("");

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/confd/src/confd/infix-system-software.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static int infix_system_sw_state(sr_session_ctx_t *session, uint32_t sub_id,
RaucInstaller *rauc;
struct lyd_node *sw;

DEBUG("");
DEBUG("%s", path);

rauc = infix_system_sw_new_rauc();
if (!rauc)
Expand Down
6 changes: 4 additions & 2 deletions src/confd/src/lib/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <sysrepo.h>
#include <sysrepo.h>
#include "srx_module.h"
#include "common.h"

extern int debug;

#ifndef HAVE_VASPRINTF
int vasprintf(char **strp, const char *fmt, va_list ap);
Expand All @@ -15,8 +18,7 @@ int vasprintf(char **strp, const char *fmt, va_list ap);
int asprintf(char **strp, const char *fmt, ...);
#endif

#define DEBUG(fmt, ...)
//#define DEBUG(fmt, ...) syslog(LOG_DEBUG, "%s: "fmt, __func__, ##__VA_ARGS__)
#define DEBUG(fmt, ...) do { if (debug) syslog(LOG_DEBUG, fmt, ##__VA_ARGS__); } while (0)
#define INFO(fmt, ...) syslog(LOG_INFO, fmt, ##__VA_ARGS__)
#define ERROR(fmt, ...) syslog(LOG_ERR, "%s: " fmt, __func__, ##__VA_ARGS__)
#define ERRNO(fmt, ...) syslog(LOG_ERR, "%s: " fmt ": %s", __func__, ##__VA_ARGS__, strerror(errno))
Expand Down
2 changes: 2 additions & 0 deletions src/confd/src/lib/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <sys/wait.h>
#include <libite/lite.h>

int debug; /* Sets debug level (0:off) */

/* TODO remove once confd / statd lib situation is resolved */
#ifndef vasprintf
int vasprintf(char **strp, const char *fmt, va_list ap);
Expand Down
2 changes: 1 addition & 1 deletion src/statd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CPPFLAGS := -I$(CONFD_SRC_LIB)
TARGET = statd
SRC = statd.c
LIB_SRC = $(CONFD_SRC_LIB)/helpers.c $(CONFD_SRC_LIB)/lyx.c $(CONFD_LIB)/vasprintf.c
LIB_HDR = $(CONFD_SRC_LIB)/helpers.h $(CONFD_SRC_LIB)/lyx.h
LIB_HDR = $(CONFD_SRC_LIB)/helpers.h $(CONFD_SRC_LIB)/lyx.h $(CONFD_SRC_LIB)/common.h

all: $(TARGET)

Expand Down
32 changes: 25 additions & 7 deletions src/statd/statd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define NL_BUF_SIZE 4096 /* Arbitrary chosen */

TAILQ_HEAD(sub_head, sub);
int debug;

/* This should, with some modifications, be able to hold other subscription
* types, not only interfaces.
Expand Down Expand Up @@ -235,7 +236,7 @@ static int ly_add_ip_link(const struct ly_ctx *ctx, struct lyd_node **parent, ch
return SR_ERR_OK;
}

static int sr_ifaces_cb(sr_session_ctx_t *session, uint32_t, const char *,
static int sr_ifaces_cb(sr_session_ctx_t *session, uint32_t, const char *path,
const char *, const char *, uint32_t,
struct lyd_node **parent, void *priv)
{
Expand Down Expand Up @@ -267,11 +268,16 @@ static int sr_ifaces_cb(sr_session_ctx_t *session, uint32_t, const char *,
return err;
}

static void sig_event_cb(struct ev_loop *loop, struct ev_signal *, int)
static void sigint_cb(struct ev_loop *loop, struct ev_signal *, int)
{
ev_break(loop, EVBREAK_ALL);
}

static void sigusr1_cb(struct ev_loop *, struct ev_signal *, int)
{
debug ^= 1;
}

static void sr_event_cb(struct ev_loop *, struct ev_io *w, int)
{
struct sub *sub = (struct sub *)w->data;
Expand Down Expand Up @@ -442,13 +448,21 @@ static int sub_to_ifaces(struct statd *statd)
return SR_ERR_OK;
}

int main(void)
int main(int argc, char *argv[])
{
struct ev_signal sig_watcher;
struct ev_signal sigint_watcher, sigusr1_watcher;
struct statd statd = {};
int log_opts = LOG_USER;
sr_conn_ctx_t *sr_conn;
int err;

if (argc > 1 && !strcmp(argv[1], "-d")) {
log_opts |= LOG_PERROR;
debug = 1;
}

openlog("statd", log_opts, 0);

TAILQ_INIT(&statd.subs);
statd.ev_loop = EV_DEFAULT;

Expand Down Expand Up @@ -484,9 +498,13 @@ int main(void)
return EXIT_FAILURE;
}

ev_signal_init(&sig_watcher, sig_event_cb, SIGINT);
sig_watcher.data = &statd;
ev_signal_start(statd.ev_loop, &sig_watcher);
ev_signal_init(&sigint_watcher, sigint_cb, SIGINT);
sigint_watcher.data = &statd;
ev_signal_start(statd.ev_loop, &sigint_watcher);

ev_signal_init(&sigusr1_watcher, sigusr1_cb, SIGUSR1);
sigusr1_watcher.data = &statd;
ev_signal_start(statd.ev_loop, &sigusr1_watcher);

ev_io_init(&statd.nl.watcher, nl_event_cb, statd.nl.sd, EV_READ);
statd.nl.watcher.data = &statd;
Expand Down

0 comments on commit bef2ee1

Please sign in to comment.