]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/db_param.c
e31e782759830629536f25a8d95dcffc7517b57c
[netatalk.git] / etc / cnid_dbd / db_param.c
1 /*
2  * $Id: db_param.c,v 1.9 2009-11-23 19:04:14 franklahm Exp $
3  *
4  * Copyright (C) Joerg Lenneis 2003
5  * Copyright (c) Frank Lahm 2009
6  * All Rights Reserved.  See COPYING.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <errno.h>
18 #include <sys/param.h>
19 #include <sys/un.h>
20 #include <sys/select.h>
21 #include <atalk/logger.h>
22
23 #include "db_param.h"
24
25 #define DB_PARAM_FN       "db_param"
26 #define MAXKEYLEN         64
27
28 #define DEFAULT_LOGFILE_AUTOREMOVE 1
29 #define DEFAULT_CACHESIZE          (8 * 1024) /* KB, so 8 MB */
30 #define DEFAULT_MAXLOCKS           5000
31 #define DEFAULT_MAXLOCKOBJS        5000
32 #define DEFAULT_FLUSH_FREQUENCY    1000
33 #define DEFAULT_FLUSH_INTERVAL     1800
34 #define DEFAULT_USOCK_FILE         "usock"
35 #define DEFAULT_FD_TABLE_SIZE      512
36 #define DEFAULT_IDLE_TIMEOUT       (10 * 60)
37
38 static struct db_param params;
39 static int parse_err;
40
41 static size_t usock_maxlen(void)
42 {
43     struct sockaddr_un addr;
44     return sizeof(addr.sun_path) - 1;
45 }
46
47 static int make_pathname(char *path, char *dir, char *fn, size_t maxlen)
48 {
49     size_t len;
50
51     if (fn[0] != '/') {
52         len = strlen(dir);
53         if (len + 1 + strlen(fn) > maxlen)
54             return -1;      
55         strcpy(path, dir);
56         if (path[len - 1] != '/')
57             strcat(path, "/");
58         strcat(path, fn);
59     } else {
60         if (strlen(fn) > maxlen)
61             return -1;
62         strcpy(path, fn);
63     }
64     return 0;
65 }
66
67 static void default_params(struct db_param *dbp, char *dir)
68 {        
69     dbp->logfile_autoremove  = DEFAULT_LOGFILE_AUTOREMOVE;
70     dbp->cachesize           = DEFAULT_CACHESIZE;
71     dbp->maxlocks            = DEFAULT_MAXLOCKS;
72     dbp->maxlockobjs         = DEFAULT_MAXLOCKOBJS;
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';
78     }
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;
83
84     return;
85 }
86
87 static int parse_int(char *val)
88 {
89     char *tmp;
90     int   result = 0;
91
92     result = strtol(val, &tmp, 10);
93     if (tmp[0] != '\0') {
94         LOG(log_error, logtype_cnid, "invalid characters in token %s", val);
95         parse_err++;
96     }
97     return result;
98 }
99
100
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. */
104
105 struct db_param *db_param_read(char *dir)
106 {
107     FILE *fp;
108     static char key[MAXKEYLEN + 1];
109     static char val[MAXPATHLEN + 1];
110     static char pfn[MAXPATHLEN + 1];
111     int    items;
112     
113     default_params(&params, dir);
114     params.dir = dir;
115     
116     if (make_pathname(pfn, dir, DB_PARAM_FN, MAXPATHLEN) < 0) {
117         LOG(log_error, logtype_cnid, "Parameter filename too long");
118         return NULL;
119     }
120
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");
125                 return NULL;
126             } else {
127                 return &params;
128             }
129         } else {
130             LOG(log_error, logtype_cnid, "error opening %s: %s", pfn, strerror(errno));
131             return NULL;
132         }
133     }
134     parse_err = 0;
135
136     while ((items = fscanf(fp, " %s %s", key, val)) != EOF) {
137         if (items != 2) {
138             LOG(log_error, logtype_cnid, "error parsing config file");
139             parse_err++;
140             break;
141         }
142
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);
147                 parse_err++;
148             } else
149                 LOG(log_info, logtype_cnid, "db_param: setting UNIX domain socket filename to %s", params.usock_file);
150         }
151
152         if (! strcmp(key, "fd_table_size")) {
153             params.fd_table_size = parse_int(val);
154             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);
155         } else if (! strcmp(key, "logfile_autoremove")) {
156             params.logfile_autoremove = parse_int(val);
157             LOG(log_info, logtype_cnid, "db_param: setting logfile_autoremove to %d", params.logfile_autoremove);
158         } else if (! strcmp(key, "cachesize")) {
159             params.cachesize = parse_int(val);
160             LOG(log_info, logtype_cnid, "db_param: setting cachesize to %d", params.cachesize);
161         } else if (! strcmp(key, "maxlocks")) {
162             params.maxlocks = parse_int(val);
163             LOG(log_info, logtype_cnid, "db_param: setting maxlocks to %d", params.maxlocks);
164         } else if (! strcmp(key, "maxlockobjs")) {
165             params.maxlockobjs = parse_int(val);
166             LOG(log_info, logtype_cnid, "db_param: setting maxlockobjs to %d", params.maxlockobjs);
167         } else if (! strcmp(key, "flush_frequency")) {
168             params.flush_frequency = parse_int(val);
169             LOG(log_info, logtype_cnid, "db_param: setting flush_frequency to %d", params.flush_frequency);
170         } else if (! strcmp(key, "flush_interval")) {
171             params.flush_interval = parse_int(val);
172             LOG(log_info, logtype_cnid, "db_param: setting flush_interval to %d", params.flush_interval);
173         } else if (! strcmp(key, "idle_timeout")) {
174             params.idle_timeout = parse_int(val);
175             LOG(log_info, logtype_cnid, "db_param: setting idle timeout to %d", params.idle_timeout);
176         }
177
178         if (parse_err)
179             break;
180     }
181     
182     if (strlen(params.usock_file) == 0) {
183         LOG(log_error, logtype_cnid, "default usock filename too long");
184         parse_err++;
185     }
186
187     fclose(fp);
188     if (! parse_err) {
189         /* sanity checks */
190         if (params.flush_frequency <= 0) 
191             params.flush_frequency = 86400;
192
193         if (params.flush_interval <= 0)
194             params.flush_interval = 1000000;
195
196         if (params.fd_table_size <= 2)
197             params.fd_table_size = 32;
198
199         if (params.idle_timeout <= 0)
200             params.idle_timeout = 86400;
201
202         return &params;
203     }
204     else
205         return NULL;
206 }
207
208
209