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