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;
334 int fd_to_close[4], *pfd_to_close = fd_to_close;
336 if (*tapechanger != '/') {
337 tapechanger = vstralloc(libexecdir, "/", tapechanger, versionsuffix(),
339 malloc_mark(tapechanger);
341 cmdstr = vstralloc(tapechanger, " ",
347 fprintf(stderr, "changer: opening pipe to: %s\n", cmdstr);
351 amfree(changer_resultstr);
353 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
354 changer_resultstr = vstralloc ("<error> ",
355 "could not create pipe for \"",
364 /* make sure fd[0] > 2 && fd[1] > 2 */
365 pfd_to_close = fd_to_close;
368 *pfd_to_close++ = fd[0];
373 *pfd_to_close++ = fd[1];
376 while (pfd_to_close > fd_to_close) {
377 close(*--pfd_to_close);
380 if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
381 snprintf(num1, SIZEOF(num1), "%d", fd[0]);
382 snprintf(num2, SIZEOF(num2), "%d", FD_SETSIZE-1);
383 changer_resultstr = vstralloc ("<error> ",
384 "could not create pipe for \"",
387 "socketpair 0: descriptor ",
389 " out of range ( .. ",
396 if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) {
397 snprintf(num1, SIZEOF(num1), "%d", fd[1]);
398 snprintf(num2, SIZEOF(num2), "%d", FD_SETSIZE-1);
399 changer_resultstr = vstralloc ("<error> ",
400 "could not create pipe for \"",
403 "socketpair 1: descriptor ",
405 " out of range ( .. ",
413 switch(changer_pid = fork()) {
415 changer_resultstr = vstralloc ("<error> ",
416 "could not fork for \"",
424 if(dup2(fd[1], 1) == -1 || dup2(dbfd(), 2) == -1) {
425 changer_resultstr = vstralloc ("<error> ",
426 "could not open pipe to \"",
431 (void)fullwrite(fd[1], changer_resultstr, strlen(changer_resultstr));
436 if(config_dir && chdir(config_dir) == -1) {
437 changer_resultstr = vstralloc ("<error> ",
438 "could not cd to \"",
443 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
447 execle(tapechanger, tapechanger, cmd, arg, (char *)NULL,
450 execle(tapechanger, tapechanger, cmd, (char *)NULL, safe_env());
452 changer_resultstr = vstralloc ("<error> ",
458 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
464 if((changer_resultstr = areads(fd[0])) == NULL) {
465 changer_resultstr = vstralloc ("<error> ",
466 "could not read result from \"",
468 errno ? "\": " : "\"",
469 errno ? strerror(errno) : "",
474 if ((pid = wait(&wait_exitcode)) == -1) {
478 changer_resultstr = vstralloc ("<error> ",
487 } else if (pid != changer_pid) {
488 snprintf(num1, SIZEOF(num1), "%ld", (long)pid);
489 changer_resultstr = vstralloc ("<error> ",
492 "\" returned unexpected pid ",
502 /* mark out-of-control changers as fatal error */
503 if(WIFSIGNALED(wait_exitcode)) {
504 snprintf(num1, SIZEOF(num1), "%d", WTERMSIG(wait_exitcode));
505 changer_resultstr = newvstralloc (changer_resultstr,
508 " (got signal ", num1, ")",
512 exitcode = WEXITSTATUS(wait_exitcode);
521 dbprintf(("changer: got exit: %d str: %s\n", exitcode, changer_resultstr));
531 * This function commands the changerscript to look for a tape named
532 * searchlabel. If is found, the changerscript answers with the device,
533 * in which the tape can be accessed.
545 dbprintf(("changer_search: %s\n",searchlabel));
546 rc = run_changer_command("-search", searchlabel, outslotstr, &rest);
549 if(*rest == '\0') return report_bad_resultstr();
551 *devicename = newstralloc(*devicename, rest);
557 * Because barcodelabel are short, and may not be the same as the
558 * amandalabels, the changerscript should be informed, which tapelabel
559 * is associated with a tape. This function should be called after
560 * giving a label for a tape. (Maybe also, when the label and the associated
561 * slot is known. e.g. during library scan.
572 char *curslotstr = NULL;
573 int nslots, backwards, searchable;
575 dbprintf(("changer_label: %s for slot %s\n",labelstr,slotsp));
576 rc = changer_query(&nslots, &curslotstr, &backwards,&searchable);
579 if ((rc == 0) && (searchable == 1)){
580 dbprintf(("changer_label: calling changer -label %s\n",labelstr));
581 rc = run_changer_command("-label", labelstr, &slotstr, &rest);