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