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