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