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

Last change on this file since 412 was 412, checked in by andree, 14 years ago

Cleanup after ntfsclone is done: remove fifo created in /tmp.

  • Property svn:keywords set to Id
File size: 21.4 KB
Line 
1/* libmondo-fork.c
2   $Id: libmondo-fork.c 412 2006-02-20 10:17:16Z andree $
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 412 2006-02-20 10:17:16Z andree $";
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 = NULL;
132    char *tmp = NULL;
133
134    /*@ pointers **************************************************** */
135    FILE *fin;
136
137    size_t n = 0;
138
139    /*@******************************************************************** */
140
141    assert_string_is_neither_NULL_nor_zerolength(call);
142    if ((fin = popen(call, "r"))) {
143        for (getline(&tmp, &n, fin); !feof(fin);
144             getline(&tmp, &n, fin)) {
145            if (strlen(tmp) > 1) {
146                if (result != NULL) {
147                    paranoid_free(result);
148                }
149                asprintf(&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#define MONDO_POPMSG  "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
162
163/**
164 * Call mkisofs to create an ISO image.
165 * @param bkpinfo The backup information structure. Fields used:
166 * - @c bkpinfo->manual_cd_tray
167 * - @c bkpinfo->backup_media_type
168 * - @c bkpinfo->please_dont_eject_when_restoring
169 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
170 * - @c _ISO_ will become the ISO file (@p isofile)
171 * - @c _CD#_ becomes the CD number (@p cd_no)
172 * - @c _ERR_ becomes the logfile (@p g_logfile)
173 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
174 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
175 * @param logstub Unused.
176 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
177 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
178 * @bug @p logstub is unused.
179 */
180int
181eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
182                      char *basic_call, char *isofile,
183                      int cd_no, char *logstub, char *what_i_am_doing)
184{
185
186    /*@ int's  *** */
187    int retval = 0;
188
189
190    /*@ buffers      *** */
191    char *midway_call, *ultimate_call, *tmp, *command,
192        *cd_number_str;
193    char *p;
194
195/*@***********   End Variables ***************************************/
196
197    log_msg(3, "Starting");
198    assert(bkpinfo != NULL);
199    assert_string_is_neither_NULL_nor_zerolength(basic_call);
200    assert_string_is_neither_NULL_nor_zerolength(isofile);
201    assert_string_is_neither_NULL_nor_zerolength(logstub);
202    if (!(midway_call = malloc(1200))) {
203        fatal_error("Cannot malloc midway_call");
204    }
205    if (!(ultimate_call = malloc(1200))) {
206        fatal_error("Cannot malloc ultimate_call");
207    }
208    if (!(tmp = malloc(1200))) {
209        fatal_error("Cannot malloc tmp");
210    }
211
212    asprintf(&cd_number_str, "%d", cd_no);
213    resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
214    resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
215    paranoid_free(cd_number_str);
216
217    resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
218    log_msg(4, "basic call = '%s'", basic_call);
219    log_msg(4, "midway_call = '%s'", midway_call);
220    log_msg(4, "tmp = '%s'", tmp);
221    log_msg(4, "ultimate call = '%s'", ultimate_call);
222    asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
223
224    log_to_screen
225        ("Please be patient. Do not be alarmed by on-screen inactivity.");
226    log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
227            what_i_am_doing);
228    strcpy(tmp, command);
229    if (bkpinfo->manual_cd_tray) {
230        p = strstr(tmp, "2>>");
231        if (p) {
232            sprintf(p, "   ");
233            while (*p == ' ') {
234                p++;
235            }
236            for (; *p != ' '; p++) {
237                *p = ' ';
238            }
239        }
240        paranoid_free(command);
241        asprintf(&command, tmp);
242#ifndef _XWIN
243        if (!g_text_mode) {
244            newtSuspend();
245        }
246#endif
247        log_msg(1, "command = '%s'", command);
248        retval += system(command);
249        if (!g_text_mode) {
250            newtResume();
251        }
252        if (retval) {
253            log_msg(2, "Basic call '%s' returned an error.", basic_call);
254            popup_and_OK("Press ENTER to continue.");
255            popup_and_OK
256                ("mkisofs and/or cdrecord returned an error. CD was not created");
257        }
258    }
259    /* if text mode then do the above & RETURN; if not text mode, do this... */
260    else {
261        log_msg(3, "command = '%s'", command);
262//      yes_this_is_a_goto:
263        retval =
264            run_external_binary_with_percentage_indicator_NEW
265            (what_i_am_doing, command);
266    }
267    paranoid_free(command);
268
269    paranoid_free(midway_call);
270    paranoid_free(ultimate_call);
271    paranoid_free(tmp);
272    return (retval);
273}
274
275
276/**
277 * Run a program and log its output (stdout and stderr) to the logfile.
278 * @param program The program to run. Passed to the shell, so you can use pipes etc.
279 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
280 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
281 */
282int run_program_and_log_output(char *program, int debug_level)
283{
284    /*@ buffer ****************************************************** */
285    char *callstr;
286    char *incoming = NULL;
287    char tmp[MAX_STR_LEN * 2];
288
289    /*@ int ********************************************************* */
290    int res;
291    int i;
292    size_t n = 0;
293    int len;
294    bool log_if_failure = FALSE;
295    bool log_if_success = FALSE;
296
297    /*@ pointers *************************************************** */
298    FILE *fin;
299    char *p;
300
301    /*@ end vars *************************************************** */
302
303    assert(program != NULL);
304    if (!program[0]) {
305        log_msg(2, "Warning - asked to run zerolength program");
306        return (1);
307    }
308
309    if (debug_level <= g_loglevel) {
310        log_if_success = TRUE;
311        log_if_failure = TRUE;
312    }
313    asprintf(&callstr,
314            "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
315            program);
316    while ((p = strchr(callstr, '\r'))) {
317        *p = ' ';
318    }
319    while ((p = strchr(callstr, '\n'))) {
320        *p = ' ';
321    }                           /* single '=' is intentional */
322
323
324    len = (int) strlen(program);
325    for (i = 0; i < 35 - len / 2; i++) {
326        tmp[i] = '-';
327    }
328    tmp[i] = '\0';
329    strcat(tmp, " ");
330    strcat(tmp, program);
331    strcat(tmp, " ");
332    for (i = 0; i < 35 - len / 2; i++) {
333        strcat(tmp, "-");
334    }
335    res = system(callstr);
336    if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
337        log_msg(0, "running: %s", callstr);
338        log_msg(0,
339                "--------------------------------start of output-----------------------------");
340    }
341    paranoid_free(callstr);
342
343    if (log_if_failure
344        &&
345        system
346        ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
347    {
348        log_OS_error("Command failed");
349    }
350    unlink("/tmp/mondo-run-prog-thing.err");
351    fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
352    if (fin) {
353        for (getline(&incoming, &n, fin); !feof(fin);
354             getline(&incoming, &n, fin)) {
355            /* patch by Heiko Schlittermann */
356            p = incoming;
357            while (p && *p) {
358                if ((p = strchr(p, '%'))) {
359                    memmove(p, p + 1, strlen(p) + 1);
360                    p += 2;
361                }
362            }
363            /* end of patch */
364            strip_spaces(incoming);
365            if ((res == 0 && log_if_success)
366                || (res != 0 && log_if_failure)) {
367                log_msg(0, incoming);
368            }
369        }
370        paranoid_free(incoming);
371        paranoid_fclose(fin);
372    }
373    unlink("/tmp/mondo-run-prog-thing.tmp");
374    if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
375        log_msg(0,
376                "--------------------------------end of output------------------------------");
377        if (res) {
378            log_msg(0, "...ran with res=%d", res);
379        } else {
380            log_msg(0, "...ran just fine. :-)");
381        }
382    }
383//  else
384//    { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
385    return (res);
386}
387
388
389/**
390 * Run a program and log its output to the screen.
391 * @param basic_call The program to run.
392 * @param what_i_am_doing The title of the evalcall form.
393 * @return The return value of the command (varies, but 0 almost always means success).
394 * @see run_program_and_log_output
395 * @see log_to_screen
396 */
397int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
398{
399    /*@ int ******************************************************** */
400    int retval = 0;
401    int res = 0;
402    int i;
403
404    /*@ pointers **************************************************** */
405    FILE *fin;
406
407    /*@ buffers **************************************************** */
408    char *tmp;
409    char *command;
410    char *lockfile;
411
412    /*@ end vars *************************************************** */
413
414    assert_string_is_neither_NULL_nor_zerolength(basic_call);
415
416    asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
417    mkstemp(lockfile);
418    asprintf(&command,
419            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
420            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
421    open_evalcall_form(what_i_am_doing);
422    asprintf(&tmp, "Executing %s", basic_call);
423    log_msg(2, tmp);
424    paranoid_free(tmp);
425
426    if (!(fin = popen(command, "r"))) {
427        log_OS_error("Unable to popen-in command");
428        asprintf(&tmp, "Failed utterly to call '%s'", command);
429        log_to_screen(tmp);
430        paranoid_free(tmp);
431        paranoid_free(lockfile);
432        paranoid_free(command);
433        return (1);
434    }
435    paranoid_free(command);
436
437    if (!does_file_exist(lockfile)) {
438        log_to_screen("Waiting for external binary to start");
439        for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
440            log_msg(3, "Waiting for lockfile %s to exist", lockfile);
441        }
442    }
443    /* This works on Newt, and it gives quicker updates. */
444    for (; does_file_exist(lockfile); sleep(1)) {
445        log_file_end_to_screen(MONDO_LOGFILE, "");
446        update_evalcall_form(1);
447    }
448    /* Evaluate the status returned by pclose to get the exit code of the called program. */
449    errno = 0;
450    res = pclose(fin);
451    /* Log actual pclose errors. */
452    if (errno)
453        log_msg(5, "pclose err: %d", errno);
454    /* Check if we have a valid status. If we do, extract the called program's exit code. */
455    /* If we don't, highlight this fact by returning -1. */
456    if (WIFEXITED(res)) {
457        retval = WEXITSTATUS(res);
458    } else {
459        retval = -1;
460    }
461    close_evalcall_form();
462    unlink(lockfile);
463    paranoid_free(lockfile);
464
465    return (retval);
466}
467
468
469/**
470 * Apparently used. @bug This has a purpose, but what?
471 */
472#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
473#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
474
475
476
477
478int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
479{
480// if dir=='w' then copy from orig to archived
481// if dir=='r' then copy from archived to orig
482    char *tmp;
483    char *buf;
484    long int bytes_to_be_read, bytes_read_in, bytes_written_out =
485        0, bufcap, subsliceno = 0;
486    int retval = 0;
487    FILE *fin;
488    FILE *fout;
489    FILE *ftmp;
490
491    log_msg(5, "Opening.");
492    bufcap = 256L * 1024L;
493    if (!(buf = malloc(bufcap))) {
494        fatal_error("Failed to malloc() buf");
495    }
496
497    if (direction == 'w') {
498        fin = f_orig;
499        fout = f_archived;
500        asprintf(&tmp, "%-64s", PIMP_START_SZ);
501        if (fwrite(tmp, 1, 64, fout) != 64) {
502            fatal_error("Can't write the introductory block");
503        }
504        paranoid_free(tmp);
505
506        while (1) {
507            bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
508            if (bytes_read_in == 0) {
509                break;
510            }
511            asprintf(&tmp, "%-64ld", bytes_read_in);
512            if (fwrite(tmp, 1, 64, fout) != 64) {
513                fatal_error("Cannot write introductory block");
514            }
515            paranoid_free(tmp);
516
517            log_msg(7,
518                    "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
519                    subsliceno, bytes_read_in, bytes_to_be_read);
520            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
521            asprintf(&tmp, "%-64ld", subsliceno);
522            if (fwrite(tmp, 1, 64, fout) != 64) {
523                fatal_error("Cannot write post-thingy block");
524            }
525            paranoid_free(tmp);
526
527            log_msg(7, "Subslice #%d written OK", subsliceno);
528            subsliceno++;
529        }
530        asprintf(&tmp, "%-64ld", 0L);
531        if (fwrite(tmp, 1, 64L, fout) != 64L) {
532            fatal_error("Cannot write final introductory block");
533        }
534    } else {
535        fin = f_archived;
536        fout = f_orig;
537        if (!(tmp = malloc(64L))) {
538            fatal_error("Failed to malloc() tmp");
539        }
540        if (fread(tmp, 1, 64L, fin) != 64L) {
541            fatal_error("Cannot read the introductory block");
542        }
543        log_msg(5, "tmp is %s", tmp);
544        if (!strstr(tmp, PIMP_START_SZ)) {
545            fatal_error("Can't find intro blk");
546        }
547        if (fread(tmp, 1, 64L, fin) != 64L) {
548            fatal_error("Cannot read introductory blk");
549        }
550        bytes_to_be_read = atol(tmp);
551        while (bytes_to_be_read > 0) {
552            log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
553                    bytes_to_be_read);
554            bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
555            if (bytes_read_in != bytes_to_be_read) {
556                fatal_error
557                    ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
558            }
559            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
560            if (fread(tmp, 1, 64, fin) != 64) {
561                fatal_error("Cannot read post-thingy block");
562            }
563            if (atol(tmp) != subsliceno) {
564                log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
565                        subsliceno, atol(tmp), tmp);
566            }
567            log_msg(7, "Subslice #%ld read OK", subsliceno);
568            subsliceno++;
569            if (fread(tmp, 1, 64, fin) != 64) {
570                fatal_error("Cannot read introductory block");
571            }
572            bytes_to_be_read = atol(tmp);
573        }
574    }
575
576//  log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
577
578    if (direction == 'w') {
579        asprintf(&tmp, "%-64s", PIMP_END_SZ);
580        if (fwrite(tmp, 1, 64, fout) != 64) {
581            fatal_error("Can't write the final block");
582        }
583        paranoid_free(tmp);
584    } else {
585        log_msg(1, "tmpA is %s", tmp);
586        if (!strstr(tmp, PIMP_END_SZ)) {
587            if (fread(tmp, 1, 64, fin) != 64) {
588                fatal_error("Can't read the final block");
589            }
590            log_msg(5, "tmpB is %s", tmp);
591            if (!strstr(tmp, PIMP_END_SZ)) {
592                ftmp = fopen("/tmp/out.leftover", "w");
593                bytes_read_in = fread(tmp, 1, 64, fin);
594                log_msg(1, "bytes_read_in = %ld", bytes_read_in);
595//      if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
596                fwrite(tmp, 1, bytes_read_in, ftmp);
597                paranoid_free(tmp);
598               
599                if (!(tmp = malloc(512))) {
600                    fatal_error("Failed to malloc() tmp");
601                }
602                fread(tmp, 1, 512, fin);
603                log_msg(0, "tmp = '%s'", tmp);
604                fwrite(tmp, 1, 512, ftmp);
605                fclose(ftmp);
606                fatal_error("Missing terminating block");
607            }
608        }
609        paranoid_free(tmp);
610    }
611
612    paranoid_free(buf);
613    log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
614    return (retval);
615}
616
617/**
618 * Feed @p input_device through ntfsclone to @p output_fname.
619 * @param input_device The device to image.
620 * @param output_fname The file to write.
621 * @return 0 for success, nonzero for failure.
622 */
623int feed_into_ntfsprog(char *input_device, char *output_fname)
624{
625// BACKUP
626    int res = -1;
627    char*command;
628
629    if (!does_file_exist(input_device)) {
630        fatal_error("input device does not exist");
631    }
632    if ( !find_home_of_exe("ntfsclone")) {
633        fatal_error("ntfsclone not found");
634    }
635    malloc_string(command);
636    sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
637    res = run_program_and_log_output(command, 5);
638    paranoid_free(command);
639    unlink(output_fname);
640    return (res);
641}
642
643
644int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
645{
646
647    /*@ int *************************************************************** */
648    int res = 0;
649    int percentage = 0;
650    int maxpc = 0;
651    int pcno = 0;
652    int last_pcno = 0;
653
654    /*@ buffers *********************************************************** */
655    char *command;
656    char *tempfile;
657    /*@ pointers ********************************************************** */
658    FILE *pin;
659
660    assert_string_is_neither_NULL_nor_zerolength(cmd);
661
662    asprintf(&tempfile,
663           call_program_and_get_last_line_of_output
664           ("mktemp -q /tmp/mondo.XXXXXXXX"));
665    asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
666            tempfile);
667    log_msg(3, command);
668    open_evalcall_form(tt);
669
670    if (!(pin = popen(command, "r"))) {
671        log_OS_error("fmt err");
672        paranoid_free(command);
673        paranoid_free(tempfile);
674        return (1);
675    }
676    paranoid_free(command);
677
678    maxpc = 100;
679// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
680    for (sleep(1); does_file_exist(tempfile); sleep(1)) {
681        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
682        if (pcno < 0 || pcno > 100) {
683            log_msg(5, "Weird pc#");
684            continue;
685        }
686        percentage = pcno * 100 / maxpc;
687        if (pcno <= 5 && last_pcno > 40) {
688            close_evalcall_form();
689            open_evalcall_form("Verifying...");
690        }
691        last_pcno = pcno;
692        update_evalcall_form(percentage);
693    }
694// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
695    close_evalcall_form();
696    if (pclose(pin)) {
697        res++;
698        log_OS_error("Unable to pclose");
699    }
700    unlink(tempfile);
701    paranoid_free(tempfile);
702    return (res);
703}
704
705
706void *run_prog_in_bkgd_then_exit(void *info)
707{
708    char *sz_command;
709    static int res = 4444;
710
711    res = 999;
712    sz_command = (char *) info;
713    log_msg(4, "sz_command = '%s'", sz_command);
714    res = system(sz_command);
715    if (res > 256 && res != 4444) {
716        res = res / 256;
717    }
718    log_msg(4, "child res = %d", res);
719    sz_command[0] = '\0';
720    pthread_exit((void *) (&res));
721}
722
723
724int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
725{
726
727    /*@ int *************************************************************** */
728    int res = 0;
729    int percentage = 0;
730    int maxpc = 100;
731    int pcno = 0;
732    int last_pcno = 0;
733    int counter = 0;
734
735    /*@ buffers *********************************************************** */
736    char *command;
737    /*@ pointers ********************************************************** */
738    static int chldres = 0;
739    int *pchild_result;
740    pthread_t childthread;
741
742    pchild_result = &chldres;
743    assert_string_is_neither_NULL_nor_zerolength(cmd);
744    assert_string_is_neither_NULL_nor_zerolength(tt);
745    *pchild_result = 999;
746
747    asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
748    log_msg(3, "command = '%s'", command);
749    if ((res =
750         pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
751                        (void *) command))) {
752        fatal_error("Unable to create an archival thread");
753    }
754
755    log_msg(8, "Parent running");
756    open_evalcall_form(tt);
757    for (sleep(1); command[0] != '\0'; sleep(1)) {
758        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
759        if (pcno <= 0 || pcno > 100) {
760            log_msg(8, "Weird pc#");
761            continue;
762        }
763        percentage = pcno * 100 / maxpc;
764        if (pcno <= 5 && last_pcno >= 40) {
765            close_evalcall_form();
766            open_evalcall_form("Verifying...");
767        }
768        if (counter++ >= 5) {
769            counter = 0;
770            log_file_end_to_screen(MONDO_LOGFILE, "");
771        }
772        last_pcno = pcno;
773        update_evalcall_form(percentage);
774    }
775    paranoid_free(command);
776
777    log_file_end_to_screen(MONDO_LOGFILE, "");
778    close_evalcall_form();
779    pthread_join(childthread, (void *) (&pchild_result));
780    if (pchild_result) {
781        res = *pchild_result;
782    } else {
783        res = 666;
784    }
785    log_msg(3, "Parent res = %d", res);
786    return (res);
787}
788
789
790/**
791 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
792 * @param input_fifo The ntfsclone file to read.
793 * @param output_device Where to put the output.
794 * @return The return value of ntfsclone (0 for success).
795 */
796int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
797{
798// RESTORE
799    int res = -1;
800    char *command;
801
802    if ( !find_home_of_exe("ntfsclone")) {
803        fatal_error("ntfsclone not found");
804    }
805    asprintf(&command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
806    res = run_program_and_log_output(command, 5);
807    paranoid_free(command);
808    return (res);
809}
Note: See TracBrowser for help on using the repository browser.