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 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"))) {
\r
92 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);
\r
95 exit(pre_edit_service(argc - 2, argv + 2));
\r
97 if (str_equiv(argv[1], _T("remove"))) {
\r
99 print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);
\r
102 exit(pre_remove_service(argc - 2, argv + 2));
\r
106 /* Thread local storage for error message buffer */
\r
107 tls_index = TlsAlloc();
\r
109 /* Register messages */
\r
110 if (is_admin) create_messages();
\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
122 if (_fileno(stdin) < 0) {
\r
123 /* Set up function pointers. */
\r
124 if (get_imports()) exit(111);
\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
137 else exit(usage(1));
\r
139 /* And nothing more to do */
\r