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