2 * $Id: db_param.c,v 1.9 2009-11-23 19:04:14 franklahm Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * Copyright (c) Frank Lahm 2009
6 * All Rights Reserved. See COPYING.
11 #endif /* HAVE_CONFIG_H */
18 #include <sys/param.h>
20 #include <sys/select.h>
21 #include <atalk/logger.h>
25 #define DB_PARAM_FN "db_param"
28 #define DEFAULT_LOGFILE_AUTOREMOVE 1
29 #define DEFAULT_CACHESIZE (8 * 1024)
30 #define DEFAULT_FLUSH_FREQUENCY 1000
31 #define DEFAULT_FLUSH_INTERVAL 1800
32 #define DEFAULT_USOCK_FILE "usock"
33 #define DEFAULT_FD_TABLE_SIZE 512
34 #define DEFAULT_IDLE_TIMEOUT (10 * 60)
36 static struct db_param params;
39 static size_t usock_maxlen(void)
41 struct sockaddr_un addr;
42 return sizeof(addr.sun_path) - 1;
45 static int make_pathname(char *path, char *dir, char *fn, size_t maxlen)
51 if (len + 1 + strlen(fn) > maxlen)
54 if (path[len - 1] != '/')
58 if (strlen(fn) > maxlen)
65 static void default_params(struct db_param *dbp, char *dir)
67 dbp->logfile_autoremove = DEFAULT_LOGFILE_AUTOREMOVE;
68 dbp->cachesize = DEFAULT_CACHESIZE;
69 dbp->flush_frequency = DEFAULT_FLUSH_FREQUENCY;
70 dbp->flush_interval = DEFAULT_FLUSH_INTERVAL;
71 if (make_pathname(dbp->usock_file, dir, DEFAULT_USOCK_FILE, usock_maxlen()) < 0) {
72 /* Not an error yet, it might be set in the config file */
73 dbp->usock_file[0] = '\0';
75 dbp->fd_table_size = DEFAULT_FD_TABLE_SIZE;
76 if ( dbp->fd_table_size > FD_SETSIZE -1)
77 dbp->fd_table_size = FD_SETSIZE -1;
78 dbp->idle_timeout = DEFAULT_IDLE_TIMEOUT;
83 static int parse_int(char *val)
88 result = strtol(val, &tmp, 10);
90 LOG(log_error, logtype_cnid, "invalid characters in token %s", val);
97 /* TODO: This configuration file reading routine is neither very robust (%s
98 buffer overflow) nor elegant, we need to add support for whitespace in
101 struct db_param *db_param_read(char *dir, enum identity id)
104 static char key[MAXKEYLEN + 1];
105 static char val[MAXPATHLEN + 1];
106 static char pfn[MAXPATHLEN + 1];
109 default_params(¶ms, dir);
112 if (make_pathname(pfn, dir, DB_PARAM_FN, MAXPATHLEN) < 0) {
113 LOG(log_error, logtype_cnid, "Parameter filename too long");
117 if ((fp = fopen(pfn, "r")) == NULL) {
118 if (errno == ENOENT) {
119 if (strlen(params.usock_file) == 0) {
120 LOG(log_error, logtype_cnid, "default usock filename too long");
126 LOG(log_error, logtype_cnid, "error opening %s: %s", pfn, strerror(errno));
132 while ((items = fscanf(fp, " %s %s", key, val)) != EOF) {
134 LOG(log_error, logtype_cnid, "error parsing config file");
139 /* Config for both cnid_meta and dbd */
140 if (! strcmp(key, "usock_file")) {
141 if (make_pathname(params.usock_file, dir, val, usock_maxlen()) < 0) {
142 LOG(log_error, logtype_cnid, "usock filename %s too long", val);
145 LOG(log_info, logtype_cnid, "db_param: setting UNIX domain socket filename to %s", params.usock_file);
148 /* Config for cnid_metad only */
150 /* Currently empty */
153 /* Config for dbd only */
154 else if (id == CNID_DBD ) {
155 if (! strcmp(key, "fd_table_size")) {
156 params.fd_table_size = parse_int(val);
157 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);
158 } else if (! strcmp(key, "logfile_autoremove")) {
159 params.logfile_autoremove = parse_int(val);
160 LOG(log_info, logtype_cnid, "db_param: setting logfile_autoremove to %d", params.logfile_autoremove);
161 } else if (! strcmp(key, "cachesize")) {
162 params.cachesize = parse_int(val);
163 LOG(log_info, logtype_cnid, "db_param: setting cachesize to %d", params.cachesize);
164 } else if (! strcmp(key, "flush_frequency")) {
165 params.flush_frequency = parse_int(val);
166 LOG(log_info, logtype_cnid, "db_param: setting flush_frequency to %d", params.flush_frequency);
167 } else if (! strcmp(key, "flush_interval")) {
168 params.flush_interval = parse_int(val);
169 LOG(log_info, logtype_cnid, "db_param: setting flush_interval to %d", params.flush_interval);
170 } else if (! strcmp(key, "idle_timeout")) {
171 params.idle_timeout = parse_int(val);
172 LOG(log_info, logtype_cnid, "db_param: setting idle timeout to %d", params.idle_timeout);
179 if (strlen(params.usock_file) == 0) {
180 LOG(log_error, logtype_cnid, "default usock filename too long");
187 if (params.flush_frequency <= 0)
188 params.flush_frequency = 86400;
190 if (params.flush_interval <= 0)
191 params.flush_interval = 1000000;
193 if (params.fd_table_size <= 2)
194 params.fd_table_size = 32;
196 if (params.idle_timeout <= 0)
197 params.idle_timeout = 86400;