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) 2000-2003 Kern Sibbald and John Walker
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";
59 /* Imported functions */
60 extern void terminate_child();
61 extern int job_cmd(JCR *jcr);
63 /* Forward referenced functions */
64 static int label_cmd(JCR *jcr);
65 static int setdebug_cmd(JCR *jcr);
66 static int cancel_cmd(JCR *cjcr);
67 static int mount_cmd(JCR *jcr);
68 static int unmount_cmd(JCR *jcr);
69 static int status_cmd(JCR *sjcr);
70 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname,
72 static void send_blocked_status(JCR *jcr, DEVICE *dev);
76 int (*func)(JCR *jcr);
80 * The following are the recognized commands from the Director.
82 static struct s_cmds cmds[] = {
83 {"JobId=", job_cmd}, /* start Job */
84 {"setdebug=", setdebug_cmd}, /* set debug level */
85 {"cancel", cancel_cmd},
86 {"label", label_cmd}, /* label a tape */
88 {"unmount", unmount_cmd},
89 {"status", status_cmd},
90 {NULL, NULL} /* list terminator */
95 * Connection request. We accept connections either from the
96 * Director or a Client.
98 * Note, we are running as a seperate thread of the Storage daemon.
99 * and it is because a Director has made a connection with
100 * us on the "Message" channel.
102 * Basic tasks done here:
103 * - Create a JCR record
104 * - Authenticate the Director
105 * - We wait for a command
106 * - We execute the command
107 * - We continue or exit depending on the return status
109 void *connection_request(void *arg)
111 BSOCK *bs = (BSOCK *)arg;
115 char name[MAX_NAME_LENGTH];
117 if (bnet_recv(bs) <= 0) {
118 Emsg0(M_ERROR, 0, "Connection request failed.\n");
123 * See if this is a File daemon connection
125 if (sscanf(bs->msg, "Hello Start Job %127s calling\n", name) == 1) {
126 handle_filed_connection(bs, name);
130 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
131 jcr->dir_bsock = bs; /* save Director bsock */
133 Dmsg0(1000, "stored in start_job\n");
136 * Authenticate the Director
138 if (!authenticate_director(jcr)) {
139 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
143 Dmsg0(90, "Message channel init completed.\n");
145 for (quit=0; !quit;) {
148 if ((bnet_stat = bnet_recv(bs)) <= 0) {
149 break; /* connection terminated */
151 Dmsg1(9, "<dird: %s\n", bs->msg);
153 for (i=0; cmds[i].cmd; i++) {
154 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
155 if (!cmds[i].func(jcr)) { /* do command */
156 quit = TRUE; /* error, get out */
157 Dmsg1(90, "Command %s requsts quit\n", cmds[i].cmd);
159 found = TRUE; /* indicate command found */
163 if (!found) { /* command not found */
164 bnet_fsend(bs, derrmsg);
169 if (bnet_stat != BNET_TERMINATE) {
170 bnet_sig(bs, BNET_TERMINATE);
177 * Set debug level as requested by the Director
180 static int setdebug_cmd(JCR *jcr)
182 BSOCK *dir = jcr->dir_bsock;
185 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
186 if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
187 bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
191 return bnet_fsend(dir, OKsetdebug, level);
198 static int cancel_cmd(JCR *cjcr)
200 BSOCK *dir = cjcr->dir_bsock;
202 char Job[MAX_NAME_LENGTH];
205 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
206 if (!(jcr=get_jcr_by_full_name(Job))) {
207 bnet_fsend(dir, _("3992 Job %s not found.\n"), Job);
210 oldStatus = jcr->JobStatus;
211 set_jcr_job_status(jcr, JS_Cancelled);
212 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
213 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
216 if (jcr->file_bsock) {
217 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
219 /* If thread waiting on mount, wake him */
220 if (jcr->device && jcr->device->dev &&
221 (jcr->device->dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
222 jcr->device->dev->dev_blocked == BST_UNMOUNTED ||
223 jcr->device->dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
224 pthread_cond_signal(&jcr->device->dev->wait_next_vol);
226 bnet_fsend(dir, _("3000 Job %s marked to be cancelled.\n"), jcr->Job);
230 bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
232 bnet_sig(dir, BNET_EOD);
240 static int label_cmd(JCR *jcr)
242 POOLMEM *dname, *volname, *poolname, *mtype;
243 BSOCK *dir = jcr->dir_bsock;
249 dname = get_memory(dir->msglen+1);
250 volname = get_memory(dir->msglen+1);
251 poolname = get_memory(dir->msglen+1);
252 mtype = get_memory(dir->msglen+1);
253 if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
254 dname, volname, poolname, mtype, &slot) == 5) {
255 unbash_spaces(dname);
256 unbash_spaces(volname);
257 unbash_spaces(poolname);
258 unbash_spaces(mtype);
261 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
262 /* Find resource, and make sure we were able to open it */
263 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
264 Dmsg1(20, "Found device %s\n", device->hdr.name);
271 /******FIXME**** compare MediaTypes */
272 jcr->device = device;
275 P(dev->mutex); /* Use P to avoid indefinite block */
276 if (!(dev->state & ST_OPENED)) {
277 if (open_dev(dev, volname, READ_WRITE) < 0) {
278 bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
280 label_volume_if_ok(jcr, dev, volname, poolname, slot);
281 force_close_dev(dev);
283 /* Under certain "safe" conditions, we can steal the lock */
284 } else if (dev->dev_blocked &&
285 (dev->dev_blocked == BST_UNMOUNTED ||
286 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
287 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
288 label_volume_if_ok(jcr, dev, volname, poolname, slot);
289 } else if (dev->state & ST_READ || dev->num_writers) {
290 if (dev->state & ST_READ) {
291 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
294 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
295 dev_name(dev), dev->num_writers);
297 } else { /* device not being used */
298 label_volume_if_ok(jcr, dev, volname, poolname, slot);
302 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
305 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
306 strcpy(dname, dir->msg);
307 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), dname);
310 free_memory(volname);
311 free_memory(poolname);
313 bnet_sig(dir, BNET_EOD);
318 * Read the tape label and determine if we can safely
319 * label the tape (not a Bacula volume), then label it.
321 * Enter with the mutex set
323 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname,
326 BSOCK *dir = jcr->dir_bsock;
330 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
332 strcpy(jcr->VolumeName, vname);
333 jcr->VolCatInfo.Slot = slot;
334 autoload_device(jcr, dev, 0, dir); /* autoload if possible */
335 block = new_block(dev);
337 /* Ensure that the device is open -- not autoload_device() closes it */
338 for ( ; !(dev->state & ST_OPENED); ) {
339 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
340 if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
343 bnet_fsend(dir, _("3903 Unable to open device %s. ERR=%s\n"),
344 dev_name(dev), strerror_dev(dev));
349 /* See what we have for a Volume */
350 switch (read_dev_volume_label(jcr, dev, block)) {
352 case VOL_VERSION_ERROR:
353 case VOL_LABEL_ERROR:
355 bnet_fsend(dir, _("3901 Cannot label Volume because it is \
356 already labeled: %s\n"), dev->VolHdr.VolName);
360 if (!write_volume_label_to_dev(jcr, jcr->device, vname, poolname)) {
361 bnet_fsend(dir, _("3903 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
364 strcpy(jcr->VolumeName, vname);
365 bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"),
366 vname, dev->dev_name);
369 bnet_fsend(dir, _("3902 Cannot label Volume. \
370 Unknown status %d from read_volume_label()\n"), jcr->label_status);
375 return_device_lock(dev, &hold);
380 * Read the tape label
382 * Enter with the mutex set
384 static int read_label(JCR *jcr, DEVICE *dev)
387 BSOCK *dir = jcr->dir_bsock;
392 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
395 jcr->VolumeName[0] = 0;
396 block = new_block(dev);
397 dev->state &= ~ST_LABEL; /* force read of label */
398 switch (read_dev_volume_label(jcr, dev, block)) {
400 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
404 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s\n"),
405 dev->dev_name, jcr->errmsg);
411 return_device_lock(dev, &hold);
417 * Mount command from Director
419 static int mount_cmd(JCR *jcr)
422 BSOCK *dir = jcr->dir_bsock;
427 dev_name = (char *) get_memory(dir->msglen);
428 if (sscanf(dir->msg, "mount %s", dev_name) == 1) {
429 unbash_spaces(dev_name);
432 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
433 /* Find resource, and make sure we were able to open it */
434 if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
435 Dmsg1(20, "Found device %s\n", device->hdr.name);
442 jcr->device = device;
444 P(dev->mutex); /* Use P to avoid indefinite block */
445 switch (dev->dev_blocked) { /* device blocked? */
447 case BST_WAITING_FOR_SYSOP:
448 /* Someone is waiting, wake him */
449 Dmsg0(90, "Waiting for mount attempt to wake thread\n");
450 pthread_cond_signal(&dev->wait_next_vol);
451 bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev->dev_name);
454 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
456 /* We freed the device, so reopen it and wake any waiting threads */
457 if (open_dev(dev, NULL, READ_WRITE) < 0) {
458 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
462 block = new_block(dev);
463 read_dev_volume_label(jcr, dev, block);
465 if (dev->dev_blocked == BST_UNMOUNTED) {
466 Dmsg0(90, "Unmounted unblocking device\n");
467 read_label(jcr, dev);
470 Dmsg0(90, "Unmounted waiting for mount attempt to wake thread\n");
471 dev->dev_blocked = BST_WAITING_FOR_SYSOP;
472 pthread_cond_signal(&dev->wait_next_vol);
474 if (dev->state & ST_LABEL) {
475 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
476 dev->dev_name, dev->VolHdr.VolName);
478 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
479 "Try unmounting and remounting the Volume.\n"),
484 case BST_DOING_ACQUIRE:
485 bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"),
489 case BST_WRITING_LABEL:
490 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev->dev_name);
493 case BST_NOT_BLOCKED:
494 if (dev->state & ST_OPENED) {
495 if (dev->state & ST_LABEL) {
496 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
497 dev->dev_name, dev->VolHdr.VolName);
499 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
500 "Try unmounting and remounting the Volume.\n"),
504 if (!dev_is_tape(dev)) {
505 bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
508 if (open_dev(dev, NULL, READ_WRITE) < 0) {
509 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
513 read_label(jcr, dev);
514 if (dev->state & ST_LABEL) {
515 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
516 dev->dev_name, dev->VolHdr.VolName);
518 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
519 "Try unmounting and remounting the Volume.\n"),
526 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
531 bnet_fsend(dir, _("3999 Device %s not found\n"), dev_name);
534 strcpy(dev_name, dir->msg);
535 bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), dev_name);
537 free_memory(dev_name);
538 bnet_sig(dir, BNET_EOD);
543 * unmount command from Director
545 static int unmount_cmd(JCR *jcr)
548 BSOCK *dir = jcr->dir_bsock;
553 dname = (char *) get_memory(dir->msglen+1);
554 if (sscanf(dir->msg, "unmount %s", dname) == 1) {
555 unbash_spaces(dname);
558 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
559 /* Find resource, and make sure we were able to open it */
560 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
561 Dmsg1(20, "Found device %s\n", device->hdr.name);
568 jcr->device = device;
570 P(dev->mutex); /* Use P to avoid indefinite block */
571 if (!(dev->state & ST_OPENED)) {
572 Dmsg0(90, "Device already unmounted\n");
573 bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"), dev_name(dev));
575 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
576 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
578 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
581 force_close_dev(dev);
582 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
583 bnet_fsend(dir, _("3001 Device %s unmounted.\n"), dev_name(dev));
585 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
586 bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
589 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
590 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
593 } else if (dev->state & ST_READ || dev->num_writers) {
594 if (dev->state & ST_READ) {
595 Dmsg0(90, "Device in read mode\n");
596 bnet_fsend(dir, _("3904 Device %s is busy with 1 reader.\n"),
599 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
600 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
601 dev_name(dev), dev->num_writers);
604 } else { /* device not being used */
605 Dmsg0(90, "Device not in use, unmounting\n");
606 block_device(dev, BST_UNMOUNTED);
607 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
610 force_close_dev(dev);
611 bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
615 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
618 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
619 strcpy(dname, dir->msg);
620 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), dname);
623 bnet_sig(dir, BNET_EOD);
628 * Status command from Director
630 static int status_cmd(JCR *jcr)
634 int found, bps, sec, bpb;
635 BSOCK *user = jcr->dir_bsock;
636 char dt[MAX_TIME_LENGTH];
637 char b1[30], b2[30], b3[30];
639 bnet_fsend(user, "\n%s Version: " VERSION " (" BDATE ")\n", my_name);
640 bstrftime(dt, sizeof(dt), daemon_start_time);
641 bnet_fsend(user, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
642 last_job.NumJobs == 1 ? "" : "s");
643 if (last_job.NumJobs > 0) {
646 bstrftime(dt, sizeof(dt), last_job.end_time);
647 bnet_fsend(user, _("Last Job %s finished at %s\n"), last_job.Job, dt);
649 jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
650 bnet_fsend(user, _(" Files=%s Bytes=%s Termination Status=%s\n"),
651 edit_uint64_with_commas(last_job.JobFiles, b1),
652 edit_uint64_with_commas(last_job.JobBytes, b2),
657 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
658 for (dev=device->dev; dev; dev=dev->next) {
659 if (dev->state & ST_OPENED) {
660 if (dev->state & ST_LABEL) {
661 bnet_fsend(user, _("Device %s is mounted with Volume %s\n"),
662 dev_name(dev), dev->VolHdr.VolName);
664 bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"), dev_name(dev));
666 send_blocked_status(jcr, dev);
667 bpb = dev->VolCatInfo.VolCatBlocks;
671 bpb = dev->VolCatInfo.VolCatBytes / bpb;
672 bnet_fsend(user, _(" Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
673 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
674 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
675 edit_uint64_with_commas(bpb, b3));
676 bnet_fsend(user, _(" Positioned at File=%s Block=%s\n"),
677 edit_uint64_with_commas(dev->file, b1),
678 edit_uint64_with_commas(dev->block_num, b2));
681 bnet_fsend(user, _("Device %s is not open.\n"), dev_name(dev));
682 send_blocked_status(jcr, dev);
690 /* NOTE, we reuse a calling argument jcr. Be warned! */
691 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
692 if (jcr->JobStatus == JS_WaitFD) {
693 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
694 job_type_to_str(jcr->JobType), jcr->Job);
697 bnet_fsend(user, _("%s %s job %s is using device %s volume %s\n"),
698 job_level_to_str(jcr->JobLevel),
699 job_type_to_str(jcr->JobType),
700 jcr->Job, jcr->device->device_name,
702 sec = time(NULL) - jcr->run_time;
706 bps = jcr->JobBytes / sec;
707 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
708 edit_uint64_with_commas(jcr->JobFiles, b1),
709 edit_uint64_with_commas(jcr->JobBytes, b2),
710 edit_uint64_with_commas(bps, b3));
713 if (jcr->file_bsock) {
714 bnet_fsend(user, " FDReadSeqNo=%" lld " fd=%d\n",
715 jcr->file_bsock->read_seqno, jcr->file_bsock->fd);
717 bnet_fsend(user, " FDSocket closed\n");
721 free_locked_jcr(jcr);
725 bnet_fsend(user, _("No jobs running.\n"));
729 bnet_fsend(user, "\n\n");
730 dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
732 bnet_fsend(user, "====\n");
734 bnet_sig(user, BNET_EOD);
738 static void send_blocked_status(JCR *jcr, DEVICE *dev)
740 BSOCK *user = jcr->dir_bsock;
742 switch (dev->dev_blocked) {
744 bnet_fsend(user, _(" Device is BLOCKED. User unmounted.\n"));
746 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
747 bnet_fsend(user, _(" Device is BLOCKED. User unmounted during wait for media/mount.\n"));
749 case BST_WAITING_FOR_SYSOP:
750 if (jcr->JobStatus == JS_WaitMount) {
751 bnet_fsend(user, _(" Device is BLOCKED waiting for mount.\n"));
753 bnet_fsend(user, _(" Device is BLOCKED waiting for appendable media.\n"));
756 case BST_DOING_ACQUIRE:
757 bnet_fsend(user, _(" Device is being initialized.\n"));
759 case BST_WRITING_LABEL:
760 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));