Freeciv-3.3
Loading...
Searching...
No Matches
srv_entrypoint.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include "fc_prehdrs.h"
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27
28#ifdef HAVE_SIGNAL_H
29#include <signal.h>
30#endif
31
32#ifdef FREECIV_MSWINDOWS
33#include <windows.h>
34#endif
35
36/* utility */
37#include "deprecations.h"
38#include "fc_cmdline.h"
39#include "fciconv.h"
40#include "fcintl.h"
41#include "log.h"
42#include "support.h"
43
44/* common */
45#include "capstr.h"
46#include "fc_cmdhelp.h"
47#include "game.h"
48#include "version.h"
49
50/* server */
51#include "aiiface.h"
52#include "console.h"
53#include "meta.h"
54#include "sernet.h"
55#include "srv_main.h"
56#include "srv_signal.h"
57
58/**********************************************************************/
63int main(int argc, char *argv[])
64{
65 int inx;
66 bool showhelp = FALSE;
67 bool showvers = FALSE;
68 char *option = NULL;
69
70 /* Load Windows post-crash debugger */
71#ifdef FREECIV_MSWINDOWS
72# ifndef FREECIV_NDEBUG
73 if (LoadLibrary("exchndl.dll") == NULL) {
74# ifdef FREECIV_DEBUG
75 fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
76# endif /* FREECIV_DEBUG */
77 }
78# endif /* FREECIV_NDEBUG */
79#endif /* FREECIV_MSWINDOWS */
80
82
83 /* Initialize server */
84 srv_init();
85
86 /* Parse command-line arguments... */
87
89
90 game.server.meta_info.type[0] = '\0';
91
92 /* No, we don't use GNU's getopt or even the "standard" getopt */
93 /* Yes, we do have reasons ;) */
94 /* FIXME: and that are? */
95 inx = 1;
96 while (inx < argc) {
97 if ((option = get_option_malloc("--file", argv, &inx, argc,
98 FALSE))) {
100 free(option);
101 } else if (is_option("--help", argv[inx])) {
102 showhelp = TRUE;
103 break;
104 } else if ((option = get_option_malloc("--log", argv, &inx, argc, TRUE))) {
106#ifndef FREECIV_NDEBUG
107 } else if (is_option("--Fatal", argv[inx])) {
108 if (inx + 1 >= argc || '-' == argv[inx + 1][0]) {
110 } else if (str_to_int(argv[inx + 1], &srvarg.fatal_assertions)) {
111 inx++;
112 } else {
113 fc_fprintf(stderr, _("Invalid signal number \"%s\".\n"),
114 argv[inx + 1]);
115 inx++;
116 showhelp = TRUE;
117 }
118#endif /* FREECIV_NDEBUG */
119 } else if ((option = get_option_malloc("--Ranklog", argv, &inx, argc, TRUE))) {
121 } else if (is_option("--keep", argv[inx])) {
123 /* Implies --meta */
125 } else if (is_option("--nometa", argv[inx])) {
126 fc_fprintf(stderr, _("Warning: the %s option is obsolete. "
127 "Use -m to enable the metaserver.\n"), argv[inx]);
128 showhelp = TRUE;
129 } else if (is_option("--meta", argv[inx])) {
131 } else if ((option = get_option_malloc("--Metaserver",
132 argv, &inx, argc, FALSE))) {
134 free(option);
135 srvarg.metaserver_no_send = FALSE; /* --Metaserver implies --meta */
136 } else if ((option = get_option_malloc("--identity",
137 argv, &inx, argc, FALSE))) {
139 free(option);
140 } else if ((option = get_option_malloc("--port", argv, &inx, argc, FALSE))) {
141 if (!str_to_int(option, &srvarg.port)) {
142 showhelp = TRUE;
143 break;
144 }
145 free(option);
146 } else if ((option = get_option_malloc("--bind", argv, &inx, argc, TRUE))) {
148 } else if ((option = get_option_malloc("--Bind-meta", argv, &inx, argc, TRUE))) {
150#ifdef FREECIV_WEB
151 } else if ((option = get_option_malloc("--type", argv, &inx, argc, FALSE))) {
153 free(option);
154#endif /* FREECIV_WEB */
155 } else if ((option = get_option_malloc("--read", argv, &inx, argc, TRUE)))
157 else if ((option = get_option_malloc("--quitidle", argv, &inx, argc, FALSE))) {
159 showhelp = TRUE;
160 break;
161 }
162 free(option);
163 } else if (is_option("--exit-on-end", argv[inx])) {
165 } else if ((option = get_option_malloc("--debug", argv, &inx, argc, FALSE))) {
167 showhelp = TRUE;
168 break;
169 }
170 free(option);
171#ifdef HAVE_FCDB
172 } else if ((option = get_option_malloc("--Database", argv, &inx, argc, FALSE))) {
173 /* Freed after file has been loaded - not here nor in server quit */
176 } else if (is_option("--auth", argv[inx])) {
178 } else if (is_option("--Guests", argv[inx])) {
180 } else if (is_option("--Newusers", argv[inx])) {
182#endif /* HAVE_FCDB */
183 } else if ((option = get_option_malloc("--Serverid", argv, &inx, argc, FALSE))) {
185 free(option);
186 } else if ((option = get_option_malloc("--saves", argv, &inx, argc, TRUE))) {
188 } else if ((option = get_option_malloc("--scenarios", argv, &inx, argc, TRUE))) {
190 } else if ((option = get_option_malloc("--ruleset", argv, &inx, argc, TRUE))) {
192 } else if (is_option("--version", argv[inx])) {
193 showvers = TRUE;
194 } else if ((option = get_option_malloc("--Announce", argv, &inx, argc, FALSE))) {
195 if (!fc_strcasecmp(option, "ipv4")) {
197 } else if (!fc_strcasecmp(option, "none")) {
199#ifdef FREECIV_IPV6_SUPPORT
200 } else if (!fc_strcasecmp(option, "ipv6")) {
202#endif /* IPv6 support */
203 } else {
204 log_error(_("Illegal value \"%s\" for --Announce"), option);
205 }
206 free(option);
207 } else if (is_option("--warnings", argv[inx])) {
209#ifdef AI_MODULES
210 } else if ((option = get_option_malloc("--LoadAI", argv, &inx, argc, FALSE))) {
211 if (!load_ai_module(option)) {
212 fc_fprintf(stderr, _("Failed to load AI module \"%s\"\n"), option);
214 }
215 free(option);
216#endif /* AI_MODULES */
217 } else {
218 fc_fprintf(stderr, _("Error: unknown option '%s'\n"), argv[inx]);
219 showhelp = TRUE;
220 break;
221 }
222 inx++;
223 }
224
225 if (showvers && !showhelp) {
228 }
229 con_write(C_VERSION, _("This is the server for %s"), freeciv_name_version());
230 /* TRANS: No full stop after the URL, could cause confusion. */
231 con_write(C_COMMENT, _("You can learn a lot about Freeciv at %s"),
233
234 if (showhelp) {
235 struct cmdhelp *help = cmdhelp_new(argv[0]);
236
237 cmdhelp_add(help, "A",
238 /* TRANS: "Announce" is exactly what user must type, do not translate. */
239 _("Announce PROTO"),
240 _("Announce game in LAN using protocol PROTO "
241 "(IPv4/IPv6/none)"));
242#ifdef HAVE_FCDB
243 cmdhelp_add(help, "D",
244 /* TRANS: "Database" is exactly what user must type, do not translate. */
245 _("Database FILE"),
246 _("Enable database connection with configuration from "
247 "FILE."));
248 cmdhelp_add(help, "a", "auth",
249 _("Enable server authentication (requires --Database)."));
250 cmdhelp_add(help, "G", "Guests",
251 _("Allow guests to login if auth is enabled."));
252 cmdhelp_add(help, "N", "Newusers",
253 _("Allow new users to login if auth is enabled."));
254#endif /* HAVE_FCDB */
255 cmdhelp_add(help, "b",
256 /* TRANS: "bind" is exactly what user must type, do not translate. */
257 _("bind ADDR"),
258 _("Listen for clients on ADDR"));
259 cmdhelp_add(help, "B", "Bind-meta ADDR",
260 _("Connect to metaserver from this address"));
261#ifdef FREECIV_DEBUG
262 cmdhelp_add(help, "d",
263 /* TRANS: "debug" is exactly what user must type, do not translate. */
264 _("debug LEVEL"),
265 _("Set debug log level (one of f,e,w,n,v,d, or "
266 "d:file1,min,max:...)"));
267#else /* FREECIV_DEBUG */
268 cmdhelp_add(help, "d",
269 /* TRANS: "debug" is exactly what user must type, do not translate. */
270 _("debug LEVEL"),
271 _("Set debug log level (one of f,e,w,n,v)"));
272#endif /* FREECIV_DEBUG */
273#ifndef FREECIV_NDEBUG
274 cmdhelp_add(help, "F",
275 /* TRANS: "Fatal" is exactly what user must type, do not translate. */
276 _("Fatal [SIGNAL]"),
277 _("Raise a signal on failed assertion"));
278#endif /* FREECIV_NDEBUG */
279 cmdhelp_add(help, "f",
280 /* TRANS: "file" is exactly what user must type, do not translate. */
281 _("file FILE"),
282 _("Load saved game FILE"));
283 cmdhelp_add(help, "h", "help",
284 _("Print a summary of the options"));
285 cmdhelp_add(help, "i",
286 /* TRANS: "identity" is exactly what user must type, do not translate. */
287 _("identity ADDR"),
288 _("Be known as ADDR at metaserver or LAN client"));
289 cmdhelp_add(help, "l",
290 /* TRANS: "log" is exactly what user must type, do not translate. */
291 _("log FILE"),
292 _("Use FILE as logfile"));
293 cmdhelp_add(help, "m", "meta",
294 _("Notify metaserver and send server's info"));
295 cmdhelp_add(help, "M",
296 /* TRANS: "Metaserver" is exactly what user must type, do not translate. */
297 _("Metaserver ADDR"),
298 _("Set ADDR as metaserver address"));
299#ifdef FREECIV_WEB
300 cmdhelp_add(help, "t",
301 /* TRANS: "type" is exactly what user must type, do not translate. */
302 _("type TYPE"),
303 _("Set TYPE as server type in metaserver"));
304#endif /* FREECIV_WEB */
305 cmdhelp_add(help, "k", "keep",
306 _("Keep updating game information on metaserver even after "
307 "failure")),
308 cmdhelp_add(help, "p",
309 /* TRANS: "port" is exactly what user must type, do not translate. */
310 _("port PORT"),
311 _("Listen for clients on port PORT"));
312 cmdhelp_add(help, "q",
313 /* TRANS: "quitidle" is exactly what user must type, do not translate. */
314 _("quitidle TIME"),
315 _("Quit if no players for TIME seconds"));
316 cmdhelp_add(help, "e", "exit-on-end",
317 _("When a game ends, exit instead of restarting"));
318 cmdhelp_add(help, "s",
319 /* TRANS: "saves" is exactly what user must type, do not translate. */
320 _("saves DIR"),
321 _("Save games to directory DIR"));
322 cmdhelp_add(help, NULL,
323 /* TRANS: "scenarios" is exactly what user must type, do not translate. */
324 _("scenarios DIR"),
325 _("Save scenarios to directory DIR"));
326 cmdhelp_add(help, "S",
327 /* TRANS: "Serverid" is exactly what user must type, do not translate. */
328 _("Serverid ID"),
329 _("Sets the server id to ID"));
330 cmdhelp_add(help, "r",
331 /* TRANS: "read" is exactly what user must type, do not translate. */
332 _("read FILE"),
333 _("Read startup script FILE"));
334 cmdhelp_add(help, "R",
335 /* TRANS: "Ranklog" is exactly what user must type, do not translate. */
336 _("Ranklog FILE"),
337 _("Use FILE as ranking logfile"));
338 cmdhelp_add(help, NULL,
339 /* TRANS: "ruleset" is exactly what user must type, do not translate. */
340 _("ruleset RULESET"),
341 _("Load ruleset RULESET"));
342#ifdef AI_MODULES
343 cmdhelp_add(help, "L",
344 /* TRANS: "LoadAI" is exactly what user must type, do not translate. */
345 _("LoadAI MODULE"),
346 _("Load ai module MODULE. Can appear multiple times"));
347#endif /* AI_MODULES */
348 cmdhelp_add(help, "v", "version",
349 _("Print the version number"));
350 cmdhelp_add(help, "w", "warnings",
351 _("Warn about deprecated modpack constructs"));
352
353 /* The function below prints a header and footer for the options.
354 * Furthermore, the options are sorted. */
356 cmdhelp_destroy(help);
357
359 }
360
361#ifdef HAVE_FCDB
364 _("Requested authentication with --auth, "
365 "but no --Database given\n"));
367 }
368#endif /* HAVE_FCDB */
369
370 /* disallow running as root -- too dangerous */
371 dont_run_as_root(argv[0], "freeciv_server");
372
374
375 /* have arguments, call the main server loop... */
376 srv_main();
377
378 /* Technically, we won't ever get here. We exit via server_quit. */
379
380 /* done */
382}
bool load_ai_module(const char *modname)
void init_our_capability(void)
Definition capstr.c:87
char * incite_cost
Definition comments.c:76
void con_write(enum rfc_status rfc_status, const char *message,...)
Definition console.c:203
@ C_COMMENT
Definition console.h:37
@ C_VERSION
Definition console.h:38
void deprecation_warnings_enable(void)
void cmdhelp_destroy(struct cmdhelp *pcmdhelp)
Definition fc_cmdhelp.c:70
void cmdhelp_display(struct cmdhelp *pcmdhelp, bool sort, bool gui_options, bool report_bugs)
Definition fc_cmdhelp.c:104
struct cmdhelp * cmdhelp_new(const char *cmdname)
Definition fc_cmdhelp.c:57
void cmdhelp_add(struct cmdhelp *pcmdhelp, const char *shortarg, const char *longarg, const char *helpstr,...)
Definition fc_cmdhelp.c:86
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
char * get_option_malloc(const char *option_name, char **argv, int *i, int argc, bool gc)
Definition fc_cmdline.c:50
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:61
bool log_parse_level_str(const char *level_str, enum log_level *ret_level)
Definition log.c:86
#define log_error(message,...)
Definition log.h:104
@ ANNOUNCE_IPV6
Definition net_types.h:56
@ ANNOUNCE_IPV4
Definition net_types.h:55
@ ANNOUNCE_NONE
Definition net_types.h:54
#define ANNOUNCE_DEFAULT
Definition net_types.h:59
void dont_run_as_root(const char *argv0, const char *fallback)
Definition shared.c:1542
bool str_to_int(const char *str, int *pint)
Definition shared.c:515
int main(int argc, char *argv[])
struct server_arguments srvarg
Definition srv_main.c:181
void fc__noreturn srv_main(void)
Definition srv_main.c:3566
void srv_init(void)
Definition srv_main.c:232
void setup_interrupt_handlers(void)
Definition srv_signal.c:111
char type[20]
Definition game.h:274
struct civ_game::@32::@36::@40 meta_info
struct civ_game::@32::@36 server
bool metaconnection_persistent
Definition srv_main.h:30
char * bind_addr
Definition srv_main.h:34
enum log_level loglevel
Definition srv_main.h:40
char metaserver_addr[256]
Definition srv_main.h:29
char * scenarios_pathname
Definition srv_main.h:47
enum announce_type announce
Definition srv_main.h:60
char load_filename[512]
Definition srv_main.h:44
char * fcdb_conf
Definition srv_main.h:56
bool auth_allow_guests
Definition srv_main.h:58
char * saves_pathname
Definition srv_main.h:46
char * log_filename
Definition srv_main.h:42
char * ranklog_filename
Definition srv_main.h:43
bool metaserver_no_send
Definition srv_main.h:28
char * bind_meta_addr
Definition srv_main.h:38
bool auth_allow_newusers
Definition srv_main.h:59
char * script_filename
Definition srv_main.h:45
char serverid[256]
Definition srv_main.h:49
char identity_name[256]
Definition srv_main.h:31
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define sz_strlcpy(dest, src)
Definition support.h:195
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
const char * freeciv_name_version(void)
Definition version.c:35