Merge pre-4-2-20-invasive. Summary of changes:
* Acquire resources before daemonizing, fail in foreground * Initialize threads and signals after daemonizing * Make most file names relative to data directory * emp_server and files make it their working directory * emp_config() no longer screws up some file names * Missing or incorrect econfig is now fatal * Don't log to default log file when econfig changes it
This commit is contained in:
commit
5b57f4c222
17 changed files with 201 additions and 267 deletions
|
@ -37,8 +37,8 @@
|
|||
#include <stddef.h>
|
||||
|
||||
struct empfile {
|
||||
char *name; /* file name (e.g., "treaty") */
|
||||
char *file; /* file path */
|
||||
char *name; /* Empire name (e.g., "treaty") */
|
||||
char *file; /* file name (relative to data directory) */
|
||||
int flags; /* misc stuff */
|
||||
int mode; /* O_flags */
|
||||
int size; /* size of object */
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
#ifndef _ITEM_H_
|
||||
#define _ITEM_H_
|
||||
|
||||
#include "misc.h"
|
||||
|
||||
typedef enum {
|
||||
IPKG, /* "inefficient" packaging (eff<60) */
|
||||
NPKG, /* no special packaging */
|
||||
|
|
|
@ -124,6 +124,7 @@ typedef short coord;
|
|||
#define days(x) (60*60*24*(x))
|
||||
|
||||
extern int debug;
|
||||
extern int daemonize;
|
||||
|
||||
/*
|
||||
* If EXPR is true, an internal error occured.
|
||||
|
|
|
@ -37,6 +37,9 @@
|
|||
|
||||
#include "nsc.h"
|
||||
|
||||
/* Default econfig file */
|
||||
extern char dflt_econfig[];
|
||||
|
||||
struct option_list {
|
||||
char *opt_key;
|
||||
int *opt_valuep;
|
||||
|
|
|
@ -48,7 +48,8 @@
|
|||
extern void close_files(void);
|
||||
extern void panic(int sig);
|
||||
extern void shutdwn(int sig);
|
||||
extern void start_server(int, char *);
|
||||
extern void init_server(void);
|
||||
extern void start_server(int);
|
||||
#if defined(_WIN32)
|
||||
extern void loc_NTTerm(void);
|
||||
#endif
|
||||
|
|
|
@ -35,9 +35,9 @@
|
|||
#define SERVICE_H
|
||||
|
||||
#define DEFAULT_SERVICE_NAME "Empire Server"
|
||||
extern char *config_file;
|
||||
|
||||
extern int install_service(char *program_name, char *service_name, int datadir_set);
|
||||
extern int install_service(char *program_name, char *service_name,
|
||||
int datadir_set, char *config_file);
|
||||
extern int remove_service(char *service_name);
|
||||
extern void WINAPI service_main(DWORD argc, LPTSTR *argv);
|
||||
#endif
|
||||
|
|
|
@ -472,7 +472,7 @@ empth_exit(void)
|
|||
if (pThread->bMainThread) {
|
||||
/* The main line. Wait forever. */
|
||||
while (1) {
|
||||
if (!debug) {
|
||||
if (daemonize) {
|
||||
if (service_stopped())
|
||||
shutdwn(0);
|
||||
Sleep(3);
|
||||
|
|
|
@ -54,7 +54,7 @@ disassoc(void)
|
|||
|
||||
if (fork() != 0)
|
||||
exit(0);
|
||||
for (i = 0; i < 10; i++)
|
||||
for (i = 0; i < 2; i++)
|
||||
(void)close(i);
|
||||
(void)open("/", O_RDONLY, 0);
|
||||
(void)dup2(0, 1);
|
||||
|
|
|
@ -88,10 +88,8 @@ emp_config(char *file)
|
|||
int errors = 0;
|
||||
int i;
|
||||
|
||||
if (file == NULL) {
|
||||
fixup_files();
|
||||
return 0;
|
||||
}
|
||||
if (!file)
|
||||
file = dflt_econfig;
|
||||
if ((fp = fopen(file, "r")) == NULL) {
|
||||
fprintf(stderr, "Can't open %s for reading (%s)\n",
|
||||
file, strerror(errno));
|
||||
|
@ -159,51 +157,11 @@ emp_config(char *file)
|
|||
}
|
||||
}
|
||||
fclose(fp);
|
||||
fixup_files();
|
||||
WORLD_X &= ~1; /* make even */
|
||||
|
||||
return -errors;
|
||||
}
|
||||
|
||||
struct otherfiles {
|
||||
char **files;
|
||||
char *name;
|
||||
};
|
||||
|
||||
/* list of other well known files... -maybe tailor these oneday
|
||||
* anyway - meantime they are all relative to datadir */
|
||||
static struct otherfiles ofiles[] = {
|
||||
{&motdfil, "motd"},
|
||||
{&downfil, "down"},
|
||||
{&disablefil, "disable"},
|
||||
{&banfil, "ban"},
|
||||
{&authfil, "auth"},
|
||||
{&annfil, "ann"},
|
||||
{×tampfil, "timestamp"},
|
||||
{&teldir, "tel"},
|
||||
{&telfil, "tel/tel"},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* fix up the empfile struct to reference full path names */
|
||||
static void
|
||||
fixup_files(void)
|
||||
{
|
||||
struct empfile *ep;
|
||||
struct otherfiles *op;
|
||||
s_char buf[1024];
|
||||
|
||||
for (ep = empfile; ep < &empfile[EF_MAX]; ep++) {
|
||||
sprintf(buf, "%s/%s", datadir, ep->name);
|
||||
ep->file = strdup(buf);
|
||||
}
|
||||
|
||||
for (op = ofiles; op->files; op++) {
|
||||
sprintf(buf, "%s/%s", datadir, op->name);
|
||||
*op->files = strdup(buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* find the key in the table */
|
||||
static struct keymatch *
|
||||
keylookup(register s_char *command, struct keymatch *tbl)
|
||||
|
@ -222,9 +180,7 @@ keylookup(register s_char *command, struct keymatch *tbl)
|
|||
void
|
||||
print_config(FILE *fp)
|
||||
{
|
||||
struct empfile *ep;
|
||||
struct option_list *op;
|
||||
struct otherfiles *ofp;
|
||||
struct keymatch *kp;
|
||||
|
||||
fprintf(fp, "# Empire Configuration File:\n");
|
||||
|
@ -263,11 +219,6 @@ print_config(FILE *fp)
|
|||
}
|
||||
|
||||
fprintf(fp, "\n");
|
||||
for (ep = empfile; ep < &empfile[EF_MAX]; ep++)
|
||||
fprintf(fp, "# File %s -> %s\n", ep->name, ep->file);
|
||||
for (ofp = ofiles; ofp->files; ofp++)
|
||||
fprintf(fp, "# File %s -> %s\n", ofp->name, *(ofp->files));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -36,14 +36,10 @@
|
|||
|
||||
#include "prototypes.h"
|
||||
#include "service.h"
|
||||
#include "../gen/getopt.h"
|
||||
#include "optlist.h"
|
||||
|
||||
|
||||
char *config_file = NULL;
|
||||
|
||||
int
|
||||
install_service(char *program_name, char *service_name, int datadir_set)
|
||||
install_service(char *program_name, char *service_name, int datadir_set, char *config_file)
|
||||
{
|
||||
char strDir[1024];
|
||||
HANDLE schSCManager,schService;
|
||||
|
@ -74,8 +70,7 @@ install_service(char *program_name, char *service_name, int datadir_set)
|
|||
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
|
||||
|
||||
if (schSCManager == NULL) {
|
||||
logerror("install_service failed to open Service Control Manager");
|
||||
printf("Install service: failed to open Service Control Manager.\n");
|
||||
fprintf(stderr, "install_service failed to open Service Control Manager\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
@ -96,8 +91,7 @@ install_service(char *program_name, char *service_name, int datadir_set)
|
|||
NULL); /* no password */
|
||||
|
||||
if (schService == NULL) {
|
||||
logerror("install_service failed to create service %s", service_name);
|
||||
printf("Install service: failed to create service %s.\n", service_name);
|
||||
fprintf(stderr, "install_service failed to create service %s\n", service_name);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
sdBuf.lpDescription = "Server for Empire game";
|
||||
|
@ -106,11 +100,9 @@ install_service(char *program_name, char *service_name, int datadir_set)
|
|||
schService, /* handle to service */
|
||||
SERVICE_CONFIG_DESCRIPTION, /* change: description */
|
||||
&sdBuf)) { /* value: new description */
|
||||
logerror("install_service failed to set the description");
|
||||
printf("Install service: failed to set the description.\n");
|
||||
fprintf(stderr, "install_service failed to set the description\n");
|
||||
}
|
||||
|
||||
logerror("install_service successfully created the service %s", service_name);
|
||||
printf("Service %s installed.\n", service_name);
|
||||
CloseServiceHandle(schService);
|
||||
return EXIT_SUCCESS;
|
||||
|
@ -130,31 +122,26 @@ remove_service(char *service_name)
|
|||
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
||||
|
||||
if (schSCManager == NULL) {
|
||||
logerror("remove_service failed to open Service Control Manager");
|
||||
printf("remove service: failed to open Service Control Manager.\n");
|
||||
fprintf(stderr, "remove_service failed to open Service Control Manager\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
|
||||
|
||||
if (hService == NULL) {
|
||||
logerror("remove_service failed to open service %s", service_name);
|
||||
printf("Remove service: failed to open service %s.\n", service_name);
|
||||
fprintf(stderr, "remove_service failed to open service %s\n", service_name);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (DeleteService(hService) == 0) {
|
||||
logerror("remove_service failed to remove service %s", service_name);
|
||||
printf("Remove service: failed to remove service %s.\n", service_name);
|
||||
fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (CloseServiceHandle(hService) == 0) {
|
||||
logerror("remove_service failed to close service %s", service_name);
|
||||
printf("Remove service: failed to close service %s.\n", service_name);
|
||||
fprintf(stderr, "remove_service failed to close service %s\n", service_name);
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
logerror("remove_service successfully removed service %s", service_name);
|
||||
printf("Service %s removed.\n", service_name);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -215,29 +202,7 @@ service_ctrl_handler(DWORD Opcode)
|
|||
|
||||
void WINAPI
|
||||
service_main(DWORD argc, LPTSTR *argv)
|
||||
{
|
||||
int op;
|
||||
s_char tbuf[256];
|
||||
DWORD status;
|
||||
|
||||
optind = 1;
|
||||
opterr = 1;
|
||||
while ((op = getopt(argc, argv, "D:e:")) != EOF) {
|
||||
switch (op) {
|
||||
case 'D':
|
||||
datadir = optarg;
|
||||
break;
|
||||
case 'e':
|
||||
config_file = optarg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (config_file == NULL) {
|
||||
sprintf(tbuf, "%s/econfig", datadir);
|
||||
config_file = tbuf;
|
||||
}
|
||||
|
||||
{
|
||||
service_status.dwServiceType = SERVICE_WIN32;
|
||||
service_status.dwCurrentState = SERVICE_START_PENDING;
|
||||
service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
||||
|
@ -254,8 +219,7 @@ service_main(DWORD argc, LPTSTR *argv)
|
|||
return;
|
||||
}
|
||||
|
||||
/* Initialization code goes here. */
|
||||
start_server(0, config_file);
|
||||
start_server(0);
|
||||
|
||||
/* Initialization complete - report running status. */
|
||||
service_status.dwCurrentState = SERVICE_RUNNING;
|
||||
|
@ -263,18 +227,17 @@ service_main(DWORD argc, LPTSTR *argv)
|
|||
service_status.dwWaitHint = 0;
|
||||
|
||||
if (!SetServiceStatus (service_status_handle, &service_status)) {
|
||||
status = GetLastError();
|
||||
logerror("SetServiceStatus error %ld\n",status);
|
||||
logerror("SetServiceStatus error %ld\n", GetLastError());
|
||||
}
|
||||
|
||||
empth_exit();
|
||||
|
||||
/* We should never get here. But, just in case... */
|
||||
/* We should never get here. But, just in case... */
|
||||
close_files();
|
||||
|
||||
loc_NTTerm();
|
||||
|
||||
// This is where the service does its work.
|
||||
/* This is where the service does its work. */
|
||||
logerror("Returning the Main Thread \n",0);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -53,49 +53,49 @@
|
|||
#include "commodity.h"
|
||||
|
||||
struct empfile empfile[] = {
|
||||
{"sect", EMPDIR "/data/sector", EFF_XY | EFF_OWNER,
|
||||
{"sect", "sector", EFF_XY | EFF_OWNER,
|
||||
0, sizeof(struct sctstr), 0, 0, 0, offsetof(struct sctstr, sct_item),
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"ship", EMPDIR "/data/ship", EFF_XY | EFF_OWNER | EFF_GROUP,
|
||||
{"ship", "ship", EFF_XY | EFF_OWNER | EFF_GROUP,
|
||||
0, sizeof(struct shpstr), 0, 0, 0, offsetof(struct shpstr, shp_item),
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"plane", EMPDIR "/data/plane", EFF_XY | EFF_OWNER | EFF_GROUP,
|
||||
{"plane", "plane", EFF_XY | EFF_OWNER | EFF_GROUP,
|
||||
0, sizeof(struct plnstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"land", EMPDIR "/data/land", EFF_XY | EFF_OWNER | EFF_GROUP,
|
||||
{"land", "land", EFF_XY | EFF_OWNER | EFF_GROUP,
|
||||
0, sizeof(struct lndstr), 0, 0, 0, offsetof(struct lndstr, lnd_item),
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"nuke", EMPDIR "/data/nuke", EFF_XY | EFF_OWNER,
|
||||
{"nuke", "nuke", EFF_XY | EFF_OWNER,
|
||||
0, sizeof(struct nukstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"news", EMPDIR "/data/news", 0,
|
||||
{"news", "news", 0,
|
||||
0, sizeof(struct nwsstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"treaty", EMPDIR "/data/treaty", 0,
|
||||
{"treaty", "treaty", 0,
|
||||
0, sizeof(struct trtstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"trade", EMPDIR "/data/trade", 0,
|
||||
{"trade", "trade", 0,
|
||||
0, sizeof(struct trdstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"pow", EMPDIR "/data/power", 0,
|
||||
{"pow", "power", 0,
|
||||
0, sizeof(struct powstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"nat", EMPDIR "/data/nation", 0,
|
||||
{"nat", "nation", 0,
|
||||
0, sizeof(struct natstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"loan", EMPDIR "/data/loan", 0,
|
||||
{"loan", "loan", 0,
|
||||
0, sizeof(struct lonstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"map", EMPDIR "/data/map", 0,
|
||||
{"map", "map", 0,
|
||||
0, DEF_WORLD_X * DEF_WORLD_Y / 2, 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"bmap", EMPDIR "/data/bmap", 0,
|
||||
{"bmap", "bmap", 0,
|
||||
0, DEF_WORLD_X * DEF_WORLD_Y / 2, 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"commodity", EMPDIR "/data/commodity", 0,
|
||||
{"commodity", "commodity", 0,
|
||||
0, sizeof(struct comstr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0},
|
||||
{"lost", EMPDIR "/data/lostitems", 0, 0,
|
||||
{"lost", "lostitems", 0, 0,
|
||||
sizeof(struct loststr), 0, 0, 0, 0,
|
||||
-1, -1, 0, 0, 0, 0, 0}
|
||||
};
|
||||
|
|
|
@ -33,19 +33,21 @@
|
|||
*/
|
||||
|
||||
#include "gamesdef.h"
|
||||
#include "misc.h"
|
||||
|
||||
char dflt_econfig[] = EMPDIR "/data/econfig";
|
||||
|
||||
char *infodir = EMPDIR "/info.nr";
|
||||
char *datadir = EMPDIR "/data";
|
||||
char *teldir = EMPDIR "/data/tel";
|
||||
|
||||
char *motdfil = EMPDIR "/data/motd";
|
||||
char *downfil = EMPDIR "/data/down";
|
||||
char *disablefil = EMPDIR "/data/disable";
|
||||
char *telfil = EMPDIR "/data/tel/tel";
|
||||
char *annfil = EMPDIR "/data/ann";
|
||||
char *banfil = EMPDIR "/data/ban";
|
||||
char *authfil = EMPDIR "/data/auth";
|
||||
char *timestampfil = EMPDIR "/data/timestamp";
|
||||
/* relative to DATADIR */
|
||||
char *teldir = "tel";
|
||||
char *motdfil = "motd";
|
||||
char *downfil = "down";
|
||||
char *disablefil = "disable";
|
||||
char *telfil = "tel/tel";
|
||||
char *annfil = "ann";
|
||||
char *banfil = "ban";
|
||||
char *authfil = "auth";
|
||||
char *timestampfil = "timestamp";
|
||||
|
||||
char *loginport = EMP_PORT;
|
||||
|
|
|
@ -63,13 +63,61 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct emp_qelem Players;
|
||||
static struct emp_qelem Players;
|
||||
static int player_socket;
|
||||
|
||||
void
|
||||
player_init(void)
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
struct servent *sp;
|
||||
int s;
|
||||
short port;
|
||||
int val;
|
||||
|
||||
emp_initque(&Players);
|
||||
init_player_commands();
|
||||
|
||||
|
||||
sp = getservbyname("empire", "tcp");
|
||||
if (sp == 0)
|
||||
port = htons(atoi(loginport));
|
||||
else
|
||||
port = sp->s_port;
|
||||
sin.sin_addr.s_addr = INADDR_ANY;
|
||||
sin.sin_port = port;
|
||||
sin.sin_family = AF_INET;
|
||||
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
logerror("inet socket create");
|
||||
exit(1);
|
||||
}
|
||||
val = 1;
|
||||
#if !(defined(__linux__) && defined(__alpha__))
|
||||
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
|
||||
< 0) {
|
||||
logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
logerror("Alpha/Linux? You don't support SO_REUSEADDR yet, do you?\n");
|
||||
#endif
|
||||
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
||||
logerror("inet socket bind");
|
||||
exit(1);
|
||||
}
|
||||
#ifdef LISTENMAXCONN /* because someone in linux world didn't want to use
|
||||
* SOMAXCONN as defined in the header files... */
|
||||
if (listen(s, LISTENMAXCONN) < 0) {
|
||||
logerror("inet socket listen");
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
if (listen(s, SOMAXCONN) < 0) {
|
||||
logerror("inet socket listen");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
player_socket = s;
|
||||
}
|
||||
|
||||
struct player *
|
||||
|
@ -199,10 +247,7 @@ void
|
|||
player_accept(void *unused)
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
struct servent *sp;
|
||||
int s;
|
||||
short port;
|
||||
int val;
|
||||
int s = player_socket;
|
||||
struct player *np;
|
||||
int len;
|
||||
int ns;
|
||||
|
@ -210,45 +255,6 @@ player_accept(void *unused)
|
|||
int stacksize;
|
||||
char buf[128];
|
||||
|
||||
player_init();
|
||||
sp = getservbyname("empire", "tcp");
|
||||
if (sp == 0)
|
||||
port = htons(atoi(loginport));
|
||||
else
|
||||
port = sp->s_port;
|
||||
sin.sin_addr.s_addr = INADDR_ANY;
|
||||
sin.sin_port = port;
|
||||
sin.sin_family = AF_INET;
|
||||
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
logerror("inet socket create");
|
||||
exit(1);
|
||||
}
|
||||
val = 1;
|
||||
#if !(defined(__linux__) && defined(__alpha__))
|
||||
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
|
||||
< 0) {
|
||||
logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
logerror("Alpha/Linux? You don't support SO_REUSEADDR yet, do you?\n");
|
||||
#endif
|
||||
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
||||
logerror("inet socket bind");
|
||||
exit(1);
|
||||
}
|
||||
#ifdef LISTENMAXCONN /* because someone in linux world didn't want to use
|
||||
* SOMAXCONN as defined in the header files... */
|
||||
if (listen(s, LISTENMAXCONN) < 0) {
|
||||
logerror("inet socket listen");
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
if (listen(s, SOMAXCONN) < 0) {
|
||||
logerror("inet socket listen");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
while (1) {
|
||||
empth_select(s, EMPTH_FD_READ);
|
||||
len = sizeof(sin);
|
||||
|
|
|
@ -33,14 +33,11 @@
|
|||
* Doug Hay, 1998
|
||||
*/
|
||||
|
||||
#if defined(aix) || defined(linux)
|
||||
#include <unistd.h>
|
||||
#endif /* aix or linux */
|
||||
|
||||
#include <signal.h>
|
||||
#if !defined(_WIN32)
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
@ -51,6 +48,7 @@
|
|||
#include <process.h>
|
||||
#include "../lib/gen/getopt.h"
|
||||
#include "service.h"
|
||||
#include "direct.h"
|
||||
#endif
|
||||
|
||||
#include "misc.h"
|
||||
|
@ -77,12 +75,10 @@ static void loc_NTInit(void);
|
|||
|
||||
static int mainpid = 0;
|
||||
|
||||
/*
|
||||
* Debugging?
|
||||
* If yes, don't fork into background, don't catch certain signals,
|
||||
* call abort() on internal error.
|
||||
*/
|
||||
/* Debugging? If yes call abort() on internal error. */
|
||||
int debug = 0;
|
||||
/* Run as daemon? If yes, detach from controlling terminal etc. */
|
||||
int daemonize = 1;
|
||||
|
||||
static void
|
||||
print_usage(char *program_name)
|
||||
|
@ -108,11 +104,12 @@ main(int argc, char **argv)
|
|||
char *service_name = NULL;
|
||||
int remove_service_set = 0;
|
||||
int datadir_set = 0;
|
||||
#else
|
||||
char *config_file = NULL;
|
||||
#endif
|
||||
char *config_file = NULL;
|
||||
int op;
|
||||
#if defined(__linux__) && defined(_EMPTH_POSIX)
|
||||
s_char tbuf[256];
|
||||
#endif
|
||||
|
||||
mainpid = getpid();
|
||||
|
||||
|
@ -130,6 +127,7 @@ main(int argc, char **argv)
|
|||
break;
|
||||
case 'd':
|
||||
debug++;
|
||||
daemonize = 0;
|
||||
break;
|
||||
case 'e':
|
||||
config_file = optarg;
|
||||
|
@ -155,9 +153,11 @@ main(int argc, char **argv)
|
|||
#else
|
||||
case 'p':
|
||||
flags |= EMPTH_PRINT;
|
||||
daemonize = 0;
|
||||
break;
|
||||
case 's':
|
||||
flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
|
||||
daemonize = 0;
|
||||
break;
|
||||
#endif
|
||||
case 'h':
|
||||
|
@ -167,64 +167,68 @@ main(int argc, char **argv)
|
|||
}
|
||||
}
|
||||
|
||||
loginit("server");
|
||||
|
||||
#if defined(_WIN32)
|
||||
if ((debug || datadir_set || config_file != NULL) &&
|
||||
remove_service_set) {
|
||||
logerror("Can't use -d, -D or -e with either "
|
||||
"-r or -R options when starting the server");
|
||||
printf("Can't use -d, -D or -e with either -r "
|
||||
"or -R options\n");
|
||||
fprintf(stderr, "Can't use -d, -D or -e with either "
|
||||
"-r or -R options when starting the server\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (debug && install_service_set) {
|
||||
logerror("Can't use -d with either "
|
||||
"-i or -I options when starting the server");
|
||||
printf("Can't use -d with either -i "
|
||||
"or -I options\n");
|
||||
fprintf(stderr, "Can't use -d with either "
|
||||
"-i or -I options when starting the server\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (install_service_set && remove_service_set) {
|
||||
logerror("Can't use both -r or -R and -i or -I options when starting "
|
||||
"the server");
|
||||
printf("Can't use both -r or -R and -i or -I options\n");
|
||||
fprintf(stderr, "Can't use both -r or -R and -i or -I options when starting "
|
||||
"the server\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (install_service_set)
|
||||
return install_service(argv[0], service_name, datadir_set);
|
||||
#endif /* _WIN32 */
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (remove_service_set)
|
||||
return remove_service(service_name);
|
||||
#endif /* _WIN32 */
|
||||
|
||||
if (config_file == NULL) {
|
||||
sprintf(tbuf, "%s/econfig", datadir);
|
||||
config_file = tbuf;
|
||||
if (emp_config(config_file) < 0)
|
||||
exit(EXIT_FAILURE);
|
||||
if (chdir(datadir)) {
|
||||
fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
logerror("------------------------------------------------------");
|
||||
logerror("Empire server (pid %d) started", (int)getpid());
|
||||
#if defined(_WIN32)
|
||||
if (install_service_set)
|
||||
return install_service(argv[0], service_name, datadir_set, config_file);
|
||||
#endif /* _WIN32 */
|
||||
|
||||
init_server();
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (debug == 0) {
|
||||
if (daemonize != 0) {
|
||||
SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
|
||||
if (StartServiceCtrlDispatcher(DispatchTable))
|
||||
return 0;
|
||||
else
|
||||
else {
|
||||
/*
|
||||
* If it is service startup error then exit otherwise
|
||||
* start server in the foreground
|
||||
*/
|
||||
if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
|
||||
logerror("Failed to dispatch service (%d)", GetLastError());
|
||||
printf("Failed to dispatch service (%d)\n", GetLastError());
|
||||
exit(EXIT_FAILURE);
|
||||
} else /* start in the foreground */
|
||||
debug = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (debug == 0 && flags == 0) {
|
||||
daemonize = 0;
|
||||
#else /* !_WIN32 */
|
||||
if (daemonize)
|
||||
disassoc();
|
||||
}
|
||||
#endif
|
||||
|
||||
start_server(flags, config_file);
|
||||
#endif /* !_WIN32 */
|
||||
start_server(flags);
|
||||
|
||||
#if defined(__linux__) && defined(_EMPTH_POSIX)
|
||||
strcpy(tbuf, argv[0]);
|
||||
|
@ -248,20 +252,39 @@ main(int argc, char **argv)
|
|||
|
||||
|
||||
void
|
||||
start_server(int flags, char *config_file)
|
||||
init_server(void)
|
||||
{
|
||||
srand(time(NULL));
|
||||
#if defined(_WIN32)
|
||||
loc_NTInit();
|
||||
#endif
|
||||
update_policy_check();
|
||||
nullify_objects();
|
||||
global_init();
|
||||
shutdown_init();
|
||||
player_init();
|
||||
ef_init();
|
||||
init_files();
|
||||
io_init();
|
||||
init_nreport();
|
||||
|
||||
if (opt_MOB_ACCESS) {
|
||||
/* This fixes up mobility upon restart */
|
||||
mobility_init();
|
||||
}
|
||||
|
||||
loginit("server");
|
||||
logerror("------------------------------------------------------");
|
||||
logerror("Empire server (pid %d) started", (int)getpid());
|
||||
}
|
||||
|
||||
void
|
||||
start_server(int flags)
|
||||
{
|
||||
#ifdef POSIXSIGNALS
|
||||
struct sigaction act;
|
||||
#endif /* POSIXSIGNALS */
|
||||
|
||||
#if defined(_WIN32)
|
||||
loc_NTInit();
|
||||
#endif
|
||||
emp_config(config_file);
|
||||
update_policy_check();
|
||||
|
||||
nullify_objects();
|
||||
|
||||
#if !defined(_WIN32)
|
||||
/* signal() should not be used with mit pthreads. Anyway if u
|
||||
have a posix threads u definitly have posix signals -- Sasha */
|
||||
|
@ -302,20 +325,8 @@ start_server(int flags, char *config_file)
|
|||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif /* POSIXSIGNALS */
|
||||
#endif /* _WIN32 */
|
||||
empth_init((char **)&player, flags);
|
||||
srand(time(NULL));
|
||||
global_init();
|
||||
shutdown_init();
|
||||
player_init();
|
||||
ef_init();
|
||||
init_files();
|
||||
io_init();
|
||||
init_nreport();
|
||||
|
||||
if (opt_MOB_ACCESS) {
|
||||
/* This fixes up mobility upon restart */
|
||||
mobility_init();
|
||||
}
|
||||
empth_init((char **)&player, flags);
|
||||
|
||||
empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
|
||||
"AcceptPlayers", "Accept network connections", 0);
|
||||
|
|
|
@ -64,13 +64,11 @@ static int quiet = 0;
|
|||
/* lower URAN_MIN for more uranium */
|
||||
#define URAN_MIN 56
|
||||
|
||||
#if defined(aix) || defined(linux) || defined(solaris)
|
||||
#include <unistd.h>
|
||||
#endif /* aix or linux */
|
||||
#if defined(_WIN32)
|
||||
#include "../lib/gen/getopt.h"
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -191,7 +189,6 @@ main(int argc, char *argv[])
|
|||
{
|
||||
int opt;
|
||||
char *config_file = NULL;
|
||||
char tbuf[512];
|
||||
int i = 0;
|
||||
|
||||
rnd_seed = time(NULL);
|
||||
|
@ -222,11 +219,8 @@ main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
srandom(rnd_seed);
|
||||
if (config_file == NULL) {
|
||||
sprintf(tbuf, "%s/econfig", datadir);
|
||||
config_file = tbuf;
|
||||
}
|
||||
emp_config(config_file);
|
||||
if (emp_config(config_file))
|
||||
exit(1);
|
||||
|
||||
parse_args(argc - optind, argv + optind);
|
||||
if (allocate_memory() == -1)
|
||||
|
@ -406,12 +400,16 @@ static int
|
|||
allocate_memory(void)
|
||||
{
|
||||
int i;
|
||||
char *fname;
|
||||
|
||||
sect_fptr = fopen(empfile[EF_SECTOR].file, "wb");
|
||||
fname = malloc(strlen(datadir) + 1 + strlen(empfile[EF_SECTOR].file) + 1);
|
||||
sprintf(fname, "%s/%s", datadir, empfile[EF_SECTOR].file);
|
||||
sect_fptr = fopen(fname, "wb");
|
||||
if (sect_fptr == NULL) {
|
||||
perror(empfile[EF_SECTOR].file);
|
||||
perror(fname);
|
||||
return -1;
|
||||
}
|
||||
free(fname);
|
||||
sectsbuf =
|
||||
(struct sctstr *)calloc((YSIZE * XSIZE), sizeof(struct sctstr));
|
||||
sects = (struct sctstr **)calloc(YSIZE, sizeof(struct sctstr *));
|
||||
|
|
|
@ -33,10 +33,6 @@
|
|||
* Steve McClure, 1998
|
||||
*/
|
||||
|
||||
#if defined(aix) || defined(linux) || defined(solaris)
|
||||
#include <unistd.h>
|
||||
#endif /* aix or linux */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#if !defined(_WIN32)
|
||||
|
@ -46,6 +42,7 @@
|
|||
#include <direct.h>
|
||||
#include "../lib/gen/getopt.h"
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -77,7 +74,6 @@ int
|
|||
main(int argc, char *argv[])
|
||||
{
|
||||
s_char buf[255];
|
||||
s_char tbuf[512];
|
||||
s_char *filename;
|
||||
int x, y;
|
||||
struct natstr nat;
|
||||
|
@ -102,12 +98,9 @@ main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
/* Try to use the existing data directory */
|
||||
if (config_file == NULL) {
|
||||
sprintf(tbuf, "%s/econfig", datadir);
|
||||
config_file = tbuf;
|
||||
}
|
||||
emp_config(config_file);
|
||||
if (emp_config(config_file) < 0)
|
||||
exit(1);
|
||||
|
||||
empfile[EF_MAP].size = (WORLD_X * WORLD_Y) / 2;
|
||||
empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
|
||||
|
||||
|
@ -116,6 +109,11 @@ main(int argc, char *argv[])
|
|||
printf("Can't make game directory\n");
|
||||
exit(1);
|
||||
}
|
||||
if (chdir(datadir)) {
|
||||
fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!force) {
|
||||
printf("WARNING: this blasts the existing game in %s (if any)\n",
|
||||
datadir);
|
||||
|
|
|
@ -31,18 +31,16 @@
|
|||
* Julian Onions
|
||||
*/
|
||||
|
||||
#include <gamesdef.h>
|
||||
#include <stdio.h>
|
||||
#include "optlist.h"
|
||||
#include "prototypes.h"
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
if (argc > 1)
|
||||
emp_config(argv[1]);
|
||||
else
|
||||
emp_config(NULL);
|
||||
|
||||
if (emp_config(argv[1]) < 0)
|
||||
exit(1);
|
||||
print_config(stdout);
|
||||
exit(0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue