source: MondoRescue/trunk/mondo/mondo/common/libmondo-fork.c@ 130

Last change on this file since 130 was 130, checked in by bcornec, 18 years ago

memory management revision on libmondo-fork.c

  • Property svn:keywords set to Id
File size: 29.2 KB
Line 
1/* $Id: libmondo-fork.c 130 2005-11-22 16:47:53Z bcornec $
2 subroutines for handling forking/pthreads/etc.
3*/
4
5
6#include "my-stuff.h"
7#include "mondostructures.h"
8#include "libmondo-fork.h"
9#include "libmondo-string-EXT.h"
10#include "libmondo-gui-EXT.h"
11#include "libmondo-files-EXT.h"
12#include "libmondo-tools-EXT.h"
13#include "lib-common-externs.h"
14
15/*@unused@*/
16//static char cvsid[] = "$Id: libmondo-fork.c 130 2005-11-22 16:47:53Z bcornec $";
17
18extern char *g_tmpfs_mountpt;
19extern t_bkptype g_backup_media_type;
20extern bool g_text_mode;
21pid_t g_buffer_pid = 0;
22
23
24/**
25 * Call a program and retrieve its last line of output.
26 * @param call The program to run.
27 * @return The last line of its output.
28 * @note The returned value points to static storage that will be overwritten with each call.
29 */
30char *call_program_and_get_last_line_of_output(char *call)
31{
32 /*@ buffers ***************************************************** */
33 static char *result = NULL;
34 char *tmp = NULL;
35
36 /*@ pointers **************************************************** */
37 FILE *fin;
38
39 int n = 0;
40
41 /*@******************************************************************** */
42
43 assert_string_is_neither_NULL_nor_zerolength(call);
44 if ((fin = popen(call, "r"))) {
45 for (getline(&tmp, &n, fin); !feof(fin);
46 getline(&tmp, &n, fin)) {
47 if (strlen(tmp) > 1) {
48 if (result != NULL) {
49 paranoid_free(result);
50 }
51 asprintf(&result, tmp);
52 }
53 }
54 paranoid_pclose(fin);
55 } else {
56 log_OS_error("Unable to popen call");
57 }
58 strip_spaces(result);
59 paranoid_free(tmp);
60 return (result);
61}
62
63#define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
64
65/**
66 * Call mkisofs to create an ISO image.
67 * @param bkpinfo The backup information structure. Fields used:
68 * - @c bkpinfo->manual_cd_tray
69 * - @c bkpinfo->backup_media_type
70 * - @c bkpinfo->please_dont_eject_when_restoring
71 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
72 * - @c _ISO_ will become the ISO file (@p isofile)
73 * - @c _CD#_ becomes the CD number (@p cd_no)
74 * - @c _ERR_ becomes the logfile (@p g_logfile)
75 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
76 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
77 * @param logstub Unused.
78 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
79 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
80 * @bug @p logstub is unused.
81 */
82int
83eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
84 char *basic_call, char *isofile,
85 int cd_no, char *logstub, char *what_i_am_doing)
86{
87
88 /*@ int's *** */
89 int retval = 0;
90
91
92 /*@ buffers *** */
93 char *midway_call, *ultimate_call, *tmp, *command,
94 *cd_number_str;
95 char *p;
96
97/*@*********** End Variables ***************************************/
98
99 log_msg(3, "Starting");
100 assert(bkpinfo != NULL);
101 assert_string_is_neither_NULL_nor_zerolength(basic_call);
102 assert_string_is_neither_NULL_nor_zerolength(isofile);
103 assert_string_is_neither_NULL_nor_zerolength(logstub);
104 if (!(midway_call = malloc(1200))) {
105 fatal_error("Cannot malloc midway_call");
106 }
107 if (!(ultimate_call = malloc(1200))) {
108 fatal_error("Cannot malloc ultimate_call");
109 }
110 if (!(tmp = malloc(1200))) {
111 fatal_error("Cannot malloc tmp");
112 }
113
114 asprintf(&cd_number_str, "%d", cd_no);
115 resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
116 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
117 paranoid_free(cd_number_str);
118
119 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
120 log_msg(4, "basic call = '%s'", basic_call);
121 log_msg(4, "midway_call = '%s'", midway_call);
122 log_msg(4, "tmp = '%s'", tmp);
123 log_msg(4, "ultimate call = '%s'", ultimate_call);
124 asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
125
126 log_to_screen
127 ("Please be patient. Do not be alarmed by on-screen inactivity.");
128 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
129 what_i_am_doing);
130 strcpy(tmp, command);
131 if (bkpinfo->manual_cd_tray) {
132 p = strstr(tmp, "2>>");
133 if (p) {
134 sprintf(p, " ");
135 while (*p == ' ') {
136 p++;
137 }
138 for (; *p != ' '; p++) {
139 *p = ' ';
140 }
141 }
142 paranoid_free(command);
143 asprintf(&command, tmp);
144#ifndef _XWIN
145 if (!g_text_mode) {
146 newtSuspend();
147 }
148#endif
149 log_msg(1, "command = '%s'", command);
150 retval += system(command);
151 if (!g_text_mode) {
152 newtResume();
153 }
154 if (retval) {
155 log_msg(2, "Basic call '%s' returned an error.", basic_call);
156 popup_and_OK("Press ENTER to continue.");
157 popup_and_OK
158 ("mkisofs and/or cdrecord returned an error. CD was not created");
159 }
160 }
161 /* if text mode then do the above & RETURN; if not text mode, do this... */
162 else {
163 log_msg(3, "command = '%s'", command);
164// yes_this_is_a_goto:
165 retval =
166 run_external_binary_with_percentage_indicator_NEW
167 (what_i_am_doing, command);
168 }
169 paranoid_free(command);
170
171 paranoid_free(midway_call);
172 paranoid_free(ultimate_call);
173 paranoid_free(tmp);
174 return (retval);
175}
176
177
178/**
179 * Run a program and log its output (stdout and stderr) to the logfile.
180 * @param program The program to run. Passed to the shell, so you can use pipes etc.
181 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
182 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
183 */
184int run_program_and_log_output(char *program, int debug_level)
185{
186 /*@ buffer ****************************************************** */
187 char *callstr;
188 char *incoming = NULL;
189 char tmp[MAX_STR_LEN * 2];
190
191 /*@ int ********************************************************* */
192 int res;
193 int i;
194 int n = 0;
195 int len;
196 bool log_if_failure = FALSE;
197 bool log_if_success = FALSE;
198
199 /*@ pointers *************************************************** */
200 FILE *fin;
201 char *p;
202
203 /*@ end vars *************************************************** */
204
205 assert(program != NULL);
206 if (!program[0]) {
207 log_msg(2, "Warning - asked to run zerolength program");
208 return (1);
209 }
210
211 if (debug_level <= g_loglevel) {
212 log_if_success = TRUE;
213 log_if_failure = TRUE;
214 }
215 asprintf(&callstr,
216 "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
217 program);
218 while ((p = strchr(callstr, '\r'))) {
219 *p = ' ';
220 }
221 while ((p = strchr(callstr, '\n'))) {
222 *p = ' ';
223 } /* single '=' is intentional */
224
225
226 len = (int) strlen(program);
227 for (i = 0; i < 35 - len / 2; i++) {
228 tmp[i] = '-';
229 }
230 tmp[i] = '\0';
231 strcat(tmp, " ");
232 strcat(tmp, program);
233 strcat(tmp, " ");
234 for (i = 0; i < 35 - len / 2; i++) {
235 strcat(tmp, "-");
236 }
237 res = system(callstr);
238 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
239 log_msg(0, "running: %s", callstr);
240 log_msg(0,
241 "--------------------------------start of output-----------------------------");
242 }
243 paranoid_free(callstr);
244
245 if (log_if_failure
246 &&
247 system
248 ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
249 {
250 log_OS_error("Command failed");
251 }
252 unlink("/tmp/mondo-run-prog-thing.err");
253 fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
254 if (fin) {
255 for (getline(&incoming, &n, fin); !feof(fin);
256 getline(&incoming, &n, fin)) {
257 /* patch by Heiko Schlittermann */
258 p = incoming;
259 while (p && *p) {
260 if ((p = strchr(p, '%'))) {
261 memmove(p, p + 1, strlen(p) + 1);
262 p += 2;
263 }
264 }
265 /* end of patch */
266 strip_spaces(incoming);
267 if ((res == 0 && log_if_success)
268 || (res != 0 && log_if_failure)) {
269 log_msg(0, incoming);
270 }
271 }
272 paranoid_free(incoming);
273 paranoid_fclose(fin);
274 }
275 unlink("/tmp/mondo-run-prog-thing.tmp");
276 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
277 log_msg(0,
278 "--------------------------------end of output------------------------------");
279 if (res) {
280 log_msg(0, "...ran with res=%d", res);
281 } else {
282 log_msg(0, "...ran just fine. :-)");
283 }
284 }
285// else
286// { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
287 return (res);
288}
289
290
291/**
292 * Run a program and log its output to the screen.
293 * @param basic_call The program to run.
294 * @param what_i_am_doing The title of the evalcall form.
295 * @return The return value of the command (varies, but 0 almost always means success).
296 * @see run_program_and_log_output
297 * @see log_to_screen
298 */
299int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
300{
301 /*@ int ******************************************************** */
302 int retval = 0;
303 int res = 0;
304 int n = 0;
305 int i;
306
307 /*@ pointers **************************************************** */
308 FILE *fin;
309
310 /*@ buffers **************************************************** */
311 char *tmp;
312 char *command;
313 char *lockfile;
314
315 /*@ end vars *************************************************** */
316
317 assert_string_is_neither_NULL_nor_zerolength(basic_call);
318
319 asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
320 mkstemp(lockfile);
321 asprintf(&command,
322 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
323 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
324 open_evalcall_form(what_i_am_doing);
325 asprintf(&tmp, "Executing %s", basic_call);
326 log_msg(2, tmp);
327 paranoid_free(tmp);
328
329 if (!(fin = popen(command, "r"))) {
330 log_OS_error("Unable to popen-in command");
331 asprintf(&tmp, "Failed utterly to call '%s'", command);
332 log_to_screen(tmp);
333 paranoid_free(tmp);
334 paranoid_free(lockfile);
335 paranoid_free(command);
336 return (1);
337 }
338 paranoid_free(command);
339
340 if (!does_file_exist(lockfile)) {
341 log_to_screen("Waiting for external binary to start");
342 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
343 log_msg(3, "Waiting for lockfile %s to exist", lockfile);
344 }
345 }
346#ifdef _XWIN
347 /* This only can update when newline goes into the file,
348 but it's *much* prettier/faster on Qt. */
349 tmp = NULL;
350 while (does_file_exist(lockfile)) {
351 while (!feof(fin)) {
352 if (!getline(&tmp, &n, fin))
353 break;
354 log_to_screen(tmp);
355 }
356 usleep(500000);
357 }
358 paranoid_free(tmp);
359#else
360 /* This works on Newt, and it gives quicker updates. */
361 for (; does_file_exist(lockfile); sleep(1)) {
362 log_file_end_to_screen(MONDO_LOGFILE, "");
363 update_evalcall_form(1);
364 }
365#endif
366 /* Evaluate the status returned by pclose to get the exit code of the called program. */
367 errno = 0;
368 res = pclose(fin);
369 /* Log actual pclose errors. */
370 if (errno)
371 log_msg(5, "pclose err: %d", errno);
372 /* Check if we have a valid status. If we do, extract the called program's exit code. */
373 /* If we don't, highlight this fact by returning -1. */
374 if (WIFEXITED(res)) {
375 retval = WEXITSTATUS(res);
376 } else {
377 retval = -1;
378 }
379 close_evalcall_form();
380 unlink(lockfile);
381 paranoid_free(lockfile);
382
383 return (retval);
384}
385
386
387/**
388 * Thread callback to run a command (partimage) in the background.
389 * @param xfb A transfer block of @c char, containing:
390 * - xfb:[0] A marker, should be set to 2. Decremented to 1 while the command is running and 0 when it's finished.
391 * - xfb:[1] The command's return value, if xfb:[0] is 0.
392 * - xfb+2: A <tt>NULL</tt>-terminated string containing the command to be run.
393 * @return NULL to pthread_join.
394 */
395void *call_partimage_in_bkgd(void *xfb)
396{
397 char *transfer_block;
398 int retval = 0;
399
400 g_buffer_pid = getpid();
401 unlink("/tmp/null");
402 log_msg(1, "starting");
403 transfer_block = (char *) xfb;
404 transfer_block[0]--; // should now be 1
405 retval = system(transfer_block + 2);
406 if (retval) {
407 log_OS_error("partimage returned an error");
408 }
409 transfer_block[1] = retval;
410 transfer_block[0]--; // should now be 0
411 g_buffer_pid = 0;
412 log_msg(1, "returning");
413 pthread_exit(NULL);
414}
415
416
417/**
418 * File to touch if we want partimage to wait for us.
419 */
420#define PAUSE_PARTIMAGE_FNAME "/tmp/PAUSE-PARTIMAGE-FOR-MONDO"
421
422/**
423 * Apparently used. @bug This has a purpose, but what?
424 */
425#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
426#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
427
428/**
429 * Marker to start the next subvolume for Partimage.
430 */
431#define NEXT_SUBVOL_PLEASE "I-grew-up-on-the-crime-side,-the-New-York-Times-side,-where-staying-alive-was-no-jive"
432
433/**
434 * Marker to end the partimage file.
435 */
436#define NO_MORE_SUBVOLS "On-second-hand,momma-bounced-on-old-man,-and-so-we-moved-to-Shaolin-Land."
437
438int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
439{
440// if dir=='w' then copy from orig to archived
441// if dir=='r' then copy from archived to orig
442 char *tmp;
443 char *buf;
444 long int bytes_to_be_read, bytes_read_in, bytes_written_out =
445 0, bufcap, subsliceno = 0;
446 int retval = 0;
447 FILE *fin;
448 FILE *fout;
449 FILE *ftmp;
450
451 log_msg(5, "Opening.");
452 bufcap = 256L * 1024L;
453 if (!(buf = malloc(bufcap))) {
454 fatal_error("Failed to malloc() buf");
455 }
456
457 if (direction == 'w') {
458 fin = f_orig;
459 fout = f_archived;
460 asprintf(&tmp, "%-64s", PIMP_START_SZ);
461 if (fwrite(tmp, 1, 64, fout) != 64) {
462 fatal_error("Can't write the introductory block");
463 }
464 paranoid_free(tmp);
465
466 while (1) {
467 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
468 if (bytes_read_in == 0) {
469 break;
470 }
471 asprintf(&tmp, "%-64ld", bytes_read_in);
472 if (fwrite(tmp, 1, 64, fout) != 64) {
473 fatal_error("Cannot write introductory block");
474 }
475 paranoid_free(tmp);
476
477 log_msg(7,
478 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
479 subsliceno, bytes_read_in, bytes_to_be_read);
480 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
481 asprintf(&tmp, "%-64ld", subsliceno);
482 if (fwrite(tmp, 1, 64, fout) != 64) {
483 fatal_error("Cannot write post-thingy block");
484 }
485 paranoid_free(tmp);
486
487 log_msg(7, "Subslice #%d written OK", subsliceno);
488 subsliceno++;
489 }
490 asprintf(&tmp, "%-64ld", 0L);
491 if (fwrite(tmp, 1, 64L, fout) != 64L) {
492 fatal_error("Cannot write final introductory block");
493 }
494 } else {
495 fin = f_archived;
496 fout = f_orig;
497 if (!(tmp = malloc(64L))) {
498 fatal_error("Failed to malloc() tmp");
499 }
500 if (fread(tmp, 1, 64L, fin) != 64L) {
501 fatal_error("Cannot read the introductory block");
502 }
503 log_msg(5, "tmp is %s", tmp);
504 if (!strstr(tmp, PIMP_START_SZ)) {
505 fatal_error("Can't find intro blk");
506 }
507 if (fread(tmp, 1, 64L, fin) != 64L) {
508 fatal_error("Cannot read introductory blk");
509 }
510 bytes_to_be_read = atol(tmp);
511 while (bytes_to_be_read > 0) {
512 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
513 bytes_to_be_read);
514 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
515 if (bytes_read_in != bytes_to_be_read) {
516 fatal_error
517 ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
518 }
519 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
520 if (fread(tmp, 1, 64, fin) != 64) {
521 fatal_error("Cannot read post-thingy block");
522 }
523 if (atol(tmp) != subsliceno) {
524 log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
525 subsliceno, atol(tmp), tmp);
526 }
527 log_msg(7, "Subslice #%ld read OK", subsliceno);
528 subsliceno++;
529 if (fread(tmp, 1, 64, fin) != 64) {
530 fatal_error("Cannot read introductory block");
531 }
532 bytes_to_be_read = atol(tmp);
533 }
534 }
535
536// log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
537
538 if (direction == 'w') {
539 asprintf(&tmp, "%-64s", PIMP_END_SZ);
540 if (fwrite(tmp, 1, 64, fout) != 64) {
541 fatal_error("Can't write the final block");
542 }
543 paranoid_free(tmp);
544 } else {
545 log_msg(1, "tmpA is %s", tmp);
546 if (!strstr(tmp, PIMP_END_SZ)) {
547 if (fread(tmp, 1, 64, fin) != 64) {
548 fatal_error("Can't read the final block");
549 }
550 log_msg(5, "tmpB is %s", tmp);
551 if (!strstr(tmp, PIMP_END_SZ)) {
552 ftmp = fopen("/tmp/out.leftover", "w");
553 bytes_read_in = fread(tmp, 1, 64, fin);
554 log_msg(1, "bytes_read_in = %ld", bytes_read_in);
555// if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
556 fwrite(tmp, 1, bytes_read_in, ftmp);
557 paranoid_free(tmp);
558
559 if (!(tmp = malloc(512))) {
560 fatal_error("Failed to malloc() tmp");
561 }
562 fread(tmp, 1, 512, fin);
563 log_msg(0, "tmp = '%s'", tmp);
564 fwrite(tmp, 1, 512, ftmp);
565 fclose(ftmp);
566 fatal_error("Missing terminating block");
567 }
568 }
569 paranoid_free(tmp);
570 }
571
572 paranoid_free(buf);
573 log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
574 return (retval);
575}
576
577
578/**
579 * Call partimage from @p input_device to @p output_fname.
580 * @param input_device The device to read.
581 * @param output_fname The file to write.
582 * @return 0 for success, nonzero for failure.
583 */
584int dynamically_create_pipes_and_copy_from_them_to_output_file(char
585 *input_device, char
586 *output_fname)
587{
588 char *curr_fifo;
589 char *prev_fifo = NULL;
590 char *next_fifo;
591 char *command;
592 char *sz_call_to_partimage;
593 int fifo_number = 0;
594 struct stat buf;
595 pthread_t partimage_thread;
596 int res = 0;
597 char *tmpstub;
598 FILE *fout;
599 FILE *fin;
600 char *tmp;
601
602 log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt);
603 if (g_tmpfs_mountpt && g_tmpfs_mountpt[0]
604 && does_file_exist(g_tmpfs_mountpt)) {
605 asprintf(&tmpstub, g_tmpfs_mountpt);
606 } else {
607 asprintf(&tmpstub, "/tmp");
608 }
609 paranoid_system("rm -f /tmp/*PARTIMAGE*");
610 asprintf(&command, "rm -Rf %s/pih-fifo-*", tmpstub);
611 paranoid_system(command);
612 paranoid_free(command);
613
614 asprintf(&tmp, "%s/pih-fifo-%ld", tmpstub, (long int) random());
615 paranoid_free(tmpstub);
616 tmpstub = tmp;
617 paranoid_free(tmp);
618
619 mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though...
620 asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
621 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
622 mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
623 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
624 asprintf(&sz_call_to_partimage,
625 "%c%cpartimagehack " PARTIMAGE_PARAMS
626 " save %s %s > /tmp/stdout 2> /tmp/stderr", 2, 0, input_device,
627 tmpstub);
628 log_msg(5, "curr_fifo = %s", curr_fifo);
629 log_msg(5, "next_fifo = %s", next_fifo);
630 log_msg(5, "sz_call_to_partimage call is '%s'",
631 sz_call_to_partimage + 2);
632 if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) {
633 log_msg(5, "Deleting %s", output_fname);
634 unlink(output_fname);
635 }
636 if (!(fout = fopen(output_fname, "w"))) {
637 fatal_error("Unable to openout to output_fname");
638 }
639 res =
640 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
641 (void *) sz_call_to_partimage);
642 if (res) {
643 fatal_error("Failed to create thread to call partimage");
644 }
645 log_msg(1, "Running fore/back at same time");
646 log_to_screen("Working with partimagehack...");
647 while (sz_call_to_partimage[0] > 0) {
648 asprintf(&tmp, "%s\n", NEXT_SUBVOL_PLEASE);
649 if (fwrite(tmp, 1, 128, fout) != 128) {
650 fatal_error("Cannot write interim block");
651 }
652 paranoid_free(tmp);
653
654 log_msg(5, "fifo_number=%d", fifo_number);
655 log_msg(4, "Cat'ting %s", curr_fifo);
656 if (!(fin = fopen(curr_fifo, "r"))) {
657 fatal_error("Unable to openin from fifo");
658 }
659 if (prev_fifo != NULL) {
660 log_msg(5, "Deleting %s", prev_fifo);
661 unlink(prev_fifo); // just in case
662 paranoid_free(prev_fifo);
663 }
664 copy_from_src_to_dest(fin, fout, 'w');
665 paranoid_fclose(fin);
666 fifo_number++;
667
668 prev_fifo = curr_fifo;
669 curr_fifo = next_fifo;
670 log_msg(5, "Creating %s", next_fifo);
671 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
672 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo exists before we cat this one
673 system("sync");
674 sleep(5);
675 }
676 asprintf(&tmp, "%s\n", NO_MORE_SUBVOLS);
677 if (fwrite(tmp, 1, 128, fout) != 128) {
678 fatal_error("Cannot write interim block");
679 }
680 if (fwrite(tmp, 1, 128, fout) != 128) {
681 fatal_error("Cannot write interim block");
682 }
683 if (fwrite(tmp, 1, 128, fout) != 128) {
684 fatal_error("Cannot write interim block");
685 }
686 if (fwrite(tmp, 1, 128, fout) != 128) {
687 fatal_error("Cannot write interim block");
688 }
689 paranoid_free(tmp);
690 paranoid_fclose(fout);
691 log_to_screen("Cleaning up after partimagehack...");
692 log_msg(3, "Final fifo_number=%d", fifo_number);
693 paranoid_system("sync");
694 unlink(next_fifo);
695 paranoid_free(next_fifo);
696
697 unlink(curr_fifo);
698 paranoid_free(curr_fifo);
699
700 unlink(prev_fifo);
701 paranoid_free(prev_fifo);
702
703 log_to_screen("Finished cleaning up.");
704
705// if (!lstat(sz_wait_for_this_file, &statbuf))
706// { log_msg(3, "WARNING! %s was not processed.", sz_wait_for_this_file); }
707 log_msg(2, "Waiting for pthread_join() to join.");
708 pthread_join(partimage_thread, NULL);
709 res = sz_call_to_partimage[1];
710 paranoid_free(sz_call_to_partimage);
711 log_msg(2, "pthread_join() joined OK.");
712 log_msg(1, "Partimagehack(save) returned %d", res);
713 unlink(tmpstub);
714 paranoid_free(tmpstub);
715
716 return (res);
717}
718
719
720/**
721 * Feed @p input_device through partimage to @p output_fname.
722 * @param input_device The device to image.
723 * @param output_fname The file to write.
724 * @return 0 for success, nonzero for failure.
725 */
726int feed_into_partimage(char *input_device, char *output_fname)
727{
728// BACKUP
729 int res;
730
731 if (!does_file_exist(input_device)) {
732 fatal_error("input device does not exist");
733 }
734 if (!find_home_of_exe("partimagehack")) {
735 fatal_error("partimagehack not found");
736 }
737 res =
738 dynamically_create_pipes_and_copy_from_them_to_output_file
739 (input_device, output_fname);
740 return (res);
741}
742
743
744int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
745{
746
747 /*@ int *************************************************************** */
748 int res = 0;
749 int percentage = 0;
750 int maxpc = 0;
751 int pcno = 0;
752 int last_pcno = 0;
753
754 /*@ buffers *********************************************************** */
755 char *command;
756 char *tempfile;
757 /*@ pointers ********************************************************** */
758 FILE *pin;
759
760 assert_string_is_neither_NULL_nor_zerolength(cmd);
761
762 asprintf(&tempfile,
763 call_program_and_get_last_line_of_output
764 ("mktemp -q /tmp/mondo.XXXXXXXX"));
765 asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
766 tempfile);
767 log_msg(3, command);
768 open_evalcall_form(tt);
769
770 if (!(pin = popen(command, "r"))) {
771 log_OS_error("fmt err");
772 paranoid_free(command);
773 paranoid_free(tempfile);
774 return (1);
775 }
776 paranoid_free(command);
777
778 maxpc = 100;
779// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
780 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
781 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
782 if (pcno < 0 || pcno > 100) {
783 log_msg(5, "Weird pc#");
784 continue;
785 }
786 percentage = pcno * 100 / maxpc;
787 if (pcno <= 5 && last_pcno > 40) {
788 close_evalcall_form();
789 open_evalcall_form("Verifying...");
790 }
791 last_pcno = pcno;
792 update_evalcall_form(percentage);
793 }
794// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
795 close_evalcall_form();
796 if (pclose(pin)) {
797 res++;
798 log_OS_error("Unable to pclose");
799 }
800 unlink(tempfile);
801 paranoid_free(tempfile);
802 return (res);
803}
804
805
806void *run_prog_in_bkgd_then_exit(void *info)
807{
808 char *sz_command;
809 static int res = 4444;
810
811 res = 999;
812 sz_command = (char *) info;
813 log_msg(4, "sz_command = '%s'", sz_command);
814 res = system(sz_command);
815 if (res > 256 && res != 4444) {
816 res = res / 256;
817 }
818 log_msg(4, "child res = %d", res);
819 sz_command[0] = '\0';
820 pthread_exit((void *) (&res));
821}
822
823
824int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
825{
826
827 /*@ int *************************************************************** */
828 int res = 0;
829 int percentage = 0;
830 int maxpc = 100;
831 int pcno = 0;
832 int last_pcno = 0;
833 int counter = 0;
834
835 /*@ buffers *********************************************************** */
836 char *command;
837 /*@ pointers ********************************************************** */
838 static int chldres = 0;
839 int *pchild_result;
840 pthread_t childthread;
841
842 pchild_result = &chldres;
843 assert_string_is_neither_NULL_nor_zerolength(cmd);
844 assert_string_is_neither_NULL_nor_zerolength(tt);
845 *pchild_result = 999;
846
847 asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
848 log_msg(3, "command = '%s'", command);
849 if ((res =
850 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
851 (void *) command))) {
852 fatal_error("Unable to create an archival thread");
853 }
854
855 log_msg(8, "Parent running");
856 open_evalcall_form(tt);
857 for (sleep(1); command[0] != '\0'; sleep(1)) {
858 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
859 if (pcno <= 0 || pcno > 100) {
860 log_msg(8, "Weird pc#");
861 continue;
862 }
863 percentage = pcno * 100 / maxpc;
864 if (pcno <= 5 && last_pcno >= 40) {
865 close_evalcall_form();
866 open_evalcall_form("Verifying...");
867 }
868 if (counter++ >= 5) {
869 counter = 0;
870 log_file_end_to_screen(MONDO_LOGFILE, "");
871 }
872 last_pcno = pcno;
873 update_evalcall_form(percentage);
874 }
875 paranoid_free(command);
876
877 log_file_end_to_screen(MONDO_LOGFILE, "");
878 close_evalcall_form();
879 pthread_join(childthread, (void *) (&pchild_result));
880 if (pchild_result) {
881 res = *pchild_result;
882 } else {
883 res = 666;
884 }
885 log_msg(3, "Parent res = %d", res);
886 return (res);
887}
888
889#define PIH_LOG "/var/log/partimage-debug.log"
890
891/**
892 * Feed @p input_fifo through partimage (restore) to @p output_device.
893 * @param input_fifo The partimage file to read.
894 * @param output_device Where to put the output.
895 * @return The return value of partimagehack (0 for success).
896 * @bug Probably unnecessary, as the partimage is just a sparse file. We could use @c dd to restore it.
897 */
898int feed_outfrom_partimage(char *output_device, char *input_fifo)
899{
900// RESTORE
901 char *tmp;
902 char *stuff;
903 char *sz_call_to_partimage;
904 pthread_t partimage_thread;
905 int res;
906 char *curr_fifo;
907 char *prev_fifo;
908 char *oldest_fifo = NULL;
909 char *next_fifo;
910 char *afternxt_fifo;
911 int fifo_number = 0;
912 char *tmpstub;
913 FILE *fin;
914 FILE *fout;
915
916 log_msg(1, "output_device=%s", output_device);
917 log_msg(1, "input_fifo=%s", input_fifo);
918 asprintf(&tmpstub, "/tmp");
919
920 log_msg(1, "tmpstub was %s", tmpstub);
921 asprintf(&stuff, tmpstub);
922 paranoid_free(tmpstub);
923
924 asprintf(&tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random());
925 paranoid_free(stuff);
926
927 log_msg(1, "tmpstub is now %s", tmpstub);
928 unlink("/tmp/PARTIMAGEHACK-POSITION");
929 unlink(PAUSE_PARTIMAGE_FNAME);
930 paranoid_system("rm -f /tmp/*PARTIMAGE*");
931 asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
932 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
933 asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
934 mkfifo(PIH_LOG, S_IRWXU | S_IRWXG);
935 mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
936 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
937 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG);
938 system("cat " PIH_LOG " > /dev/null &");
939 log_msg(3, "curr_fifo = %s", curr_fifo);
940 log_msg(3, "next_fifo = %s", next_fifo);
941 if (!does_file_exist(input_fifo)) {
942 fatal_error("input fifo does not exist");
943 }
944 if (!(fin = fopen(input_fifo, "r"))) {
945 fatal_error("Unable to openin from input_fifo");
946 }
947 if (!find_home_of_exe("partimagehack")) {
948 fatal_error("partimagehack not found");
949 }
950 asprintf(&sz_call_to_partimage,
951 "%c%cpartimagehack " PARTIMAGE_PARAMS
952 " restore %s %s > /dev/null 2>> %s", 2, 0, output_device, curr_fifo,
953 MONDO_LOGFILE);
954 log_msg(1, "output_device = %s", output_device);
955 log_msg(1, "curr_fifo = %s", curr_fifo);
956 log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage + 2);
957 res =
958 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
959 (void *) sz_call_to_partimage);
960 if (res) {
961 fatal_error("Failed to create thread to call partimage");
962 }
963 log_msg(1, "Running fore/back at same time");
964 log_msg(2, " Trying to openin %s", input_fifo);
965 if (!does_file_exist(input_fifo)) {
966 log_msg(2, "Warning - %s does not exist", input_fifo);
967 }
968 while (!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) {
969 log_msg(6, "Waiting for partimagehack (restore) to start");
970 sleep(1);
971 }
972
973 if (!(tmp = malloc(128))) {
974 fatal_error("Failed to malloc() tmp");
975 }
976 while (sz_call_to_partimage[0] > 0) {
977 if (fread(tmp, 1, 128, fin) != 128) {
978 fatal_error("Cannot read introductory block");
979 }
980 if (strstr(tmp, NEXT_SUBVOL_PLEASE)) {
981 log_msg(2, "Great. Next subvol coming up.");
982 } else if (strstr(tmp, NO_MORE_SUBVOLS)) {
983 log_msg(2, "Great. That was the last subvol.");
984 break;
985 } else {
986 log_msg(2, "WTF is this? '%s'", tmp);
987 fatal_error("Unknown interim block");
988 }
989 if (feof(fin)) {
990 log_msg(1, "Eof(fin) detected. Breaking.");
991 break;
992 }
993 log_msg(3, "Processing subvol %d", fifo_number);
994 log_msg(5, "fifo_number=%d", fifo_number);
995 if (!(fout = fopen(curr_fifo, "w"))) {
996 fatal_error("Cannot openout to curr_fifo");
997 }
998 copy_from_src_to_dest(fout, fin, 'r');
999 paranoid_fclose(fout);
1000 fifo_number++;
1001 if (oldest_fifo != NULL) {
1002 log_msg(6, "Deleting %s", oldest_fifo);
1003 unlink(oldest_fifo); // just in case
1004 paranoid_free(oldest_fifo);
1005 }
1006 oldest_fifo = prev_fifo;
1007 prev_fifo = curr_fifo;
1008 curr_fifo = next_fifo;
1009 next_fifo = afternxt_fifo;
1010 asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
1011 log_msg(6, "Creating %s", afternxt_fifo);
1012 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we access current fifo
1013 fflush(fin);
1014// system("sync");
1015 usleep(1000L * 100L);
1016 }
1017 paranoid_free(tmp);
1018 paranoid_free(tmpstub);
1019
1020 paranoid_fclose(fin);
1021 paranoid_system("sync");
1022 log_msg(1, "Partimagehack has finished. Great. Fin-closing.");
1023 log_msg(1, "Waiting for pthread_join");
1024 pthread_join(partimage_thread, NULL);
1025 res = sz_call_to_partimage[1];
1026 paranoid_free(sz_call_to_partimage);
1027
1028 log_msg(1, "Yay. Partimagehack (restore) returned %d", res);
1029 unlink(prev_fifo);
1030 paranoid_free(prev_fifo);
1031
1032 unlink(curr_fifo);
1033 paranoid_free(curr_fifo);
1034
1035 unlink(next_fifo);
1036 paranoid_free(next_fifo);
1037
1038 unlink(afternxt_fifo);
1039 paranoid_free(afternxt_fifo);
1040
1041 unlink(PIH_LOG);
1042 /* BERLIOS : pas de unlink(oldest_fifo) ??? */
1043 paranoid_free(oldest_fifo);
1044
1045 return (res);
1046}
Note: See TracBrowser for help on using the repository browser.