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 * File daemon commands are handled in fdcmd.c
11 * Kern Sibbald, May MMI
17 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation; either version 2 of
22 the License, or (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received a copy of the GNU General Public
30 License along with this program; if not, write to the Free
31 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
39 /* Exported variables */
41 /* Imported variables */
42 extern BSOCK *filed_chan;
43 extern int r_first, r_last;
44 extern struct s_res resources[];
45 extern char my_name[];
46 extern time_t daemon_start_time;
47 extern struct s_last_job last_job;
49 /* Static variables */
50 static char derrmsg[] = "3900 Invalid command\n";
51 static char OKsetdebug[] = "3000 OK setdebug=%d\n";
54 /* Imported functions */
55 extern void terminate_child();
56 extern int job_cmd(JCR *jcr);
58 /* Forward referenced functions */
59 static int label_cmd(JCR *jcr);
60 static int setdebug_cmd(JCR *jcr);
61 static int cancel_cmd(JCR *cjcr);
62 static int mount_cmd(JCR *jcr);
63 static int unmount_cmd(JCR *jcr);
64 static int status_cmd(JCR *sjcr);
65 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname);
69 int (*func)(JCR *jcr);
73 * The following are the recognized commands from the Director.
75 static struct s_cmds cmds[] = {
76 {"JobId=", job_cmd}, /* start Job */
77 {"setdebug=", setdebug_cmd}, /* set debug level */
78 {"cancel", cancel_cmd},
79 {"label", label_cmd}, /* label a tape */
81 {"unmount", unmount_cmd},
82 {"status", status_cmd},
83 {NULL, NULL} /* list terminator */
88 * Connection request. We accept connections either from the
89 * Director or a Client.
91 * Note, we are running as a seperate thread of the Storage daemon.
92 * and it is because a Director has made a connection with
93 * us on the "Message" channel.
95 * Basic tasks done here:
96 * - Create a JCR record
97 * - Authenticate the Director
98 * - We wait for a command
99 * - We execute the command
100 * - We continue or exit depending on the return status
102 void connection_request(void *arg)
104 BSOCK *bs = (BSOCK *)arg;
108 char name[MAX_NAME_LENGTH];
110 if (bnet_recv(bs) <= 0) {
111 Emsg0(M_ERROR, 0, "Connection request failed.\n");
116 * See if this is a File daemon connection
118 if (sscanf(bs->msg, "Hello Start Job %127s calling\n", name) == 1) {
119 handle_filed_connection(bs, name);
123 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
124 jcr->dir_bsock = bs; /* save Director bsock */
126 Dmsg0(1000, "stored in start_job\n");
129 * Authenticate the Director
131 if (!authenticate_director(jcr)) {
132 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
136 Dmsg0(90, "Message channel init completed.\n");
138 for (quit=0; !quit;) {
141 if ((bnet_stat = bnet_recv(bs)) <= 0) {
142 break; /* connection terminated */
144 Dmsg1(9, "<dird: %s\n", bs->msg);
146 for (i=0; cmds[i].cmd; i++) {
147 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
148 if (!cmds[i].func(jcr)) { /* do command */
149 quit = TRUE; /* error, get out */
150 Dmsg1(90, "Command %s requsts quit\n", cmds[i].cmd);
152 found = TRUE; /* indicate command found */
156 if (!found) { /* command not found */
157 bnet_fsend(bs, derrmsg);
162 if (bnet_stat != BNET_TERMINATE) {
163 bnet_sig(bs, BNET_TERMINATE);
170 * Set debug level as requested by the Director
173 static int setdebug_cmd(JCR *jcr)
175 BSOCK *dir = jcr->dir_bsock;
178 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
179 if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
180 bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
184 return bnet_fsend(dir, OKsetdebug, level);
191 static int cancel_cmd(JCR *cjcr)
193 BSOCK *dir = cjcr->dir_bsock;
195 char Job[MAX_NAME_LENGTH];
198 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
199 if (!(jcr=get_jcr_by_full_name(Job))) {
200 bnet_fsend(dir, _("3992 Job %s not found.\n"), Job);
203 oldStatus = jcr->JobStatus;
204 jcr->JobStatus = JS_Cancelled;
205 if (!jcr->authenticated && jcr->JobStatus == JS_WaitFD) {
206 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
209 if (jcr->file_bsock) {
210 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
212 bnet_fsend(dir, _("3000 Job %s Status=%c marked to be cancelled.\n"),
213 jcr->Job, oldStatus);
217 bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
219 bnet_sig(dir, BNET_EOD);
227 static int label_cmd(JCR *jcr)
229 POOLMEM *dname, *volname, *poolname, *mtype;
230 BSOCK *dir = jcr->dir_bsock;
236 dname = get_memory(dir->msglen+1);
237 volname = get_memory(dir->msglen+1);
238 poolname = get_memory(dir->msglen+1);
239 mtype = get_memory(dir->msglen+1);
240 if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
241 dname, volname, poolname, mtype, &slot) == 5) {
242 unbash_spaces(dname);
243 unbash_spaces(volname);
244 unbash_spaces(poolname);
245 unbash_spaces(mtype);
248 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
249 /* Find resource, and make sure we were able to open it */
250 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
251 Dmsg1(20, "Found device %s\n", device->hdr.name);
258 /******FIXME**** compare MediaTypes */
259 jcr->device = device;
262 /* *****FIXME***** add autochanger code */
266 if (!(dev->state & ST_OPENED)) {
268 } else if (dev->dev_blocked &&
269 dev->dev_blocked != BST_DOING_ACQUIRE) { /* device blocked? */
271 } else if (dev->state & ST_READ || dev->num_writers) {
272 if (dev->state & ST_READ) {
273 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
276 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
277 dev_name(dev), dev->num_writers);
279 } else { /* device not being used */
283 new_steal_device_lock(dev, &hold, BST_WRITING_LABEL);
284 if (!(dev->state & ST_OPENED)) {
285 if (open_dev(dev, volname, READ_WRITE) < 0) {
286 bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
288 label_volume_if_ok(jcr, dev, volname, poolname);
289 force_close_dev(dev);
292 label_volume_if_ok(jcr, dev, volname, poolname);
294 new_return_device_lock(dev, &hold);
299 if (!(dev->state & ST_OPENED)) {
300 if (open_dev(dev, volname, READ_WRITE) < 0) {
301 bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
303 label_volume_if_ok(jcr, dev, volname, poolname);
304 force_close_dev(dev);
306 } else if (dev->dev_blocked &&
307 dev->dev_blocked != BST_DOING_ACQUIRE) { /* device blocked? */
308 label_volume_if_ok(jcr, dev, volname, poolname);
309 } else if (dev->state & ST_READ || dev->num_writers) {
310 if (dev->state & ST_READ) {
311 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
314 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
315 dev_name(dev), dev->num_writers);
317 } else { /* device not being used */
318 label_volume_if_ok(jcr, dev, volname, poolname);
323 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
326 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
327 strcpy(dname, dir->msg);
328 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), dname);
331 free_memory(volname);
332 free_memory(poolname);
334 bnet_sig(dir, BNET_EOD);
339 * Read the tape label and determine if we can safely
340 * label the tape (not a Bacula volume), then label it.
342 * Enter with the mutex set
344 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname)
346 BSOCK *dir = jcr->dir_bsock;
350 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
352 strcpy(jcr->VolumeName, vname);
353 block = new_block(dev);
354 switch (read_dev_volume_label(jcr, dev, block)) {
356 case VOL_VERSION_ERROR:
357 case VOL_LABEL_ERROR:
359 bnet_fsend(dir, _("3901 Cannot label Volume because it is \
360 already labeled: %s\n"), dev->VolHdr.VolName);
364 write_volume_label_to_dev(jcr, jcr->device, vname, poolname);
365 strcpy(jcr->VolumeName, vname);
366 bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"),
367 vname, dev->dev_name);
370 bnet_fsend(dir, _("3902 Cannot label Volume. \
371 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;
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);
468 new_unlock_device(dev);
471 Dmsg0(90, "Unmounted waiting for mount attempt to wake thread\n");
472 dev->dev_blocked = BST_WAITING_FOR_SYSOP;
473 pthread_cond_signal(&dev->wait_next_vol);
475 if (dev->state & ST_LABEL) {
476 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
477 dev->dev_name, dev->VolHdr.VolName);
479 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\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"),
503 if (!dev_is_tape(dev)) {
504 bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
507 if (open_dev(dev, NULL, READ_WRITE) < 0) {
508 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
512 read_label(jcr, dev);
513 if (dev->state & ST_LABEL) {
514 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
515 dev->dev_name, dev->VolHdr.VolName);
517 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"),
524 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
529 bnet_fsend(dir, _("3999 Device %s not found\n"), dev_name);
532 strcpy(dev_name, dir->msg);
533 bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), dev_name);
535 free_memory(dev_name);
536 bnet_sig(dir, BNET_EOD);
541 * unmount command from Director
543 static int unmount_cmd(JCR *jcr)
546 BSOCK *dir = jcr->dir_bsock;
551 dname = (char *) get_memory(dir->msglen+1);
552 if (sscanf(dir->msg, "unmount %s", dname) == 1) {
553 unbash_spaces(dname);
556 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
557 /* Find resource, and make sure we were able to open it */
558 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
559 Dmsg1(20, "Found device %s\n", device->hdr.name);
566 jcr->device = device;
569 if (!(dev->state & ST_OPENED)) {
570 Dmsg0(90, "Device already unmounted\n");
571 bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"), dev_name(dev));
573 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
574 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
576 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
579 force_close_dev(dev);
580 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
581 bnet_fsend(dir, _("3001 Device %s unmounted.\n"), dev_name(dev));
583 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
584 bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
587 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
588 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
591 } else if (dev->state & ST_READ || dev->num_writers) {
592 if (dev->state & ST_READ) {
593 Dmsg0(90, "Device in read mode\n");
594 bnet_fsend(dir, _("3904 Device %s is busy with 1 reader.\n"),
597 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
598 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
599 dev_name(dev), dev->num_writers);
602 } else { /* device not being used */
603 Dmsg0(90, "Device not in use, unmounting\n");
604 new_lock_device_state(dev, BST_UNMOUNTED);
605 block_device(dev, BST_UNMOUNTED);
606 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
609 force_close_dev(dev);
610 bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
614 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
617 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
618 strcpy(dname, dir->msg);
619 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), dname);
622 bnet_sig(dir, BNET_EOD);
627 * Status command from Director
629 static int status_cmd(JCR *jcr)
633 int found, bps, sec, bpb;
634 BSOCK *user = jcr->dir_bsock;
635 char dt[MAX_TIME_LENGTH];
636 char b1[30], b2[30], b3[30];
638 bnet_fsend(user, "\n%s Version: " VERSION " (" DATE ")\n", my_name);
639 bstrftime(dt, sizeof(dt), daemon_start_time);
640 bnet_fsend(user, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
641 last_job.NumJobs == 1 ? "" : "s");
642 if (last_job.NumJobs > 0) {
645 bstrftime(dt, sizeof(dt), last_job.end_time);
646 bnet_fsend(user, _("Last Job %s finished at %s\n"), last_job.Job, dt);
648 jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
649 bnet_fsend(user, _(" Files=%s Bytes=%s Termination Status=%s\n"),
650 edit_uint64_with_commas(last_job.JobFiles, b1),
651 edit_uint64_with_commas(last_job.JobBytes, b2),
656 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
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 switch (dev->dev_blocked) {
668 bnet_fsend(user, _(" Deviced is blocked. User unmounted.\n"));
670 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
671 bnet_fsend(user, _(" Deviced is blocked. User unmounted during wait for media/mount.\n"));
673 case BST_WAITING_FOR_SYSOP:
674 if (jcr->JobStatus == JS_WaitMount) {
675 bnet_fsend(user, _(" Device is blocked waiting for mount.\n"));
677 bnet_fsend(user, _(" Device is blocked waiting for appendable media.\n"));
680 case BST_DOING_ACQUIRE:
681 bnet_fsend(user, _(" Device is being initialized.\n"));
683 case BST_WRITING_LABEL:
684 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
689 bpb = dev->VolCatInfo.VolCatBlocks;
693 bpb = dev->VolCatInfo.VolCatBytes / bpb;
694 bnet_fsend(user, _(" Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
695 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
696 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
697 edit_uint64_with_commas(bpb, b3));
698 bnet_fsend(user, _(" Positioned at File=%s Block=%s\n"),
699 edit_uint64_with_commas(dev->file, b1),
700 edit_uint64_with_commas(dev->block_num, b2));
703 bnet_fsend(user, _("Device %s is not open.\n"), dev_name(dev));
711 /* NOTE, we reuse a calling argument jcr. Be warned! */
712 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
713 if (jcr->JobStatus == JS_WaitFD) {
714 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
715 job_type_to_str(jcr->JobType), jcr->Job);
718 bnet_fsend(user, _("%s %s job %s is using device %s\n"),
719 job_level_to_str(jcr->JobLevel),
720 job_type_to_str(jcr->JobType),
721 jcr->Job, jcr->device->device_name);
722 sec = time(NULL) - jcr->run_time;
726 bps = jcr->JobBytes / sec;
727 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
728 edit_uint64_with_commas(jcr->JobFiles, b1),
729 edit_uint64_with_commas(jcr->JobBytes, b2),
730 edit_uint64_with_commas(bps, b3));
733 if (jcr->file_bsock) {
734 bnet_fsend(user, " FDReadSeqNo=%" lld " fd=%d\n",
735 jcr->file_bsock->read_seqno, jcr->file_bsock->fd);
737 bnet_fsend(user, " FDSocket closed\n");
741 free_locked_jcr(jcr);
745 bnet_fsend(user, _("No jobs running.\n"));
749 bnet_fsend(user, "\n\n");
750 dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
752 bnet_fsend(user, "====\n");
754 bnet_sig(user, BNET_EOD);