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