Changeset 171 in MondoRescue for trunk/mondo/mondo/common/libmondo-devices.c


Ignore:
Timestamp:
Dec 8, 2005, 5:20:29 PM (18 years ago)
Author:
bcornec
Message:

memory management continues:

  • mondoarchive handled completely
  • bkpinfo, begining of dyn. alloc.
  • lot of changes around memory everywhere

=> even if it compiles, i'm pretty sure it doesn't work yet (even not tried)

File:
1 edited

Legend:

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

    r162 r171  
    1 /* libmondo-devices.c                 Subroutines for handling devices
    2    $Id$
    3 */
    4 
     1/* $Id$
     2 * Subroutines for handling devices
     3 */
    54/**
    65 * @file
     
    4342extern char *g_selfmounted_isodir;
    4443
    45 static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
    46 static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
    47 static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
     44static char *g_cdrw_drive_is_here = NULL;
     45static char *g_cdrom_drive_is_here = NULL;
     46static char *g_dvd_drive_is_here = NULL;
    4847
    4948
     
    6160void set_g_cdrom_and_g_dvd_to_bkpinfo_value(struct s_bkpinfo *bkpinfo)
    6261{
    63     strcpy(g_cdrom_drive_is_here, bkpinfo->media_device);   // just in case
    64     strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
     62    if (bkpinfo->media_device != NULL) {
     63        paranoid_free(g_cdrom_drive_is_here);
     64        asprintf(&g_cdrom_drive_is_here, bkpinfo->media_device);    // just in case
     65    }
     66    if (bkpinfo->media_device != NULL) {
     67        paranoid_free(g_dvd_drive_is_here);
     68        asprintf(&g_dvd_drive_is_here, bkpinfo->media_device);  // just in case
     69    }
    6570}
    6671
     
    152157 * @ingroup stringGroup
    153158 */
    154 static char *bkptype_to_string(t_bkptype bt)
    155 {
    156     static char *output = NULL;
     159char *bkptype_to_string(t_bkptype bt)
     160{
     161    char *output = NULL;
    157162
    158163    paranoid_free(output);
     
    319324    /*@ ints ******************************************************* */
    320325    int res = 0;
    321     int n = 0;
     326    size_t n = 0;
    322327
    323328    /*@ pointers *************************************************** */
     
    432437 * @see mount_CDROM_here
    433438 */
    434 int find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint)
     439bool find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint)
    435440{
    436441    /*@ buffers ***************************************************** */
    437442
    438443    /*@ int's  ****************************************************** */
    439     int res;
    440     char *dev;
     444    bool res = TRUE;
     445    char *dev = NULL;
    441446
    442447    /*@ end vars **************************************************** */
    443448
    444     malloc_string(dev);
    445449    assert(bkpinfo != NULL);
    446450    assert_string_is_neither_NULL_nor_zerolength(mountpoint);
    447451
    448452    if (g_backup_media_type == dvd) {
    449         strcpy(dev, g_dvd_drive_is_here);
    450         if (!dev[0]) {
    451             find_dvd_device(dev, FALSE);
    452         }
    453     } else {
    454         strcpy(dev, g_cdrom_drive_is_here);
    455         if (!dev[0]) {
    456             find_cdrom_device(dev, FALSE);
     453        if (g_dvd_drive_is_here != NULL) {
     454            asprintf(&dev, g_dvd_drive_is_here);
     455        } else {
     456            dev = find_dvd_device();
     457        }
     458    } else {
     459        if (g_cdrom_drive_is_here != NULL) {
     460            asprintf(&dev, g_cdrom_drive_is_here);
     461        } else {
     462            dev = find_cdrom_device(FALSE);
    457463        }
    458464    }
     
    462468    }
    463469
    464     if (!dev[0] || (res = mount_CDROM_here(dev, mountpoint))) {
     470    if ((dev == NULL) || (! mount_CDROM_here(dev, mountpoint))) {
    465471        if (!popup_and_get_string
    466472            ("CD-ROM device", "Please enter your CD-ROM's /dev device",
    467473             dev, MAX_STR_LEN / 4)) {
    468             res = 1;
     474            res = FALSE;
    469475        } else {
    470476            res = mount_CDROM_here(dev, mountpoint);
     
    477483    }
    478484    paranoid_free(dev);
    479     return (res);
     485    return(res);
    480486}
    481487
     
    483489/**
    484490 * Locate a CD-R/W writer's SCSI node.
    485  * @param cdrw_device SCSI node will be placed here.
    486  * @return 0 for success, nonzero for failure.
    487  */
    488 int find_cdrw_device(char *cdrw_device)
     491 * @param cdrw_device SCSI node will be placed here. Caller needs to free it.
     492 * @return the cdrw device or NULL if not found
     493 */
     494char *find_cdrw_device(void)
    489495{
    490496    /*@ buffers ************************ */
     
    493499    char *cdr_exe;
    494500    char *command;
    495 
    496     if (g_cdrw_drive_is_here[0]) {
    497         strcpy(cdrw_device, g_cdrw_drive_is_here);
     501    char *cdrw_device;
     502
     503    if (g_cdrw_drive_is_here != NULL) {
     504        asprintf(&cdrw_device, g_cdrw_drive_is_here);
    498505        log_msg(3, "Been there, done that. Returning %s", cdrw_device);
    499         return (0);
     506        return(cdrw_device);
    500507    }
    501508    if (g_backup_media_type == dvd) {
    502509        log_msg(1,
    503510                "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
    504         return (1);
     511        return(NULL);
    505512    }
    506513    run_program_and_log_output("insmod ide-scsi", -1);
     
    517524        paranoid_free(command);
    518525    } else {
    519         asprintf(&tmp, "");
     526        asprintf(&tmp, " ");
    520527    }
    521528    paranoid_free(cdr_exe);
     
    523530    if (strlen(tmp) < 2) {
    524531        paranoid_free(tmp);
    525         return 1;
    526     } else {
    527         strcpy(cdrw_device, tmp);
    528         paranoid_free(tmp);
     532        return(NULL);
     533    } else {
     534        cdrw_device = tmp;
    529535
    530536        asprintf(&comment, "Found CDRW device - %s", cdrw_device);
     
    532538        paranoid_free(comment);
    533539
    534         strcpy(g_cdrw_drive_is_here, cdrw_device);
    535         return (0);
     540        paranoid_free(g_cdrw_drive_is_here);
     541        asprintf(&g_cdrw_drive_is_here, cdrw_device);
     542        return(cdrw_device);
    536543    }
    537544}
     
    542549 * Several different methods may be used to find the device, including
    543550 * calling @c cdrecord, searching @c dmesg, and trial-and-error.
    544  * @param output Where to put the located /dev entry.
     551 * @param output Where to put the located /dev entry. Needs to be freed by the caller.
    545552 * @param try_to_mount Whether to mount the CD as part of the test; if mount
    546553 * fails then return failure.
    547  * @return 0 for success, nonzero for failure.
    548  */
    549 int find_cdrom_device(char *output, bool try_to_mount)
     554 * @return output if success or NULL otherwise.
     555 */
     556char *find_cdrom_device(bool try_to_mount)
    550557{
    551558    /*@ pointers **************************************************** */
     
    554561    char *q;
    555562    char *r;
    556     int n = 0;
    557     int retval = 0;
     563    char *output = NULL;
     564    size_t n = 0;
    558565
    559566    /*@ bool's ****************************************************** */
     
    571578    static char the_last_place_i_found_it[MAX_STR_LEN] = "";
    572579
    573     /*@ intialize *************************************************** */
    574 
    575     output[0] = '\0';
    576 
    577580    /*@ end vars **************************************************** */
    578581
    579     if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
    580         strcpy(output, g_cdrom_drive_is_here);
     582    if ((g_cdrom_drive_is_here != NULL) && !isdigit(g_cdrom_drive_is_here[0])) {
     583        asprintf(&output, g_cdrom_drive_is_here);
    581584        log_msg(3, "Been there, done that. Returning %s", output);
    582         return(0);
     585        return(output);
    583586    }
    584587    if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
    585         strcpy(output, the_last_place_i_found_it);
     588        asprintf(&output, the_last_place_i_found_it);
    586589        log_msg(3,
    587590                "find_cdrom_device() --- returning last found location - '%s'",
    588591                output);
    589         return(0);
     592        return(output);
    590593    }
    591594
     
    596599    }
    597600    if (!find_home_of_exe(cdr_exe)) {
    598         strcpy(output, "/dev/cdrom");
     601        asprintf(&output, "/dev/cdrom");
    599602        log_msg(4, "Can't find cdrecord; assuming %s", output);
    600603        if (!does_device_exist(output)) {
    601604            log_msg(4, "That didn't work. Sorry.");
    602605            paranoid_free(cdr_exe);
    603             return(1);
     606            paranoid_free(output);
     607            return(NULL);
    604608        } else {
    605609            paranoid_free(cdr_exe);
    606             return(0);
     610            return(output);
    607611        }
    608612    }
     
    615619        paranoid_free(cdr_exe);
    616620        paranoid_free(command);
    617         return (1);
     621        return (NULL);
    618622    }
    619623    paranoid_free(command);
     
    672676                                    tmp);
    673677                        } else {
    674                             sprintf(output, "/dev/%s", tmp);
     678                            asprintf(&output, "/dev/%s", tmp);
    675679                            found_it = TRUE;
    676680                        }
     
    713717                                {
    714718                                    paranoid_free(cdr_exe);
    715                                     return(1);
     719                                    return(NULL);
    716720                                }
    717721                            }
     
    727731            log_msg(4, "Well, I'll use the DVD - %s - as a last resort",
    728732                    dvd_last_resort);
    729             strcpy(output, dvd_last_resort);
     733            paranoid_free(output);
     734            asprintf(&output, dvd_last_resort);
    730735            found_it = TRUE;
    731736        }
     
    741746                    output);
    742747            found_it = FALSE;
    743             output[0] = '\0';
     748            paranoid_free(output);
    744749        }
    745750        paranoid_free(tmp);
     
    749754        log_msg(4, "(find_cdrom_device) --> '%s'", output);
    750755        if (!does_device_exist(output)) {
     756            log_msg(4, "OK, I was wrong, I haven't found it... yet.");
    751757            found_it = FALSE;
    752             log_msg(4, "OK, I was wrong, I haven't found it... yet.");
     758            paranoid_free(output);
    753759        }
    754760    }
     
    784790                                    {
    785791                                        paranoid_free(cdr_exe);
    786                                         return(1);
     792                                        return(NULL);
    787793                                    }
    788794                                }
     
    800806
    801807    if (found_it && try_to_mount) {
    802         if (mount_CDROM_here(output, mountpoint)) {
     808        if (! mount_CDROM_here(output, mountpoint)) {
    803809            log_msg(4, "[Cardigans] I've changed my mind");
    804810            found_it = FALSE;
     811            paranoid_free(output);
    805812        } else {
    806813            asprintf(&tmp, "%s/archives", mountpoint);
     
    808815                log_msg(4, "[Cardigans] I'll take it back");
    809816                found_it = FALSE;
     817                paranoid_free(output);
    810818            } else {
    811819                asprintf(&command, "umount %s", output);
     
    823831        if (!does_file_exist(output)) {
    824832            log_msg(3, "I still haven't found it.");
    825             return (1);
     833            paranoid_free(output);
     834            return(NULL);
    826835        }
    827836        log_msg(3, "(find_cdrom_device) --> '%s'", output);
    828837        strcpy(the_last_place_i_found_it, output);
    829         strcpy(g_cdrom_drive_is_here, output);
    830         return(0);
     838        paranoid_free(g_cdrom_drive_is_here);
     839        asprintf(&g_cdrom_drive_is_here, output);
     840        return(output);
    831841    }
    832842
     
    834844            "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2",
    835845            cdr_exe, g_cdrw_drive_is_here);
     846    paranoid_free(cdr_exe);
     847
    836848    log_msg(1, "command=%s", command);
    837849    asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     
    839851
    840852    if (tmp[0]) {
    841         strcpy(output, tmp);
     853        output = tmp;
    842854        log_msg(4, "Finally found it at %s", output);
    843         retval = 0;
    844     } else {
     855    } else {
     856        paranoid_free(tmp);
     857        paranoid_free(output);
    845858        log_msg(4, "Still couldn't find it.");
    846         retval = 1;
    847     }
    848     paranoid_free(tmp);
    849     paranoid_free(cdr_exe);
    850     return (retval);
    851 }
    852 
    853 
    854 int find_dvd_device(char *output, bool try_to_mount)
     859    }
     860    return(output);
     861}
     862
     863
     864char *find_dvd_device()
    855865{
    856866    char *tmp;
    857867    int retval = 0, devno = -1;
    858 
    859     if (g_dvd_drive_is_here[0]) {
    860         strcpy(output, g_dvd_drive_is_here);
     868    char *output = NULL;
     869
     870    if (g_dvd_drive_is_here != NULL) {
     871        asprintf(&output, g_dvd_drive_is_here);
    861872        log_msg(3, "Been there, done that. Returning %s", output);
    862         return (0);
     873        return (output);
    863874    }
    864875
     
    880891    if (devno >= 0) {
    881892        retval = 0;
    882         sprintf(output, "/dev/scd%d", devno);
    883         strcpy(g_dvd_drive_is_here, output);
     893        asprintf(&output, "/dev/scd%d", devno);
     894        paranoid_free(g_dvd_drive_is_here);
     895        asprintf(&g_dvd_drive_is_here, output);
    884896        log_msg(2, "I think DVD is at %s", output);
    885897    } else {
    886898        log_msg(2, "I cannot find DVD");
    887         retval = 1;
    888     }
    889 
    890     if (try_to_mount) {
    891         log_msg(1, "Ignoring the fact that try_to_mount==TRUE");
    892     }
    893     return (retval);
     899    }
     900
     901    return(output);
    894902}
    895903
     
    10641072    char *device_with_space;
    10651073    char *tmp;
    1066     int n = 0;
     1074    size_t n = 0;
    10671075
    10681076#ifdef __FreeBSD__
     
    12121220 * @param device The device (or file if g_ISO_restore_mode) to mount.
    12131221 * @param mountpoint The place to mount it.
    1214  * @return 0 for success, nonzero for failure.
    1215  */
    1216 int mount_CDROM_here(char *device, char *mountpoint)
     1222 * @return TRUE for success, FALSE for failure.
     1223 */
     1224bool mount_CDROM_here(char *device, char *mountpoint)
    12171225{
    12181226    /*@ buffer ****************************************************** */
     
    12251233    make_hole_for_dir(mountpoint);
    12261234    if (isdigit(device[0])) {
    1227         find_cdrom_device(device, FALSE);
     1235        paranoid_free(device);
     1236        device = find_cdrom_device(FALSE);
    12281237    }
    12291238    if (g_ISO_restore_mode) {
     
    12381247            fatal_error(command);
    12391248        }
    1240         strcpy(device, dev);
    1241         paranoid_free(dev);
     1249        paranoid_free(device);
     1250        device = dev;
    12421251#endif
    12431252    }
     
    12631272    paranoid_free(command);
    12641273
    1265     return (retval);
     1274    if (retval == 0) {
     1275        return(TRUE);
     1276    } else {
     1277        return(FALSE);
     1278    }
    12661279}
    12671280
     
    13311344        }
    13321345        log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
    1333         if (mount_CDROM_here(tmp, MNT_CDROM)) {
     1346        if (! mount_CDROM_here(tmp, MNT_CDROM)) {
    13341347            fatal_error("Mommy!");
    13351348        }
     
    14661479                    media_descriptor_string(bkpinfo->backup_media_type));
    14671480            if (bkpinfo->backup_media_type == dvd) {
    1468                 find_dvd_device(bkpinfo->media_device, FALSE);
     1481                paranoid_free(bkpinfo->media_device);
     1482                bkpinfo->media_device = find_dvd_device();
    14691483                asprintf(&tmp, "1");
    14701484                asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE);    // 4.7 salesman's GB = 4.482 real GB = 4582 MB
    14711485                log_msg(1, "Setting to DVD defaults");
    14721486            } else {
    1473                 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
     1487                paranoid_alloc(bkpinfo->media_device,VANILLA_SCSI_CDROM );
    14741488                asprintf(&tmp, "4");
    14751489                asprintf(&sz_size, "650");
     
    15081522    case cdstream:
    15091523        if (bkpinfo->disaster_recovery) {
    1510             strcpy(bkpinfo->media_device, "/dev/cdrom");
     1524            paranoid_alloc(bkpinfo->media_device, "/dev/cdrom");
    15111525            log_msg(2, "CD-ROM device assumed to be at %s",
    15121526                    bkpinfo->media_device);
    15131527        } else if (bkpinfo->restore_data
    15141528                   || bkpinfo->backup_media_type == dvd) {
    1515             if (!bkpinfo->media_device[0]) {
    1516                 strcpy(bkpinfo->media_device, "/dev/cdrom");
     1529            if (bkpinfo->media_device == NULL) {
     1530                paranoid_alloc(bkpinfo->media_device, "/dev/cdrom");
    15171531            }                   // just for the heck of it :)
    15181532            log_msg(1, "bkpinfo->media_device = %s",
    15191533                    bkpinfo->media_device);
    1520             if (bkpinfo->backup_media_type == dvd
    1521                 || find_cdrom_device(bkpinfo->media_device, FALSE)) {
     1534            if ((bkpinfo->backup_media_type == dvd)
     1535                || ((tmp = find_cdrom_device(FALSE)) != NULL)) {
     1536                paranoid_free(bkpinfo->media_device);
     1537                bkpinfo->media_device = tmp;
    15221538                log_msg(1, "bkpinfo->media_device = %s",
    15231539                        bkpinfo->media_device);
     
    15381554                    bkpinfo->media_device);
    15391555        } else {
    1540             if (find_cdrw_device(bkpinfo->media_device)) {
    1541                 bkpinfo->media_device[0] = '\0';
    1542             }
    1543             if (bkpinfo->media_device[0]) {
     1556            paranoid_free(bkpinfo->media_device);
     1557            bkpinfo->media_device = find_cdrw_device();
     1558            if (bkpinfo->media_device != NULL) {
    15441559                asprintf(&tmp,
    15451560                        "I think I've found your %s burner at SCSI node %s; am I right on the money?",
     
    15481563                        bkpinfo->media_device);
    15491564                if (!ask_me_yes_or_no(tmp)) {
    1550                     bkpinfo->media_device[0] = '\0';
     1565                    paranoid_free(bkpinfo->media_device);
    15511566                }
    15521567                paranoid_free(tmp);
    1553             }
    1554             if (!bkpinfo->media_device[0]) {
     1568            } else {
    15551569                if (g_kernel_version < 2.6) {
    15561570                    i = popup_and_get_string("Device node?",
     
    15841598    case tape:
    15851599
     1600        paranoid_free(bkpinfo->media_device);
    15861601        if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) {
    15871602            log_msg(3, "Ok, using vanilla scsi tape.");
    1588             strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
     1603            paranoid_alloc(bkpinfo->media_device,VANILLA_SCSI_TAPE );
    15891604            if ((fin = fopen(bkpinfo->media_device, "r"))) {
    15901605                paranoid_fclose(fin);
    15911606            } else {
    1592                 strcpy(bkpinfo->media_device, "/dev/osst0");
    1593             }
    1594         }
    1595         if (bkpinfo->media_device[0]) {
     1607                paranoid_alloc(bkpinfo->media_device,"/dev/osst0");
     1608            }
     1609        }
     1610        if (bkpinfo->media_device != NULL) {
    15961611            if ((fin = fopen(bkpinfo->media_device, "r"))) {
    15971612                paranoid_fclose(fin);
     
    16061621                    bkpinfo->media_device);
    16071622            if (!ask_me_yes_or_no(tmp)) {
    1608                 bkpinfo->media_device[0] = '\0';
     1623                paranoid_free(bkpinfo->media_device);
    16091624            }
    16101625            paranoid_free(tmp);
    16111626        }
    1612         if (!bkpinfo->media_device[0]) {
     1627        if (bkpinfo->media_device == NULL) {
    16131628            if (!popup_and_get_string
    16141629                ("Device name?",
     
    19071922#ifndef __FreeBSD__
    19081923        if (!ask_me_yes_or_no
    1909             ("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please."))
    1910 #endif
    1911         {
    1912             strcpy(bkpinfo->kernel_path, "FAILSAFE");
    1913         }
     1924            ("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please.")) {
     1925            paranoid_alloc(bkpinfo->kernel_path, "FAILSAFE");
     1926        }
     1927#endif
    19141928
    19151929        if (!ask_me_yes_or_no
     
    20682082 * If @p dev cannot be read, set @p output to "".
    20692083 * @param dev The device to check for.
    2070  * @param output Set to @p dev if @p dev exists, "" otherwise.
     2084 * @param output Set to @p dev if @p dev exists, NULL otherwise. Needs to be freed by caller
    20712085 * @return TRUE if @p dev exists, FALSE if it doesn't.
    20722086 */
     
    20752089    char *command;
    20762090
     2091    paranoid_free(output);
    20772092    if (!dev || dev[0] == '\0') {
    2078         output[0] = '\0';
    20792093        return (FALSE);
    20802094    }
     
    20892103    if (!run_program_and_log_output(command, FALSE)
    20902104        && !run_program_and_log_output(command, FALSE)) {
    2091         strcpy(output, dev);
     2105        asprintf(&output, dev);
    20922106        log_msg(4, "Found it - %s", dev);
    20932107        paranoid_free(command);
    20942108        return (TRUE);
    20952109    } else {
    2096         output[0] = '\0';
    20972110        log_msg(4, "It's not %s", dev);
    20982111        paranoid_free(command);
     
    21282141    }
    21292142
    2130     asprintf(&mountdev, bkpinfo->media_device);
    2131     if (!mountdev[0]) {
     2143    if (bkpinfo->media_device == NULL) {
    21322144        log_it
    21332145            ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
    2134         find_cdrom_device(bkpinfo->media_device, FALSE);
     2146        bkpinfo->media_device = find_cdrom_device(FALSE);
    21352147    }
    21362148    if (!is_this_device_mounted(MNT_CDROM)) {
    2137         mount_CDROM_here(mountdev, MNT_CDROM);
     2149        (void)mount_CDROM_here(bkpinfo->media_device, MNT_CDROM);
    21382150    }
    21392151    cd_number =
    21402152        atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
    2141     paranoid_free(mountdev);
    21422153    return (cd_number);
    21432154}
     
    22432254    int count_lilos = 0;
    22442255    int count_grubs = 0;
    2245     int n = 0;
     2256    size_t n = 0;
    22462257
    22472258    /*@ end vars *************************************************** */
Note: See TracChangeset for help on using the changeset viewer.