source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-fork.c@ 2383

Last change on this file since 2383 was 2383, checked in by Bruno Cornec, 15 years ago

call_program_and_get_last_line_of_output is now allocating memory and returning that string

  • Property svn:keywords set to Id
File size: 18.3 KB
Line 
1/* libmondo-fork.c
2 $Id: libmondo-fork.c 2383 2009-09-10 00:55:51Z bruno $
3
4- subroutines for handling forking/pthreads/etc.
5*/
6
7
8#include "my-stuff.h"
9#include "mr_mem.h"
10#include "mondostructures.h"
11#include "libmondo-fork.h"
12#include "libmondo-string-EXT.h"
13#include "libmondo-gui-EXT.h"
14#include "libmondo-files-EXT.h"
15#include "libmondo-tools-EXT.h"
16#include "lib-common-externs.h"
17
18/*@unused@*/
19//static char cvsid[] = "$Id: libmondo-fork.c 2383 2009-09-10 00:55:51Z bruno $";
20
21extern t_bkptype g_backup_media_type;
22extern bool g_text_mode;
23extern char *MONDO_LOGFILE;
24
25/* Reference to global bkpinfo */
26extern struct s_bkpinfo *bkpinfo;
27pid_t g_buffer_pid = 0;
28
29
30/**
31 * Call a program and retrieve its last line of output.
32 * @param call The program to run.
33 * @return The last line of its output.
34 * @note The returned value points to static storage that will be overwritten with each call.
35 */
36char *call_program_and_get_last_line_of_output(char *call)
37{
38 /*@ buffers ***************************************************** */
39 char *result = NULL;
40
41 /*@ pointers **************************************************** */
42 FILE *fin;
43
44 /*@******************************************************************** */
45
46 assert_string_is_neither_NULL_nor_zerolength(call);
47 if ((fin = popen(call, "r"))) {
48 for (mr_getline(result, fin); !feof(fin); mr_getline(result, fin));
49 paranoid_pclose(fin);
50 } else {
51 log_OS_error("Unable to popen call");
52 }
53 mr_strip_spaces(result);
54 return(result);
55}
56
57
58
59
60
61
62#define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
63
64/**
65 * Call mkisofs to create an ISO image.
66 * @param bkpinfo The backup information structure. Fields used:
67 * - @c bkpinfo->manual_cd_tray
68 * - @c bkpinfo->backup_media_type
69 * - @c bkpinfo->please_dont_eject_when_restoring
70 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
71 * - @c _ISO_ will become the ISO file (@p isofile)
72 * - @c _CD#_ becomes the CD number (@p cd_no)
73 * - @c _ERR_ becomes the logfile (@p g_logfile)
74 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
75 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
76 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
77 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
78 */
79int
80eval_call_to_make_ISO(char *basic_call, char *isofile, int cd_no, char *what_i_am_doing) {
81
82 /*@ int's *** */
83 int retval = 0;
84
85
86 /*@ buffers *** */
87 char *midway_call = NULL;
88 char *ultimate_call = NULL;
89 char *tmp = NULL;
90 char *incoming, *old_stderr;
91
92 char *cd_number_str = NULL;
93 char *command = NULL;
94 char *p;
95 char *tmp1 = NULL;
96 char *tmp2 = NULL;
97
98/*@*********** End Variables ***************************************/
99
100 log_msg(3, "Starting");
101 assert(bkpinfo != NULL);
102 assert_string_is_neither_NULL_nor_zerolength(isofile);
103 malloc_string(incoming);
104 malloc_string(old_stderr);
105
106 incoming[0] = '\0';
107 old_stderr[0] = '\0';
108
109 if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) {
110 mr_asprintf(tmp1, "su - %s -c \"%s\"", bkpinfo->netfs_user, basic_call);
111 } else {
112 mr_asprintf(tmp1, "%s", basic_call);
113 }
114
115 mr_asprintf(cd_number_str, "%d", cd_no);
116 log_msg(4, "basic call = '%s'", tmp1);
117 midway_call = resolve_naff_tokens(tmp1, isofile, "_ISO_");
118 mr_free(tmp1);
119
120 log_msg(4, "midway_call = '%s'", midway_call);
121 tmp = resolve_naff_tokens(midway_call, cd_number_str, "_CD#_");
122 mr_free(cd_number_str);
123 mr_free(midway_call);
124
125 log_msg(4, "tmp = '%s'", tmp);
126 ultimate_call = resolve_naff_tokens(tmp, MONDO_LOGFILE, "_ERR_");
127 mr_free(tmp);
128
129 log_msg(4, "ultimate call = '%s'", ultimate_call);
130 mr_asprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
131 mr_free(ultimate_call);
132
133 log_to_screen
134 ("Please be patient. Do not be alarmed by on-screen inactivity.");
135 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
136 what_i_am_doing);
137 if (bkpinfo->manual_cd_tray) {
138 mr_asprintf(tmp2, "%s", command);
139 p = strstr(tmp2, "2>>");
140 if (p) {
141 *p++ = ' ';
142 *p++ = ' ';
143 *p++ = ' ';
144 while (*p == ' ') {
145 p++;
146 }
147 for (; (*p != ' ') && (*p != '\0'); p++) {
148 *p = ' ';
149 }
150 }
151 mr_free(command);
152 command = tmp2;
153 if (!g_text_mode) {
154 newtSuspend();
155 }
156 log_msg(1, "command = '%s'", command);
157 retval += system(command);
158 if (!g_text_mode) {
159 newtResume();
160 }
161 if (retval) {
162 popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created");
163 }
164 }
165 /* if text mode then do the above & RETURN; if not text mode, do this... */
166 else {
167 log_msg(3, "command = '%s'", command);
168 retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
169 }
170 mr_free(command);
171 paranoid_free(incoming);
172 paranoid_free(old_stderr);
173 return (retval);
174}
175
176
177/**
178 * Call copy of data to create an USB image.
179 * @param bkpinfo The backup information structure. Fields used:
180 * - @c bkpinfo->backup_media_type
181 * @return Exit code of @c copy (0 is success, anything else indicates failure).
182 */
183int
184eval_call_to_make_USB(char *command, char *what_i_am_doing) {
185
186 /*@ int's *** */
187 int retval = 0;
188
189
190/*@*********** End Variables ***************************************/
191
192 log_msg(3, "Starting");
193 assert(bkpinfo != NULL);
194
195 log_to_screen
196 ("Please be patient. Do not be alarmed by on-screen inactivity.");
197 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
198 what_i_am_doing);
199
200 if (!g_text_mode) {
201 newtSuspend();
202 }
203 log_msg(1, "command = '%s'", command);
204 if (!g_text_mode) {
205 retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
206 } else {
207 retval += system(command);
208 }
209 if (!g_text_mode) {
210 newtResume();
211 }
212
213 return (retval);
214}
215
216
217
218
219/**
220 * Run a program and log its output (stdout and stderr) to the logfile.
221 * @param program The program to run. Passed to the shell, so you can use pipes etc.
222 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
223 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
224 */
225int run_program_and_log_output(char *program, int debug_level)
226{
227 /*@ buffer ****************************************************** */
228 char *callstr = NULL;
229 char incoming[MAX_STR_LEN * 2];
230 char *tmp1 = NULL;
231
232 /*@ int ********************************************************* */
233 int res;
234 bool log_if_failure = FALSE;
235 bool log_if_success = FALSE;
236
237 /*@ pointers *************************************************** */
238 FILE *fin;
239 char *p;
240
241 /*@ end vars *************************************************** */
242
243 assert(program != NULL);
244 if (!program[0]) {
245 log_msg(2, "Warning - asked to run zerolength program");
246 return (1);
247 }
248
249 if (debug_level <= g_loglevel) {
250 log_if_success = TRUE;
251 log_if_failure = TRUE;
252 }
253 mr_asprintf(callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err", program, bkpinfo->tmpdir, bkpinfo->tmpdir);
254 while ((p = strchr(callstr, '\r'))) {
255 *p = ' ';
256 }
257 while ((p = strchr(callstr, '\n'))) {
258 *p = ' ';
259 } /* single '=' is intentional */
260
261
262 res = system(callstr);
263 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
264 log_msg(0, "running: %s", callstr);
265 log_msg(0, "--------------------------------start of output-----------------------------");
266 }
267 mr_free(callstr);
268
269 mr_asprintf(callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
270 if (log_if_failure && system(callstr)) {
271 log_OS_error("Command failed");
272 }
273 mr_free(callstr);
274
275 mr_asprintf(tmp1, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
276 unlink(tmp1);
277 mr_free(tmp1);
278
279 mr_asprintf(tmp1, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
280 fin = fopen(tmp1, "r");
281 if (fin) {
282 for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin); fgets(incoming, MAX_STR_LEN, fin)) {
283 p = incoming;
284 while (p && *p) {
285 if ((p = strchr(p, '%'))) {
286 memmove(p, p + 1, strlen(p) + 1);
287 p += 2;
288 }
289 }
290 strip_spaces(incoming);
291 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
292 log_msg(0, incoming);
293 }
294 }
295 paranoid_fclose(fin);
296 }
297 unlink(tmp1);
298 mr_free(tmp1);
299
300 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
301 log_msg(0,
302 "--------------------------------end of output------------------------------");
303 if (res) {
304 log_msg(0, "...ran with res=%d", res);
305 } else {
306 log_msg(0, "...ran just fine. :-)");
307 }
308 }
309 return (res);
310}
311
312
313
314/**
315 * Run a program and log its output to the screen.
316 * @param basic_call The program to run.
317 * @param what_i_am_doing The title of the evalcall form.
318 * @return The return value of the command (varies, but 0 almost always means success).
319 * @see run_program_and_log_output
320 * @see log_to_screen
321 */
322int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
323{
324 /*@ int ******************************************************** */
325 int retval = 0;
326 int res = 0;
327 int i;
328
329 /*@ pointers **************************************************** */
330 FILE *fin;
331
332 /*@ buffers **************************************************** */
333 char *command = NULL;
334 char *lockfile = NULL;
335
336 /*@ end vars *************************************************** */
337
338 assert_string_is_neither_NULL_nor_zerolength(basic_call);
339
340 mr_asprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
341
342 mr_asprintf(command, "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
343 open_evalcall_form(what_i_am_doing);
344 log_msg(2, "Executing %s", basic_call);
345
346 if (!(fin = popen(command, "r"))) {
347 log_OS_error("Unable to popen-in command");
348 log_to_screen("Failed utterly to call '%s'", command);
349 mr_free(command);
350 mr_free(lockfile);
351 return (1);
352 }
353 mr_free(command);
354
355 if (!does_file_exist(lockfile)) {
356 log_to_screen("Waiting for external binary to start");
357 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
358 log_msg(3, "Waiting for lockfile %s to exist", lockfile);
359 }
360 }
361
362 for (; does_file_exist(lockfile); sleep(1)) {
363 log_file_end_to_screen(MONDO_LOGFILE, "");
364 update_evalcall_form(1);
365 }
366
367 /* Evaluate the status returned by pclose to get the exit code of the called program. */
368 errno = 0;
369 res = pclose(fin);
370 /* Log actual pclose errors. */
371 if (errno) log_msg(5, "pclose err: %d", errno);
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)) {
375 retval = WEXITSTATUS(res);
376 } else {
377 retval = -1;
378 }
379 close_evalcall_form();
380 unlink(lockfile);
381 mr_free(lockfile);
382
383 return (retval);
384}
385
386
387
388
389/**
390 * Apparently unused. @bug This has a purpose, but what?
391 */
392#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
393#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
394
395
396
397
398int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
399{
400// if dir=='w' then copy from orig to archived
401// if dir=='r' then copy from archived to orig
402 char *tmp = NULL;
403 char *tmp1 = NULL;
404 char *buf = NULL;
405 char *filestr = NULL;
406 long int bytes_to_be_read, bytes_read_in, bytes_written_out =
407 0, bufcap, subsliceno = 0;
408 int retval = 0;
409 FILE *fin;
410 FILE *fout;
411 FILE *ftmp;
412 int tmpcap = 512;
413
414 log_msg(5, "Opening.");
415
416 bufcap = 256L * 1024L;
417 if (!(buf = malloc(bufcap))) {
418 fatal_error("Failed to malloc() buf");
419 }
420
421 if (direction == 'w') {
422 fin = f_orig;
423 fout = f_archived;
424 mr_asprintf(tmp, "%-64s", PIMP_START_SZ);
425 if (fwrite(tmp, 1, 64, fout) != 64) {
426 mr_free(tmp);
427 fatal_error("Can't write the introductory block");
428 }
429 mr_free(tmp);
430
431 while (1) {
432 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
433 if (bytes_read_in == 0) {
434 break;
435 }
436 mr_asprintf(tmp, "%-64ld", bytes_read_in);
437 if (fwrite(tmp, 1, 64, fout) != 64) {
438 mr_free(tmp);
439 fatal_error("Cannot write introductory block");
440 }
441 mr_free(tmp);
442
443 log_msg(7,
444 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
445 subsliceno, bytes_read_in, bytes_to_be_read);
446 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
447 mr_asprintf(tmp, "%-64ld", subsliceno);
448 if (fwrite(tmp, 1, 64, fout) != 64) {
449 mr_free(tmp);
450 fatal_error("Cannot write post-thingy block");
451 }
452 mr_free(tmp);
453 log_msg(7, "Subslice #%d written OK", subsliceno);
454 subsliceno++;
455 }
456 mr_asprintf(tmp, "%-64ld", 0L);
457 if (fwrite(tmp, 1, 64L, fout) != 64L) {
458 mr_free(tmp);
459 fatal_error("Cannot write final introductory block");
460 }
461 mr_free(tmp);
462
463 mr_asprintf(tmp, "%-64s", PIMP_END_SZ);
464 if (fwrite(tmp, 1, 64, fout) != 64) {
465 mr_free(tmp);
466 fatal_error("Can't write the final block");
467 }
468 mr_free(tmp);
469 } else {
470 fin = f_archived;
471 fout = f_orig;
472 if (!(tmp1 = malloc(tmpcap))) {
473 fatal_error("Failed to malloc() tmp");
474 }
475 if (fread(tmp1, 1, 64L, fin) != 64L) {
476 mr_free(tmp1);
477 fatal_error("Cannot read the introductory block");
478 }
479 log_msg(5, "tmp1 is %s", tmp1);
480 if (!strstr(tmp1, PIMP_START_SZ)) {
481 mr_free(tmp1);
482 fatal_error("Can't find intro blk");
483 }
484 if (fread(tmp1, 1, 64L, fin) != 64L) {
485 mr_free(tmp1);
486 fatal_error("Cannot read introductory blk");
487 }
488 bytes_to_be_read = atol(tmp1);
489 while (bytes_to_be_read > 0) {
490 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read);
491 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
492 if (bytes_read_in != bytes_to_be_read) {
493 mr_free(tmp1);
494 fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives.");
495 }
496 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
497 if (fread(tmp1, 1, 64, fin) != 64) {
498 mr_free(tmp1);
499 fatal_error("Cannot read post-thingy block");
500 }
501 if (atol(tmp1) != subsliceno) {
502 log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp1), tmp1);
503 }
504 log_msg(7, "Subslice #%ld read OK", subsliceno);
505 subsliceno++;
506 if (fread(tmp1, 1, 64, fin) != 64) {
507 mr_free(tmp1);
508 fatal_error("Cannot read introductory block");
509 }
510 bytes_to_be_read = atol(tmp1);
511 }
512 log_msg(1, "tmpA is %s", tmp1);
513 if (!strstr(tmp1, PIMP_END_SZ)) {
514 if (fread(tmp1, 1, 64, fin) != 64) {
515 mr_free(tmp1);
516 fatal_error("Can't read the final block");
517 }
518 log_msg(5, "tmpB is %s", tmp1);
519 if (!strstr(tmp1, PIMP_END_SZ)) {
520 mr_asprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
521 ftmp = fopen(filestr, "w");
522 mr_free(filestr);
523
524 bytes_read_in = fread(tmp1, 1, 64, fin);
525 log_msg(1, "bytes_read_in = %ld", bytes_read_in);
526
527 fwrite(tmp1, 1, bytes_read_in, ftmp);
528 fread(tmp1, 1, tmpcap, fin);
529 log_msg(0, "tmp1 = '%s'", tmp1);
530 fwrite(tmp1, 1, tmpcap, ftmp);
531 fclose(ftmp);
532 mr_free(tmp1);
533 fatal_error("Missing terminating block");
534 }
535 }
536 mr_free(tmp1);
537 }
538
539 paranoid_free(buf);
540 log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
541 return (retval);
542}
543
544
545
546
547/**
548 * Feed @p input_device through ntfsclone to @p output_fname.
549 * @param input_device The device to image.
550 * @param output_fname The file to write.
551 * @return 0 for success, nonzero for failure.
552 */
553int feed_into_ntfsprog(char *input_device, char *output_fname)
554{
555// BACKUP
556 int res = -1;
557 char *command = NULL;
558
559 if (!does_file_exist(input_device)) {
560 fatal_error("input device does not exist");
561 }
562 command = find_home_of_exe("ntfsclone");
563 if (!command) {
564 mr_free(command);
565 fatal_error("ntfsclone not found");
566 }
567 mr_free(command);
568
569 mr_asprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
570 res = run_program_and_log_output(command, 5);
571 mr_free(command);
572
573 unlink(output_fname);
574 return (res);
575}
576
577
578void *run_prog_in_bkgd_then_exit(void *info)
579{
580 char *sz_command;
581 static int res = 4444;
582
583 res = 999;
584 sz_command = (char *) info;
585 log_msg(4, "sz_command = '%s'", sz_command);
586 res = system(sz_command);
587 if (res > 256 && res != 4444) {
588 res = res / 256;
589 }
590 log_msg(4, "child res = %d", res);
591 sz_command[0] = '\0';
592 pthread_exit((void *) (&res));
593}
594
595
596
597
598int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) {
599
600 /*@ int *************************************************************** */
601 int res = 0;
602 int percentage = 0;
603 int maxpc = 100;
604 int pcno = 0;
605 int last_pcno = 0;
606 int counter = 0;
607
608 /*@ buffers *********************************************************** */
609 char *command = NULL;
610 char *title = NULL;
611 /*@ pointers ********************************************************** */
612 static int chldres = 0;
613 int *pchild_result;
614 pthread_t childthread;
615
616 pchild_result = &chldres;
617 assert_string_is_neither_NULL_nor_zerolength(cmd);
618 assert_string_is_neither_NULL_nor_zerolength(tt);
619 *pchild_result = 999;
620
621 mr_asprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
622 log_msg(3, "command = '%s'", command);
623 if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void *) command))) {
624 fatal_error("Unable to create an archival thread");
625 }
626
627 log_msg(8, "Parent running");
628 mr_asprintf(title, "%s", tt);
629 open_evalcall_form(title);
630 mr_free(title);
631
632 for (sleep(1); command[0] != '\0'; sleep(1)) {
633 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
634 if (pcno < 0 || pcno > 100) {
635 log_msg(8, "Weird pc# %d", pcno);
636 continue;
637 }
638 percentage = pcno * 100 / maxpc;
639 if (pcno <= 5 && last_pcno >= 40) {
640 close_evalcall_form();
641 mr_asprintf(title, "Verifying...");
642 open_evalcall_form(title);
643 mr_free(title);
644 }
645 if (counter++ >= 5) {
646 counter = 0;
647 log_file_end_to_screen(MONDO_LOGFILE, "");
648 }
649 last_pcno = pcno;
650 update_evalcall_form(percentage);
651 }
652 mr_free(command);
653
654 log_file_end_to_screen(MONDO_LOGFILE, "");
655 close_evalcall_form();
656 pthread_join(childthread, (void *) (&pchild_result));
657 if (pchild_result) {
658 res = *pchild_result;
659 } else {
660 res = 666;
661 }
662 log_msg(3, "Parent res = %d", res);
663 return (res);
664}
665
666
667/**
668 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
669 * @param input_fifo The ntfsclone file to read.
670 * @param output_device Where to put the output.
671 * @return The return value of ntfsclone (0 for success).
672 */
673int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
674{
675// RESTORE
676 int res = -1;
677 char *command = NULL;
678
679 command = find_home_of_exe("ntfsclone");
680 if (!command) {
681 mr_free(command);
682 fatal_error("ntfsclone not found");
683 }
684 mr_free(command);
685
686 mr_asprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
687 res = run_program_and_log_output(command, 5);
688 mr_free(command);
689 return (res);
690}
Note: See TracBrowser for help on using the repository browser.