Freeciv-3.4
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 <stdlib.h>
21#include <string.h>
22
23#ifdef HAVE_UNISTD_H
24#include <unistd.h>
25#endif
26
27#ifdef HAVE_SIGNAL_H
28#include <signal.h>
29#endif
30
31/* utility */
32#include "deprecations.h"
33#include "executable.h"
34#include "fc_cmdline.h"
35#include "fciconv.h"
36#include "fcintl.h"
37#include "log.h"
38#include "support.h"
39
40/* common */
41#include "capstr.h"
42#include "fc_cmdhelp.h"
43#include "game.h"
44#include "version.h"
45
46/* server */
47#include "aiiface.h"
48#include "console.h"
49#include "meta.h"
50#include "sernet.h"
51#include "srv_main.h"
52#include "srv_signal.h"
53
54/**********************************************************************/
59int main(int argc, char *argv[])
60{
61 int inx;
62 bool showhelp = FALSE;
63 bool showvers = FALSE;
64 char *option = NULL;
65
68
69 /* Initialize server */
70 srv_init();
71
72 /* Parse command-line arguments... */
73
75
76 game.server.meta_info.type[0] = '\0';
77
78 /* No, we don't use GNU's getopt or even the "standard" getopt */
79 /* Yes, we do have reasons ;) */
80 /* FIXME: and that are? */
81 inx = 1;
82 while (inx < argc) {
83 if ((option = get_option_malloc("--file", argv, &inx, argc,
84 FALSE))) {
86 free(option);
87 } else if (is_option("--help", argv[inx])) {
88 showhelp = TRUE;
89 break;
90 } else if ((option = get_option_malloc("--log", argv, &inx, argc, TRUE))) {
92#ifndef FREECIV_NDEBUG
93 } else if (is_option("--Fatal", argv[inx])) {
94 if (inx + 1 >= argc || '-' == argv[inx + 1][0]) {
96 } else if (str_to_int(argv[inx + 1], &srvarg.fatal_assertions)) {
97 inx++;
98 } else {
99 fc_fprintf(stderr, _("Invalid signal number \"%s\".\n"),
100 argv[inx + 1]);
101 inx++;
102 showhelp = TRUE;
103 }
104#endif /* FREECIV_NDEBUG */
105 } else if ((option = get_option_malloc("--Ranklog", argv, &inx, argc, TRUE))) {
107 } else if (is_option("--keep", argv[inx])) {
109 /* Implies --meta */
111 } else if (is_option("--nometa", argv[inx])) {
112 fc_fprintf(stderr, _("Warning: the %s option is obsolete. "
113 "Use -m to enable the metaserver.\n"), argv[inx]);
114 showhelp = TRUE;
115 } else if (is_option("--meta", argv[inx])) {
117 } else if ((option = get_option_malloc("--Metaserver",
118 argv, &inx, argc, FALSE))) {
120 free(option);
121 srvarg.metaserver_no_send = FALSE; /* --Metaserver implies --meta */
122 } else if ((option = get_option_malloc("--identity",
123 argv, &inx, argc, FALSE))) {
125 free(option);
126 } else if ((option = get_option_malloc("--port", argv, &inx, argc, FALSE))) {
127 if (!str_to_int(option, &srvarg.port)) {
128 showhelp = TRUE;
129 break;
130 }
131 free(option);
132 } else if ((option = get_option_malloc("--bind", argv, &inx, argc, TRUE))) {
134 } else if ((option = get_option_malloc("--Bind-meta", argv, &inx, argc, TRUE))) {
136#ifdef FREECIV_WEB
137 } else if ((option = get_option_malloc("--type", argv, &inx, argc, FALSE))) {
139 free(option);
140#endif /* FREECIV_WEB */
141 } else if ((option = get_option_malloc("--read", argv, &inx, argc, TRUE)))
143 else if ((option = get_option_malloc("--quitidle", argv, &inx, argc, FALSE))) {
145 showhelp = TRUE;
146 break;
147 }
148 free(option);
149 } else if (is_option("--exit-on-end", argv[inx])) {
151 } else if ((option = get_option_malloc("--debug", argv, &inx, argc, FALSE))) {
153 showhelp = TRUE;
154 break;
155 }
156 free(option);
157#ifdef HAVE_FCDB
158 } else if ((option = get_option_malloc("--Database", argv, &inx, argc, FALSE))) {
159 /* Freed after file has been loaded - not here nor in server quit */
162 } else if (is_option("--auth", argv[inx])) {
164 } else if (is_option("--Guests", argv[inx])) {
166 } else if (is_option("--Newusers", argv[inx])) {
168#endif /* HAVE_FCDB */
169 } else if ((option = get_option_malloc("--Serverid", argv, &inx, argc, FALSE))) {
171 free(option);
172 } else if ((option = get_option_malloc("--saves", argv, &inx, argc, TRUE))) {
174 } else if ((option = get_option_malloc("--scenarios", argv, &inx, argc, TRUE))) {
176 } else if ((option = get_option_malloc("--ruleset", argv, &inx, argc, TRUE))) {
178 } else if (is_option("--version", argv[inx])) {
179 showvers = TRUE;
180 } else if ((option = get_option_malloc("--Announce", argv, &inx, argc, FALSE))) {
181 if (!fc_strcasecmp(option, "ipv4")) {
183 } else if (!fc_strcasecmp(option, "none")) {
185#ifdef FREECIV_IPV6_SUPPORT
186 } else if (!fc_strcasecmp(option, "ipv6")) {
188#endif /* IPv6 support */
189 } else {
190 log_error(_("Illegal value \"%s\" for --Announce"), option);
191 }
192 free(option);
193 } else if (is_option("--warnings", argv[inx])) {
195#ifdef AI_MODULES
196 } else if ((option = get_option_malloc("--LoadAI", argv, &inx, argc, FALSE))) {
197 if (!load_ai_module(option)) {
198 fc_fprintf(stderr, _("Failed to load AI module \"%s\"\n"), option);
200 }
201 free(option);
202#endif /* AI_MODULES */
203 } else {
204 fc_fprintf(stderr, _("Error: unknown option '%s'\n"), argv[inx]);
205 showhelp = TRUE;
206 break;
207 }
208 inx++;
209 }
210
211 if (showvers && !showhelp) {
214 }
215 con_write(C_VERSION, _("This is the server for %s"), freeciv_name_version());
216 /* TRANS: No full stop after the URL, could cause confusion. */
217 con_write(C_COMMENT, _("You can learn a lot about Freeciv at %s"),
219
220 if (showhelp) {
221 struct cmdhelp *help = cmdhelp_new(argv[0]);
222
223 cmdhelp_add(help, "A",
224 /* TRANS: "Announce" is exactly what user must type, do not translate. */
225 _("Announce PROTO"),
226 _("Announce game in LAN using protocol PROTO "
227 "(IPv4/IPv6/none)"));
228#ifdef HAVE_FCDB
229 cmdhelp_add(help, "D",
230 /* TRANS: "Database" is exactly what user must type, do not translate. */
231 _("Database FILE"),
232 _("Enable database connection with configuration from "
233 "FILE."));
234 cmdhelp_add(help, "a", "auth",
235 _("Enable server authentication (requires --Database)."));
236 cmdhelp_add(help, "G", "Guests",
237 _("Allow guests to login if auth is enabled."));
238 cmdhelp_add(help, "N", "Newusers",
239 _("Allow new users to login if auth is enabled."));
240#endif /* HAVE_FCDB */
241 cmdhelp_add(help, "b",
242 /* TRANS: "bind" is exactly what user must type, do not translate. */
243 _("bind ADDR"),
244 _("Listen for clients on ADDR"));
245 cmdhelp_add(help, "B", "Bind-meta ADDR",
246 _("Connect to metaserver from this address"));
247#ifdef FREECIV_DEBUG
248 cmdhelp_add(help, "d",
249 /* TRANS: "debug" is exactly what user must type, do not translate. */
250 _("debug LEVEL"),
251 _("Set debug log level (one of f,e,w,n,v,d, or "
252 "d:file1,min,max:...)"));
253#else /* FREECIV_DEBUG */
254 cmdhelp_add(help, "d",
255 /* TRANS: "debug" is exactly what user must type, do not translate. */
256 _("debug LEVEL"),
257 _("Set debug log level (one of f,e,w,n,v)"));
258#endif /* FREECIV_DEBUG */
259#ifndef FREECIV_NDEBUG
260 cmdhelp_add(help, "F",
261 /* TRANS: "Fatal" is exactly what user must type, do not translate. */
262 _("Fatal [SIGNAL]"),
263 _("Raise a signal on failed assertion"));
264#endif /* FREECIV_NDEBUG */
265 cmdhelp_add(help, "f",
266 /* TRANS: "file" is exactly what user must type, do not translate. */
267 _("file FILE"),
268 _("Load saved game FILE"));
269 cmdhelp_add(help, "h", "help",
270 _("Print a summary of the options"));
271 cmdhelp_add(help, "i",
272 /* TRANS: "identity" is exactly what user must type, do not translate. */
273 _("identity ADDR"),
274 _("Be known as ADDR at metaserver or LAN client"));
275 cmdhelp_add(help, "l",
276 /* TRANS: "log" is exactly what user must type, do not translate. */
277 _("log FILE"),
278 _("Use FILE as logfile"));
279 cmdhelp_add(help, "m", "meta",
280 _("Notify metaserver and send server's info"));
281 cmdhelp_add(help, "M",
282 /* TRANS: "Metaserver" is exactly what user must type, do not translate. */
283 _("Metaserver ADDR"),
284 _("Set ADDR as metaserver address"));
285#ifdef FREECIV_WEB
286 cmdhelp_add(help, "t",
287 /* TRANS: "type" is exactly what user must type, do not translate. */
288 _("type TYPE"),
289 _("Set TYPE as server type in metaserver"));
290#endif /* FREECIV_WEB */
291 cmdhelp_add(help, "k", "keep",
292 _("Keep updating game information on metaserver even after "
293 "failure")),
294 cmdhelp_add(help, "p",
295 /* TRANS: "port" is exactly what user must type, do not translate. */
296 _("port PORT"),
297 _("Listen for clients on port PORT"));
298 cmdhelp_add(help, "q",
299 /* TRANS: "quitidle" is exactly what user must type, do not translate. */
300 _("quitidle TIME"),
301 _("Quit if no players for TIME seconds"));
302 cmdhelp_add(help, "e", "exit-on-end",
303 _("When a game ends, exit instead of restarting"));
304 cmdhelp_add(help, "s",
305 /* TRANS: "saves" is exactly what user must type, do not translate. */
306 _("saves DIR"),
307 _("Save games to directory DIR"));
308 cmdhelp_add(help, NULL,
309 /* TRANS: "scenarios" is exactly what user must type, do not translate. */
310 _("scenarios DIR"),
311 _("Save scenarios to directory DIR"));
312 cmdhelp_add(help, "S",
313 /* TRANS: "Serverid" is exactly what user must type, do not translate. */
314 _("Serverid ID"),
315 _("Sets the server id to ID"));
316 cmdhelp_add(help, "r",
317 /* TRANS: "read" is exactly what user must type, do not translate. */
318 _("read FILE"),
319 _("Read startup script FILE"));
320 cmdhelp_add(help, "R",
321 /* TRANS: "Ranklog" is exactly what user must type, do not translate. */
322 _("Ranklog FILE"),
323 _("Use FILE as ranking logfile"));
324 cmdhelp_add(help, NULL,
325 /* TRANS: "ruleset" is exactly what user must type, do not translate. */
326 _("ruleset RULESET"),
327 _("Load ruleset RULESET"));
328#ifdef AI_MODULES
329 cmdhelp_add(help, "L",
330 /* TRANS: "LoadAI" is exactly what user must type, do not translate. */
331 _("LoadAI MODULE"),
332 _("Load ai module MODULE. Can appear multiple times"));
333#endif /* AI_MODULES */
334 cmdhelp_add(help, "v", "version",
335 _("Print the version number"));
336 cmdhelp_add(help, "w", "warnings",
337 _("Warn about deprecated modpack constructs"));
338
339 /* The function below prints a header and footer for the options.
340 * Furthermore, the options are sorted. */
342 cmdhelp_destroy(help);
343
345 }
346
347#ifdef HAVE_FCDB
350 _("Requested authentication with --auth, "
351 "but no --Database given\n"));
353 }
354#endif /* HAVE_FCDB */
355
356 /* disallow running as root -- too dangerous */
357 dont_run_as_root(argv[0], "freeciv_server");
358
360
361 /* have arguments, call the main server loop... */
362 srv_main();
363
364 /* Technically, we won't ever get here. We exit via server_quit. */
365
366 /* done */
368}
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 executable_init(void)
Definition executable.c:34
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:62
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:182
void fc__noreturn srv_main(void)
Definition srv_main.c:3572
void srv_init(void)
Definition srv_main.c:233
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