]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed.c
Server address binding + bscan updates -- see kes25Sep02
[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, 2001, 2002 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 /* Forward referenced functions */
36 void terminate_filed(int sig);
37
38 /* Exported variables */
39
40
41 #ifdef HAVE_CYGWIN
42 int win32_client = 1;
43 #else
44 int win32_client = 0;
45 #endif
46
47
48 #define CONFIG_FILE "./bacula-fd.conf" /* default config file */
49
50 static char *configfile = NULL;
51 static int foreground = 0;
52 static workq_t dir_workq;             /* queue of work from Director */
53
54 CLIENT *me;                           /* my resource */
55
56 static void usage()
57 {
58    fprintf(stderr, _(
59 "\nVersion: " VERSION " (" DATE ")\n\n"
60 "Usage: filed [-f -s] [-c config_file] [-d debug_level] [config_file]\n"  
61 "        -c <file>   use <file> as configuration file\n"
62 "        -dnn        set debug level to nn\n"
63 "        -f          run in foreground (for debugging)\n"
64 "        -s          no signals (for debugging)\n"
65 "        -t          test configuration file and exit\n"
66 "        -?          print this message.\n"
67 "\n"));         
68    exit(1);
69 }
70
71
72 /********************************************************************* 
73  *
74  *  Main Bacula Unix Client Program                        
75  *
76  */
77 #ifdef HAVE_CYGWIN
78 #define main BaculaMain
79 #endif
80 int main (int argc, char *argv[])
81 {
82    int ch;
83    int no_signals = FALSE;
84    int test_config = FALSE;
85    DIRRES *director;
86
87    init_stack_dump();
88    my_name_is(argc, argv, "filed");
89    init_msg(NULL, NULL);
90    daemon_start_time = time(NULL);
91
92    memset(&last_job, 0, sizeof(last_job));
93
94    while ((ch = getopt(argc, argv, "c:d:fst?")) != -1) {
95       switch (ch) {
96          case 'c':                    /* configuration file */
97             if (configfile != NULL) {
98                free(configfile);
99             }
100             configfile = bstrdup(optarg);
101             break;
102
103          case 'd':                    /* debug level */
104             debug_level = atoi(optarg);
105             if (debug_level <= 0) {
106                debug_level = 1; 
107             }
108             break;
109
110          case 'f':                    /* run in foreground */
111             foreground = TRUE;
112             break;
113
114          case 's':
115             no_signals = TRUE;
116             break;
117
118          case 't':
119             test_config = TRUE;
120             break;
121
122          case '?':
123          default:
124             usage();
125
126       }  
127    }
128    argc -= optind;
129    argv += optind;
130
131    if (argc) {
132       if (configfile != NULL)
133          free(configfile);
134       configfile = bstrdup(*argv);
135       argc--; 
136       argv++;
137    }
138    if (argc) {
139       usage();
140    }
141
142    if (!no_signals) {
143       init_signals(terminate_filed);
144    }
145
146    if (configfile == NULL) {
147       configfile = bstrdup(CONFIG_FILE);
148    }
149
150    parse_config(configfile);
151
152    LockRes();
153    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
154    UnlockRes();
155    if (!director) {
156       Emsg1(M_ABORT, 0, _("No Director resource defined in %s\n"),
157          configfile);
158    }
159
160    LockRes();
161    me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
162    UnlockRes();
163    if (!me) {
164       Emsg1(M_ABORT, 0, _("No File daemon resource defined in %s\n\
165 Without that I don't know who I am :-(\n"), configfile);
166    } else {
167       my_name_is(0, NULL, me->hdr.name);
168       if (!me->messages) {
169          LockRes();
170          me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
171          UnlockRes();
172          if (!me->messages) {
173              Emsg1(M_ABORT, 0, _("No Messages resource defined in %s\n"), configfile);
174          }
175       }
176       close_msg(NULL);                /* close temp message handler */
177       init_msg(NULL, me->messages);   /* open user specified message handler */
178    }
179    working_directory = me->working_directory;
180
181    if (test_config) {
182       terminate_filed(0);
183    }
184
185    if (!foreground) {
186       daemon_start();
187       init_stack_dump();              /* set new pid */
188    }
189
190    create_pid_file(me->pid_directory, "bacula-fd", me->FDport);
191
192 #ifdef BOMB
193    me += 1000000;
194 #endif
195
196    set_thread_concurrency(10);
197
198    start_watchdog();                  /* start watchdog thread */
199
200    /* Become server, and handle requests */
201    Dmsg1(10, "filed: listening on port %d\n", me->FDport);
202    bnet_thread_server(me->FDaddr, me->FDport, me->MaxConcurrentJobs, 
203                       &dir_workq, handle_client_request);
204
205    exit(0);                           /* should never get here */
206 }
207
208 void terminate_filed(int sig)
209 {
210    stop_watchdog();
211
212    if (configfile != NULL) {
213       free(configfile);
214    }
215    if (debug_level > 5) {
216       print_memory_pool_stats(); 
217    }
218    delete_pid_file(me->pid_directory, "bacula-fd", me->FDport);
219    free_config_resources();
220    term_msg();
221    close_memory_pool();               /* release free memory in pool */
222    sm_dump(False);                    /* dump orphaned buffers */
223    exit(1);
224 }