Changeset 296 in MondoRescue for branches/2.06/mondo/mondo/common/libmondo-fork.c
- Timestamp:
- Jan 11, 2006, 10:10:31 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.06/mondo/mondo/common/libmondo-fork.c
r154 r296 4 4 - subroutines for handling forking/pthreads/etc. 5 5 6 7 01/20/2006 8 - replaced partimagehack with ntfsclone 6 9 7 10 06/20/2004 … … 493 496 494 497 495 496 /**497 * Thread callback to run a command (partimage) in the background.498 * @param xfb A transfer block of @c char, containing:499 * - xfb:[0] A marker, should be set to 2. Decremented to 1 while the command is running and 0 when it's finished.500 * - xfb:[1] The command's return value, if xfb:[0] is 0.501 * - xfb+2: A <tt>NULL</tt>-terminated string containing the command to be run.502 * @return NULL to pthread_join.503 */504 void *call_partimage_in_bkgd(void *xfb)505 {506 char *transfer_block;507 int retval = 0;508 509 g_buffer_pid = getpid();510 unlink("/tmp/null");511 log_msg(1, "starting");512 transfer_block = (char *) xfb;513 transfer_block[0]--; // should now be 1514 retval = system(transfer_block + 2);515 if (retval) {516 log_OS_error("partimage returned an error");517 }518 transfer_block[1] = retval;519 transfer_block[0]--; // should now be 0520 g_buffer_pid = 0;521 log_msg(1, "returning");522 pthread_exit(NULL);523 }524 525 526 /**527 * File to touch if we want partimage to wait for us.528 */529 #define PAUSE_PARTIMAGE_FNAME "/tmp/PAUSE-PARTIMAGE-FOR-MONDO"530 531 498 /** 532 499 * Apparently unused. @bug This has a purpose, but what? … … 535 502 #define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka" 536 503 537 /** 538 * Marker to start the next subvolume for Partimage. 539 */ 540 #define NEXT_SUBVOL_PLEASE "I-grew-up-on-the-crime-side,-the-New-York-Times-side,-where-staying-alive-was-no-jive" 541 542 /** 543 * Marker to end the partimage file. 544 */ 545 #define NO_MORE_SUBVOLS "On-second-hand,momma-bounced-on-old-man,-and-so-we-moved-to-Shaolin-Land." 504 505 546 506 547 507 int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction) … … 674 634 675 635 636 637 676 638 /** 677 * Call partimage from @p input_device to @p output_fname. 678 * @param input_device The device to read. 679 * @param output_fname The file to write. 680 * @return 0 for success, nonzero for failure. 681 */ 682 int dynamically_create_pipes_and_copy_from_them_to_output_file(char 683 *input_device, 684 char 685 *output_fname) 686 { 687 char *curr_fifo; 688 char *prev_fifo; 689 char *next_fifo; 690 char *command; 691 char *sz_call_to_partimage; 692 int fifo_number = 0; 693 struct stat buf; 694 pthread_t partimage_thread; 695 int res = 0; 696 char *tmpstub; 697 FILE *fout; 698 FILE *fin; 699 char *tmp; 700 701 malloc_string(tmpstub); 702 malloc_string(curr_fifo); 703 malloc_string(prev_fifo); 704 malloc_string(next_fifo); 705 malloc_string(command); 706 malloc_string(sz_call_to_partimage); 707 malloc_string(tmp); 708 709 log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt); 710 if (g_tmpfs_mountpt && g_tmpfs_mountpt[0] 711 && does_file_exist(g_tmpfs_mountpt)) { 712 strcpy(tmpstub, g_tmpfs_mountpt); 713 } else { 714 strcpy(tmpstub, "/tmp"); 715 } 716 paranoid_system("rm -f /tmp/*PARTIMAGE*"); 717 sprintf(command, "rm -Rf %s/pih-fifo-*", tmpstub); 718 paranoid_system(command); 719 sprintf(tmpstub + strlen(tmpstub), "/pih-fifo-%ld", 720 (long int) random()); 721 mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though... 722 sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); 723 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); 724 mkfifo(curr_fifo, S_IRWXU | S_IRWXG); 725 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage 726 sz_call_to_partimage[0] = 2; 727 sz_call_to_partimage[1] = 0; 728 sprintf(sz_call_to_partimage + 2, 729 "partimagehack " PARTIMAGE_PARAMS 730 " save %s %s > /tmp/stdout 2> /tmp/stderr", input_device, 731 tmpstub); 732 log_msg(5, "curr_fifo = %s", curr_fifo); 733 log_msg(5, "next_fifo = %s", next_fifo); 734 log_msg(5, "sz_call_to_partimage call is '%s'", 735 sz_call_to_partimage + 2); 736 if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) { 737 log_msg(5, "Deleting %s", output_fname); 738 unlink(output_fname); 739 } 740 if (!(fout = fopen(output_fname, "w"))) { 741 fatal_error("Unable to openout to output_fname"); 742 } 743 res = 744 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, 745 (void *) sz_call_to_partimage); 746 if (res) { 747 fatal_error("Failed to create thread to call partimage"); 748 } 749 log_msg(1, "Running fore/back at same time"); 750 log_to_screen("Working with partimagehack..."); 751 while (sz_call_to_partimage[0] > 0) { 752 sprintf(tmp, "%s\n", NEXT_SUBVOL_PLEASE); 753 if (fwrite(tmp, 1, 128, fout) != 128) { 754 fatal_error("Cannot write interim block"); 755 } 756 log_msg(5, "fifo_number=%d", fifo_number); 757 log_msg(4, "Cat'ting %s", curr_fifo); 758 if (!(fin = fopen(curr_fifo, "r"))) { 759 fatal_error("Unable to openin from fifo"); 760 } 761 // if (!sz_call_to_partimage[0]) { break; } 762 log_msg(5, "Deleting %s", prev_fifo); 763 unlink(prev_fifo); // just in case 764 copy_from_src_to_dest(fin, fout, 'w'); 765 paranoid_fclose(fin); 766 fifo_number++; 767 strcpy(prev_fifo, curr_fifo); 768 strcpy(curr_fifo, next_fifo); 769 log_msg(5, "Creating %s", next_fifo); 770 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); 771 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo exists before we cat this one 772 system("sync"); 773 sleep(5); 774 } 775 sprintf(tmp, "%s\n", NO_MORE_SUBVOLS); 776 if (fwrite(tmp, 1, 128, fout) != 128) { 777 fatal_error("Cannot write interim block"); 778 } 779 if (fwrite(tmp, 1, 128, fout) != 128) { 780 fatal_error("Cannot write interim block"); 781 } 782 if (fwrite(tmp, 1, 128, fout) != 128) { 783 fatal_error("Cannot write interim block"); 784 } 785 if (fwrite(tmp, 1, 128, fout) != 128) { 786 fatal_error("Cannot write interim block"); 787 } 788 paranoid_fclose(fout); 789 log_to_screen("Cleaning up after partimagehack..."); 790 log_msg(3, "Final fifo_number=%d", fifo_number); 791 paranoid_system("sync"); 792 unlink(next_fifo); 793 unlink(curr_fifo); 794 unlink(prev_fifo); 795 log_to_screen("Finished cleaning up."); 796 797 // if (!lstat(sz_wait_for_this_file, &statbuf)) 798 // { log_msg(3, "WARNING! %s was not processed.", sz_wait_for_this_file); } 799 log_msg(2, "Waiting for pthread_join() to join."); 800 pthread_join(partimage_thread, NULL); 801 res = sz_call_to_partimage[1]; 802 log_msg(2, "pthread_join() joined OK."); 803 log_msg(1, "Partimagehack(save) returned %d", res); 804 unlink(tmpstub); 805 paranoid_free(curr_fifo); 806 paranoid_free(prev_fifo); 807 paranoid_free(next_fifo); 808 paranoid_free(tmpstub); 809 paranoid_free(tmp); 810 paranoid_free(command); 811 return (res); 812 } 813 814 815 /** 816 * Feed @p input_device through partimage to @p output_fname. 639 * Feed @p input_device through ntfsclone to @p output_fname. 817 640 * @param input_device The device to image. 818 641 * @param output_fname The file to write. 819 642 * @return 0 for success, nonzero for failure. 820 643 */ 821 int feed_into_ partimage(char *input_device, char *output_fname)644 int feed_into_ntfsprog(char *input_device, char *output_fname) 822 645 { 823 646 // BACKUP 824 int res; 647 int res = -1; 648 char*command; 825 649 826 650 if (!does_file_exist(input_device)) { 827 651 fatal_error("input device does not exist"); 828 652 } 829 if (!find_home_of_exe("partimagehack")) { 830 fatal_error("partimagehack not found"); 831 } 832 res = 833 dynamically_create_pipes_and_copy_from_them_to_output_file 834 (input_device, output_fname); 653 if ( !find_home_of_exe("ntfsclone")) { 654 fatal_error("ntfsclone not found"); 655 } 656 malloc_string(command); 657 sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device); 658 res = run_program_and_log_output(command, 5); 659 paranoid_free(command); 835 660 return (res); 836 661 } … … 1000 825 1001 826 1002 #define PIH_LOG "/var/log/partimage-debug.log"1003 1004 827 1005 828 /** 1006 * Feed @p input_fifo through partimage (restore) to @p output_device.1007 * @param input_fifo The partimage file to read.829 * Feed @p input_fifo through ntfsclone (restore) to @p output_device. 830 * @param input_fifo The ntfsclone file to read. 1008 831 * @param output_device Where to put the output. 1009 * @return The return value of partimagehack (0 for success). 1010 * @bug Probably unnecessary, as the partimage is just a sparse file. We could use @c dd to restore it. 832 * @return The return value of ntfsclone (0 for success). 1011 833 */ 1012 int feed_outfrom_ partimage(char *output_device, char *input_fifo)834 int feed_outfrom_ntfsprog(char *output_device, char *input_fifo) 1013 835 { 1014 836 // RESTORE 1015 char *tmp; 1016 // char *command; 1017 char *stuff; 1018 char *sz_call_to_partimage; 1019 pthread_t partimage_thread; 1020 int res; 1021 char *curr_fifo; 1022 char *prev_fifo; 1023 char *oldest_fifo; 1024 char *next_fifo; 1025 char *afternxt_fifo; 1026 int fifo_number = 0; 1027 char *tmpstub; 1028 FILE *fin; 1029 FILE *fout; 1030 1031 malloc_string(curr_fifo); 1032 malloc_string(prev_fifo); 1033 malloc_string(next_fifo); 1034 malloc_string(afternxt_fifo); 1035 malloc_string(oldest_fifo); 1036 malloc_string(tmp); 1037 sz_call_to_partimage = malloc(1000); 1038 malloc_string(stuff); 1039 malloc_string(tmpstub); 1040 1041 log_msg(1, "output_device=%s", output_device); 1042 log_msg(1, "input_fifo=%s", input_fifo); 1043 /* I don't trust g_tmpfs_mountpt 1044 if (g_tmpfs_mountpt[0]) 1045 { 1046 strcpy(tmpstub, g_tmpfs_mountpt); 1047 } 1048 else 1049 { 1050 */ 1051 strcpy(tmpstub, "/tmp"); 1052 // } 1053 log_msg(1, "tmpstub was %s", tmpstub); 1054 strcpy(stuff, tmpstub); 1055 sprintf(tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random()); 1056 log_msg(1, "tmpstub is now %s", tmpstub); 1057 unlink("/tmp/PARTIMAGEHACK-POSITION"); 1058 unlink(PAUSE_PARTIMAGE_FNAME); 1059 paranoid_system("rm -f /tmp/*PARTIMAGE*"); 1060 sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); 1061 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); 1062 sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2); 1063 mkfifo(PIH_LOG, S_IRWXU | S_IRWXG); 1064 mkfifo(curr_fifo, S_IRWXU | S_IRWXG); 1065 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage 1066 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); 1067 system("cat " PIH_LOG " > /dev/null &"); 1068 log_msg(3, "curr_fifo = %s", curr_fifo); 1069 log_msg(3, "next_fifo = %s", next_fifo); 1070 if (!does_file_exist(input_fifo)) { 1071 fatal_error("input fifo does not exist"); 1072 } 1073 if (!(fin = fopen(input_fifo, "r"))) { 1074 fatal_error("Unable to openin from input_fifo"); 1075 } 1076 if (!find_home_of_exe("partimagehack")) { 1077 fatal_error("partimagehack not found"); 1078 } 1079 sz_call_to_partimage[0] = 2; 1080 sz_call_to_partimage[1] = 0; 1081 sprintf(sz_call_to_partimage + 2, 1082 "partimagehack " PARTIMAGE_PARAMS 1083 " restore %s %s > /dev/null 2>> %s", output_device, curr_fifo, 1084 MONDO_LOGFILE); 1085 log_msg(1, "output_device = %s", output_device); 1086 log_msg(1, "curr_fifo = %s", curr_fifo); 1087 log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage + 2); 1088 res = 1089 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, 1090 (void *) sz_call_to_partimage); 1091 if (res) { 1092 fatal_error("Failed to create thread to call partimage"); 1093 } 1094 log_msg(1, "Running fore/back at same time"); 1095 log_msg(2, " Trying to openin %s", input_fifo); 1096 if (!does_file_exist(input_fifo)) { 1097 log_msg(2, "Warning - %s does not exist", input_fifo); 1098 } 1099 while (!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) { 1100 log_msg(6, "Waiting for partimagehack (restore) to start"); 1101 sleep(1); 1102 } 1103 while (sz_call_to_partimage[0] > 0) { 1104 if (fread(tmp, 1, 128, fin) != 128) { 1105 fatal_error("Cannot read introductory block"); 1106 } 1107 if (strstr(tmp, NEXT_SUBVOL_PLEASE)) { 1108 log_msg(2, "Great. Next subvol coming up."); 1109 } else if (strstr(tmp, NO_MORE_SUBVOLS)) { 1110 log_msg(2, "Great. That was the last subvol."); 1111 break; 1112 } else { 1113 log_msg(2, "WTF is this? '%s'", tmp); 1114 fatal_error("Unknown interim block"); 1115 } 1116 if (feof(fin)) { 1117 log_msg(1, "Eof(fin) detected. Breaking."); 1118 break; 1119 } 1120 log_msg(3, "Processing subvol %d", fifo_number); 1121 log_msg(5, "fifo_number=%d", fifo_number); 1122 if (!(fout = fopen(curr_fifo, "w"))) { 1123 fatal_error("Cannot openout to curr_fifo"); 1124 } 1125 log_msg(6, "Deleting %s", oldest_fifo); 1126 copy_from_src_to_dest(fout, fin, 'r'); 1127 paranoid_fclose(fout); 1128 fifo_number++; 1129 unlink(oldest_fifo); // just in case 1130 strcpy(oldest_fifo, prev_fifo); 1131 strcpy(prev_fifo, curr_fifo); 1132 strcpy(curr_fifo, next_fifo); 1133 strcpy(next_fifo, afternxt_fifo); 1134 sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2); 1135 log_msg(6, "Creating %s", afternxt_fifo); 1136 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we access current fifo 1137 fflush(fin); 1138 // system("sync"); 1139 usleep(1000L * 100L); 1140 } 1141 paranoid_fclose(fin); 1142 paranoid_system("sync"); 1143 log_msg(1, "Partimagehack has finished. Great. Fin-closing."); 1144 log_msg(1, "Waiting for pthread_join"); 1145 pthread_join(partimage_thread, NULL); 1146 res = sz_call_to_partimage[1]; 1147 log_msg(1, "Yay. Partimagehack (restore) returned %d", res); 1148 unlink(prev_fifo); 1149 unlink(curr_fifo); 1150 unlink(next_fifo); 1151 unlink(afternxt_fifo); 1152 unlink(PIH_LOG); 1153 paranoid_free(tmp); 1154 paranoid_free(sz_call_to_partimage); 1155 paranoid_free(stuff); 1156 paranoid_free(prev_fifo); 1157 paranoid_free(curr_fifo); 1158 paranoid_free(next_fifo); 1159 paranoid_free(afternxt_fifo); 1160 paranoid_free(oldest_fifo); 1161 paranoid_free(tmpstub); 837 int res = -1; 838 char *command; 839 840 if ( !find_home_of_exe("ntfsclone")) { 841 fatal_error("ntfsclone not found"); 842 } 843 malloc_string(command); 844 sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo); 845 res = run_program_and_log_output(command, 5); 846 paranoid_free(command); 1162 847 return (res); 1163 848 }
Note:
See TracChangeset
for help on using the changeset viewer.