]> 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-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  */
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 "misc.h"
45 #include "optlist.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "server.h"
49
50 empth_sem_t *update_sem;
51 time_t update_time;
52
53 static void update_wait(void *unused);
54 static int run_hook(char *cmd, char *name);
55
56 /*ARGSUSED*/
57 void
58 update_sched(void *unused)
59 {
60     int wind;
61     time_t now, delta;
62
63     update_sem = empth_sem_create("Update", 0);
64     empth_create(PP_SCHED, update_wait, (50 * 1024), 0, "UpdateWait",
65                  "Waits until players idle", 0);
66     time(&now);
67     if (s_p_etu <= 0) {
68         logerror("bad value for s_p_etu (%d)", s_p_etu);
69         s_p_etu = 2 * 60;
70         logerror("setting s_p_etu to %d", s_p_etu);
71     }
72     while (1) {
73         time(&now);
74         next_update_time(&now, &update_time, &delta);
75         if (update_window > 0) {
76             wind = (random() % update_window);
77             update_time += wind;
78             delta += wind;
79         }
80         logerror("Next update at %s", ctime(&update_time));
81         logerror("Next update in %ld seconds", (long)delta);
82         /* sleep until update is scheduled to go off */
83         empth_sleep(update_time);
84         time(&now);
85         now += adj_update;
86         if (!gamehours(now)) {
87             logerror("No update permitted (hours restriction)");
88             continue;
89         }
90         if (!updatetime(&now)) {
91             logerror("No update wanted");
92             continue;
93         }
94         if (updates_disabled()) {
95             logerror("Updates disabled...skipping update");
96             continue;
97         }
98         empth_sem_signal(update_sem);
99     }
100     /*NOTREACHED*/
101 }
102
103 void
104 update_force(void *seconds)
105 {
106     time_t now;
107
108     time(&now);
109     empth_sleep(now + *(int *)seconds);
110     empth_sem_signal(update_sem);
111     empth_exit();
112 }
113
114 /*ARGSUSED*/
115 static void
116 update_wait(void *unused)
117 {
118     struct player *p;
119     int running;
120     time_t now;
121     int stacksize;
122     struct player *dp;
123
124     while (1) {
125         empth_sem_wait(update_sem);
126         update_pending = 1;
127         running = 0;
128         for (p = player_next(0); p != 0; p = player_next(p)) {
129             if (p->state != PS_PLAYING)
130                 continue;
131             if (p->command) {
132                 pr_flash(p, "Update aborting command\n");
133                 p->aborted = 1;
134                 empth_wakeup(p->proc);
135                 running++;
136             }
137         }
138         time(&now);
139         if (running) {
140             /* sleep a few, wait for aborts to take effect */
141             empth_sleep(now + 2);
142         }
143         if (*pre_update_hook) {
144             if (run_hook(pre_update_hook, "pre-update")) {
145                 update_pending = 0;
146                 continue;
147             }
148         }
149         /* 
150          * we rely on the fact that update's priority is the highest
151          * in the land so it can finish before it yields.
152          */
153         dp = player_new(-1);
154         if (!dp) {
155             logerror("can't create dummy player for update");
156             update_pending = 0;
157             continue;
158         }
159         stacksize = 100000 +
160 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
161                                         sizeof(char *));
162
163         empth_create(PP_UPDATE, update_main, stacksize, 0,
164                      "UpdateRun", "Updates the world", dp);
165     }
166     /*NOTREACHED*/
167 }
168
169 static int
170 run_hook(char *cmd, char *name)
171 {
172     int status;
173     
174     fflush(NULL);
175     
176     status = system(cmd);
177     if (status == 0)
178         ;                       /* successful exit */
179     else if (status == -1)
180         logerror("couldn't execute command processor for %s hook (%s)",
181                  name, strerror(errno));
182 #ifndef _WIN32
183     else if (WIFEXITED(status))
184         logerror("%s hook terminated unsuccessfully (exit status %d)",
185                  name, WEXITSTATUS(status));
186     else if (WIFSIGNALED(status))
187         logerror("%s hook terminated abnormally (signal %d)",
188                  name, WTERMSIG(status));
189 #endif
190     else if (status)
191         logerror("%s hook terminated strangely (status %d)",
192                  name, status);
193     return status;
194 }