]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
- Modified ANSI label code to preserve any ANSI label
[bacula/bacula] / bacula / src / stored / autochanger.c
1 /*
2  *
3  *  Routines for handling the autochanger.
4  *
5  *   Kern Sibbald, August MMII
6  *                            
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"                   /* pull in global headers */
30 #include "stored.h"                   /* pull in Storage Deamon headers */
31
32 /* Forward referenced functions */
33 char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd);
34 static int get_autochanger_loaded_slot(DCR *dcr);
35
36
37 /*
38  * Called here to do an autoload using the autochanger, if
39  *  configured, and if a Slot has been defined for this Volume.
40  *  On success this routine loads the indicated tape, but the
41  *  label is not read, so it must be verified.
42  *
43  *  Note if dir is not NULL, it is the console requesting the
44  *   autoload for labeling, so we respond directly to the
45  *   dir bsock.
46  *
47  *  Returns: 1 on success
48  *           0 on failure (no changer available)
49  *          -1 on error on autochanger
50  */
51 int autoload_device(DCR *dcr, int writing, BSOCK *dir)
52 {
53    JCR *jcr = dcr->jcr;
54    DEVICE *dev = dcr->dev;
55    int slot;
56    int drive = dev->device->drive_index;
57    int rtn_stat = -1;                 /* error status */
58
59    slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
60    /*
61     * Handle autoloaders here.  If we cannot autoload it, we
62     *  will return FALSE to ask the sysop.
63     */
64    if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
65       if (dir) {
66          return 0;                    /* For user, bail out right now */
67       }
68       if (dir_find_next_appendable_volume(dcr)) {
69          slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
70       } else {
71          slot = 0;
72       }
73    }
74    Dmsg1(400, "Want changer slot=%d\n", slot);
75
76    if (slot > 0 && dcr->device->changer_name && dcr->device->changer_command) {
77       uint32_t timeout = dcr->device->max_changer_wait;
78       POOLMEM *changer;
79       int loaded, status;
80
81       changer = get_pool_memory(PM_FNAME);
82
83       loaded = get_autochanger_loaded_slot(dcr);
84
85       /* If tape we want is not loaded, load it. */
86       if (loaded != slot) {
87          offline_or_rewind_dev(dev);
88          /* We are going to load a new tape, so close the device */
89          force_close_dev(dev);
90          if (loaded != 0 && loaded != -1) {        /* must unload drive */
91             Dmsg0(400, "Doing changer unload.\n");
92             Jmsg(jcr, M_INFO, 0,
93                  _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
94                  loaded, drive);
95             dcr->VolCatInfo.Slot = loaded;   /* slot to be unloaded */
96             changer = edit_device_codes(dcr, changer, "unload");
97             status = run_program(changer, timeout, NULL);
98             if (status != 0) {
99                berrno be;
100                be.set_errno(status);
101                Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
102                     slot, drive, be.strerror());
103             }
104
105             Dmsg1(400, "unload status=%d\n", status);
106          }
107          /*
108           * Load the desired cassette
109           */
110          Dmsg1(400, "Doing changer load slot %d\n", slot);
111          Jmsg(jcr, M_INFO, 0,
112               _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
113               slot, drive);
114          dcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
115          changer = edit_device_codes(dcr, changer, "load");
116          status = run_program(changer, timeout, NULL);
117          if (status == 0) {
118             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
119                     slot, drive);
120          } else {
121            berrno be;
122            be.set_errno(status);
123             Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
124                     slot, drive, be.strerror());
125          }
126          Dmsg2(400, "load slot %d status=%d\n", slot, status);
127       } else {
128          status = 0;                  /* we got what we want */
129       }
130       free_pool_memory(changer);
131       Dmsg1(400, "After changer, status=%d\n", status);
132       if (status == 0) {              /* did we succeed? */
133          rtn_stat = 1;                /* tape loaded by changer */
134       }
135    } else {
136       rtn_stat = 0;                   /* no changer found */
137    }
138    return rtn_stat;
139 }
140
141 static int get_autochanger_loaded_slot(DCR *dcr)
142 {
143    JCR *jcr = dcr->jcr;
144    POOLMEM *changer, *results;
145    int status, loaded;
146    uint32_t timeout = dcr->device->max_changer_wait;
147    int drive = dcr->device->drive_index;
148
149    results = get_pool_memory(PM_MESSAGE);
150    changer = get_pool_memory(PM_FNAME);
151
152    /* Find out what is loaded, zero means device is unloaded */
153    Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
154         drive);
155    changer = edit_device_codes(dcr, changer, "loaded");
156    status = run_program(changer, timeout, results);
157    Dmsg3(50, "run_prog: %s stat=%d result=%s\n", changer, status, results);
158    if (status == 0) {
159       loaded = atoi(results);
160       if (loaded > 0) {
161          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
162               drive, loaded);
163       } else {
164          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
165               drive);
166       }
167    } else {
168       berrno be;
169       be.set_errno(status);
170       Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
171            drive, be.strerror());
172       loaded = -1;              /* force unload */
173    }
174    free_pool_memory(changer);
175    free_pool_memory(results);
176    return loaded;
177 }
178
179 /*
180  * The Volume is not in the correct slot, so mark this
181  *   Volume as not being in the Changer.
182  */
183 void invalid_slot_in_catalog(DCR *dcr)
184 {
185    JCR *jcr = dcr->jcr;
186    DEVICE *dev = dcr->dev;
187    Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
188 "    Setting slot to zero in catalog.\n"),
189         dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
190    dcr->VolCatInfo.InChanger = false;
191    dev->VolCatInfo.InChanger = false;
192    Dmsg0(400, "update vol info in mount\n");
193    dir_update_volume_info(dcr, true);  /* set new status */
194 }
195
196 /*
197  * List the Volumes that are in the autoloader possibly
198  *   with their barcodes.
199  *   We assume that it is always the Console that is calling us.
200  */
201 bool autochanger_list(DCR *dcr, BSOCK *dir)
202 {
203    DEVICE *dev = dcr->dev;
204    JCR *jcr = dcr->jcr;
205    uint32_t timeout = dcr->device->max_changer_wait;
206    POOLMEM *changer;
207    BPIPE *bpipe;
208    int slot, loaded;
209    int len = sizeof_pool_memory(dir->msg) - 1;
210
211    if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
212        !dcr->device->changer_command) {
213       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
214       return false;
215    }
216
217    changer = get_pool_memory(PM_FNAME);
218    offline_or_rewind_dev(dev);
219    /* We are going to load a new tape, so close the device */
220    force_close_dev(dev);
221
222    /* First unload any tape */
223    loaded = get_autochanger_loaded_slot(dcr);
224    if (loaded > 0) {
225       bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
226       slot = dcr->VolCatInfo.Slot;
227       dcr->VolCatInfo.Slot = loaded;
228       changer = edit_device_codes(dcr, changer, "unload");
229       int stat = run_program(changer, timeout, NULL);
230       if (stat != 0) {
231          berrno be;
232          be.set_errno(stat);
233          Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d\" command: ERR=%s.\n"),
234               loaded, be.strerror());
235       }
236       dcr->VolCatInfo.Slot = slot;
237    }
238
239    /* Now list slots occupied */
240    changer = edit_device_codes(dcr, changer, "list");
241    bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
242    bpipe = open_bpipe(changer, timeout, "r");
243    if (!bpipe) {
244       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
245       free_pool_memory(changer);
246       return false;
247    }
248    /* Get output from changer */
249    while (fgets(dir->msg, len, bpipe->rfd)) {
250       dir->msglen = strlen(dir->msg);
251       bnet_send(dir);
252    }
253    int stat = close_bpipe(bpipe);
254    if (stat != 0) {
255       berrno be;
256       be.set_errno(stat);
257       bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
258    }
259    bnet_sig(dir, BNET_EOD);
260
261    free_pool_memory(changer);
262    return true;
263 }
264
265
266 /*
267  * Edit codes into ChangerCommand
268  *  %% = %
269  *  %a = archive device name
270  *  %c = changer device name
271  *  %d = changer drive index
272  *  %f = Client's name
273  *  %j = Job name
274  *  %o = command
275  *  %s = Slot base 0
276  *  %S = Slot base 1
277  *  %v = Volume name
278  *
279  *
280  *  omsg = edited output message
281  *  imsg = input string containing edit codes (%x)
282  *  cmd = command string (load, unload, ...)
283  *
284  */
285 char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd)
286 {
287    const char *p;
288    const char *str;
289    char add[20];
290    const char *imsg = dcr->device->changer_command;
291
292    *omsg = 0;
293    Dmsg1(800, "edit_device_codes: %s\n", imsg);
294    for (p=imsg; *p; p++) {
295       if (*p == '%') {
296          switch (*++p) {
297          case '%':
298             str = "%";
299             break;
300          case 'a':
301             str = dev_name(dcr->dev);
302             break;
303          case 'c':
304             str = NPRT(dcr->device->changer_name);
305             break;
306          case 'd':
307             sprintf(add, "%d", dcr->dev->drive_index);
308             str = add;
309             break;
310          case 'o':
311             str = NPRT(cmd);
312             break;
313          case 's':
314             sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
315             str = add;
316             break;
317          case 'S':
318             sprintf(add, "%d", dcr->VolCatInfo.Slot);
319             str = add;
320             break;
321          case 'j':                    /* Job name */
322             str = dcr->jcr->Job;
323             break;
324          case 'v':
325             str = NPRT(dcr->VolumeName);
326             break;
327          case 'f':
328             str = NPRT(dcr->jcr->client_name);
329             break;
330
331          default:
332             add[0] = '%';
333             add[1] = *p;
334             add[2] = 0;
335             str = add;
336             break;
337          }
338       } else {
339          add[0] = *p;
340          add[1] = 0;
341          str = add;
342       }
343       Dmsg1(900, "add_str %s\n", str);
344       pm_strcat(&omsg, (char *)str);
345       Dmsg1(800, "omsg=%s\n", omsg);
346    }
347    return omsg;
348 }