2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 #include "tray-monitor.h"
21 #include <QInputDialog>
24 /* Static variables */
25 char *configfile = NULL;
26 static MONITOR *monitor = NULL;
27 static CONFIG *config = NULL;
28 static TrayUI *mainwidget = NULL;
29 static TSched *scheduler = NULL;
31 #define CONFIG_FILE "./bacula-tray-monitor.conf" /* default configuration file */
34 #define HOME_VAR "APPDATA"
35 #define CONFIG_FILE_HOME "bacula-tray-monitor.conf" /* In $HOME */
37 #define HOME_VAR "HOME"
38 #define CONFIG_FILE_HOME ".bacula-tray-monitor.conf" /* In $HOME */
45 "\n%sVersion: %s (%s) %s %s %s\n\n"
46 "Usage: tray-monitor [-c config_file] [-d debug_level]\n"
47 " -c <file> set configuration file to file\n"
48 " -d <nn> set debug level to <nn>\n"
49 " -dt print timestamp in debug output\n"
50 " -t test - read configuration and exit\n"
51 " -W 0/1 force the detection of the systray\n"
52 " -? print this message.\n"
53 "\n"), 2004, "", VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
56 void refresh_tray(TrayUI *t)
70 mon = (MONITOR *) GetNextRes(R_MONITOR, NULL);
71 t->spinRefresh->setValue(mon?mon->RefreshInterval:60);
73 foreach_res(r, R_CLIENT) {
76 foreach_res(r, R_DIRECTOR) {
79 foreach_res(r, R_STORAGE) {
84 void display_error(const char *fmt, ...)
87 POOL_MEM tmp(PM_MESSAGE);
95 maxlen = tmp.size() - 1;
96 va_start(arg_ptr, fmt);
97 bvsnprintf(tmp.c_str(), maxlen, fmt, arg_ptr);
100 msgBox.setIcon(QMessageBox::Critical);
101 msgBox.setText(tmp.c_str());
105 void error_handler(const char *file, int line, LEX */* lc */, const char *msg, ...)
109 va_start(arg_ptr, msg);
110 vsnprintf(tmp.c_str(), tmp.size(), msg, arg_ptr);
112 display_error("Error %s:%d %s\n", file, line, tmp.c_str());
115 int tls_pem_callback(char *buf, int size, const void * /*userdata*/)
118 QString text = QInputDialog::getText(mainwidget, _("TLS PassPhrase"),
119 buf, QLineEdit::Normal,
120 QDir::home().dirName(), &ok);
122 bstrncpy(buf, text.toUtf8().data(), size);
131 bool displaycfg=false;
135 Dmsg0(50, "reload the configuration!\n");
143 if (stat(configfile, &sp) != 0) {
145 Dmsg2(50, "Unable to find %s. ERR=%s\n", configfile, be.bstrerror());
150 config = New(CONFIG());
151 if (!parse_tmon_config(config, configfile, M_ERROR)) {
152 Dmsg1(50, "Error while parsing %s\n", configfile);
153 // TODO: Display a warning message an open the configuration
159 foreach_res(monitor, R_MONITOR) {
162 if (!displaycfg && nitems != 1) {
163 Mmsg(config->m_errmsg,
164 _("Error: %d Monitor resources defined in %s. "
165 "You must define one Monitor resource.\n"),
169 monitor = (MONITOR*)GetNextRes(R_MONITOR, (RES *)NULL);
172 display_error(config->m_errmsg);
174 refresh_tray(mainwidget);
175 if (monitor && monitor->command_dir) {
176 scheduler->init(monitor->command_dir);
179 Dmsg0(50, "Do not start the scheduler\n");
185 /*********************************************************************
187 * Main Bacula Tray Monitor -- User Interface Program
190 int main(int argc, char *argv[])
192 QApplication app(argc, argv);
194 bool test_config = false, display_cfg = false;
198 setlocale(LC_ALL, "");
199 bindtextdomain("bacula", LOCALEDIR);
200 textdomain("bacula");
203 my_name_is(argc, argv, "tray-monitor");
205 init_msg(NULL, NULL, NULL);
207 working_directory = getenv("TMP");
209 if (working_directory == NULL) {
210 working_directory = "/tmp";
215 struct sigaction sigignore;
216 sigignore.sa_flags = 0;
217 sigignore.sa_handler = SIG_IGN;
218 sigfillset(&sigignore.sa_mask);
219 sigaction(SIGPIPE, &sigignore, NULL);
222 while ((ch = getopt(argc, argv, "c:d:th?TW:")) != -1) {
224 case 'c': /* configuration file */
225 if (configfile != NULL) {
228 configfile = bstrdup(optarg);
232 tray.have_systray = (atoi(optarg) != 0);
240 if (*optarg == 't') {
241 dbg_timestamp = true;
243 debug_level = atoi(optarg);
244 if (debug_level <= 0) {
269 /* Keep generated files for ourself */
272 if (configfile == NULL) {
273 if (getenv(HOME_VAR) != NULL) {
274 int len = strlen(getenv(HOME_VAR)) + strlen(CONFIG_FILE_HOME) + 5;
275 configfile = (char *) malloc(len);
276 bsnprintf(configfile, len, "%s/%s", getenv(HOME_VAR), CONFIG_FILE_HOME);
279 configfile = bstrdup(CONFIG_FILE);
282 Dmsg1(50, "configfile=%s\n", configfile);
284 // We need to initialize the scheduler before the reload() command
287 OSDependentInit(); /* Initialize Windows path handling */
288 (void)WSA_Init(); /* Initialize Windows sockets */
290 display_cfg = reload();
295 /* If we have a systray, we always keep the application*/
296 if (tray.have_systray) {
297 app.setQuitOnLastWindowClosed(false);
299 } else { /* Without a systray, we quit when we close */
300 app.setQuitOnLastWindowClosed(true);
302 tray.setupUi(&tray, monitor);
311 (void)WSACleanup(); /* Cleanup Windows sockets */
317 bfree_and_null(configfile);