]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
1f5f271f3813115f16add28c52c5e3a0d12e1a46
[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-2003 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 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
34
35
36 /*
37  * Called here to do an autoload using the autochanger, if
38  *  configured, and if a Slot has been defined for this Volume.
39  *  On success this routine loads the indicated tape, but the
40  *  label is not read, so it must be verified.
41  *
42  *  Note if dir is not NULL, it is the console requesting the 
43  *   autoload for labeling, so we respond directly to the
44  *   dir bsock.
45  *
46  *  Returns: 1 on success
47  *           0 on failure
48  */
49 int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
50 {
51    int slot = jcr->VolCatInfo.Slot;
52    int rtn_stat = 0;
53      
54    /*
55     * Handle autoloaders here.  If we cannot autoload it, we
56     *  will return FALSE to ask the sysop.
57     */
58    if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
59       if (dir) {
60          return 0;                    /* For user, bail out right now */
61       }
62       if (dir_find_next_appendable_volume(jcr)) {
63          slot = jcr->VolCatInfo.Slot; 
64       }
65    }
66    Dmsg1(100, "Want changer slot=%d\n", slot);
67
68    if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
69       uint32_t timeout = jcr->device->max_changer_wait;
70       POOLMEM *changer, *results;
71       int status, loaded;
72
73       results = get_pool_memory(PM_MESSAGE);
74       changer = get_pool_memory(PM_FNAME);
75
76       /* Find out what is loaded, zero means device is unloaded */
77       changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
78                    "loaded");
79       status = run_program(changer, timeout, results);
80       Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
81       if (status == 0) {
82          loaded = atoi(results);
83       } else {
84          loaded = -1;              /* force unload */
85       }
86       Dmsg1(100, "loaded=%s\n", results);
87
88       /* If bad status or tape we want is not loaded, load it. */
89       if (status != 0 || loaded != slot) { 
90          if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
91             offline_dev(dev);
92          }
93          /* We are going to load a new tape, so close the device */
94          force_close_dev(dev);
95          if (loaded != 0) {        /* must unload drive */
96             Dmsg0(100, "Doing changer unload.\n");
97             if (dir) {
98                bnet_fsend(dir, _("3902 Issuing autochanger \"unload\" command.\n"));
99             } else {
100                Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"unload\" command.\n"));
101             }
102             changer = edit_device_codes(jcr, changer, 
103                         jcr->device->changer_command, "unload");
104             status = run_program(changer, timeout, NULL);
105             Dmsg1(100, "unload status=%d\n", status);
106          }
107          /*
108           * Load the desired cassette    
109           */
110          Dmsg1(100, "Doing changer load slot %d\n", slot);
111          if (dir) {
112             bnet_fsend(dir, _("3903 Issuing autochanger \"load slot %d\" command.\n"),
113                slot);
114          } else {
115             Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"load slot %d\" command.\n"),
116                slot);
117          }
118          changer = edit_device_codes(jcr, changer, 
119                       jcr->device->changer_command, "load");
120          status = run_program(changer, timeout, NULL);
121          if (status == 0) {
122             Jmsg(jcr, M_INFO, 0, _("Autochanger \"load slot\" status is OK.\n"));
123          } else {
124             Jmsg(jcr, M_INFO, 0, _("Bad autochanger \"load slot\" status = %d.\n"),
125                status);
126          }
127          Dmsg2(100, "load slot %d status=%d\n", slot, status);
128       }
129       free_pool_memory(changer);
130       free_pool_memory(results);
131       Dmsg1(100, "After changer, status=%d\n", status);
132       if (status == 0) {           /* did we succeed? */
133          rtn_stat = 1;             /* tape loaded by changer */
134       }
135    }
136    return rtn_stat;
137 }
138
139 /*
140  * List the Volumes that are in the autoloader possibly
141  *   with their barcodes.
142  */
143 int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
144 {
145    uint32_t timeout = jcr->device->max_changer_wait;
146    POOLMEM *changer;
147    BPIPE *bpipe;
148    int len = sizeof_pool_memory(dir->msg) - 1;
149
150    if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
151        !jcr->device->changer_command) {
152       bnet_fsend(dir, _("Not a changer device.\n"));
153       return 0;
154    }
155
156    changer = get_pool_memory(PM_FNAME);
157    if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
158       offline_dev(dev);
159    }
160    /* We are going to load a new tape, so close the device */
161    force_close_dev(dev);
162
163    /* First unload any tape */
164    bnet_fsend(dir, _("3902 Issuing autochanger \"unload\" command.\n"));
165    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
166    run_program(changer, timeout, NULL);
167
168    /* Now list slots occupied */
169    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
170    bnet_fsend(dir, _("3903 Issuing autochanger \"list\" command.\n"));
171    bpipe = open_bpipe(changer, timeout, "r");
172    if (!bpipe) {
173       bnet_fsend(dir, _("Open bpipe failed.\n"));
174       goto bail_out;
175    }
176    /* Get output from changer */
177    while (fgets(dir->msg, len, bpipe->rfd)) { 
178       dir->msglen = strlen(dir->msg);
179       bnet_send(dir);
180    }
181    bnet_sig(dir, BNET_EOD);
182    close_bpipe(bpipe);
183
184 bail_out:
185    free_pool_memory(changer);
186    return 1;
187 }
188
189
190
191 /*
192  * Edit codes into ChangerCommand
193  *  %% = %
194  *  %a = archive device name
195  *  %c = changer device name
196  *  %f = Client's name
197  *  %j = Job name
198  *  %o = command
199  *  %s = Slot base 0
200  *  %S = Slot base 1
201  *  %v = Volume name
202  *
203  *
204  *  omsg = edited output message
205  *  imsg = input string containing edit codes (%x)
206  *  cmd = command string (load, unload, ...) 
207  *
208  */
209 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd) 
210 {
211    char *p;
212    const char *str;
213    char add[20];
214
215    *omsg = 0;
216    Dmsg1(200, "edit_device_codes: %s\n", imsg);
217    for (p=imsg; *p; p++) {
218       if (*p == '%') {
219          switch (*++p) {
220          case '%':
221             str = "%";
222             break;
223          case 'a':
224             str = jcr->device->dev->dev_name;
225             break;
226          case 'c':
227             str = NPRT(jcr->device->changer_name);
228             break;
229          case 'o':
230             str = NPRT(cmd);
231             break;
232          case 's':
233             sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
234             str = add;
235             break;
236          case 'S':
237             sprintf(add, "%d", jcr->VolCatInfo.Slot);
238             str = add;
239             break;
240          case 'j':                    /* Job name */
241             str = jcr->Job;
242             break;
243          case 'v':
244             str = NPRT(jcr->VolumeName);
245             break;
246          case 'f':
247             str = NPRT(jcr->client_name);
248             break;
249
250          default:
251             add[0] = '%';
252             add[1] = *p;
253             add[2] = 0;
254             str = add;
255             break;
256          }
257       } else {
258          add[0] = *p;
259          add[1] = 0;
260          str = add;
261       }
262       Dmsg1(200, "add_str %s\n", str);
263       pm_strcat(&omsg, (char *)str);
264       Dmsg1(200, "omsg=%s\n", omsg);
265    }
266    return omsg;
267 }