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

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

Merge trunk memory management for mondoarchive
Rename main.c to mondoarchive.c

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