]> git.pond.sub.org Git - empserver/blob - src/server/update.c
Update copyright notice
[empserver] / src / server / update.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  update.c: Update scheduler
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996
33  *     Ron Koenderink, 2005
34  *     Markus Armbruster, 2007-2009
35  */
36
37 #include <config.h>
38
39 #include <errno.h>
40 #ifndef _WIN32
41 #include <sys/wait.h>
42 #endif
43 #include <time.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 /*
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,
89                                  "Update", dp);
90     if (!update_thread)
91         exit_nomem();
92 }
93
94 /*
95  * Get the schedule for future updates into update_time[].
96  * Return 0 on success, -1 on failure.
97  */
98 static int
99 update_get_schedule(void)
100 {
101     time_t now = time(NULL);
102
103     if (read_schedule(schedulefil, update_time,
104                       sizeof(update_time) / sizeof(*update_time),
105                       now + 30, update_schedule_anchor) < 0) {
106         logerror("No update schedule!");
107         update_time[0] = 0;
108         return -1;
109     }
110     logerror("Update schedule read");
111     return 0;
112 }
113
114 /*ARGSUSED*/
115 static void
116 update_sched(void *unused)
117 {
118     time_t next_update, now;
119
120     player->proc = empth_self();
121     player->cnum = 0;
122     player->god = 1;
123
124     for (;;) {
125         /*
126          * Sleep until the next scheduled update or an unscheduled
127          * wakeup.
128          */
129         next_update = update_time[0];
130         if (next_update) {
131             if (update_window > 0)
132                 next_update += random() % update_window;
133             logerror("Next update at %s", ctime(&next_update));
134             /* sleep until update is scheduled to go off */
135             empth_sleep(next_update);
136         } else {
137             logerror("No update scheduled");
138             /* want to sleep forever, but empthread doesn't provide that */
139             while (empth_sleep(time(NULL) + (60 * 60 * 24)) >= 0) ;
140         }
141
142         now = time(NULL);
143         if (next_update != 0 && now >= next_update) {
144             /* scheduled update time reached */
145             if (now >= next_update + 60)
146                 logerror("Missed the update!");
147             else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
148                 ;
149             else if (updates_disabled())
150                 logerror("Updates disabled...skipping update");
151             else
152                 update_wanted = 1;
153             update_schedule_anchor = update_time[0];
154         }
155         /* else unscheduled update if update_wanted is set */
156
157         if (update_wanted) {
158             update_wanted = 0;
159             update_run();
160         }
161
162         update_get_schedule();
163     }
164     /*NOTREACHED*/
165 }
166
167 /*
168  * Trigger an update.
169  * Return 0 on success, -1 on failure.
170  */
171 int
172 update_trigger(void)
173 {
174     logerror("Triggering unscheduled update");
175     update_wanted = 1;
176     empth_wakeup(update_thread);
177     return 0;
178 }
179
180 /*
181  * Reload the update schedule.
182  * Return 0 on success, -1 on failure.
183  */
184 int
185 update_reschedule(void)
186 {
187     empth_wakeup(update_thread);
188     return 0;
189 }
190
191 static void
192 update_run(void)
193 {
194     struct player *p;
195
196     for (p = player_next(NULL); p; p = player_next(p)) {
197         if (p->state != PS_PLAYING)
198             continue;
199         if (p->command) {
200             pr_flash(p, "Update aborting command\n");
201             p->may_sleep = PLAYER_SLEEP_NEVER;
202             p->aborted = 1;
203             empth_wakeup(p->proc);
204         }
205     }
206     empth_rwlock_wrlock(play_lock);
207     if (*pre_update_hook) {
208         if (run_hook(pre_update_hook, "pre-update")) {
209             empth_rwlock_unlock(play_lock);
210             return;
211         }
212     }
213     update_running = 1;
214     update_main();
215     update_running = 0;
216     empth_rwlock_unlock(play_lock);
217 }
218
219 int
220 run_hook(char *cmd, char *name)
221 {
222     int status;
223
224     fflush(NULL);
225
226     status = system(cmd);
227     if (status == 0)
228         ;                       /* successful exit */
229     else if (status == -1)
230         logerror("couldn't execute command processor for %s hook (%s)",
231                  name, strerror(errno));
232 #ifndef _WIN32
233     else if (WIFEXITED(status))
234         logerror("%s hook terminated unsuccessfully (exit status %d)",
235                  name, WEXITSTATUS(status));
236     else if (WIFSIGNALED(status))
237         logerror("%s hook terminated abnormally (signal %d)",
238                  name, WTERMSIG(status));
239 #endif
240     else if (status)
241         logerror("%s hook terminated strangely (status %d)",
242                  name, status);
243     return status;
244 }