1 | /* libmondo-fork.c |
---|
2 | $Id: libmondo-fork.c 128 2005-11-19 01:27:41Z bcornec $ |
---|
3 | |
---|
4 | - subroutines for handling forking/pthreads/etc. |
---|
5 | |
---|
6 | |
---|
7 | 06/20/2004 |
---|
8 | - create fifo /var/log/partimagehack-debug.log and empty it |
---|
9 | to keep ramdisk from filling up |
---|
10 | |
---|
11 | 04/13/2004 |
---|
12 | - >= should be <= g_loglevel |
---|
13 | |
---|
14 | 11/15/2003 |
---|
15 | - changed a few []s to char*s |
---|
16 | |
---|
17 | 10/12 |
---|
18 | - rewrote partimagehack handling (multiple fifos, chunks, etc.) |
---|
19 | |
---|
20 | 10/11 |
---|
21 | - partimagehack now has debug level of N (set in my-stuff.h) |
---|
22 | |
---|
23 | 10/08 |
---|
24 | - call to partimagehack when restoring will now log errors to /var/log/....log |
---|
25 | |
---|
26 | 10/06 |
---|
27 | - cleaned up logging a bit |
---|
28 | |
---|
29 | 09/30 |
---|
30 | - line 735 - missing char* cmd in sprintf() |
---|
31 | |
---|
32 | 09/28 |
---|
33 | - added run_external_binary_with_percentage_indicator() |
---|
34 | - rewritten eval_call_to_make_ISO() |
---|
35 | |
---|
36 | 09/18 |
---|
37 | - call mkstemp instead of mktemp |
---|
38 | |
---|
39 | 09/13 |
---|
40 | - major NTFS hackage |
---|
41 | |
---|
42 | 09/12 |
---|
43 | - paranoid_system("rm -f /tmp/ *PARTIMAGE*") before calling partimagehack |
---|
44 | |
---|
45 | 09/11 |
---|
46 | - forward-ported unbroken feed_*_partimage() subroutines |
---|
47 | from early August 2003 |
---|
48 | |
---|
49 | 09/08 |
---|
50 | - detect & use partimagehack if it exists |
---|
51 | |
---|
52 | 09/05 |
---|
53 | - finally finished partimagehack hack :) |
---|
54 | |
---|
55 | 07/04 |
---|
56 | - added subroutines to wrap around partimagehack |
---|
57 | |
---|
58 | 04/27 |
---|
59 | - don't echo (...res=%d...) at end of log_it() |
---|
60 | unnecessarily |
---|
61 | - replace newtFinished() and newtInit() with |
---|
62 | newtSuspend() and newtResume() |
---|
63 | |
---|
64 | 04/24 |
---|
65 | - added some assert()'s and log_OS_error()'s |
---|
66 | |
---|
67 | 04/09 |
---|
68 | - cleaned up run_program_and_log_output() |
---|
69 | |
---|
70 | 04/07 |
---|
71 | - cleaned up code a bit |
---|
72 | - let run_program_and_log_output() accept -1 (only log if _no error_) |
---|
73 | |
---|
74 | 01/02/2003 |
---|
75 | - in eval_call_to_make_ISO(), append output to MONDO_LOGFILE |
---|
76 | instead of a temporary stderr text file |
---|
77 | |
---|
78 | 12/10 |
---|
79 | - patch by Heiko Schlittermann to handle % chars in issue.net |
---|
80 | |
---|
81 | 11/18 |
---|
82 | - if mkisofs in eval_call_to_make_ISO() returns an error then return it, |
---|
83 | whether ISO was created or not |
---|
84 | |
---|
85 | 10/30 |
---|
86 | - if mkisofs in eval_call_to_make_ISO() returns an error then find out if |
---|
87 | the output (ISO) file has been created; if it has then return 0 anyway |
---|
88 | |
---|
89 | 08/01 - 09/30 |
---|
90 | - run_program_and_log_output() now takes boolean operator to specify |
---|
91 | whether it will log its activities in the event of _success_ |
---|
92 | - system() now includes 2>/dev/null |
---|
93 | - enlarged some tmp[]'s |
---|
94 | - added run_program_and_log_to_screen() and run_program_and_log_output() |
---|
95 | |
---|
96 | 07/24 |
---|
97 | - created |
---|
98 | */ |
---|
99 | |
---|
100 | |
---|
101 | #include "my-stuff.h" |
---|
102 | #include "mondostructures.h" |
---|
103 | #include "libmondo-fork.h" |
---|
104 | #include "libmondo-string-EXT.h" |
---|
105 | #include "libmondo-gui-EXT.h" |
---|
106 | #include "libmondo-files-EXT.h" |
---|
107 | #include "libmondo-tools-EXT.h" |
---|
108 | #include "lib-common-externs.h" |
---|
109 | |
---|
110 | /*@unused@*/ |
---|
111 | //static char cvsid[] = "$Id: libmondo-fork.c 128 2005-11-19 01:27:41Z bcornec $"; |
---|
112 | |
---|
113 | extern char *g_tmpfs_mountpt; |
---|
114 | extern t_bkptype g_backup_media_type; |
---|
115 | extern bool g_text_mode; |
---|
116 | pid_t g_buffer_pid = 0; |
---|
117 | |
---|
118 | |
---|
119 | /** |
---|
120 | * Call a program and retrieve its last line of output. |
---|
121 | * @param call The program to run. |
---|
122 | * @return The last line of its output. |
---|
123 | * @note The returned value points to static storage that will be overwritten with each call. |
---|
124 | */ |
---|
125 | char *call_program_and_get_last_line_of_output(char *call) |
---|
126 | { |
---|
127 | /*@ buffers ***************************************************** */ |
---|
128 | static char result[512]; |
---|
129 | char *tmp; |
---|
130 | |
---|
131 | /*@ pointers **************************************************** */ |
---|
132 | FILE *fin; |
---|
133 | |
---|
134 | /*@ initialize data ********************************************* */ |
---|
135 | malloc_string(tmp); |
---|
136 | result[0] = '\0'; |
---|
137 | tmp[0] = '\0'; |
---|
138 | |
---|
139 | /*@******************************************************************** */ |
---|
140 | |
---|
141 | assert_string_is_neither_NULL_nor_zerolength(call); |
---|
142 | if ((fin = popen(call, "r"))) { |
---|
143 | for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin); |
---|
144 | fgets(tmp, MAX_STR_LEN, fin)) { |
---|
145 | if (strlen(tmp) > 1) { |
---|
146 | strcpy(result, tmp); |
---|
147 | } |
---|
148 | } |
---|
149 | paranoid_pclose(fin); |
---|
150 | } else { |
---|
151 | log_OS_error("Unable to popen call"); |
---|
152 | } |
---|
153 | strip_spaces(result); |
---|
154 | return (result); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | #define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag." |
---|
163 | |
---|
164 | /** |
---|
165 | * Call mkisofs to create an ISO image. |
---|
166 | * @param bkpinfo The backup information structure. Fields used: |
---|
167 | * - @c bkpinfo->manual_cd_tray |
---|
168 | * - @c bkpinfo->backup_media_type |
---|
169 | * - @c bkpinfo->please_dont_eject_when_restoring |
---|
170 | * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are: |
---|
171 | * - @c _ISO_ will become the ISO file (@p isofile) |
---|
172 | * - @c _CD#_ becomes the CD number (@p cd_no) |
---|
173 | * - @c _ERR_ becomes the logfile (@p g_logfile) |
---|
174 | * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs). |
---|
175 | * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number. |
---|
176 | * @param logstub Unused. |
---|
177 | * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog. |
---|
178 | * @return Exit code of @c mkisofs (0 is success, anything else indicates failure). |
---|
179 | * @bug @p logstub is unused. |
---|
180 | */ |
---|
181 | int |
---|
182 | eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo, |
---|
183 | char *basic_call, char *isofile, |
---|
184 | int cd_no, char *logstub, char *what_i_am_doing) |
---|
185 | { |
---|
186 | |
---|
187 | /*@ int's *** */ |
---|
188 | int retval = 0; |
---|
189 | |
---|
190 | |
---|
191 | /*@ buffers *** */ |
---|
192 | char *midway_call, *ultimate_call, *tmp, *command, *incoming, |
---|
193 | *old_stderr, *cd_number_str; |
---|
194 | char *p; |
---|
195 | |
---|
196 | /*@*********** End Variables ***************************************/ |
---|
197 | |
---|
198 | log_msg(3, "Starting"); |
---|
199 | assert(bkpinfo != NULL); |
---|
200 | assert_string_is_neither_NULL_nor_zerolength(basic_call); |
---|
201 | assert_string_is_neither_NULL_nor_zerolength(isofile); |
---|
202 | assert_string_is_neither_NULL_nor_zerolength(logstub); |
---|
203 | if (!(midway_call = malloc(1200))) { |
---|
204 | fatal_error("Cannot malloc midway_call"); |
---|
205 | } |
---|
206 | if (!(ultimate_call = malloc(1200))) { |
---|
207 | fatal_error("Cannot malloc ultimate_call"); |
---|
208 | } |
---|
209 | if (!(tmp = malloc(1200))) { |
---|
210 | fatal_error("Cannot malloc tmp"); |
---|
211 | } |
---|
212 | if (!(command = malloc(1200))) { |
---|
213 | fatal_error("Cannot malloc command"); |
---|
214 | } |
---|
215 | malloc_string(incoming); |
---|
216 | malloc_string(old_stderr); |
---|
217 | malloc_string(cd_number_str); |
---|
218 | |
---|
219 | incoming[0] = '\0'; |
---|
220 | old_stderr[0] = '\0'; |
---|
221 | |
---|
222 | sprintf(cd_number_str, "%d", cd_no); |
---|
223 | resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_"); |
---|
224 | resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_"); |
---|
225 | resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_"); |
---|
226 | log_msg(4, "basic call = '%s'", basic_call); |
---|
227 | log_msg(4, "midway_call = '%s'", midway_call); |
---|
228 | log_msg(4, "tmp = '%s'", tmp); |
---|
229 | log_msg(4, "ultimate call = '%s'", ultimate_call); |
---|
230 | sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE); |
---|
231 | |
---|
232 | log_to_screen |
---|
233 | ("Please be patient. Do not be alarmed by on-screen inactivity."); |
---|
234 | log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'", |
---|
235 | what_i_am_doing); |
---|
236 | strcpy(tmp, command); |
---|
237 | if (bkpinfo->manual_cd_tray) { |
---|
238 | p = strstr(tmp, "2>>"); |
---|
239 | if (p) { |
---|
240 | sprintf(p, " "); |
---|
241 | while (*p == ' ') { |
---|
242 | p++; |
---|
243 | } |
---|
244 | for (; *p != ' '; p++) { |
---|
245 | *p = ' '; |
---|
246 | } |
---|
247 | } |
---|
248 | strcpy(command, tmp); |
---|
249 | #ifndef _XWIN |
---|
250 | if (!g_text_mode) { |
---|
251 | newtSuspend(); |
---|
252 | } |
---|
253 | #endif |
---|
254 | log_msg(1, "command = '%s'", command); |
---|
255 | retval += system(command); |
---|
256 | if (!g_text_mode) { |
---|
257 | newtResume(); |
---|
258 | } |
---|
259 | if (retval) { |
---|
260 | log_msg(2, "Basic call '%s' returned an error.", basic_call); |
---|
261 | popup_and_OK("Press ENTER to continue."); |
---|
262 | popup_and_OK |
---|
263 | ("mkisofs and/or cdrecord returned an error. CD was not created"); |
---|
264 | } |
---|
265 | } |
---|
266 | /* if text mode then do the above & RETURN; if not text mode, do this... */ |
---|
267 | else { |
---|
268 | log_msg(3, "command = '%s'", command); |
---|
269 | // yes_this_is_a_goto: |
---|
270 | retval = |
---|
271 | run_external_binary_with_percentage_indicator_NEW |
---|
272 | (what_i_am_doing, command); |
---|
273 | } |
---|
274 | |
---|
275 | paranoid_free(midway_call); |
---|
276 | paranoid_free(ultimate_call); |
---|
277 | paranoid_free(tmp); |
---|
278 | paranoid_free(command); |
---|
279 | paranoid_free(incoming); |
---|
280 | paranoid_free(old_stderr); |
---|
281 | paranoid_free(cd_number_str); |
---|
282 | /* |
---|
283 | if (bkpinfo->backup_media_type == dvd && !bkpinfo->please_dont_eject_when_restoring) |
---|
284 | { |
---|
285 | log_msg(3, "Ejecting DVD device"); |
---|
286 | eject_device(bkpinfo->media_device); |
---|
287 | } |
---|
288 | */ |
---|
289 | return (retval); |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | /** |
---|
296 | * Run a program and log its output (stdout and stderr) to the logfile. |
---|
297 | * @param program The program to run. Passed to the shell, so you can use pipes etc. |
---|
298 | * @param debug_level If @p g_loglevel is higher than this, do not log the output. |
---|
299 | * @return The exit code of @p program (depends on the command, but 0 almost always indicates success). |
---|
300 | */ |
---|
301 | int run_program_and_log_output(char *program, int debug_level) |
---|
302 | { |
---|
303 | /*@ buffer ****************************************************** */ |
---|
304 | char callstr[MAX_STR_LEN * 2]; |
---|
305 | char incoming[MAX_STR_LEN * 2]; |
---|
306 | char tmp[MAX_STR_LEN * 2]; |
---|
307 | char initial_label[MAX_STR_LEN * 2]; |
---|
308 | |
---|
309 | /*@ int ********************************************************* */ |
---|
310 | int res; |
---|
311 | int i; |
---|
312 | int len; |
---|
313 | bool log_if_failure = FALSE; |
---|
314 | bool log_if_success = FALSE; |
---|
315 | |
---|
316 | /*@ pointers *************************************************** */ |
---|
317 | FILE *fin; |
---|
318 | char *p; |
---|
319 | |
---|
320 | /*@ end vars *************************************************** */ |
---|
321 | |
---|
322 | assert(program != NULL); |
---|
323 | if (!program[0]) { |
---|
324 | log_msg(2, "Warning - asked to run zerolength program"); |
---|
325 | return (1); |
---|
326 | } |
---|
327 | // if (debug_level == TRUE) { debug_level=5; } |
---|
328 | |
---|
329 | // assert_string_is_neither_NULL_nor_zerolength(program); |
---|
330 | |
---|
331 | if (debug_level <= g_loglevel) { |
---|
332 | log_if_success = TRUE; |
---|
333 | log_if_failure = TRUE; |
---|
334 | } |
---|
335 | sprintf(callstr, |
---|
336 | "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err", |
---|
337 | program); |
---|
338 | while ((p = strchr(callstr, '\r'))) { |
---|
339 | *p = ' '; |
---|
340 | } |
---|
341 | while ((p = strchr(callstr, '\n'))) { |
---|
342 | *p = ' '; |
---|
343 | } /* single '=' is intentional */ |
---|
344 | |
---|
345 | |
---|
346 | len = (int) strlen(program); |
---|
347 | for (i = 0; i < 35 - len / 2; i++) { |
---|
348 | tmp[i] = '-'; |
---|
349 | } |
---|
350 | tmp[i] = '\0'; |
---|
351 | strcat(tmp, " "); |
---|
352 | strcat(tmp, program); |
---|
353 | strcat(tmp, " "); |
---|
354 | for (i = 0; i < 35 - len / 2; i++) { |
---|
355 | strcat(tmp, "-"); |
---|
356 | } |
---|
357 | strcpy(initial_label, tmp); |
---|
358 | res = system(callstr); |
---|
359 | if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) { |
---|
360 | log_msg(0, "running: %s", callstr); |
---|
361 | log_msg(0, |
---|
362 | "--------------------------------start of output-----------------------------"); |
---|
363 | } |
---|
364 | if (log_if_failure |
---|
365 | && |
---|
366 | system |
---|
367 | ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null")) |
---|
368 | { |
---|
369 | log_OS_error("Command failed"); |
---|
370 | } |
---|
371 | unlink("/tmp/mondo-run-prog-thing.err"); |
---|
372 | fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r"); |
---|
373 | if (fin) { |
---|
374 | for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin); |
---|
375 | fgets(incoming, MAX_STR_LEN, fin)) { |
---|
376 | /* patch by Heiko Schlittermann */ |
---|
377 | p = incoming; |
---|
378 | while (p && *p) { |
---|
379 | if ((p = strchr(p, '%'))) { |
---|
380 | memmove(p, p + 1, strlen(p) + 1); |
---|
381 | p += 2; |
---|
382 | } |
---|
383 | } |
---|
384 | /* end of patch */ |
---|
385 | strip_spaces(incoming); |
---|
386 | if ((res == 0 && log_if_success) |
---|
387 | || (res != 0 && log_if_failure)) { |
---|
388 | log_msg(0, incoming); |
---|
389 | } |
---|
390 | } |
---|
391 | paranoid_fclose(fin); |
---|
392 | } |
---|
393 | unlink("/tmp/mondo-run-prog-thing.tmp"); |
---|
394 | if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) { |
---|
395 | log_msg(0, |
---|
396 | "--------------------------------end of output------------------------------"); |
---|
397 | if (res) { |
---|
398 | log_msg(0, "...ran with res=%d", res); |
---|
399 | } else { |
---|
400 | log_msg(0, "...ran just fine. :-)"); |
---|
401 | } |
---|
402 | } |
---|
403 | // else |
---|
404 | // { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); } |
---|
405 | return (res); |
---|
406 | } |
---|
407 | |
---|
408 | |
---|
409 | |
---|
410 | /** |
---|
411 | * Run a program and log its output to the screen. |
---|
412 | * @param basic_call The program to run. |
---|
413 | * @param what_i_am_doing The title of the evalcall form. |
---|
414 | * @return The return value of the command (varies, but 0 almost always means success). |
---|
415 | * @see run_program_and_log_output |
---|
416 | * @see log_to_screen |
---|
417 | */ |
---|
418 | int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing) |
---|
419 | { |
---|
420 | /*@ int ******************************************************** */ |
---|
421 | int retval = 0; |
---|
422 | int res = 0; |
---|
423 | int i; |
---|
424 | |
---|
425 | /*@ pointers **************************************************** */ |
---|
426 | FILE *fin; |
---|
427 | |
---|
428 | /*@ buffers **************************************************** */ |
---|
429 | char tmp[MAX_STR_LEN * 2]; |
---|
430 | char command[MAX_STR_LEN * 2]; |
---|
431 | char lockfile[MAX_STR_LEN]; |
---|
432 | |
---|
433 | /*@ end vars *************************************************** */ |
---|
434 | |
---|
435 | assert_string_is_neither_NULL_nor_zerolength(basic_call); |
---|
436 | |
---|
437 | sprintf(lockfile, "/tmp/mojo-jojo.blah.XXXXXX"); |
---|
438 | mkstemp(lockfile); |
---|
439 | sprintf(command, |
---|
440 | "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", |
---|
441 | lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile); |
---|
442 | open_evalcall_form(what_i_am_doing); |
---|
443 | sprintf(tmp, "Executing %s", basic_call); |
---|
444 | log_msg(2, tmp); |
---|
445 | if (!(fin = popen(command, "r"))) { |
---|
446 | log_OS_error("Unable to popen-in command"); |
---|
447 | sprintf(tmp, "Failed utterly to call '%s'", command); |
---|
448 | log_to_screen(tmp); |
---|
449 | return (1); |
---|
450 | } |
---|
451 | if (!does_file_exist(lockfile)) { |
---|
452 | log_to_screen("Waiting for external binary to start"); |
---|
453 | for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) { |
---|
454 | log_msg(3, "Waiting for lockfile %s to exist", lockfile); |
---|
455 | } |
---|
456 | } |
---|
457 | #ifdef _XWIN |
---|
458 | /* This only can update when newline goes into the file, |
---|
459 | but it's *much* prettier/faster on Qt. */ |
---|
460 | while (does_file_exist(lockfile)) { |
---|
461 | while (!feof(fin)) { |
---|
462 | if (!fgets(tmp, 512, fin)) |
---|
463 | break; |
---|
464 | log_to_screen(tmp); |
---|
465 | } |
---|
466 | usleep(500000); |
---|
467 | } |
---|
468 | #else |
---|
469 | /* This works on Newt, and it gives quicker updates. */ |
---|
470 | for (; does_file_exist(lockfile); sleep(1)) { |
---|
471 | log_file_end_to_screen(MONDO_LOGFILE, ""); |
---|
472 | update_evalcall_form(1); |
---|
473 | } |
---|
474 | #endif |
---|
475 | paranoid_pclose(fin); |
---|
476 | retval += res; |
---|
477 | close_evalcall_form(); |
---|
478 | unlink(lockfile); |
---|
479 | return (retval); |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | |
---|
484 | |
---|
485 | |
---|
486 | /** |
---|
487 | * Thread callback to run a command (partimage) in the background. |
---|
488 | * @param xfb A transfer block of @c char, containing: |
---|
489 | * - xfb:[0] A marker, should be set to 2. Decremented to 1 while the command is running and 0 when it's finished. |
---|
490 | * - xfb:[1] The command's return value, if xfb:[0] is 0. |
---|
491 | * - xfb+2: A <tt>NULL</tt>-terminated string containing the command to be run. |
---|
492 | * @return NULL to pthread_join. |
---|
493 | */ |
---|
494 | void *call_partimage_in_bkgd(void *xfb) |
---|
495 | { |
---|
496 | char *transfer_block; |
---|
497 | int retval = 0; |
---|
498 | |
---|
499 | g_buffer_pid = getpid(); |
---|
500 | unlink("/tmp/null"); |
---|
501 | log_msg(1, "starting"); |
---|
502 | transfer_block = (char *) xfb; |
---|
503 | transfer_block[0]--; // should now be 1 |
---|
504 | retval = system(transfer_block + 2); |
---|
505 | if (retval) { |
---|
506 | log_OS_error("partimage returned an error"); |
---|
507 | } |
---|
508 | transfer_block[1] = retval; |
---|
509 | transfer_block[0]--; // should now be 0 |
---|
510 | g_buffer_pid = 0; |
---|
511 | log_msg(1, "returning"); |
---|
512 | pthread_exit(NULL); |
---|
513 | } |
---|
514 | |
---|
515 | |
---|
516 | /** |
---|
517 | * File to touch if we want partimage to wait for us. |
---|
518 | */ |
---|
519 | #define PAUSE_PARTIMAGE_FNAME "/tmp/PAUSE-PARTIMAGE-FOR-MONDO" |
---|
520 | |
---|
521 | /** |
---|
522 | * Apparently unused. @bug This has a purpose, but what? |
---|
523 | */ |
---|
524 | #define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8" |
---|
525 | #define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka" |
---|
526 | |
---|
527 | /** |
---|
528 | * Marker to start the next subvolume for Partimage. |
---|
529 | */ |
---|
530 | #define NEXT_SUBVOL_PLEASE "I-grew-up-on-the-crime-side,-the-New-York-Times-side,-where-staying-alive-was-no-jive" |
---|
531 | |
---|
532 | /** |
---|
533 | * Marker to end the partimage file. |
---|
534 | */ |
---|
535 | #define NO_MORE_SUBVOLS "On-second-hand,momma-bounced-on-old-man,-and-so-we-moved-to-Shaolin-Land." |
---|
536 | |
---|
537 | int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction) |
---|
538 | { |
---|
539 | // if dir=='w' then copy from orig to archived |
---|
540 | // if dir=='r' then copy from archived to orig |
---|
541 | char *tmp; |
---|
542 | char *buf; |
---|
543 | long int bytes_to_be_read, bytes_read_in, bytes_written_out = |
---|
544 | 0, bufcap, subsliceno = 0; |
---|
545 | int retval = 0; |
---|
546 | FILE *fin; |
---|
547 | FILE *fout; |
---|
548 | FILE *ftmp; |
---|
549 | |
---|
550 | log_msg(5, "Opening."); |
---|
551 | malloc_string(tmp); |
---|
552 | tmp[0] = '\0'; |
---|
553 | bufcap = 256L * 1024L; |
---|
554 | if (!(buf = malloc(bufcap))) { |
---|
555 | fatal_error("Failed to malloc() buf"); |
---|
556 | } |
---|
557 | |
---|
558 | if (direction == 'w') { |
---|
559 | fin = f_orig; |
---|
560 | fout = f_archived; |
---|
561 | sprintf(tmp, "%-64s", PIMP_START_SZ); |
---|
562 | if (fwrite(tmp, 1, 64, fout) != 64) { |
---|
563 | fatal_error("Can't write the introductory block"); |
---|
564 | } |
---|
565 | while (1) { |
---|
566 | bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin); |
---|
567 | if (bytes_read_in == 0) { |
---|
568 | break; |
---|
569 | } |
---|
570 | sprintf(tmp, "%-64ld", bytes_read_in); |
---|
571 | if (fwrite(tmp, 1, 64, fout) != 64) { |
---|
572 | fatal_error("Cannot write introductory block"); |
---|
573 | } |
---|
574 | log_msg(7, |
---|
575 | "subslice #%ld --- I have read %ld of %ld bytes in from f_orig", |
---|
576 | subsliceno, bytes_read_in, bytes_to_be_read); |
---|
577 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); |
---|
578 | sprintf(tmp, "%-64ld", subsliceno); |
---|
579 | if (fwrite(tmp, 1, 64, fout) != 64) { |
---|
580 | fatal_error("Cannot write post-thingy block"); |
---|
581 | } |
---|
582 | log_msg(7, "Subslice #%d written OK", subsliceno); |
---|
583 | subsliceno++; |
---|
584 | } |
---|
585 | sprintf(tmp, "%-64ld", 0L); |
---|
586 | if (fwrite(tmp, 1, 64L, fout) != 64L) { |
---|
587 | fatal_error("Cannot write final introductory block"); |
---|
588 | } |
---|
589 | } else { |
---|
590 | fin = f_archived; |
---|
591 | fout = f_orig; |
---|
592 | if (fread(tmp, 1, 64L, fin) != 64L) { |
---|
593 | fatal_error("Cannot read the introductory block"); |
---|
594 | } |
---|
595 | log_msg(5, "tmp is %s", tmp); |
---|
596 | if (!strstr(tmp, PIMP_START_SZ)) { |
---|
597 | fatal_error("Can't find intro blk"); |
---|
598 | } |
---|
599 | if (fread(tmp, 1, 64L, fin) != 64L) { |
---|
600 | fatal_error("Cannot read introductory blk"); |
---|
601 | } |
---|
602 | bytes_to_be_read = atol(tmp); |
---|
603 | while (bytes_to_be_read > 0) { |
---|
604 | log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, |
---|
605 | bytes_to_be_read); |
---|
606 | bytes_read_in = fread(buf, 1, bytes_to_be_read, fin); |
---|
607 | if (bytes_read_in != bytes_to_be_read) { |
---|
608 | fatal_error |
---|
609 | ("Danger, WIll Robinson. Failed to read whole subvol from archives."); |
---|
610 | } |
---|
611 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); |
---|
612 | if (fread(tmp, 1, 64, fin) != 64) { |
---|
613 | fatal_error("Cannot read post-thingy block"); |
---|
614 | } |
---|
615 | if (atol(tmp) != subsliceno) { |
---|
616 | log_msg(1, "Wanted subslice %ld but got %ld ('%s')", |
---|
617 | subsliceno, atol(tmp), tmp); |
---|
618 | } |
---|
619 | log_msg(7, "Subslice #%ld read OK", subsliceno); |
---|
620 | subsliceno++; |
---|
621 | if (fread(tmp, 1, 64, fin) != 64) { |
---|
622 | fatal_error("Cannot read introductory block"); |
---|
623 | } |
---|
624 | bytes_to_be_read = atol(tmp); |
---|
625 | } |
---|
626 | } |
---|
627 | |
---|
628 | // log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in); |
---|
629 | |
---|
630 | if (direction == 'w') { |
---|
631 | sprintf(tmp, "%-64s", PIMP_END_SZ); |
---|
632 | if (fwrite(tmp, 1, 64, fout) != 64) { |
---|
633 | fatal_error("Can't write the final block"); |
---|
634 | } |
---|
635 | } else { |
---|
636 | log_msg(1, "tmpA is %s", tmp); |
---|
637 | if (!strstr(tmp, PIMP_END_SZ)) { |
---|
638 | if (fread(tmp, 1, 64, fin) != 64) { |
---|
639 | fatal_error("Can't read the final block"); |
---|
640 | } |
---|
641 | log_msg(5, "tmpB is %s", tmp); |
---|
642 | if (!strstr(tmp, PIMP_END_SZ)) { |
---|
643 | ftmp = fopen("/tmp/out.leftover", "w"); |
---|
644 | bytes_read_in = fread(tmp, 1, 64, fin); |
---|
645 | log_msg(1, "bytes_read_in = %ld", bytes_read_in); |
---|
646 | // if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); } |
---|
647 | fwrite(tmp, 1, bytes_read_in, ftmp); |
---|
648 | sprintf(tmp, "I am here - %ld", ftell(fin)); |
---|
649 | // log_msg(0, tmp); |
---|
650 | fread(tmp, 1, 512, fin); |
---|
651 | log_msg(0, "tmp = '%s'", tmp); |
---|
652 | fwrite(tmp, 1, 512, ftmp); |
---|
653 | fclose(ftmp); |
---|
654 | fatal_error("Missing terminating block"); |
---|
655 | } |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | paranoid_free(buf); |
---|
660 | paranoid_free(tmp); |
---|
661 | log_msg(3, "Successfully copied %ld bytes", bytes_written_out); |
---|
662 | return (retval); |
---|
663 | } |
---|
664 | |
---|
665 | |
---|
666 | /** |
---|
667 | * Call partimage from @p input_device to @p output_fname. |
---|
668 | * @param input_device The device to read. |
---|
669 | * @param output_fname The file to write. |
---|
670 | * @return 0 for success, nonzero for failure. |
---|
671 | */ |
---|
672 | int dynamically_create_pipes_and_copy_from_them_to_output_file(char |
---|
673 | *input_device, |
---|
674 | char |
---|
675 | *output_fname) |
---|
676 | { |
---|
677 | char *curr_fifo; |
---|
678 | char *prev_fifo; |
---|
679 | char *next_fifo; |
---|
680 | char *command; |
---|
681 | char *sz_call_to_partimage; |
---|
682 | int fifo_number = 0; |
---|
683 | struct stat buf; |
---|
684 | pthread_t partimage_thread; |
---|
685 | int res = 0; |
---|
686 | char *tmpstub; |
---|
687 | FILE *fout; |
---|
688 | FILE *fin; |
---|
689 | char *tmp; |
---|
690 | |
---|
691 | malloc_string(tmpstub); |
---|
692 | malloc_string(curr_fifo); |
---|
693 | malloc_string(prev_fifo); |
---|
694 | malloc_string(next_fifo); |
---|
695 | malloc_string(command); |
---|
696 | malloc_string(sz_call_to_partimage); |
---|
697 | malloc_string(tmp); |
---|
698 | |
---|
699 | log_msg(1, "g_tmpfs_mountpt = %s", g_tmpfs_mountpt); |
---|
700 | if (g_tmpfs_mountpt && g_tmpfs_mountpt[0] |
---|
701 | && does_file_exist(g_tmpfs_mountpt)) { |
---|
702 | strcpy(tmpstub, g_tmpfs_mountpt); |
---|
703 | } else { |
---|
704 | strcpy(tmpstub, "/tmp"); |
---|
705 | } |
---|
706 | paranoid_system("rm -f /tmp/*PARTIMAGE*"); |
---|
707 | sprintf(command, "rm -Rf %s/pih-fifo-*", tmpstub); |
---|
708 | paranoid_system(command); |
---|
709 | sprintf(tmpstub + strlen(tmpstub), "/pih-fifo-%ld", |
---|
710 | (long int) random()); |
---|
711 | mkfifo(tmpstub, S_IRWXU | S_IRWXG); // never used, though... |
---|
712 | sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); |
---|
713 | sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); |
---|
714 | mkfifo(curr_fifo, S_IRWXU | S_IRWXG); |
---|
715 | mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage |
---|
716 | sz_call_to_partimage[0] = 2; |
---|
717 | sz_call_to_partimage[1] = 0; |
---|
718 | sprintf(sz_call_to_partimage + 2, |
---|
719 | "partimagehack " PARTIMAGE_PARAMS |
---|
720 | " save %s %s > /tmp/stdout 2> /tmp/stderr", input_device, |
---|
721 | tmpstub); |
---|
722 | log_msg(5, "curr_fifo = %s", curr_fifo); |
---|
723 | log_msg(5, "next_fifo = %s", next_fifo); |
---|
724 | log_msg(5, "sz_call_to_partimage call is '%s'", |
---|
725 | sz_call_to_partimage + 2); |
---|
726 | if (!lstat(output_fname, &buf) && S_ISREG(buf.st_mode)) { |
---|
727 | log_msg(5, "Deleting %s", output_fname); |
---|
728 | unlink(output_fname); |
---|
729 | } |
---|
730 | if (!(fout = fopen(output_fname, "w"))) { |
---|
731 | fatal_error("Unable to openout to output_fname"); |
---|
732 | } |
---|
733 | res = |
---|
734 | pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, |
---|
735 | (void *) sz_call_to_partimage); |
---|
736 | if (res) { |
---|
737 | fatal_error("Failed to create thread to call partimage"); |
---|
738 | } |
---|
739 | log_msg(1, "Running fore/back at same time"); |
---|
740 | log_to_screen("Working with partimagehack..."); |
---|
741 | while (sz_call_to_partimage[0] > 0) { |
---|
742 | sprintf(tmp, "%s\n", NEXT_SUBVOL_PLEASE); |
---|
743 | if (fwrite(tmp, 1, 128, fout) != 128) { |
---|
744 | fatal_error("Cannot write interim block"); |
---|
745 | } |
---|
746 | log_msg(5, "fifo_number=%d", fifo_number); |
---|
747 | log_msg(4, "Cat'ting %s", curr_fifo); |
---|
748 | if (!(fin = fopen(curr_fifo, "r"))) { |
---|
749 | fatal_error("Unable to openin from fifo"); |
---|
750 | } |
---|
751 | // if (!sz_call_to_partimage[0]) { break; } |
---|
752 | log_msg(5, "Deleting %s", prev_fifo); |
---|
753 | unlink(prev_fifo); // just in case |
---|
754 | copy_from_src_to_dest(fin, fout, 'w'); |
---|
755 | paranoid_fclose(fin); |
---|
756 | fifo_number++; |
---|
757 | strcpy(prev_fifo, curr_fifo); |
---|
758 | strcpy(curr_fifo, next_fifo); |
---|
759 | log_msg(5, "Creating %s", next_fifo); |
---|
760 | sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); |
---|
761 | mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo exists before we cat this one |
---|
762 | system("sync"); |
---|
763 | sleep(5); |
---|
764 | } |
---|
765 | sprintf(tmp, "%s\n", NO_MORE_SUBVOLS); |
---|
766 | if (fwrite(tmp, 1, 128, fout) != 128) { |
---|
767 | fatal_error("Cannot write interim block"); |
---|
768 | } |
---|
769 | if (fwrite(tmp, 1, 128, fout) != 128) { |
---|
770 | fatal_error("Cannot write interim block"); |
---|
771 | } |
---|
772 | if (fwrite(tmp, 1, 128, fout) != 128) { |
---|
773 | fatal_error("Cannot write interim block"); |
---|
774 | } |
---|
775 | if (fwrite(tmp, 1, 128, fout) != 128) { |
---|
776 | fatal_error("Cannot write interim block"); |
---|
777 | } |
---|
778 | paranoid_fclose(fout); |
---|
779 | log_to_screen("Cleaning up after partimagehack..."); |
---|
780 | log_msg(3, "Final fifo_number=%d", fifo_number); |
---|
781 | paranoid_system("sync"); |
---|
782 | unlink(next_fifo); |
---|
783 | unlink(curr_fifo); |
---|
784 | unlink(prev_fifo); |
---|
785 | log_to_screen("Finished cleaning up."); |
---|
786 | |
---|
787 | // if (!lstat(sz_wait_for_this_file, &statbuf)) |
---|
788 | // { log_msg(3, "WARNING! %s was not processed.", sz_wait_for_this_file); } |
---|
789 | log_msg(2, "Waiting for pthread_join() to join."); |
---|
790 | pthread_join(partimage_thread, NULL); |
---|
791 | res = sz_call_to_partimage[1]; |
---|
792 | log_msg(2, "pthread_join() joined OK."); |
---|
793 | log_msg(1, "Partimagehack(save) returned %d", res); |
---|
794 | unlink(tmpstub); |
---|
795 | paranoid_free(curr_fifo); |
---|
796 | paranoid_free(prev_fifo); |
---|
797 | paranoid_free(next_fifo); |
---|
798 | paranoid_free(tmpstub); |
---|
799 | paranoid_free(tmp); |
---|
800 | paranoid_free(command); |
---|
801 | return (res); |
---|
802 | } |
---|
803 | |
---|
804 | |
---|
805 | /** |
---|
806 | * Feed @p input_device through partimage to @p output_fname. |
---|
807 | * @param input_device The device to image. |
---|
808 | * @param output_fname The file to write. |
---|
809 | * @return 0 for success, nonzero for failure. |
---|
810 | */ |
---|
811 | int feed_into_partimage(char *input_device, char *output_fname) |
---|
812 | { |
---|
813 | // BACKUP |
---|
814 | int res; |
---|
815 | |
---|
816 | if (!does_file_exist(input_device)) { |
---|
817 | fatal_error("input device does not exist"); |
---|
818 | } |
---|
819 | if (!find_home_of_exe("partimagehack")) { |
---|
820 | fatal_error("partimagehack not found"); |
---|
821 | } |
---|
822 | res = |
---|
823 | dynamically_create_pipes_and_copy_from_them_to_output_file |
---|
824 | (input_device, output_fname); |
---|
825 | return (res); |
---|
826 | } |
---|
827 | |
---|
828 | |
---|
829 | |
---|
830 | |
---|
831 | |
---|
832 | int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd) |
---|
833 | { |
---|
834 | |
---|
835 | /*@ int *************************************************************** */ |
---|
836 | int res = 0; |
---|
837 | int percentage = 0; |
---|
838 | int maxpc = 0; |
---|
839 | int pcno = 0; |
---|
840 | int last_pcno = 0; |
---|
841 | |
---|
842 | /*@ buffers *********************************************************** */ |
---|
843 | char *command; |
---|
844 | char *tempfile; |
---|
845 | char *title; |
---|
846 | /*@ pointers ********************************************************** */ |
---|
847 | FILE *pin; |
---|
848 | |
---|
849 | malloc_string(title); |
---|
850 | malloc_string(command); |
---|
851 | malloc_string(tempfile); |
---|
852 | assert_string_is_neither_NULL_nor_zerolength(cmd); |
---|
853 | assert_string_is_neither_NULL_nor_zerolength(title); |
---|
854 | |
---|
855 | strcpy(title, tt); |
---|
856 | strcpy(tempfile, |
---|
857 | call_program_and_get_last_line_of_output |
---|
858 | ("mktemp -q /tmp/mondo.XXXXXXXX")); |
---|
859 | sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile, |
---|
860 | tempfile); |
---|
861 | log_msg(3, command); |
---|
862 | open_evalcall_form(title); |
---|
863 | if (!(pin = popen(command, "r"))) { |
---|
864 | log_OS_error("fmt err"); |
---|
865 | return (1); |
---|
866 | } |
---|
867 | maxpc = 100; |
---|
868 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD |
---|
869 | for (sleep(1); does_file_exist(tempfile); sleep(1)) { |
---|
870 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE); |
---|
871 | if (pcno < 0 || pcno > 100) { |
---|
872 | log_msg(5, "Weird pc#"); |
---|
873 | continue; |
---|
874 | } |
---|
875 | percentage = pcno * 100 / maxpc; |
---|
876 | if (pcno <= 5 && last_pcno > 40) { |
---|
877 | close_evalcall_form(); |
---|
878 | strcpy(title, "Verifying..."); |
---|
879 | open_evalcall_form(title); |
---|
880 | } |
---|
881 | last_pcno = pcno; |
---|
882 | update_evalcall_form(percentage); |
---|
883 | } |
---|
884 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD |
---|
885 | close_evalcall_form(); |
---|
886 | if (pclose(pin)) { |
---|
887 | res++; |
---|
888 | log_OS_error("Unable to pclose"); |
---|
889 | } |
---|
890 | unlink(tempfile); |
---|
891 | paranoid_free(command); |
---|
892 | paranoid_free(tempfile); |
---|
893 | paranoid_free(title); |
---|
894 | return (res); |
---|
895 | } |
---|
896 | |
---|
897 | |
---|
898 | |
---|
899 | |
---|
900 | void *run_prog_in_bkgd_then_exit(void *info) |
---|
901 | { |
---|
902 | char *sz_command; |
---|
903 | static int res = 4444; |
---|
904 | |
---|
905 | res = 999; |
---|
906 | sz_command = (char *) info; |
---|
907 | log_msg(4, "sz_command = '%s'", sz_command); |
---|
908 | res = system(sz_command); |
---|
909 | if (res > 256 && res != 4444) { |
---|
910 | res = res / 256; |
---|
911 | } |
---|
912 | log_msg(4, "child res = %d", res); |
---|
913 | sz_command[0] = '\0'; |
---|
914 | pthread_exit((void *) (&res)); |
---|
915 | } |
---|
916 | |
---|
917 | |
---|
918 | |
---|
919 | |
---|
920 | int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) |
---|
921 | { |
---|
922 | |
---|
923 | /*@ int *************************************************************** */ |
---|
924 | int res = 0; |
---|
925 | int percentage = 0; |
---|
926 | int maxpc = 100; |
---|
927 | int pcno = 0; |
---|
928 | int last_pcno = 0; |
---|
929 | int counter = 0; |
---|
930 | |
---|
931 | /*@ buffers *********************************************************** */ |
---|
932 | char *command; |
---|
933 | char *title; |
---|
934 | /*@ pointers ********************************************************** */ |
---|
935 | static int chldres = 0; |
---|
936 | int *pchild_result; |
---|
937 | pthread_t childthread; |
---|
938 | |
---|
939 | pchild_result = &chldres; |
---|
940 | assert_string_is_neither_NULL_nor_zerolength(cmd); |
---|
941 | assert_string_is_neither_NULL_nor_zerolength(tt); |
---|
942 | *pchild_result = 999; |
---|
943 | |
---|
944 | malloc_string(title); |
---|
945 | malloc_string(command); |
---|
946 | strcpy(title, tt); |
---|
947 | sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE); |
---|
948 | log_msg(3, "command = '%s'", command); |
---|
949 | if ((res = |
---|
950 | pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, |
---|
951 | (void *) command))) { |
---|
952 | fatal_error("Unable to create an archival thread"); |
---|
953 | } |
---|
954 | |
---|
955 | log_msg(8, "Parent running"); |
---|
956 | open_evalcall_form(title); |
---|
957 | for (sleep(1); command[0] != '\0'; sleep(1)) { |
---|
958 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE); |
---|
959 | if (pcno <= 0 || pcno > 100) { |
---|
960 | log_msg(8, "Weird pc#"); |
---|
961 | continue; |
---|
962 | } |
---|
963 | percentage = pcno * 100 / maxpc; |
---|
964 | if (pcno <= 5 && last_pcno >= 40) { |
---|
965 | close_evalcall_form(); |
---|
966 | strcpy(title, "Verifying..."); |
---|
967 | open_evalcall_form(title); |
---|
968 | } |
---|
969 | if (counter++ >= 5) { |
---|
970 | counter = 0; |
---|
971 | log_file_end_to_screen(MONDO_LOGFILE, ""); |
---|
972 | } |
---|
973 | last_pcno = pcno; |
---|
974 | update_evalcall_form(percentage); |
---|
975 | } |
---|
976 | log_file_end_to_screen(MONDO_LOGFILE, ""); |
---|
977 | close_evalcall_form(); |
---|
978 | pthread_join(childthread, (void *) (&pchild_result)); |
---|
979 | if (pchild_result) { |
---|
980 | res = *pchild_result; |
---|
981 | } else { |
---|
982 | res = 666; |
---|
983 | } |
---|
984 | log_msg(3, "Parent res = %d", res); |
---|
985 | paranoid_free(command); |
---|
986 | paranoid_free(title); |
---|
987 | return (res); |
---|
988 | } |
---|
989 | |
---|
990 | |
---|
991 | |
---|
992 | #define PIH_LOG "/var/log/partimage-debug.log" |
---|
993 | |
---|
994 | |
---|
995 | /** |
---|
996 | * Feed @p input_fifo through partimage (restore) to @p output_device. |
---|
997 | * @param input_fifo The partimage file to read. |
---|
998 | * @param output_device Where to put the output. |
---|
999 | * @return The return value of partimagehack (0 for success). |
---|
1000 | * @bug Probably unnecessary, as the partimage is just a sparse file. We could use @c dd to restore it. |
---|
1001 | */ |
---|
1002 | int feed_outfrom_partimage(char *output_device, char *input_fifo) |
---|
1003 | { |
---|
1004 | // RESTORE |
---|
1005 | char *tmp; |
---|
1006 | // char *command; |
---|
1007 | char *stuff; |
---|
1008 | char *sz_call_to_partimage; |
---|
1009 | pthread_t partimage_thread; |
---|
1010 | int res; |
---|
1011 | char *curr_fifo; |
---|
1012 | char *prev_fifo; |
---|
1013 | char *oldest_fifo; |
---|
1014 | char *next_fifo; |
---|
1015 | char *afternxt_fifo; |
---|
1016 | int fifo_number = 0; |
---|
1017 | char *tmpstub; |
---|
1018 | FILE *fin; |
---|
1019 | FILE *fout; |
---|
1020 | |
---|
1021 | malloc_string(curr_fifo); |
---|
1022 | malloc_string(prev_fifo); |
---|
1023 | malloc_string(next_fifo); |
---|
1024 | malloc_string(afternxt_fifo); |
---|
1025 | malloc_string(oldest_fifo); |
---|
1026 | malloc_string(tmp); |
---|
1027 | sz_call_to_partimage = malloc(1000); |
---|
1028 | malloc_string(stuff); |
---|
1029 | malloc_string(tmpstub); |
---|
1030 | |
---|
1031 | log_msg(1, "output_device=%s", output_device); |
---|
1032 | log_msg(1, "input_fifo=%s", input_fifo); |
---|
1033 | /* I don't trust g_tmpfs_mountpt |
---|
1034 | if (g_tmpfs_mountpt[0]) |
---|
1035 | { |
---|
1036 | strcpy(tmpstub, g_tmpfs_mountpt); |
---|
1037 | } |
---|
1038 | else |
---|
1039 | { |
---|
1040 | */ |
---|
1041 | strcpy(tmpstub, "/tmp"); |
---|
1042 | // } |
---|
1043 | log_msg(1, "tmpstub was %s", tmpstub); |
---|
1044 | strcpy(stuff, tmpstub); |
---|
1045 | sprintf(tmpstub, "%s/pih-fifo-%ld", stuff, (long int) random()); |
---|
1046 | log_msg(1, "tmpstub is now %s", tmpstub); |
---|
1047 | unlink("/tmp/PARTIMAGEHACK-POSITION"); |
---|
1048 | unlink(PAUSE_PARTIMAGE_FNAME); |
---|
1049 | paranoid_system("rm -f /tmp/*PARTIMAGE*"); |
---|
1050 | sprintf(curr_fifo, "%s.%03d", tmpstub, fifo_number); |
---|
1051 | sprintf(next_fifo, "%s.%03d", tmpstub, fifo_number + 1); |
---|
1052 | sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2); |
---|
1053 | mkfifo(PIH_LOG, S_IRWXU | S_IRWXG); |
---|
1054 | mkfifo(curr_fifo, S_IRWXU | S_IRWXG); |
---|
1055 | mkfifo(next_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we call partimage |
---|
1056 | mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); |
---|
1057 | system("cat " PIH_LOG " > /dev/null &"); |
---|
1058 | log_msg(3, "curr_fifo = %s", curr_fifo); |
---|
1059 | log_msg(3, "next_fifo = %s", next_fifo); |
---|
1060 | if (!does_file_exist(input_fifo)) { |
---|
1061 | fatal_error("input fifo does not exist"); |
---|
1062 | } |
---|
1063 | if (!(fin = fopen(input_fifo, "r"))) { |
---|
1064 | fatal_error("Unable to openin from input_fifo"); |
---|
1065 | } |
---|
1066 | if (!find_home_of_exe("partimagehack")) { |
---|
1067 | fatal_error("partimagehack not found"); |
---|
1068 | } |
---|
1069 | sz_call_to_partimage[0] = 2; |
---|
1070 | sz_call_to_partimage[1] = 0; |
---|
1071 | sprintf(sz_call_to_partimage + 2, |
---|
1072 | "partimagehack " PARTIMAGE_PARAMS |
---|
1073 | " restore %s %s > /dev/null 2>> %s", output_device, curr_fifo, |
---|
1074 | MONDO_LOGFILE); |
---|
1075 | log_msg(1, "output_device = %s", output_device); |
---|
1076 | log_msg(1, "curr_fifo = %s", curr_fifo); |
---|
1077 | log_msg(1, "sz_call_to_partimage+2 = %s", sz_call_to_partimage + 2); |
---|
1078 | res = |
---|
1079 | pthread_create(&partimage_thread, NULL, call_partimage_in_bkgd, |
---|
1080 | (void *) sz_call_to_partimage); |
---|
1081 | if (res) { |
---|
1082 | fatal_error("Failed to create thread to call partimage"); |
---|
1083 | } |
---|
1084 | log_msg(1, "Running fore/back at same time"); |
---|
1085 | log_msg(2, " Trying to openin %s", input_fifo); |
---|
1086 | if (!does_file_exist(input_fifo)) { |
---|
1087 | log_msg(2, "Warning - %s does not exist", input_fifo); |
---|
1088 | } |
---|
1089 | while (!does_file_exist("/tmp/PARTIMAGEHACK-POSITION")) { |
---|
1090 | log_msg(6, "Waiting for partimagehack (restore) to start"); |
---|
1091 | sleep(1); |
---|
1092 | } |
---|
1093 | while (sz_call_to_partimage[0] > 0) { |
---|
1094 | if (fread(tmp, 1, 128, fin) != 128) { |
---|
1095 | fatal_error("Cannot read introductory block"); |
---|
1096 | } |
---|
1097 | if (strstr(tmp, NEXT_SUBVOL_PLEASE)) { |
---|
1098 | log_msg(2, "Great. Next subvol coming up."); |
---|
1099 | } else if (strstr(tmp, NO_MORE_SUBVOLS)) { |
---|
1100 | log_msg(2, "Great. That was the last subvol."); |
---|
1101 | break; |
---|
1102 | } else { |
---|
1103 | log_msg(2, "WTF is this? '%s'", tmp); |
---|
1104 | fatal_error("Unknown interim block"); |
---|
1105 | } |
---|
1106 | if (feof(fin)) { |
---|
1107 | log_msg(1, "Eof(fin) detected. Breaking."); |
---|
1108 | break; |
---|
1109 | } |
---|
1110 | log_msg(3, "Processing subvol %d", fifo_number); |
---|
1111 | log_msg(5, "fifo_number=%d", fifo_number); |
---|
1112 | if (!(fout = fopen(curr_fifo, "w"))) { |
---|
1113 | fatal_error("Cannot openout to curr_fifo"); |
---|
1114 | } |
---|
1115 | log_msg(6, "Deleting %s", oldest_fifo); |
---|
1116 | copy_from_src_to_dest(fout, fin, 'r'); |
---|
1117 | paranoid_fclose(fout); |
---|
1118 | fifo_number++; |
---|
1119 | unlink(oldest_fifo); // just in case |
---|
1120 | strcpy(oldest_fifo, prev_fifo); |
---|
1121 | strcpy(prev_fifo, curr_fifo); |
---|
1122 | strcpy(curr_fifo, next_fifo); |
---|
1123 | strcpy(next_fifo, afternxt_fifo); |
---|
1124 | sprintf(afternxt_fifo, "%s.%03d", tmpstub, fifo_number + 2); |
---|
1125 | log_msg(6, "Creating %s", afternxt_fifo); |
---|
1126 | mkfifo(afternxt_fifo, S_IRWXU | S_IRWXG); // make sure _next_ fifo already exists before we access current fifo |
---|
1127 | fflush(fin); |
---|
1128 | // system("sync"); |
---|
1129 | usleep(1000L * 100L); |
---|
1130 | } |
---|
1131 | paranoid_fclose(fin); |
---|
1132 | paranoid_system("sync"); |
---|
1133 | log_msg(1, "Partimagehack has finished. Great. Fin-closing."); |
---|
1134 | log_msg(1, "Waiting for pthread_join"); |
---|
1135 | pthread_join(partimage_thread, NULL); |
---|
1136 | res = sz_call_to_partimage[1]; |
---|
1137 | log_msg(1, "Yay. Partimagehack (restore) returned %d", res); |
---|
1138 | unlink(prev_fifo); |
---|
1139 | unlink(curr_fifo); |
---|
1140 | unlink(next_fifo); |
---|
1141 | unlink(afternxt_fifo); |
---|
1142 | unlink(PIH_LOG); |
---|
1143 | paranoid_free(tmp); |
---|
1144 | paranoid_free(sz_call_to_partimage); |
---|
1145 | paranoid_free(stuff); |
---|
1146 | paranoid_free(prev_fifo); |
---|
1147 | paranoid_free(curr_fifo); |
---|
1148 | paranoid_free(next_fifo); |
---|
1149 | paranoid_free(afternxt_fifo); |
---|
1150 | paranoid_free(oldest_fifo); |
---|
1151 | paranoid_free(tmpstub); |
---|
1152 | return (res); |
---|
1153 | } |
---|