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