4 * Kern Sibbald, March MM
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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);
41 /* Exported variables */
42 CLIENT *me; /* my resource */
43 char OK_msg[] = "2000 OK\n";
44 char TERM_msg[] = "2999 Terminate\n";
45 bool no_signals = false;
47 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
48 const int win32_client = 1;
50 const int win32_client = 0;
54 #define CONFIG_FILE "./bacula-fd.conf" /* default config file */
56 static char *configfile = NULL;
57 static bool foreground = false;
58 static bool inetd_request = false;
59 static workq_t dir_workq; /* queue of work from Director */
60 static pthread_t server_tid;
66 "Copyright (C) 2000-2004 Kern Sibbald and John Walker\n"
67 "\nVersion: " VERSION " (" BDATE ")\n\n"
68 "Usage: bacula-fd [-f -s] [-c config_file] [-d debug_level]\n"
69 " -c <file> use <file> as configuration file\n"
70 " -dnn set debug level to nn\n"
71 " -f run in foreground (for debugging)\n"
74 " -s no signals (for debugging)\n"
75 " -t test configuration file and exit\n"
77 " -v verbose user messages\n"
78 " -? print this message.\n"
84 /*********************************************************************
86 * Main Bacula Unix Client Program
89 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
90 #define main BaculaMain
93 int main (int argc, char *argv[])
96 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);
171 init_signals(terminate_filed);
173 /* This reduces the number of signals facilitating debugging */
174 watchdog_sleep_time = 120; /* long timeout for debugging */
177 if (configfile == NULL) {
178 configfile = bstrdup(CONFIG_FILE);
181 parse_config(configfile);
184 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
187 Emsg1(M_ABORT, 0, _("No Director resource defined in %s\n"),
192 me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
195 Emsg1(M_ABORT, 0, _("No File daemon resource defined in %s\n\
196 Without that I don't know who I am :-(\n"), configfile);
198 my_name_is(0, NULL, me->hdr.name);
201 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
204 Emsg1(M_ABORT, 0, _("No Messages resource defined in %s\n"), configfile);
207 close_msg(NULL); /* close temp message handler */
208 init_msg(NULL, me->messages); /* open user specified message handler */
211 set_working_directory(me->working_directory);
217 if (!foreground &&!inetd_request) {
219 init_stack_dump(); /* set new pid */
222 /* Maximum 1 daemon at a time */
223 create_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
224 read_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
232 set_thread_concurrency(10);
235 start_watchdog(); /* start watchdog thread */
236 init_jcr_subsystem(); /* start JCR watchdogs etc. */
238 server_tid = pthread_self();
241 /* Socket is on fd 0 */
242 struct sockaddr client_addr;
244 socklen_t client_addr_len = sizeof(client_addr);
245 if (getsockname(0, &client_addr, &client_addr_len) == 0) {
246 /* MA BUG 6 remove ifdefs */
247 port = sockaddr_get_port_net_order(&client_addr);
249 BSOCK *bs = init_bsock(NULL, 0, "client", "unknown client", port, &client_addr);
250 handle_client_request((void *)bs);
252 /* Become server, and handle requests */
254 foreach_dlist(p, me->FDaddrs) {
255 Dmsg1(10, "filed: listening on port %d\n", p->get_port_host_order());
257 bnet_thread_server(me->FDaddrs, me->MaxConcurrentJobs, &dir_workq, handle_client_request);
261 exit(0); /* should never get here */
264 void terminate_filed(int sig)
266 bnet_stop_thread_server(server_tid);
267 write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
268 delete_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
269 if (configfile != NULL) {
272 if (debug_level > 5) {
273 print_memory_pool_stats();
275 free_config_resources();
278 close_memory_pool(); /* release free memory in pool */
279 sm_dump(false); /* dump orphaned buffers */