]> arthur.barton.de Git - netatalk.git/commitdiff
Dont read db_param in cnid_metad and use volinfo with refcounting
authorFrank Lahm <franklahm@googlemail.com>
Thu, 9 Dec 2010 15:50:12 +0000 (16:50 +0100)
committerFrank Lahm <franklahm@googlemail.com>
Thu, 9 Dec 2010 15:50:12 +0000 (16:50 +0100)
etc/cnid_dbd/cnid_metad.c
etc/cnid_dbd/db_param.c
etc/cnid_dbd/db_param.h
etc/cnid_dbd/main.c
include/atalk/volinfo.h
libatalk/util/volinfo.c

index a377c809d1ad611caa8b0bd6e7f4d4cc39e5a7ed..85ea7a4fb21b88fd71b19a05a4376fe49c142b7f 100644 (file)
@@ -91,7 +91,6 @@
 #include <atalk/paths.h>
 #include <atalk/volinfo.h>
 
-#include "db_param.h"
 #include "usockfd.h"
 
 #define DBHOME        ".AppleDB"
@@ -109,7 +108,7 @@ static volatile sig_atomic_t sigchild = 0;
 #define DEFAULTPORT  "4700"
 
 struct server {
-    char  *name;
+    struct volinfo *volinfo;
     pid_t pid;
     time_t tm;                    /* When respawned last */
     int count;                    /* Times respawned in the last TESTTIME secondes */
@@ -140,11 +139,11 @@ static void sigterm_handler(int sig)
     daemon_exit(0);
 }
 
