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