Added str_number().
[nssm.git] / nssm.cpp
1 #include "nssm.h"\r
2 \r
3 extern unsigned long tls_index;\r
4 extern bool is_admin;\r
5 extern imports_t imports;\r
6 \r
7 /* Are two strings case-insensitively equivalent? */\r
8 int str_equiv(const TCHAR *a, const TCHAR *b) {\r
9   size_t len = _tcslen(a);\r
10   if (_tcslen(b) != len) return 0;\r
11   if (_tcsnicmp(a, b, len)) return 0;\r
12   return 1;\r
13 }\r
14 \r
15 /* Convert a string to a number. */\r
16 int str_number(const TCHAR *string, unsigned long *number) {\r
17   if (! string) return 1;\r
18 \r
19   TCHAR *bogus;\r
20   *number = _tcstoul(string, &bogus, 0);\r
21   if (*bogus) return 2;\r
22 \r
23   return 0;\r
24 }\r
25 \r
26 /* Remove basename of a path. */\r
27 void strip_basename(TCHAR *buffer) {\r
28   size_t len = _tcslen(buffer);\r
29   size_t i;\r
30   for (i = len; i && buffer[i] != _T('\\') && buffer[i] != _T('/'); i--);\r
31   /* X:\ is OK. */\r
32   if (i && buffer[i - 1] == _T(':')) i++;\r
33   buffer[i] = _T('\0');\r
34 }\r
35 \r
36 /* How to use me correctly */\r
37 int usage(int ret) {\r
38   if (GetConsoleWindow()) print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
39   else popup_message(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
40   return(ret);\r
41 }\r
42 \r
43 void check_admin() {\r
44   is_admin = false;\r
45 \r
46   /* Lifted from MSDN examples */\r
47   PSID AdministratorsGroup;\r
48   SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;\r
49   if (! AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup)) return;\r
50   CheckTokenMembership(0, AdministratorsGroup, /*XXX*/(PBOOL) &is_admin);\r
51   FreeSid(AdministratorsGroup);\r
52 }\r
53 \r
54 /* See if we were launched from a console window. */\r
55 static void check_console() {\r
56   /* If we're running in a service context there will be no console window. */\r
57   HWND console = GetConsoleWindow();\r
58   if (! console) return;\r
59 \r
60   unsigned long pid;\r
61   if (! GetWindowThreadProcessId(console, &pid)) return;\r
62 \r
63   /*\r
64     If the process associated with the console window handle is the same as\r
65     this process, we were not launched from an existing console.  The user\r
66     probably double-clicked our executable.\r
67   */\r
68   if (GetCurrentProcessId() != pid) return;\r
69 \r
70   /* We close our new console so that subsequent messages appear in a popup. */\r
71   FreeConsole();\r
72 }\r
73 \r
74 int _tmain(int argc, TCHAR **argv) {\r
75   check_console();\r
76 \r
77   /* Remember if we are admin */\r
78   check_admin();\r
79 \r
80   /* Elevate */\r
81   if (argc > 1) {\r
82     /* Valid commands are install, edit or remove */\r
83     if (str_equiv(argv[1], _T("install"))) {\r
84       if (! is_admin) {\r
85         print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);\r
86         exit(100);\r
87       }\r
88       exit(pre_install_service(argc - 2, argv + 2));\r
89     }\r
90     if (str_equiv(argv[1], _T("edit"))) {\r
91       if (! is_admin) {\r
92         print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);\r
93         exit(100);\r
94       }\r
95       exit(pre_edit_service(argc - 2, argv + 2));\r
96     }\r
97     if (str_equiv(argv[1], _T("remove"))) {\r
98       if (! is_admin) {\r
99         print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);\r
100         exit(100);\r
101       }\r
102       exit(pre_remove_service(argc - 2, argv + 2));\r
103     }\r
104   }\r
105 \r
106   /* Thread local storage for error message buffer */\r
107   tls_index = TlsAlloc();\r
108 \r
109   /* Register messages */\r
110   if (is_admin) create_messages();\r
111 \r
112   /*\r
113     Optimisation for Windows 2000:\r
114     When we're run from the command line the StartServiceCtrlDispatcher() call\r
115     will time out after a few seconds on Windows 2000.  On newer versions the\r
116     call returns instantly.  Check for stdin first and only try to call the\r
117     function if there's no input stream found.  Although it's possible that\r
118     we're running with input redirected it's much more likely that we're\r
119     actually running as a service.\r
120     This will save time when running with no arguments from a command prompt.\r
121   */\r
122   if (_fileno(stdin) < 0) {\r
123     /* Set up function pointers. */\r
124     if (get_imports()) exit(111);\r
125 \r
126     /* Start service magic */\r
127     SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
128     if (! StartServiceCtrlDispatcher(table)) {\r
129       unsigned long error = GetLastError();\r
130       /* User probably ran nssm with no argument */\r
131       if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));\r
132       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);\r
133       free_imports();\r
134       exit(100);\r
135     }\r
136   }\r
137   else exit(usage(1));\r
138 \r
139   /* And nothing more to do */\r
140   exit(0);\r
141 }\r