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