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