[PATCH] lib: Remove struct stat from RIB API

  • From: Dimitri Staessens <dimitri@ouroboros.rocks>
  • To: ouroboros@xxxxxxxxxxxxx
  • Date: Sat, 26 Jun 2021 10:25:04 +0200

The RIB API had a struct stat in the getattr() function, which made
all components that exposed variables via the RIB dependent on
<sys/stat.h>. The rib now has its own struct rib_attr to set
attributes such as size and last modified time.

Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks>
---
 include/ouroboros/rib.h            | 10 ++++++---
 src/ipcpd/ipcp.c                   | 32 +++++++++-------------------
 src/ipcpd/unicast/dt.c             | 34 ++++++++++++------------------
 src/ipcpd/unicast/fa.c             | 34 ++++++++++++------------------
 src/ipcpd/unicast/pol/link_state.c | 34 ++++++++++++------------------
 src/lib/rib.c                      | 20 +++++++++++++-----
 6 files changed, 74 insertions(+), 90 deletions(-)

diff --git a/include/ouroboros/rib.h b/include/ouroboros/rib.h
index c2812bc..3a74bee 100644
--- a/include/ouroboros/rib.h
+++ b/include/ouroboros/rib.h
@@ -25,18 +25,22 @@
 
 #define RIB_PATH_LEN 128
 
-#include <sys/stat.h>
 #include <sys/types.h>
 
 struct rib;
 
+struct rib_attr {
+        size_t size;  /* Size of RIB document  */
+        time_t mtime; /* Last modified time    */
+};
+
 struct rib_ops {
         int (* read)(const char * path,
                      char *       buf,
                      size_t       len);
         int (* readdir)(char *** entries);
-        int (* getattr)(const char *  path,
-                        struct stat * st);
+        int (* getattr)(const char *      path,
+                        struct rib_attr * attr);
 };
 
 int  rib_init(const char * prefix);
diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c
index 56cdb5d..65a28d6 100644
--- a/src/ipcpd/ipcp.c
+++ b/src/ipcpd/ipcp.c
@@ -24,7 +24,6 @@
 #define _DEFAULT_SOURCE
 #else
 #define _POSIX_C_SOURCE 200112L
-#define __XSI_VISIBLE   500
 #endif
 
 #if defined(__linux__) && !defined(DISABLE_CORE_LOCK)
@@ -32,13 +31,6 @@
 #define NPROC (sysconf(_SC_NPROCESSORS_ONLN))
 #endif
 
-#if defined(__linux__) || defined(__CYGWIN__)
-#define _DEFAULT_SOURCE
-#else
-#define _POSIX_C_SOURCE 200112L
-#define __XSI_VISIBLE   500
-#endif
-
 #include "config.h"
 
 #define OUROBOROS_PREFIX  "ipcpd/ipcp"
@@ -107,9 +99,9 @@ void ipcp_hash_str(char *          buf,
         buf[2 * i] = '\0';
 }
 
