]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
- Correct buffer length passed to inet_ntop() in address_conf.c
[bacula/bacula] / bacula / src / stored / job.c
1 /*
2  *   Job control and execution for Storage Daemon
3  *
4  *   Kern Sibbald, 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 "stored.h"
31
32 /* Imported variables */
33 extern uint32_t VolSessionTime;
34
35 /* Imported functions */
36 extern uint32_t newVolSessionId();
37
38 /* Forward referenced functions */
39 static bool use_device_cmd(JCR *jcr);
40
41 /* Requests from the Director daemon */
42 static char jobcmd[] = "JobId=%d job=%127s job_name=%127s client_name=%127s "
43          "type=%d level=%d FileSet=%127s NoAttr=%d SpoolAttr=%d FileSetMD5=%127s "
44          "SpoolData=%d";
45 static char use_device[] = "use device=%127s media_type=%127s pool_name=%127s pool_type=%127s\n";
46
47 /* Responses sent to Director daemon */
48 static char OKjob[]     = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
49 static char OK_device[] = "3000 OK use device\n";
50 static char NO_device[] = "3914 Device \"%s\" not in SD Device resources.\n";
51 static char BAD_use[]   = "3913 Bad use command: %s\n";
52 static char BAD_job[]   = "3915 Bad Job command: %s\n";
53
54 /*
55  * Director requests us to start a job
56  * Basic tasks done here:
57  *  - We pickup the JobId to be run from the Director.
58  *  - We pickup the device, media, and pool from the Director
59  *  - Wait for a connection from the File Daemon (FD)
60  *  - Accept commands from the FD (i.e. run the job)
61  *  - Return when the connection is terminated or
62  *    there is an error.
63  */
64 bool job_cmd(JCR *jcr)
65 {
66    int JobId, errstat;
67    char auth_key[100];
68    BSOCK *dir = jcr->dir_bsock;
69    POOL_MEM job_name, client_name, job, fileset_name, fileset_md5;
70    int JobType, level, spool_attributes, no_attributes, spool_data;
71    struct timeval tv;
72    struct timezone tz;
73    struct timespec timeout;
74    JCR *ojcr;
75
76    /*
77     * Get JobId and permissions from Director
78     */
79    Dmsg1(100, "Job_cmd: %s\n", dir->msg);
80    if (sscanf(dir->msg, jobcmd, &JobId, job.c_str(), job_name.c_str(), 
81               client_name.c_str(),
82               &JobType, &level, fileset_name.c_str(), &no_attributes,
83               &spool_attributes, fileset_md5.c_str(), &spool_data) != 11) {
84       pm_strcpy(jcr->errmsg, dir->msg);
85       bnet_fsend(dir, BAD_job, jcr->errmsg);
86       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), jcr->errmsg);
87       set_jcr_job_status(jcr, JS_ErrorTerminated);
88       return false;
89    }
90    /*         
91     * Since this job could be rescheduled, we
92     *  check to see if we have it already. If so
93     *  free the old jcr and use the new one.
94     */
95    ojcr = get_jcr_by_full_name(job.c_str());
96    if (ojcr && !ojcr->authenticated) {
97       Dmsg2(100, "Found ojcr=0x%x Job %s\n", (unsigned)(long)ojcr, job.c_str());
98       free_jcr(ojcr);
99    }
100    jcr->JobId = JobId;
101    jcr->VolSessionId = newVolSessionId();
102    jcr->VolSessionTime = VolSessionTime;
103    bstrncpy(jcr->Job, job, sizeof(jcr->Job));
104    unbash_spaces(job_name);
105    jcr->job_name = get_pool_memory(PM_NAME);
106    pm_strcpy(jcr->job_name, job_name);
107    unbash_spaces(client_name);
108    jcr->client_name = get_pool_memory(PM_NAME);
109    pm_strcpy(jcr->client_name, client_name);
110    unbash_spaces(fileset_name);
111    jcr->fileset_name = get_pool_memory(PM_NAME);
112    pm_strcpy(jcr->fileset_name, fileset_name);
113    jcr->JobType = JobType;
114    jcr->JobLevel = level;
115    jcr->no_attributes = no_attributes;
116    jcr->spool_attributes = spool_attributes;
117    jcr->spool_data = spool_data;
118    jcr->fileset_md5 = get_pool_memory(PM_NAME);
119    pm_strcpy(jcr->fileset_md5, fileset_md5);
120
121    jcr->authenticated = false;
122
123    /*
124     * Pass back an authorization key for the File daemon
125     */
126    make_session_key(auth_key, NULL, 1);
127    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
128    Dmsg1(110, ">dird: %s", dir->msg);
129    jcr->sd_auth_key = bstrdup(auth_key);
130    memset(auth_key, 0, sizeof(auth_key));    
131
132    /*
133     * Wait for the device, media, and pool information
134     */
135    if (!use_device_cmd(jcr)) {
136       set_jcr_job_status(jcr, JS_ErrorTerminated);
137       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
138       return false;
139    }
140
141    /* The following jobs don't need the FD */
142    switch (jcr->JobType) {
143    case JT_MIGRATION:
144    case JT_COPY:
145    case JT_ARCHIVE:
146       jcr->authenticated = true;
147       run_job(jcr);
148       return false;
149    }
150
151    set_jcr_job_status(jcr, JS_WaitFD);          /* wait for FD to connect */
152    dir_send_job_status(jcr);
153
154    gettimeofday(&tv, &tz);
155    timeout.tv_nsec = tv.tv_usec * 1000; 
156    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
157
158    Dmsg1(100, "%s waiting on FD to contact SD\n", jcr->Job);
159    /*
160     * Wait for the File daemon to contact us to start the Job,
161     *  when he does, we will be released, unless the 30 minutes
162     *  expires.
163     */
164    P(jcr->mutex);
165    for ( ;!job_canceled(jcr); ) {
166       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
167       if (errstat == 0 || errstat == ETIMEDOUT) {
168          break;
169       }
170    }
171    V(jcr->mutex);
172
173    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
174
175    if (jcr->authenticated && !job_canceled(jcr)) {
176       Dmsg1(100, "Running job %s\n", jcr->Job);
177       run_job(jcr);                   /* Run the job */
178    }
179    return false;
180 }
181
182 /*
183  * After receiving a connection (in job.c) if it is
184  *   from the File daemon, this routine is called.
185  */  
186 void handle_filed_connection(BSOCK *fd, char *job_name)
187 {
188    JCR *jcr;
189
190    bmicrosleep(0, 50000);             /* wait 50 millisecs */
191    if (!(jcr=get_jcr_by_full_name(job_name))) {
192       Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
193       Dmsg1(100, "Job name not found: %s\n", job_name);
194       return;
195    }
196
197    jcr->file_bsock = fd;
198    jcr->file_bsock->jcr = jcr;
199
200    Dmsg1(110, "Found Job %s\n", job_name);
201
202    if (jcr->authenticated) {
203       Jmsg2(jcr, M_FATAL, 0, "Hey!!!! JobId %u Job %s already authenticated.\n", 
204          jcr->JobId, jcr->Job);
205       free_jcr(jcr);
206       return;
207    }
208   
209    /*
210     * Authenticate the File daemon
211     */
212    if (jcr->authenticated || !authenticate_filed(jcr)) {
213       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
214       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
215    } else {
216       jcr->authenticated = true;
217       Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
218    }
219
220    P(jcr->mutex);
221    if (!jcr->authenticated) {
222       set_jcr_job_status(jcr, JS_ErrorTerminated);
223    }
224    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
225    V(jcr->mutex);
226    free_jcr(jcr);
227    return;
228 }
229
230
231 /*  
232  *   Use Device command from Director
233  *   He tells is what Device Name to use, the Media Type, 
234  *      the Pool Name, and the Pool Type.
235  *
236  *    Ensure that the device exists and is opened, then store
237  *      the media and pool info in the JCR.
238  */
239 static bool use_device_cmd(JCR *jcr)
240 {
241    POOL_MEM dev_name, media_type, pool_name, pool_type;
242    BSOCK *dir = jcr->dir_bsock;
243    DEVRES *device;
244
245    if (bnet_recv(dir) <= 0) {
246       Jmsg0(jcr, M_FATAL, 0, _("No Device from Director\n"));
247       return false;
248    }
249    
250    Dmsg1(120, "Use device: %s", dir->msg);
251    if (sscanf(dir->msg, use_device, dev_name.c_str(), media_type.c_str(), 
252               pool_name.c_str(), pool_type.c_str()) == 4) {
253       unbash_spaces(dev_name);
254       unbash_spaces(media_type);
255       unbash_spaces(pool_name);
256       unbash_spaces(pool_type);
257       LockRes();
258       foreach_res(device, R_DEVICE) {
259          /* Find resource, and make sure we were able to open it */
260          if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0 && 
261              device->dev && strcmp(device->media_type, media_type.c_str()) == 0) {
262             const int name_len = MAX_NAME_LENGTH;
263             DCR *dcr;
264             UnlockRes();
265             dcr = new_dcr(jcr, device->dev);
266             if (!dcr) {
267                return false;
268             }
269             Dmsg1(120, "Found device %s\n", device->hdr.name);
270             bstrncpy(dcr->pool_name, pool_name, name_len);
271             bstrncpy(dcr->pool_type, pool_type, name_len);
272             bstrncpy(dcr->media_type, media_type, name_len);
273             bstrncpy(dcr->dev_name, dev_name, name_len);
274             jcr->device = device;
275             Dmsg1(220, "Got: %s", dir->msg);
276             return bnet_fsend(dir, OK_device);
277          }
278       }
279       UnlockRes();
280       if (verbose) {
281          unbash_spaces(dir->msg);
282          pm_strcpy(jcr->errmsg, dir->msg);
283          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
284       }
285       Jmsg(jcr, M_FATAL, 0, _("\n"
286          "     Device \"%s\" with MediaType \"%s\" requested by Dir not found in SD Device resources.\n"),
287            dev_name.c_str(), media_type.c_str());
288       bnet_fsend(dir, NO_device, dev_name.c_str());
289    } else {
290       unbash_spaces(dir->msg);
291       pm_strcpy(jcr->errmsg, dir->msg);
292       if (verbose) {
293          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
294       }
295       Jmsg(jcr, M_FATAL, 0, _("Bad Use Device command: %s\n"), jcr->errmsg);
296       bnet_fsend(dir, BAD_use, jcr->errmsg);
297    }
298
299    return false;                      /* ERROR return */
300 }
301
302 /* 
303  * Destroy the Job Control Record and associated
304  * resources (sockets).
305  */
306 void stored_free_jcr(JCR *jcr) 
307 {
308    if (jcr->file_bsock) {
309       bnet_close(jcr->file_bsock);
310       jcr->file_bsock = NULL;
311    }
312    if (jcr->job_name) {
313       free_pool_memory(jcr->job_name);
314    }
315    if (jcr->client_name) {
316       free_memory(jcr->client_name);
317       jcr->client_name = NULL;
318    }
319    if (jcr->fileset_name) {
320       free_memory(jcr->fileset_name);
321    }
322    if (jcr->fileset_md5) {
323       free_memory(jcr->fileset_md5);
324    }
325    if (jcr->bsr) {
326       free_bsr(jcr->bsr);
327       jcr->bsr = NULL;
328    }
329    if (jcr->RestoreBootstrap) {
330       unlink(jcr->RestoreBootstrap);
331       free_pool_memory(jcr->RestoreBootstrap);
332       jcr->RestoreBootstrap = NULL;
333    }
334    if (jcr->next_dev || jcr->prev_dev) {
335       Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
336    }
337    pthread_cond_destroy(&jcr->job_start_wait);
338    if (jcr->dcr) {
339       free_dcr(jcr->dcr);
340       jcr->dcr = NULL;
341    }
342    return;
343 }