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