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