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