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

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

merge -r781:793 $SVN_M/branches/stable

  • Property svn:keywords set to Id
File size: 12.4 KB
Line 
1/*
2 * $Id: main.c 794 2006-09-13 23:05:18Z 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 794 2006-09-13 23:05:18Z 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
126/**
127 * Backup/verify the user's data.
128 * What did you think it did, anyway? :-)
129 */
130int main(int argc, char *argv[])
131{
132 struct s_bkpinfo *bkpinfo;
133 char *tmp;
134 int res = 0;
135 int retval = 0;
136 char *say_at_end = NULL;
137
138#ifdef ENABLE_NLS
139 setlocale(LC_ALL, "");
140 (void) textdomain("mondo");
141#endif
142/* Make sure I'm root; abort if not */
143 if (getuid() != 0) {
144 fprintf(stderr, _("Please run as root.\n"));
145 exit(127);
146 }
147
148 /* If -V, -v or --version then echo version no. and quit */
149 if (argc == 2
150 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
151 || !strcmp(argv[argc - 1], "--version"))) {
152 printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
153 exit(0);
154 }
155
156 /* Initialize variables */
157 malloc_libmondo_global_strings();
158
159 res = 0;
160 retval = 0;
161 diffs = 0;
162 printf(_("Initializing...\n"));
163 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
164 fatal_error("Cannot malloc bkpinfo");
165 }
166
167 /* Initialize Configuration Structure */
168 mr_archive_init_conf(bkpinfo->mr_conf);
169
170
171 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
172 asprintf(&tmp, "/sbin:/usr/sbin:%s:/usr/local/sbin", getenv("PATH"));
173 setenv("PATH", tmp, 1);
174 paranoid_free(tmp);
175
176 /* Add the ARCH environment variable for ia64 purposes */
177 setenv("ARCH", get_architecture(), 1);
178
179 /* Add MONDO_SHARE environment variable for mindi */
180 setenv_mondo_share();
181
182 unlink(MONDO_LOGFILE);
183
184/* Configure the bkpinfo structure, global file paths, etc. */
185 g_main_pid = getpid();
186 log_msg(9, "This");
187
188 register_pid(g_main_pid, "mondo");
189 set_signals(TRUE); // catch SIGTERM, etc.
190 run_program_and_log_output("date", 1);
191 run_program_and_log_output("dmesg -n1", TRUE);
192
193 log_msg(9, "Next");
194 welcome_to_mondoarchive();
195 distro_specific_kludges_at_start_of_mondoarchive();
196 // BERLIOS : too early, bkpinfo is not initialized ??
197 //s-printf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
198 g_kernel_version = get_kernel_version();
199
200 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
201 g_loglevel = 10;
202 g_text_mode = TRUE;
203 setup_newt_stuff();
204 if (!strstr(argv[2], "filelist")) {
205 printf(_("Sorry - filelist goes first\n"));
206 finish(1);
207 } else {
208 finish(get_fattr_list(argv[2], argv[3]));
209 }
210 finish(0);
211 }
212 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
213 g_loglevel = 10;
214 g_text_mode = TRUE;
215 setup_newt_stuff();
216 finish(set_fattr_list(argv[2], argv[3]));
217 }
218
219 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
220 g_loglevel = 10;
221 g_text_mode = TRUE;
222 setup_newt_stuff();
223 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
224 printf("in=%s; out=%s\n", argv[2], tmp);
225 paranoid_free(tmp);
226 finish(1);
227 }
228
229 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
230 g_loglevel = 10;
231 g_text_mode = TRUE;
232 setup_newt_stuff();
233 if (!strstr(argv[2], "filelist")) {
234 printf(_("Sorry - filelist goes first\n"));
235 finish(1);
236 } else {
237 finish(get_acl_list(argv[2], argv[3]));
238 }
239 finish(0);
240 }
241 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
242 g_loglevel = 10;
243 g_text_mode = TRUE;
244 setup_newt_stuff();
245 finish(set_acl_list(argv[2], argv[3]));
246 }
247
248 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
249 g_loglevel = 10;
250 g_text_mode = TRUE;
251 setup_newt_stuff();
252 if ((tmp = find_cdrw_device()) == NULL) {
253 printf(_("Failed to find CDR-RW drive\n"));
254 } else {
255 printf(_("CD-RW is at %s\n"), tmp);
256 }
257 paranoid_free(tmp);
258
259 if ((tmp = find_cdrom_device(FALSE)) == NULL) {
260 printf(_("Failed to find CD-ROM drive\n"));
261 } else {
262 printf(_("CD-ROM is at %s\n"), tmp);
263 }
264 paranoid_free(tmp);
265 finish(0);
266 }
267
268 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
269 g_loglevel = 10;
270 g_text_mode = TRUE;
271 setup_newt_stuff();
272 if ((tmp = find_dvd_device()) == NULL) {
273 printf(_("Failed to find DVD drive\n"));
274 } else {
275 printf(_("DVD is at %s\n"), tmp);
276 }
277 paranoid_free(tmp);
278 finish(0);
279 }
280
281 if (argc > 2 && !strcmp(argv[1], "disksize")) {
282 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
283 finish(0);
284 }
285 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
286 if (is_dev_an_NTFS_dev(argv[2])) {
287 printf(_("%s is indeed an NTFS dev\n"), argv[2]);
288 } else {
289 printf(_("%s is _not_ an NTFS dev\n"), argv[2]);
290 }
291 finish(0);
292 }
293
294 if (pre_param_configuration(bkpinfo)) {
295 fatal_error
296 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
297 }
298
299/* Process command line, if there is one. If not, ask user for info. */
300 if (argc == 1) {
301 g_text_mode = FALSE;
302 setup_newt_stuff();
303 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
304 if (res) {
305 fatal_error
306 ("Syntax error. Please review the parameters you have supplied and try again.");
307 }
308 } else {
309 res = handle_incoming_parameters(argc, argv, bkpinfo);
310 if (res) {
311 printf
312 (_("Errors were detected in the command line you supplied.\n"));
313 printf(_("Please review the log file - %s \n"),MONDO_LOGFILE);
314 log_msg(1, "Mondoarchive will now exit.");
315 finish(1);
316 }
317 setup_newt_stuff();
318 }
319
320/* Finish configuring global structures */
321 if (post_param_configuration(bkpinfo)) {
322 fatal_error
323 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
324 }
325
326 log_to_screen
327 (_("BusyBox's sources are available from http://www.busybox.net"));
328
329 /* If we're meant to backup then backup */
330 if (bkpinfo->backup_data) {
331 res = backup_data(bkpinfo);
332 retval += res;
333 if (res) {
334 asprintf(&say_at_end,
335 _("Data archived. Please check the logs, just as a precaution. "));
336 } else {
337 asprintf(&say_at_end, _("Data archived OK. "));
338 }
339 }
340
341/* If we're meant to verify then verify */
342 if (bkpinfo->verify_data) {
343 res = verify_data(bkpinfo);
344 if (res < 0) {
345 asprintf(&say_at_end, _("%d difference%c found."), -res,
346 (-res != 1) ? 's' : ' ');
347 res = 0;
348 }
349 retval += res;
350 }
351
352 /* Offer to write floppy disk images to physical disks */
353 if (bkpinfo->backup_data && !g_skip_floppies) {
354 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
355 retval += res;
356 }
357
358 /* Report result of entire operation (success? errors?) */
359 if (retval == 0) {
360 mvaddstr_and_log_it(g_currentY++, 0,
361 _("Backup and/or verify ran to completion. Everything appears to be fine."));
362 } else {
363 mvaddstr_and_log_it(g_currentY++, 0,
364 _("Backup and/or verify ran to completion. However, errors did occur."));
365 }
366
367 if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
368 log_to_screen
369 (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it."));
370 }
371
372
373 if (length_of_file("/tmp/changed.files") > 2) {
374 if (g_text_mode) {
375 log_to_screen
376 (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
377 } else {
378 log_msg(1,
379 _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
380 log_msg(2, "Calling popup_changelist_from_file()");
381 popup_changelist_from_file("/tmp/changed.files");
382 log_msg(2, "Returned from popup_changelist_from_file()");
383 }
384 } else {
385 unlink("/tmp/changed.files");
386 }
387 log_to_screen(say_at_end);
388 paranoid_free(say_at_end);
389
390 asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
391 run_program_and_log_output(tmp, TRUE);
392 paranoid_free(tmp);
393
394 asprintf(&g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
395 bkpinfo->scratchdir);
396 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
397
398 run_program_and_log_output("mount", 2);
399
400 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
401 system("rm -Rf /mondo.tmp* /mondo.scratch.*");
402 if (retval == 0) {
403 printf(_("Mondoarchive ran OK.\n"));
404 } else {
405 printf(_("Errors occurred during backup. Please check logfile.\n"));
406 }
407 distro_specific_kludges_at_end_of_mondoarchive();
408 register_pid(0, "mondo");
409 set_signals(FALSE);
410 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
411 system(g_erase_tmpdir_and_scratchdir);
412 free_libmondo_global_strings();
413 paranoid_free(bkpinfo);
414
415 unlink("/tmp/filelist.full");
416 unlink("/tmp/filelist.full.gz");
417
418 run_program_and_log_output("date", 1);
419
420 if (!g_text_mode) {
421 popup_and_OK
422 (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
423 log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
424 finish(retval);
425 } else {
426 printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
427 exit(retval);
428 }
429
430 return EXIT_SUCCESS;
431}
Note: See TracBrowser for help on using the repository browser.