]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dircmd.c
Backport from BEE
[bacula/bacula] / bacula / src / stored / dircmd.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *  This file handles accepting Director Commands
18  *
19  *    Most Director commands are handled here, with the
20  *    exception of the Job command command and subsequent
21  *    subcommands that are handled
22  *    in job.c.
23  *
24  *    N.B. in this file, in general we must use P(dev->mutex) rather
25  *      than dev->rLock() so that we can examine the blocked
26  *      state rather than blocking ourselves because a Job
27  *      thread has the device blocked. In some "safe" cases,
28  *      we can do things to a blocked device. CAREFUL!!!!
29  *
30  *    File daemon commands are handled in fdcmd.c
31  *
32  *     Written by Kern Sibbald, May MMI
33  *
34  */
35
36 #include "bacula.h"
37 #include "stored.h"
38
39 /* Exported variables */
40
41 /* Imported variables */
42 extern BSOCK *filed_chan;
43 extern struct s_last_job last_job;
44 extern bool init_done;
45
46 /* Static variables */
47 static char derrmsg[]     = "3900 Invalid command:";
48 static char OKsetdebug[]  = "3000 OK setdebug=%ld trace=%ld options=%s tags=%s\n";
49 static char invalid_cmd[] = "3997 Invalid command for a Director with Monitor directive enabled.\n";
50 static char OK_bootstrap[]    = "3000 OK bootstrap\n";
51 static char ERROR_bootstrap[] = "3904 Error bootstrap\n";
52 static char OKclient[] = "3000 OK client command\n";
53
54 /* Imported functions */
55 extern void terminate_child();
56 extern bool job_cmd(JCR *jcr);
57 extern bool use_cmd(JCR *jcr);
58 extern bool run_cmd(JCR *jcr);
59 extern bool status_cmd(JCR *sjcr);
60 extern bool qstatus_cmd(JCR *jcr);
61 //extern bool query_cmd(JCR *jcr);
62
63 /* Forward referenced functions */
64 static bool client_cmd(JCR *jcr);
65 static bool storage_cmd(JCR *jcr);
66 static bool label_cmd(JCR *jcr);
67 static bool die_cmd(JCR *jcr);
68 static bool relabel_cmd(JCR *jcr);
69 static bool readlabel_cmd(JCR *jcr);
70 static bool release_cmd(JCR *jcr);
71 static bool setdebug_cmd(JCR *jcr);
72 static bool cancel_cmd(JCR *cjcr);
73 static bool mount_cmd(JCR *jcr);
74 static bool unmount_cmd(JCR *jcr);
75 //static bool action_on_purge_cmd(JCR *jcr);
76 static bool bootstrap_cmd(JCR *jcr);
77 static bool changer_cmd(JCR *sjcr);
78 static bool do_label(JCR *jcr, int relabel);
79 static DCR *find_device(JCR *jcr, POOL_MEM &dev_name,
80                         POOLMEM *media_type, int drive);
81 static void read_volume_label(JCR *jcr, DCR *dcr, DEVICE *dev, int Slot);
82 static void label_volume_if_ok(DCR *dcr, char *oldname,
83                                char *newname, char *poolname,
84                                int Slot, int relabel);
85 static bool try_autoload_device(JCR *jcr, DCR *dcr, int slot, const char *VolName);
86 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
87
88 /* Responses send to Director for storage command */
89 static char BADcmd[]  = "2902 Bad %s\n";
90 static char OKstore[] = "2000 OK storage\n";
91
92 /* Commands received from director that need scanning */
93 static char storaddr[] = "storage address=%s port=%d ssl=%d Job=%127s Authentication=%127s";
94
95 struct s_cmds {
96    const char *cmd;
97    bool (*func)(JCR *jcr);
98    bool monitoraccess;                      /* set if monitors can access this cmd */
99 };
100
101 /*
102  * The following are the recognized commands from the Director.
103  */
104 static struct s_cmds cmds[] = {
105    {"JobId=",      job_cmd,         0},     /* start Job */
106    {"autochanger", changer_cmd,     0},
107    {"bootstrap",   bootstrap_cmd,   0},
108    {"cancel",      cancel_cmd,      0},
109    {"client",      client_cmd,      0},     /* client address */
110    {".die",        die_cmd,         0},
111    {"label",       label_cmd,       0},     /* label a tape */
112    {"mount",       mount_cmd,       0},
113    {"readlabel",   readlabel_cmd,   0},
114    {"release",     release_cmd,     0},
115    {"relabel",     relabel_cmd,     0},     /* relabel a tape */
116    {"setdebug=",   setdebug_cmd,    0},     /* set debug level */
117    {"status",      status_cmd,      1},
118    {".status",     qstatus_cmd,     1},
119    {"stop",        cancel_cmd,      0},
120    {"storage",     storage_cmd,     0},     /* get SD addr from Dir */
121    {"unmount",     unmount_cmd,     0},
122    {"use storage=", use_cmd,        0},
123    {"run",         run_cmd,         0},
124 // {"query",       query_cmd,       0},
125    {NULL,        NULL}                      /* list terminator */
126 };
127
128
129 /*
130  * Connection request. We accept connections either from the
131  *  Director or a Client (File daemon).
132  *
133  * Note, we are running as a seperate thread of the Storage daemon.
134  *  and it is because a Director has made a connection with
135  *  us on the "Message" channel.
136  *
137  * Basic tasks done here:
138  *  - Create a JCR record
139  *  - If it was from the FD, call handle_filed_connection()
140  *  - Authenticate the Director
141  *  - We wait for a command
142  *  - We execute the command
143  *  - We continue or exit depending on the return status
144  */
145 void *handle_connection_request(void *arg)
146 {
147    BSOCK *bs = (BSOCK *)arg;
148    JCR *jcr;
149    int i;
150    int fd_version, sd_version;
151    bool found, quit;
152    int bnet_stat = 0;
153    char name[500];
154    char tbuf[100];
155
156    if (bs->recv() <= 0) {
157       Jmsg1(NULL, M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
158       bmicrosleep(5, 0);   /* make user wait 5 seconds */
159       bs->destroy();
160       return NULL;
161    }
162
163    /*
164     * Do a sanity check on the message received
165     */
166    if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)) {
167       Pmsg1(000, "<filed: %s", bs->msg);
168       Jmsg2(NULL, M_ERROR, 0, _("Invalid connection from %s. Len=%d\n"), bs->who(), bs->msglen);
169       bmicrosleep(5, 0);   /* make user wait 5 seconds */
170       bs->destroy();
171       return NULL;
172    }
173
174    Dmsg1(100, "Conn: %s", bs->msg);
175    fd_version = 0;
176    sd_version = 0;
177    /*
178     * See if this is a File daemon connection. If so
179     *   call FD handler.
180     */
181    if (sscanf(bs->msg, "Hello Bacula SD: Start Job %127s %d %d", name, &fd_version, &sd_version) == 3 ||
182        sscanf(bs->msg, "Hello FD: Bacula Storage calling Start Job %127s %d", name, &sd_version) == 2 ||
183        sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
184       Dmsg1(050, "Got a FD connection at %s\n", bstrftimes(tbuf, sizeof(tbuf),
185             (utime_t)time(NULL)));
186       Dmsg1(50, "%s", bs->msg);
187       handle_filed_connection(bs, name, fd_version, sd_version);
188       return NULL;
189    }
190
191    /*
192     * This is a connection from the Director, so setup a JCR
193     */
194    Dmsg1(050, "Got a DIR connection at %s\n", bstrftimes(tbuf, sizeof(tbuf),
195          (utime_t)time(NULL)));
196    jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
197    jcr->dir_bsock = bs;               /* save Director bsock */
198    jcr->dir_bsock->set_jcr(jcr);
199    jcr->dcrs = New(alist(10, not_owned_by_alist));
200    /* Initialize FD start condition variable */
201    int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
202    if (errstat != 0) {
203       berrno be;
204       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
205       goto bail_out;
206    }
207
208    Dmsg0(1000, "stored in start_job\n");
209
210    /*
211     * Authenticate the Director
212     */
213    /* We should have: Hello SD: Bacula Director <dirname> calling */
214    if (!authenticate_director(jcr)) {
215       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
216       goto bail_out;
217    }
218    Dmsg0(90, "Message channel init completed.\n");
219
220    for (quit=false; !quit;) {
221       /* Read command */
222       if ((bnet_stat = bs->recv()) <= 0) {
223          break;               /* connection terminated */
224       }
225       Dmsg1(199, "<dird: %s\n", bs->msg);
226       /* Ensure that device initialization is complete */
227       while (!init_done) {
228          bmicrosleep(1, 0);
229       }
230       found = false;
231       for (i=0; cmds[i].cmd; i++) {
232         if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
233            if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
234               Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
235               bs->fsend(invalid_cmd);
236               bs->signal(BNET_EOD);
237               break;
238            }
239            Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
240            if (!cmds[i].func(jcr)) { /* do command */
241               quit = true; /* error, get out */
242               Dmsg1(190, "Command %s requests quit\n", cmds[i].cmd);
243            }
244            found = true;             /* indicate command found */
245            break;
246         }
247       }
248       if (!found) {                   /* command not found */
249         POOL_MEM err_msg;
250         Mmsg(err_msg, "%s %s\n", derrmsg, bs->msg);
251         bs->fsend(err_msg.c_str());
252         break;
253       }
254    }
255 bail_out:
256    generate_daemon_event(jcr, "JobEnd");
257    generate_plugin_event(jcr, bsdEventJobEnd);
258    dequeue_messages(jcr);             /* send any queued messages */
259    bs->signal(BNET_TERMINATE);
260    free_plugins(jcr);                 /* release instantiated plugins */
261    free_jcr(jcr);
262    return NULL;
263 }
264
265
266 /*
267  * Force SD to die, and hopefully dump itself.  Turned on only
268  *  in development version.
269  */
270 static bool die_cmd(JCR *jcr)
271 {
272 #ifdef DEVELOPER
273    JCR *djcr = NULL;
274    int a;
275    BSOCK *dir = jcr->dir_bsock;
276    pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;
277
278    if (strstr(dir->msg, "deadlock")) {
279       Pmsg0(000, "I have been requested to deadlock ...\n");
280       P(m);
281       P(m);
282    }
283
284    Pmsg1(000, "I have been requested to die ... (%s)\n", dir->msg);
285    a = djcr->JobId;   /* ref NULL pointer */
286    djcr->JobId = a;
287 #endif
288    return 0;
289 }
290
291 /*
292  * Get address of client from Director
293  *   We attempt to connect to the client (an FD or SD) and
294  *   authenticate it.
295  */
296 static bool client_cmd(JCR *jcr)
297 {
298    int client_port;                 /* client port */
299    int enable_ssl;                 /* enable ssl */
300    BSOCK *dir = jcr->dir_bsock;
301    BSOCK *cl = new_bsock();        /* client bsock */
302
303    Dmsg1(100, "ClientCmd: %s", dir->msg);
304    jcr->sd_calls_client = true;
305    if (sscanf(dir->msg, "client address=%s port=%d ssl=%d", &jcr->client_addr, &client_port,
306               &enable_ssl) != 3) {
307       pm_strcpy(jcr->errmsg, dir->msg);
308       Jmsg(jcr, M_FATAL, 0, _("Bad client command: %s"), jcr->errmsg);
309       Dmsg1(050, "Bad client command: %s", jcr->errmsg);
310       goto bail_out;
311    }
312
313    Dmsg3(110, "Connect to client: %s:%d ssl=%d\n", jcr->client_addr, client_port,
314          enable_ssl);
315    /* Open command communications with Client */
316    /* Try to connect for 1 hour at 10 second intervals */
317    if (!cl->connect(jcr, 10, (int)me->ClientConnectTimeout, me->heartbeat_interval,
318                 _("Client daemon"), jcr->client_addr, NULL, client_port, 1)) {
319       /* destroy() OK because cl is local */
320       cl->destroy();
321       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Client daemon: %s:%d\n"),
322           jcr->client_addr, client_port);
323       Dmsg2(100, "Failed to connect to Client daemon: %s:%d\n",
324           jcr->client_addr, client_port);
325       goto bail_out;
326    }
327    Dmsg0(110, "SD connection OK to Client.\n");
328
329    /* Send Hello */
330    cl->fsend("Hello FD: Bacula Storage calling Start Job %s 1\n", jcr->Job);
331    jcr->file_bsock = cl;
332    jcr->file_bsock->set_jcr(jcr);
333    /* Send OK to Director */
334    return dir->fsend(OKclient);
335
336 bail_out:
337    jcr->setJobStatus(JS_ErrorTerminated);
338    dir->fsend("3902 Bad %s cmd\n", "client");
339    return 0;
340 }
341
342 /*
343  * Get address of storage daemon from Director
344  */
345 static bool storage_cmd(JCR *jcr)
346 {
347    int stored_port;                /* storage daemon port */
348    int enable_ssl;                 /* enable ssl to sd */
349    char sd_auth_key[200];
350    BSOCK *dir = jcr->dir_bsock;
351    BSOCK *sd = new_bsock();        /* storage daemon bsock */
352    char Job[MAX_NAME_LENGTH];
353
354    Dmsg1(050, "StorageCmd: %s", dir->msg);
355    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port,
356               &enable_ssl, Job, sd_auth_key) != 5) {
357       pm_strcpy(jcr->errmsg, dir->msg);
358       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
359       Pmsg1(010, "Bad storage command: %s", jcr->errmsg);
360       goto bail_out;
361    }
362
363    unbash_spaces(Job);
364    if (jcr->sd_auth_key) {
365       bfree_and_null(jcr->sd_auth_key);
366       jcr->sd_auth_key = bstrdup(sd_auth_key);
367    }
368    if (stored_port != 0) {
369       Dmsg2(050, "sd_calls=%d sd_client=%d\n", jcr->sd_calls_client,
370          jcr->sd_client);
371       jcr->sd_calls_client = false;   /* We are doing the connecting */
372       Dmsg3(050, "Connect to storage and wait: %s:%d ssl=%d\n", jcr->stored_addr, stored_port,
373             enable_ssl);
374       /* Open command communications with Storage daemon */
375       /* Try to connect for 1 hour at 10 second intervals */
376       if (!sd->connect(jcr, 10, (int)me->ClientConnectTimeout, me->heartbeat_interval,
377                 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
378          /* destroy() OK because sd is local */
379          sd->destroy();
380          Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
381              jcr->stored_addr, stored_port);
382          Dmsg2(010, "Failed to connect to Storage daemon: %s:%d\n",
383              jcr->stored_addr, stored_port);
384          goto bail_out;
385       }
386
387       Dmsg0(050, "Connection OK to SD.\n");
388
389       jcr->store_bsock = sd;
390    } else {                      /* The storage daemon called us */
391       jcr->sd_calls_client = true;
392       /* We should already have a storage connection! */
393       if (jcr->file_bsock && jcr->store_bsock == NULL) {
394          jcr->store_bsock = jcr->file_bsock;
395       }
396       if (jcr->store_bsock == NULL) {
397          Jmsg0(jcr, M_FATAL, 0, _("In storage_cmd port==0, no prior Storage connection.\n"));
398          Pmsg0(010, "In storage_cmd port==0, no prior Storage connection.\n");
399          goto bail_out;
400       }
401    }
402
403    if (!authenticate_storagedaemon(jcr, Job)) {
404       goto bail_out;
405    }
406    /*
407     * We are a client so we read from the socket we just
408     *   opened as if we were a FD, so set file_bsock and
409     *   clear the store_bsock.
410     */
411    jcr->file_bsock = jcr->store_bsock;
412    jcr->store_bsock = NULL;
413    jcr->authenticated = true;    /* Dir authentication is sufficient */
414    Dmsg1(050, "=== Storage_cmd authenticated Job=%s with SD.\n", Job);
415
416    /* Send OK to Director */
417    return dir->fsend(OKstore);
418
419 bail_out:
420    Dmsg0(100, "Send storage command failed.\n");
421    dir->fsend(BADcmd, "storage");
422    return false;
423 }
424
425
426 /*
427  * Set debug level as requested by the Director
428  *
429  */
430 static bool setdebug_cmd(JCR *jcr)
431 {
432    BSOCK *dir = jcr->dir_bsock;
433    int32_t trace_flag, lvl, hangup; /* hangup is ignored right now */
434    int64_t level;
435    char options[60];
436    char tags[512];
437    *tags = *options = 0;
438
439    Dmsg1(10, "setdebug_cmd: %s", dir->msg);
440
441    if (sscanf(dir->msg, "setdebug=%ld trace=%ld hangup=%ld options=%55s tags=%511s",
442               &lvl, &trace_flag, &hangup, options, tags) != 5)
443    {
444       if (sscanf(dir->msg, "setdebug=%ld trace=%ld", &lvl, &trace_flag) != 2 || lvl < 0) {
445          dir->fsend(_("3991 Bad setdebug command: %s\n"), dir->msg);
446          return 0;
447       }
448    }
449    level = lvl;
450    set_trace(trace_flag);
451    set_debug_flags(options);
452    if (!debug_parse_tags(tags, &level)) {
453       *tags = 0;
454    }
455    debug_level = level;
456
457    return dir->fsend(OKsetdebug, lvl, trace_flag, options, tags);
458 }
459
460
461 /*
462  * Cancel a Job
463  *   Be careful, we switch to using the job's JCR! So, using
464  *   BSOCKs on that jcr can have two threads in the same code.
465  */
466 static bool cancel_cmd(JCR *cjcr)
467 {
468    BSOCK *dir = cjcr->dir_bsock;
469    int oldStatus;
470    char Job[MAX_NAME_LENGTH];
471    JCR *jcr;
472    int status;
473    const char *reason;
474
475    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
476       status = JS_Canceled;
477       reason = "canceled";
478    } else {
479       dir->fsend(_("3903 Error scanning cancel command.\n"));
480       goto bail_out;
481    }
482    if (!(jcr=get_jcr_by_full_name(Job))) {
483       dir->fsend(_("3904 Job %s not found.\n"), Job);
484    } else {
485       oldStatus = jcr->JobStatus;
486       jcr->setJobStatus(status);
487       Dmsg2(800, "Cancel JobId=%d %p\n", jcr->JobId, jcr);
488       if (!jcr->authenticated && oldStatus == JS_WaitFD) {
489          pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
490       }
491       if (jcr->file_bsock) {
492          jcr->file_bsock->set_terminated();
493          jcr->file_bsock->set_timed_out();
494          Dmsg2(800, "Term bsock jid=%d %p\n", jcr->JobId, jcr);
495       } else {
496          /* Still waiting for FD to connect, release it */
497          pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
498          Dmsg2(800, "Signal FD connect jid=%d %p\n", jcr->JobId, jcr);
499       }
500       /* If thread waiting on mount, wake him */
501       if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->waiting_for_mount()) {
502          pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
503          Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
504          pthread_cond_broadcast(&wait_device_release);
505       }
506       if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->waiting_for_mount()) {
507          pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
508          Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
509          pthread_cond_broadcast(&wait_device_release);
510       }
511       dir->fsend(_("3000 JobId=%ld Job=\"%s\" marked to be %s.\n"),
512          jcr->JobId, jcr->Job, reason);
513       free_jcr(jcr);
514    }
515
516 bail_out:
517    dir->signal(BNET_EOD);
518    return 1;
519 }
520
521 /*
522  * Label a Volume
523  *
524  */
525 static bool label_cmd(JCR *jcr)
526 {
527    return do_label(jcr, 0);
528 }
529
530 static bool relabel_cmd(JCR *jcr)
531 {
532    return do_label(jcr, 1);
533 }
534
535 static bool do_label(JCR *jcr, int relabel)
536 {
537    POOLMEM *newname, *oldname, *poolname, *mtype;
538    POOL_MEM dev_name;
539    BSOCK *dir = jcr->dir_bsock;
540    DCR *dcr;
541    DEVICE *dev;
542    bool ok = false;
543    int32_t slot, drive;
544
545    newname = get_memory(dir->msglen+1);
546    oldname = get_memory(dir->msglen+1);
547    poolname = get_memory(dir->msglen+1);
548    mtype = get_memory(dir->msglen+1);
549    if (relabel) {
550       if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
551                  "MediaType=%127s Slot=%d drive=%d",
552                   dev_name.c_str(), oldname, newname, poolname, mtype,
553                   &slot, &drive) == 7) {
554          ok = true;
555       }
556    } else {
557       *oldname = 0;
558       if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s "
559                  "MediaType=%127s Slot=%d drive=%d",
560           dev_name.c_str(), newname, poolname, mtype, &slot, &drive) == 6) {
561          ok = true;
562       }
563    }
564    if (ok) {
565       unbash_spaces(newname);
566       unbash_spaces(oldname);
567       unbash_spaces(poolname);
568       unbash_spaces(mtype);
569       dcr = find_device(jcr, dev_name, mtype, drive);
570       if (dcr) {
571          dev = dcr->dev;
572          dev->Lock();                 /* Use P to avoid indefinite block */
573          if (!dev->is_open() && !dev->is_busy()) {
574             Dmsg1(400, "Can %slabel. Device is not open\n", relabel?"re":"");
575             label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
576             dev->close();
577          /* Under certain "safe" conditions, we can steal the lock */
578          } else if (dev->can_steal_lock()) {
579             Dmsg0(400, "Can relabel. can_steal_lock\n");
580             label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
581          } else if (dev->is_busy() || dev->is_blocked()) {
582             send_dir_busy_message(dir, dev);
583          } else {                     /* device not being used */
584             Dmsg0(400, "Can relabel. device not used\n");
585             label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
586          }
587          dev->Unlock();
588          free_dcr(dcr);
589       } else {
590          dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
591       }
592    } else {
593       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
594       pm_strcpy(jcr->errmsg, dir->msg);
595       dir->fsend(_("3903 Error scanning label command: %s\n"), jcr->errmsg);
596    }
597    free_memory(oldname);
598    free_memory(newname);
599    free_memory(poolname);
600    free_memory(mtype);
601    dir->signal(BNET_EOD);
602    return true;
603 }
604
605 /*
606  * Read the tape label and determine if we can safely
607  * label the tape (not a Bacula volume), then label it.
608  *
609  *  Enter with the mutex set
610  */
611 static void label_volume_if_ok(DCR *dcr, char *oldname,
612                                char *newname, char *poolname,
613                                int slot, int relabel)
614 {
615    BSOCK *dir = dcr->jcr->dir_bsock;
616    bsteal_lock_t hold;
617    DEVICE *dev = dcr->dev;
618    int label_status;
619    int mode;
620    const char *volname = (relabel == 1) ? oldname : newname;
621    char ed1[50];
622
623    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
624    Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
625
626    Dmsg0(90, "try_autoload_device - looking for volume_info\n");
627    if (!try_autoload_device(dcr->jcr, dcr, slot, volname)) {
628       goto bail_out;                  /* error */
629    }
630
631    /* Ensure that the device is open -- autoload_device() closes it */
632    if (dev->is_tape()) {
633       mode = OPEN_READ_WRITE;
634    } else {
635       mode = CREATE_READ_WRITE;
636    }
637
638    if (relabel) {
639       dev->truncating = true;         /* let open() know we will truncate it */
640    }
641    /* Set old volume name for open if relabeling */
642    dcr->setVolCatName(volname);
643    if (!dev->open(dcr, mode)) {
644       dir->fsend(_("3910 Unable to open device \"%s\": ERR=%s\n"),
645          dev->print_name(), dev->bstrerror());
646       goto bail_out;
647    }
648
649    /* See what we have for a Volume */
650    label_status = read_dev_volume_label(dcr);
651
652    /* Set new volume name */
653    dcr->setVolCatName(newname);
654    switch(label_status) {
655    case VOL_NAME_ERROR:
656    case VOL_VERSION_ERROR:
657    case VOL_LABEL_ERROR:
658    case VOL_OK:
659       if (!relabel) {
660          dir->fsend(_(
661             "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
662              dev->VolHdr.VolumeName);
663          break;
664       }
665
666       /* Relabel request. If oldname matches, continue */
667       if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
668          dir->fsend(_("3921 Wrong volume mounted.\n"));
669          break;
670       }
671       if (dev->label_type != B_BACULA_LABEL) {
672          dir->fsend(_("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
673          break;
674       }
675       /* Fall through wanted! */
676    case VOL_IO_ERROR:
677    case VOL_NO_LABEL:
678       if (!write_new_volume_label_to_dev(dcr, newname, poolname,
679               relabel, true /* write dvd now */)) {
680          dir->fsend(_("3912 Failed to label Volume: ERR=%s\n"), dev->bstrerror());
681          break;
682       }
683       bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
684       /* The following 3000 OK label. string is scanned in ua_label.c */
685       dir->fsend("3000 OK label. VolBytes=%s DVD=%d Volume=\"%s\" Device=%s\n",
686                  edit_uint64(dev->VolCatInfo.VolCatBytes, ed1),
687                  dev->is_dvd()?1:0, newname, dev->print_name());
688       break;
689    case VOL_TYPE_ERROR:
690       dir->fsend(_("3915 Failed to label Volume: ERR=%s\n"), dev->errmsg);
691       break;
692    case VOL_NO_MEDIA:
693       dir->fsend(_("3914 Failed to label Volume (no media): ERR=%s\n"), dev->bstrerror());
694       break;
695    default:
696       dir->fsend(_("3913 Cannot label Volume. "
697 "Unknown status %d from read_volume_label()\n"), label_status);
698       break;
699    }
700
701 bail_out:
702    if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
703       dev->close();
704    }
705    if (!dev->is_open()) {
706       dev->clear_volhdr();
707    }
708    volume_unused(dcr);                   /* no longer using volume */
709    give_back_device_lock(dev, &hold);
710    return;
711 }
712
713
714 /*
715  * Read the tape label
716  *
717  *  Enter with the mutex set
718  */
719 static bool read_label(DCR *dcr)
720 {
721    int ok;
722    JCR *jcr = dcr->jcr;
723    BSOCK *dir = jcr->dir_bsock;
724    bsteal_lock_t hold;
725    DEVICE *dev = dcr->dev;
726
727    steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
728
729    dcr->VolumeName[0] = 0;
730    dev->clear_labeled();              /* force read of label */
731    switch (read_dev_volume_label(dcr)) {
732    case VOL_OK:
733       dir->fsend(_("3001 Mounted Volume: %s\n"), dev->VolHdr.VolumeName);
734       ok = true;
735       break;
736    default:
737       dir->fsend(_("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
738          dev->print_name(), jcr->errmsg);
739       ok = false;
740       break;
741    }
742    volume_unused(dcr);
743    give_back_device_lock(dev, &hold);
744    return ok;
745 }
746
747 /*
748  * Searches for device by name, and if found, creates a dcr and
749  *  returns it.
750  */
751 static DCR *find_device(JCR *jcr, POOL_MEM &devname,
752                         POOLMEM *media_type, int drive)
753 {
754    DEVRES *device;
755    AUTOCHANGER *changer;
756    bool found = false;
757    DCR *dcr = NULL;
758
759    unbash_spaces(devname);
760    foreach_res(device, R_DEVICE) {
761       /* Find resource, and make sure we were able to open it */
762       if (strcmp(device->hdr.name, devname.c_str()) == 0 &&
763           (!media_type || strcmp(device->media_type, media_type) ==0)) {
764          if (!device->dev) {
765             device->dev = init_dev(jcr, device);
766          }
767          if (!device->dev) {
768             Jmsg(jcr, M_WARNING, 0, _("\n"
769                "     Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
770                  devname.c_str());
771             continue;
772          }
773          Dmsg1(20, "Found device %s\n", device->hdr.name);
774          found = true;
775          break;
776       }
777    }
778    if (!found) {
779       foreach_res(changer, R_AUTOCHANGER) {
780          /* Find resource, and make sure we were able to open it */
781          if (strcmp(devname.c_str(), changer->hdr.name) == 0) {
782             /* Try each device in this AutoChanger */
783             foreach_alist(device, changer->device) {
784                Dmsg1(100, "Try changer device %s\n", device->hdr.name);
785                if (!device->dev) {
786                   device->dev = init_dev(jcr, device);
787                }
788                if (!device->dev) {
789                   Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
790                   Jmsg(jcr, M_WARNING, 0, _("\n"
791                      "     Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
792                        device->hdr.name, devname.c_str());
793                   continue;
794                }
795                if (!device->dev->autoselect) {
796                   Dmsg1(100, "Device %s not autoselect skipped.\n", devname.c_str());
797                   continue;              /* device is not available */
798                }
799                if ((drive < 0 || drive == (int)device->dev->drive_index) &&
800                    (!media_type || strcmp(device->media_type, media_type) ==0)) {
801                   Dmsg1(20, "Found changer device %s\n", device->hdr.name);
802                   found = true;
803                   break;
804                }
805                Dmsg3(100, "Device %s drive wrong: want=%d got=%d skipping\n",
806                   devname.c_str(), drive, (int)device->dev->drive_index);
807             }
808             break;                    /* we found it but could not open a device */
809          }
810       }
811    }
812
813    if (found) {
814       Dmsg1(100, "Found device %s\n", device->hdr.name);
815       dcr = new_dcr(jcr, NULL, device->dev);
816       dcr->device = device;
817    }
818    return dcr;
819 }
820
821
822 /*
823  * Mount command from Director
824  */
825 static bool mount_cmd(JCR *jcr)
826 {
827    POOL_MEM devname;
828    BSOCK *dir = jcr->dir_bsock;
829    DEVICE *dev;
830    DCR *dcr;
831    int32_t drive;
832    int32_t slot = 0;
833    bool ok;
834
835    ok = sscanf(dir->msg, "mount %127s drive=%d slot=%d", devname.c_str(),
836                &drive, &slot) == 3;
837    if (!ok) {
838       ok = sscanf(dir->msg, "mount %127s drive=%d", devname.c_str(), &drive) == 2;
839    }
840    Dmsg3(100, "ok=%d drive=%d slot=%d\n", ok, drive, slot);
841    if (ok) {
842       dcr = find_device(jcr, devname, NULL, drive);
843       if (dcr) {
844          dev = dcr->dev;
845          dev->Lock();                 /* Use P to avoid indefinite block */
846          Dmsg2(100, "mount cmd blocked=%d must_unload=%d\n", dev->blocked(),
847             dev->must_unload());
848          switch (dev->blocked()) {         /* device blocked? */
849          case BST_WAITING_FOR_SYSOP:
850             /* Someone is waiting, wake him */
851             Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
852             dev->set_blocked(BST_MOUNT);
853             dir->fsend("3001 OK mount requested. %sDevice=%s\n",
854                        slot>0?_("Specified slot ignored. "):"",
855                        dev->print_name());
856             Dmsg1(100, "JobId=%u broadcast wait_next_vol\n", (uint32_t)dcr->jcr->JobId);
857             pthread_cond_broadcast(&dev->wait_next_vol);
858             Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
859             pthread_cond_broadcast(&wait_device_release);
860             break;
861
862          /* In both of these two cases, we (the user) unmounted the Volume */
863          case BST_UNMOUNTED_WAITING_FOR_SYSOP:
864          case BST_UNMOUNTED:
865             Dmsg2(100, "Unmounted changer=%d slot=%d\n", dev->is_autochanger(), slot);
866             if (dev->is_autochanger() && slot > 0) {
867                try_autoload_device(jcr, dcr, slot, "");
868             }
869             /* We freed the device, so reopen it and wake any waiting threads */
870             if (!dev->open(dcr, OPEN_READ_ONLY)) {
871                dir->fsend(_("3901 Unable to open device \"%s\": ERR=%s\n"),
872                   dev->print_name(), dev->bstrerror());
873                if (dev->blocked() == BST_UNMOUNTED) {
874                   /* We blocked the device, so unblock it */
875                   Dmsg0(100, "Unmounted. Unblocking device\n");
876                   unblock_device(dev);
877                }
878                break;
879             }
880             read_dev_volume_label(dcr);
881             if (dev->blocked() == BST_UNMOUNTED) {
882                /* We blocked the device, so unblock it */
883                Dmsg0(100, "Unmounted. Unblocking device\n");
884                read_label(dcr);       /* this should not be necessary */
885                unblock_device(dev);
886             } else {
887                Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
888                dev->set_blocked(BST_MOUNT);
889             }
890             if (dev->is_labeled()) {
891                dir->fsend(_("3001 Device \"%s\" is mounted with Volume \"%s\"\n"),
892                   dev->print_name(), dev->VolHdr.VolumeName);
893             } else {
894                dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
895                                  "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
896                           dev->print_name());
897             }
898             pthread_cond_broadcast(&dev->wait_next_vol);
899             Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
900             pthread_cond_broadcast(&wait_device_release);
901             break;
902
903          case BST_DOING_ACQUIRE:
904             dir->fsend(_("3001 Device \"%s\" is doing acquire.\n"),
905                        dev->print_name());
906             break;
907
908          case BST_WRITING_LABEL:
909             dir->fsend(_("3903 Device \"%s\" is being labeled.\n"),
910                dev->print_name());
911             break;
912
913          case BST_NOT_BLOCKED:
914             Dmsg2(100, "Not blocked changer=%d slot=%d\n", dev->is_autochanger(), slot);
915             if (dev->is_autochanger() && slot > 0) {
916                try_autoload_device(jcr, dcr, slot, "");
917             }
918             if (dev->is_open()) {
919                if (dev->is_labeled()) {
920                   dir->fsend(_("3001 Device \"%s\" is mounted with Volume \"%s\"\n"),
921                      dev->print_name(), dev->VolHdr.VolumeName);
922                } else {
923                   dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
924                                  "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
925                              dev->print_name());
926                }
927             } else if (dev->is_tape()) {
928                if (!dev->open(dcr, OPEN_READ_ONLY)) {
929                   dir->fsend(_("3901 Unable to open device \"%s\": ERR=%s\n"),
930                      dev->print_name(), dev->bstrerror());
931                   break;
932                }
933                read_label(dcr);
934                if (dev->is_labeled()) {
935                   dir->fsend(_("3001 Device \"%s\" is already mounted with Volume \"%s\"\n"),
936                      dev->print_name(), dev->VolHdr.VolumeName);
937                } else {
938                   dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
939                                     "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
940                              dev->print_name());
941                }
942                if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
943                   dev->close();
944                }
945             } else if (dev->is_unmountable()) {
946                if (dev->mount(1)) {
947                   dir->fsend(_("3002 Device \"%s\" is mounted.\n"), dev->print_name());
948                } else {
949                   dir->fsend(_("3907 %s"), dev->bstrerror());
950                }
951             } else { /* must be file */
952                dir->fsend(_("3906 File device \"%s\" is always mounted.\n"),
953                   dev->print_name());
954                pthread_cond_broadcast(&dev->wait_next_vol);
955                Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
956                pthread_cond_broadcast(&wait_device_release);
957             }
958             break;
959
960          case BST_RELEASING:
961             dir->fsend(_("3930 Device \"%s\" is being released.\n"), dev->print_name());
962             break;
963
964          default:
965             dir->fsend(_("3905 Unknown wait state %d\n"), dev->blocked());
966             break;
967          }
968          dev->Unlock();
969          free_dcr(dcr);
970       } else {
971          dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
972       }
973    } else {
974       pm_strcpy(jcr->errmsg, dir->msg);
975       dir->fsend(_("3909 Error scanning mount command: %s\n"), jcr->errmsg);
976    }
977    dir->signal(BNET_EOD);
978    return true;
979 }
980
981 /*
982  * unmount command from Director
983  */
984 static bool unmount_cmd(JCR *jcr)
985 {
986    POOL_MEM devname;
987    BSOCK *dir = jcr->dir_bsock;
988    DEVICE *dev;
989    DCR *dcr;
990    int32_t drive;
991
992    if (sscanf(dir->msg, "unmount %127s drive=%d", devname.c_str(), &drive) == 2) {
993       dcr = find_device(jcr, devname, NULL, drive);
994       if (dcr) {
995          dev = dcr->dev;
996          dev->Lock();                 /* Use P to avoid indefinite block */
997          if (!dev->is_open()) {
998             if (!dev->is_busy()) {
999                unload_autochanger(dcr, -1);
1000             }
1001             if (dev->is_unmountable()) {
1002                if (dev->unmount(0)) {
1003                   dir->fsend(_("3002 Device \"%s\" unmounted.\n"),
1004                      dev->print_name());
1005                } else {
1006                   dir->fsend(_("3907 %s"), dev->bstrerror());
1007                }
1008             } else {
1009                Dmsg0(90, "Device already unmounted\n");
1010                dir->fsend(_("3901 Device \"%s\" is already unmounted.\n"),
1011                   dev->print_name());
1012             }
1013          } else if (dev->blocked() == BST_WAITING_FOR_SYSOP) {
1014             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
1015                dev->blocked());
1016             if (!unload_autochanger(dcr, -1)) {
1017                /*
1018                 * ***FIXME**** what is this ???? -- probably we had
1019                 *   the wrong volume so we must free it and try again. KES
1020                 */
1021                dev->close();
1022                free_volume(dev);
1023             }
1024             if (dev->is_unmountable() && !dev->unmount(0)) {
1025                dir->fsend(_("3907 %s"), dev->bstrerror());
1026             } else {
1027                dev->set_blocked(BST_UNMOUNTED_WAITING_FOR_SYSOP);
1028                dir->fsend(_("3001 Device \"%s\" unmounted.\n"),
1029                   dev->print_name());
1030             }
1031
1032          } else if (dev->blocked() == BST_DOING_ACQUIRE) {
1033             dir->fsend(_("3902 Device \"%s\" is busy in acquire.\n"),
1034                dev->print_name());
1035
1036          } else if (dev->blocked() == BST_WRITING_LABEL) {
1037             dir->fsend(_("3903 Device \"%s\" is being labeled.\n"),
1038                dev->print_name());
1039
1040          } else if (dev->is_busy()) {
1041             send_dir_busy_message(dir, dev);
1042          } else {                     /* device not being used */
1043             Dmsg0(90, "Device not in use, unmounting\n");
1044             /* On FreeBSD, I am having ASSERT() failures in block_device()
1045              * and I can only imagine that the thread id that we are
1046              * leaving in no_wait_id is being re-used. So here,
1047              * we simply do it by hand.  Gross, but a solution.
1048              */
1049             /*  block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
1050             dev->set_blocked(BST_UNMOUNTED);
1051             clear_thread_id(dev->no_wait_id);
1052             if (!unload_autochanger(dcr, -1)) {
1053                dev->close();
1054                free_volume(dev);
1055             }
1056             if (dev->is_unmountable() && !dev->unmount(0)) {
1057                dir->fsend(_("3907 %s"), dev->bstrerror());
1058             } else {
1059                dir->fsend(_("3002 Device \"%s\" unmounted.\n"),
1060                   dev->print_name());
1061             }
1062          }
1063          dev->Unlock();
1064          free_dcr(dcr);
1065       } else {
1066          dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1067       }
1068    } else {
1069       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1070       pm_strcpy(jcr->errmsg, dir->msg);
1071       dir->fsend(_("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
1072    }
1073    dir->signal(BNET_EOD);
1074    return true;
1075 }
1076
1077 #if 0
1078 /*
1079  * The truncate command will recycle a volume. The director can call this
1080  * after purging a volume so that disk space will not be wasted. Only useful
1081  * for File Storage, of course.
1082  *
1083  *
1084  * It is currently disabled
1085  */
1086 static bool action_on_purge_cmd(JCR *jcr)
1087 {
1088    BSOCK *dir = jcr->dir_bsock;
1089
1090    char devname[MAX_NAME_LENGTH];
1091    char volumename[MAX_NAME_LENGTH];
1092    int32_t action;
1093
1094    /* TODO: Need to find a free device and ask for slot to the director */
1095    if (sscanf(dir->msg,
1096               "action_on_purge %127s vol=%127s action=%d",
1097               devname, volumename, &action)!= 5)
1098    {
1099       dir->fsend(_("3916 Error scanning action_on_purge command\n"));
1100       goto done;
1101    }
1102    unbash_spaces(volumename);
1103    unbash_spaces(devname);
1104
1105    /* Check if action is correct */
1106    if (action & AOP_TRUNCTATE) {
1107
1108    }
1109    /* ... */
1110
1111 done:
1112    dir->signal(BNET_EOD);
1113    return true;
1114 }
1115 #endif
1116
1117 /*
1118  * Release command from Director. This rewinds the device and if
1119  *   configured does a offline and ensures that Bacula will
1120  *   re-read the label of the tape before continuing. This gives
1121  *   the operator the chance to change the tape anytime before the
1122  *   next job starts.
1123  */
1124 static bool release_cmd(JCR *jcr)
1125 {
1126    POOL_MEM devname;
1127    BSOCK *dir = jcr->dir_bsock;
1128    DEVICE *dev;
1129    DCR *dcr;
1130    int32_t drive;
1131
1132    if (sscanf(dir->msg, "release %127s drive=%d", devname.c_str(), &drive) == 2) {
1133       dcr = find_device(jcr, devname, NULL, drive);
1134       if (dcr) {
1135          dev = dcr->dev;
1136          dev->Lock();                 /* Use P to avoid indefinite block */
1137          if (!dev->is_open()) {
1138             if (!dev->is_busy()) {
1139                unload_autochanger(dcr, -1);
1140             }
1141             Dmsg0(90, "Device already released\n");
1142             dir->fsend(_("3921 Device \"%s\" already released.\n"),
1143                dev->print_name());
1144
1145          } else if (dev->blocked() == BST_WAITING_FOR_SYSOP) {
1146             Dmsg2(90, "%d waiter dev_block=%d.\n", dev->num_waiting,
1147                dev->blocked());
1148             unload_autochanger(dcr, -1);
1149             dir->fsend(_("3922 Device \"%s\" waiting for sysop.\n"),
1150                dev->print_name());
1151
1152          } else if (dev->blocked() == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
1153             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
1154                dev->blocked());
1155             dir->fsend(_("3922 Device \"%s\" waiting for mount.\n"),
1156                dev->print_name());
1157
1158          } else if (dev->blocked() == BST_DOING_ACQUIRE) {
1159             dir->fsend(_("3923 Device \"%s\" is busy in acquire.\n"),
1160                dev->print_name());
1161
1162          } else if (dev->blocked() == BST_WRITING_LABEL) {
1163             dir->fsend(_("3914 Device \"%s\" is being labeled.\n"),
1164                dev->print_name());
1165
1166          } else if (dev->is_busy()) {
1167             send_dir_busy_message(dir, dev);
1168          } else {                     /* device not being used */
1169             Dmsg0(90, "Device not in use, releasing\n");
1170             dcr->release_volume();
1171             dir->fsend(_("3022 Device \"%s\" released.\n"),
1172                dev->print_name());
1173          }
1174          dev->Unlock();
1175          free_dcr(dcr);
1176       } else {
1177          dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1178       }
1179    } else {
1180       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1181       pm_strcpy(jcr->errmsg, dir->msg);
1182       dir->fsend(_("3927 Error scanning release command: %s\n"), jcr->errmsg);
1183    }
1184    dir->signal(BNET_EOD);
1185    return true;
1186 }
1187
1188 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1189 static uint32_t bsr_uniq = 0;
1190
1191 static bool get_bootstrap_file(JCR *jcr, BSOCK *sock)
1192 {
1193    POOLMEM *fname = get_pool_memory(PM_FNAME);
1194    FILE *bs;
1195    bool ok = false;
1196
1197    if (jcr->RestoreBootstrap) {
1198       unlink(jcr->RestoreBootstrap);
1199       free_pool_memory(jcr->RestoreBootstrap);
1200    }
1201    P(bsr_mutex);
1202    bsr_uniq++;
1203    Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1204       jcr->Job, bsr_uniq);
1205    V(bsr_mutex);
1206    Dmsg1(400, "bootstrap=%s\n", fname);
1207    jcr->RestoreBootstrap = fname;
1208    bs = fopen(fname, "a+b");           /* create file */
1209    if (!bs) {
1210       berrno be;
1211       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1212          jcr->RestoreBootstrap, be.bstrerror());
1213       goto bail_out;
1214    }
1215    Dmsg0(150, "=== Bootstrap file ===\n");
1216    while (sock->recv() >= 0) {
1217        Dmsg1(150, "%s", sock->msg);
1218        fputs(sock->msg, bs);
1219    }
1220    fclose(bs);
1221    Dmsg0(150, "=== end bootstrap file ===\n");
1222    jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
1223    if (!jcr->bsr) {
1224       Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
1225       goto bail_out;
1226    }
1227    if (chk_dbglvl(150)) {
1228       dump_bsr(jcr->bsr, true);
1229    }
1230    /* If we got a bootstrap, we are reading, so create read volume list */
1231    create_restore_volume_list(jcr);
1232    ok = true;
1233
1234 bail_out:
1235    unlink(jcr->RestoreBootstrap);
1236    free_pool_memory(jcr->RestoreBootstrap);
1237    jcr->RestoreBootstrap = NULL;
1238    if (!ok) {
1239       sock->fsend(ERROR_bootstrap);
1240       return false;
1241    }
1242    return sock->fsend(OK_bootstrap);
1243 }
1244
1245 static bool bootstrap_cmd(JCR *jcr)
1246 {
1247    return get_bootstrap_file(jcr, jcr->dir_bsock);
1248 }
1249
1250 /*
1251  * Autochanger command from Director
1252  */
1253 static bool changer_cmd(JCR *jcr)
1254 {
1255    POOL_MEM devname;
1256    BSOCK *dir = jcr->dir_bsock;
1257    DEVICE *dev;
1258    DCR *dcr;
1259    const char *cmd = NULL;
1260    bool ok = false;
1261    /*
1262     * A safe_cmd may call autochanger script but does not load/unload
1263     *    slots so it can be done at the same time that the drive is open.
1264     */
1265    bool safe_cmd = false;
1266
1267    if (sscanf(dir->msg, "autochanger listall %127s", devname.c_str()) == 1) {
1268       cmd = "listall";
1269       safe_cmd = ok = true;
1270    } else if (sscanf(dir->msg, "autochanger list %127s", devname.c_str()) == 1) {
1271       cmd = "list";
1272       safe_cmd = ok = true;
1273    } else if (sscanf(dir->msg, "autochanger slots %127s", devname.c_str()) == 1) {
1274       cmd = "slots";
1275       safe_cmd = ok = true;
1276    } else if (sscanf(dir->msg, "autochanger drives %127s", devname.c_str()) == 1) {
1277       cmd = "drives";
1278       safe_cmd = ok = true;
1279    }
1280    if (ok) {
1281       dcr = find_device(jcr, devname, NULL, -1);
1282       if (dcr) {
1283          dev = dcr->dev;
1284          dev->Lock();                 /* Use P to avoid indefinite block */
1285          if (!dev->device->changer_res) {
1286             dir->fsend(_("3998 Device \"%s\" is not an autochanger.\n"),
1287                dev->print_name());
1288          /* Under certain "safe" conditions, we can steal the lock */
1289          } else if (safe_cmd || !dev->is_open() || dev->can_steal_lock()) {
1290             autochanger_cmd(dcr, dir, cmd);
1291          } else if (dev->is_busy() || dev->is_blocked()) {
1292             send_dir_busy_message(dir, dev);
1293          } else {                     /* device not being used */
1294             autochanger_cmd(dcr, dir, cmd);
1295          }
1296          dev->Unlock();
1297          free_dcr(dcr);
1298       } else {
1299          dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1300       }
1301    } else {  /* error on scanf */
1302       pm_strcpy(jcr->errmsg, dir->msg);
1303       dir->fsend(_("3908 Error scanning autochanger drives/list/slots command: %s\n"),
1304          jcr->errmsg);
1305    }
1306    dir->signal(BNET_EOD);
1307    return true;
1308 }
1309
1310 /*
1311  * Read and return the Volume label
1312  */
1313 static bool readlabel_cmd(JCR *jcr)
1314 {
1315    POOL_MEM devname;
1316    BSOCK *dir = jcr->dir_bsock;
1317    DEVICE *dev;
1318    DCR *dcr;
1319    int32_t Slot, drive;
1320
1321    if (sscanf(dir->msg, "readlabel %127s Slot=%d drive=%d", devname.c_str(),
1322        &Slot, &drive) == 3) {
1323       dcr = find_device(jcr, devname, NULL, drive);
1324       if (dcr) {
1325          dev = dcr->dev;
1326          dev->Lock();                 /* Use P to avoid indefinite block */
1327          if (!dev->is_open()) {
1328             read_volume_label(jcr, dcr, dev, Slot);
1329             dev->close();
1330          /* Under certain "safe" conditions, we can steal the lock */
1331          } else if (dev->can_steal_lock()) {
1332             read_volume_label(jcr, dcr, dev, Slot);
1333          } else if (dev->is_busy() || dev->is_blocked()) {
1334             send_dir_busy_message(dir, dev);
1335          } else {                     /* device not being used */
1336             read_volume_label(jcr, dcr, dev, Slot);
1337          }
1338          dev->Unlock();
1339          free_dcr(dcr);
1340       } else {
1341          dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1342       }
1343    } else {
1344       pm_strcpy(jcr->errmsg, dir->msg);
1345       dir->fsend(_("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
1346    }
1347    dir->signal(BNET_EOD);
1348    return true;
1349 }
1350
1351
1352 /*
1353  * Read the tape label
1354  *
1355  *  Enter with the mutex set
1356  */
1357 static void read_volume_label(JCR *jcr, DCR *dcr, DEVICE *dev, int Slot)
1358 {
1359    BSOCK *dir = jcr->dir_bsock;
1360    bsteal_lock_t hold;
1361
1362    dcr->set_dev(dev);
1363    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
1364
1365    if (!try_autoload_device(jcr, dcr, Slot, "")) {
1366       goto bail_out;                  /* error */
1367    }
1368
1369    dev->clear_labeled();              /* force read of label */
1370    switch (read_dev_volume_label(dcr)) {
1371    case VOL_OK:
1372       /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
1373       dir->fsend(_("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolumeName, Slot);
1374       Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolumeName);
1375       break;
1376    default:
1377       dir->fsend(_("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
1378                  dev->print_name(), jcr->errmsg);
1379       break;
1380    }
1381
1382 bail_out:
1383    give_back_device_lock(dev, &hold);
1384    return;
1385 }
1386
1387 static bool try_autoload_device(JCR *jcr, DCR *dcr, int slot, const char *VolName)
1388 {
1389    BSOCK *dir = jcr->dir_bsock;
1390
1391    bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
1392    dcr->VolCatInfo.Slot = slot;
1393    dcr->VolCatInfo.InChanger = slot > 0;
1394    if (autoload_device(dcr, 0, dir) < 0) {    /* autoload if possible */
1395       return false;
1396    }
1397    return true;
1398 }
1399
1400 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev)
1401 {
1402    if (dev->is_blocked()) {
1403       switch (dev->blocked()) {
1404       case BST_UNMOUNTED:
1405          dir->fsend(_("3931 Device \"%s\" is BLOCKED. user unmounted.\n"),
1406             dev->print_name());
1407          break;
1408       case BST_UNMOUNTED_WAITING_FOR_SYSOP:
1409          dir->fsend(_("3932 Device \"%s\" is BLOCKED. user unmounted during wait for media/mount.\n"),
1410              dev->print_name());
1411          break;
1412       case BST_WAITING_FOR_SYSOP:
1413          dir->fsend(_("3933 Device \"%s\" is BLOCKED waiting for media.\n"),
1414             dev->print_name());
1415          break;
1416       case BST_DOING_ACQUIRE:
1417          dir->fsend(_("3934 Device \"%s\" is being initialized.\n"),
1418             dev->print_name());
1419          break;
1420       case BST_WRITING_LABEL:
1421          dir->fsend(_("3935 Device \"%s\" is blocked labeling a Volume.\n"),
1422             dev->print_name());
1423          break;
1424       default:
1425          dir->fsend(_("3935 Device \"%s\" is blocked for unknown reason.\n"),
1426             dev->print_name());
1427          break;
1428       }
1429    } else if (dev->can_read()) {
1430        dir->fsend(_("3936 Device \"%s\" is busy reading.\n"),
1431                    dev->print_name());;
1432    } else {
1433        dir->fsend(_("3937 Device \"%s\" is busy with writers=%d reserved=%d.\n"),
1434           dev->print_name(), dev->num_writers, dev->num_reserved());
1435    }
1436 }