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

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

memory management continues:

  • mondoarchive handled completely
  • bkpinfo, begining of dyn. alloc.
  • lot of changes around memory everywhere

=> even if it compiles, i'm pretty sure it doesn't work yet (even not tried)

  • Property svn:keywords set to Id
File size: 28.9 KB
Line 
1/* $Id: libmondo-fork.c 171 2005-12-08 16:20:29Z 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 171 2005-12-08 16:20:29Z 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 size_t 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 size_t 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 i;
305
306 /*@ pointers **************************************************** */
307 FILE *fin;
308
309 /*@ buffers **************************************************** */
310 char *tmp;
311 char *command;
312 char *lockfile;
313
314 /*@ end vars *************************************************** */
315
316 assert_string_is_neither_NULL_nor_zerolength(basic_call);
317
318 asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
319 mkstemp(lockfile);
320 asprintf(&command,
321 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
322 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
323 open_evalcall_form(what_i_am_doing);
324 asprintf(&tmp, "Executing %s", basic_call);
325 log_msg(2, tmp);
326 paranoid_free(tmp);
327
328 if (!(fin = popen(command, "r"))) {
329 log_OS_error("Unable to popen-in command");
330 asprintf(&tmp, "Failed utterly to call '%s'", command);
331 log_to_screen(tmp);
332 paranoid_free(tmp);
333 paranoid_free(lockfile);
334 paranoid_free(command);
335 return (1);
336 }
337 paranoid_free(command);
338
339 if (!does_file_exist(lockfile)) {
340 log_to_screen("Waiting for external binary to start");
341 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
342 log_msg(3, "Waiting for lockfile %s to exist", lockfile);
343 }
344 }
345 /* This works on Newt, and it gives quicker updates. */
346 for (; does_file_exist(lockfile); sleep(1)) {
347 log_file_end_to_screen(MONDO_LOGFILE, "");
348 update_evalcall_form(1);
349 }
350 /* Evaluate the status returned by pclose to get the exit code of the called program. */
351 errno = 0;
352 res = pclose(fin);
353 /* Log actual pclose errors. */
354 if (errno)
355 log_msg(5, "pclose err: %d", errno);
356 /* Check if we have a valid status. If we do, extract the called program's exit code. */
357 /* If we don't, highlight this fact by returning -1. */
358 if (WIFEXITED(res)) {
359 retval = WEXITSTATUS(res);
360 } else {
361 retval = -1;
362 }
363 close_evalcall_form();
364 unlink(lockfile);
365 paranoid_free(lockfile);
366
367 return (retval);
368}
369
370
371/**
372 * Thread callback to run a command (partimage) in the background.
373 * @param xfb A transfer block of @c char, containing:
374 * - xfb:[0] A marker, should be set to 2. Decremented to 1 while the command is running and 0 when it's finished.
375 * - xfb:[1] The command's return value, if xfb:[0] is 0.
376 * - xfb+2: A <tt>NULL</tt>-terminated string containing the command to be run.
377 * @return NULL to pthread_join.
378 */
379void *call_partimage_in_bkgd(void *xfb)
380{
381 char *transfer_block;
382 int retval = 0;
383
384 g_buffer_pid = getpid();
385 unlink("/tmp/null");
386 log_msg(1, "starting");
387 transfer_block = (char *) xfb;
388 transfer_block[0]--; // should now be 1
389 retval = system(transfer_block + 2);
390 if (retval) {
391 log_OS_error("partimage returned an error");
392 }
393 transfer_block[1] = retval;
394 transfer_block[0]--; // should now be 0
395 g_buffer_pid = 0;
396 log_msg(1, "returning");
397 pthread_exit(NULL);
398}
399
400
401/**
402 * File to touch if we want partimage to wait for us.
403 */
404#define PAUSE_PARTIMAGE_FNAME "/tmp/PAUSE-PARTIMAGE-FOR-MONDO"
405
406/**
407 * Apparently used. @bug This has a purpose, but what?
408 */
409#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
410#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
411
412/**
413 * Marker to start the next subvolume for Partimage.
414 */
415#define NEXT_SUBVOL_PLEASE "I-grew-up-on-the-crime-side,-the-New-York-Times-side,-where-staying-alive-was-no-jive"
416
417/**
418 * Marker to end the partimage file.
419 */
420#define NO_MORE_SUBVOLS "On-second-hand,momma-bounced-on-old-man,-and-so-we-moved-to-Shaolin-Land."
421
422int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
423{
424// if dir=='w' then copy from orig to archived
425// if dir=='r' then copy from archived to orig
426 char *tmp;
427 char *buf;
428 long int bytes_to_be_read, bytes_read_in, bytes_written_out =
429 0, bufcap, subsliceno = 0;
430 int retval = 0;
431 FILE *fin;
432 FILE *fout;
433 FILE *ftmp;
434
435 log_msg(5, "Opening.");
436 bufcap = 256L * 1024L;
437 if (!(buf = malloc(bufcap))) {
438 fatal_error("Failed to malloc() buf");
439 }
440
441 if (direction == 'w') {
442 fin = f_orig;
443 fout = f_archived;
444 asprintf(&tmp, "%-64s", PIMP_START_SZ);
445 if (fwrite(tmp, 1, 64, fout) != 64) {
446 fatal_error("Can't write the introductory block");
447 }
448 paranoid_free(tmp);
449
450 while (1) {
451 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
452 if (bytes_read_in == 0) {
453 break;
454 }
455 asprintf(&tmp, "%-64ld", bytes_read_in);
456 if (fwrite(tmp, 1, 64, fout) != 64) {
457 fatal_error("Cannot write introductory block");
458 }
459 paranoid_free(tmp);
460
461 log_msg(7,
462 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
463 subsliceno, bytes_read_in, bytes_to_be_read);
464 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
465 asprintf(&tmp, "%-64ld", subsliceno);
466 if (fwrite(tmp, 1, 64, fout) != 64) {
467 fatal_error("Cannot write post-thingy block");
468 }
469 paranoid_free(tmp);
470
471 log_msg(7, "Subslice #%d written OK", subsliceno);
472 subsliceno++;
473 }
474 asprintf(&tmp, "%-64ld", 0L);
475 if (fwrite(tmp, 1, 64L, fout) != 64L) {
476 fatal_error("Cannot write final introductory block");
477 }
478 } else {
479 fin = f_archived;
480 fout = f_orig;
481 if (!(tmp = malloc(64L))) {
482 fatal_error("Failed to malloc() tmp");
483 }
484 if (fread(tmp, 1, 64L, fin) != 64L) {
485 fatal_error("Cannot read the introductory block");
486 }
487 log_msg(5, "tmp is %s", tmp);
488 if (!strstr(tmp, PIMP_START_SZ)) {
489 fatal_error("Can't find intro blk");
490 }
491 if (fread(tmp, 1, 64L, fin) != 64L) {
492 fatal_error("Cannot read introductory blk");
493 }
494 bytes_to_be_read = atol(tmp);
495 while (bytes_to_be_read > 0) {
496 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
497 bytes_to_be_read);
498 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
499 if (bytes_read_in != bytes_to_be_read) {
500 fatal_error
501 ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
502 }
503 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
504 if (fread(tmp, 1, 64, fin) != 64) {
505 fatal_error("Cannot read post-thingy block");
506 }
507 if (atol(tmp) != subsliceno) {
508 log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
509 subsliceno, atol(tmp), tmp);
510 }
511 log_msg(7, "Subslice #%ld read OK", subsliceno);
512 subsliceno++;
513 if (fread(tmp, 1, 64, fin) != 64) {
514 fatal_error("Cannot read introductory block");
515 }
516 bytes_to_be_read = atol(tmp);
517 }
518 }
519
520// log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
521
522 if (direction == 'w') {
523 asprintf(&tmp, "%-64s", PIMP_END_SZ);
524 if (fwrite(tmp, 1, 64, fout) != 64) {
525 fatal_error("Can't write the final block");
526 }
527 paranoid_free(tmp);
528 } else {
529 log_msg(1, "tmpA is %s", tmp);
530 if (!strstr(tmp, PIMP_END_SZ)) {
531 if (fread(tmp, 1, 64, fin) != 64) {
532 fatal_error("Can't read the final block");
533 }
534 log_msg(5, "tmpB is %s", tmp);
535 if (!strstr(tmp, PIMP_END_SZ)) {
536 ftmp = fopen("/tmp/out.leftover", "w");
537 bytes_read_in = fread(tmp, 1, 64, fin);
538 log_msg(1, "bytes_read_in = %ld", bytes_read_in);
539// if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
540 fwrite(tmp, 1, bytes_read_in, ftmp);
541 paranoid_free(tmp);
542
543 if (!(tmp = malloc(512))) {
544 fatal_error("Failed to malloc() tmp");
545 }
546 fread(tmp, 1, 512, fin);
547 log_msg(0, "tmp = '%s'", tmp);
548 fwrite(tmp, 1, 512, ftmp);
549 fclose(ftmp);
550 fatal_error("Missing terminating block");
551 }
552 }
553 paranoid_free(tmp);
554 }
555
556 paranoid_free(buf);
557 log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
558 return (retval);
559}
560
561
562/**
563 * Call partimage from @p input_device to @p output_fname.
564 * @param input_device The device to read.
565 * @param output_fname The file to write.
566 * @return 0 for success, nonzero for failure.
567 */
568int dynamically_create_pipes_and_copy_from_them_to_output_file(char
569 *input_device, char
570 *output_fname)
571{
572 char *curr_fifo;
573 char *prev_fifo = NULL;
574 char *next_fifo;
575 char *command;
576 char *sz_call_to_partimage;
577 int fifo_number = 0;
578 struct stat buf;
579 pthread_t partimage_thread;
580 int res = 0;
581 char *tmpstub;
582 FILE *fout;
583 FILE *fin;
584 char *tmp;
585
586 log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt);
587 if (g_tmpfs_mountpt && g_tmpfs_mountpt[0]
588 && does_file_exist(g_tmpfs_mountpt)) {
589 asprintf(&tmpstub, g_tmpfs_mountpt);
590 } else {
591 asprintf(&tmpstub, "/tmp");
592 }
593 paranoid_system("rm -f /tmp/*PARTIMAGE*");
594 asprintf(&command, "rm -Rf %s/pih-fifo-*", tmpstub);
595 paranoid_system(command);
596 paranoid_free(command);
597
598 asprintf(&tmp, "%s/pih-fifo-%ld", tmpstub, (long int) random());
599 paranoid_free(tmpstub);
600 tmpstub = tmp;
601 paranoid_free(tmp);
602
603 mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though...
604 asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
605 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
606 mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
607 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
608 asprintf(&sz_call_to_partimage,
609 "%c%cpartimagehack " PARTIMAGE_PARAMS
610 " save %s %s > /tmp/stdout 2> /tmp/stderr", 2, 0, input_device,
611 tmpstub);
612 log_msg(5, "curr_fifo = %s", curr_fifo);
613 log_msg(5, "next_fifo = %s", next_fifo);
614 log_msg(5, "sz_call_to_partimage call is '%s'",
615 sz_call_to_partimage + 2);
616 if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) {
617 log_msg(5, "Deleting %s", output_fname);
618 unlink(output_fname);
619 }
620 if (!(fout = fopen(output_fname, "w"))) {
621 fatal_error("Unable to openout to output_fname");
622 }
623 res =
624 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
625 (void *) sz_call_to_partimage);
626 if (res) {
627 fatal_error("Failed to create thread to call partimage");
628 }
629 log_msg(1, "Running fore/back at same time");
630 log_to_screen("Working with partimagehack...");
631 while (sz_call_to_partimage[0] > 0) {
632 asprintf(&tmp, "%s\n", NEXT_SUBVOL_PLEASE);
633 if (fwrite(tmp, 1, 128, fout) != 128) {
634 fatal_error("Cannot write interim block");
635 }
636 paranoid_free(tmp);
637
638 log_msg(5, "fifo_number=%d", fifo_number);
639 log_msg(4, "Cat'ting %s", curr_fifo);
640 if (!(fin = fopen(curr_fifo, "r"))) {
641 fatal_error("Unable to openin from fifo");
642 }
643 if (prev_fifo != NULL) {
644 log_msg(5, "Deleting %s", prev_fifo);
645 unlink(prev_fifo); // just in case
646 paranoid_free(prev_fifo);
647 }
648 copy_from_src_to_dest(fin, fout, 'w');
649 paranoid_fclose(fin);
650 fifo_number++;
651
652 prev_fifo = curr_fifo;
653 curr_fifo = next_fifo;
654 log_msg(5, "Creating %s", next_fifo);
655 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
656 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo exists before we cat this one
657 system("sync");
658 sleep(5);
659 }
660 asprintf(&tmp, "%s\n", NO_MORE_SUBVOLS);
661 if (fwrite(tmp, 1, 128, fout) != 128) {
662 fatal_error("Cannot write interim block");
663 }
664 if (fwrite(tmp, 1, 128, fout) != 128) {
665 fatal_error("Cannot write interim block");
666 }
667 if (fwrite(tmp, 1, 128, fout) != 128) {
668 fatal_error("Cannot write interim block");
669 }
670 if (fwrite(tmp, 1, 128, fout) != 128) {
671 fatal_error("Cannot write interim block");
672 }
673 paranoid_free(tmp);
674 paranoid_fclose(fout);
675 log_to_screen("Cleaning up after partimagehack...");
676 log_msg(3, "Final fifo_number=%d", fifo_number);
677 paranoid_system("sync");
678 unlink(next_fifo);
679 paranoid_free(next_fifo);
680
681 unlink(curr_fifo);
682 paranoid_free(curr_fifo);
683
684 unlink(prev_fifo);
685 paranoid_free(prev_fifo);
686
687 log_to_screen("Finished cleaning up.");
688
689// if (!lstat(sz_wait_for_this_file, &statbuf))
690// { log_msg(3, "WARNING! %s was not processed.", sz_wait_for_this_file); }
691 log_msg(2, "Waiting for pthread_join() to join.");
692 pthread_join(partimage_thread, NULL);
693 res = sz_call_to_partimage[1];
694 paranoid_free(sz_call_to_partimage);
695 log_msg(2, "pthread_join() joined OK.");
696 log_msg(1, "Partimagehack(save) returned %d", res);
697 unlink(tmpstub);
698 paranoid_free(tmpstub);
699
700 return (res);
701}
702
703
704/**
705 * Feed @p input_device through partimage to @p output_fname.
706 * @param input_device The device to image.
707 * @param output_fname The file to write.
708 * @return 0 for success, nonzero for failure.
709 */
710int feed_into_partimage(char *input_device, char *output_fname)
711{
712// BACKUP
713 int res;
714
715 if (!does_file_exist(input_device)) {
716 fatal_error("input device does not exist");
717 }
718 if (!find_home_of_exe("partimagehack")) {
719 fatal_error("partimagehack not found");
720 }
721 res =
722 dynamically_create_pipes_and_copy_from_them_to_output_file
723 (input_device, output_fname);
724 return (res);
725}
726
727
728int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
729{
730
731 /*@ int *************************************************************** */
732 int res = 0;
733 int percentage = 0;
734 int maxpc = 0;
735 int pcno = 0;
736 int last_pcno = 0;
737
738 /*@ buffers *********************************************************** */
739 char *command;
740 char *tempfile;
741 /*@ pointers ********************************************************** */
742 FILE *pin;
743
744 assert_string_is_neither_NULL_nor_zerolength(cmd);
745
746 asprintf(&tempfile,
747 call_program_and_get_last_line_of_output
748 ("mktemp -q /tmp/mondo.XXXXXXXX"));
749 asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
750 tempfile);
751 log_msg(3, command);
752 open_evalcall_form(tt);
753
754 if (!(pin = popen(command, "r"))) {
755 log_OS_error("fmt err");
756 paranoid_free(command);
757 paranoid_free(tempfile);
758 return (1);
759 }
760 paranoid_free(command);
761
762 maxpc = 100;
763// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
764 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
765 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
766 if (pcno < 0 || pcno > 100) {
767 log_msg(5, "Weird pc#");
768 continue;
769 }
770 percentage = pcno * 100 / maxpc;
771 if (pcno <= 5 && last_pcno > 40) {
772 close_evalcall_form();
773 open_evalcall_form("Verifying...");
774 }
775 last_pcno = pcno;
776 update_evalcall_form(percentage);
777 }
778// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
779 close_evalcall_form();
780 if (pclose(pin)) {
781 res++;
782 log_OS_error("Unable to pclose");
783 }
784 unlink(tempfile);
785 paranoid_free(tempfile);
786 return (res);
787}
788
789
790void *run_prog_in_bkgd_then_exit(void *info)
791{
792 char *sz_command;
793 static int res = 4444;
794
795 res = 999;
796 sz_command = (char *) info;
797 log_msg(4, "sz_command = '%s'", sz_command);
798 res = system(sz_command);
799 if (res > 256 && res != 4444) {
800 res = res / 256;
801 }
802 log_msg(4, "child res = %d", res);
803 sz_command[0] = '\0';
804 pthread_exit((void *) (&res));
805}
806
807
808int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
809{
810
811 /*@ int *************************************************************** */
812 int res = 0;
813 int percentage = 0;
814 int maxpc = 100;
815 int pcno = 0;
816 int last_pcno = 0;
817 int counter = 0;
818
819 /*@ buffers *********************************************************** */
820 char *command;
821 /*@ pointers ********************************************************** */
822 static int chldres = 0;
823 int *pchild_result;
824 pthread_t childthread;
825
826 pchild_result = &chldres;
827 assert_string_is_neither_NULL_nor_zerolength(cmd);
828 assert_string_is_neither_NULL_nor_zerolength(tt);
829 *pchild_result = 999;
830
831 asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
832 log_msg(3, "command = '%s'", command);
833 if ((res =
834 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
835 (void *) command))) {
836 fatal_error("Unable to create an archival thread");
837 }
838
839 log_msg(8, "Parent running");
840 open_evalcall_form(tt);
841 for (sleep(1); command[0] != '\0'; sleep(1)) {
842 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
843 if (pcno <= 0 || pcno > 100) {
844 log_msg(8, "Weird pc#");
845 continue;
846 }
847 percentage = pcno * 100 / maxpc;
848 if (pcno <= 5 && last_pcno >= 40) {
849 close_evalcall_form();
850 open_evalcall_form("Verifying...");
851 }
852 if (counter++ >= 5) {
853 counter = 0;
854 log_file_end_to_screen(MONDO_LOGFILE, "");
855 }
856 last_pcno = pcno;
857 update_evalcall_form(percentage);
858 }
859 paranoid_free(command);
860
861 log_file_end_to_screen(MONDO_LOGFILE, "");
862 close_evalcall_form();
863 pthread_join(childthread, (void *) (&pchild_result));
864 if (pchild_result) {
865 res = *pchild_result;
866 } else {
867 res = 666;
868 }
869 log_msg(3, "Parent res = %d", res);
870 return (res);
871}
872
873#define PIH_LOG "/var/log/partimage-debug.log"
874
875/**
876 * Feed @p input_fifo through partimage (restore) to @p output_device.
877 * @param input_fifo The partimage file to read.
878 * @param output_device Where to put the output.
879 * @return The return value of partimagehack (0 for success).
880 * @bug Probably unnecessary, as the partimage is just a sparse file. We could use @c dd to restore it.
881 */
882int feed_outfrom_partimage(char *output_device, char *input_fifo)
883{
884// RESTORE
885 char *tmp;
886 char *stuff;
887 char *sz_call_to_partimage;
888 pthread_t partimage_thread;
889 int res;
890 char *curr_fifo;
891 char *prev_fifo = NULL;
892 char *oldest_fifo = NULL;
893 char *next_fifo;
894 char *afternxt_fifo;
895 int fifo_number = 0;
896 char *tmpstub;
897 FILE *fin;
898 FILE *fout;
899
900 log_msg(1, "output_device=%s", output_device);
901 log_msg(1, "input_fifo=%s", input_fifo);
902 asprintf(&tmpstub, "/tmp");
903
904 log_msg(1, "tmpstub was %s", tmpstub);
905 asprintf(&stuff, tmpstub);
906 paranoid_free(tmpstub);
907
908 asprintf(&tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random());
909 paranoid_free(stuff);
910
911 log_msg(1, "tmpstub is now %s", tmpstub);
912 unlink("/tmp/PARTIMAGEHACK-POSITION");
913 unlink(PAUSE_PARTIMAGE_FNAME);
914 paranoid_system("rm -f /tmp/*PARTIMAGE*");
915 asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
916 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
917 asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
918 mkfifo(PIH_LOG, S_IRWXU | S_IRWXG);
919 mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
920 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
921 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG);
922 system("cat " PIH_LOG " > /dev/null &");
923 log_msg(3, "curr_fifo = %s", curr_fifo);
924 log_msg(3, "next_fifo = %s", next_fifo);
925 if (!does_file_exist(input_fifo)) {
926 fatal_error("input fifo does not exist");
927 }
928 if (!(fin = fopen(input_fifo, "r"))) {
929 fatal_error("Unable to openin from input_fifo");
930 }
931 if (!find_home_of_exe("partimagehack")) {
932 fatal_error("partimagehack not found");
933 }
934 asprintf(&sz_call_to_partimage,
935 "%c%cpartimagehack " PARTIMAGE_PARAMS
936 " restore %s %s > /dev/null 2>> %s", 2, 0, output_device, curr_fifo,
937 MONDO_LOGFILE);
938 log_msg(1, "output_device = %s", output_device);
939 log_msg(1, "curr_fifo = %s", curr_fifo);
940 log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage + 2);
941 res =
942 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
943 (void *) sz_call_to_partimage);
944 if (res) {
945 fatal_error("Failed to create thread to call partimage");
946 }
947 log_msg(1, "Running fore/back at same time");
948 log_msg(2, " Trying to openin %s", input_fifo);
949 if (!does_file_exist(input_fifo)) {
950 log_msg(2, "Warning - %s does not exist", input_fifo);
951 }
952 while (!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) {
953 log_msg(6, "Waiting for partimagehack (restore) to start");
954 sleep(1);
955 }
956
957 if (!(tmp = malloc(128))) {
958 fatal_error("Failed to malloc() tmp");
959 }
960 while (sz_call_to_partimage[0] > 0) {
961 if (fread(tmp, 1, 128, fin) != 128) {
962 fatal_error("Cannot read introductory block");
963 }
964 if (strstr(tmp, NEXT_SUBVOL_PLEASE)) {
965 log_msg(2, "Great. Next subvol coming up.");
966 } else if (strstr(tmp, NO_MORE_SUBVOLS)) {
967 log_msg(2, "Great. That was the last subvol.");
968 break;
969 } else {
970 log_msg(2, "WTF is this? '%s'", tmp);
971 fatal_error("Unknown interim block");
972 }
973 if (feof(fin)) {
974 log_msg(1, "Eof(fin) detected. Breaking.");
975 break;
976 }
977 log_msg(3, "Processing subvol %d", fifo_number);
978 log_msg(5, "fifo_number=%d", fifo_number);
979 if (!(fout = fopen(curr_fifo, "w"))) {
980 fatal_error("Cannot openout to curr_fifo");
981 }
982 copy_from_src_to_dest(fout, fin, 'r');
983 paranoid_fclose(fout);
984 fifo_number++;
985 if (oldest_fifo != NULL) {
986 log_msg(6, "Deleting %s", oldest_fifo);
987 unlink(oldest_fifo); // just in case
988 paranoid_free(oldest_fifo);
989 }
990 oldest_fifo = prev_fifo;
991 prev_fifo = curr_fifo;
992 curr_fifo = next_fifo;
993 next_fifo = afternxt_fifo;
994 asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
995 log_msg(6, "Creating %s", afternxt_fifo);
996 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we access current fifo
997 fflush(fin);
998// system("sync");
999 usleep(1000L * 100L);
1000 }
1001 paranoid_free(tmp);
1002 paranoid_free(tmpstub);
1003
1004 paranoid_fclose(fin);
1005 paranoid_system("sync");
1006 log_msg(1, "Partimagehack has finished. Great. Fin-closing.");
1007 log_msg(1, "Waiting for pthread_join");
1008 pthread_join(partimage_thread, NULL);
1009 res = sz_call_to_partimage[1];
1010 paranoid_free(sz_call_to_partimage);
1011
1012 log_msg(1, "Yay. Partimagehack (restore) returned %d", res);
1013 unlink(prev_fifo);
1014 paranoid_free(prev_fifo);
1015
1016 unlink(curr_fifo);
1017 paranoid_free(curr_fifo);
1018
1019 unlink(next_fifo);
1020 paranoid_free(next_fifo);
1021
1022 unlink(afternxt_fifo);
1023 paranoid_free(afternxt_fifo);
1024
1025 unlink(PIH_LOG);
1026 /* BERLIOS : pas de unlink(oldest_fifo) ??? */
1027 paranoid_free(oldest_fifo);
1028
1029 return (res);
1030}
Note: See TracBrowser for help on using the repository browser.