]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
Enhance bvfs performance .bvfs_update for MySQL
[bacula/bacula] / bacula / src / qt-console / main.cpp
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *  Main program for bat (qt-console)
22  *
23  *   Written by Kern Sibbald, January MMVII
24  *
25  */ 
26
27
28 #include "bat.h"
29 #include <QApplication>
30 #include <QTranslator>
31
32 /*
33  * We need Qt version 4.8.4 or later to be able to comple correctly
34  */
35 #if QT_VERSION < 0x040804
36 #error "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
37 #error "You need Qt version 4.8.4 or later to build Bat"
38 #error "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
39 #endif
40
41 MainWin *mainWin;
42 QApplication *app;
43
44 /* Forward referenced functions */
45 void terminate_console(int sig);                                
46 static void usage();
47 static int check_resources();
48
49 extern bool parse_bat_config(CONFIG *config, const char *configfile, int exit_code);
50 extern void message_callback(int /* type */, char *msg);
51
52
53 #define CONFIG_FILE "bat.conf"     /* default configuration file */
54
55 /* Static variables */
56 static CONFIG *config;
57 static char *configfile = NULL;
58
59 int main(int argc, char *argv[])
60 {
61    int ch;
62    bool no_signals = true;
63    bool test_config = false;
64
65
66    app = new QApplication(argc, argv);        
67    app->setStyle(new QPlastiqueStyle());
68    app->setQuitOnLastWindowClosed(true);
69    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
70      
71    QTranslator qtTranslator;
72    qtTranslator.load(QString("qt_") + QLocale::system().name(),QLibraryInfo::location(QLibraryInfo::TranslationsPath));
73    app->installTranslator(&qtTranslator);
74
75    QTranslator batTranslator;
76    batTranslator.load(QString("bat_") + QLocale::system().name(),QLibraryInfo::location(QLibraryInfo::TranslationsPath));
77    app->installTranslator(&batTranslator);
78
79    register_message_callback(message_callback);
80
81 #ifdef xENABLE_NLS
82    setlocale(LC_ALL, "");
83    bindtextdomain("bacula", LOCALEDIR);
84    textdomain("bacula");
85 #endif
86
87 #ifdef HAVE_WIN32
88    set_trace(true);          /* output to trace file */
89 #endif
90
91    init_stack_dump();
92    my_name_is(argc, argv, "bat");
93    lmgr_init_thread();
94    init_msg(NULL, NULL);
95    working_directory  = "/tmp";
96
97 #ifndef HAVE_WIN32
98    struct sigaction sigignore;
99    sigignore.sa_flags = 0;
100    sigignore.sa_handler = SIG_IGN;
101    sigfillset(&sigignore.sa_mask);
102    sigaction(SIGPIPE, &sigignore, NULL);
103    sigaction(SIGUSR2, &sigignore, NULL);
104 #endif
105
106    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
107       switch (ch) {
108       case 'c':                    /* configuration file */
109          if (configfile != NULL) {
110             free(configfile);
111          }
112          configfile = bstrdup(optarg);
113          break;
114
115       case 'd':
116          debug_level = atoi(optarg);
117          if (debug_level <= 0)
118             debug_level = 1;
119          break;
120
121       case 's':                    /* turn off signals */
122          no_signals = true;
123          break;
124
125       case 't':
126          test_config = true;
127          break;
128
129       case '?':
130       default:
131          usage();
132       }
133    }
134    argc -= optind;
135    argv += optind;
136
137
138    if (!no_signals) {
139       init_signals(terminate_console);
140    }
141
142    if (argc) {
143       usage();
144    }
145
146    OSDependentInit();
147 #ifdef HAVE_WIN32
148    WSA_Init();                        /* Initialize Windows sockets */
149 #endif
150
151    if (configfile == NULL) {
152       configfile = bstrdup(CONFIG_FILE);
153    }
154
155    config = new_config_parser();
156    parse_bat_config(config, configfile, M_ERROR_TERM);
157
158    if (init_crypto() != 0) {
159       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
160    }
161
162    if (!check_resources()) {
163       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
164    }
165    if (test_config) {
166       exit(0);
167    }
168
169    mainWin = new MainWin;
170    mainWin->show();
171
172    return app->exec();
173 }
174
175 void terminate_console(int /*sig*/)
176 {
177 #ifdef HAVE_WIN32
178    WSACleanup();                  /* TODO: check when we have to call it */
179 #endif
180    exit(0);
181 }
182
183 static void usage()
184 {
185    fprintf(stderr, _(
186 PROG_COPYRIGHT
187 "\nVersion: %s (%s) %s %s %s\n\n"
188 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
189 "       -c <file>   set configuration file to file\n"
190 "       -dnn        set debug level to nn\n"
191 "       -s          no signals\n"
192 "       -t          test - read configuration and exit\n"
193 "       -?          print this message.\n"
194 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
195
196    exit(1);
197 }
198
199 /*
200  * Make a quick check to see that we have all the
201  * resources needed.
202  */
203 static int check_resources()
204 {
205    bool ok = true;
206    DIRRES *director;
207    int numdir;
208    bool tls_needed;
209
210    LockRes();
211
212    numdir = 0;
213    foreach_res(director, R_DIRECTOR) {
214       numdir++;
215       /* tls_require implies tls_enable */
216       if (director->tls_require) {
217          if (have_tls) {
218             director->tls_enable = true;
219          } else {
220             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
221             ok = false;
222             continue;
223          }
224       }
225       tls_needed = director->tls_enable || director->tls_authenticate;
226
227       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed) {
228          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
229                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
230                              " At least one CA certificate store is required.\n"),
231                              director->hdr.name, configfile);
232          ok = false;
233       }
234    }
235    
236    if (numdir == 0) {
237       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
238                           "Without that I don't how to speak to the Director :-(\n"), configfile);
239       ok = false;
240    }
241
242    CONRES *cons;
243    /* Loop over Consoles */
244    foreach_res(cons, R_CONSOLE) {
245       /* tls_require implies tls_enable */
246       if (cons->tls_require) {
247          if (have_tls) {
248             cons->tls_enable = true;
249          } else {
250             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
251             ok = false;
252             continue;
253          }
254       }
255       tls_needed = cons->tls_enable || cons->tls_authenticate;
256
257       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && tls_needed) {
258          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
259                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
260                              cons->hdr.name, configfile);
261          ok = false;
262       }
263    }
264
265    UnlockRes();
266
267    return ok;
268 }