4 * Kern Sibbald, March MM
10 Copyright (C) 2000-2005 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* Imported Functions */
33 extern void *handle_client_request(void *dir_sock);
35 /* Imported Variables */
36 extern time_t watchdog_sleep_time;
38 /* Forward referenced functions */
39 void terminate_filed(int sig);
40 static int check_resources();
42 /* Exported variables */
43 CLIENT *me; /* my resource */
44 char OK_msg[] = "2000 OK\n";
45 char TERM_msg[] = "2999 Terminate\n";
46 bool no_signals = false;
48 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
49 const int win32_client = 1;
51 const int win32_client = 0;
55 #define CONFIG_FILE "./bacula-fd.conf" /* default config file */
57 static char *configfile = NULL;
58 static bool foreground = false;
59 static bool inetd_request = false;
60 static workq_t dir_workq; /* queue of work from Director */
61 static pthread_t server_tid;
67 "Copyright (C) 2000-2005 Kern Sibbald\n"
68 "\nVersion: " VERSION " (" BDATE ")\n\n"
69 "Usage: bacula-fd [-f -s] [-c config_file] [-d debug_level]\n"
70 " -c <file> use <file> as configuration file\n"
71 " -dnn set debug level to nn\n"
72 " -f run in foreground (for debugging)\n"
75 " -s no signals (for debugging)\n"
76 " -t test configuration file and exit\n"
78 " -v verbose user messages\n"
79 " -? print this message.\n"
85 /*********************************************************************
87 * Main Bacula Unix Client Program
90 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
91 #define main BaculaMain
94 int main (int argc, char *argv[])
97 bool test_config = false;
102 my_name_is(argc, argv, "bacula-fd");
103 textdomain("bacula");
104 init_msg(NULL, NULL);
105 daemon_start_time = time(NULL);
107 while ((ch = getopt(argc, argv, "c:d:fg:istu:v?")) != -1) {
109 case 'c': /* configuration file */
110 if (configfile != NULL) {
113 configfile = bstrdup(optarg);
116 case 'd': /* debug level */
117 debug_level = atoi(optarg);
118 if (debug_level <= 0) {
123 case 'f': /* run in foreground */
127 case 'g': /* set group */
132 inetd_request = true;
142 case 'u': /* set userid */
146 case 'v': /* verbose */
160 if (configfile != NULL)
162 configfile = bstrdup(*argv);
170 server_tid = pthread_self();
172 init_signals(terminate_filed);
174 /* This reduces the number of signals facilitating debugging */
175 watchdog_sleep_time = 120; /* long timeout for debugging */
178 if (configfile == NULL) {
179 configfile = bstrdup(CONFIG_FILE);
182 parse_config(configfile);
185 if (init_tls() != 0) {
186 Emsg0(M_ERROR, 0, _("TLS library initialization failed.\n"));
191 if (!check_resources()) {
192 Emsg1(M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
196 set_working_directory(me->working_directory);
202 if (!foreground &&!inetd_request) {
204 init_stack_dump(); /* set new pid */
207 /* Maximum 1 daemon at a time */
208 create_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
209 read_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
217 init_python_interpreter(me->hdr.name, me->scripts_directory, "FDStartUp");
219 set_thread_concurrency(10);
222 start_watchdog(); /* start watchdog thread */
223 init_jcr_subsystem(); /* start JCR watchdogs etc. */
225 server_tid = pthread_self();
228 /* Socket is on fd 0 */
229 struct sockaddr client_addr;
231 socklen_t client_addr_len = sizeof(client_addr);
232 if (getsockname(0, &client_addr, &client_addr_len) == 0) {
233 /* MA BUG 6 remove ifdefs */
234 port = sockaddr_get_port_net_order(&client_addr);
236 BSOCK *bs = init_bsock(NULL, 0, "client", "unknown client", port, &client_addr);
237 handle_client_request((void *)bs);
239 /* Become server, and handle requests */
241 foreach_dlist(p, me->FDaddrs) {
242 Dmsg1(10, "filed: listening on port %d\n", p->get_port_host_order());
244 bnet_thread_server(me->FDaddrs, me->MaxConcurrentJobs, &dir_workq, handle_client_request);
248 exit(0); /* should never get here */
251 void terminate_filed(int sig)
253 bnet_stop_thread_server(server_tid);
254 generate_daemon_event(NULL, "Exit");
255 write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
256 delete_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
258 if (configfile != NULL) {
261 if (debug_level > 5) {
262 print_memory_pool_stats();
264 free_config_resources();
270 close_memory_pool(); /* release free memory in pool */
271 sm_dump(false); /* dump orphaned buffers */
276 * Make a quick check to see that we have all the
279 static int check_resources()
286 me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
288 Emsg1(M_FATAL, 0, _("No File daemon resource defined in %s\n"
289 "Without that I don't know who I am :-(\n"), configfile);
292 if (GetNextRes(R_CLIENT, (RES *) me) != NULL) {
293 Emsg1(M_FATAL, 0, _("Only one Client resource permitted in %s\n"),
297 my_name_is(0, NULL, me->hdr.name);
299 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
301 Emsg1(M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
306 /* tls_require implies tls_enable */
307 if (me->tls_require) {
308 me->tls_enable = true;
311 if ((!me->tls_ca_certfile && !me->tls_ca_certdir) && me->tls_enable) {
312 Emsg1(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
313 " or \"TLS CA Certificate Dir\" are defined for File daemon in %s.\n"),
318 /* If everything is well, attempt to initialize our per-resource TLS context */
319 if (OK && (me->tls_enable || me->tls_require)) {
320 /* Initialize TLS context:
321 * Args: CA certfile, CA certdir, Certfile, Keyfile,
322 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
323 me->tls_ctx = new_tls_context(me->tls_ca_certfile,
324 me->tls_ca_certdir, me->tls_certfile, me->tls_keyfile,
325 NULL, NULL, NULL, true);
328 Emsg2(M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
329 me->hdr.name, configfile);
334 #endif /* HAVE_TLS */
338 /* Verify that a director record exists */
340 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
343 Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"),
349 foreach_res(director, R_DIRECTOR) {
350 /* tls_require implies tls_enable */
351 if (director->tls_require) {
352 director->tls_enable = true;
355 if (!director->tls_certfile && director->tls_enable) {
356 Emsg2(M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
357 director->hdr.name, configfile);
361 if (!director->tls_keyfile && director->tls_enable) {
362 Emsg2(M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
363 director->hdr.name, configfile);
367 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable && director->tls_verify_peer) {
368 Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
369 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
370 " At least one CA certificate store is required"
371 " when using \"TLS Verify Peer\".\n"),
372 director->hdr.name, configfile);
376 /* If everything is well, attempt to initialize our per-resource TLS context */
377 if (OK && (director->tls_enable || director->tls_require)) {
378 /* Initialize TLS context:
379 * Args: CA certfile, CA certdir, Certfile, Keyfile,
380 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
381 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
382 director->tls_ca_certdir, director->tls_certfile,
383 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
384 director->tls_verify_peer);
386 if (!director->tls_ctx) {
387 Emsg2(M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
388 director->hdr.name, configfile);
393 #endif /* HAVE_TLS */
398 close_msg(NULL); /* close temp message handler */
399 init_msg(NULL, me->messages); /* open user specified message handler */