2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: changer.c,v 1.29 2006/01/14 04:37:19 paddy_s Exp $
29 * interface routines for tape changers
39 * If we don't have the new-style wait access functions, use our own,
40 * compatible with old-style BSD systems at least. Note that we don't
41 * care about the case w_stopval == WSTOPPED since we don't ask to see
42 * stopped processes, so should never get them from wait.
45 # define WEXITSTATUS(r) (((union wait *) &(r))->w_retcode)
46 # define WTERMSIG(r) (((union wait *) &(r))->w_termsig)
49 # define WIFSIGNALED(r) (((union wait *) &(r))->w_termsig != 0)
53 int changer_debug = 0;
54 char *changer_resultstr = NULL;
56 static char *tapechanger = NULL;
59 static int changer_command P((char *cmd, char *arg));
63 tapechanger = getconf_str(CNF_TPCHANGER);
64 return strcmp(tapechanger, "") != 0;
68 static int report_bad_resultstr()
72 s = vstralloc("badly formed result from changer: ",
73 "\"", changer_resultstr, "\"",
75 amfree(changer_resultstr);
76 changer_resultstr = s;
80 static int run_changer_command(cmd, arg, slotstr, rest)
94 exitcode = changer_command(cmd, arg);
95 s = changer_resultstr;
98 skip_whitespace(s, ch);
99 if(ch == '\0') return report_bad_resultstr();
101 skip_non_whitespace(s, ch);
103 *slotstr = newstralloc(*slotstr, slot);
106 skip_whitespace(s, ch);
110 if(ch == '\0') return report_bad_resultstr();
111 result_copy = stralloc(s - 1);
112 amfree(changer_resultstr);
113 changer_resultstr = result_copy;
119 int changer_reset(slotstr)
124 return run_changer_command("-reset", (char *) NULL, slotstr, &rest);
127 int changer_clean(slotstr)
132 return run_changer_command("-clean", (char *) NULL, slotstr, &rest);
135 int changer_eject(slotstr)
140 return run_changer_command("-eject", (char *) NULL, slotstr, &rest);
143 int changer_loadslot(inslotstr, outslotstr, devicename)
144 char *inslotstr, **outslotstr, **devicename;
149 rc = run_changer_command("-slot", inslotstr, outslotstr, &rest);
152 if(*rest == '\0') return report_bad_resultstr();
154 *devicename = newstralloc(*devicename, rest);
158 /* This function is somewhat equal to changer_info with one additional
159 parameter, to get information, if the changer is able to search for
160 tapelabels himself. E.g. Barcodereader
161 The changer_script answers with an additional parameter, if it is able
162 to search. This one should be 1, if it is able to search, and 0 if it
163 knows about the extension. If the additional answer is omitted, the
164 changer is not able to search for a tape.
166 int changer_query(nslotsp, curslotstr, backwardsp, searchable)
167 int *nslotsp, *backwardsp, *searchable;
173 rc = run_changer_command("-info", (char *) NULL, curslotstr, &rest);
176 dbprintf(("changer_query: changer return was %s\n",rest));
177 if (sscanf(rest, "%d %d %d", nslotsp, backwardsp, searchable) != 3) {
178 if (sscanf(rest, "%d %d", nslotsp, backwardsp) != 2) {
179 return report_bad_resultstr();
184 dbprintf(("changer_query: searchable = %d\n",*searchable));
188 int changer_info(nslotsp, curslotstr, backwardsp)
189 int *nslotsp, *backwardsp;
195 rc = run_changer_command("-info", (char *) NULL, curslotstr, &rest);
198 if (sscanf(rest, "%d %d", nslotsp, backwardsp) != 2) {
199 return report_bad_resultstr();
205 /* ---------------------------- */
207 /* This function first uses searchlabel and changer_search, if
208 the library is able to find a tape itself. If it is not, or if
209 the tape could not be found, then the normal scan is done.
211 See interface documentation in changer.h.
213 void changer_find(user_data, user_init, user_slot, searchlabel)
215 int (*user_init) P((void *user_data, int rc, int nslots, int backwards,
217 int (*user_slot) P((void *user_data, int rc, char *slotstr,
221 char *slotstr, *device = NULL, *curslotstr = NULL;
222 int nslots, checked, backwards, rc, done, searchable;
224 rc = changer_query(&nslots, &curslotstr, &backwards, &searchable);
225 done = user_init(user_data, rc, nslots, backwards, searchable);
228 if (searchlabel != NULL)
230 dbprintf(("changer_find: looking for %s changer is searchable = %d\n",
231 searchlabel, searchable));
233 dbprintf(("changer_find: looking for NULL changer is searchable = %d\n",
237 if ((searchlabel!=NULL) && searchable && !done){
238 rc=changer_search(searchlabel,&curslotstr,&device);
240 done = user_slot(user_data, rc,curslotstr,device);
246 while(!done && checked < nslots) {
247 rc = changer_loadslot(slotstr, &curslotstr, &device);
249 done = user_slot(user_data, rc, curslotstr, device);
251 done = user_slot(user_data, 0, curslotstr, device);
260 /* ---------------------------- */
262 void changer_current(user_data, user_init, user_slot)
264 int (*user_init) P((void *ud, int rc, int nslots, int backwards, int searchable));
265 int (*user_slot) P((void *ud, int rc, char *slotstr, char *device));
267 char *device = NULL, *curslotstr = NULL;
268 int nslots, backwards, rc, done, searchable;
270 rc = changer_query(&nslots, &curslotstr, &backwards, &searchable);
271 done = user_init(user_data, rc, nslots, backwards, searchable);
274 rc = changer_loadslot("current", &curslotstr, &device);
276 done = user_slot(user_data, rc, curslotstr, device);
278 done = user_slot(user_data, 0, curslotstr, device);
284 /* ---------------------------- */
286 static int changer_command(cmd, arg)
291 amwait_t wait_exitcode;
293 char num1[NUM_STR_SIZE];
294 char num2[NUM_STR_SIZE];
296 pid_t pid, changer_pid = 0;
298 if (*tapechanger != '/') {
299 tapechanger = vstralloc(libexecdir, "/", tapechanger, versionsuffix(),
301 malloc_mark(tapechanger);
303 cmdstr = vstralloc(tapechanger, " ",
309 fprintf(stderr, "changer: opening pipe to: %s\n", cmdstr);
313 amfree(changer_resultstr);
315 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
316 changer_resultstr = vstralloc ("<error> ",
317 "could not create pipe for \"",
325 if(fd[0] < 0 || fd[0] >= FD_SETSIZE) {
326 snprintf(num1, sizeof(num1), "%d", fd[0]);
327 snprintf(num2, sizeof(num2), "%d", FD_SETSIZE-1);
328 changer_resultstr = vstralloc ("<error> ",
329 "could not create pipe for \"",
332 "socketpair 0: descriptor ",
334 " out of range ( .. ",
341 if(fd[1] < 0 || fd[1] >= FD_SETSIZE) {
342 snprintf(num1, sizeof(num1), "%d", fd[1]);
343 snprintf(num2, sizeof(num2), "%d", FD_SETSIZE-1);
344 changer_resultstr = vstralloc ("<error> ",
345 "could not create pipe for \"",
348 "socketpair 1: descriptor ",
350 " out of range ( .. ",
358 switch(changer_pid = fork()) {
360 changer_resultstr = vstralloc ("<error> ",
361 "could not fork for \"",
369 if(dup2(fd[1], 1) == -1 || dup2(fd[1], 2) == -1) {
370 changer_resultstr = vstralloc ("<error> ",
371 "could not open pipe to \"",
376 (void)fullwrite(fd[1], changer_resultstr, strlen(changer_resultstr));
381 if(config_dir && chdir(config_dir) == -1) {
382 changer_resultstr = vstralloc ("<error> ",
383 "could not cd to \"",
388 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
392 execle(tapechanger, tapechanger, cmd, arg, NULL, safe_env());
394 execle(tapechanger, tapechanger, cmd, NULL, safe_env());
396 changer_resultstr = vstralloc ("<error> ",
402 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
408 if((changer_resultstr = areads(fd[0])) == NULL) {
409 changer_resultstr = vstralloc ("<error> ",
410 "could not read result from \"",
412 errno ? "\": " : "\"",
413 errno ? strerror(errno) : "",
418 if ((pid = wait(&wait_exitcode)) == -1) {
422 changer_resultstr = vstralloc ("<error> ",
431 } else if (pid != changer_pid) {
432 snprintf(num1, sizeof(num1), "%ld", (long)pid);
433 changer_resultstr = vstralloc ("<error> ",
436 "\" returned unexpected pid ",
446 /* mark out-of-control changers as fatal error */
447 if(WIFSIGNALED(wait_exitcode)) {
448 snprintf(num1, sizeof(num1), "%d", WTERMSIG(wait_exitcode));
449 changer_resultstr = newvstralloc (changer_resultstr,
452 " (got signal ", num1, ")",
456 exitcode = WEXITSTATUS(wait_exitcode);
464 dbprintf(("changer: got exit: %d str: %s\n", exitcode, changer_resultstr));
471 /* This function commands the changerscript to look for a tape named
472 searchlabel. If is found, the changerscript answers with the device,
473 in which the tape can be accessed.
475 int changer_search(searchlabel, outslotstr, devicename)
476 char *searchlabel, **outslotstr, **devicename;
481 dbprintf(("changer_search: %s\n",searchlabel));
482 rc = run_changer_command("-search", searchlabel, outslotstr, &rest);
485 if(*rest == '\0') return report_bad_resultstr();
487 *devicename = newstralloc(*devicename, rest);
491 /* Because barcodelabel are short, and may not be the same as the
492 amandalabels, the changerscript should be informed, which tapelabel
493 is associated with a tape. This function should be called after
494 giving a label for a tape. (Maybe also, when the label and the associated
495 slot is known. e.g. during library scan.
497 int changer_label (slotsp,labelstr)
504 char *curslotstr = NULL;
505 int nslots, backwards, searchable;
507 dbprintf(("changer_label: %s for slot %s\n",labelstr,slotsp));
508 rc = changer_query(&nslots, &curslotstr, &backwards,&searchable);
511 if ((rc == 0) && (searchable == 1)){
512 dbprintf(("changer_label: calling changer -label %s\n",labelstr));
513 rc = run_changer_command("-label", labelstr, &slotstr, &rest);