2 Copyright (C) 2000-2003 Kern Sibbald and John Walker
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 This file is patterned after the VNC Win32 code by ATT
21 Copyright (2000) Kern E. Sibbald
29 #include "winbacula.h"
31 #include "winservice.h"
34 #include "../../findlib/winapi.h"
36 extern int BaculaMain(int argc, char **argv);
37 extern int terminate_filed(int sig);
39 extern BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint);
42 HINSTANCE hAppInstance;
43 const char *szAppName = "Bacula";
46 /* Imported variables */
47 extern DWORD g_servicethread;
49 #define MAX_COMMAND_ARGS 100
50 static char *command_args[MAX_COMMAND_ARGS] = {"bacula-fd", NULL};
51 static int num_command_args = 1;
52 static pid_t main_pid;
53 static pthread_t main_tid;
56 * WinMain parses the command line and either calls the main App
57 * routine or, under NT, the main service routine.
59 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
60 PSTR CmdLine, int iCmdShow)
62 char *szCmdLine = CmdLine;
63 char *wordPtr, *tempPtr;
66 /* Save the application instance and main thread id */
67 hAppInstance = hInstance;
68 mainthreadId = GetCurrentThreadId();
71 main_tid = pthread_self();
74 * Funny things happen with the command line if the
75 * execution comes from c:/Program Files/bacula/bacula.exe
76 * We get a command line like: Files/bacula/bacula.exe" options
77 * I.e. someone stops scanning command line on a space, not
78 * realizing that the filename is quoted!!!!!!!!!!
79 * So if first character is not a double quote and
80 * the last character before first space is a double
81 * quote, we throw away the junk.
85 while (*wordPtr && *wordPtr != ' ')
87 if (wordPtr > szCmdLine) /* backup to char before space */
89 /* if first character is not a quote and last is, junk it */
90 if (*szCmdLine != '"' && *wordPtr == '"') {
91 szCmdLine = wordPtr + 1;
93 // MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
95 /* Build Unix style argc *argv[] */
97 /* Don't NULL command_args[0] !!! */
98 for (i=1;i<MAX_COMMAND_ARGS;i++)
99 command_args[i] = NULL;
103 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
105 if (*wordPtr == '\"') {
108 } else if (*wordPtr == '/') {
109 /* Skip Windows options */
110 while (*wordPtr && (*wordPtr != ' ' && *wordPtr != '\t'))
112 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
116 while (*wordPtr && num_command_args < MAX_COMMAND_ARGS) {
119 while (*tempPtr && *tempPtr != '\"')
123 while (*tempPtr && *tempPtr != ' ')
128 command_args[num_command_args++] = wordPtr;
130 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
132 if (*wordPtr == '\"') {
140 * Now process Windows command line options
143 * Make the command-line lowercase and parse it
145 for (i = 0; i < (int)strlen(szCmdLine); i++) {
146 szCmdLine[i] = tolower(szCmdLine[i]);
149 BOOL argfound = FALSE;
150 for (i = 0; i < (int)strlen(szCmdLine); i++) {
151 if (szCmdLine[i] <= ' ') {
155 if (szCmdLine[i] == '-') {
156 while (szCmdLine[i] && szCmdLine[i] != ' ') {
164 // Now check for command-line arguments
167 // Used on NT to connect to Bacula
168 if (strncmp(&szCmdLine[i], BaculaRunServiceHelper, strlen(BaculaRunServiceHelper)) == 0) {
169 // NB : This flag MUST be parsed BEFORE "-service", otherwise it will match
170 // the wrong option! (This code should really be replaced with a simple
171 // parser machine and parse-table...)
173 // Run the Bacula Service Helper app
174 bacService::PostUserHelperMessage();
178 if (strncmp(&szCmdLine[i], BaculaRunService, strlen(BaculaRunService)) == 0) {
179 // Run Bacula as a service
180 return bacService::BaculaServiceMain();
182 // /run (this is the default if no command line arguments)
183 if (strncmp(&szCmdLine[i], BaculaRunAsUserApp, strlen(BaculaRunAsUserApp)) == 0) {
184 // Bacula is being run as a user-level program
185 return BaculaAppMain();
188 if (strncmp(&szCmdLine[i], BaculaInstallService, strlen(BaculaInstallService)) == 0) {
189 // Install Bacula as a service
190 bacService::InstallService();
191 i+=strlen(BaculaInstallService);
195 if (strncmp(&szCmdLine[i], BaculaRemoveService, strlen(BaculaRemoveService)) == 0) {
196 // Remove the Bacula service
197 bacService::RemoveService();
198 i+=strlen(BaculaRemoveService);
203 if (strncmp(&szCmdLine[i], BaculaShowAbout, strlen(BaculaShowAbout)) == 0) {
204 // Show the About dialog of an existing instance of Bacula
205 bacService::ShowAboutBox();
206 i+=strlen(BaculaShowAbout);
211 if (strncmp(&szCmdLine[i], BaculaShowStatus, strlen(BaculaShowStatus)) == 0) {
212 // Show the Status dialog of an existing instance of Bacula
213 bacService::ShowStatus();
214 i+=strlen(BaculaShowStatus);
219 if (strncmp(&szCmdLine[i], BaculaShowEvents, strlen(BaculaShowEvents)) == 0) {
220 // Show the Events dialog of an existing instance of Bacula
221 bacService::ShowEvents();
222 i+=strlen(BaculaShowEvents);
228 if (strncmp(&szCmdLine[i], BaculaKillRunningCopy, strlen(BaculaKillRunningCopy)) == 0) {
229 // Kill any already running copy of Bacula
230 bacService::KillRunningCopy();
231 i+=strlen(BaculaKillRunningCopy);
236 if (strncmp(&szCmdLine[i], BaculaShowHelp, strlen(BaculaShowHelp)) == 0) {
237 MessageBox(NULL, BaculaUsageText, "Bacula Usage", MB_OK | MB_ICONINFORMATION);
238 i+=strlen(BaculaShowHelp);
242 MessageBox(NULL, szCmdLine, "Bad Command Line Options", MB_OK);
244 // Show the usage dialog
245 MessageBox(NULL, BaculaUsageText, "Bacula Usage", MB_OK | MB_ICONINFORMATION);
249 // If no arguments were given then just run
258 * Called as a thread from BaculaAppMain()
259 * Here we handle the Windows messages
261 //DWORD WINAPI Main_Msg_Loop(LPVOID lpwThreadParam)
262 void *Main_Msg_Loop(LPVOID lpwThreadParam)
264 DWORD old_servicethread = g_servicethread;
267 pthread_detach(pthread_self());
269 /* Since we are the only thread with a message loop
270 * mark ourselves as the service thread so that
271 * we can receive all the window events.
273 g_servicethread = GetCurrentThreadId();
275 // Create tray icon & menu if we're running as an app
276 bacMenu *menu = new bacMenu();
278 // log_error_message("Could not create sys tray menu");
283 // Now enter the Windows message handling loop until told to quit!
285 while (GetMessage(&msg, NULL, 0,0) ) {
286 TranslateMessage(&msg);
287 DispatchMessage(&msg);
294 if (old_servicethread != 0) { /* started as NT service */
295 // Mark that we're no longer running
298 // Tell the service manager that we've stopped.
299 ReportStatus(SERVICE_STOPPED, g_error, 0);
301 pthread_kill(main_tid, SIGTERM); /* ask main thread to terminate */
303 kill(main_pid, SIGTERM); /* ask main thread to terminate */
309 * This is the main routine for Bacula when running as an application
310 * (under Windows 95 or Windows NT)
311 * Under NT, Bacula can also run as a service. The BaculaServerMain routine,
312 * defined in the bacService header, is used instead when running as a service.
319 HINSTANCE hLib = LoadLibrary("KERNEL32.DLL");
321 p_GetFileAttributesEx = (t_GetFileAttributesEx)
322 GetProcAddress(hLib, "GetFileAttributesExA");
323 p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
324 GetProcAddress(hLib, "SetProcessShutdownParameters");
325 p_BackupRead = (t_BackupRead)
326 GetProcAddress(hLib, "BackupRead");
327 p_BackupWrite = (t_BackupWrite)
328 GetProcAddress(hLib, "BackupWrite");
331 hLib = LoadLibrary("ADVAPI32.DLL");
333 p_OpenProcessToken = (t_OpenProcessToken)
334 GetProcAddress(hLib, "OpenProcessToken");
335 p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
336 GetProcAddress(hLib, "AdjustTokenPrivileges");
337 p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
338 GetProcAddress(hLib, "LookupPrivilegeValueA");
342 * Even if these are defined, don't use on old
345 if (bacService::IsWin95()) {
347 p_BackupWrite = NULL;
350 // Set this process to be the last application to be shut down.
351 if (p_SetProcessShutdownParameters) {
352 p_SetProcessShutdownParameters(0x100, 0);
355 HWND hservwnd = FindWindow(MENU_CLASS_NAME, NULL);
356 if (hservwnd != NULL) {
357 // We don't allow multiple instances!
358 MessageBox(NULL, "Another instance of Bacula is already running", szAppName, MB_OK);
362 // Create a thread to handle the Windows messages
363 // (void)CreateThread(NULL, 0, Main_Msg_Loop, NULL, 0, &dwThreadID);
364 pthread_create(&tid, NULL, Main_Msg_Loop, (void *)0);
366 // Call the "real" Bacula
367 BaculaMain(num_command_args, command_args);