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