#include "nssm.h"\r
\r
+extern unsigned long tls_index;\r
+extern bool is_admin;\r
+\r
/* String function */\r
int str_equiv(const char *a, const char *b) {\r
int i;\r
\r
/* How to use me correctly */\r
int usage(int ret) {\r
- fprintf(stderr, "NSSM: The non-sucking service manager\n");\r
- fprintf(stderr, "Version %s, %s\n", NSSM_VERSION, NSSM_DATE);\r
- fprintf(stderr, "Usage: nssm <option> [args]\n\n");\r
- fprintf(stderr, "To show service installation GUI:\n\n");\r
- fprintf(stderr, " nssm install [<servicename>]\n\n");\r
- fprintf(stderr, "To install a service without confirmation:\n\n");\r
- fprintf(stderr, " nssm install <servicename> <app> [<args>]\n\n");\r
- fprintf(stderr, "To show service removal GUI:\n\n");\r
- fprintf(stderr, " nssm remove [<servicename>]\n\n");\r
- fprintf(stderr, "To remove a service without confirmation:\n\n");\r
- fprintf(stderr, " nssm remove <servicename> confirm\n");\r
+ print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
return(ret);\r
}\r
\r
+void check_admin() {\r
+ is_admin = false;\r
+\r
+ /* Lifted from MSDN examples */\r
+ PSID AdministratorsGroup;\r
+ SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;\r
+ if (! AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup)) return;\r
+ CheckTokenMembership(0, AdministratorsGroup, /*XXX*/(PBOOL) &is_admin);\r
+ FreeSid(AdministratorsGroup);\r
+}\r
+\r
int main(int argc, char **argv) {\r
- /* Require an argument since users may try to run nssm directly */\r
- if (argc == 1) exit(usage(1));\r
+ /* Remember if we are admin */\r
+ check_admin();\r
\r
- /* Valid commands are install or remove */\r
- if (str_equiv(argv[1], "install")) {\r
- exit(pre_install_service(argc - 2, argv + 2));\r
- }\r
- if (str_equiv(argv[1], "remove")) {\r
- exit(pre_remove_service(argc - 2, argv + 2));\r
+ /* Elevate */\r
+ if (argc > 1) {\r
+ /* Valid commands are install or remove */\r
+ if (str_equiv(argv[1], "install")) {\r
+ if (! is_admin) {\r
+ print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);\r
+ exit(100);\r
+ }\r
+ exit(pre_install_service(argc - 2, argv + 2));\r
+ }\r
+ if (str_equiv(argv[1], "remove")) {\r
+ if (! is_admin) {\r
+ print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);\r
+ exit(100);\r
+ }\r
+ exit(pre_remove_service(argc - 2, argv + 2));\r
+ }\r
}\r
- /* Undocumented: "run" is used to actually do service stuff */\r
- if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2));\r
+\r
+ /* Thread local storage for error message buffer */\r
+ tls_index = TlsAlloc();\r
\r
/* Register messages */\r
- create_messages();\r
+ if (is_admin) create_messages();\r
\r
- /* Start service magic */\r
- SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
- if (! StartServiceCtrlDispatcher(table)) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(GetLastError()), 0);\r
- return 100;\r
+ /*\r
+ Optimisation for Windows 2000:\r
+ When we're run from the command line the StartServiceCtrlDispatcher() call\r
+ will time out after a few seconds on Windows 2000. On newer versions the\r
+ call returns instantly. Check for stdin first and only try to call the\r
+ function if there's no input stream found. Although it's possible that\r
+ we're running with input redirected it's much more likely that we're\r
+ actually running as a service.\r
+ This will save time when running with no arguments from a command prompt.\r
+ */\r
+ if (_fileno(stdin) < 0) {\r
+ /* Start service magic */\r
+ SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
+ if (! StartServiceCtrlDispatcher(table)) {\r
+ unsigned long error = GetLastError();\r
+ /* User probably ran nssm with no argument */\r
+ if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);\r
+ exit(100);\r
+ }\r
}\r
+ else exit(usage(1));\r
\r
/* And nothing more to do */\r
- return 0;\r
+ exit(0);\r
}\r