-static int ipcp_stat_read(const char * path,
-                          char *       buf,
-                          size_t       len)
+static int ipcp_rib_read(const char * path,
+                         char *       buf,
+                         size_t       len)
 {
         if (len < LAYER_NAME_SIZE + 2) /* trailing \n */
                 return 0;
@@ -158,7 +150,7 @@ static int ipcp_stat_read(const char * path,
         return strlen(buf);
 }
 
-static int ipcp_stat_readdir(char *** buf)
+static int ipcp_rib_readdir(char *** buf)
 {
         int  i = 0;
 
@@ -188,24 +180,20 @@ static int ipcp_stat_readdir(char *** buf)
         return -1;
 }
 
-static int ipcp_stat_getattr(const char * path,
-                             struct stat * st)
+static int ipcp_rib_getattr(const char *      path,
+                            struct rib_attr * attr)
 {
         (void) path;
 
-        st->st_mode  = S_IFREG | 0755;
-        st->st_nlink = 1;
-        st->st_uid   = getuid();
-        st->st_gid   = getgid();
-        st->st_size  = LAYER_NAME_SIZE;
+        attr->size = LAYER_NAME_SIZE;
 
         return 0;
 }
 
 static struct rib_ops r_ops = {
-        .read    = ipcp_stat_read,
-        .readdir = ipcp_stat_readdir,
-        .getattr = ipcp_stat_getattr
+        .read    = ipcp_rib_read,
+        .readdir = ipcp_rib_readdir,
+        .getattr = ipcp_rib_getattr
 };
 
 static void * acceptloop(void * o)
diff --git a/src/ipcpd/unicast/dt.c b/src/ipcpd/unicast/dt.c
index c57ce03..38fb51f 100644
--- a/src/ipcpd/unicast/dt.c
+++ b/src/ipcpd/unicast/dt.c
@@ -24,7 +24,6 @@
 #define _DEFAULT_SOURCE
 #else
 #define _POSIX_C_SOURCE 200112L
-#define __XSI_VISIBLE 500
 #endif
 
 #include "config.h"
@@ -176,9 +175,9 @@ struct {
         pthread_t          listener;
 } dt;
 
-static int dt_stat_read(const char * path,
-                        char *       buf,
-                        size_t       len)
+static int dt_rib_read(const char * path,
+                       char *       buf,
+                       size_t       len)
 {
 #ifdef IPCP_FLOW_STATS
         int         fd;
@@ -271,7 +270,7 @@ static int dt_stat_read(const char * path,
 #endif
 }
 
-static int dt_stat_readdir(char *** buf)
+static int dt_rib_readdir(char *** buf)
 {
 #ifdef IPCP_FLOW_STATS
         char   entry[RIB_PATH_LEN + 1];
@@ -329,41 +328,36 @@ static int dt_stat_readdir(char *** buf)
 #endif
 }
 
-static int dt_stat_getattr(const char *  path,
-                           struct stat * st)
+static int dt_rib_getattr(const char *      path,
+                          struct rib_attr * attr)
 {
 #ifdef IPCP_FLOW_STATS
         int fd;
 
         fd = atoi(path);
 
-        st->st_mode  = S_IFREG | 0755;
-        st->st_nlink = 1;
-        st->st_uid   = getuid();
-        st->st_gid   = getgid();
-
         pthread_mutex_lock(&dt.stat[fd].lock);
 
         if (dt.stat[fd].stamp != -1) {
-                st->st_size  = STAT_FILE_LEN;
-                st->st_mtime = dt.stat[fd].stamp;
+                attr->size  = STAT_FILE_LEN;
+                attr->mtime = dt.stat[fd].stamp;
         } else {
-                st->st_size  = 0;
-                st->st_mtime = 0;
+                attr->size  = 0;
+                attr->mtime = 0;
         }
 
         pthread_mutex_unlock(&dt.stat[fd].lock);
 #else
         (void) path;
-        (void) st;
+        (void) attr;
 #endif
         return 0;
 }
 
 static struct rib_ops r_ops = {
-        .read    = dt_stat_read,
-        .readdir = dt_stat_readdir,
-        .getattr = dt_stat_getattr
+        .read    = dt_rib_read,
+        .readdir = dt_rib_readdir,
+        .getattr = dt_rib_getattr
 };
 
 #ifdef IPCP_FLOW_STATS
diff --git a/src/ipcpd/unicast/fa.c b/src/ipcpd/unicast/fa.c
index 14303a2..428b5af 100644
--- a/src/ipcpd/unicast/fa.c
+++ b/src/ipcpd/unicast/fa.c
@@ -24,7 +24,6 @@
 #define _DEFAULT_SOURCE
 #else
 #define _POSIX_C_SOURCE 200112L
-#define __XSI_VISIBLE 500
 #endif
 
 #include "config.h"
@@ -122,9 +121,9 @@ struct {
         struct psched *  psched;
 } fa;
 
-static int fa_stat_read(const char * path,
-                        char *       buf,
-                        size_t       len)
+static int fa_rib_read(const char * path,
+                       char *       buf,
+                       size_t       len)
 {
 #ifdef IPCP_FLOW_STATS
         struct fa_flow * flow;
@@ -197,7 +196,7 @@ static int fa_stat_read(const char * path,
 #endif
 }
 
-static int fa_stat_readdir(char *** buf)
+static int fa_rib_readdir(char *** buf)
 {
 #ifdef IPCP_FLOW_STATS
         char   entry[RIB_PATH_LEN + 1];
@@ -249,8 +248,8 @@ static int fa_stat_readdir(char *** buf)
 #endif
 }
 
-static int fa_stat_getattr(const char *  path,
-                           struct stat * st)
+static int fa_rib_getattr(const char *      path,
+                          struct rib_attr * attr)
 {
 #ifdef IPCP_FLOW_STATS
         int              fd;
@@ -258,35 +257,30 @@ static int fa_stat_getattr(const char *  path,
 
         fd = atoi(path);
 
-        st->st_mode  = S_IFREG | 0755;
-        st->st_nlink = 1;
-        st->st_uid   = getuid();
-        st->st_gid   = getgid();
-
         flow = &fa.flows[fd];
 
         pthread_rwlock_rdlock(&fa.flows_lock);
 
         if (flow->stamp != 0) {
-                st->st_size  = 1536;
-                st->st_mtime = flow->stamp;
+                attr->size  = 1536;
+                attr->mtime = flow->stamp;
         } else {
-                st->st_size  = 0;
-                st->st_mtime = 0;
+                attr->size  = 0;
+                attr->mtime = 0;
         }
 
         pthread_rwlock_unlock(&fa.flows_lock);
 #else
         (void) path;
-        (void) st;
+        (void) attr;
 #endif
         return 0;
 }
 
 static struct rib_ops r_ops = {
-        .read    = fa_stat_read,
-        .readdir = fa_stat_readdir,
-        .getattr = fa_stat_getattr
+        .read    = fa_rib_read,
+        .readdir = fa_rib_readdir,
+        .getattr = fa_rib_getattr
 };
 
 static int eid_to_fd(uint64_t eid)
diff --git a/src/ipcpd/unicast/pol/link_state.c 
b/src/ipcpd/unicast/pol/link_state.c
index 6904488..882bb8a 100644
--- a/src/ipcpd/unicast/pol/link_state.c
+++ b/src/ipcpd/unicast/pol/link_state.c
@@ -24,7 +24,6 @@
 #define _DEFAULT_SOURCE
 #else
 #define _POSIX_C_SOURCE 200112L
-#define __XSI_VISIBLE 500
 #endif
 
 #include "config.h"
@@ -180,14 +179,14 @@ static struct adjacency * get_adj(const char * path)
         return NULL;
 }
 
-static int lsdb_getattr(const char *  path,
-                        struct stat * st)
+static int lsdb_rib_getattr(const char *      path,
+                            struct rib_attr * attr)
 {
         struct adjacency * adj;
         struct timespec    now;
 
         assert(path);
-        assert(st);
+        assert(attr);
 
         clock_gettime(CLOCK_REALTIME_COARSE, &now);
 
@@ -195,26 +194,21 @@ static int lsdb_getattr(const char *  path,
 
         adj = get_adj(path);
         if (adj != NULL) {
-                st->st_mtime = adj->stamp;
-                st->st_size  = LS_ENTRY_SIZE;
+                attr->mtime = adj->stamp;
+                attr->size  = LS_ENTRY_SIZE;
         } else {
-                st->st_mtime = now.tv_sec;
-                st->st_size  = 0;
+                attr->mtime = now.tv_sec;
+                attr->size  = 0;
         }
 
-        st->st_mode  = S_IFREG | 0755;
-        st->st_nlink = 1;
-        st->st_uid   = getuid();
-        st->st_gid   = getgid();
-
         pthread_rwlock_unlock(&ls.db_lock);
 
         return 0;
 }
 
-static int lsdb_read(const char * path,
-                     char *       buf,
-                     size_t       len)
+static int lsdb_rib_read(const char * path,
+                         char *       buf,
+                         size_t       len)
 {
         struct adjacency * a;
         int                size;
@@ -242,7 +236,7 @@ static int lsdb_read(const char * path,
         return -1;
 }
 
-static int lsdb_readdir(char *** buf)
+static int lsdb_rib_readdir(char *** buf)
 {
         struct list_head * p;
         char               entry[RIB_PATH_LEN + 1];
@@ -305,9 +299,9 @@ static int lsdb_readdir(char *** buf)
 }
 
 static struct rib_ops r_ops = {
-        .read    = lsdb_read,
-        .readdir = lsdb_readdir,
-        .getattr = lsdb_getattr
+        .read    = lsdb_rib_read,
+        .readdir = lsdb_rib_readdir,
+        .getattr = lsdb_rib_getattr
 };
 
 static int lsdb_add_nb(uint64_t     addr,
diff --git a/src/lib/rib.c b/src/lib/rib.c
index 0418252..c675b6c 100644
--- a/src/lib/rib.c
+++ b/src/lib/rib.c
@@ -24,15 +24,12 @@
 
 #include "config.h"
 
-#if defined (__FreeBSD__)
-#define __XSI_VISIBLE 500
-#endif
-
 #include <ouroboros/errno.h>
 #include <ouroboros/list.h>
 #include <ouroboros/rib.h>
 #include <ouroboros/utils.h>
 
+
 #include <assert.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -45,7 +42,10 @@
 #define FUSE_USE_VERSION  26
 #if defined (__linux__)
 #define __USE_XOPEN
+#elif defined (__FreeBSD__)
+#define __XSI_VISIBLE 500
 #endif /* __linux__ */
+#include <sys/stat.h>
 #include <fuse.h>
 
 #ifndef CLOCK_REALTIME_COARSE
@@ -187,6 +187,7 @@ static size_t __getattr(const char *  path,
         struct list_head * p;
         char               comp[RIB_PATH_LEN + 1];
         char *             c;
+        struct rib_attr    attr;
 
         if (strlen(path) > RIB_PATH_LEN)
                 return -1;
@@ -203,7 +204,7 @@ static size_t __getattr(const char *  path,
         list_for_each(p, &rib.reg_comps) {
                 struct reg_comp * r = list_entry(p, struct reg_comp, next);
                 if (strcmp(comp, r->path) == 0) {
-                        size_t ret = r->ops->getattr(c + 1, st);
+                        size_t ret = r->ops->getattr(c + 1, &attr);
                         pthread_rwlock_unlock(&rib.lock);
                         return ret;
                 }
@@ -211,6 +212,15 @@ static size_t __getattr(const char *  path,
 
         pthread_rwlock_unlock(&rib.lock);
 
+        memset(&attr, 0, sizeof(attr));
+
+        st->st_mode  = S_IFREG | 0755;
+        st->st_nlink = 1;
+        st->st_uid   = getuid();
+        st->st_gid   = getgid();
+        st->st_size  = attr.size;
+        st->st_mtime = attr.mtime;
+
         return -1;
 }
 
-- 
2.32.0


Other related posts: