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.36 2006/08/24 01:57:16 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(char *cmd, char *arg);
60 static int report_bad_resultstr(void);
61 static int run_changer_command(char *cmd, char *arg, char **slotstr, char **rest);
66 tapechanger = getconf_str(CNF_TPCHANGER);
67 return strcmp(tapechanger, "") != 0;
72 report_bad_resultstr(void)
76 s = vstralloc("badly formed result from changer: ",
77 "\"", changer_resultstr, "\"",
79 amfree(changer_resultstr);
80 changer_resultstr = s;
103 exitcode = changer_command(cmd, arg);
104 s = changer_resultstr;
107 skip_whitespace(s, ch);
108 if(ch == '\0') return report_bad_resultstr();
110 skip_non_whitespace(s, ch);
113 *slotstr = newstralloc(*slotstr, slot);
117 skip_whitespace(s, ch);
123 if(ch == '\0') return report_bad_resultstr();
124 result_copy = stralloc(s - 1);
125 amfree(changer_resultstr);
126 changer_resultstr = result_copy;
138 return run_changer_command("-reset", (char *) NULL, slotstr, &rest);
147 return run_changer_command("-clean", (char *) NULL, slotstr, &rest);
156 return run_changer_command("-eject", (char *) NULL, slotstr, &rest);
168 rc = run_changer_command("-slot", inslotstr, outslotstr, &rest);
171 if(*rest == '\0') return report_bad_resultstr();
173 *devicename = newstralloc(*devicename, rest);
179 * This function is somewhat equal to changer_info with one additional
180 * parameter, to get information, if the changer is able to search for
181 * tapelabels himself. E.g. Barcodereader
182 * The changer_script answers with an additional parameter, if it is able
183 * to search. This one should be 1, if it is able to search, and 0 if it
184 * knows about the extension. If the additional answer is omitted, the
185 * changer is not able to search for a tape.
198 rc = run_changer_command("-info", (char *) NULL, curslotstr, &rest);
201 dbprintf(("changer_query: changer return was %s\n",rest));
202 if (sscanf(rest, "%d %d %d", nslotsp, backwardsp, searchable) != 3) {
203 if (sscanf(rest, "%d %d", nslotsp, backwardsp) != 2) {
204 return report_bad_resultstr();
209 dbprintf(("changer_query: searchable = %d\n",*searchable));
222 rc = run_changer_command("-info", (char *) NULL, curslotstr, &rest);
225 if (sscanf(rest, "%d %d", nslotsp, backwardsp) != 2) {
226 return report_bad_resultstr();
232 /* ---------------------------- */
235 * This function first uses searchlabel and changer_search, if
236 * the library is able to find a tape itself. If it is not, or if
237 * the tape could not be found, then the normal scan is done.
239 * See interface documentation in changer.h.
245 int (*user_init)(void *, int, int, int, int),
246 int (*user_slot)(void *, int, char *, char *),
249 char *slotstr, *device = NULL, *curslotstr = NULL;
250 int nslots, checked, backwards, rc, done, searchable;
252 rc = changer_query(&nslots, &curslotstr, &backwards, &searchable);
255 /* Problem with the changer script. Bail. */
256 fprintf(stderr, "Changer problem: %s\n", changer_resultstr);
260 done = user_init(user_data, rc, nslots, backwards, searchable);
263 if (searchlabel != NULL)
265 dbprintf(("changer_find: looking for %s changer is searchable = %d\n",
266 searchlabel, searchable));
268 dbprintf(("changer_find: looking for NULL changer is searchable = %d\n",
272 if ((searchlabel!=NULL) && searchable && !done){
273 rc=changer_search(searchlabel, &curslotstr, &device);
275 done = user_slot(user_data, rc, curslotstr, device);
281 while(!done && checked < nslots) {
282 rc = changer_loadslot(slotstr, &curslotstr, &device);
284 done = user_slot(user_data, rc, curslotstr, device);
286 done = user_slot(user_data, 0, curslotstr, device);
295 /* ---------------------------- */
300 int (*user_init)(void *, int, int, int, int),
301 int (*user_slot)(void *, int, char *, char *))
303 char *device = NULL, *curslotstr = NULL;
304 int nslots, backwards, rc, done, searchable;
306 rc = changer_query(&nslots, &curslotstr, &backwards, &searchable);
307 done = user_init(user_data, rc, nslots, backwards, searchable);
310 rc = changer_loadslot("current", &curslotstr, &device);
312 done = user_slot(user_data, rc, curslotstr, device);
314 done = user_slot(user_data, 0, curslotstr, device);
320 /* ---------------------------- */
328 amwait_t wait_exitcode = 1;
330 char num1[NUM_STR_SIZE];
331 char num2[NUM_STR_SIZE];
333 pid_t pid, changer_pid = 0;
335 if (*tapechanger != '/') {
336 tapechanger = vstralloc(libexecdir, "/", tapechanger, versionsuffix(),
338 malloc_mark(tapechanger);
340 cmdstr = vstralloc(tapechanger, " ",
346 fprintf(stderr, "changer: opening pipe to: %s\n", cmdstr);
350 amfree(changer_resultstr);
352 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
353 changer_resultstr = vstralloc ("<error> ",
354 "could not create pipe for \"",
363 /* make sure fd[0] != 1 */
370 if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
371 snprintf(num1, SIZEOF(num1), "%d", fd[0]);
372 snprintf(num2, SIZEOF(num2), "%d", FD_SETSIZE-1);
373 changer_resultstr = vstralloc ("<error> ",
374 "could not create pipe for \"",
377 "socketpair 0: descriptor ",
379 " out of range ( .. ",
386 if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) {
387 snprintf(num1, SIZEOF(num1), "%d", fd[1]);
388 snprintf(num2, SIZEOF(num2), "%d", FD_SETSIZE-1);
389 changer_resultstr = vstralloc ("<error> ",
390 "could not create pipe for \"",
393 "socketpair 1: descriptor ",
395 " out of range ( .. ",
403 switch(changer_pid = fork()) {
405 changer_resultstr = vstralloc ("<error> ",
406 "could not fork for \"",
414 if(dup2(fd[1], 1) == -1 || dup2(fd[1], 2) == -1) {
415 changer_resultstr = vstralloc ("<error> ",
416 "could not open pipe to \"",
421 (void)fullwrite(fd[1], changer_resultstr, strlen(changer_resultstr));
426 if(config_dir && chdir(config_dir) == -1) {
427 changer_resultstr = vstralloc ("<error> ",
428 "could not cd to \"",
433 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
437 execle(tapechanger, tapechanger, cmd, arg, (char *)NULL,
440 execle(tapechanger, tapechanger, cmd, (char *)NULL, safe_env());
442 changer_resultstr = vstralloc ("<error> ",
448 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
454 if((changer_resultstr = areads(fd[0])) == NULL) {
455 changer_resultstr = vstralloc ("<error> ",
456 "could not read result from \"",
458 errno ? "\": " : "\"",
459 errno ? strerror(errno) : "",
464 if ((pid = wait(&wait_exitcode)) == -1) {
468 changer_resultstr = vstralloc ("<error> ",
477 } else if (pid != changer_pid) {
478 snprintf(num1, SIZEOF(num1), "%ld", (long)pid);
479 changer_resultstr = vstralloc ("<error> ",
482 "\" returned unexpected pid ",
492 /* mark out-of-control changers as fatal error */
493 if(WIFSIGNALED(wait_exitcode)) {
494 snprintf(num1, SIZEOF(num1), "%d", WTERMSIG(wait_exitcode));
495 changer_resultstr = newvstralloc (changer_resultstr,
498 " (got signal ", num1, ")",
502 exitcode = WEXITSTATUS(wait_exitcode);
511 dbprintf(("changer: got exit: %d str: %s\n", exitcode, changer_resultstr));
521 * This function commands the changerscript to look for a tape named
522 * searchlabel. If is found, the changerscript answers with the device,
523 * in which the tape can be accessed.
535 dbprintf(("changer_search: %s\n",searchlabel));
536 rc = run_changer_command("-search", searchlabel, outslotstr, &rest);
539 if(*rest == '\0') return report_bad_resultstr();
541 *devicename = newstralloc(*devicename, rest);
547 * Because barcodelabel are short, and may not be the same as the
548 * amandalabels, the changerscript should be informed, which tapelabel
549 * is associated with a tape. This function should be called after
550 * giving a label for a tape. (Maybe also, when the label and the associated
551 * slot is known. e.g. during library scan.
562 char *curslotstr = NULL;
563 int nslots, backwards, searchable;
565 dbprintf(("changer_label: %s for slot %s\n",labelstr,slotsp));
566 rc = changer_query(&nslots, &curslotstr, &backwards,&searchable);
569 if ((rc == 0) && (searchable == 1)){
570 dbprintf(("changer_label: calling changer -label %s\n",labelstr));
571 rc = run_changer_command("-label", labelstr, &slotstr, &rest);