]> git.pond.sub.org Git - empserver/blob - src/server/update.c
(shutdwn): Take exclusive update lock for shutdown. This makes the
[empserver] / src / server / update.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 "misc.h"
46 #include "optlist.h"
47 #include "player.h"
48 #include "prototypes.h"
49 #include "server.h"
50
51 empth_sem_t *update_sem;
52 empth_rwlock_t *update_lock;
53 int update_pending;
54 time_t update_time;
55
56 static void update_sched(void *);
57 static void update_force(void *);
58 static void update_wait(void *unused);
59 static int run_hook(char *cmd, char *name);
60
61 void
62 update_init(void)
63 {
64     struct player *dp;
65     int stacksize;
66
67     update_sem = empth_sem_create("Update", 0);
68     update_lock = empth_rwlock_create("Update");
69     if (!update_sem || !update_lock)
70         exit_nomem();
71
72     dp = player_new(-1);
73     if (!dp)
74         exit_nomem();
75     /* FIXME ancient black magic; figure out true stack need */
76     stacksize = 100000 +
77 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
78                                         sizeof(char *));
79     if (!empth_create(PP_UPDATE, update_wait, stacksize, 0,
80                       "Update", "Updates the world", dp))
81         exit_nomem();
82
83     if (!empth_create(PP_SCHED, update_sched, 50 * 1024, 0,
84                       "UpdateSched", "Schedules updates to occur", NULL))
85         exit_nomem();
86 }
87
88 /*ARGSUSED*/
89 static void
90 update_sched(void *unused)
91 {
92     int wind;
93     time_t now, delta;
94
95     if (s_p_etu <= 0) {
96         logerror("bad value for s_p_etu (%d)", s_p_etu);
97         s_p_etu = 2 * 60;
98         logerror("setting s_p_etu to %d", s_p_etu);
99     }
100     while (1) {
101         time(&now);
102         next_update_time(&now, &update_time, &delta);
103         if (update_window > 0) {
104             wind = (random() % update_window);
105             update_time += wind;
106             delta += wind;
107         }
108         logerror("Next update at %s", ctime(&update_time));
109         logerror("Next update in %ld seconds", (long)delta);
110         /* sleep until update is scheduled to go off */
111         empth_sleep(update_time);
112         time(&now);
113         now += adj_update;
114         if (!gamehours(now)) {
115             logerror("No update permitted (hours restriction)");
116             continue;
117         }
118         if (!updatetime(&now)) {
119             logerror("No update wanted");
120             continue;
121         }
122         if (updates_disabled()) {
123             logerror("Updates disabled...skipping update");
124             continue;
125         }
126         empth_sem_signal(update_sem);
127     }
128     /*NOTREACHED*/
129 }
130
131 /*
132  * Trigger an update SECS_FROM_NOW seconds from now.
133  * Return 0 on success, -1 on failure.
134  */
135 int
136 update_trigger(time_t secs_from_now)
137 {
138     time_t *secp;
139
140     if (secs_from_now < 0)
141         return -1;
142
143     if (secs_from_now == 0) {
144         empth_sem_signal(update_sem);
145         return 0;
146     }
147
148     /* FIXME make triggers overwrite, not accumulate */
149     secp = malloc(sizeof(time_t));
150     if (!secp)
151         return -1;
152     *secp = secs_from_now;
153     if (!empth_create(PP_SCHED, update_force, 50 * 1024, 0, "forceUpdate",
154                       "Schedules an update", secp))
155         return -1;
156     return 0;
157 }
158
159 static void
160 update_force(void *seconds)
161 {
162     time_t now;
163
164     time(&now);
165     empth_sleep(now + *(time_t *)seconds);
166     empth_sem_signal(update_sem);
167     free(seconds);
168     empth_exit();
169 }
170
171 /*ARGSUSED*/
172 static void
173 update_wait(void *unused)
174 {
175     struct player *p;
176
177     player->proc = empth_self();
178     player->cnum = 0;
179     player->god = 1;
180
181     while (1) {
182         empth_sem_wait(update_sem);
183         update_pending = 1;
184         for (p = player_next(0); p != 0; 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->aborted = 1;
190                 empth_wakeup(p->proc);
191             }
192         }
193         empth_rwlock_wrlock(update_lock);
194         if (*pre_update_hook) {
195             if (run_hook(pre_update_hook, "pre-update")) {
196                 update_pending = 0;
197                 empth_rwlock_unlock(update_lock);
198                 continue;
199             }
200         }
201         update_main();
202         update_pending = 0;
203         empth_rwlock_unlock(update_lock);
204     }
205     /*NOTREACHED*/
206 }
207
208 static int
209 run_hook(char *cmd, char *name)
210 {
211     int status;
212     
213     fflush(NULL);
214     
215     status = system(cmd);
216     if (status == 0)
217         ;                       /* successful exit */
218     else if (status == -1)
219         logerror("couldn't execute command processor for %s hook (%s)",
220                  name, strerror(errno));
221 #ifndef _WIN32
222     else if (WIFEXITED(status))
223         logerror("%s hook terminated unsuccessfully (exit status %d)",
224                  name, WEXITSTATUS(status));
225     else if (WIFSIGNALED(status))
226         logerror("%s hook terminated abnormally (signal %d)",
227                  name, WTERMSIG(status));
228 #endif
229     else if (status)
230         logerror("%s hook terminated strangely (status %d)",
231                  name, status);
232     return status;
233 }