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, 2001, 2002 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 jcr->JobStatus = JS_Cancelled;
212 if (!jcr->authenticated && jcr->JobStatus == 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 bnet_fsend(dir, _("3000 Job %s Status=%c marked to be cancelled.\n"),
220 jcr->Job, oldStatus);
224 bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
226 bnet_sig(dir, BNET_EOD);
234 static int label_cmd(JCR *jcr)
236 POOLMEM *dname, *volname, *poolname, *mtype;
237 BSOCK *dir = jcr->dir_bsock;
243 dname = get_memory(dir->msglen+1);
244 volname = get_memory(dir->msglen+1);
245 poolname = get_memory(dir->msglen+1);
246 mtype = get_memory(dir->msglen+1);
247 if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
248 dname, volname, poolname, mtype, &slot) == 5) {
249 unbash_spaces(dname);
250 unbash_spaces(volname);
251 unbash_spaces(poolname);
252 unbash_spaces(mtype);
255 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
256 /* Find resource, and make sure we were able to open it */
257 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
258 Dmsg1(20, "Found device %s\n", device->hdr.name);
265 /******FIXME**** compare MediaTypes */
266 jcr->device = device;
269 P(dev->mutex); /* Use P to avoid indefinite block */
270 if (!(dev->state & ST_OPENED)) {
271 if (open_dev(dev, volname, READ_WRITE) < 0) {
272 bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
274 label_volume_if_ok(jcr, dev, volname, poolname, slot);
275 force_close_dev(dev);
277 /* Under certain "safe" conditions, we can steal the lock */
278 } else if (dev->dev_blocked &&
279 (dev->dev_blocked == BST_UNMOUNTED ||
280 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
281 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
282 label_volume_if_ok(jcr, dev, volname, poolname, slot);
283 } else if (dev->state & ST_READ || dev->num_writers) {
284 if (dev->state & ST_READ) {
285 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
288 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
289 dev_name(dev), dev->num_writers);
291 } else { /* device not being used */
292 label_volume_if_ok(jcr, dev, volname, poolname, slot);
296 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
299 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
300 strcpy(dname, dir->msg);
301 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), dname);
304 free_memory(volname);
305 free_memory(poolname);
307 bnet_sig(dir, BNET_EOD);
312 * Read the tape label and determine if we can safely
313 * label the tape (not a Bacula volume), then label it.
315 * Enter with the mutex set
317 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname,
320 BSOCK *dir = jcr->dir_bsock;
324 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
326 strcpy(jcr->VolumeName, vname);
327 jcr->VolCatInfo.Slot = slot;
328 autoload_device(jcr, dev, 0, dir); /* autoload if possible */
329 block = new_block(dev);
331 /* Ensure that the device is open -- not autoload_device() closes it */
332 for ( ; !(dev->state & ST_OPENED); ) {
333 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
334 if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
337 bnet_fsend(dir, _("3903 Unable to open device %s. ERR=%s\n"),
338 dev_name(dev), strerror_dev(dev));
343 /* See what we have for a Volume */
344 switch (read_dev_volume_label(jcr, dev, block)) {
346 case VOL_VERSION_ERROR:
347 case VOL_LABEL_ERROR:
349 bnet_fsend(dir, _("3901 Cannot label Volume because it is \
350 already labeled: %s\n"), dev->VolHdr.VolName);
354 if (!write_volume_label_to_dev(jcr, jcr->device, vname, poolname)) {
355 bnet_fsend(dir, _("3903 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
358 strcpy(jcr->VolumeName, vname);
359 bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"),
360 vname, dev->dev_name);
363 bnet_fsend(dir, _("3902 Cannot label Volume. \
364 Unknown status %d from read_volume_label()\n"), jcr->label_status);
369 return_device_lock(dev, &hold);
374 * Read the tape label
376 * Enter with the mutex set
378 static int read_label(JCR *jcr, DEVICE *dev)
381 BSOCK *dir = jcr->dir_bsock;
386 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
389 jcr->VolumeName[0] = 0;
390 block = new_block(dev);
391 dev->state &= ~ST_LABEL; /* force read of label */
392 switch (read_dev_volume_label(jcr, dev, block)) {
394 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
398 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s\n"),
399 dev->dev_name, jcr->errmsg);
405 return_device_lock(dev, &hold);
411 * Mount command from Director
413 static int mount_cmd(JCR *jcr)
416 BSOCK *dir = jcr->dir_bsock;
421 dev_name = (char *) get_memory(dir->msglen);
422 if (sscanf(dir->msg, "mount %s", dev_name) == 1) {
423 unbash_spaces(dev_name);
426 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
427 /* Find resource, and make sure we were able to open it */
428 if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
429 Dmsg1(20, "Found device %s\n", device->hdr.name);
436 jcr->device = device;
438 P(dev->mutex); /* Use P to avoid indefinite block */
439 switch (dev->dev_blocked) { /* device blocked? */
441 case BST_WAITING_FOR_SYSOP:
442 /* Someone is waiting, wake him */
443 Dmsg0(90, "Waiting for mount attempt to wake thread\n");
444 pthread_cond_signal(&dev->wait_next_vol);
445 bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev->dev_name);
448 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
450 /* We freed the device, so reopen it and wake any waiting threads */
451 if (open_dev(dev, NULL, READ_WRITE) < 0) {
452 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
456 block = new_block(dev);
457 read_dev_volume_label(jcr, dev, block);
459 if (dev->dev_blocked == BST_UNMOUNTED) {
460 Dmsg0(90, "Unmounted unblocking device\n");
461 read_label(jcr, dev);
464 Dmsg0(90, "Unmounted waiting for mount attempt to wake thread\n");
465 dev->dev_blocked = BST_WAITING_FOR_SYSOP;
466 pthread_cond_signal(&dev->wait_next_vol);
468 if (dev->state & ST_LABEL) {
469 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
470 dev->dev_name, dev->VolHdr.VolName);
472 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"),
477 case BST_DOING_ACQUIRE:
478 bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"),
482 case BST_WRITING_LABEL:
483 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev->dev_name);
486 case BST_NOT_BLOCKED:
487 if (dev->state & ST_OPENED) {
488 if (dev->state & ST_LABEL) {
489 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
490 dev->dev_name, dev->VolHdr.VolName);
492 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"),
496 if (!dev_is_tape(dev)) {
497 bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
500 if (open_dev(dev, NULL, READ_WRITE) < 0) {
501 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
505 read_label(jcr, dev);
506 if (dev->state & ST_LABEL) {
507 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
508 dev->dev_name, dev->VolHdr.VolName);
510 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"),
517 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
522 bnet_fsend(dir, _("3999 Device %s not found\n"), dev_name);
525 strcpy(dev_name, dir->msg);
526 bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), dev_name);
528 free_memory(dev_name);
529 bnet_sig(dir, BNET_EOD);
534 * unmount command from Director
536 static int unmount_cmd(JCR *jcr)
539 BSOCK *dir = jcr->dir_bsock;
544 dname = (char *) get_memory(dir->msglen+1);
545 if (sscanf(dir->msg, "unmount %s", dname) == 1) {
546 unbash_spaces(dname);
549 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
550 /* Find resource, and make sure we were able to open it */
551 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
552 Dmsg1(20, "Found device %s\n", device->hdr.name);
559 jcr->device = device;
561 P(dev->mutex); /* Use P to avoid indefinite block */
562 if (!(dev->state & ST_OPENED)) {
563 Dmsg0(90, "Device already unmounted\n");
564 bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"), dev_name(dev));
566 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
567 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
569 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
572 force_close_dev(dev);
573 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
574 bnet_fsend(dir, _("3001 Device %s unmounted.\n"), dev_name(dev));
576 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
577 bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
580 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
581 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
584 } else if (dev->state & ST_READ || dev->num_writers) {
585 if (dev->state & ST_READ) {
586 Dmsg0(90, "Device in read mode\n");
587 bnet_fsend(dir, _("3904 Device %s is busy with 1 reader.\n"),
590 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
591 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
592 dev_name(dev), dev->num_writers);
595 } else { /* device not being used */
596 Dmsg0(90, "Device not in use, unmounting\n");
597 block_device(dev, BST_UNMOUNTED);
598 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
601 force_close_dev(dev);
602 bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
606 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
609 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
610 strcpy(dname, dir->msg);
611 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), dname);
614 bnet_sig(dir, BNET_EOD);
619 * Status command from Director
621 static int status_cmd(JCR *jcr)
625 int found, bps, sec, bpb;
626 BSOCK *user = jcr->dir_bsock;
627 char dt[MAX_TIME_LENGTH];
628 char b1[30], b2[30], b3[30];
630 bnet_fsend(user, "\n%s Version: " VERSION " (" DATE ")\n", my_name);
631 bstrftime(dt, sizeof(dt), daemon_start_time);
632 bnet_fsend(user, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
633 last_job.NumJobs == 1 ? "" : "s");
634 if (last_job.NumJobs > 0) {
637 bstrftime(dt, sizeof(dt), last_job.end_time);
638 bnet_fsend(user, _("Last Job %s finished at %s\n"), last_job.Job, dt);
640 jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
641 bnet_fsend(user, _(" Files=%s Bytes=%s Termination Status=%s\n"),
642 edit_uint64_with_commas(last_job.JobFiles, b1),
643 edit_uint64_with_commas(last_job.JobBytes, b2),
648 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
651 if (dev->state & ST_OPENED) {
652 if (dev->state & ST_LABEL) {
653 bnet_fsend(user, _("Device %s is mounted with Volume %s\n"),
654 dev_name(dev), dev->VolHdr.VolName);
656 bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"), dev_name(dev));
658 send_blocked_status(jcr, dev);
659 bpb = dev->VolCatInfo.VolCatBlocks;
663 bpb = dev->VolCatInfo.VolCatBytes / bpb;
664 bnet_fsend(user, _(" Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
665 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
666 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
667 edit_uint64_with_commas(bpb, b3));
668 bnet_fsend(user, _(" Positioned at File=%s Block=%s\n"),
669 edit_uint64_with_commas(dev->file, b1),
670 edit_uint64_with_commas(dev->block_num, b2));
673 bnet_fsend(user, _("Device %s is not open.\n"), dev_name(dev));
674 send_blocked_status(jcr, dev);
682 /* NOTE, we reuse a calling argument jcr. Be warned! */
683 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
684 if (jcr->JobStatus == JS_WaitFD) {
685 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
686 job_type_to_str(jcr->JobType), jcr->Job);
689 bnet_fsend(user, _("%s %s job %s is using device %s\n"),
690 job_level_to_str(jcr->JobLevel),
691 job_type_to_str(jcr->JobType),
692 jcr->Job, jcr->device->device_name);
693 sec = time(NULL) - jcr->run_time;
697 bps = jcr->JobBytes / sec;
698 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
699 edit_uint64_with_commas(jcr->JobFiles, b1),
700 edit_uint64_with_commas(jcr->JobBytes, b2),
701 edit_uint64_with_commas(bps, b3));
704 if (jcr->file_bsock) {
705 bnet_fsend(user, " FDReadSeqNo=%" lld " fd=%d\n",
706 jcr->file_bsock->read_seqno, jcr->file_bsock->fd);
708 bnet_fsend(user, " FDSocket closed\n");
712 free_locked_jcr(jcr);
716 bnet_fsend(user, _("No jobs running.\n"));
720 bnet_fsend(user, "\n\n");
721 dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
723 bnet_fsend(user, "====\n");
725 bnet_sig(user, BNET_EOD);
729 static void send_blocked_status(JCR *jcr, DEVICE *dev)
731 BSOCK *user = jcr->dir_bsock;
733 switch (dev->dev_blocked) {
735 bnet_fsend(user, _(" Device is BLOCKED. User unmounted.\n"));
737 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
738 bnet_fsend(user, _(" Device is BLOCKED. User unmounted during wait for media/mount.\n"));
740 case BST_WAITING_FOR_SYSOP:
741 if (jcr->JobStatus == JS_WaitMount) {
742 bnet_fsend(user, _(" Device is BLOCKED waiting for mount.\n"));
744 bnet_fsend(user, _(" Device is BLOCKED waiting for appendable media.\n"));
747 case BST_DOING_ACQUIRE:
748 bnet_fsend(user, _(" Device is being initialized.\n"));
750 case BST_WRITING_LABEL:
751 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));