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

Last change on this file since 2937 was 2937, checked in by Bruno Cornec, 9 years ago

svn merge -r 2849:2935 ../3.0

  • Adds 3 binaries called potentially by udev o support USB key mount at restore time (Victor Gattegno)
  • mindi only sets the boot flag of a USB device if none is present already (should avoid flip/flop situation)
  • Fix a compatibility bug in analyze-my-lvm whete exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Precise that -J is not supported by mondorestore yet.
  • Fix a compatibility bug in mindi where exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Install an MBR on USB devices when possible
  • Improve error message in case mondorestore isn't found
  • Adds support for more virtio devices for KVM restore
  • Improve mbr.bin handling
  • Fix an issue in test of FindKernel? for Xen and non Xen cases
    • Fix #492 by removing an extra useless fi
    • Move fvat nd fat modules to CDROM_MODS in order to have them in the initrd to support saving logs to a USB device e.g.
    • Fix an error when dmidecode reports comments before system name
    • Fix previous broken patch on comment management for bkphw
    • Improve msgs printed during kbd analysis
    • Fix a small remaining pb with new comment support
    • Try to again improve the mount/umount stuff in mondo -Adds mention to OEL, CentOS, ScientifLinux? as RHEL clones
  • Fix a panadoid_free done on the wrong variable (solve a memory leak)
  • Solve #495 by skipping the potential (auto-read-only) when analyzin /proc/mdstat result
  • Fix some markup in man pages
  • adds the post-nuke script sample for Debian (Guillaume Pernot/Andrée? Leidenfrost)
  • Avoid a useless umount when changing of afio set, let insist_on_cd check and perform if needed
  • Copy everything under .ssh of root account to allow for sshfs mount without passwd at restore time.
  • Adds support for bnx2i driver
  • Try to fix #496 by supporting the protocol in the output of the mount command (sshfs#...)
  • Uses the -d option of the mount command to avoid leaving loop devices in use. Probably provoked by the usage of a more recent busybox, but anyway that option doesn't hurt, and does nothing for non-loop devices. Should fix the problems reported on the ML with NFS and more than 8 ISO images (8 being the default number of loop devices available)
  • Attempt to fix #436 by adding some newtDrawForm where appropriate.
  • Fix bugs in the kernel detection routine of mindi (some echo were done without capturing their output, confusing the return). This should also help the Xen detection issue
  • Change the logging function to be nearer from 2.2.10, and start using some
  • Initialize some global fields with an empty string, now that newt Draws more regularly progress forms
  • Mindi progression still not good, but showing now 3% when calling mindi. Would need a specific handling for the progress bar to evolve more afterwards
  • Add compatibility with the latest SSSTK + PSP content for HP ProLiant? + branding homogeneous now. We only take conrep out of the SSSTK pending a package availability. Using hpacuscripting now. Review of parameters called following test made.
  • Fix #501 by pushing BOOT_SIZE to 36864 for certain Ubuntu cases
  • Fix some remaining inconsistencis in the ProLiant? HW backup. ready for usage.
  • Fix an issue on udevadm call (needs an intermediate variable)
  • Fix a remaining ProLiant? tool call
  • Remove again some wrong variable initialization for Net in order to not have strange messages now we call Draw more often
  • Adds support for hwaddr (MAC addresses) at restore time, and map to the correct NIC if it's found at restore time (based on code from Stefan Heijmans)
  • Try to Fix #499 by changing init in order to never exit, and rebooting using sysrq in /proc (not portable). Maybe should move to using a real init, and a separated rcS script.
  • Fixes the shell msg error by using setsid as per Busybox FAQ
  • Fix #491 by correctly checking Xen kernel used for a dom0 env (apollo13)
  • Improves logging in Die()
  • Improve again ProLiant? HW support, by adding references to the yet to come SSSTK rpm so that conrep is called at its future right place.
  • Improve mindi support for kernel 3.x
  • Fix #499 for good, by using a real init from busybox and moving our init script to etc/init.d/rcS. We also now call the reboot from busybox to exit, which indeed reset the system correctly (in a VM at least which wasn't the case before). Reauires a new mindi-busybox as well.
  • Attempt to solve #503 by handling /lib64 separately for udev and only if not a link (to /lib) for Ubuntu 64
  • Improve again previous tests for bug #503
  • Improve -d option description for NFS
  • Allow http download for ftp content
  • New italian mirror
  • Fix #505 by documenting environment variables
    • Fix a bug where on OBDR mode compression was turned off to create the OBDR content but not back on (report from the mailing list)
  • solve http://trac.mondorescue.org/ticket/520 by Victor Gattegno
  • Fix #524 as option -U needs a size also
  • Removes useless gendf option
  • Fix #511 now stabgrub is called also after conf file modifications
  • Adds support for SUID binaries
  • remove a useless xenkernelname variable from mindi
  • Should fix #512 by clearing old scratchdir before changing to a new
  • Simplify the function engaging SCSI for Smart Array controllers, and support as many controller as we have
  • Add some binaries used on SLES 10 by udev
  • Fix a syntax issue in newt-specific.c
  • Fix a memory free bug where a used variable was freed to early
  • Fix a memory free bug where a used variable was misused in asprintf/free
  • Fix #498 by using an external program (hpsa_obdr_mode) which can set the mode of the tape to CD-ROM or Sequential at will.

That program will have to be downloaded from http://cciss.sf.net

  • Adds support for the route command in mindi-busybox
  • Force block size back to 32768 in OBDR mode after skipping the 2 tape marks, to read the additional rootfs data corerctly - Fix #498 for good it seems
  • Adds support for a new pata_atiixp driver
  • Fix #525 and improve printing when no /etc/raidtab, but still md devices
  • Exclude /run similarly to /sys and /proc
  • Fix a bug in mindi when having multiple swap partiions with same starting string (/dev/dm-1 and /dev/dm-16). We now add a space in the grep to avoid mischoice.
  • Fix grub installation if we had to go in the hack part which wasn't using the correct variables to make decisions !
  • Remove all mention of mondorescue.com from code
  • Fix some website typo on the download page and precise that we now develop on Mageia
  • Exclude sysfs devpts and tmpfs partitions found in RHEL 6.2 from analyze by mindi (made error messages)
  • Force activation of first partition which should not be optional as everything is destroyed before
  • Fix a bug in analyze-my-lvm where LVs were created in random order (the one of lvscan) instead of using the dm-xxx order, which creates issues at restore time between the mapping created then and the one we had at backup time which could be inconssistent

-mondoarchive now exists if the mt command is missing when needed

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