Changeset 130 in MondoRescue for trunk


Ignore:
Timestamp:
Nov 22, 2005, 5:47:53 PM (18 years ago)
Author:
bcornec
Message:

memory management revision on libmondo-fork.c

Location:
trunk/mondo/mondo/common
Files:
2 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/mondo/common/libmondo-fork.c

    r89 r130  
    1 /* libmondo-fork.c
    2    $Id$
    3 
    4 - subroutines for handling forking/pthreads/etc.
    5 
    6 
    7 06/20/2004
    8 - create fifo /var/log/partimagehack-debug.log and empty it
    9   to keep ramdisk from filling up
    10 
    11 04/13/2004
    12 - >= should be <= g_loglevel
    13 
    14 11/15/2003
    15 - changed a few []s to char*s
    16  
    17 10/12
    18 - rewrote partimagehack handling (multiple fifos, chunks, etc.)
    19 
    20 10/11
    21 - partimagehack now has debug level of N (set in my-stuff.h)
    22 
    23 10/08
    24 - call to partimagehack when restoring will now log errors to /var/log/....log
    25 
    26 10/06
    27 - cleaned up logging a bit
    28 
    29 09/30
    30 - line 735 - missing char* cmd in sprintf()
    31 
    32 09/28
    33 - added run_external_binary_with_percentage_indicator()
    34 - rewritten eval_call_to_make_ISO()
    35 
    36 09/18
    37 - call mkstemp instead of mktemp
    38 
    39 09/13
    40 - major NTFS hackage
    41 
    42 09/12
    43 - paranoid_system("rm -f /tmp/ *PARTIMAGE*") before calling partimagehack
    44 
    45 09/11
    46 - forward-ported unbroken feed_*_partimage() subroutines
    47   from early August 2003
    48 
    49 09/08
    50 - detect & use partimagehack if it exists
    51 
    52 09/05
    53 - finally finished partimagehack hack :)
    54 
    55 07/04
    56 - added subroutines to wrap around partimagehack
    57 
    58 04/27
    59 - don't echo (...res=%d...) at end of log_it()
    60   unnecessarily
    61 - replace newtFinished() and newtInit() with
    62   newtSuspend() and newtResume()
    63 
    64 04/24
    65 - added some assert()'s and log_OS_error()'s
    66 
    67 04/09
    68 - cleaned up run_program_and_log_output()
    69 
    70 04/07
    71 - cleaned up code a bit
    72 - let run_program_and_log_output() accept -1 (only log if _no error_)
    73 
    74 01/02/2003
    75 - in eval_call_to_make_ISO(), append output to MONDO_LOGFILE
    76   instead of a temporary stderr text file
    77 
    78 12/10
    79 - patch by Heiko Schlittermann to handle % chars in issue.net
    80 
    81 11/18
    82 - if mkisofs in eval_call_to_make_ISO() returns an error then return it,
    83   whether ISO was created or not
    84 
    85 10/30
    86 - if mkisofs in eval_call_to_make_ISO() returns an error then find out if
    87   the output (ISO) file has been created; if it has then return 0 anyway
    88 
    89 08/01 - 09/30
    90 - run_program_and_log_output() now takes boolean operator to specify
    91   whether it will log its activities in the event of _success_
    92 - system() now includes 2>/dev/null
    93 - enlarged some tmp[]'s
    94 - added run_program_and_log_to_screen() and run_program_and_log_output()
    95 
    96 07/24
    97 - created
     1/* $Id$
     2  subroutines for handling forking/pthreads/etc.
    983*/
    994
     
    12631{
    12732    /*@ buffers ***************************************************** */
    128     static char result[512];
    129     char *tmp;
     33    static char *result = NULL;
     34    char *tmp = NULL;
    13035
    13136    /*@ pointers **************************************************** */
    13237    FILE *fin;
    13338
    134     /*@ initialize data ********************************************* */
    135     malloc_string(tmp);
    136     result[0] = '\0';
    137     tmp[0] = '\0';
     39    int n = 0;
    13840
    13941    /*@******************************************************************** */
     
    14143    assert_string_is_neither_NULL_nor_zerolength(call);
    14244    if ((fin = popen(call, "r"))) {
    143         for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
    144              fgets(tmp, MAX_STR_LEN, fin)) {
     45        for (getline(&tmp, &n, fin); !feof(fin);
     46             getline(&tmp, &n, fin)) {
    14547            if (strlen(tmp) > 1) {
    146                 strcpy(result, tmp);
     48                if (result != NULL) {
     49                    paranoid_free(result);
     50                }
     51                asprintf(&result, tmp);
    14752            }
    14853        }
     
    15257    }
    15358    strip_spaces(result);
     59    paranoid_free(tmp);
    15460    return (result);
    15561}
    156 
    157 
    158 
    159 
    160 
    16162
    16263#define MONDO_POPMSG  "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
     
    19091
    19192    /*@ buffers      *** */
    192     char *midway_call, *ultimate_call, *tmp, *command, *incoming,
    193         *old_stderr, *cd_number_str;
     93    char *midway_call, *ultimate_call, *tmp, *command,
     94        *cd_number_str;
    19495    char *p;
    19596
     
    210111        fatal_error("Cannot malloc tmp");
    211112    }
    212     if (!(command = malloc(1200))) {
    213         fatal_error("Cannot malloc command");
    214     }
    215     malloc_string(incoming);
    216     malloc_string(old_stderr);
    217     malloc_string(cd_number_str);
    218 
    219     incoming[0] = '\0';
    220     old_stderr[0] = '\0';
    221 
    222     sprintf(cd_number_str, "%d", cd_no);
     113
     114    asprintf(&cd_number_str, "%d", cd_no);
    223115    resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
    224116    resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
     117    paranoid_free(cd_number_str);
     118
    225119    resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
    226120    log_msg(4, "basic call = '%s'", basic_call);
     
    228122    log_msg(4, "tmp = '%s'", tmp);
    229123    log_msg(4, "ultimate call = '%s'", ultimate_call);
    230     sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
     124    asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
    231125
    232126    log_to_screen
     
    246140            }
    247141        }
    248         strcpy(command, tmp);
     142        paranoid_free(command);
     143        asprintf(&command, tmp);
    249144#ifndef _XWIN
    250145        if (!g_text_mode) {
     
    272167            (what_i_am_doing, command);
    273168    }
     169    paranoid_free(command);
    274170
    275171    paranoid_free(midway_call);
    276172    paranoid_free(ultimate_call);
    277173    paranoid_free(tmp);
    278     paranoid_free(command);
    279     paranoid_free(incoming);
    280     paranoid_free(old_stderr);
    281     paranoid_free(cd_number_str);
    282 /*
    283   if (bkpinfo->backup_media_type == dvd && !bkpinfo->please_dont_eject_when_restoring)
    284     {
    285       log_msg(3, "Ejecting DVD device");
    286       eject_device(bkpinfo->media_device);
    287     }
    288 */
    289174    return (retval);
    290175}
    291 
    292 
    293176
    294177
     
    302185{
    303186    /*@ buffer ****************************************************** */
    304     char callstr[MAX_STR_LEN * 2];
    305     char incoming[MAX_STR_LEN * 2];
     187    char *callstr;
     188    char *incoming = NULL;
    306189    char tmp[MAX_STR_LEN * 2];
    307     char initial_label[MAX_STR_LEN * 2];
    308190
    309191    /*@ int ********************************************************* */
    310192    int res;
    311193    int i;
     194    int n = 0;
    312195    int len;
    313196    bool log_if_failure = FALSE;
     
    325208        return (1);
    326209    }
    327 //  if (debug_level == TRUE) { debug_level=5; }
    328 
    329     //  assert_string_is_neither_NULL_nor_zerolength(program);
    330210
    331211    if (debug_level <= g_loglevel) {
     
    333213        log_if_failure = TRUE;
    334214    }
    335     sprintf(callstr,
     215    asprintf(&callstr,
    336216            "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
    337217            program);
     
    355235        strcat(tmp, "-");
    356236    }
    357     strcpy(initial_label, tmp);
    358237    res = system(callstr);
    359238    if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
     
    362241                "--------------------------------start of output-----------------------------");
    363242    }
     243    paranoid_free(callstr);
     244
    364245    if (log_if_failure
    365246        &&
     
    372253    fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
    373254    if (fin) {
    374         for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
    375              fgets(incoming, MAX_STR_LEN, fin)) {
     255        for (getline(&incoming, &n, fin); !feof(fin);
     256             getline(&incoming, &n, fin)) {
    376257            /* patch by Heiko Schlittermann */
    377258            p = incoming;
     
    389270            }
    390271        }
     272        paranoid_free(incoming);
    391273        paranoid_fclose(fin);
    392274    }
     
    407289
    408290
    409 
    410291/**
    411292 * Run a program and log its output to the screen.
     
    421302    int retval = 0;
    422303    int res = 0;
     304    int n = 0;
    423305    int i;
    424306
     
    427309
    428310    /*@ buffers **************************************************** */
    429     char tmp[MAX_STR_LEN * 2];
    430     char command[MAX_STR_LEN * 2];
    431     char lockfile[MAX_STR_LEN];
     311    char *tmp;
     312    char *command;
     313    char *lockfile;
    432314
    433315    /*@ end vars *************************************************** */
     
    435317    assert_string_is_neither_NULL_nor_zerolength(basic_call);
    436318
    437     sprintf(lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
     319    asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
    438320    mkstemp(lockfile);
    439     sprintf(command,
     321    asprintf(&command,
    440322            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
    441323            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
    442324    open_evalcall_form(what_i_am_doing);
    443     sprintf(tmp, "Executing %s", basic_call);
     325    asprintf(&tmp, "Executing %s", basic_call);
    444326    log_msg(2, tmp);
     327    paranoid_free(tmp);
     328
    445329    if (!(fin = popen(command, "r"))) {
    446330        log_OS_error("Unable to popen-in command");
    447         sprintf(tmp, "Failed utterly to call '%s'", command);
     331        asprintf(&tmp, "Failed utterly to call '%s'", command);
    448332        log_to_screen(tmp);
     333        paranoid_free(tmp);
     334        paranoid_free(lockfile);
     335        paranoid_free(command);
    449336        return (1);
    450337    }
     338    paranoid_free(command);
     339
    451340    if (!does_file_exist(lockfile)) {
    452341        log_to_screen("Waiting for external binary to start");
     
    458347    /* This only can update when newline goes into the file,
    459348       but it's *much* prettier/faster on Qt. */
     349    tmp = NULL;
    460350    while (does_file_exist(lockfile)) {
    461351        while (!feof(fin)) {
    462             if (!fgets(tmp, 512, fin))
     352            if (!getline(&tmp, &n, fin))
    463353                break;
    464354            log_to_screen(tmp);
     
    466356        usleep(500000);
    467357    }
     358    paranoid_free(tmp);
    468359#else
    469360    /* This works on Newt, and it gives quicker updates. */
     
    488379    close_evalcall_form();
    489380    unlink(lockfile);
     381    paranoid_free(lockfile);
     382
    490383    return (retval);
    491384}
    492 
    493 
    494 
    495385
    496386
     
    531421
    532422/**
    533  * Apparently unused. @bug This has a purpose, but what?
     423 * Apparently used. @bug This has a purpose, but what?
    534424 */
    535425#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
     
    560450
    561451    log_msg(5, "Opening.");
    562     malloc_string(tmp);
    563     tmp[0] = '\0';
    564452    bufcap = 256L * 1024L;
    565453    if (!(buf = malloc(bufcap))) {
     
    570458        fin = f_orig;
    571459        fout = f_archived;
    572         sprintf(tmp, "%-64s", PIMP_START_SZ);
     460        asprintf(&tmp, "%-64s", PIMP_START_SZ);
    573461        if (fwrite(tmp, 1, 64, fout) != 64) {
    574462            fatal_error("Can't write the introductory block");
    575463        }
     464        paranoid_free(tmp);
     465
    576466        while (1) {
    577467            bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
     
    579469                break;
    580470            }
    581             sprintf(tmp, "%-64ld", bytes_read_in);
     471            asprintf(&tmp, "%-64ld", bytes_read_in);
    582472            if (fwrite(tmp, 1, 64, fout) != 64) {
    583473                fatal_error("Cannot write introductory block");
    584474            }
     475            paranoid_free(tmp);
     476
    585477            log_msg(7,
    586478                    "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
    587479                    subsliceno, bytes_read_in, bytes_to_be_read);
    588480            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
    589             sprintf(tmp, "%-64ld", subsliceno);
     481            asprintf(&tmp, "%-64ld", subsliceno);
    590482            if (fwrite(tmp, 1, 64, fout) != 64) {
    591483                fatal_error("Cannot write post-thingy block");
    592484            }
     485            paranoid_free(tmp);
     486
    593487            log_msg(7, "Subslice #%d written OK", subsliceno);
    594488            subsliceno++;
    595489        }
    596         sprintf(tmp, "%-64ld", 0L);
     490        asprintf(&tmp, "%-64ld", 0L);
    597491        if (fwrite(tmp, 1, 64L, fout) != 64L) {
    598492            fatal_error("Cannot write final introductory block");
     
    601495        fin = f_archived;
    602496        fout = f_orig;
     497        if (!(tmp = malloc(64L))) {
     498            fatal_error("Failed to malloc() tmp");
     499        }
    603500        if (fread(tmp, 1, 64L, fin) != 64L) {
    604501            fatal_error("Cannot read the introductory block");
     
    640537
    641538    if (direction == 'w') {
    642         sprintf(tmp, "%-64s", PIMP_END_SZ);
     539        asprintf(&tmp, "%-64s", PIMP_END_SZ);
    643540        if (fwrite(tmp, 1, 64, fout) != 64) {
    644541            fatal_error("Can't write the final block");
    645542        }
     543        paranoid_free(tmp);
    646544    } else {
    647545        log_msg(1, "tmpA is %s", tmp);
     
    657555//      if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
    658556                fwrite(tmp, 1, bytes_read_in, ftmp);
    659                 sprintf(tmp, "I am here - %ld", ftell(fin));
    660 //    log_msg(0, tmp);
     557                paranoid_free(tmp);
     558               
     559                if (!(tmp = malloc(512))) {
     560                    fatal_error("Failed to malloc() tmp");
     561                }
    661562                fread(tmp, 1, 512, fin);
    662563                log_msg(0, "tmp = '%s'", tmp);
     
    666567            }
    667568        }
     569        paranoid_free(tmp);
    668570    }
    669571
    670572    paranoid_free(buf);
    671     paranoid_free(tmp);
    672573    log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
    673574    return (retval);
     
    686587{
    687588    char *curr_fifo;
    688     char *prev_fifo;
     589    char *prev_fifo = NULL;
    689590    char *next_fifo;
    690591    char *command;
     
    699600    char *tmp;
    700601
    701     malloc_string(tmpstub);
    702     malloc_string(curr_fifo);
    703     malloc_string(prev_fifo);
    704     malloc_string(next_fifo);
    705     malloc_string(command);
    706     malloc_string(sz_call_to_partimage);
    707     malloc_string(tmp);
    708 
    709602    log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt);
    710603    if (g_tmpfs_mountpt && g_tmpfs_mountpt[0]
    711604        && does_file_exist(g_tmpfs_mountpt)) {
    712         strcpy(tmpstub, g_tmpfs_mountpt);
     605        asprintf(&tmpstub, g_tmpfs_mountpt);
    713606    } else {
    714         strcpy(tmpstub, "/tmp");
     607        asprintf(&tmpstub, "/tmp");
    715608    }
    716609    paranoid_system("rm -f /tmp/*PARTIMAGE*");
    717     sprintf(command, "rm -Rf %s/pih-fifo-*", tmpstub);
     610    asprintf(&command, "rm -Rf %s/pih-fifo-*", tmpstub);
    718611    paranoid_system(command);
    719     sprintf(tmpstub + strlen(tmpstub), "/pih-fifo-%ld",
    720             (long int) random());
     612    paranoid_free(command);
     613
     614    asprintf(&tmp, "%s/pih-fifo-%ld", tmpstub, (long int) random());
     615    paranoid_free(tmpstub);
     616    tmpstub = tmp;
     617    paranoid_free(tmp);
     618
    721619    mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though...
    722     sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number);
    723     sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
     620    asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
     621    asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
    724622    mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
    725623    mkfifo(next_fifo, S_IRWXU | S_IRWXG);   // make sure _next_ fifo already exists before we call partimage
    726     sz_call_to_partimage[0] = 2;
    727     sz_call_to_partimage[1] = 0;
    728     sprintf(sz_call_to_partimage + 2,
    729             "partimagehack " PARTIMAGE_PARAMS
    730             " save %s %s > /tmp/stdout 2> /tmp/stderr", input_device,
     624    asprintf(&sz_call_to_partimage,
     625            "%c%cpartimagehack " PARTIMAGE_PARAMS
     626            " save %s %s > /tmp/stdout 2> /tmp/stderr", 2, 0, input_device,
    731627            tmpstub);
    732628    log_msg(5, "curr_fifo   = %s", curr_fifo);
     
    750646    log_to_screen("Working with partimagehack...");
    751647    while (sz_call_to_partimage[0] > 0) {
    752         sprintf(tmp, "%s\n", NEXT_SUBVOL_PLEASE);
     648        asprintf(&tmp, "%s\n", NEXT_SUBVOL_PLEASE);
    753649        if (fwrite(tmp, 1, 128, fout) != 128) {
    754650            fatal_error("Cannot write interim block");
    755651        }
     652        paranoid_free(tmp);
     653
    756654        log_msg(5, "fifo_number=%d", fifo_number);
    757655        log_msg(4, "Cat'ting %s", curr_fifo);
     
    759657            fatal_error("Unable to openin from fifo");
    760658        }
    761 //      if (!sz_call_to_partimage[0]) { break; }
    762         log_msg(5, "Deleting %s", prev_fifo);
    763         unlink(prev_fifo);      // just in case
     659        if (prev_fifo !=  NULL) {
     660            log_msg(5, "Deleting %s", prev_fifo);
     661            unlink(prev_fifo);      // just in case
     662            paranoid_free(prev_fifo);
     663        }
    764664        copy_from_src_to_dest(fin, fout, 'w');
    765665        paranoid_fclose(fin);
    766666        fifo_number++;
    767         strcpy(prev_fifo, curr_fifo);
    768         strcpy(curr_fifo, next_fifo);
     667
     668        prev_fifo = curr_fifo;
     669        curr_fifo = next_fifo;
    769670        log_msg(5, "Creating %s", next_fifo);
    770         sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
     671        asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
    771672        mkfifo(next_fifo, S_IRWXU | S_IRWXG);   // make sure _next_ fifo exists before we cat this one
    772673        system("sync");
    773674        sleep(5);
    774675    }
    775     sprintf(tmp, "%s\n", NO_MORE_SUBVOLS);
     676    asprintf(&tmp, "%s\n", NO_MORE_SUBVOLS);
    776677    if (fwrite(tmp, 1, 128, fout) != 128) {
    777678        fatal_error("Cannot write interim block");
     
    786687        fatal_error("Cannot write interim block");
    787688    }
     689    paranoid_free(tmp);
    788690    paranoid_fclose(fout);
    789691    log_to_screen("Cleaning up after partimagehack...");
     
    791693    paranoid_system("sync");
    792694    unlink(next_fifo);
     695    paranoid_free(next_fifo);
     696
    793697    unlink(curr_fifo);
     698    paranoid_free(curr_fifo);
     699
    794700    unlink(prev_fifo);
     701    paranoid_free(prev_fifo);
     702
    795703    log_to_screen("Finished cleaning up.");
    796704
     
    800708    pthread_join(partimage_thread, NULL);
    801709    res = sz_call_to_partimage[1];
     710    paranoid_free(sz_call_to_partimage);
    802711    log_msg(2, "pthread_join() joined OK.");
    803712    log_msg(1, "Partimagehack(save) returned %d", res);
    804713    unlink(tmpstub);
    805     paranoid_free(curr_fifo);
    806     paranoid_free(prev_fifo);
    807     paranoid_free(next_fifo);
    808714    paranoid_free(tmpstub);
    809     paranoid_free(tmp);
    810     paranoid_free(command);
     715
    811716    return (res);
    812717}
     
    835740    return (res);
    836741}
    837 
    838 
    839 
    840742
    841743
     
    853755    char *command;
    854756    char *tempfile;
    855     char *title;
    856757    /*@ pointers ********************************************************** */
    857758    FILE *pin;
    858759
    859     malloc_string(title);
    860     malloc_string(command);
    861     malloc_string(tempfile);
    862760    assert_string_is_neither_NULL_nor_zerolength(cmd);
    863     assert_string_is_neither_NULL_nor_zerolength(title);
    864 
    865     strcpy(title, tt);
    866     strcpy(tempfile,
     761
     762    asprintf(&tempfile,
    867763           call_program_and_get_last_line_of_output
    868764           ("mktemp -q /tmp/mondo.XXXXXXXX"));
    869     sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
     765    asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
    870766            tempfile);
    871767    log_msg(3, command);
    872     open_evalcall_form(title);
     768    open_evalcall_form(tt);
     769
    873770    if (!(pin = popen(command, "r"))) {
    874771        log_OS_error("fmt err");
     772        paranoid_free(command);
     773        paranoid_free(tempfile);
    875774        return (1);
    876775    }
     776    paranoid_free(command);
     777
    877778    maxpc = 100;
    878779// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
     
    886787        if (pcno <= 5 && last_pcno > 40) {
    887788            close_evalcall_form();
    888             strcpy(title, "Verifying...");
    889             open_evalcall_form(title);
     789            open_evalcall_form("Verifying...");
    890790        }
    891791        last_pcno = pcno;
     
    899799    }
    900800    unlink(tempfile);
    901     paranoid_free(command);
    902801    paranoid_free(tempfile);
    903     paranoid_free(title);
    904802    return (res);
    905803}
    906 
    907 
    908804
    909805
     
    924820    pthread_exit((void *) (&res));
    925821}
    926 
    927 
    928822
    929823
     
    941835    /*@ buffers *********************************************************** */
    942836    char *command;
    943     char *title;
    944837    /*@ pointers ********************************************************** */
    945838    static int chldres = 0;
     
    952845    *pchild_result = 999;
    953846
    954     malloc_string(title);
    955     malloc_string(command);
    956     strcpy(title, tt);
    957     sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
     847    asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
    958848    log_msg(3, "command = '%s'", command);
    959849    if ((res =
     
    964854
    965855    log_msg(8, "Parent running");
    966     open_evalcall_form(title);
     856    open_evalcall_form(tt);
    967857    for (sleep(1); command[0] != '\0'; sleep(1)) {
    968858        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
     
    974864        if (pcno <= 5 && last_pcno >= 40) {
    975865            close_evalcall_form();
    976             strcpy(title, "Verifying...");
    977             open_evalcall_form(title);
     866            open_evalcall_form("Verifying...");
    978867        }
    979868        if (counter++ >= 5) {
     
    984873        update_evalcall_form(percentage);
    985874    }
     875    paranoid_free(command);
     876
    986877    log_file_end_to_screen(MONDO_LOGFILE, "");
    987878    close_evalcall_form();
     
    993884    }
    994885    log_msg(3, "Parent res = %d", res);
    995     paranoid_free(command);
    996     paranoid_free(title);
    997886    return (res);
    998887}
    999888
    1000 
    1001 
    1002889#define PIH_LOG "/var/log/partimage-debug.log"
    1003 
    1004890
    1005891/**
     
    1014900// RESTORE
    1015901    char *tmp;
    1016 //  char *command;
    1017902    char *stuff;
    1018903    char *sz_call_to_partimage;
     
    1021906    char *curr_fifo;
    1022907    char *prev_fifo;
    1023     char *oldest_fifo;
     908    char *oldest_fifo = NULL;
    1024909    char *next_fifo;
    1025910    char *afternxt_fifo;
     
    1029914    FILE *fout;
    1030915
    1031     malloc_string(curr_fifo);
    1032     malloc_string(prev_fifo);
    1033     malloc_string(next_fifo);
    1034     malloc_string(afternxt_fifo);
    1035     malloc_string(oldest_fifo);
    1036     malloc_string(tmp);
    1037     sz_call_to_partimage = malloc(1000);
    1038     malloc_string(stuff);
    1039     malloc_string(tmpstub);
    1040 
    1041916    log_msg(1, "output_device=%s", output_device);
    1042917    log_msg(1, "input_fifo=%s", input_fifo);
    1043 /* I don't trust g_tmpfs_mountpt
    1044   if (g_tmpfs_mountpt[0])
    1045     {
    1046       strcpy(tmpstub, g_tmpfs_mountpt);
    1047     }
    1048   else
    1049     {
    1050 */
    1051     strcpy(tmpstub, "/tmp");
    1052 //    }
     918    asprintf(&tmpstub, "/tmp");
     919
    1053920    log_msg(1, "tmpstub was %s", tmpstub);
    1054     strcpy(stuff, tmpstub);
    1055     sprintf(tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random());
     921    asprintf(&stuff, tmpstub);
     922    paranoid_free(tmpstub);
     923
     924    asprintf(&tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random());
     925    paranoid_free(stuff);
     926
    1056927    log_msg(1, "tmpstub is now %s", tmpstub);
    1057928    unlink("/tmp/PARTIMAGEHACK-POSITION");
    1058929    unlink(PAUSE_PARTIMAGE_FNAME);
    1059930    paranoid_system("rm -f /tmp/*PARTIMAGE*");
    1060     sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number);
    1061     sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
    1062     sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
     931    asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
     932    asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
     933    asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
    1063934    mkfifo(PIH_LOG, S_IRWXU | S_IRWXG);
    1064935    mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
     
    1077948        fatal_error("partimagehack not found");
    1078949    }
    1079     sz_call_to_partimage[0] = 2;
    1080     sz_call_to_partimage[1] = 0;
    1081     sprintf(sz_call_to_partimage + 2,
    1082             "partimagehack " PARTIMAGE_PARAMS
    1083             " restore %s %s > /dev/null 2>> %s", output_device, curr_fifo,
     950    asprintf(&sz_call_to_partimage,
     951            "%c%cpartimagehack " PARTIMAGE_PARAMS
     952            " restore %s %s > /dev/null 2>> %s", 2, 0, output_device, curr_fifo,
    1084953            MONDO_LOGFILE);
    1085954    log_msg(1, "output_device = %s", output_device);
     
    1101970        sleep(1);
    1102971    }
     972
     973    if (!(tmp = malloc(128))) {
     974        fatal_error("Failed to malloc() tmp");
     975    }
    1103976    while (sz_call_to_partimage[0] > 0) {
    1104977        if (fread(tmp, 1, 128, fin) != 128) {
     
    1123996            fatal_error("Cannot openout to curr_fifo");
    1124997        }
    1125         log_msg(6, "Deleting %s", oldest_fifo);
    1126998        copy_from_src_to_dest(fout, fin, 'r');
    1127999        paranoid_fclose(fout);
    11281000        fifo_number++;
    1129         unlink(oldest_fifo);    // just in case
    1130         strcpy(oldest_fifo, prev_fifo);
    1131         strcpy(prev_fifo, curr_fifo);
    1132         strcpy(curr_fifo, next_fifo);
    1133         strcpy(next_fifo, afternxt_fifo);
    1134         sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
     1001        if (oldest_fifo != NULL) {
     1002            log_msg(6, "Deleting %s", oldest_fifo);
     1003            unlink(oldest_fifo);    // just in case
     1004            paranoid_free(oldest_fifo);
     1005        }
     1006        oldest_fifo = prev_fifo;
     1007        prev_fifo = curr_fifo;
     1008        curr_fifo = next_fifo;
     1009        next_fifo = afternxt_fifo;
     1010        asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
    11351011        log_msg(6, "Creating %s", afternxt_fifo);
    11361012        mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG);   // make sure _next_ fifo already exists before we access current fifo
     
    11391015        usleep(1000L * 100L);
    11401016    }
     1017    paranoid_free(tmp);
     1018    paranoid_free(tmpstub);
     1019
    11411020    paranoid_fclose(fin);
    11421021    paranoid_system("sync");
     
    11451024    pthread_join(partimage_thread, NULL);
    11461025    res = sz_call_to_partimage[1];
     1026    paranoid_free(sz_call_to_partimage);
     1027
    11471028    log_msg(1, "Yay. Partimagehack (restore) returned %d", res);
    11481029    unlink(prev_fifo);
     1030    paranoid_free(prev_fifo);
     1031
    11491032    unlink(curr_fifo);
     1033    paranoid_free(curr_fifo);
     1034
    11501035    unlink(next_fifo);
     1036    paranoid_free(next_fifo);
     1037
    11511038    unlink(afternxt_fifo);
     1039    paranoid_free(afternxt_fifo);
     1040
    11521041    unlink(PIH_LOG);
    1153     paranoid_free(tmp);
    1154     paranoid_free(sz_call_to_partimage);
    1155     paranoid_free(stuff);
    1156     paranoid_free(prev_fifo);
    1157     paranoid_free(curr_fifo);
    1158     paranoid_free(next_fifo);
    1159     paranoid_free(afternxt_fifo);
     1042    /* BERLIOS : pas de unlink(oldest_fifo) ??? */
    11601043    paranoid_free(oldest_fifo);
    1161     paranoid_free(tmpstub);
     1044
    11621045    return (res);
    11631046}
Note: See TracChangeset for help on using the changeset viewer.