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