]> git.pond.sub.org Git - empserver/blob - src/server/update.c
Fix xdump updates not to dump bogus extra updates
[empserver] / src / server / update.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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-2010
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 "empthread.h"
44 #include "file.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 /*
53  * Update is running.
54  * Can be used to suppress messages, or direct them to bulletins.
55  */
56 int update_running;
57
58 static time_t update_schedule_anchor;
59 static int update_wanted;
60
61 static empth_t *update_thread;
62
63 static int update_get_schedule(void);
64 static void update_sched(void *);
65 static void update_run(void);
66
67 void
68 update_init(void)
69 {
70     struct player *dp;
71     int stacksize;
72
73     update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
74     if (update_get_schedule() < 0)
75         exit(1);
76
77     play_lock = empth_rwlock_create("Update");
78     if (!play_lock)
79         exit_nomem();
80
81     dp = player_new(-1);
82     if (!dp)
83         exit_nomem();
84     /* FIXME ancient black magic; figure out true stack need */
85     stacksize = 100000 +
86 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
87                                         sizeof(char *));
88     update_thread = empth_create(update_sched, stacksize, 0, "Update", dp);
89     if (!update_thread)
90         exit_nomem();
91 }
92
93 /*
94  * Get the schedule for future updates into update_time[].
95  * Return 0 on success, -1 on failure.
96  */
97 static int
98 update_get_schedule(void)
99 {
100     time_t now = time(NULL);
101     int n = sizeof(update_time) / sizeof(*update_time);
102     int i;
103
104     ef_truncate(EF_UPDATES, 0);
105     ef_extend(EF_UPDATES, n - 1);
106     if (read_schedule(schedulefil, update_time, n,
107                       now + 30, update_schedule_anchor) < 0) {
108         logerror("No update schedule!");
109         ef_truncate(EF_UPDATES, 0);
110         return -1;
111     }
112     logerror("Update schedule read");
113     for (i = 0; update_time[i]; i++) ;
114     ef_truncate(EF_UPDATES, i);
115     return 0;
116 }
117
118 /*ARGSUSED*/
119 static void
120 update_sched(void *unused)
121 {
122     time_t next_update, now;
123
124     player->proc = empth_self();
125     player->cnum = 0;
126     player->god = 1;
127
128     for (;;) {
129         /*
130          * Sleep until the next scheduled update or an unscheduled
131          * wakeup.
132          */
133         next_update = update_time[0];
134         if (next_update) {
135             if (update_window > 0)
136                 next_update += random() % update_window;
137             logerror("Next update at %s", ctime(&next_update));
138             /* sleep until update is scheduled to go off */
139             empth_sleep(next_update);
140         } else {
141             logerror("No update scheduled");
142             /* want to sleep forever, but empthread doesn't provide that */
143             while (empth_sleep(time(NULL) + (60 * 60 * 24)) >= 0) ;
144         }
145
146         now = time(NULL);
147         if (next_update != 0 && now >= next_update) {
148             /* scheduled update time reached */
149             if (now >= next_update + 60)
150                 logerror("Missed the update!");
151             else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
152                 ;
153             else if (updates_disabled())
154                 logerror("Updates disabled...skipping update");
155             else
156                 update_wanted = 1;
157             update_schedule_anchor = update_time[0];
158         }
159         /* else unscheduled update if update_wanted is set */
160
161         if (update_wanted) {
162             update_wanted = 0;
163             update_run();
164         }
165
166         update_get_schedule();
167     }
168     /*NOTREACHED*/
169 }
170
171 /*
172  * Trigger an update.
173  * Return 0 on success, -1 on failure.
174  */
175 int
176 update_trigger(void)
177 {
178     logerror("Triggering unscheduled update");
179     update_wanted = 1;
180     empth_wakeup(update_thread);
181     return 0;
182 }
183
184 /*
185  * Reload the update schedule.
186  * Return 0 on success, -1 on failure.
187  */
188 int
189 update_reschedule(void)
190 {
191     empth_wakeup(update_thread);
192     return 0;
193 }
194
195 static void
196 update_run(void)
197 {
198     struct player *p;
199
200     for (p = player_next(NULL); p; p = player_next(p)) {
201         if (p->state != PS_PLAYING)
202             continue;
203         if (p->command) {
204             pr_flash(p, "Update aborting command\n");
205             p->may_sleep = PLAYER_SLEEP_NEVER;
206             p->aborted = 1;
207             empth_wakeup(p->proc);
208         }
209     }
210     empth_rwlock_wrlock(play_lock);
211     if (*pre_update_hook) {
212         if (run_hook(pre_update_hook, "pre-update")) {
213             empth_rwlock_unlock(play_lock);
214             return;
215         }
216     }
217     update_running = 1;
218     update_main();
219     update_running = 0;
220     empth_rwlock_unlock(play_lock);
221 }
222
223 int
224 run_hook(char *cmd, char *name)
225 {
226     int status;
227
228     fflush(NULL);
229
230     status = system(cmd);
231     if (status == 0)
232         ;                       /* successful exit */
233     else if (status == -1)
234         logerror("couldn't execute command processor for %s hook (%s)",
235                  name, strerror(errno));
236 #ifndef _WIN32
237     else if (WIFEXITED(status))
238         logerror("%s hook terminated unsuccessfully (exit status %d)",
239                  name, WEXITSTATUS(status));
240     else if (WIFSIGNALED(status))
241         logerror("%s hook terminated abnormally (signal %d)",
242                  name, WTERMSIG(status));
243 #endif
244     else if (status)
245         logerror("%s hook terminated strangely (status %d)",
246                  name, status);
247     return status;
248 }