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