3 extern unsigned long tls_index;
\r
4 extern bool is_admin;
\r
5 extern imports_t imports;
\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
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
20 *number = _tcstoul(string, &bogus, 0);
\r
21 if (*bogus) return 2;
\r
26 /* Remove basename of a path. */
\r
27 void strip_basename(TCHAR *buffer) {
\r
28 size_t len = _tcslen(buffer);
\r
30 for (i = len; i && buffer[i] != _T('\\') && buffer[i] != _T('/'); i--);
\r
32 if (i && buffer[i - 1] == _T(':')) i++;
\r
33 buffer[i] = _T('\0');
\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
43 void check_admin() {
\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
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
61 if (! GetWindowThreadProcessId(console, &pid)) return;
\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
68 if (GetCurrentProcessId() != pid) return;
\r
70 /* We close our new console so that subsequent messages appear in a popup. */
\r
74 int _tmain(int argc, TCHAR **argv) {
\r
77 /* Remember if we are admin */
\r
84 start, stop, pause, continue, install, edit, get, set, reset, unset, remove
\r
86 if (str_equiv(argv[1], _T("start"))) exit(control_service(0, argc - 2, argv + 2));
\r
87 if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));
\r
88 if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));
\r
89 if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));
\r
90 if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));
\r
91 if (str_equiv(argv[1], _T("install"))) {
\r
93 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);
\r
96 exit(pre_install_service(argc - 2, argv + 2));
\r
98 if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) {
\r
100 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);
\r
103 int ret = pre_edit_service(argc - 1, argv + 1);
\r
104 /* There might be a password here. */
\r
105 for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR));
\r
108 if (str_equiv(argv[1], _T("remove"))) {
\r
110 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);
\r
113 exit(pre_remove_service(argc - 2, argv + 2));
\r
117 /* Thread local storage for error message buffer */
\r
118 tls_index = TlsAlloc();
\r
120 /* Register messages */
\r
121 if (is_admin) create_messages();
\r
124 Optimisation for Windows 2000:
\r
125 When we're run from the command line the StartServiceCtrlDispatcher() call
\r
126 will time out after a few seconds on Windows 2000. On newer versions the
\r
127 call returns instantly. Check for stdin first and only try to call the
\r
128 function if there's no input stream found. Although it's possible that
\r
129 we're running with input redirected it's much more likely that we're
\r
130 actually running as a service.
\r
131 This will save time when running with no arguments from a command prompt.
\r
133 if (_fileno(stdin) < 0) {
\r
134 /* Set up function pointers. */
\r
135 if (get_imports()) exit(111);
\r
137 /* Start service magic */
\r
138 SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
\r
139 if (! StartServiceCtrlDispatcher(table)) {
\r
140 unsigned long error = GetLastError();
\r
141 /* User probably ran nssm with no argument */
\r
142 if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));
\r
143 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);
\r
148 else exit(usage(1));
\r
150 /* And nothing more to do */
\r