]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed.c
7e7ee3be1dd3f1beabf1fa7c4428cb6742161010
[bacula/bacula] / bacula / src / filed / filed.c
1 /*
2  *  Bacula File Daemon
3  *
4  *    Kern Sibbald, March MM
5  *
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "filed.h"
29
30 /* Imported Functions */
31 extern void handle_client_request(void *dir_sock);
32
33 /* Forward referenced functions */
34 void terminate_filed(int sig);
35
36 /* Exported variables */
37
38
39 #ifdef HAVE_CYGWIN
40 int win32_client = 1;
41 #else
42 int win32_client = 0;
43 #endif
44
45
46 #define CONFIG_FILE "./bacula-fd.conf" /* default config file */
47
48 static char *configfile = NULL;
49 static int foreground = 0;
50 static workq_t dir_workq;             /* queue of work from Director */
51
52 static void usage()
53 {
54    fprintf(stderr, _(
55 "\nVersion: " VERSION " (" DATE ")\n\n"
56 "Usage: filed [-f -s] [-c config_file] [-d debug_level] [config_file]\n"  
57 "        -c <file>   use <file> as configuration file\n"
58 "        -dnn        set debug level to nn\n"
59 "        -f          run in foreground (for debugging)\n"
60 "        -s          no signals (for debugging)\n"
61 "        -t          test configuration file and exit\n"
62 "        -?          print this message.\n"
63 "\n"));         
64    exit(1);
65 }
66
67
68 /********************************************************************* 
69  *
70  *  Main Bacula Unix Client Program                        
71  *
72  */
73 #ifdef HAVE_CYGWIN
74 #define main BaculaMain
75 #endif
76 int main (int argc, char *argv[])
77 {
78    int ch;
79    int no_signals = FALSE;
80    int test_config = FALSE;
81    CLIENT *me;                        /* my resource */
82    DIRRES *director;
83
84    init_stack_dump();
85    my_name_is(argc, argv, "filed");
86    daemon_start_time = time(NULL);
87
88    memset(&last_job, 0, sizeof(last_job));
89
90    while ((ch = getopt(argc, argv, "c:d:fst?")) != -1) {
91       switch (ch) {
92          case 'c':                    /* configuration file */
93             if (configfile != NULL) {
94                free(configfile);
95             }
96             configfile = bstrdup(optarg);
97             break;
98
99          case 'd':                    /* debug level */
100             debug_level = atoi(optarg);
101             if (debug_level <= 0) {
102                debug_level = 1; 
103             }
104             break;
105
106          case 'f':                    /* run in foreground */
107             foreground = TRUE;
108             break;
109
110          case 's':
111             no_signals = TRUE;
112             break;
113
114          case 't':
115             test_config = TRUE;
116             break;
117
118          case '?':
119          default:
120             usage();
121
122       }  
123    }
124    argc -= optind;
125    argv += optind;
126
127    if (argc) {
128       if (configfile != NULL)
129          free(configfile);
130       configfile = bstrdup(*argv);
131       argc--; 
132       argv++;
133    }
134    if (argc) {
135       usage();
136    }
137
138    if (!no_signals) {
139       init_signals(terminate_filed);
140    }
141
142    if (configfile == NULL) {
143       configfile = bstrdup(CONFIG_FILE);
144    }
145
146
147    init_msg(NULL);
148    parse_config(configfile);
149
150    LockRes();
151    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
152    UnlockRes();
153    if (!director) {
154       Emsg1(M_ABORT, 0, _("No Director resource defined in %s\n"),
155          configfile);
156    }
157
158    LockRes();
159    me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
160    UnlockRes();
161    if (!me) {
162       Emsg1(M_ABORT, 0, _("No File daemon resource defined in %s\n\
163 Without that I don't know who I am :-(\n"), configfile);
164    } else {
165       my_name_is(0, (char **)NULL, me->hdr.name);
166    }
167    working_directory = me->working_directory;
168
169    if (test_config) {
170       terminate_filed(0);
171    }
172
173    if (!foreground) {
174       daemon_start();
175       init_stack_dump();              /* set new pid */
176    }
177
178 #ifdef BOMB
179    me += 1000000;
180 #endif
181
182    init_watchdog();                   /* start watchdog thread */
183
184    /* Become server, and handle requests */
185    Dmsg1(10, "filed: listening on port %d\n", me->FDport);
186    bnet_thread_server(me->FDport, 10, &dir_workq, handle_client_request);
187
188    exit(0);                           /* should never get here */
189 }
190
191 void terminate_filed(int sig)
192 {
193    term_watchdog();
194
195    if (configfile != NULL) {
196       free(configfile);
197    }
198    if (debug_level > 5) {
199       print_memory_pool_stats(); 
200    }
201    free_config_resources();
202    close_memory_pool();               /* free memory in pool */
203    term_msg();
204    sm_dump(False);                    /* dump orphaned buffers */
205    exit(1);
206 }