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

Last change on this file since 815 was 815, checked in by Bruno Cornec, 18 years ago

merge -r807:814 $SVN_M/branches/stable

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