]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/filed/winmain.cpp
Update .cvsignore files
[bacula/bacula] / bacula / src / win32 / filed / winmain.cpp
1 /*
2    Copyright (C) 2000-2006 Kern Sibbald
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License
6    version 2 as amended with additional clauses defined in the
7    file LICENSE in the main source directory.
8
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 
12    the file LICENSE for additional details.
13
14    This file is patterned after the VNC Win32 code by ATT
15   
16 */
17
18 #include <unistd.h>
19 #include <ctype.h>
20
21 #include "bacula.h"
22 #include "winbacula.h"
23 #include "wintray.h"
24 #include "winservice.h"
25 #include <signal.h>
26 #include <pthread.h>
27
28 extern int BaculaMain(int argc, char *argv[]);
29 extern void terminate_filed(int sig);
30 extern DWORD g_error;
31 extern BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint);
32 extern void d_msg(const char *, int, int, const char *, ...);
33 extern void VSSInit();
34
35 /* Globals */
36 HINSTANCE       hAppInstance;
37 const char      *szAppName = "Bacula";
38 DWORD           mainthreadId;
39 bool            silent = false;
40
41 /* Imported variables */
42 extern DWORD    g_servicethread;
43 extern DWORD    g_platform_id;
44
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;
49 static pthread_t main_tid;
50
51 /*
52  * WinMain parses the command line and either calls the main App
53  * routine or, under NT, the main service routine.
54  */
55 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
56                    PSTR CmdLine, int iCmdShow)
57 {
58    char *szCmdLine = CmdLine;
59    char *wordPtr, *tempPtr;
60    int i, quote;
61
62    /* Save the application instance and main thread id */
63    hAppInstance = hInstance;
64    mainthreadId = GetCurrentThreadId();
65
66    main_pid = getpid();
67    main_tid = pthread_self();
68
69    /*
70     * Funny things happen with the command line if the
71     * execution comes from c:/Program Files/bacula/bacula.exe
72     * We get a command line like: Files/bacula/bacula.exe" options
73     * I.e. someone stops scanning command line on a space, not
74     * realizing that the filename is quoted!!!!!!!!!!
75     * So if first character is not a double quote and
76     * the last character before first space is a double
77     * quote, we throw away the junk.
78     */
79
80    wordPtr = szCmdLine;
81    while (*wordPtr && *wordPtr != ' ')
82       wordPtr++;
83    if (wordPtr > szCmdLine)      /* backup to char before space */
84       wordPtr--;
85    /* if first character is not a quote and last is, junk it */
86    if (*szCmdLine != '"' && *wordPtr == '"') {
87       szCmdLine = wordPtr + 1;
88    }
89 // MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
90
91    /* Build Unix style argc *argv[] */      
92
93    /* Don't NULL command_args[0] !!! */
94    for (i=1;i<MAX_COMMAND_ARGS;i++)
95       command_args[i] = NULL;
96
97    wordPtr = szCmdLine;
98    quote = 0;
99    while  (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
100       wordPtr++;
101    if (*wordPtr == '\"') {
102       quote = 1;
103       wordPtr++;
104    } else if (*wordPtr == '/') {
105       /* Skip Windows options */
106       while (*wordPtr && (*wordPtr != ' ' && *wordPtr != '\t'))
107          wordPtr++;
108       while  (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
109          wordPtr++;
110    }
111    if (*wordPtr) {
112       while (*wordPtr && num_command_args < MAX_COMMAND_ARGS) {
113          tempPtr = wordPtr;
114          if (quote) {
115             while (*tempPtr && *tempPtr != '\"')
116             tempPtr++;
117             quote = 0;
118          } else {
119             while (*tempPtr && *tempPtr != ' ')
120             tempPtr++;
121          }
122          if (*tempPtr)
123             *(tempPtr++) = '\0';
124          command_args[num_command_args++] = wordPtr;
125          wordPtr = tempPtr;
126          while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
127             wordPtr++;
128          if (*wordPtr == '\"') {
129             quote = 1;
130             wordPtr++;
131          }
132       }
133    }
134
135    /*
136     * Now process Windows command line options
137     *   as defined by ATT
138     *
139     * Make the command-line lowercase and parse it
140     */
141    for (i = 0; i < (int)strlen(szCmdLine); i++) {
142       szCmdLine[i] = tolower(szCmdLine[i]);
143    }
144
145    bool argfound = false;
146    for (i = 0; i < (int)strlen(szCmdLine); i++) {
147       if (szCmdLine[i] <= ' ') {
148          continue;
149       }
150
151       if (szCmdLine[i] == '-') {
152          while (szCmdLine[i] && szCmdLine[i] != ' ') {
153             i++;
154          }
155          continue;
156       }
157
158       argfound = true;
159
160       /* Now check for command-line arguments */
161
162       /* /silent install quietly -- no prompts */
163       if (strncmp(&szCmdLine[i], "/silent", strlen("/silent")) == 0) {
164          silent = true;
165          i += strlen("/silent");
166       }
167
168       /* /service start service */
169       if (strncmp(&szCmdLine[i], BaculaRunService, strlen(BaculaRunService)) == 0) {
170          /* Run Bacula as a service */
171          return bacService::BaculaServiceMain();
172       }
173       /* /run  (this is the default if no command line arguments) */
174       if (strncmp(&szCmdLine[i], BaculaRunAsUserApp, strlen(BaculaRunAsUserApp)) == 0) {
175          /* Bacula is being run as a user-level program */
176          return BaculaAppMain();
177       }
178       /* /install */
179       if (strncmp(&szCmdLine[i], BaculaInstallService, strlen(BaculaInstallService)) == 0) {
180          /* Install Bacula as a service */
181          bacService::InstallService();
182          i += strlen(BaculaInstallService);
183          continue;
184       }
185       /* /remove */
186       if (strncmp(&szCmdLine[i], BaculaRemoveService, strlen(BaculaRemoveService)) == 0) {
187          /* Remove the Bacula service */
188          bacService::RemoveService();
189          i += strlen(BaculaRemoveService);
190          continue;
191       }
192
193       /* /about */
194       if (strncmp(&szCmdLine[i], BaculaShowAbout, strlen(BaculaShowAbout)) == 0) {
195          /* Show Bacula's about box */
196          bacService::ShowAboutBox();
197          i += strlen(BaculaShowAbout);
198          continue;
199       }
200
201       /* /status */
202       if (strncmp(&szCmdLine[i], BaculaShowStatus, strlen(BaculaShowStatus)) == 0) {
203          /* Show Bacula's status box */                             
204          bacService::ShowStatus();
205          i += strlen(BaculaShowStatus);
206          continue;
207       }
208
209       /* /kill */
210       if (strncmp(&szCmdLine[i], BaculaKillRunningCopy, strlen(BaculaKillRunningCopy)) == 0) {
211          /* Kill running copy of Bacula */
212          bacService::KillRunningCopy();
213          i += strlen(BaculaKillRunningCopy);
214          continue;
215       }
216
217       /* /help */
218       if (strncmp(&szCmdLine[i], BaculaShowHelp, strlen(BaculaShowHelp)) == 0) {
219          MessageBox(NULL, BaculaUsageText, _("Bacula Usage"), MB_OK|MB_ICONINFORMATION);
220          i += strlen(BaculaShowHelp);
221          continue;
222       }
223       
224       MessageBox(NULL, szCmdLine, _("Bad Command Line Options"), MB_OK);
225
226       /* Show the usage dialog */
227       MessageBox(NULL, BaculaUsageText, _("Bacula Usage"), MB_OK | MB_ICONINFORMATION);
228       break;
229    }
230
231    /* If no arguments were given then just run */
232    if (!argfound) {
233       BaculaAppMain();
234    }
235    return 0;
236 }
237
238
239 /*
240  * Called as a thread from BaculaAppMain()
241  * Here we handle the Windows messages
242  */
243 //DWORD WINAPI Main_Msg_Loop(LPVOID lpwThreadParam)
244 void *Main_Msg_Loop(LPVOID lpwThreadParam) 
245 {
246    DWORD old_servicethread = g_servicethread;
247
248
249    pthread_detach(pthread_self());
250
251    /* Since we are the only thread with a message loop
252     * mark ourselves as the service thread so that
253     * we can receive all the window events.
254     */
255    g_servicethread = GetCurrentThreadId();
256
257    /* Create tray icon & menu if we're running as an app */
258    bacMenu *menu = new bacMenu();
259    if (menu == NULL) {
260 //    log_error_message("Could not create sys tray menu");
261       PostQuitMessage(0);
262    }
263
264
265    /* Now enter the Windows message handling loop until told to quit! */
266    MSG msg;
267    while (GetMessage(&msg, NULL, 0,0) ) {
268       TranslateMessage(&msg);
269       DispatchMessage(&msg);
270    }
271
272    if (menu != NULL) {
273       delete menu;
274    }
275
276    if (old_servicethread != 0) { /* started as NT service */
277       /* Mark that we're no longer running */
278       g_servicethread = 0;
279
280       /* Tell the service manager that we've stopped. */
281       ReportStatus(SERVICE_STOPPED, g_error, 0);
282    }  
283    /* Tell main program to go away */
284    terminate_filed(0);
285
286    /* Should not get here */
287    pthread_kill(main_tid, SIGTERM);   /* ask main thread to terminate */
288    sleep(1);
289    kill(main_pid, SIGTERM);           /* ask main thread to terminate */
290    _exit(0);
291 }
292  
293
294 /*
295  * This is the main routine for Bacula when running as an application
296  * (under Windows 95 or Windows NT)
297  * Under NT, Bacula can also run as a service.  The BaculaServerMain routine,
298  * defined in the bacService header, is used instead when running as a service.
299  */
300 int BaculaAppMain()
301 {
302  /* DWORD dwThreadID; */
303    pthread_t tid;
304
305    InitWinAPIWrapper();
306    VSSInit();
307
308    WSA_Init();
309
310    /* Set this process to be the last application to be shut down. */
311    if (p_SetProcessShutdownParameters) {
312       p_SetProcessShutdownParameters(0x100, 0);
313    }
314
315    HWND hservwnd = FindWindow(MENU_CLASS_NAME, NULL);
316    if (hservwnd != NULL) {
317       /* We don't allow multiple instances! */
318       MessageBox(NULL, _("Another instance of Bacula is already running"), szAppName, MB_OK);
319       _exit(0);
320    }
321
322
323    /* Create a thread to handle the Windows messages */
324 // (void)CreateThread(NULL, 0, Main_Msg_Loop, NULL, 0, &dwThreadID);
325    pthread_create(&tid, NULL,  Main_Msg_Loop, (void *)0);
326
327    /* Call the "real" Bacula */
328    BaculaMain(num_command_args, command_args);
329    PostQuitMessage(0);
330    WSACleanup();
331    _exit(0);
332 }