1 /*
2 Bacula(R) - The Network Backup Solution
3
4 Copyright (C) 2000-2020 Kern Sibbald
5
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
8
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
13
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
16
17 Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20 *
21 * Routines for getting tape worm status
22 *
23 * Written by Kern Sibbald, September MMXVIII
24 *
25 */
26
27 #include "bacula.h" /* pull in global headers */
28 #include "stored.h" /* pull in Storage Deamon headers */
29
30
31 static const int dbglvl = 50;
32
33
get_tape_worm(DCR * dcr)34 bool tape_dev::get_tape_worm(DCR *dcr)
35 {
36 JCR *jcr = dcr->jcr;
37
38 if (!job_canceled(jcr) && dcr->device->worm_command &&
39 dcr->device->control_name) {
40 POOLMEM *wormcmd;
41 int status = 1;
42 bool is_worm = false;
43 int worm_val = 0;
44 BPIPE *bpipe;
45 char line[MAXSTRING];
46 const char *fmt = " %d";
47
48 wormcmd = get_pool_memory(PM_FNAME);
49 wormcmd = edit_device_codes(dcr, wormcmd, dcr->device->worm_command, "");
50 /* Wait maximum 5 minutes */
51 bpipe = open_bpipe(wormcmd, 60 * 5, "r");
52 if (bpipe) {
53 while (fgets(line, (int)sizeof(line), bpipe->rfd)) {
54 is_worm = false;
55 if (bsscanf(line, fmt, &worm_val) == 1) {
56 if (worm_val > 0) {
57 is_worm = true;
58 }
59 }
60 }
61 status = close_bpipe(bpipe);
62 free_pool_memory(wormcmd);
63 return is_worm;
64 } else {
65 status = errno;
66 }
67 if (status != 0) {
68 berrno be;
69 Jmsg(jcr, M_WARNING, 0, _("3997 Bad worm command status: %s: ERR=%s.\n"),
70 wormcmd, be.bstrerror(status));
71 Dmsg2(dbglvl, _("3997 Bad worm command status: %s: ERR=%s.\n"),
72 wormcmd, be.bstrerror(status));
73 }
74
75 Dmsg1(400, "worm script status=%d\n", status);
76 free_pool_memory(wormcmd);
77 } else {
78 if (!dcr->device->worm_command) {
79 Dmsg1(dbglvl, "Cannot get tape worm status: no Worm Command specified for device %s\n",
80 print_name());
81 Dmsg1(dbglvl, "Cannot get tape worm status: no Worm Command specified for device %s\n",
82 print_name());
83
84 }
85 if (!dcr->device->control_name) {
86 Dmsg1(dbglvl, "Cannot get tape worm status: no Control Device specified for device %s\n",
87 print_name());
88 Dmsg1(dbglvl, "Cannot get tape worm status: no Control Device specified for device %s\n",
89 print_name());
90 }
91 }
92 return false;
93 }
94