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