source: MondoRescue/trunk/mondo/src/mondoarchive/main.c@ 1081

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

merge -r1078:1080 $SVN_M/branches/stable

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