[130] | 1 | /* $Id: libmondo-fork.c 147 2005-12-01 09:26:01Z bcornec $
|
---|
| 2 | subroutines for handling forking/pthreads/etc.
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | #include "my-stuff.h"
|
---|
| 7 | #include "mondostructures.h"
|
---|
| 8 | #include "libmondo-fork.h"
|
---|
| 9 | #include "libmondo-string-EXT.h"
|
---|
| 10 | #include "libmondo-gui-EXT.h"
|
---|
| 11 | #include "libmondo-files-EXT.h"
|
---|
| 12 | #include "libmondo-tools-EXT.h"
|
---|
| 13 | #include "lib-common-externs.h"
|
---|
| 14 |
|
---|
| 15 | /*@unused@*/
|
---|
[59] | 16 | //static char cvsid[] = "$Id: libmondo-fork.c 147 2005-12-01 09:26:01Z bcornec $";
|
---|
[1] | 17 |
|
---|
| 18 | extern char *g_tmpfs_mountpt;
|
---|
| 19 | extern t_bkptype g_backup_media_type;
|
---|
| 20 | extern bool g_text_mode;
|
---|
[59] | 21 | pid_t g_buffer_pid = 0;
|
---|
[1] | 22 |
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Call a program and retrieve its last line of output.
|
---|
| 26 | * @param call The program to run.
|
---|
| 27 | * @return The last line of its output.
|
---|
| 28 | * @note The returned value points to static storage that will be overwritten with each call.
|
---|
| 29 | */
|
---|
[59] | 30 | char *call_program_and_get_last_line_of_output(char *call)
|
---|
[1] | 31 | {
|
---|
[59] | 32 | /*@ buffers ***************************************************** */
|
---|
[130] | 33 | static char *result = NULL;
|
---|
| 34 | char *tmp = NULL;
|
---|
[1] | 35 |
|
---|
[59] | 36 | /*@ pointers **************************************************** */
|
---|
| 37 | FILE *fin;
|
---|
[1] | 38 |
|
---|
[130] | 39 | int n = 0;
|
---|
[1] | 40 |
|
---|
[59] | 41 | /*@******************************************************************** */
|
---|
[1] | 42 |
|
---|
[59] | 43 | assert_string_is_neither_NULL_nor_zerolength(call);
|
---|
| 44 | if ((fin = popen(call, "r"))) {
|
---|
[130] | 45 | for (getline(&tmp, &n, fin); !feof(fin);
|
---|
| 46 | getline(&tmp, &n, fin)) {
|
---|
[59] | 47 | if (strlen(tmp) > 1) {
|
---|
[130] | 48 | if (result != NULL) {
|
---|
| 49 | paranoid_free(result);
|
---|
| 50 | }
|
---|
| 51 | asprintf(&result, tmp);
|
---|
[59] | 52 | }
|
---|
| 53 | }
|
---|
| 54 | paranoid_pclose(fin);
|
---|
| 55 | } else {
|
---|
| 56 | log_OS_error("Unable to popen call");
|
---|
[1] | 57 | }
|
---|
[59] | 58 | strip_spaces(result);
|
---|
[130] | 59 | paranoid_free(tmp);
|
---|
[59] | 60 | return (result);
|
---|
[1] | 61 | }
|
---|
| 62 |
|
---|
| 63 | #define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * Call mkisofs to create an ISO image.
|
---|
| 67 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 68 | * - @c bkpinfo->manual_cd_tray
|
---|
| 69 | * - @c bkpinfo->backup_media_type
|
---|
| 70 | * - @c bkpinfo->please_dont_eject_when_restoring
|
---|
| 71 | * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
|
---|
| 72 | * - @c _ISO_ will become the ISO file (@p isofile)
|
---|
| 73 | * - @c _CD#_ becomes the CD number (@p cd_no)
|
---|
| 74 | * - @c _ERR_ becomes the logfile (@p g_logfile)
|
---|
| 75 | * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
|
---|
| 76 | * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
|
---|
| 77 | * @param logstub Unused.
|
---|
| 78 | * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
|
---|
| 79 | * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
|
---|
| 80 | * @bug @p logstub is unused.
|
---|
| 81 | */
|
---|
| 82 | int
|
---|
[59] | 83 | eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
|
---|
| 84 | char *basic_call, char *isofile,
|
---|
| 85 | int cd_no, char *logstub, char *what_i_am_doing)
|
---|
[1] | 86 | {
|
---|
| 87 |
|
---|
[59] | 88 | /*@ int's *** */
|
---|
| 89 | int retval = 0;
|
---|
[1] | 90 |
|
---|
| 91 |
|
---|
[59] | 92 | /*@ buffers *** */
|
---|
[130] | 93 | char *midway_call, *ultimate_call, *tmp, *command,
|
---|
| 94 | *cd_number_str;
|
---|
[59] | 95 | char *p;
|
---|
[1] | 96 |
|
---|
| 97 | /*@*********** End Variables ***************************************/
|
---|
| 98 |
|
---|
[59] | 99 | log_msg(3, "Starting");
|
---|
| 100 | assert(bkpinfo != NULL);
|
---|
| 101 | assert_string_is_neither_NULL_nor_zerolength(basic_call);
|
---|
| 102 | assert_string_is_neither_NULL_nor_zerolength(isofile);
|
---|
| 103 | assert_string_is_neither_NULL_nor_zerolength(logstub);
|
---|
| 104 | if (!(midway_call = malloc(1200))) {
|
---|
| 105 | fatal_error("Cannot malloc midway_call");
|
---|
| 106 | }
|
---|
| 107 | if (!(ultimate_call = malloc(1200))) {
|
---|
| 108 | fatal_error("Cannot malloc ultimate_call");
|
---|
| 109 | }
|
---|
| 110 | if (!(tmp = malloc(1200))) {
|
---|
| 111 | fatal_error("Cannot malloc tmp");
|
---|
| 112 | }
|
---|
[1] | 113 |
|
---|
[130] | 114 | asprintf(&cd_number_str, "%d", cd_no);
|
---|
[59] | 115 | resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
|
---|
| 116 | resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
|
---|
[130] | 117 | paranoid_free(cd_number_str);
|
---|
| 118 |
|
---|
[59] | 119 | resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
|
---|
| 120 | log_msg(4, "basic call = '%s'", basic_call);
|
---|
| 121 | log_msg(4, "midway_call = '%s'", midway_call);
|
---|
| 122 | log_msg(4, "tmp = '%s'", tmp);
|
---|
| 123 | log_msg(4, "ultimate call = '%s'", ultimate_call);
|
---|
[130] | 124 | asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
|
---|
[1] | 125 |
|
---|
[59] | 126 | log_to_screen
|
---|
| 127 | ("Please be patient. Do not be alarmed by on-screen inactivity.");
|
---|
| 128 | log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
|
---|
| 129 | what_i_am_doing);
|
---|
| 130 | strcpy(tmp, command);
|
---|
| 131 | if (bkpinfo->manual_cd_tray) {
|
---|
| 132 | p = strstr(tmp, "2>>");
|
---|
| 133 | if (p) {
|
---|
| 134 | sprintf(p, " ");
|
---|
| 135 | while (*p == ' ') {
|
---|
| 136 | p++;
|
---|
| 137 | }
|
---|
| 138 | for (; *p != ' '; p++) {
|
---|
| 139 | *p = ' ';
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
[130] | 142 | paranoid_free(command);
|
---|
| 143 | asprintf(&command, tmp);
|
---|
[1] | 144 | #ifndef _XWIN
|
---|
[59] | 145 | if (!g_text_mode) {
|
---|
| 146 | newtSuspend();
|
---|
| 147 | }
|
---|
[1] | 148 | #endif
|
---|
[59] | 149 | log_msg(1, "command = '%s'", command);
|
---|
| 150 | retval += system(command);
|
---|
| 151 | if (!g_text_mode) {
|
---|
| 152 | newtResume();
|
---|
| 153 | }
|
---|
| 154 | if (retval) {
|
---|
| 155 | log_msg(2, "Basic call '%s' returned an error.", basic_call);
|
---|
| 156 | popup_and_OK("Press ENTER to continue.");
|
---|
| 157 | popup_and_OK
|
---|
| 158 | ("mkisofs and/or cdrecord returned an error. CD was not created");
|
---|
| 159 | }
|
---|
[1] | 160 | }
|
---|
[59] | 161 | /* if text mode then do the above & RETURN; if not text mode, do this... */
|
---|
| 162 | else {
|
---|
| 163 | log_msg(3, "command = '%s'", command);
|
---|
[1] | 164 | // yes_this_is_a_goto:
|
---|
[59] | 165 | retval =
|
---|
| 166 | run_external_binary_with_percentage_indicator_NEW
|
---|
| 167 | (what_i_am_doing, command);
|
---|
| 168 | }
|
---|
[130] | 169 | paranoid_free(command);
|
---|
[1] | 170 |
|
---|
[59] | 171 | paranoid_free(midway_call);
|
---|
| 172 | paranoid_free(ultimate_call);
|
---|
| 173 | paranoid_free(tmp);
|
---|
| 174 | return (retval);
|
---|
[1] | 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Run a program and log its output (stdout and stderr) to the logfile.
|
---|
| 180 | * @param program The program to run. Passed to the shell, so you can use pipes etc.
|
---|
| 181 | * @param debug_level If @p g_loglevel is higher than this, do not log the output.
|
---|
| 182 | * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
|
---|
| 183 | */
|
---|
[59] | 184 | int run_program_and_log_output(char *program, int debug_level)
|
---|
[1] | 185 | {
|
---|
[59] | 186 | /*@ buffer ****************************************************** */
|
---|
[130] | 187 | char *callstr;
|
---|
| 188 | char *incoming = NULL;
|
---|
[59] | 189 | char tmp[MAX_STR_LEN * 2];
|
---|
[1] | 190 |
|
---|
[59] | 191 | /*@ int ********************************************************* */
|
---|
| 192 | int res;
|
---|
| 193 | int i;
|
---|
[130] | 194 | int n = 0;
|
---|
[59] | 195 | int len;
|
---|
| 196 | bool log_if_failure = FALSE;
|
---|
| 197 | bool log_if_success = FALSE;
|
---|
[1] | 198 |
|
---|
[59] | 199 | /*@ pointers *************************************************** */
|
---|
| 200 | FILE *fin;
|
---|
| 201 | char *p;
|
---|
[1] | 202 |
|
---|
[59] | 203 | /*@ end vars *************************************************** */
|
---|
[1] | 204 |
|
---|
[59] | 205 | assert(program != NULL);
|
---|
| 206 | if (!program[0]) {
|
---|
| 207 | log_msg(2, "Warning - asked to run zerolength program");
|
---|
| 208 | return (1);
|
---|
| 209 | }
|
---|
[1] | 210 |
|
---|
[59] | 211 | if (debug_level <= g_loglevel) {
|
---|
| 212 | log_if_success = TRUE;
|
---|
| 213 | log_if_failure = TRUE;
|
---|
| 214 | }
|
---|
[130] | 215 | asprintf(&callstr,
|
---|
[59] | 216 | "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
|
---|
| 217 | program);
|
---|
| 218 | while ((p = strchr(callstr, '\r'))) {
|
---|
| 219 | *p = ' ';
|
---|
| 220 | }
|
---|
| 221 | while ((p = strchr(callstr, '\n'))) {
|
---|
| 222 | *p = ' ';
|
---|
| 223 | } /* single '=' is intentional */
|
---|
[1] | 224 |
|
---|
| 225 |
|
---|
[59] | 226 | len = (int) strlen(program);
|
---|
| 227 | for (i = 0; i < 35 - len / 2; i++) {
|
---|
| 228 | tmp[i] = '-';
|
---|
| 229 | }
|
---|
| 230 | tmp[i] = '\0';
|
---|
| 231 | strcat(tmp, " ");
|
---|
| 232 | strcat(tmp, program);
|
---|
| 233 | strcat(tmp, " ");
|
---|
| 234 | for (i = 0; i < 35 - len / 2; i++) {
|
---|
| 235 | strcat(tmp, "-");
|
---|
| 236 | }
|
---|
| 237 | res = system(callstr);
|
---|
| 238 | if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
|
---|
| 239 | log_msg(0, "running: %s", callstr);
|
---|
| 240 | log_msg(0,
|
---|
| 241 | "--------------------------------start of output-----------------------------");
|
---|
| 242 | }
|
---|
[130] | 243 | paranoid_free(callstr);
|
---|
| 244 |
|
---|
[59] | 245 | if (log_if_failure
|
---|
| 246 | &&
|
---|
| 247 | system
|
---|
| 248 | ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
|
---|
[1] | 249 | {
|
---|
[59] | 250 | log_OS_error("Command failed");
|
---|
| 251 | }
|
---|
| 252 | unlink("/tmp/mondo-run-prog-thing.err");
|
---|
| 253 | fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
|
---|
| 254 | if (fin) {
|
---|
[130] | 255 | for (getline(&incoming, &n, fin); !feof(fin);
|
---|
| 256 | getline(&incoming, &n, fin)) {
|
---|
[59] | 257 | /* patch by Heiko Schlittermann */
|
---|
| 258 | p = incoming;
|
---|
| 259 | while (p && *p) {
|
---|
| 260 | if ((p = strchr(p, '%'))) {
|
---|
| 261 | memmove(p, p + 1, strlen(p) + 1);
|
---|
| 262 | p += 2;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | /* end of patch */
|
---|
| 266 | strip_spaces(incoming);
|
---|
| 267 | if ((res == 0 && log_if_success)
|
---|
| 268 | || (res != 0 && log_if_failure)) {
|
---|
| 269 | log_msg(0, incoming);
|
---|
| 270 | }
|
---|
[1] | 271 | }
|
---|
[130] | 272 | paranoid_free(incoming);
|
---|
[59] | 273 | paranoid_fclose(fin);
|
---|
[1] | 274 | }
|
---|
[59] | 275 | unlink("/tmp/mondo-run-prog-thing.tmp");
|
---|
| 276 | if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
|
---|
| 277 | log_msg(0,
|
---|
| 278 | "--------------------------------end of output------------------------------");
|
---|
| 279 | if (res) {
|
---|
| 280 | log_msg(0, "...ran with res=%d", res);
|
---|
| 281 | } else {
|
---|
| 282 | log_msg(0, "...ran just fine. :-)");
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
[1] | 285 | // else
|
---|
| 286 | // { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
|
---|
[59] | 287 | return (res);
|
---|
[1] | 288 | }
|
---|
| 289 |
|
---|
| 290 |
|
---|
| 291 | /**
|
---|
| 292 | * Run a program and log its output to the screen.
|
---|
| 293 | * @param basic_call The program to run.
|
---|
| 294 | * @param what_i_am_doing The title of the evalcall form.
|
---|
| 295 | * @return The return value of the command (varies, but 0 almost always means success).
|
---|
| 296 | * @see run_program_and_log_output
|
---|
| 297 | * @see log_to_screen
|
---|
| 298 | */
|
---|
[59] | 299 | int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
|
---|
[1] | 300 | {
|
---|
[59] | 301 | /*@ int ******************************************************** */
|
---|
| 302 | int retval = 0;
|
---|
| 303 | int res = 0;
|
---|
| 304 | int i;
|
---|
[1] | 305 |
|
---|
[59] | 306 | /*@ pointers **************************************************** */
|
---|
| 307 | FILE *fin;
|
---|
[1] | 308 |
|
---|
[59] | 309 | /*@ buffers **************************************************** */
|
---|
[130] | 310 | char *tmp;
|
---|
| 311 | char *command;
|
---|
| 312 | char *lockfile;
|
---|
[1] | 313 |
|
---|
[59] | 314 | /*@ end vars *************************************************** */
|
---|
[1] | 315 |
|
---|
[59] | 316 | assert_string_is_neither_NULL_nor_zerolength(basic_call);
|
---|
[1] | 317 |
|
---|
[130] | 318 | asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
|
---|
[59] | 319 | mkstemp(lockfile);
|
---|
[130] | 320 | asprintf(&command,
|
---|
[59] | 321 | "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
|
---|
| 322 | lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
|
---|
| 323 | open_evalcall_form(what_i_am_doing);
|
---|
[130] | 324 | asprintf(&tmp, "Executing %s", basic_call);
|
---|
[59] | 325 | log_msg(2, tmp);
|
---|
[130] | 326 | paranoid_free(tmp);
|
---|
| 327 |
|
---|
[59] | 328 | if (!(fin = popen(command, "r"))) {
|
---|
| 329 | log_OS_error("Unable to popen-in command");
|
---|
[130] | 330 | asprintf(&tmp, "Failed utterly to call '%s'", command);
|
---|
[59] | 331 | log_to_screen(tmp);
|
---|
[130] | 332 | paranoid_free(tmp);
|
---|
| 333 | paranoid_free(lockfile);
|
---|
| 334 | paranoid_free(command);
|
---|
[59] | 335 | return (1);
|
---|
| 336 | }
|
---|
[130] | 337 | paranoid_free(command);
|
---|
| 338 |
|
---|
[59] | 339 | if (!does_file_exist(lockfile)) {
|
---|
| 340 | log_to_screen("Waiting for external binary to start");
|
---|
| 341 | for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
|
---|
| 342 | log_msg(3, "Waiting for lockfile %s to exist", lockfile);
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 | /* This works on Newt, and it gives quicker updates. */
|
---|
| 346 | for (; does_file_exist(lockfile); sleep(1)) {
|
---|
| 347 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 348 | update_evalcall_form(1);
|
---|
| 349 | }
|
---|
[68] | 350 | /* Evaluate the status returned by pclose to get the exit code of the called program. */
|
---|
| 351 | errno = 0;
|
---|
| 352 | res = pclose(fin);
|
---|
| 353 | /* Log actual pclose errors. */
|
---|
[89] | 354 | if (errno)
|
---|
| 355 | log_msg(5, "pclose err: %d", errno);
|
---|
[68] | 356 | /* Check if we have a valid status. If we do, extract the called program's exit code. */
|
---|
| 357 | /* If we don't, highlight this fact by returning -1. */
|
---|
| 358 | if (WIFEXITED(res)) {
|
---|
[89] | 359 | retval = WEXITSTATUS(res);
|
---|
[68] | 360 | } else {
|
---|
[89] | 361 | retval = -1;
|
---|
[68] | 362 | }
|
---|
[59] | 363 | close_evalcall_form();
|
---|
| 364 | unlink(lockfile);
|
---|
[130] | 365 | paranoid_free(lockfile);
|
---|
| 366 |
|
---|
[59] | 367 | return (retval);
|
---|
[1] | 368 | }
|
---|
| 369 |
|
---|
| 370 |
|
---|
| 371 | /**
|
---|
| 372 | * Thread callback to run a command (partimage) in the background.
|
---|
| 373 | * @param xfb A transfer block of @c char, containing:
|
---|
| 374 | * - xfb:[0] A marker, should be set to 2. Decremented to 1 while the command is running and 0 when it's finished.
|
---|
| 375 | * - xfb:[1] The command's return value, if xfb:[0] is 0.
|
---|
| 376 | * - xfb+2: A <tt>NULL</tt>-terminated string containing the command to be run.
|
---|
| 377 | * @return NULL to pthread_join.
|
---|
| 378 | */
|
---|
[59] | 379 | void *call_partimage_in_bkgd(void *xfb)
|
---|
[1] | 380 | {
|
---|
[59] | 381 | char *transfer_block;
|
---|
| 382 | int retval = 0;
|
---|
[1] | 383 |
|
---|
[59] | 384 | g_buffer_pid = getpid();
|
---|
| 385 | unlink("/tmp/null");
|
---|
| 386 | log_msg(1, "starting");
|
---|
| 387 | transfer_block = (char *) xfb;
|
---|
| 388 | transfer_block[0]--; // should now be 1
|
---|
| 389 | retval = system(transfer_block + 2);
|
---|
| 390 | if (retval) {
|
---|
| 391 | log_OS_error("partimage returned an error");
|
---|
| 392 | }
|
---|
| 393 | transfer_block[1] = retval;
|
---|
| 394 | transfer_block[0]--; // should now be 0
|
---|
| 395 | g_buffer_pid = 0;
|
---|
| 396 | log_msg(1, "returning");
|
---|
| 397 | pthread_exit(NULL);
|
---|
[1] | 398 | }
|
---|
| 399 |
|
---|
| 400 |
|
---|
| 401 | /**
|
---|
| 402 | * File to touch if we want partimage to wait for us.
|
---|
| 403 | */
|
---|
| 404 | #define PAUSE_PARTIMAGE_FNAME "/tmp/PAUSE-PARTIMAGE-FOR-MONDO"
|
---|
| 405 |
|
---|
| 406 | /**
|
---|
[130] | 407 | * Apparently used. @bug This has a purpose, but what?
|
---|
[1] | 408 | */
|
---|
| 409 | #define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
|
---|
| 410 | #define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
|
---|
| 411 |
|
---|
| 412 | /**
|
---|
| 413 | * Marker to start the next subvolume for Partimage.
|
---|
| 414 | */
|
---|
| 415 | #define NEXT_SUBVOL_PLEASE "I-grew-up-on-the-crime-side,-the-New-York-Times-side,-where-staying-alive-was-no-jive"
|
---|
| 416 |
|
---|
| 417 | /**
|
---|
| 418 | * Marker to end the partimage file.
|
---|
| 419 | */
|
---|
| 420 | #define NO_MORE_SUBVOLS "On-second-hand,momma-bounced-on-old-man,-and-so-we-moved-to-Shaolin-Land."
|
---|
| 421 |
|
---|
[59] | 422 | int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
|
---|
[1] | 423 | {
|
---|
| 424 | // if dir=='w' then copy from orig to archived
|
---|
| 425 | // if dir=='r' then copy from archived to orig
|
---|
[59] | 426 | char *tmp;
|
---|
| 427 | char *buf;
|
---|
| 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;
|
---|
[1] | 434 |
|
---|
[59] | 435 | log_msg(5, "Opening.");
|
---|
| 436 | bufcap = 256L * 1024L;
|
---|
| 437 | if (!(buf = malloc(bufcap))) {
|
---|
| 438 | fatal_error("Failed to malloc() buf");
|
---|
| 439 | }
|
---|
[1] | 440 |
|
---|
[59] | 441 | if (direction == 'w') {
|
---|
| 442 | fin = f_orig;
|
---|
| 443 | fout = f_archived;
|
---|
[130] | 444 | asprintf(&tmp, "%-64s", PIMP_START_SZ);
|
---|
[59] | 445 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 446 | fatal_error("Can't write the introductory block");
|
---|
| 447 | }
|
---|
[130] | 448 | paranoid_free(tmp);
|
---|
| 449 |
|
---|
[59] | 450 | while (1) {
|
---|
| 451 | bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
|
---|
| 452 | if (bytes_read_in == 0) {
|
---|
| 453 | break;
|
---|
| 454 | }
|
---|
[130] | 455 | asprintf(&tmp, "%-64ld", bytes_read_in);
|
---|
[59] | 456 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 457 | fatal_error("Cannot write introductory block");
|
---|
| 458 | }
|
---|
[130] | 459 | paranoid_free(tmp);
|
---|
| 460 |
|
---|
[59] | 461 | log_msg(7,
|
---|
| 462 | "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
|
---|
| 463 | subsliceno, bytes_read_in, bytes_to_be_read);
|
---|
| 464 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
[130] | 465 | asprintf(&tmp, "%-64ld", subsliceno);
|
---|
[59] | 466 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 467 | fatal_error("Cannot write post-thingy block");
|
---|
| 468 | }
|
---|
[130] | 469 | paranoid_free(tmp);
|
---|
| 470 |
|
---|
[59] | 471 | log_msg(7, "Subslice #%d written OK", subsliceno);
|
---|
| 472 | subsliceno++;
|
---|
| 473 | }
|
---|
[130] | 474 | asprintf(&tmp, "%-64ld", 0L);
|
---|
[59] | 475 | if (fwrite(tmp, 1, 64L, fout) != 64L) {
|
---|
| 476 | fatal_error("Cannot write final introductory block");
|
---|
| 477 | }
|
---|
| 478 | } else {
|
---|
| 479 | fin = f_archived;
|
---|
| 480 | fout = f_orig;
|
---|
[130] | 481 | if (!(tmp = malloc(64L))) {
|
---|
| 482 | fatal_error("Failed to malloc() tmp");
|
---|
| 483 | }
|
---|
[59] | 484 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
| 485 | fatal_error("Cannot read the introductory block");
|
---|
| 486 | }
|
---|
| 487 | log_msg(5, "tmp is %s", tmp);
|
---|
| 488 | if (!strstr(tmp, PIMP_START_SZ)) {
|
---|
| 489 | fatal_error("Can't find intro blk");
|
---|
| 490 | }
|
---|
| 491 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
| 492 | fatal_error("Cannot read introductory blk");
|
---|
| 493 | }
|
---|
| 494 | bytes_to_be_read = atol(tmp);
|
---|
| 495 | while (bytes_to_be_read > 0) {
|
---|
| 496 | log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
|
---|
| 497 | bytes_to_be_read);
|
---|
| 498 | bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
|
---|
| 499 | if (bytes_read_in != bytes_to_be_read) {
|
---|
| 500 | fatal_error
|
---|
| 501 | ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
|
---|
| 502 | }
|
---|
| 503 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
| 504 | if (fread(tmp, 1, 64, fin) != 64) {
|
---|
| 505 | fatal_error("Cannot read post-thingy block");
|
---|
| 506 | }
|
---|
| 507 | if (atol(tmp) != subsliceno) {
|
---|
| 508 | log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
|
---|
| 509 | subsliceno, atol(tmp), tmp);
|
---|
| 510 | }
|
---|
| 511 | log_msg(7, "Subslice #%ld read OK", subsliceno);
|
---|
| 512 | subsliceno++;
|
---|
| 513 | if (fread(tmp, 1, 64, fin) != 64) {
|
---|
| 514 | fatal_error("Cannot read introductory block");
|
---|
| 515 | }
|
---|
| 516 | bytes_to_be_read = atol(tmp);
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
[1] | 519 |
|
---|
| 520 | // log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
|
---|
| 521 |
|
---|
[59] | 522 | if (direction == 'w') {
|
---|
[130] | 523 | asprintf(&tmp, "%-64s", PIMP_END_SZ);
|
---|
[59] | 524 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 525 | fatal_error("Can't write the final block");
|
---|
| 526 | }
|
---|
[130] | 527 | paranoid_free(tmp);
|
---|
[59] | 528 | } else {
|
---|
| 529 | log_msg(1, "tmpA is %s", tmp);
|
---|
| 530 | if (!strstr(tmp, PIMP_END_SZ)) {
|
---|
| 531 | if (fread(tmp, 1, 64, fin) != 64) {
|
---|
| 532 | fatal_error("Can't read the final block");
|
---|
| 533 | }
|
---|
| 534 | log_msg(5, "tmpB is %s", tmp);
|
---|
| 535 | if (!strstr(tmp, PIMP_END_SZ)) {
|
---|
| 536 | ftmp = fopen("/tmp/out.leftover", "w");
|
---|
| 537 | bytes_read_in = fread(tmp, 1, 64, fin);
|
---|
| 538 | log_msg(1, "bytes_read_in = %ld", bytes_read_in);
|
---|
[1] | 539 | // if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
|
---|
[59] | 540 | fwrite(tmp, 1, bytes_read_in, ftmp);
|
---|
[130] | 541 | paranoid_free(tmp);
|
---|
| 542 |
|
---|
| 543 | if (!(tmp = malloc(512))) {
|
---|
| 544 | fatal_error("Failed to malloc() tmp");
|
---|
| 545 | }
|
---|
[59] | 546 | fread(tmp, 1, 512, fin);
|
---|
| 547 | log_msg(0, "tmp = '%s'", tmp);
|
---|
| 548 | fwrite(tmp, 1, 512, ftmp);
|
---|
| 549 | fclose(ftmp);
|
---|
| 550 | fatal_error("Missing terminating block");
|
---|
| 551 | }
|
---|
| 552 | }
|
---|
[130] | 553 | paranoid_free(tmp);
|
---|
[59] | 554 | }
|
---|
[1] | 555 |
|
---|
[59] | 556 | paranoid_free(buf);
|
---|
| 557 | log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
|
---|
| 558 | return (retval);
|
---|
[1] | 559 | }
|
---|
| 560 |
|
---|
| 561 |
|
---|
| 562 | /**
|
---|
| 563 | * Call partimage from @p input_device to @p output_fname.
|
---|
| 564 | * @param input_device The device to read.
|
---|
| 565 | * @param output_fname The file to write.
|
---|
| 566 | * @return 0 for success, nonzero for failure.
|
---|
| 567 | */
|
---|
[59] | 568 | int dynamically_create_pipes_and_copy_from_them_to_output_file(char
|
---|
[89] | 569 | *input_device, char
|
---|
[59] | 570 | *output_fname)
|
---|
[1] | 571 | {
|
---|
[59] | 572 | char *curr_fifo;
|
---|
[130] | 573 | char *prev_fifo = NULL;
|
---|
[59] | 574 | char *next_fifo;
|
---|
| 575 | char *command;
|
---|
| 576 | char *sz_call_to_partimage;
|
---|
| 577 | int fifo_number = 0;
|
---|
| 578 | struct stat buf;
|
---|
| 579 | pthread_t partimage_thread;
|
---|
| 580 | int res = 0;
|
---|
| 581 | char *tmpstub;
|
---|
| 582 | FILE *fout;
|
---|
| 583 | FILE *fin;
|
---|
| 584 | char *tmp;
|
---|
[1] | 585 |
|
---|
[59] | 586 | log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt);
|
---|
| 587 | if (g_tmpfs_mountpt && g_tmpfs_mountpt[0]
|
---|
| 588 | && does_file_exist(g_tmpfs_mountpt)) {
|
---|
[130] | 589 | asprintf(&tmpstub, g_tmpfs_mountpt);
|
---|
[59] | 590 | } else {
|
---|
[130] | 591 | asprintf(&tmpstub, "/tmp");
|
---|
[59] | 592 | }
|
---|
| 593 | paranoid_system("rm -f /tmp/*PARTIMAGE*");
|
---|
[130] | 594 | asprintf(&command, "rm -Rf %s/pih-fifo-*", tmpstub);
|
---|
[59] | 595 | paranoid_system(command);
|
---|
[130] | 596 | paranoid_free(command);
|
---|
| 597 |
|
---|
| 598 | asprintf(&tmp, "%s/pih-fifo-%ld", tmpstub, (long int) random());
|
---|
| 599 | paranoid_free(tmpstub);
|
---|
| 600 | tmpstub = tmp;
|
---|
| 601 | paranoid_free(tmp);
|
---|
| 602 |
|
---|
[59] | 603 | mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though...
|
---|
[130] | 604 | asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
|
---|
| 605 | asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
|
---|
[59] | 606 | mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
|
---|
| 607 | mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
|
---|
[130] | 608 | asprintf(&sz_call_to_partimage,
|
---|
| 609 | "%c%cpartimagehack " PARTIMAGE_PARAMS
|
---|
| 610 | " save %s %s > /tmp/stdout 2> /tmp/stderr", 2, 0, input_device,
|
---|
[59] | 611 | tmpstub);
|
---|
| 612 | log_msg(5, "curr_fifo = %s", curr_fifo);
|
---|
| 613 | log_msg(5, "next_fifo = %s", next_fifo);
|
---|
| 614 | log_msg(5, "sz_call_to_partimage call is '%s'",
|
---|
| 615 | sz_call_to_partimage + 2);
|
---|
| 616 | if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) {
|
---|
| 617 | log_msg(5, "Deleting %s", output_fname);
|
---|
| 618 | unlink(output_fname);
|
---|
| 619 | }
|
---|
| 620 | if (!(fout = fopen(output_fname, "w"))) {
|
---|
| 621 | fatal_error("Unable to openout to output_fname");
|
---|
| 622 | }
|
---|
| 623 | res =
|
---|
| 624 | pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
|
---|
| 625 | (void *) sz_call_to_partimage);
|
---|
| 626 | if (res) {
|
---|
| 627 | fatal_error("Failed to create thread to call partimage");
|
---|
| 628 | }
|
---|
| 629 | log_msg(1, "Running fore/back at same time");
|
---|
| 630 | log_to_screen("Working with partimagehack...");
|
---|
| 631 | while (sz_call_to_partimage[0] > 0) {
|
---|
[130] | 632 | asprintf(&tmp, "%s\n", NEXT_SUBVOL_PLEASE);
|
---|
[59] | 633 | if (fwrite(tmp, 1, 128, fout) != 128) {
|
---|
| 634 | fatal_error("Cannot write interim block");
|
---|
| 635 | }
|
---|
[130] | 636 | paranoid_free(tmp);
|
---|
| 637 |
|
---|
[59] | 638 | log_msg(5, "fifo_number=%d", fifo_number);
|
---|
| 639 | log_msg(4, "Cat'ting %s", curr_fifo);
|
---|
| 640 | if (!(fin = fopen(curr_fifo, "r"))) {
|
---|
| 641 | fatal_error("Unable to openin from fifo");
|
---|
| 642 | }
|
---|
[130] | 643 | if (prev_fifo != NULL) {
|
---|
| 644 | log_msg(5, "Deleting %s", prev_fifo);
|
---|
| 645 | unlink(prev_fifo); // just in case
|
---|
| 646 | paranoid_free(prev_fifo);
|
---|
| 647 | }
|
---|
[59] | 648 | copy_from_src_to_dest(fin, fout, 'w');
|
---|
| 649 | paranoid_fclose(fin);
|
---|
| 650 | fifo_number++;
|
---|
[130] | 651 |
|
---|
| 652 | prev_fifo = curr_fifo;
|
---|
| 653 | curr_fifo = next_fifo;
|
---|
[59] | 654 | log_msg(5, "Creating %s", next_fifo);
|
---|
[130] | 655 | asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
|
---|
[59] | 656 | mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo exists before we cat this one
|
---|
| 657 | system("sync");
|
---|
| 658 | sleep(5);
|
---|
| 659 | }
|
---|
[130] | 660 | asprintf(&tmp, "%s\n", NO_MORE_SUBVOLS);
|
---|
[59] | 661 | if (fwrite(tmp, 1, 128, fout) != 128) {
|
---|
| 662 | fatal_error("Cannot write interim block");
|
---|
| 663 | }
|
---|
| 664 | if (fwrite(tmp, 1, 128, fout) != 128) {
|
---|
| 665 | fatal_error("Cannot write interim block");
|
---|
| 666 | }
|
---|
| 667 | if (fwrite(tmp, 1, 128, fout) != 128) {
|
---|
| 668 | fatal_error("Cannot write interim block");
|
---|
| 669 | }
|
---|
| 670 | if (fwrite(tmp, 1, 128, fout) != 128) {
|
---|
| 671 | fatal_error("Cannot write interim block");
|
---|
| 672 | }
|
---|
[130] | 673 | paranoid_free(tmp);
|
---|
[59] | 674 | paranoid_fclose(fout);
|
---|
| 675 | log_to_screen("Cleaning up after partimagehack...");
|
---|
| 676 | log_msg(3, "Final fifo_number=%d", fifo_number);
|
---|
| 677 | paranoid_system("sync");
|
---|
| 678 | unlink(next_fifo);
|
---|
[130] | 679 | paranoid_free(next_fifo);
|
---|
| 680 |
|
---|
[59] | 681 | unlink(curr_fifo);
|
---|
[130] | 682 | paranoid_free(curr_fifo);
|
---|
| 683 |
|
---|
[59] | 684 | unlink(prev_fifo);
|
---|
[130] | 685 | paranoid_free(prev_fifo);
|
---|
| 686 |
|
---|
[59] | 687 | log_to_screen("Finished cleaning up.");
|
---|
[1] | 688 |
|
---|
| 689 | // if (!lstat(sz_wait_for_this_file, &statbuf))
|
---|
| 690 | // { log_msg(3, "WARNING! %s was not processed.", sz_wait_for_this_file); }
|
---|
[59] | 691 | log_msg(2, "Waiting for pthread_join() to join.");
|
---|
| 692 | pthread_join(partimage_thread, NULL);
|
---|
| 693 | res = sz_call_to_partimage[1];
|
---|
[130] | 694 | paranoid_free(sz_call_to_partimage);
|
---|
[59] | 695 | log_msg(2, "pthread_join() joined OK.");
|
---|
| 696 | log_msg(1, "Partimagehack(save) returned %d", res);
|
---|
| 697 | unlink(tmpstub);
|
---|
| 698 | paranoid_free(tmpstub);
|
---|
[130] | 699 |
|
---|
[59] | 700 | return (res);
|
---|
[1] | 701 | }
|
---|
| 702 |
|
---|
| 703 |
|
---|
| 704 | /**
|
---|
| 705 | * Feed @p input_device through partimage to @p output_fname.
|
---|
| 706 | * @param input_device The device to image.
|
---|
| 707 | * @param output_fname The file to write.
|
---|
| 708 | * @return 0 for success, nonzero for failure.
|
---|
| 709 | */
|
---|
[59] | 710 | int feed_into_partimage(char *input_device, char *output_fname)
|
---|
[1] | 711 | {
|
---|
| 712 | // BACKUP
|
---|
[59] | 713 | int res;
|
---|
[1] | 714 |
|
---|
[59] | 715 | if (!does_file_exist(input_device)) {
|
---|
| 716 | fatal_error("input device does not exist");
|
---|
| 717 | }
|
---|
| 718 | if (!find_home_of_exe("partimagehack")) {
|
---|
| 719 | fatal_error("partimagehack not found");
|
---|
| 720 | }
|
---|
| 721 | res =
|
---|
| 722 | dynamically_create_pipes_and_copy_from_them_to_output_file
|
---|
| 723 | (input_device, output_fname);
|
---|
| 724 | return (res);
|
---|
[1] | 725 | }
|
---|
| 726 |
|
---|
| 727 |
|
---|
[59] | 728 | int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
|
---|
[1] | 729 | {
|
---|
| 730 |
|
---|
[59] | 731 | /*@ int *************************************************************** */
|
---|
| 732 | int res = 0;
|
---|
[1] | 733 | int percentage = 0;
|
---|
| 734 | int maxpc = 0;
|
---|
| 735 | int pcno = 0;
|
---|
| 736 | int last_pcno = 0;
|
---|
| 737 |
|
---|
[59] | 738 | /*@ buffers *********************************************************** */
|
---|
| 739 | char *command;
|
---|
| 740 | char *tempfile;
|
---|
| 741 | /*@ pointers ********************************************************** */
|
---|
| 742 | FILE *pin;
|
---|
[1] | 743 |
|
---|
[59] | 744 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
[1] | 745 |
|
---|
[130] | 746 | asprintf(&tempfile,
|
---|
[59] | 747 | call_program_and_get_last_line_of_output
|
---|
| 748 | ("mktemp -q /tmp/mondo.XXXXXXXX"));
|
---|
[130] | 749 | asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
|
---|
[59] | 750 | tempfile);
|
---|
| 751 | log_msg(3, command);
|
---|
[130] | 752 | open_evalcall_form(tt);
|
---|
| 753 |
|
---|
[59] | 754 | if (!(pin = popen(command, "r"))) {
|
---|
| 755 | log_OS_error("fmt err");
|
---|
[130] | 756 | paranoid_free(command);
|
---|
| 757 | paranoid_free(tempfile);
|
---|
[59] | 758 | return (1);
|
---|
| 759 | }
|
---|
[130] | 760 | paranoid_free(command);
|
---|
| 761 |
|
---|
[59] | 762 | maxpc = 100;
|
---|
[1] | 763 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
|
---|
[59] | 764 | for (sleep(1); does_file_exist(tempfile); sleep(1)) {
|
---|
| 765 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
| 766 | if (pcno < 0 || pcno > 100) {
|
---|
| 767 | log_msg(5, "Weird pc#");
|
---|
| 768 | continue;
|
---|
| 769 | }
|
---|
| 770 | percentage = pcno * 100 / maxpc;
|
---|
| 771 | if (pcno <= 5 && last_pcno > 40) {
|
---|
| 772 | close_evalcall_form();
|
---|
[130] | 773 | open_evalcall_form("Verifying...");
|
---|
[59] | 774 | }
|
---|
| 775 | last_pcno = pcno;
|
---|
| 776 | update_evalcall_form(percentage);
|
---|
[1] | 777 | }
|
---|
[59] | 778 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
|
---|
| 779 | close_evalcall_form();
|
---|
| 780 | if (pclose(pin)) {
|
---|
| 781 | res++;
|
---|
| 782 | log_OS_error("Unable to pclose");
|
---|
[1] | 783 | }
|
---|
[59] | 784 | unlink(tempfile);
|
---|
| 785 | paranoid_free(tempfile);
|
---|
| 786 | return (res);
|
---|
[1] | 787 | }
|
---|
| 788 |
|
---|
| 789 |
|
---|
[59] | 790 | void *run_prog_in_bkgd_then_exit(void *info)
|
---|
[1] | 791 | {
|
---|
[59] | 792 | char *sz_command;
|
---|
| 793 | static int res = 4444;
|
---|
[1] | 794 |
|
---|
[59] | 795 | res = 999;
|
---|
| 796 | sz_command = (char *) info;
|
---|
| 797 | log_msg(4, "sz_command = '%s'", sz_command);
|
---|
| 798 | res = system(sz_command);
|
---|
| 799 | if (res > 256 && res != 4444) {
|
---|
| 800 | res = res / 256;
|
---|
| 801 | }
|
---|
| 802 | log_msg(4, "child res = %d", res);
|
---|
| 803 | sz_command[0] = '\0';
|
---|
| 804 | pthread_exit((void *) (&res));
|
---|
[1] | 805 | }
|
---|
| 806 |
|
---|
| 807 |
|
---|
[59] | 808 | int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
|
---|
[1] | 809 | {
|
---|
| 810 |
|
---|
[59] | 811 | /*@ int *************************************************************** */
|
---|
| 812 | int res = 0;
|
---|
[1] | 813 | int percentage = 0;
|
---|
| 814 | int maxpc = 100;
|
---|
| 815 | int pcno = 0;
|
---|
| 816 | int last_pcno = 0;
|
---|
[59] | 817 | int counter = 0;
|
---|
[1] | 818 |
|
---|
[59] | 819 | /*@ buffers *********************************************************** */
|
---|
| 820 | char *command;
|
---|
| 821 | /*@ pointers ********************************************************** */
|
---|
| 822 | static int chldres = 0;
|
---|
| 823 | int *pchild_result;
|
---|
| 824 | pthread_t childthread;
|
---|
[1] | 825 |
|
---|
[59] | 826 | pchild_result = &chldres;
|
---|
| 827 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
| 828 | assert_string_is_neither_NULL_nor_zerolength(tt);
|
---|
| 829 | *pchild_result = 999;
|
---|
[1] | 830 |
|
---|
[130] | 831 | asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
|
---|
[59] | 832 | log_msg(3, "command = '%s'", command);
|
---|
| 833 | if ((res =
|
---|
| 834 | pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
|
---|
| 835 | (void *) command))) {
|
---|
| 836 | fatal_error("Unable to create an archival thread");
|
---|
| 837 | }
|
---|
[1] | 838 |
|
---|
[59] | 839 | log_msg(8, "Parent running");
|
---|
[130] | 840 | open_evalcall_form(tt);
|
---|
[59] | 841 | for (sleep(1); command[0] != '\0'; sleep(1)) {
|
---|
| 842 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
| 843 | if (pcno <= 0 || pcno > 100) {
|
---|
| 844 | log_msg(8, "Weird pc#");
|
---|
| 845 | continue;
|
---|
| 846 | }
|
---|
| 847 | percentage = pcno * 100 / maxpc;
|
---|
| 848 | if (pcno <= 5 && last_pcno >= 40) {
|
---|
| 849 | close_evalcall_form();
|
---|
[130] | 850 | open_evalcall_form("Verifying...");
|
---|
[59] | 851 | }
|
---|
| 852 | if (counter++ >= 5) {
|
---|
| 853 | counter = 0;
|
---|
| 854 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 855 | }
|
---|
| 856 | last_pcno = pcno;
|
---|
| 857 | update_evalcall_form(percentage);
|
---|
[1] | 858 | }
|
---|
[130] | 859 | paranoid_free(command);
|
---|
| 860 |
|
---|
[59] | 861 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 862 | close_evalcall_form();
|
---|
| 863 | pthread_join(childthread, (void *) (&pchild_result));
|
---|
| 864 | if (pchild_result) {
|
---|
| 865 | res = *pchild_result;
|
---|
| 866 | } else {
|
---|
| 867 | res = 666;
|
---|
[1] | 868 | }
|
---|
[59] | 869 | log_msg(3, "Parent res = %d", res);
|
---|
| 870 | return (res);
|
---|
[1] | 871 | }
|
---|
| 872 |
|
---|
| 873 | #define PIH_LOG "/var/log/partimage-debug.log"
|
---|
| 874 |
|
---|
| 875 | /**
|
---|
| 876 | * Feed @p input_fifo through partimage (restore) to @p output_device.
|
---|
| 877 | * @param input_fifo The partimage file to read.
|
---|
| 878 | * @param output_device Where to put the output.
|
---|
| 879 | * @return The return value of partimagehack (0 for success).
|
---|
| 880 | * @bug Probably unnecessary, as the partimage is just a sparse file. We could use @c dd to restore it.
|
---|
| 881 | */
|
---|
[59] | 882 | int feed_outfrom_partimage(char *output_device, char *input_fifo)
|
---|
[1] | 883 | {
|
---|
| 884 | // RESTORE
|
---|
[59] | 885 | char *tmp;
|
---|
| 886 | char *stuff;
|
---|
| 887 | char *sz_call_to_partimage;
|
---|
| 888 | pthread_t partimage_thread;
|
---|
| 889 | int res;
|
---|
| 890 | char *curr_fifo;
|
---|
[147] | 891 | char *prev_fifo = NULL;
|
---|
[130] | 892 | char *oldest_fifo = NULL;
|
---|
[59] | 893 | char *next_fifo;
|
---|
| 894 | char *afternxt_fifo;
|
---|
| 895 | int fifo_number = 0;
|
---|
| 896 | char *tmpstub;
|
---|
| 897 | FILE *fin;
|
---|
| 898 | FILE *fout;
|
---|
[1] | 899 |
|
---|
[59] | 900 | log_msg(1, "output_device=%s", output_device);
|
---|
| 901 | log_msg(1, "input_fifo=%s", input_fifo);
|
---|
[130] | 902 | asprintf(&tmpstub, "/tmp");
|
---|
| 903 |
|
---|
[59] | 904 | log_msg(1, "tmpstub was %s", tmpstub);
|
---|
[130] | 905 | asprintf(&stuff, tmpstub);
|
---|
| 906 | paranoid_free(tmpstub);
|
---|
| 907 |
|
---|
| 908 | asprintf(&tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random());
|
---|
| 909 | paranoid_free(stuff);
|
---|
| 910 |
|
---|
[59] | 911 | log_msg(1, "tmpstub is now %s", tmpstub);
|
---|
| 912 | unlink("/tmp/PARTIMAGEHACK-POSITION");
|
---|
| 913 | unlink(PAUSE_PARTIMAGE_FNAME);
|
---|
| 914 | paranoid_system("rm -f /tmp/*PARTIMAGE*");
|
---|
[130] | 915 | asprintf(&curr_fifo, "%s.%03d", tmpstub, fifo_number);
|
---|
| 916 | asprintf(&next_fifo, "%s.%03d", tmpstub, fifo_number + 1);
|
---|
| 917 | asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
|
---|
[59] | 918 | mkfifo(PIH_LOG, S_IRWXU | S_IRWXG);
|
---|
| 919 | mkfifo(curr_fifo, S_IRWXU | S_IRWXG);
|
---|
| 920 | mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage
|
---|
| 921 | mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG);
|
---|
| 922 | system("cat " PIH_LOG " > /dev/null &");
|
---|
| 923 | log_msg(3, "curr_fifo = %s", curr_fifo);
|
---|
| 924 | log_msg(3, "next_fifo = %s", next_fifo);
|
---|
| 925 | if (!does_file_exist(input_fifo)) {
|
---|
| 926 | fatal_error("input fifo does not exist");
|
---|
| 927 | }
|
---|
| 928 | if (!(fin = fopen(input_fifo, "r"))) {
|
---|
| 929 | fatal_error("Unable to openin from input_fifo");
|
---|
| 930 | }
|
---|
| 931 | if (!find_home_of_exe("partimagehack")) {
|
---|
| 932 | fatal_error("partimagehack not found");
|
---|
| 933 | }
|
---|
[130] | 934 | asprintf(&sz_call_to_partimage,
|
---|
| 935 | "%c%cpartimagehack " PARTIMAGE_PARAMS
|
---|
| 936 | " restore %s %s > /dev/null 2>> %s", 2, 0, output_device, curr_fifo,
|
---|
[59] | 937 | MONDO_LOGFILE);
|
---|
| 938 | log_msg(1, "output_device = %s", output_device);
|
---|
| 939 | log_msg(1, "curr_fifo = %s", curr_fifo);
|
---|
| 940 | log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage + 2);
|
---|
| 941 | res =
|
---|
| 942 | pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd,
|
---|
| 943 | (void *) sz_call_to_partimage);
|
---|
| 944 | if (res) {
|
---|
| 945 | fatal_error("Failed to create thread to call partimage");
|
---|
| 946 | }
|
---|
| 947 | log_msg(1, "Running fore/back at same time");
|
---|
| 948 | log_msg(2, " Trying to openin %s", input_fifo);
|
---|
| 949 | if (!does_file_exist(input_fifo)) {
|
---|
| 950 | log_msg(2, "Warning - %s does not exist", input_fifo);
|
---|
| 951 | }
|
---|
| 952 | while (!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) {
|
---|
| 953 | log_msg(6, "Waiting for partimagehack (restore) to start");
|
---|
| 954 | sleep(1);
|
---|
| 955 | }
|
---|
[130] | 956 |
|
---|
| 957 | if (!(tmp = malloc(128))) {
|
---|
| 958 | fatal_error("Failed to malloc() tmp");
|
---|
| 959 | }
|
---|
[59] | 960 | while (sz_call_to_partimage[0] > 0) {
|
---|
| 961 | if (fread(tmp, 1, 128, fin) != 128) {
|
---|
| 962 | fatal_error("Cannot read introductory block");
|
---|
| 963 | }
|
---|
| 964 | if (strstr(tmp, NEXT_SUBVOL_PLEASE)) {
|
---|
| 965 | log_msg(2, "Great. Next subvol coming up.");
|
---|
| 966 | } else if (strstr(tmp, NO_MORE_SUBVOLS)) {
|
---|
| 967 | log_msg(2, "Great. That was the last subvol.");
|
---|
| 968 | break;
|
---|
| 969 | } else {
|
---|
| 970 | log_msg(2, "WTF is this? '%s'", tmp);
|
---|
| 971 | fatal_error("Unknown interim block");
|
---|
| 972 | }
|
---|
| 973 | if (feof(fin)) {
|
---|
| 974 | log_msg(1, "Eof(fin) detected. Breaking.");
|
---|
| 975 | break;
|
---|
| 976 | }
|
---|
| 977 | log_msg(3, "Processing subvol %d", fifo_number);
|
---|
| 978 | log_msg(5, "fifo_number=%d", fifo_number);
|
---|
| 979 | if (!(fout = fopen(curr_fifo, "w"))) {
|
---|
| 980 | fatal_error("Cannot openout to curr_fifo");
|
---|
| 981 | }
|
---|
| 982 | copy_from_src_to_dest(fout, fin, 'r');
|
---|
| 983 | paranoid_fclose(fout);
|
---|
| 984 | fifo_number++;
|
---|
[130] | 985 | if (oldest_fifo != NULL) {
|
---|
| 986 | log_msg(6, "Deleting %s", oldest_fifo);
|
---|
| 987 | unlink(oldest_fifo); // just in case
|
---|
| 988 | paranoid_free(oldest_fifo);
|
---|
| 989 | }
|
---|
| 990 | oldest_fifo = prev_fifo;
|
---|
| 991 | prev_fifo = curr_fifo;
|
---|
| 992 | curr_fifo = next_fifo;
|
---|
| 993 | next_fifo = afternxt_fifo;
|
---|
| 994 | asprintf(&afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2);
|
---|
[59] | 995 | log_msg(6, "Creating %s", afternxt_fifo);
|
---|
| 996 | mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we access current fifo
|
---|
| 997 | fflush(fin);
|
---|
[1] | 998 | // system("sync");
|
---|
[59] | 999 | usleep(1000L * 100L);
|
---|
| 1000 | }
|
---|
[130] | 1001 | paranoid_free(tmp);
|
---|
| 1002 | paranoid_free(tmpstub);
|
---|
| 1003 |
|
---|
[59] | 1004 | paranoid_fclose(fin);
|
---|
| 1005 | paranoid_system("sync");
|
---|
| 1006 | log_msg(1, "Partimagehack has finished. Great. Fin-closing.");
|
---|
| 1007 | log_msg(1, "Waiting for pthread_join");
|
---|
| 1008 | pthread_join(partimage_thread, NULL);
|
---|
| 1009 | res = sz_call_to_partimage[1];
|
---|
[130] | 1010 | paranoid_free(sz_call_to_partimage);
|
---|
| 1011 |
|
---|
[59] | 1012 | log_msg(1, "Yay. Partimagehack (restore) returned %d", res);
|
---|
| 1013 | unlink(prev_fifo);
|
---|
[130] | 1014 | paranoid_free(prev_fifo);
|
---|
| 1015 |
|
---|
[59] | 1016 | unlink(curr_fifo);
|
---|
[130] | 1017 | paranoid_free(curr_fifo);
|
---|
| 1018 |
|
---|
[59] | 1019 | unlink(next_fifo);
|
---|
[130] | 1020 | paranoid_free(next_fifo);
|
---|
| 1021 |
|
---|
[59] | 1022 | unlink(afternxt_fifo);
|
---|
[130] | 1023 | paranoid_free(afternxt_fifo);
|
---|
| 1024 |
|
---|
[59] | 1025 | unlink(PIH_LOG);
|
---|
[130] | 1026 | /* BERLIOS : pas de unlink(oldest_fifo) ??? */
|
---|
[59] | 1027 | paranoid_free(oldest_fifo);
|
---|
[130] | 1028 |
|
---|
[59] | 1029 | return (res);
|
---|
[1] | 1030 | }
|
---|