-
Notifications
You must be signed in to change notification settings - Fork 256
/
main.cc
349 lines (290 loc) · 9.71 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2007-2011 Free Software Foundation Europe e.V.
Copyright (C) 2016-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, August 2007
*
* Note, some of the original Bareos Windows startup and service handling code
* was derived from VNC code that was used in apcupsd then ported to
* Bareos. However, since then the code has been significantly enhanced
* and largely rewritten.
*
* Evidently due to the nature of Windows startup code and service
* handling code, certain similarities remain. Thanks to the original
* VNC authors.
*
* This is a generic main routine, which is used by all three
* of the daemons. Each one compiles it with slightly different
* #defines.
*/
#include "include/bareos.h"
#include "win32.h"
#include "who.h"
#include <signal.h>
#include <pthread.h>
#undef _WIN32_IE
#ifdef MINGW64
# define _WIN32_IE 0x0501
#else
# define _WIN32_IE 0x0401
#endif // MINGW64
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#include <commctrl.h>
/* Globals */
HINSTANCE appInstance;
DWORD mainthreadId;
bool opt_debug = false;
bool have_service_api;
DWORD service_thread_id = 0;
#define MAX_COMMAND_ARGS 100
static char* command_args[MAX_COMMAND_ARGS] = {(char*)LC_APP_NAME, NULL};
static int num_command_args = 1;
static pid_t main_pid;
static pthread_t main_tid;
const char usage[] = APP_NAME
"[/debug] [/service] [/run] [/kill] [/install] [/remove] [/help]\n";
/*
*
* Main Windows entry point.
*
* We parse the command line and either calls the main App
* or starts up the service.
*/
int WINAPI WinMain(HINSTANCE Instance,
HINSTANCE /*PrevInstance*/,
PSTR CmdLine,
int /*show*/)
{
char* cmdLine = CmdLine;
char *wordPtr, *tempPtr;
int i, quote;
OSVERSIONINFO osversioninfo;
osversioninfo.dwOSVersionInfoSize = sizeof(osversioninfo);
/* Save the application instance and main thread id */
appInstance = Instance;
mainthreadId = GetCurrentThreadId();
if (GetVersionEx(&osversioninfo)
&& osversioninfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
have_service_api = true;
}
main_pid = getpid();
main_tid = pthread_self();
INITCOMMONCONTROLSEX initCC
= {sizeof(INITCOMMONCONTROLSEX), ICC_STANDARD_CLASSES};
InitCommonControlsEx(&initCC);
/*
* Funny things happen with the command line if the
* execution comes from c:/Program Files/bareos/bareos.exe
* We get a command line like: Files/bareos/bareos.exe" options
* I.e. someone stops scanning command line on a space, not
* realizing that the filename is quoted!!!!!!!!!!
* So if first character is not a double quote and
* the last character before first space is a double
* quote, we throw away the junk.
*/
wordPtr = cmdLine;
while (*wordPtr && *wordPtr != ' ') wordPtr++;
if (wordPtr > cmdLine) /* backup to char before space */
wordPtr--;
/* if first character is not a quote and last is, junk it */
if (*cmdLine != '"' && *wordPtr == '"') { cmdLine = wordPtr + 1; }
/*
* Build Unix style argc *argv[] for the main "Unix" code
* stripping out any Windows options
*/
/* Don't NULL command_args[0] !!! */
for (i = 1; i < MAX_COMMAND_ARGS; i++) { command_args[i] = NULL; }
char* pszArgs = strdup(cmdLine);
wordPtr = pszArgs;
quote = 0;
while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t')) wordPtr++;
if (*wordPtr == '\"') {
quote = 1;
wordPtr++;
} else if (*wordPtr == '/') {
/* Skip Windows options */
while (*wordPtr && (*wordPtr != ' ' && *wordPtr != '\t')) wordPtr++;
while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t')) wordPtr++;
}
if (*wordPtr) {
while (*wordPtr && num_command_args < MAX_COMMAND_ARGS) {
tempPtr = wordPtr;
if (quote) {
while (*tempPtr && *tempPtr != '\"') tempPtr++;
quote = 0;
} else {
while (*tempPtr && *tempPtr != ' ') tempPtr++;
}
if (*tempPtr) *(tempPtr++) = '\0';
command_args[num_command_args++] = wordPtr;
wordPtr = tempPtr;
while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t')) wordPtr++;
if (*wordPtr == '\"') {
quote = 1;
wordPtr++;
}
}
}
/*
* Now process Windows command line options. Most of these options
* are single shot -- i.e. we accept one option, do something and
* Terminate.
*/
for (i = 0; i < (int)strlen(cmdLine); i++) {
char* p = &cmdLine[i];
if (*p <= ' ') { continue; /* toss junk */ }
if (*p != '/') { break; /* syntax error */ }
/* Start as a service? */
if (strncasecmp(p, "/service", 8) == 0) {
return bareosServiceMain(); /* yes, run as a service */
}
/* Stop any running copy? */
if (strncasecmp(p, "/kill", 5) == 0) { return stopRunningBareos(); }
/* Run app as user program? */
if (strncasecmp(p, "/run", 4) == 0) {
return BareosAppMain(); /* yes, run as a user program */
}
/* Install Bareos in registry? */
if (strncasecmp(p, "/install", 8) == 0) {
return installService(p + 8); /* Pass command options */
}
/* Remove Bareos registry entry? */
if (strncasecmp(p, "/remove", 7) == 0) { return removeService(); }
/* Set debug mode? -- causes more dialogs to be displayed */
if (strncasecmp(p, "/debug", 6) == 0) {
opt_debug = true;
i += 6; /* skip /debug */
continue;
}
/* Display help? -- displays usage */
if (strncasecmp(p, "/help", 5) == 0) {
MessageBox(NULL, usage, APP_DESC, MB_OK | MB_ICONINFORMATION);
return 0;
}
MessageBox(NULL, cmdLine, _("Bad Command Line Option"), MB_OK);
/* Show the usage dialog */
MessageBox(NULL, usage, APP_DESC, MB_OK | MB_ICONINFORMATION);
return 1;
}
return BareosAppMain();
}
// Minimalist winproc when don't have tray monitor
LRESULT CALLBACK bacWinProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
/*
* Called as a thread from BareosAppMain()
* Here we handle the Windows messages
*/
void* Main_Msg_Loop([[maybe_unused]] LPVOID lpwThreadParam)
{
MSG msg;
pthread_detach(pthread_self());
/*
* Since we are the only thread with a message loop
* mark ourselves as the service thread so that
* we can receive all the window events.
*/
service_thread_id = GetCurrentThreadId();
/* Create a window to handle Windows messages */
WNDCLASSEX baclass;
baclass.cbSize = sizeof(baclass);
baclass.style = 0;
baclass.lpfnWndProc = bacWinProc;
baclass.cbClsExtra = 0;
baclass.cbWndExtra = 0;
baclass.hInstance = appInstance;
baclass.hIcon = NULL;
baclass.hCursor = NULL;
baclass.hbrBackground = NULL;
baclass.lpszMenuName = NULL;
baclass.lpszClassName = APP_NAME;
baclass.hIconSm = NULL;
RegisterClassEx(&baclass);
if (CreateWindow(APP_NAME, APP_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, 0, 0, NULL, NULL, appInstance, NULL)
== NULL) {
PostQuitMessage(0);
}
/* Now enter the Windows message handling loop until told to quit! */
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/* If we get here, we are shutting down */
if (have_service_api) {
/* Mark that we're no longer running */
service_thread_id = 0;
/* Tell the service manager that we've stopped. */
ReportStatus(SERVICE_STOPPED, service_error, 0);
}
/* Tell main "Unix" program to go away */
TerminateApp(0);
/* Should not get here */
pthread_kill(main_tid, SIGTERM); /* ask main thread to Terminate */
sleep(1);
kill(main_pid, SIGTERM); /* kill main thread */
_exit(0);
}
/*
* This is the main routine for Bareos when running as an application,
* or after the service has started up.
*/
int BareosAppMain()
{
pthread_t tid;
DWORD dwCharsWritten;
OSDependentInit();
/* If no arguments were given then just run */
if (p_AttachConsole == NULL || !p_AttachConsole(ATTACH_PARENT_PROCESS)) {
if (opt_debug) { AllocConsole(); }
}
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &dwCharsWritten,
NULL);
/* Startup networking */
WSA_Init();
/* Set this process to be the last application to be shut down. */
if (p_SetProcessShutdownParameters) {
p_SetProcessShutdownParameters(0x100, 0);
}
/* Create a thread to handle the Windows messages */
pthread_create(&tid, NULL, Main_Msg_Loop, (void*)0);
/* Call the Unix Bareos daemon */
BareosMain(num_command_args, command_args);
PostQuitMessage(0); /* Terminate our main message loop */
WSACleanup();
_exit(0);
}
void PauseMsg(const char* file, const char* func, int line, const char* msg)
{
char buf[1000];
if (msg) {
Bsnprintf(buf, sizeof(buf), "%s:%s:%d %s", file, func, line, msg);
} else {
Bsnprintf(buf, sizeof(buf), "%s:%s:%d", file, func, line);
}
MessageBox(NULL, buf, "Pause", MB_OK);
}
typedef void(WINAPI* PGNSI)(LPSYSTEM_INFO);
typedef BOOL(WINAPI* PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);