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 char *dname, *volname, *poolname, *mtype;
230 BSOCK *dir = jcr->dir_bsock;
235 dname = (char *) get_memory(dir->msglen+1);
236 volname = (char *) get_memory(dir->msglen+1);
237 poolname = (char *) get_memory(dir->msglen+1);
238 mtype = (char *) get_memory(dir->msglen+1);
239 if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s",
240 dname, volname, poolname, mtype) == 4) {
241 unbash_spaces(dname);
242 unbash_spaces(volname);
243 unbash_spaces(poolname);
244 unbash_spaces(mtype);
247 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
248 /* Find resource, and make sure we were able to open it */
249 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
250 Dmsg1(20, "Found device %s\n", device->hdr.name);
257 /******FIXME**** compare MediaTypes */
258 jcr->device = device;
262 if (!(dev->state & ST_OPENED)) {
263 if (open_dev(dev, volname, READ_WRITE) < 0) {
264 bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
266 label_volume_if_ok(jcr, dev, volname, poolname);
267 force_close_dev(dev);
269 } else if (dev->dev_blocked &&
270 dev->dev_blocked != BST_DOING_ACQUIRE) { /* device blocked? */
271 label_volume_if_ok(jcr, dev, volname, poolname);
272 } else if (dev->state & ST_READ || dev->num_writers) {
273 if (dev->state & ST_READ) {
274 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
277 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
278 dev_name(dev), dev->num_writers);
280 } else { /* device not being used */
281 label_volume_if_ok(jcr, dev, volname, poolname);
285 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
288 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
289 strcpy(dname, dir->msg);
290 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), dname);
293 free_memory(volname);
294 free_memory(poolname);
296 bnet_sig(dir, BNET_EOD);
301 * Read the tape label and determine if we can safely
302 * label the tape (not a Bacula volume), then label it.
304 * Enter with the mutex set
306 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname)
308 BSOCK *dir = jcr->dir_bsock;
313 pthread_t no_wait_id;
315 blocked = dev->dev_blocked; /* save any prev blocked state */
316 dev->dev_blocked = BST_WRITING_LABEL;
317 no_wait_id = dev->no_wait_id;
318 dev->no_wait_id = pthread_self(); /* let us use the tape */
321 new_steal_device_lock(dev, &hold, BST_WRITING_LABEL);
323 strcpy(jcr->VolumeName, vname);
324 block = new_block(dev);
325 switch (read_dev_volume_label(jcr, dev, block)) {
327 case VOL_VERSION_ERROR:
328 case VOL_LABEL_ERROR:
330 bnet_fsend(dir, _("3901 Cannot label Volume because it is \
331 already labeled: %s\n"), dev->VolHdr.VolName);
335 write_volume_label_to_dev(jcr, jcr->device, vname, poolname);
336 strcpy(jcr->VolumeName, vname);
337 bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"),
338 vname, dev->dev_name);
341 bnet_fsend(dir, _("3902 Cannot label Volume. \
342 Unknown status %d from read_volume_label()\n"), jcr->label_status);
347 new_return_device_lock(dev, &hold);
349 dev->dev_blocked = blocked; /* reset blocked state */
350 dev->no_wait_id = no_wait_id; /* reset blocking thread id */
356 * Read the tape label
358 * Enter with the mutex set
360 static int read_label(JCR *jcr, DEVICE *dev)
363 BSOCK *dir = jcr->dir_bsock;
368 pthread_t no_wait_id;
370 blocked = dev->dev_blocked; /* save any prev blocked state */
371 no_wait_id = dev->no_wait_id;
372 dev->dev_blocked = BST_DOING_ACQUIRE;
373 dev->no_wait_id = pthread_self(); /* let us use the tape */
375 new_steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
376 V(dev->mutex); /* release lock */
378 jcr->VolumeName[0] = 0;
379 block = new_block(dev);
380 dev->state &= ~ST_LABEL; /* force read of label */
381 switch (read_dev_volume_label(jcr, dev, block)) {
383 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
387 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s\n"),
388 dev->dev_name, jcr->errmsg);
394 new_return_device_lock(dev, &hold);
396 dev->dev_blocked = blocked; /* reset blocked state */
397 dev->no_wait_id = no_wait_id; /* reset blocking thread id */
403 * Mount command from Director
405 static int mount_cmd(JCR *jcr)
408 BSOCK *dir = jcr->dir_bsock;
413 dev_name = (char *) get_memory(dir->msglen);
414 if (sscanf(dir->msg, "mount %s", dev_name) == 1) {
415 unbash_spaces(dev_name);
418 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
419 /* Find resource, and make sure we were able to open it */
420 if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
421 Dmsg1(20, "Found device %s\n", device->hdr.name);
428 jcr->device = device;
431 switch (dev->dev_blocked) { /* device blocked? */
433 case BST_WAITING_FOR_SYSOP:
434 /* Someone is waiting, wake him */
435 Dmsg0(90, "Waiting for mount attempt to wake thread\n");
436 pthread_cond_signal(&dev->wait_next_vol);
437 bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev->dev_name);
440 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
442 /* We freed the device, so reopen it and wake any waiting threads */
443 if (open_dev(dev, NULL, READ_WRITE) < 0) {
444 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
448 block = new_block(dev);
449 read_dev_volume_label(jcr, dev, block);
451 if (dev->dev_blocked == BST_UNMOUNTED) {
452 Dmsg0(90, "Unmounted unblocking device\n");
453 read_label(jcr, dev);
454 new_unlock_device(dev);
457 Dmsg0(90, "Unmounted waiting for mount attempt to wake thread\n");
458 dev->dev_blocked = BST_WAITING_FOR_SYSOP;
459 pthread_cond_signal(&dev->wait_next_vol);
461 if (dev->state & ST_LABEL) {
462 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
463 dev->dev_name, dev->VolHdr.VolName);
465 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"),
470 case BST_DOING_ACQUIRE:
471 bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"),
475 case BST_WRITING_LABEL:
476 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev->dev_name);
479 case BST_NOT_BLOCKED:
480 if (dev->state & ST_OPENED) {
481 if (dev->state & ST_LABEL) {
482 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
483 dev->dev_name, dev->VolHdr.VolName);
485 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"),
489 if (!dev_is_tape(dev)) {
490 bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
493 if (open_dev(dev, NULL, READ_WRITE) < 0) {
494 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
498 read_label(jcr, dev);
499 if (dev->state & ST_LABEL) {
500 bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
501 dev->dev_name, dev->VolHdr.VolName);
503 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"),
510 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
515 bnet_fsend(dir, _("3999 Device %s not found\n"), dev_name);
518 strcpy(dev_name, dir->msg);
519 bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), dev_name);
521 free_memory(dev_name);
522 bnet_sig(dir, BNET_EOD);
527 * unmount command from Director
529 static int unmount_cmd(JCR *jcr)
532 BSOCK *dir = jcr->dir_bsock;
537 dname = (char *) get_memory(dir->msglen+1);
538 if (sscanf(dir->msg, "unmount %s", dname) == 1) {
539 unbash_spaces(dname);
542 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
543 /* Find resource, and make sure we were able to open it */
544 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
545 Dmsg1(20, "Found device %s\n", device->hdr.name);
552 jcr->device = device;
555 if (!(dev->state & ST_OPENED)) {
556 Dmsg0(90, "Device already unmounted\n");
557 bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"), dev_name(dev));
559 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
560 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
562 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
565 force_close_dev(dev);
566 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
567 bnet_fsend(dir, _("3001 Device %s unmounted.\n"), dev_name(dev));
569 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
570 bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
573 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
574 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
577 } else if (dev->state & ST_READ || dev->num_writers) {
578 if (dev->state & ST_READ) {
579 Dmsg0(90, "Device in read mode\n");
580 bnet_fsend(dir, _("3904 Device %s is busy with 1 reader.\n"),
583 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
584 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
585 dev_name(dev), dev->num_writers);
588 } else { /* device not being used */
589 Dmsg0(90, "Device not in use, unmounting\n");
590 new_lock_device_state(dev, BST_UNMOUNTED);
591 block_device(dev, BST_UNMOUNTED);
592 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
595 force_close_dev(dev);
596 bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
600 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
603 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
604 strcpy(dname, dir->msg);
605 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), dname);
608 bnet_sig(dir, BNET_EOD);
613 * Status command from Director
615 static int status_cmd(JCR *jcr)
619 int found, bps, sec, bpb;
620 BSOCK *user = jcr->dir_bsock;
621 char dt[MAX_TIME_LENGTH];
622 char b1[30], b2[30], b3[30];
624 bnet_fsend(user, "\n%s Version: " VERSION " (" DATE ")\n", my_name);
625 bstrftime(dt, sizeof(dt), daemon_start_time);
626 bnet_fsend(user, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
627 last_job.NumJobs == 1 ? "" : "s");
628 if (last_job.NumJobs > 0) {
631 bstrftime(dt, sizeof(dt), last_job.end_time);
632 bnet_fsend(user, _("Last Job %s finished at %s\n"), last_job.Job, dt);
634 jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
635 bnet_fsend(user, _(" Files=%s Bytes=%s Termination Status=%s\n"),
636 edit_uint64_with_commas(last_job.JobFiles, b1),
637 edit_uint64_with_commas(last_job.JobBytes, b2),
642 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
645 if (dev->state & ST_OPENED) {
646 if (dev->state & ST_LABEL) {
647 bnet_fsend(user, _("Device %s is mounted with Volume %s\n"),
648 dev_name(dev), dev->VolHdr.VolName);
650 bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"), dev_name(dev));
652 switch (dev->dev_blocked) {
654 bnet_fsend(user, _(" Deviced is blocked. User unmounted.\n"));
656 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
657 bnet_fsend(user, _(" Deviced is blocked. User unmounted during wait for media/mount.\n"));
659 case BST_WAITING_FOR_SYSOP:
660 if (jcr->JobStatus == JS_WaitMount) {
661 bnet_fsend(user, _(" Device is blocked waiting for mount.\n"));
663 bnet_fsend(user, _(" Device is blocked waiting for appendable media.\n"));
666 case BST_DOING_ACQUIRE:
667 bnet_fsend(user, _(" Device is being initialized.\n"));
669 case BST_WRITING_LABEL:
670 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
675 bpb = dev->VolCatInfo.VolCatBlocks;
679 bpb = dev->VolCatInfo.VolCatBytes / bpb;
680 bnet_fsend(user, _(" Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
681 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
682 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
683 edit_uint64_with_commas(bpb, b3));
684 bnet_fsend(user, _(" Positioned at File=%s Block=%s\n"),
685 edit_uint64_with_commas(dev->file, b1),
686 edit_uint64_with_commas(dev->block_num, b2));
689 bnet_fsend(user, _("Device %s is not open.\n"), dev_name(dev));
697 /* NOTE, we reuse a calling argument jcr. Be warned! */
698 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
699 if (jcr->JobStatus == JS_WaitFD) {
700 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
701 job_type_to_str(jcr->JobType), jcr->Job);
704 bnet_fsend(user, _("%s %s job %s is using device %s\n"),
705 job_level_to_str(jcr->JobLevel),
706 job_type_to_str(jcr->JobType),
707 jcr->Job, jcr->device->device_name);
708 sec = time(NULL) - jcr->run_time;
712 bps = jcr->JobBytes / sec;
713 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
714 edit_uint64_with_commas(jcr->JobFiles, b1),
715 edit_uint64_with_commas(jcr->JobBytes, b2),
716 edit_uint64_with_commas(bps, b3));
719 if (jcr->file_bsock) {
720 bnet_fsend(user, " FDReadSeqNo=%" lld " fd=%d\n",
721 jcr->file_bsock->read_seqno, jcr->file_bsock->fd);
723 bnet_fsend(user, " FDSocket closed\n");
727 free_locked_jcr(jcr);
731 bnet_fsend(user, _("No jobs running.\n"));
735 bnet_fsend(user, "\n\n");
736 dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
738 bnet_fsend(user, "====\n");
740 bnet_sig(user, BNET_EOD);