]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/msgchan.c
Massive SD calling sequence reorganization
[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-2004 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 SpoolData=%d";
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 extern "C" void *msg_thread(void *arg);
58
59 /*
60  * Establish a message channel connection with the Storage daemon
61  * and perform authentication. 
62  */
63 bool connect_to_storage_daemon(JCR *jcr, int retry_interval,    
64                               int max_retry_time, int verbose)
65 {
66    BSOCK *sd;
67    STORE *store = jcr->store;
68    if (!store) {
69       store = (STORE *)jcr->storage[0]->first();
70    }
71
72    /*
73     *  Open message channel with the Storage daemon   
74     */
75    Dmsg2(200, "bnet_connect to Storage daemon %s:%d\n", store->address,
76       store->SDport);
77    sd = bnet_connect(jcr, retry_interval, max_retry_time,
78           _("Storage daemon"), store->address, 
79           NULL, store->SDport, verbose);
80    if (sd == NULL) {
81       return false;
82    }
83    sd->res = (RES *)store;        /* save pointer to other end */
84    jcr->store_bsock = sd;
85    jcr->store = store;
86
87    if (!authenticate_storage_daemon(jcr, store)) {
88       return false;
89    }
90    return true;
91 }
92
93 /*
94  * Start a job with the Storage daemon
95  */
96 int start_storage_daemon_job(JCR *jcr)
97 {
98    int status;
99    STORE *storage;
100    BSOCK *sd;
101    char auth_key[100];
102    POOL_MEM device_name, pool_name, pool_type, media_type;
103
104    storage = jcr->store;
105    sd = jcr->store_bsock;
106    /*
107     * Now send JobId and permissions, and get back the authorization key.
108     */
109    bash_spaces(jcr->job->hdr.name);
110    bash_spaces(jcr->client->hdr.name);
111    bash_spaces(jcr->fileset->hdr.name);
112    if (jcr->fileset->MD5[0] == 0) {
113       strcpy(jcr->fileset->MD5, "**Dummy**");
114    }
115    bnet_fsend(sd, jobcmd, jcr->JobId, jcr->Job, jcr->job->hdr.name, 
116               jcr->client->hdr.name, jcr->JobType, jcr->JobLevel, 
117               jcr->fileset->hdr.name, !jcr->pool->catalog_files,
118               jcr->job->SpoolAttributes, jcr->fileset->MD5, jcr->spool_data);
119    Dmsg1(200, "Jobcmd=%s\n", sd->msg);
120    unbash_spaces(jcr->job->hdr.name);
121    unbash_spaces(jcr->client->hdr.name);
122    unbash_spaces(jcr->fileset->hdr.name);
123    if (bget_dirmsg(sd) > 0) {
124        Dmsg1(110, "<stored: %s", sd->msg);
125        if (sscanf(sd->msg, OKjob, &jcr->VolSessionId, 
126                   &jcr->VolSessionTime, &auth_key) != 3) {
127           Dmsg1(100, "BadJob=%s\n", sd->msg);
128           Jmsg(jcr, M_FATAL, 0, _("Storage daemon rejected Job command: %s\n"), sd->msg);
129           return 0;
130        } else {
131           jcr->sd_auth_key = bstrdup(auth_key);
132           Dmsg1(150, "sd_auth_key=%s\n", jcr->sd_auth_key);
133        }
134    } else {
135       Jmsg(jcr, M_FATAL, 0, _("<stored: bad response to Job command: %s\n"),
136          bnet_strerror(sd));
137       return 0;
138    }
139
140    /*
141     * Send use device = xxx media = yyy pool = zzz
142     */
143    pm_strcpy(device_name, storage->dev_name);
144    pm_strcpy(media_type, storage->media_type);
145    pm_strcpy(pool_type, jcr->pool->pool_type);
146    pm_strcpy(pool_name, jcr->pool->hdr.name);
147    bash_spaces(device_name.c_str());
148    bash_spaces(media_type.c_str());
149    bash_spaces(pool_type.c_str());
150    bash_spaces(pool_name.c_str());
151    bnet_fsend(sd, use_device, device_name.c_str(), 
152               media_type.c_str(), pool_name.c_str(), pool_type.c_str());
153    Dmsg1(110, ">stored: %s", sd->msg);
154    status = response(jcr, sd, OK_device, "Use Device", NO_DISPLAY);
155    if (!status) {
156       pm_strcpy(pool_type, sd->msg); /* save message */
157       Jmsg(jcr, M_FATAL, 0, _("\n"
158          "     Storage daemon didn't accept Device \"%s\" because:\n     %s"),
159          device_name.c_str(), pool_type.c_str()/* sd->msg */);
160    }
161    return status;
162 }
163
164 /* 
165  * Start a thread to handle Storage daemon messages and
166  *  Catalog requests.
167  */
168 int start_storage_daemon_message_thread(JCR *jcr)
169 {
170    int status;
171    pthread_t thid;
172
173    P(jcr->mutex);
174    jcr->use_count++;                  /* mark in use by msg thread */
175    jcr->sd_msg_thread_done = false;
176    jcr->SD_msg_chan = 0;
177    V(jcr->mutex);
178    Dmsg0(100, "Start SD msg_thread.\n");
179    if ((status=pthread_create(&thid, NULL, msg_thread, (void *)jcr)) != 0) {
180       Jmsg1(jcr, M_ABORT, 0, _("Cannot create message thread: %s\n"), strerror(status));
181    }         
182    Dmsg0(100, "SD msg_thread started.\n");
183    /* Wait for thread to start */
184    while (jcr->SD_msg_chan == 0) {
185       bmicrosleep(0, 50);
186    }  
187    return 1;
188 }
189
190 extern "C" void msg_thread_cleanup(void *arg)
191 {
192    JCR *jcr = (JCR *)arg;
193    Dmsg0(200, "End msg_thread\n");
194    db_end_transaction(jcr, jcr->db);       /* terminate any open transaction */
195    P(jcr->mutex);
196    jcr->sd_msg_thread_done = true;
197    pthread_cond_broadcast(&jcr->term_wait); /* wakeup any waiting threads */
198    jcr->SD_msg_chan = 0;
199    V(jcr->mutex);
200    free_jcr(jcr);                     /* release jcr */
201 }
202
203 /*
204  * Handle the message channel (i.e. requests from the
205  *  Storage daemon).
206  * Note, we are running in a separate thread.
207  */
208 extern "C"
209 void *msg_thread(void *arg)
210 {
211    JCR *jcr = (JCR *)arg;
212    BSOCK *sd;
213    int JobStatus;
214    char Job[MAX_NAME_LENGTH];
215    uint32_t JobFiles;
216    uint64_t JobBytes;
217    int stat;
218
219    pthread_detach(pthread_self());
220    jcr->SD_msg_chan = pthread_self();
221    pthread_cleanup_push(msg_thread_cleanup, arg);
222    sd = jcr->store_bsock;
223
224    /* Read the Storage daemon's output.
225     */
226    Dmsg0(100, "Start msg_thread loop\n");
227    while ((stat=bget_dirmsg(sd)) >= 0) {
228       Dmsg1(200, "<stored: %s", sd->msg);
229       if (sscanf(sd->msg, Job_start, &Job) == 1) {
230          continue;
231       }
232       if (sscanf(sd->msg, Job_end, &Job, &JobStatus, &JobFiles,
233                  &JobBytes) == 4) {
234          jcr->SDJobStatus = JobStatus; /* termination status */
235          jcr->SDJobFiles = JobFiles;
236          jcr->SDJobBytes = JobBytes;
237          break;
238       }     
239       if (sscanf(sd->msg, Job_status, &Job, &JobStatus) == 2) {
240          jcr->SDJobStatus = JobStatus; /* current status */
241          continue;
242       }
243    }
244    if (is_bnet_error(sd)) {                   
245       jcr->SDJobStatus = JS_ErrorTerminated;
246    }
247    pthread_cleanup_pop(1);
248    return NULL;
249 }
250
251 void wait_for_storage_daemon_termination(JCR *jcr)
252 {
253    int cancel_count = 0;
254    /* Now wait for Storage daemon to terminate our message thread */
255    set_jcr_job_status(jcr, JS_WaitSD);
256    P(jcr->mutex);
257    while (!jcr->sd_msg_thread_done) {
258       struct timeval tv;
259       struct timezone tz;
260       struct timespec timeout;
261
262       gettimeofday(&tv, &tz);
263       timeout.tv_nsec = 0;
264       timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
265       Dmsg0(300, "I'm waiting for message thread termination.\n");
266       pthread_cond_timedwait(&jcr->term_wait, &jcr->mutex, &timeout);
267       if (job_canceled(jcr)) {
268          cancel_count++;
269       }
270       /* Give SD 30 seconds to clean up after cancel */
271       if (cancel_count == 3) {
272          break;
273       }
274    }
275    V(jcr->mutex);
276    set_jcr_job_status(jcr, JS_Terminated);
277 }