Changeset 171 in MondoRescue for trunk/mondo/mondo/common/libmondo-devices.c
- Timestamp:
- Dec 8, 2005, 5:20:29 PM (18 years ago)
- 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 */ 5 4 /** 6 5 * @file … … 43 42 extern char *g_selfmounted_isodir; 44 43 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] = "";44 static char *g_cdrw_drive_is_here = NULL; 45 static char *g_cdrom_drive_is_here = NULL; 46 static char *g_dvd_drive_is_here = NULL; 48 47 49 48 … … 61 60 void set_g_cdrom_and_g_dvd_to_bkpinfo_value(struct s_bkpinfo *bkpinfo) 62 61 { 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 } 65 70 } 66 71 … … 152 157 * @ingroup stringGroup 153 158 */ 154 staticchar *bkptype_to_string(t_bkptype bt)155 { 156 staticchar *output = NULL;159 char *bkptype_to_string(t_bkptype bt) 160 { 161 char *output = NULL; 157 162 158 163 paranoid_free(output); … … 319 324 /*@ ints ******************************************************* */ 320 325 int res = 0; 321 int n = 0;326 size_t n = 0; 322 327 323 328 /*@ pointers *************************************************** */ … … 432 437 * @see mount_CDROM_here 433 438 */ 434 intfind_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint)439 bool find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint) 435 440 { 436 441 /*@ buffers ***************************************************** */ 437 442 438 443 /*@ int's ****************************************************** */ 439 int res;440 char *dev ;444 bool res = TRUE; 445 char *dev = NULL; 441 446 442 447 /*@ end vars **************************************************** */ 443 448 444 malloc_string(dev);445 449 assert(bkpinfo != NULL); 446 450 assert_string_is_neither_NULL_nor_zerolength(mountpoint); 447 451 448 452 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); 457 463 } 458 464 } … … 462 468 } 463 469 464 if ( !dev[0] || (res =mount_CDROM_here(dev, mountpoint))) {470 if ((dev == NULL) || (! mount_CDROM_here(dev, mountpoint))) { 465 471 if (!popup_and_get_string 466 472 ("CD-ROM device", "Please enter your CD-ROM's /dev device", 467 473 dev, MAX_STR_LEN / 4)) { 468 res = 1;474 res = FALSE; 469 475 } else { 470 476 res = mount_CDROM_here(dev, mountpoint); … … 477 483 } 478 484 paranoid_free(dev); 479 return 485 return(res); 480 486 } 481 487 … … 483 489 /** 484 490 * 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 */ 494 char *find_cdrw_device(void) 489 495 { 490 496 /*@ buffers ************************ */ … … 493 499 char *cdr_exe; 494 500 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); 498 505 log_msg(3, "Been there, done that. Returning %s", cdrw_device); 499 return (0);506 return(cdrw_device); 500 507 } 501 508 if (g_backup_media_type == dvd) { 502 509 log_msg(1, 503 510 "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?"); 504 return (1);511 return(NULL); 505 512 } 506 513 run_program_and_log_output("insmod ide-scsi", -1); … … 517 524 paranoid_free(command); 518 525 } else { 519 asprintf(&tmp, " ");526 asprintf(&tmp, " "); 520 527 } 521 528 paranoid_free(cdr_exe); … … 523 530 if (strlen(tmp) < 2) { 524 531 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; 529 535 530 536 asprintf(&comment, "Found CDRW device - %s", cdrw_device); … … 532 538 paranoid_free(comment); 533 539 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); 536 543 } 537 544 } … … 542 549 * Several different methods may be used to find the device, including 543 550 * 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. 545 552 * @param try_to_mount Whether to mount the CD as part of the test; if mount 546 553 * 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 */ 556 char *find_cdrom_device(bool try_to_mount) 550 557 { 551 558 /*@ pointers **************************************************** */ … … 554 561 char *q; 555 562 char *r; 556 int n = 0;557 int retval= 0;563 char *output = NULL; 564 size_t n = 0; 558 565 559 566 /*@ bool's ****************************************************** */ … … 571 578 static char the_last_place_i_found_it[MAX_STR_LEN] = ""; 572 579 573 /*@ intialize *************************************************** */574 575 output[0] = '\0';576 577 580 /*@ end vars **************************************************** */ 578 581 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); 581 584 log_msg(3, "Been there, done that. Returning %s", output); 582 return( 0);585 return(output); 583 586 } 584 587 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); 586 589 log_msg(3, 587 590 "find_cdrom_device() --- returning last found location - '%s'", 588 591 output); 589 return( 0);592 return(output); 590 593 } 591 594 … … 596 599 } 597 600 if (!find_home_of_exe(cdr_exe)) { 598 strcpy(output, "/dev/cdrom");601 asprintf(&output, "/dev/cdrom"); 599 602 log_msg(4, "Can't find cdrecord; assuming %s", output); 600 603 if (!does_device_exist(output)) { 601 604 log_msg(4, "That didn't work. Sorry."); 602 605 paranoid_free(cdr_exe); 603 return(1); 606 paranoid_free(output); 607 return(NULL); 604 608 } else { 605 609 paranoid_free(cdr_exe); 606 return( 0);610 return(output); 607 611 } 608 612 } … … 615 619 paranoid_free(cdr_exe); 616 620 paranoid_free(command); 617 return ( 1);621 return (NULL); 618 622 } 619 623 paranoid_free(command); … … 672 676 tmp); 673 677 } else { 674 sprintf(output, "/dev/%s", tmp);678 asprintf(&output, "/dev/%s", tmp); 675 679 found_it = TRUE; 676 680 } … … 713 717 { 714 718 paranoid_free(cdr_exe); 715 return( 1);719 return(NULL); 716 720 } 717 721 } … … 727 731 log_msg(4, "Well, I'll use the DVD - %s - as a last resort", 728 732 dvd_last_resort); 729 strcpy(output, dvd_last_resort); 733 paranoid_free(output); 734 asprintf(&output, dvd_last_resort); 730 735 found_it = TRUE; 731 736 } … … 741 746 output); 742 747 found_it = FALSE; 743 output[0] = '\0';748 paranoid_free(output); 744 749 } 745 750 paranoid_free(tmp); … … 749 754 log_msg(4, "(find_cdrom_device) --> '%s'", output); 750 755 if (!does_device_exist(output)) { 756 log_msg(4, "OK, I was wrong, I haven't found it... yet."); 751 757 found_it = FALSE; 752 log_msg(4, "OK, I was wrong, I haven't found it... yet.");758 paranoid_free(output); 753 759 } 754 760 } … … 784 790 { 785 791 paranoid_free(cdr_exe); 786 return( 1);792 return(NULL); 787 793 } 788 794 } … … 800 806 801 807 if (found_it && try_to_mount) { 802 if ( mount_CDROM_here(output, mountpoint)) {808 if (! mount_CDROM_here(output, mountpoint)) { 803 809 log_msg(4, "[Cardigans] I've changed my mind"); 804 810 found_it = FALSE; 811 paranoid_free(output); 805 812 } else { 806 813 asprintf(&tmp, "%s/archives", mountpoint); … … 808 815 log_msg(4, "[Cardigans] I'll take it back"); 809 816 found_it = FALSE; 817 paranoid_free(output); 810 818 } else { 811 819 asprintf(&command, "umount %s", output); … … 823 831 if (!does_file_exist(output)) { 824 832 log_msg(3, "I still haven't found it."); 825 return (1); 833 paranoid_free(output); 834 return(NULL); 826 835 } 827 836 log_msg(3, "(find_cdrom_device) --> '%s'", output); 828 837 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); 831 841 } 832 842 … … 834 844 "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", 835 845 cdr_exe, g_cdrw_drive_is_here); 846 paranoid_free(cdr_exe); 847 836 848 log_msg(1, "command=%s", command); 837 849 asprintf(&tmp, call_program_and_get_last_line_of_output(command)); … … 839 851 840 852 if (tmp[0]) { 841 strcpy(output, tmp);853 output = tmp; 842 854 log_msg(4, "Finally found it at %s", output); 843 retval = 0; 844 } else { 855 } else { 856 paranoid_free(tmp); 857 paranoid_free(output); 845 858 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 864 char *find_dvd_device() 855 865 { 856 866 char *tmp; 857 867 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); 861 872 log_msg(3, "Been there, done that. Returning %s", output); 862 return ( 0);873 return (output); 863 874 } 864 875 … … 880 891 if (devno >= 0) { 881 892 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); 884 896 log_msg(2, "I think DVD is at %s", output); 885 897 } else { 886 898 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); 894 902 } 895 903 … … 1064 1072 char *device_with_space; 1065 1073 char *tmp; 1066 int n = 0;1074 size_t n = 0; 1067 1075 1068 1076 #ifdef __FreeBSD__ … … 1212 1220 * @param device The device (or file if g_ISO_restore_mode) to mount. 1213 1221 * @param mountpoint The place to mount it. 1214 * @return 0 for success, nonzerofor failure.1215 */ 1216 intmount_CDROM_here(char *device, char *mountpoint)1222 * @return TRUE for success, FALSE for failure. 1223 */ 1224 bool mount_CDROM_here(char *device, char *mountpoint) 1217 1225 { 1218 1226 /*@ buffer ****************************************************** */ … … 1225 1233 make_hole_for_dir(mountpoint); 1226 1234 if (isdigit(device[0])) { 1227 find_cdrom_device(device, FALSE); 1235 paranoid_free(device); 1236 device = find_cdrom_device(FALSE); 1228 1237 } 1229 1238 if (g_ISO_restore_mode) { … … 1238 1247 fatal_error(command); 1239 1248 } 1240 strcpy(device, dev);1241 paranoid_free(dev);1249 paranoid_free(device); 1250 device = dev; 1242 1251 #endif 1243 1252 } … … 1263 1272 paranoid_free(command); 1264 1273 1265 return (retval); 1274 if (retval == 0) { 1275 return(TRUE); 1276 } else { 1277 return(FALSE); 1278 } 1266 1279 } 1267 1280 … … 1331 1344 } 1332 1345 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)) { 1334 1347 fatal_error("Mommy!"); 1335 1348 } … … 1466 1479 media_descriptor_string(bkpinfo->backup_media_type)); 1467 1480 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(); 1469 1483 asprintf(&tmp, "1"); 1470 1484 asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB 1471 1485 log_msg(1, "Setting to DVD defaults"); 1472 1486 } else { 1473 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);1487 paranoid_alloc(bkpinfo->media_device,VANILLA_SCSI_CDROM ); 1474 1488 asprintf(&tmp, "4"); 1475 1489 asprintf(&sz_size, "650"); … … 1508 1522 case cdstream: 1509 1523 if (bkpinfo->disaster_recovery) { 1510 strcpy(bkpinfo->media_device, "/dev/cdrom");1524 paranoid_alloc(bkpinfo->media_device, "/dev/cdrom"); 1511 1525 log_msg(2, "CD-ROM device assumed to be at %s", 1512 1526 bkpinfo->media_device); 1513 1527 } else if (bkpinfo->restore_data 1514 1528 || 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"); 1517 1531 } // just for the heck of it :) 1518 1532 log_msg(1, "bkpinfo->media_device = %s", 1519 1533 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; 1522 1538 log_msg(1, "bkpinfo->media_device = %s", 1523 1539 bkpinfo->media_device); … … 1538 1554 bkpinfo->media_device); 1539 1555 } 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) { 1544 1559 asprintf(&tmp, 1545 1560 "I think I've found your %s burner at SCSI node %s; am I right on the money?", … … 1548 1563 bkpinfo->media_device); 1549 1564 if (!ask_me_yes_or_no(tmp)) { 1550 bkpinfo->media_device[0] = '\0';1565 paranoid_free(bkpinfo->media_device); 1551 1566 } 1552 1567 paranoid_free(tmp); 1553 } 1554 if (!bkpinfo->media_device[0]) { 1568 } else { 1555 1569 if (g_kernel_version < 2.6) { 1556 1570 i = popup_and_get_string("Device node?", … … 1584 1598 case tape: 1585 1599 1600 paranoid_free(bkpinfo->media_device); 1586 1601 if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) { 1587 1602 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 ); 1589 1604 if ((fin = fopen(bkpinfo->media_device, "r"))) { 1590 1605 paranoid_fclose(fin); 1591 1606 } 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) { 1596 1611 if ((fin = fopen(bkpinfo->media_device, "r"))) { 1597 1612 paranoid_fclose(fin); … … 1606 1621 bkpinfo->media_device); 1607 1622 if (!ask_me_yes_or_no(tmp)) { 1608 bkpinfo->media_device[0] = '\0';1623 paranoid_free(bkpinfo->media_device); 1609 1624 } 1610 1625 paranoid_free(tmp); 1611 1626 } 1612 if ( !bkpinfo->media_device[0]) {1627 if (bkpinfo->media_device == NULL) { 1613 1628 if (!popup_and_get_string 1614 1629 ("Device name?", … … 1907 1922 #ifndef __FreeBSD__ 1908 1923 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 1914 1928 1915 1929 if (!ask_me_yes_or_no … … 2068 2082 * If @p dev cannot be read, set @p output to "". 2069 2083 * @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 2071 2085 * @return TRUE if @p dev exists, FALSE if it doesn't. 2072 2086 */ … … 2075 2089 char *command; 2076 2090 2091 paranoid_free(output); 2077 2092 if (!dev || dev[0] == '\0') { 2078 output[0] = '\0';2079 2093 return (FALSE); 2080 2094 } … … 2089 2103 if (!run_program_and_log_output(command, FALSE) 2090 2104 && !run_program_and_log_output(command, FALSE)) { 2091 strcpy(output, dev);2105 asprintf(&output, dev); 2092 2106 log_msg(4, "Found it - %s", dev); 2093 2107 paranoid_free(command); 2094 2108 return (TRUE); 2095 2109 } else { 2096 output[0] = '\0';2097 2110 log_msg(4, "It's not %s", dev); 2098 2111 paranoid_free(command); … … 2128 2141 } 2129 2142 2130 asprintf(&mountdev, bkpinfo->media_device); 2131 if (!mountdev[0]) { 2143 if (bkpinfo->media_device == NULL) { 2132 2144 log_it 2133 2145 ("(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); 2135 2147 } 2136 2148 if (!is_this_device_mounted(MNT_CDROM)) { 2137 mount_CDROM_here(mountdev, MNT_CDROM);2149 (void)mount_CDROM_here(bkpinfo->media_device, MNT_CDROM); 2138 2150 } 2139 2151 cd_number = 2140 2152 atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER")); 2141 paranoid_free(mountdev);2142 2153 return (cd_number); 2143 2154 } … … 2243 2254 int count_lilos = 0; 2244 2255 int count_grubs = 0; 2245 int n = 0;2256 size_t n = 0; 2246 2257 2247 2258 /*@ end vars *************************************************** */
Note:
See TracChangeset
for help on using the changeset viewer.