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