[300] | 1 | /* libmondo-fork.c
|
---|
| 2 | $Id: libmondo-fork.c 300 2006-01-11 13:20:38Z bcornec $
|
---|
| 3 |
|
---|
| 4 | - subroutines for handling forking/pthreads/etc.
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | 01/20/2006
|
---|
| 8 | - replaced partimagehack with ntfsclone
|
---|
| 9 |
|
---|
| 10 | 06/20/2004
|
---|
| 11 | - create fifo /var/log/partimagehack-debug.log and empty it
|
---|
| 12 | to keep ramdisk from filling up
|
---|
| 13 |
|
---|
| 14 | 04/13/2004
|
---|
| 15 | - >= should be <= g_loglevel
|
---|
| 16 |
|
---|
| 17 | 11/15/2003
|
---|
| 18 | - changed a few []s to char*s
|
---|
| 19 |
|
---|
| 20 | 10/12
|
---|
| 21 | - rewrote partimagehack handling (multiple fifos, chunks, etc.)
|
---|
| 22 |
|
---|
| 23 | 10/11
|
---|
| 24 | - partimagehack now has debug level of N (set in my-stuff.h)
|
---|
| 25 |
|
---|
| 26 | 10/08
|
---|
| 27 | - call to partimagehack when restoring will now log errors to /var/log/....log
|
---|
| 28 |
|
---|
| 29 | 10/06
|
---|
| 30 | - cleaned up logging a bit
|
---|
| 31 |
|
---|
| 32 | 09/30
|
---|
| 33 | - line 735 - missing char* cmd in sprintf()
|
---|
| 34 |
|
---|
| 35 | 09/28
|
---|
| 36 | - added run_external_binary_with_percentage_indicator()
|
---|
| 37 | - rewritten eval_call_to_make_ISO()
|
---|
| 38 |
|
---|
| 39 | 09/18
|
---|
| 40 | - call mkstemp instead of mktemp
|
---|
| 41 |
|
---|
| 42 | 09/13
|
---|
| 43 | - major NTFS hackage
|
---|
| 44 |
|
---|
| 45 | 09/12
|
---|
| 46 | - paranoid_system("rm -f /tmp/ *PARTIMAGE*") before calling partimagehack
|
---|
| 47 |
|
---|
| 48 | 09/11
|
---|
| 49 | - forward-ported unbroken feed_*_partimage() subroutines
|
---|
| 50 | from early August 2003
|
---|
| 51 |
|
---|
| 52 | 09/08
|
---|
| 53 | - detect & use partimagehack if it exists
|
---|
| 54 |
|
---|
| 55 | 09/05
|
---|
| 56 | - finally finished partimagehack hack :)
|
---|
| 57 |
|
---|
| 58 | 07/04
|
---|
| 59 | - added subroutines to wrap around partimagehack
|
---|
| 60 |
|
---|
| 61 | 04/27
|
---|
| 62 | - don't echo (...res=%d...) at end of log_it()
|
---|
| 63 | unnecessarily
|
---|
| 64 | - replace newtFinished() and newtInit() with
|
---|
| 65 | newtSuspend() and newtResume()
|
---|
| 66 |
|
---|
| 67 | 04/24
|
---|
| 68 | - added some assert()'s and log_OS_error()'s
|
---|
| 69 |
|
---|
| 70 | 04/09
|
---|
| 71 | - cleaned up run_program_and_log_output()
|
---|
| 72 |
|
---|
| 73 | 04/07
|
---|
| 74 | - cleaned up code a bit
|
---|
| 75 | - let run_program_and_log_output() accept -1 (only log if _no error_)
|
---|
| 76 |
|
---|
| 77 | 01/02/2003
|
---|
| 78 | - in eval_call_to_make_ISO(), append output to MONDO_LOGFILE
|
---|
| 79 | instead of a temporary stderr text file
|
---|
| 80 |
|
---|
| 81 | 12/10
|
---|
| 82 | - patch by Heiko Schlittermann to handle % chars in issue.net
|
---|
| 83 |
|
---|
| 84 | 11/18
|
---|
| 85 | - if mkisofs in eval_call_to_make_ISO() returns an error then return it,
|
---|
| 86 | whether ISO was created or not
|
---|
| 87 |
|
---|
| 88 | 10/30
|
---|
| 89 | - if mkisofs in eval_call_to_make_ISO() returns an error then find out if
|
---|
| 90 | the output (ISO) file has been created; if it has then return 0 anyway
|
---|
| 91 |
|
---|
| 92 | 08/01 - 09/30
|
---|
| 93 | - run_program_and_log_output() now takes boolean operator to specify
|
---|
| 94 | whether it will log its activities in the event of _success_
|
---|
| 95 | - system() now includes 2>/dev/null
|
---|
| 96 | - enlarged some tmp[]'s
|
---|
| 97 | - added run_program_and_log_to_screen() and run_program_and_log_output()
|
---|
| 98 |
|
---|
| 99 | 07/24
|
---|
| 100 | - created
|
---|
[1] | 101 | */
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | #include "my-stuff.h"
|
---|
| 105 | #include "mondostructures.h"
|
---|
| 106 | #include "libmondo-fork.h"
|
---|
| 107 | #include "libmondo-string-EXT.h"
|
---|
| 108 | #include "libmondo-gui-EXT.h"
|
---|
| 109 | #include "libmondo-files-EXT.h"
|
---|
| 110 | #include "libmondo-tools-EXT.h"
|
---|
| 111 | #include "lib-common-externs.h"
|
---|
| 112 |
|
---|
| 113 | /*@unused@*/
|
---|
[59] | 114 | //static char cvsid[] = "$Id: libmondo-fork.c 300 2006-01-11 13:20:38Z bcornec $";
|
---|
[1] | 115 |
|
---|
| 116 | extern char *g_tmpfs_mountpt;
|
---|
| 117 | extern t_bkptype g_backup_media_type;
|
---|
| 118 | extern bool g_text_mode;
|
---|
[59] | 119 | pid_t g_buffer_pid = 0;
|
---|
[1] | 120 |
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * Call a program and retrieve its last line of output.
|
---|
| 124 | * @param call The program to run.
|
---|
| 125 | * @return The last line of its output.
|
---|
| 126 | * @note The returned value points to static storage that will be overwritten with each call.
|
---|
| 127 | */
|
---|
[59] | 128 | char *call_program_and_get_last_line_of_output(char *call)
|
---|
[1] | 129 | {
|
---|
[59] | 130 | /*@ buffers ***************************************************** */
|
---|
[130] | 131 | static char *result = NULL;
|
---|
| 132 | char *tmp = NULL;
|
---|
[1] | 133 |
|
---|
[59] | 134 | /*@ pointers **************************************************** */
|
---|
| 135 | FILE *fin;
|
---|
[1] | 136 |
|
---|
[171] | 137 | size_t n = 0;
|
---|
[1] | 138 |
|
---|
[59] | 139 | /*@******************************************************************** */
|
---|
[1] | 140 |
|
---|
[59] | 141 | assert_string_is_neither_NULL_nor_zerolength(call);
|
---|
| 142 | if ((fin = popen(call, "r"))) {
|
---|
[130] | 143 | for (getline(&tmp, &n, fin); !feof(fin);
|
---|
| 144 | getline(&tmp, &n, fin)) {
|
---|
[59] | 145 | if (strlen(tmp) > 1) {
|
---|
[130] | 146 | if (result != NULL) {
|
---|
| 147 | paranoid_free(result);
|
---|
| 148 | }
|
---|
| 149 | asprintf(&result, tmp);
|
---|
[59] | 150 | }
|
---|
| 151 | }
|
---|
| 152 | paranoid_pclose(fin);
|
---|
| 153 | } else {
|
---|
| 154 | log_OS_error("Unable to popen call");
|
---|
[1] | 155 | }
|
---|
[59] | 156 | strip_spaces(result);
|
---|
[130] | 157 | paranoid_free(tmp);
|
---|
[59] | 158 | return (result);
|
---|
[1] | 159 | }
|
---|
| 160 |
|
---|
| 161 | #define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Call mkisofs to create an ISO image.
|
---|
| 165 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 166 | * - @c bkpinfo->manual_cd_tray
|
---|
| 167 | * - @c bkpinfo->backup_media_type
|
---|
| 168 | * - @c bkpinfo->please_dont_eject_when_restoring
|
---|
| 169 | * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
|
---|
| 170 | * - @c _ISO_ will become the ISO file (@p isofile)
|
---|
| 171 | * - @c _CD#_ becomes the CD number (@p cd_no)
|
---|
| 172 | * - @c _ERR_ becomes the logfile (@p g_logfile)
|
---|
| 173 | * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
|
---|
| 174 | * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
|
---|
| 175 | * @param logstub Unused.
|
---|
| 176 | * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
|
---|
| 177 | * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
|
---|
| 178 | * @bug @p logstub is unused.
|
---|
| 179 | */
|
---|
| 180 | int
|
---|
[59] | 181 | eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
|
---|
| 182 | char *basic_call, char *isofile,
|
---|
| 183 | int cd_no, char *logstub, char *what_i_am_doing)
|
---|
[1] | 184 | {
|
---|
| 185 |
|
---|
[59] | 186 | /*@ int's *** */
|
---|
| 187 | int retval = 0;
|
---|
[1] | 188 |
|
---|
| 189 |
|
---|
[59] | 190 | /*@ buffers *** */
|
---|
[130] | 191 | char *midway_call, *ultimate_call, *tmp, *command,
|
---|
| 192 | *cd_number_str;
|
---|
[59] | 193 | char *p;
|
---|
[1] | 194 |
|
---|
| 195 | /*@*********** End Variables ***************************************/
|
---|
| 196 |
|
---|
[59] | 197 | log_msg(3, "Starting");
|
---|
| 198 | assert(bkpinfo != NULL);
|
---|
| 199 | assert_string_is_neither_NULL_nor_zerolength(basic_call);
|
---|
| 200 | assert_string_is_neither_NULL_nor_zerolength(isofile);
|
---|
| 201 | assert_string_is_neither_NULL_nor_zerolength(logstub);
|
---|
| 202 | if (!(midway_call = malloc(1200))) {
|
---|
| 203 | fatal_error("Cannot malloc midway_call");
|
---|
| 204 | }
|
---|
| 205 | if (!(ultimate_call = malloc(1200))) {
|
---|
| 206 | fatal_error("Cannot malloc ultimate_call");
|
---|
| 207 | }
|
---|
| 208 | if (!(tmp = malloc(1200))) {
|
---|
| 209 | fatal_error("Cannot malloc tmp");
|
---|
| 210 | }
|
---|
[1] | 211 |
|
---|
[130] | 212 | asprintf(&cd_number_str, "%d", cd_no);
|
---|
[59] | 213 | resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
|
---|
| 214 | resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
|
---|
[130] | 215 | paranoid_free(cd_number_str);
|
---|
| 216 |
|
---|
[59] | 217 | resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
|
---|
| 218 | log_msg(4, "basic call = '%s'", basic_call);
|
---|
| 219 | log_msg(4, "midway_call = '%s'", midway_call);
|
---|
| 220 | log_msg(4, "tmp = '%s'", tmp);
|
---|
| 221 | log_msg(4, "ultimate call = '%s'", ultimate_call);
|
---|
[130] | 222 | asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
|
---|
[1] | 223 |
|
---|
[59] | 224 | log_to_screen
|
---|
| 225 | ("Please be patient. Do not be alarmed by on-screen inactivity.");
|
---|
| 226 | log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
|
---|
| 227 | what_i_am_doing);
|
---|
| 228 | strcpy(tmp, command);
|
---|
| 229 | if (bkpinfo->manual_cd_tray) {
|
---|
| 230 | p = strstr(tmp, "2>>");
|
---|
| 231 | if (p) {
|
---|
| 232 | sprintf(p, " ");
|
---|
| 233 | while (*p == ' ') {
|
---|
| 234 | p++;
|
---|
| 235 | }
|
---|
| 236 | for (; *p != ' '; p++) {
|
---|
| 237 | *p = ' ';
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
[130] | 240 | paranoid_free(command);
|
---|
| 241 | asprintf(&command, tmp);
|
---|
[1] | 242 | #ifndef _XWIN
|
---|
[59] | 243 | if (!g_text_mode) {
|
---|
| 244 | newtSuspend();
|
---|
| 245 | }
|
---|
[1] | 246 | #endif
|
---|
[59] | 247 | log_msg(1, "command = '%s'", command);
|
---|
| 248 | retval += system(command);
|
---|
| 249 | if (!g_text_mode) {
|
---|
| 250 | newtResume();
|
---|
| 251 | }
|
---|
| 252 | if (retval) {
|
---|
| 253 | log_msg(2, "Basic call '%s' returned an error.", basic_call);
|
---|
| 254 | popup_and_OK("Press ENTER to continue.");
|
---|
| 255 | popup_and_OK
|
---|
| 256 | ("mkisofs and/or cdrecord returned an error. CD was not created");
|
---|
| 257 | }
|
---|
[1] | 258 | }
|
---|
[59] | 259 | /* if text mode then do the above & RETURN; if not text mode, do this... */
|
---|
| 260 | else {
|
---|
| 261 | log_msg(3, "command = '%s'", command);
|
---|
[1] | 262 | // yes_this_is_a_goto:
|
---|
[59] | 263 | retval =
|
---|
| 264 | run_external_binary_with_percentage_indicator_NEW
|
---|
| 265 | (what_i_am_doing, command);
|
---|
| 266 | }
|
---|
[130] | 267 | paranoid_free(command);
|
---|
[1] | 268 |
|
---|
[59] | 269 | paranoid_free(midway_call);
|
---|
| 270 | paranoid_free(ultimate_call);
|
---|
| 271 | paranoid_free(tmp);
|
---|
| 272 | return (retval);
|
---|
[1] | 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | /**
|
---|
| 277 | * Run a program and log its output (stdout and stderr) to the logfile.
|
---|
| 278 | * @param program The program to run. Passed to the shell, so you can use pipes etc.
|
---|
| 279 | * @param debug_level If @p g_loglevel is higher than this, do not log the output.
|
---|
| 280 | * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
|
---|
| 281 | */
|
---|
[59] | 282 | int run_program_and_log_output(char *program, int debug_level)
|
---|
[1] | 283 | {
|
---|
[59] | 284 | /*@ buffer ****************************************************** */
|
---|
[130] | 285 | char *callstr;
|
---|
| 286 | char *incoming = NULL;
|
---|
[59] | 287 | char tmp[MAX_STR_LEN * 2];
|
---|
[1] | 288 |
|
---|
[59] | 289 | /*@ int ********************************************************* */
|
---|
| 290 | int res;
|
---|
| 291 | int i;
|
---|
[171] | 292 | size_t n = 0;
|
---|
[59] | 293 | int len;
|
---|
| 294 | bool log_if_failure = FALSE;
|
---|
| 295 | bool log_if_success = FALSE;
|
---|
[1] | 296 |
|
---|
[59] | 297 | /*@ pointers *************************************************** */
|
---|
| 298 | FILE *fin;
|
---|
| 299 | char *p;
|
---|
[1] | 300 |
|
---|
[59] | 301 | /*@ end vars *************************************************** */
|
---|
[1] | 302 |
|
---|
[59] | 303 | assert(program != NULL);
|
---|
| 304 | if (!program[0]) {
|
---|
| 305 | log_msg(2, "Warning - asked to run zerolength program");
|
---|
| 306 | return (1);
|
---|
| 307 | }
|
---|
[1] | 308 |
|
---|
[59] | 309 | if (debug_level <= g_loglevel) {
|
---|
| 310 | log_if_success = TRUE;
|
---|
| 311 | log_if_failure = TRUE;
|
---|
| 312 | }
|
---|
[130] | 313 | asprintf(&callstr,
|
---|
[59] | 314 | "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
|
---|
| 315 | program);
|
---|
| 316 | while ((p = strchr(callstr, '\r'))) {
|
---|
| 317 | *p = ' ';
|
---|
| 318 | }
|
---|
| 319 | while ((p = strchr(callstr, '\n'))) {
|
---|
| 320 | *p = ' ';
|
---|
| 321 | } /* single '=' is intentional */
|
---|
[1] | 322 |
|
---|
| 323 |
|
---|
[59] | 324 | len = (int) strlen(program);
|
---|
| 325 | for (i = 0; i < 35 - len / 2; i++) {
|
---|
| 326 | tmp[i] = '-';
|
---|
| 327 | }
|
---|
| 328 | tmp[i] = '\0';
|
---|
| 329 | strcat(tmp, " ");
|
---|
| 330 | strcat(tmp, program);
|
---|
| 331 | strcat(tmp, " ");
|
---|
| 332 | for (i = 0; i < 35 - len / 2; i++) {
|
---|
| 333 | strcat(tmp, "-");
|
---|
| 334 | }
|
---|
| 335 | res = system(callstr);
|
---|
| 336 | if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
|
---|
| 337 | log_msg(0, "running: %s", callstr);
|
---|
| 338 | log_msg(0,
|
---|
| 339 | "--------------------------------start of output-----------------------------");
|
---|
| 340 | }
|
---|
[130] | 341 | paranoid_free(callstr);
|
---|
| 342 |
|
---|
[59] | 343 | if (log_if_failure
|
---|
| 344 | &&
|
---|
| 345 | system
|
---|
| 346 | ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
|
---|
[1] | 347 | {
|
---|
[59] | 348 | log_OS_error("Command failed");
|
---|
| 349 | }
|
---|
| 350 | unlink("/tmp/mondo-run-prog-thing.err");
|
---|
| 351 | fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
|
---|
| 352 | if (fin) {
|
---|
[130] | 353 | for (getline(&incoming, &n, fin); !feof(fin);
|
---|
| 354 | getline(&incoming, &n, fin)) {
|
---|
[59] | 355 | /* patch by Heiko Schlittermann */
|
---|
| 356 | p = incoming;
|
---|
| 357 | while (p && *p) {
|
---|
| 358 | if ((p = strchr(p, '%'))) {
|
---|
| 359 | memmove(p, p + 1, strlen(p) + 1);
|
---|
| 360 | p += 2;
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | /* end of patch */
|
---|
| 364 | strip_spaces(incoming);
|
---|
| 365 | if ((res == 0 && log_if_success)
|
---|
| 366 | || (res != 0 && log_if_failure)) {
|
---|
| 367 | log_msg(0, incoming);
|
---|
| 368 | }
|
---|
[1] | 369 | }
|
---|
[130] | 370 | paranoid_free(incoming);
|
---|
[59] | 371 | paranoid_fclose(fin);
|
---|
[1] | 372 | }
|
---|
[59] | 373 | unlink("/tmp/mondo-run-prog-thing.tmp");
|
---|
| 374 | if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
|
---|
| 375 | log_msg(0,
|
---|
| 376 | "--------------------------------end of output------------------------------");
|
---|
| 377 | if (res) {
|
---|
| 378 | log_msg(0, "...ran with res=%d", res);
|
---|
| 379 | } else {
|
---|
| 380 | log_msg(0, "...ran just fine. :-)");
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
[1] | 383 | // else
|
---|
| 384 | // { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
|
---|
[59] | 385 | return (res);
|
---|
[1] | 386 | }
|
---|
| 387 |
|
---|
| 388 |
|
---|
| 389 | /**
|
---|
| 390 | * Run a program and log its output to the screen.
|
---|
| 391 | * @param basic_call The program to run.
|
---|
| 392 | * @param what_i_am_doing The title of the evalcall form.
|
---|
| 393 | * @return The return value of the command (varies, but 0 almost always means success).
|
---|
| 394 | * @see run_program_and_log_output
|
---|
| 395 | * @see log_to_screen
|
---|
| 396 | */
|
---|
[59] | 397 | int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
|
---|
[1] | 398 | {
|
---|
[59] | 399 | /*@ int ******************************************************** */
|
---|
| 400 | int retval = 0;
|
---|
| 401 | int res = 0;
|
---|
| 402 | int i;
|
---|
[1] | 403 |
|
---|
[59] | 404 | /*@ pointers **************************************************** */
|
---|
| 405 | FILE *fin;
|
---|
[1] | 406 |
|
---|
[59] | 407 | /*@ buffers **************************************************** */
|
---|
[130] | 408 | char *tmp;
|
---|
| 409 | char *command;
|
---|
| 410 | char *lockfile;
|
---|
[1] | 411 |
|
---|
[59] | 412 | /*@ end vars *************************************************** */
|
---|
[1] | 413 |
|
---|
[59] | 414 | assert_string_is_neither_NULL_nor_zerolength(basic_call);
|
---|
[1] | 415 |
|
---|
[130] | 416 | asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
|
---|
[59] | 417 | mkstemp(lockfile);
|
---|
[130] | 418 | asprintf(&command,
|
---|
[59] | 419 | "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
|
---|
| 420 | lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
|
---|
| 421 | open_evalcall_form(what_i_am_doing);
|
---|
[130] | 422 | asprintf(&tmp, "Executing %s", basic_call);
|
---|
[59] | 423 | log_msg(2, tmp);
|
---|
[130] | 424 | paranoid_free(tmp);
|
---|
| 425 |
|
---|
[59] | 426 | if (!(fin = popen(command, "r"))) {
|
---|
| 427 | log_OS_error("Unable to popen-in command");
|
---|
[130] | 428 | asprintf(&tmp, "Failed utterly to call '%s'", command);
|
---|
[59] | 429 | log_to_screen(tmp);
|
---|
[130] | 430 | paranoid_free(tmp);
|
---|
| 431 | paranoid_free(lockfile);
|
---|
| 432 | paranoid_free(command);
|
---|
[59] | 433 | return (1);
|
---|
| 434 | }
|
---|
[130] | 435 | paranoid_free(command);
|
---|
| 436 |
|
---|
[59] | 437 | if (!does_file_exist(lockfile)) {
|
---|
| 438 | log_to_screen("Waiting for external binary to start");
|
---|
| 439 | for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
|
---|
| 440 | log_msg(3, "Waiting for lockfile %s to exist", lockfile);
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 | /* This works on Newt, and it gives quicker updates. */
|
---|
| 444 | for (; does_file_exist(lockfile); sleep(1)) {
|
---|
| 445 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 446 | update_evalcall_form(1);
|
---|
| 447 | }
|
---|
[68] | 448 | /* Evaluate the status returned by pclose to get the exit code of the called program. */
|
---|
| 449 | errno = 0;
|
---|
| 450 | res = pclose(fin);
|
---|
| 451 | /* Log actual pclose errors. */
|
---|
[89] | 452 | if (errno)
|
---|
| 453 | log_msg(5, "pclose err: %d", errno);
|
---|
[68] | 454 | /* Check if we have a valid status. If we do, extract the called program's exit code. */
|
---|
| 455 | /* If we don't, highlight this fact by returning -1. */
|
---|
| 456 | if (WIFEXITED(res)) {
|
---|
[89] | 457 | retval = WEXITSTATUS(res);
|
---|
[68] | 458 | } else {
|
---|
[89] | 459 | retval = -1;
|
---|
[68] | 460 | }
|
---|
[59] | 461 | close_evalcall_form();
|
---|
| 462 | unlink(lockfile);
|
---|
[130] | 463 | paranoid_free(lockfile);
|
---|
| 464 |
|
---|
[59] | 465 | return (retval);
|
---|
[1] | 466 | }
|
---|
| 467 |
|
---|
| 468 |
|
---|
| 469 | /**
|
---|
[130] | 470 | * Apparently used. @bug This has a purpose, but what?
|
---|
[1] | 471 | */
|
---|
| 472 | #define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
|
---|
| 473 | #define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
|
---|
| 474 |
|
---|
| 475 |
|
---|
| 476 |
|
---|
[300] | 477 |
|
---|
[59] | 478 | int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
|
---|
[1] | 479 | {
|
---|
| 480 | // if dir=='w' then copy from orig to archived
|
---|
| 481 | // if dir=='r' then copy from archived to orig
|
---|
[59] | 482 | char *tmp;
|
---|
| 483 | char *buf;
|
---|
| 484 | long int bytes_to_be_read, bytes_read_in, bytes_written_out =
|
---|
| 485 | 0, bufcap, subsliceno = 0;
|
---|
| 486 | int retval = 0;
|
---|
| 487 | FILE *fin;
|
---|
| 488 | FILE *fout;
|
---|
| 489 | FILE *ftmp;
|
---|
[1] | 490 |
|
---|
[59] | 491 | log_msg(5, "Opening.");
|
---|
| 492 | bufcap = 256L * 1024L;
|
---|
| 493 | if (!(buf = malloc(bufcap))) {
|
---|
| 494 | fatal_error("Failed to malloc() buf");
|
---|
| 495 | }
|
---|
[1] | 496 |
|
---|
[59] | 497 | if (direction == 'w') {
|
---|
| 498 | fin = f_orig;
|
---|
| 499 | fout = f_archived;
|
---|
[130] | 500 | asprintf(&tmp, "%-64s", PIMP_START_SZ);
|
---|
[59] | 501 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 502 | fatal_error("Can't write the introductory block");
|
---|
| 503 | }
|
---|
[130] | 504 | paranoid_free(tmp);
|
---|
| 505 |
|
---|
[59] | 506 | while (1) {
|
---|
| 507 | bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
|
---|
| 508 | if (bytes_read_in == 0) {
|
---|
| 509 | break;
|
---|
| 510 | }
|
---|
[130] | 511 | asprintf(&tmp, "%-64ld", bytes_read_in);
|
---|
[59] | 512 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 513 | fatal_error("Cannot write introductory block");
|
---|
| 514 | }
|
---|
[130] | 515 | paranoid_free(tmp);
|
---|
| 516 |
|
---|
[59] | 517 | log_msg(7,
|
---|
| 518 | "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
|
---|
| 519 | subsliceno, bytes_read_in, bytes_to_be_read);
|
---|
| 520 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
[130] | 521 | asprintf(&tmp, "%-64ld", subsliceno);
|
---|
[59] | 522 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 523 | fatal_error("Cannot write post-thingy block");
|
---|
| 524 | }
|
---|
[130] | 525 | paranoid_free(tmp);
|
---|
| 526 |
|
---|
[59] | 527 | log_msg(7, "Subslice #%d written OK", subsliceno);
|
---|
| 528 | subsliceno++;
|
---|
| 529 | }
|
---|
[130] | 530 | asprintf(&tmp, "%-64ld", 0L);
|
---|
[59] | 531 | if (fwrite(tmp, 1, 64L, fout) != 64L) {
|
---|
| 532 | fatal_error("Cannot write final introductory block");
|
---|
| 533 | }
|
---|
| 534 | } else {
|
---|
| 535 | fin = f_archived;
|
---|
| 536 | fout = f_orig;
|
---|
[130] | 537 | if (!(tmp = malloc(64L))) {
|
---|
| 538 | fatal_error("Failed to malloc() tmp");
|
---|
| 539 | }
|
---|
[59] | 540 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
| 541 | fatal_error("Cannot read the introductory block");
|
---|
| 542 | }
|
---|
| 543 | log_msg(5, "tmp is %s", tmp);
|
---|
| 544 | if (!strstr(tmp, PIMP_START_SZ)) {
|
---|
| 545 | fatal_error("Can't find intro blk");
|
---|
| 546 | }
|
---|
| 547 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
| 548 | fatal_error("Cannot read introductory blk");
|
---|
| 549 | }
|
---|
| 550 | bytes_to_be_read = atol(tmp);
|
---|
| 551 | while (bytes_to_be_read > 0) {
|
---|
| 552 | log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
|
---|
| 553 | bytes_to_be_read);
|
---|
| 554 | bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
|
---|
| 555 | if (bytes_read_in != bytes_to_be_read) {
|
---|
| 556 | fatal_error
|
---|
| 557 | ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
|
---|
| 558 | }
|
---|
| 559 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
| 560 | if (fread(tmp, 1, 64, fin) != 64) {
|
---|
| 561 | fatal_error("Cannot read post-thingy block");
|
---|
| 562 | }
|
---|
| 563 | if (atol(tmp) != subsliceno) {
|
---|
| 564 | log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
|
---|
| 565 | subsliceno, atol(tmp), tmp);
|
---|
| 566 | }
|
---|
| 567 | log_msg(7, "Subslice #%ld read OK", subsliceno);
|
---|
| 568 | subsliceno++;
|
---|
| 569 | if (fread(tmp, 1, 64, fin) != 64) {
|
---|
| 570 | fatal_error("Cannot read introductory block");
|
---|
| 571 | }
|
---|
| 572 | bytes_to_be_read = atol(tmp);
|
---|
| 573 | }
|
---|
| 574 | }
|
---|
[1] | 575 |
|
---|
| 576 | // log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
|
---|
| 577 |
|
---|
[59] | 578 | if (direction == 'w') {
|
---|
[130] | 579 | asprintf(&tmp, "%-64s", PIMP_END_SZ);
|
---|
[59] | 580 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 581 | fatal_error("Can't write the final block");
|
---|
| 582 | }
|
---|
[130] | 583 | paranoid_free(tmp);
|
---|
[59] | 584 | } else {
|
---|
| 585 | log_msg(1, "tmpA is %s", tmp);
|
---|
| 586 | if (!strstr(tmp, PIMP_END_SZ)) {
|
---|
| 587 | if (fread(tmp, 1, 64, fin) != 64) {
|
---|
| 588 | fatal_error("Can't read the final block");
|
---|
| 589 | }
|
---|
| 590 | log_msg(5, "tmpB is %s", tmp);
|
---|
| 591 | if (!strstr(tmp, PIMP_END_SZ)) {
|
---|
| 592 | ftmp = fopen("/tmp/out.leftover", "w");
|
---|
| 593 | bytes_read_in = fread(tmp, 1, 64, fin);
|
---|
| 594 | log_msg(1, "bytes_read_in = %ld", bytes_read_in);
|
---|
[1] | 595 | // if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
|
---|
[59] | 596 | fwrite(tmp, 1, bytes_read_in, ftmp);
|
---|
[130] | 597 | paranoid_free(tmp);
|
---|
| 598 |
|
---|
| 599 | if (!(tmp = malloc(512))) {
|
---|
| 600 | fatal_error("Failed to malloc() tmp");
|
---|
| 601 | }
|
---|
[59] | 602 | fread(tmp, 1, 512, fin);
|
---|
| 603 | log_msg(0, "tmp = '%s'", tmp);
|
---|
| 604 | fwrite(tmp, 1, 512, ftmp);
|
---|
| 605 | fclose(ftmp);
|
---|
| 606 | fatal_error("Missing terminating block");
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
[130] | 609 | paranoid_free(tmp);
|
---|
[59] | 610 | }
|
---|
[1] | 611 |
|
---|
[59] | 612 | paranoid_free(buf);
|
---|
| 613 | log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
|
---|
| 614 | return (retval);
|
---|
[1] | 615 | }
|
---|
| 616 |
|
---|
| 617 | /**
|
---|
[300] | 618 | * Feed @p input_device through ntfsclone to @p output_fname.
|
---|
[1] | 619 | * @param input_device The device to image.
|
---|
| 620 | * @param output_fname The file to write.
|
---|
| 621 | * @return 0 for success, nonzero for failure.
|
---|
| 622 | */
|
---|
[300] | 623 | int feed_into_ntfsprog(char *input_device, char *output_fname)
|
---|
[1] | 624 | {
|
---|
| 625 | // BACKUP
|
---|
[300] | 626 | int res = -1;
|
---|
| 627 | char*command;
|
---|
[1] | 628 |
|
---|
[59] | 629 | if (!does_file_exist(input_device)) {
|
---|
| 630 | fatal_error("input device does not exist");
|
---|
| 631 | }
|
---|
[300] | 632 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 633 | fatal_error("ntfsclone not found");
|
---|
[59] | 634 | }
|
---|
[300] | 635 | malloc_string(command);
|
---|
| 636 | sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
|
---|
| 637 | res = run_program_and_log_output(command, 5);
|
---|
| 638 | paranoid_free(command);
|
---|
[59] | 639 | return (res);
|
---|
[1] | 640 | }
|
---|
| 641 |
|
---|
| 642 |
|
---|
[59] | 643 | int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
|
---|
[1] | 644 | {
|
---|
| 645 |
|
---|
[59] | 646 | /*@ int *************************************************************** */
|
---|
| 647 | int res = 0;
|
---|
[1] | 648 | int percentage = 0;
|
---|
| 649 | int maxpc = 0;
|
---|
| 650 | int pcno = 0;
|
---|
| 651 | int last_pcno = 0;
|
---|
| 652 |
|
---|
[59] | 653 | /*@ buffers *********************************************************** */
|
---|
| 654 | char *command;
|
---|
| 655 | char *tempfile;
|
---|
| 656 | /*@ pointers ********************************************************** */
|
---|
| 657 | FILE *pin;
|
---|
[1] | 658 |
|
---|
[59] | 659 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
[1] | 660 |
|
---|
[130] | 661 | asprintf(&tempfile,
|
---|
[59] | 662 | call_program_and_get_last_line_of_output
|
---|
| 663 | ("mktemp -q /tmp/mondo.XXXXXXXX"));
|
---|
[130] | 664 | asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
|
---|
[59] | 665 | tempfile);
|
---|
| 666 | log_msg(3, command);
|
---|
[130] | 667 | open_evalcall_form(tt);
|
---|
| 668 |
|
---|
[59] | 669 | if (!(pin = popen(command, "r"))) {
|
---|
| 670 | log_OS_error("fmt err");
|
---|
[130] | 671 | paranoid_free(command);
|
---|
| 672 | paranoid_free(tempfile);
|
---|
[59] | 673 | return (1);
|
---|
| 674 | }
|
---|
[130] | 675 | paranoid_free(command);
|
---|
| 676 |
|
---|
[59] | 677 | maxpc = 100;
|
---|
[1] | 678 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
|
---|
[59] | 679 | for (sleep(1); does_file_exist(tempfile); sleep(1)) {
|
---|
| 680 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
| 681 | if (pcno < 0 || pcno > 100) {
|
---|
| 682 | log_msg(5, "Weird pc#");
|
---|
| 683 | continue;
|
---|
| 684 | }
|
---|
| 685 | percentage = pcno * 100 / maxpc;
|
---|
| 686 | if (pcno <= 5 && last_pcno > 40) {
|
---|
| 687 | close_evalcall_form();
|
---|
[130] | 688 | open_evalcall_form("Verifying...");
|
---|
[59] | 689 | }
|
---|
| 690 | last_pcno = pcno;
|
---|
| 691 | update_evalcall_form(percentage);
|
---|
[1] | 692 | }
|
---|
[59] | 693 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
|
---|
| 694 | close_evalcall_form();
|
---|
| 695 | if (pclose(pin)) {
|
---|
| 696 | res++;
|
---|
| 697 | log_OS_error("Unable to pclose");
|
---|
[1] | 698 | }
|
---|
[59] | 699 | unlink(tempfile);
|
---|
| 700 | paranoid_free(tempfile);
|
---|
| 701 | return (res);
|
---|
[1] | 702 | }
|
---|
| 703 |
|
---|
| 704 |
|
---|
[59] | 705 | void *run_prog_in_bkgd_then_exit(void *info)
|
---|
[1] | 706 | {
|
---|
[59] | 707 | char *sz_command;
|
---|
| 708 | static int res = 4444;
|
---|
[1] | 709 |
|
---|
[59] | 710 | res = 999;
|
---|
| 711 | sz_command = (char *) info;
|
---|
| 712 | log_msg(4, "sz_command = '%s'", sz_command);
|
---|
| 713 | res = system(sz_command);
|
---|
| 714 | if (res > 256 && res != 4444) {
|
---|
| 715 | res = res / 256;
|
---|
| 716 | }
|
---|
| 717 | log_msg(4, "child res = %d", res);
|
---|
| 718 | sz_command[0] = '\0';
|
---|
| 719 | pthread_exit((void *) (&res));
|
---|
[1] | 720 | }
|
---|
| 721 |
|
---|
| 722 |
|
---|
[59] | 723 | int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
|
---|
[1] | 724 | {
|
---|
| 725 |
|
---|
[59] | 726 | /*@ int *************************************************************** */
|
---|
| 727 | int res = 0;
|
---|
[1] | 728 | int percentage = 0;
|
---|
| 729 | int maxpc = 100;
|
---|
| 730 | int pcno = 0;
|
---|
| 731 | int last_pcno = 0;
|
---|
[59] | 732 | int counter = 0;
|
---|
[1] | 733 |
|
---|
[59] | 734 | /*@ buffers *********************************************************** */
|
---|
| 735 | char *command;
|
---|
| 736 | /*@ pointers ********************************************************** */
|
---|
| 737 | static int chldres = 0;
|
---|
| 738 | int *pchild_result;
|
---|
| 739 | pthread_t childthread;
|
---|
[1] | 740 |
|
---|
[59] | 741 | pchild_result = &chldres;
|
---|
| 742 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
| 743 | assert_string_is_neither_NULL_nor_zerolength(tt);
|
---|
| 744 | *pchild_result = 999;
|
---|
[1] | 745 |
|
---|
[130] | 746 | asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
|
---|
[59] | 747 | log_msg(3, "command = '%s'", command);
|
---|
| 748 | if ((res =
|
---|
| 749 | pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
|
---|
| 750 | (void *) command))) {
|
---|
| 751 | fatal_error("Unable to create an archival thread");
|
---|
| 752 | }
|
---|
[1] | 753 |
|
---|
[59] | 754 | log_msg(8, "Parent running");
|
---|
[130] | 755 | open_evalcall_form(tt);
|
---|
[59] | 756 | for (sleep(1); command[0] != '\0'; sleep(1)) {
|
---|
| 757 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
| 758 | if (pcno <= 0 || pcno > 100) {
|
---|
| 759 | log_msg(8, "Weird pc#");
|
---|
| 760 | continue;
|
---|
| 761 | }
|
---|
| 762 | percentage = pcno * 100 / maxpc;
|
---|
| 763 | if (pcno <= 5 && last_pcno >= 40) {
|
---|
| 764 | close_evalcall_form();
|
---|
[130] | 765 | open_evalcall_form("Verifying...");
|
---|
[59] | 766 | }
|
---|
| 767 | if (counter++ >= 5) {
|
---|
| 768 | counter = 0;
|
---|
| 769 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 770 | }
|
---|
| 771 | last_pcno = pcno;
|
---|
| 772 | update_evalcall_form(percentage);
|
---|
[1] | 773 | }
|
---|
[130] | 774 | paranoid_free(command);
|
---|
| 775 |
|
---|
[59] | 776 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 777 | close_evalcall_form();
|
---|
| 778 | pthread_join(childthread, (void *) (&pchild_result));
|
---|
| 779 | if (pchild_result) {
|
---|
| 780 | res = *pchild_result;
|
---|
| 781 | } else {
|
---|
| 782 | res = 666;
|
---|
[1] | 783 | }
|
---|
[59] | 784 | log_msg(3, "Parent res = %d", res);
|
---|
| 785 | return (res);
|
---|
[1] | 786 | }
|
---|
| 787 |
|
---|
| 788 |
|
---|
| 789 | /**
|
---|
[300] | 790 | * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
|
---|
| 791 | * @param input_fifo The ntfsclone file to read.
|
---|
[1] | 792 | * @param output_device Where to put the output.
|
---|
[300] | 793 | * @return The return value of ntfsclone (0 for success).
|
---|
[1] | 794 | */
|
---|
[300] | 795 | int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
|
---|
[1] | 796 | {
|
---|
| 797 | // RESTORE
|
---|
[300] | 798 | int res = -1;
|
---|
| 799 | char *command;
|
---|
[1] | 800 |
|
---|
[300] | 801 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 802 | fatal_error("ntfsclone not found");
|
---|
[59] | 803 | }
|
---|
[300] | 804 | asprintf(&command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
|
---|
| 805 | res = run_program_and_log_output(command, 5);
|
---|
| 806 | paranoid_free(command);
|
---|
[59] | 807 | return (res);
|
---|
[1] | 808 | }
|
---|