Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statd,confd: misc. fixes with improved debug support #82

Merged
merged 2 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/confd/src/confd/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,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
81 changes: 49 additions & 32 deletions src/statd/statd.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void set_sock_rcvbuf(int sd, int size)
perror("setsockopt");
return;
}
DEBUG("Socket receive buffer size increased to: %d bytes\n", size);
DEBUG("Socket receive buffer size increased to: %d bytes", size);
}

static int nl_sock_init(void)
Expand All @@ -67,7 +67,7 @@ static int nl_sock_init(void)

sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock < 0) {
ERROR("Error, creating netlink socket: %s\n", strerror(errno));
ERROR("Error, creating netlink socket: %s", strerror(errno));
return -1;
}

Expand All @@ -76,7 +76,7 @@ static int nl_sock_init(void)
addr.nl_groups = RTMGRP_LINK;

if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
ERROR("Error, binding netlink socket: %s\n", strerror(errno));
ERROR("Error, binding netlink socket: %s", strerror(errno));
close(sock);
return -1;
}
Expand Down Expand Up @@ -235,7 +235,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 All @@ -244,34 +244,39 @@ static int sr_ifaces_cb(sr_session_ctx_t *session, uint32_t, const char *,
sr_conn_ctx_t *con;
int err;

DEBUG("Incoming query for xpath: %s\n", path);
DEBUG("Incoming query for xpath: %s", path);

con = sr_session_get_connection(session);
if (!con) {
ERROR("Error, getting connection\n");
ERROR("Error, getting connection");
return SR_ERR_INTERNAL;
}

ctx = sr_acquire_context(con);
if (!ctx) {
ERROR("Error, acquiring context\n");
ERROR("Error, acquiring context");
return SR_ERR_INTERNAL;
}

err = ly_add_ip_link(ctx, parent, sub->ifname);
if (err)
ERROR("Error, adding ip link info\n");
ERROR("Error, adding ip link info");

sr_release_context(con);

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 All @@ -288,7 +293,7 @@ static int sub_to_iface(struct statd *statd, const char *ifname)

sub = sub_find_iface(&statd->subs, ifname);
if (sub) {
DEBUG("Interface %s already subscribed\n", ifname);
DEBUG("Interface %s already subscribed", ifname);
return SR_ERR_OK;
}

Expand All @@ -302,20 +307,20 @@ static int sub_to_iface(struct statd *statd, const char *ifname)
snprintf(path, sizeof(path), "%s/interface[name='%s']",
XPATH_IFACE_BASE, ifname);

DEBUG("Subscribe to events for \"%s\"\n", path);
DEBUG("Subscribe to events for \"%s\"", path);
err = sr_oper_get_subscribe(statd->sr_ses, "ietf-interfaces",
path, sr_ifaces_cb, sub,
SR_SUBSCR_DEFAULT | SR_SUBSCR_NO_THREAD | SR_SUBSCR_DONE_ONLY,
&sub->sr_sub);
if (err) {
ERROR("Error, subscribing to path \"%s\": %s\n", path, sr_strerror(err));
ERROR("Error, subscribing to path \"%s\": %s", path, sr_strerror(err));
free(sub);
return SR_ERR_INTERNAL;
}

err = sr_get_event_pipe(sub->sr_sub, &sr_ev_pipe);
if (err) {
ERROR("Error, getting sysrepo event pipe: %s\n", sr_strerror(err));
ERROR("Error, getting sysrepo event pipe: %s", sr_strerror(err));
sr_unsubscribe(sub->sr_sub);
free(sub);
return SR_ERR_INTERNAL;
Expand All @@ -336,7 +341,7 @@ static void unsub_to_ifaces(struct statd *statd)

while (!TAILQ_EMPTY(&statd->subs)) {
sub = TAILQ_FIRST(&statd->subs);
DEBUG("Unsubscribe from \"%s\" (all)\n", sub->ifname);
DEBUG("Unsubscribe from \"%s\" (all)", sub->ifname);
sub_delete(statd->ev_loop, &statd->subs, sub);
}
}
Expand All @@ -350,7 +355,7 @@ static int unsub_to_iface(struct statd *statd, char *ifname)
ERROR("Error, can't find interface to delete (%s)", ifname);
return SR_ERR_INTERNAL;
}
DEBUG("Unsubscribe from \"%s\"\n", sub->ifname);
DEBUG("Unsubscribe from \"%s\"", sub->ifname);
sub_delete(statd->ev_loop, &statd->subs, sub);

return SR_ERR_OK;
Expand Down Expand Up @@ -393,7 +398,7 @@ static void nl_event_cb(struct ev_loop *, struct ev_io *w, int)

len = recv(statd->nl.sd, buf, sizeof(buf), 0);
if (len < 0) {
ERROR("Error, netlink recv failed: %s\n", strerror(errno));
ERROR("Error, netlink recv failed: %s", strerror(errno));
close(statd->nl.sd);
/* NOTE: This is likely caused by a full kernel buffer, which
* means we can't trust our list. So we exit hard and let finit
Expand All @@ -405,7 +410,7 @@ static void nl_event_cb(struct ev_loop *, struct ev_io *w, int)
for (nlh = (struct nlmsghdr *)buf; NLMSG_OK(nlh, len); nlh = NLMSG_NEXT(nlh, len)) {
err = nl_process_msg(nlh, statd);
if (err)
ERROR("Error, processing netlink message: %s\n", sr_strerror(err));
ERROR("Error, processing netlink message: %s", sr_strerror(err));
}
}

Expand Down Expand Up @@ -442,61 +447,73 @@ 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;

statd.nl.sd = nl_sock_init();
if (statd.nl.sd < 0) {
ERROR("Error, opening netlink socket\n");
ERROR("Error, opening netlink socket");
return EXIT_FAILURE;
}
INFO("Status daemon starting\n");
INFO("Status daemon starting");

set_sock_rcvbuf(statd.nl.sd, SOCK_RMEM_SIZE);

err = sr_connect(SR_CONN_DEFAULT, &sr_conn);
if (err) {
ERROR("Error, connecting to sysrepo: %s\n", sr_strerror(err));
ERROR("Error, connecting to sysrepo: %s", sr_strerror(err));
return EXIT_FAILURE;
}
DEBUG("Connected to sysrepo\n");
DEBUG("Connected to sysrepo");

err = sr_session_start(sr_conn, SR_DS_OPERATIONAL, &statd.sr_ses);
if (err) {
ERROR("Error, start sysrepo session: %s\n", sr_strerror(err));
ERROR("Error, start sysrepo session: %s", sr_strerror(err));
sr_disconnect(sr_conn);
return EXIT_FAILURE;
}
DEBUG("Session started (%p)\n", statd.sr_ses);
DEBUG("Session started (%p)", statd.sr_ses);

DEBUG("Attempting to register existing interfaces\n");
DEBUG("Attempting to register existing interfaces");
err = sub_to_ifaces(&statd);
if (err) {
ERROR("Error, registering existing interfaces\n");
ERROR("Error, registering existing interfaces");
sr_disconnect(sr_conn);
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;
ev_io_start(statd.ev_loop, &statd.nl.watcher);

INFO("Status daemon entering main event loop\n");
INFO("Status daemon entering main event loop");
ev_run(statd.ev_loop, 0);

/* We should never get here during normal operation */
INFO("Status daemon shutting down\n");
INFO("Status daemon shutting down");
unsub_to_ifaces(&statd);
sr_session_stop(statd.sr_ses);
sr_disconnect(sr_conn);
Expand Down