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.2.1 2006/04/24 14:43:01 martinea 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)
98 exitcode = changer_command(cmd, arg);
99 s = changer_resultstr;
102 skip_whitespace(s, ch);
103 if(ch == '\0') return report_bad_resultstr();
105 skip_non_whitespace(s, ch);
108 *slotstr = newstralloc(*slotstr, slot);
112 skip_whitespace(s, ch);
118 if(ch == '\0') return report_bad_resultstr();
119 result_copy = stralloc(s - 1);
120 amfree(changer_resultstr);
121 changer_resultstr = result_copy;
127 int changer_reset(slotstr)
132 return run_changer_command("-reset", (char *) NULL, slotstr, &rest);
135 int changer_clean(slotstr)
140 return run_changer_command("-clean", (char *) NULL, slotstr, &rest);
143 int changer_eject(slotstr)
148 return run_changer_command("-eject", (char *) NULL, slotstr, &rest);
151 int changer_loadslot(inslotstr, outslotstr, devicename)
152 char *inslotstr, **outslotstr, **devicename;
157 rc = run_changer_command("-slot", inslotstr, outslotstr, &rest);
160 if(*rest == '\0') return report_bad_resultstr();
162 *devicename = newstralloc(*devicename, rest);
166 /* This function is somewhat equal to changer_info with one additional
167 parameter, to get information, if the changer is able to search for
168 tapelabels himself. E.g. Barcodereader
169 The changer_script answers with an additional parameter, if it is able
170 to search. This one should be 1, if it is able to search, and 0 if it
171 knows about the extension. If the additional answer is omitted, the
172 changer is not able to search for a tape.
174 int changer_query(nslotsp, curslotstr, backwardsp, searchable)
175 int *nslotsp, *backwardsp, *searchable;
181 rc = run_changer_command("-info", (char *) NULL, curslotstr, &rest);
184 dbprintf(("changer_query: changer return was %s\n",rest));
185 if (sscanf(rest, "%d %d %d", nslotsp, backwardsp, searchable) != 3) {
186 if (sscanf(rest, "%d %d", nslotsp, backwardsp) != 2) {
187 return report_bad_resultstr();
192 dbprintf(("changer_query: searchable = %d\n",*searchable));
196 int changer_info(nslotsp, curslotstr, backwardsp)
197 int *nslotsp, *backwardsp;
203 rc = run_changer_command("-info", (char *) NULL, curslotstr, &rest);
206 if (sscanf(rest, "%d %d", nslotsp, backwardsp) != 2) {
207 return report_bad_resultstr();
213 /* ---------------------------- */
215 /* This function first uses searchlabel and changer_search, if
216 the library is able to find a tape itself. If it is not, or if
217 the tape could not be found, then the normal scan is done.
219 See interface documentation in changer.h.
221 void changer_find(user_data, user_init, user_slot, searchlabel)
223 int (*user_init) P((void *user_data, int rc, int nslots, int backwards,
225 int (*user_slot) P((void *user_data, int rc, char *slotstr,
229 char *slotstr, *device = NULL, *curslotstr = NULL;
230 int nslots, checked, backwards, rc, done, searchable;
232 rc = changer_query(&nslots, &curslotstr, &backwards, &searchable);
233 done = user_init(user_data, rc, nslots, backwards, searchable);
236 if (searchlabel != NULL)
238 dbprintf(("changer_find: looking for %s changer is searchable = %d\n",
239 searchlabel, searchable));
241 dbprintf(("changer_find: looking for NULL changer is searchable = %d\n",
245 if ((searchlabel!=NULL) && searchable && !done){
246 rc=changer_search(searchlabel,&curslotstr,&device);
248 done = user_slot(user_data, rc,curslotstr,device);
254 while(!done && checked < nslots) {
255 rc = changer_loadslot(slotstr, &curslotstr, &device);
257 done = user_slot(user_data, rc, curslotstr, device);
259 done = user_slot(user_data, 0, curslotstr, device);
268 /* ---------------------------- */
270 void changer_current(user_data, user_init, user_slot)
272 int (*user_init) P((void *ud, int rc, int nslots, int backwards, int searchable));
273 int (*user_slot) P((void *ud, int rc, char *slotstr, char *device));
275 char *device = NULL, *curslotstr = NULL;
276 int nslots, backwards, rc, done, searchable;
278 rc = changer_query(&nslots, &curslotstr, &backwards, &searchable);
279 done = user_init(user_data, rc, nslots, backwards, searchable);
282 rc = changer_loadslot("current", &curslotstr, &device);
284 done = user_slot(user_data, rc, curslotstr, device);
286 done = user_slot(user_data, 0, curslotstr, device);
292 /* ---------------------------- */
294 static int changer_command(cmd, arg)
299 amwait_t wait_exitcode;
301 char num1[NUM_STR_SIZE];
302 char num2[NUM_STR_SIZE];
304 pid_t pid, changer_pid = 0;
306 if (*tapechanger != '/') {
307 tapechanger = vstralloc(libexecdir, "/", tapechanger, versionsuffix(),
309 malloc_mark(tapechanger);
311 cmdstr = vstralloc(tapechanger, " ",
317 fprintf(stderr, "changer: opening pipe to: %s\n", cmdstr);
321 amfree(changer_resultstr);
323 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
324 changer_resultstr = vstralloc ("<error> ",
325 "could not create pipe for \"",
333 if(fd[0] < 0 || fd[0] >= FD_SETSIZE) {
334 snprintf(num1, sizeof(num1), "%d", fd[0]);
335 snprintf(num2, sizeof(num2), "%d", FD_SETSIZE-1);
336 changer_resultstr = vstralloc ("<error> ",
337 "could not create pipe for \"",
340 "socketpair 0: descriptor ",
342 " out of range ( .. ",
349 if(fd[1] < 0 || fd[1] >= FD_SETSIZE) {
350 snprintf(num1, sizeof(num1), "%d", fd[1]);
351 snprintf(num2, sizeof(num2), "%d", FD_SETSIZE-1);
352 changer_resultstr = vstralloc ("<error> ",
353 "could not create pipe for \"",
356 "socketpair 1: descriptor ",
358 " out of range ( .. ",
366 switch(changer_pid = fork()) {
368 changer_resultstr = vstralloc ("<error> ",
369 "could not fork for \"",
377 if(dup2(fd[1], 1) == -1 || dup2(fd[1], 2) == -1) {
378 changer_resultstr = vstralloc ("<error> ",
379 "could not open pipe to \"",
384 (void)fullwrite(fd[1], changer_resultstr, strlen(changer_resultstr));
389 if(config_dir && chdir(config_dir) == -1) {
390 changer_resultstr = vstralloc ("<error> ",
391 "could not cd to \"",
396 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
400 execle(tapechanger, tapechanger, cmd, arg, NULL, safe_env());
402 execle(tapechanger, tapechanger, cmd, NULL, safe_env());
404 changer_resultstr = vstralloc ("<error> ",
410 (void)fullwrite(2, changer_resultstr, strlen(changer_resultstr));
416 if((changer_resultstr = areads(fd[0])) == NULL) {
417 changer_resultstr = vstralloc ("<error> ",
418 "could not read result from \"",
420 errno ? "\": " : "\"",
421 errno ? strerror(errno) : "",
426 if ((pid = wait(&wait_exitcode)) == -1) {
430 changer_resultstr = vstralloc ("<error> ",
439 } else if (pid != changer_pid) {
440 snprintf(num1, sizeof(num1), "%ld", (long)pid);
441 changer_resultstr = vstralloc ("<error> ",
444 "\" returned unexpected pid ",
454 /* mark out-of-control changers as fatal error */
455 if(WIFSIGNALED(wait_exitcode)) {
456 snprintf(num1, sizeof(num1), "%d", WTERMSIG(wait_exitcode));
457 changer_resultstr = newvstralloc (changer_resultstr,
460 " (got signal ", num1, ")",
464 exitcode = WEXITSTATUS(wait_exitcode);
472 dbprintf(("changer: got exit: %d str: %s\n", exitcode, changer_resultstr));
479 /* This function commands the changerscript to look for a tape named
480 searchlabel. If is found, the changerscript answers with the device,
481 in which the tape can be accessed.
483 int changer_search(searchlabel, outslotstr, devicename)
484 char *searchlabel, **outslotstr, **devicename;
489 dbprintf(("changer_search: %s\n",searchlabel));
490 rc = run_changer_command("-search", searchlabel, outslotstr, &rest);
493 if(*rest == '\0') return report_bad_resultstr();
495 *devicename = newstralloc(*devicename, rest);
499 /* Because barcodelabel are short, and may not be the same as the
500 amandalabels, the changerscript should be informed, which tapelabel
501 is associated with a tape. This function should be called after
502 giving a label for a tape. (Maybe also, when the label and the associated
503 slot is known. e.g. during library scan.
505 int changer_label (slotsp,labelstr)
512 char *curslotstr = NULL;
513 int nslots, backwards, searchable;
515 dbprintf(("changer_label: %s for slot %s\n",labelstr,slotsp));
516 rc = changer_query(&nslots, &curslotstr, &backwards,&searchable);
519 if ((rc == 0) && (searchable == 1)){
520 dbprintf(("changer_label: calling changer -label %s\n",labelstr));
521 rc = run_changer_command("-label", labelstr, &slotstr, &rest);