2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2000 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.
26 /* $Id: taper.c 6512 2007-05-24 17:00:24Z ian $
28 * moves files from holding disk to tape, or from a socket to tape
31 /* FIXME: This file needs to use gettext. */
41 #include "server_util.h"
44 #include "taperscan.h"
45 #include "taper-source.h"
46 #include "timestamp.h"
50 /* FIXME: This should not be here. */
51 #define CONNECT_TIMEOUT (2*60)
53 /* Use this instead of global variables, so that we are reentrant. */
56 char * driver_start_time;
58 char * next_tape_label;
59 char * next_tape_device;
60 taper_scan_tracker_t * taper_scan_tracker;
76 static gboolean label_new_tape(taper_state_t * state, dump_info_t * dump_info);
78 static void init_taper_state(taper_state_t* state) {
80 state->driver_start_time = NULL;
81 state->taper_scan_tracker = taper_scan_tracker_new();
84 static void cleanup(taper_state_t * state) {
85 amfree(state->driver_start_time);
86 amfree(state->next_tape_label);
87 amfree(state->next_tape_device);
88 taper_scan_tracker_free(state->taper_scan_tracker);
89 if (state->device != NULL) {
90 g_object_unref(state->device);
95 static void free_dump_info(dump_info_t * info) {
97 amfree(info->hostname);
98 amfree(info->diskname);
99 amfree(info->timestamp);
100 amfree(info->id_string);
101 if (info->source != NULL) {
102 g_object_unref(info->source);
107 /* Validate that a command has the proper number of arguments, and
108 print a meaningful error message if not. It returns only if the
109 check is successful. */
110 static void validate_args(cmd_t cmd, struct cmdargs * args,
114 for (i = 0; argnames[i] != NULL; i ++) {
115 if (i > args->argc) {
116 error("error [taper %s: not enough args: %s]",
117 cmdstr[cmd], argnames[i]);
120 if (i < args->argc) {
121 error("error [taper %s: Too many args: Got %d, expected %d.]",
122 cmdstr[cmd], args->argc, i);
126 /* Open a socket to the dumper. Returns TRUE if everything is happy, FALSE
128 static gboolean open_read_socket(dump_info_t * info, char * split_diskbuffer,
129 guint64 splitsize, guint64 fallback_splitsize) {
134 struct addrinfo *res;
136 if ((result = resolve_hostname("localhost", 0, &res, NULL) != 0)) {
139 int save_errno = errno;
140 char *qdiskname = quote_string(info->diskname);
142 m = vstralloc("[localhost resolve failure: ",
143 strerror(save_errno),
147 putresult(TAPE_ERROR, "%s %s\n", info->handle, q);
148 log_add(L_FAIL, "%s %s %s %d %s",
149 info->hostname, qdiskname, info->timestamp,
157 socket = stream_server(res->ai_family, &port, 0, STREAM_BUFSIZE, 0);
163 int save_errno = errno;
164 char *qdiskname = quote_string(info->diskname);
166 m = vstralloc("[port create failure: ",
167 strerror(save_errno),
171 putresult(TAPE_ERROR, "%s %s\n", info->handle, q);
172 log_add(L_FAIL, "%s %s %s %d %s",
173 info->hostname, qdiskname, info->timestamp,
181 putresult(PORT, "%d\n", port);
183 fd = stream_accept(socket, CONNECT_TIMEOUT, 0, STREAM_BUFSIZE);
187 int save_errno = errno;
188 char *qdiskname = quote_string(info->diskname);
189 m = vstralloc("[port connect failure: ",
190 strerror(save_errno),
194 putresult(TAPE_ERROR, "%s %s\n", info->handle, q);
195 log_add(L_FAIL, "%s %s %s %d %s",
196 info->hostname, qdiskname, info->timestamp,
207 info->source = taper_source_new(info->handle, PORT_WRITE, NULL, fd,
208 split_diskbuffer, splitsize,
210 /* FIXME: This should be handled properly. */
211 g_assert(info->source != NULL);
216 ConsumerFunctor next_consumer;
217 gpointer next_consumer_data;
218 guint64 bytes_written;
219 } CountingConsumerData;
221 /* A ConsumerFunctor. This consumer just passes its arguments on to a
222 second consumer, but counts the number of bytes successfully
224 static int counting_consumer(gpointer user_data, queue_buffer_t * buffer) {
226 CountingConsumerData * data = user_data;
228 result = data->next_consumer(data->next_consumer_data, buffer);
231 data->bytes_written += result;
237 static gboolean boolean_prolong(void * data) {
239 return TRUE; /* Do not interrupt. */
241 return *(gboolean*)data;
245 /* A (simpler) wrapper around taper_scan(). */
246 static gboolean simple_taper_scan(taper_state_t * state,
247 gboolean* prolong, char ** error_message) {
248 char ** label = &(state->next_tape_label);
249 char ** device = &(state->next_tape_device);
250 char *timestamp = NULL;
252 result = taper_scan(NULL, label, ×tamp, device,
253 state->taper_scan_tracker,
254 CHAR_taperscan_output_callback,
255 error_message, boolean_prolong, prolong);
256 if (prolong != NULL && !*prolong) {
257 g_fprintf(stderr, _("Cancelled taper scan.\n"));
259 } else if (result < 0) {
260 g_fprintf(stderr, _("Failed taper scan: %s\n"), (*error_message)?(*error_message):_("(no error message)"));
264 g_fprintf(stderr, _("taper: using label `%s' date `%s'\n"), *label,
265 state->driver_start_time);
268 _("Will write new label `%s' to new (previously non-amanda) tape"),
278 taper_state_t * state;
279 gboolean prolong; /* scan stops when this is FALSE. */
281 } tape_search_request_t;
283 /* A GThread that runs taper_scan. */
284 static gpointer tape_search_thread(gpointer data) {
285 tape_search_request_t * request = data;
287 if (request->state->next_tape_label != NULL &&
288 request->state->next_tape_device != NULL) {
289 return GINT_TO_POINTER(TRUE);
291 amfree(request->state->next_tape_label);
292 amfree(request->state->next_tape_device);
295 return GINT_TO_POINTER
296 (simple_taper_scan(request->state,
298 &(request->errmsg)));
301 static void log_taper_scan_errmsg(char * errmsg) {
310 log_add(L_WARNING,"%s", c1);
316 log_add(L_WARNING,"%s", c1);
320 /* If handle is NULL, then this function assumes that we are in startup mode.
321 * In that case it will wait for a command from driver. If handle is not NULL,
322 * this this function will ask for permission with REQUEST-NEW-TAPE. */
323 static gboolean find_new_tape(taper_state_t * state, dump_info_t * dump) {
324 GThread * tape_search = NULL;
325 tape_search_request_t search_request;
326 gboolean use_threads;
330 if (state->device != NULL) {
334 /* We save the value here in case it changes while we're running. */
335 use_threads = g_thread_supported();
337 search_request.state = state;
338 search_request.prolong = TRUE;
339 search_request.errmsg = NULL;
341 tape_search = g_thread_create(tape_search_thread,
342 &search_request, TRUE, NULL);
345 putresult(REQUEST_NEW_TAPE, "%s\n", dump->handle);
349 g_fprintf(stderr, "taper: Got odd message from driver, expected NEW-TAPE or NO-NEW-TAPE.\n");
352 gboolean search_result;
354 search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
357 GPOINTER_TO_INT(tape_search_thread(&search_request));
360 /* We don't say NEW_TAPE until we actually write the label. */
361 amfree(search_request.errmsg);
364 putresult(NO_NEW_TAPE, "%s\n", dump->handle);
365 log_taper_scan_errmsg(search_request.errmsg);
370 search_request.prolong = FALSE;
372 g_thread_join(tape_search);
378 /* Returns TRUE if the old volume details are not the same as the new ones. */
379 static gboolean check_volume_changed(Device * device,
380 char * old_label, char * old_timestamp) {
381 /* If one is NULL and the other is not, something changed. */
382 if ((old_label == NULL) != (device->volume_label == NULL))
384 if ((old_timestamp == NULL) != (device->volume_time == NULL))
386 /* If details were not NULL and is now different, we have a difference. */
387 if (old_label != NULL && strcmp(old_label, device->volume_label) != 0)
389 if (old_timestamp != NULL &&
390 strcmp(old_timestamp, device->volume_time) != 0)
393 /* If we got here, everything is cool. */
399 taper_state_t *state)
401 char *tapelist_name = NULL;
402 char *tapelist_name_old = NULL;
404 tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
405 if (state->cur_tape == 0) {
406 tapelist_name_old = stralloc2(tapelist_name, ".yesterday");
408 char cur_str[NUM_STR_SIZE];
409 g_snprintf(cur_str, SIZEOF(cur_str), "%d", state->cur_tape - 1);
410 tapelist_name_old = vstralloc(tapelist_name,
411 ".today.", cur_str, NULL);
414 if (write_tapelist(tapelist_name_old)) {
415 error("could not write tapelist: %s", strerror(errno));
418 amfree(tapelist_name_old);
420 remove_tapelabel(state->device->volume_label);
421 add_tapelabel(state->driver_start_time,
422 state->device->volume_label);
423 if (write_tapelist(tapelist_name)) {
424 error("could not write tapelist: %s", strerror(errno));
427 amfree(tapelist_name);
430 /* Find and label a new tape, if one is not already open. Returns TRUE
431 * if a tape could be written. */
432 static gboolean find_and_label_new_tape(taper_state_t * state,
433 dump_info_t * dump_info) {
434 if (state->device != NULL) {
438 if (!find_new_tape(state, dump_info)) {
442 return label_new_tape(state, dump_info);
445 static gboolean label_new_tape(taper_state_t * state, dump_info_t * dump_info) {
446 char *old_volume_name = NULL;
447 char *old_volume_time = NULL;
448 tape_search_request_t request;
449 gboolean search_result;
450 ReadLabelStatusFlags status;
452 /* If we got here, it means that we have found a tape to label and
453 * have gotten permission from the driver to write it. But we
454 * still can say NO-NEW-TAPE if a problem shows up, and must still
455 * say NEW-TAPE if one doesn't. */
457 state->device = device_open(state->next_tape_device);
458 amfree(state->next_tape_device);
459 if (state->device == NULL)
462 device_set_startup_properties_from_config(state->device);
464 /* if we have an error, and are sure it isn't just an unlabeled volume,
465 * then skip this volume */
466 status = device_read_label(state->device);
467 if ((status & ~READ_LABEL_STATUS_VOLUME_UNLABELED) &&
468 !(status & READ_LABEL_STATUS_VOLUME_UNLABELED))
471 old_volume_name = g_strdup(state->device->volume_label);
472 old_volume_time = g_strdup(state->device->volume_time);
474 if (!device_start(state->device, ACCESS_WRITE, state->next_tape_label,
475 state->driver_start_time)) {
478 /* Something broke, see if we can tell if the volume was erased or
480 g_fprintf(stderr, "taper: Error writing label %s to device %s.\n",
481 state->next_tape_label, state->device->device_name);
483 if (!device_finish(state->device))
484 goto request_new_volume;
486 /* This time, if we can't read the label, assume we've overwritten
487 * the volume or otherwise corrupted it */
488 status = device_read_label(state->device);
489 if ((status & ~READ_LABEL_STATUS_VOLUME_UNLABELED) &&
490 !(status & READ_LABEL_STATUS_VOLUME_UNLABELED))
491 goto request_new_volume;
493 tape_used = check_volume_changed(state->device, old_volume_name,
496 goto request_new_volume;
502 amfree(old_volume_name);
503 amfree(old_volume_time);
504 amfree(state->next_tape_label);
506 update_tapelist(state);
509 log_add(L_START, "datestamp %s label %s tape %d",
510 state->driver_start_time, state->device->volume_label,
512 putresult(NEW_TAPE, "%s %s\n", dump_info->handle,
513 state->device->volume_label);
518 /* Tell the driver we overwrote this volume, even if it was empty, and request
521 g_object_unref(state->device);
522 state->device = NULL;
525 putresult(NEW_TAPE, "%s %s\n", dump_info->handle,
526 state->next_tape_label);
527 if (old_volume_name) {
528 log_add(L_WARNING, "Problem writing label '%s' to volume %s, "
529 "volume may be erased.\n",
530 state->next_tape_label, old_volume_name);
532 log_add(L_WARNING, "Problem writing label '%s' to new volume, "
533 "volume may be erased.\n", state->next_tape_label);
536 amfree(state->next_tape_label);
537 amfree(old_volume_name);
538 amfree(old_volume_time);
540 return find_and_label_new_tape(state, dump_info);
543 /* grab a new volume without talking to the driver again -- we do this if we're
544 * confident we didn't overwrite the last tape we got. */
546 g_object_unref(state->device);
547 state->device = NULL;
550 if (old_volume_name) {
551 log_add(L_WARNING, "Problem writing label '%s' to volume '%s', "
552 "old volume data intact\n",
553 state->next_tape_label, old_volume_name);
555 log_add(L_WARNING, "Problem writing label '%s' to new volume, "
556 "old volume data intact\n", state->next_tape_label);
559 amfree(state->next_tape_label);
560 amfree(old_volume_name);
561 amfree(old_volume_time);
563 request.state = state;
564 request.prolong = TRUE;
565 request.errmsg = NULL;
566 search_result = GPOINTER_TO_INT(tape_search_thread(&request));
568 amfree(request.errmsg);
569 return label_new_tape(state, dump_info);
571 /* Problem finding a new tape! */
572 log_taper_scan_errmsg(request.errmsg);
573 putresult(NO_NEW_TAPE, "%s\n", dump_info->handle);
578 /* Find out if the dump is PARTIAL or not, and set the proper driver
579 and logfile tags for the dump. */
580 static void find_completion_tags(dump_info_t * dump_info, /* IN */
581 cmd_t * result_cmd, /* OUT */
582 logtype_t * result_log /* OUT */) {
583 if (taper_source_is_partial(dump_info->source)) {
584 *result_cmd = PARTIAL;
585 *result_log = L_PARTIAL;
588 *result_log = L_DONE;
592 /* Put an L_PARTIAL message to the logfile. */
593 static void put_partial_log(dump_info_t * dump_info, double dump_time,
594 guint64 dump_kbytes) {
595 char * qdiskname = quote_string(dump_info->diskname);
597 log_add(L_PARTIAL, "%s %s %s %d %d [sec %f kb %ju kps %f] \"\"",
598 dump_info->hostname, qdiskname, dump_info->timestamp,
599 dump_info->current_part, dump_info->level, dump_time,
600 (uintmax_t)dump_kbytes, dump_kbytes / dump_time);
604 /* Figure out what to do after a part attempt. Returns TRUE if another
605 attempt should proceed for this dump; FALSE if we are done. */
606 static gboolean finish_part_attempt(taper_state_t * taper_state,
607 dump_info_t * dump_info,
608 queue_result_flags queue_result,
609 GTimeVal run_time, guint64 run_bytes) {
610 double part_time = g_timeval_to_double(run_time);
611 guint64 part_kbytes = run_bytes / 1024;
612 double part_kbps = run_bytes / (1024 * part_time);
614 char * qdiskname = quote_string(dump_info->diskname);
616 if (queue_result == QUEUE_SUCCESS) {
617 dump_info->total_time = timesadd(run_time, dump_info->total_time);
618 dump_info->total_bytes += run_bytes;
620 log_add(L_PART, "%s %d %s %s %s %d/%d %d [sec %f kb %ju kps %f]",
621 taper_state->device->volume_label,
622 taper_state->device->file, dump_info->hostname, qdiskname,
623 dump_info->timestamp, dump_info->current_part,
624 taper_source_predict_parts(dump_info->source),
625 dump_info->level, part_time, (uintmax_t)part_kbytes, part_kbps);
626 putresult(PARTDONE, "%s %s %d %ju \"[sec %f kb %ju kps %f]\"\n",
627 dump_info->handle, taper_state->device->volume_label,
628 taper_state->device->file, (uintmax_t)part_kbytes, part_time,
629 (uintmax_t)part_kbytes, part_kbps);
631 if (taper_source_get_end_of_data(dump_info->source)) {
633 logtype_t result_log;
634 double dump_time = g_timeval_to_double(dump_info->total_time);
635 guint64 dump_kbytes = dump_info->total_bytes / 1024;
636 double dump_kbps = dump_info->total_bytes / (1024 * dump_time);
638 find_completion_tags(dump_info, &result_cmd, &result_log);
640 g_object_unref(dump_info->source);
641 dump_info->source = NULL;
643 log_add(result_log, "%s %s %s %d %d [sec %f kb %ju kps %f]",
644 dump_info->hostname, qdiskname, dump_info->timestamp,
645 dump_info->current_part, dump_info->level, dump_time,
646 (uintmax_t)dump_kbytes, dump_kbps);
647 putresult(result_cmd, "%s INPUT-GOOD TAPE-GOOD "
648 "\"[sec %f kb %ju kps %f]\" \"\" \"\"\n",
649 dump_info->handle, dump_time, (uintmax_t)dump_kbytes,
654 } else if (taper_source_get_end_of_part(dump_info->source)) {
655 taper_source_start_new_part(dump_info->source);
656 dump_info->current_part ++;
660 /* If we didn't read EOF or EOP, then an error
661 occured. But we read QUEUE_SUCCESS, so something is
663 g_assert_not_reached();
665 char * volume_label = strdup(taper_state->device->volume_label);
666 int file_number = taper_state->device->file;
667 double dump_time, dump_kbps;
670 /* A problem occured. */
671 if (queue_result & QUEUE_CONSUMER_ERROR) {
672 /* Close the device. */
673 device_finish(taper_state->device);
674 g_object_unref(taper_state->device);
675 taper_state->device = NULL;
678 log_add(L_PARTPARTIAL,
679 "%s %d %s %s %s %d/%d %d [sec %f kb %ju kps %f] \"\"",
680 volume_label, file_number, dump_info->hostname, qdiskname,
681 dump_info->timestamp, dump_info->current_part,
682 taper_source_predict_parts(dump_info->source),
683 dump_info->level, part_time, (uintmax_t)part_kbytes, part_kbps);
684 amfree(volume_label);
686 if ((queue_result & QUEUE_CONSUMER_ERROR) &&
687 (!(queue_result & QUEUE_PRODUCER_ERROR)) &&
688 taper_source_seek_to_part_start(dump_info->source)) {
689 /* It is recoverable. */
690 log_add(L_INFO, "Will request retry of failed split part.");
691 if (find_and_label_new_tape(taper_state, dump_info)) {
692 /* dump_info->current_part is unchanged. */
698 dump_info->total_time = timesadd(run_time, dump_info->total_time);
699 dump_info->total_bytes += run_bytes;
700 dump_time = g_timeval_to_double(dump_info->total_time);
701 dump_kbytes = dump_info->total_bytes / 1024;
702 dump_kbps = dump_info->total_bytes / (1024 * dump_time);
705 "%s INPUT-%s TAPE-%s "
706 "\"[sec %f kb %ju kps %f]\" \"\" \"\"\n",
708 (queue_result & QUEUE_PRODUCER_ERROR) ? "ERROR" : "GOOD",
709 (queue_result & QUEUE_CONSUMER_ERROR) ? "ERROR" : "GOOD",
710 dump_time, (uintmax_t)dump_kbytes, dump_kbps);
711 put_partial_log(dump_info, dump_time, dump_kbytes);
718 /* Generate the actual header structure to write to tape. This means dropping
719 * bits related to the holding disk, and adding bits for split dumps. */
720 static dumpfile_t * munge_headers(dump_info_t * dump_info) {
724 rval = taper_source_get_first_header(dump_info->source);
730 rval->cont_filename[0] = '\0';
732 expected_splits = taper_source_predict_parts(dump_info->source);
734 if (expected_splits != 1) {
735 rval->type = F_SPLIT_DUMPFILE;
736 rval->partnum = dump_info->current_part;
737 rval->totalparts = expected_splits;
743 /* We call this when we can't find a tape to write data to. This could
744 happen with the first (or only) part of a file, but it could also
745 happen with an intermediate part of a split dump. dump_bytes
746 is 0 if this is the first part of a dump. */
747 static void bail_no_volume(dump_info_t * dump_info) {
748 if (dump_info->total_bytes > 0) {
749 /* Second or later part of a split dump, so PARTIAL message. */
750 double dump_time = g_timeval_to_double(dump_info->total_time);
751 guint64 dump_kbytes = dump_info->total_bytes / 1024;
752 double dump_kbps = dump_kbytes / dump_time;
754 "%s INPUT-GOOD TAPE-ERROR "
755 "\"[sec %f kb %ju kps %f]\" \"\" \"no new tape\"\n",
757 dump_time, (uintmax_t)dump_kbytes, dump_kbps);
758 put_partial_log(dump_info, dump_time, dump_kbytes);
760 char * qdiskname = quote_string(dump_info->diskname);
762 "%s INPUT-GOOD TAPE-ERROR \"\" \"No new tape.\"\n",
764 log_add(L_FAIL, "%s %s %s %d \"No new tape.\"",
765 dump_info->hostname, qdiskname, dump_info->timestamp,
771 /* Link up the TaperSource with the Device, including retries etc. */
772 static void run_device_output(taper_state_t * taper_state,
773 dump_info_t * dump_info) {
776 dump_info->current_part = 1;
777 dump_info->total_time.tv_sec = 0;
778 dump_info->total_time.tv_usec = 0;
779 dump_info->total_bytes = 0;
782 GTimeVal start_time, end_time, run_time;
783 StreamingRequirement streaming_mode;
784 queue_result_flags queue_result;
785 CountingConsumerData consumer_data;
786 dumpfile_t *this_header;
789 this_header = munge_headers(dump_info);
790 if (this_header == NULL) {
791 char * qdiskname = quote_string(dump_info->diskname);
793 "%s INPUT-ERROR TAPE-GOOD \"Failed reading dump header.\" \"\"\n",
795 log_add(L_FAIL, "%s %s %s %d \"Failed reading dump header.\"",
796 dump_info->hostname, qdiskname, dump_info->timestamp,
802 if (!find_and_label_new_tape(taper_state, dump_info)) {
803 bail_no_volume(dump_info);
808 if (!device_start_file(taper_state->device, this_header)) {
809 bail_no_volume(dump_info);
815 bzero(&val, sizeof(val));
816 if (!device_property_get(taper_state->device, PROPERTY_STREAMING, &val)
817 || !G_VALUE_HOLDS(&val, STREAMING_REQUIREMENT_TYPE)) {
818 g_fprintf(stderr, "taper: Couldn't get streaming type!\n");
819 streaming_mode = STREAMING_REQUIREMENT_REQUIRED;
821 streaming_mode = g_value_get_enum(&val);
824 file_number = taper_state->device->file;
826 consumer_data.next_consumer = device_write_consumer;
827 consumer_data.next_consumer_data = taper_state->device;
828 consumer_data.bytes_written = 0;
830 g_get_current_time(&start_time);
832 if (getconf_seen(CNF_DEVICE_OUTPUT_BUFFER_SIZE)) {
833 max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE);
834 if (getconf_seen(CNF_TAPEBUFS)) {
836 "Configuration directives 'device_output_buffer_size' "
838 "'tapebufs' are incompatible; using former.\n");
840 } else if (getconf_seen(CNF_TAPEBUFS)) {
841 max_memory = getconf_int(CNF_TAPEBUFS) *
842 device_write_max_size(taper_state->device);
845 max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE);
848 queue_result = do_consumer_producer_queue_full
849 (taper_source_producer,
853 device_write_max_size(taper_state->device), max_memory,
856 g_get_current_time(&end_time);
857 run_time = timesub(end_time, start_time);
859 /* The device_write_consumer may have closed the file with a short
860 * write, so we only finish here if it needs it. */
861 if (taper_state->device->in_file &&
862 !device_finish_file(taper_state->device)) {
863 queue_result = queue_result | QUEUE_CONSUMER_ERROR;
866 if (!finish_part_attempt(taper_state, dump_info, queue_result,
867 run_time, consumer_data.bytes_written)) {
873 /* Handle a PORT_WRITE command. */
874 static void process_port_write(taper_state_t * state,
875 struct cmdargs * cmdargs) {
876 dump_info_t dump_state;
878 guint64 fallback_splitsize;
879 char * split_diskbuffer;
880 char * argnames[] = {"command", /* 1 */
887 "split_diskbuffer", /* 8 */
888 "fallback_splitsize", /* 9 */
891 validate_args(PORT_WRITE, cmdargs, argnames);
893 dump_state.handle = g_strdup(cmdargs->argv[2]);
894 dump_state.hostname = g_strdup(cmdargs->argv[3]);
895 dump_state.diskname = unquote_string(cmdargs->argv[4]);
898 dump_state.level = strtol(cmdargs->argv[5], NULL, 10);
900 error("error [taper PORT-WRITE: Invalid dump level %s]",
902 g_assert_not_reached();
905 dump_state.timestamp = strdup(cmdargs->argv[6]);
908 splitsize = g_ascii_strtoull(cmdargs->argv[7], NULL, 10);
910 error("error [taper PORT-WRITE: Invalid splitsize %s]",
912 g_assert_not_reached();
915 if (strcmp(cmdargs->argv[8], "NULL") == 0) {
916 split_diskbuffer = NULL;
918 split_diskbuffer = g_strdup(cmdargs->argv[8]);
922 fallback_splitsize = g_ascii_strtoull(cmdargs->argv[9], NULL, 10);
924 error("error [taper PORT-WRITE: Invalid fallback_splitsize %s]",
926 g_assert_not_reached();
929 dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
933 if (!open_read_socket(&dump_state, split_diskbuffer, splitsize,
934 fallback_splitsize)) {
935 free(split_diskbuffer);
938 free(split_diskbuffer);
940 run_device_output(state, &dump_state);
942 free_dump_info(&dump_state);
945 /* Handle a FILE_WRITE command. */
946 static void process_file_write(taper_state_t * state,
947 struct cmdargs * cmdargs) {
948 dump_info_t dump_state;
949 char * holding_disk_file;
951 char * argnames[] = {"command", /* 1 */
961 validate_args(FILE_WRITE, cmdargs, argnames);
963 dump_state.handle = g_strdup(cmdargs->argv[2]);
964 holding_disk_file = unquote_string(cmdargs->argv[3]);
965 dump_state.hostname = g_strdup(cmdargs->argv[4]);
966 dump_state.diskname = unquote_string(cmdargs->argv[5]);
969 dump_state.level = strtol(cmdargs->argv[6], NULL, 10);
971 error("error [taper FILE-WRITE: Invalid dump level %s]",
973 g_assert_not_reached();
976 dump_state.timestamp = strdup(cmdargs->argv[7]);
979 splitsize = g_ascii_strtoull(cmdargs->argv[8], NULL, 10);
981 error("error [taper FILE-WRITE: Invalid splitsize %s]",
983 g_assert_not_reached();
986 dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
990 dump_state.source = taper_source_new(dump_state.handle, FILE_WRITE,
991 holding_disk_file, -1,
992 NULL, splitsize, -1);
993 /* FIXME: This should be handled properly. */
994 g_assert(dump_state.source != NULL);
996 run_device_output(state, &dump_state);
998 free_dump_info(&dump_state);
999 amfree(holding_disk_file);
1002 /* Send QUITTING message to driver and associated logging. Always
1004 static gboolean send_quitting(taper_state_t * state) {
1005 putresult(QUITTING, "\n");
1006 g_fprintf(stderr,"taper: DONE\n");
1011 /* This function recieves the START_TAPER command from driver, and
1012 returns the attached timestamp. */
1013 static gboolean find_first_tape(taper_state_t * state) {
1015 /* Note: cmdargs.argv is never freed. In the entire Amanda codebase. */
1016 struct cmdargs cmdargs;
1017 tape_search_request_t search_request;
1018 GThread * tape_search = NULL;
1019 gboolean use_threads;
1021 /* We save the value here in case it changes while we're running. */
1022 use_threads = g_thread_supported();
1024 search_request.state = state;
1025 search_request.prolong = TRUE;
1026 search_request.errmsg = NULL;
1029 tape_search = g_thread_create(tape_search_thread,
1030 &search_request, TRUE, NULL);
1033 cmd = getcmd(&cmdargs);
1037 gboolean search_result;
1038 state->driver_start_time = strdup(cmdargs.argv[2]);
1040 search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
1043 GPOINTER_TO_INT(tape_search_thread(&search_request));
1045 if (search_result) {
1046 putresult(TAPER_OK, "\n");
1048 putresult(TAPE_ERROR, "Could not find a tape to use.\n");
1049 log_add(L_ERROR, "no-tape [%s]", "Could not find a tape to use");
1050 if (search_request.errmsg != NULL) {
1052 c = c1 = search_request.errmsg;
1053 while (*c != '\0') {
1056 log_add(L_WARNING,"%s", c1);
1061 if (strlen(c1) > 1 )
1062 log_add(L_WARNING,"%s", c1);
1065 amfree(search_request.errmsg);
1069 search_request.prolong = FALSE;
1071 g_thread_join(tape_search);
1073 return send_quitting(state);
1075 error("error [file_reader_side cmd %d argc %d]", cmd, cmdargs.argc);
1078 g_assert_not_reached();
1081 /* In running mode (not startup mode), get a command from driver and
1083 static gboolean process_driver_command(taper_state_t * state) {
1085 struct cmdargs cmdargs;
1088 /* This will return QUIT if driver has died. */
1089 cmd = getcmd(&cmdargs);
1103 process_port_write(state, &cmdargs);
1118 process_file_write(state, &cmdargs);
1122 return send_quitting(state);
1124 if (cmdargs.argc >= 1) {
1125 q = squote(cmdargs.argv[1]);
1126 } else if (cmdargs.argc >= 0) {
1127 q = squote(cmdargs.argv[0]);
1129 q = stralloc("(no input?)");
1131 putresult(BAD_COMMAND, "%s\n", q);
1139 int main(int argc, char ** argv) {
1140 char * tapelist_name;
1142 taper_state_t state;
1143 config_overwrites_t *cfg_ovr = NULL;
1144 char *cfg_opt = NULL;
1147 * Configure program for internationalization:
1148 * 1) Only set the message locale for now.
1149 * 2) Set textdomain for all amanda related programs to "amanda"
1150 * We don't want to be forced to support dozens of message catalogs.
1152 setlocale(LC_MESSAGES, "C");
1153 textdomain("amanda");
1161 init_taper_state(&state);
1163 /* Don't die when child closes pipe */
1164 signal(SIGPIPE, SIG_IGN);
1166 g_fprintf(stderr, _("%s: pid %ld executable %s version %s\n"),
1167 get_pname(), (long) getpid(), argv[0], version());
1168 dbprintf(_("%s: pid %ld executable %s version %s\n"),
1169 get_pname(), (long) getpid(), argv[0], version());
1171 /* Process options */
1173 cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
1176 error("Too many arguments!\n");
1177 g_assert_not_reached();
1181 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD | CONFIG_INIT_FATAL,
1183 apply_config_overwrites(cfg_ovr);
1187 set_logerror(logerror);
1189 check_running_as(RUNNING_AS_DUMPUSER);
1191 dbrename(config_name, DBG_SUBDIR_SERVER);
1193 tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
1195 if (read_tapelist(tapelist_name) != 0) {
1196 error("could not load tapelist \"%s\"", tapelist_name);
1197 g_assert_not_reached();
1199 amfree(tapelist_name);
1201 have_changer = changer_init();
1202 if (have_changer < 0) {
1203 error("changer initialization failed: %s", strerror(errno));
1204 g_assert_not_reached();
1207 state.next_tape_label = NULL;
1208 state.next_tape_device = NULL;
1211 if (!find_first_tape(&state)) {
1212 return EXIT_SUCCESS;
1215 while (process_driver_command(&state));
1216 return EXIT_SUCCESS;