(main,install_service,remove_service,service_main) [_WIN32]:

Add the ability to specify the service name, to install
multiple services and to set -D and -e options during install.
This commit is contained in:
Ron Koenderink 2004-12-30 02:19:44 +00:00
parent cc1a92e512
commit 70fcb70707
4 changed files with 100 additions and 47 deletions

View file

@ -421,28 +421,33 @@ extern int pln_identchance(struct plnstr *, int, int);
extern void pln_set_tech(struct plnstr *, int); extern void pln_set_tech(struct plnstr *, int);
/* pr.c */ /* pr.c */
extern void pr(s_char *, ...) ATTRIBUTE((format (printf, 1, 2))); extern void pr(s_char *, ...) ATTRIBUTE((format (printf, 1, 2)));
extern void prnf(s_char *buf); extern void UTF8prnf(s_char *buf);
extern void pr_id(struct player *, int, s_char *, ...) extern void pr_id(struct player *, int, s_char *, ...)
ATTRIBUTE((format (printf, 3, 4))); ATTRIBUTE((format (printf, 3, 4)));
extern void pr_flash(struct player *, s_char *, ...) extern void pr_flash(struct player *, s_char *, ...)
ATTRIBUTE((format (printf, 2, 3))); ATTRIBUTE((format (printf, 2, 3)));
extern void UTF8pr_flash(struct player *, s_char *, ...)
ATTRIBUTE((format (printf, 2, 3)));
extern void pr_inform(struct player *, s_char *, ...) extern void pr_inform(struct player *, s_char *, ...)
ATTRIBUTE((format (printf, 2, 3))); ATTRIBUTE((format (printf, 2, 3)));
extern void pr_wall(s_char *, ...) extern void pr_wall(s_char *, ...)
ATTRIBUTE((format (printf, 1, 2))); ATTRIBUTE((format (printf, 1, 2)));
extern void pr_player(struct player *pl, int id, s_char *buf); extern void pr_player(struct player *pl, int id, s_char *buf);
extern void UTF8pr_player(struct player *pl, int id, s_char *buf);
extern void pr_hilite(s_char *buf); extern void pr_hilite(s_char *buf);
extern void prredir(s_char *redir); extern void prredir(s_char *redir);
extern void prexec(s_char *file); extern void prexec(s_char *file);
extern void prprompt(int min, int btu); extern void prprompt(int min, int btu);
extern void showvers(int vers); extern void showvers(int vers);
extern int prmptrd(s_char *prompt, s_char *str, int size); extern int prmptrd(s_char *prompt, s_char *str, int size);
extern int UTF8prmptrd(s_char *prompt, s_char *str, int size);
extern void prdate(void); extern void prdate(void);
extern void prxy(s_char *format, coord x, coord y, natid country); extern void prxy(s_char *format, coord x, coord y, natid country);
extern void PR(int, s_char *, ...) ATTRIBUTE((format (printf, 2, 3))); extern void PR(int, s_char *, ...) ATTRIBUTE((format (printf, 2, 3)));
extern void PRdate(natid cn); extern void PRdate(natid cn);
extern void pr_beep(void); extern void pr_beep(void);
extern void mpr(int, s_char *, ...) ATTRIBUTE((format (printf, 2, 3))); extern void mpr(int, s_char *, ...) ATTRIBUTE((format (printf, 2, 3)));
extern void prtoascii(s_char *buf);
/* radmap.c */ /* radmap.c */
extern int deltx(struct range *, coord); extern int deltx(struct range *, coord);

View file

@ -34,7 +34,10 @@
#ifndef SERVICE_H #ifndef SERVICE_H
#define SERVICE_H #define SERVICE_H
extern int install_service(char *program_name); #define DEFAULT_SERVICE_NAME "Empire Server"
extern int remove_service(void); extern char *config_file;
extern int install_service(char *program_name, char *service_name, int datadir_set);
extern int remove_service(char *service_name);
extern void WINAPI service_main(DWORD argc, LPTSTR *argv); extern void WINAPI service_main(DWORD argc, LPTSTR *argv);
#endif #endif

View file

