Jump to content

[Patch] Add support for custom world service name (win32)


Guest FragFrog

Recommended Posts

What features does the patch add? The ability to specify a custom name for the world service.

For which repository revision was the patch created? 9429, 0.12 branch (but I think applicable for master as well?).

Who has been writing this patch? I have, FragFrog

I prefer installing Mangos as a service whenever possible - that way it can be automatically restarted and does not even require a logged in user to run, unlike using restarter programs etcetera. A limitation so far has been that you can only install one world servce (at least as far as I have been able to figure out) since service names must be unique. This patch allows users to (optionally) specify a custom service to be used when installing mangosd as a service, so you can install several realms as a service on the same machine.

Usage: mangosd -s install -n customName or, for uninstalling, mangosd -s uninstall -n customName

As said, custom name is optional: without using the -n modifier installing mangos as service works exactly the same as without this patch.

Notes:

- There is a 255 character limit for service names (hence the char array size). I have not added a check for this, I very much doubt anyone will ever create a service name this long.

- Of course this is only applicable for running mangos on windows.

- I considered adding a '--help' option to show usage information, as the service commands are a bit hard to figure out. Decided against it to keep the patch minmal, but might be worth considering.

- I tried to stick to Mangos' coding style, but old habbits die hard, so there might be a few anomalies in there :D

- Order of parameters is variable, so -s install -n name works, but -n name -s install works as well.

From 8e6eff9878dfa727ed0a240a0a327473ba7b19ec Mon Sep 17 00:00:00 2001
From: FragFrog <[email protected]>
Date: Sat, 27 Feb 2010 13:21:08 +0100
Subject: [PATCH] Allow custom service name

---
src/mangosd/Main.cpp        |   30 +++++++++++++++++-------------
src/shared/ServiceWin32.cpp |   30 +++++++++++++++++++++++++++++-
src/shared/ServiceWin32.h   |    1 +
3 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/src/mangosd/Main.cpp b/src/mangosd/Main.cpp
index 218fc5b..5bd8ded 100644
--- a/src/mangosd/Main.cpp
+++ b/src/mangosd/Main.cpp
@@ -34,8 +34,8 @@

#ifdef WIN32
#include "ServiceWin32.h"
-char serviceName[] = "mangosd";
-char serviceLongName[] = "MaNGOS world service";
+char serviceName[235];
+char serviceLongName[255];
char serviceDescription[] = "Massive Network Game Object Server";
/*
 * -1 - not in service mode
@@ -63,6 +63,7 @@ void usage(const char *prog)
        "    --service                run as service\\n\\r"
        "    -s install               install service\\n\\r"
        "    -s uninstall             uninstall service\\n\\r"
+        "    -n name                  [Optional] specify service name\\n\\r"
        #endif
        ,prog);
}
@@ -77,12 +78,12 @@ extern int main(int argc, char **argv)

    ///- Command line parsing to get the configuration file name
    char const* cfg_file = _MANGOSD_CONFIG;
-    int c=1;
-    while( c < argc )
+    int c = 1;
+    while (c < argc)
    {
-        if( strcmp(argv[c],"-c") == 0)
+        if (strcmp(argv[c], "-c") == 0)
        {
-            if( ++c >= argc )
+            if (++c >= argc)
            {
                sLog.outError("Runtime-Error: -c option requires an input argument");
                usage(argv[0]);
@@ -92,7 +93,7 @@ extern int main(int argc, char **argv)
                cfg_file = argv[c];
        }

-        if( strcmp(argv[c],"--version") == 0)
+        if (strcmp(argv[c], "--version") == 0)
        {
            printf("%s\\n", _FULLVERSION(REVISION_DATE,REVISION_TIME,REVISION_NR,REVISION_ID));
            return 0;
@@ -102,23 +103,26 @@ extern int main(int argc, char **argv)
        ////////////
        //Services//
        ////////////
-        if( strcmp(argv[c],"-s") == 0)
+        if (strcmp(argv[c], "-s") == 0)
        {
-            if( ++c >= argc )
+            if (++c >= argc)
            {
                sLog.outError("Runtime-Error: -s option requires an input argument");
                usage(argv[0]);
                return 1;
            }
-            if( strcmp(argv[c],"install") == 0)
+
+            WinServiceName(argc, argv);
+
+            if (strcmp(argv[c], "install") == 0)
            {
                if (WinServiceInstall())
                    sLog.outString("Installing service");
                return 1;
            }
-            else if( strcmp(argv[c],"uninstall") == 0)
+            else if (strcmp(argv[c],"uninstall") == 0)
            {
-                if(WinServiceUninstall())
+                if (WinServiceUninstall())
                    sLog.outString("Uninstalling service");
                return 1;
            }
@@ -129,7 +133,7 @@ extern int main(int argc, char **argv)
                return 1;
            }
        }
-        if( strcmp(argv[c],"--service") == 0)
+        if (strcmp(argv[c], "--service") == 0)
        {
            WinServiceRun();
        }
diff --git a/src/shared/ServiceWin32.cpp b/src/shared/ServiceWin32.cpp
index 843b6b0..245ab69 100644
--- a/src/shared/ServiceWin32.cpp
+++ b/src/shared/ServiceWin32.cpp
@@ -242,6 +242,7 @@ void WINAPI ServiceMain(DWORD argc, char *argv[])
    }
}

+
bool WinServiceRun()
{
    SERVICE_TABLE_ENTRY serviceTable[] =
@@ -257,4 +258,31 @@ bool WinServiceRun()
    }
    return true;
}
-#endif
+
+// Look for (optional) non-default service name. To install multiple mangosd
+// services, a different name is required for each one.
+void WinServiceName (int argc, char **argv) 
+{
+    char defaultName[]        = "mangosd";
+    char defaultLongName[255] = "MaNGOS world service";
+
+    for (int c = 1; c < argc; c++) {
+        if (strcmp(argv[c], "-n") == 0)
+        {
+            if (++c > argc)
+                sLog.outError("Runtime-Error: -n option requires an input argument");
+            else {
+              strcpy(serviceName, argv[c]);
+              strcat(defaultLongName, " ");
+              strcat(defaultLongName, serviceName);
+              strcpy(serviceLongName, defaultLongName);
+              sLog.outString("Using service name %s (Long: %s)", serviceName, serviceLongName);
+              return;
+            }
+        }
+    }
+    strcpy(serviceName, defaultName);
+    strcpy(serviceLongName, defaultLongName);
+}
+
+#endif
\\ No newline at end of file
diff --git a/src/shared/ServiceWin32.h b/src/shared/ServiceWin32.h
index 77e8226..45c3eb6 100644
--- a/src/shared/ServiceWin32.h
+++ b/src/shared/ServiceWin32.h
@@ -23,6 +23,7 @@
bool WinServiceInstall();
bool WinServiceUninstall();
bool WinServiceRun();
+void WinServiceName (int argc, char **argv);

#endif                                                      // _WIN32_SERVICE_
#endif                                                      // WIN32
-- 
1.6.1.9.g97c34

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use