source: MondoRescue/branches/2.2.2/mondo/src/common/libmondo-fork.c@ 1298

Last change on this file since 1298 was 1236, checked in by Bruno Cornec, 17 years ago

Port from stable important patches for 2.2.2:
mindi-busybox:

  • Improve Gentoo packaging

mindi:

  • Better messages for analyze-my-lvm
  • Add support for newer Qlogic drivers (qla2300 & 2400), mpt, dm and ohci
  • Fix a bug in mindi for the FAILSAFE support
  • PATH fixed for init in restore (/usr/games added for petris)
  • Fiw a bug where restore failing because no archive files are found when -G is used
  • /media is now completely excluded as per StandardsCompliance

mondo:

  • Handle no compression + verify correctly
  • various HOWTO fixes
  • Fix temporarily a bug when a biggiefile > 32MB was compressed below the size

of a slice (16MB)

  • Better module loading in insmod_crucial_modules
  • Improve Gentoo packaging
  • Small typo fix for mondorestore man page
  • Small memory management improvements
  • Store NFS config only once
  • Fix a flaw in libmondo-mountlist.c (there since rev [1] !!)
  • Increased MAX_STR_LEN to 384 to make it divisible without remainder by eight

for 64 bits platforms

  • Fix a bug where no bzip2 format file would be found when supporting gzip
  • CentOS fixes
  • Property svn:keywords set to Id
