]> git.pond.sub.org Git - empserver/blob - src/server/update.c
New macro ARRAY_SIZE()
[empserver] / src / server / update.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  update.c: Update scheduler
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Steve McClure, 1996
32  *     Ron Koenderink, 2005
33  *     Markus Armbruster, 2007-2020
34  */
35
36 #include <config.h>
37
38 #include <errno.h>
39 #ifndef _WIN32
40 #include <sys/wait.h>
41 #endif
42 #include <time.h>
43 #include "chance.h"
44 #include "empthread.h"
45 #include "game.h"
46 #include "misc.h"
47 #include "optlist.h"
48 #include "player.h"
49 #include "prototypes.h"
50 #include "server.h"
51
52 static time_t update_schedule_anchor;
53 static int update_wanted;
54
55 static empth_t *update_thread;
56
57 static int update_get_schedule(void);
58 static void update_sched(void *);
59 static void update_run(void);
60
61 void
62 update_init(void)
63 {
64     struct player *dp;
65
66     update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
67     if (update_get_schedule() < 0)
68         exit(1);
69
70     dp = player_new(-1);
71     if (!dp)
72         exit_nomem();
73     update_thread = empth_create(update_sched, 512 * 1024, 0, "Update", dp);
74     if (!update_thread)
75         exit_nomem();
76 }
77
78 /*
79  * Get the schedule for future updates into update_time[].
80  * Return 0 on success, -1 on failure.
81  */
82 static int
83 update_get_schedule(void)
84 {
85     time_t now = time(NULL);
86     int n = ARRAY_SIZE(update_time);
87     int i;
88
89     ef_truncate(EF_UPDATES, 0);
90     ef_extend(EF_UPDATES, n - 1);
91     if (read_schedule(schedulefil, update_time, n,
92                       now + 30, update_schedule_anchor) < 0) {
93         logerror("No update schedule!");
94         ef_truncate(EF_UPDATES, 0);
95         return -1;
96     }
97     logerror("Update schedule read");
98     for (i = 0; update_time[i]; i++) ;
99     ef_truncate(EF_UPDATES, i);
100     return 0;
101 }
102
103 /*ARGSUSED*/
104 static void
105 update_sched(void *unused)
106 {
107     time_t next_update, now;
108
109     player->proc = empth_self();
110     player->cnum = 0;
111     player->god = 1;
112
113     for (;;) {
114         /*
115          * Sleep until the next scheduled update or an unscheduled
116          * wakeup.
117          */
118         next_update = update_time[0];
119         if (next_update) {
120             if (update_window > 0)
121                 next_update += roll0(update_window);
122             logerror("Next update at %s", ctime(&next_update));
123             /* sleep until update is scheduled to go off */
124             empth_sleep(next_update);
125         } else {
126             logerror("No update scheduled");
127             /* want to sleep forever, but empthread doesn't provide that */
128             while (empth_sleep(time(NULL) + (60 * 60 * 24)) >= 0) ;
129         }
130
131         now = time(NULL);
132         if (next_update != 0 && now >= next_update) {
133             /* scheduled update time reached */
134             if (now >= next_update + 60)
135                 logerror("Missed the update!");
136             else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
137                 ;
138             else if (updates_disabled())
139                 logerror("Updates disabled...skipping update");
140             else
141                 update_wanted = 1;
142             update_schedule_anchor = update_time[0];
143         }
144         /* else unscheduled update if update_wanted is set */
145
146         if (update_wanted) {
147             update_wanted = 0;
148             update_run();
149         }
150
151         update_get_schedule();
152     }
153 }
154
155 /*
156  * Trigger an update.
157  * Return 0 on success, -1 on failure.
158  */
159 int
160 update_trigger(void)
161 {
162     logerror("Triggering unscheduled update");
163     update_wanted = 1;
164     empth_wakeup(update_thread);
165     return 0;
166 }
167
168 /*
169  * Reload the update schedule.
170  * Return 0 on success, -1 on failure.
171  */
172 int
173 update_reschedule(void)
174 {
175     empth_wakeup(update_thread);
176     return 0;
177 }
178
179 static void
180 update_run(void)
181 {
182     struct player *p;
183
184     for (p = player_next(NULL); p; p = player_next(p)) {
185         if (p->state != PS_PLAYING)
186             continue;
187         if (p->command) {
188             pr_flash(p, "Update aborting command\n");
189             p->may_sleep = PLAYER_SLEEP_NEVER;
190             p->aborted = 1;
191             empth_wakeup(p->proc);
192         }
193     }
194     empth_rwlock_wrlock(update_lock);
195     if (*pre_update_hook) {
196         if (run_hook(pre_update_hook, "pre-update")) {
197             empth_rwlock_unlock(update_lock);
198             return;
199         }
200     }
201     update_main();
202     empth_rwlock_unlock(update_lock);
203 }
204
205 int
206 run_hook(char *cmd, char *name)
207 {
208     int status;
209
210     fflush(NULL);
211
212     status = system(cmd);
213     if (status == 0)
214         ;                       /* successful exit */
215     else if (status == -1)
216         logerror("couldn't execute command processor for %s hook (%s)",
217                  name, strerror(errno));
218 #ifndef _WIN32
219     else if (WIFEXITED(status))
220         logerror("%s hook terminated unsuccessfully (exit status %d)",
221                  name, WEXITSTATUS(status));
222     else if (WIFSIGNALED(status))
223         logerror("%s hook terminated abnormally (signal %d)",
224                  name, WTERMSIG(status));
225 #endif
226     else if (status)
227         logerror("%s hook terminated strangely (status %d)",
228                  name, status);
229     return status;
230 }