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