2 Copyright (C) 2000-2005 Kern Sibbald
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
30 #include "winbacula.h"
31 //#include "wintray.h"
32 #include "winservice.h"
34 extern int BaculaMain(int argc, char *argv[]);
35 extern void terminate_dird(int sig);
37 extern BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint);
38 extern void d_msg(const char *, int, int, const char *, ...);
41 HINSTANCE hAppInstance;
42 const char *szAppName = "Bacula";
45 /* Imported variables */
46 extern DWORD g_servicethread;
48 #define MAX_COMMAND_ARGS 100
49 static char *command_args[MAX_COMMAND_ARGS] = {"bacula-dird", NULL};
50 static int num_command_args = 1;
51 static pid_t main_pid;
52 static pthread_t main_tid;
55 * WinMain parses the command line and either calls the main App
56 * routine or, under NT, the main service routine.
58 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
59 PSTR CmdLine, int iCmdShow)
61 char *szCmdLine = CmdLine;
62 char *wordPtr, *tempPtr;
65 /* Save the application instance and main thread id */
66 hAppInstance = hInstance;
67 mainthreadId = GetCurrentThreadId();
70 main_tid = pthread_self();
73 * Funny things happen with the command line if the
74 * execution comes from c:/Program Files/bacula/bacula.exe
75 * We get a command line like: Files/bacula/bacula.exe" options
76 * I.e. someone stops scanning command line on a space, not
77 * realizing that the filename is quoted!!!!!!!!!!
78 * So if first character is not a double quote and
79 * the last character before first space is a double
80 * quote, we throw away the junk.
84 while (*wordPtr && *wordPtr != ' ')
86 if (wordPtr > szCmdLine) /* backup to char before space */
88 /* if first character is not a quote and last is, junk it */
89 if (*szCmdLine != '"' && *wordPtr == '"') {
90 szCmdLine = wordPtr + 1;
92 // MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
94 /* Build Unix style argc *argv[] */
96 /* Don't NULL command_args[0] !!! */
97 for (i=1;i<MAX_COMMAND_ARGS;i++)
98 command_args[i] = NULL;
102 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
104 if (*wordPtr == '\"') {
107 } else if (*wordPtr == '/') {
108 /* Skip Windows options */
109 while (*wordPtr && (*wordPtr != ' ' && *wordPtr != '\t'))
111 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
115 while (*wordPtr && num_command_args < MAX_COMMAND_ARGS) {
118 while (*tempPtr && *tempPtr != '\"')
122 while (*tempPtr && *tempPtr != ' ')
127 command_args[num_command_args++] = wordPtr;
129 while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
131 if (*wordPtr == '\"') {
139 * Now process Windows command line options
142 * Make the command-line lowercase and parse it
144 for (i = 0; i < (int)strlen(szCmdLine); i++) {
145 szCmdLine[i] = tolower(szCmdLine[i]);
148 bool argfound = false;
149 for (i = 0; i < (int)strlen(szCmdLine); i++) {
150 if (szCmdLine[i] <= ' ') {
154 if (szCmdLine[i] == '-') {
155 while (szCmdLine[i] && szCmdLine[i] != ' ') {
163 /* Now check for command-line arguments */
165 /* /service helper - probably only needed on win9x */
166 if (strncmp(&szCmdLine[i], BaculaRunServiceHelper, strlen(BaculaRunServiceHelper)) == 0
167 && g_platform_id == VER_PLATFORM_WIN32_NT) {
168 /* exit with result "okay" */
172 /* /service start service */
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 Bacula's about box */
200 bacService::ShowAboutBox();
201 i += strlen(BaculaShowAbout);
206 if (strncmp(&szCmdLine[i], BaculaShowStatus, strlen(BaculaShowStatus)) == 0) {
207 /* Show Bacula's status box */
208 bacService::ShowStatus();
209 i += strlen(BaculaShowStatus);
214 if (strncmp(&szCmdLine[i], BaculaKillRunningCopy, strlen(BaculaKillRunningCopy)) == 0) {
215 /* Kill running copy of Bacula */
216 bacService::KillRunningCopy();
217 i += strlen(BaculaKillRunningCopy);
222 if (strncmp(&szCmdLine[i], BaculaShowHelp, strlen(BaculaShowHelp)) == 0) {
223 MessageBox(NULL, BaculaUsageText, _("Bacula Usage"), MB_OK|MB_ICONINFORMATION);
224 i += strlen(BaculaShowHelp);
228 MessageBox(NULL, szCmdLine, _("Bad Command Line Options"), MB_OK);
230 /* Show the usage dialog */
231 MessageBox(NULL, BaculaUsageText, _("Bacula Usage"), MB_OK | MB_ICONINFORMATION);
235 /* If no arguments were given then just run */
244 * Called as a thread from BaculaAppMain()
245 * Here we handle the Windows messages
247 //DWORD WINAPI Main_Msg_Loop(LPVOID lpwThreadParam)
248 void *Main_Msg_Loop(LPVOID lpwThreadParam)
250 DWORD old_servicethread = g_servicethread;
253 pthread_detach(pthread_self());
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();
262 /* Create tray icon & menu if we're running as an app */
263 bacMenu *menu = new bacMenu();
265 // log_error_message("Could not create sys tray menu");
270 /* Now enter the Windows message handling loop until told to quit! */
272 while (GetMessage(&msg, NULL, 0,0) ) {
273 TranslateMessage(&msg);
274 DispatchMessage(&msg);
283 if (old_servicethread != 0) { /* started as NT service */
284 /* Mark that we're no longer running */
287 /* Tell the service manager that we've stopped. */
288 ReportStatus(SERVICE_STOPPED, g_error, 0);
290 /* Tell main program to go away */
293 /* Should not get here */
294 pthread_kill(main_tid, SIGTERM); /* ask main thread to terminate */
296 kill(main_pid, SIGTERM); /* ask main thread to terminate */
302 * This is the main routine for Bacula when running as an application
303 * (under Windows 95 or Windows NT)
304 * Under NT, Bacula can also run as a service. The BaculaServerMain routine,
305 * defined in the bacService header, is used instead when running as a service.
309 /* DWORD dwThreadID; */
316 /* Set this process to be the last application to be shut down. */
317 if (p_SetProcessShutdownParameters) {
318 p_SetProcessShutdownParameters(0x100, 0);
322 HWND hservwnd = FindWindow(MENU_CLASS_NAME, NULL);
323 if (hservwnd != NULL) {
324 /* We don't allow multiple instances! */
325 MessageBox(NULL, _("Another instance of Bacula is already running"), szAppName, MB_OK);
330 /* Create a thread to handle the Windows messages */
331 // (void)CreateThread(NULL, 0, Main_Msg_Loop, NULL, 0, &dwThreadID);
332 pthread_create(&tid, NULL, Main_Msg_Loop, (void *)0);
334 /* Call the "real" Bacula */
335 BaculaMain(num_command_args, command_args);