[1] | 1 | /* libmondo-fork.c
|
---|
[128] | 2 | $Id: libmondo-fork.c 2296 2009-07-23 12:53:30Z 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 2296 2009-07-23 12:53:30Z 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 | }
|
---|
[2296] | 285 | mr_asprintf(&callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err", program, bkpinfo->tmpdir, bkpinfo->tmpdir);
|
---|
[128] | 286 | while ((p = strchr(callstr, '\r'))) {
|
---|
| 287 | *p = ' ';
|
---|
| 288 | }
|
---|
| 289 | while ((p = strchr(callstr, '\n'))) {
|
---|
| 290 | *p = ' ';
|
---|
| 291 | } /* single '=' is intentional */
|
---|
[1] | 292 |
|
---|
| 293 |
|
---|
[128] | 294 | len = (int) strlen(program);
|
---|
| 295 | for (i = 0; i < 35 - len / 2; i++) {
|
---|
| 296 | tmp[i] = '-';
|
---|
| 297 | }
|
---|
| 298 | tmp[i] = '\0';
|
---|
| 299 | strcat(tmp, " ");
|
---|
| 300 | strcat(tmp, program);
|
---|
| 301 | strcat(tmp, " ");
|
---|
| 302 | for (i = 0; i < 35 - len / 2; i++) {
|
---|
| 303 | strcat(tmp, "-");
|
---|
| 304 | }
|
---|
| 305 | strcpy(initial_label, tmp);
|
---|
| 306 | res = system(callstr);
|
---|
| 307 | if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
|
---|
| 308 | log_msg(0, "running: %s", callstr);
|
---|
| 309 | log_msg(0,
|
---|
| 310 | "--------------------------------start of output-----------------------------");
|
---|
| 311 | }
|
---|
[2275] | 312 | mr_free(callstr);
|
---|
| 313 |
|
---|
| 314 | mr_asprintf(&callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
|
---|
[1644] | 315 | if (log_if_failure && system(callstr)) {
|
---|
[128] | 316 | log_OS_error("Command failed");
|
---|
| 317 | }
|
---|
[2275] | 318 | mr_free(callstr);
|
---|
| 319 |
|
---|
| 320 | mr_asprintf(&tmp1, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
|
---|
| 321 | unlink(tmp1);
|
---|
| 322 | mr_free(tmp1);
|
---|
| 323 |
|
---|
| 324 | mr_asprintf(&tmp1, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
|
---|
| 325 | fin = fopen(tmp1, "r");
|
---|
[128] | 326 | if (fin) {
|
---|
| 327 | for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
|
---|
| 328 | fgets(incoming, MAX_STR_LEN, fin)) {
|
---|
| 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 | strip_spaces(incoming);
|
---|
| 337 | if ((res == 0 && log_if_success)
|
---|
| 338 | || (res != 0 && log_if_failure)) {
|
---|
| 339 | log_msg(0, incoming);
|
---|
| 340 | }
|
---|
[1] | 341 | }
|
---|
[128] | 342 | paranoid_fclose(fin);
|
---|
[1] | 343 | }
|
---|
[2275] | 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 | }
|
---|
| 356 | return (res);
|
---|
[1] | 357 | }
|
---|
| 358 |
|
---|
| 359 |
|
---|
| 360 |
|
---|
| 361 | /**
|
---|
| 362 | * Run a program and log its output to the screen.
|
---|
| 363 | * @param basic_call The program to run.
|
---|
| 364 | * @param what_i_am_doing The title of the evalcall form.
|
---|
| 365 | * @return The return value of the command (varies, but 0 almost always means success).
|
---|
| 366 | * @see run_program_and_log_output
|
---|
| 367 | * @see log_to_screen
|
---|
| 368 | */
|
---|
[128] | 369 | int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
|
---|
[1] | 370 | {
|
---|
[128] | 371 | /*@ int ******************************************************** */
|
---|
| 372 | int retval = 0;
|
---|
| 373 | int res = 0;
|
---|
| 374 | int i;
|
---|
[1] | 375 |
|
---|
[128] | 376 | /*@ pointers **************************************************** */
|
---|
| 377 | FILE *fin;
|
---|
[1] | 378 |
|
---|
[128] | 379 | /*@ buffers **************************************************** */
|
---|
[2275] | 380 | char *tmp = NULL;
|
---|
| 381 | char *command = NULL;
|
---|
| 382 | char *lockfile = NULL;
|
---|
| 383 | char tmp1[MAX_STR_LEN *2];
|
---|
[1] | 384 |
|
---|
[128] | 385 | /*@ end vars *************************************************** */
|
---|
[1] | 386 |
|
---|
[128] | 387 | assert_string_is_neither_NULL_nor_zerolength(basic_call);
|
---|
[1] | 388 |
|
---|
[2275] | 389 | mr_asprintf(&lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
|
---|
[1644] | 390 |
|
---|
[2296] | 391 | mr_asprintf(&command, "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
|
---|
[128] | 392 | open_evalcall_form(what_i_am_doing);
|
---|
[2275] | 393 | mr_asprintf(&tmp, "Executing %s", basic_call);
|
---|
[128] | 394 | log_msg(2, tmp);
|
---|
[2275] | 395 | mr_free(tmp);
|
---|
| 396 |
|
---|
[128] | 397 | if (!(fin = popen(command, "r"))) {
|
---|
| 398 | log_OS_error("Unable to popen-in command");
|
---|
[2275] | 399 | mr_asprintf(&tmp, "Failed utterly to call '%s'", command);
|
---|
[128] | 400 | log_to_screen(tmp);
|
---|
[2275] | 401 | mr_free(tmp);
|
---|
| 402 | mr_free(command);
|
---|
| 403 | mr_free(lockfile);
|
---|
[128] | 404 | return (1);
|
---|
| 405 | }
|
---|
[2275] | 406 | mr_free(command);
|
---|
| 407 |
|
---|
[128] | 408 | if (!does_file_exist(lockfile)) {
|
---|
[541] | 409 | log_to_screen("Waiting for external binary to start");
|
---|
[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 | }
|
---|
[1] | 414 | #ifdef _XWIN
|
---|
[128] | 415 | /* This only can update when newline goes into the file,
|
---|
| 416 | but it's *much* prettier/faster on Qt. */
|
---|
| 417 | while (does_file_exist(lockfile)) {
|
---|
| 418 | while (!feof(fin)) {
|
---|
[2275] | 419 | if (!fgets(tmp1, 512, fin))
|
---|
[128] | 420 | break;
|
---|
[2275] | 421 | log_to_screen(tmp1);
|
---|
[128] | 422 | }
|
---|
| 423 | usleep(500000);
|
---|
| 424 | }
|
---|
[1] | 425 | #else
|
---|
[128] | 426 | /* This works on Newt, and it gives quicker updates. */
|
---|
| 427 | for (; does_file_exist(lockfile); sleep(1)) {
|
---|
| 428 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 429 | update_evalcall_form(1);
|
---|
| 430 | }
|
---|
[1] | 431 | #endif
|
---|
[154] | 432 | /* Evaluate the status returned by pclose to get the exit code of the called program. */
|
---|
| 433 | errno = 0;
|
---|
| 434 | res = pclose(fin);
|
---|
| 435 | /* Log actual pclose errors. */
|
---|
| 436 | if (errno) log_msg(5, "pclose err: %d", errno);
|
---|
| 437 | /* Check if we have a valid status. If we do, extract the called program's exit code. */
|
---|
| 438 | /* If we don't, highlight this fact by returning -1. */
|
---|
| 439 | if (WIFEXITED(res)) {
|
---|
| 440 | retval = WEXITSTATUS(res);
|
---|
| 441 | } else {
|
---|
| 442 | retval = -1;
|
---|
| 443 | }
|
---|
[128] | 444 | close_evalcall_form();
|
---|
| 445 | unlink(lockfile);
|
---|
[2275] | 446 | mr_free(lockfile);
|
---|
| 447 |
|
---|
[128] | 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
|
---|
[2275] | 467 | char *tmp = NULL;
|
---|
| 468 | char *tmp1 = NULL;
|
---|
| 469 | char *buf = NULL;
|
---|
| 470 | char *filestr = NULL;
|
---|
[128] | 471 | long int bytes_to_be_read, bytes_read_in, bytes_written_out =
|
---|
| 472 | 0, bufcap, subsliceno = 0;
|
---|
| 473 | int retval = 0;
|
---|
| 474 | FILE *fin;
|
---|
| 475 | FILE *fout;
|
---|
| 476 | FILE *ftmp;
|
---|
[2178] | 477 | int tmpcap = 512;
|
---|
[1] | 478 |
|
---|
[128] | 479 | log_msg(5, "Opening.");
|
---|
[2275] | 480 |
|
---|
[128] | 481 | bufcap = 256L * 1024L;
|
---|
| 482 | if (!(buf = malloc(bufcap))) {
|
---|
| 483 | fatal_error("Failed to malloc() buf");
|
---|
| 484 | }
|
---|
[1] | 485 |
|
---|
[128] | 486 | if (direction == 'w') {
|
---|
| 487 | fin = f_orig;
|
---|
| 488 | fout = f_archived;
|
---|
[2275] | 489 | mr_asprintf(&tmp, "%-64s", PIMP_START_SZ);
|
---|
[128] | 490 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
[2275] | 491 | mr_free(tmp);
|
---|
[128] | 492 | fatal_error("Can't write the introductory block");
|
---|
| 493 | }
|
---|
[2275] | 494 | mr_free(tmp);
|
---|
| 495 |
|
---|
[128] | 496 | while (1) {
|
---|
| 497 | bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
|
---|
| 498 | if (bytes_read_in == 0) {
|
---|
| 499 | break;
|
---|
| 500 | }
|
---|
[2275] | 501 | mr_asprintf(&tmp, "%-64ld", bytes_read_in);
|
---|
[128] | 502 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
[2275] | 503 | mr_free(tmp);
|
---|
[128] | 504 | fatal_error("Cannot write introductory block");
|
---|
| 505 | }
|
---|
[2275] | 506 | mr_free(tmp);
|
---|
| 507 |
|
---|
[128] | 508 | log_msg(7,
|
---|
| 509 | "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
|
---|
| 510 | subsliceno, bytes_read_in, bytes_to_be_read);
|
---|
| 511 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
[2275] | 512 | mr_asprintf(&tmp, "%-64ld", subsliceno);
|
---|
[128] | 513 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
[2275] | 514 | mr_free(tmp);
|
---|
[128] | 515 | fatal_error("Cannot write post-thingy block");
|
---|
| 516 | }
|
---|
[2275] | 517 | mr_free(tmp);
|
---|
[128] | 518 | log_msg(7, "Subslice #%d written OK", subsliceno);
|
---|
| 519 | subsliceno++;
|
---|
| 520 | }
|
---|
[2275] | 521 | mr_asprintf(&tmp, "%-64ld", 0L);
|
---|
[128] | 522 | if (fwrite(tmp, 1, 64L, fout) != 64L) {
|
---|
[2275] | 523 | mr_free(tmp);
|
---|
[128] | 524 | fatal_error("Cannot write final introductory block");
|
---|
| 525 | }
|
---|
[2275] | 526 | mr_free(tmp);
|
---|
| 527 |
|
---|
| 528 | mr_asprintf(&tmp, "%-64s", PIMP_END_SZ);
|
---|
| 529 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
| 530 | mr_free(tmp);
|
---|
| 531 | fatal_error("Can't write the final block");
|
---|
| 532 | }
|
---|
| 533 | mr_free(tmp);
|
---|
[128] | 534 | } else {
|
---|
| 535 | fin = f_archived;
|
---|
| 536 | fout = f_orig;
|
---|
[2275] | 537 | if (!(tmp1 = malloc(tmpcap))) {
|
---|
| 538 | fatal_error("Failed to malloc() tmp");
|
---|
| 539 | }
|
---|
| 540 | if (fread(tmp1, 1, 64L, fin) != 64L) {
|
---|
| 541 | mr_free(tmp1);
|
---|
[128] | 542 | fatal_error("Cannot read the introductory block");
|
---|
| 543 | }
|
---|
[2275] | 544 | log_msg(5, "tmp1 is %s", tmp1);
|
---|
| 545 | if (!strstr(tmp1, PIMP_START_SZ)) {
|
---|
| 546 | mr_free(tmp1);
|
---|
[128] | 547 | fatal_error("Can't find intro blk");
|
---|
| 548 | }
|
---|
[2275] | 549 | if (fread(tmp1, 1, 64L, fin) != 64L) {
|
---|
| 550 | mr_free(tmp1);
|
---|
[128] | 551 | fatal_error("Cannot read introductory blk");
|
---|
| 552 | }
|
---|
[2275] | 553 | bytes_to_be_read = atol(tmp1);
|
---|
[128] | 554 | while (bytes_to_be_read > 0) {
|
---|
[2275] | 555 | log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read);
|
---|
[128] | 556 | bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
|
---|
| 557 | if (bytes_read_in != bytes_to_be_read) {
|
---|
[2275] | 558 | mr_free(tmp1);
|
---|
| 559 | fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives.");
|
---|
[128] | 560 | }
|
---|
| 561 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
[2275] | 562 | if (fread(tmp1, 1, 64, fin) != 64) {
|
---|
| 563 | mr_free(tmp1);
|
---|
[128] | 564 | fatal_error("Cannot read post-thingy block");
|
---|
| 565 | }
|
---|
[2275] | 566 | if (atol(tmp1) != subsliceno) {
|
---|
| 567 | log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp1), tmp1);
|
---|
[128] | 568 | }
|
---|
| 569 | log_msg(7, "Subslice #%ld read OK", subsliceno);
|
---|
| 570 | subsliceno++;
|
---|
[2275] | 571 | if (fread(tmp1, 1, 64, fin) != 64) {
|
---|
| 572 | mr_free(tmp1);
|
---|
[128] | 573 | fatal_error("Cannot read introductory block");
|
---|
| 574 | }
|
---|
[2275] | 575 | bytes_to_be_read = atol(tmp1);
|
---|
[128] | 576 | }
|
---|
[2275] | 577 | log_msg(1, "tmpA is %s", tmp1);
|
---|
| 578 | if (!strstr(tmp1, PIMP_END_SZ)) {
|
---|
| 579 | if (fread(tmp1, 1, 64, fin) != 64) {
|
---|
| 580 | mr_free(tmp1);
|
---|
[128] | 581 | fatal_error("Can't read the final block");
|
---|
| 582 | }
|
---|
[2275] | 583 | log_msg(5, "tmpB is %s", tmp1);
|
---|
| 584 | if (!strstr(tmp1, PIMP_END_SZ)) {
|
---|
| 585 | mr_asprintf(&filestr, "%s/out.leftover", bkpinfo->tmpdir);
|
---|
[1644] | 586 | ftmp = fopen(filestr, "w");
|
---|
[2275] | 587 | mr_free(filestr);
|
---|
| 588 |
|
---|
| 589 | bytes_read_in = fread(tmp1, 1, 64, fin);
|
---|
[128] | 590 | log_msg(1, "bytes_read_in = %ld", bytes_read_in);
|
---|
[2275] | 591 |
|
---|
| 592 | fwrite(tmp1, 1, bytes_read_in, ftmp);
|
---|
| 593 | fread(tmp1, 1, tmpcap, fin);
|
---|
| 594 | log_msg(0, "tmp1 = '%s'", tmp1);
|
---|
| 595 | fwrite(tmp1, 1, tmpcap, ftmp);
|
---|
[128] | 596 | fclose(ftmp);
|
---|
[2275] | 597 | mr_free(tmp1);
|
---|
[128] | 598 | fatal_error("Missing terminating block");
|
---|
| 599 | }
|
---|
| 600 | }
|
---|
[2275] | 601 | mr_free(tmp1);
|
---|
[128] | 602 | }
|
---|
[1] | 603 |
|
---|
[128] | 604 | paranoid_free(buf);
|
---|
| 605 | log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
|
---|
| 606 | return (retval);
|
---|
[1] | 607 | }
|
---|
| 608 |
|
---|
| 609 |
|
---|
| 610 |
|
---|
| 611 |
|
---|
| 612 | /**
|
---|
[296] | 613 | * Feed @p input_device through ntfsclone to @p output_fname.
|
---|
[1] | 614 | * @param input_device The device to image.
|
---|
| 615 | * @param output_fname The file to write.
|
---|
| 616 | * @return 0 for success, nonzero for failure.
|
---|
| 617 | */
|
---|
[296] | 618 | int feed_into_ntfsprog(char *input_device, char *output_fname)
|
---|
[1] | 619 | {
|
---|
| 620 | // BACKUP
|
---|
[296] | 621 | int res = -1;
|
---|
[2275] | 622 | char*command = NULL;
|
---|
[1] | 623 |
|
---|
[128] | 624 | if (!does_file_exist(input_device)) {
|
---|
| 625 | fatal_error("input device does not exist");
|
---|
| 626 | }
|
---|
[296] | 627 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 628 | fatal_error("ntfsclone not found");
|
---|
[128] | 629 | }
|
---|
[2275] | 630 | mr_asprintf(&command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
|
---|
[296] | 631 | res = run_program_and_log_output(command, 5);
|
---|
[2275] | 632 | mr_free(command);
|
---|
| 633 |
|
---|
[411] | 634 | unlink(output_fname);
|
---|
[128] | 635 | return (res);
|
---|
[1] | 636 | }
|
---|
| 637 |
|
---|
| 638 |
|
---|
[128] | 639 | void *run_prog_in_bkgd_then_exit(void *info)
|
---|
[1] | 640 | {
|
---|
[128] | 641 | char *sz_command;
|
---|
| 642 | static int res = 4444;
|
---|
[1] | 643 |
|
---|
[128] | 644 | res = 999;
|
---|
| 645 | sz_command = (char *) info;
|
---|
| 646 | log_msg(4, "sz_command = '%s'", sz_command);
|
---|
| 647 | res = system(sz_command);
|
---|
| 648 | if (res > 256 && res != 4444) {
|
---|
| 649 | res = res / 256;
|
---|
| 650 | }
|
---|
| 651 | log_msg(4, "child res = %d", res);
|
---|
| 652 | sz_command[0] = '\0';
|
---|
| 653 | pthread_exit((void *) (&res));
|
---|
[1] | 654 | }
|
---|
| 655 |
|
---|
| 656 |
|
---|
| 657 |
|
---|
| 658 |
|
---|
[128] | 659 | int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
|
---|
[1] | 660 | {
|
---|
| 661 |
|
---|
[128] | 662 | /*@ int *************************************************************** */
|
---|
| 663 | int res = 0;
|
---|
[1] | 664 | int percentage = 0;
|
---|
| 665 | int maxpc = 100;
|
---|
| 666 | int pcno = 0;
|
---|
| 667 | int last_pcno = 0;
|
---|
[128] | 668 | int counter = 0;
|
---|
[1] | 669 |
|
---|
[128] | 670 | /*@ buffers *********************************************************** */
|
---|
[2275] | 671 | char *command = NULL;
|
---|
[128] | 672 | char *title;
|
---|
| 673 | /*@ pointers ********************************************************** */
|
---|
| 674 | static int chldres = 0;
|
---|
| 675 | int *pchild_result;
|
---|
| 676 | pthread_t childthread;
|
---|
[1] | 677 |
|
---|
[128] | 678 | pchild_result = &chldres;
|
---|
| 679 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
| 680 | assert_string_is_neither_NULL_nor_zerolength(tt);
|
---|
| 681 | *pchild_result = 999;
|
---|
[1] | 682 |
|
---|
[128] | 683 | malloc_string(title);
|
---|
| 684 | strcpy(title, tt);
|
---|
[2275] | 685 | mr_asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
|
---|
[128] | 686 | log_msg(3, "command = '%s'", command);
|
---|
[2275] | 687 | if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void *) command))) {
|
---|
[128] | 688 | fatal_error("Unable to create an archival thread");
|
---|
| 689 | }
|
---|
[1] | 690 |
|
---|
[128] | 691 | log_msg(8, "Parent running");
|
---|
| 692 | open_evalcall_form(title);
|
---|
| 693 | for (sleep(1); command[0] != '\0'; sleep(1)) {
|
---|
| 694 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
| 695 | if (pcno <= 0 || pcno > 100) {
|
---|
| 696 | log_msg(8, "Weird pc#");
|
---|
| 697 | continue;
|
---|
| 698 | }
|
---|
| 699 | percentage = pcno * 100 / maxpc;
|
---|
| 700 | if (pcno <= 5 && last_pcno >= 40) {
|
---|
| 701 | close_evalcall_form();
|
---|
[541] | 702 | strcpy(title, "Verifying...");
|
---|
[128] | 703 | open_evalcall_form(title);
|
---|
| 704 | }
|
---|
| 705 | if (counter++ >= 5) {
|
---|
| 706 | counter = 0;
|
---|
| 707 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 708 | }
|
---|
| 709 | last_pcno = pcno;
|
---|
| 710 | update_evalcall_form(percentage);
|
---|
[1] | 711 | }
|
---|
[2275] | 712 | mr_free(command);
|
---|
| 713 |
|
---|
[128] | 714 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 715 | close_evalcall_form();
|
---|
| 716 | pthread_join(childthread, (void *) (&pchild_result));
|
---|
| 717 | if (pchild_result) {
|
---|
| 718 | res = *pchild_result;
|
---|
| 719 | } else {
|
---|
| 720 | res = 666;
|
---|
[1] | 721 | }
|
---|
[128] | 722 | log_msg(3, "Parent res = %d", res);
|
---|
| 723 | paranoid_free(title);
|
---|
| 724 | return (res);
|
---|
[1] | 725 | }
|
---|
| 726 |
|
---|
| 727 |
|
---|
| 728 |
|
---|
| 729 |
|
---|
| 730 | /**
|
---|
[296] | 731 | * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
|
---|
| 732 | * @param input_fifo The ntfsclone file to read.
|
---|
[1] | 733 | * @param output_device Where to put the output.
|
---|
[296] | 734 | * @return The return value of ntfsclone (0 for success).
|
---|
[1] | 735 | */
|
---|
[296] | 736 | int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
|
---|
[1] | 737 | {
|
---|
| 738 | // RESTORE
|
---|
[296] | 739 | int res = -1;
|
---|
[2275] | 740 | char *command = NULL;
|
---|
[1] | 741 |
|
---|
[296] | 742 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 743 | fatal_error("ntfsclone not found");
|
---|
[128] | 744 | }
|
---|
[2275] | 745 | mr_asprintf(&command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
|
---|
[296] | 746 | res = run_program_and_log_output(command, 5);
|
---|
[2275] | 747 | mr_free(command);
|
---|
[128] | 748 | return (res);
|
---|
[1] | 749 | }
|
---|