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:
Markus Armbruster 2005-03-10 18:35:27 +00:00
commit 5b57f4c222
17 changed files with 201 additions and 267 deletions

View file

@ -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);

View file

@ -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"},
{&timestampfil, "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));
}

View file

@ -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;
}