source: MondoRescue/trunk/mondo/mondo/mondoarchive/main.c@ 58

Last change on this file since 58 was 30, checked in by bcornec, 19 years ago

Id property added on files to allow for better conf. management

  • Property svn:keywords set to Id
File size: 17.3 KB
Line 
1/***************************************************************************
2 main.c - description
3 -------------------
4 begin : Fri Apr 19 16:40:35 EDT 2002
5 copyright : (C) 2002 by Stan Benoit
6 email : troff@nakedsoul.org
7 cvsid : $Id: main.c 30 2005-09-28 23:32:28Z bcornec $
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19/** change log ****** MONDO-DEVEL
20
21
2212/10
23- disable stopping/starting of autofs
24
2510/01
26- update g_erase_tmpdir_and_scratchdir to delete user-specified tmpdir, scratchdir
27
2806/19
29- added AUX_VER
30
3106/14/2004
32- use mondorescue.iso, not mindi.iso
33
3402/10/2004
35- tell users where BusyBox's sources are
36
3711/14/2003
38- cleaned up logging at end#
39
4010/23
41- don't try to test-read tape ... That's already
42 handled by post_param_configuration()
43
4410/19
45- if your PATH var is too long, abort
46
4709/23
48- added some comments
49- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
50 and free_libmondo_global_strings() - which are in libmondo-tools.c
51- better magicdev support
52
53
5409/16
55- delete /var/log/partimagehack-debug.log at start of main()
56
5709/15
58- added askbootloader
59
6009/09
61- if your tape is weird, I'll pause between backup and verify
62- fixed silly bug in main() - re: say_at_end
63
6401/01 - 08/31
65- call 'dmesg -n1' at start, to shut the kernel logger up
66- moved g_erase_tmpdir_and_scratchdir to common/newt-specific.c
67- added 'don't panic' msg to start of logfile
68- added 'nice(20)' to main()
69- added lots of assert()'s and log_OS_error()'s
70- clean-up (Hugo)
71- make post_param_configuration() setup g_erase_tmpdir_and_scratchdir
72- if --version then print & exit quickly
73- re-run g_erase_tmpdir_and_scratchdir via system() at very end
74
75Year: 2002
76- if user goes root with 'su' instead of 'su -' then
77 workaround it by setting PATH correctly
78- wipe mondoarchive.log at very beginning
79- cleaned up code
80- if changed.files.N exists then copy to changes.files for display
81- run_program_and_log_output() now takes boolean operator to specify
82 whether it will log its activities in the event of _success_
83- added popup list of changed files
84- removed 'beta-quality' warnings
85- if kernel not found and mondo in graphics mode then popup and ask
86 for kernel path+filename
87- fixed tmp[] 'too small' bug
88- unmount and eject CD at end of verify cycle
89- moved interactively_obtain...() to libmondo-stream.c
90- wrote stuff to autodetect tape+cdrw+etc.
91- renamed from main.c to mondo-archive.c
92- fore+after warnings that this code is beta-quality
93- abort if running from ramdisk
94- remount floppy at end & unmount at start if Mandrake
95- took out #debug stuff
96- add 2> /dev/null to 'find' command
97- add support for bkpinfo->nonbootable_backup
98- add main function begin comment and debug conditional
99 compilation - Stan Benoit
100- add debug statements to build a run tree. Stan Benoit
101**** end change log **********/
102
103
104/**
105 * @file
106 * The main file for mondoarchive.
107 */
108
109/************************* #include statements *************************/
110#include <pthread.h>
111//#include <config.h>
112//#include "../../config.h"
113#include <stdio.h>
114#include <stdlib.h>
115#include "../common/my-stuff.h"
116#include "../common/mondostructures.h"
117#include "../common/libmondo.h"
118#include "mondo-cli-EXT.h"
119
120#ifndef VERSION
121#define VERSION AUX_VER
122#endif
123
124
125char g_version[] = VERSION;
126
127
128// for CVS
129//static char cvsid[] = "$Id: main.c 30 2005-09-28 23:32:28Z bcornec $";
130
131/************************* external variables *************************/
132extern void set_signals(int);
133extern int g_current_media_number;
134extern void register_pid(pid_t, char*);
135extern int g_currentY;
136extern bool g_text_mode;
137extern char *g_boot_mountpt;
138extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
139extern char *g_mondo_home;
140extern char *g_tmpfs_mountpt;
141extern char *g_erase_tmpdir_and_scratchdir;
142extern char *g_cdrw_drive_is_here;
143static char *g_cdrom_drive_is_here=NULL;
144static char *g_dvd_drive_is_here=NULL;
145extern double g_kernel_version;
146
147/***************** global vars, used only by main.c ******************/
148bool g_skip_floppies;
149long diffs;
150
151extern t_bkptype g_backup_media_type;
152extern int g_loglevel;
153
154/****************** subroutines used only by main.c ******************/
155
156
157/**
158 * Print a "don't panic" message to the log and a message about the logfile to the screen.
159 */
160void welcome_to_mondoarchive()
161{
162 log_msg (0, "Mondo Archive v%s --- http://www.mondorescue.org", VERSION);
163 log_msg (0, "running on %s architecture",get_architecture());
164 log_msg (0,"-----------------------------------------------------------");
165 log_msg (0, "NB: Mondo logs almost everything, so don't panic if you see");
166 log_msg (0, "some error messages. Please read them carefully before you");
167 log_msg (0, "decide to break out in a cold sweat. Despite (or perhaps");
168 log_msg (0, "because of) the wealth of messages. some users are inclined");
169 log_msg (0, "to stop reading this log. If Mondo stopped for some reason,");
170 log_msg (0, "chances are it's detailed here. More than likely there's a");
171 log_msg (0, "message at the very end of this log that will tell you what");
172 log_msg (0, "is wrong. Please read it! -Devteam");
173 log_msg (0,"-----------------------------------------------------------");
174
175 log_msg (0, "Zero...");
176 log_msg (1, "One...");
177 log_msg (2, "Two...");
178 log_msg (3, "Three...");
179 log_msg (4, "Four...");
180 log_msg (5, "Five...");
181 log_msg (6, "Six...");
182 log_msg (7, "Seven...");
183 log_msg (8, "Eight...");
184 printf ("See %s for details of backup run.\n", MONDO_LOGFILE);
185}
186
187
188extern char *g_magicdev_command;
189
190/**
191 * Do whatever is necessary to insure a successful backup on the Linux distribution
192 * of the day.
193 */
194void distro_specific_kludges_at_start_of_mondoarchive()
195{
196 log_msg (2, "Unmounting old ramdisks if necessary");
197 stop_magicdev_if_necessary(); // for RH+Gnome users
198 run_program_and_log_output("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
199 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
200 // stop_autofs_if_necessary(); // for Xandros users
201 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
202 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
203}
204
205
206
207/**
208 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
209 */
210void distro_specific_kludges_at_end_of_mondoarchive()
211{
212// char tmp[500];
213 log_msg( 2, "Restarting magicdev if necessary");
214 sync();
215 restart_magicdev_if_necessary(); // for RH+Gnome users
216
217 log_msg( 2, "Restarting autofs if necessary");
218 sync();
219 // restart_autofs_if_necessary(); // for Xandros users
220
221 log_msg( 2, "Restarting supermounts if necessary");
222 sync();
223 remount_supermounts_if_necessary(); // for Mandrake users
224
225 log_msg( 2, "Unmounting /boot if necessary");
226 sync();
227 unmount_boot_if_necessary(); // for Gentoo users
228
229// log_msg( 2, "Cleaning up KDE desktop");
230// clean_up_KDE_desktop_if_necessary();
231}
232
233
234/*-----------------------------------------------------------*/
235
236
237
238/**
239 * Backup/verify the user's data.
240 * What did you think it did, anyway? :-)
241 */
242int
243main (int argc, char *argv[])
244{
245 struct s_bkpinfo *bkpinfo;
246 char *tmp;
247 int res, retval;
248 char *say_at_end;
249
250/* Make sure I'm root; abort if not */
251 if (getuid () != 0)
252 {
253 fprintf (stderr, "Please run as root.\r\n");
254 exit (127);
255 }
256
257/* If -V, -v or --version then echo version no. and quit */
258 if (argc==2 && (!strcmp(argv[argc-1], "-v") || !strcmp(argv[argc-1], "-V") || !strcmp(argv[argc-1], "--version")))
259 { printf("mondoarchive v%s\nSee man page for help\n", VERSION); exit(0); }
260
261/* Initialize variables */
262
263 malloc_libmondo_global_strings();
264 malloc_string(tmp);
265 malloc_string(say_at_end);
266
267 res = 0;
268 retval = 0;
269 diffs = 0;
270 say_at_end[0] = '\0';
271 unlink("/var/log/partimagehack-debug.log");
272 printf ("Initializing...\n");
273 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) { fatal_error("Cannot malloc bkpinfo"); }
274
275
276/* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
277 strncpy(tmp, getenv("PATH"), MAX_STR_LEN-1);
278 tmp[MAX_STR_LEN-1] = '\0';
279 if (strlen(tmp)>=MAX_STR_LEN-33)
280 { fatal_error("Your PATH environmental variable is too long. Please shorten it."); }
281 strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
282 setenv("PATH", tmp, 1);
283
284/* Add the ARCH environment variable for ia64 purposes */
285 strncpy(tmp, get_architecture(), MAX_STR_LEN-1);
286 tmp[MAX_STR_LEN-1] = '\0';
287 setenv("ARCH", tmp, 1);
288
289 unlink(MONDO_LOGFILE);
290
291/* Configure the bkpinfo structure, global file paths, etc. */
292 g_main_pid = getpid();
293 log_msg(9, "This");
294
295 register_pid(g_main_pid, "mondo");
296 set_signals(TRUE); // catch SIGTERM, etc.
297 nice(10);
298 run_program_and_log_output("dmesg -n1", TRUE);
299
300 log_msg(9, "Next");
301 welcome_to_mondoarchive();
302 distro_specific_kludges_at_start_of_mondoarchive();
303 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
304 g_kernel_version = get_kernel_version();
305
306 if (argc==4 && !strcmp(argv[1], "getfattr"))
307 {
308 g_loglevel = 10;
309 g_text_mode = TRUE;
310 setup_newt_stuff();
311 if (!strstr(argv[2], "filelist"))
312 {
313 printf("Sorry - filelist goes first\n");
314 finish(1);
315 }
316 else
317 {
318 finish(get_fattr_list(argv[2], argv[3]));
319 }
320 finish(0);
321 }
322 if (argc==4 && !strcmp(argv[1], "setfattr"))
323 {
324 g_loglevel = 10;
325// chdir("/tmp");
326 g_text_mode = TRUE;
327 setup_newt_stuff();
328 finish(set_fattr_list(argv[2], argv[3]));
329 }
330
331 if (argc==3 && !strcmp(argv[1], "wildcards"))
332 {
333 g_loglevel = 10;
334 g_text_mode = TRUE;
335 setup_newt_stuff();
336 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
337 printf("in=%s; out=%s\n", argv[2], tmp);
338 finish(1);
339 }
340
341 if (argc==4 && !strcmp(argv[1], "getfacl"))
342 {
343 g_loglevel = 10;
344 g_text_mode = TRUE;
345 setup_newt_stuff();
346 if (!strstr(argv[2], "filelist"))
347 {
348 printf("Sorry - filelist goes first\n");
349 finish(1);
350 }
351 else
352 {
353 finish(get_acl_list(argv[2], argv[3]));
354 }
355 finish(0);
356 }
357 if (argc==4 && !strcmp(argv[1], "setfacl"))
358 {
359 g_loglevel = 10;
360// chdir("/tmp");
361 g_text_mode = TRUE;
362 setup_newt_stuff();
363 finish(set_acl_list(argv[2], argv[3]));
364 }
365
366 if (argc>2 && !strcmp(argv[1], "find-cd"))
367 {
368 g_loglevel = 10;
369 g_text_mode = TRUE;
370 setup_newt_stuff();
371 if (find_cdrw_device(tmp))
372 { printf("Failed to find CDR-RW drive\n"); }
373 else
374 { printf("CD-RW is at %s\n", tmp); }
375 tmp[0] = '\0';
376 if (find_cdrom_device(tmp, atoi(argv[2])))
377 { printf("Failed to find CD-ROM drive\n"); }
378 else
379 { printf("CD-ROM is at %s\n", tmp); }
380 finish(0);
381 }
382
383 if (argc>2 && !strcmp(argv[1], "find-dvd"))
384 {
385 g_loglevel = 10;
386 g_text_mode = TRUE;
387 setup_newt_stuff();
388 if (find_dvd_device(tmp, atoi(argv[2])))
389 { printf("Failed to find DVD drive\n"); }
390 else
391 { printf("DVD is at %s\n", tmp); }
392 finish(0);
393 }
394
395 if (argc>2 && !strcmp(argv[1], "disksize"))
396 {
397 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
398 finish(0);
399 }
400 if (argc>2 && !strcmp(argv[1], "test-dev"))
401 {
402 if (is_dev_an_NTFS_dev(argv[2]))
403 { printf("%s is indeed an NTFS dev\n", argv[2]); }
404 else
405 { printf("%s is _not_ an NTFS dev\n", argv[2]); }
406 finish(0);
407 }
408
409 if (pre_param_configuration(bkpinfo)) { fatal_error("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting..."); }
410
411/* Process command line, if there is one. If not, ask user for info. */
412 if (argc==1)
413 {
414 g_text_mode = FALSE;
415 setup_newt_stuff ();
416 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
417 if (res) { fatal_error("Syntax error. Please review the parameters you have supplied and try again."); }
418 }
419 else
420 {
421 res = handle_incoming_parameters (argc, argv, bkpinfo);
422 if (res) {
423 printf("Errors were detected in the command line you supplied.\n");
424 printf("Please review the log file - " MONDO_LOGFILE "\n");
425 log_msg(1, "Mondoarchive will now exit.");
426 finish(1);
427 }
428 setup_newt_stuff ();
429 }
430
431/* Finish configuring global structures */
432 if (post_param_configuration (bkpinfo)) { fatal_error("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting..."); }
433
434 log_to_screen("BusyBox's sources are available from http://www.busybox.net");
435 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
436
437 /* If we're meant to backup then backup */
438 if (bkpinfo->backup_data)
439 {
440/*
441 log_to_screen("INFERNAL PORPOISES");
442 res = archive_this_fileset_with_star(bkpinfo, "/tmp/filelist.0", "/tmp/0.star.bz2", 0);
443 log_to_screen("atfws returned %d", res);
444 finish(0);
445*/
446 res = backup_data(bkpinfo);
447 retval += res;
448 if (res)
449 { strcat (say_at_end, "Data archived. Please check the logs, just as a precaution. "); }
450 else
451 { strcat (say_at_end, "Data archived OK. "); }
452 }
453
454/* If we're meant to verify then verify */
455 if (bkpinfo->verify_data)
456 {
457 res = verify_data(bkpinfo);
458 if (res<0)
459 {
460 sprintf(tmp, "%d difference%c found.", -res, (-res != 1) ? 's' : ' ');
461 strcat(say_at_end, tmp);
462 log_to_screen(tmp);
463 res=0;
464 }
465 retval += res;
466 }
467
468/* Offer to write floppy disk images to physical disks */
469 if (bkpinfo->backup_data && !g_skip_floppies)
470 {
471 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
472 retval += res;
473// res = offer_to_write_boot_ISO_to_physical_CD(bkpinfo);
474// retval += res;
475 }
476
477/* Report result of entire operation (success? errors?) */
478 if (!retval)
479 {
480 mvaddstr_and_log_it (g_currentY++, 0,
481 "Backup and/or verify ran to completion. Everything appears to be fine.");
482 }
483 else
484 {
485 mvaddstr_and_log_it (g_currentY++, 0,
486 "Backup and/or verify ran to completion. However, errors did occur.");
487 }
488
489 if (does_file_exist ("/root/images/mindi/mondorescue.iso"))
490 {
491 log_to_screen ("/root/images/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.");
492 }
493
494
495 if (length_of_file ("/tmp/changed.files") > 2)
496 {
497 if (g_text_mode) { log_to_screen("Type 'less /tmp/changed.files' to see which files don't match the archives"); }
498 else
499 {
500 log_msg (1, "Type 'less /tmp/changed.files' to see which files don't match the archives");
501 log_msg (2, "Calling popup_changelist_from_file()");
502 popup_changelist_from_file("/tmp/changed.files");
503 log_msg (2, "Returned from popup_changelist_from_file()");
504 }
505 }
506 else
507 {
508 unlink ("/tmp/changed.files");
509 }
510 log_to_screen (say_at_end);
511 sprintf (tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
512 run_program_and_log_output (tmp, TRUE);
513 run_program_and_log_output (g_erase_tmpdir_and_scratchdir, TRUE);
514
515 run_program_and_log_output("mount", 2);
516
517 if (bkpinfo->please_dont_eject)
518 {
519 log_msg(5, "Not ejecting at end. Fair enough.");
520 }
521 else
522 {
523 log_msg(5, "Ejecting at end.");
524 if (!find_cdrom_device(tmp, FALSE) || !find_dvd_device(tmp, FALSE))
525 {
526 log_msg(1, "Ejecting %s", tmp);
527 eject_device(tmp);
528 }
529 }
530
531 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
532 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
533 if (!retval) { printf("Mondoarchive ran OK.\n"); }
534 else { printf("Errors occurred during backup. Please check logfile.\n"); }
535 distro_specific_kludges_at_end_of_mondoarchive();
536 register_pid(0,"mondo");
537 set_signals(FALSE);
538 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
539 system(g_erase_tmpdir_and_scratchdir);
540 free_libmondo_global_strings();
541 paranoid_free(say_at_end);
542 paranoid_free(tmp);
543 paranoid_free(bkpinfo);
544
545 unlink("/tmp/filelist.full");
546 unlink("/tmp/filelist.full.gz");
547
548 if (!g_cdrom_drive_is_here) { log_msg(10, "FYI, g_cdrom_drive_is_here was never used"); }
549 if (!g_dvd_drive_is_here) { log_msg(10, "FYI, g_dvd_drive_is_here was never used"); }
550
551 if (!g_text_mode)
552 {
553 popup_and_OK
554 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
555 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
556 finish(retval);
557 }
558 else
559 {
560 printf ("See %s for details of backup run.\n", MONDO_LOGFILE);
561 exit(retval);
562 }
563
564 return EXIT_SUCCESS;
565}
566
567
Note: See TracBrowser for help on using the repository browser.