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