2 * This file handles accepting Director Commands
4 * Most Director commands are handled here, with the
5 * exception of the Job command command and subsequent
6 * subcommands that are handled
9 * N.B. in this file, in general we must use P(dev->mutex) rather
10 * than lock_device(dev) so that we can examine the blocked
11 * state rather than blocking ourselves. In some "safe" cases,
12 * we can do things to a blocked device. CAREFUL!!!!
14 * File daemon commands are handled in fdcmd.c
16 * Kern Sibbald, May MMI
22 Copyright (C) 2001-2005 Kern Sibbald
24 This program is free software; you can redistribute it and/or
25 modify it under the terms of the GNU General Public License as
26 published by the Free Software Foundation; either version 2 of
27 the License, or (at your option) any later version.
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 General Public License for more details.
34 You should have received a copy of the GNU General Public
35 License along with this program; if not, write to the Free
36 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
44 /* Exported variables */
46 /* Imported variables */
47 extern BSOCK *filed_chan;
48 extern int r_first, r_last;
49 extern struct s_res resources[];
50 extern char my_name[];
51 extern time_t daemon_start_time;
52 extern struct s_last_job last_job;
54 /* Static variables */
55 static char derrmsg[] = "3900 Invalid command\n";
56 static char OKsetdebug[] = "3000 OK setdebug=%d\n";
57 static char illegal_cmd[] = "3997 Illegal command for a Director with Monitor directive enabled\n";
59 /* Imported functions */
60 extern void terminate_child();
61 extern bool job_cmd(JCR *jcr);
62 extern bool use_cmd(JCR *jcr);
63 extern bool run_cmd(JCR *jcr);
64 extern bool status_cmd(JCR *sjcr);
65 extern bool qstatus_cmd(JCR *jcr);
66 extern bool query_cmd(JCR *jcr);
68 /* Forward referenced functions */
69 static bool label_cmd(JCR *jcr);
70 static bool relabel_cmd(JCR *jcr);
71 static bool readlabel_cmd(JCR *jcr);
72 static bool release_cmd(JCR *jcr);
73 static bool setdebug_cmd(JCR *jcr);
74 static bool cancel_cmd(JCR *cjcr);
75 static bool mount_cmd(JCR *jcr);
76 static bool unmount_cmd(JCR *jcr);
77 static bool autochanger_cmd(JCR *sjcr);
78 static bool do_label(JCR *jcr, int relabel);
79 static DEVICE *find_device(JCR *jcr, POOL_MEM &dev_name);
80 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot);
81 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
82 char *newname, char *poolname,
83 int Slot, int relabel);
84 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName);
85 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
89 bool (*func)(JCR *jcr);
90 int monitoraccess; /* specify if monitors have access to this function */
94 * The following are the recognized commands from the Director.
96 static struct s_cmds cmds[] = {
97 {"JobId=", job_cmd, 0}, /* start Job */
98 {"autochanger", autochanger_cmd, 0},
99 {"bootstrap", bootstrap_cmd, 0},
100 {"cancel", cancel_cmd, 0},
101 {"label", label_cmd, 0}, /* label a tape */
102 {"mount", mount_cmd, 0},
103 {"readlabel", readlabel_cmd, 0},
104 {"release", release_cmd, 0},
105 {"relabel", relabel_cmd, 0}, /* relabel a tape */
106 {"setdebug=", setdebug_cmd, 0}, /* set debug level */
107 {"status", status_cmd, 1},
108 {".status", qstatus_cmd, 1},
109 {"unmount", unmount_cmd, 0},
110 {"use device=", use_cmd, 0},
112 {"query", query_cmd, 0},
113 {NULL, NULL} /* list terminator */
118 * Connection request. We accept connections either from the
119 * Director or a Client (File daemon).
121 * Note, we are running as a seperate thread of the Storage daemon.
122 * and it is because a Director has made a connection with
123 * us on the "Message" channel.
125 * Basic tasks done here:
126 * - Create a JCR record
127 * - If it was from the FD, call handle_filed_connection()
128 * - Authenticate the Director
129 * - We wait for a command
130 * - We execute the command
131 * - We continue or exit depending on the return status
133 void *handle_connection_request(void *arg)
135 BSOCK *bs = (BSOCK *)arg;
140 char name[MAX_NAME_LENGTH];
142 if (bnet_recv(bs) <= 0) {
143 Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
149 * Do a sanity check on the message received
151 if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)-25) {
152 Emsg1(M_ERROR, 0, _("Invalid connection. Len=%d\n"), bs->msglen);
157 * See if this is a File daemon connection. If so
160 Dmsg1(110, "Conn: %s", bs->msg);
161 if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
162 handle_filed_connection(bs, name);
166 Dmsg0(110, "Start Dir Job\n");
167 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
168 jcr->dir_bsock = bs; /* save Director bsock */
169 jcr->dir_bsock->jcr = jcr;
170 jcr->dcrs = New(alist(10, not_owned_by_alist));
171 /* Initialize FD start condition variable */
172 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
174 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
178 Dmsg0(1000, "stored in start_job\n");
181 * Authenticate the Director
183 if (!authenticate_director(jcr)) {
184 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
187 Dmsg0(90, "Message channel init completed.\n");
189 for (quit=false; !quit;) {
191 if ((bnet_stat = bnet_recv(bs)) <= 0) {
192 break; /* connection terminated */
194 Dmsg1(199, "<dird: %s\n", bs->msg);
196 for (i=0; cmds[i].cmd; i++) {
197 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
198 if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
199 Dmsg1(100, "Command %s illegal.\n", cmds[i].cmd);
200 bnet_fsend(bs, illegal_cmd);
201 bnet_sig(bs, BNET_EOD);
204 Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
205 if (!cmds[i].func(jcr)) { /* do command */
206 quit = true; /* error, get out */
207 Dmsg1(190, "Command %s requsts quit\n", cmds[i].cmd);
209 found = true; /* indicate command found */
213 if (!found) { /* command not found */
214 bnet_fsend(bs, derrmsg);
219 dequeue_messages(jcr); /* send any queued messages */
220 bnet_sig(bs, BNET_TERMINATE);
226 * Set debug level as requested by the Director
229 static bool setdebug_cmd(JCR *jcr)
231 BSOCK *dir = jcr->dir_bsock;
232 int level, trace_flag;
234 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
235 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
236 bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
240 set_trace(trace_flag);
241 return bnet_fsend(dir, OKsetdebug, level);
248 static bool cancel_cmd(JCR *cjcr)
250 BSOCK *dir = cjcr->dir_bsock;
252 char Job[MAX_NAME_LENGTH];
255 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
256 if (!(jcr=get_jcr_by_full_name(Job))) {
257 bnet_fsend(dir, _("3992 Job %s not found.\n"), Job);
260 oldStatus = jcr->JobStatus;
261 set_jcr_job_status(jcr, JS_Canceled);
262 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
263 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
266 if (jcr->file_bsock) {
267 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
269 /* If thread waiting on mount, wake him */
270 if (jcr->dcr && jcr->dcr->dev &&
271 (jcr->dcr->dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
272 jcr->dcr->dev->dev_blocked == BST_UNMOUNTED ||
273 jcr->dcr->dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
274 pthread_cond_signal(&jcr->dcr->dev->wait_next_vol);
276 bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
280 bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
282 bnet_sig(dir, BNET_EOD);
290 static bool label_cmd(JCR *jcr)
292 return do_label(jcr, 0);
295 static bool relabel_cmd(JCR *jcr)
297 return do_label(jcr, 1);
300 static bool do_label(JCR *jcr, int relabel)
302 POOLMEM *newname, *oldname, *poolname, *mtype;
304 BSOCK *dir = jcr->dir_bsock;
309 newname = get_memory(dir->msglen+1);
310 oldname = get_memory(dir->msglen+1);
311 poolname = get_memory(dir->msglen+1);
312 mtype = get_memory(dir->msglen+1);
314 if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s MediaType=%127s Slot=%d",
315 dev_name.c_str(), oldname, newname, poolname, mtype, &slot) == 6) {
320 if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s MediaType=%127s Slot=%d",
321 dev_name.c_str(), newname, poolname, mtype, &slot) == 5) {
326 unbash_spaces(newname);
327 unbash_spaces(oldname);
328 unbash_spaces(poolname);
329 unbash_spaces(mtype);
330 dev = find_device(jcr, dev_name);
332 /******FIXME**** compare MediaTypes */
333 P(dev->mutex); /* Use P to avoid indefinite block */
334 if (!dev->is_open()) {
335 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
336 force_close_dev(dev);
337 /* Under certain "safe" conditions, we can steal the lock */
338 } else if (dev->dev_blocked &&
339 (dev->dev_blocked == BST_UNMOUNTED ||
340 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
341 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
342 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
343 } else if (dev->is_busy()) {
344 send_dir_busy_message(dir, dev);
345 } else { /* device not being used */
346 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
350 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dev_name.c_str());
353 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
354 pm_strcpy(jcr->errmsg, dir->msg);
355 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
357 free_memory(oldname);
358 free_memory(newname);
359 free_memory(poolname);
361 bnet_sig(dir, BNET_EOD);
366 * Read the tape label and determine if we can safely
367 * label the tape (not a Bacula volume), then label it.
369 * Enter with the mutex set
371 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
372 char *newname, char *poolname,
373 int slot, int relabel)
375 BSOCK *dir = jcr->dir_bsock;
381 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
383 if (!try_autoload_device(jcr, slot, newname)) {
384 goto bail_out; /* error */
387 /* See what we have for a Volume */
389 label_status = read_dev_volume_label_guess(dcr, 1);
391 label_status = read_dev_volume_label(dcr);
394 switch(label_status) {
396 case VOL_VERSION_ERROR:
397 case VOL_LABEL_ERROR:
401 "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
402 dev->VolHdr.VolName);
405 /* Relabel request. If oldname matches, continue */
406 if (strcmp(oldname, dev->VolHdr.VolName) != 0) {
407 bnet_fsend(dir, _("3921 Wrong volume mounted.\n"));
410 if (dev->label_type != B_BACULA_LABEL) {
411 bnet_fsend(dir, _("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
414 /* Fall through wanted! */
417 if (!write_new_volume_label_to_dev(dcr, newname, poolname)) {
418 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
421 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
422 /* The following 3000 OK label. string is scanned in ua_label.c */
423 bnet_fsend(dir, "3000 OK label. Volume=%s Device=%s\n",
424 newname, dev_name(dev));
427 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
430 bnet_fsend(dir, _("3913 Cannot label Volume. "
431 "Unknown status %d from read_volume_label()\n"), label_status);
436 give_back_device_lock(dev, &hold);
442 * Read the tape label
444 * Enter with the mutex set
446 static bool read_label(DCR *dcr)
450 BSOCK *dir = jcr->dir_bsock;
452 DEVICE *dev = dcr->dev;
454 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
456 dcr->VolumeName[0] = 0;
457 dev->state &= ~ST_LABEL; /* force read of label */
458 switch (read_dev_volume_label(dcr)) {
460 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
464 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
465 dev_name(dev), jcr->errmsg);
469 give_back_device_lock(dev, &hold);
473 static DEVICE *find_device(JCR *jcr, POOL_MEM &devname)
476 AUTOCHANGER *changer;
479 unbash_spaces(devname);
481 foreach_res(device, R_DEVICE) {
482 /* Find resource, and make sure we were able to open it */
483 if (fnmatch(device->hdr.name, devname.c_str(), 0) == 0) {
485 device->dev = init_dev(jcr, NULL, device);
488 Jmsg(jcr, M_WARNING, 0, _("\n"
489 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
493 Dmsg1(20, "Found device %s\n", device->hdr.name);
498 foreach_res(changer, R_AUTOCHANGER) {
499 /* Find resource, and make sure we were able to open it */
500 if (fnmatch(devname.c_str(), changer->hdr.name, 0) == 0) {
501 /* Try each device in this AutoChanger */
502 foreach_alist(device, changer->device) {
503 Dmsg1(100, "Try changer device %s\n", device->hdr.name);
505 device->dev = init_dev(jcr, NULL, device);
508 Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
509 Jmsg(jcr, M_WARNING, 0, _("\n"
510 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
511 device->hdr.name, devname.c_str());
514 if (!device->dev->autoselect) {
515 continue; /* device is not available */
517 Dmsg1(20, "Found changer device %s\n", device->hdr.name);
521 break; /* we found it but could not open a device */
526 jcr->dcr = new_dcr(jcr, device->dev);
528 jcr->dcr->device = device;
529 return jcr->dcr->dev;
537 * Mount command from Director
539 static bool mount_cmd(JCR *jcr)
542 BSOCK *dir = jcr->dir_bsock;
546 if (sscanf(dir->msg, "mount %127s", devname.c_str()) == 1) {
547 dev = find_device(jcr, devname);
550 P(dev->mutex); /* Use P to avoid indefinite block */
551 switch (dev->dev_blocked) { /* device blocked? */
552 case BST_WAITING_FOR_SYSOP:
553 /* Someone is waiting, wake him */
554 Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
555 dev->dev_blocked = BST_MOUNT;
556 bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev_name(dev));
557 pthread_cond_signal(&dev->wait_next_vol);
560 /* In both of these two cases, we (the user) unmounted the Volume */
561 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
563 /* We freed the device, so reopen it and wake any waiting threads */
564 if (open_dev(dev, NULL, OPEN_READ_WRITE) < 0) {
565 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
569 read_dev_volume_label(dcr);
570 if (dev->dev_blocked == BST_UNMOUNTED) {
571 /* We blocked the device, so unblock it */
572 Dmsg0(100, "Unmounted. Unblocking device\n");
573 read_label(dcr); /* this should not be necessary */
576 Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
577 dev->dev_blocked = BST_MOUNT;
579 if (dev->is_labeled()) {
580 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
581 dev_name(dev), dev->VolHdr.VolName);
583 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
584 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
587 pthread_cond_signal(&dev->wait_next_vol);
590 case BST_DOING_ACQUIRE:
591 bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"),
595 case BST_WRITING_LABEL:
596 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev_name(dev));
599 case BST_NOT_BLOCKED:
600 if (dev->is_open()) {
601 if (dev->is_labeled()) {
602 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
603 dev_name(dev), dev->VolHdr.VolName);
605 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
606 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
610 if (!dev_is_tape(dev)) {
611 bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
614 if (open_dev(dev, NULL, OPEN_READ_WRITE) < 0) {
615 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
620 if (dev->is_labeled()) {
621 bnet_fsend(dir, _("3001 Device %s is already mounted with Volume \"%s\"\n"),
622 dev_name(dev), dev->VolHdr.VolName);
624 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
625 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
632 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
637 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
640 pm_strcpy(jcr->errmsg, dir->msg);
641 bnet_fsend(dir, _("3909 Error scanning mount command: %s\n"), jcr->errmsg);
643 bnet_sig(dir, BNET_EOD);
648 * unmount command from Director
650 static bool unmount_cmd(JCR *jcr)
653 BSOCK *dir = jcr->dir_bsock;
656 if (sscanf(dir->msg, "unmount %127s", devname.c_str()) == 1) {
657 dev = find_device(jcr, devname);
659 P(dev->mutex); /* Use P to avoid indefinite block */
660 if (!dev->is_open()) {
661 Dmsg0(90, "Device already unmounted\n");
662 bnet_fsend(dir, _("3901 Device \"%s\" is already unmounted.\n"), dev_name(dev));
664 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
665 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
667 open_dev(dev, NULL, 0); /* fake open for close */
668 offline_or_rewind_dev(dev);
669 force_close_dev(dev);
670 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
671 bnet_fsend(dir, _("3001 Device \"%s\" unmounted.\n"), dev_name(dev));
673 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
674 bnet_fsend(dir, _("3902 Device \"%s\" is busy in acquire.\n"), dev_name(dev));
676 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
677 bnet_fsend(dir, _("3903 Device \"%s\" is being labeled.\n"), dev_name(dev));
679 } else if (dev->is_busy()) {
680 send_dir_busy_message(dir, dev);
681 } else { /* device not being used */
682 Dmsg0(90, "Device not in use, unmounting\n");
683 /* On FreeBSD, I am having ASSERT() failures in block_device()
684 * and I can only imagine that the thread id that we are
685 * leaving in no_wait_id is being re-used. So here,
686 * we simply do it by hand. Gross, but a solution.
688 /* block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
689 dev->dev_blocked = BST_UNMOUNTED;
691 open_dev(dev, NULL, 0); /* fake open for close */
692 offline_or_rewind_dev(dev);
693 force_close_dev(dev);
694 bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
698 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
701 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
702 pm_strcpy(jcr->errmsg, dir->msg);
703 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
705 bnet_sig(dir, BNET_EOD);
710 * Release command from Director. This rewinds the device and if
711 * configured does a offline and ensures that Bacula will
712 * re-read the label of the tape before continuing. This gives
713 * the operator the chance to change the tape anytime before the
716 static bool release_cmd(JCR *jcr)
719 BSOCK *dir = jcr->dir_bsock;
722 if (sscanf(dir->msg, "release %127s", devname.c_str()) == 1) {
723 dev = find_device(jcr, devname);
725 P(dev->mutex); /* Use P to avoid indefinite block */
726 if (!dev->is_open()) {
727 Dmsg0(90, "Device already released\n");
728 bnet_fsend(dir, _("3911 Device %s already released.\n"), dev_name(dev));
730 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
731 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
732 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
734 bnet_fsend(dir, _("3912 Device %s waiting for mount.\n"), dev_name(dev));
736 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
737 bnet_fsend(dir, _("3913 Device %s is busy in acquire.\n"), dev_name(dev));
739 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
740 bnet_fsend(dir, _("3914 Device %s is being labeled.\n"), dev_name(dev));
742 } else if (dev->is_busy()) {
743 send_dir_busy_message(dir, dev);
744 } else { /* device not being used */
745 Dmsg0(90, "Device not in use, unmounting\n");
746 release_volume(jcr->dcr);
747 bnet_fsend(dir, _("3012 Device %s released.\n"), dev_name(dev));
751 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
754 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
755 pm_strcpy(jcr->errmsg, dir->msg);
756 bnet_fsend(dir, _("3917 Error scanning release command: %s\n"), jcr->errmsg);
758 bnet_sig(dir, BNET_EOD);
765 * Autochanger command from Director
767 static bool autochanger_cmd(JCR *jcr)
770 BSOCK *dir = jcr->dir_bsock;
774 if (sscanf(dir->msg, "autochanger list %127s ", devname.c_str()) == 1) {
775 dev = find_device(jcr, devname);
778 P(dev->mutex); /* Use P to avoid indefinite block */
779 if (!dev_is_tape(dev)) {
780 bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"), dev_name(dev));
781 } else if (!dev->is_open()) {
782 autochanger_list(dcr, dir);
783 /* Under certain "safe" conditions, we can steal the lock */
784 } else if (dev->dev_blocked &&
785 (dev->dev_blocked == BST_UNMOUNTED ||
786 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
787 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
788 autochanger_list(dcr, dir);
789 } else if (dev->is_busy()) {
790 send_dir_busy_message(dir, dev);
791 } else { /* device not being used */
792 autochanger_list(dcr, dir);
796 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
798 } else { /* error on scanf */
799 pm_strcpy(jcr->errmsg, dir->msg);
800 bnet_fsend(dir, _("3908 Error scanning autocharger list command: %s\n"),
803 bnet_sig(dir, BNET_EOD);
808 * Read and return the Volume label
810 static bool readlabel_cmd(JCR *jcr)
813 BSOCK *dir = jcr->dir_bsock;
817 if (sscanf(dir->msg, "readlabel %127s Slot=%d", devname.c_str(), &Slot) == 2) {
818 dev = find_device(jcr, devname);
820 P(dev->mutex); /* Use P to avoid indefinite block */
821 if (!dev->is_open()) {
822 read_volume_label(jcr, dev, Slot);
823 force_close_dev(dev);
824 /* Under certain "safe" conditions, we can steal the lock */
825 } else if (dev->dev_blocked &&
826 (dev->dev_blocked == BST_UNMOUNTED ||
827 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
828 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
829 read_volume_label(jcr, dev, Slot);
830 } else if (dev->is_busy()) {
831 send_dir_busy_message(dir, dev);
832 } else { /* device not being used */
833 read_volume_label(jcr, dev, Slot);
837 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
840 pm_strcpy(jcr->errmsg, dir->msg);
841 bnet_fsend(dir, _("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
843 bnet_sig(dir, BNET_EOD);
848 * Read the tape label
850 * Enter with the mutex set
852 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
854 BSOCK *dir = jcr->dir_bsock;
859 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
861 if (!try_autoload_device(jcr, Slot, "")) {
862 goto bail_out; /* error */
865 dev->state &= ~ST_LABEL; /* force read of label */
866 switch (read_dev_volume_label(dcr)) {
868 /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
869 bnet_fsend(dir, _("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolName, Slot);
870 Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolName);
873 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
874 dev_name(dev), jcr->errmsg);
879 give_back_device_lock(dev, &hold);
883 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName)
886 BSOCK *dir = jcr->dir_bsock;
887 DEVICE *dev = dcr->dev;
889 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
890 dcr->VolCatInfo.Slot = slot;
891 dcr->VolCatInfo.InChanger = slot > 0;
892 if (autoload_device(dcr, 0, dir) < 0) { /* autoload if possible */
896 /* Ensure that the device is open -- autoload_device() closes it */
897 for ( ; !dev->is_open(); ) {
898 if (open_dev(dev, dcr->VolumeName, OPEN_READ_WRITE) < 0) {
899 bnet_fsend(dir, _("3910 Unable to open device %s. ERR=%s\n"),
900 dev_name(dev), dev->strerror());
907 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev)
909 if (dev->can_read()) {
910 bnet_fsend(dir, _("3911 Device \"%s\" is busy reading.\n"),
913 bnet_fsend(dir, _("3912 Device \"%s\" is busy with %d writer(s).\n"),
914 dev_name(dev), dev->num_writers);