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

Last change on this file since 1316 was 1316, checked in by Bruno Cornec, 13 years ago

MONDO_LOGFILE is rather a char* exported by each main program (consolidation of those mecanisms in .h files with ps_*)

  • Property svn:keywords set to Id
File size: 22.4 KB
Line 
1/* libmondo-fork.c
2   $Id: libmondo-fork.c 1316 2007-04-16 14:50:28Z 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 1316 2007-04-16 14:50:28Z 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,
342            "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
343            program);
344    while ((p = strchr(callstr, '\r'))) {
345        *p = ' ';
346    }
347    while ((p = strchr(callstr, '\n'))) {
348        *p = ' ';
349    }                           /* single '=' is intentional */
350
351
352    len = (int) strlen(program);
353    for (i = 0; i < 35 - len / 2; i++) {
354        tmp[i] = '-';
355    }
356    tmp[i] = '\0';
357    strcat(tmp, " ");
358    strcat(tmp, program);
359    strcat(tmp, " ");
360    for (i = 0; i < 35 - len / 2; i++) {
361        strcat(tmp, "-");
362    }
363    strcpy(initial_label, tmp);
364    res = system(callstr);
365    if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
366        log_msg(0, "running: %s", callstr);
367        log_msg(0,
368                "--------------------------------start of output-----------------------------");
369    }
370    if (log_if_failure
371        &&
372        system
373        ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
374    {
375        log_OS_error("Command failed");
376    }
377    unlink("/tmp/mondo-run-prog-thing.err");
378    fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
379    if (fin) {
380        for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
381             fgets(incoming, MAX_STR_LEN, fin)) {
382            /* patch by Heiko Schlittermann */
383            p = incoming;
384            while (p && *p) {
385                if ((p = strchr(p, '%'))) {
386                    memmove(p, p + 1, strlen(p) + 1);
387                    p += 2;
388                }
389            }
390            /* end of patch */
391            strip_spaces(incoming);
392            if ((res == 0 && log_if_success)
393                || (res != 0 && log_if_failure)) {
394                log_msg(0, incoming);
395            }
396        }
397        paranoid_fclose(fin);
398    }
399    unlink("/tmp/mondo-run-prog-thing.tmp");
400    if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
401        log_msg(0,
402                "--------------------------------end of output------------------------------");
403        if (res) {
404            log_msg(0, "...ran with res=%d", res);
405        } else {
406            log_msg(0, "...ran just fine. :-)");
407        }
408    }
409//  else
410//    { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
411    return (res);
412}
413
414
415
416/**
417 * Run a program and log its output to the screen.
418 * @param basic_call The program to run.
419 * @param what_i_am_doing The title of the evalcall form.
420 * @return The return value of the command (varies, but 0 almost always means success).
421 * @see run_program_and_log_output
422 * @see log_to_screen
423 */
424int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
425{
426    /*@ int ******************************************************** */
427    int retval = 0;
428    int res = 0;
429    int i;
430
431    /*@ pointers **************************************************** */
432    FILE *fin;
433
434    /*@ buffers **************************************************** */
435    char tmp[MAX_STR_LEN * 2];
436    char command[MAX_STR_LEN * 2];
437    char lockfile[MAX_STR_LEN];
438
439    /*@ end vars *************************************************** */
440
441    assert_string_is_neither_NULL_nor_zerolength(basic_call);
442
443    sprintf(lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
444    mkstemp(lockfile);
445    sprintf(command,
446            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
447            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
448    open_evalcall_form(what_i_am_doing);
449    sprintf(tmp, "Executing %s", basic_call);
450    log_msg(2, tmp);
451    if (!(fin = popen(command, "r"))) {
452        log_OS_error("Unable to popen-in command");
453        sprintf(tmp, "Failed utterly to call '%s'", command);
454        log_to_screen(tmp);
455        return (1);
456    }
457    if (!does_file_exist(lockfile)) {
458        log_to_screen("Waiting for external binary to start");
459        for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
460            log_msg(3, "Waiting for lockfile %s to exist", lockfile);
461        }
462    }
463#ifdef _XWIN
464    /* This only can update when newline goes into the file,
465       but it's *much* prettier/faster on Qt. */
466    while (does_file_exist(lockfile)) {
467        while (!feof(fin)) {
468            if (!fgets(tmp, 512, fin))
469                break;
470            log_to_screen(tmp);
471        }
472        usleep(500000);
473    }
474#else
475    /* This works on Newt, and it gives quicker updates. */
476    for (; does_file_exist(lockfile); sleep(1)) {
477        log_file_end_to_screen(MONDO_LOGFILE, "");
478        update_evalcall_form(1);
479    }
480#endif
481    /* Evaluate the status returned by pclose to get the exit code of the called program. */
482    errno = 0;
483    res = pclose(fin);
484    /* Log actual pclose errors. */
485    if (errno) log_msg(5, "pclose err: %d", errno);
486    /* Check if we have a valid status. If we do, extract the called program's exit code. */
487    /* If we don't, highlight this fact by returning -1. */
488    if (WIFEXITED(res)) {
489        retval = WEXITSTATUS(res);
490    } else {
491        retval = -1;
492    }
493    close_evalcall_form();
494    unlink(lockfile);
495    return (retval);
496}
497
498
499
500
501/**
502 * Apparently unused. @bug This has a purpose, but what?
503 */
504#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
505#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
506
507
508
509
510int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
511{
512// if dir=='w' then copy from orig to archived
513// if dir=='r' then copy from archived to orig
514    char *tmp;
515    char *buf;
516    long int bytes_to_be_read, bytes_read_in, bytes_written_out =
517        0, bufcap, subsliceno = 0;
518    int retval = 0;
519    FILE *fin;
520    FILE *fout;
521    FILE *ftmp;
522
523    log_msg(5, "Opening.");
524    malloc_string(tmp);
525    tmp[0] = '\0';
526    bufcap = 256L * 1024L;
527    if (!(buf = malloc(bufcap))) {
528        fatal_error("Failed to malloc() buf");
529    }
530
531    if (direction == 'w') {
532        fin = f_orig;
533        fout = f_archived;
534        sprintf(tmp, "%-64s", PIMP_START_SZ);
535        if (fwrite(tmp, 1, 64, fout) != 64) {
536            fatal_error("Can't write the introductory block");
537        }
538        while (1) {
539            bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
540            if (bytes_read_in == 0) {
541                break;
542            }
543            sprintf(tmp, "%-64ld", bytes_read_in);
544            if (fwrite(tmp, 1, 64, fout) != 64) {
545                fatal_error("Cannot write introductory block");
546            }
547            log_msg(7,
548                    "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
549                    subsliceno, bytes_read_in, bytes_to_be_read);
550            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
551            sprintf(tmp, "%-64ld", subsliceno);
552            if (fwrite(tmp, 1, 64, fout) != 64) {
553                fatal_error("Cannot write post-thingy block");
554            }
555            log_msg(7, "Subslice #%d written OK", subsliceno);
556            subsliceno++;
557        }
558        sprintf(tmp, "%-64ld", 0L);
559        if (fwrite(tmp, 1, 64L, fout) != 64L) {
560            fatal_error("Cannot write final introductory block");
561        }
562    } else {
563        fin = f_archived;
564        fout = f_orig;
565        if (fread(tmp, 1, 64L, fin) != 64L) {
566            fatal_error("Cannot read the introductory block");
567        }
568        log_msg(5, "tmp is %s", tmp);
569        if (!strstr(tmp, PIMP_START_SZ)) {
570            fatal_error("Can't find intro blk");
571        }
572        if (fread(tmp, 1, 64L, fin) != 64L) {
573            fatal_error("Cannot read introductory blk");
574        }
575        bytes_to_be_read = atol(tmp);
576        while (bytes_to_be_read > 0) {
577            log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
578                    bytes_to_be_read);
579            bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
580            if (bytes_read_in != bytes_to_be_read) {
581                fatal_error
582                    ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
583            }
584            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
585            if (fread(tmp, 1, 64, fin) != 64) {
586                fatal_error("Cannot read post-thingy block");
587            }
588            if (atol(tmp) != subsliceno) {
589                log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
590                        subsliceno, atol(tmp), tmp);
591            }
592            log_msg(7, "Subslice #%ld read OK", subsliceno);
593            subsliceno++;
594            if (fread(tmp, 1, 64, fin) != 64) {
595                fatal_error("Cannot read introductory block");
596            }
597            bytes_to_be_read = atol(tmp);
598        }
599    }
600
601//  log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
602
603    if (direction == 'w') {
604        sprintf(tmp, "%-64s", PIMP_END_SZ);
605        if (fwrite(tmp, 1, 64, fout) != 64) {
606            fatal_error("Can't write the final block");
607        }
608    } else {
609        log_msg(1, "tmpA is %s", tmp);
610        if (!strstr(tmp, PIMP_END_SZ)) {
611            if (fread(tmp, 1, 64, fin) != 64) {
612                fatal_error("Can't read the final block");
613            }
614            log_msg(5, "tmpB is %s", tmp);
615            if (!strstr(tmp, PIMP_END_SZ)) {
616                ftmp = fopen("/tmp/out.leftover", "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    strcpy(tempfile,
696           call_program_and_get_last_line_of_output
697           ("mktemp -q /tmp/mondo.XXXXXXXX"));
698    sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
699            tempfile);
700    log_msg(3, command);
701    open_evalcall_form(title);
702    if (!(pin = popen(command, "r"))) {
703        log_OS_error("fmt err");
704        return (1);
705    }
706    maxpc = 100;
707// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
708    for (sleep(1); does_file_exist(tempfile); sleep(1)) {
709        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
710        if (pcno < 0 || pcno > 100) {
711            log_msg(5, "Weird pc#");
712            continue;
713        }
714        percentage = pcno * 100 / maxpc;
715        if (pcno <= 5 && last_pcno > 40) {
716            close_evalcall_form();
717            strcpy(title, "Verifying...");
718            open_evalcall_form(title);
719        }
720        last_pcno = pcno;
721        update_evalcall_form(percentage);
722    }
723// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
724    close_evalcall_form();
725    if (pclose(pin)) {
726        res++;
727        log_OS_error("Unable to pclose");
728    }
729    unlink(tempfile);
730    paranoid_free(command);
731    paranoid_free(tempfile);
732    paranoid_free(title);
733    return (res);
734}
735
736
737
738
739void *run_prog_in_bkgd_then_exit(void *info)
740{
741    char *sz_command;
742    static int res = 4444;
743
744    res = 999;
745    sz_command = (char *) info;
746    log_msg(4, "sz_command = '%s'", sz_command);
747    res = system(sz_command);
748    if (res > 256 && res != 4444) {
749        res = res / 256;
750    }
751    log_msg(4, "child res = %d", res);
752    sz_command[0] = '\0';
753    pthread_exit((void *) (&res));
754}
755
756
757
758
759int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
760{
761
762    /*@ int *************************************************************** */
763    int res = 0;
764    int percentage = 0;
765    int maxpc = 100;
766    int pcno = 0;
767    int last_pcno = 0;
768    int counter = 0;
769
770    /*@ buffers *********************************************************** */
771    char *command;
772    char *title;
773    /*@ pointers ********************************************************** */
774    static int chldres = 0;
775    int *pchild_result;
776    pthread_t childthread;
777
778    pchild_result = &chldres;
779    assert_string_is_neither_NULL_nor_zerolength(cmd);
780    assert_string_is_neither_NULL_nor_zerolength(tt);
781    *pchild_result = 999;
782
783    malloc_string(title);
784    malloc_string(command);
785    strcpy(title, tt);
786    sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
787    log_msg(3, "command = '%s'", command);
788    if ((res =
789         pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
790                        (void *) command))) {
791        fatal_error("Unable to create an archival thread");
792    }
793
794    log_msg(8, "Parent running");
795    open_evalcall_form(title);
796    for (sleep(1); command[0] != '\0'; sleep(1)) {
797        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
798        if (pcno <= 0 || pcno > 100) {
799            log_msg(8, "Weird pc#");
800            continue;
801        }
802        percentage = pcno * 100 / maxpc;
803        if (pcno <= 5 && last_pcno >= 40) {
804            close_evalcall_form();
805            strcpy(title, "Verifying...");
806            open_evalcall_form(title);
807        }
808        if (counter++ >= 5) {
809            counter = 0;
810            log_file_end_to_screen(MONDO_LOGFILE, "");
811        }
812        last_pcno = pcno;
813        update_evalcall_form(percentage);
814    }
815    log_file_end_to_screen(MONDO_LOGFILE, "");
816    close_evalcall_form();
817    pthread_join(childthread, (void *) (&pchild_result));
818    if (pchild_result) {
819        res = *pchild_result;
820    } else {
821        res = 666;
822    }
823    log_msg(3, "Parent res = %d", res);
824    paranoid_free(command);
825    paranoid_free(title);
826    return (res);
827}
828
829
830
831
832/**
833 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
834 * @param input_fifo The ntfsclone file to read.
835 * @param output_device Where to put the output.
836 * @return The return value of ntfsclone (0 for success).
837 */
838int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
839{
840// RESTORE
841    int res = -1;
842    char *command;
843
844    if ( !find_home_of_exe("ntfsclone")) {
845        fatal_error("ntfsclone not found");
846    }
847    malloc_string(command);
848    sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
849    res = run_program_and_log_output(command, 5);
850    paranoid_free(command);
851    return (res);
852}
Note: See TracBrowser for help on using the repository browser.