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