]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
- Correct bug in parse_config error handling.
[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-2005 Kern Sibbald
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 static int get_autochanger_loaded_slot(DCR *dcr);
34 static void lock_changer(DCR *dcr);
35 static void unlock_changer(DCR *dcr);
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    POOLMEM *changer;
59
60    slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
61    /*
62     * Handle autoloaders here.  If we cannot autoload it, we
63     *  will return FALSE to ask the sysop.
64     */
65    if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
66       if (dir) {
67          return 0;                    /* For user, bail out right now */
68       }
69       if (dir_find_next_appendable_volume(dcr)) {
70          slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
71       } else {
72          slot = 0;
73       }
74    }
75    Dmsg1(400, "Want changer slot=%d\n", slot);
76
77    changer = get_pool_memory(PM_FNAME);
78    if (slot > 0 && dcr->device->changer_name && dcr->device->changer_command) {
79       uint32_t timeout = dcr->device->max_changer_wait;
80       int loaded, status;
81
82       loaded = get_autochanger_loaded_slot(dcr);
83
84       /* If tape we want is not loaded, load it. */
85       if (loaded != slot) {
86          offline_or_rewind_dev(dev);
87          /* We are going to load a new tape, so close the device */
88          force_close_dev(dev);
89          lock_changer(dcr);
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, 
97                         dcr->device->changer_command, "unload");
98             status = run_program(changer, timeout, NULL);
99             if (status != 0) {
100                berrno be;
101                be.set_errno(status);
102                Jmsg(jcr, M_FATAL, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
103                     slot, drive, be.strerror());
104                goto bail_out;
105             }
106
107             Dmsg1(400, "unload status=%d\n", status);
108          }
109          /*
110           * Load the desired cassette
111           */
112          Dmsg1(400, "Doing changer load slot %d\n", slot);
113          Jmsg(jcr, M_INFO, 0,
114               _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
115               slot, drive);
116          dcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
117          changer = edit_device_codes(dcr, changer, 
118                       dcr->device->changer_command, "load");
119          status = run_program(changer, timeout, NULL);
120          if (status == 0) {
121             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
122                     slot, drive);
123          } else {
124            berrno be;
125            be.set_errno(status);
126             Jmsg(jcr, M_FATAL, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
127                     slot, drive, be.strerror());
128             goto bail_out;
129          }
130          unlock_changer(dcr);
131          Dmsg2(400, "load slot %d status=%d\n", slot, status);
132       } else {
133          status = 0;                  /* we got what we want */
134       }
135       Dmsg1(400, "After changer, status=%d\n", status);
136       if (status == 0) {              /* did we succeed? */
137          rtn_stat = 1;                /* tape loaded by changer */
138       }
139    } else {
140       rtn_stat = 0;                   /* no changer found */
141    }
142    free_pool_memory(changer);
143    return rtn_stat;
144
145 bail_out:
146    free_pool_memory(changer);
147    unlock_changer(dcr);
148    return -1;
149
150 }
151
152 static int get_autochanger_loaded_slot(DCR *dcr)
153 {
154    JCR *jcr = dcr->jcr;
155    POOLMEM *changer, *results;
156    int status, loaded;
157    uint32_t timeout = dcr->device->max_changer_wait;
158    int drive = dcr->device->drive_index;
159
160    results = get_pool_memory(PM_MESSAGE);
161    changer = get_pool_memory(PM_FNAME);
162
163    lock_changer(dcr);
164
165    /* Find out what is loaded, zero means device is unloaded */
166    Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
167         drive);
168    changer = edit_device_codes(dcr, changer, 
169                 dcr->device->changer_command, "loaded");
170    status = run_program(changer, timeout, results);
171    Dmsg3(50, "run_prog: %s stat=%d result=%s", changer, status, results);
172    if (status == 0) {
173       loaded = atoi(results);
174       if (loaded > 0) {
175          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
176               drive, loaded);
177       } else {
178          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
179               drive);
180       }
181    } else {
182       berrno be;
183       be.set_errno(status);
184       Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
185            drive, be.strerror());
186       loaded = -1;              /* force unload */
187    }
188    unlock_changer(dcr);
189    free_pool_memory(changer);
190    free_pool_memory(results);
191    return loaded;
192 }
193
194 static void lock_changer(DCR *dcr)
195 {
196    AUTOCHANGER *changer_res = dcr->device->changer_res;
197    if (changer_res) {
198       Dmsg1(100, "Locking changer %s\n", changer_res->hdr.name);
199       P(changer_res->changer_mutex);  /* Lock changer script */
200    }
201 }
202
203 static void unlock_changer(DCR *dcr)
204 {
205    AUTOCHANGER *changer_res = dcr->device->changer_res;
206    if (changer_res) {
207       Dmsg1(100, "Unlocking changer %s\n", changer_res->hdr.name);
208       V(changer_res->changer_mutex);  /* Unlock changer script */
209    }
210 }
211
212
213 /*
214  * List the Volumes that are in the autoloader possibly
215  *   with their barcodes.
216  *   We assume that it is always the Console that is calling us.
217  */
218 bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)  
219 {
220    DEVICE *dev = dcr->dev;
221    JCR *jcr = dcr->jcr;
222    uint32_t timeout = dcr->device->max_changer_wait;
223    POOLMEM *changer;
224    BPIPE *bpipe;
225    int slot, loaded;
226    int len = sizeof_pool_memory(dir->msg) - 1;
227    bool ok = false;
228    int stat;
229
230    if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
231        !dcr->device->changer_command) {
232       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
233       return false;
234    }
235
236    changer = get_pool_memory(PM_FNAME);
237    /* List command? */
238    if (strcmp(cmd, "list") == 0) {
239       int drive = dev->device->drive_index;
240       /* Yes, to get a good listing, we unload any volumes */
241       offline_or_rewind_dev(dev);
242       /* We are going to load a new tape, so close the device */
243       force_close_dev(dev);
244
245       /* First unload any tape */
246       loaded = get_autochanger_loaded_slot(dcr);
247       if (loaded > 0) {
248          bnet_fsend(dir,
249             _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
250             loaded, drive);
251          slot = dcr->VolCatInfo.Slot;
252          dcr->VolCatInfo.Slot = loaded;
253          changer = edit_device_codes(dcr, changer, 
254                       dcr->device->changer_command, "unload");
255          lock_changer(dcr);
256          int stat = run_program(changer, timeout, NULL);
257          unlock_changer(dcr);
258          if (stat != 0) {
259             berrno be;
260             be.set_errno(stat);
261             Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
262                     slot, drive, be.strerror());
263          }
264          dcr->VolCatInfo.Slot = slot;
265       }
266    }
267
268    /* Now issue the command */
269    changer = edit_device_codes(dcr, changer, 
270                  dcr->device->changer_command, cmd);
271    bnet_fsend(dir, _("3306 Issuing autochanger \"%s\" command.\n"), cmd);
272    lock_changer(dcr);
273    bpipe = open_bpipe(changer, timeout, "r");
274    if (!bpipe) {
275       unlock_changer(dcr);
276       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
277       goto bail_out;
278    }
279    if (strcmp(cmd, "list") == 0) {
280       /* Get output from changer */
281       while (fgets(dir->msg, len, bpipe->rfd)) {
282          dir->msglen = strlen(dir->msg);
283          Dmsg1(100, "<stored: %s\n", dir->msg);
284          bnet_send(dir);
285       }
286    } else {
287       /* For slots command, read a single line */
288       bstrncpy(dir->msg, "slots=", len);
289       fgets(dir->msg+6, len-6, bpipe->rfd);
290       dir->msglen = strlen(dir->msg);
291       Dmsg1(100, "<stored: %s", dir->msg);
292       bnet_send(dir);
293    }
294                  
295    stat = close_bpipe(bpipe);
296    unlock_changer(dcr);
297    if (stat != 0) {
298       berrno be;
299       be.set_errno(stat);
300       bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
301    }
302    bnet_sig(dir, BNET_EOD);
303    ok = true;
304
305 bail_out:
306    free_pool_memory(changer);
307    return true;
308 }
309
310
311 /*
312  * Edit codes into ChangerCommand
313  *  %% = %
314  *  %a = archive device name
315  *  %c = changer device name
316  *  %d = changer drive index
317  *  %f = Client's name
318  *  %j = Job name
319  *  %o = command
320  *  %s = Slot base 0
321  *  %S = Slot base 1
322  *  %v = Volume name
323  *
324  *
325  *  omsg = edited output message
326  *  imsg = input string containing edit codes (%x)
327  *  cmd = command string (load, unload, ...)
328  *
329  */
330 char *edit_device_codes(DCR *dcr, char *omsg, const char *imsg, const char *cmd)
331 {
332    const char *p;
333    const char *str;
334    char add[20];
335
336    *omsg = 0;
337    Dmsg1(1800, "edit_device_codes: %s\n", imsg);
338    for (p=imsg; *p; p++) {
339       if (*p == '%') {
340          switch (*++p) {
341          case '%':
342             str = "%";
343             break;
344          case 'a':
345             str = dcr->dev->archive_name();
346             break;
347          case 'c':
348             str = NPRT(dcr->device->changer_name);
349             break;
350          case 'd':
351             sprintf(add, "%d", dcr->dev->drive_index);
352             str = add;
353             break;
354          case 'o':
355             str = NPRT(cmd);
356             break;
357          case 's':
358             sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
359             str = add;
360             break;
361          case 'S':
362             sprintf(add, "%d", dcr->VolCatInfo.Slot);
363             str = add;
364             break;
365          case 'j':                    /* Job name */
366             str = dcr->jcr->Job;
367             break;
368          case 'v':
369             str = NPRT(dcr->VolumeName);
370             break;
371          case 'f':
372             str = NPRT(dcr->jcr->client_name);
373             break;
374
375          default:
376             add[0] = '%';
377             add[1] = *p;
378             add[2] = 0;
379             str = add;
380             break;
381          }
382       } else {
383          add[0] = *p;
384          add[1] = 0;
385          str = add;
386       }
387       Dmsg1(1900, "add_str %s\n", str);
388       pm_strcat(&omsg, (char *)str);
389       Dmsg1(1800, "omsg=%s\n", omsg);
390    }
391    Dmsg1(800, "omsg=%s\n", omsg);
392    return omsg;
393 }