Changeset 688 in MondoRescue for trunk/mondo/mondo/mondorestore/mondo-prep.c


Ignore:
Timestamp:
Jul 17, 2006, 3:44:46 PM (18 years ago)
Author:
bcornec
Message:

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/mondo/mondorestore/mondo-prep.c

    r687 r688  
    11/***************************************************************************
    2 mondo-prep.c  -  description
    3 -----------------
    4 
    5 begin: Fri Apr 19 16:40:35 EDT 2002
    6 copyright : (C) 2002 Mondo  Hugo Rabson
    7 email     : Hugo Rabson <hugorabson@msn.com>
    8 edited by : by Stan Benoit 4/2002
    9 email     : troff@nakedsoul.org
    10 cvsid     : $Id$
    11 ***************************************************************************/
    12 
    13 /***************************************************************************
    14  *                                                                         *
    15  *   This program is free software; you can redistribute it and/or modify  *
    16  *   it under the terms of the GNU General Public License as published by  *
    17  *   the Free Software Foundation; either version 2 of the License, or     *
    18  *   (at your option) any later version.                                   *
    19  *                                                                         *
    20  ***************************************************************************/
    21 
    22 /* mondo-prep.c             Hugo Rabson
    23 
    24 
    25 
    26 07/20
    27 - when starting RAID, don't do it twice :)
    28 - moved Joshua's new get_phys_size_of_drive() code
    29   from resize_drive... into get_phys_size_of_drive()
    30 
    31 06/29
    32 - make sure software RAID devices are formatted IF user says they're to be
    33 - drivelist is struct now, not char[][]
    34 
    35 06/26
    36 - drop make_relevant_partition_bootable(); do it yourself in C (mostly)
    37 - offer to reboot if partition table is locked up by the kernel
    38 
    39 06/22
    40 - be sure not to resize non-NTFS images when restoring
    41 - set non-NTFS images' partition types properly
    42 
    43 06/19
    44 - shut down all LVMs and VGs before prepping
    45 
    46 05/07
    47 - usage of parted2fdisk instead of fdisk alone (ia32/ia64 compatibility)
    48   BCO
    49 
    50 03/31
    51 - rewrote partitioning and formatting code to call fdisk once per disk
    52 
    53 10/21/2003
    54 - suspend/resume Newt gui before/after calling do_my_funky_lvm_stuff()
    55 
    56 10/20
    57 - rewrote format_everything() - what a mess it was.
    58   It now does things in three phases:-
    59   - formats software RAID devices (/dev/md0, etc.)
    60   - formats and configures LVM devices
    61   - formats regular partitions (/dev/hda1, /dev/sdb2, etc.)
    62     and any LVMs recently prepped
    63 
    64 10/07
    65 - use strstr(format, "raid") instead of strcmp(format,"raid") to determin
    66   if partition is a RAID component
    67 
    68 09/23
    69 - better comments
    70 
    71 09/18
    72 - better logging of RAID activity
    73 
    74 05/05
    75 - added Joshua Oreman's FreeBSD patches
    76 
    77 04/30
    78 - added textonly mode
    79 
    80 04/24
    81 - added lots of assert()'s and log_OS_error()'s
    82 
    83 04/21
    84 - format_everything() --- don't let bar go too far
    85 - mkfs -c to check for bad blocks when formatting
    86 
    87 04/04
    88 - misc clean-up (Tom Mortell)
    89 
    90 01/15/2003
    91 - added code for LVM and SW Raid (Brian Borgeson)
    92 
    93 12/10/2002
    94 - line 1238: friendlier output
    95 
    96 11/20
    97 - when wiping a given device in preparation for partitioning + formatting
    98   it, don't wipe the MBR; just the partition table. That allows for
    99   stupid-ass Compaq users who like to play with their MBR's.
    100 - disable mountlist.txt-->mountlist.txt.pre-resize copying (superfluous)
    101 
    102 09/09
    103 - run_program_and_log_output() now takes boolean operator to specify
    104   whether it will log its activities in the event of _success_
    105 
    106 07/01 - 07/31
    107 - added hooks to libmondo
    108 - RAID enhancements (Philippe de Muyter)
    109 
    110 01/01 - 03/31
    111 - partition_device() will refuse to partition /dev/mdX now (cos it's a
    112   virtual drive); however, it will return 0 (i.e. not an error)
    113 - make_list_of_drives() will exclude /dev/md* from list
    114 - mkreiserfs -ff instead of -q (Andy Glass)
    115 - don't resize drive if drive not found (or if its size cannot be det'd)
    116 - when generating list of drives from mountlist, skip the 'p' at the end
    117   of drive name if /dev/ida/ or /dev/cciss/; just do it (Michael Eisenberg)
    118 - don't specify journal size when formatting ext3
    119   (used to have -Jsize=10 in the call to mkfs)
    120 - handles files >2GB in size
    121 - format interactively, if Interactive Mode
    122 - removed reference to g_tape_size
    123 - when executing /tmp/i-want-my-lvm, only record the error# if the command
    124   was _not_ an 'insmod' command
    125 - pass partition size to fdisk in Kilobytes now, not Megabytes
    126 - log fdisk's output to /tmp/mondo-restore.log if it fails
    127 - don't try to format partitions of type 'image'
    128 - don't type to set types of 'image' partitions
    129 - if format code is 1 or 2 chars then assume it is a hex string
    130 - took out all '/ /' comments
    131 - don't extrapolate/add partition from RAID dev to mountlist if it's already
    132   present in mountlist
    133 - less repetitive logging in the event of vacuum-packing of last part'n
    134 - no extrapolation at all: RAID partitions should be listed in mountlist
    135   already, thanks to either Mindi v0.5x or the mountlist editor itself
    136 - no longer say, 'and logging to...' when setting a partition's type
    137 - don't run mkfs on RAID partitions (/dev/hd*, /dev/sd*); just set type
    138 - the setting of a partition's type now takes place in a separate subroutine
    139   from the subroutine that actually creates the partition
    140 - no need to set type if 83: 83 is the default (under fdisk)
    141 - turned on '-Wall'; cleaned up some cruft
    142 - if vacuum-packing partition (i.e. size=0MB --> max) then say, "(maximum)"
    143   not, "(0 MB)"
    144 
    145 11/22/2001
    146 - preliminary code review
    147 - created on Nov 22nd, 2001
    148 */
     2 * $Id
     3**/
    1494
    1505/**
     
    15914#include "../common/libmondo.h"
    16015#include "mondo-rstr-tools-EXT.h"
     16
    16117#include <sys/ioctl.h>
    16218#include <linux/hdreg.h>
    16319#include <math.h>
     20#include <unistd.h>
    16421
    16522
     
    18542extern char *g_mountlist_fname;
    18643extern long g_current_progress, g_maximum_progress;
    187 
    18844extern bool g_text_mode;
    189 
    19045extern void pause_for_N_seconds(int, char *);
    19146
    192 
    19347FILE *g_fprep = NULL;
    194 
    195 
    196 
    19748int g_partition_table_locked_up = 0;
    198 
    199 
    200 
    201 
    202 
    203 
    204 
    205 
    20649
    20750
     
    20952{
    21053    char *command;
    211     char *tmp;
    21254    int lino;
    21355    int i;
     
    21860// If LVMs are present and a zero-and-reboot wasn't recently undertaken
    21961// then zero & insist on reboot.
    220     malloc_string(command);
    221     malloc_string(tmp);
    22262    buf = malloc(blocksize);
    22363    if (does_file_exist("/tmp/i-want-my-lvm"))  // FIXME - cheating :)
     
    22666        make_list_of_drives_in_mountlist(mountlist, drivelist);
    22767        for (lino = 0; lino < drivelist->entries; lino++) {
    228             sprintf(command,
    229                     "dd if=%s bs=512 count=1 2> /dev/null | grep \"%s\"",
    230                     drivelist->el[lino].device, MONDO_WAS_HERE);
     68            asprintf(&command,
     69                     "dd if=%s bs=512 count=1 2> /dev/null | grep \"%s\"",
     70                     drivelist->el[lino].device, MONDO_WAS_HERE);
    23171            if (!run_program_and_log_output(command, 1)) {
    23272                log_msg(1, "Found MONDO_WAS_HERE marker on drive#%d (%s)",
    23373                        lino, drivelist->el[lino].device);
     74                paranoid_free(command);
    23475                break;
    23576            }
     77            paranoid_free(command);
    23678        }
    23779
     
    23981// zero & reboot
    24082            log_to_screen
    241                 (_("I am sorry for the inconvenience but I must ask you to reboot."));
    242             log_to_screen
    243                 (_("I need to reset the Master Boot Record; in order to be"));
    244             log_to_screen
    245                 (_("sure the kernel notices, I must reboot after doing it."));
     83                (_
     84                 ("I am sorry for the inconvenience but I must ask you to reboot."));
     85            log_to_screen(_
     86                          ("I need to reset the Master Boot Record; in order to be"));
     87            log_to_screen(_
     88                          ("sure the kernel notices, I must reboot after doing it."));
    24689            log_to_screen("Please hit 'Enter' to reboot.");
    24790            for (lino = 0; lino < drivelist->entries; lino++) {
     
    265108                }
    266109            }
    267             system("sync");
    268             system("sync");
    269             system("sync");
     110            sync();
     111            sync();
     112            sync();
    270113            popup_and_OK
    271                 (_("I must now reboot. Please leave the boot media in the drive and repeat your actions - e.g. type 'nuke' - and it should work fine."));
     114                (_
     115                 ("I must reboot now. Please leave the boot media in the drive and repeat your actions - e.g. type 'nuke' - and it should work fine."));
    272116            system("reboot");
    273117        }
    274118    }
    275119// Still here? Cool!
    276     paranoid_free(command);
    277     paranoid_free(tmp);
    278120    log_msg(1, "Cool. I didn't have to wipe anything.");
    279121}
    280 
    281 
    282 
    283 
    284122
    285123
     
    311149    return (i);
    312150}
    313 
    314 
    315 
    316 
    317 
    318 
    319 
    320 
    321151
    322152
     
    330160 * @return The number of errors encountered (0 for success).
    331161 */
    332 
    333 
    334162int do_my_funky_lvm_stuff(bool just_erase_existing_volumes,
    335163                          bool vacuum_pack)
    336164{
    337     /**  buffers **********************************************/
    338     char *tmp;
    339     char *incoming;
    340     char *command;
    341     char *lvscan_sz;
    342     char *lvremove_sz;
    343     char *pvscan_sz;
    344     char *vgscan_sz;
    345     char *vgcreate_sz;
    346     char *vgchange_sz;
    347     char *vgremove_sz;
    348 //  char *do_this_last;
    349 
    350165    /** char **************************************************/
    351     char *p;
    352     char *q;
     166    char *tmp = NULL;
     167    char *tmp1 = NULL;
     168    char *incoming = NULL;
     169    char *command = NULL;
     170    char *lvscan_sz = NULL;
     171    char *lvremove_sz = NULL;
     172    char *pvscan_sz = NULL;
     173    char *vgscan_sz = NULL;
     174    char *vgchange_sz = NULL;
     175    char *vgremove_sz = NULL;
     176    char *p = NULL;
     177    char *q = NULL;
    353178
    354179    /** int ***************************************************/
    355180    int retval = 0;
    356181    int res = 0;
    357     int i;
     182    int i = 0;
    358183    int lvmversion = 1;
    359184    long extents;
    360185    fpos_t orig_pos;
     186    size_t n = 0;
     187    size_t n1 = 0;
    361188
    362189    /** pointers **********************************************/
     
    374201    }
    375202
    376     malloc_string(tmp);
    377     malloc_string(incoming);
    378     malloc_string(lvscan_sz);
    379     malloc_string(lvremove_sz);
    380     malloc_string(vgscan_sz);
    381     malloc_string(pvscan_sz);
    382     malloc_string(vgcreate_sz);
    383     malloc_string(vgchange_sz);
    384     malloc_string(vgremove_sz);
    385 //  malloc_string(do_this_last); // postpone lvcreate call if necessary
    386     command = malloc(512);
    387 
    388 //  do_this_last[0] = '\0';
    389203    iamhere("STARTING");
    390204    log_msg(1, "OK, opened i-want-my-lvm. Shutting down LVM volumes...");
    391205    if (find_home_of_exe("lvm"))    // found it :) cool
    392206    {
    393         strcpy(lvscan_sz, "lvm lvscan");
    394         strcpy(lvremove_sz, "lvm lvremove");
    395         strcpy(vgscan_sz, "lvm vgscan");
    396         strcpy(pvscan_sz, "lvm pvscan");
    397         strcpy(vgcreate_sz, "lvm vgcreate");
    398         strcpy(vgchange_sz, "lvm vgchange");
    399         strcpy(vgremove_sz, "lvm vgremove");
     207        asprintf(&lvscan_sz, "lvm lvscan");
     208        asprintf(&lvremove_sz, "lvm lvremove");
     209        asprintf(&vgscan_sz, "lvm vgscan");
     210        asprintf(&pvscan_sz, "lvm pvscan");
     211        asprintf(&vgchange_sz, "lvm vgchange");
     212        asprintf(&vgremove_sz, "lvm vgremove");
    400213    } else {
    401         strcpy(lvscan_sz, "lvscan");
    402         strcpy(lvremove_sz, "lvremove");
    403         strcpy(vgscan_sz, "vgscan");
    404         strcpy(pvscan_sz, "pvscan");
    405         strcpy(vgcreate_sz, "vgcreate");
    406         strcpy(vgchange_sz, "vgchange");
    407         strcpy(vgremove_sz, "vgremove");
    408     }
    409     sprintf(command,
     214        asprintf(&lvscan_sz, "lvscan");
     215        asprintf(&lvremove_sz, "lvremove");
     216        asprintf(&vgscan_sz, "vgscan");
     217        asprintf(&pvscan_sz, "pvscan");
     218        asprintf(&vgchange_sz, "vgchange");
     219        asprintf(&vgremove_sz, "vgremove");
     220    }
     221    asprintf(&command,
    410222            "for i in `%s | cut -d\"'\" -f2 | sort -r` ; do echo \"Shutting down lv $i\" >> "
    411223            MONDO_LOGFILE "; %s -f $i; done", lvscan_sz, lvremove_sz);
     224    paranoid_free(lvscan_sz);
     225    paranoid_free(lvremove_sz);
     226
    412227    run_program_and_log_output(command, 5);
     228    paranoid_free(command);
     229
    413230    sleep(1);
    414     sprintf(command,
     231    asprintf(&command,
    415232            "for i in `%s | grep -i lvm | cut -d'\"' -f2` ; do %s -a n $i ; %s $i; echo \"Shutting down vg $i\" >> "
    416             MONDO_LOGFILE "; done; %s -a n", vgscan_sz, vgchange_sz,
    417             vgremove_sz, vgremove_sz);
     233            MONDO_LOGFILE "; done; %s -a n", vgscan_sz, vgchange_sz, vgremove_sz, vgremove_sz);
     234    paranoid_free(vgchange_sz);
     235    paranoid_free(vgremove_sz);
     236
    418237    run_program_and_log_output(command, 5);
     238    paranoid_free(command);
     239
    419240    if (just_erase_existing_volumes) {
    420241        paranoid_fclose(fin);
    421242        log_msg(1, "Closed i-want-my-lvm. Finished erasing LVMs.");
    422         retval = 0;
    423         goto end_of_i_want_my_lvm;
     243        sync();
     244        sync();
     245        sync();
     246        sleep(1);
     247        iamhere("ENDING");
     248        log_msg(1, "Not many errors. Returning 0.");
     249        return (0);
    424250    }
    425251
    426252    log_msg(1, "OK, rewound i-want-my-lvm. Doing funky stuff...");
    427253    rewind(fin);
    428     for (fgets(incoming, 512, fin); !feof(fin); fgets(incoming, 512, fin)) {
     254    for (getline(&incoming, &n1, fin); !feof(fin); getline(&incoming, &n1, fin)) {
    429255        fgetpos(fin, &orig_pos);
     256        /* BERLIOS : Strange no ? */
    430257        if (incoming[0] != '#') {
    431258            continue;
     
    433260        if (res && strstr(command, "create") && vacuum_pack) {
    434261            sleep(2);
    435             system("sync");
    436             system("sync");
    437             system("sync");
     262            sync();
     263            sync();
     264            sync();
    438265        }
    439266        if ((p = strstr(incoming, "vgcreate"))) {
    440267// include next line(s) if they end in /dev (cos we've got a broken i-want-my-lvm)
    441             for (fgets(tmp, 512, fin); !feof(fin); fgets(tmp, 512, fin)) {
     268            for (getline(&tmp, &n, fin); !feof(fin); getline(&tmp, &n, fin)) {
    442269                if (tmp[0] == '#') {
    443270                    fsetpos(fin, &orig_pos);
     
    445272                } else {
    446273                    fgetpos(fin, &orig_pos);
    447                     strcat(incoming, tmp);
     274                    asprintf(&tmp1, "%s%s", incoming, tmp);
     275                    paranoid_free(incoming);
     276                    incoming = tmp1;
    448277                }
    449278            }
     279            paranoid_free(tmp);
     280
    450281            for (q = incoming; *q != '\0'; q++) {
    451282                if (*q < 32) {
     
    453284                }
    454285            }
    455             strcpy(tmp, p + strlen("vgcreate") + 1);
    456             for (q = tmp; *q > 32; q++);
     286            malloc_string(tmp1);
     287            strcpy(tmp1, p + strlen("vgcreate") + 1);
     288            for (q = tmp1; *q > 32; q++);
    457289            *q = '\0';
    458             log_msg(1, "Deleting old entries at /dev/%s", tmp);
    459 //             sprintf(command, "%s -f %s", vgremove_sz, tmp);
    460 //             run_program_and_log_output(command, 1);
    461             sprintf(command, "rm -Rf /dev/%s", tmp);
    462             run_program_and_log_output(command, 1);
     290            log_msg(1, "Deleting old entries at /dev/%s", tmp1);
     291            asprintf(&tmp, "rm -Rf /dev/%s", tmp1);
     292            paranoid_free(tmp1);
     293            run_program_and_log_output(tmp, 1);
     294            paranoid_free(tmp);
     295
    463296            run_program_and_log_output(vgscan_sz, 1);
    464297            run_program_and_log_output(pvscan_sz, 1);
     
    468301        }
    469302        for (p = incoming + 1; *p == ' '; p++);
    470         strcpy(command, p);
     303        paranoid_free(command);
     304        asprintf(&command, p);
    471305        for (p = command; *p != '\0'; p++);
    472306        for (; *(p - 1) < 32; p--);
     
    485319            if (strstr(command, "lvm lvcreate"))
    486320                lvmversion = 2;
     321            /* BERLIOS : this tmp may be uninitialized ?
    487322            log_it("%s... so I'll get creative.", tmp);
     323            */
    488324            if (lvmversion == 2) {
    489                 strcpy(tmp, call_program_and_get_last_line_of_output
    490                        ("tail -n5 "MONDO_LOGFILE" | grep Insufficient | tail -n1"));
     325                tmp = call_program_and_get_last_line_of_output
     326                        ("tail -n5 " MONDO_LOGFILE
     327                        " | grep Insufficient | tail -n1");
    491328            } else {
    492                 strcpy(tmp, call_program_and_get_last_line_of_output
    493                        ("tail -n5 "MONDO_LOGFILE" | grep lvcreate | tail -n1"));
     329                tmp = call_program_and_get_last_line_of_output
     330                       ("tail -n5 " MONDO_LOGFILE
     331                        " | grep lvcreate | tail -n1");
    494332            }
    495333            for (p = tmp; *p != '\0' && !isdigit(*p); p++);
    496334            extents = atol(p);
    497335            log_msg(5, "p='%s' --> extents=%ld", p, extents);
     336            paranoid_free(tmp);
    498337            p = strstr(command, "-L");
    499338            if (!p) {
     
    543382            retval++;
    544383        }
    545         sprintf(tmp, "echo \"%s\" >> /tmp/out.sh", command);
     384        asprintf(&tmp, "echo \"%s\" >> /tmp/out.sh", command);
    546385        system(tmp);
     386        paranoid_free(tmp);
    547387        sleep(1);
    548388    }
    549389    paranoid_fclose(fin);
    550     log_msg(1, "Closed i-want-my-lvm. Finished doing funky stuff.");
    551   end_of_i_want_my_lvm:
    552     paranoid_free(tmp);
    553     paranoid_free(incoming);
    554     paranoid_free(command);
    555     paranoid_free(lvscan_sz);
    556     paranoid_free(lvremove_sz);
    557390    paranoid_free(vgscan_sz);
    558391    paranoid_free(pvscan_sz);
    559     paranoid_free(vgcreate_sz);
    560     paranoid_free(vgchange_sz);
    561     paranoid_free(vgremove_sz);
    562 //  paranoid_free(do_this_last);
    563     system("sync");
    564     system("sync");
    565     system("sync");
     392    paranoid_free(command);
     393    paranoid_free(incoming);
     394
     395    log_msg(1, "Closed i-want-my-lvm. Finished doing funky stuff.");
     396    sync();
     397    sync();
     398    sync();
    566399    sleep(1);
    567400    iamhere("ENDING");
     
    589422                                                     *old_mountlist)
    590423{
    591     /** pointers *********************************************************/
    592     FILE *fin;
    593 
    594     /** int **************************************************************/
    595     int lino;
    596     int j;
    597 
    598     /** buffers **********************************************************/
    599     char *incoming;
    600     char *tmp;
    601 
    602     /** pointers *********************************************************/
    603     char *p;
     424    FILE *fin = NULL;
     425    int lino = 0;
     426    int j = 0;
     427    char *incoming = NULL;
     428    char *tmp = NULL;
     429    char *p = NULL;
     430    size_t n = 0;
    604431
    605432    /** init *************************************************************/
     
    608435    /** end **************************************************************/
    609436
    610     malloc_string(incoming);
    611     malloc_string(tmp);
    612437    assert(new_mountlist != NULL);
    613438    assert(old_mountlist != NULL);
     
    615440#ifdef __FreeBSD__
    616441    log_to_screen
    617         (_("I don't know how to extrapolate the mountlist on FreeBSD. Sorry."));
     442        (_
     443         ("I don't know how to extrapolate the mountlist on FreeBSD. Sorry."));
    618444    return (1);
    619445#endif
     
    624450            if (!does_file_exist("/etc/raidtab")) {
    625451                log_to_screen
    626                     (_("Cannot find /etc/raidtab - cannot extrapolate the fdisk entries"));
     452                    (_
     453                     ("Cannot find /etc/raidtab - cannot extrapolate the fdisk entries"));
    627454                finish(1);
    628455            }
     
    631458                finish(1);
    632459            }
    633             for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin)
     460            for (getline(&incoming, &n, fin); !feof(fin)
    634461                 && !strstr(incoming, old_mountlist->el[lino].device);
    635                  fgets(incoming, MAX_STR_LEN - 1, fin));
     462                 getline(&incoming, &n, fin));
    636463            if (!feof(fin)) {
    637                 sprintf(tmp, "Investigating %s",
     464                asprintf(&tmp, "Investigating %s",
    638465                        old_mountlist->el[lino].device);
    639466                log_it(tmp);
    640                 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin)
     467                paranoid_free(tmp);
     468
     469                for (getline(&incoming, &n, fin); !feof(fin)
    641470                     && !strstr(incoming, "raiddev");
    642                      fgets(incoming, MAX_STR_LEN - 1, fin)) {
     471                     getline(&incoming, &n, fin)) {
    643472                    if (strstr(incoming, OSSWAP("device", "drive"))
    644473                        && !strchr(incoming, '#')) {
     
    647476                        *p = '\0';
    648477                        for (p--; p > incoming && *(p - 1) > 32; p--);
    649                         sprintf(tmp, "Extrapolating %s", p);
     478                        asprintf(&tmp, "Extrapolating %s", p);
    650479                        log_it(tmp);
     480                        paranoid_free(tmp);
     481
    651482                        for (j = 0;
    652483                             j < new_mountlist->entries
     
    666497                            new_mountlist->entries++;
    667498                        } else {
    668                             sprintf(tmp,
    669                                     "Not adding %s to mountlist: it's already there",
    670                                     p);
     499                            asprintf(&tmp,
     500                                    "Not adding %s to mountlist: it's already there", p);
    671501                            log_it(tmp);
     502                            paranoid_free(tmp);
    672503                        }
    673504                    }
    674505                }
    675506            }
     507            paranoid_free(incoming);
    676508            paranoid_fclose(fin);
    677509        } else {
     
    687519        }
    688520    }
    689     paranoid_free(incoming);
    690     paranoid_free(tmp);
    691521
    692522    return (0);
     
    702532 * @return 0 for success, nonzero for failure.
    703533 */
    704 int create_raid_device_via_mdadm(struct raidlist_itself *raidlist, char *device)
     534int create_raid_device_via_mdadm(struct raidlist_itself *raidlist,
     535                                 char *device)
    705536{
    706537  /** int **************************************************************/
    707   int i  = 0;
    708   int j  = 0;
    709   int res = 0;
    710  
     538    int i = 0;
     539    int j = 0;
     540    int res = 0;
     541
    711542  /** buffers ***********************************************************/
    712   char *devices = NULL;
    713   char *strtmp  = NULL;
    714   char *level   = NULL;
    715   char *program = NULL;
    716  
    717   // leave straight away if raidlist is initial or has no entries
    718   if (!raidlist || raidlist->entries == 0) {
    719     log_msg(1, "No RAID arrays found.");
    720     return 1;
    721   } else {
    722     log_msg(1, "%d RAID arrays found.", raidlist->entries);
    723   }
    724   // find raidlist entry for requested device
    725   for (i = 0; i < raidlist->entries; i++) {
    726     if (!strcmp(raidlist->el[i].raid_device, device)) break;
    727   }
    728   // check whether RAID device was found in raidlist
    729   if (i == raidlist->entries) {
    730     log_msg(1, "RAID device %s not found in list.", device);
    731     return 1;
    732   }
    733   // create device list from normal disks followed by spare ones
    734   asprintf(&devices, raidlist->el[i].data_disks.el[0].device);
    735   for (j = 1; j < raidlist->el[i].data_disks.entries; j++) {
    736     asprintf(&strtmp, "%s", devices);
    737     paranoid_free(devices);
    738     asprintf(&devices, "%s %s", strtmp,
    739          raidlist->el[i].data_disks.el[j].device);
    740     paranoid_free(strtmp);
    741   }
    742   for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) {
    743     asprintf(&strtmp, "%s", devices);
    744     paranoid_free(devices);
    745     asprintf(&devices, "%s %s", strtmp,
    746          raidlist->el[i].spare_disks.el[j].device);
    747     paranoid_free(strtmp);
    748   }
    749   // translate RAID level
    750   if (raidlist->el[i].raid_level == -2) {
    751     asprintf(&level, "multipath");
    752   } else if (raidlist->el[i].raid_level == -1) {
    753     asprintf(&level, "linear");
    754   } else {
    755     asprintf(&level, "raid%d", raidlist->el[i].raid_level);
    756   }
    757   // create RAID device:
    758   // - RAID device, number of devices and devices mandatory
    759   // - parity algorithm, chunk size and spare devices optional
    760   // - faulty devices ignored
    761   // - persistent superblock always used as this is recommended
    762   asprintf(&program,
    763        "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d",
    764        raidlist->el[i].raid_device, level,
    765        raidlist->el[i].data_disks.entries);
    766   if (raidlist->el[i].parity != -1) {
    767     asprintf(&strtmp, "%s", program);
    768     paranoid_free(program);
    769     switch(raidlist->el[i].parity) {
    770     case 0:
    771       asprintf(&program, "%s --parity=%s", strtmp, "la");
    772       break;
    773     case 1:
    774       asprintf(&program, "%s --parity=%s", strtmp, "ra");
    775       break;
    776     case 2:
    777       asprintf(&program, "%s --parity=%s", strtmp, "ls");
    778       break;
    779     case 3:
    780       asprintf(&program, "%s --parity=%s", strtmp, "rs");
    781       break;
    782     default:
    783       fatal_error("Unknown RAID parity algorithm.");
    784       break;
    785     }
    786     paranoid_free(strtmp);
    787   }
    788   if (raidlist->el[i].chunk_size != -1) {
    789     asprintf(&strtmp, "%s", program);
    790     paranoid_free(program);
    791     asprintf(&program, "%s --chunk=%d", strtmp, raidlist->el[i].chunk_size);
    792     paranoid_free(strtmp);
    793   }
    794   if (raidlist->el[i].spare_disks.entries > 0) {
    795     asprintf(&strtmp, "%s", program);
    796     paranoid_free(program);
    797     asprintf(&program, "%s --spare-devices=%d", strtmp,
    798          raidlist->el[i].spare_disks.entries);
    799     paranoid_free(strtmp);
    800   }
    801   asprintf(&strtmp, "%s", program);
    802   paranoid_free(program);
    803   asprintf(&program, "%s %s", strtmp, devices);
    804   paranoid_free(strtmp);
    805   res = run_program_and_log_output(program, 1);
    806   // free memory
    807   paranoid_free(devices);
    808   paranoid_free(level);
    809   paranoid_free(program);
    810   // return to calling instance
    811   return res;
     543    char *devices = NULL;
     544    char *strtmp = NULL;
     545    char *level = NULL;
     546    char *program = NULL;
     547
     548    // leave straight away if raidlist is initial or has no entries
     549    if (!raidlist || raidlist->entries == 0) {
     550        log_msg(1, "No RAID arrays found.");
     551        return 1;
     552    } else {
     553        log_msg(1, "%d RAID arrays found.", raidlist->entries);
     554    }
     555    // find raidlist entry for requested device
     556    for (i = 0; i < raidlist->entries; i++) {
     557        if (!strcmp(raidlist->el[i].raid_device, device))
     558            break;
     559    }
     560    // check whether RAID device was found in raidlist
     561    if (i == raidlist->entries) {
     562        log_msg(1, "RAID device %s not found in list.", device);
     563        return 1;
     564    }
     565    // create device list from normal disks followed by spare ones
     566    asprintf(&devices, raidlist->el[i].data_disks.el[0].device);
     567    for (j = 1; j < raidlist->el[i].data_disks.entries; j++) {
     568        asprintf(&strtmp, "%s", devices);
     569        paranoid_free(devices);
     570        asprintf(&devices, "%s %s", strtmp,
     571                 raidlist->el[i].data_disks.el[j].device);
     572        paranoid_free(strtmp);
     573    }
     574    for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) {
     575        asprintf(&strtmp, "%s", devices);
     576        paranoid_free(devices);
     577        asprintf(&devices, "%s %s", strtmp,
     578                 raidlist->el[i].spare_disks.el[j].device);
     579        paranoid_free(strtmp);
     580    }
     581    // translate RAID level
     582    if (raidlist->el[i].raid_level == -2) {
     583        asprintf(&level, "multipath");
     584    } else if (raidlist->el[i].raid_level == -1) {
     585        asprintf(&level, "linear");
     586    } else {
     587        asprintf(&level, "raid%d", raidlist->el[i].raid_level);
     588    }
     589    // create RAID device:
     590    // - RAID device, number of devices and devices mandatory
     591    // - parity algorithm, chunk size and spare devices optional
     592    // - faulty devices ignored
     593    // - persistent superblock always used as this is recommended
     594    asprintf(&program,
     595             "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d",
     596             raidlist->el[i].raid_device, level,
     597             raidlist->el[i].data_disks.entries);
     598    if (raidlist->el[i].parity != -1) {
     599        asprintf(&strtmp, "%s", program);
     600        paranoid_free(program);
     601        switch (raidlist->el[i].parity) {
     602        case 0:
     603            asprintf(&program, "%s --parity=%s", strtmp, "la");
     604            break;
     605        case 1:
     606            asprintf(&program, "%s --parity=%s", strtmp, "ra");
     607            break;
     608        case 2:
     609            asprintf(&program, "%s --parity=%s", strtmp, "ls");
     610            break;
     611        case 3:
     612            asprintf(&program, "%s --parity=%s", strtmp, "rs");
     613            break;
     614        default:
     615            fatal_error("Unknown RAID parity algorithm.");
     616            break;
     617        }
     618        paranoid_free(strtmp);
     619    }
     620    if (raidlist->el[i].chunk_size != -1) {
     621        asprintf(&strtmp, "%s", program);
     622        paranoid_free(program);
     623        asprintf(&program, "%s --chunk=%d", strtmp,
     624                 raidlist->el[i].chunk_size);
     625        paranoid_free(strtmp);
     626    }
     627    if (raidlist->el[i].spare_disks.entries > 0) {
     628        asprintf(&strtmp, "%s", program);
     629        paranoid_free(program);
     630        asprintf(&program, "%s --spare-devices=%d", strtmp,
     631                 raidlist->el[i].spare_disks.entries);
     632        paranoid_free(strtmp);
     633    }
     634    asprintf(&strtmp, "%s", program);
     635    paranoid_free(program);
     636    asprintf(&program, "%s %s", strtmp, devices);
     637    paranoid_free(strtmp);
     638    res = run_program_and_log_output(program, 1);
     639    // free memory
     640    paranoid_free(devices);
     641    paranoid_free(level);
     642    paranoid_free(program);
     643    // return to calling instance
     644    return res;
    812645}
    813646
     
    823656 * @return 0 for success, nonzero for failure.
    824657 */
    825 int format_device(char *device, char *format, struct raidlist_itself *raidlist)
     658int format_device(char *device, char *format,
     659                  struct raidlist_itself *raidlist)
    826660{
    827661    /** int **************************************************************/
    828     int res;
     662    int res = 0;
    829663    int retval = 0;
    830664#ifdef __FreeBSD__
     
    833667
    834668    /** buffers ***********************************************************/
    835     char *program;
    836     char *tmp;
     669    char *program = NULL;
     670    char *tmp = NULL;
     671    char *tmp1 = NULL;
     672    char *line = NULL;
     673    char *status = NULL;
     674    FILE *pin;
     675    FILE *fin;
     676    size_t n = 0;
     677    size_t n1 = 0;
    837678
    838679    /** end ****************************************************************/
    839680
    840     malloc_string(program);
    841     malloc_string(tmp);
    842681    assert_string_is_neither_NULL_nor_zerolength(device);
    843682    assert(format != NULL);
    844683
    845684    if (strstr(format, "raid")) {   // do not form RAID disks; do it to /dev/md* instead
    846         sprintf(tmp, "Not formatting %s (it is a RAID disk)", device);
     685        asprintf(&tmp, "Not formatting %s (it is a RAID disk)", device);
    847686        log_it(tmp);
    848         paranoid_free(program);
    849687        paranoid_free(tmp);
    850688        return (0);
     
    853691    if (strcmp(format, "swap") == 0) {
    854692        log_it("Not formatting %s - it's swap", device);
    855         paranoid_free(program);
    856         paranoid_free(tmp);
    857693        return (0);
    858694    }
    859695#endif
    860696    if (strlen(format) <= 2) {
    861         sprintf(tmp,
     697        asprintf(&tmp,
    862698                "%s has a really small format type ('%s') - this is probably a hexadecimal string, which would suggest the partition is an image --- I shouldn't format it",
    863699                device, format);
    864700        log_it(tmp);
    865         paranoid_free(program);
    866701        paranoid_free(tmp);
    867702        return (0);
    868703    }
    869704    if (is_this_device_mounted(device)) {
    870         sprintf(tmp, _("%s is mounted - cannot format it       "), device);
     705        asprintf(&tmp, _("%s is mounted - cannot format it       "), device);
    871706        log_to_screen(tmp);
    872         paranoid_free(program);
    873707        paranoid_free(tmp);
    874708        return (1);
     
    880714            if (!does_file_exist("/tmp/raidconf.txt")) {
    881715                log_to_screen
    882                     (_("/tmp/raidconf.txt does not exist. I therefore cannot start Vinum."));
     716                    (_
     717                     ("/tmp/raidconf.txt does not exist. I therefore cannot start Vinum."));
    883718            } else {
    884719                int res;
     
    888723                if (res) {
    889724                    log_to_screen
    890                         (_("`vinum create /tmp/raidconf.txt' returned errors. Please fix them and re-run mondorestore."));
     725                        (_
     726                         ("`vinum create /tmp/raidconf.txt' returned errors. Please fix them and re-run mondorestore."));
    891727                    finish(1);
    892728                }
     
    896732
    897733        if (vinum_started_yet) {
    898             FILE *fin;
    899             char line[MAX_STR_LEN];
    900             sprintf(tmp,
    901                     _("Initializing Vinum device %s (this may take a *long* time)"),
     734            asprintf(&tmp,
     735                    _
     736                    ("Initializing Vinum device %s (this may take a *long* time)"),
    902737                    device);
    903738            log_to_screen(tmp);
     739            paranoid_free(tmp);
     740
    904741            /* format raid partition */
    905             //      sprintf (program, "mkraid --really-force %s", device); --- disabled -- BB, 02/12/2003
    906             sprintf(program,
     742            asprintf(&program,
    907743                    "for plex in `vinum lv -r %s | grep '^P' | tr '\t' ' ' | tr -s ' ' | cut -d' ' -f2`; do echo $plex; done > /tmp/plexes",
    908744                    basename(device));
    909745            system(program);
     746
    910747            if (g_fprep) {
    911748                fprintf(g_fprep, "%s\n", program);
    912749            }
     750            paranoid_free(program);
     751
    913752            fin = fopen("/tmp/plexes", "r");
    914             while (fgets(line, MAX_STR_LEN - 1, fin)) {
     753            while (getline(&line, &n, fin)) {
    915754                if (strchr(line, '\n'))
    916755                    *(strchr(line, '\n')) = '\0';   // get rid of the \n on the end
    917756
    918                 sprintf(tmp, "Initializing plex: %s", line);
     757                asprintf(&tmp, "Initializing plex: %s", line);
    919758                open_evalcall_form(tmp);
    920                 sprintf(tmp, "vinum init %s", line);
     759                paranoid_free(tmp);
     760
     761                asprintf(&tmp, "vinum init %s", line);
    921762                system(tmp);
     763                paranoid_free(tmp);
     764
    922765                while (1) {
    923                     sprintf(tmp,
     766                    asprintf(&tmp,
    924767                            "vinum lp -r %s | grep '^S' | head -1 | tr -s ' ' | cut -d: -f2 | cut -f1 | sed 's/^ //' | sed 's/I //' | sed 's/%%//'",
    925768                            line);
    926                     FILE *pin = popen(tmp, "r");
    927                     char status[MAX_STR_LEN / 4];
    928                     fgets(status, MAX_STR_LEN / 4 - 1, pin);
     769                    pin = popen(tmp, "r");
     770                    paranoid_free(tmp);
     771
     772                    getline(&status, &n1, pin);
    929773                    pclose(pin);
    930774
     
    934778                    update_evalcall_form(atoi(status));
    935779                    usleep(250000);
     780                    paranoid_free(status);
    936781                }
    937782                close_evalcall_form();
    938783            }
     784            paranoid_free(line);
    939785            fclose(fin);
    940786            unlink("/tmp/plexes");
     
    942788        }
    943789#else
    944         sprintf(tmp, _("Initializing RAID device %s"), device);
     790        asprintf(&tmp, _("Initializing RAID device %s"), device);
    945791        log_to_screen(tmp);
     792        paranoid_free(tmp);
    946793
    947794// Shouldn't be necessary.
    948795        log_to_screen(_("Stopping %s"), device);
    949796        stop_raid_device(device);
    950         system("sync");
     797        sync();
    951798        sleep(1);
     799        /* BERLIOS: This code is wrong as program has not been initialized
    952800        if (g_fprep) {
    953801            fprintf(g_fprep, "%s\n", program);
    954802        }
     803        */
    955804
    956805        log_msg(1, "Making %s", device);
    957806        // use mkraid if it exists, otherwise use mdadm
    958         if (run_program_and_log_output("which mkraid", FALSE)) {
    959             res = create_raid_device_via_mdadm(raidlist, device);
    960             log_msg(1, "Creating RAID device %s via mdadm returned %d", device, res);
     807        if (run_program_and_log_output("which mkraid", FALSE)) {
     808            res = create_raid_device_via_mdadm(raidlist, device);
     809            log_msg(1, "Creating RAID device %s via mdadm returned %d",
     810                    device, res);
    961811        } else {
    962             sprintf(program, "mkraid --really-force %s", device);
     812            asprintf(&program, "mkraid --really-force %s", device);
    963813            res = run_program_and_log_output(program, 1);
    964814            log_msg(1, "%s returned %d", program, res);
    965             system("sync");
     815            sync();
    966816            sleep(3);
    967817            start_raid_device(device);
     
    969819                fprintf(g_fprep, "%s\n", program);
    970820            }
    971         }
    972         system("sync");
     821            paranoid_free(program);
     822        }
     823        sync();
    973824        sleep(2);
    974 //      log_to_screen("Starting %s", device);
    975 //      sprintf(program, "raidstart %s", device);
    976 //      res = run_program_and_log_output(program, 1);
    977 //      log_msg(1, "%s returned %d", program, res);
    978 //      system("sync"); sleep(1);
    979 #endif
    980         system("sync");
     825#endif
     826        sync();
    981827        sleep(1);
    982828        newtResume();
     
    987833    if (!strcmp(format, "lvm")) {
    988834        log_msg(1, "Don't format %s - it's part of an lvm volume", device);
    989         paranoid_free(program);
    990         paranoid_free(tmp);
    991835        return (0);
    992836    }
     837    /* This function allocates program */
    993838    res = which_format_command_do_i_need(format, program);
    994     sprintf(tmp, "%s %s", program, device);
    995839    if (strstr(program, "kludge")) {
    996         strcat(tmp, " /");
    997     }
    998     sprintf(program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp);
    999     sprintf(tmp, "Formatting %s as %s", device, format);
     840        asprintf(&tmp, "%s %s /", program, device);
     841    } else {
     842        asprintf(&tmp, "%s %s", program, device);
     843    }
     844    paranoid_free(program);
     845
     846    asprintf(&program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp);
     847    paranoid_free(tmp);
     848
     849    asprintf(&tmp, "Formatting %s as %s", device, format);
    1000850    update_progress_form(tmp);
     851
    1001852    res = run_program_and_log_output(program, FALSE);
    1002853    if (res && strstr(program, "kludge")) {
    1003         sprintf(tmp, "Kludge failed; using regular mkfs.%s to format %s",
    1004                 format, device);
    1005854#ifdef __FreeBSD__
    1006         sprintf(program, "newfs_msdos -F 32 %s", device);
     855        paranoid_free(program);
     856        asprintf(&program, "newfs_msdos -F 32 %s", device);
    1007857#else
    1008858#ifdef __IA64__
    1009859        /* For EFI partitions take fat16
    1010860         * as we want to make small ones */
    1011         sprintf(program, "mkfs -t %s -F 16 %s", format, device);
     861        paranoid_free(program);
     862        asprintf(&program, "mkfs -t %s -F 16 %s", format, device);
    1012863#else
    1013         sprintf(program, "mkfs -t %s -F 32 %s", format, device);
     864        paranoid_free(program);
     865        asprintf(&program, "mkfs -t %s -F 32 %s", format, device);
    1014866#endif
    1015867#endif
     
    1019871        }
    1020872    }
     873    paranoid_free(program);
     874
    1021875    retval += res;
    1022876    if (retval) {
    1023         strcat(tmp, _("...failed"));
     877        asprintf(&tmp1, "%s%s",tmp, _("...failed"));
    1024878    } else {
    1025         strcat(tmp, _("...OK"));
    1026     }
    1027 
    1028     log_to_screen(tmp);
    1029     paranoid_free(program);
     879        asprintf(&tmp1, "%s%s",tmp, _("...OK"));
     880    }
    1030881    paranoid_free(tmp);
    1031     system("sync");
     882
     883    log_to_screen(tmp1);
     884    paranoid_free(tmp1);
     885    sync();
    1032886    sleep(1);
    1033887    return (retval);
    1034888}
    1035 
    1036 
    1037 
    1038889
    1039890
     
    1044895 * @return The number of errors encountered (0 for success).
    1045896 */
    1046 int format_everything(struct mountlist_itself *mountlist, bool interactively,
    1047                           struct raidlist_itself *raidlist)
     897int format_everything(struct mountlist_itself *mountlist,
     898                      bool interactively, struct raidlist_itself *raidlist)
    1048899{
    1049900    /** int **************************************************************/
     
    1068919
    1069920    assert(mountlist != NULL);
    1070     malloc_string(tmp);
    1071     sprintf(tmp, "format_everything (mountlist, interactively = %s",
     921    asprintf(&tmp, "format_everything (mountlist, interactively = %s",
    1072922            (interactively) ? "true" : "false");
    1073923    log_it(tmp);
     924    paranoid_free(tmp);
     925
    1074926    mvaddstr_and_log_it(g_currentY, 0, _("Formatting partitions     "));
    1075927    open_progress_form(_("Formatting partitions"),
     
    1085937    log_msg(1, "Stopping all RAID devices");
    1086938    stop_all_raid_devices(mountlist);
    1087     system("sync");
    1088     system("sync");
    1089     system("sync");
     939    sync();
     940    sync();
     941    sync();
    1090942    sleep(2);
    1091943    log_msg(1, "Prepare soft-RAIDs");   // prep and format too
     
    1096948            if (interactively) {
    1097949                // ask user if we should format the current device
    1098                 sprintf(tmp, "Shall I format %s (%s) ?", me->device,
     950                asprintf(&tmp, "Shall I format %s (%s) ?", me->device,
    1099951                        me->mountpoint);
    1100952                do_it = ask_me_yes_or_no(tmp);
     953                paranoid_free(tmp);
    1101954            } else {
    1102955                do_it = TRUE;
     
    1109962        }
    1110963    }
    1111     system("sync");
    1112     system("sync");
    1113     system("sync");
     964    sync();
     965    sync();
     966    sync();
    1114967    sleep(2);
    1115968// This last step is probably necessary
     
    1126979            newtSuspend();
    1127980        }
    1128 /*
    1129         for(i=0; i<3; i++)
    1130           {
    1131             res = do_my_funky_lvm_stuff(FALSE, FALSE);
    1132             if (!res) { break; }
    1133             sleep(3);
    1134             res = do_my_funky_lvm_stuff(TRUE, FALSE);
    1135             sleep(3);
    1136           }
    1137         if (res) {
    1138             log_msg(1, "Vacuum-packing...");
    1139 */
    1140981        res = do_my_funky_lvm_stuff(FALSE, TRUE);
    1141 /*
    1142         }
    1143 */
    1144982        if (!g_text_mode) {
    1145983            newtResume();
     
    1150988            log_to_screen("Failed to initialize LVM");
    1151989        }
    1152         // retval += res;
    1153990        if (res) {
    1154991            retval++;
     
    1162999        me = &mountlist->el[lino];  // the current mountlist entry
    11631000        if (!strcmp(me->mountpoint, "image")) {
    1164             sprintf(tmp, "Not formatting %s - it's an image", me->device);
     1001            asprintf(&tmp, "Not formatting %s - it's an image", me->device);
    11651002            log_it(tmp);
     1003            paranoid_free(tmp);
    11661004        } else if (!strcmp(me->format, "raid")) {
    1167             sprintf(tmp, "Not formatting %s - it's a raid-let",
     1005            asprintf(&tmp, "Not formatting %s - it's a raid-let",
    11681006                    me->device);
    11691007            log_it(tmp);
     1008            paranoid_free(tmp);
    11701009            continue;
    11711010        } else if (!strcmp(me->format, "lvm")) {
    1172             sprintf(tmp, "Not formatting %s - it's an LVM", me->device);
     1011            asprintf(&tmp, "Not formatting %s - it's an LVM", me->device);
    11731012            log_it(tmp);
     1013            paranoid_free(tmp);
    11741014            continue;
    11751015        } else if (!strncmp(me->device, "/dev/md", 7)) {
    1176             sprintf(tmp, "Already formatted %s - it's a soft-RAID dev",
     1016            asprintf(&tmp, "Already formatted %s - it's a soft-RAID dev",
    11771017                    me->device);
    11781018            log_it(tmp);
     1019            paranoid_free(tmp);
    11791020            continue;
    11801021        } else if (!does_file_exist(me->device)
    11811022                   && strncmp(me->device, "/dev/hd", 7)
    11821023                   && strncmp(me->device, "/dev/sd", 7)) {
    1183             sprintf(tmp,
     1024            asprintf(&tmp,
    11841025                    "Not formatting %s yet - doesn't exist - probably an LVM",
    11851026                    me->device);
    11861027            log_it(tmp);
     1028            paranoid_free(tmp);
    11871029            continue;
    11881030        } else {
    11891031            if (interactively) {
    11901032                // ask user if we should format the current device
    1191                 sprintf(tmp, "Shall I format %s (%s) ?", me->device,
     1033                asprintf(&tmp, "Shall I format %s (%s) ?", me->device,
    11921034                        me->mountpoint);
    11931035                do_it = ask_me_yes_or_no(tmp);
     1036                paranoid_free(tmp);
    11941037            } else {
    11951038                do_it = TRUE;
     
    12151058        mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
    12161059        log_to_screen
    1217             (_("Errors occurred during the formatting of your hard drives."));
     1060            (_
     1061             ("Errors occurred during the formatting of your hard drives."));
    12181062    } else {
    12191063        mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
    12201064    }
    12211065
    1222     sprintf(tmp, "format_everything () - %s",
     1066    asprintf(&tmp, "format_everything () - %s",
    12231067            (retval) ? "failed!" : "finished successfully");
    12241068    log_it(tmp);
     1069    paranoid_free(tmp);
    12251070
    12261071    if (g_partition_table_locked_up > 0) {
     
    12281073//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
    12291074            log_to_screen
    1230                 (_("Partition table locked up %d times. At least one 'mkfs' (format) command"),
     1075                (_
     1076                 ("Partition table locked up %d times. At least one 'mkfs' (format) command"),
    12311077                 g_partition_table_locked_up);
    1232             log_to_screen
    1233                 (_("failed. I think these two events are related. Sometimes, fdisk's ioctl() call"));
    1234             log_to_screen
    1235                 (_("to refresh its copy of the partition table causes the kernel to lock the "));
    1236             log_to_screen
    1237                 (_("partition table. I believe this has just happened."));
     1078            log_to_screen(_
     1079                          ("failed. I think these two events are related. Sometimes, fdisk's ioctl() call"));
     1080            log_to_screen(_
     1081                          ("to refresh its copy of the partition table causes the kernel to lock the "));
     1082            log_to_screen(_
     1083                          ("partition table. I believe this has just happened."));
    12381084            if (ask_me_yes_or_no
    1239                 (_("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue.")))
     1085                (_
     1086                 ("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue.")))
    12401087            {
    1241                 system("sync");
    1242                 system("sync");
    1243                 system("sync");
     1088                sync();
     1089                sync();
     1090                sync();
    12441091                system("reboot");
    12451092            }
    12461093        } else {
    12471094            log_to_screen
    1248                 (_("Partition table locked up %d time%c. However, disk formatting succeeded."),
     1095                (_
     1096                 ("Partition table locked up %d time%c. However, disk formatting succeeded."),
    12491097                 g_partition_table_locked_up,
    12501098                 (g_partition_table_locked_up == 1) ? '.' : 's');
     
    12541102    system("clear");
    12551103    newtResume();
    1256     paranoid_free(tmp);
    12571104    return (retval);
    12581105}
     
    12811128    /** end **************************************************************/
    12821129
    1283     malloc_string(tmp);
    12841130    assert_string_is_neither_NULL_nor_zerolength(drivename);
    12851131
    12861132    if (devno_we_must_allow_for >= 5) {
    1287         sprintf(tmp, "Making dummy primary %s%d", drivename, 1);
     1133        asprintf(&tmp, "Making dummy primary %s%d", drivename, 1);
    12881134        log_it(tmp);
     1135        paranoid_free(tmp);
     1136
    12891137        g_maximum_progress++;
    12901138        res =
     
    12991147    }
    13001148    for (; current_devno < devno_we_must_allow_for; current_devno++) {
    1301         sprintf(tmp, "Creating dummy partition %s%d", drivename,
     1149        asprintf(&tmp, "Creating dummy partition %s%d", drivename,
    13021150                current_devno);
    13031151        log_it(tmp);
     1152        paranoid_free(tmp);
     1153
    13041154        g_maximum_progress++;
    13051155        res =
     
    13091159        previous_devno = current_devno;
    13101160    }
    1311     paranoid_free(tmp);
    13121161    return (previous_devno);
    13131162}
     
    13401189    }
    13411190}
     1191
    13421192
    13431193/* The following 2 functions are stolen from /usr/src/sbin/disklabel/disklabel.c */
     
    14421292    static struct disklabel loclab;
    14431293    struct partition *dp;
    1444     char lnamebuf[BBSIZE];
     1294    char *lnamebuf;
    14451295    int f;
    14461296    u_int secsize, u;
    14471297    off_t mediasize;
    14481298
    1449     (void) snprintf(lnamebuf, BBSIZE, "%s", dkname);
     1299    asprintf(&lnamebuf, "%s", dkname);
    14501300    if ((f = open(lnamebuf, O_RDONLY)) == -1) {
    14511301        warn("cannot open %s", lnamebuf);
     1302        paranoid_free(lnamebuf);
    14521303        return (NULL);
    14531304    }
     1305    paranoid_free(lnamebuf);
    14541306
    14551307    /* New world order */
     
    15221374                         char *drivename, struct disklabel *ret)
    15231375{
    1524     char subdev_str[MAX_STR_LEN];
    1525     char command[MAX_STR_LEN];
     1376    char *subdev_str;
     1377    char *command;
    15261378    struct disklabel *lp;
    15271379    int i, lo = 0;
     
    15331385    for (c = 'a'; c <= 'z'; ++c) {
    15341386        int idx;
    1535         sprintf(subdev_str, "%s%c", drivename, c);
     1387        asprintf(&subdev_str, "%s%c", drivename, c);
    15361388        if ((idx = find_device_in_mountlist(mountlist, subdev_str)) < 0) {
    15371389            lp->d_partitions[c - 'a'].p_size = 0;
     
    15581410                lp->d_partitions[c - 'a'].p_fstype = FS_OTHER;
    15591411        }
     1412        paranoid_free(subdev_str);
    15601413    }
    15611414
     
    15881441    display_disklabel(ftmp, lp);
    15891442    fclose(ftmp);
    1590     sprintf(command, "disklabel -wr %s auto", canonical_name(drivename));
     1443    asprintf(&command, "disklabel -wr %s auto", canonical_name(drivename));
    15911444    retval += run_program_and_log_output(command, TRUE);
    1592     sprintf(command, "disklabel -R %s /tmp/disklabel",
     1445    paranoid_free(command);
     1446
     1447    asprintf(&command, "disklabel -R %s /tmp/disklabel",
    15931448            canonical_name(drivename));
    15941449    retval += run_program_and_log_output(command, TRUE);
     1450    paranoid_free(command);
    15951451    if (ret)
    15961452        *ret = *lp;
     
    16091465{
    16101466    /** int *************************************************************/
    1611     int current_devno;
     1467    int current_devno = 0;
    16121468    int previous_devno = 0;
    1613     int lino;
     1469    int lino = 0;
    16141470    int retval = 0;
    1615     int i;
     1471    int i = 0;
    16161472    FILE *pout_to_fdisk = NULL;
    16171473
    16181474#ifdef __FreeBSD__
    16191475    bool fbsd_part = FALSE;
    1620     char subdev_str[MAX_STR_LEN];
     1476    char *subdev_str = NULL;
    16211477#endif
    16221478
     
    16251481
    16261482    /** buffers *********************************************************/
    1627     char *device_str;
    1628     char *format;
    1629     char *tmp;
     1483    char *device_str = NULL;
     1484    char *format = NULL;
     1485    char *tmp = NULL;
     1486    char *tmp1 = NULL;
    16301487
    16311488    /** end *************************************************************/
     
    16341491    assert_string_is_neither_NULL_nor_zerolength(drivename);
    16351492
    1636     malloc_string(device_str);
    1637     malloc_string(format);
    1638     malloc_string(tmp);
    1639 
    1640     sprintf(tmp, "Partitioning drive %s", drivename);
     1493    asprintf(&tmp, "Partitioning drive %s", drivename);
    16411494    log_it(tmp);
     1495    paranoid_free(tmp);
    16421496
    16431497#if __FreeBSD__
     
    16461500#else
    16471501    make_hole_for_file(FDISK_LOG);
    1648     sprintf(tmp, "parted2fdisk %s >> %s 2>> %s", drivename, FDISK_LOG, FDISK_LOG);
     1502    asprintf(&tmp, "parted2fdisk %s >> %s 2>> %s", drivename, FDISK_LOG,
     1503            FDISK_LOG);
    16491504    pout_to_fdisk = popen(tmp, "w");
     1505    paranoid_free(tmp);
     1506
    16501507    if (!pout_to_fdisk) {
    1651         log_to_screen(_("Cannot call parted2fdisk to configure %s"), drivename);
    1652         paranoid_free(device_str);
    1653         paranoid_free(format);
    1654         paranoid_free(tmp);
     1508        log_to_screen(_("Cannot call parted2fdisk to configure %s"),
     1509                      drivename);
    16551510        return (1);
    16561511    }
    16571512#endif
    16581513    for (current_devno = 1; current_devno < 99; current_devno++) {
    1659         build_partition_name(device_str, drivename, current_devno);
     1514        device_str = build_partition_name(drivename, current_devno);
    16601515        lino = find_device_in_mountlist(mountlist, device_str);
    16611516
     
    16701525                // try DangerouslyDedicated mode
    16711526                for (c = 'a'; c <= 'z'; c++) {
    1672                     sprintf(subdev_str, "%s%c", drivename, c);
    1673                     if (find_device_in_mountlist(mountlist, subdev_str) >
    1674                         0) {
     1527                    asprintf(&subdev_str, "%s%c", drivename, c);
     1528                    if (find_device_in_mountlist(mountlist, subdev_str) > 0) {
    16751529                        fbsd_part = TRUE;
    16761530                    }
     1531                    paranoid_free(subdev_str);
    16771532                }
    16781533                if (fbsd_part) {
     
    16851540                    if (system(command)) {
    16861541                        log_to_screen
    1687                             (_("Warning! Unable to make the drive bootable."));
     1542                            (_
     1543                             ("Warning! Unable to make the drive bootable."));
    16881544                    }
    16891545                    paranoid_free(device_str);
    1690                     paranoid_free(format);
    1691                     paranoid_free(tmp);
     1546
    16921547                    return r;
    16931548                }
    16941549            }
    16951550            for (c = 'a'; c <= 'z'; c++) {
    1696                 sprintf(subdev_str, "%s%c", device_str, c);
     1551                asprintf(&subdev_str, "%s%c", device_str, c);
    16971552                if (find_device_in_mountlist(mountlist, subdev_str) > 0) {
    16981553                    fbsd_part = TRUE;
    16991554                }
     1555                paranoid_free(subdev_str);
    17001556            }
    17011557            // Now we check the subpartitions of the current partition.
     
    17031559                int i, line;
    17041560
    1705                 strcpy(format, "ufs");
     1561                asprintf(&format, "ufs");
    17061562                partsize = 0;
    17071563                for (i = 'a'; i < 'z'; ++i) {
    1708                     sprintf(subdev_str, "%s%c", device_str, i);
     1564                    asprintf(&subdev_str, "%s%c", device_str, i);
    17091565                    line = find_device_in_mountlist(mountlist, subdev_str);
     1566                    paranoid_free(subdev_str);
     1567
    17101568                    if (line > 0) {
    17111569                        // We found one! Add its size to the total size.
     
    17301588#if __FreeBSD__
    17311589            // FreeBSD doesn't let you write to blk devices in <512byte chunks.
    1732 //          sprintf(tmp, "dd if=/dev/zero of=%s count=1 bs=512", drivename);
    1733 //          if (run_program_and_log_output(tmp, TRUE)) {
    17341590            file = open(drivename, O_WRONLY);
    17351591            if (!file) {
    1736                 sprintf(tmp,
     1592                asprintf(&tmp,
    17371593                        _("Warning - unable to open %s for wiping it's partition table"),
    17381594                        drivename);
    17391595                log_to_screen(tmp);
     1596                paranoid_free(tmp);
    17401597            }
    17411598
    17421599            for (i = 0; i < 512; i++) {
    17431600                if (!write(file, "\0", 1)) {
    1744                     sprintf(tmp, _("Warning - unable to write to %s"),
     1601                    asprintf(&tmp, _("Warning - unable to write to %s"),
    17451602                            drivename);
    17461603                    log_to_screen(tmp);
     1604                    paranoid_free(tmp);
    17471605                }
    17481606            }
    1749             system("sync");
     1607            sync();
    17501608#else
    17511609            iamhere("New, kernel-friendly partition remover");
     
    17541612                fflush(pout_to_fdisk);
    17551613            }
    1756 //          sprintf(tmp, "dd if=/dev/zero of=%s count=1 bs=512", drivename);
    1757 //          run_program_and_log_output(tmp, 1);
    17581614#endif
    17591615            if (current_devno > 1) {
     
    17671623#endif
    17681624
    1769             strcpy(format, mountlist->el[lino].format);
     1625            asprintf(&format, mountlist->el[lino].format);
    17701626            partsize = mountlist->el[lino].size;
    17711627
     
    17761632        if (current_devno == 5 && previous_devno == 4) {
    17771633            log_to_screen
    1778                 (_("You must leave at least one partition spare as the Extended partition."));
     1634                (_
     1635                 ("You must leave at least one partition spare as the Extended partition."));
    17791636            paranoid_free(device_str);
    17801637            paranoid_free(format);
    1781             paranoid_free(tmp);
     1638
    17821639            return (1);
    17831640        }
     
    17891646#ifdef __FreeBSD__
    17901647        if ((current_devno <= 4) && fbsd_part) {
    1791             sprintf(tmp, "disklabel -B %s", basename(device_str));
     1648            asprintf(&tmp, "disklabel -B %s", basename(device_str));
    17921649            retval += label_drive_or_slice(mountlist, device_str, 0);
    17931650            if (system(tmp)) {
     
    17951652                    (_("Warning! Unable to make the slice bootable."));
    17961653            }
     1654            paranoid_free(tmp);
    17971655        }
    17981656#endif
     
    18001658        previous_devno = current_devno;
    18011659    }
     1660    paranoid_free(device_str);
     1661    paranoid_free(format);
    18021662
    18031663    if (pout_to_fdisk) {
    1804 // mark relevant partition as bootable
    1805         sprintf(tmp, "a\n%s\n",
    1806                 call_program_and_get_last_line_of_output
    1807                 ("make-me-bootable /tmp/mountlist.txt dummy"));
     1664        // mark relevant partition as bootable
     1665        tmp1 = call_program_and_get_last_line_of_output
     1666                ("make-me-bootable /tmp/mountlist.txt dummy");
     1667        asprintf(&tmp, "a\n%s\n", tmp1);
     1668        paranoid_free(tmp1);
     1669
    18081670        fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
    1809 // close fdisk
     1671        paranoid_free(tmp);
     1672
     1673        // close fdisk
    18101674        fput_string_one_char_at_a_time(pout_to_fdisk, "w\n");
    1811         system("sync");
     1675        sync();
    18121676        paranoid_pclose(pout_to_fdisk);
    18131677        log_msg(0,
    18141678                "------------------- fdisk.log looks like this ------------------");
    1815         sprintf(tmp, "cat %s >> %s", FDISK_LOG, MONDO_LOGFILE);
     1679        asprintf(&tmp, "cat %s >> %s", FDISK_LOG, MONDO_LOGFILE);
    18161680        system(tmp);
     1681        paranoid_free(tmp);
     1682
    18171683        log_msg(0,
    18181684                "------------------- end of fdisk.log... word! ------------------");
    1819         sprintf(tmp, "tail -n6 %s | grep -F \"16: \"", FDISK_LOG);
     1685        asprintf(&tmp, "tail -n6 %s | grep -F \"16: \"", FDISK_LOG);
    18201686        if (!run_program_and_log_output(tmp, 5)) {
    18211687            g_partition_table_locked_up++;
    18221688            log_to_screen
    1823                 (_("A flaw in the Linux kernel has locked the partition table."));
    1824         }
    1825     }
    1826     paranoid_free(device_str);
    1827     paranoid_free(format);
    1828     paranoid_free(tmp);
     1689                (_
     1690                 ("A flaw in the Linux kernel has locked the partition table."));
     1691        }
     1692        paranoid_free(tmp);
     1693    }
    18291694    return (retval);
    18301695}
     1696
    18311697
    18321698/**
     
    18511717    char *partition_name;
    18521718    char *tmp;
    1853     char *logfile;
    18541719    char *output;
    18551720
     
    18611726    /** end ***************************************************************/
    18621727
    1863     malloc_string(program);
    1864     malloc_string(partition_name);
    1865     malloc_string(tmp);
    1866     malloc_string(logfile);
    1867     malloc_string(output);
    1868 
    18691728    assert_string_is_neither_NULL_nor_zerolength(drive);
    18701729    assert(format != NULL);
     
    18741733
    18751734    if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
    1876         sprintf(tmp, "Not partitioning %s - it is a virtual drive", drive);
     1735        asprintf(&tmp, "Not partitioning %s - it is a virtual drive", drive);
    18771736        log_it(tmp);
    1878         paranoid_free(program);
    1879         paranoid_free(partition_name);
    18801737        paranoid_free(tmp);
    1881         paranoid_free(logfile);
    1882         paranoid_free(output);
    18831738        return (0);
    18841739    }
    1885     build_partition_name(partition_name, drive, partno);
     1740    partition_name = build_partition_name(drive, partno);
    18861741    if (partsize <= 0) {
    1887         sprintf(tmp, "Partitioning device %s (max size)", partition_name);
     1742        asprintf(&tmp, "Partitioning device %s (max size)", partition_name);
    18881743    } else {
    1889         sprintf(tmp, "Partitioning device %s (%lld MB)", partition_name,
     1744        asprintf(&tmp, "Partitioning device %s (%lld MB)", partition_name,
    18901745                (long long) partsize / 1024);
    18911746    }
    18921747    update_progress_form(tmp);
    18931748    log_it(tmp);
     1749    paranoid_free(tmp);
    18941750
    18951751    if (is_this_device_mounted(partition_name)) {
    1896         sprintf(tmp, _("%s is mounted, and should not be partitioned"),
     1752        asprintf(&tmp, _("%s is mounted, and should not be partitioned"),
    18971753                partition_name);
     1754        paranoid_free(partition_name);
     1755
    18981756        log_to_screen(tmp);
    1899         paranoid_free(program);
    1900         paranoid_free(partition_name);
    19011757        paranoid_free(tmp);
    1902         paranoid_free(logfile);
    1903         paranoid_free(output);
    19041758        return (1);
    1905 /*
    1906     } else if (does_partition_exist(drive, partno)) {
    1907         sprintf(tmp, "%s already has a partition", partition_name);
    1908         log_to_screen(tmp);
    1909         return (1);
    1910 */
    1911     }
    1912 
    1913 
    1914     /*  sprintf(tmp,"Partitioning %s  ",partition_name); */
    1915     /*  mvaddstr_and_log_it(g_currentY+1,30,tmp); */
     1759    }
     1760
    19161761    p = (char *) strrchr(partition_name, '/');
    1917     sprintf(logfile, "/tmp/fdisk.log.%s", ++p);
    1918     sprintf(program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE,
     1762    asprintf(&program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE,
    19191763            MONDO_LOGFILE);
    19201764
    1921     /* BERLIOS: shoould not be called each time */
     1765    /* BERLIOS: should not be called each time */
    19221766    part_table_fmt = which_partition_format(drive);
    1923     output[0] = '\0';
    19241767    /* make it a primary/extended/logical */
    19251768    if (partno <= 4) {
    1926         sprintf(output + strlen(output), "n\np\n%d\n", partno);
     1769        asprintf(&output,"n\np\n%d\n", partno);
    19271770    } else {
    19281771        /* MBR needs an extended partition if more than 4 partitions */
     
    19311774                if (prev_partno >= 4) {
    19321775                    log_to_screen
    1933                         (_("You need to leave at least one partition free, for 'extended/logical'"));
     1776                        (_
     1777                         ("You need to leave at least one partition free, for 'extended/logical'"));
     1778                    paranoid_free(partition_name);
    19341779                    paranoid_free(program);
    1935                     paranoid_free(partition_name);
    1936                     paranoid_free(tmp);
    1937                     paranoid_free(logfile);
     1780
    19381781                    paranoid_free(output);
    19391782                    return (1);
    19401783                } else {
    1941                     sprintf(output + strlen(output), "n\ne\n%d\n\n\n",
    1942                             prev_partno + 1);
     1784                    asprintf(&output,"n\ne\n%d\n\n\n",prev_partno + 1);
    19431785                }
    19441786            }
    1945             strcat(output + strlen(output), "n\nl\n");
     1787            asprintf(&tmp,"%sn\nl\n",output);
     1788            paranoid_free(output);
     1789            output = tmp;
    19461790        } else {
    19471791            /* GPT allows more than 4 primary partitions */
    1948             sprintf(output + strlen(output), "n\np\n%d\n", partno);
     1792            asprintf(&output,"n\np\n%d\n",partno);
    19491793        }
    19501794    }
    19511795    paranoid_free(part_table_fmt);
    1952     strcat(output + strlen(output), "\n");  /*start block (ENTER for next free blk */
     1796    /*start block (ENTER for next free blk */
     1797    asprintf(&tmp,"%s\n",output);
     1798    paranoid_free(output);
     1799    output = tmp;
     1800
    19531801    if (partsize > 0) {
    19541802        if (!strcmp(format, "7")) {
     
    19561804            partsize += 512;
    19571805        }
    1958         sprintf(output + strlen(output), "+%lldK", (long long) (partsize));
    1959     }
    1960     strcat(output + strlen(output), "\n");
     1806        asprintf(&tmp,"%s+%lldK", output,  (long long) (partsize));
     1807        paranoid_free(output);
     1808        output = tmp;
     1809    }
     1810    asprintf(&tmp,"%s\n",output);
     1811    paranoid_free(output);
     1812    output = tmp;
    19611813#if 0
    19621814/*
    19631815#endif
    1964     sprintf(tmp,"PARTSIZE = +%ld",(long)partsize);
     1816    asprintf(&tmp,"PARTSIZE = +%ld",(long)partsize);
    19651817    log_it(tmp);
     1818    paranoid_free(tmp);
     1819
    19661820    log_it("---fdisk command---");
    19671821    log_it(output);
     
    19771831        fput_string_one_char_at_a_time(pout_to_fdisk, output);
    19781832        fput_string_one_char_at_a_time(pout_to_fdisk, "\n\np\n");
    1979         strcpy(tmp, last_line_of_file(FDISK_LOG));
     1833        tmp = last_line_of_file(FDISK_LOG);
    19801834        if (strstr(tmp, " (m ")) {
    19811835            log_msg(1, "Successfully created %s%d", drive, partno);
     
    19851839                    partno);
    19861840        }
     1841        paranoid_free(tmp);
     1842
    19871843        if (!retval) {
    19881844            log_msg(1, "Trying to set %s%d's partition type now", drive,
     
    20021858        }
    20031859    } else {
    2004         strcat(output, "w\n\n");
     1860        asprintf(&tmp,"%sw\n\n",output);
     1861        paranoid_free(output);
     1862        output = tmp;
     1863
    20051864        if (g_fprep) {
    20061865            fprintf(g_fprep, "echo \"%s\" | %s\n", output, program);
     
    20201879                                 format, -1);
    20211880            if (res) {
    2022                 sprintf(tmp, "Failed to vacuum-pack %s", partition_name);
     1881                asprintf(&tmp, "Failed to vacuum-pack %s", partition_name);
    20231882                log_it(tmp);
     1883                paranoid_free(tmp);
     1884
    20241885                retval++;
    20251886            } else {
     
    20321893                                   partsize);
    20331894            if (retval) {
    2034                 sprintf(tmp, "Partitioned %s but failed to set its type",
     1895                asprintf(&tmp, "Partitioned %s but failed to set its type",
    20351896                        partition_name);
    20361897                log_it(tmp);
     1898                paranoid_free(tmp);
    20371899            } else {
    20381900                if (partsize > 0) {
    2039                     sprintf(tmp, "Partition %s created+configured OK",
     1901                    asprintf(&tmp, "Partition %s created+configured OK",
    20401902                            partition_name);
    20411903                    log_to_screen(tmp);
     1904                    paranoid_free(tmp);
    20421905                } else {
    20431906                    log_it("Returning from a successful vacuum-pack");
     
    20451908            }
    20461909        } else {
    2047             sprintf(tmp, "Failed to partition %s", partition_name);
     1910            asprintf(&tmp, "Failed to partition %s", partition_name);
    20481911            if (partsize > 0) {
    20491912                log_to_screen(tmp);
     
    20511914                log_it(tmp);
    20521915            }
     1916            paranoid_free(tmp);
    20531917            retval++;
    20541918        }
     
    20561920    g_current_progress++;
    20571921    log_it("partition_device() --- leaving");
     1922    paranoid_free(partition_name);
    20581923    paranoid_free(program);
    2059     paranoid_free(partition_name);
    2060     paranoid_free(tmp);
    2061     paranoid_free(logfile);
    20621924    paranoid_free(output);
    20631925    return (retval);
    20641926}
    2065 
    20661927
    20671928
     
    21241985    open_progress_form(_("Partitioning devices"),
    21251986                       _("I am now going to partition all your drives."),
    2126                        _("This should not take more than five minutes."), "",
    2127                        mountlist->entries);
     1987                       _("This should not take more than five minutes."),
     1988                       "", mountlist->entries);
    21281989
    21291990    make_list_of_drives_in_mountlist(mountlist, drivelist);
     
    21381999        mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
    21392000        log_to_screen
    2140             (_("Errors occurred during the partitioning of your hard drives."));
     2001            (_
     2002             ("Errors occurred during the partitioning of your hard drives."));
    21412003    } else {
    21422004        mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
     
    21682030{
    21692031    /** buffers *********************************************************/
    2170     char *partition;
    2171     char *command;
    2172     char *output;
    2173     char *tmp;
    2174     char *partcode;
    2175     char *logfile;
     2032    char *partition = NULL;
     2033    char *command = NULL;
     2034    char *output = NULL;
     2035    char *tmp = NULL;
     2036    char *tmp1 = NULL;
     2037    char *partcode = NULL;
    21762038
    21772039    /** pointers *********************************************************/
    2178     char *p;
    2179     FILE *fout;
     2040    char *p = NULL;
     2041    FILE *fout = NULL;
    21802042
    21812043    /** int **************************************************************/
     
    21872049    assert(format != NULL);
    21882050
    2189     malloc_string(partition);
    2190     malloc_string(command);
    2191     malloc_string(output);
    2192     malloc_string(tmp);
    2193     malloc_string(partcode);
    2194     malloc_string(logfile);
    2195 
    2196     build_partition_name(partition, drive, partno);
     2051    partition = build_partition_name(drive, partno);
    21972052    p = (char *) strrchr(partition, '/');
    2198     sprintf(logfile, "/tmp/fdisk-set-type.%s.log", ++p);
    21992053    if (strcmp(format, "swap") == 0) {
    2200         strcpy(partcode, "82");
     2054        asprintf(&partcode, "82");
    22012055    } else if (strcmp(format, "vfat") == 0) {
    22022056        if (partsize / 1024 > 8192) {
    2203             strcpy(partcode, "c");
     2057            asprintf(&partcode, "c");
    22042058        } else {
    2205             strcpy(partcode, "b");
     2059            asprintf(&partcode, "b");
    22062060        }
    22072061    } else if (strcmp(format, "ext2") == 0
     
    22092063               || strcmp(format, "ext3") == 0 || strcmp(format, "xfs") == 0
    22102064               || strcmp(format, "jfs") == 0) {
    2211         strcpy(partcode, "83");
     2065        asprintf(&partcode, "83");
    22122066    } else if (strcmp(format, "minix") == 0) {
    2213         strcpy(partcode, "81");
     2067        asprintf(&partcode, "81");
    22142068    } else if (strcmp(format, "raid") == 0) {
    2215         strcpy(partcode, "fd");
     2069        asprintf(&partcode, "fd");
    22162070    } else if ((strcmp(format, "ufs") == 0)
    22172071               || (strcmp(format, "ffs") == 0)) {   /* raid autodetect */
    2218         strcpy(partcode, "a5");
     2072        asprintf(&partcode, "a5");
    22192073    } else if (strcmp(format, "lvm") == 0) {
    2220         strcpy(partcode, "8e");
     2074        asprintf(&partcode, "8e");
    22212075    } else if (format[0] == '\0') { /* LVM physical partition */
    2222         partcode[0] = '\0';
     2076        asprintf(&partcode, "");
    22232077    } else if (strlen(format) >= 1 && strlen(format) <= 2) {
    2224         strcpy(partcode, format);
     2078        asprintf(&partcode, format);
    22252079    } else {
    22262080        /* probably an image */
    2227         sprintf(tmp,
     2081        asprintf(&tmp,
    22282082                "Unknown format ('%s') - using supplied string anyway",
    22292083                format);
    22302084        mvaddstr_and_log_it(g_currentY++, 0, tmp);
     2085        paranoid_free(tmp);
    22312086#ifdef __FreeBSD__
    2232         strcpy(partcode, format);   // was a5
     2087        asprintf(&partcode, format);    // was a5
    22332088#else
    2234         strcpy(partcode, format);   // was 83
    2235 #endif
    2236     }
    2237     sprintf(tmp, "Setting %s's type to %s (%s)", partition, format,
     2089        asprintf(&partcode, format);    // was 83
     2090#endif
     2091    }
     2092    asprintf(&tmp, "Setting %s's type to %s (%s)", partition, format,
    22382093            partcode);
     2094    paranoid_free(partition);
     2095
    22392096    log_msg(1, tmp);
     2097    paranoid_free(tmp);
    22402098    if (partcode[0] != '\0' && strcmp(partcode, "83")) {    /* no need to set type if 83: 83 is default */
    22412099
     
    22432101            res = 0;
    22442102            fput_string_one_char_at_a_time(pout_to_fdisk, "t\n");
     2103            tmp1 = last_line_of_file(FDISK_LOG);
    22452104            if (partno > 1
    2246                 || strstr(last_line_of_file(FDISK_LOG), " (1-4)")) {
     2105                || strstr(tmp1, " (1-4)")) {
    22472106                log_msg(5, "Specifying partno (%d) - yay", partno);
    2248                 sprintf(tmp, "%d\n", partno);
     2107                asprintf(&tmp, "%d\n", partno);
    22492108                fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
    2250                 log_msg(5, "A - last line = '%s'",
    2251                         last_line_of_file(FDISK_LOG));
    2252             }
    2253 
    2254             sprintf(tmp, "%s\n", partcode);
     2109                paranoid_free(tmp);
     2110                paranoid_free(tmp1);
     2111                tmp1 = last_line_of_file(FDISK_LOG);
     2112                log_msg(5, "A - last line = '%s'", tmp1);
     2113            }
     2114            paranoid_free(tmp1);
     2115
     2116            asprintf(&tmp, "%s\n", partcode);
    22552117            fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
    2256             log_msg(5, "B - last line = '%s'",
    2257                     last_line_of_file(FDISK_LOG));
     2118            paranoid_free(tmp);
     2119            tmp1 = last_line_of_file(FDISK_LOG);
     2120            log_msg(5, "B - last line = '%s'",tmp1);
     2121            paranoid_free(tmp1);
     2122
    22582123            fput_string_one_char_at_a_time(pout_to_fdisk, "\n");
    2259             log_msg(5, "C - last line = '%s'",
    2260                     last_line_of_file(FDISK_LOG));
    2261 
    2262             strcpy(tmp, last_line_of_file(FDISK_LOG));
     2124            tmp1 = last_line_of_file(FDISK_LOG);
     2125            log_msg(5, "C - last line = '%s'",tmp1);
     2126            paranoid_free(tmp1);
     2127
     2128            tmp = last_line_of_file(FDISK_LOG);
    22632129            if (!strstr(tmp, " (m ")) {
    22642130                log_msg(1, "last line = '%s'; part type set failed", tmp);
     
    22662132                fput_string_one_char_at_a_time(pout_to_fdisk, "\n");
    22672133            }
     2134            paranoid_free(tmp);
    22682135            fput_string_one_char_at_a_time(pout_to_fdisk, "p\n");
    22692136        } else {
    2270             sprintf(output, "t\n%d\n%s\n", partno, partcode);
    2271             strcat(output, "w\n");
    2272             sprintf(command, "parted2fdisk %s >> %s 2>> %s", drive,
     2137            asprintf(&output, "t\n%d\n%s\nw\n", partno, partcode);
     2138            asprintf(&command, "parted2fdisk %s >> %s 2>> %s", drive,
    22732139                    MONDO_LOGFILE, MONDO_LOGFILE);
    22742140            log_msg(5, "output = '%s'", output);
     
    22842150                paranoid_pclose(fout);
    22852151            }
    2286         }
     2152            paranoid_free(command);
     2153            paranoid_free(output);
     2154        }
     2155        /* BERLIOS: Useless as command not initialized in all cases
    22872156        if (res) {
    22882157            log_OS_error(command);
    22892158        }
    2290     }
    2291 
    2292     paranoid_free(partition);
    2293     paranoid_free(command);
    2294     paranoid_free(output);
    2295     paranoid_free(tmp);
     2159        */
     2160    }
    22962161    paranoid_free(partcode);
    2297     paranoid_free(logfile);
     2162
    22982163
    22992164    return (res);
     
    23132178
    23142179    assert_string_is_neither_NULL_nor_zerolength(raid_device);
    2315     malloc_string(program);
    23162180
    23172181#ifdef __FreeBSD__
     
    23202184        return 1;
    23212185    }
    2322     sprintf(program, "vinum start -f %s", raid_device);
     2186    asprintf(&program, "vinum start -f %s", raid_device);
    23232187#else
    2324     sprintf(program, "raidstart %s", raid_device);
     2188    asprintf(&program, "raidstart %s", raid_device);
    23252189//      sprintf (program, "raidstart " RAID_DEVICE_STUB "*");
    23262190#endif
     
    23302194        fprintf(g_fprep, "%s\n", program);
    23312195    }
     2196    paranoid_free(program);
     2197
    23322198    if (res) {
    23332199        log_msg(1, "Warning - failed to start RAID device %s",
     
    23382204    return (retval);
    23392205}
    2340 
    23412206
    23422207
     
    23582223
    23592224    assert_string_is_neither_NULL_nor_zerolength(raid_device);
    2360     malloc_string(program);
    23612225
    23622226#ifdef __FreeBSD__
     
    23652229        return 1;
    23662230    }
    2367     sprintf(program, "vinum stop -f %s", raid_device);
     2231    asprintf(&program, "vinum stop -f %s", raid_device);
    23682232#else
    2369     // use raidstop if it exists, otherwise use mdadm
    2370     if (run_program_and_log_output("which raidstop", FALSE)) {
    2371         sprintf(program, "mdadm -S %s", raid_device);
     2233    // use raidstop if it exists, otherwise use mdadm
     2234    if (run_program_and_log_output("which raidstop", FALSE)) {
     2235        asprintf(&program, "mdadm -S %s", raid_device);
    23722236    } else {
    2373         sprintf(program, "raidstop %s", raid_device);
     2237        asprintf(&program, "raidstop %s", raid_device);
    23742238    }
    23752239#endif
     
    23792243        fprintf(g_fprep, "%s\n", program);
    23802244    }
     2245    paranoid_free(program);
     2246
    23812247    if (res) {
    23822248        log_msg(1, "Warning - failed to stop RAID device %s", raid_device);
     
    24092275    return (retval);
    24102276}
     2277
    24112278
    24122279/**
     
    24212288    int retval = 0;
    24222289#ifndef __FreeBSD__
    2423     int res;
     2290    int res = 0;
    24242291#endif
    24252292
    24262293    /** char ************************************************************/
    2427     char *incoming;
     2294    char *incoming = NULL;
    24282295#ifndef __FreeBSD__
    2429     char *dev;
     2296    char *dev = NULL;
    24302297#endif
    24312298    /** pointers ********************************************************/
    24322299#ifndef __FreeBSD__
    2433     char *p;
    2434 #endif
    2435     FILE *fin;
    2436     int i;
     2300    char *p = NULL;
     2301#endif
     2302    FILE *fin = NULL;
     2303    int i = 0;
     2304    size_t n = 0;
    24372305
    24382306    /** end ****************************************************************/
    24392307
    2440     malloc_string(dev);
    2441     malloc_string(incoming);
    24422308    assert(mountlist != NULL);
    24432309
     
    24492315             "r");
    24502316        if (!fin) {
    2451             paranoid_free(dev);
    2452             paranoid_free(incoming);
    24532317            return (1);
    24542318        }
    2455         for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);
    2456              fgets(incoming, MAX_STR_LEN - 1, fin)) {
     2319        for (getline(&incoming, &n, fin); !feof(fin);
     2320             getline(&incoming, &n, fin)) {
    24572321            retval += stop_raid_device(incoming);
    24582322        }
     
    24612325        if (!fin) {
    24622326            log_OS_error("/proc/mdstat");
    2463             paranoid_free(dev);
    2464             paranoid_free(incoming);
    24652327            return (1);
    24662328        }
    2467         for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);
    2468              fgets(incoming, MAX_STR_LEN - 1, fin)) {
     2329        for (getline(&incoming, &n, fin); !feof(fin);
     2330             getline(&incoming, &n, fin)) {
    24692331            for (p = incoming;
    24702332                 *p != '\0' && (*p != 'm' || *(p + 1) != 'd'
    24712333                                || !isdigit(*(p + 2))); p++);
    24722334            if (*p != '\0') {
    2473                 sprintf(dev, "/dev/%s", p);
     2335                asprintf(&dev, "/dev/%s", p);
     2336                /* BERLIOS : 32 Hard coded value */
    24742337                for (p = dev; *p > 32; p++);
    24752338                *p = '\0';
    24762339                res = stop_raid_device(dev);
    2477             }
    2478         }
    2479 #endif
     2340                paranoid_free(dev);
     2341            }
     2342        }
     2343#endif
     2344        paranoid_free(incoming);
    24802345    }
    24812346    paranoid_fclose(fin);
     
    24832348        log_msg(1, "Warning - unable to stop some RAID devices");
    24842349    }
    2485     paranoid_free(dev);
    2486     paranoid_free(incoming);
    2487     system("sync");
    2488     system("sync");
    2489     system("sync");
     2350    sync();
     2351    sync();
     2352    sync();
    24902353    sleep(1);
    24912354    return (retval);
    24922355}
    2493 
    24942356
    24952357
     
    25102372    /** end ***************************************************************/
    25112373
    2512     malloc_string(tmp);
    25132374    assert_string_is_neither_NULL_nor_zerolength(format);
    25142375    assert(program != NULL);
     
    25422403        sprintf(program, "mkfs -t %s -c", format);  // -c checks for bad blocks
    25432404#endif
    2544         sprintf(tmp, "Unknown format (%s) - assuming '%s' will do", format,
     2405        asprintf(&tmp, "Unknown format (%s) - assuming '%s' will do", format,
    25452406                program);
    25462407        log_it(tmp);
     2408        paranoid_free(tmp);
    25472409        res = 0;
    25482410    }
    2549     paranoid_free(tmp);
    25502411    return (res);
    25512412}
     
    25732434    /** end *************************************************************/
    25742435
    2575     malloc_string(tmp);
    25762436    assert(mountlist != NULL);
    25772437    assert_string_is_neither_NULL_nor_zerolength(drive_name);
     
    25842444            original_size_of_drive += mountlist->el[partno].size;
    25852445        } else {
    2586             sprintf(tmp, "Skipping %s", mountlist->el[partno].device);
     2446            asprintf(&tmp, "Skipping %s", mountlist->el[partno].device);
    25872447//                      log_to_screen(tmp);
     2448            paranoid_free(tmp);
    25882449        }
    25892450    }
    25902451    original_size_of_drive = original_size_of_drive / 1024;
    2591     paranoid_free(tmp);
    25922452    return (original_size_of_drive);
    25932453}
     
    26322492    /** end **************************************************************/
    26332493
    2634     malloc_string(tmp);
    26352494    assert(mountlist != NULL);
    26362495    assert_string_is_neither_NULL_nor_zerolength(drive_name);
     
    26392498        if (strncmp(drive_name, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))
    26402499            == 0) {
    2641             paranoid_free(tmp);
    26422500            return;
    26432501        }
     
    26532511    if (current_size_of_drive <= 0) {
    26542512        log_it("Not resizing to match %s - can't find drive", drive_name);
    2655         paranoid_free(tmp);
    26562513        return;
    26572514    }
    2658     sprintf(tmp, _("Expanding entries to suit drive %s (%ld MB)"), drive_name,
    2659             current_size_of_drive);
     2515    asprintf(&tmp, _("Expanding entries to suit drive %s (%ld MB)"),
     2516            drive_name, current_size_of_drive);
    26602517    log_to_screen(tmp);
     2518    paranoid_free(tmp);
    26612519
    26622520    drivemntlist = malloc(sizeof(struct mountlist_reference));
     
    26752533
    26762534    if (original_size_of_drive <= 0) {
    2677         sprintf(tmp, _("Cannot resize %s's entries. Drive not found."),
     2535        asprintf(&tmp, _("Cannot resize %s's entries. Drive not found."),
    26782536                drive_name);
    26792537        log_to_screen(tmp);
     
    26832541    factor =
    26842542        (float) (current_size_of_drive) / (float) (original_size_of_drive);
    2685     sprintf(tmp, "Disk %s was %ld MB; is now %ld MB; factor = %f",
     2543    asprintf(&tmp, "Disk %s was %ld MB; is now %ld MB; factor = %f",
    26862544            drive_name, original_size_of_drive, current_size_of_drive,
    26872545            factor);
    26882546    log_to_screen(tmp);
     2547    paranoid_free(tmp);
    26892548
    26902549    lastpart = drivemntlist->entries - 1;
     
    27052564            newsizL = (long) new_size;
    27062565        }
    2707         sprintf(tmp, _("Changing %s from %lld KB to %ld KB"),
     2566        asprintf(&tmp, _("Changing %s from %lld KB to %ld KB"),
    27082567                drivemntlist->el[partno]->device,
    27092568                drivemntlist->el[partno]->size, newsizL);
    27102569        log_to_screen(tmp);
     2570        paranoid_free(tmp);
    27112571        drivemntlist->el[partno]->size = newsizL;
    27122572    }
    27132573    final_size = get_phys_size_of_drive(drive_name);
    2714     sprintf(tmp, _("final_size = %ld MB"), final_size);
     2574    asprintf(&tmp, _("final_size = %ld MB"), final_size);
    27152575    log_to_screen(tmp);
    27162576    paranoid_free(tmp);
     
    27782638    log_msg(1, "Creating list of partitions for drive %s", drive_name);
    27792639
    2780     tmp_drive_name = strdup(drive_name);
     2640    asprintf(&tmp_drive_name,drive_name);
    27812641    if (!tmp_drive_name)
    27822642        fatal_error("Out of memory");
     
    27982658        }
    27992659    }
    2800     if (tmp_drive_name)
    2801         free(tmp_drive_name);
     2660    paranoid_free(tmp_drive_name);
    28022661}
    28032662
Note: See TracChangeset for help on using the changeset viewer.