]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
401f5c09f8bab386dd974e6dfb85526d770abfad
[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(400, "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       Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded\" command.\n"));
78       changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
79                    "loaded");
80       status = run_program(changer, timeout, results);
81       Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
82       if (status == 0) {
83          loaded = atoi(results);
84          if (loaded > 0) {
85             Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded\", result is Slot %d.\n"),
86               loaded);
87          } else {
88             Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded\", result is not loaded.\n"));
89          }
90       } else {
91          Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded\" command, status=%d.\n"), status);
92          loaded = -1;              /* force unload */
93       }
94       Dmsg1(400, "loaded=%s\n", results);
95
96       /* If bad status or tape we want is not loaded, load it. */
97       if (status != 0 || loaded != slot) { 
98          offline_or_rewind_dev(dev);
99          /* We are going to load a new tape, so close the device */
100          force_close_dev(dev);
101          if (loaded != 0) {        /* must unload drive */
102             Dmsg0(400, "Doing changer unload.\n");
103             Jmsg(jcr, M_INFO, 0, _("3303 Issuing autochanger \"unload\" command.\n"));
104             changer = edit_device_codes(jcr, changer, 
105                         jcr->device->changer_command, "unload");
106             status = run_program(changer, timeout, NULL);
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, _("3304 Issuing autochanger \"load slot %d\" command.\n"), 
114               slot);
115          changer = edit_device_codes(jcr, changer, 
116                       jcr->device->changer_command, "load");
117          status = run_program(changer, timeout, NULL);
118          if (status == 0) {
119             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d\", status is OK.\n"),
120                     slot);
121          } else {
122             Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d\", status=%d.\n"),
123                     slot, status);
124          }
125          Dmsg2(400, "load slot %d status=%d\n", slot, status);
126       }
127       free_pool_memory(changer);
128       free_pool_memory(results);
129       Dmsg1(400, "After changer, status=%d\n", status);
130       if (status == 0) {           /* did we succeed? */
131          rtn_stat = 1;             /* tape loaded by changer */
132       }
133    }
134    return rtn_stat;
135 }
136
137 /*
138  * The Volume is not in the correct slot, so mark this 
139  *   Volume as not being in the Changer.
140  */
141 void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev)
142 {
143    Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
144 "    Setting slot to zero in catalog.\n"),
145         jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
146    jcr->VolCatInfo.InChanger = false;
147    dev->VolCatInfo.InChanger = false;
148    Dmsg0(100, "update vol info in mount\n");
149    dir_update_volume_info(jcr, dev, 1);  /* set new status */
150 }
151
152 /*
153  * List the Volumes that are in the autoloader possibly
154  *   with their barcodes.
155  *   We assume that it is always the Console that is calling us.
156  */
157 int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
158 {
159    uint32_t timeout = jcr->device->max_changer_wait;
160    POOLMEM *changer;
161    BPIPE *bpipe;
162    int len = sizeof_pool_memory(dir->msg) - 1;
163
164    if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
165        !jcr->device->changer_command) {
166       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
167       return 0;
168    }
169
170    changer = get_pool_memory(PM_FNAME);
171    offline_or_rewind_dev(dev);
172    /* We are going to load a new tape, so close the device */
173    force_close_dev(dev);
174
175    /* First unload any tape */
176    bnet_fsend(dir, _("3305 Issuing autochanger \"unload\" command.\n"));
177    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
178    run_program(changer, timeout, NULL);
179
180    /* Now list slots occupied */
181    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
182    bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
183    bpipe = open_bpipe(changer, timeout, "r");
184    if (!bpipe) {
185       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
186       goto bail_out;
187    }
188    /* Get output from changer */
189    while (fgets(dir->msg, len, bpipe->rfd)) { 
190       dir->msglen = strlen(dir->msg);
191       bnet_send(dir);
192    }
193    bnet_sig(dir, BNET_EOD);
194    close_bpipe(bpipe);
195
196 bail_out:
197    free_pool_memory(changer);
198    return 1;
199 }
200
201
202
203 /*
204  * Edit codes into ChangerCommand
205  *  %% = %
206  *  %a = archive device name
207  *  %c = changer device name
208  *  %f = Client's name
209  *  %j = Job name
210  *  %o = command
211  *  %s = Slot base 0
212  *  %S = Slot base 1
213  *  %v = Volume name
214  *
215  *
216  *  omsg = edited output message
217  *  imsg = input string containing edit codes (%x)
218  *  cmd = command string (load, unload, ...) 
219  *
220  */
221 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd) 
222 {
223    char *p;
224    const char *str;
225    char add[20];
226
227    *omsg = 0;
228    Dmsg1(400, "edit_device_codes: %s\n", imsg);
229    for (p=imsg; *p; p++) {
230       if (*p == '%') {
231          switch (*++p) {
232          case '%':
233             str = "%";
234             break;
235          case 'a':
236             str = dev_name(jcr->device->dev);
237             break;
238          case 'c':
239             str = NPRT(jcr->device->changer_name);
240             break;
241          case 'o':
242             str = NPRT(cmd);
243             break;
244          case 's':
245             sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
246             str = add;
247             break;
248          case 'S':
249             sprintf(add, "%d", jcr->VolCatInfo.Slot);
250             str = add;
251             break;
252          case 'j':                    /* Job name */
253             str = jcr->Job;
254             break;
255          case 'v':
256             str = NPRT(jcr->VolumeName);
257             break;
258          case 'f':
259             str = NPRT(jcr->client_name);
260             break;
261
262          default:
263             add[0] = '%';
264             add[1] = *p;
265             add[2] = 0;
266             str = add;
267             break;
268          }
269       } else {
270          add[0] = *p;
271          add[1] = 0;
272          str = add;
273       }
274       Dmsg1(400, "add_str %s\n", str);
275       pm_strcat(&omsg, (char *)str);
276       Dmsg1(400, "omsg=%s\n", omsg);
277    }
278    return omsg;
279 }