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