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