]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed.c
- Fix new Python code to work for Director.
[bacula/bacula] / bacula / src / filed / filed.c
1 /*
2  *  Bacula File Daemon
3  *
4  *    Kern Sibbald, March MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2005 Kern Sibbald
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31
32 /* Imported Functions */
33 extern void *handle_client_request(void *dir_sock);
34
35 /* Imported Variables */
36 extern time_t watchdog_sleep_time;
37
38 /* Forward referenced functions */
39 void terminate_filed(int sig);
40
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;
46
47 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
48 const int win32_client = 1;
49 #else
50 const int win32_client = 0;
51 #endif
52
53
54 #define CONFIG_FILE "./bacula-fd.conf" /* default config file */
55
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;
61
62
63 static void usage()
64 {
65    Pmsg0(-1, _(
66 "Copyright (C) 2000-2005 Kern Sibbald\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"
72 "        -g          groupid\n"
73 "        -i          inetd request\n"
74 "        -s          no signals (for debugging)\n"
75 "        -t          test configuration file and exit\n"
76 "        -u          userid\n"
77 "        -v          verbose user messages\n"
78 "        -?          print this message.\n"
79 "\n"));
80    exit(1);
81 }
82
83
84 /*********************************************************************
85  *
86  *  Main Bacula Unix Client Program
87  *
88  */
89 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
90 #define main BaculaMain
91 #endif
92
93 int main (int argc, char *argv[])
94 {
95    int ch;
96    bool test_config = false;
97    DIRRES *director;
98    char *uid = NULL;
99    char *gid = NULL;
100
101    init_stack_dump();
102    my_name_is(argc, argv, "bacula-fd");
103    textdomain("bacula");
104    init_msg(NULL, NULL);
105    daemon_start_time = time(NULL);
106
107    while ((ch = getopt(argc, argv, "c:d:fg:istu:v?")) != -1) {
108       switch (ch) {
109       case 'c':                    /* configuration file */
110          if (configfile != NULL) {
111             free(configfile);
112          }
113          configfile = bstrdup(optarg);
114          break;
115
116       case 'd':                    /* debug level */
117          debug_level = atoi(optarg);
118          if (debug_level <= 0) {
119             debug_level = 1;
120          }
121          break;
122
123       case 'f':                    /* run in foreground */
124          foreground = true;
125          break;
126
127       case 'g':                    /* set group */
128          gid = optarg;
129          break;
130
131       case 'i':
132          inetd_request = true;
133          break;
134       case 's':
135          no_signals = true;
136          break;
137
138       case 't':
139          test_config = true;
140          break;
141
142       case 'u':                    /* set userid */
143          uid = optarg;
144          break;
145
146       case 'v':                    /* verbose */
147          verbose++;
148          break;
149
150       case '?':
151       default:
152          usage();
153
154       }
155    }
156    argc -= optind;
157    argv += optind;
158
159    if (argc) {
160       if (configfile != NULL)
161          free(configfile);
162       configfile = bstrdup(*argv);
163       argc--;
164       argv++;
165    }
166    if (argc) {
167       usage();
168    }
169
170    server_tid = pthread_self();
171    if (!no_signals) {
172       init_signals(terminate_filed);
173    } else {
174       /* This reduces the number of signals facilitating debugging */
175       watchdog_sleep_time = 120;      /* long timeout for debugging */
176    }
177
178    if (configfile == NULL) {
179       configfile = bstrdup(CONFIG_FILE);
180    }
181
182    parse_config(configfile);
183
184    LockRes();
185    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
186    UnlockRes();
187    if (!director) {
188       Emsg1(M_ABORT, 0, _("No Director resource defined in %s\n"),
189          configfile);
190    }
191
192    LockRes();
193    me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
194    UnlockRes();
195    if (!me) {
196       Emsg1(M_ABORT, 0, _("No File daemon resource defined in %s\n"
197 "Without that I don't know who I am :-(\n"), configfile);
198    } else {
199       my_name_is(0, NULL, me->hdr.name);
200       if (!me->messages) {
201          LockRes();
202          me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
203          UnlockRes();
204          if (!me->messages) {
205              Emsg1(M_ABORT, 0, _("No Messages resource defined in %s\n"), configfile);
206          }
207       }
208       close_msg(NULL);                /* close temp message handler */
209       init_msg(NULL, me->messages);   /* open user specified message handler */
210    }
211
212    set_working_directory(me->working_directory);
213
214    if (test_config) {
215       terminate_filed(0);
216    }
217
218    if (!foreground &&!inetd_request) {
219       daemon_start();
220       init_stack_dump();              /* set new pid */
221    }
222
223    /* Maximum 1 daemon at a time */
224    create_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
225    read_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
226
227    drop(uid, gid);
228
229 #ifdef BOMB
230    me += 1000000;
231 #endif
232
233    init_python_interpreter(me->hdr.name, me->scripts_directory, "FDStartUp");
234
235    set_thread_concurrency(10);
236
237    if (!no_signals) {
238       start_watchdog();               /* start watchdog thread */
239       init_jcr_subsystem();           /* start JCR watchdogs etc. */
240    }
241    server_tid = pthread_self();
242
243    if (inetd_request) {
244       /* Socket is on fd 0 */
245       struct sockaddr client_addr;
246       int port = -1;
247       socklen_t client_addr_len = sizeof(client_addr);
248       if (getsockname(0, &client_addr, &client_addr_len) == 0) {
249                 /* MA BUG 6 remove ifdefs */
250                 port = sockaddr_get_port_net_order(&client_addr);
251       }
252       BSOCK *bs = init_bsock(NULL, 0, "client", "unknown client", port, &client_addr);
253       handle_client_request((void *)bs);
254    } else {
255       /* Become server, and handle requests */
256       IPADDR *p;
257       foreach_dlist(p, me->FDaddrs) {
258          Dmsg1(10, "filed: listening on port %d\n", p->get_port_host_order());
259       }
260       bnet_thread_server(me->FDaddrs, me->MaxConcurrentJobs, &dir_workq, handle_client_request);
261    }
262
263    terminate_filed(0);
264    exit(0);                           /* should never get here */
265 }
266
267 void terminate_filed(int sig)
268 {
269    bnet_stop_thread_server(server_tid);
270    write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
271    delete_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
272    if (configfile != NULL) {
273       free(configfile);
274    }
275    if (debug_level > 5) {
276       print_memory_pool_stats();
277    }
278    free_config_resources();
279    term_msg();
280    stop_watchdog();
281    close_memory_pool();               /* release free memory in pool */
282    sm_dump(false);                    /* dump orphaned buffers */
283    exit(sig);
284 }