Finished all of the code relating to the "global server info" stuff defined
[citadel.git] / citadel / control.c
1 /*
2  * control.c
3  *
4  * This module handles states which are global to the entire server.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <time.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <pthread.h>
18 #include <syslog.h>
19 #include "citadel.h"
20 #include "server.h"
21 #include "proto.h"
22
23 struct CitControl CitControl;
24
25 /*
26  * get_control  -  read the control record into memory.
27  */
28 void get_control() {
29         FILE *fp;
30
31         /* Zero it out.  If the control record on disk is missing or short,
32          * the system functions with all control record fields initialized
33          * to zero.
34          */
35         bzero(&CitControl, sizeof(struct CitControl));
36         fp = fopen("citadel.control", "rb");
37         if (fp == NULL) return;
38
39         fread(&CitControl, sizeof(struct CitControl), 1, fp);
40         fclose(fp);
41         }
42
43 /*
44  * put_control  -  write the control record to disk.
45  */
46 void put_control() {
47         FILE *fp;
48
49         fp = fopen("citadel.control", "wb");
50         if (fp != NULL) {
51                 fwrite(&CitControl, sizeof(struct CitControl), 1, fp);
52                 fclose(fp);
53                 }
54         }
55
56
57 /*
58  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
59  */
60 long get_new_message_number() {
61
62         begin_critical_section(S_CONTROL);
63         get_control();
64         ++CitControl.MMhighest;
65         put_control();
66         end_critical_section(S_CONTROL);
67         return(CitControl.MMhighest);
68         }
69
70
71 /*
72  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
73  */
74 long get_new_user_number() {
75
76         begin_critical_section(S_CONTROL);
77         get_control();
78         ++CitControl.MMnextuser;
79         put_control();
80         end_critical_section(S_CONTROL);
81         return(CitControl.MMhighest);
82         }