#include <atalk/paths.h>
#include <atalk/volinfo.h>
-#include "db_param.h"
#include "usockfd.h"
#define DBHOME ".AppleDB"
#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 */
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];
}
}
}
/* -------------------- */
-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;
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) {
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;
}
}
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 */
&&
/* 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 {
}
/* ------------------ */
-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);
char *dbdpn = _PATH_CNID_DBD;
char *host = DEFAULTHOST;
char *port = DEFAULTPORT;
- struct db_param *dbp;
int i;
int cc;
uid_t uid = 0;
char *loglevel = NULL;
char *logfile = NULL;
sigset_t set;
+ struct volinfo *volinfo;
set_processname("cnid_metad");
}
/* 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 */
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);
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];
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;
}
#include <sys/param.h>
#include <sys/cdefs.h>
-enum identity {
- METAD,
- CNID_DBD
-};
-
struct db_param {
char *dir;
int logfile_autoremove;
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 */
/* 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");
} vol_opt_name_t;
struct volinfo {
+ int retaincount;
+ int malloced;
+
char *v_name;
char *v_path;
int v_flags;
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);
}
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) {
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.