-static struct server *test_usockfn(char *dir)
+static struct server *test_usockfn(struct volinfo *volinfo)
 {
     int i;
     for (i = 0; i < MAXVOLS; i++) {
-        if (srv[i].name && !strcmp(srv[i].name, dir)) {
+        if ((srv[i].volinfo) && (strcmp(srv[i].volinfo->v_path, volinfo->v_path) == 0)) {
             return &srv[i];
         }
     }
@@ -205,7 +204,7 @@ static int send_cred(int socket, int fd)
 }
 
 /* -------------------- */
-static int maybe_start_dbd(char *dbdpn, char *volpath, char *usockfn)
+static int maybe_start_dbd(char *dbdpn, struct volinfo *volinfo)
 {
     pid_t pid;
     struct server *up;
@@ -214,10 +213,11 @@ static int maybe_start_dbd(char *dbdpn, char *volpath, char *usockfn)
     time_t t;
     char buf1[8];
     char buf2[8];
+    char *volpath = volinfo->v_path;
 
     LOG(log_debug, logtype_cnid, "maybe_start_dbd: Volume: \"%s\"", volpath);
 
-    up = test_usockfn(volpath);
+    up = test_usockfn(volinfo);
     if (up && up->pid) {
         /* we already have a process, send our fd */
         if (send_cred(up->control_fd, rqstfd) < 0) {
@@ -233,11 +233,12 @@ static int maybe_start_dbd(char *dbdpn, char *volpath, char *usockfn)
     if (!up) {
         /* find an empty slot */
         for (i = 0; i < MAXVOLS; i++) {
-            if ( !srv[i].name ) {
+            if (srv[i].volinfo == NULL) {
                 up = &srv[i];
+                up->volinfo = volinfo;
+                retainvolinfo(volinfo);
                 up->tm = t;
                 up->count = 0;
-                up->name = strdup(volpath);
                 break;
             }
         }
@@ -245,8 +246,7 @@ static int maybe_start_dbd(char *dbdpn, char *volpath, char *usockfn)
             LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
             return -1;
         }
-    }
-    else {
+    } else {
         /* we have a slot but no process, check for respawn too fast */
         if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
              &&
@@ -306,7 +306,7 @@ static int maybe_start_dbd(char *dbdpn, char *volpath, char *usockfn)
             /* there's a pb with the db inform child
              * it will run recover, delete the db whatever
              */
-            LOG(log_error, logtype_cnid, "try with -d %s", up->name);
+            LOG(log_error, logtype_cnid, "try with -d %s", up->volinfo->v_path);
             ret = execlp(dbdpn, dbdpn, "-d", volpath, buf1, buf2, logconfig, NULL);
         }
         else {
@@ -326,12 +326,12 @@ static int maybe_start_dbd(char *dbdpn, char *volpath, char *usockfn)
 }
 
 /* ------------------ */
-static int set_dbdir(char *dbdir, int len)
+static int set_dbdir(char *dbdir)
 {
+    int len;
     struct stat st;
 
-    if (!len)
-        return -1;
+    len = strlen(dbdir);
 
     if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
         LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
@@ -467,7 +467,6 @@ int main(int argc, char *argv[])
     char  *dbdpn = _PATH_CNID_DBD;
     char  *host = DEFAULTHOST;
     char  *port = DEFAULTPORT;
-    struct db_param *dbp;
     int    i;
     int    cc;
     uid_t  uid = 0;
@@ -478,6 +477,7 @@ int main(int argc, char *argv[])
     char   *loglevel = NULL;
     char   *logfile  = NULL;
     sigset_t set;
+    struct volinfo *volinfo;
 
     set_processname("cnid_metad");
 
@@ -538,7 +538,7 @@ int main(int argc, char *argv[])
     }
 
     /* Check PID lockfile and become a daemon */
-    switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, 0)) {
+    switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, debug)) {
     case -1: /* error */
         daemon_exit(EXITERR_SYS);
     case 0: /* child */
@@ -632,22 +632,18 @@ int main(int argc, char *argv[])
         volpath[len] = '\0';
 
         /* Load .volinfo file */
-        struct volinfo volinfo;
-        if (loadvolinfo(volpath, &volinfo) == -1) {
-            LOG(log_error, logtype_cnid, "Cant load volinfo for \"%s\"", volpath);
+        if ((volinfo = allocvolinfo(volpath)) == NULL) {
+            LOG(log_severe, logtype_cnid, "allocvolinfo: %s", strerror(errno));
             goto loop_end;
         }
 
-        if (set_dbdir(volinfo.v_dbpath, strlen(volinfo.v_dbpath)) < 0) {
+        if (set_dbdir(volinfo->v_dbpath) < 0) {
             goto loop_end;
         }
 
-        if ((dbp = db_param_read(volinfo.v_dbpath, METAD)) == NULL) {
-            LOG(log_error, logtype_cnid, "Error reading config file for \"%s\"",
-                volinfo.v_dbpath);
-            goto loop_end;
-        }
-        maybe_start_dbd(dbdpn, volpath, dbp->usock_file);
+        maybe_start_dbd(dbdpn, volinfo);
+
+        (void)closevolinfo(volinfo);
 
     loop_end:
         close(rqstfd);
index eda624aec6edc645542704e0856772a579835d60..e31e782759830629536f25a8d95dcffc7517b57c 100644 (file)
@@ -102,7 +102,7 @@ static int parse_int(char *val)
    buffer overflow) nor elegant, we need to add support for whitespace in
    filenames as well. */
 
-struct db_param *db_param_read(char *dir, enum identity id)
+struct db_param *db_param_read(char *dir)
 {
     FILE *fp;
     static char key[MAXKEYLEN + 1];
@@ -149,39 +149,32 @@ struct db_param *db_param_read(char *dir, enum identity id)
                 LOG(log_info, logtype_cnid, "db_param: setting UNIX domain socket filename to %s", params.usock_file);
         }
 
-        /* Config for cnid_metad only */
-        if ( id == METAD ) {
-            /* Currently empty */
+        if (! strcmp(key, "fd_table_size")) {
+            params.fd_table_size = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting max number of concurrent afpd connections per volume (fd_table_size) to %d", params.fd_table_size);
+        } else if (! strcmp(key, "logfile_autoremove")) {
+            params.logfile_autoremove = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting logfile_autoremove to %d", params.logfile_autoremove);
+        } else if (! strcmp(key, "cachesize")) {
+            params.cachesize = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting cachesize to %d", params.cachesize);
+        } else if (! strcmp(key, "maxlocks")) {
+            params.maxlocks = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting maxlocks to %d", params.maxlocks);
+        } else if (! strcmp(key, "maxlockobjs")) {
+            params.maxlockobjs = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting maxlockobjs to %d", params.maxlockobjs);
+        } else if (! strcmp(key, "flush_frequency")) {
+            params.flush_frequency = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting flush_frequency to %d", params.flush_frequency);
+        } else if (! strcmp(key, "flush_interval")) {
+            params.flush_interval = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting flush_interval to %d", params.flush_interval);
+        } else if (! strcmp(key, "idle_timeout")) {
+            params.idle_timeout = parse_int(val);
+            LOG(log_info, logtype_cnid, "db_param: setting idle timeout to %d", params.idle_timeout);
         }
 
-        /* Config for dbd only */
-        else if (id == CNID_DBD ) {
-            if (! strcmp(key, "fd_table_size")) {
-                params.fd_table_size = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting max number of concurrent afpd connections per volume (fd_table_size) to %d", params.fd_table_size);
-            } else if (! strcmp(key, "logfile_autoremove")) {
-                params.logfile_autoremove = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting logfile_autoremove to %d", params.logfile_autoremove);
-            } else if (! strcmp(key, "cachesize")) {
-                params.cachesize = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting cachesize to %d", params.cachesize);
-            } else if (! strcmp(key, "maxlocks")) {
-                params.maxlocks = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting maxlocks to %d", params.maxlocks);
-            } else if (! strcmp(key, "maxlockobjs")) {
-                params.maxlockobjs = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting maxlockobjs to %d", params.maxlockobjs);
-            } else if (! strcmp(key, "flush_frequency")) {
-                params.flush_frequency = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting flush_frequency to %d", params.flush_frequency);
-            } else if (! strcmp(key, "flush_interval")) {
-                params.flush_interval = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting flush_interval to %d", params.flush_interval);
-            } else if (! strcmp(key, "idle_timeout")) {
-                params.idle_timeout = parse_int(val);
-                LOG(log_info, logtype_cnid, "db_param: setting idle timeout to %d", params.idle_timeout);
-            }
-        }
         if (parse_err)
             break;
     }
index 57ade7c5e425057f6f26c7e57269ed0010ba7814..67ec62e4e18c8fc6f40c5f02c8e9f5aa72948621 100644 (file)
 #include <sys/param.h>
 #include <sys/cdefs.h>
 
-enum identity {
-    METAD,
-    CNID_DBD
-};
-
 struct db_param {
     char *dir;
     int logfile_autoremove;
@@ -29,8 +24,7 @@ struct db_param {
     int max_vols;
 };
 
-extern struct db_param *      db_param_read  (char *, enum identity);
-
+extern struct db_param *db_param_read  (char *);
 
 #endif /* CNID_DBD_DB_PARAM_H */
 
index 87c0a57edafbae59f9ed6630fab915979a166ba8..62ddf9144c703a5fddfad1346132b6a2e1649a92 100644 (file)
@@ -372,7 +372,7 @@ int main(int argc, char *argv[])
     /* SIGINT and SIGTERM are always off, unless we are in pselect */
     block_sigs_onoff(1);
 
-    if ((dbp = db_param_read(dbpath, CNID_DBD)) == NULL)
+    if ((dbp = db_param_read(dbpath)) == NULL)
         exit(1);
     LOG(log_maxdebug, logtype_cnid, "Finished parsing db_param config file");
 
index bd8c2a76e1bacadd7c3f75e736a9f299bdc7c94f..0f3a4024a6a36faedc4993783a731369e56237b3 100644 (file)
@@ -14,6 +14,9 @@ typedef struct {
 } vol_opt_name_t;
 
 struct volinfo {
+    int                 retaincount;
+    int                 malloced;
+
     char                *v_name;
     char                *v_path;
     int                 v_flags;
@@ -32,7 +35,10 @@ struct volinfo {
     char                *v_dbd_port;
 };
 
+struct volinfo *allocvolinfo(char *path);
 extern int loadvolinfo(char *path, struct volinfo *vol);
+extern void retainvolinfo(struct volinfo *vol);
+extern int closevolinfo(struct volinfo *vol);
 extern int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port);
 extern int vol_load_charsets(struct volinfo *vol);
 
index d95dedc3d5d9b803b95403b7a9667655ee1a01ea..62afbc6559b490aa0d6c62a76380bc170aefe0c5 100644 (file)
@@ -294,10 +294,9 @@ static int parseline ( char *buf, struct volinfo *vol)
         }
         break;
       case CNID_DBPATH:
-        if ((vol->v_dbpath = strdup(value)) == NULL) {
-           fprintf (stderr, "strdup: %s", strerror(errno));
-            return -1;
-        }
+          if ((vol->v_dbpath = malloc(MAXPATHLEN+1)) == NULL)
+              return -1;
+          strcpy(vol->v_dbpath, value);
         break;
       case ADOUBLE_VER:
         if (strcasecmp(value, "v1") == 0) {
@@ -413,10 +412,63 @@ int loadvolinfo (char *path, struct volinfo *vol)
     if ((vol->v_flags & AFPVOL_INV_DOTS))
         vol->v_ad_options |= ADVOL_INVDOTS;
 
+    vol->retaincount = 1;
+
     fclose(fp);
     return 0;
 }
 
+/*!
+ * Allocate a struct volinfo object for refcounting usage with retain and close, and
+ * call loadvolinfo with it
+ */
+struct volinfo *allocvolinfo(char *path)
+{
+    struct volinfo *p = malloc(sizeof(struct volinfo));
+    if (p == NULL)
+        return NULL;
+
+    if (loadvolinfo(path, p) == -1)
+        return NULL;
+
+    p->malloced = 1;
+
+    return p;
+}
+
+void retainvolinfo(struct volinfo *vol)
+{
+    vol->retaincount++;
+}
+
+/*!
+ * Decrement retain count, free resources when retaincount reaches 0
+ */
+int closevolinfo(struct volinfo *volinfo)
+{
+    if (volinfo->retaincount <= 0)
+        abort();
+
+    volinfo->retaincount--;
+
+    if (volinfo->retaincount == 0) {
+        free(volinfo->v_name); volinfo->v_name = NULL;
+        free(volinfo->v_path); volinfo->v_path = NULL;
+        free(volinfo->v_cnidscheme); volinfo->v_cnidscheme = NULL;
+        free(volinfo->v_dbpath); volinfo->v_dbpath = NULL;
+        free(volinfo->v_volcodepage); volinfo->v_volcodepage = NULL;
+        free(volinfo->v_maccodepage); volinfo->v_maccodepage = NULL;
+        free(volinfo->v_dbd_host); volinfo->v_dbd_host = NULL;
+        free(volinfo->v_dbd_port); volinfo->v_dbd_port = NULL;
+        if (volinfo->malloced) {
+            volinfo->malloced = 0;
+            free(volinfo);
+        }
+    }
+
+    return 0;
+}
+
 /*
  * Save the volume options to a file, used by shell utilities. Writing the file
  * everytime a volume is opened is unnecessary, but it shouldn't hurt much.