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