(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);
/* pr.c */
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 *, ...)
ATTRIBUTE((format (printf, 3, 4)));
extern void pr_flash(struct player *, s_char *, ...)
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 *, ...)
ATTRIBUTE((format (printf, 2, 3)));
extern void pr_wall(s_char *, ...)
ATTRIBUTE((format (printf, 1, 2)));
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 prredir(s_char *redir);
extern void prexec(s_char *file);
extern void prprompt(int min, int btu);
extern void showvers(int vers);
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 prxy(s_char *format, coord x, coord y, natid country);
extern void PR(int, s_char *, ...) ATTRIBUTE((format (printf, 2, 3)));
extern void PRdate(natid cn);
extern void pr_beep(void);
extern void mpr(int, s_char *, ...) ATTRIBUTE((format (printf, 2, 3)));
extern void prtoascii(s_char *buf);
/* radmap.c */
extern int deltx(struct range *, coord);

View file

@ -34,7 +34,10 @@
#ifndef SERVICE_H
#define SERVICE_H
extern int install_service(char *program_name);
extern int remove_service(void);
#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 remove_service(char *service_name);
extern void WINAPI service_main(DWORD argc, LPTSTR *argv);
#endif

View file

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

View file

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