LIBNL 3.0 classid lookup path

Thomas Graf tgraf at infradead.org
Thu Mar 24 16:18:14 EDT 2011


On Thu, Mar 24, 2011 at 06:40:54PM +0100, nicolas sitbon wrote:
> Le jeudi 24 mars 2011, Thomas Graf <tgraf at infradead.org> a écrit :
> > On Thu, Mar 24, 2011 at 05:02:11PM +0100, Sitbon Nicolas wrote:
> >> I'm currently looking for potential issues for migrating my
> >> application from libnl 2.0 to the newest 3.0.
> >>
> >> It seems libnl try to read the file /etc/libnl/classid, /etc/libnl
> >> comes from SYSCONFDIR (lib/route/classid.c:313) which is a compile
> >> time constant. My problem is that I don't know where my clients will
> >> install my application (libnl is shipped with it and it's a closed
> >> source application) so I can't configure (--sysconfdir) the path at
> >> compile time. Do you think it might be possible to add a way to
> >> modify the path to classid at runtime (environment variable)?
> >
> > I guess adding support for recognizing an environment variable
> > NLSYSCONFDIR would solve your problem?

commit f523f297f7fed8b64fe4c2a6e3791d31b2d2448b
Author: Thomas Graf <tgraf at suug.ch>
Date:   Thu Mar 24 21:14:52 2011 +0100

    Allow NLSYSCONFDIR environment variable to overwrite built-in sysconfdir

diff --git a/include/netlink-local.h b/include/netlink-local.h
index 32e8302..9acc0e4 100644
--- a/include/netlink-local.h
+++ b/include/netlink-local.h
@@ -186,4 +186,16 @@ static inline int wait_for_ack(struct nl_sock *sk)
                return nl_wait_for_ack(sk);
 }
 
+static inline int build_sysconf_path(char **strp, const char *filename)
+{
+       char *sysconfdir;
+
+       sysconfdir = getenv("NLSYSCONFDIR");
+
+       if (!sysconfdir)
+               sysconfdir = SYSCONFDIR;
+
+       return asprintf(strp, "%s/%s", sysconfdir, filename);
+}
+
 #endif
diff --git a/lib/route/classid.c b/lib/route/classid.c
index 35cafe6..6af0ee3 100644
--- a/lib/route/classid.c
+++ b/lib/route/classid.c
@@ -310,7 +310,8 @@ int rtnl_tc_read_classid_file(void)
        FILE *fd;
        int err;
 
-       asprintf(&path, "%s/classid", SYSCONFDIR);
+       if (build_sysconf_path(&path, "classid") < 0)
+               return -NLE_NOMEM;
 
        /* if stat fails, just (re-)read the file */
        if (stat(path, &st) == 0) {
@@ -392,7 +393,7 @@ int rtnl_classid_generate(const char *name, uint32_t *result, uint32_t parent)
 
        NL_DBG(2, "Generated new classid %#x\n", classid);
 
-       if (asprintf(&path, "%s/classid", SYSCONFDIR) < 0)
+       if (build_sysconf_path(&path, "classid") < 0)
                return -NLE_NOMEM;
 
        if (!(fd = fopen(path, "a"))) {
diff --git a/lib/route/pktloc.c b/lib/route/pktloc.c
index 4d7d9dd..b677ab5 100644
--- a/lib/route/pktloc.c
+++ b/lib/route/pktloc.c
@@ -94,7 +94,8 @@ static int read_pktlocs(void)
        int i, err;
        FILE *fd;
 
-       asprintf(&path, "%s/pktloc", SYSCONFDIR);
+       if (build_sysconf_path(&path, "pktloc") < 0)
+               return -NLE_NOMEM;
 
        /* if stat fails, just try to read the file */
        if (stat(path, &st) == 0) {
diff --git a/src/Makefile.am b/src/Makefile.am
index 64d1cce..e196b58 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,7 +2,7 @@
 
 SUBDIRS = lib
 
-AM_CPPFLAGS  = -Wall -I${top_srcdir}/include -I${top_builddir}/include -D_GNU_SOURCE
+AM_CPPFLAGS  = -Wall -I${top_srcdir}/include -I${top_builddir}/include -D_GNU_SOURCE -DSYSCONFDIR
 AM_LDFLAGS = -L${top_builddir}/lib -L${top_builddir}/src/lib -lnl-cli -lnl -lnl-nf -lnl-genl -lnl
 
 sbin_PROGRAMS = \




More information about the libnl mailing list