-
Notifications
You must be signed in to change notification settings - Fork 256
/
dir_cmd.c
1742 lines (1568 loc) · 55.1 KB
/
dir_cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2001-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* This file handles accepting Director Commands
*
* Most Director commands are handled here, with the
* exception of the Job command command and subsequent
* subcommands that are handled
* in job.c.
*
* N.B. in this file, in general we must use P(dev->mutex) rather
* than dev->r_lock() so that we can examine the blocked
* state rather than blocking ourselves because a Job
* thread has the device blocked. In some "safe" cases,
* we can do things to a blocked device. CAREFUL!!!!
*
* File daemon commands are handled in fd_cmds.c
*
* Kern Sibbald, May MMI
*/
#include "bareos.h"
#include "stored.h"
/* Exported variables */
/* Imported variables */
extern BSOCK *filed_chan;
extern struct s_last_job last_job;
extern bool init_done;
/* Commands received from director that need scanning */
static char setbandwidth[] =
"setbandwidth=%lld Job=%127s";
static char setdebugv0cmd[] =
"setdebug=%d trace=%d";
static char setdebugv1cmd[] =
"setdebug=%d trace=%d timestamp=%d";
static char cancelcmd[] =
"cancel Job=%127s";
static char relabelcmd[] =
"relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
"MediaType=%127s Slot=%d drive=%d MinBlocksize=%d MaxBlocksize=%d";
static char labelcmd[] =
"label %127s VolumeName=%127s PoolName=%127s "
"MediaType=%127s Slot=%d drive=%d MinBlocksize=%d MaxBlocksize=%d";
static char mountslotcmd[] =
"mount %127s drive=%d slot=%d";
static char mountcmd[] =
"mount %127s drive=%d";
static char unmountcmd[] =
"unmount %127s drive=%d";
#if 0
static char actionopcmd[] =
"action_on_purge %127s vol=%127s action=%d";
#endif
static char releasecmd[] =
"release %127s drive=%d";
static char readlabelcmd[] =
"readlabel %127s Slot=%d drive=%d";
static char replicatecmd[] =
"replicate Job=%127s address=%s port=%d ssl=%d Authorization=%100s";
static char passiveclientcmd[] =
"passive client address=%s port=%d ssl=%d";
static char resolvecmd[] =
"resolve %s";
static char pluginoptionscmd[] =
"pluginoptions %s";
/* Responses sent to Director */
static char derrmsg[] =
"3900 Invalid command:";
static char OKsetdebugv0[] =
"3000 OK setdebug=%d tracefile=%s\n";
static char OKsetdebugv1[] =
"3000 OK setdebug=%d trace=%d timestamp=%d tracefile=%s\n";
static char invalid_cmd[] =
"3997 Invalid command for a Director with Monitor directive enabled.\n";
static char OK_bootstrap[] =
"3000 OK bootstrap\n";
static char ERROR_bootstrap[] =
"3904 Error bootstrap\n";
static char OK_replicate[] =
"3000 OK replicate\n";
static char BADcmd[] =
"3991 Bad %s command: %s\n";
static char OKBandwidth[] =
"2000 OK Bandwidth\n";
static char OKpassive[] =
"2000 OK passive client\n";
static char OKpluginoptions[] =
"2000 OK plugin options\n";
/* Imported functions */
extern bool finish_cmd(JCR *jcr);
extern bool job_cmd(JCR *jcr);
extern bool nextrun_cmd(JCR *jcr);
extern bool dotstatus_cmd(JCR *jcr);
//extern bool query_cmd(JCR *jcr);
extern bool status_cmd(JCR *sjcr);
extern bool use_cmd(JCR *jcr);
extern bool stats_cmd(JCR *jcr);
extern bool do_job_run(JCR *jcr);
extern bool do_mac_run(JCR *jcr);
extern void terminate_child();
/* Forward referenced functions */
//static bool action_on_purge_cmd(JCR *jcr);
static bool bootstrap_cmd(JCR *jcr);
static bool cancel_cmd(JCR *cjcr);
static bool changer_cmd(JCR *jcr);
static bool die_cmd(JCR *jcr);
static bool label_cmd(JCR *jcr);
static bool listen_cmd(JCR *jcr);
static bool mount_cmd(JCR *jcr);
static bool passive_cmd(JCR *jcr);
static bool pluginoptions_cmd(JCR *jcr);
static bool readlabel_cmd(JCR *jcr);
static bool resolve_cmd(JCR *jcr);
static bool relabel_cmd(JCR *jcr);
static bool release_cmd(JCR *jcr);
static bool replicate_cmd(JCR *jcr);
static bool run_cmd(JCR *jcr);
static bool setbandwidth_cmd(JCR *jcr);
static bool setdebug_cmd(JCR *jcr);
static bool unmount_cmd(JCR *jcr);
static DCR *find_device(JCR *jcr, POOL_MEM &dev_name, int drive, BLOCKSIZES *blocksizes);
static void read_volume_label(JCR *jcr, DCR *dcr, DEVICE *dev, int Slot);
static void label_volume_if_ok(DCR *dcr, char *oldname,
char *newname, char *poolname,
int Slot, bool relabel);
static bool try_autoload_device(JCR *jcr, DCR *dcr, int slot, const char *VolName);
static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
struct s_cmds {
const char *cmd;
bool (*func)(JCR *jcr);
bool monitoraccess; /* set if monitors can access this cmd */
};
/*
* The following are the recognized commands from the Director.
*
* Keywords are sorted first longest match when the keywords start with the same string.
*/
static struct s_cmds cmds[] = {
// { "action_on_purge", action_on_purge_cmd, false },
{ "autochanger", changer_cmd, false },
{ "bootstrap", bootstrap_cmd, false },
{ "cancel", cancel_cmd, false },
{ ".die", die_cmd, false },
{ "finish", finish_cmd, false }, /* End of backup */
{ "JobId=", job_cmd, false }, /* Start Job */
{ "label", label_cmd, false }, /* Label a tape */
{ "listen", listen_cmd, false }, /* Listen for an incoming Storage Job */
{ "mount", mount_cmd, false },
{ "nextrun", nextrun_cmd, false }, /* Prepare for next backup/restore part of same Job */
{ "passive", passive_cmd, false },
{ "pluginoptions", pluginoptions_cmd, false },
// { "query", query_cmd, false },
{ "readlabel", readlabel_cmd, false },
{ "relabel", relabel_cmd, false }, /* Relabel a tape */
{ "release", release_cmd, false },
{ "resolve", resolve_cmd, false },
{ "replicate", replicate_cmd, false }, /* Replicate data to an external SD */
{ "run", run_cmd, false }, /* Start of Job */
{ "setbandwidth=", setbandwidth_cmd, false },
{ "setdebug=", setdebug_cmd, false }, /* Set debug level */
{ "stats", stats_cmd, false },
{ "status", status_cmd, true },
{ ".status", dotstatus_cmd, true },
{ "unmount", unmount_cmd, false },
{ "use storage=", use_cmd, false },
{ NULL, NULL, false } /* list terminator */
};
/*
* Connection request from an director.
*
* Basic tasks done here:
* - Create a JCR record
* - Authenticate the Director
* - We wait for a command
* - We execute the command
* - We continue or exit depending on the return status
*/
static void *handle_director_connection(BSOCK *dir, char *job_name)
{
JCR *jcr;
int i, errstat;
int bnet_stat = 0;
bool found, quit;
/*
* This is a connection from the Director, so setup a JCR
*/
jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
new_plugins(jcr); /* instantiate plugins */
jcr->dir_bsock = dir; /* save Director bsock */
jcr->dir_bsock->set_jcr(jcr);
jcr->dcrs = New(alist(10, not_owned_by_alist));
/*
* Initialize Start Job condition variable
*/
errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
if (errstat != 0) {
berrno be;
Jmsg1(jcr, M_FATAL, 0, _("Unable to init job start cond variable: ERR=%s\n"), be.bstrerror(errstat));
goto bail_out;
}
/*
* Initialize End Job condition variable
*/
errstat = pthread_cond_init(&jcr->job_end_wait, NULL);
if (errstat != 0) {
berrno be;
Jmsg1(jcr, M_FATAL, 0, _("Unable to init job end cond variable: ERR=%s\n"), be.bstrerror(errstat));
goto bail_out;
}
Dmsg0(1000, "stored in start_job\n");
set_jcr_in_tsd(jcr);
/*
* Authenticate the Director
*/
if (!authenticate_director(jcr)) {
Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
goto bail_out;
}
Dmsg0(90, "Message channel init completed.\n");
quit = false;
while (!quit) {
/*
* Read command
*/
if ((bnet_stat = dir->recv()) <= 0) {
break; /* connection terminated */
}
Dmsg1(199, "<dird: %s", dir->msg);
/*
* Ensure that device initialization is complete
*/
while (!init_done) {
bmicrosleep(1, 0);
}
found = false;
for (i = 0; cmds[i].cmd; i++) {
if (bstrncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd))) {
if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
dir->fsend(invalid_cmd);
dir->signal(BNET_EOD);
break;
}
Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
if (!cmds[i].func(jcr)) { /* do command */
quit = true; /* error, get out */
Dmsg1(190, "Command %s requests quit\n", cmds[i].cmd);
}
found = true; /* indicate command found */
break;
}
}
if (!found) { /* command not found */
POOL_MEM err_msg;
Mmsg(err_msg, "%s %s\n", derrmsg, dir->msg);
dir->fsend(err_msg.c_str());
break;
}
}
bail_out:
generate_plugin_event(jcr, bsdEventJobEnd);
dequeue_messages(jcr); /* send any queued messages */
dir->signal(BNET_TERMINATE);
free_plugins(jcr); /* release instantiated plugins */
free_jcr(jcr);
return NULL;
}
/*
* Connection request. We accept connections either from the
* Director, Storage Daemon or a Client (File daemon).
*
* Note, we are running as a seperate thread of the Storage daemon.
*
* Basic tasks done here:
* - If it was a connection from the FD, call handle_filed_connection()
* - If it was a connection from an other SD, call handle_stored_connection()
* - Otherwise it was a connection from the DIR, call handle_director_connection()
*/
void *handle_connection_request(void *arg)
{
BSOCK *bs = (BSOCK *)arg;
char name[500];
char tbuf[100];
if (bs->recv() <= 0) {
Emsg1(M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
bmicrosleep(5, 0); /* make user wait 5 seconds */
bs->close();
return NULL;
}
/*
* Do a sanity check on the message received
*/
if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)) {
Dmsg1(000, "<filed: %s", bs->msg);
Emsg2(M_ERROR, 0, _("Invalid connection from %s. Len=%d\n"), bs->who(), bs->msglen);
bmicrosleep(5, 0); /* make user wait 5 seconds */
bs->close();
return NULL;
}
Dmsg1(110, "Conn: %s", bs->msg);
/*
* See if this is a File daemon connection. If so call FD handler.
*/
if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
Dmsg1(110, "Got a FD connection at %s\n", bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return handle_filed_connection(bs, name);
}
/*
* See if this is a Storage daemon connection. If so call SD handler.
*/
if (sscanf(bs->msg, "Hello Start Storage Job %127s", name) == 1) {
Dmsg1(110, "Got a SD connection at %s\n", bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return handle_stored_connection(bs, name);
}
Dmsg1(110, "Got a DIR connection at %s\n", bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return handle_director_connection(bs, name);
}
/*
* Force SD to die, and hopefully dump itself. Turned on only in development version.
*/
static bool die_cmd(JCR *jcr)
{
#ifdef DEVELOPER
JCR *djcr = NULL;
int a;
BSOCK *dir = jcr->dir_bsock;
pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;
if (strstr(dir->msg, "deadlock")) {
Pmsg0(000, "I have been requested to deadlock ...\n");
P(m);
P(m);
}
Pmsg1(000, "I have been requested to die ... (%s)\n", dir->msg);
a = djcr->JobId; /* ref NULL pointer */
djcr->JobId = a;
#endif
return false;
}
/**
* Set bandwidth limit as requested by the Director
*/
static bool setbandwidth_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
int64_t bw = 0;
JCR *cjcr;
char Job[MAX_NAME_LENGTH];
*Job = 0;
if (sscanf(dir->msg, setbandwidth, &bw, Job) != 2 || bw < 0) {
pm_strcpy(jcr->errmsg, dir->msg);
dir->fsend(_("2991 Bad setbandwidth command: %s\n"), jcr->errmsg);
return false;
}
if (*Job) {
if (!(cjcr = get_jcr_by_full_name(Job))) {
dir->fsend(_("2901 Job %s not found.\n"), Job);
} else {
cjcr->max_bandwidth = bw;
if (cjcr->store_bsock) {
cjcr->store_bsock->set_bwlimit(bw);
if (me->allow_bw_bursting) {
cjcr->store_bsock->set_bwlimit_bursting();
}
}
free_jcr(cjcr);
}
} else { /* No job requested, apply globally */
me->max_bandwidth_per_job = bw; /* Overwrite directive */
}
return dir->fsend(OKBandwidth);
}
/*
* Set debug level as requested by the Director
*/
static bool setdebug_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
int32_t level, trace_flag, timestamp_flag;
int scan;
Dmsg1(10, "setdebug_cmd: %s", dir->msg);
scan = sscanf(dir->msg, setdebugv1cmd, &level, &trace_flag, ×tamp_flag);
if (scan != 3) {
scan = sscanf(dir->msg, setdebugv0cmd, &level, &trace_flag);
}
if ((scan != 3 && scan != 2) || level < 0) {
dir->fsend(BADcmd, "setdebug", dir->msg);
return false;
}
POOL_MEM tracefilename(PM_FNAME);
Mmsg(tracefilename, "%s/%s.trace", TRACEFILEDIRECTORY, my_name);
debug_level = level;
set_trace(trace_flag);
if (scan == 3) {
set_timestamp(timestamp_flag);
Dmsg4(50, "level=%d trace=%d timestamp=%d tracefilename=%s\n", level, get_trace(), get_timestamp(), tracefilename.c_str());
return dir->fsend(OKsetdebugv1, level, get_trace(), get_timestamp(), tracefilename.c_str());
} else {
Dmsg3(50, "level=%d trace=%d\n", level, get_trace(), tracefilename.c_str());
return dir->fsend(OKsetdebugv0, level, tracefilename.c_str());
}
}
/*
* Cancel a Job
* Be careful, we switch to using the job's JCR! So, using
* BSOCKs on that jcr can have two threads in the same code.
*/
static bool cancel_cmd(JCR *cjcr)
{
BSOCK *dir = cjcr->dir_bsock;
int oldStatus;
char Job[MAX_NAME_LENGTH];
JobId_t JobId;
JCR *jcr;
int status;
const char *reason;
if (sscanf(dir->msg, cancelcmd, Job) == 1) {
status = JS_Canceled;
reason = "canceled";
} else {
dir->fsend(_("3903 Error scanning cancel command.\n"));
goto bail_out;
}
/*
* See if the Jobname is a number only then its a JobId.
*/
if (is_a_number(Job)) {
JobId = str_to_int64(Job);
if (!(jcr = get_jcr_by_id(JobId))) {
dir->fsend(_("3904 Job %s not found.\n"), Job);
goto bail_out;
}
} else {
if (!(jcr = get_jcr_by_full_name(Job))) {
dir->fsend(_("3904 Job %s not found.\n"), Job);
goto bail_out;
}
}
oldStatus = jcr->JobStatus;
jcr->setJobStatus(status);
Dmsg2(800, "Cancel JobId=%d %p\n", jcr->JobId, jcr);
if (!jcr->authenticated && (oldStatus == JS_WaitFD || oldStatus == JS_WaitSD)) {
pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
}
if (jcr->file_bsock) {
jcr->file_bsock->set_terminated();
jcr->file_bsock->set_timed_out();
Dmsg2(800, "Term bsock jid=%d %p\n", jcr->JobId, jcr);
} else {
if (oldStatus != JS_WaitSD) {
/*
* Still waiting for FD to connect, release it
*/
pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
Dmsg2(800, "Signal FD connect jid=%d %p\n", jcr->JobId, jcr);
}
}
/*
* If thread waiting on mount, wake him
*/
if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->waiting_for_mount()) {
pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
release_device_cond();
}
if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->waiting_for_mount()) {
pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
release_device_cond();
}
/*
* See if the Job has a certain protocol.
* When canceling a NDMP job make sure we call the end_of_ndmp_* functions.
*/
switch (jcr->getJobProtocol()) {
case PT_NDMP:
switch (jcr->getJobType()) {
case JT_BACKUP:
end_of_ndmp_backup(jcr);
break;
case JT_RESTORE:
end_of_ndmp_restore(jcr);
break;
default:
break;
}
}
pthread_cond_signal(&jcr->job_end_wait); /* wake waiting job */
dir->fsend(_("3000 JobId=%ld Job=\"%s\" marked to be %s.\n"), jcr->JobId, jcr->Job, reason);
free_jcr(jcr);
bail_out:
dir->signal(BNET_EOD);
return true;
}
/*
* Resolve a hostname
*/
static bool resolve_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
dlist *addr_list;
const char *errstr;
char addresses[2048];
char hostname[2048];
sscanf(dir->msg, resolvecmd, &hostname);
if ((addr_list = bnet_host2ipaddrs(hostname, 0, &errstr)) == NULL) {
dir->fsend(_("%s: Failed to resolve %s\n"), my_name, hostname);
goto bail_out;
}
dir->fsend(_("%s resolves %s to %s\n"), my_name, hostname,
build_addresses_str(addr_list, addresses, sizeof(addresses), false));
free_addresses(addr_list);
bail_out:
dir->signal(BNET_EOD);
return true;
}
static bool do_label(JCR *jcr, bool relabel)
{
int len;
POOLMEM *newname,
*oldname,
*poolname,
*mediatype;
POOL_MEM dev_name;
BSOCK *dir = jcr->dir_bsock;
DCR *dcr;
DEVICE *dev;
BLOCKSIZES blocksizes;
bool ok = false;
int32_t slot, drive;
//, max_block_size, min_block_size;
/*
* Determine the length of the temporary buffers.
* If the total length of the incoming message is less
* then MAX_NAME_LENGTH we can use that as the upper limit.
* If the incomming message is bigger then MAX_NAME_LENGTH
* limit the temporary buffer to MAX_NAME_LENGTH bytes as
* we use a sscanf %127s for reading the temorary buffer.
*/
len = dir->msglen + 1;
if (len > MAX_NAME_LENGTH) {
len = MAX_NAME_LENGTH;
}
newname = get_memory(len);
oldname = get_memory(len);
poolname = get_memory(len);
mediatype = get_memory(len);
if (relabel) {
if (sscanf(dir->msg, relabelcmd, dev_name.c_str(), oldname,
newname, poolname, mediatype, &slot, &drive,
&blocksizes.min_block_size, &blocksizes.max_block_size) == 9) {
ok = true;
}
} else {
*oldname = 0;
if (sscanf(dir->msg, labelcmd, dev_name.c_str(), newname,
poolname, mediatype, &slot, &drive,
&blocksizes.min_block_size, &blocksizes.max_block_size) == 8) {
ok = true;
}
}
if (ok) {
unbash_spaces(newname);
unbash_spaces(oldname);
unbash_spaces(poolname);
unbash_spaces(mediatype);
dcr = find_device(jcr, dev_name, drive, &blocksizes);
if (dcr) {
dev = dcr->dev;
dev->Lock(); /* Use P to avoid indefinite block */
dcr->VolMinBlocksize = blocksizes.min_block_size;
dcr->VolMaxBlocksize = blocksizes.max_block_size;
dev->set_blocksizes(dcr); /* apply blocksizes from dcr to dev */
if (!dev->is_open() && !dev->is_busy()) {
Dmsg1(400, "Can %slabel. Device is not open\n", relabel ? "re" : "");
label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
dev->close(dcr);
/* Under certain "safe" conditions, we can steal the lock */
} else if (dev->can_steal_lock()) {
Dmsg0(400, "Can relabel. can_steal_lock\n");
label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
} else if (dev->is_busy() || dev->is_blocked()) {
send_dir_busy_message(dir, dev);
} else { /* device not being used */
Dmsg0(400, "Can relabel. device not used\n");
label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
}
dev->Unlock();
free_dcr(dcr);
} else {
dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
}
} else {
/* NB dir->msg gets clobbered in bnet_fsend, so save command */
pm_strcpy(jcr->errmsg, dir->msg);
dir->fsend(_("3903 Error scanning label command: %s\n"), jcr->errmsg);
}
free_memory(oldname);
free_memory(newname);
free_memory(poolname);
free_memory(mediatype);
dir->signal(BNET_EOD);
return true;
}
/*
* Label a Volume
*/
static bool label_cmd(JCR *jcr)
{
return do_label(jcr, false);
}
static bool relabel_cmd(JCR *jcr)
{
return do_label(jcr, true);
}
/*
* Read the tape label and determine if we can safely
* label the tape (not a Bareos volume), then label it.
*
* Enter with the mutex set
*/
static void label_volume_if_ok(DCR *dcr, char *oldname,
char *newname, char *poolname,
int slot, bool relabel)
{
BSOCK *dir = dcr->jcr->dir_bsock;
bsteal_lock_t hold;
DEVICE *dev = dcr->dev;
int label_status;
int mode;
const char *volname = relabel ? oldname : newname;
char ed1[50];
steal_device_lock(dev, &hold, BST_WRITING_LABEL);
Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
Dmsg0(90, "try_autoload_device - looking for volume_info\n");
if (!try_autoload_device(dcr->jcr, dcr, slot, volname)) {
goto bail_out; /* error */
}
/* Ensure that the device is open -- autoload_device() closes it */
if (dev->is_tape()) {
mode = OPEN_READ_WRITE;
} else {
mode = CREATE_READ_WRITE;
}
/* Set old volume name for open if relabeling */
dcr->setVolCatName(volname);
if (!dev->open(dcr, mode)) {
dir->fsend(_("3910 Unable to open device \"%s\": ERR=%s\n"),
dev->print_name(), dev->bstrerror());
goto bail_out;
}
/* See what we have for a Volume */
label_status = read_dev_volume_label(dcr);
/* Set new volume name */
dcr->setVolCatName(newname);
switch(label_status) {
case VOL_NAME_ERROR:
case VOL_VERSION_ERROR:
case VOL_LABEL_ERROR:
case VOL_OK:
if (!relabel) {
dir->fsend(_("3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
dev->VolHdr.VolumeName);
goto bail_out;
}
/* Relabel request. If oldname matches, continue */
if (!bstrcmp(oldname, dev->VolHdr.VolumeName)) {
dir->fsend(_("3921 Wrong volume mounted.\n"));
goto bail_out;
}
if (dev->label_type != B_BAREOS_LABEL) {
dir->fsend(_("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
goto bail_out;
}
/* Fall through wanted! */
case VOL_IO_ERROR:
case VOL_NO_LABEL:
if (!write_new_volume_label_to_dev(dcr, newname, poolname, relabel)) {
dir->fsend(_("3912 Failed to label Volume: ERR=%s\n"), dev->bstrerror());
goto bail_out;
}
bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
/* The following 3000 OK label. string is scanned in ua_label.c */
dir->fsend("3000 OK label. VolBytes=%s Volume=\"%s\" Device=%s\n",
edit_uint64(dev->VolCatInfo.VolCatBytes, ed1),
newname, dev->print_name());
break;
case VOL_NO_MEDIA:
dir->fsend(_("3914 Failed to label Volume (no media): ERR=%s\n"), dev->bstrerror());
break;
default:
dir->fsend(_("3913 Cannot label Volume. "
"Unknown status %d from read_volume_label()\n"), label_status);
break;
}
bail_out:
if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
dev->close(dcr);
}
if (!dev->is_open()) {
dev->clear_volhdr();
}
volume_unused(dcr); /* no longer using volume */
give_back_device_lock(dev, &hold);
return;
}
/*
* Read the tape label
*
* Enter with the mutex set
*/
static bool read_label(DCR *dcr)
{
int ok;
JCR *jcr = dcr->jcr;
BSOCK *dir = jcr->dir_bsock;
bsteal_lock_t hold;
DEVICE *dev = dcr->dev;
steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
dcr->VolumeName[0] = 0;
dev->clear_labeled(); /* force read of label */
switch (read_dev_volume_label(dcr)) {
case VOL_OK:
dir->fsend(_("3001 Mounted Volume: %s\n"), dev->VolHdr.VolumeName);
ok = true;
break;
default:
dir->fsend(_("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
dev->print_name(), jcr->errmsg);
ok = false;
break;
}
volume_unused(dcr);
give_back_device_lock(dev, &hold);
return ok;
}
/*
* Searches for device by name, and if found, creates a dcr and returns it.
*/
static DCR *find_device(JCR *jcr, POOL_MEM &devname, int drive, BLOCKSIZES *blocksizes)
{
DEVRES *device;
AUTOCHANGERRES *changer;
bool found = false;
DCR *dcr = NULL;
unbash_spaces(devname);
foreach_res(device, R_DEVICE) {
/* Find resource, and make sure we were able to open it */
if (bstrcmp(device->hdr.name, devname.c_str())) {
if (!device->dev) {
device->dev = init_dev(jcr, device);
}
if (!device->dev) {
Jmsg(jcr, M_WARNING, 0, _("\n"
" Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
devname.c_str());
continue;
}
Dmsg1(20, "Found device %s\n", device->hdr.name);
found = true;
break;
}
}
if (!found) {
foreach_res(changer, R_AUTOCHANGER) {
/* Find resource, and make sure we were able to open it */
if (bstrcmp(devname.c_str(), changer->hdr.name)) {
/* Try each device in this AutoChanger */
foreach_alist(device, changer->device) {
Dmsg1(100, "Try changer device %s\n", device->hdr.name);
if (!device->dev) {
device->dev = init_dev(jcr, device);
}
if (!device->dev) {
Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
Jmsg(jcr, M_WARNING, 0, _("\n"
" Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
device->hdr.name, devname.c_str());
continue;
}
if (!device->dev->autoselect) {
Dmsg1(100, "Device %s not autoselect skipped.\n", devname.c_str());
continue; /* device is not available */
}
if (drive < 0 || drive == (int)device->dev->drive_index) {
Dmsg1(20, "Found changer device %s\n", device->hdr.name);
found = true;
break;
}
Dmsg3(100, "Device %s drive wrong: want=%d got=%d skipping\n",
devname.c_str(), drive, (int)device->dev->drive_index);
}
break; /* we found it but could not open a device */
}
}
}
if (found) {
Dmsg1(100, "Found device %s\n", device->hdr.name);
dcr = New(SD_DCR);
setup_new_dcr_device(jcr, dcr, device->dev, blocksizes);
dcr->set_will_write();
dcr->device = device;
}
return dcr;
}
/*
* Mount command from Director
*/
static bool mount_cmd(JCR *jcr)
{
POOL_MEM devname;
BSOCK *dir = jcr->dir_bsock;
DEVICE *dev;
DCR *dcr;
int32_t drive;
int32_t slot = 0;
bool ok;
ok = sscanf(dir->msg, mountslotcmd, devname.c_str(), &drive, &slot) == 3;
if (!ok) {
ok = sscanf(dir->msg, mountcmd, devname.c_str(), &drive) == 2;
}
Dmsg3(100, "ok=%d drive=%d slot=%d\n", ok, drive, slot);
if (ok) {
dcr = find_device(jcr, devname, drive, NULL);
if (dcr) {
dev = dcr->dev;
dev->Lock(); /* Use P to avoid indefinite block */
Dmsg2(100, "mount cmd blocked=%d must_unload=%d\n", dev->blocked(),
dev->must_unload());
switch (dev->blocked()) { /* device blocked? */
case BST_WAITING_FOR_SYSOP:
/* Someone is waiting, wake him */
Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
dev->set_blocked(BST_MOUNT);
dir->fsend("3001 OK mount requested. %sDevice=%s\n",
(slot > 0) ? _("Specified slot ignored. ") : "", dev->print_name());
pthread_cond_broadcast(&dev->wait_next_vol);
Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
release_device_cond();
break;
/* In both of these two cases, we (the user) unmounted the Volume */
case BST_UNMOUNTED_WAITING_FOR_SYSOP:
case BST_UNMOUNTED:
Dmsg2(100, "Unmounted changer=%d slot=%d\n", dev->is_autochanger(), slot);
if (dev->is_autochanger() && slot > 0) {
try_autoload_device(jcr, dcr, slot, "");
}
/* We freed the device, so reopen it and wake any waiting threads */
if (!dev->open(dcr, OPEN_READ_ONLY)) {
dir->fsend(_("3901 Unable to open device \"%s\": ERR=%s\n"), dev->print_name(), dev->bstrerror());
if (dev->blocked() == BST_UNMOUNTED) {
/* We blocked the device, so unblock it */
Dmsg0(100, "Unmounted. Unblocking device\n");
unblock_device(dev);
}
break;
}
read_dev_volume_label(dcr);
if (dev->blocked() == BST_UNMOUNTED) {
/* We blocked the device, so unblock it */
Dmsg0(100, "Unmounted. Unblocking device\n");
read_label(dcr); /* this should not be necessary */
unblock_device(dev);
} else {
Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
dev->set_blocked(BST_MOUNT);
}
if (dev->is_labeled()) {
dir->fsend(_("3001 Device \"%s\" is mounted with Volume \"%s\"\n"), dev->print_name(), dev->VolHdr.VolumeName);
} else {
dir->fsend(_("3905 Device \"%s\" open but no Bareos volume is mounted.\n"
"If this is not a blank tape, try unmounting and remounting the Volume.\n"), dev->print_name());
}
pthread_cond_broadcast(&dev->wait_next_vol);
Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
release_device_cond();
break;
case BST_DOING_ACQUIRE:
dir->fsend(_("3001 Device \"%s\" is doing acquire.\n"), dev->print_name());
break;
case BST_WRITING_LABEL:
dir->fsend(_("3903 Device \"%s\" is being labeled.\n"), dev->print_name());
break;
case BST_NOT_BLOCKED:
Dmsg2(100, "Not blocked changer=%d slot=%d\n", dev->is_autochanger(), slot);
if (dev->is_autochanger() && slot > 0) {
try_autoload_device(jcr, dcr, slot, "");
}