Imported Upstream version 3.1.0
[debian/amanda] / server-src / driverio.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 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  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: driverio.c,v 1.92 2006/08/24 01:57:16 paddy_s Exp $
29  *
30  * I/O-related functions for driver program
31  */
32 #include "amanda.h"
33 #include "util.h"
34 #include "clock.h"
35 #include "server_util.h"
36 #include "conffile.h"
37 #include "diskfile.h"
38 #include "infofile.h"
39 #include "logfile.h"
40 #include "timestamp.h"
41
42 #define GLOBAL          /* the global variables defined here */
43 #include "driverio.h"
44
45 int nb_chunker = 0;
46
47 static const char *childstr(int);
48
49 void
50 init_driverio(void)
51 {
52     dumper_t *dumper;
53
54     taper = -1;
55
56     for(dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
57         dumper->fd = -1;
58     }
59 }
60
61
62 static const char *
63 childstr(
64     int fd)
65 {
66     static char buf[NUM_STR_SIZE + 32];
67     dumper_t *dumper;
68
69     if (fd == taper)
70         return ("taper");
71
72     for (dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
73         if (dumper->fd == fd)
74             return (dumper->name);
75         if (dumper->chunker && dumper->chunker->fd == fd)
76             return (dumper->chunker->name);
77     }
78     g_snprintf(buf, SIZEOF(buf), _("unknown child (fd %d)"), fd);
79     return (buf);
80 }
81
82
83 void
84 startup_tape_process(
85     char *taper_program)
86 {
87     int    fd[2];
88     char **config_options;
89
90     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
91         error(_("taper pipe: %s"), strerror(errno));
92         /*NOTREACHED*/
93     }
94     if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
95         error(_("taper socketpair 0: descriptor %d out of range (0 .. %d)\n"),
96               fd[0], (int)FD_SETSIZE-1);
97         /*NOTREACHED*/
98     }
99     if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) {
100         error(_("taper socketpair 1: descriptor %d out of range (0 .. %d)\n"),
101               fd[1], (int)FD_SETSIZE-1);
102         /*NOTREACHED*/
103     }
104
105     switch(taper_pid = fork()) {
106     case -1:
107         error(_("fork taper: %s"), strerror(errno));
108         /*NOTREACHED*/
109
110     case 0:     /* child process */
111         aclose(fd[0]);
112         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
113             error(_("taper dup2: %s"), strerror(errno));
114         config_options = get_config_options(2);
115         config_options[0] = "taper";
116         config_options[1] = get_config_name();
117         safe_fd(-1, 0);
118         execve(taper_program, config_options, safe_env());
119         error("exec %s: %s", taper_program, strerror(errno));
120         /*NOTREACHED*/
121
122     default:    /* parent process */
123         aclose(fd[1]);
124         taper = fd[0];
125         taper_ev_read = NULL;
126     }
127 }
128
129 void
130 startup_dump_process(
131     dumper_t *dumper,
132     char *dumper_program)
133 {
134     int    fd[2];
135     char **config_options;
136
137     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
138         error(_("%s pipe: %s"), dumper->name, strerror(errno));
139         /*NOTREACHED*/
140     }
141
142     switch(dumper->pid = fork()) {
143     case -1:
144         error(_("fork %s: %s"), dumper->name, strerror(errno));
145         /*NOTREACHED*/
146
147     case 0:             /* child process */
148         aclose(fd[0]);
149         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
150             error(_("%s dup2: %s"), dumper->name, strerror(errno));
151         config_options = get_config_options(2);
152         config_options[0] = dumper->name ? dumper->name : "dumper",
153         config_options[1] = get_config_name();
154         safe_fd(-1, 0);
155         execve(dumper_program, config_options, safe_env());
156         error(_("exec %s (%s): %s"), dumper_program,
157               dumper->name, strerror(errno));
158         /*NOTREACHED*/
159
160     default:    /* parent process */
161         aclose(fd[1]);
162         dumper->fd = fd[0];
163         dumper->ev_read = NULL;
164         dumper->busy = dumper->down = 0;
165         dumper->dp = NULL;
166         g_fprintf(stderr,_("driver: started %s pid %u\n"),
167                 dumper->name, (unsigned)dumper->pid);
168         fflush(stderr);
169     }
170 }
171
172 void
173 startup_dump_processes(
174     char *dumper_program,
175     int inparallel,
176     char *timestamp)
177 {
178     int i;
179     dumper_t *dumper;
180     char number[NUM_STR_SIZE];
181
182     for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
183         g_snprintf(number, SIZEOF(number), "%d", i);
184         dumper->name = stralloc2("dumper", number);
185         dumper->chunker = &chktable[i];
186         chktable[i].name = stralloc2("chunker", number);
187         chktable[i].dumper = dumper;
188         chktable[i].fd = -1;
189
190         startup_dump_process(dumper, dumper_program);
191         dumper_cmd(dumper, START, NULL, (void *)timestamp);
192     }
193 }
194
195 void
196 startup_chunk_process(
197     chunker_t *chunker,
198     char *chunker_program)
199 {
200     int    fd[2];
201     char **config_options;
202
203     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
204         error(_("%s pipe: %s"), chunker->name, strerror(errno));
205         /*NOTREACHED*/
206     }
207
208     switch(chunker->pid = fork()) {
209     case -1:
210         error(_("fork %s: %s"), chunker->name, strerror(errno));
211         /*NOTREACHED*/
212
213     case 0:             /* child process */
214         aclose(fd[0]);
215         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
216             error(_("%s dup2: %s"), chunker->name, strerror(errno));
217             /*NOTREACHED*/
218         }
219         config_options = get_config_options(2);
220         config_options[0] = chunker->name ? chunker->name : "chunker",
221         config_options[1] = get_config_name();
222         safe_fd(-1, 0);
223         execve(chunker_program, config_options, safe_env());
224         error(_("exec %s (%s): %s"), chunker_program,
225               chunker->name, strerror(errno));
226         /*NOTREACHED*/
227
228     default:    /* parent process */
229         aclose(fd[1]);
230         chunker->down = 0;
231         chunker->fd = fd[0];
232         chunker->ev_read = NULL;
233         g_fprintf(stderr,_("driver: started %s pid %u\n"),
234                 chunker->name, (unsigned)chunker->pid);
235         fflush(stderr);
236     }
237 }
238
239 cmd_t
240 getresult(
241     int fd,
242     int show,
243     int *result_argc,
244     char ***result_argv)
245 {
246     cmd_t t;
247     char *line;
248
249     if((line = areads(fd)) == NULL) {
250         if(errno) {
251             error(_("reading result from %s: %s"), childstr(fd), strerror(errno));
252             /*NOTREACHED*/
253         }
254         *result_argv = NULL;
255         *result_argc = 0;                               /* EOF */
256     } else {
257         *result_argv = split_quoted_strings(line);
258         *result_argc = g_strv_length(*result_argv);
259     }
260
261     if(show) {
262         g_printf(_("driver: result time %s from %s:"),
263                walltime_str(curclock()),
264                childstr(fd));
265         if(line) {
266             g_printf(" %s", line);
267             putchar('\n');
268         } else {
269             g_printf(" (eof)\n");
270         }
271         fflush(stdout);
272     }
273     amfree(line);
274
275     if(*result_argc < 1) return BOGUS;
276
277     for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
278         if(strcmp((*result_argv)[0], cmdstr[t]) == 0) return t;
279
280     return BOGUS;
281 }
282
283
284 int
285 taper_cmd(
286     cmd_t cmd,
287     void *ptr,
288     char *destname,
289     int level,
290     char *datestamp)
291 {
292     char *cmdline = NULL;
293     char number[NUM_STR_SIZE];
294     char splitsize[NUM_STR_SIZE];
295     char fallback_splitsize[NUM_STR_SIZE];
296     char orig_kb[NUM_STR_SIZE];
297     char *diskbuffer = NULL;
298     disk_t *dp;
299     char *qname;
300     char *qdest;
301     char *q;
302
303     switch(cmd) {
304     case START_TAPER:
305         cmdline = vstralloc(cmdstr[cmd], " ", (char *)ptr, "\n", NULL);
306         break;
307     case FILE_WRITE:
308         dp = (disk_t *) ptr;
309         qname = quote_string(dp->name);
310         qdest = quote_string(destname);
311         g_snprintf(number, SIZEOF(number), "%d", level);
312         g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
313                  (long long)dp->tape_splitsize * 1024);
314         g_snprintf(orig_kb, SIZEOF(orig_kb), "%jd",
315                  (intmax_t)sched(dp)->origsize);
316         cmdline = vstralloc(cmdstr[cmd],
317                             " ", disk2serial(dp),
318                             " ", qdest,
319                             " ", dp->host->hostname,
320                             " ", qname,
321                             " ", number,
322                             " ", datestamp,
323                             " ", splitsize,
324                             " ", orig_kb,
325                             "\n", NULL);
326         amfree(qdest);
327         amfree(qname);
328         break;
329     case PORT_WRITE:
330         dp = (disk_t *) ptr;
331         qname = quote_string(dp->name);
332         g_snprintf(number, SIZEOF(number), "%d", level);
333
334         /*
335           If we haven't been given a place to buffer split dumps to disk,
336           make the argument something besides and empty string so's taper
337           won't get confused
338         */
339         if(!dp->split_diskbuffer || dp->split_diskbuffer[0] == '\0'){
340             diskbuffer = "NULL";
341         } else {
342             diskbuffer = dp->split_diskbuffer;
343         }
344         g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
345                  (long long)dp->tape_splitsize * 1024);
346         g_snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), "%lld",
347                  (long long)dp->fallback_splitsize * 1024);
348         cmdline = vstralloc(cmdstr[cmd],
349                             " ", disk2serial(dp),
350                             " ", dp->host->hostname,
351                             " ", qname,
352                             " ", number,
353                             " ", datestamp,
354                             " ", splitsize,
355                             " ", diskbuffer,
356                             " ", fallback_splitsize,
357                             "\n", NULL);
358         amfree(qname);
359         break;
360     case DONE: /* handle */
361         dp = (disk_t *) ptr;
362         g_snprintf(number, SIZEOF(number), "%jd",
363                  (intmax_t)(sched(dp)->origsize));
364         cmdline = vstralloc(cmdstr[cmd],
365                             " ", disk2serial(dp),
366                             " ", number,
367                             "\n", NULL);
368         break;
369     case FAILED: /* handle */
370         dp = (disk_t *) ptr;
371         cmdline = vstralloc(cmdstr[cmd],
372                             " ", disk2serial(dp),
373                             "\n", NULL);
374         break;
375     case NO_NEW_TAPE:
376         q = quote_string((char *)ptr);
377         cmdline = vstralloc(cmdstr[cmd],
378                             " ", q,
379                             "\n", NULL);
380         amfree(q);
381         break;
382     case NEW_TAPE:
383     case QUIT:
384         cmdline = stralloc2(cmdstr[cmd], "\n");
385         break;
386     default:
387         error(_("Don't know how to send %s command to taper"), cmdstr[cmd]);
388         /*NOTREACHED*/
389     }
390
391     /*
392      * Note: cmdline already has a '\n'.
393      */
394     g_printf(_("driver: send-cmd time %s to taper: %s"),
395            walltime_str(curclock()), cmdline);
396     fflush(stdout);
397     if ((full_write(taper, cmdline, strlen(cmdline))) < strlen(cmdline)) {
398         g_printf(_("writing taper command '%s' failed: %s\n"),
399                 cmdline, strerror(errno));
400         fflush(stdout);
401         amfree(cmdline);
402         return 0;
403     }
404     if(cmd == QUIT) aclose(taper);
405     amfree(cmdline);
406     return 1;
407 }
408
409 int
410 dumper_cmd(
411     dumper_t *dumper,
412     cmd_t cmd,
413     disk_t *dp,
414     char   *mesg)
415 {
416     char *cmdline = NULL;
417     char number[NUM_STR_SIZE];
418     char numberport[NUM_STR_SIZE];
419     char *o;
420     char *device;
421     char *features;
422     char *qname;
423     char *qmesg;
424
425     switch(cmd) {
426     case START:
427         cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
428         break;
429     case PORT_DUMP:
430         if(dp && dp->device) {
431             device = dp->device;
432         }
433         else {
434             device = "NODEVICE";
435         }
436
437         if (dp != NULL) {
438             application_t *application = NULL;
439             char *plugin;
440             char *qplugin;
441             char *qamandad_path;
442             char *qclient_username;
443             char *qclient_port;
444             char *qssh_keys;
445
446             if (dp->application != NULL) {
447                 application = lookup_application(dp->application);
448                 g_assert(application != NULL);
449             }
450
451             device = quote_string((dp->device) ? dp->device : "NODEVICE");
452             qname = quote_string(dp->name);
453             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
454             g_snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
455             features = am_feature_to_string(dp->host->features);
456             if (am_has_feature(dp->host->features, fe_req_xml)) {
457                 o = xml_optionstr(dp, 1);
458                 if (application) {
459                     char *xml_app;
460                     xml_app = xml_application(dp, application,
461                                               dp->host->features);
462                     vstrextend(&o, xml_app, NULL);
463                     amfree(xml_app);
464                 }
465                 o = quote_string(o);
466             } else {
467                 o = optionstr(dp);
468             }
469
470             g_assert(dp->program);
471             if (0 == strcmp(dp->program, "APPLICATION")) {
472                 g_assert(application != NULL);
473                 plugin = application_get_plugin(application);
474             } else {
475                 plugin = dp->program;
476             }
477             qplugin = quote_string(plugin);
478             qamandad_path = quote_string(dp->amandad_path);
479             qclient_username = quote_string(dp->client_username);
480             qclient_port = quote_string(dp->client_port);
481             qssh_keys = quote_string(dp->ssh_keys);
482             dbprintf("security_driver %s\n", dp->auth);
483
484             cmdline = vstralloc(cmdstr[cmd],
485                             " ", disk2serial(dp),
486                             " ", numberport,
487                             " ", dp->host->hostname,
488                             " ", features,
489                             " ", qname,
490                             " ", device,
491                             " ", number,
492                             " ", sched(dp)->dumpdate,
493                             " ", qplugin,
494                             " ", qamandad_path,
495                             " ", qclient_username,
496                             " ", qclient_port,
497                             " ", qssh_keys,
498                             " ", dp->auth,
499                             " ", data_path_to_string(dp->data_path),
500                             " ", dp->dataport_list,
501                             " |", o,
502                             "\n", NULL);
503             amfree(qplugin);
504             amfree(qamandad_path);
505             amfree(qclient_username);
506             amfree(qclient_port);
507             amfree(qssh_keys);
508             amfree(features);
509             amfree(o);
510             amfree(qname);
511             amfree(device);
512         } else {
513                 error(_("PORT-DUMP without disk pointer\n"));
514                 /*NOTREACHED*/
515         }
516         break;
517     case QUIT:
518     case ABORT:
519         qmesg = quote_string(mesg);
520         cmdline = vstralloc(cmdstr[cmd], " ", qmesg, "\n", NULL );
521         amfree(qmesg);
522         break;
523     default:
524         error(_("Don't know how to send %s command to dumper"), cmdstr[cmd]);
525         /*NOTREACHED*/
526     }
527
528     /*
529      * Note: cmdline already has a '\n'.
530      */
531     if(dumper->down) {
532         g_printf(_("driver: send-cmd time %s ignored to down dumper %s: %s"),
533                walltime_str(curclock()), dumper->name, cmdline);
534     } else {
535         g_printf(_("driver: send-cmd time %s to %s: %s"),
536                walltime_str(curclock()), dumper->name, cmdline);
537         fflush(stdout);
538         if (full_write(dumper->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
539             g_printf(_("writing %s command: %s\n"), dumper->name, strerror(errno));
540             fflush(stdout);
541             amfree(cmdline);
542             return 0;
543         }
544         if (cmd == QUIT) aclose(dumper->fd);
545     }
546     amfree(cmdline);
547     return 1;
548 }
549
550 int
551 chunker_cmd(
552     chunker_t *chunker,
553     cmd_t cmd,
554     disk_t *dp,
555     char   *mesg)
556 {
557     char *cmdline = NULL;
558     char number[NUM_STR_SIZE];
559     char chunksize[NUM_STR_SIZE];
560     char use[NUM_STR_SIZE];
561     char *o;
562     int activehd=0;
563     assignedhd_t **h=NULL;
564     char *features;
565     char *qname;
566     char *qdest;
567
568     switch(cmd) {
569     case START:
570         cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
571         break;
572     case PORT_WRITE:
573         if(dp && sched(dp) && sched(dp)->holdp) {
574             h = sched(dp)->holdp;
575             activehd = sched(dp)->activehd;
576         }
577
578         if (dp && h) {
579             qname = quote_string(dp->name);
580             qdest = quote_string(sched(dp)->destname);
581             h[activehd]->disk->allocated_dumpers++;
582             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
583             g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
584                     (long long)holdingdisk_get_chunksize(h[0]->disk->hdisk));
585             g_snprintf(use, SIZEOF(use), "%lld",
586                     (long long)h[0]->reserved);
587             features = am_feature_to_string(dp->host->features);
588             o = optionstr(dp);
589             cmdline = vstralloc(cmdstr[cmd],
590                             " ", disk2serial(dp),
591                             " ", qdest,
592                             " ", dp->host->hostname,
593                             " ", features,
594                             " ", qname,
595                             " ", number,
596                             " ", sched(dp)->dumpdate,
597                             " ", chunksize,
598                             " ", dp->program,
599                             " ", use,
600                             " |", o,
601                             "\n", NULL);
602             amfree(features);
603             amfree(o);
604             amfree(qdest);
605             amfree(qname);
606         } else {
607                 error(_("%s command without disk and holding disk.\n"),
608                       cmdstr[cmd]);
609                 /*NOTREACHED*/
610         }
611         break;
612     case CONTINUE:
613         if(dp && sched(dp) && sched(dp)->holdp) {
614             h = sched(dp)->holdp;
615             activehd = sched(dp)->activehd;
616         }
617
618         if(dp && h) {
619             qname = quote_string(dp->name);
620             qdest = quote_string(h[activehd]->destname);
621             h[activehd]->disk->allocated_dumpers++;
622             g_snprintf(chunksize, SIZEOF(chunksize), "%lld", 
623                      (long long)holdingdisk_get_chunksize(h[activehd]->disk->hdisk));
624             g_snprintf(use, SIZEOF(use), "%lld", 
625                      (long long)(h[activehd]->reserved - h[activehd]->used));
626             cmdline = vstralloc(cmdstr[cmd],
627                                 " ", disk2serial(dp),
628                                 " ", qdest,
629                                 " ", chunksize,
630                                 " ", use,
631                                 "\n", NULL );
632             amfree(qdest);
633             amfree(qname);
634         } else {
635             cmdline = stralloc2(cmdstr[cmd], "\n");
636         }
637         break;
638     case QUIT:
639     case ABORT:
640         {
641             char *q = quote_string(mesg);
642             cmdline = vstralloc(cmdstr[cmd], " ", q, "\n", NULL);
643             amfree(q);
644         }
645         break;
646     case DONE:
647     case FAILED:
648         if( dp ) {
649             cmdline = vstralloc(cmdstr[cmd],
650                                 " ", disk2serial(dp),
651                                 "\n",  NULL);
652         } else {
653             cmdline = vstralloc(cmdstr[cmd], "\n");
654         }
655         break;
656     default:
657         error(_("Don't know how to send %s command to chunker"), cmdstr[cmd]);
658         /*NOTREACHED*/
659     }
660
661     /*
662      * Note: cmdline already has a '\n'.
663      */
664     g_printf(_("driver: send-cmd time %s to %s: %s"),
665            walltime_str(curclock()), chunker->name, cmdline);
666     fflush(stdout);
667     if (full_write(chunker->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
668         g_printf(_("writing %s command: %s\n"), chunker->name, strerror(errno));
669         fflush(stdout);
670         amfree(cmdline);
671         return 0;
672     }
673     if (cmd == QUIT) aclose(chunker->fd);
674     amfree(cmdline);
675     return 1;
676 }
677
678 #define MAX_SERIAL MAX_DUMPERS+1        /* one for the taper */
679
680 long generation = 1;
681
682 struct serial_s {
683     long gen;
684     disk_t *dp;
685 } stable[MAX_SERIAL];
686
687 disk_t *
688 serial2disk(
689     char *str)
690 {
691     int rc, s;
692     long gen;
693
694     rc = sscanf(str, "%d-%ld", &s, &gen);
695     if(rc != 2) {
696         error(_("error [serial2disk \"%s\" parse error]"), str);
697         /*NOTREACHED*/
698     } else if (s < 0 || s >= MAX_SERIAL) {
699         error(_("error [serial out of range 0..%d: %d]"), MAX_SERIAL, s);
700         /*NOTREACHED*/
701     }
702     if(gen != stable[s].gen)
703         g_printf(_("driver: serial2disk error time %s serial gen mismatch %s\n"),
704                walltime_str(curclock()), str);
705     return stable[s].dp;
706 }
707
708 void
709 free_serial(
710     char *str)
711 {
712     int rc, s;
713     long gen;
714
715     rc = sscanf(str, _("%d-%ld"), &s, &gen);
716     if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
717         /* nuke self to get core dump for Brett */
718         g_fprintf(stderr, _("driver: free_serial: str \"%s\" rc %d s %d\n"),
719                 str, rc, s);
720         fflush(stderr);
721         abort();
722     }
723
724     if(gen != stable[s].gen)
725         g_printf(_("driver: free_serial error time %s serial gen mismatch %s\n"),
726                walltime_str(curclock()),str);
727     stable[s].gen = 0;
728     stable[s].dp = NULL;
729 }
730
731
732 void
733 free_serial_dp(
734     disk_t *dp)
735 {
736     int s;
737
738     for(s = 0; s < MAX_SERIAL; s++) {
739         if(stable[s].dp == dp) {
740             stable[s].gen = 0;
741             stable[s].dp = NULL;
742             return;
743         }
744     }
745
746     g_printf(_("driver: error time %s serial not found\n"),
747            walltime_str(curclock()));
748 }
749
750
751 void
752 check_unfree_serial(void)
753 {
754     int s;
755
756     /* find used serial number */
757     for(s = 0; s < MAX_SERIAL; s++) {
758         if(stable[s].gen != 0 || stable[s].dp != NULL) {
759             g_printf(_("driver: error time %s bug: serial in use: %02d-%05ld\n"),
760                    walltime_str(curclock()), s, stable[s].gen);
761         }
762     }
763 }
764
765 char *disk2serial(
766     disk_t *dp)
767 {
768     int s;
769     static char str[NUM_STR_SIZE];
770
771     for(s = 0; s < MAX_SERIAL; s++) {
772         if(stable[s].dp == dp) {
773             g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
774             return str;
775         }
776     }
777
778     /* find unused serial number */
779     for(s = 0; s < MAX_SERIAL; s++)
780         if(stable[s].gen == 0 && stable[s].dp == NULL)
781             break;
782     if(s >= MAX_SERIAL) {
783         g_printf(_("driver: error time %s bug: out of serial numbers\n"),
784                walltime_str(curclock()));
785         s = 0;
786     }
787
788     stable[s].gen = generation++;
789     stable[s].dp = dp;
790
791     g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
792     return str;
793 }
794
795 void
796 update_info_dumper(
797      disk_t *dp,
798      off_t origsize,
799      off_t dumpsize,
800      time_t dumptime)
801 {
802     int level, i;
803     info_t info;
804     stats_t *infp;
805     perf_t *perfp;
806     char *conf_infofile;
807
808     level = sched(dp)->level;
809
810     conf_infofile = config_dir_relative(getconf_str(CNF_INFOFILE));
811     if (open_infofile(conf_infofile)) {
812         error(_("could not open info db \"%s\""), conf_infofile);
813         /*NOTREACHED*/
814     }
815     amfree(conf_infofile);
816
817     get_info(dp->host->hostname, dp->name, &info);
818
819     /* Clean up information about this and higher-level dumps.  This
820        assumes that update_info_dumper() is always run before
821        update_info_taper(). */
822     for (i = level; i < DUMP_LEVELS; ++i) {
823       infp = &info.inf[i];
824       infp->size = (off_t)-1;
825       infp->csize = (off_t)-1;
826       infp->secs = (time_t)-1;
827       infp->date = (time_t)-1;
828       infp->label[0] = '\0';
829       infp->filenum = 0;
830     }
831
832     /* now store information about this dump */
833     infp = &info.inf[level];
834     infp->size = origsize;
835     infp->csize = dumpsize;
836     infp->secs = dumptime;
837     infp->date = get_time_from_timestamp(sched(dp)->datestamp);
838
839     if(level == 0) perfp = &info.full;
840     else perfp = &info.incr;
841
842     /* Update the stats, but only if the new values are meaningful */
843     if(dp->compress != COMP_NONE && origsize > (off_t)0) {
844         newperf(perfp->comp, (double)dumpsize/(double)origsize);
845     }
846     if(dumptime > (time_t)0) {
847         if((off_t)dumptime >= dumpsize)
848             newperf(perfp->rate, 1);
849         else
850             newperf(perfp->rate, (double)dumpsize/(double)dumptime);
851     }
852
853     if(origsize >= (off_t)0 && getconf_int(CNF_RESERVE)<100) {
854         info.command = NO_COMMAND;
855     }
856
857     if (origsize >= (off_t)0 && level == info.last_level) {
858         info.consecutive_runs++;
859     } else if (origsize >= (off_t)0 || level < info.last_level) {
860         info.last_level = level;
861         info.consecutive_runs = 1;
862     }
863
864     if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
865         for(i=NB_HISTORY-1;i>0;i--) {
866             info.history[i] = info.history[i-1];
867         }
868
869         info.history[0].level = level;
870         info.history[0].size  = origsize;
871         info.history[0].csize = dumpsize;
872         info.history[0].date  = get_time_from_timestamp(sched(dp)->datestamp);
873         info.history[0].secs  = dumptime;
874     }
875
876     if (put_info(dp->host->hostname, dp->name, &info)) {
877         int save_errno = errno;
878         g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
879                   dp->host->hostname, dp->name, strerror(save_errno));
880         log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
881                 dp->host->hostname, dp->name, strerror(save_errno));
882         error(_("infofile update failed (%s,'%s'): %s\n"),
883               dp->host->hostname, dp->name, strerror(save_errno));
884         /*NOTREACHED*/
885     }
886
887     close_infofile();
888 }
889
890 void
891 update_info_taper(
892     disk_t *dp,
893     char *label,
894     off_t filenum,
895     int level)
896 {
897     info_t info;
898     stats_t *infp;
899     int rc;
900
901     rc = open_infofile(getconf_str(CNF_INFOFILE));
902     if(rc) {
903         error(_("could not open infofile %s: %s (%d)"), getconf_str(CNF_INFOFILE),
904               strerror(errno), rc);
905         /*NOTREACHED*/
906     }
907
908     get_info(dp->host->hostname, dp->name, &info);
909
910     infp = &info.inf[level];
911     /* XXX - should we record these two if no-record? */
912     strncpy(infp->label, label, SIZEOF(infp->label)-1);
913     infp->label[SIZEOF(infp->label)-1] = '\0';
914     infp->filenum = filenum;
915
916     info.command = NO_COMMAND;
917
918     if (put_info(dp->host->hostname, dp->name, &info)) {
919         int save_errno = errno;
920         g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
921                   dp->host->hostname, dp->name, strerror(save_errno));
922         log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
923                 dp->host->hostname, dp->name, strerror(save_errno));
924         error(_("infofile update failed (%s,'%s'): %s\n"),
925               dp->host->hostname, dp->name, strerror(save_errno));
926         /*NOTREACHED*/
927     }
928     close_infofile();
929 }
930
931 /* Free an array of pointers to assignedhd_t after freeing the
932  * assignedhd_t themselves. The array must be NULL-terminated.
933  */
934 void free_assignedhd(
935     assignedhd_t **ahd)
936 {
937     int i;
938
939     if( !ahd ) { return; }
940
941     for( i = 0; ahd[i]; i++ ) {
942         amfree(ahd[i]->destname);
943         amfree(ahd[i]);
944     }
945     amfree(ahd);
946 }