source: MondoRescue/branches/stable/mondo/src/mondoarchive/main.c@ 1139

Last change on this file since 1139 was 1132, checked in by Bruno Cornec, 17 years ago

mondoarchive now uses mr_msg_init

  • Property svn:keywords set to Id
File size: 13.0 KB
RevLine 
[1]1/***************************************************************************
[1080]2* $Id: main.c 1132 2007-02-11 01:33:58Z bruno $
3*/
[1]4
5
6/**
7 * @file
8 * The main file for mondoarchive.
9 */
10
11/************************* #include statements *************************/
12#include <pthread.h>
13#include <stdio.h>
14#include <stdlib.h>
[1100]15#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
18
[1067]19#include "my-stuff.h"
[1]20#include "../common/mondostructures.h"
21#include "../common/libmondo.h"
22#include "mondo-cli-EXT.h"
23
[1087]24#include "mr_str.h"
[1112]25#include "mr_msg.h"
[1102]26#include "mr_file.h"
[1087]27
[1]28// for CVS
[128]29//static char cvsid[] = "$Id: main.c 1132 2007-02-11 01:33:58Z bruno $";
[1]30
31/************************* external variables *************************/
32extern void set_signals(int);
33extern int g_current_media_number;
[128]34extern void register_pid(pid_t, char *);
[1]35extern int g_currentY;
36extern bool g_text_mode;
37extern char *g_boot_mountpt;
38extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
39extern char *g_mondo_home;
40extern char *g_tmpfs_mountpt;
41extern char *g_erase_tmpdir_and_scratchdir;
42extern char *g_cdrw_drive_is_here;
[128]43static char *g_cdrom_drive_is_here = NULL;
44static char *g_dvd_drive_is_here = NULL;
[1]45extern double g_kernel_version;
46
47/***************** global vars, used only by main.c ******************/
48bool g_skip_floppies;
49long diffs;
[792]50char *ps_options = "auxww";
[928]51char *ps_proc_id = "$2";
[1]52
53extern t_bkptype g_backup_media_type;
54extern int g_loglevel;
55
56/****************** subroutines used only by main.c ******************/
57
58
59/**
60 * Print a "don't panic" message to the log and a message about the logfile to the screen.
61 */
62void welcome_to_mondoarchive()
63{
[1108]64 mr_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
[251]65 PACKAGE_VERSION);
[1108]66 mr_msg(0, "running on %s architecture", get_architecture());
67 mr_msg(0,
[128]68 "-----------------------------------------------------------");
[1108]69 mr_msg(0,
[128]70 "NB: Mondo logs almost everything, so don't panic if you see");
[1108]71 mr_msg(0,
[128]72 "some error messages. Please read them carefully before you");
[1108]73 mr_msg(0,
[128]74 "decide to break out in a cold sweat. Despite (or perhaps");
[1108]75 mr_msg(0,
[128]76 "because of) the wealth of messages. some users are inclined");
[1108]77 mr_msg(0,
[128]78 "to stop reading this log. If Mondo stopped for some reason,");
[1108]79 mr_msg(0,
[128]80 "chances are it's detailed here. More than likely there's a");
[1108]81 mr_msg(0,
[128]82 "message at the very end of this log that will tell you what");
[1108]83 mr_msg(0,
[128]84 "is wrong. Please read it! -Devteam");
[1108]85 mr_msg(0,
[128]86 "-----------------------------------------------------------");
[1]87
[1108]88 mr_msg(0, "Zero...");
89 mr_msg(1, "One...");
90 mr_msg(2, "Two...");
91 mr_msg(3, "Three...");
92 mr_msg(4, "Four...");
93 mr_msg(5, "Five...");
94 mr_msg(6, "Six...");
95 mr_msg(7, "Seven...");
96 mr_msg(8, "Eight...");
[128]97 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[1]98}
99
100
101extern char *g_magicdev_command;
102
103/**
104 * Do whatever is necessary to insure a successful backup on the Linux distribution
105 * of the day.
106 */
107void distro_specific_kludges_at_start_of_mondoarchive()
108{
[1108]109 mr_msg(2, "Unmounting old ramdisks if necessary");
[128]110 stop_magicdev_if_necessary(); // for RH+Gnome users
111 run_program_and_log_output
112 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
113 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
114 // stop_autofs_if_necessary(); // for Xandros users
115 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
116 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]117}
118
119
120
121/**
122 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
123 */
124void distro_specific_kludges_at_end_of_mondoarchive()
125{
126// char tmp[500];
[1108]127 mr_msg(2, "Restarting magicdev if necessary");
[128]128 sync();
129 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]130
[1108]131 mr_msg(2, "Restarting autofs if necessary");
[128]132 sync();
133 // restart_autofs_if_necessary(); // for Xandros users
[1]134
[1108]135 mr_msg(2, "Restarting supermounts if necessary");
[128]136 sync();
137 remount_supermounts_if_necessary(); // for Mandrake users
[1]138
[1108]139 mr_msg(2, "Unmounting /boot if necessary");
[128]140 sync();
141 unmount_boot_if_necessary(); // for Gentoo users
[1]142
[1108]143// mr_msg( 2, "Cleaning up KDE desktop");
[1]144// clean_up_KDE_desktop_if_necessary();
145}
146
147/*-----------------------------------------------------------*/
148
149
150
151/**
152 * Backup/verify the user's data.
153 * What did you think it did, anyway? :-)
154 */
[128]155int main(int argc, char *argv[])
[1]156{
[128]157 struct s_bkpinfo *bkpinfo;
[1100]158 struct stat stbuf;
[128]159 char *tmp;
160 int res, retval;
161 char *say_at_end;
[1]162
163/* Make sure I'm root; abort if not */
[128]164 if (getuid() != 0) {
[541]165 fprintf(stderr, "Please run as root.\r\n");
[128]166 exit(127);
167 }
[1]168
169/* If -V, -v or --version then echo version no. and quit */
[128]170 if (argc == 2
171 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
172 || !strcmp(argv[argc - 1], "--version"))) {
[541]173 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
[128]174 exit(0);
175 }
[1]176
177/* Initialize variables */
178
[128]179 malloc_libmondo_global_strings();
180 malloc_string(tmp);
181 malloc_string(say_at_end);
[1]182
[128]183 res = 0;
184 retval = 0;
185 diffs = 0;
186 say_at_end[0] = '\0';
[541]187 printf("Initializing...\n");
[1]188
[810]189 /* initialize log file with time stamp */
190 unlink(MONDO_LOGFILE);
[1132]191 mr_msg_init(MONDO_LOGFILE,4);
[1108]192 mr_msg(0, "Time started: %s", mr_date());
[1]193
[1132]194 bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
195
[807]196 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[128]197 strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
198 tmp[MAX_STR_LEN - 1] = '\0';
199 if (strlen(tmp) >= MAX_STR_LEN - 33) {
200 fatal_error
201 ("Your PATH environmental variable is too long. Please shorten it.");
202 }
203 strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
204 setenv("PATH", tmp, 1);
[1]205
[807]206 /* Add the ARCH environment variable for ia64 purposes */
[128]207 strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
208 tmp[MAX_STR_LEN - 1] = '\0';
209 setenv("ARCH", tmp, 1);
[1]210
[1100]211 /* Add MONDO_SHARE + others environment variable for mindi */
[424]212 setenv_mondo_share();
[258]213
[1100]214 if (stat(MONDO_CACHE, &stbuf) != 0) {
215 mr_mkdir(MONDO_CACHE,0x755);
216 }
217
[807]218 /* Configure the bkpinfo structure, global file paths, etc. */
[128]219 g_main_pid = getpid();
[1108]220 mr_msg(9, "This");
[1]221
[128]222 register_pid(g_main_pid, "mondo");
223 set_signals(TRUE); // catch SIGTERM, etc.
224 run_program_and_log_output("dmesg -n1", TRUE);
[1]225
[1108]226 mr_msg(9, "Next");
[128]227 welcome_to_mondoarchive();
228 distro_specific_kludges_at_start_of_mondoarchive();
229 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
230 bkpinfo->scratchdir);
231 g_kernel_version = get_kernel_version();
[1]232
[128]233 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
234 g_loglevel = 10;
235 g_text_mode = TRUE;
236 setup_newt_stuff();
237 if (!strstr(argv[2], "filelist")) {
[541]238 printf("Sorry - filelist goes first\n");
[128]239 finish(1);
240 } else {
241 finish(get_fattr_list(argv[2], argv[3]));
242 }
243 finish(0);
[1]244 }
[128]245 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
246 g_loglevel = 10;
[1]247// chdir("/tmp");
[128]248 g_text_mode = TRUE;
249 setup_newt_stuff();
250 finish(set_fattr_list(argv[2], argv[3]));
[1]251 }
[128]252
253 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
254 g_loglevel = 10;
255 g_text_mode = TRUE;
256 setup_newt_stuff();
257 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
258 printf("in=%s; out=%s\n", argv[2], tmp);
259 finish(1);
[1]260 }
[128]261
262 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
263 g_loglevel = 10;
264 g_text_mode = TRUE;
265 setup_newt_stuff();
266 if (!strstr(argv[2], "filelist")) {
[541]267 printf("Sorry - filelist goes first\n");
[128]268 finish(1);
269 } else {
270 finish(get_acl_list(argv[2], argv[3]));
271 }
272 finish(0);
273 }
274 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
275 g_loglevel = 10;
[1]276// chdir("/tmp");
[128]277 g_text_mode = TRUE;
278 setup_newt_stuff();
279 finish(set_acl_list(argv[2], argv[3]));
280 }
[1]281
[128]282 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
283 g_loglevel = 10;
284 g_text_mode = TRUE;
285 setup_newt_stuff();
286 if (find_cdrw_device(tmp)) {
[541]287 printf("Failed to find CDR-RW drive\n");
[128]288 } else {
[541]289 printf("CD-RW is at %s\n", tmp);
[128]290 }
291 tmp[0] = '\0';
292 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[541]293 printf("Failed to find CD-ROM drive\n");
[128]294 } else {
[541]295 printf("CD-ROM is at %s\n", tmp);
[128]296 }
297 finish(0);
298 }
[1]299
[128]300 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
301 g_loglevel = 10;
302 g_text_mode = TRUE;
303 setup_newt_stuff();
304 if (find_dvd_device(tmp, atoi(argv[2]))) {
[541]305 printf("Failed to find DVD drive\n");
[128]306 } else {
[541]307 printf("DVD is at %s\n", tmp);
[128]308 }
309 finish(0);
310 }
[1]311
[128]312 if (argc > 2 && !strcmp(argv[1], "disksize")) {
313 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
314 finish(0);
315 }
316 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
317 if (is_dev_an_NTFS_dev(argv[2])) {
[541]318 printf("%s is indeed an NTFS dev\n", argv[2]);
[128]319 } else {
[541]320 printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]321 }
322 finish(0);
323 }
324
325 if (pre_param_configuration(bkpinfo)) {
326 fatal_error
327 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
328 }
329
[1]330/* Process command line, if there is one. If not, ask user for info. */
[128]331 if (argc == 1) {
332 g_text_mode = FALSE;
333 setup_newt_stuff();
334 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
335 if (res) {
336 fatal_error
337 ("Syntax error. Please review the parameters you have supplied and try again.");
338 }
339 } else {
340 res = handle_incoming_parameters(argc, argv, bkpinfo);
341 if (res) {
342 printf
[541]343 ("Errors were detected in the command line you supplied.\n");
344 printf("Please review the log file - " MONDO_LOGFILE "\n");
[1108]345 mr_msg(1, "Mondoarchive will now exit.");
[128]346 finish(1);
347 }
348 setup_newt_stuff();
[1]349 }
350
351/* Finish configuring global structures */
[128]352 if (post_param_configuration(bkpinfo)) {
353 fatal_error
354 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
355 }
[1]356
[128]357 log_to_screen
[541]358 ("BusyBox's sources are available from http://www.busybox.net");
[128]359 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
360 bkpinfo->scratchdir);
[1]361
[128]362 /* If we're meant to backup then backup */
363 if (bkpinfo->backup_data) {
[541]364 res = backup_data(bkpinfo);
[128]365 retval += res;
366 if (res) {
367 strcat(say_at_end,
[541]368 "Data archived. Please check the logs, just as a precaution. ");
[128]369 } else {
[541]370 strcat(say_at_end, "Data archived OK. ");
[128]371 }
372 }
[1]373
374/* If we're meant to verify then verify */
[128]375 if (bkpinfo->verify_data) {
376 res = verify_data(bkpinfo);
377 if (res < 0) {
[541]378 sprintf(tmp, "%d difference%c found.", -res,
[128]379 (-res != 1) ? 's' : ' ');
380 strcat(say_at_end, tmp);
381 log_to_screen(tmp);
382 res = 0;
383 }
384 retval += res;
385 }
[1]386
387/* Offer to write floppy disk images to physical disks */
[128]388 if (bkpinfo->backup_data && !g_skip_floppies) {
389 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
390 retval += res;
[1]391// res = offer_to_write_boot_ISO_to_physical_CD(bkpinfo);
392// retval += res;
[128]393 }
[1]394
395/* Report result of entire operation (success? errors?) */
[128]396 if (!retval) {
397 mvaddstr_and_log_it(g_currentY++, 0,
[541]398 "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]399 } else {
400 mvaddstr_and_log_it(g_currentY++, 0,
[541]401 "Backup and/or verify ran to completion. However, errors did occur.");
[128]402 }
[1]403
[1044]404 if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
[128]405 log_to_screen
[1044]406 ("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]407 }
[1]408
409
[128]410 if (length_of_file("/tmp/changed.files") > 2) {
411 if (g_text_mode) {
412 log_to_screen
[541]413 ("Type 'less /tmp/changed.files' to see which files don't match the archives");
[128]414 } else {
[1108]415 mr_msg(1,
[541]416 "Type 'less /tmp/changed.files' to see which files don't match the archives");
[1108]417 mr_msg(2, "Calling popup_changelist_from_file()");
[128]418 popup_changelist_from_file("/tmp/changed.files");
[1108]419 mr_msg(2, "Returned from popup_changelist_from_file()");
[128]420 }
421 } else {
422 unlink("/tmp/changed.files");
423 }
424 log_to_screen(say_at_end);
425 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
426 run_program_and_log_output(tmp, TRUE);
427 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
[1]428
[128]429 run_program_and_log_output("mount", 2);
[1]430
[128]431 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
432 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
433 if (!retval) {
[541]434 printf("Mondoarchive ran OK.\n");
[128]435 } else {
[541]436 printf("Errors occurred during backup. Please check logfile.\n");
[128]437 }
438 distro_specific_kludges_at_end_of_mondoarchive();
439 register_pid(0, "mondo");
440 set_signals(FALSE);
441 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
442 system(g_erase_tmpdir_and_scratchdir);
443 free_libmondo_global_strings();
[1080]444 mr_free(say_at_end);
445 mr_free(tmp);
446 mr_free(bkpinfo);
[1]447
[128]448 unlink("/tmp/filelist.full");
449 unlink("/tmp/filelist.full.gz");
[1]450
[128]451 if (!g_cdrom_drive_is_here) {
[1108]452 mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
[128]453 }
454 if (!g_dvd_drive_is_here) {
[1108]455 mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
[128]456 }
[1]457
[810]458 /* finalize log file with time stamp */
[1108]459 mr_msg(0, "Time finished: %s", mr_date());
[1132]460 mr_msg_close();
[810]461
[128]462 if (!g_text_mode) {
463 popup_and_OK
[541]464 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
465 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
[128]466 finish(retval);
467 } else {
[541]468 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]469 exit(retval);
470 }
471
472 return EXIT_SUCCESS;
[1]473}
Note: See TracBrowser for help on using the repository browser.