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