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