Changeset 59 in MondoRescue for trunk/mondo/mondo/common/libmondo-fork.c
- Timestamp:
- Oct 11, 2005, 1:34:31 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/mondo/mondo/common/libmondo-fork.c
r30 r59 1 1 /* libmondo-fork.c 2 $Id : libmondo-fork.c,v 1.3 2004/06/21 20:20:36 hugo Exp$2 $Id$ 3 3 4 4 - subroutines for handling forking/pthreads/etc. … … 109 109 110 110 /*@unused@*/ 111 //static char cvsid[] = "$Id : libmondo-fork.c,v 1.3 2004/06/21 20:20:36 hugo Exp$";111 //static char cvsid[] = "$Id$"; 112 112 113 113 extern char *g_tmpfs_mountpt; 114 114 extern t_bkptype g_backup_media_type; 115 115 extern bool g_text_mode; 116 pid_t g_buffer_pid =0;116 pid_t g_buffer_pid = 0; 117 117 118 118 … … 123 123 * @note The returned value points to static storage that will be overwritten with each call. 124 124 */ 125 char * 126 call_program_and_get_last_line_of_output (char *call) 125 char *call_program_and_get_last_line_of_output(char *call) 127 126 { 128 /*@ buffers ******************************************************/ 129 static char result[512]; 130 char *tmp; 131 132 /*@ pointers *****************************************************/ 133 FILE *fin; 134 135 /*@ initialize data **********************************************/ 136 malloc_string(tmp); 137 result[0] = '\0'; 138 tmp[0] = '\0'; 139 140 /*@*********************************************************************/ 141 142 assert_string_is_neither_NULL_nor_zerolength(call); 143 if ((fin = popen (call, "r"))) 144 { 145 for (fgets (tmp, MAX_STR_LEN, fin); !feof (fin); 146 fgets (tmp, MAX_STR_LEN, fin)) 147 { 148 if (strlen (tmp) > 1) 149 { 150 strcpy (result, tmp); 151 } 152 } 153 paranoid_pclose (fin); 154 } 155 else 156 { 157 log_OS_error("Unable to popen call"); 158 } 159 strip_spaces (result); 160 return (result); 127 /*@ buffers ***************************************************** */ 128 static char result[512]; 129 char *tmp; 130 131 /*@ pointers **************************************************** */ 132 FILE *fin; 133 134 /*@ initialize data ********************************************* */ 135 malloc_string(tmp); 136 result[0] = '\0'; 137 tmp[0] = '\0'; 138 139 /*@******************************************************************** */ 140 141 assert_string_is_neither_NULL_nor_zerolength(call); 142 if ((fin = popen(call, "r"))) { 143 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin); 144 fgets(tmp, MAX_STR_LEN, fin)) { 145 if (strlen(tmp) > 1) { 146 strcpy(result, tmp); 147 } 148 } 149 paranoid_pclose(fin); 150 } else { 151 log_OS_error("Unable to popen call"); 152 } 153 strip_spaces(result); 154 return (result); 161 155 } 162 156 … … 186 180 */ 187 181 int 188 eval_call_to_make_ISO 189 190 182 eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo, 183 char *basic_call, char *isofile, 184 int cd_no, char *logstub, char *what_i_am_doing) 191 185 { 192 186 193 /*@ int's ****/ 194 int retval = 0; 195 196 197 /*@ buffers ****/ 198 char *midway_call, *ultimate_call, *tmp, *command, *incoming, *old_stderr, *cd_number_str; 199 char *p; 187 /*@ int's *** */ 188 int retval = 0; 189 190 191 /*@ buffers *** */ 192 char *midway_call, *ultimate_call, *tmp, *command, *incoming, 193 *old_stderr, *cd_number_str; 194 char *p; 200 195 201 196 /*@*********** End Variables ***************************************/ 202 197 203 log_msg(3, "Starting"); 204 assert(bkpinfo!=NULL); 205 assert_string_is_neither_NULL_nor_zerolength(basic_call); 206 assert_string_is_neither_NULL_nor_zerolength(isofile); 207 assert_string_is_neither_NULL_nor_zerolength(logstub); 208 if (!(midway_call = malloc(1200))) { fatal_error("Cannot malloc midway_call"); } 209 if (!(ultimate_call = malloc(1200))) { fatal_error("Cannot malloc ultimate_call"); } 210 if (!(tmp = malloc(1200))) { fatal_error("Cannot malloc tmp"); } 211 if (!(command = malloc(1200))) { fatal_error("Cannot malloc command"); } 212 malloc_string(incoming); 213 malloc_string(old_stderr); 214 malloc_string(cd_number_str); 215 216 incoming[0]='\0'; 217 old_stderr[0] = '\0'; 218 219 sprintf (cd_number_str, "%d", cd_no); 220 resolve_naff_tokens (midway_call, basic_call, isofile, "_ISO_"); 221 resolve_naff_tokens (tmp, midway_call, cd_number_str, "_CD#_"); 222 resolve_naff_tokens (ultimate_call, tmp, MONDO_LOGFILE, "_ERR_"); 223 log_msg (4, "basic call = '%s'", basic_call); 224 log_msg (4, "midway_call = '%s'", midway_call); 225 log_msg (4, "tmp = '%s'", tmp); 226 log_msg (4, "ultimate call = '%s'", ultimate_call); 227 sprintf( command, "%s >> %s", ultimate_call, MONDO_LOGFILE ); 228 229 log_to_screen("Please be patient. Do not be alarmed by on-screen inactivity."); 230 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'", what_i_am_doing); 231 strcpy (tmp, command); 232 if (bkpinfo->manual_cd_tray) 233 { 234 p = strstr (tmp, "2>>"); 235 if (p) 236 { 237 sprintf (p, " "); 238 while(*p==' ') { p++; } 239 for (; *p != ' '; p++) 240 { 241 *p = ' '; 242 } 243 } 244 strcpy(command, tmp); 198 log_msg(3, "Starting"); 199 assert(bkpinfo != NULL); 200 assert_string_is_neither_NULL_nor_zerolength(basic_call); 201 assert_string_is_neither_NULL_nor_zerolength(isofile); 202 assert_string_is_neither_NULL_nor_zerolength(logstub); 203 if (!(midway_call = malloc(1200))) { 204 fatal_error("Cannot malloc midway_call"); 205 } 206 if (!(ultimate_call = malloc(1200))) { 207 fatal_error("Cannot malloc ultimate_call"); 208 } 209 if (!(tmp = malloc(1200))) { 210 fatal_error("Cannot malloc tmp"); 211 } 212 if (!(command = malloc(1200))) { 213 fatal_error("Cannot malloc command"); 214 } 215 malloc_string(incoming); 216 malloc_string(old_stderr); 217 malloc_string(cd_number_str); 218 219 incoming[0] = '\0'; 220 old_stderr[0] = '\0'; 221 222 sprintf(cd_number_str, "%d", cd_no); 223 resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_"); 224 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_"); 225 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_"); 226 log_msg(4, "basic call = '%s'", basic_call); 227 log_msg(4, "midway_call = '%s'", midway_call); 228 log_msg(4, "tmp = '%s'", tmp); 229 log_msg(4, "ultimate call = '%s'", ultimate_call); 230 sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE); 231 232 log_to_screen 233 ("Please be patient. Do not be alarmed by on-screen inactivity."); 234 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'", 235 what_i_am_doing); 236 strcpy(tmp, command); 237 if (bkpinfo->manual_cd_tray) { 238 p = strstr(tmp, "2>>"); 239 if (p) { 240 sprintf(p, " "); 241 while (*p == ' ') { 242 p++; 243 } 244 for (; *p != ' '; p++) { 245 *p = ' '; 246 } 247 } 248 strcpy(command, tmp); 245 249 #ifndef _XWIN 246 if (!g_text_mode) { newtSuspend(); } 250 if (!g_text_mode) { 251 newtSuspend(); 252 } 247 253 #endif 248 log_msg (1, "command = '%s'", command); 249 retval += system (command); 250 if (!g_text_mode) { newtResume(); } 251 if (retval) 252 { 253 log_msg (2, "Basic call '%s' returned an error.", basic_call); 254 popup_and_OK ("Press ENTER to continue."); 255 popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created"); 256 } 257 } 258 /* if text mode then do the above & RETURN; if not text mode, do this... */ 259 else 260 { 261 log_msg (3, "command = '%s'", command); 254 log_msg(1, "command = '%s'", command); 255 retval += system(command); 256 if (!g_text_mode) { 257 newtResume(); 258 } 259 if (retval) { 260 log_msg(2, "Basic call '%s' returned an error.", basic_call); 261 popup_and_OK("Press ENTER to continue."); 262 popup_and_OK 263 ("mkisofs and/or cdrecord returned an error. CD was not created"); 264 } 265 } 266 /* if text mode then do the above & RETURN; if not text mode, do this... */ 267 else { 268 log_msg(3, "command = '%s'", command); 262 269 // yes_this_is_a_goto: 263 retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command); 264 } 265 266 paranoid_free(midway_call); 267 paranoid_free(ultimate_call); 268 paranoid_free(tmp); 269 paranoid_free(command); 270 paranoid_free(incoming); 271 paranoid_free(old_stderr); 272 paranoid_free(cd_number_str); 270 retval = 271 run_external_binary_with_percentage_indicator_NEW 272 (what_i_am_doing, command); 273 } 274 275 paranoid_free(midway_call); 276 paranoid_free(ultimate_call); 277 paranoid_free(tmp); 278 paranoid_free(command); 279 paranoid_free(incoming); 280 paranoid_free(old_stderr); 281 paranoid_free(cd_number_str); 273 282 /* 274 283 if (bkpinfo->backup_media_type == dvd && !bkpinfo->please_dont_eject_when_restoring) … … 278 287 } 279 288 */ 280 289 return (retval); 281 290 } 282 291 … … 290 299 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success). 291 300 */ 292 int 293 run_program_and_log_output (char *program, int debug_level) 301 int run_program_and_log_output(char *program, int debug_level) 294 302 { 295 /*@ buffer *******************************************************/ 296 char callstr[MAX_STR_LEN*2]; 297 char incoming[MAX_STR_LEN*2]; 298 char tmp[MAX_STR_LEN*2]; 299 char initial_label[MAX_STR_LEN*2]; 300 301 /*@ int **********************************************************/ 302 int res; 303 int i; 304 int len; 305 bool log_if_failure=FALSE; 306 bool log_if_success=FALSE; 307 308 /*@ pointers ****************************************************/ 309 FILE *fin; 310 char *p; 311 312 /*@ end vars ****************************************************/ 313 314 assert(program!=NULL); 315 if (!program[0]) 316 { 317 log_msg(2, "Warning - asked to run zerolength program"); 318 return(1); 319 } 303 /*@ buffer ****************************************************** */ 304 char callstr[MAX_STR_LEN * 2]; 305 char incoming[MAX_STR_LEN * 2]; 306 char tmp[MAX_STR_LEN * 2]; 307 char initial_label[MAX_STR_LEN * 2]; 308 309 /*@ int ********************************************************* */ 310 int res; 311 int i; 312 int len; 313 bool log_if_failure = FALSE; 314 bool log_if_success = FALSE; 315 316 /*@ pointers *************************************************** */ 317 FILE *fin; 318 char *p; 319 320 /*@ end vars *************************************************** */ 321 322 assert(program != NULL); 323 if (!program[0]) { 324 log_msg(2, "Warning - asked to run zerolength program"); 325 return (1); 326 } 320 327 // if (debug_level == TRUE) { debug_level=5; } 321 328 322 // assert_string_is_neither_NULL_nor_zerolength(program); 323 324 if (debug_level <= g_loglevel) { log_if_success = TRUE; log_if_failure = TRUE; } 325 sprintf (callstr, "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err", 326 program); 327 while ((p = strchr (callstr, '\r'))) 328 { 329 *p = ' '; 330 } 331 while ((p = strchr (callstr, '\n'))) 332 { 333 *p = ' '; 334 } /* single '=' is intentional */ 335 336 337 len = (int) strlen (program); 338 for (i = 0; i < 35 - len / 2; i++) 339 { 340 tmp[i] = '-'; 341 } 342 tmp[i] = '\0'; 343 strcat (tmp, " "); 344 strcat (tmp, program); 345 strcat (tmp, " "); 346 for (i = 0; i < 35 - len / 2; i++) 347 { 348 strcat (tmp, "-"); 349 } 350 strcpy(initial_label, tmp); 351 res = system (callstr); 352 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) { 353 log_msg (0, "running: %s", callstr); 354 log_msg (0, "--------------------------------start of output-----------------------------"); 355 } 356 if (log_if_failure && system ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null")) 357 { 358 log_OS_error("Command failed"); 359 } 360 unlink ("/tmp/mondo-run-prog-thing.err"); 361 fin = fopen ("/tmp/mondo-run-prog-thing.tmp", "r"); 362 if (fin) 363 { 364 for (fgets (incoming, MAX_STR_LEN, fin); !feof (fin); 365 fgets (incoming, MAX_STR_LEN, fin)) 329 // assert_string_is_neither_NULL_nor_zerolength(program); 330 331 if (debug_level <= g_loglevel) { 332 log_if_success = TRUE; 333 log_if_failure = TRUE; 334 } 335 sprintf(callstr, 336 "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err", 337 program); 338 while ((p = strchr(callstr, '\r'))) { 339 *p = ' '; 340 } 341 while ((p = strchr(callstr, '\n'))) { 342 *p = ' '; 343 } /* single '=' is intentional */ 344 345 346 len = (int) strlen(program); 347 for (i = 0; i < 35 - len / 2; i++) { 348 tmp[i] = '-'; 349 } 350 tmp[i] = '\0'; 351 strcat(tmp, " "); 352 strcat(tmp, program); 353 strcat(tmp, " "); 354 for (i = 0; i < 35 - len / 2; i++) { 355 strcat(tmp, "-"); 356 } 357 strcpy(initial_label, tmp); 358 res = system(callstr); 359 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) { 360 log_msg(0, "running: %s", callstr); 361 log_msg(0, 362 "--------------------------------start of output-----------------------------"); 363 } 364 if (log_if_failure 365 && 366 system 367 ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null")) 366 368 { 367 /* patch by Heiko Schlittermann */ 368 p = incoming; 369 while (p && *p) 370 { 371 if ((p = strchr(p, '%'))) 372 { 373 memmove(p, p+1, strlen(p) +1); 374 p += 2; 375 } 376 } 377 /* end of patch */ 378 strip_spaces (incoming); 379 if ((res==0 && log_if_success) || (res!=0 && log_if_failure)) 380 { log_msg (0, incoming); } 381 } 382 paranoid_fclose (fin); 383 } 384 unlink("/tmp/mondo-run-prog-thing.tmp"); 385 if ((res==0 && log_if_success) || (res!=0 && log_if_failure)) 386 { 387 log_msg (0, "--------------------------------end of output------------------------------"); 388 if (res) { log_msg (0,"...ran with res=%d", res); } 389 else { log_msg(0, "...ran just fine. :-)"); } 390 } 369 log_OS_error("Command failed"); 370 } 371 unlink("/tmp/mondo-run-prog-thing.err"); 372 fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r"); 373 if (fin) { 374 for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin); 375 fgets(incoming, MAX_STR_LEN, fin)) { 376 /* patch by Heiko Schlittermann */ 377 p = incoming; 378 while (p && *p) { 379 if ((p = strchr(p, '%'))) { 380 memmove(p, p + 1, strlen(p) + 1); 381 p += 2; 382 } 383 } 384 /* end of patch */ 385 strip_spaces(incoming); 386 if ((res == 0 && log_if_success) 387 || (res != 0 && log_if_failure)) { 388 log_msg(0, incoming); 389 } 390 } 391 paranoid_fclose(fin); 392 } 393 unlink("/tmp/mondo-run-prog-thing.tmp"); 394 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) { 395 log_msg(0, 396 "--------------------------------end of output------------------------------"); 397 if (res) { 398 log_msg(0, "...ran with res=%d", res); 399 } else { 400 log_msg(0, "...ran just fine. :-)"); 401 } 402 } 391 403 // else 392 404 // { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); } 393 405 return (res); 394 406 } 395 407 … … 404 416 * @see log_to_screen 405 417 */ 406 int 407 run_program_and_log_to_screen (char *basic_call, char *what_i_am_doing) 418 int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing) 408 419 { 409 /*@ int *********************************************************/ 410 int retval = 0; 411 int res = 0; 412 int i; 413 414 /*@ pointers *****************************************************/ 415 FILE *fin; 416 417 /*@ buffers *****************************************************/ 418 char tmp[MAX_STR_LEN*2]; 419 char command[MAX_STR_LEN*2]; 420 char lockfile[MAX_STR_LEN]; 421 422 /*@ end vars ****************************************************/ 423 424 assert_string_is_neither_NULL_nor_zerolength(basic_call); 425 426 sprintf (lockfile, "/tmp/mojo-jojo.blah.XXXXXX"); 427 mkstemp (lockfile); 428 sprintf (command, 429 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", 430 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile); 431 open_evalcall_form (what_i_am_doing); 432 sprintf (tmp, "Executing %s", basic_call); 433 log_msg (2, tmp); 434 if (!(fin = popen (command, "r"))) 435 { 436 log_OS_error("Unable to popen-in command"); 437 sprintf (tmp, "Failed utterly to call '%s'", command); 438 log_to_screen (tmp); 439 return (1); 440 } 441 if (!does_file_exist(lockfile)) 442 { 443 log_to_screen("Waiting for external binary to start"); 444 for (i = 0; i < 60 && !does_file_exist (lockfile); sleep (1), i++) 445 { 446 log_msg(3, "Waiting for lockfile %s to exist", lockfile); 447 } 448 } 420 /*@ int ******************************************************** */ 421 int retval = 0; 422 int res = 0; 423 int i; 424 425 /*@ pointers **************************************************** */ 426 FILE *fin; 427 428 /*@ buffers **************************************************** */ 429 char tmp[MAX_STR_LEN * 2]; 430 char command[MAX_STR_LEN * 2]; 431 char lockfile[MAX_STR_LEN]; 432 433 /*@ end vars *************************************************** */ 434 435 assert_string_is_neither_NULL_nor_zerolength(basic_call); 436 437 sprintf(lockfile, "/tmp/mojo-jojo.blah.XXXXXX"); 438 mkstemp(lockfile); 439 sprintf(command, 440 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", 441 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile); 442 open_evalcall_form(what_i_am_doing); 443 sprintf(tmp, "Executing %s", basic_call); 444 log_msg(2, tmp); 445 if (!(fin = popen(command, "r"))) { 446 log_OS_error("Unable to popen-in command"); 447 sprintf(tmp, "Failed utterly to call '%s'", command); 448 log_to_screen(tmp); 449 return (1); 450 } 451 if (!does_file_exist(lockfile)) { 452 log_to_screen("Waiting for external binary to start"); 453 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) { 454 log_msg(3, "Waiting for lockfile %s to exist", lockfile); 455 } 456 } 449 457 #ifdef _XWIN 450 /* This only can update when newline goes into the file, 451 but it's *much* prettier/faster on Qt. */ 452 while (does_file_exist (lockfile)) { 453 while (!feof (fin)) { 454 if (!fgets (tmp, 512, fin)) break; 455 log_to_screen (tmp); 456 } 457 usleep (500000); 458 } 458 /* This only can update when newline goes into the file, 459 but it's *much* prettier/faster on Qt. */ 460 while (does_file_exist(lockfile)) { 461 while (!feof(fin)) { 462 if (!fgets(tmp, 512, fin)) 463 break; 464 log_to_screen(tmp); 465 } 466 usleep(500000); 467 } 459 468 #else 460 /* This works on Newt, and it gives quicker updates. */ 461 for (; does_file_exist (lockfile); sleep (1)) 462 { 463 log_file_end_to_screen (MONDO_LOGFILE, ""); 464 update_evalcall_form (1); 465 } 469 /* This works on Newt, and it gives quicker updates. */ 470 for (; does_file_exist(lockfile); sleep(1)) { 471 log_file_end_to_screen(MONDO_LOGFILE, ""); 472 update_evalcall_form(1); 473 } 466 474 #endif 467 paranoid_pclose(fin);468 469 close_evalcall_form();470 unlink(lockfile);471 475 paranoid_pclose(fin); 476 retval += res; 477 close_evalcall_form(); 478 unlink(lockfile); 479 return (retval); 472 480 } 473 481 … … 484 492 * @return NULL to pthread_join. 485 493 */ 486 void *call_partimage_in_bkgd(void *xfb)494 void *call_partimage_in_bkgd(void *xfb) 487 495 { 488 char *transfer_block; 489 int retval=0; 490 491 g_buffer_pid = getpid(); 492 unlink("/tmp/null"); 493 log_msg(1, "starting"); 494 transfer_block = (char*)xfb; 495 transfer_block[0] --; // should now be 1 496 retval = system(transfer_block+2); 497 if (retval) { log_OS_error("partimage returned an error"); } 498 transfer_block[1] = retval; 499 transfer_block[0] --; // should now be 0 500 g_buffer_pid = 0; 501 log_msg(1, "returning"); 502 pthread_exit(NULL); 496 char *transfer_block; 497 int retval = 0; 498 499 g_buffer_pid = getpid(); 500 unlink("/tmp/null"); 501 log_msg(1, "starting"); 502 transfer_block = (char *) xfb; 503 transfer_block[0]--; // should now be 1 504 retval = system(transfer_block + 2); 505 if (retval) { 506 log_OS_error("partimage returned an error"); 507 } 508 transfer_block[1] = retval; 509 transfer_block[0]--; // should now be 0 510 g_buffer_pid = 0; 511 log_msg(1, "returning"); 512 pthread_exit(NULL); 503 513 } 504 514 … … 525 535 #define NO_MORE_SUBVOLS "On-second-hand,momma-bounced-on-old-man,-and-so-we-moved-to-Shaolin-Land." 526 536 527 int copy_from_src_to_dest(FILE *f_orig, FILE*f_archived, char direction)537 int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction) 528 538 { 529 539 // if dir=='w' then copy from orig to archived 530 540 // if dir=='r' then copy from archived to orig 531 char*tmp; 532 char*buf; 533 long int bytes_to_be_read, bytes_read_in, bytes_written_out=0, bufcap, subsliceno=0; 534 int retval=0; 535 FILE*fin; 536 FILE*fout; 537 FILE*ftmp; 538 539 log_msg(5, "Opening."); 540 malloc_string(tmp); 541 tmp[0] = '\0'; 542 bufcap = 256L*1024L; 543 if (!(buf = malloc(bufcap))) { fatal_error("Failed to malloc() buf"); } 544 545 if (direction=='w') 546 { 547 fin = f_orig; 548 fout = f_archived; 549 sprintf(tmp, "%-64s", PIMP_START_SZ); 550 if (fwrite(tmp, 1, 64, fout)!=64) { fatal_error("Can't write the introductory block"); } 551 while(1) 552 { 553 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin); 554 if (bytes_read_in == 0) { break; } 555 sprintf(tmp, "%-64ld", bytes_read_in); 556 if (fwrite(tmp, 1, 64, fout)!=64) { fatal_error("Cannot write introductory block"); } 557 log_msg(7, "subslice #%ld --- I have read %ld of %ld bytes in from f_orig", subsliceno, bytes_read_in, bytes_to_be_read); 558 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); 559 sprintf(tmp, "%-64ld", subsliceno); 560 if (fwrite(tmp, 1, 64, fout)!=64) { fatal_error("Cannot write post-thingy block"); } 561 log_msg(7, "Subslice #%d written OK", subsliceno); 562 subsliceno++; 563 } 564 sprintf(tmp, "%-64ld", 0L); 565 if (fwrite(tmp, 1, 64L, fout)!=64L) { fatal_error("Cannot write final introductory block"); } 566 } 567 else 568 { 569 fin = f_archived; 570 fout = f_orig; 571 if (fread(tmp, 1, 64L, fin)!=64L) { fatal_error("Cannot read the introductory block"); } 572 log_msg(5, "tmp is %s", tmp); 573 if (!strstr(tmp, PIMP_START_SZ)){ fatal_error("Can't find intro blk"); } 574 if (fread(tmp, 1, 64L, fin)!=64L) { fatal_error("Cannot read introductory blk"); } 575 bytes_to_be_read = atol(tmp); 576 while(bytes_to_be_read > 0) 577 { 578 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read); 579 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin); 580 if (bytes_read_in != bytes_to_be_read) { fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives."); } 581 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); 582 if (fread(tmp, 1, 64, fin)!=64) { fatal_error("Cannot read post-thingy block"); } 583 if (atol(tmp) != subsliceno) { log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp), tmp); } 584 log_msg(7, "Subslice #%ld read OK", subsliceno); 585 subsliceno++; 586 if (fread(tmp, 1, 64, fin)!=64) { fatal_error("Cannot read introductory block"); } 587 bytes_to_be_read = atol(tmp); 588 } 589 } 541 char *tmp; 542 char *buf; 543 long int bytes_to_be_read, bytes_read_in, bytes_written_out = 544 0, bufcap, subsliceno = 0; 545 int retval = 0; 546 FILE *fin; 547 FILE *fout; 548 FILE *ftmp; 549 550 log_msg(5, "Opening."); 551 malloc_string(tmp); 552 tmp[0] = '\0'; 553 bufcap = 256L * 1024L; 554 if (!(buf = malloc(bufcap))) { 555 fatal_error("Failed to malloc() buf"); 556 } 557 558 if (direction == 'w') { 559 fin = f_orig; 560 fout = f_archived; 561 sprintf(tmp, "%-64s", PIMP_START_SZ); 562 if (fwrite(tmp, 1, 64, fout) != 64) { 563 fatal_error("Can't write the introductory block"); 564 } 565 while (1) { 566 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin); 567 if (bytes_read_in == 0) { 568 break; 569 } 570 sprintf(tmp, "%-64ld", bytes_read_in); 571 if (fwrite(tmp, 1, 64, fout) != 64) { 572 fatal_error("Cannot write introductory block"); 573 } 574 log_msg(7, 575 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig", 576 subsliceno, bytes_read_in, bytes_to_be_read); 577 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); 578 sprintf(tmp, "%-64ld", subsliceno); 579 if (fwrite(tmp, 1, 64, fout) != 64) { 580 fatal_error("Cannot write post-thingy block"); 581 } 582 log_msg(7, "Subslice #%d written OK", subsliceno); 583 subsliceno++; 584 } 585 sprintf(tmp, "%-64ld", 0L); 586 if (fwrite(tmp, 1, 64L, fout) != 64L) { 587 fatal_error("Cannot write final introductory block"); 588 } 589 } else { 590 fin = f_archived; 591 fout = f_orig; 592 if (fread(tmp, 1, 64L, fin) != 64L) { 593 fatal_error("Cannot read the introductory block"); 594 } 595 log_msg(5, "tmp is %s", tmp); 596 if (!strstr(tmp, PIMP_START_SZ)) { 597 fatal_error("Can't find intro blk"); 598 } 599 if (fread(tmp, 1, 64L, fin) != 64L) { 600 fatal_error("Cannot read introductory blk"); 601 } 602 bytes_to_be_read = atol(tmp); 603 while (bytes_to_be_read > 0) { 604 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, 605 bytes_to_be_read); 606 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin); 607 if (bytes_read_in != bytes_to_be_read) { 608 fatal_error 609 ("Danger, WIll Robinson. Failed to read whole subvol from archives."); 610 } 611 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); 612 if (fread(tmp, 1, 64, fin) != 64) { 613 fatal_error("Cannot read post-thingy block"); 614 } 615 if (atol(tmp) != subsliceno) { 616 log_msg(1, "Wanted subslice %ld but got %ld ('%s')", 617 subsliceno, atol(tmp), tmp); 618 } 619 log_msg(7, "Subslice #%ld read OK", subsliceno); 620 subsliceno++; 621 if (fread(tmp, 1, 64, fin) != 64) { 622 fatal_error("Cannot read introductory block"); 623 } 624 bytes_to_be_read = atol(tmp); 625 } 626 } 590 627 591 628 // log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in); 592 629 593 if (direction=='w') 594 { 595 sprintf(tmp, "%-64s", PIMP_END_SZ); 596 if (fwrite(tmp, 1, 64, fout)!=64) { fatal_error("Can't write the final block"); } 597 } 598 else 599 { 600 log_msg(1, "tmpA is %s", tmp); 601 if (!strstr(tmp, PIMP_END_SZ)) 602 { 603 if (fread(tmp, 1, 64, fin)!=64) { fatal_error("Can't read the final block"); } 604 log_msg(5, "tmpB is %s", tmp); 605 if (!strstr(tmp, PIMP_END_SZ)) 606 { 607 ftmp = fopen("/tmp/out.leftover", "w"); 608 bytes_read_in = fread(tmp, 1, 64, fin); 609 log_msg(1, "bytes_read_in = %ld", bytes_read_in); 630 if (direction == 'w') { 631 sprintf(tmp, "%-64s", PIMP_END_SZ); 632 if (fwrite(tmp, 1, 64, fout) != 64) { 633 fatal_error("Can't write the final block"); 634 } 635 } else { 636 log_msg(1, "tmpA is %s", tmp); 637 if (!strstr(tmp, PIMP_END_SZ)) { 638 if (fread(tmp, 1, 64, fin) != 64) { 639 fatal_error("Can't read the final block"); 640 } 641 log_msg(5, "tmpB is %s", tmp); 642 if (!strstr(tmp, PIMP_END_SZ)) { 643 ftmp = fopen("/tmp/out.leftover", "w"); 644 bytes_read_in = fread(tmp, 1, 64, fin); 645 log_msg(1, "bytes_read_in = %ld", bytes_read_in); 610 646 // if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); } 611 612 613 // 614 615 616 617 618 619 620 621 622 623 624 625 626 return(retval);647 fwrite(tmp, 1, bytes_read_in, ftmp); 648 sprintf(tmp, "I am here - %ld", ftell(fin)); 649 // log_msg(0, tmp); 650 fread(tmp, 1, 512, fin); 651 log_msg(0, "tmp = '%s'", tmp); 652 fwrite(tmp, 1, 512, ftmp); 653 fclose(ftmp); 654 fatal_error("Missing terminating block"); 655 } 656 } 657 } 658 659 paranoid_free(buf); 660 paranoid_free(tmp); 661 log_msg(3, "Successfully copied %ld bytes", bytes_written_out); 662 return (retval); 627 663 } 628 664 … … 634 670 * @return 0 for success, nonzero for failure. 635 671 */ 636 int dynamically_create_pipes_and_copy_from_them_to_output_file(char*input_device, char*output_fname) 672 int dynamically_create_pipes_and_copy_from_them_to_output_file(char 673 *input_device, 674 char 675 *output_fname) 637 676 { 638 char *curr_fifo; 639 char *prev_fifo; 640 char *next_fifo; 641 char*command; 642 char *sz_call_to_partimage; 643 int fifo_number=0; 644 struct stat buf; 645 pthread_t partimage_thread; 646 int res=0; 647 char *tmpstub; 648 FILE*fout; 649 FILE*fin; 650 char *tmp; 651 652 malloc_string(tmpstub); 653 malloc_string(curr_fifo); 654 malloc_string(prev_fifo); 655 malloc_string(next_fifo); 656 malloc_string(command); 657 malloc_string(sz_call_to_partimage); 658 malloc_string(tmp); 659 660 log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt); 661 if (g_tmpfs_mountpt && g_tmpfs_mountpt[0] && does_file_exist(g_tmpfs_mountpt)) 662 { strcpy(tmpstub, g_tmpfs_mountpt); } 663 else 664 { strcpy(tmpstub, "/tmp"); } 665 paranoid_system("rm -f /tmp/*PARTIMAGE*"); 666 sprintf(command, "rm -Rf %s/pih-fifo-*", tmpstub); 667 paranoid_system(command); 668 sprintf(tmpstub+strlen(tmpstub), "/pih-fifo-%ld", (long int)random()); 669 mkfifo(tmpstub, S_IRWXU|S_IRWXG); // never used, though... 670 sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); 671 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number+1); 672 mkfifo(curr_fifo, S_IRWXU|S_IRWXG); 673 mkfifo(next_fifo, S_IRWXU|S_IRWXG); // make sure _next_ fifo already exists before we call partimage 674 sz_call_to_partimage[0]=2; 675 sz_call_to_partimage[1]=0; 676 sprintf(sz_call_to_partimage+2, "partimagehack " PARTIMAGE_PARAMS " save %s %s > /tmp/stdout 2> /tmp/stderr", input_device, tmpstub); 677 log_msg(5, "curr_fifo = %s", curr_fifo); 678 log_msg(5, "next_fifo = %s", next_fifo); 679 log_msg(5, "sz_call_to_partimage call is '%s'", sz_call_to_partimage+2); 680 if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) 681 { log_msg(5, "Deleting %s", output_fname); unlink(output_fname); } 682 if (!(fout = fopen(output_fname, "w"))) { fatal_error("Unable to openout to output_fname"); } 683 res = pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, (void*)sz_call_to_partimage); 684 if (res) { fatal_error("Failed to create thread to call partimage"); } 685 log_msg(1, "Running fore/back at same time"); 686 log_to_screen("Working with partimagehack..."); 687 while(sz_call_to_partimage[0]>0) 688 { 689 sprintf(tmp, "%s\n", NEXT_SUBVOL_PLEASE); 690 if (fwrite(tmp, 1, 128, fout)!=128) { fatal_error("Cannot write interim block"); } 691 log_msg(5, "fifo_number=%d", fifo_number); 692 log_msg(4, "Cat'ting %s", curr_fifo); 693 if (!(fin = fopen(curr_fifo, "r"))) { fatal_error("Unable to openin from fifo"); } 677 char *curr_fifo; 678 char *prev_fifo; 679 char *next_fifo; 680 char *command; 681 char *sz_call_to_partimage; 682 int fifo_number = 0; 683 struct stat buf; 684 pthread_t partimage_thread; 685 int res = 0; 686 char *tmpstub; 687 FILE *fout; 688 FILE *fin; 689 char *tmp; 690 691 malloc_string(tmpstub); 692 malloc_string(curr_fifo); 693 malloc_string(prev_fifo); 694 malloc_string(next_fifo); 695 malloc_string(command); 696 malloc_string(sz_call_to_partimage); 697 malloc_string(tmp); 698 699 log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt); 700 if (g_tmpfs_mountpt && g_tmpfs_mountpt[0] 701 && does_file_exist(g_tmpfs_mountpt)) { 702 strcpy(tmpstub, g_tmpfs_mountpt); 703 } else { 704 strcpy(tmpstub, "/tmp"); 705 } 706 paranoid_system("rm -f /tmp/*PARTIMAGE*"); 707 sprintf(command, "rm -Rf %s/pih-fifo-*", tmpstub); 708 paranoid_system(command); 709 sprintf(tmpstub + strlen(tmpstub), "/pih-fifo-%ld", 710 (long int) random()); 711 mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though... 712 sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); 713 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); 714 mkfifo(curr_fifo, S_IRWXU | S_IRWXG); 715 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage 716 sz_call_to_partimage[0] = 2; 717 sz_call_to_partimage[1] = 0; 718 sprintf(sz_call_to_partimage + 2, 719 "partimagehack " PARTIMAGE_PARAMS 720 " save %s %s > /tmp/stdout 2> /tmp/stderr", input_device, 721 tmpstub); 722 log_msg(5, "curr_fifo = %s", curr_fifo); 723 log_msg(5, "next_fifo = %s", next_fifo); 724 log_msg(5, "sz_call_to_partimage call is '%s'", 725 sz_call_to_partimage + 2); 726 if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) { 727 log_msg(5, "Deleting %s", output_fname); 728 unlink(output_fname); 729 } 730 if (!(fout = fopen(output_fname, "w"))) { 731 fatal_error("Unable to openout to output_fname"); 732 } 733 res = 734 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, 735 (void *) sz_call_to_partimage); 736 if (res) { 737 fatal_error("Failed to create thread to call partimage"); 738 } 739 log_msg(1, "Running fore/back at same time"); 740 log_to_screen("Working with partimagehack..."); 741 while (sz_call_to_partimage[0] > 0) { 742 sprintf(tmp, "%s\n", NEXT_SUBVOL_PLEASE); 743 if (fwrite(tmp, 1, 128, fout) != 128) { 744 fatal_error("Cannot write interim block"); 745 } 746 log_msg(5, "fifo_number=%d", fifo_number); 747 log_msg(4, "Cat'ting %s", curr_fifo); 748 if (!(fin = fopen(curr_fifo, "r"))) { 749 fatal_error("Unable to openin from fifo"); 750 } 694 751 // if (!sz_call_to_partimage[0]) { break; } 695 log_msg(5, "Deleting %s", prev_fifo); 696 unlink(prev_fifo); // just in case 697 copy_from_src_to_dest(fin, fout, 'w'); 698 paranoid_fclose(fin); 699 fifo_number ++; 700 strcpy(prev_fifo, curr_fifo); 701 strcpy(curr_fifo, next_fifo); 702 log_msg(5, "Creating %s", next_fifo); 703 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number+1); 704 mkfifo(next_fifo, S_IRWXU|S_IRWXG); // make sure _next_ fifo exists before we cat this one 705 system("sync"); 706 sleep(5); 707 } 708 sprintf(tmp, "%s\n", NO_MORE_SUBVOLS); 709 if (fwrite(tmp, 1, 128, fout)!=128) { fatal_error("Cannot write interim block"); } 710 if (fwrite(tmp, 1, 128, fout)!=128) { fatal_error("Cannot write interim block"); } 711 if (fwrite(tmp, 1, 128, fout)!=128) { fatal_error("Cannot write interim block"); } 712 if (fwrite(tmp, 1, 128, fout)!=128) { fatal_error("Cannot write interim block"); } 713 paranoid_fclose(fout); 714 log_to_screen("Cleaning up after partimagehack..."); 715 log_msg(3, "Final fifo_number=%d", fifo_number); 716 paranoid_system("sync"); 717 unlink(next_fifo); 718 unlink(curr_fifo); 719 unlink(prev_fifo); 720 log_to_screen("Finished cleaning up."); 752 log_msg(5, "Deleting %s", prev_fifo); 753 unlink(prev_fifo); // just in case 754 copy_from_src_to_dest(fin, fout, 'w'); 755 paranoid_fclose(fin); 756 fifo_number++; 757 strcpy(prev_fifo, curr_fifo); 758 strcpy(curr_fifo, next_fifo); 759 log_msg(5, "Creating %s", next_fifo); 760 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); 761 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo exists before we cat this one 762 system("sync"); 763 sleep(5); 764 } 765 sprintf(tmp, "%s\n", NO_MORE_SUBVOLS); 766 if (fwrite(tmp, 1, 128, fout) != 128) { 767 fatal_error("Cannot write interim block"); 768 } 769 if (fwrite(tmp, 1, 128, fout) != 128) { 770 fatal_error("Cannot write interim block"); 771 } 772 if (fwrite(tmp, 1, 128, fout) != 128) { 773 fatal_error("Cannot write interim block"); 774 } 775 if (fwrite(tmp, 1, 128, fout) != 128) { 776 fatal_error("Cannot write interim block"); 777 } 778 paranoid_fclose(fout); 779 log_to_screen("Cleaning up after partimagehack..."); 780 log_msg(3, "Final fifo_number=%d", fifo_number); 781 paranoid_system("sync"); 782 unlink(next_fifo); 783 unlink(curr_fifo); 784 unlink(prev_fifo); 785 log_to_screen("Finished cleaning up."); 721 786 722 787 // if (!lstat(sz_wait_for_this_file, &statbuf)) 723 788 // { log_msg(3, "WARNING! %s was not processed.", sz_wait_for_this_file); } 724 725 726 727 728 729 730 731 732 733 734 735 736 return(res);789 log_msg(2, "Waiting for pthread_join() to join."); 790 pthread_join(partimage_thread, NULL); 791 res = sz_call_to_partimage[1]; 792 log_msg(2, "pthread_join() joined OK."); 793 log_msg(1, "Partimagehack(save) returned %d", res); 794 unlink(tmpstub); 795 paranoid_free(curr_fifo); 796 paranoid_free(prev_fifo); 797 paranoid_free(next_fifo); 798 paranoid_free(tmpstub); 799 paranoid_free(tmp); 800 paranoid_free(command); 801 return (res); 737 802 } 738 803 … … 744 809 * @return 0 for success, nonzero for failure. 745 810 */ 746 int feed_into_partimage(char *input_device, char*output_fname)811 int feed_into_partimage(char *input_device, char *output_fname) 747 812 { 748 813 // BACKUP 749 int res; 750 751 if (!does_file_exist(input_device)) { fatal_error ("input device does not exist"); } 752 if ( !find_home_of_exe("partimagehack")) { fatal_error( "partimagehack not found" ); } 753 res = dynamically_create_pipes_and_copy_from_them_to_output_file(input_device, output_fname); 754 return(res); 814 int res; 815 816 if (!does_file_exist(input_device)) { 817 fatal_error("input device does not exist"); 818 } 819 if (!find_home_of_exe("partimagehack")) { 820 fatal_error("partimagehack not found"); 821 } 822 res = 823 dynamically_create_pipes_and_copy_from_them_to_output_file 824 (input_device, output_fname); 825 return (res); 755 826 } 756 827 … … 759 830 760 831 761 int 762 run_external_binary_with_percentage_indicator_OLD (char *tt, char *cmd) 832 int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd) 763 833 { 764 834 765 /*@ int *************************************************************** */766 835 /*@ int *************************************************************** */ 836 int res = 0; 767 837 int percentage = 0; 768 838 int maxpc = 0; … … 770 840 int last_pcno = 0; 771 841 772 /*@ buffers ************************************************************/ 773 char *command; 774 char *tempfile; 775 char *title; 776 /*@ pointers ***********************************************************/ 777 FILE *pin; 778 779 malloc_string(title); 780 malloc_string(command); 781 malloc_string(tempfile); 782 assert_string_is_neither_NULL_nor_zerolength(cmd); 783 assert_string_is_neither_NULL_nor_zerolength(title); 784 785 strcpy (title, tt); 786 strcpy (tempfile, 787 call_program_and_get_last_line_of_output 788 ("mktemp -q /tmp/mondo.XXXXXXXX")); 789 sprintf (command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile, 790 tempfile); 791 log_msg (3, command); 792 open_evalcall_form (title); 793 if (!(pin = popen (command, "r"))) 794 { 795 log_OS_error ("fmt err"); 796 return (1); 797 } 798 maxpc = 100; 842 /*@ buffers *********************************************************** */ 843 char *command; 844 char *tempfile; 845 char *title; 846 /*@ pointers ********************************************************** */ 847 FILE *pin; 848 849 malloc_string(title); 850 malloc_string(command); 851 malloc_string(tempfile); 852 assert_string_is_neither_NULL_nor_zerolength(cmd); 853 assert_string_is_neither_NULL_nor_zerolength(title); 854 855 strcpy(title, tt); 856 strcpy(tempfile, 857 call_program_and_get_last_line_of_output 858 ("mktemp -q /tmp/mondo.XXXXXXXX")); 859 sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile, 860 tempfile); 861 log_msg(3, command); 862 open_evalcall_form(title); 863 if (!(pin = popen(command, "r"))) { 864 log_OS_error("fmt err"); 865 return (1); 866 } 867 maxpc = 100; 799 868 // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD 800 for (sleep (1); does_file_exist (tempfile); sleep (1)) 801 { 802 pcno = grab_percentage_from_last_line_of_file (MONDO_LOGFILE); 803 if (pcno < 0 || pcno > 100) 804 { 805 log_msg (5, "Weird pc#"); 806 continue; 807 } 808 percentage = pcno * 100 / maxpc; 809 if (pcno <= 5 && last_pcno > 40) 810 { 811 close_evalcall_form (); 812 strcpy (title, "Verifying..."); 813 open_evalcall_form (title); 814 } 815 last_pcno = pcno; 816 update_evalcall_form (percentage); 817 } 869 for (sleep(1); does_file_exist(tempfile); sleep(1)) { 870 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE); 871 if (pcno < 0 || pcno > 100) { 872 log_msg(5, "Weird pc#"); 873 continue; 874 } 875 percentage = pcno * 100 / maxpc; 876 if (pcno <= 5 && last_pcno > 40) { 877 close_evalcall_form(); 878 strcpy(title, "Verifying..."); 879 open_evalcall_form(title); 880 } 881 last_pcno = pcno; 882 update_evalcall_form(percentage); 883 } 818 884 // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD 819 close_evalcall_form (); 820 if (pclose (pin)) { res++; log_OS_error("Unable to pclose"); } 821 unlink (tempfile); 822 paranoid_free(command); 823 paranoid_free(tempfile); 824 paranoid_free(title); 825 return (res); 885 close_evalcall_form(); 886 if (pclose(pin)) { 887 res++; 888 log_OS_error("Unable to pclose"); 889 } 890 unlink(tempfile); 891 paranoid_free(command); 892 paranoid_free(tempfile); 893 paranoid_free(title); 894 return (res); 826 895 } 827 896 … … 829 898 830 899 831 void *run_prog_in_bkgd_then_exit(void*info)900 void *run_prog_in_bkgd_then_exit(void *info) 832 901 { 833 char*sz_command; 834 static int res=4444; 835 836 res=999; 837 sz_command = (char*)info; 838 log_msg(4, "sz_command = '%s'", sz_command); 839 res = system(sz_command); 840 if (res>256 && res!=4444) { res=res / 256; } 841 log_msg(4, "child res = %d", res); 842 sz_command[0] = '\0'; 843 pthread_exit((void*)(&res)); 902 char *sz_command; 903 static int res = 4444; 904 905 res = 999; 906 sz_command = (char *) info; 907 log_msg(4, "sz_command = '%s'", sz_command); 908 res = system(sz_command); 909 if (res > 256 && res != 4444) { 910 res = res / 256; 911 } 912 log_msg(4, "child res = %d", res); 913 sz_command[0] = '\0'; 914 pthread_exit((void *) (&res)); 844 915 } 845 916 … … 847 918 848 919 849 int 850 run_external_binary_with_percentage_indicator_NEW (char*tt, char*cmd) 920 int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) 851 921 { 852 922 853 /*@ int *************************************************************** */854 923 /*@ int *************************************************************** */ 924 int res = 0; 855 925 int percentage = 0; 856 926 int maxpc = 100; 857 927 int pcno = 0; 858 928 int last_pcno = 0; 859 int counter=0; 860 861 /*@ buffers ************************************************************/ 862 char *command; 863 char *title; 864 /*@ pointers ***********************************************************/ 865 static int chldres=0; 866 int *pchild_result; 867 pthread_t childthread; 868 869 pchild_result = &chldres; 870 assert_string_is_neither_NULL_nor_zerolength(cmd); 871 assert_string_is_neither_NULL_nor_zerolength(tt); 872 *pchild_result = 999; 873 874 malloc_string(title); 875 malloc_string(command); 876 strcpy (title, tt); 877 sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE); 878 log_msg (3, "command = '%s'", command); 879 if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void*)command))) 880 { fatal_error("Unable to create an archival thread"); } 881 882 log_msg(8, "Parent running"); 883 open_evalcall_form (title); 884 for (sleep (1); command[0]!='\0'; sleep (1)) 885 { 886 pcno = grab_percentage_from_last_line_of_file (MONDO_LOGFILE); 887 if (pcno <= 0 || pcno > 100) 888 { 889 log_msg (8, "Weird pc#"); 890 continue; 891 } 892 percentage = pcno * 100 / maxpc; 893 if (pcno <= 5 && last_pcno >= 40) 894 { 895 close_evalcall_form (); 896 strcpy (title, "Verifying..."); 897 open_evalcall_form (title); 898 } 899 if (counter++ >= 5) 900 { 901 counter=0; 902 log_file_end_to_screen (MONDO_LOGFILE, ""); 903 } 904 last_pcno = pcno; 905 update_evalcall_form (percentage); 906 } 907 log_file_end_to_screen (MONDO_LOGFILE, ""); 908 close_evalcall_form (); 909 pthread_join(childthread, (void*)(&pchild_result)); 910 if (pchild_result) { res = *pchild_result; } 911 else { res = 666; } 912 log_msg(3, "Parent res = %d", res); 913 paranoid_free(command); 914 paranoid_free(title); 915 return (res); 929 int counter = 0; 930 931 /*@ buffers *********************************************************** */ 932 char *command; 933 char *title; 934 /*@ pointers ********************************************************** */ 935 static int chldres = 0; 936 int *pchild_result; 937 pthread_t childthread; 938 939 pchild_result = &chldres; 940 assert_string_is_neither_NULL_nor_zerolength(cmd); 941 assert_string_is_neither_NULL_nor_zerolength(tt); 942 *pchild_result = 999; 943 944 malloc_string(title); 945 malloc_string(command); 946 strcpy(title, tt); 947 sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE); 948 log_msg(3, "command = '%s'", command); 949 if ((res = 950 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, 951 (void *) command))) { 952 fatal_error("Unable to create an archival thread"); 953 } 954 955 log_msg(8, "Parent running"); 956 open_evalcall_form(title); 957 for (sleep(1); command[0] != '\0'; sleep(1)) { 958 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE); 959 if (pcno <= 0 || pcno > 100) { 960 log_msg(8, "Weird pc#"); 961 continue; 962 } 963 percentage = pcno * 100 / maxpc; 964 if (pcno <= 5 && last_pcno >= 40) { 965 close_evalcall_form(); 966 strcpy(title, "Verifying..."); 967 open_evalcall_form(title); 968 } 969 if (counter++ >= 5) { 970 counter = 0; 971 log_file_end_to_screen(MONDO_LOGFILE, ""); 972 } 973 last_pcno = pcno; 974 update_evalcall_form(percentage); 975 } 976 log_file_end_to_screen(MONDO_LOGFILE, ""); 977 close_evalcall_form(); 978 pthread_join(childthread, (void *) (&pchild_result)); 979 if (pchild_result) { 980 res = *pchild_result; 981 } else { 982 res = 666; 983 } 984 log_msg(3, "Parent res = %d", res); 985 paranoid_free(command); 986 paranoid_free(title); 987 return (res); 916 988 } 917 989 … … 928 1000 * @bug Probably unnecessary, as the partimage is just a sparse file. We could use @c dd to restore it. 929 1001 */ 930 int feed_outfrom_partimage(char *output_device, char*input_fifo)1002 int feed_outfrom_partimage(char *output_device, char *input_fifo) 931 1003 { 932 1004 // RESTORE 933 1005 char *tmp; 934 1006 // char *command; 935 936 937 938 939 940 941 942 943 944 int fifo_number=0;945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 1007 char *stuff; 1008 char *sz_call_to_partimage; 1009 pthread_t partimage_thread; 1010 int res; 1011 char *curr_fifo; 1012 char *prev_fifo; 1013 char *oldest_fifo; 1014 char *next_fifo; 1015 char *afternxt_fifo; 1016 int fifo_number = 0; 1017 char *tmpstub; 1018 FILE *fin; 1019 FILE *fout; 1020 1021 malloc_string(curr_fifo); 1022 malloc_string(prev_fifo); 1023 malloc_string(next_fifo); 1024 malloc_string(afternxt_fifo); 1025 malloc_string(oldest_fifo); 1026 malloc_string(tmp); 1027 sz_call_to_partimage = malloc(1000); 1028 malloc_string(stuff); 1029 malloc_string(tmpstub); 1030 1031 log_msg(1, "output_device=%s", output_device); 1032 log_msg(1, "input_fifo=%s", input_fifo); 961 1033 /* I don't trust g_tmpfs_mountpt 962 1034 if (g_tmpfs_mountpt[0]) … … 967 1039 { 968 1040 */ 969 1041 strcpy(tmpstub, "/tmp"); 970 1042 // } 971 log_msg(1, "tmpstub was %s", tmpstub); 972 strcpy(stuff, tmpstub); 973 sprintf(tmpstub, "%s/pih-fifo-%ld", stuff, (long int)random()); 974 log_msg(1, "tmpstub is now %s", tmpstub); 975 unlink("/tmp/PARTIMAGEHACK-POSITION"); 976 unlink(PAUSE_PARTIMAGE_FNAME); 977 paranoid_system("rm -f /tmp/*PARTIMAGE*"); 978 sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); 979 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number+1); 980 sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number+2); 981 mkfifo(PIH_LOG, S_IRWXU|S_IRWXG); 982 mkfifo(curr_fifo, S_IRWXU|S_IRWXG); 983 mkfifo(next_fifo, S_IRWXU|S_IRWXG); // make sure _next_ fifo already exists before we call partimage 984 mkfifo(afternxt_fifo, S_IRWXU|S_IRWXG); 985 system("cat " PIH_LOG " > /dev/null &"); 986 log_msg(3, "curr_fifo = %s", curr_fifo); 987 log_msg(3, "next_fifo = %s", next_fifo); 988 if (!does_file_exist(input_fifo)) { fatal_error ("input fifo does not exist" ); } 989 if (!(fin = fopen(input_fifo, "r"))) { fatal_error ("Unable to openin from input_fifo"); } 990 if ( !find_home_of_exe("partimagehack")) { fatal_error( "partimagehack not found" ); } 991 sz_call_to_partimage[0]=2; 992 sz_call_to_partimage[1]=0; 993 sprintf(sz_call_to_partimage+2, "partimagehack " PARTIMAGE_PARAMS " restore %s %s > /dev/null 2>> %s", output_device, curr_fifo, MONDO_LOGFILE); 994 log_msg(1, "output_device = %s", output_device); 995 log_msg(1, "curr_fifo = %s", curr_fifo); 996 log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage+2); 997 res = pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, (void*)sz_call_to_partimage); 998 if (res) { fatal_error("Failed to create thread to call partimage"); } 999 log_msg(1, "Running fore/back at same time"); 1000 log_msg(2," Trying to openin %s", input_fifo); 1001 if (!does_file_exist(input_fifo)) { log_msg(2, "Warning - %s does not exist", input_fifo); } 1002 while(!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) 1003 { 1004 log_msg(6, "Waiting for partimagehack (restore) to start"); 1005 sleep(1); 1006 } 1007 while(sz_call_to_partimage[0]>0) 1008 { 1009 if (fread(tmp, 1, 128, fin)!=128) { fatal_error("Cannot read introductory block"); } 1010 if (strstr(tmp, NEXT_SUBVOL_PLEASE)) 1011 { log_msg(2, "Great. Next subvol coming up."); } 1012 else if (strstr(tmp, NO_MORE_SUBVOLS)) 1013 { log_msg(2, "Great. That was the last subvol."); break; } 1014 else 1015 { log_msg(2, "WTF is this? '%s'", tmp); fatal_error("Unknown interim block"); } 1016 if (feof(fin)) { log_msg(1, "Eof(fin) detected. Breaking."); break; } 1017 log_msg(3, "Processing subvol %d", fifo_number); 1018 log_msg(5, "fifo_number=%d", fifo_number); 1019 if (!(fout = fopen(curr_fifo, "w"))) { fatal_error("Cannot openout to curr_fifo"); } 1020 log_msg(6, "Deleting %s", oldest_fifo); 1021 copy_from_src_to_dest(fout, fin, 'r'); 1022 paranoid_fclose(fout); 1023 fifo_number ++; 1024 unlink(oldest_fifo); // just in case 1025 strcpy(oldest_fifo, prev_fifo); 1026 strcpy(prev_fifo, curr_fifo); 1027 strcpy(curr_fifo, next_fifo); 1028 strcpy(next_fifo, afternxt_fifo); 1029 sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number+2); 1030 log_msg(6, "Creating %s", afternxt_fifo); 1031 mkfifo(afternxt_fifo, S_IRWXU|S_IRWXG); // make sure _next_ fifo already exists before we access current fifo 1032 fflush(fin); 1043 log_msg(1, "tmpstub was %s", tmpstub); 1044 strcpy(stuff, tmpstub); 1045 sprintf(tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random()); 1046 log_msg(1, "tmpstub is now %s", tmpstub); 1047 unlink("/tmp/PARTIMAGEHACK-POSITION"); 1048 unlink(PAUSE_PARTIMAGE_FNAME); 1049 paranoid_system("rm -f /tmp/*PARTIMAGE*"); 1050 sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); 1051 sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); 1052 sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2); 1053 mkfifo(PIH_LOG, S_IRWXU | S_IRWXG); 1054 mkfifo(curr_fifo, S_IRWXU | S_IRWXG); 1055 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage 1056 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); 1057 system("cat " PIH_LOG " > /dev/null &"); 1058 log_msg(3, "curr_fifo = %s", curr_fifo); 1059 log_msg(3, "next_fifo = %s", next_fifo); 1060 if (!does_file_exist(input_fifo)) { 1061 fatal_error("input fifo does not exist"); 1062 } 1063 if (!(fin = fopen(input_fifo, "r"))) { 1064 fatal_error("Unable to openin from input_fifo"); 1065 } 1066 if (!find_home_of_exe("partimagehack")) { 1067 fatal_error("partimagehack not found"); 1068 } 1069 sz_call_to_partimage[0] = 2; 1070 sz_call_to_partimage[1] = 0; 1071 sprintf(sz_call_to_partimage + 2, 1072 "partimagehack " PARTIMAGE_PARAMS 1073 " restore %s %s > /dev/null 2>> %s", output_device, curr_fifo, 1074 MONDO_LOGFILE); 1075 log_msg(1, "output_device = %s", output_device); 1076 log_msg(1, "curr_fifo = %s", curr_fifo); 1077 log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage + 2); 1078 res = 1079 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, 1080 (void *) sz_call_to_partimage); 1081 if (res) { 1082 fatal_error("Failed to create thread to call partimage"); 1083 } 1084 log_msg(1, "Running fore/back at same time"); 1085 log_msg(2, " Trying to openin %s", input_fifo); 1086 if (!does_file_exist(input_fifo)) { 1087 log_msg(2, "Warning - %s does not exist", input_fifo); 1088 } 1089 while (!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) { 1090 log_msg(6, "Waiting for partimagehack (restore) to start"); 1091 sleep(1); 1092 } 1093 while (sz_call_to_partimage[0] > 0) { 1094 if (fread(tmp, 1, 128, fin) != 128) { 1095 fatal_error("Cannot read introductory block"); 1096 } 1097 if (strstr(tmp, NEXT_SUBVOL_PLEASE)) { 1098 log_msg(2, "Great. Next subvol coming up."); 1099 } else if (strstr(tmp, NO_MORE_SUBVOLS)) { 1100 log_msg(2, "Great. That was the last subvol."); 1101 break; 1102 } else { 1103 log_msg(2, "WTF is this? '%s'", tmp); 1104 fatal_error("Unknown interim block"); 1105 } 1106 if (feof(fin)) { 1107 log_msg(1, "Eof(fin) detected. Breaking."); 1108 break; 1109 } 1110 log_msg(3, "Processing subvol %d", fifo_number); 1111 log_msg(5, "fifo_number=%d", fifo_number); 1112 if (!(fout = fopen(curr_fifo, "w"))) { 1113 fatal_error("Cannot openout to curr_fifo"); 1114 } 1115 log_msg(6, "Deleting %s", oldest_fifo); 1116 copy_from_src_to_dest(fout, fin, 'r'); 1117 paranoid_fclose(fout); 1118 fifo_number++; 1119 unlink(oldest_fifo); // just in case 1120 strcpy(oldest_fifo, prev_fifo); 1121 strcpy(prev_fifo, curr_fifo); 1122 strcpy(curr_fifo, next_fifo); 1123 strcpy(next_fifo, afternxt_fifo); 1124 sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2); 1125 log_msg(6, "Creating %s", afternxt_fifo); 1126 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we access current fifo 1127 fflush(fin); 1033 1128 // system("sync"); 1034 usleep(1000L*100L);1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 return(res);1129 usleep(1000L * 100L); 1130 } 1131 paranoid_fclose(fin); 1132 paranoid_system("sync"); 1133 log_msg(1, "Partimagehack has finished. Great. Fin-closing."); 1134 log_msg(1, "Waiting for pthread_join"); 1135 pthread_join(partimage_thread, NULL); 1136 res = sz_call_to_partimage[1]; 1137 log_msg(1, "Yay. Partimagehack (restore) returned %d", res); 1138 unlink(prev_fifo); 1139 unlink(curr_fifo); 1140 unlink(next_fifo); 1141 unlink(afternxt_fifo); 1142 unlink(PIH_LOG); 1143 paranoid_free(tmp); 1144 paranoid_free(sz_call_to_partimage); 1145 paranoid_free(stuff); 1146 paranoid_free(prev_fifo); 1147 paranoid_free(curr_fifo); 1148 paranoid_free(next_fifo); 1149 paranoid_free(afternxt_fifo); 1150 paranoid_free(oldest_fifo); 1151 paranoid_free(tmpstub); 1152 return (res); 1058 1153 } 1059
Note:
See TracChangeset
for help on using the changeset viewer.