]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/filed/winmain.cpp
Fixed symbol file generation on Microsoft Visual Studio builds.
[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-fd";
38 DWORD           mainthreadId;
39 bool            silent = false;
40
41 /* Imported variables */
42 extern DWORD    g_servicethread;
43
44 #define MAX_COMMAND_ARGS 100
45 static char *command_args[MAX_COMMAND_ARGS] = {"bacula-fd", NULL};
46 static int num_command_args = 1;
47 static pid_t main_pid;
48 static pthread_t main_tid;
49
50 /*
51  * WinMain parses the command line and either calls the main App
52  * routine or, under NT, the main service routine.
53  */
54 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
55                    PSTR CmdLine, int iCmdShow)
56 {
57    char *szCmdLine = CmdLine;
58    char *wordPtr, *tempPtr;
59    int i, quote;
60
61    /* Save the application instance and main thread id */
62    hAppInstance = hInstance;
63    mainthreadId = GetCurrentThreadId();
64
65    main_pid = getpid();
66    main_tid = pthread_self();
67
68    /*
69     * Funny things happen with the command line if the
70     * execution comes from c:/Program Files/bacula/bacula.exe
71     * We get a command line like: Files/bacula/bacula.exe" options
72     * I.e. someone stops scanning command line on a space, not
73     * realizing that the filename is quoted!!!!!!!!!!
74     * So if first character is not a double quote and
75     * the last character before first space is a double
76     * quote, we throw away the junk.
77     */
78
79    wordPtr = szCmdLine;
80    while (*wordPtr && *wordPtr != ' ')
81       wordPtr++;
82    if (wordPtr > szCmdLine)      /* backup to char before space */
83       wordPtr--;
84    /* if first character is not a quote and last is, junk it */
85    if (*szCmdLine != '"' && *wordPtr == '"') {
86       szCmdLine = wordPtr + 1;
87    }
88 // MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
89
90    /* Build Unix style argc *argv[] */      
91
92    /* Don't NULL command_args[0] !!! */
93    for (i=1;i<MAX_COMMAND_ARGS;i++)
94       command_args[i] = NULL;
95
96    char *pszArgs = bstrdup(szCmdLine);
97    wordPtr = pszArgs;
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     */
138    bool argfound = false;
139    for (i = 0; i < (int)strlen(szCmdLine); i++) {
140       if (szCmdLine[i] <= ' ') {
141          continue;
142       }
143
144       if (szCmdLine[i] != '/') {
145          break;
146       }
147
148       argfound = true;
149
150       /* Now check for command-line arguments */
151
152       /* /silent install quietly -- no prompts */
153       if (strnicmp(&szCmdLine[i], "/silent", strlen("/silent")) == 0) {
154          silent = true;
155          i += strlen("/silent");
156          continue;
157       }
158
159       /* /service start service */
160       if (strnicmp(&szCmdLine[i], BaculaRunService, strlen(BaculaRunService)) == 0) {
161          /* Run Bacula as a service */
162          return bacService::BaculaServiceMain();
163       }
164       /* /run  (this is the default if no command line arguments) */
165       if (strnicmp(&szCmdLine[i], BaculaRunAsUserApp, strlen(BaculaRunAsUserApp)) == 0) {
166          /* Bacula is being run as a user-level program */
167          return BaculaAppMain();
168       }
169       /* /install */
170       if (strnicmp(&szCmdLine[i], BaculaInstallService, strlen(BaculaInstallService)) == 0) {
171          /* Install Bacula as a service */
172          bacService::InstallService(&szCmdLine[i + strlen(BaculaInstallService)]);
173          i += strlen(BaculaInstallService);
174          continue;
175       }
176       /* /remove */
177       if (strnicmp(&szCmdLine[i], BaculaRemoveService, strlen(BaculaRemoveService)) == 0) {
178          /* Remove the Bacula service */
179          bacService::RemoveService();
180          i += strlen(BaculaRemoveService);
181          continue;
182       }
183
184       /* /about */
185       if (strnicmp(&szCmdLine[i], BaculaShowAbout, strlen(BaculaShowAbout)) == 0) {
186          /* Show Bacula's about box */
187          bacService::ShowAboutBox();
188          i += strlen(BaculaShowAbout);
189          continue;
190       }
191
192       /* /status */
193       if (strnicmp(&szCmdLine[i], BaculaShowStatus, strlen(BaculaShowStatus)) == 0) {
194          /* Show Bacula's status box */                             
195          bacService::ShowStatus();
196          i += strlen(BaculaShowStatus);
197          continue;
198       }
199
200       /* /kill */
201       if (strnicmp(&szCmdLine[i], BaculaKillRunningCopy, strlen(BaculaKillRunningCopy)) == 0) {
202          /* Kill running copy of Bacula */
203          bacService::KillRunningCopy();
204          i += strlen(BaculaKillRunningCopy);
205          continue;
206       }
207
208       /* /help */
209       if (strnicmp(&szCmdLine[i], BaculaShowHelp, strlen(BaculaShowHelp)) == 0) {
210          MessageBox(NULL, BaculaUsageText, _("Bacula Usage"), MB_OK|MB_ICONINFORMATION);
211          i += strlen(BaculaShowHelp);
212          continue;
213       }
214       
215       MessageBox(NULL, szCmdLine, _("Bad Command Line Options"), MB_OK);
216
217       /* Show the usage dialog */
218       MessageBox(NULL, BaculaUsageText, _("Bacula Usage"), MB_OK | MB_ICONINFORMATION);
219       break;
220    }
221
222    /* If no arguments were given then just run */
223    if (!argfound) {
224       BaculaAppMain();
225    }
226    return 0;
227 }
228
229
230 /*
231  * Called as a thread from BaculaAppMain()
232  * Here we handle the Windows messages
233  */
234 //DWORD WINAPI Main_Msg_Loop(LPVOID lpwThreadParam)
235 void *Main_Msg_Loop(LPVOID lpwThreadParam) 
236 {
237    DWORD old_servicethread = g_servicethread;
238
239
240    pthread_detach(pthread_self());
241
242    /* Since we are the only thread with a message loop
243     * mark ourselves as the service thread so that
244     * we can receive all the window events.
245     */
246    g_servicethread = GetCurrentThreadId();
247
248    /* Create tray icon & menu if we're running as an app */
249    bacMenu *menu = new bacMenu();
250    if (menu == NULL) {
251 //    log_error_message("Could not create sys tray menu");
252       PostQuitMessage(0);
253    }
254
255    /* Now enter the Windows message handling loop until told to quit! */
256    MSG msg;
257    while (GetMessage(&msg, NULL, 0,0) ) {
258       TranslateMessage(&msg);
259       DispatchMessage(&msg);
260    }
261
262    if (menu != NULL) {
263       delete menu;
264    }
265
266    if (old_servicethread != 0) { /* started as NT service */
267       /* Mark that we're no longer running */
268       g_servicethread = 0;
269
270       /* Tell the service manager that we've stopped. */
271       ReportStatus(SERVICE_STOPPED, g_error, 0);
272    }  
273    /* Tell main program to go away */
274    terminate_filed(0);
275
276    /* Should not get here */
277    pthread_kill(main_tid, SIGTERM);   /* ask main thread to terminate */
278    sleep(1);
279    kill(main_pid, SIGTERM);           /* ask main thread to terminate */
280    _exit(0);
281 }
282  
283
284 /*
285  * This is the main routine for Bacula when running as an application
286  * (under Windows 95 or Windows NT)
287  * Under NT, Bacula can also run as a service.  The BaculaServerMain routine,
288  * defined in the bacService header, is used instead when running as a service.
289  */
290 int BaculaAppMain()
291 {
292  /* DWORD dwThreadID; */
293    pthread_t tid;
294
295    InitWinAPIWrapper();
296    VSSInit();
297
298    WSA_Init();
299
300    /* Set this process to be the last application to be shut down. */
301    if (p_SetProcessShutdownParameters) {
302       p_SetProcessShutdownParameters(0x100, 0);
303    }
304
305    HWND hservwnd = FindWindow(MENU_CLASS_NAME, NULL);
306    if (hservwnd != NULL) {
307       /* We don't allow multiple instances! */
308       MessageBox(NULL, _("Another instance of Bacula is already running"), szAppName, MB_OK);
309       _exit(0);
310    }
311
312    /* Create a thread to handle the Windows messages */
313    pthread_create(&tid, NULL,  Main_Msg_Loop, (void *)0);
314
315    /* Call the "real" Bacula */
316    BaculaMain(num_command_args, command_args);
317    PostQuitMessage(0);
318    WSACleanup();
319    _exit(0);
320 }