Tue Aug 18 00:42:33 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[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(void) {
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(void) {
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(void) {
61         begin_critical_section(S_CONTROL);
62         get_control();
63         ++CitControl.MMhighest;
64         put_control();
65         end_critical_section(S_CONTROL);
66         return(CitControl.MMhighest);
67         }
68
69
70 /*
71  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
72  */
73 long get_new_user_number(void) {
74         begin_critical_section(S_CONTROL);
75         get_control();
76         ++CitControl.MMnextuser;
77         put_control();
78         end_critical_section(S_CONTROL);
79         return(CitControl.MMnextuser);
80         }