57782754606b943b9410bf5a930cc72ff9329864
[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 CLOSE_VOLUME:
432         dp = (disk_t *) ptr;
433         cmdline = g_strjoin(NULL, cmdstr[cmd],
434                             " ", sched(dp)->taper->name,
435                             "\n", NULL);
436         break;
437     case FILE_WRITE:
438         dp = (disk_t *) ptr;
439         qname = quote_string(dp->name);
440         qdest = quote_string(destname);
441         g_snprintf(number, SIZEOF(number), "%d", level);
442         if (sched(dp)->origsize >= 0)
443             origsize = sched(dp)->origsize;
444         else
445             origsize = 0;
446         g_snprintf(orig_kb, SIZEOF(orig_kb), "%ju", origsize);
447         splitargs = taper_splitting_args(dp);
448         cmdline = vstralloc(cmdstr[cmd],
449                             " ", sched(dp)->taper->name,
450                             " ", disk2serial(dp),
451                             " ", qdest,
452                             " ", dp->host->hostname,
453                             " ", qname,
454                             " ", number,
455                             " ", datestamp,
456                             " ", splitargs,
457                                  orig_kb,
458                             "\n", NULL);
459         amfree(splitargs);
460         amfree(qdest);
461         amfree(qname);
462         break;
463
464     case PORT_WRITE:
465         dp = (disk_t *) ptr;
466         qname = quote_string(dp->name);
467         g_snprintf(number, SIZEOF(number), "%d", level);
468         data_path = data_path_to_string(dp->data_path);
469
470         /*
471           If we haven't been given a place to buffer split dumps to disk,
472           make the argument something besides and empty string so's taper
473           won't get confused
474         */
475         splitargs = taper_splitting_args(dp);
476         cmdline = vstralloc(cmdstr[cmd],
477                             " ", sched(dp)->taper->name,
478                             " ", disk2serial(dp),
479                             " ", dp->host->hostname,
480                             " ", qname,
481                             " ", number,
482                             " ", datestamp,
483                             " ", splitargs,
484                                  data_path,
485                             "\n", NULL);
486         amfree(splitargs);
487         amfree(qname);
488         break;
489     case DONE: /* handle */
490         dp = (disk_t *) ptr;
491         if (sched(dp)->origsize >= 0)
492             origsize = sched(dp)->origsize;
493         else
494             origsize = 0;
495         g_snprintf(number, SIZEOF(number), "%ju", origsize);
496         cmdline = vstralloc(cmdstr[cmd],
497                             " ", sched(dp)->taper->name,
498                             " ", disk2serial(dp),
499                             " ", number,
500                             "\n", NULL);
501         break;
502     case FAILED: /* handle */
503         dp = (disk_t *) ptr;
504         cmdline = vstralloc(cmdstr[cmd],
505                             " ", sched(dp)->taper->name,
506                             " ", disk2serial(dp),
507                             "\n", NULL);
508         break;
509     case NO_NEW_TAPE:
510         dp = (disk_t *) ptr;
511         q = quote_string(destname);     /* reason why no new tape */
512         cmdline = vstralloc(cmdstr[cmd],
513                             " ", sched(dp)->taper->name,
514                             " ", disk2serial(dp),
515                             " ", q,
516                             "\n", NULL);
517         amfree(q);
518         break;
519     case NEW_TAPE:
520         dp = (disk_t *) ptr;
521         cmdline = vstralloc(cmdstr[cmd],
522                             " ", sched(dp)->taper->name,
523                             " ", disk2serial(dp),
524                             "\n", NULL);
525         break;
526     case START_SCAN:
527         dp = (disk_t *) ptr;
528         cmdline = vstralloc(cmdstr[cmd],
529                             " ", sched(dp)->taper->name,
530                             " ", disk2serial(dp),
531                             "\n", NULL);
532         break;
533     case TAKE_SCRIBE_FROM:
534         dp = (disk_t *) ptr;
535         cmdline = vstralloc(cmdstr[cmd],
536                             " ", sched(dp)->taper->name,
537                             " ", disk2serial(dp),
538                             " ", destname,  /* name of worker */
539                             "\n", NULL);
540         break;
541     case QUIT:
542         cmdline = stralloc2(cmdstr[cmd], "\n");
543         break;
544     default:
545         error(_("Don't know how to send %s command to taper"), cmdstr[cmd]);
546         /*NOTREACHED*/
547     }
548
549     /*
550      * Note: cmdline already has a '\n'.
551      */
552     g_printf(_("driver: send-cmd time %s to taper: %s"),
553            walltime_str(curclock()), cmdline);
554     fflush(stdout);
555     if ((full_write(taper_fd, cmdline, strlen(cmdline))) < strlen(cmdline)) {
556         g_printf(_("writing taper command '%s' failed: %s\n"),
557                 cmdline, strerror(errno));
558         fflush(stdout);
559         amfree(cmdline);
560         return 0;
561     }
562     if(cmd == QUIT) aclose(taper_fd);
563     amfree(cmdline);
564     return 1;
565 }
566
567 int
568 dumper_cmd(
569     dumper_t *dumper,
570     cmd_t cmd,
571     disk_t *dp,
572     char   *mesg)
573 {
574     char *cmdline = NULL;
575     char number[NUM_STR_SIZE];
576     char numberport[NUM_STR_SIZE];
577     char *o, *oo;
578     char *device;
579     char *features;
580     char *qname;
581     char *qmesg;
582
583     switch(cmd) {
584     case START:
585         cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
586         break;
587     case PORT_DUMP:
588         if(dp && dp->device) {
589             device = dp->device;
590         }
591         else {
592             device = "NODEVICE";
593         }
594
595         if (dp != NULL) {
596             application_t *application = NULL;
597             char *plugin;
598             char *qplugin;
599             char *qamandad_path;
600             char *qclient_username;
601             char *qclient_port;
602             char *qssh_keys;
603             char *d_prop;
604
605             if (dp->application != NULL) {
606                 application = lookup_application(dp->application);
607                 g_assert(application != NULL);
608             }
609
610             device = quote_string((dp->device) ? dp->device : "NODEVICE");
611             qname = quote_string(dp->name);
612             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
613             g_snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
614             features = am_feature_to_string(dp->host->features);
615             if (am_has_feature(dp->host->features, fe_req_xml)) {
616                 o = xml_optionstr(dp, 1);
617
618                 d_prop = xml_dumptype_properties(dp);
619                 vstrextend(&o, d_prop, NULL);
620                 amfree(d_prop);
621
622                 if (application) {
623                     char *xml_app;
624                     xml_app = xml_application(dp, application,
625                                               dp->host->features);
626                     vstrextend(&o, xml_app, NULL);
627                     amfree(xml_app);
628                 }
629                 oo = quote_string(o);
630                 amfree(o);
631                 o = oo;
632             } else {
633                 o = optionstr(dp);
634             }
635
636             g_assert(dp->program);
637             if (0 == strcmp(dp->program, "APPLICATION")) {
638                 g_assert(application != NULL);
639                 plugin = application_get_plugin(application);
640             } else {
641                 plugin = dp->program;
642             }
643             qplugin = quote_string(plugin);
644             qamandad_path = quote_string(dp->amandad_path);
645             qclient_username = quote_string(dp->client_username);
646             qclient_port = quote_string(dp->client_port);
647             qssh_keys = quote_string(dp->ssh_keys);
648             dbprintf("security_driver %s\n", dp->auth);
649
650             cmdline = vstralloc(cmdstr[cmd],
651                             " ", disk2serial(dp),
652                             " ", numberport,
653                             " ", dp->host->hostname,
654                             " ", features,
655                             " ", qname,
656                             " ", device,
657                             " ", number,
658                             " ", sched(dp)->dumpdate,
659                             " ", qplugin,
660                             " ", qamandad_path,
661                             " ", qclient_username,
662                             " ", qclient_port,
663                             " ", qssh_keys,
664                             " ", dp->auth,
665                             " ", data_path_to_string(dp->data_path),
666                             " ", dp->dataport_list,
667                             " |", o,
668                             "\n", NULL);
669             amfree(qplugin);
670             amfree(qamandad_path);
671             amfree(qclient_username);
672             amfree(qclient_port);
673             amfree(qssh_keys);
674             amfree(features);
675             amfree(o);
676             amfree(qname);
677             amfree(device);
678         } else {
679                 error(_("PORT-DUMP without disk pointer\n"));
680                 /*NOTREACHED*/
681         }
682         break;
683     case QUIT:
684     case ABORT:
685         qmesg = quote_string(mesg);
686         cmdline = vstralloc(cmdstr[cmd], " ", qmesg, "\n", NULL );
687         amfree(qmesg);
688         break;
689     default:
690         error(_("Don't know how to send %s command to dumper"), cmdstr[cmd]);
691         /*NOTREACHED*/
692     }
693
694     /*
695      * Note: cmdline already has a '\n'.
696      */
697     if(dumper->down) {
698         g_printf(_("driver: send-cmd time %s ignored to down dumper %s: %s"),
699                walltime_str(curclock()), dumper->name, cmdline);
700     } else {
701         g_printf(_("driver: send-cmd time %s to %s: %s"),
702                walltime_str(curclock()), dumper->name, cmdline);
703         fflush(stdout);
704         if (full_write(dumper->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
705             g_printf(_("writing %s command: %s\n"), dumper->name, strerror(errno));
706             fflush(stdout);
707             amfree(cmdline);
708             return 0;
709         }
710         if (cmd == QUIT) aclose(dumper->fd);
711     }
712     amfree(cmdline);
713     return 1;
714 }
715
716 int
717 chunker_cmd(
718     chunker_t *chunker,
719     cmd_t cmd,
720     disk_t *dp,
721     char   *mesg)
722 {
723     char *cmdline = NULL;
724     char number[NUM_STR_SIZE];
725     char chunksize[NUM_STR_SIZE];
726     char use[NUM_STR_SIZE];
727     char *o;
728     int activehd=0;
729     assignedhd_t **h=NULL;
730     char *features;
731     char *qname;
732     char *qdest;
733
734     switch(cmd) {
735     case START:
736         cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
737         break;
738     case PORT_WRITE:
739         if(dp && sched(dp) && sched(dp)->holdp) {
740             h = sched(dp)->holdp;
741             activehd = sched(dp)->activehd;
742         }
743
744         if (dp && h) {
745             qname = quote_string(dp->name);
746             qdest = quote_string(sched(dp)->destname);
747             h[activehd]->disk->allocated_dumpers++;
748             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
749             g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
750                     (long long)holdingdisk_get_chunksize(h[0]->disk->hdisk));
751             g_snprintf(use, SIZEOF(use), "%lld",
752                     (long long)h[0]->reserved);
753             features = am_feature_to_string(dp->host->features);
754             o = optionstr(dp);
755             cmdline = vstralloc(cmdstr[cmd],
756                             " ", disk2serial(dp),
757                             " ", qdest,
758                             " ", dp->host->hostname,
759                             " ", features,
760                             " ", qname,
761                             " ", number,
762                             " ", sched(dp)->dumpdate,
763                             " ", chunksize,
764                             " ", dp->program,
765                             " ", use,
766                             " |", o,
767                             "\n", NULL);
768             amfree(features);
769             amfree(o);
770             amfree(qdest);
771             amfree(qname);
772         } else {
773                 error(_("%s command without disk and holding disk.\n"),
774                       cmdstr[cmd]);
775                 /*NOTREACHED*/
776         }
777         break;
778     case CONTINUE:
779         if(dp && sched(dp) && sched(dp)->holdp) {
780             h = sched(dp)->holdp;
781             activehd = sched(dp)->activehd;
782         }
783
784         if(dp && h) {
785             qname = quote_string(dp->name);
786             qdest = quote_string(h[activehd]->destname);
787             h[activehd]->disk->allocated_dumpers++;
788             g_snprintf(chunksize, SIZEOF(chunksize), "%lld", 
789                      (long long)holdingdisk_get_chunksize(h[activehd]->disk->hdisk));
790             g_snprintf(use, SIZEOF(use), "%lld", 
791                      (long long)(h[activehd]->reserved - h[activehd]->used));
792             cmdline = vstralloc(cmdstr[cmd],
793                                 " ", disk2serial(dp),
794                                 " ", qdest,
795                                 " ", chunksize,
796                                 " ", use,
797                                 "\n", NULL );
798             amfree(qdest);
799             amfree(qname);
800         } else {
801             cmdline = stralloc2(cmdstr[cmd], "\n");
802         }
803         break;
804     case QUIT:
805     case ABORT:
806         {
807             char *q = quote_string(mesg);
808             cmdline = vstralloc(cmdstr[cmd], " ", q, "\n", NULL);
809             amfree(q);
810         }
811         break;
812     case DONE:
813     case FAILED:
814         if( dp ) {
815             cmdline = vstralloc(cmdstr[cmd],
816                                 " ", disk2serial(dp),
817                                 "\n",  NULL);
818         } else {
819             cmdline = vstralloc(cmdstr[cmd], "\n", NULL);
820         }
821         break;
822     default:
823         error(_("Don't know how to send %s command to chunker"), cmdstr[cmd]);
824         /*NOTREACHED*/
825     }
826
827     /*
828      * Note: cmdline already has a '\n'.
829      */
830     g_printf(_("driver: send-cmd time %s to %s: %s"),
831            walltime_str(curclock()), chunker->name, cmdline);
832     fflush(stdout);
833     if (full_write(chunker->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
834         g_printf(_("writing %s command: %s\n"), chunker->name, strerror(errno));
835         fflush(stdout);
836         amfree(cmdline);
837         return 0;
838     }
839     if (cmd == QUIT) aclose(chunker->fd);
840     amfree(cmdline);
841     return 1;
842 }
843
844 #define MAX_SERIAL MAX_DUMPERS*2        /* one for each dumper and taper */
845
846 long generation = 1;
847
848 struct serial_s {
849     long gen;
850     disk_t *dp;
851 } stable[MAX_SERIAL];
852
853 disk_t *
854 serial2disk(
855     char *str)
856 {
857     int rc, s;
858     long gen;
859
860     rc = sscanf(str, "%d-%ld", &s, &gen);
861     if(rc != 2) {
862         error(_("error [serial2disk \"%s\" parse error]"), str);
863         /*NOTREACHED*/
864     } else if (s < 0 || s >= MAX_SERIAL) {
865         error(_("error [serial out of range 0..%d: %d]"), MAX_SERIAL, s);
866         /*NOTREACHED*/
867     }
868     if(gen != stable[s].gen)
869         g_printf(_("driver: serial2disk error time %s serial gen mismatch %s\n"),
870                walltime_str(curclock()), str);
871     return stable[s].dp;
872 }
873
874 void
875 free_serial(
876     char *str)
877 {
878     int rc, s;
879     long gen;
880
881     rc = sscanf(str, _("%d-%ld"), &s, &gen);
882     if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
883         /* nuke self to get core dump for Brett */
884         g_fprintf(stderr, _("driver: free_serial: str \"%s\" rc %d s %d\n"),
885                 str, rc, s);
886         fflush(stderr);
887         abort();
888     }
889
890     if(gen != stable[s].gen)
891         g_printf(_("driver: free_serial error time %s serial gen mismatch %s\n"),
892                walltime_str(curclock()),str);
893     stable[s].gen = 0;
894     stable[s].dp = NULL;
895 }
896
897
898 void
899 free_serial_dp(
900     disk_t *dp)
901 {
902     int s;
903
904     for(s = 0; s < MAX_SERIAL; s++) {
905         if(stable[s].dp == dp) {
906             stable[s].gen = 0;
907             stable[s].dp = NULL;
908             return;
909         }
910     }
911
912     g_printf(_("driver: error time %s serial not found for disk %s\n"),
913            walltime_str(curclock()), dp->name);
914 }
915
916
917 void
918 check_unfree_serial(void)
919 {
920     int s;
921
922     /* find used serial number */
923     for(s = 0; s < MAX_SERIAL; s++) {
924         if(stable[s].gen != 0 || stable[s].dp != NULL) {
925             g_printf(_("driver: error time %s bug: serial in use: %02d-%05ld\n"),
926                    walltime_str(curclock()), s, stable[s].gen);
927         }
928     }
929 }
930
931 char *disk2serial(
932     disk_t *dp)
933 {
934     int s;
935     static char str[NUM_STR_SIZE];
936
937     for(s = 0; s < MAX_SERIAL; s++) {
938         if(stable[s].dp == dp) {
939             g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
940             return str;
941         }
942     }
943
944     /* find unused serial number */
945     for(s = 0; s < MAX_SERIAL; s++)
946         if(stable[s].gen == 0 && stable[s].dp == NULL)
947             break;
948     if(s >= MAX_SERIAL) {
949         g_printf(_("driver: error time %s bug: out of serial numbers\n"),
950                walltime_str(curclock()));
951         s = 0;
952     }
953
954     stable[s].gen = generation++;
955     stable[s].dp = dp;
956
957     g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
958     return str;
959 }
960
961 void
962 update_info_dumper(
963      disk_t *dp,
964      off_t origsize,
965      off_t dumpsize,
966      time_t dumptime)
967 {
968     int level, i;
969     info_t info;
970     stats_t *infp;
971     perf_t *perfp;
972     char *conf_infofile;
973
974     level = sched(dp)->level;
975
976     conf_infofile = config_dir_relative(getconf_str(CNF_INFOFILE));
977     if (open_infofile(conf_infofile)) {
978         error(_("could not open info db \"%s\""), conf_infofile);
979         /*NOTREACHED*/
980     }
981     amfree(conf_infofile);
982
983     get_info(dp->host->hostname, dp->name, &info);
984
985     /* Clean up information about this and higher-level dumps.  This
986        assumes that update_info_dumper() is always run before
987        update_info_taper(). */
988     for (i = level; i < DUMP_LEVELS; ++i) {
989       infp = &info.inf[i];
990       infp->size = (off_t)-1;
991       infp->csize = (off_t)-1;
992       infp->secs = (time_t)-1;
993       infp->date = (time_t)-1;
994       infp->label[0] = '\0';
995       infp->filenum = 0;
996     }
997
998     /* now store information about this dump */
999     infp = &info.inf[level];
1000     infp->size = origsize;
1001     infp->csize = dumpsize;
1002     infp->secs = dumptime;
1003     if (sched(dp)->timestamp == 0) {
1004         infp->date = 0;
1005     } else {
1006         infp->date = get_time_from_timestamp(sched(dp)->datestamp);
1007     }
1008
1009     if(level == 0) perfp = &info.full;
1010     else perfp = &info.incr;
1011
1012     /* Update the stats, but only if the new values are meaningful */
1013     if(dp->compress != COMP_NONE && origsize > (off_t)0) {
1014         newperf(perfp->comp, (double)dumpsize/(double)origsize);
1015     }
1016     if(dumptime > (time_t)0) {
1017         if((off_t)dumptime >= dumpsize)
1018             newperf(perfp->rate, 1);
1019         else
1020             newperf(perfp->rate, (double)dumpsize/(double)dumptime);
1021     }
1022
1023     if(origsize >= (off_t)0 && getconf_int(CNF_RESERVE)<100) {
1024         info.command = NO_COMMAND;
1025     }
1026
1027     if (origsize >= (off_t)0 && level == info.last_level) {
1028         info.consecutive_runs++;
1029     } else if (origsize >= (off_t)0) {
1030         info.last_level = level;
1031         info.consecutive_runs = 1;
1032     }
1033
1034     if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
1035         for(i=NB_HISTORY-1;i>0;i--) {
1036             info.history[i] = info.history[i-1];
1037         }
1038
1039         info.history[0].level = level;
1040         info.history[0].size  = origsize;
1041         info.history[0].csize = dumpsize;
1042         if (sched(dp)->timestamp == 0) {
1043             info.history[0].date = 0;
1044         } else {
1045             info.history[0].date = get_time_from_timestamp(sched(dp)->datestamp);
1046         }
1047         info.history[0].secs  = dumptime;
1048     }
1049
1050     if (put_info(dp->host->hostname, dp->name, &info)) {
1051         int save_errno = errno;
1052         g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
1053                   dp->host->hostname, dp->name, strerror(save_errno));
1054         log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
1055                 dp->host->hostname, dp->name, strerror(save_errno));
1056         error(_("infofile update failed (%s,'%s'): %s\n"),
1057               dp->host->hostname, dp->name, strerror(save_errno));
1058         /*NOTREACHED*/
1059     }
1060
1061     close_infofile();
1062 }
1063
1064 void
1065 update_info_taper(
1066     disk_t *dp,
1067     char *label,
1068     off_t filenum,
1069     int level)
1070 {
1071     info_t info;
1072     stats_t *infp;
1073     int rc;
1074
1075     rc = open_infofile(getconf_str(CNF_INFOFILE));
1076     if(rc) {
1077         error(_("could not open infofile %s: %s (%d)"), getconf_str(CNF_INFOFILE),
1078               strerror(errno), rc);
1079         /*NOTREACHED*/
1080     }
1081
1082     get_info(dp->host->hostname, dp->name, &info);
1083
1084     infp = &info.inf[level];
1085     /* XXX - should we record these two if no-record? */
1086     strncpy(infp->label, label, SIZEOF(infp->label)-1);
1087     infp->label[SIZEOF(infp->label)-1] = '\0';
1088     infp->filenum = filenum;
1089
1090     info.command = NO_COMMAND;
1091
1092     if (put_info(dp->host->hostname, dp->name, &info)) {
1093         int save_errno = errno;
1094         g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
1095                   dp->host->hostname, dp->name, strerror(save_errno));
1096         log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
1097                 dp->host->hostname, dp->name, strerror(save_errno));
1098         error(_("infofile update failed (%s,'%s'): %s\n"),
1099               dp->host->hostname, dp->name, strerror(save_errno));
1100         /*NOTREACHED*/
1101     }
1102     close_infofile();
1103 }
1104
1105 /* Free an array of pointers to assignedhd_t after freeing the
1106  * assignedhd_t themselves. The array must be NULL-terminated.
1107  */
1108 void free_assignedhd(
1109     assignedhd_t **ahd)
1110 {
1111     int i;
1112
1113     if( !ahd ) { return; }
1114
1115     for( i = 0; ahd[i]; i++ ) {
1116         amfree(ahd[i]->destname);
1117         amfree(ahd[i]);
1118     }
1119     amfree(ahd);
1120 }