]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
Initial revision
[bacula/bacula] / bacula / src / stored / job.c
1 /*
2  *   Job control and execution for Storage Daemon
3  *
4  */
5 /*
6    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23  */
24
25 #include "bacula.h"
26 #include "stored.h"
27
28 /* Imported variables */
29 extern uint32_t VolSessionTime;
30
31 /* Imported functions */
32 extern uint32_t newVolSessionId();
33
34 /* Forward referenced functions */
35 static int use_device_cmd(JCR *jcr);
36
37 /* Requests from the Director daemon */
38 static char jobcmd[]     = "JobId=%d job=%127s job_name=%127s client_name=%127s Allow=";
39 static char use_device[] = "use device=%s media_type=%s pool_name=%s pool_type=%s\n";
40
41 /* Responses sent to Director daemon */
42 static char OKjob[]     = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
43 static char OK_device[] = "3000 OK use device\n";
44 static char NO_device[] = "3904 Device not available\n";
45 static char BAD_use[]   = "3903 Bad use command\n";
46 static char BAD_job[]   = "3905 Bad Job command\n";
47
48
49
50 /*
51  * Director requests us to start a job
52  * Basic tasks done here:
53  *  - We pickup the JobId to be run from the Director.
54  *  - We pickup the device, media, and pool from the Director
55  *  - Wait for a connection from the File Daemon (FD)
56  *  - Accept commands from the FD (i.e. run the job)
57  *  - Return when the connection is terminated or
58  *    there is an error.
59  */
60 int job_cmd(JCR *jcr)
61 {
62    int JobId, errstat;
63    char auth_key[100];
64    BSOCK *dir = jcr->dir_bsock;
65    char *job_name, *client_name, *job;
66    struct timeval tv;
67    struct timezone tz;
68    struct timespec timeout;
69
70    /*
71     * Get JobId and permissions from Director
72     */
73
74    Dmsg1(30, "Job_cmd: %s\n", dir->msg);
75    job = (char *) get_memory(dir->msglen);
76    job_name = (char *) get_memory(dir->msglen);
77    client_name = (char *)get_memory(dir->msglen);
78    if (sscanf(dir->msg, jobcmd, &JobId, job, job_name, client_name) != 4) {
79       bnet_fsend(dir, BAD_job);
80       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), dir->msg);
81       free_memory(job);
82       free_memory(job_name);
83       free_memory(client_name);
84       jcr->JobStatus = JS_ErrorTerminated;
85       return 0;
86    }
87    jcr->JobId = JobId;
88    jcr->VolSessionId = newVolSessionId();
89    jcr->VolSessionTime = VolSessionTime;
90    strcpy(jcr->Job, job);
91    unbash_spaces(job_name);
92    jcr->job_name = (char *) get_memory(strlen(job_name) + 1);
93    strcpy(jcr->job_name, job_name);
94    unbash_spaces(client_name);
95    jcr->client_name = bstrdup(client_name);
96    free_memory(job);
97    free_memory(job_name);
98    free_memory(client_name);
99
100    /* Initialize FD start condition variable */
101    if ((errstat = pthread_cond_init(&jcr->job_start_wait, NULL)) != 0) {
102       Emsg1(M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
103       jcr->JobStatus = JS_ErrorTerminated;
104       return 0;
105    }
106    jcr->authenticated = FALSE;
107
108    /*
109     * Pass back an authorization key for the File daemon
110     */
111    gettimeofday(&tv, &tz);
112    srandom(tv.tv_usec + tv.tv_sec);
113    sprintf(auth_key, "%ld", (long)random());
114    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
115    Dmsg1(10, ">dird: %s", dir->msg);
116    jcr->sd_auth_key = bstrdup(auth_key);
117
118    /*
119     * Wait for the device, media, and pool information
120     */
121    if (!use_device_cmd(jcr)) {
122       jcr->JobStatus = JS_ErrorTerminated;
123       return 0;
124    }
125
126    jcr->JobStatus = JS_WaitFD;        /* wait for FD to connect */
127    dir_send_job_status(jcr);
128
129    gettimeofday(&tv, &tz);
130    timeout.tv_nsec = tv.tv_usec * 1000; 
131    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
132
133
134    Dmsg1(200, "%s waiting on job_start_wait\n", jcr->Job);
135    P(jcr->mutex);
136    for ( ;!job_cancelled(jcr); ) {
137       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
138       if (errstat == 0 || errstat == ETIMEDOUT) {
139          break;
140       }
141    }
142    V(jcr->mutex);
143
144    if (jcr->authenticated && !job_cancelled(jcr)) {
145       run_job(jcr);                   /* Run the job */
146    }
147    return 0;
148 }
149
150 /*
151  * This entry point is only called if we have a separate
152  *   Storage Daemon Data port. Otherwise, the connection
153  *   is made to the main port, and if it is a File daemon
154  *   calling, handl_filed_connection() is called directly.
155  */
156 void connection_from_filed(void *arg)
157 {
158    BSOCK *fd = (BSOCK *)arg;
159    char job_name[MAX_NAME_LENGTH];
160
161    Dmsg0(200, "enter connection_from_filed\n");
162    if (bnet_recv(fd) <= 0) {
163       Emsg0(M_ERROR, 0, "Unable to authenticate Client.\n");
164       return;
165    }
166    Dmsg1(100, "got: %s\n", fd->msg);
167
168    if (sscanf(fd->msg, "Hello Start Job %127s\n", job_name) != 1) {
169       Emsg1(M_ERROR, 0, _("Authentication failure: %s"), fd->msg);
170       return;
171    }
172    handle_filed_connection(fd, job_name);
173    return;
174 }
175
176 void handle_filed_connection(BSOCK *fd, char *job_name)
177 {
178    JCR *jcr;
179
180    if (!(jcr=get_jcr_by_full_name(job_name))) {
181       Emsg1(M_ERROR, 0, "Job name not found: %s", job_name);
182       return;
183    }
184
185    jcr->file_bsock = fd;
186
187    Dmsg1(100, "Found Job %s\n", job_name);
188
189    if (jcr->authenticated) {
190       Dmsg2(000, "Hey!!!! JobId %d Job %s already authenticated.\n", 
191          jcr->JobId, jcr->Job);
192    }
193   
194    /*
195     * Authenticate the File daemon
196     */
197    if (jcr->authenticated || !authenticate_filed(jcr)) {
198       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
199       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
200    } else {
201       jcr->authenticated = TRUE;
202       Dmsg1(100, "OK Authentication Job %s\n", jcr->Job);
203    }
204
205    P(jcr->mutex);
206    if (!jcr->authenticated) {
207       jcr->JobStatus = JS_ErrorTerminated;
208    }
209    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
210    V(jcr->mutex);
211    free_jcr(jcr);
212    return;
213 }
214
215
216 /*  
217  *   Use Device command from Director
218  *   He tells is what Device Name to use, the Media Type, 
219  *      the Pool Name, and the Pool Type.
220  *
221  *    Ensure that the device exists and is opened, then store
222  *      the media and pool info in the JCR.
223  */
224 static int use_device_cmd(JCR *jcr)
225 {
226    char *dev_name, *media_type, *pool_name, *pool_type;
227    BSOCK *dir = jcr->dir_bsock;
228    DEVRES *device;
229
230    if (bnet_recv(dir) <= 0) {
231       Emsg0(M_FATAL, 0, "No Device from Director\n");
232       return 0;
233    }
234    
235    Dmsg1(20, "Use device: %s", dir->msg);
236    dev_name = (char *) get_memory(dir->msglen);
237    media_type = (char *) get_memory(dir->msglen);
238    pool_name = (char *) get_memory(dir->msglen);
239    pool_type = (char *) get_memory(dir->msglen);
240    if (sscanf(dir->msg, use_device, dev_name, media_type, pool_name, pool_type) == 4) {
241       unbash_spaces(dev_name);
242       unbash_spaces(media_type);
243       unbash_spaces(pool_name);
244       unbash_spaces(pool_type);
245       device = NULL;
246       LockRes();
247       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
248          /* Find resource, and make sure we were able to open it */
249          if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
250             Dmsg1(20, "Found device %s\n", device->hdr.name);
251             jcr->pool_name = (char *) get_memory(strlen(pool_name) + 1);
252             strcpy(jcr->pool_name, pool_name);
253             jcr->pool_type = (char *) get_memory(strlen(pool_type) + 1);
254             strcpy(jcr->pool_type, pool_type);
255             jcr->media_type = (char *) get_memory(strlen(media_type) + 1);
256             strcpy(jcr->media_type, media_type);
257             jcr->dev_name = (char *) get_memory(strlen(dev_name) + 1);
258             strcpy(jcr->dev_name, dev_name);
259             jcr->device = device;
260             Dmsg4(20, use_device, dev_name, media_type, pool_name, pool_type);
261             free_memory(dev_name);
262             free_memory(media_type);
263             free_memory(pool_name);
264             free_memory(pool_type);
265             UnlockRes();
266             return bnet_fsend(dir, OK_device);
267          }
268       }
269       UnlockRes();
270       Jmsg(jcr, M_FATAL, 0, _("Requested device %s not found. Cannot continue.\n"),
271            dev_name);
272       bnet_fsend(dir, NO_device);
273    } else {
274       Emsg1(M_FATAL, 0, _("store<dir: Bad Use Device command: %s\n"), dir->msg);
275       bnet_fsend(dir, BAD_use);
276    }
277
278    free_memory(dev_name);
279    free_memory(media_type);
280    free_memory(pool_name);
281    free_memory(pool_type);
282    return 0;                          /* ERROR return */
283 }
284
285 /* 
286  * Destroy the Job Control Record and associated
287  * resources (sockets).
288  */
289 void stored_free_jcr(JCR *jcr) 
290 {
291    if (jcr->file_bsock) {
292       bnet_close(jcr->file_bsock);
293    }
294    if (jcr->pool_name) {
295       free_memory(jcr->pool_name);
296    }
297    if (jcr->pool_type) {
298       free_memory(jcr->pool_type);
299    }
300    if (jcr->media_type) {
301       free_memory(jcr->media_type);
302    }
303    if (jcr->dev_name) {
304       free_memory(jcr->dev_name);
305    }
306    if (jcr->job_name) {
307       free_pool_memory(jcr->job_name);
308    }
309    return;
310 }