source: MondoRescue/trunk/mondo/mondo/common/libmondo-fork.c@ 130

Last change on this file since 130 was 130, checked in by bcornec, 18 years ago

memory management revision on libmondo-fork.c

  • Property svn:keywords set to Id
File size: 29.2 KB
RevLine 
[130]1/* $Id: libmondo-fork.c 130 2005-11-22 16:47:53Z bcornec $
2 subroutines for handling forking/pthreads/etc.
[1]3*/
4
5
6#include "my-stuff.h"
7#include "mondostructures.h"
8#include "libmondo-fork.h"
9#include "libmondo-string-EXT.h"
10#include "libmondo-gui-EXT.h"
11#include "libmondo-files-EXT.h"
12#include "libmondo-tools-EXT.h"
13#include "lib-common-externs.h"
14
15/*@unused@*/
[59]16//static char cvsid[] = "$Id: libmondo-fork.c 130 2005-11-22 16:47:53Z bcornec $";
[1]17
18extern char *g_tmpfs_mountpt;
19extern t_bkptype g_backup_media_type;
20extern bool g_text_mode;
[59]21pid_t g_buffer_pid = 0;
[1]22
23
24/**
25 * Call a program and retrieve its last line of output.
26 * @param call The program to run.
27 * @return The last line of its output.
28 * @note The returned value points to static storage that will be overwritten with each call.
29 */
[59]30char *call_program_and_get_last_line_of_output(char *call)
[1]31{
[59]32 /*@ buffers ***************************************************** */
[130]33 static char *result = NULL;
34 char *tmp = NULL;
[1]35
[59]36 /*@ pointers **************************************************** */
37 FILE *fin;
[1]38
[130]39 int n = 0;
[1]40
[59]41 /*@******************************************************************** */
[1]42
[59]43 assert_string_is_neither_NULL_nor_zerolength(call);
44 if ((fin = popen(call, "r"))) {
[130]45 for (getline(&tmp, &n, fin); !feof(fin);
46 getline(&tmp, &n, fin)) {
[59]47 if (strlen(tmp) > 1) {
[130]48 if (result != NULL) {
49 paranoid_free(result);
50 }
51 asprintf(&result, tmp);
[59]52 }
53 }
54 paranoid_pclose(fin);
55 } else {
56 log_OS_error("Unable to popen call");
[1]57 }
[59]58 strip_spaces(result);
[130]59 paranoid_free(tmp);
[59]60 return (result);
[1]61}
62
63#define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
64
65/**
66 * Call mkisofs to create an ISO image.
67 * @param bkpinfo The backup information structure. Fields used:
68 * - @c bkpinfo->manual_cd_tray
69 * - @c bkpinfo->backup_media_type
70 * - @c bkpinfo->please_dont_eject_when_restoring
71 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
72 * - @c _ISO_ will become the ISO file (@p isofile)
73 * - @c _CD#_ becomes the CD number (@p cd_no)
74 * - @c _ERR_ becomes the logfile (@p g_logfile)
75 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
76 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
77 * @param logstub Unused.
78 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
79 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
80 * @bug @p logstub is unused.
81 */
82int
[59]83eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
84 char *basic_call, char *isofile,
85 int cd_no, char *logstub, char *what_i_am_doing)
[1]86{
87
[59]88 /*@ int's *** */
89 int retval = 0;
[1]90
91
[59]92 /*@ buffers *** */
[130]93 char *midway_call, *ultimate_call, *tmp, *command,
94 *cd_number_str;
[59]95 char *p;
[1]96
97/*@*********** End Variables ***************************************/
98
[59]99 log_msg(3, "Starting");
100 assert(bkpinfo != NULL);
101 assert_string_is_neither_NULL_nor_zerolength(basic_call);
102 assert_string_is_neither_NULL_nor_zerolength(isofile);
103 assert_string_is_neither_NULL_nor_zerolength(logstub);
104 if (!(midway_call = malloc(1200))) {
105 fatal_error("Cannot malloc midway_call");
106 }
107 if (!(ultimate_call = malloc(1200))) {
108 fatal_error("Cannot malloc ultimate_call");
109 }
110 if (!(tmp = malloc(1200))) {
111 fatal_error("Cannot malloc tmp");
112 }
[1]113
[130]114 asprintf(&cd_number_str, "%d", cd_no);
[59]115 resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
116 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
[130]117 paranoid_free(cd_number_str);
118
[59]119 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
120 log_msg(4, "basic call = '%s'", basic_call);
121 log_msg(4, "midway_call = '%s'", midway_call);
122 log_msg(4, "tmp = '%s'", tmp);
123 log_msg(4, "ultimate call = '%s'", ultimate_call);
[130]124 asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
[1]125
[59]126 log_to_screen
127 ("Please be patient. Do not be alarmed by on-screen inactivity.");
128 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
129 what_i_am_doing);
130 strcpy(tmp, command);
131 if (bkpinfo->manual_cd_tray) {
132 p = strstr(tmp, "2>>");
133 if (p) {
134 sprintf(p, " ");
135 while (*p == ' ') {
136 p++;
137 }
138 for (; *p != ' '; p++) {
139 *p = ' ';
140 }
141 }
[130]142 paranoid_free(command);
143 asprintf(&command, tmp);
[1]144#ifndef _XWIN
[59]145 if (!g_text_mode) {
146 newtSuspend();
147 }
[1]148#endif
[59]149 log_msg(1, "command = '%s'", command);
150 retval += system(command);
151 if (!g_text_mode) {
152 newtResume();
153 }
154 if (retval) {
155 log_msg(2, "Basic call '%s' returned an error.", basic_call);
156 popup_and_OK("Press ENTER to continue.");
157 popup_and_OK
158 ("mkisofs and/or cdrecord returned an error. CD was not created");
159 }
[1]160 }
[59]161 /* if text mode then do the above & RETURN; if not text mode, do this... */
162 else {
163 log_msg(3, "command = '%s'", command);
[1]164// yes_this_is_a_goto:
[59]165 retval =
166 run_external_binary_with_percentage_indicator_NEW
167 (what_i_am_doing, command);
168 }
[130]169 paranoid_free(command);
[1]170
[59]171 paranoid_free(midway_call);
172 paranoid_free(ultimate_call);
173 paranoid_free(tmp);
174 return (retval);
[1]175}
176
177
178/**
179 * Run a program and log its output (stdout and stderr) to the logfile.
180 * @param program The program to run. Passed to the shell, so you can use pipes etc.
181 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
182 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
183 */
[59]184int run_program_and_log_output(char *program, int debug_level)
[1]185{
[59]186 /*@ buffer ****************************************************** */
[130]187 char *callstr;
188 char *incoming = NULL;
[59]189 char tmp[MAX_STR_LEN * 2];
[1]190
[59]191 /*@ int ********************************************************* */
192 int res;
193 int i;
[130]194 int n = 0;
[59]195 int len;
196 bool log_if_failure = FALSE;
197 bool log_if_success = FALSE;
[1]198
[59]199 /*@ pointers *************************************************** */
200 FILE *fin;
201 char *p;
[1]202
[59]203 /*@ end vars *************************************************** */
[1]204
[59]205 assert(program != NULL);
206 if (!program[0]) {
207 log_msg(2, "Warning - asked to run zerolength program");
208 return (1);
209 }
[1]210
[59]211 if (debug_level <= g_loglevel) {
212 log_if_success = TRUE;
213 log_if_failure = TRUE;
214 }
[130]215 asprintf(&callstr,
[59]216 "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
217 program);
218 while ((p = strchr(callstr, '\r'))) {
219 *p = ' ';
220 }
221 while ((p = strchr(callstr, '\n'))) {
222 *p = ' ';
223 } /* single '=' is intentional */
[1]224
225
[59]226 len = (int) strlen(program);
227 for (i = 0; i < 35 - len / 2; i++) {
228 tmp[i] = '-';
229 }
230 tmp[i] = '\0';
231 strcat(tmp, " ");
232 strcat(tmp, program);
233 strcat(tmp, " ");
234 for (i = 0; i < 35 - len / 2; i++) {
235 strcat(tmp, "-");
236 }
237 res = system(callstr);
238 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
239 log_msg(0, "running: %s", callstr);
240 log_msg(0,
241 "--------------------------------start of output-----------------------------");
242 }
[130]243 paranoid_free(callstr);
244
[59]245 if (log_if_failure
246 &&
247 system
248 ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
[1]249 {
[59]250 log_OS_error("Command failed");
251 }
252 unlink("/tmp/mondo-run-prog-thing.err");
253 fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
254 if (fin) {
[130]255 for (getline(&incoming, &n, fin); !feof(fin);
256 getline(&incoming, &n, fin)) {
[59]257 /* patch by Heiko Schlittermann */
258 p = incoming;
259 while (p && *p) {
260 if ((p = strchr(p, '%'))) {
261 memmove(p, p + 1, strlen(p) + 1);
262 p += 2;
263 }
264 }
265 /* end of patch */
266 strip_spaces(incoming);
267 if ((res == 0 && log_if_success)
268 || (res != 0 && log_if_failure)) {
269 log_msg(0, incoming);
270 }
[1]271 }
[130]272 paranoid_free(incoming);
[59]273 paranoid_fclose(fin);
[1]274 }
[59]275 unlink("/tmp/mondo-run-prog-thing.tmp");
276 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
277 log_msg(0,
278 "--------------------------------end of output------------------------------");
279 if (res) {
280 log_msg(0, "...ran with res=%d", res);
281 } else {
282 log_msg(0, "...ran just fine. :-)");
283 }
284 }
[1]285// else
286// { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
[59]287 return (res);
[1]288}
289
290
291/**
292 * Run a program and log its output to the screen.
293 * @param basic_call The program to run.
294 * @param what_i_am_doing The title of the evalcall form.
295 * @return The return value of the command (varies, but 0 almost always means success).
296 * @see run_program_and_log_output
297 * @see log_to_screen
298 */
[59]299int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
[1]300{
[59]301 /*@ int ******************************************************** */
302 int retval = 0;
303 int res = 0;
[130]304 int n = 0;
[59]305 int i;
[1]306
[59]307 /*@ pointers **************************************************** */
308 FILE *fin;
[1]309
[59]310 /*@ buffers **************************************************** */
[130]311 char *tmp;
312 char *command;
313 char *lockfile;
[1]314
[59]315 /*@ end vars *************************************************** */
[1]316
[59]317 assert_string_is_neither_NULL_nor_zerolength(basic_call);
[1]318
[130]319 asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
[59]320 mkstemp(lockfile);
[130]321 asprintf(&command,
[59]322 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
323 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
324 open_evalcall_form(what_i_am_doing);
[130]325 asprintf(&tmp, "Executing %s", basic_call);
[59]326 log_msg(2, tmp);
[130]327 paranoid_free(tmp);
328
[59]329 if (!(fin = popen(command, "r"))) {
330 log_OS_error("Unable to popen-in command");
[130]331 asprintf(&tmp, "Failed utterly to call '%s'", command);
[59]332 log_to_screen(tmp);
[130]333 paranoid_free(tmp);
334 paranoid_free(lockfile);
335 paranoid_free(command);
[59]336 return (1);
337 }
[130]338 paranoid_free(command);
339
[59]340 if (!does_file_exist(lockfile)) {
341 log_to_screen("Waiting for external binary to start");
342 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
343 log_msg(3, "Waiting for lockfile %s to exist", lockfile);
344 }
345 }
[1]346#ifdef _XWIN
[59]347 /* This only can update when newline goes into the file,
348 but it's *much* prettier/faster on Qt. */
[130]349 tmp = NULL;
[59]350 while (does_file_exist(lockfile)) {
351 while (!feof(fin)) {
[130]352 if (!getline(&tmp, &n, fin))
[59]353 break;
354 log_to_screen(tmp);
355 }
356 usleep(500000);
357 }
[130]358 paranoid_free(tmp);
[1]359#else
[59]360 /* This works on Newt, and it gives quicker updates. */
361 for (; does_file_exist(lockfile); sleep(1)) {
362 log_file_end_to_screen(MONDO_LOGFILE, "");
363 update_evalcall_form(1);
364 }
[1]365#endif
[68]366 /* Evaluate the status returned by pclose to get the exit code of the called program. */
367 errno = 0;
368 res = pclose(fin);
369 /* Log actual pclose errors. */
[89]370 if (errno)
371 log_msg(5, "pclose err: %d", errno);
[68]372 /* Check if we have a valid status. If we do, extract the called program's exit code. */
373 /* If we don't, highlight this fact by returning -1. */
374 if (WIFEXITED(res)) {
[89]375 retval = WEXITSTATUS(res);
[68]376 } else {
[89]377 retval = -1;
[68]378 }
[59]379 close_evalcall_form();
380 unlink(lockfile);
[130]381 paranoid_free(lockfile);
382
[59]383 return (retval);
[1]384}
385
386
387/**
388 * Thread callback to run a command (partimage) in the background.
389 * @param xfb A transfer block of @c char, containing:
390 * - xfb:[0] A marker, should be set to 2. Decremented to 1 while the command is running and 0 when it's finished.
391 * - xfb:[1] The command's return value, if xfb:[0] is 0.
392 * - xfb+2: A <tt>NULL</tt>-terminated string containing the command to be run.
393 * @return NULL to pthread_join.
394 */
[59]395void *call_partimage_in_bkgd(void *xfb)
[1]396{
[59]397 char *transfer_block;
398 int retval = 0;
[1]399
[59]400 g_buffer_pid = getpid();
401 unlink("/tmp/null");
402 log_msg(1, "starting");
403 transfer_block = (char *) xfb;
404 transfer_block[0]--; // should now be 1
405 retval = system(transfer_block + 2);
406 if (retval) {
407 log_OS_error("partimage returned an error");
408 }
409 transfer_block[1] = retval;
410 transfer_block[0]--; // should now be 0
411 g_buffer_pid = 0;
412 log_msg(1, "returning");
413 pthread_exit(NULL);
[1]414}
415
416
417/**
418 * File to touch if we want partimage to wait for us.
419 */
420#define PAUSE_PARTIMAGE_FNAME "/tmp/PAUSE-PARTIMAGE-FOR-MONDO"
421
422/**
[130]423 * Apparently used. @bug This has a purpose, but what?
[1]424 */
425#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
426#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
427
428/**
429 * Marker to start the next subvolume for Partimage.
430 */
431#define NEXT_SUBVOL_PLEASE "I-grew-up-on-the-crime-side,-the-New-York-Times-side,-where-staying-alive-was-no-jive"
432
433/**
434 * Marker to end the partimage file.
435 */
436#define NO_MORE_SUBVOLS "On-second-hand,momma-bounced-on-old-man,-and-so-we-moved-to-Shaolin-Land."
437
[59]438int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
[1]439{
440// if dir=='w' then copy from orig to archived
441// if dir=='r' then copy from archived to orig
[59]442 char *tmp;
443 char *buf;
444 long int bytes_to_be_read, bytes_read_in, bytes_written_out =
445 0, bufcap, subsliceno = 0;
446 int retval = 0;
447 FILE *fin;
448 FILE *fout;
449 FILE *ftmp;
[1]450
[59]451 log_msg(5, "Opening.");
452 bufcap = 256L * 1024L;
453 if (!(buf = malloc(bufcap))) {
454 fatal_error("Failed to malloc() buf");
455 }
[1]456
[59]457 if (direction == 'w') {
458 fin = f_orig;
459 fout = f_archived;
[130]460 asprintf(&tmp, "%-64s", PIMP_START_SZ);
[59]461 if (fwrite(tmp, 1, 64, fout) != 64) {
462 fatal_error("Can't write the introductory block");
463 }
[130]464 paranoid_free(tmp);
465
[59]466 while (1) {
467 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
468 if (bytes_read_in == 0) {
469 break;
470 }
[130]471 asprintf(&tmp, "%-64ld", bytes_read_in);
[59]472 if (fwrite(tmp, 1, 64, fout) != 64) {
473 fatal_error("Cannot write introductory block");
474 }
[130]475 paranoid_free(tmp);
476
[59]477 log_msg(7,
478 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
479 subsliceno, bytes_read_in, bytes_to_be_read);
480 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
[130]481 asprintf(&tmp, "%-64ld", subsliceno);
[59]482 if (fwrite(tmp, 1, 64, fout) != 64) {
483 fatal_error("Cannot write post-thingy block");
484 }
[130]485 paranoid_free(tmp);
486
[59]487 log_msg(7, "Subslice #%d written OK", subsliceno);
488 subsliceno++;
489 }
[130]490 asprintf(&tmp, "%-64ld", 0L);
[59]491 if (fwrite(tmp, 1, 64L, fout) != 64L) {
492 fatal_error("Cannot write final introductory block");
493 }
494 } else {
495 fin = f_archived;
496 fout = f_orig;
[130]497 if (!(tmp = malloc(64L))) {
498 fatal_error("Failed to malloc() tmp");
499 }
[59]500 if (fread(tmp, 1, 64L, fin) != 64L) {
501 fatal_error("Cannot read the introductory block");
502 }
503 log_msg(5, "tmp is %s", tmp);
504 if (!strstr(tmp, PIMP_START_SZ)) {
505 fatal_error("Can't find intro blk");
506 }
507 if (fread(tmp, 1, 64L, fin) != 64L) {
508 fatal_error("Cannot read introductory blk");
509 }
510 bytes_to_be_read = atol(tmp);
511 while (bytes_to_be_read > 0) {
512 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
513 bytes_to_be_read);
514 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
515 if (bytes_read_in != bytes_to_be_read) {
516 fatal_error
517 ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
518 }
519 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
520 if (fread(tmp, 1, 64, fin) != 64) {
521 fatal_error("Cannot read post-thingy block");
522 }
523 if (atol(tmp) != subsliceno) {
524 log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
525 subsliceno, atol(tmp), tmp);
526 }
527 log_msg(7, "Subslice #%ld read OK", subsliceno);
528 subsliceno++;
529 if (fread(tmp, 1, 64, fin) != 64) {
530 fatal_error("Cannot read introductory block");
531 }
532 bytes_to_be_read = atol(tmp);
533 }
534 }
[1]535
536// log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
537
[59]538 if (direction == 'w') {
[130]539 asprintf(&tmp, "%-64s", PIMP_END_SZ);
[59]540 if (fwrite(tmp, 1, 64, fout) != 64) {
541 fatal_error("Can't write the final block");
542 }
[130]543 paranoid_free(tmp);
[59]544 } else {
545 log_msg(1, "tmpA is %s", tmp);
546 if (!strstr(tmp, PIMP_END_SZ)) {
547 if (fread(tmp, 1, 64, fin) != 64) {
548 fatal_error("Can't read the final block");
549 }
550 log_msg(5, "tmpB is %s", tmp);
551 if (!strstr(tmp, PIMP_END_SZ)) {
552 ftmp = fopen("/tmp/out.leftover", "w");
553 bytes_read_in = fread(tmp, 1, 64, fin);
554 log_msg(1, "bytes_read_in = %ld", bytes_read_in);
[1]555// if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
[59]556 fwrite(tmp, 1, bytes_read_in, ftmp);
[130]557 paranoid_free(tmp);
558
559 if (!(tmp = malloc(512))) {
560 fatal_error("Failed to malloc() tmp");
561 }
[59]562 fread(tmp, 1, 512, fin);
563 log_msg(0, "tmp = '%s'", tmp);
564 fwrite(tmp, 1, 512, ftmp);
565 fclose(ftmp);
566 fatal_error("Missing terminating block");
567 }
568 }
[130]569 paranoid_free(tmp);
[59]570 }
[1]571
[59]572 paranoid_free(buf);
573 log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
574 return (retval);
[1]575}
576
577
578/**
579 * Call partimage from @p input_device to @p output_fname.
580 * @param input_device The device to read.
581 * @param output_fname The file to write.
582 * @return 0 for success, nonzero for failure.
583 */
[59]584int dynamically_create_pipes_and_copy_from_them_to_output_file(char
[89]585 *input_device, char
[59]586 *output_fname)
[1]587{
[59]588 char *curr_fifo;
[130]589 char *prev_fifo = NULL;
[59]590 char *next_fifo;
591 char *command;
592 char *sz_call_to_partimage;
593 int fifo_number = 0;
594 struct stat buf;
595 pthread_t partimage_thread;
596 int res = 0;
597 char *tmpstub;
598 FILE *fout;
599 FILE *fin;
600 char *tmp;
[1]601
[59]602 log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt);
603 if (g_tmpfs_mountpt && g_tmpfs_mountpt[0]
604 && does_file_exist(g_tmpfs_mountpt)) {
[130]605 asprintf(&tmpstub, g_tmpfs_mountpt);
[59]606 } else {
[130]607 asprintf(&tmpstub, "/tmp");
[59]608 }
609 paranoid_system("rm -f /tmp/*PARTIMAGE*");
[130]610 asprintf(&command, "rm -Rf %s/pih-fifo-*", tmpstub);
[59]611 paranoid_system(command);
[130]612 paranoid_free(command);
613
614 asprintf(&tmp, "%s/pih-fifo-%ld", tmpstub, (long int) random());
615 paranoid_free(tmpstub);
616 tmpstub = tmp;
617 paranoid_free(tmp);
618
[59]619 mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though...
[130]620 asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
621 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
[59]622 mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
623 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
[130]624 asprintf(&sz_call_to_partimage,
625 "%c%cpartimagehack " PARTIMAGE_PARAMS
626 " save %s %s > /tmp/stdout 2> /tmp/stderr", 2, 0, input_device,
[59]627 tmpstub);
628 log_msg(5, "curr_fifo = %s", curr_fifo);
629 log_msg(5, "next_fifo = %s", next_fifo);
630 log_msg(5, "sz_call_to_partimage call is '%s'",
631 sz_call_to_partimage + 2);
632 if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) {
633 log_msg(5, "Deleting %s", output_fname);
634 unlink(output_fname);
635 }
636 if (!(fout = fopen(output_fname, "w"))) {
637 fatal_error("Unable to openout to output_fname");
638 }
639 res =
640 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
641 (void *) sz_call_to_partimage);
642 if (res) {
643 fatal_error("Failed to create thread to call partimage");
644 }
645 log_msg(1, "Running fore/back at same time");
646 log_to_screen("Working with partimagehack...");
647 while (sz_call_to_partimage[0] > 0) {
[130]648 asprintf(&tmp, "%s\n", NEXT_SUBVOL_PLEASE);
[59]649 if (fwrite(tmp, 1, 128, fout) != 128) {
650 fatal_error("Cannot write interim block");
651 }
[130]652 paranoid_free(tmp);
653
[59]654 log_msg(5, "fifo_number=%d", fifo_number);
655 log_msg(4, "Cat'ting %s", curr_fifo);
656 if (!(fin = fopen(curr_fifo, "r"))) {
657 fatal_error("Unable to openin from fifo");
658 }
[130]659 if (prev_fifo != NULL) {
660 log_msg(5, "Deleting %s", prev_fifo);
661 unlink(prev_fifo); // just in case
662 paranoid_free(prev_fifo);
663 }
[59]664 copy_from_src_to_dest(fin, fout, 'w');
665 paranoid_fclose(fin);
666 fifo_number++;
[130]667
668 prev_fifo = curr_fifo;
669 curr_fifo = next_fifo;
[59]670 log_msg(5, "Creating %s", next_fifo);
[130]671 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
[59]672 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo exists before we cat this one
673 system("sync");
674 sleep(5);
675 }
[130]676 asprintf(&tmp, "%s\n", NO_MORE_SUBVOLS);
[59]677 if (fwrite(tmp, 1, 128, fout) != 128) {
678 fatal_error("Cannot write interim block");
679 }
680 if (fwrite(tmp, 1, 128, fout) != 128) {
681 fatal_error("Cannot write interim block");
682 }
683 if (fwrite(tmp, 1, 128, fout) != 128) {
684 fatal_error("Cannot write interim block");
685 }
686 if (fwrite(tmp, 1, 128, fout) != 128) {
687 fatal_error("Cannot write interim block");
688 }
[130]689 paranoid_free(tmp);
[59]690 paranoid_fclose(fout);
691 log_to_screen("Cleaning up after partimagehack...");
692 log_msg(3, "Final fifo_number=%d", fifo_number);
693 paranoid_system("sync");
694 unlink(next_fifo);
[130]695 paranoid_free(next_fifo);
696
[59]697 unlink(curr_fifo);
[130]698 paranoid_free(curr_fifo);
699
[59]700 unlink(prev_fifo);
[130]701 paranoid_free(prev_fifo);
702
[59]703 log_to_screen("Finished cleaning up.");
[1]704
705// if (!lstat(sz_wait_for_this_file, &statbuf))
706// { log_msg(3, "WARNING! %s was not processed.", sz_wait_for_this_file); }
[59]707 log_msg(2, "Waiting for pthread_join() to join.");
708 pthread_join(partimage_thread, NULL);
709 res = sz_call_to_partimage[1];
[130]710 paranoid_free(sz_call_to_partimage);
[59]711 log_msg(2, "pthread_join() joined OK.");
712 log_msg(1, "Partimagehack(save) returned %d", res);
713 unlink(tmpstub);
714 paranoid_free(tmpstub);
[130]715
[59]716 return (res);
[1]717}
718
719
720/**
721 * Feed @p input_device through partimage to @p output_fname.
722 * @param input_device The device to image.
723 * @param output_fname The file to write.
724 * @return 0 for success, nonzero for failure.
725 */
[59]726int feed_into_partimage(char *input_device, char *output_fname)
[1]727{
728// BACKUP
[59]729 int res;
[1]730
[59]731 if (!does_file_exist(input_device)) {
732 fatal_error("input device does not exist");
733 }
734 if (!find_home_of_exe("partimagehack")) {
735 fatal_error("partimagehack not found");
736 }
737 res =
738 dynamically_create_pipes_and_copy_from_them_to_output_file
739 (input_device, output_fname);
740 return (res);
[1]741}
742
743
[59]744int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
[1]745{
746
[59]747 /*@ int *************************************************************** */
748 int res = 0;
[1]749 int percentage = 0;
750 int maxpc = 0;
751 int pcno = 0;
752 int last_pcno = 0;
753
[59]754 /*@ buffers *********************************************************** */
755 char *command;
756 char *tempfile;
757 /*@ pointers ********************************************************** */
758 FILE *pin;
[1]759
[59]760 assert_string_is_neither_NULL_nor_zerolength(cmd);
[1]761
[130]762 asprintf(&tempfile,
[59]763 call_program_and_get_last_line_of_output
764 ("mktemp -q /tmp/mondo.XXXXXXXX"));
[130]765 asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
[59]766 tempfile);
767 log_msg(3, command);
[130]768 open_evalcall_form(tt);
769
[59]770 if (!(pin = popen(command, "r"))) {
771 log_OS_error("fmt err");
[130]772 paranoid_free(command);
773 paranoid_free(tempfile);
[59]774 return (1);
775 }
[130]776 paranoid_free(command);
777
[59]778 maxpc = 100;
[1]779// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
[59]780 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
781 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
782 if (pcno < 0 || pcno > 100) {
783 log_msg(5, "Weird pc#");
784 continue;
785 }
786 percentage = pcno * 100 / maxpc;
787 if (pcno <= 5 && last_pcno > 40) {
788 close_evalcall_form();
[130]789 open_evalcall_form("Verifying...");
[59]790 }
791 last_pcno = pcno;
792 update_evalcall_form(percentage);
[1]793 }
[59]794// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
795 close_evalcall_form();
796 if (pclose(pin)) {
797 res++;
798 log_OS_error("Unable to pclose");
[1]799 }
[59]800 unlink(tempfile);
801 paranoid_free(tempfile);
802 return (res);
[1]803}
804
805
[59]806void *run_prog_in_bkgd_then_exit(void *info)
[1]807{
[59]808 char *sz_command;
809 static int res = 4444;
[1]810
[59]811 res = 999;
812 sz_command = (char *) info;
813 log_msg(4, "sz_command = '%s'", sz_command);
814 res = system(sz_command);
815 if (res > 256 && res != 4444) {
816 res = res / 256;
817 }
818 log_msg(4, "child res = %d", res);
819 sz_command[0] = '\0';
820 pthread_exit((void *) (&res));
[1]821}
822
823
[59]824int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
[1]825{
826
[59]827 /*@ int *************************************************************** */
828 int res = 0;
[1]829 int percentage = 0;
830 int maxpc = 100;
831 int pcno = 0;
832 int last_pcno = 0;
[59]833 int counter = 0;
[1]834
[59]835 /*@ buffers *********************************************************** */
836 char *command;
837 /*@ pointers ********************************************************** */
838 static int chldres = 0;
839 int *pchild_result;
840 pthread_t childthread;
[1]841
[59]842 pchild_result = &chldres;
843 assert_string_is_neither_NULL_nor_zerolength(cmd);
844 assert_string_is_neither_NULL_nor_zerolength(tt);
845 *pchild_result = 999;
[1]846
[130]847 asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
[59]848 log_msg(3, "command = '%s'", command);
849 if ((res =
850 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
851 (void *) command))) {
852 fatal_error("Unable to create an archival thread");
853 }
[1]854
[59]855 log_msg(8, "Parent running");
[130]856 open_evalcall_form(tt);
[59]857 for (sleep(1); command[0] != '\0'; sleep(1)) {
858 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
859 if (pcno <= 0 || pcno > 100) {
860 log_msg(8, "Weird pc#");
861 continue;
862 }
863 percentage = pcno * 100 / maxpc;
864 if (pcno <= 5 && last_pcno >= 40) {
865 close_evalcall_form();
[130]866 open_evalcall_form("Verifying...");
[59]867 }
868 if (counter++ >= 5) {
869 counter = 0;
870 log_file_end_to_screen(MONDO_LOGFILE, "");
871 }
872 last_pcno = pcno;
873 update_evalcall_form(percentage);
[1]874 }
[130]875 paranoid_free(command);
876
[59]877 log_file_end_to_screen(MONDO_LOGFILE, "");
878 close_evalcall_form();
879 pthread_join(childthread, (void *) (&pchild_result));
880 if (pchild_result) {
881 res = *pchild_result;
882 } else {
883 res = 666;
[1]884 }
[59]885 log_msg(3, "Parent res = %d", res);
886 return (res);
[1]887}
888
889#define PIH_LOG "/var/log/partimage-debug.log"
890
891/**
892 * Feed @p input_fifo through partimage (restore) to @p output_device.
893 * @param input_fifo The partimage file to read.
894 * @param output_device Where to put the output.
895 * @return The return value of partimagehack (0 for success).
896 * @bug Probably unnecessary, as the partimage is just a sparse file. We could use @c dd to restore it.
897 */
[59]898int feed_outfrom_partimage(char *output_device, char *input_fifo)
[1]899{
900// RESTORE
[59]901 char *tmp;
902 char *stuff;
903 char *sz_call_to_partimage;
904 pthread_t partimage_thread;
905 int res;
906 char *curr_fifo;
907 char *prev_fifo;
[130]908 char *oldest_fifo = NULL;
[59]909 char *next_fifo;
910 char *afternxt_fifo;
911 int fifo_number = 0;
912 char *tmpstub;
913 FILE *fin;
914 FILE *fout;
[1]915
[59]916 log_msg(1, "output_device=%s", output_device);
917 log_msg(1, "input_fifo=%s", input_fifo);
[130]918 asprintf(&tmpstub, "/tmp");
919
[59]920 log_msg(1, "tmpstub was %s", tmpstub);
[130]921 asprintf(&stuff, tmpstub);
922 paranoid_free(tmpstub);
923
924 asprintf(&tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random());
925 paranoid_free(stuff);
926
[59]927 log_msg(1, "tmpstub is now %s", tmpstub);
928 unlink("/tmp/PARTIMAGEHACK-POSITION");
929 unlink(PAUSE_PARTIMAGE_FNAME);
930 paranoid_system("rm -f /tmp/*PARTIMAGE*");
[130]931 asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
932 asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
933 asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
[59]934 mkfifo(PIH_LOG, S_IRWXU | S_IRWXG);
935 mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
936 mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
937 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG);
938 system("cat " PIH_LOG " > /dev/null &");
939 log_msg(3, "curr_fifo = %s", curr_fifo);
940 log_msg(3, "next_fifo = %s", next_fifo);
941 if (!does_file_exist(input_fifo)) {
942 fatal_error("input fifo does not exist");
943 }
944 if (!(fin = fopen(input_fifo, "r"))) {
945 fatal_error("Unable to openin from input_fifo");
946 }
947 if (!find_home_of_exe("partimagehack")) {
948 fatal_error("partimagehack not found");
949 }
[130]950 asprintf(&sz_call_to_partimage,
951 "%c%cpartimagehack " PARTIMAGE_PARAMS
952 " restore %s %s > /dev/null 2>> %s", 2, 0, output_device, curr_fifo,
[59]953 MONDO_LOGFILE);
954 log_msg(1, "output_device = %s", output_device);
955 log_msg(1, "curr_fifo = %s", curr_fifo);
956 log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage + 2);
957 res =
958 pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
959 (void *) sz_call_to_partimage);
960 if (res) {
961 fatal_error("Failed to create thread to call partimage");
962 }
963 log_msg(1, "Running fore/back at same time");
964 log_msg(2, " Trying to openin %s", input_fifo);
965 if (!does_file_exist(input_fifo)) {
966 log_msg(2, "Warning - %s does not exist", input_fifo);
967 }
968 while (!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) {
969 log_msg(6, "Waiting for partimagehack (restore) to start");
970 sleep(1);
971 }
[130]972
973 if (!(tmp = malloc(128))) {
974 fatal_error("Failed to malloc() tmp");
975 }
[59]976 while (sz_call_to_partimage[0] > 0) {
977 if (fread(tmp, 1, 128, fin) != 128) {
978 fatal_error("Cannot read introductory block");
979 }
980 if (strstr(tmp, NEXT_SUBVOL_PLEASE)) {
981 log_msg(2, "Great. Next subvol coming up.");
982 } else if (strstr(tmp, NO_MORE_SUBVOLS)) {
983 log_msg(2, "Great. That was the last subvol.");
984 break;
985 } else {
986 log_msg(2, "WTF is this? '%s'", tmp);
987 fatal_error("Unknown interim block");
988 }
989 if (feof(fin)) {
990 log_msg(1, "Eof(fin) detected. Breaking.");
991 break;
992 }
993 log_msg(3, "Processing subvol %d", fifo_number);
994 log_msg(5, "fifo_number=%d", fifo_number);
995 if (!(fout = fopen(curr_fifo, "w"))) {
996 fatal_error("Cannot openout to curr_fifo");
997 }
998 copy_from_src_to_dest(fout, fin, 'r');
999 paranoid_fclose(fout);
1000 fifo_number++;
[130]1001 if (oldest_fifo != NULL) {
1002 log_msg(6, "Deleting %s", oldest_fifo);
1003 unlink(oldest_fifo); // just in case
1004 paranoid_free(oldest_fifo);
1005 }
1006 oldest_fifo = prev_fifo;
1007 prev_fifo = curr_fifo;
1008 curr_fifo = next_fifo;
1009 next_fifo = afternxt_fifo;
1010 asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
[59]1011 log_msg(6, "Creating %s", afternxt_fifo);
1012 mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we access current fifo
1013 fflush(fin);
[1]1014// system("sync");
[59]1015 usleep(1000L * 100L);
1016 }
[130]1017 paranoid_free(tmp);
1018 paranoid_free(tmpstub);
1019
[59]1020 paranoid_fclose(fin);
1021 paranoid_system("sync");
1022 log_msg(1, "Partimagehack has finished. Great. Fin-closing.");
1023 log_msg(1, "Waiting for pthread_join");
1024 pthread_join(partimage_thread, NULL);
1025 res = sz_call_to_partimage[1];
[130]1026 paranoid_free(sz_call_to_partimage);
1027
[59]1028 log_msg(1, "Yay. Partimagehack (restore) returned %d", res);
1029 unlink(prev_fifo);
[130]1030 paranoid_free(prev_fifo);
1031
[59]1032 unlink(curr_fifo);
[130]1033 paranoid_free(curr_fifo);
1034
[59]1035 unlink(next_fifo);
[130]1036 paranoid_free(next_fifo);
1037
[59]1038 unlink(afternxt_fifo);
[130]1039 paranoid_free(afternxt_fifo);
1040
[59]1041 unlink(PIH_LOG);
[130]1042 /* BERLIOS : pas de unlink(oldest_fifo) ??? */
[59]1043 paranoid_free(oldest_fifo);
[130]1044
[59]1045 return (res);
[1]1046}
Note: See TracBrowser for help on using the repository browser.