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