* Removed the session_count() function. Instead, keep a reference count
[citadel.git] / citadel / config.c
1 /*
2  * This function reads the citadel.config file.  It should be called at
3  * the beginning of EVERY Citadel program.
4  *
5  * $Id$
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "citadel.h"
17 #include "config.h"
18
19 struct config config;
20 char bbs_home_directory[PATH_MAX] = BBSDIR;
21 int home_specified = 0;
22
23 /*
24  * get_config() is called during the initialization of any program which
25  * directly accesses Citadel data files.  It verifies the system's integrity
26  * and reads citadel.config into memory.
27  */
28 void get_config(void) {
29         FILE *cfp;
30         struct stat st;
31
32         if (chdir(home_specified ? bbs_home_directory : BBSDIR) != 0) {
33                 fprintf(stderr,
34                         "This program could not be started.\n"
35                         "Unable to change directory to %s\n"
36                         "Error: %s\n",
37                         (home_specified ? bbs_home_directory : BBSDIR),
38                         strerror(errno));
39                 exit(1);
40         }
41         cfp = fopen("citadel.config", "rb");
42         if (cfp == NULL) {
43                 fprintf(stderr, "This program could not be started.\n"
44                         "Unable to open %s/citadel.config\n"
45                         "Error: %s\n",
46                         (home_specified ? bbs_home_directory : BBSDIR),
47                         strerror(errno));
48                 exit(1);
49         }
50         fread((char *) &config, sizeof(struct config), 1, cfp);
51         if (fstat(fileno(cfp), &st)) {
52                 perror("citadel.config");
53                 exit(1);
54         }
55         if (st.st_uid != BBSUID || st.st_mode != (S_IFREG | S_IRUSR | S_IWUSR)) {
56                 fprintf(stderr, "check the permissions on citadel.config\n");
57                 exit(1);
58         }
59         fclose(cfp);
60
61         if (config.c_setup_level != REV_LEVEL) {
62                 fprintf(stderr, "config: Your data files are out of date.  ");
63                 fprintf(stderr, "Run setup to update them.\n");
64                 fprintf(stderr,
65                         "        This program requires level %d.%02d\n",
66                         (REV_LEVEL / 100), (REV_LEVEL % 100));
67                 fprintf(stderr,
68                         "        Data files are currently at %d.%02d\n",
69                         (config.c_setup_level / 100),
70                         (config.c_setup_level % 100));
71                 exit(1);
72         }
73
74         /* Default maximum message length is 'unlimited' (max int)
75          * and the minimum is 8192
76          */
77         if (config.c_maxmsglen <= 0)
78                 config.c_maxmsglen = INT_MAX;
79         if (config.c_maxmsglen < 8192)
80                 config.c_maxmsglen = 8192;
81
82         /* Default lower and upper limits on number of worker threads */
83
84         if (config.c_min_workers < 3)           /* no less than 3 */
85                 config.c_min_workers = 5;
86
87         if (config.c_max_workers == 0)                  /* default maximum */
88                 config.c_max_workers = 256;
89
90         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
91                 config.c_max_workers = config.c_min_workers;
92 }
93
94
95 /*
96  * Occasionally, we will need to write the config file, because some operations
97  * change site-wide parameters.
98  */
99 void put_config(void)
100 {
101         FILE *cfp;
102
103         if ((cfp = fopen("citadel.config", "rb+")) == NULL)
104                 perror("citadel.config");
105         else {
106                 fwrite((char *) &config, sizeof(struct config), 1, cfp);
107                 fclose(cfp);
108         }
109 }