2 Copyright (C) 2000, 2001, 2002 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"
32 extern int BaculaMain(int argc, char **argv);
33 extern int terminate_filed(int sig);
35 extern BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint);
38 HINSTANCE hAppInstance;
39 const char *szAppName = "Bacula";
42 /* Imported variables */
43 extern DWORD g_servicethread;
45 #define MAX_COMMAND_ARGS 100
46 static char *command_args[MAX_COMMAND_ARGS] = {"bacula-fd", NULL};
47 static int num_command_args = 1;
48 static pid_t main_pid;
51 * WinMain parses the command line and either calls the main App
52 * routine or, under NT, the main service routine.
54 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
55 PSTR CmdLine, int iCmdShow)
57 char *szCmdLine = CmdLine;
58 char *wordPtr,*tempPtr;
61 /* Save the application instance and main thread id */
62 hAppInstance = hInstance;
63 mainthreadId = GetCurrentThreadId();
68 * Funny things happen with the command line if the
69 * execution comes from c:/Program Files/bacula/bacula.exe
70 * We get a command line like: Files/bacula/bacula.exe" options
71 * I.e. someone stops scanning command line on a space, not
72 * realizing that the filename is quoted!!!!!!!!!!
73 * So if first character is not a double quote and
74 * the last character before first space is a double
75 * quote, we throw away the junk.
78 while (*wordPtr && *wordPtr != ' ')
80 if (wordPtr > szCmdLine) /* backup to char before space */
82 /* if first character is not a quote and last is, junk it */
83 if (*szCmdLine != '"' && *wordPtr == '"')
84 szCmdLine = wordPtr + 1;
85 // MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
87 /* Build Unix style argc *argv[] */
89 /* Don't NULL command_args[0] !!! */
90 for (i=1;i<MAX_COMMAND_ARGS;i++)
91 command_args[i] = NULL;
95 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
97 if (*wordPtr == '\"') {
100 } else if (*wordPtr == '/') {
101 /* Skip Windows options */
102 while (*wordPtr && (*wordPtr != ' ' && *wordPtr != '\t'))
104 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
108 while (*wordPtr && num_command_args < MAX_COMMAND_ARGS) {
111 while (*tempPtr && *tempPtr != '\"')
115 while (*tempPtr && *tempPtr != ' ')
120 command_args[num_command_args++] = wordPtr;
122 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
124 if (*wordPtr == '\"') {
132 * Now process Windows command line options
135 * Make the command-line lowercase and parse it
137 for (i = 0; i < (int)strlen(szCmdLine); i++) {
138 szCmdLine[i] = tolower(szCmdLine[i]);
141 BOOL argfound = FALSE;
142 for (i = 0; i < (int)strlen(szCmdLine); i++) {
143 if (szCmdLine[i] <= ' ')
146 if (szCmdLine[i] == '-') {
147 while (szCmdLine[i] && szCmdLine[i] != ' ')
154 // Now check for command-line arguments
157 // Used on NT to connect to Bacula
158 if (strncmp(&szCmdLine[i], BaculaRunServiceHelper, strlen(BaculaRunServiceHelper)) == 0) {
159 // NB : This flag MUST be parsed BEFORE "-service", otherwise it will match
160 // the wrong option! (This code should really be replaced with a simple
161 // parser machine and parse-table...)
163 // Run the Bacula Service Helper app
164 bacService::PostUserHelperMessage();
168 if (strncmp(&szCmdLine[i], BaculaRunService, strlen(BaculaRunService)) == 0) {
169 // Run Bacula as a service
170 return bacService::BaculaServiceMain();
172 // /run (this is the default if no command line arguments)
173 if (strncmp(&szCmdLine[i], BaculaRunAsUserApp, strlen(BaculaRunAsUserApp)) == 0) {
174 // Bacula is being run as a user-level program
175 return BaculaAppMain();
178 if (strncmp(&szCmdLine[i], BaculaInstallService, strlen(BaculaInstallService)) == 0) {
179 // Install Bacula as a service
180 bacService::InstallService();
181 i+=strlen(BaculaInstallService);
185 if (strncmp(&szCmdLine[i], BaculaRemoveService, strlen(BaculaRemoveService)) == 0) {
186 // Remove the Bacula service
187 bacService::RemoveService();
188 i+=strlen(BaculaRemoveService);
193 if (strncmp(&szCmdLine[i], BaculaShowAbout, strlen(BaculaShowAbout)) == 0) {
194 // Show the About dialog of an existing instance of Bacula
195 bacService::ShowAboutBox();
196 i+=strlen(BaculaShowAbout);
201 if (strncmp(&szCmdLine[i], BaculaShowStatus, strlen(BaculaShowStatus)) == 0) {
202 // Show the Status dialog of an existing instance of Bacula
203 bacService::ShowStatus();
204 i+=strlen(BaculaShowStatus);
209 if (strncmp(&szCmdLine[i], BaculaShowEvents, strlen(BaculaShowEvents)) == 0) {
210 // Show the Events dialog of an existing instance of Bacula
211 bacService::ShowEvents();
212 i+=strlen(BaculaShowEvents);
218 if (strncmp(&szCmdLine[i], BaculaKillRunningCopy, strlen(BaculaKillRunningCopy)) == 0) {
219 // Kill any already running copy of Bacula
220 bacService::KillRunningCopy();
221 i+=strlen(BaculaKillRunningCopy);
226 if (strncmp(&szCmdLine[i], BaculaShowHelp, strlen(BaculaShowHelp)) == 0) {
227 MessageBox(NULL, BaculaUsageText, "Bacula Usage", MB_OK | MB_ICONINFORMATION);
228 i+=strlen(BaculaShowHelp);
232 MessageBox(NULL, szCmdLine, "Bad Command Line Options", MB_OK);
234 // Show the usage dialog
235 MessageBox(NULL, BaculaUsageText, "Bacula Usage", MB_OK | MB_ICONINFORMATION);
239 // If no arguments were given then just run
248 * Called as a thread from BaculaAppMain()
249 * Here we handle the Windows messages
251 DWORD WINAPI Main_Msg_Loop(LPVOID lpwThreadParam)
253 DWORD old_servicethread = g_servicethread;
255 /* Since we are the only thread with a message loop
256 * mark ourselves as the service thread so that
257 * we can receive all the window events.
259 g_servicethread = GetCurrentThreadId();
261 // Create tray icon & menu if we're running as an app
262 bacMenu *menu = new bacMenu();
268 // Now enter the Windows message handling loop until told to quit!
270 while (GetMessage(&msg, NULL, 0,0) ) {
271 TranslateMessage(&msg);
272 DispatchMessage(&msg);
278 if (old_servicethread != 0) { /* started as NT service */
279 // Mark that we're no longer running
282 // Tell the service manager that we've stopped.
283 ReportStatus(SERVICE_STOPPED, g_error, 0);
285 kill(main_pid, SIGTERM); /* ask main thread to terminate */
291 * This is the main routine for Bacula when running as an application
292 * (under Windows 95 or Windows NT)
293 * Under NT, Bacula can also run as a service. The BaculaServerMain routine,
294 * defined in the bacService header, is used instead when running as a service.
300 // Set this process to be the last application to be shut down.
301 SetProcessShutdownParameters(0x100, 0);
303 HWND hservwnd = FindWindow(MENU_CLASS_NAME, NULL);
304 if (hservwnd != NULL) {
305 // We don't allow multiple instances!
306 MessageBox(NULL, "Another instance of Bacula is already running", szAppName, MB_OK);
310 // Create a thread to handle the Windows messages
311 (void)CreateThread(NULL, 0, Main_Msg_Loop, NULL, 0, &dwThreadID);
313 // Call the "real" Bacula
314 BaculaMain(num_command_args, command_args);