@ -41,10 +41,11 @@
#include "../gen/getopt.h" #include "../gen/getopt.h"
#include "optlist.h" #include "optlist.h"
#define SERVICE_NAME "Empire Server"
char *config_file = NULL;
int int
install_service(char *program_name) install_service(char *program_name, char *service_name, int datadir_set)
{ {
char strDir[1024]; char strDir[1024];
HANDLE schSCManager,schService; HANDLE schSCManager,schService;
@ -52,12 +53,26 @@ install_service(char *program_name)
SERVICE_DESCRIPTION sdBuf; SERVICE_DESCRIPTION sdBuf;
if (strrchr(program_name,'\\') == NULL) { if (strrchr(program_name,'\\') == NULL) {
GetCurrentDirectory(1024,strDir); GetCurrentDirectory(sizeof(strDir), strDir);
strcat(strDir, "\\"); strcat(strDir, "\\");
strcat(strDir, program_name); strcat(strDir, program_name);
} else } else
strcpy(strDir, program_name); strcpy(strDir, program_name);
if (datadir_set) {
strcat(strDir, " -D ");
strcat(strDir, datadir);
}
if (config_file != NULL) {
strcat(strDir, " -e ");
strcat(strDir, config_file);
}
if (service_name == NULL)
service_name = DEFAULT_SERVICE_NAME;
else if (service_name[0] == '\0')
service_name = DEFAULT_SERVICE_NAME;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) { if (schSCManager == NULL) {
@ -69,8 +84,8 @@ install_service(char *program_name)
lpszBinaryPathName = strDir; lpszBinaryPathName = strDir;
schService = CreateService(schSCManager, schService = CreateService(schSCManager,
SERVICE_NAME, service_name,
SERVICE_NAME, /* service name to display */ service_name, /* service name to display */
SERVICE_ALL_ACCESS, /* desired access */ SERVICE_ALL_ACCESS, /* desired access */
SERVICE_WIN32_OWN_PROCESS, /* service type */ SERVICE_WIN32_OWN_PROCESS, /* service type */
SERVICE_AUTO_START, /* start type */ SERVICE_AUTO_START, /* start type */
@ -83,8 +98,8 @@ install_service(char *program_name)
NULL); /* no password */ NULL); /* no password */
if (schService == NULL) { if (schService == NULL) {
logerror("install_service failed to create service"); logerror("install_service failed to create service %s", service_name);
printf("Install service: failed to create service.\n"); printf("Install service: failed to create service %s.\n", service_name);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
sdBuf.lpDescription = "Server for Empire game"; sdBuf.lpDescription = "Server for Empire game";
@ -97,19 +112,24 @@ install_service(char *program_name)
printf("Install service: failed to set the description.\n"); printf("Install service: failed to set the description.\n");
} }
logerror("install_service successfully created the service"); logerror("install_service successfully created the service %s", service_name);
printf("Service installed.\n"); printf("Service %s installed.\n", service_name);
CloseServiceHandle(schService); CloseServiceHandle(schService);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int int
remove_service(void) remove_service(char *service_name)
{ {
HANDLE schSCManager; HANDLE schSCManager;
SC_HANDLE hService; SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (service_name == NULL)
service_name = DEFAULT_SERVICE_NAME;
else if (service_name[0] == '\0')
service_name = DEFAULT_SERVICE_NAME;
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) { if (schSCManager == NULL) {
logerror("remove_service failed to open Service Control Manager"); logerror("remove_service failed to open Service Control Manager");
@ -117,27 +137,27 @@ remove_service(void)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
hService = OpenService(schSCManager,SERVICE_NAME,SERVICE_ALL_ACCESS); hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
if (hService == NULL) { if (hService == NULL) {
logerror("remove_service failed to open service"); logerror("remove_service failed to open service %s", service_name);
printf("Remove service: failed to open service.\n"); printf("Remove service: failed to open service %s.\n", service_name);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (DeleteService(hService) == 0) { if (DeleteService(hService) == 0) {
logerror("remove_service failed to remove service"); logerror("remove_service failed to remove service %s", service_name);
printf("Remove service: failed to remove service.\n"); printf("Remove service: failed to remove service %s.\n", service_name);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (CloseServiceHandle(hService) == 0) { if (CloseServiceHandle(hService) == 0) {
logerror("remove_service failed to close service"); logerror("remove_service failed to close service %s", service_name);
printf("Remove service: failed to close service.\n"); printf("Remove service: failed to close service %s.\n", service_name);
return EXIT_FAILURE; return EXIT_FAILURE;
} else { } else {
logerror("remove_service successfully removed service"); logerror("remove_service successfully removed service %s", service_name);
printf("Service removed.\n"); printf("Service %s removed.\n", service_name);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
} }
@ -198,19 +218,21 @@ service_ctrl_handler(DWORD Opcode)
void WINAPI void WINAPI
service_main(DWORD argc, LPTSTR *argv) service_main(DWORD argc, LPTSTR *argv)
{ {
char *config_file = NULL;
int op; int op;
s_char tbuf[256]; s_char tbuf[256];
DWORD status; DWORD status;
// DebugBreak();
optind = 1;
opterr = 1;
while ((op = getopt(argc, argv, "D:e:")) != EOF) { while ((op = getopt(argc, argv, "D:e:")) != EOF) {
switch (op) { switch (op) {
case 'D': case 'D':
datadir = optarg; datadir = optarg;
break; break;
case 'e': case 'e':
config_file = optarg; config_file = optarg;
break; break;
} }
} }
@ -228,7 +250,7 @@ service_main(DWORD argc, LPTSTR *argv)
service_status.dwWaitHint = 0; service_status.dwWaitHint = 0;
service_status_handle = RegisterServiceCtrlHandler( service_status_handle = RegisterServiceCtrlHandler(
SERVICE_NAME, service_ctrl_handler); DEFAULT_SERVICE_NAME, service_ctrl_handler);
if (service_status_handle == (SERVICE_STATUS_HANDLE)0) { if (service_status_handle == (SERVICE_STATUS_HANDLE)0) {
logerror("RegisterServiceCtrlHandler failed %d\n", GetLastError()); logerror("RegisterServiceCtrlHandler failed %d\n", GetLastError());

View file

@ -88,9 +88,9 @@ static void
print_usage(char *program_name) print_usage(char *program_name)
{ {
#if defined(_WIN32) #if defined(_WIN32)
printf("Usage: %s -i -r -D datadir -e config_file -d\n", program_name); printf("Usage: %s -i -I service_name -r -R service_name -D datadir -e config_file -d\n", program_name);
printf("-i install service\n"); printf("-i install service with the default name %s\n", DEFAULT_SERVICE_NAME);
printf("-r remove service\n"); printf("-r remove service with the default name %s\n", DEFAULT_SERVICE_NAME);
#else #else
printf("Usage: %s -D datadir -e config_file -d -p -s\n", program_name); printf("Usage: %s -D datadir -e config_file -d -p -s\n", program_name);
printf("-p print flag\n"); printf("-p print flag\n");
@ -105,11 +105,13 @@ main(int argc, char **argv)
int flags = 0; int flags = 0;
#if defined(_WIN32) #if defined(_WIN32)
int install_service_set = 0; int install_service_set = 0;
char *service_name = NULL;
int remove_service_set = 0; int remove_service_set = 0;
int datadir_set = 0; int datadir_set = 0;
#else
char *config_file = NULL;
#endif #endif
int op; int op;
char *config_file = NULL;
s_char tbuf[256]; s_char tbuf[256];
#if defined(_WIN32) #if defined(_WIN32)
@ -121,7 +123,7 @@ main(int argc, char **argv)
mainpid = getpid(); mainpid = getpid();
#if defined(_WIN32) #if defined(_WIN32)
while ((op = getopt(argc, argv, "D:de:irh")) != EOF) { while ((op = getopt(argc, argv, "D:de:iI:rR:h")) != EOF) {
#else #else
while ((op = getopt(argc, argv, "D:de:psh")) != EOF) { while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
#endif #endif
@ -139,9 +141,20 @@ main(int argc, char **argv)
config_file = optarg; config_file = optarg;
break; break;
#if defined(_WIN32) #if defined(_WIN32)
case 'I':
service_name = optarg;
/*
* fall out
*/
case 'i': case 'i':
install_service_set++; install_service_set++;
break; break;
break;
case 'R':
service_name = optarg;
/*
* fall out
*/
case 'r': case 'r':
remove_service_set++; remove_service_set++;
break; break;
@ -162,23 +175,30 @@ main(int argc, char **argv)
#if defined(_WIN32) #if defined(_WIN32)
if ((debug || datadir_set || config_file != NULL) && if ((debug || datadir_set || config_file != NULL) &&
(install_service_set || remove_service_set)) { remove_service_set) {
logerror("Can't use -d or -D or -e with either " logerror("Can't use -d, -D or -e with either "
"-r or -i options when starting the server"); "-r or -R options when starting the server");
printf("Can't use -d or -D or -e with either -r " printf("Can't use -d, -D or -e with either -r "
"or -i options\n"); "or -R options\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");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (install_service_set && remove_service_set) { if (install_service_set && remove_service_set) {
logerror("Can't use both -r and -i options when starting " logerror("Can't use both -r or -R and -i or -I options when starting "
"the server"); "the server");
printf("Can't use both -r and -i options\n"); printf("Can't use both -r or -R and -i or -I options\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (install_service_set) if (install_service_set)
return install_service(argv[0]); return install_service(argv[0], service_name, datadir_set);
if (remove_service_set) if (remove_service_set)
return remove_service(); return remove_service(service_name);
#endif /* _WIN32 */ #endif /* _WIN32 */
if (config_file == NULL) { if (config_file == NULL) {
@ -395,7 +415,7 @@ panic(int sig)
void void
shutdwn(int sig) shutdwn(int sig)
{ {
struct player *p; struct player *p,*phold;
time_t now; time_t now;
#if defined(__linux__) && defined(_EMPTH_POSIX) #if defined(__linux__) && defined(_EMPTH_POSIX)
@ -423,7 +443,8 @@ shutdwn(int sig)
logerror("Shutdown commencing (cleaning up threads.)"); logerror("Shutdown commencing (cleaning up threads.)");
for (p = player_next(0); p != 0; p = player_next(p)) { p = player_next(0);
while (p != 0) {
if (p->state != PS_PLAYING) if (p->state != PS_PLAYING)
continue; continue;
pr_flash(p, "Server shutting down...\n"); pr_flash(p, "Server shutting down...\n");
@ -432,7 +453,9 @@ shutdwn(int sig)
if (p->command) { if (p->command) {
pr_flash(p, "Shutdown aborting command\n"); pr_flash(p, "Shutdown aborting command\n");
} }
empth_wakeup(p->proc); phold = p;
p = player_next(p);
empth_wakeup(phold->proc);
} }
if (!sig) { if (!sig) {