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