source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-fork.c @ 1644

Last change on this file since 1644 was 1644, checked in by Bruno Cornec, 12 years ago

Apply patch from Andree Leidenfrost, modified a bit to use bkpinfo->tmpdir instead of /tmp
or MINDI_CACHE when appropriate. Fix security issues in mondo
Thanks al ot Andree for catching all those issues.
Will not compile needs more work as bkpinfo->tmpdir isn't available everywhere
Should become a global in 3.x when only containing pointers.

  • Property svn:keywords set to Id
File size: 22.6 KB
Line 
1/* libmondo-fork.c
2   $Id: libmondo-fork.c 1644 2007-09-23 00:41:29Z 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 1644 2007-09-23 00:41:29Z bruno $";
115
116extern char *g_tmpfs_mountpt;
117extern t_bkptype g_backup_media_type;
118extern bool g_text_mode;
119extern char *MONDO_LOGFILE;
120pid_t g_buffer_pid = 0;
121
122
123/**
124 * Call a program and retrieve its last line of output.
125 * @param call The program to run.
126 * @return The last line of its output.
127 * @note The returned value points to static storage that will be overwritten with each call.
128 */
129char *call_program_and_get_last_line_of_output(char *call)
130{
131    /*@ buffers ***************************************************** */
132    static char result[512];
133    char *tmp;
134
135    /*@ pointers **************************************************** */
136    FILE *fin;
137
138    /*@ initialize data ********************************************* */
139    malloc_string(tmp);
140    result[0] = '\0';
141    tmp[0] = '\0';
142
143    /*@******************************************************************** */
144
145    assert_string_is_neither_NULL_nor_zerolength(call);
146    if ((fin = popen(call, "r"))) {
147        for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
148             fgets(tmp, MAX_STR_LEN, fin)) {
149            if (strlen(tmp) > 1) {
150                strcpy(result, tmp);
151            }
152        }
153        paranoid_pclose(fin);
154    } else {
155        log_OS_error("Unable to popen call");
156    }
157    strip_spaces(result);
158    paranoid_free(tmp);
159    return (result);
160}
161
162
163
164
165
166
167#define MONDO_POPMSG  "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
168
169/**
170 * Call mkisofs to create an ISO image.
171 * @param bkpinfo The backup information structure. Fields used:
172 * - @c bkpinfo->manual_cd_tray
173 * - @c bkpinfo->backup_media_type
174 * - @c bkpinfo->please_dont_eject_when_restoring
175 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
176 * - @c _ISO_ will become the ISO file (@p isofile)
177 * - @c _CD#_ becomes the CD number (@p cd_no)
178 * - @c _ERR_ becomes the logfile (@p g_logfile)
179 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
180 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
181 * @param logstub Unused.
182 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
183 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
184 * @bug @p logstub is unused.
185 */
186int
187eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
188                      char *basic_call, char *isofile,
189                      int cd_no, char *logstub, char *what_i_am_doing)
190{
191
192    /*@ int's  *** */
193    int retval = 0;
194
195
196    /*@ buffers      *** */
197    char *midway_call, *ultimate_call, *tmp, *command, *incoming,
198        *old_stderr, *cd_number_str;
199    char *p;
200
201/*@***********   End Variables ***************************************/
202
203    log_msg(3, "Starting");
204    assert(bkpinfo != NULL);
205    // BERLIOS: doesn't work even if the string is correct !
206    //assert_string_is_neither_NULL_nor_zerolength(basic_call);
207    assert_string_is_neither_NULL_nor_zerolength(isofile);
208    assert_string_is_neither_NULL_nor_zerolength(logstub);
209    if (!(midway_call = malloc(1200))) {
210        fatal_error("Cannot malloc midway_call");
211    }
212    if (!(ultimate_call = malloc(1200))) {
213        fatal_error("Cannot malloc ultimate_call");
214    }
215    if (!(tmp = malloc(1200))) {
216        fatal_error("Cannot malloc tmp");
217    }
218    if (!(command = malloc(1200))) {
219        fatal_error("Cannot malloc command");
220    }
221    malloc_string(incoming);
222    malloc_string(old_stderr);
223    malloc_string(cd_number_str);
224
225    incoming[0] = '\0';
226    old_stderr[0] = '\0';
227
228    sprintf(cd_number_str, "%d", cd_no);
229    resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
230    resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
231    resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
232    log_msg(4, "basic call = '%s'", basic_call);
233    log_msg(4, "midway_call = '%s'", midway_call);
234    log_msg(4, "tmp = '%s'", tmp);
235    log_msg(4, "ultimate call = '%s'", ultimate_call);
236    sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
237
238    log_to_screen
239        ("Please be patient. Do not be alarmed by on-screen inactivity.");
240    log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
241            what_i_am_doing);
242    strcpy(tmp, command);
243    if (bkpinfo->manual_cd_tray) {
244        p = strstr(tmp, "2>>");
245        if (p) {
246            sprintf(p, "   ");
247            while (*p == ' ') {
248                p++;
249            }
250            for (; *p != ' '; p++) {
251                *p = ' ';
252            }
253        }
254        strcpy(command, tmp);
255#ifndef _XWIN
256        if (!g_text_mode) {
257            newtSuspend();
258        }
259#endif
260        log_msg(1, "command = '%s'", command);
261        retval += system(command);
262        if (!g_text_mode) {
263            newtResume();
264        }
265        if (retval) {
266            log_msg(2, "Basic call '%s' returned an error.", basic_call);
267            popup_and_OK("Press ENTER to continue.");
268            popup_and_OK
269                ("mkisofs and/or cdrecord returned an error. CD was not created");
270        }
271    }
272    /* if text mode then do the above & RETURN; if not text mode, do this... */
273    else {
274        log_msg(3, "command = '%s'", command);
275//      yes_this_is_a_goto:
276        retval =
277            run_external_binary_with_percentage_indicator_NEW
278            (what_i_am_doing, command);
279    }
280
281    paranoid_free(midway_call);
282    paranoid_free(ultimate_call);
283    paranoid_free(tmp);
284    paranoid_free(command);
285    paranoid_free(incoming);
286    paranoid_free(old_stderr);
287    paranoid_free(cd_number_str);
288/*
289  if (bkpinfo->backup_media_type == dvd && !bkpinfo->please_dont_eject_when_restoring)
290    {
291      log_msg(3, "Ejecting DVD device");
292      eject_device(bkpinfo->media_device);
293    }
294*/
295    return (retval);
296}
297
298
299
300
301/**
302 * Run a program and log its output (stdout and stderr) to the logfile.
303 * @param program The program to run. Passed to the shell, so you can use pipes etc.
304 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
305 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
306 */
307int run_program_and_log_output(char *program, int debug_level)
308{
309    /*@ buffer ****************************************************** */
310    char callstr[MAX_STR_LEN * 2];
311    char incoming[MAX_STR_LEN * 2];
312    char tmp[MAX_STR_LEN * 2];
313    char initial_label[MAX_STR_LEN * 2];
314
315    /*@ int ********************************************************* */
316    int res;
317    int i;
318    int len;
319    bool log_if_failure = FALSE;
320    bool log_if_success = FALSE;
321
322    /*@ pointers *************************************************** */
323    FILE *fin;
324    char *p;
325
326    /*@ end vars *************************************************** */
327
328    assert(program != NULL);
329    if (!program[0]) {
330        log_msg(2, "Warning - asked to run zerolength program");
331        return (1);
332    }
333//  if (debug_level == TRUE) { debug_level=5; }
334
335    //  assert_string_is_neither_NULL_nor_zerolength(program);
336
337    if (debug_level <= g_loglevel) {
338        log_if_success = TRUE;
339        log_if_failure = TRUE;
340    }
341    sprintf(callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err",
342             program, bkpinfo->tmpdir, bkpinfo->tmpdir);
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    sprintf(callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
370    if (log_if_failure && system(callstr)) {
371        log_OS_error("Command failed");
372    }
373    sprintf(tmp, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
374    unlink(tmp);
375    sprintf(tmp, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
376    fin = fopen(tmp, "r");
377    if (fin) {
378        for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
379             fgets(incoming, MAX_STR_LEN, fin)) {
380            /* patch by Heiko Schlittermann */
381            p = incoming;
382            while (p && *p) {
383                if ((p = strchr(p, '%'))) {
384                    memmove(p, p + 1, strlen(p) + 1);
385                    p += 2;
386                }
387            }
388            /* end of patch */
389            strip_spaces(incoming);
390            if ((res == 0 && log_if_success)
391                || (res != 0 && log_if_failure)) {
392                log_msg(0, incoming);
393            }
394        }
395        paranoid_fclose(fin);
396    }
397    unlink(tmp);
398    if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
399        log_msg(0,
400                "--------------------------------end of output------------------------------");
401        if (res) {
402            log_msg(0, "...ran with res=%d", res);
403        } else {
404            log_msg(0, "...ran just fine. :-)");
405        }
406    }
407//  else
408//    { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
409    return (res);
410}
411
412
413
414/**
415 * Run a program and log its output to the screen.
416 * @param basic_call The program to run.
417 * @param what_i_am_doing The title of the evalcall form.
418 * @return The return value of the command (varies, but 0 almost always means success).
419 * @see run_program_and_log_output
420 * @see log_to_screen
421 */
422int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
423{
424    /*@ int ******************************************************** */
425    int retval = 0;
426    int res = 0;
427    int i;
428
429    /*@ pointers **************************************************** */
430    FILE *fin;
431
432    /*@ buffers **************************************************** */
433    char tmp[MAX_STR_LEN * 2];
434    char command[MAX_STR_LEN * 2];
435    char lockfile[MAX_STR_LEN];
436
437    /*@ end vars *************************************************** */
438
439    assert_string_is_neither_NULL_nor_zerolength(basic_call);
440
441    sprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
442
443    sprintf(command,
444            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
445            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
446    open_evalcall_form(what_i_am_doing);
447    sprintf(tmp, "Executing %s", basic_call);
448    log_msg(2, tmp);
449    if (!(fin = popen(command, "r"))) {
450        log_OS_error("Unable to popen-in command");
451        sprintf(tmp, "Failed utterly to call '%s'", command);
452        log_to_screen(tmp);
453        return (1);
454    }
455    if (!does_file_exist(lockfile)) {
456        log_to_screen("Waiting for external binary to start");
457        for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
458            log_msg(3, "Waiting for lockfile %s to exist", lockfile);
459        }
460    }
461#ifdef _XWIN
462    /* This only can update when newline goes into the file,
463       but it's *much* prettier/faster on Qt. */
464    while (does_file_exist(lockfile)) {
465        while (!feof(fin)) {
466            if (!fgets(tmp, 512, fin))
467                break;
468            log_to_screen(tmp);
469        }
470        usleep(500000);
471    }
472#else
473    /* This works on Newt, and it gives quicker updates. */
474    for (; does_file_exist(lockfile); sleep(1)) {
475        log_file_end_to_screen(MONDO_LOGFILE, "");
476        update_evalcall_form(1);
477    }
478#endif
479    /* Evaluate the status returned by pclose to get the exit code of the called program. */
480    errno = 0;
481    res = pclose(fin);
482    /* Log actual pclose errors. */
483    if (errno) log_msg(5, "pclose err: %d", errno);
484    /* Check if we have a valid status. If we do, extract the called program's exit code. */
485    /* If we don't, highlight this fact by returning -1. */
486    if (WIFEXITED(res)) {
487        retval = WEXITSTATUS(res);
488    } else {
489        retval = -1;
490    }
491    close_evalcall_form();
492    unlink(lockfile);
493    return (retval);
494}
495
496
497
498
499/**
500 * Apparently unused. @bug This has a purpose, but what?
501 */
502#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
503#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
504
505
506
507
508int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
509{
510// if dir=='w' then copy from orig to archived
511// if dir=='r' then copy from archived to orig
512    char *tmp;
513    char *buf;
514    char filestr[MAX_STR_LEN];
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                sprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
616                ftmp = fopen(filestr, "w");
617                bytes_read_in = fread(tmp, 1, 64, fin);
618                log_msg(1, "bytes_read_in = %ld", bytes_read_in);
619//      if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
620                fwrite(tmp, 1, bytes_read_in, ftmp);
621                sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
622//    log_msg(0, tmp);
623                fread(tmp, 1, 512, fin);
624                log_msg(0, "tmp = '%s'", tmp);
625                fwrite(tmp, 1, 512, ftmp);
626                fclose(ftmp);
627                fatal_error("Missing terminating block");
628            }
629        }
630    }
631
632    paranoid_free(buf);
633    paranoid_free(tmp);
634    log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
635    return (retval);
636}
637
638
639
640
641/**
642 * Feed @p input_device through ntfsclone to @p output_fname.
643 * @param input_device The device to image.
644 * @param output_fname The file to write.
645 * @return 0 for success, nonzero for failure.
646 */
647int feed_into_ntfsprog(char *input_device, char *output_fname)
648{
649// BACKUP
650    int res = -1;
651    char*command;
652
653    if (!does_file_exist(input_device)) {
654        fatal_error("input device does not exist");
655    }
656    if ( !find_home_of_exe("ntfsclone")) {
657        fatal_error("ntfsclone not found");
658    }
659    malloc_string(command);
660    sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
661    res = run_program_and_log_output(command, 5);
662    paranoid_free(command);
663    unlink(output_fname);
664    return (res);
665}
666
667
668
669
670
671int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
672{
673
674    /*@ int *************************************************************** */
675    int res = 0;
676    int percentage = 0;
677    int maxpc = 0;
678    int pcno = 0;
679    int last_pcno = 0;
680
681    /*@ buffers *********************************************************** */
682    char *command;
683    char *tempfile;
684    char *title;
685    /*@ pointers ********************************************************** */
686    FILE *pin;
687
688    malloc_string(title);
689    malloc_string(command);
690    malloc_string(tempfile);
691    assert_string_is_neither_NULL_nor_zerolength(cmd);
692    assert_string_is_neither_NULL_nor_zerolength(title);
693
694    strcpy(title, tt);
695    sprintf(tempfile, "%s/mondo.binperc", bkpinfo->tmpdir);
696    sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
697            tempfile);
698    log_msg(3, command);
699    open_evalcall_form(title);
700    if (!(pin = popen(command, "r"))) {
701        log_OS_error("fmt err");
702        return (1);
703    }
704    maxpc = 100;
705// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
706    for (sleep(1); does_file_exist(tempfile); sleep(1)) {
707        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
708        if (pcno < 0 || pcno > 100) {
709            log_msg(5, "Weird pc#");
710            continue;
711        }
712        percentage = pcno * 100 / maxpc;
713        if (pcno <= 5 && last_pcno > 40) {
714            close_evalcall_form();
715            strcpy(title, "Verifying...");
716            open_evalcall_form(title);
717        }
718        last_pcno = pcno;
719        update_evalcall_form(percentage);
720    }
721// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
722    close_evalcall_form();
723    if (pclose(pin)) {
724        res++;
725        log_OS_error("Unable to pclose");
726    }
727    unlink(tempfile);
728    paranoid_free(command);
729    paranoid_free(tempfile);
730    paranoid_free(title);
731    return (res);
732}
733
734
735
736
737void *run_prog_in_bkgd_then_exit(void *info)
738{
739    char *sz_command;
740    static int res = 4444;
741
742    res = 999;
743    sz_command = (char *) info;
744    log_msg(4, "sz_command = '%s'", sz_command);
745    res = system(sz_command);
746    if (res > 256 && res != 4444) {
747        res = res / 256;
748    }
749    log_msg(4, "child res = %d", res);
750    sz_command[0] = '\0';
751    pthread_exit((void *) (&res));
752}
753
754
755
756
757int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
758{
759
760    /*@ int *************************************************************** */
761    int res = 0;
762    int percentage = 0;
763    int maxpc = 100;
764    int pcno = 0;
765    int last_pcno = 0;
766    int counter = 0;
767
768    /*@ buffers *********************************************************** */
769    char *command;
770    char *title;
771    /*@ pointers ********************************************************** */
772    static int chldres = 0;
773    int *pchild_result;
774    pthread_t childthread;
775
776    pchild_result = &chldres;
777    assert_string_is_neither_NULL_nor_zerolength(cmd);
778    assert_string_is_neither_NULL_nor_zerolength(tt);
779    *pchild_result = 999;
780
781    malloc_string(title);
782    malloc_string(command);
783    strcpy(title, tt);
784    sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
785    log_msg(3, "command = '%s'", command);
786    if ((res =
787         pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
788                        (void *) command))) {
789        fatal_error("Unable to create an archival thread");
790    }
791
792    log_msg(8, "Parent running");
793    open_evalcall_form(title);
794    for (sleep(1); command[0] != '\0'; sleep(1)) {
795        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
796        if (pcno <= 0 || pcno > 100) {
797            log_msg(8, "Weird pc#");
798            continue;
799        }
800        percentage = pcno * 100 / maxpc;
801        if (pcno <= 5 && last_pcno >= 40) {
802            close_evalcall_form();
803            strcpy(title, "Verifying...");
804            open_evalcall_form(title);
805        }
806        if (counter++ >= 5) {
807            counter = 0;
808            log_file_end_to_screen(MONDO_LOGFILE, "");
809        }
810        last_pcno = pcno;
811        update_evalcall_form(percentage);
812    }
813    log_file_end_to_screen(MONDO_LOGFILE, "");
814    close_evalcall_form();
815    pthread_join(childthread, (void *) (&pchild_result));
816    if (pchild_result) {
817        res = *pchild_result;
818    } else {
819        res = 666;
820    }
821    log_msg(3, "Parent res = %d", res);
822    paranoid_free(command);
823    paranoid_free(title);
824    return (res);
825}
826
827
828
829
830/**
831 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
832 * @param input_fifo The ntfsclone file to read.
833 * @param output_device Where to put the output.
834 * @return The return value of ntfsclone (0 for success).
835 */
836int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
837{
838// RESTORE
839    int res = -1;
840    char *command;
841
842    if ( !find_home_of_exe("ntfsclone")) {
843        fatal_error("ntfsclone not found");
844    }
845    malloc_string(command);
846    sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
847    res = run_program_and_log_output(command, 5);
848    paranoid_free(command);
849    return (res);
850}
Note: See TracBrowser for help on using the repository browser.