]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
- Integrated TLS network encryption
[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  * The Volume is not in the correct slot, so mark this
215  *   Volume as not being in the Changer.
216  */
217 void mark_volume_not_inchanger(DCR *dcr)
218 {
219    JCR *jcr = dcr->jcr;
220    DEVICE *dev = dcr->dev;
221    Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
222 "    Setting InChanger to zero in catalog.\n"),
223         dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
224    dcr->VolCatInfo.InChanger = false;
225    dev->VolCatInfo.InChanger = false;
226    Dmsg0(400, "update vol info in mount\n");
227    dir_update_volume_info(dcr, true);  /* set new status */
228 }
229
230 /*
231  * List the Volumes that are in the autoloader possibly
232  *   with their barcodes.
233  *   We assume that it is always the Console that is calling us.
234  */
235 bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)  
236 {
237    DEVICE *dev = dcr->dev;
238    JCR *jcr = dcr->jcr;
239    uint32_t timeout = dcr->device->max_changer_wait;
240    POOLMEM *changer;
241    BPIPE *bpipe;
242    int slot, loaded;
243    int len = sizeof_pool_memory(dir->msg) - 1;
244    bool ok = false;
245    int stat;
246
247    if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
248        !dcr->device->changer_command) {
249       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
250       return false;
251    }
252
253    changer = get_pool_memory(PM_FNAME);
254    /* List command? */
255    if (strcmp(cmd, "list") == 0) {
256       int drive = dev->device->drive_index;
257       /* Yes, to get a good listing, we unload any volumes */
258       offline_or_rewind_dev(dev);
259       /* We are going to load a new tape, so close the device */
260       force_close_dev(dev);
261
262       /* First unload any tape */
263       loaded = get_autochanger_loaded_slot(dcr);
264       if (loaded > 0) {
265          bnet_fsend(dir,
266             _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
267             loaded, drive);
268          slot = dcr->VolCatInfo.Slot;
269          dcr->VolCatInfo.Slot = loaded;
270          changer = edit_device_codes(dcr, changer, 
271                       dcr->device->changer_command, "unload");
272          lock_changer(dcr);
273          int stat = run_program(changer, timeout, NULL);
274          unlock_changer(dcr);
275          if (stat != 0) {
276             berrno be;
277             be.set_errno(stat);
278             Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
279                     slot, drive, be.strerror());
280          }
281          dcr->VolCatInfo.Slot = slot;
282       }
283    }
284
285    /* Now issue the command */
286    changer = edit_device_codes(dcr, changer, 
287                  dcr->device->changer_command, cmd);
288    bnet_fsend(dir, _("3306 Issuing autochanger \"%s\" command.\n"), cmd);
289    lock_changer(dcr);
290    bpipe = open_bpipe(changer, timeout, "r");
291    if (!bpipe) {
292       unlock_changer(dcr);
293       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
294       goto bail_out;
295    }
296    if (strcmp(cmd, "list") == 0) {
297       /* Get output from changer */
298       while (fgets(dir->msg, len, bpipe->rfd)) {
299          dir->msglen = strlen(dir->msg);
300          Dmsg1(100, "<stored: %s\n", dir->msg);
301          bnet_send(dir);
302       }
303    } else {
304       /* For slots command, read a single line */
305       bstrncpy(dir->msg, "slots=", len);
306       fgets(dir->msg+6, len-6, bpipe->rfd);
307       dir->msglen = strlen(dir->msg);
308       Dmsg1(100, "<stored: %s", dir->msg);
309       bnet_send(dir);
310    }
311                  
312    stat = close_bpipe(bpipe);
313    unlock_changer(dcr);
314    if (stat != 0) {
315       berrno be;
316       be.set_errno(stat);
317       bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
318    }
319    bnet_sig(dir, BNET_EOD);
320    ok = true;
321
322 bail_out:
323    free_pool_memory(changer);
324    return true;
325 }
326
327
328 /*
329  * Edit codes into ChangerCommand
330  *  %% = %
331  *  %a = archive device name
332  *  %c = changer device name
333  *  %d = changer drive index
334  *  %f = Client's name
335  *  %j = Job name
336  *  %o = command
337  *  %s = Slot base 0
338  *  %S = Slot base 1
339  *  %v = Volume name
340  *
341  *
342  *  omsg = edited output message
343  *  imsg = input string containing edit codes (%x)
344  *  cmd = command string (load, unload, ...)
345  *
346  */
347 char *edit_device_codes(DCR *dcr, char *omsg, const char *imsg, const char *cmd)
348 {
349    const char *p;
350    const char *str;
351    char add[20];
352
353    *omsg = 0;
354    Dmsg1(1800, "edit_device_codes: %s\n", imsg);
355    for (p=imsg; *p; p++) {
356       if (*p == '%') {
357          switch (*++p) {
358          case '%':
359             str = "%";
360             break;
361          case 'a':
362             str = dcr->dev->archive_name();
363             break;
364          case 'c':
365             str = NPRT(dcr->device->changer_name);
366             break;
367          case 'd':
368             sprintf(add, "%d", dcr->dev->drive_index);
369             str = add;
370             break;
371          case 'o':
372             str = NPRT(cmd);
373             break;
374          case 's':
375             sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
376             str = add;
377             break;
378          case 'S':
379             sprintf(add, "%d", dcr->VolCatInfo.Slot);
380             str = add;
381             break;
382          case 'j':                    /* Job name */
383             str = dcr->jcr->Job;
384             break;
385          case 'v':
386             str = NPRT(dcr->VolumeName);
387             break;
388          case 'f':
389             str = NPRT(dcr->jcr->client_name);
390             break;
391
392          default:
393             add[0] = '%';
394             add[1] = *p;
395             add[2] = 0;
396             str = add;
397             break;
398          }
399       } else {
400          add[0] = *p;
401          add[1] = 0;
402          str = add;
403       }
404       Dmsg1(1900, "add_str %s\n", str);
405       pm_strcat(&omsg, (char *)str);
406       Dmsg1(1800, "omsg=%s\n", omsg);
407    }
408    Dmsg1(800, "omsg=%s\n", omsg);
409    return omsg;
410 }