]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed.c
3d8e788d22ecd4ea7e410224f1e93b6a6901b3ad
[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-2004 Kern Sibbald and John Walker
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    fprintf(stderr, _(
66 "\nVersion: " VERSION " (" BDATE ")\n\n"
67 "Usage: bacula-fd [-f -s] [-c config_file] [-d debug_level]\n"  
68 "        -c <file>   use <file> as configuration file\n"
69 "        -dnn        set debug level to nn\n"
70 "        -f          run in foreground (for debugging)\n"
71 "        -g          groupid\n"
72 "        -i          inetd request\n"
73 "        -s          no signals (for debugging)\n"
74 "        -t          test configuration file and exit\n"
75 "        -u          userid\n"
76 "        -v          verbose user messages\n"
77 "        -?          print this message.\n"
78 "\n"));         
79    exit(1);
80 }
81
82
83 /********************************************************************* 
84  *
85  *  Main Bacula Unix Client Program                        
86  *
87  */
88 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
89 #define main BaculaMain
90 #endif
91
92 int main (int argc, char *argv[])
93 {
94    int ch;
95    bool test_config = false;
96    DIRRES *director;
97    char *uid = NULL;
98    char *gid = NULL;
99
100    init_stack_dump();
101    my_name_is(argc, argv, "bacula-fd");
102    textdomain("bacula");
103    init_msg(NULL, NULL);
104    daemon_start_time = time(NULL);
105
106    while ((ch = getopt(argc, argv, "c:d:fg:istu:v?")) != -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':                    /* debug level */
116          debug_level = atoi(optarg);
117          if (debug_level <= 0) {
118             debug_level = 1; 
119          }
120          break;
121
122       case 'f':                    /* run in foreground */
123          foreground = true;
124          break;
125
126       case 'g':                    /* set group */
127          gid = optarg;
128          break;
129
130       case 'i':
131          inetd_request = true;
132          break;
133       case 's':
134          no_signals = true;
135          break;
136
137       case 't':
138          test_config = true;
139          break;
140
141       case 'u':                    /* set userid */
142          uid = optarg;
143          break;
144
145       case 'v':                    /* verbose */
146          verbose++;
147          break;
148
149       case '?':
150       default:
151          usage();
152
153       }  
154    }
155    argc -= optind;
156    argv += optind;
157
158    if (argc) {
159       if (configfile != NULL)
160          free(configfile);
161       configfile = bstrdup(*argv);
162       argc--; 
163       argv++;
164    }
165    if (argc) {
166       usage();
167    }
168
169    if (!no_signals) {
170       init_signals(terminate_filed);
171    } else {
172       /* This reduces the number of signals facilitating debugging */
173       watchdog_sleep_time = 120;      /* long timeout for debugging */
174    }
175
176    if (configfile == NULL) {
177       configfile = bstrdup(CONFIG_FILE);
178    }
179
180    parse_config(configfile);
181
182    LockRes();
183    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
184    UnlockRes();
185    if (!director) {
186       Emsg1(M_ABORT, 0, _("No Director resource defined in %s\n"),
187          configfile);
188    }
189
190    LockRes();
191    me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
192    UnlockRes();
193    if (!me) {
194       Emsg1(M_ABORT, 0, _("No File daemon resource defined in %s\n\
195 Without that I don't know who I am :-(\n"), configfile);
196    } else {
197       my_name_is(0, NULL, me->hdr.name);
198       if (!me->messages) {
199          LockRes();
200          me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
201          UnlockRes();
202          if (!me->messages) {
203              Emsg1(M_ABORT, 0, _("No Messages resource defined in %s\n"), configfile);
204          }
205       }
206       close_msg(NULL);                /* close temp message handler */
207       init_msg(NULL, me->messages);   /* open user specified message handler */
208    }
209
210    set_working_directory(me->working_directory);
211
212    if (test_config) {
213       terminate_filed(0);
214    }
215
216    if (!foreground &&!inetd_request) {
217       daemon_start();
218       init_stack_dump();              /* set new pid */
219    }
220
221    /* Maximum 1 daemon at a time */
222    create_pid_file(me->pid_directory, "bacula-fd", me->FDport);
223    read_state_file(me->working_directory, "bacula-fd", me->FDport);
224
225    drop(uid, gid);
226
227 #ifdef BOMB
228    me += 1000000;
229 #endif
230
231    set_thread_concurrency(10);
232
233    if (!no_signals) {
234       start_watchdog();               /* start watchdog thread */
235       init_jcr_subsystem();           /* start JCR watchdogs etc. */
236    }
237    server_tid = pthread_self();
238
239    if (inetd_request) {
240       /* Socket is on fd 0 */          
241       struct sockaddr_in client_addr;
242       memset(&client_addr, 0, sizeof(client_addr));
243       BSOCK *bs = init_bsock(NULL, 0, "client", "unknown client", me->FDport, 
244                              &client_addr);
245       handle_client_request((void *)bs);
246    } else {
247       /* Become server, and handle requests */
248       Dmsg1(10, "filed: listening on port %d\n", me->FDport);
249       bnet_thread_server(me->FDaddr, me->FDport, me->MaxConcurrentJobs, 
250                       &dir_workq, handle_client_request);
251    }
252
253    terminate_filed(0);
254    exit(0);                           /* should never get here */
255 }
256
257 void terminate_filed(int sig)
258 {
259    bnet_stop_thread_server(server_tid);
260    write_state_file(me->working_directory, "bacula-fd", me->FDport);
261    delete_pid_file(me->pid_directory, "bacula-fd", me->FDport);
262    if (configfile != NULL) {
263       free(configfile);
264    }
265    if (debug_level > 5) {
266       print_memory_pool_stats(); 
267    }
268    free_config_resources();
269    term_msg();
270    stop_watchdog();
271    close_memory_pool();               /* release free memory in pool */
272    sm_dump(false);                    /* dump orphaned buffers */
273    exit(sig);
274 }