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

Last change on this file since 687 was 687, checked in by bcornec, 13 years ago

merge -r671:686 $SVN_M/branches/stable

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