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
27 #include "winbacula.h"
29 #include "winservice.h"
33 extern int BaculaMain(int argc, char **argv);
34 extern int terminate_filed(int sig);
36 extern BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint);
39 HINSTANCE hAppInstance;
40 const char *szAppName = "Bacula";
43 /* Imported variables */
44 extern DWORD g_servicethread;
46 #define MAX_COMMAND_ARGS 100
47 static char *command_args[MAX_COMMAND_ARGS] = {"bacula-fd", NULL};
48 static int num_command_args = 1;
49 static pid_t main_pid;
50 static pthread_t main_tid;
53 * WinMain parses the command line and either calls the main App
54 * routine or, under NT, the main service routine.
56 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
57 PSTR CmdLine, int iCmdShow)
59 char *szCmdLine = CmdLine;
60 char *wordPtr,*tempPtr;
63 /* Save the application instance and main thread id */
64 hAppInstance = hInstance;
65 mainthreadId = GetCurrentThreadId();
68 main_tid = pthread_self();
71 * Funny things happen with the command line if the
72 * execution comes from c:/Program Files/bacula/bacula.exe
73 * We get a command line like: Files/bacula/bacula.exe" options
74 * I.e. someone stops scanning command line on a space, not
75 * realizing that the filename is quoted!!!!!!!!!!
76 * So if first character is not a double quote and
77 * the last character before first space is a double
78 * quote, we throw away the junk.
81 while (*wordPtr && *wordPtr != ' ')
83 if (wordPtr > szCmdLine) /* backup to char before space */
85 /* if first character is not a quote and last is, junk it */
86 if (*szCmdLine != '"' && *wordPtr == '"')
87 szCmdLine = wordPtr + 1;
88 // MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
90 /* Build Unix style argc *argv[] */
92 /* Don't NULL command_args[0] !!! */
93 for (i=1;i<MAX_COMMAND_ARGS;i++)
94 command_args[i] = NULL;
98 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
100 if (*wordPtr == '\"') {
103 } else if (*wordPtr == '/') {
104 /* Skip Windows options */
105 while (*wordPtr && (*wordPtr != ' ' && *wordPtr != '\t'))
107 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
111 while (*wordPtr && num_command_args < MAX_COMMAND_ARGS) {
114 while (*tempPtr && *tempPtr != '\"')
118 while (*tempPtr && *tempPtr != ' ')
123 command_args[num_command_args++] = wordPtr;
125 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
127 if (*wordPtr == '\"') {
135 * Now process Windows command line options
138 * Make the command-line lowercase and parse it
140 for (i = 0; i < (int)strlen(szCmdLine); i++) {
141 szCmdLine[i] = tolower(szCmdLine[i]);
144 BOOL argfound = FALSE;
145 for (i = 0; i < (int)strlen(szCmdLine); i++) {
146 if (szCmdLine[i] <= ' ') {
150 if (szCmdLine[i] == '-') {
151 while (szCmdLine[i] && szCmdLine[i] != ' ') {
159 // Now check for command-line arguments
162 // Used on NT to connect to Bacula
163 if (strncmp(&szCmdLine[i], BaculaRunServiceHelper, strlen(BaculaRunServiceHelper)) == 0) {
164 // NB : This flag MUST be parsed BEFORE "-service", otherwise it will match
165 // the wrong option! (This code should really be replaced with a simple
166 // parser machine and parse-table...)
168 // Run the Bacula Service Helper app
169 bacService::PostUserHelperMessage();
173 if (strncmp(&szCmdLine[i], BaculaRunService, strlen(BaculaRunService)) == 0) {
174 // Run Bacula as a service
175 return bacService::BaculaServiceMain();
177 // /run (this is the default if no command line arguments)
178 if (strncmp(&szCmdLine[i], BaculaRunAsUserApp, strlen(BaculaRunAsUserApp)) == 0) {
179 // Bacula is being run as a user-level program
180 return BaculaAppMain();
183 if (strncmp(&szCmdLine[i], BaculaInstallService, strlen(BaculaInstallService)) == 0) {
184 // Install Bacula as a service
185 bacService::InstallService();
186 i+=strlen(BaculaInstallService);
190 if (strncmp(&szCmdLine[i], BaculaRemoveService, strlen(BaculaRemoveService)) == 0) {
191 // Remove the Bacula service
192 bacService::RemoveService();
193 i+=strlen(BaculaRemoveService);
198 if (strncmp(&szCmdLine[i], BaculaShowAbout, strlen(BaculaShowAbout)) == 0) {
199 // Show the About dialog of an existing instance of Bacula
200 bacService::ShowAboutBox();
201 i+=strlen(BaculaShowAbout);
206 if (strncmp(&szCmdLine[i], BaculaShowStatus, strlen(BaculaShowStatus)) == 0) {
207 // Show the Status dialog of an existing instance of Bacula
208 bacService::ShowStatus();
209 i+=strlen(BaculaShowStatus);
214 if (strncmp(&szCmdLine[i], BaculaShowEvents, strlen(BaculaShowEvents)) == 0) {
215 // Show the Events dialog of an existing instance of Bacula
216 bacService::ShowEvents();
217 i+=strlen(BaculaShowEvents);
223 if (strncmp(&szCmdLine[i], BaculaKillRunningCopy, strlen(BaculaKillRunningCopy)) == 0) {
224 // Kill any already running copy of Bacula
225 bacService::KillRunningCopy();
226 i+=strlen(BaculaKillRunningCopy);
231 if (strncmp(&szCmdLine[i], BaculaShowHelp, strlen(BaculaShowHelp)) == 0) {
232 MessageBox(NULL, BaculaUsageText, "Bacula Usage", MB_OK | MB_ICONINFORMATION);
233 i+=strlen(BaculaShowHelp);
237 MessageBox(NULL, szCmdLine, "Bad Command Line Options", MB_OK);
239 // Show the usage dialog
240 MessageBox(NULL, BaculaUsageText, "Bacula Usage", MB_OK | MB_ICONINFORMATION);
244 // If no arguments were given then just run
253 * Called as a thread from BaculaAppMain()
254 * Here we handle the Windows messages
256 //DWORD WINAPI Main_Msg_Loop(LPVOID lpwThreadParam)
257 void *Main_Msg_Loop(LPVOID lpwThreadParam)
259 DWORD old_servicethread = g_servicethread;
262 pthread_detach(pthread_self());
264 /* Since we are the only thread with a message loop
265 * mark ourselves as the service thread so that
266 * we can receive all the window events.
268 g_servicethread = GetCurrentThreadId();
270 // Create tray icon & menu if we're running as an app
271 bacMenu *menu = new bacMenu();
273 // LogErrorMsg("Could not create sys tray menu");
278 // Now enter the Windows message handling loop until told to quit!
280 while (GetMessage(&msg, NULL, 0,0) ) {
281 TranslateMessage(&msg);
282 DispatchMessage(&msg);
289 if (old_servicethread != 0) { /* started as NT service */
290 // Mark that we're no longer running
293 // Tell the service manager that we've stopped.
294 ReportStatus(SERVICE_STOPPED, g_error, 0);
296 pthread_kill(main_tid, SIGTERM); /* ask main thread to terminate */
298 kill(main_pid, SIGTERM); /* ask main thread to terminate */
304 * This is the main routine for Bacula when running as an application
305 * (under Windows 95 or Windows NT)
306 * Under NT, Bacula can also run as a service. The BaculaServerMain routine,
307 * defined in the bacService header, is used instead when running as a service.
314 // Set this process to be the last application to be shut down.
315 SetProcessShutdownParameters(0x100, 0);
317 HWND hservwnd = FindWindow(MENU_CLASS_NAME, NULL);
318 if (hservwnd != NULL) {
319 // We don't allow multiple instances!
320 MessageBox(NULL, "Another instance of Bacula is already running", szAppName, MB_OK);
324 // Create a thread to handle the Windows messages
325 // (void)CreateThread(NULL, 0, Main_Msg_Loop, NULL, 0, &dwThreadID);
326 pthread_create(&tid, NULL, Main_Msg_Loop, (void *)0);
328 // Call the "real" Bacula
329 BaculaMain(num_command_args, command_args);