Imported Upstream version 3.2.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_fd = -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_fd)
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     int   taper_parallel_write,
87     gboolean no_taper)
88 {
89     int       fd[2];
90     int       i;
91     char    **config_options;
92     taper_t  *taper;
93
94     /* always allocate the tapetable */
95     tapetable = calloc(sizeof(taper_t), taper_parallel_write+1);
96
97     for (taper = tapetable, i = 0; i < taper_parallel_write; taper++, i++) {
98         taper->name = g_strdup_printf("worker%d", i);
99         taper->sendresult = 0;
100         taper->input_error = NULL;
101         taper->tape_error = NULL;
102         taper->result = 0;
103         taper->dumper = NULL;
104         taper->disk = NULL;
105         taper->first_label = NULL;
106         taper->first_fileno = 0;
107         taper->state = TAPER_STATE_DEFAULT;
108         taper->left = 0;
109         taper->written = 0;
110
111         /* jump right to degraded mode if there's no taper */
112         if (no_taper) {
113             taper->tape_error = g_strdup("no taper started (--no-taper)");
114             taper->result = BOGUS;
115         }
116     }
117
118     /* don't start the taper if we're not supposed to */
119     if (no_taper)
120         return;
121
122     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
123         error(_("taper pipe: %s"), strerror(errno));
124         /*NOTREACHED*/
125     }
126     if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
127         error(_("taper socketpair 0: descriptor %d out of range (0 .. %d)\n"),
128               fd[0], (int)FD_SETSIZE-1);
129         /*NOTREACHED*/
130     }
131     if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) {
132         error(_("taper socketpair 1: descriptor %d out of range (0 .. %d)\n"),
133               fd[1], (int)FD_SETSIZE-1);
134         /*NOTREACHED*/
135     }
136
137     switch(taper_pid = fork()) {
138     case -1:
139         error(_("fork taper: %s"), strerror(errno));
140         /*NOTREACHED*/
141
142     case 0:     /* child process */
143         aclose(fd[0]);
144         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
145             error(_("taper dup2: %s"), strerror(errno));
146         config_options = get_config_options(2);
147         config_options[0] = "taper";
148         config_options[1] = get_config_name();
149         safe_fd(-1, 0);
150         execve(taper_program, config_options, safe_env());
151         error("exec %s: %s", taper_program, strerror(errno));
152         /*NOTREACHED*/
153
154     default:    /* parent process */
155         aclose(fd[1]);
156         taper_fd = fd[0];
157         taper_ev_read = NULL;
158     }
159 }
160
161 void
162 startup_dump_process(
163     dumper_t *dumper,
164     char *dumper_program)
165 {
166     int    fd[2];
167     char **config_options;
168
169     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
170         error(_("%s pipe: %s"), dumper->name, strerror(errno));
171         /*NOTREACHED*/
172     }
173
174     switch(dumper->pid = fork()) {
175     case -1:
176         error(_("fork %s: %s"), dumper->name, strerror(errno));
177         /*NOTREACHED*/
178
179     case 0:             /* child process */
180         aclose(fd[0]);
181         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
182             error(_("%s dup2: %s"), dumper->name, strerror(errno));
183         config_options = get_config_options(2);
184         config_options[0] = dumper->name ? dumper->name : "dumper",
185         config_options[1] = get_config_name();
186         safe_fd(-1, 0);
187         execve(dumper_program, config_options, safe_env());
188         error(_("exec %s (%s): %s"), dumper_program,
189               dumper->name, strerror(errno));
190         /*NOTREACHED*/
191
192     default:    /* parent process */
193         aclose(fd[1]);
194         dumper->fd = fd[0];
195         dumper->ev_read = NULL;
196         dumper->busy = dumper->down = 0;
197         dumper->dp = NULL;
198         g_fprintf(stderr,_("driver: started %s pid %u\n"),
199                 dumper->name, (unsigned)dumper->pid);
200         fflush(stderr);
201     }
202 }
203
204 void
205 startup_dump_processes(
206     char *dumper_program,
207     int inparallel,
208     char *timestamp)
209 {
210     int i;
211     dumper_t *dumper;
212     char number[NUM_STR_SIZE];
213
214     for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
215         g_snprintf(number, SIZEOF(number), "%d", i);
216         dumper->name = stralloc2("dumper", number);
217         dumper->chunker = &chktable[i];
218         chktable[i].name = stralloc2("chunker", number);
219         chktable[i].dumper = dumper;
220         chktable[i].fd = -1;
221
222         startup_dump_process(dumper, dumper_program);
223         dumper_cmd(dumper, START, NULL, (void *)timestamp);
224     }
225 }
226
227 void
228 startup_chunk_process(
229     chunker_t *chunker,
230     char *chunker_program)
231 {
232     int    fd[2];
233     char **config_options;
234
235     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
236         error(_("%s pipe: %s"), chunker->name, strerror(errno));
237         /*NOTREACHED*/
238     }
239
240     switch(chunker->pid = fork()) {
241     case -1:
242         error(_("fork %s: %s"), chunker->name, strerror(errno));
243         /*NOTREACHED*/
244
245     case 0:             /* child process */
246         aclose(fd[0]);
247         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
248             error(_("%s dup2: %s"), chunker->name, strerror(errno));
249             /*NOTREACHED*/
250         }
251         config_options = get_config_options(2);
252         config_options[0] = chunker->name ? chunker->name : "chunker",
253         config_options[1] = get_config_name();
254         safe_fd(-1, 0);
255         execve(chunker_program, config_options, safe_env());
256         error(_("exec %s (%s): %s"), chunker_program,
257               chunker->name, strerror(errno));
258         /*NOTREACHED*/
259
260     default:    /* parent process */
261         aclose(fd[1]);
262         chunker->down = 0;
263         chunker->fd = fd[0];
264         chunker->ev_read = NULL;
265         g_fprintf(stderr,_("driver: started %s pid %u\n"),
266                 chunker->name, (unsigned)chunker->pid);
267         fflush(stderr);
268     }
269 }
270
271 cmd_t
272 getresult(
273     int fd,
274     int show,
275     int *result_argc,
276     char ***result_argv)
277 {
278     cmd_t t;
279     char *line;
280
281     if((line = areads(fd)) == NULL) {
282         if(errno) {
283             g_fprintf(stderr, _("reading result from %s: %s"), childstr(fd), strerror(errno));
284         }
285         *result_argv = NULL;
286         *result_argc = 0;                               /* EOF */
287     } else {
288         *result_argv = split_quoted_strings(line);
289         *result_argc = g_strv_length(*result_argv);
290     }
291
292     if(show) {
293         g_printf(_("driver: result time %s from %s:"),
294                walltime_str(curclock()),
295                childstr(fd));
296         if(line) {
297             g_printf(" %s", line);
298             putchar('\n');
299         } else {
300             g_printf(" (eof)\n");
301         }
302         fflush(stdout);
303     }
304     amfree(line);
305
306     if(*result_argc < 1) return BOGUS;
307
308     for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
309         if(strcmp((*result_argv)[0], cmdstr[t]) == 0) return t;
310
311     return BOGUS;
312 }
313
314
315 static char *
316 taper_splitting_args(
317         disk_t *dp)
318 {
319     GString *args = NULL;
320     char *q = NULL;
321     dumptype_t *dt = dp->config;
322     tapetype_t *tt;
323
324     tt = lookup_tapetype(getconf_str(CNF_TAPETYPE));
325     g_assert(tt != NULL);
326
327     args = g_string_new("");
328
329     /* old dumptype-based parameters, using empty strings when not seen */
330     if (dt) { /* 'dt' may be NULL for flushes */
331         if (dumptype_seen(dt, DUMPTYPE_TAPE_SPLITSIZE)) {
332             g_string_append_printf(args, "%ju ",
333                         (uintmax_t)dumptype_get_tape_splitsize(dt)*1024);
334         } else {
335             g_string_append(args, "\"\" ");
336         }
337
338         q = quote_string(dumptype_seen(dt, DUMPTYPE_SPLIT_DISKBUFFER)?
339                 dumptype_get_split_diskbuffer(dt) : "");
340         g_string_append_printf(args, "%s ", q);
341         g_free(q);
342
343         if (dumptype_seen(dt, DUMPTYPE_FALLBACK_SPLITSIZE)) {
344             g_string_append_printf(args, "%ju ",
345                         (uintmax_t)dumptype_get_fallback_splitsize(dt)*1024);
346         } else {
347             g_string_append(args, "\"\" ");
348         }
349
350         if (dumptype_seen(dt, DUMPTYPE_ALLOW_SPLIT)) {
351             g_string_append_printf(args, "%d ",
352                         (int)dumptype_get_allow_split(dt));
353         } else {
354             g_string_append(args, "\"\" ");
355         }
356     } else {
357         g_string_append(args, "\"\" \"\" \"\" \"\" ");
358     }
359
360     /* new tapetype-based parameters */
361     if (tapetype_seen(tt, TAPETYPE_PART_SIZE)) {
362         g_string_append_printf(args, "%ju ",
363                     (uintmax_t)tapetype_get_part_size(tt)*1024);
364     } else {
365         g_string_append(args, "\"\" ");
366     }
367
368     q = "";
369     if (tapetype_seen(tt, TAPETYPE_PART_CACHE_TYPE)) {
370         switch (tapetype_get_part_cache_type(tt)) {
371             default:
372             case PART_CACHE_TYPE_NONE:
373                 q = "none";
374                 break;
375
376             case PART_CACHE_TYPE_MEMORY:
377                 q = "memory";
378                 break;
379
380             case PART_CACHE_TYPE_DISK:
381                 q = "disk";
382                 break;
383         }
384     }
385     q = quote_string(q);
386     g_string_append_printf(args, "%s ", q);
387     g_free(q);
388
389     q = quote_string(tapetype_seen(tt, TAPETYPE_PART_CACHE_DIR)?
390             tapetype_get_part_cache_dir(tt) : "");
391     g_string_append_printf(args, "%s ", q);
392     g_free(q);
393
394     if (tapetype_seen(tt, TAPETYPE_PART_CACHE_MAX_SIZE)) {
395         g_string_append_printf(args, "%ju ",
396                     (uintmax_t)tapetype_get_part_cache_max_size(tt)*1024);
397     } else {
398         g_string_append(args, "\"\" ");
399     }
400
401
402     return g_string_free(args, FALSE);
403 }
404
405 int
406 taper_cmd(
407     cmd_t cmd,
408     void *ptr,
409     char *destname,
410     int level,
411     char *datestamp)
412 {
413     char *cmdline = NULL;
414     char number[NUM_STR_SIZE];
415     char orig_kb[NUM_STR_SIZE];
416     char *data_path;
417     disk_t *dp;
418     char *qname;
419     char *qdest;
420     char *q;
421     char *splitargs;
422     uintmax_t origsize;
423
424     switch(cmd) {
425     case START_TAPER:
426         cmdline = vstralloc(cmdstr[cmd],
427                             " ", destname,
428                             " ", datestamp,
429                             "\n", NULL);
430         break;
431     case FILE_WRITE:
432         dp = (disk_t *) ptr;
433         qname = quote_string(dp->name);
434         qdest = quote_string(destname);
435         g_snprintf(number, SIZEOF(number), "%d", level);
436         if (sched(dp)->origsize >= 0)
437             origsize = sched(dp)->origsize;
438         else
439             origsize = 0;
440         g_snprintf(orig_kb, SIZEOF(orig_kb), "%ju", origsize);
441         splitargs = taper_splitting_args(dp);
442         cmdline = vstralloc(cmdstr[cmd],
443                             " ", sched(dp)->taper->name,
444                             " ", disk2serial(dp),
445                             " ", qdest,
446                             " ", dp->host->hostname,
447                             " ", qname,
448                             " ", number,
449                             " ", datestamp,
450                             " ", splitargs,
451                                  orig_kb,
452                             "\n", NULL);
453         amfree(splitargs);
454         amfree(qdest);
455         amfree(qname);
456         break;
457
458     case PORT_WRITE:
459         dp = (disk_t *) ptr;
460         qname = quote_string(dp->name);
461         g_snprintf(number, SIZEOF(number), "%d", level);
462         data_path = data_path_to_string(dp->data_path);
463
464         /*
465           If we haven't been given a place to buffer split dumps to disk,
466           make the argument something besides and empty string so's taper
467           won't get confused
468         */
469         splitargs = taper_splitting_args(dp);
470         cmdline = vstralloc(cmdstr[cmd],
471                             " ", sched(dp)->taper->name,
472                             " ", disk2serial(dp),
473                             " ", dp->host->hostname,
474                             " ", qname,
475                             " ", number,
476                             " ", datestamp,
477                             " ", splitargs,
478                                  data_path,
479                             "\n", NULL);
480         amfree(splitargs);
481         amfree(qname);
482         break;
483     case DONE: /* handle */
484         dp = (disk_t *) ptr;
485         if (sched(dp)->origsize >= 0)
486             origsize = sched(dp)->origsize;
487         else
488             origsize = 0;
489         g_snprintf(number, SIZEOF(number), "%ju", origsize);
490         cmdline = vstralloc(cmdstr[cmd],
491                             " ", sched(dp)->taper->name,
492                             " ", disk2serial(dp),
493                             " ", number,
494                             "\n", NULL);
495         break;
496     case FAILED: /* handle */
497         dp = (disk_t *) ptr;
498         cmdline = vstralloc(cmdstr[cmd],
499                             " ", sched(dp)->taper->name,
500                             " ", disk2serial(dp),
501                             "\n", NULL);
502         break;
503     case NO_NEW_TAPE:
504         dp = (disk_t *) ptr;
505         q = quote_string(destname);     /* reason why no new tape */
506         cmdline = vstralloc(cmdstr[cmd],
507                             " ", sched(dp)->taper->name,
508                             " ", disk2serial(dp),
509                             " ", q,
510                             "\n", NULL);
511         amfree(q);
512         break;
513     case NEW_TAPE:
514         dp = (disk_t *) ptr;
515         cmdline = vstralloc(cmdstr[cmd],
516                             " ", sched(dp)->taper->name,
517                             " ", disk2serial(dp),
518                             "\n", NULL);
519         break;
520     case START_SCAN:
521         dp = (disk_t *) ptr;
522         cmdline = vstralloc(cmdstr[cmd],
523                             " ", sched(dp)->taper->name,
524                             " ", disk2serial(dp),
525                             "\n", NULL);
526         break;
527     case TAKE_SCRIBE_FROM:
528         dp = (disk_t *) ptr;
529         cmdline = vstralloc(cmdstr[cmd],
530                             " ", sched(dp)->taper->name,
531                             " ", disk2serial(dp),
532                             " ", destname,  /* name of worker */
533                             "\n", NULL);
534         break;
535     case QUIT:
536         cmdline = stralloc2(cmdstr[cmd], "\n");
537         break;
538     default:
539         error(_("Don't know how to send %s command to taper"), cmdstr[cmd]);
540         /*NOTREACHED*/
541     }
542
543     /*
544      * Note: cmdline already has a '\n'.
545      */
546     g_printf(_("driver: send-cmd time %s to taper: %s"),
547            walltime_str(curclock()), cmdline);
548     fflush(stdout);
549     if ((full_write(taper_fd, cmdline, strlen(cmdline))) < strlen(cmdline)) {
550         g_printf(_("writing taper command '%s' failed: %s\n"),
551                 cmdline, strerror(errno));
552         fflush(stdout);
553         amfree(cmdline);
554         return 0;
555     }
556     if(cmd == QUIT) aclose(taper_fd);
557     amfree(cmdline);
558     return 1;
559 }
560
561 int
562 dumper_cmd(
563     dumper_t *dumper,
564     cmd_t cmd,
565     disk_t *dp,
566     char   *mesg)
567 {
568     char *cmdline = NULL;
569     char number[NUM_STR_SIZE];
570     char numberport[NUM_STR_SIZE];
571     char *o, *oo;
572     char *device;
573     char *features;
574     char *qname;
575     char *qmesg;
576
577     switch(cmd) {
578     case START:
579         cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
580         break;
581     case PORT_DUMP:
582         if(dp && dp->device) {
583             device = dp->device;
584         }
585         else {
586             device = "NODEVICE";
587         }
588
589         if (dp != NULL) {
590             application_t *application = NULL;
591             char *plugin;
592             char *qplugin;
593             char *qamandad_path;
594             char *qclient_username;
595             char *qclient_port;
596             char *qssh_keys;
597
598             if (dp->application != NULL) {
599                 application = lookup_application(dp->application);
600                 g_assert(application != NULL);
601             }
602
603             device = quote_string((dp->device) ? dp->device : "NODEVICE");
604             qname = quote_string(dp->name);
605             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
606             g_snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
607             features = am_feature_to_string(dp->host->features);
608             if (am_has_feature(dp->host->features, fe_req_xml)) {
609                 o = xml_optionstr(dp, 1);
610                 if (application) {
611                     char *xml_app;
612                     xml_app = xml_application(dp, application,
613                                               dp->host->features);
614                     vstrextend(&o, xml_app, NULL);
615                     amfree(xml_app);
616                 }
617                 oo = quote_string(o);
618                 amfree(o);
619                 o = oo;
620             } else {
621                 o = optionstr(dp);
622             }
623
624             g_assert(dp->program);
625             if (0 == strcmp(dp->program, "APPLICATION")) {
626                 g_assert(application != NULL);
627                 plugin = application_get_plugin(application);
628             } else {
629                 plugin = dp->program;
630             }
631             qplugin = quote_string(plugin);
632             qamandad_path = quote_string(dp->amandad_path);
633             qclient_username = quote_string(dp->client_username);
634             qclient_port = quote_string(dp->client_port);
635             qssh_keys = quote_string(dp->ssh_keys);
636             dbprintf("security_driver %s\n", dp->auth);
637
638             cmdline = vstralloc(cmdstr[cmd],
639                             " ", disk2serial(dp),
640                             " ", numberport,
641                             " ", dp->host->hostname,
642                             " ", features,
643                             " ", qname,
644                             " ", device,
645                             " ", number,
646                             " ", sched(dp)->dumpdate,
647                             " ", qplugin,
648                             " ", qamandad_path,
649                             " ", qclient_username,
650                             " ", qclient_port,
651                             " ", qssh_keys,
652                             " ", dp->auth,
653                             " ", data_path_to_string(dp->data_path),
654                             " ", dp->dataport_list,
655                             " |", o,
656                             "\n", NULL);
657             amfree(qplugin);
658             amfree(qamandad_path);
659             amfree(qclient_username);
660             amfree(qclient_port);
661             amfree(qssh_keys);
662             amfree(features);
663             amfree(o);
664             amfree(qname);
665             amfree(device);
666         } else {
667                 error(_("PORT-DUMP without disk pointer\n"));
668                 /*NOTREACHED*/
669         }
670         break;
671     case QUIT:
672     case ABORT:
673         qmesg = quote_string(mesg);
674         cmdline = vstralloc(cmdstr[cmd], " ", qmesg, "\n", NULL );
675         amfree(qmesg);
676         break;
677     default:
678         error(_("Don't know how to send %s command to dumper"), cmdstr[cmd]);
679         /*NOTREACHED*/
680     }
681
682     /*
683      * Note: cmdline already has a '\n'.
684      */
685     if(dumper->down) {
686         g_printf(_("driver: send-cmd time %s ignored to down dumper %s: %s"),
687                walltime_str(curclock()), dumper->name, cmdline);
688     } else {
689         g_printf(_("driver: send-cmd time %s to %s: %s"),
690                walltime_str(curclock()), dumper->name, cmdline);
691         fflush(stdout);
692         if (full_write(dumper->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
693             g_printf(_("writing %s command: %s\n"), dumper->name, strerror(errno));
694             fflush(stdout);
695             amfree(cmdline);
696             return 0;
697         }
698         if (cmd == QUIT) aclose(dumper->fd);
699     }
700     amfree(cmdline);
701     return 1;
702 }
703
704 int
705 chunker_cmd(
706     chunker_t *chunker,
707     cmd_t cmd,
708     disk_t *dp,
709     char   *mesg)
710 {
711     char *cmdline = NULL;
712     char number[NUM_STR_SIZE];
713     char chunksize[NUM_STR_SIZE];
714     char use[NUM_STR_SIZE];
715     char *o;
716     int activehd=0;
717     assignedhd_t **h=NULL;
718     char *features;
719     char *qname;
720     char *qdest;
721
722     switch(cmd) {
723     case START:
724         cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
725         break;
726     case PORT_WRITE:
727         if(dp && sched(dp) && sched(dp)->holdp) {
728             h = sched(dp)->holdp;
729             activehd = sched(dp)->activehd;
730         }
731
732         if (dp && h) {
733             qname = quote_string(dp->name);
734             qdest = quote_string(sched(dp)->destname);
735             h[activehd]->disk->allocated_dumpers++;
736             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
737             g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
738                     (long long)holdingdisk_get_chunksize(h[0]->disk->hdisk));
739             g_snprintf(use, SIZEOF(use), "%lld",
740                     (long long)h[0]->reserved);
741             features = am_feature_to_string(dp->host->features);
742             o = optionstr(dp);
743             cmdline = vstralloc(cmdstr[cmd],
744                             " ", disk2serial(dp),
745                             " ", qdest,
746                             " ", dp->host->hostname,
747                             " ", features,
748                             " ", qname,
749                             " ", number,
750                             " ", sched(dp)->dumpdate,
751                             " ", chunksize,
752                             " ", dp->program,
753                             " ", use,
754                             " |", o,
755                             "\n", NULL);
756             amfree(features);
757             amfree(o);
758             amfree(qdest);
759             amfree(qname);
760         } else {
761                 error(_("%s command without disk and holding disk.\n"),
762                       cmdstr[cmd]);
763                 /*NOTREACHED*/
764         }
765         break;
766     case CONTINUE:
767         if(dp && sched(dp) && sched(dp)->holdp) {
768             h = sched(dp)->holdp;
769             activehd = sched(dp)->activehd;
770         }
771
772         if(dp && h) {
773             qname = quote_string(dp->name);
774             qdest = quote_string(h[activehd]->destname);
775             h[activehd]->disk->allocated_dumpers++;
776             g_snprintf(chunksize, SIZEOF(chunksize), "%lld", 
777                      (long long)holdingdisk_get_chunksize(h[activehd]->disk->hdisk));
778             g_snprintf(use, SIZEOF(use), "%lld", 
779                      (long long)(h[activehd]->reserved - h[activehd]->used));
780             cmdline = vstralloc(cmdstr[cmd],
781                                 " ", disk2serial(dp),
782                                 " ", qdest,
783                                 " ", chunksize,
784                                 " ", use,
785                                 "\n", NULL );
786             amfree(qdest);
787             amfree(qname);
788         } else {
789             cmdline = stralloc2(cmdstr[cmd], "\n");
790         }
791         break;
792     case QUIT:
793     case ABORT:
794         {
795             char *q = quote_string(mesg);
796             cmdline = vstralloc(cmdstr[cmd], " ", q, "\n", NULL);
797             amfree(q);
798         }
799         break;
800     case DONE:
801     case FAILED:
802         if( dp ) {
803             cmdline = vstralloc(cmdstr[cmd],
804                                 " ", disk2serial(dp),
805                                 "\n",  NULL);
806         } else {
807             cmdline = vstralloc(cmdstr[cmd], "\n");
808         }
809         break;
810     default:
811         error(_("Don't know how to send %s command to chunker"), cmdstr[cmd]);
812         /*NOTREACHED*/
813     }
814
815     /*
816      * Note: cmdline already has a '\n'.
817      */
818     g_printf(_("driver: send-cmd time %s to %s: %s"),
819            walltime_str(curclock()), chunker->name, cmdline);
820     fflush(stdout);
821     if (full_write(chunker->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
822         g_printf(_("writing %s command: %s\n"), chunker->name, strerror(errno));
823         fflush(stdout);
824         amfree(cmdline);
825         return 0;
826     }
827     if (cmd == QUIT) aclose(chunker->fd);
828     amfree(cmdline);
829     return 1;
830 }
831
832 #define MAX_SERIAL MAX_DUMPERS*2        /* one for each dumper and taper */
833
834 long generation = 1;
835
836 struct serial_s {
837     long gen;
838     disk_t *dp;
839 } stable[MAX_SERIAL];
840
841 disk_t *
842 serial2disk(
843     char *str)
844 {
845     int rc, s;
846     long gen;
847
848     rc = sscanf(str, "%d-%ld", &s, &gen);
849     if(rc != 2) {
850         error(_("error [serial2disk \"%s\" parse error]"), str);
851         /*NOTREACHED*/
852     } else if (s < 0 || s >= MAX_SERIAL) {
853         error(_("error [serial out of range 0..%d: %d]"), MAX_SERIAL, s);
854         /*NOTREACHED*/
855     }
856     if(gen != stable[s].gen)
857         g_printf(_("driver: serial2disk error time %s serial gen mismatch %s\n"),
858                walltime_str(curclock()), str);
859     return stable[s].dp;
860 }
861
862 void
863 free_serial(
864     char *str)
865 {
866     int rc, s;
867     long gen;
868
869     rc = sscanf(str, _("%d-%ld"), &s, &gen);
870     if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
871         /* nuke self to get core dump for Brett */
872         g_fprintf(stderr, _("driver: free_serial: str \"%s\" rc %d s %d\n"),
873                 str, rc, s);
874         fflush(stderr);
875         abort();
876     }
877
878     if(gen != stable[s].gen)
879         g_printf(_("driver: free_serial error time %s serial gen mismatch %s\n"),
880                walltime_str(curclock()),str);
881     stable[s].gen = 0;
882     stable[s].dp = NULL;
883 }
884
885
886 void
887 free_serial_dp(
888     disk_t *dp)
889 {
890     int s;
891
892     for(s = 0; s < MAX_SERIAL; s++) {
893         if(stable[s].dp == dp) {
894             stable[s].gen = 0;
895             stable[s].dp = NULL;
896             return;
897         }
898     }
899
900     g_printf(_("driver: error time %s serial not found for disk %s\n"),
901            walltime_str(curclock()), dp->name);
902 }
903
904
905 void
906 check_unfree_serial(void)
907 {
908     int s;
909
910     /* find used serial number */
911     for(s = 0; s < MAX_SERIAL; s++) {
912         if(stable[s].gen != 0 || stable[s].dp != NULL) {
913             g_printf(_("driver: error time %s bug: serial in use: %02d-%05ld\n"),
914                    walltime_str(curclock()), s, stable[s].gen);
915         }
916     }
917 }
918
919 char *disk2serial(
920     disk_t *dp)
921 {
922     int s;
923     static char str[NUM_STR_SIZE];
924
925     for(s = 0; s < MAX_SERIAL; s++) {
926         if(stable[s].dp == dp) {
927             g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
928             return str;
929         }
930     }
931
932     /* find unused serial number */
933     for(s = 0; s < MAX_SERIAL; s++)
934         if(stable[s].gen == 0 && stable[s].dp == NULL)
935             break;
936     if(s >= MAX_SERIAL) {
937         g_printf(_("driver: error time %s bug: out of serial numbers\n"),
938                walltime_str(curclock()));
939         s = 0;
940     }
941
942     stable[s].gen = generation++;
943     stable[s].dp = dp;
944
945     g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
946     return str;
947 }
948
949 void
950 update_info_dumper(
951      disk_t *dp,
952      off_t origsize,
953      off_t dumpsize,
954      time_t dumptime)
955 {
956     int level, i;
957     info_t info;
958     stats_t *infp;
959     perf_t *perfp;
960     char *conf_infofile;
961
962     level = sched(dp)->level;
963
964     conf_infofile = config_dir_relative(getconf_str(CNF_INFOFILE));
965     if (open_infofile(conf_infofile)) {
966         error(_("could not open info db \"%s\""), conf_infofile);
967         /*NOTREACHED*/
968     }
969     amfree(conf_infofile);
970
971     get_info(dp->host->hostname, dp->name, &info);
972
973     /* Clean up information about this and higher-level dumps.  This
974        assumes that update_info_dumper() is always run before
975        update_info_taper(). */
976     for (i = level; i < DUMP_LEVELS; ++i) {
977       infp = &info.inf[i];
978       infp->size = (off_t)-1;
979       infp->csize = (off_t)-1;
980       infp->secs = (time_t)-1;
981       infp->date = (time_t)-1;
982       infp->label[0] = '\0';
983       infp->filenum = 0;
984     }
985
986     /* now store information about this dump */
987     infp = &info.inf[level];
988     infp->size = origsize;
989     infp->csize = dumpsize;
990     infp->secs = dumptime;
991     infp->date = get_time_from_timestamp(sched(dp)->datestamp);
992
993     if(level == 0) perfp = &info.full;
994     else perfp = &info.incr;
995
996     /* Update the stats, but only if the new values are meaningful */
997     if(dp->compress != COMP_NONE && origsize > (off_t)0) {
998         newperf(perfp->comp, (double)dumpsize/(double)origsize);
999     }
1000     if(dumptime > (time_t)0) {
1001         if((off_t)dumptime >= dumpsize)
1002             newperf(perfp->rate, 1);
1003         else
1004             newperf(perfp->rate, (double)dumpsize/(double)dumptime);
1005     }
1006
1007     if(origsize >= (off_t)0 && getconf_int(CNF_RESERVE)<100) {
1008         info.command = NO_COMMAND;
1009     }
1010
1011     if (origsize >= (off_t)0 && level == info.last_level) {
1012         info.consecutive_runs++;
1013     } else if (origsize >= (off_t)0 || level < info.last_level) {
1014         info.last_level = level;
1015         info.consecutive_runs = 1;
1016     }
1017
1018     if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
1019         for(i=NB_HISTORY-1;i>0;i--) {
1020             info.history[i] = info.history[i-1];
1021         }
1022
1023         info.history[0].level = level;
1024         info.history[0].size  = origsize;
1025         info.history[0].csize = dumpsize;
1026         info.history[0].date  = get_time_from_timestamp(sched(dp)->datestamp);
1027         info.history[0].secs  = dumptime;
1028     }
1029
1030     if (put_info(dp->host->hostname, dp->name, &info)) {
1031         int save_errno = errno;
1032         g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
1033                   dp->host->hostname, dp->name, strerror(save_errno));
1034         log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
1035                 dp->host->hostname, dp->name, strerror(save_errno));
1036         error(_("infofile update failed (%s,'%s'): %s\n"),
1037               dp->host->hostname, dp->name, strerror(save_errno));
1038         /*NOTREACHED*/
1039     }
1040
1041     close_infofile();
1042 }
1043
1044 void
1045 update_info_taper(
1046     disk_t *dp,
1047     char *label,
1048     off_t filenum,
1049     int level)
1050 {
1051     info_t info;
1052     stats_t *infp;
1053     int rc;
1054
1055     rc = open_infofile(getconf_str(CNF_INFOFILE));
1056     if(rc) {
1057         error(_("could not open infofile %s: %s (%d)"), getconf_str(CNF_INFOFILE),
1058               strerror(errno), rc);
1059         /*NOTREACHED*/
1060     }
1061
1062     get_info(dp->host->hostname, dp->name, &info);
1063
1064     infp = &info.inf[level];
1065     /* XXX - should we record these two if no-record? */
1066     strncpy(infp->label, label, SIZEOF(infp->label)-1);
1067     infp->label[SIZEOF(infp->label)-1] = '\0';
1068     infp->filenum = filenum;
1069
1070     info.command = NO_COMMAND;
1071
1072     if (put_info(dp->host->hostname, dp->name, &info)) {
1073         int save_errno = errno;
1074         g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
1075                   dp->host->hostname, dp->name, strerror(save_errno));
1076         log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
1077                 dp->host->hostname, dp->name, strerror(save_errno));
1078         error(_("infofile update failed (%s,'%s'): %s\n"),
1079               dp->host->hostname, dp->name, strerror(save_errno));
1080         /*NOTREACHED*/
1081     }
1082     close_infofile();
1083 }
1084
1085 /* Free an array of pointers to assignedhd_t after freeing the
1086  * assignedhd_t themselves. The array must be NULL-terminated.
1087  */
1088 void free_assignedhd(
1089     assignedhd_t **ahd)
1090 {
1091     int i;
1092
1093     if( !ahd ) { return; }
1094
1095     for( i = 0; ahd[i]; i++ ) {
1096         amfree(ahd[i]->destname);
1097         amfree(ahd[i]);
1098     }
1099     amfree(ahd);
1100 }