File size: 22.4 KB
Line 
1/* libmondo-fork.c
2 $Id: libmondo-fork.c 1236 2007-03-11 23:23:16Z bruno $
3
4- subroutines for handling forking/pthreads/etc.
5
6
701/20/2006
8- replaced partimagehack with ntfsclone
9
1006/20/2004
11- create fifo /var/log/partimagehack-debug.log and empty it
12 to keep ramdisk from filling up
13
1404/13/2004
15- >= should be <= g_loglevel
16
1711/15/2003
18- changed a few []s to char*s
19
2010/12
21- rewrote partimagehack handling (multiple fifos, chunks, etc.)
22
2310/11
24- partimagehack now has debug level of N (set in my-stuff.h)
25
2610/08
27- call to partimagehack when restoring will now log errors to /var/log/....log
28
2910/06
30- cleaned up logging a bit
31
3209/30
33- line 735 - missing char* cmd in sprintf()
34
3509/28
36- added run_external_binary_with_percentage_indicator()
37- rewritten eval_call_to_make_ISO()
38
3909/18
40- call mkstemp instead of mktemp
41
4209/13
43- major NTFS hackage
44
4509/12
46- paranoid_system("rm -f /tmp/ *PARTIMAGE*") before calling partimagehack
47
4809/11
49- forward-ported unbroken feed_*_partimage() subroutines
50 from early August 2003
51
5209/08
53- detect & use partimagehack if it exists
54
5509/05
56- finally finished partimagehack hack :)
57
5807/04
59- added subroutines to wrap around partimagehack
60
6104/27
62- don't echo (...res=%d...) at end of log_it()
63 unnecessarily
64- replace newtFinished() and newtInit() with
65 newtSuspend() and newtResume()
66
6704/24
68- added some assert()'s and log_OS_error()'s
69
7004/09
71- cleaned up run_program_and_log_output()
72
7304/07
74- cleaned up code a bit
75- let run_program_and_log_output() accept -1 (only log if _no error_)
76
7701/02/2003
78- in eval_call_to_make_ISO(), append output to MONDO_LOGFILE
79 instead of a temporary stderr text file
80
8112/10
82- patch by Heiko Schlittermann to handle % chars in issue.net
83
8411/18
85- if mkisofs in eval_call_to_make_ISO() returns an error then return it,
86 whether ISO was created or not
87
8810/30
89- if mkisofs in eval_call_to_make_ISO() returns an error then find out if
90 the output (ISO) file has been created; if it has then return 0 anyway
91
9208/01 - 09/30
93- run_program_and_log_output() now takes boolean operator to specify
94 whether it will log its activities in the event of _success_
95- system() now includes 2>/dev/null
96- enlarged some tmp[]'s
97- added run_program_and_log_to_screen() and run_program_and_log_output()
98
9907/24
100- created
101*/
102
103
104#include "my-stuff.h"
105#include "mondostructures.h"
106#include "libmondo-fork.h"
107#include "libmondo-string-EXT.h"
108#include "libmondo-gui-EXT.h"
109#include "libmondo-files-EXT.h"
110#include "libmondo-tools-EXT.h"
111#include "lib-common-externs.h"
112
113/*@unused@*/
114//static char cvsid[] = "$Id: libmondo-fork.c 1236 2007-03-11 23:23:16Z bruno $";
115
116extern char *g_tmpfs_mountpt;
117extern t_bkptype g_backup_media_type;
118extern bool g_text_mode;
119pid_t g_buffer_pid = 0;
120
121
122/**
123 * Call a program and retrieve its last line of output.
124 * @param call The program to run.
125 * @return The last line of its output.
126 * @note The returned value points to static storage that will be overwritten with each call.
127 */
128char *call_program_and_get_last_line_of_output(char *call)
129{
130 /*@ buffers ***************************************************** */
131 static char result[512];
132 char *tmp;
133
134 /*@ pointers **************************************************** */
135 FILE *fin;
136
137 /*@ initialize data ********************************************* */
138 malloc_string(tmp);
139 result[0] = '\0';
140 tmp[0] = '\0';
141
142 /*@******************************************************************** */
143
144 assert_string_is_neither_NULL_nor_zerolength(call);
145 if ((fin = popen(call, "r"))) {
146 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
147 fgets(tmp, MAX_STR_LEN, fin)) {
148 if (strlen(tmp) > 1) {
149 strcpy(result, tmp);
150 }
151 }
152 paranoid_pclose(fin);
153 } else {
154 log_OS_error("Unable to popen call");
155 }
156 strip_spaces(result);
157 paranoid_free(tmp);
158 return (result);
159}
160
161
162
163
164
165
166#define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
167
168/**
169 * Call mkisofs to create an ISO image.
170 * @param bkpinfo The backup information structure. Fields used:
171 * - @c bkpinfo->manual_cd_tray
172 * - @c bkpinfo->backup_media_type
173 * - @c bkpinfo->please_dont_eject_when_restoring
174 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
175 * - @c _ISO_ will become the ISO file (@p isofile)
176 * - @c _CD#_ becomes the CD number (@p cd_no)
177 * - @c _ERR_ becomes the logfile (@p g_logfile)
178 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
179 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
180 * @param logstub Unused.
181 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
182 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
183 * @bug @p logstub is unused.
184 */
185int
186eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
187 char *basic_call, char *isofile,
188 int cd_no, char *logstub, char *what_i_am_doing)
189{
190
191 /*@ int's *** */
192 int retval = 0;
193
194
195 /*@ buffers *** */
196 char *midway_call, *ultimate_call, *tmp, *command, *incoming,
197 *old_stderr, *cd_number_str;
198 char *p;
199
200/*@*********** End Variables ***************************************/
201
202 log_msg(3, "Starting");
203 assert(bkpinfo != NULL);
204 // BERLIOS: doesn't work even if the string is correct !
205 //assert_string_is_neither_NULL_nor_zerolength(basic_call);
206 assert_string_is_neither_NULL_nor_zerolength(isofile);
207 assert_string_is_neither_NULL_nor_zerolength(logstub);
208 if (!(midway_call = malloc(1200))) {
209 fatal_error("Cannot malloc midway_call");
210 }
211 if (!(ultimate_call = malloc(1200))) {
212 fatal_error("Cannot malloc ultimate_call");
213 }
214 if (!(tmp = malloc(1200))) {
215 fatal_error("Cannot malloc tmp");
216 }
217 if (!(command = malloc(1200))) {
218 fatal_error("Cannot malloc command");
219 }
220 malloc_string(incoming);
221 malloc_string(old_stderr);
222 malloc_string(cd_number_str);
223
224 incoming[0] = '\0';
225 old_stderr[0] = '\0';
226
227 sprintf(cd_number_str, "%d", cd_no);
228 resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
229 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
230 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
231 log_msg(4, "basic call = '%s'", basic_call);
232 log_msg(4, "midway_call = '%s'", midway_call);
233 log_msg(4, "tmp = '%s'", tmp);
234 log_msg(4, "ultimate call = '%s'", ultimate_call);
235 sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
236
237 log_to_screen
238 ("Please be patient. Do not be alarmed by on-screen inactivity.");
239 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
240 what_i_am_doing);
241 strcpy(tmp, command);
242 if (bkpinfo->manual_cd_tray) {
243 p = strstr(tmp, "2>>");
244 if (p) {
245 sprintf(p, " ");
246 while (*p == ' ') {
247 p++;
248 }
249 for (; *p != ' '; p++) {
250 *p = ' ';
251 }
252 }
253 strcpy(command, tmp);
254#ifndef _XWIN
255 if (!g_text_mode) {
256 newtSuspend();
257 }
258#endif
259 log_msg(1, "command = '%s'", command);
260 retval += system(command);
261 if (!g_text_mode) {
262 newtResume();
263 }
264 if (retval) {
265 log_msg(2, "Basic call '%s' returned an error.", basic_call);
266 popup_and_OK("Press ENTER to continue.");
267 popup_and_OK
268 ("mkisofs and/or cdrecord returned an error. CD was not created");
269 }
270 }
271 /* if text mode then do the above & RETURN; if not text mode, do this... */
272 else {
273 log_msg(3, "command = '%s'", command);
274// yes_this_is_a_goto:
275 retval =
276 run_external_binary_with_percentage_indicator_NEW
277 (what_i_am_doing, command);
278 }
279
280 paranoid_free(midway_call);
281 paranoid_free(ultimate_call);
282 paranoid_free(tmp);
283 paranoid_free(command);
284 paranoid_free(incoming);
285 paranoid_free(old_stderr);
286 paranoid_free(cd_number_str);
287/*
288 if (bkpinfo->backup_media_type == dvd && !bkpinfo->please_dont_eject_when_restoring)
289 {
290 log_msg(3, "Ejecting DVD device");
291 eject_device(bkpinfo->media_device);
292 }
293*/
294 return (retval);
295}
296
297
298
299
300/**
301 * Run a program and log its output (stdout and stderr) to the logfile.
302 * @param program The program to run. Passed to the shell, so you can use pipes etc.
303 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
304 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
305 */
306int run_program_and_log_output(char *program, int debug_level)
307{
308 /*@ buffer ****************************************************** */
309 char callstr[MAX_STR_LEN * 2];
310 char incoming[MAX_STR_LEN * 2];
311 char tmp[MAX_STR_LEN * 2];
312 char initial_label[MAX_STR_LEN * 2];
313
314 /*@ int ********************************************************* */
315 int res;
316 int i;
317 int len;
318 bool log_if_failure = FALSE;
319 bool log_if_success = FALSE;
320
321 /*@ pointers *************************************************** */
322 FILE *fin;
323 char *p;
324
325 /*@ end vars *************************************************** */
326
327 assert(program != NULL);
328 if (!program[0]) {
329 log_msg(2, "Warning - asked to run zerolength program");
330 return (1);
331 }
332// if (debug_level == TRUE) { debug_level=5; }
333
334 // assert_string_is_neither_NULL_nor_zerolength(program);
335
336 if (debug_level <= g_loglevel) {
337 log_if_success = TRUE;
338 log_if_failure = TRUE;
339 }
340 sprintf(callstr,
341 "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
342 program);
343 while ((p = strchr(callstr, '\r'))) {
344 *p = ' ';
345 }
346 while ((p = strchr(callstr, '\n'))) {
347 *p = ' ';
348 } /* single '=' is intentional */
349
350
351 len = (int) strlen(program);
352 for (i = 0; i < 35 - len / 2; i++) {
353 tmp[i] = '-';
354 }
355 tmp[i] = '\0';
356 strcat(tmp, " ");
357 strcat(tmp, program);
358 strcat(tmp, " ");
359 for (i = 0; i < 35 - len / 2; i++) {
360 strcat(tmp, "-");
361 }
362 strcpy(initial_label, tmp);
363 res = system(callstr);
364 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
365 log_msg(0, "running: %s", callstr);
366 log_msg(0,
367 "--------------------------------start of output-----------------------------");
368 }
369 if (log_if_failure
370 &&
371 system
372 ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
373 {
374 log_OS_error("Command failed");
375 }
376 unlink("/tmp/mondo-run-prog-thing.err");
377 fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
378 if (fin) {
379 for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
380 fgets(incoming, MAX_STR_LEN, fin)) {
381 /* patch by Heiko Schlittermann */
382 p = incoming;
383 while (p && *p) {
384 if ((p = strchr(p, '%'))) {
385 memmove(p, p + 1, strlen(p) + 1);
386 p += 2;
387 }
388 }
389 /* end of patch */
390 strip_spaces(incoming);
391 if ((res == 0 && log_if_success)
392 || (res != 0 && log_if_failure)) {
393 log_msg(0, incoming);
394 }
395 }
396 paranoid_fclose(fin);
397 }
398 unlink("/tmp/mondo-run-prog-thing.tmp");
399 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
400 log_msg(0,
401 "--------------------------------end of output------------------------------");
402 if (res) {
403 log_msg(0, "...ran with res=%d", res);
404 } else {
405 log_msg(0, "...ran just fine. :-)");
406 }
407 }
408// else
409// { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
410 return (res);
411}
412
413
414
415/**
416 * Run a program and log its output to the screen.
417 * @param basic_call The program to run.
418 * @param what_i_am_doing The title of the evalcall form.
419 * @return The return value of the command (varies, but 0 almost always means success).
420 * @see run_program_and_log_output
421 * @see log_to_screen
422 */
423int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
424{
425 /*@ int ******************************************************** */
426 int retval = 0;
427 int res = 0;
428 int i;
429
430 /*@ pointers **************************************************** */
431 FILE *fin;
432
433 /*@ buffers **************************************************** */
434 char tmp[MAX_STR_LEN * 2];
435 char command[MAX_STR_LEN * 2];
436 char lockfile[MAX_STR_LEN];
437
438 /*@ end vars *************************************************** */
439
440 assert_string_is_neither_NULL_nor_zerolength(basic_call);
441
442 sprintf(lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
443 mkstemp(lockfile);
444 sprintf(command,
445 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
446 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
447 open_evalcall_form(what_i_am_doing);
448 sprintf(tmp, "Executing %s", basic_call);
449 log_msg(2, tmp);
450 if (!(fin = popen(command, "r"))) {
451 log_OS_error("Unable to popen-in command");
452 sprintf(tmp, "Failed utterly to call '%s'", command);
453 log_to_screen(tmp);
454 return (1);
455 }
456 if (!does_file_exist(lockfile)) {
457 log_to_screen("Waiting for external binary to start");
458 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
459 log_msg(3, "Waiting for lockfile %s to exist", lockfile);
460 }
461 }
462#ifdef _XWIN
463 /* This only can update when newline goes into the file,
464 but it's *much* prettier/faster on Qt. */
465 while (does_file_exist(lockfile)) {
466 while (!feof(fin)) {
467 if (!fgets(tmp, 512, fin))
468 break;
469 log_to_screen(tmp);
470 }
471 usleep(500000);
472 }
473#else
474 /* This works on Newt, and it gives quicker updates. */
475 for (; does_file_exist(lockfile); sleep(1)) {
476 log_file_end_to_screen(MONDO_LOGFILE, "");
477 update_evalcall_form(1);
478 }
479#endif
480 /* Evaluate the status returned by pclose to get the exit code of the called program. */
481 errno = 0;
482 res = pclose(fin);
483 /* Log actual pclose errors. */
484 if (errno) log_msg(5, "pclose err: %d", errno);
485 /* Check if we have a valid status. If we do, extract the called program's exit code. */
486 /* If we don't, highlight this fact by returning -1. */
487 if (WIFEXITED(res)) {
488 retval = WEXITSTATUS(res);
489 } else {
490 retval = -1;
491 }
492 close_evalcall_form();
493 unlink(lockfile);
494 return (retval);
495}
496
497
498
499
500/**
501 * Apparently unused. @bug This has a purpose, but what?
502 */
503#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
504#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
505
506
507
508
509int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
510{
511// if dir=='w' then copy from orig to archived
512// if dir=='r' then copy from archived to orig
513 char *tmp;
514 char *buf;
515 long int bytes_to_be_read, bytes_read_in, bytes_written_out =
516 0, bufcap, subsliceno = 0;
517 int retval = 0;
518 FILE *fin;
519 FILE *fout;
520 FILE *ftmp;
521
522 log_msg(5, "Opening.");
523 malloc_string(tmp);
524 tmp[0] = '\0';
525 bufcap = 256L * 1024L;
526 if (!(buf = malloc(bufcap))) {
527 fatal_error("Failed to malloc() buf");
528 }
529
530 if (direction == 'w') {
531 fin = f_orig;
532 fout = f_archived;
533 sprintf(tmp, "%-64s", PIMP_START_SZ);
534 if (fwrite(tmp, 1, 64, fout) != 64) {
535 fatal_error("Can't write the introductory block");
536 }
537 while (1) {
538 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
539 if (bytes_read_in == 0) {
540 break;
541 }
542 sprintf(tmp, "%-64ld", bytes_read_in);
543 if (fwrite(tmp, 1, 64, fout) != 64) {
544 fatal_error("Cannot write introductory block");
545 }
546 log_msg(7,
547 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
548 subsliceno, bytes_read_in, bytes_to_be_read);
549 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
550 sprintf(tmp, "%-64ld", subsliceno);
551 if (fwrite(tmp, 1, 64, fout) != 64) {
552 fatal_error("Cannot write post-thingy block");
553 }
554 log_msg(7, "Subslice #%d written OK", subsliceno);
555 subsliceno++;
556 }
557 sprintf(tmp, "%-64ld", 0L);
558 if (fwrite(tmp, 1, 64L, fout) != 64L) {
559 fatal_error("Cannot write final introductory block");
560 }
561 } else {
562 fin = f_archived;
563 fout = f_orig;
564 if (fread(tmp, 1, 64L, fin) != 64L) {
565 fatal_error("Cannot read the introductory block");
566 }
567 log_msg(5, "tmp is %s", tmp);
568 if (!strstr(tmp, PIMP_START_SZ)) {
569 fatal_error("Can't find intro blk");
570 }
571 if (fread(tmp, 1, 64L, fin) != 64L) {
572 fatal_error("Cannot read introductory blk");
573 }
574 bytes_to_be_read = atol(tmp);
575 while (bytes_to_be_read > 0) {
576 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
577 bytes_to_be_read);
578 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
579 if (bytes_read_in != bytes_to_be_read) {
580 fatal_error
581 ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
582 }
583 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
584 if (fread(tmp, 1, 64, fin) != 64) {
585 fatal_error("Cannot read post-thingy block");
586 }
587 if (atol(tmp) != subsliceno) {
588 log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
589 subsliceno, atol(tmp), tmp);
590 }
591 log_msg(7, "Subslice #%ld read OK", subsliceno);
592 subsliceno++;
593 if (fread(tmp, 1, 64, fin) != 64) {
594 fatal_error("Cannot read introductory block");
595 }
596 bytes_to_be_read = atol(tmp);
597 }
598 }
599
600// log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
601
602 if (direction == 'w') {
603 sprintf(tmp, "%-64s", PIMP_END_SZ);
604 if (fwrite(tmp, 1, 64, fout) != 64) {
605 fatal_error("Can't write the final block");
606 }
607 } else {
608 log_msg(1, "tmpA is %s", tmp);
609 if (!strstr(tmp, PIMP_END_SZ)) {
610 if (fread(tmp, 1, 64, fin) != 64) {
611 fatal_error("Can't read the final block");
612 }
613 log_msg(5, "tmpB is %s", tmp);
614 if (!strstr(tmp, PIMP_END_SZ)) {
615 ftmp = fopen("/tmp/out.leftover", "w");
616 bytes_read_in = fread(tmp, 1, 64, fin);
617 log_msg(1, "bytes_read_in = %ld", bytes_read_in);
618// if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
619 fwrite(tmp, 1, bytes_read_in, ftmp);
620 sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
621// log_msg(0, tmp);
622 fread(tmp, 1, 512, fin);
623 log_msg(0, "tmp = '%s'", tmp);
624 fwrite(tmp, 1, 512, ftmp);
625 fclose(ftmp);
626 fatal_error("Missing terminating block");
627 }
628 }
629 }
630
631 paranoid_free(buf);
632 paranoid_free(tmp);
633 log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
634 return (retval);
635}
636
637
638
639
640/**
641 * Feed @p input_device through ntfsclone to @p output_fname.
642 * @param input_device The device to image.
643 * @param output_fname The file to write.
644 * @return 0 for success, nonzero for failure.
645 */
646int feed_into_ntfsprog(char *input_device, char *output_fname)
647{
648// BACKUP
649 int res = -1;
650 char*command;
651
652 if (!does_file_exist(input_device)) {
653 fatal_error("input device does not exist");
654 }
655 if ( !find_home_of_exe("ntfsclone")) {
656 fatal_error("ntfsclone not found");
657 }
658 malloc_string(command);
659 sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
660 res = run_program_and_log_output(command, 5);
661 paranoid_free(command);
662 unlink(output_fname);
663 return (res);
664}
665
666
667
668
669
670int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
671{
672
673 /*@ int *************************************************************** */
674 int res = 0;
675 int percentage = 0;
676 int maxpc = 0;
677 int pcno = 0;
678 int last_pcno = 0;
679
680 /*@ buffers *********************************************************** */
681 char *command;
682 char *tempfile;
683 char *title;
684 /*@ pointers ********************************************************** */
685 FILE *pin;
686
687 malloc_string(title);
688 malloc_string(command);
689 malloc_string(tempfile);
690 assert_string_is_neither_NULL_nor_zerolength(cmd);
691 assert_string_is_neither_NULL_nor_zerolength(title);
692
693 strcpy(title, tt);
694 strcpy(tempfile,
695 call_program_and_get_last_line_of_output
696 ("mktemp -q /tmp/mondo.XXXXXXXX"));
697 sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
698 tempfile);
699 log_msg(3, command);
700 open_evalcall_form(title);
701 if (!(pin = popen(command, "r"))) {
702 log_OS_error("fmt err");
703 return (1);
704 }
705 maxpc = 100;
706// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
707 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
708 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
709 if (pcno < 0 || pcno > 100) {
710 log_msg(5, "Weird pc#");
711 continue;
712 }
713 percentage = pcno * 100 / maxpc;
714 if (pcno <= 5 && last_pcno > 40) {
715 close_evalcall_form();
716 strcpy(title, "Verifying...");
717 open_evalcall_form(title);
718 }
719 last_pcno = pcno;
720 update_evalcall_form(percentage);
721 }
722// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
723 close_evalcall_form();
724 if (pclose(pin)) {
725 res++;
726 log_OS_error("Unable to pclose");
727 }
728 unlink(tempfile);
729 paranoid_free(command);
730 paranoid_free(tempfile);
731 paranoid_free(title);
732 return (res);
733}
734
735
736
737
738void *run_prog_in_bkgd_then_exit(void *info)
739{
740 char *sz_command;
741 static int res = 4444;
742
743 res = 999;
744 sz_command = (char *) info;
745 log_msg(4, "sz_command = '%s'", sz_command);
746 res = system(sz_command);
747 if (res > 256 && res != 4444) {
748 res = res / 256;
749 }
750 log_msg(4, "child res = %d", res);
751 sz_command[0] = '\0';
752 pthread_exit((void *) (&res));
753}
754
755
756
757
758int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
759{
760
761 /*@ int *************************************************************** */
762 int res = 0;
763 int percentage = 0;
764 int maxpc = 100;
765 int pcno = 0;
766 int last_pcno = 0;
767 int counter = 0;
768
769 /*@ buffers *********************************************************** */
770 char *command;
771 char *title;
772 /*@ pointers ********************************************************** */
773 static int chldres = 0;
774 int *pchild_result;
775 pthread_t childthread;
776
777 pchild_result = &chldres;
778 assert_string_is_neither_NULL_nor_zerolength(cmd);
779 assert_string_is_neither_NULL_nor_zerolength(tt);
780 *pchild_result = 999;
781
782 malloc_string(title);
783 malloc_string(command);
784 strcpy(title, tt);
785 sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
786 log_msg(3, "command = '%s'", command);
787 if ((res =
788 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
789 (void *) command))) {
790 fatal_error("Unable to create an archival thread");
791 }
792
793 log_msg(8, "Parent running");
794 open_evalcall_form(title);
795 for (sleep(1); command[0] != '\0'; sleep(1)) {
796 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
797 if (pcno <= 0 || pcno > 100) {
798 log_msg(8, "Weird pc#");
799 continue;
800 }
801 percentage = pcno * 100 / maxpc;
802 if (pcno <= 5 && last_pcno >= 40) {
803 close_evalcall_form();
804 strcpy(title, "Verifying...");
805 open_evalcall_form(title);
806 }
807 if (counter++ >= 5) {
808 counter = 0;
809 log_file_end_to_screen(MONDO_LOGFILE, "");
810 }
811 last_pcno = pcno;
812 update_evalcall_form(percentage);
813 }
814 log_file_end_to_screen(MONDO_LOGFILE, "");
815 close_evalcall_form();
816 pthread_join(childthread, (void *) (&pchild_result));
817 if (pchild_result) {
818 res = *pchild_result;
819 } else {
820 res = 666;
821 }
822 log_msg(3, "Parent res = %d", res);
823 paranoid_free(command);
824 paranoid_free(title);
825 return (res);
826}
827
828
829
830
831/**
832 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
833 * @param input_fifo The ntfsclone file to read.
834 * @param output_device Where to put the output.
835 * @return The return value of ntfsclone (0 for success).
836 */
837int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
838{
839// RESTORE
840 int res = -1;
841 char *command;
842
843 if ( !find_home_of_exe("ntfsclone")) {
844 fatal_error("ntfsclone not found");
845 }
846 malloc_string(command);
847 sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
848 res = run_program_and_log_output(command, 5);
849 paranoid_free(command);
850 return (res);
851}
Note: See TracBrowser for help on using the repository browser.