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
82 /* Valid commands are install, edit, get, set, reset, unset or remove */
\r
83 if (str_equiv(argv[1], _T("install"))) {
\r
85 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);
\r
88 exit(pre_install_service(argc - 2, argv + 2));
\r
90 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
92 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);
\r
95 int ret = pre_edit_service(argc - 1, argv + 1);
\r
96 /* There might be a password here. */
\r
97 for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR));
\r
100 if (str_equiv(argv[1], _T("remove"))) {
\r
102 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);
\r
105 exit(pre_remove_service(argc - 2, argv + 2));
\r
109 /* Thread local storage for error message buffer */
\r
110 tls_index = TlsAlloc();
\r
112 /* Register messages */
\r
113 if (is_admin) create_messages();
\r
116 Optimisation for Windows 2000:
\r
117 When we're run from the command line the StartServiceCtrlDispatcher() call
\r
118 will time out after a few seconds on Windows 2000. On newer versions the
\r
119 call returns instantly. Check for stdin first and only try to call the
\r
120 function if there's no input stream found. Although it's possible that
\r
121 we're running with input redirected it's much more likely that we're
\r
122 actually running as a service.
\r
123 This will save time when running with no arguments from a command prompt.
\r
125 if (_fileno(stdin) < 0) {
\r
126 /* Set up function pointers. */
\r
127 if (get_imports()) exit(111);
\r
129 /* Start service magic */
\r
130 SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
\r
131 if (! StartServiceCtrlDispatcher(table)) {
\r
132 unsigned long error = GetLastError();
\r
133 /* User probably ran nssm with no argument */
\r
134 if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));
\r
135 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);
\r
140 else exit(usage(1));
\r
142 /* And nothing more to do */
\r