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