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

Last change on this file since 507 was 507, checked in by bcornec, 18 years ago

merge -r489:506 $SVN_M/branches/stable

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