2 * $Id: db_param.c,v 1.7 2009-10-19 05:02:35 didg Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * Copyright (c) Frank Lahm 2009
6 * All Rights Reserved. See COPYING.
11 #endif /* HAVE_CONFIG_H */
15 #endif /* HAVE_UNISTD_H */
22 #include <sys/param.h>
24 #include <sys/select.h>
25 #include <atalk/logger.h>
29 #define DB_PARAM_FN "db_param"
32 #define DEFAULT_LOGFILE_AUTOREMOVE 1
33 #define DEFAULT_CACHESIZE 8 * 1024
34 #define DEFAULT_FLUSH_FREQUENCY 100
35 #define DEFAULT_FLUSH_INTERVAL 1800
36 #define DEFAULT_USOCK_FILE "usock"
37 #define DEFAULT_FD_TABLE_SIZE 512
38 #define DEFAULT_IDLE_TIMEOUT 10 * 60
40 static struct db_param params;
43 static size_t usock_maxlen(void)
45 struct sockaddr_un addr;
46 return sizeof(addr.sun_path) - 1;
49 static int make_pathname(char *path, char *dir, char *fn, size_t maxlen)
55 if (len + 1 + strlen(fn) > maxlen)
58 if (path[len - 1] != '/')
62 if (strlen(fn) > maxlen)
69 static void default_params(struct db_param *dbp, char *dir)
71 dbp->logfile_autoremove = DEFAULT_LOGFILE_AUTOREMOVE;
72 dbp->cachesize = DEFAULT_CACHESIZE;
73 dbp->flush_frequency = DEFAULT_FLUSH_FREQUENCY;
74 dbp->flush_interval = DEFAULT_FLUSH_INTERVAL;
75 if (make_pathname(dbp->usock_file, dir, DEFAULT_USOCK_FILE, usock_maxlen()) < 0) {
76 /* Not an error yet, it might be set in the config file */
77 dbp->usock_file[0] = '\0';
79 dbp->fd_table_size = DEFAULT_FD_TABLE_SIZE;
80 if ( dbp->fd_table_size > FD_SETSIZE -1)
81 dbp->fd_table_size = FD_SETSIZE -1;
82 dbp->idle_timeout = DEFAULT_IDLE_TIMEOUT;
87 static int parse_int(char *val)
92 result = strtol(val, &tmp, 10);
94 LOG(log_error, logtype_cnid, "invalid characters in token %s", val);
101 /* TODO: This configuration file reading routine is neither very robust (%s
102 buffer overflow) nor elegant, we need to add support for whitespace in
103 filenames as well. */
105 struct db_param *db_param_read(char *dir, enum identity id)
108 static char key[MAXKEYLEN + 1];
109 static char val[MAXPATHLEN + 1];
110 static char pfn[MAXPATHLEN + 1];
113 default_params(¶ms, dir);
116 if (make_pathname(pfn, dir, DB_PARAM_FN, MAXPATHLEN) < 0) {
117 LOG(log_error, logtype_cnid, "Parameter filename too long");
121 if ((fp = fopen(pfn, "r")) == NULL) {
122 if (errno == ENOENT) {
123 if (strlen(params.usock_file) == 0) {
124 LOG(log_error, logtype_cnid, "default usock filename too long");
130 LOG(log_error, logtype_cnid, "error opening %s: %s", pfn, strerror(errno));
136 while ((items = fscanf(fp, " %s %s", key, val)) != EOF) {
138 LOG(log_error, logtype_cnid, "error parsing config file");
143 /* Config for both cnid_meta and dbd */
144 if (! strcmp(key, "usock_file")) {
145 if (make_pathname(params.usock_file, dir, val, usock_maxlen()) < 0) {
146 LOG(log_error, logtype_cnid, "usock filename %s too long", val);
149 LOG(log_info, logtype_cnid, "db_param: setting UNIX domain socket filename to %s", params.usock_file);
152 /* Config for cnid_metad only */
154 /* Currently empty */
157 /* Config for dbd only */
158 else if (id == CNID_DBD ) {
159 if (! strcmp(key, "fd_table_size")) {
160 params.fd_table_size = parse_int(val);
161 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);
162 } else if (! strcmp(key, "logfile_autoremove")) {
163 params.logfile_autoremove = parse_int(val);
164 LOG(log_info, logtype_cnid, "db_param: setting logfile_autoremove to %d", params.logfile_autoremove);
165 } else if (! strcmp(key, "cachesize")) {
166 params.cachesize = parse_int(val);
167 LOG(log_info, logtype_cnid, "db_param: setting cachesize to %d", params.cachesize);
168 } else if (! strcmp(key, "flush_frequency")) {
169 params.flush_frequency = parse_int(val);
170 LOG(log_info, logtype_cnid, "db_param: setting flush_frequency to %d", params.flush_frequency);
171 } else if (! strcmp(key, "flush_interval")) {
172 params.flush_interval = parse_int(val);
173 LOG(log_info, logtype_cnid, "db_param: setting flush_interval to %d", params.flush_interval);
174 } else if (! strcmp(key, "idle_timeout")) {
175 params.idle_timeout = parse_int(val);
176 LOG(log_info, logtype_cnid, "db_param: setting idle timeout to %d", params.idle_timeout);
183 if (strlen(params.usock_file) == 0) {
184 LOG(log_error, logtype_cnid, "default usock filename too long");