4b54430c2ee4add5766afc24ae42360d268eebba
[debian/amanda] / server-src / taper.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /* $Id: taper.c 6512 2007-05-24 17:00:24Z ian $
27  *
28  * moves files from holding disk to tape, or from a socket to tape
29  */
30
31 /* FIXME: This file needs to use gettext. */
32
33 #include <glib.h>
34 #include "physmem.h"
35
36 #include "changer.h"
37 #include "clock.h"
38 #include "conffile.h"
39 #include "device.h"
40 #include "logfile.h"
41 #include "server_util.h"
42 #include "stream.h"
43 #include "tapefile.h"
44 #include "taperscan.h"
45 #include "taper-source.h"
46 #include "timestamp.h"
47 #include "token.h"
48 #include "version.h"
49
50 /* FIXME: This should not be here. */
51 #define CONNECT_TIMEOUT (2*60)
52
53 /* Use this instead of global variables, so that we are reentrant. */
54 typedef struct {
55     Device * device;
56     char * driver_start_time;
57     int    cur_tape;
58     char * next_tape_label;
59     char * next_tape_device;
60     taper_scan_tracker_t * taper_scan_tracker;
61 } taper_state_t;
62
63 typedef struct {
64     char * handle;
65     char * hostname;
66     char * diskname;
67     int level;
68     char * timestamp;
69     char * id_string;
70     TaperSource * source;
71     int current_part;
72     GTimeVal total_time;
73     guint64 total_bytes;
74 } dump_info_t;
75
76 static gboolean label_new_tape(taper_state_t * state, dump_info_t * dump_info);
77
78 static void init_taper_state(taper_state_t* state) {
79     state->device = NULL;
80     state->driver_start_time = NULL;
81     state->taper_scan_tracker = taper_scan_tracker_new();
82 }
83
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);
91         state->device = NULL;
92     }
93 }
94
95 static void free_dump_info(dump_info_t * info) {
96     amfree(info->handle);
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);
103         info->source = NULL;
104     }
105 }
106
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,
111                           char ** argnames) {
112     int i;
113     
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]);
118         }
119     }
120     if (i < args->argc) {
121         error("error [taper %s: Too many args: Got %d, expected %d.]",
122               cmdstr[cmd], args->argc, i);
123     }
124 }
125
126 /* Open a socket to the dumper. Returns TRUE if everything is happy, FALSE
127    otherwise. */
128 static gboolean open_read_socket(dump_info_t * info, char * split_diskbuffer,
129                              guint64 splitsize, guint64 fallback_splitsize) {
130     in_port_t port = 0;
131     int socket;
132     int fd;
133     int result;
134     struct addrinfo *res;
135
136     if ((result = resolve_hostname("localhost", 0, &res, NULL) != 0)) {
137         char *m;
138         char *q;
139         int save_errno = errno;
140         char *qdiskname = quote_string(info->diskname);
141
142         m = vstralloc("[localhost resolve failure: ",
143                       strerror(save_errno),
144                       "]",
145                       NULL);
146         q = squote(m);
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,
150                 info->level, q);
151         amfree(qdiskname);
152         amfree(m);
153         amfree(q);
154         return FALSE;
155     }
156
157     socket = stream_server(res->ai_family, &port, 0, STREAM_BUFSIZE, 0);
158     freeaddrinfo(res);
159
160     if (socket < 0) {
161         char *m;
162         char *q;
163         int save_errno = errno;
164         char *qdiskname = quote_string(info->diskname);
165
166         m = vstralloc("[port create failure: ",
167                       strerror(save_errno),
168                       "]",
169                       NULL);
170         q = squote(m);
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,
174                 info->level, q);
175         amfree(qdiskname);
176         amfree(m);
177         amfree(q);
178         return FALSE;
179     }
180
181     putresult(PORT, "%d\n", port);
182
183     fd = stream_accept(socket, CONNECT_TIMEOUT, 0, STREAM_BUFSIZE);
184
185     if (fd < 0) {
186         char *m, *q;
187         int save_errno = errno;
188         char *qdiskname = quote_string(info->diskname);
189         m = vstralloc("[port connect failure: ",
190                       strerror(save_errno),
191                       "]",
192                       NULL);
193         q = squote(m);
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,
197                 info->level, q);
198         amfree(qdiskname);
199         aclose(socket);
200         amfree(m);
201         amfree(q);
202         return FALSE;
203     } else {
204         aclose(socket);
205     }
206
207     info->source = taper_source_new(info->handle, PORT_WRITE, NULL, fd,
208                                     split_diskbuffer, splitsize,
209                                     fallback_splitsize);
210     /* FIXME: This should be handled properly. */
211     g_assert(info->source != NULL);
212     return TRUE;
213 }
214
215 typedef struct {
216     ConsumerFunctor next_consumer;
217     gpointer next_consumer_data;
218     guint64 bytes_written;
219 } CountingConsumerData;
220
221 /* A ConsumerFunctor. This consumer just passes its arguments on to a
222    second consumer, but counts the number of bytes successfully
223    written. */
224 static int counting_consumer(gpointer user_data, queue_buffer_t * buffer) {
225     int result;
226     CountingConsumerData * data = user_data;
227
228     result = data->next_consumer(data->next_consumer_data, buffer);
229     
230     if (result > 0) {
231         data->bytes_written += result;
232     }
233
234     return result;
235 }
236
237 static gboolean boolean_prolong(void * data) {
238     if (data == NULL) {
239         return TRUE; /* Do not interrupt. */
240     } else {
241         return *(gboolean*)data;
242     }
243 }
244
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;
251     int result;
252     result = taper_scan(NULL, label, &timestamp, 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"));
258         return FALSE;
259     } else if (result < 0) {
260         g_fprintf(stderr, _("Failed taper scan: %s\n"), (*error_message)?(*error_message):_("(no error message)"));
261         amfree(timestamp);
262         return FALSE;
263     } else {
264         g_fprintf(stderr, _("taper: using label `%s' date `%s'\n"), *label,
265                 state->driver_start_time);
266         if (result == 3) {
267             log_add(L_INFO,
268             _("Will write new label `%s' to new (previously non-amanda) tape"),
269                     *label);
270         }
271
272     }
273     amfree(timestamp);
274     return TRUE;
275 }
276
277 typedef struct {
278     taper_state_t * state;
279     gboolean prolong; /* scan stops when this is FALSE. */
280     char *errmsg;
281 } tape_search_request_t;
282
283 /* A GThread that runs taper_scan. */
284 static gpointer tape_search_thread(gpointer data) {
285     tape_search_request_t * request = data;
286
287     if (request->state->next_tape_label != NULL &&
288         request->state->next_tape_device != NULL) {
289         return GINT_TO_POINTER(TRUE);
290     } else {
291         amfree(request->state->next_tape_label);
292         amfree(request->state->next_tape_device);
293     }
294
295     return GINT_TO_POINTER
296         (simple_taper_scan(request->state,
297                            &(request->prolong),
298                            &(request->errmsg)));
299 }
300
301 static void log_taper_scan_errmsg(char * errmsg) {
302     char *c, *c1;
303     if (errmsg == NULL)
304         return;
305
306     c = c1 = errmsg;
307     while (*c != '\0') {
308         if (*c == '\n') {
309             *c = '\0';
310             log_add(L_WARNING,"%s", c1);
311             c1 = c+1;
312         }
313         c++;
314     }
315     if (strlen(c1) > 1 )
316         log_add(L_WARNING,"%s", c1);
317     amfree(errmsg);
318 }
319
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;
327     cmd_t cmd;
328     struct cmdargs args;
329
330     if (state->device != NULL) {
331         return TRUE;
332     }
333
334     /* We save the value here in case it changes while we're running. */
335     use_threads = g_thread_supported();
336
337     search_request.state = state;
338     search_request.prolong = TRUE;
339     search_request.errmsg = NULL;
340     if (use_threads) {
341         tape_search = g_thread_create(tape_search_thread,
342                                       &search_request, TRUE, NULL);
343     }
344     
345     putresult(REQUEST_NEW_TAPE, "%s\n", dump->handle);
346     cmd = getcmd(&args);
347     switch (cmd) {
348     default:
349         g_fprintf(stderr, "taper: Got odd message from driver, expected NEW-TAPE or NO-NEW-TAPE.\n");
350         /* FALLTHROUGH. */
351     case NEW_TAPE: {
352         gboolean search_result;
353         if (use_threads) {
354             search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
355         } else {
356             search_result =
357                 GPOINTER_TO_INT(tape_search_thread(&search_request));
358         }
359         if (search_result) {
360             /* We don't say NEW_TAPE until we actually write the label. */
361             amfree(search_request.errmsg);
362             return TRUE;
363         } else {
364             putresult(NO_NEW_TAPE, "%s\n", dump->handle);
365             log_taper_scan_errmsg(search_request.errmsg);
366             return FALSE;
367         }
368     }
369     case NO_NEW_TAPE:
370         search_request.prolong = FALSE;
371         if (use_threads) {
372             g_thread_join(tape_search);
373         }
374         return FALSE;
375     }
376 }
377
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))
383         return TRUE;
384     if ((old_timestamp == NULL) != (device->volume_time == NULL))
385         return TRUE;
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)
388         return TRUE;
389     if (old_timestamp != NULL &&
390         strcmp(old_timestamp, device->volume_time) != 0)
391         return TRUE;
392
393     /* If we got here, everything is cool. */
394     return FALSE;
395 }
396
397 /* Find and label a new tape, if one is not already open. Returns TRUE
398  * if a tape could be written. */
399 static gboolean find_and_label_new_tape(taper_state_t * state,
400                                         dump_info_t * dump_info) {
401     if (state->device != NULL) {
402         return TRUE;
403     }
404     
405     if (!find_new_tape(state, dump_info)) {
406         return FALSE;
407     }
408
409     return label_new_tape(state, dump_info);
410 }
411
412 static gboolean label_new_tape(taper_state_t * state, dump_info_t * dump_info) {
413     char *tapelist_name;
414     char *tapelist_name_old;
415     char * old_volume_name;
416     char * old_volume_time;
417
418     /* If we got here, it means that we have found a tape to label and
419      * have gotten permission from the driver to write it. But we
420      * still can say NO-NEW-TAPE if a problem shows up, and must still
421      * say NEW-TAPE if one doesn't. */
422
423     state->device = device_open(state->next_tape_device);
424     amfree(state->next_tape_device);
425     if (state->device == NULL) {
426         amfree(state->next_tape_label);
427         return FALSE;
428     }
429     
430     device_set_startup_properties_from_config(state->device);
431     device_read_label(state->device);
432     old_volume_name = g_strdup(state->device->volume_label);
433     old_volume_time = g_strdup(state->device->volume_time);
434     
435     if (!device_start(state->device, ACCESS_WRITE, state->next_tape_label,
436                       state->driver_start_time)) {
437         gboolean tape_used;
438         /* Something broke, see if we can tell if the volume was erased or
439          * not. */
440         g_fprintf(stderr, "taper: Error writing label %s to device %s.\n",
441                 state->next_tape_label, state->device->device_name);
442         device_finish(state->device);
443         device_read_label(state->device);
444         tape_used = check_volume_changed(state->device, old_volume_name, 
445                                          old_volume_time);
446         g_object_unref(state->device);
447         state->device = NULL;
448         amfree(state->next_tape_label);
449         /* If the volume was written, we tell the driver and then immediately
450          * try again. */
451         if (tape_used) {
452             putresult(NEW_TAPE, "%s %s\n", dump_info->handle,
453                       state->device->volume_label);
454             if (old_volume_name) {
455                 log_add(L_WARNING, "Problem writing label to volume %s, "
456                         "volume may be erased.\n", old_volume_name);
457             } else {
458                 log_add(L_WARNING, "Problem writing label %s to new volume, "
459                         "volume may be erased.\n", state->next_tape_label);
460             }
461             amfree(old_volume_name);
462             amfree(old_volume_time);
463             return find_and_label_new_tape(state, dump_info);
464         } else {
465             /* Otherwise, we grab a new tape without talking to the driver
466              * again. */
467             tape_search_request_t request;
468             gboolean search_result;
469             if (old_volume_name) {
470                 log_add(L_WARNING, "Problem writing label to volume %s, "
471                         "old volume data intact\n", old_volume_name);
472             } else {
473                 log_add(L_WARNING, "Problem writing label %s to new volume, "
474                         "old volume data intact\n", state->next_tape_label);
475             }
476             amfree(old_volume_name);
477             amfree(old_volume_time);
478             request.state = state;
479             request.prolong = TRUE;
480             request.errmsg = NULL;
481             search_result = GPOINTER_TO_INT(tape_search_thread(&request));
482             if (search_result) {
483                 amfree(request.errmsg);
484                 return label_new_tape(state, dump_info);
485             } else {
486                 /* Problem finding a new tape! */
487                 log_taper_scan_errmsg(request.errmsg);
488                 putresult(NO_NEW_TAPE, "%s\n", dump_info->handle);
489                 return FALSE;
490             }
491         }
492     } else {
493         amfree(old_volume_name);
494         amfree(old_volume_time);
495     }
496
497     amfree(state->next_tape_label);
498
499     tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
500     if (state->cur_tape == 0) {
501         tapelist_name_old = stralloc2(tapelist_name, ".yesterday");
502     } else {
503         char cur_str[NUM_STR_SIZE];
504         g_snprintf(cur_str, SIZEOF(cur_str), "%d", state->cur_tape - 1);
505         tapelist_name_old = vstralloc(tapelist_name,
506                                       ".today.", cur_str, NULL);
507     }
508
509     if (write_tapelist(tapelist_name_old)) {
510         error("could not write tapelist: %s", strerror(errno));
511         /*NOTREACHED*/
512     }
513     amfree(tapelist_name_old);
514
515     remove_tapelabel(state->device->volume_label);
516     add_tapelabel(state->driver_start_time,
517                   state->device->volume_label);
518     if (write_tapelist(tapelist_name)) {
519         error("could not write tapelist: %s", strerror(errno));
520         /*NOTREACHED*/
521     }
522     amfree(tapelist_name);
523     state->cur_tape++;
524
525     log_add(L_START, "datestamp %s label %s tape %d",
526             state->driver_start_time, state->device->volume_label,
527             state->cur_tape);
528     putresult(NEW_TAPE, "%s %s\n", dump_info->handle,
529               state->device->volume_label);
530
531     return TRUE;
532 }
533
534 /* Find out if the dump is PARTIAL or not, and set the proper driver
535    and logfile tags for the dump. */
536 static void find_completion_tags(dump_info_t * dump_info, /* IN */
537                                  cmd_t * result_cmd,      /* OUT */
538                                  logtype_t * result_log   /* OUT */) {
539     if (taper_source_is_partial(dump_info->source)) {
540         *result_cmd = PARTIAL;
541         *result_log = L_PARTIAL;
542     } else {
543         *result_cmd = DONE;
544         *result_log = L_DONE;
545     }
546 }
547
548 /* Put an L_PARTIAL message to the logfile. */
549 static void put_partial_log(dump_info_t * dump_info, double dump_time,
550                             guint64 dump_kbytes) {
551     char * qdiskname = quote_string(dump_info->diskname);
552
553     log_add(L_PARTIAL, "%s %s %s %d %d [sec %f kb %ju kps %f] \"\"",
554             dump_info->hostname, qdiskname, dump_info->timestamp,
555             dump_info->current_part, dump_info->level, dump_time,
556             (uintmax_t)dump_kbytes, dump_kbytes / dump_time);
557     amfree(qdiskname);
558 }
559
560 /* Figure out what to do after a part attempt. Returns TRUE if another
561    attempt should proceed for this dump; FALSE if we are done. */
562 static gboolean finish_part_attempt(taper_state_t * taper_state,
563                                     dump_info_t * dump_info,
564                                     queue_result_flags queue_result,
565                                     GTimeVal run_time, guint64 run_bytes) {
566     double part_time = g_timeval_to_double(run_time);
567     guint64 part_kbytes = run_bytes / 1024;
568     double part_kbps = run_bytes / (1024 * part_time);
569         
570     char * qdiskname = quote_string(dump_info->diskname);
571
572     if (queue_result == QUEUE_SUCCESS) {
573         dump_info->total_time = timesadd(run_time, dump_info->total_time);
574         dump_info->total_bytes += run_bytes;
575
576         log_add(L_PART, "%s %d %s %s %s %d/%d %d [sec %f kb %ju kps %f]",
577                 taper_state->device->volume_label,
578                 taper_state->device->file, dump_info->hostname, qdiskname,
579                 dump_info->timestamp, dump_info->current_part,
580                 taper_source_predict_parts(dump_info->source),
581                 dump_info->level, part_time, (uintmax_t)part_kbytes, part_kbps);
582         putresult(PARTDONE, "%s %s %d %ju \"[sec %f kb %ju kps %f]\"\n",
583                   dump_info->handle, taper_state->device->volume_label,
584                   taper_state->device->file, (uintmax_t)part_kbytes, part_time,
585                   (uintmax_t)part_kbytes, part_kbps);
586         
587         if (taper_source_get_end_of_data(dump_info->source)) {
588             cmd_t result_cmd;
589             logtype_t result_log;
590             double dump_time = g_timeval_to_double(dump_info->total_time);
591             guint64 dump_kbytes = dump_info->total_bytes / 1024;
592             double dump_kbps = dump_info->total_bytes / (1024 * dump_time);
593
594             find_completion_tags(dump_info, &result_cmd, &result_log);
595
596             g_object_unref(dump_info->source);
597             dump_info->source = NULL;
598         
599             log_add(result_log, "%s %s %s %d %d [sec %f kb %ju kps %f]",
600                     dump_info->hostname, qdiskname, dump_info->timestamp,
601                     dump_info->current_part, dump_info->level, dump_time,
602                     (uintmax_t)dump_kbytes, dump_kbps);
603             putresult(result_cmd, "%s INPUT-GOOD TAPE-GOOD "
604                       "\"[sec %f kb %ju kps %f]\" \"\" \"\"\n",
605                       dump_info->handle, dump_time, (uintmax_t)dump_kbytes,
606                       dump_kbps);
607             
608             amfree(qdiskname);
609             return FALSE;
610         } else if (taper_source_get_end_of_part(dump_info->source)) {
611             taper_source_start_new_part(dump_info->source);
612             dump_info->current_part ++;
613             amfree(qdiskname);
614             return TRUE;
615         }
616         /* If we didn't read EOF or EOP, then an error
617            occured. But we read QUEUE_SUCCESS, so something is
618            b0rked. */
619         g_assert_not_reached();
620     } else {
621         char * volume_label = strdup(taper_state->device->volume_label);
622         int file_number = taper_state->device->file;
623         double dump_time, dump_kbps;
624         guint64 dump_kbytes;
625
626         /* A problem occured. */
627         if (queue_result & QUEUE_CONSUMER_ERROR) {
628             /* Close the device. */
629             device_finish(taper_state->device);
630             g_object_unref(taper_state->device);
631             taper_state->device = NULL;
632         }
633         
634         log_add(L_PARTPARTIAL,
635                 "%s %d %s %s %s %d/%d %d [sec %f kb %ju kps %f] \"\"",
636                 volume_label, file_number, dump_info->hostname, qdiskname,
637                 dump_info->timestamp, dump_info->current_part,
638                 taper_source_predict_parts(dump_info->source),
639                 dump_info->level, part_time, (uintmax_t)part_kbytes, part_kbps);
640         amfree(volume_label);
641         
642         if ((queue_result & QUEUE_CONSUMER_ERROR) &&
643             (!(queue_result & QUEUE_PRODUCER_ERROR)) &&
644             taper_source_seek_to_part_start(dump_info->source)) {
645             /* It is recoverable. */
646             log_add(L_INFO, "Will request retry of failed split part.");
647             if (find_and_label_new_tape(taper_state, dump_info)) {
648                 /* dump_info->current_part is unchanged. */
649                 amfree(qdiskname);
650                 return TRUE;
651             }
652         }
653
654         dump_info->total_time = timesadd(run_time, dump_info->total_time);
655         dump_info->total_bytes += run_bytes;
656         dump_time = g_timeval_to_double(dump_info->total_time);
657         dump_kbytes = dump_info->total_bytes / 1024;
658         dump_kbps = dump_info->total_bytes / (1024 * dump_time);
659         
660         putresult(PARTIAL,
661                   "%s INPUT-%s TAPE-%s "
662                   "\"[sec %f kb %ju kps %f]\" \"\" \"\"\n",
663                   dump_info->handle,
664                   (queue_result & QUEUE_PRODUCER_ERROR) ? "ERROR" : "GOOD",
665                   (queue_result & QUEUE_CONSUMER_ERROR) ? "ERROR" : "GOOD",
666                   dump_time, (uintmax_t)dump_kbytes, dump_kbps);
667         put_partial_log(dump_info, dump_time, dump_kbytes);
668     }
669
670     amfree(qdiskname);
671     return FALSE;
672 }
673
674 /* Generate the actual header structure to write to tape. This means dropping
675  * bits related to the holding disk, and adding bits for split dumps. */
676 static dumpfile_t * munge_headers(dump_info_t * dump_info) {
677     dumpfile_t * rval;
678     int expected_splits;
679     
680     rval = taper_source_get_first_header(dump_info->source);
681
682     if (rval == NULL) {
683         return NULL;
684     }
685
686     rval->cont_filename[0] = '\0';
687
688     expected_splits = taper_source_predict_parts(dump_info->source);
689
690     if (expected_splits != 1) {
691         rval->type = F_SPLIT_DUMPFILE;
692         rval->partnum = dump_info->current_part;
693         rval->totalparts = expected_splits;
694     }
695
696     return rval;
697 }
698
699 /* We call this when we can't find a tape to write data to. This could
700    happen with the first (or only) part of a file, but it could also
701    happen with an intermediate part of a split dump. dump_bytes
702    is 0 if this is the first part of a dump. */
703 static void bail_no_volume(dump_info_t * dump_info) {
704     if (dump_info->total_bytes > 0) {
705         /* Second or later part of a split dump, so PARTIAL message. */
706         double dump_time = g_timeval_to_double(dump_info->total_time);
707         guint64 dump_kbytes = dump_info->total_bytes / 1024;
708         double dump_kbps = dump_kbytes / dump_time;
709         putresult(PARTIAL,
710                   "%s INPUT-GOOD TAPE-ERROR "
711                   "\"[sec %f kb %ju kps %f]\" \"\" \"no new tape\"\n",
712                   dump_info->handle, 
713                   dump_time, (uintmax_t)dump_kbytes, dump_kbps);
714         put_partial_log(dump_info, dump_time, dump_kbytes);
715     } else {
716         char * qdiskname = quote_string(dump_info->diskname);
717         putresult(FAILED,
718                   "%s INPUT-GOOD TAPE-ERROR \"\" \"No new tape.\"\n",
719                   dump_info->handle);
720         log_add(L_FAIL, "%s %s %s %d \"No new tape.\"",
721                 dump_info->hostname, qdiskname, dump_info->timestamp,
722                 dump_info->level);
723         amfree(qdiskname);
724     }
725 }
726
727 /* Link up the TaperSource with the Device, including retries etc. */
728 static void run_device_output(taper_state_t * taper_state,
729                               dump_info_t * dump_info) {
730     GValue val;
731     guint file_number;
732     dump_info->current_part = 1;
733     dump_info->total_time.tv_sec = 0;
734     dump_info->total_time.tv_usec = 0;
735     dump_info->total_bytes = 0;
736
737     for (;;) {
738         GTimeVal start_time, end_time, run_time;
739         StreamingRequirement streaming_mode;
740         queue_result_flags queue_result;
741         CountingConsumerData consumer_data;
742         dumpfile_t *this_header;
743         size_t max_memory;
744         
745         this_header = munge_headers(dump_info);
746         if (this_header == NULL) {
747             char * qdiskname = quote_string(dump_info->diskname);
748             putresult(FAILED,
749              "%s INPUT-ERROR TAPE-GOOD \"Failed reading dump header.\" \"\"\n",
750                       dump_info->handle);
751             log_add(L_FAIL, "%s %s %s %d \"Failed reading dump header.\"",
752                     dump_info->hostname, qdiskname, dump_info->timestamp,
753                     dump_info->level);
754             amfree(qdiskname);
755             return;
756         }            
757
758         if (!find_and_label_new_tape(taper_state, dump_info)) {
759             bail_no_volume(dump_info);
760             amfree(this_header);
761             return;
762         }
763
764         if (!device_start_file(taper_state->device, this_header)) {
765             bail_no_volume(dump_info);
766             amfree(this_header);
767             return;
768         }
769         amfree(this_header);
770
771         bzero(&val, sizeof(val));
772         if (!device_property_get(taper_state->device, PROPERTY_STREAMING, &val)
773             || !G_VALUE_HOLDS(&val, STREAMING_REQUIREMENT_TYPE)) {
774             g_fprintf(stderr, "taper: Couldn't get streaming type!\n");
775             streaming_mode = STREAMING_REQUIREMENT_REQUIRED;
776         } else {
777             streaming_mode = g_value_get_enum(&val);
778         }
779     
780         file_number = taper_state->device->file;
781
782         consumer_data.next_consumer = device_write_consumer;
783         consumer_data.next_consumer_data = taper_state->device;
784         consumer_data.bytes_written = 0;
785
786         g_get_current_time(&start_time);
787
788         if (getconf_seen(CNF_DEVICE_OUTPUT_BUFFER_SIZE)) {
789             max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE);
790             if (getconf_seen(CNF_TAPEBUFS)) {
791                 g_fprintf(stderr,
792                         "Configuration directives 'device_output_buffer_size' "
793                         "and \n"
794                         "'tapebufs' are incompatible; using former.\n");
795             }
796         } else if (getconf_seen(CNF_TAPEBUFS)) {
797             max_memory = getconf_int(CNF_TAPEBUFS) *
798                 device_write_max_size(taper_state->device);
799         } else {
800             /* Use default. */
801             max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE);
802         }
803
804         queue_result = do_consumer_producer_queue_full
805             (taper_source_producer,
806              dump_info->source,
807              counting_consumer,
808              &consumer_data,
809              device_write_max_size(taper_state->device), max_memory,
810              streaming_mode);
811
812         g_get_current_time(&end_time);
813         run_time = timesub(end_time, start_time);
814
815         /* The device_write_consumer may have closed the file with a short
816          * write, so we only finish here if it needs it. */
817         if (taper_state->device->in_file &&
818             !device_finish_file(taper_state->device)) {
819             queue_result = queue_result | QUEUE_CONSUMER_ERROR;
820         }
821
822         if (!finish_part_attempt(taper_state, dump_info, queue_result,
823                                  run_time, consumer_data.bytes_written)) {
824             break;
825         }
826     }
827 }
828
829 /* Handle a PORT_WRITE command. */
830 static void process_port_write(taper_state_t * state,
831                                struct cmdargs * cmdargs) {
832     dump_info_t dump_state;
833     guint64 splitsize;
834     guint64 fallback_splitsize;
835     char * split_diskbuffer;
836     char * argnames[] = {"command",               /* 1 */
837                          "handle",                /* 2 */
838                          "hostname",              /* 3 */
839                          "diskname",              /* 4 */
840                          "level",                 /* 5 */
841                          "datestamp",             /* 6 */
842                          "splitsize",             /* 7 */
843                          "split_diskbuffer",      /* 8 */
844                          "fallback_splitsize",    /* 9 */
845                           NULL };
846
847     validate_args(PORT_WRITE, cmdargs, argnames);
848
849     dump_state.handle = g_strdup(cmdargs->argv[2]);
850     dump_state.hostname = g_strdup(cmdargs->argv[3]);
851     dump_state.diskname = unquote_string(cmdargs->argv[4]);
852     
853     errno = 0;
854     dump_state.level = strtol(cmdargs->argv[5], NULL, 10);
855     if (errno != 0) {
856         error("error [taper PORT-WRITE: Invalid dump level %s]",
857               cmdargs->argv[5]);
858         g_assert_not_reached();
859     }
860     
861     dump_state.timestamp = strdup(cmdargs->argv[6]);
862
863     errno = 0;
864     splitsize = g_ascii_strtoull(cmdargs->argv[7], NULL, 10);
865     if (errno != 0) {
866         error("error [taper PORT-WRITE: Invalid splitsize %s]",
867               cmdargs->argv[7]);
868         g_assert_not_reached();
869     }
870     
871     if (strcmp(cmdargs->argv[8], "NULL") == 0) {
872         split_diskbuffer = NULL;
873     } else {
874         split_diskbuffer = g_strdup(cmdargs->argv[8]);
875     }
876     
877     errno = 0;
878     fallback_splitsize = g_ascii_strtoull(cmdargs->argv[9], NULL, 10);
879     if (errno != 0) {
880         error("error [taper PORT-WRITE: Invalid fallback_splitsize %s]",
881               cmdargs->argv[9]);
882         g_assert_not_reached();
883     }
884
885     dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
886                                            dump_state.diskname,
887                                            dump_state.level);
888     
889     if (!open_read_socket(&dump_state, split_diskbuffer, splitsize,
890                           fallback_splitsize)) {
891         free(split_diskbuffer);
892         return;
893     }
894     free(split_diskbuffer);
895
896     run_device_output(state, &dump_state);
897
898     free_dump_info(&dump_state);
899 }
900
901 /* Handle a FILE_WRITE command. */
902 static void process_file_write(taper_state_t * state,
903                                struct cmdargs * cmdargs) {
904     dump_info_t dump_state;
905     char * holding_disk_file;
906     guint64 splitsize;
907     char * argnames[] = {"command",               /* 1 */
908                          "handle",                /* 2 */
909                          "filename",              /* 3 */
910                          "hostname",              /* 4 */
911                          "diskname",              /* 5 */
912                          "level",                 /* 6 */
913                          "datestamp",             /* 7 */
914                          "splitsize",             /* 8 */
915                           NULL };
916
917     validate_args(FILE_WRITE, cmdargs, argnames);
918
919     dump_state.handle = g_strdup(cmdargs->argv[2]);
920     holding_disk_file = unquote_string(cmdargs->argv[3]);
921     dump_state.hostname = g_strdup(cmdargs->argv[4]);
922     dump_state.diskname = unquote_string(cmdargs->argv[5]);
923     
924     errno = 0;
925     dump_state.level = strtol(cmdargs->argv[6], NULL, 10);
926     if (errno != 0) {
927         error("error [taper FILE-WRITE: Invalid dump level %s]",
928               cmdargs->argv[5]);
929         g_assert_not_reached();
930     }
931     
932     dump_state.timestamp = strdup(cmdargs->argv[7]);
933
934     errno = 0;
935     splitsize = g_ascii_strtoull(cmdargs->argv[8], NULL, 10);
936     if (errno != 0) {
937         error("error [taper FILE-WRITE: Invalid splitsize %s]",
938               cmdargs->argv[8]);
939         g_assert_not_reached();
940     }
941
942     dump_state.id_string = g_strdup_printf("%s:%s.%d", dump_state.hostname,
943                                            dump_state.diskname,
944                                            dump_state.level);
945     
946     dump_state.source = taper_source_new(dump_state.handle, FILE_WRITE,
947                                          holding_disk_file, -1,
948                                          NULL, splitsize, -1);
949     /* FIXME: This should be handled properly. */
950     g_assert(dump_state.source != NULL);
951
952     run_device_output(state, &dump_state);
953
954     free_dump_info(&dump_state);
955     amfree(holding_disk_file);
956 }
957
958 /* Send QUITTING message to driver and associated logging. Always
959    returns false. */
960 static gboolean send_quitting(taper_state_t * state) {
961     putresult(QUITTING, "\n");
962     g_fprintf(stderr,"taper: DONE\n");
963     cleanup(state);
964     return FALSE;
965 }
966
967 /* This function recieves the START_TAPER command from driver, and
968    returns the attached timestamp. */
969 static gboolean find_first_tape(taper_state_t * state) {
970     cmd_t cmd;
971     /* Note: cmdargs.argv is never freed. In the entire Amanda codebase. */
972     struct cmdargs cmdargs;
973     tape_search_request_t search_request;
974     GThread * tape_search = NULL;
975     gboolean use_threads;
976
977     /* We save the value here in case it changes while we're running. */
978     use_threads = g_thread_supported();
979
980     search_request.state = state;
981     search_request.prolong = TRUE;
982     search_request.errmsg = NULL;
983     
984     if (use_threads) {
985         tape_search = g_thread_create(tape_search_thread,
986                                       &search_request, TRUE, NULL);
987     }
988
989     cmd = getcmd(&cmdargs);
990
991     switch (cmd) {
992     case START_TAPER: {
993         gboolean search_result;
994         state->driver_start_time = strdup(cmdargs.argv[2]);
995         if (use_threads) {
996             search_result = GPOINTER_TO_INT(g_thread_join(tape_search));
997         } else {
998             search_result =
999                 GPOINTER_TO_INT(tape_search_thread(&search_request));
1000         }
1001         if (search_result) {
1002             putresult(TAPER_OK, "\n");
1003         } else {
1004             putresult(TAPE_ERROR, "Could not find a tape to use.\n");
1005             log_add(L_ERROR, "no-tape [%s]", "Could not find a tape to use");
1006             if (search_request.errmsg != NULL) {
1007                 char *c, *c1;
1008                 c = c1 = search_request.errmsg;
1009                 while (*c != '\0') {
1010                     if (*c == '\n') {
1011                         *c = '\0';
1012                         log_add(L_WARNING,"%s", c1);
1013                         c1 = c+1;
1014                     }
1015                     c++;
1016                 }
1017                 if (strlen(c1) > 1 )
1018                     log_add(L_WARNING,"%s", c1);
1019             }
1020         }
1021         amfree(search_request.errmsg);
1022         return TRUE;
1023     }
1024     case QUIT:
1025         search_request.prolong = FALSE;
1026         if (use_threads) {
1027             g_thread_join(tape_search);
1028         }
1029         return send_quitting(state);
1030     default:
1031         error("error [file_reader_side cmd %d argc %d]", cmd, cmdargs.argc);
1032     }
1033
1034     g_assert_not_reached();
1035 }
1036
1037 /* In running mode (not startup mode), get a command from driver and
1038    deal with it. */
1039 static gboolean process_driver_command(taper_state_t * state) {
1040     cmd_t cmd;
1041     struct cmdargs cmdargs;
1042     char * q;
1043
1044     /* This will return QUIT if driver has died. */
1045     cmd = getcmd(&cmdargs);
1046     switch (cmd) {
1047     case PORT_WRITE:
1048         /*
1049          * PORT-WRITE
1050          *   handle
1051          *   hostname
1052          *   features
1053          *   diskname
1054          *   level
1055          *   datestamp
1056          *   splitsize
1057          *   split_diskbuffer
1058          */
1059         process_port_write(state, &cmdargs);
1060         break;
1061         
1062     case FILE_WRITE:
1063         /*
1064          * FILE-WRITE
1065          *   handle
1066          *   filename
1067          *   hostname
1068          *   features
1069          *   diskname
1070          *   level
1071          *   datestamp
1072          *   splitsize
1073          */
1074         process_file_write(state, &cmdargs);
1075         break;
1076         
1077     case QUIT:
1078         return send_quitting(state);
1079     default:
1080         if (cmdargs.argc >= 1) {
1081             q = squote(cmdargs.argv[1]);
1082         } else if (cmdargs.argc >= 0) {
1083             q = squote(cmdargs.argv[0]);
1084         } else {
1085             q = stralloc("(no input?)");
1086         }
1087         putresult(BAD_COMMAND, "%s\n", q);
1088         amfree(q);
1089         break;
1090     }
1091
1092     return TRUE;
1093 }
1094
1095 int main(int argc, char ** argv) {
1096     char * tapelist_name;
1097     int have_changer;
1098     taper_state_t state;
1099     config_overwrites_t *cfg_ovr = NULL;
1100     char *cfg_opt = NULL;
1101
1102     /*
1103      * Configure program for internationalization:
1104      *   1) Only set the message locale for now.
1105      *   2) Set textdomain for all amanda related programs to "amanda"
1106      *      We don't want to be forced to support dozens of message catalogs.
1107      */
1108     setlocale(LC_MESSAGES, "C");
1109     textdomain("amanda");
1110     
1111     safe_fd(-1, 0);
1112     set_pname("taper");
1113
1114     dbopen("server");
1115
1116     device_api_init();
1117     init_taper_state(&state);
1118
1119     /* Don't die when child closes pipe */
1120     signal(SIGPIPE, SIG_IGN);
1121
1122     g_fprintf(stderr, _("%s: pid %ld executable %s version %s\n"),
1123             get_pname(), (long) getpid(), argv[0], version());
1124     dbprintf(_("%s: pid %ld executable %s version %s\n"),
1125               get_pname(), (long) getpid(), argv[0], version());
1126
1127     /* Process options */
1128
1129     cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
1130
1131     if(argc > 2) {
1132         error("Too many arguments!\n");
1133         g_assert_not_reached();
1134     }
1135     if (argc > 1)
1136         cfg_opt = argv[1];
1137     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD | CONFIG_INIT_FATAL,
1138                 cfg_opt);
1139     apply_config_overwrites(cfg_ovr);
1140
1141     safe_cd();
1142
1143     set_logerror(logerror);
1144
1145     check_running_as(RUNNING_AS_DUMPUSER);
1146
1147     dbrename(config_name, DBG_SUBDIR_SERVER);
1148
1149     tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST));
1150
1151     if (read_tapelist(tapelist_name) != 0) {
1152         error("could not load tapelist \"%s\"", tapelist_name);
1153         g_assert_not_reached();
1154     }
1155     amfree(tapelist_name);
1156
1157     have_changer = changer_init();
1158     if (have_changer < 0) {
1159         error("changer initialization failed: %s", strerror(errno));
1160         g_assert_not_reached();
1161     }
1162
1163     state.next_tape_label = NULL;
1164     state.next_tape_device = NULL;
1165     state.cur_tape = 0;
1166     
1167     if (!find_first_tape(&state)) {
1168         return EXIT_SUCCESS;
1169     }
1170
1171     while (process_driver_command(&state));
1172     return EXIT_SUCCESS;
1173 }