source: MondoRescue/branches/3.1/mondo/src/common/libmondo-fork.c@ 3148

Last change on this file since 3148 was 3148, checked in by Bruno Cornec, 11 years ago

2nd phase for svn merge -r 2935:3146 ../3.0

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