]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/msgchan.c
Add jcr to DB arguments
[bacula/bacula] / bacula / src / dird / msgchan.c
1 /*
2  *
3  *   Bacula Director -- msgchan.c -- handles the message channel
4  *    to the Storage daemon and the File daemon.
5  *
6  *     Kern Sibbald, August MM
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *  Basic tasks done here:
11  *    Open a message channel with the Storage daemon
12  *      to authenticate ourself and to pass the JobId.
13  *    Create a thread to interact with the Storage daemon
14  *      who returns a job status and requests Catalog services, etc.
15  *
16  *   Version $Id$
17  */
18 /*
19    Copyright (C) 2000-2003 Kern Sibbald and John Walker
20
21    This program is free software; you can redistribute it and/or
22    modify it under the terms of the GNU General Public License as
23    published by the Free Software Foundation; either version 2 of
24    the License, or (at your option) any later version.
25
26    This program is distributed in the hope that it will be useful,
27    but WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29    General Public License for more details.
30
31    You should have received a copy of the GNU General Public
32    License along with this program; if not, write to the Free
33    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34    MA 02111-1307, USA.
35
36  */
37
38 #include "bacula.h"
39 #include "dird.h"
40
41 /* Commands sent to Storage daemon */
42 static char jobcmd[]     = "JobId=%d job=%s job_name=%s client_name=%s \
43 type=%d level=%d FileSet=%s NoAttr=%d SpoolAttr=%d FileSetMD5=%s\n";
44 static char use_device[] = "use device=%s media_type=%s pool_name=%s pool_type=%s\n";
45
46 /* Response from Storage daemon */
47 static char OKjob[]      = "3000 OK Job SDid=%d SDtime=%d Authorization=%100s\n";
48 static char OK_device[]  = "3000 OK use device\n";
49
50 /* Storage Daemon requests */
51 static char Job_start[]  = "3010 Job %127s start\n";
52 static char Job_end[]    = 
53    "3099 Job %127s end JobStatus=%d JobFiles=%d JobBytes=%" lld "\n";
54 static char Job_status[] = "3012 Job %127s jobstatus %d\n";
55
56 /* Forward referenced functions */
57 static void *msg_thread(void *arg);
58
59 /*
60  * Establish a message channel connection with the Storage daemon
61  * and perform authentication. 
62  */
63 int connect_to_storage_daemon(JCR *jcr, int retry_interval,    
64                               int max_retry_time, int verbose)
65 {
66    BSOCK *sd;
67
68    /*
69     *  Open message channel with the Storage daemon   
70     */
71    Dmsg2(200, "bnet_connect to Storage daemon %s:%d\n", jcr->store->address,
72       jcr->store->SDport);
73    sd = bnet_connect(jcr, retry_interval, max_retry_time,
74           _("Storage daemon"), jcr->store->address, 
75           NULL, jcr->store->SDport, verbose);
76    if (sd == NULL) {
77       return 0;
78    }
79    sd->res = (RES *)jcr->store;        /* save pointer to other end */
80    jcr->store_bsock = sd;
81
82    if (!authenticate_storage_daemon(jcr)) {
83       return 0;
84    }
85    return 1;
86 }
87
88 /*
89  * Start a job with the Storage daemon
90  */
91 int start_storage_daemon_job(JCR *jcr)
92 {
93    int status;
94    STORE *storage;
95    BSOCK *sd;
96    char auth_key[100];
97    POOLMEM *device_name, *pool_name, *pool_type, *media_type;
98    int device_name_len, pool_name_len, pool_type_len, media_type_len;
99
100    storage = jcr->store;
101    sd = jcr->store_bsock;
102    /*
103     * Now send JobId and permissions, and get back the authorization key.
104     */
105    bash_spaces(jcr->job->hdr.name);
106    bash_spaces(jcr->client->hdr.name);
107    bash_spaces(jcr->fileset->hdr.name);
108    if (jcr->fileset->MD5[0] == 0) {
109       strcpy(jcr->fileset->MD5, "**Dummy**");
110    }
111    bnet_fsend(sd, jobcmd, jcr->JobId, jcr->Job, jcr->job->hdr.name, 
112               jcr->client->hdr.name, jcr->JobType, jcr->JobLevel, 
113               jcr->fileset->hdr.name, !jcr->pool->catalog_files,
114               jcr->job->SpoolAttributes, jcr->fileset->MD5);
115    Dmsg1(200, "Jobcmd=%s\n", sd->msg);
116    unbash_spaces(jcr->job->hdr.name);
117    unbash_spaces(jcr->client->hdr.name);
118    unbash_spaces(jcr->fileset->hdr.name);
119    if (bnet_recv(sd) > 0) {
120        Dmsg1(110, "<stored: %s", sd->msg);
121        if (sscanf(sd->msg, OKjob, &jcr->VolSessionId, 
122                   &jcr->VolSessionTime, &auth_key) != 3) {
123           Dmsg1(100, "BadJob=%s\n", sd->msg);
124           Jmsg(jcr, M_FATAL, 0, _("Storage daemon rejected Job command: %s\n"), sd->msg);
125           return 0;
126        } else {
127           jcr->sd_auth_key = bstrdup(auth_key);
128           Dmsg1(150, "sd_auth_key=%s\n", jcr->sd_auth_key);
129        }
130    } else {
131       Jmsg(jcr, M_FATAL, 0, _("<stored: bad response to Job command: %s\n"),
132          bnet_strerror(sd));
133       return 0;
134    }
135
136    /*
137     * Send use device = xxx media = yyy pool = zzz
138     */
139    device_name_len = strlen(storage->dev_name) + 1;
140    media_type_len = strlen(storage->media_type) + 1;
141    pool_type_len = strlen(jcr->pool->pool_type) + 1;
142    pool_name_len = strlen(jcr->pool->hdr.name) + 1;
143    device_name = get_memory(device_name_len);
144    pool_name = get_memory(pool_name_len);
145    pool_type = get_memory(pool_type_len);
146    media_type = get_memory(media_type_len);
147    memcpy(device_name, storage->dev_name, device_name_len);
148    memcpy(media_type, storage->media_type, media_type_len);
149    memcpy(pool_type, jcr->pool->pool_type, pool_type_len);
150    memcpy(pool_name, jcr->pool->hdr.name, pool_name_len);
151    bash_spaces(device_name);
152    bash_spaces(media_type);
153    bash_spaces(pool_type);
154    bash_spaces(pool_name);
155    sd->msg = check_pool_memory_size(sd->msg, sizeof(device_name) +
156       device_name_len + media_type_len + pool_type_len + pool_name_len);
157    bnet_fsend(sd, use_device, device_name, media_type, pool_name, pool_type);
158    Dmsg1(110, ">stored: %s", sd->msg);
159    status = response(sd, OK_device, "Use Device");
160
161    free_memory(device_name);
162    free_memory(media_type);
163    free_memory(pool_name);
164    free_memory(pool_type);
165
166    return status;
167 }
168
169 /* 
170  * Start a thread to handle Storage daemon messages and
171  *  Catalog requests.
172  */
173 int start_storage_daemon_message_thread(JCR *jcr)
174 {
175    int status;
176    pthread_t thid;
177
178    P(jcr->mutex);
179    jcr->use_count++;                  /* mark in use by msg thread */
180    V(jcr->mutex);
181    if ((status=pthread_create(&thid, NULL, msg_thread, (void *)jcr)) != 0) {
182       Jmsg1(jcr, M_ABORT, 0, _("Cannot create message thread: %s\n"), strerror(status));
183    }         
184    jcr->SD_msg_chan = thid;
185    return 1;
186 }
187
188 static void msg_thread_cleanup(void *arg)
189 {
190    JCR *jcr = (JCR *)arg;
191    Dmsg0(200, "End msg_thread\n");
192    db_end_transaction(jcr, jcr->db);       /* terminate any open transaction */
193    P(jcr->mutex);
194    jcr->msg_thread_done = TRUE;
195    pthread_cond_broadcast(&jcr->term_wait); /* wakeup any waiting threads */
196    V(jcr->mutex);
197    free_jcr(jcr);                     /* release jcr */
198 }
199
200 /*
201  * Handle the message channel (i.e. requests from the
202  *  Storage daemon).
203  * Note, we are running in a separate thread.
204  */
205 static void *msg_thread(void *arg)
206 {
207    JCR *jcr = (JCR *)arg;
208    BSOCK *sd;
209    int JobStatus;
210    char Job[MAX_NAME_LENGTH];
211    uint32_t JobFiles;
212    uint64_t JobBytes;
213    int stat;
214
215    pthread_cleanup_push(msg_thread_cleanup, arg);
216    Dmsg0(200, "msg_thread\n");
217    sd = jcr->store_bsock;
218    pthread_detach(pthread_self());
219
220    /* Read the Storage daemon's output.
221     */
222    Dmsg0(200, "Start msg_thread loop\n");
223    while ((stat=bget_msg(sd, 0)) >= 0) {
224       Dmsg1(200, "<stored: %s", sd->msg);
225       if (sscanf(sd->msg, Job_start, &Job) == 1) {
226          continue;
227       }
228       if (sscanf(sd->msg, Job_end, &Job, &JobStatus, &JobFiles,
229                  &JobBytes) == 4) {
230          jcr->SDJobStatus = JobStatus; /* termination status */
231          jcr->JobFiles = JobFiles;
232          jcr->JobBytes = JobBytes;
233          break;
234       }     
235       if (sscanf(sd->msg, Job_status, &Job, &JobStatus) == 2) {
236          jcr->SDJobStatus = JobStatus; /* current status */
237          continue;
238       }
239    }
240    if (is_bnet_error(sd)) {                   
241       jcr->SDJobStatus = JS_ErrorTerminated;
242    }
243    pthread_cleanup_pop(1);
244    return NULL;
245 }
246
247 void wait_for_storage_daemon_termination(JCR *jcr)
248 {
249    int cancel_count = 0;
250    /* Now wait for Storage daemon to terminate our message thread */
251    P(jcr->mutex);
252    set_jcr_job_status(jcr, JS_WaitSD);
253    while (!jcr->msg_thread_done) {
254       struct timeval tv;
255       struct timezone tz;
256       struct timespec timeout;
257
258       gettimeofday(&tv, &tz);
259       timeout.tv_nsec = 0;
260       timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
261       Dmsg0(300, "I'm waiting for message thread termination.\n");
262       pthread_cond_timedwait(&jcr->term_wait, &jcr->mutex, &timeout);
263       if (job_cancelled(jcr)) {
264          cancel_count++;
265       }
266       /* Give SD 30 seconds to clean up after cancel */
267       if (cancel_count == 3) {
268          break;
269       }
270    }
271    V(jcr->mutex);
272    set_jcr_job_status(jcr, jcr->SDJobStatus);
273 }