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

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

merge -r193:196 $SVN_M/branches/2.05

  • Property svn:keywords set to Id
File size: 12.2 KB
RevLine 
[171]1/*
2 * $Id: main.c 197 2005-12-13 23:12:15Z 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#ifndef VERSION
25#define VERSION AUX_VER
26#endif
27
28
29char g_version[] = VERSION;
30
31
32// for CVS
[59]33//static char cvsid[] = "$Id: main.c 197 2005-12-13 23:12:15Z bcornec $";
[1]34
35/************************* external variables *************************/
36extern void set_signals(int);
37extern int g_current_media_number;
[59]38extern void register_pid(pid_t, char *);
[1]39extern int g_currentY;
40extern bool g_text_mode;
41extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
42extern char *g_mondo_home;
43extern char *g_tmpfs_mountpt;
44extern char *g_erase_tmpdir_and_scratchdir;
45extern double g_kernel_version;
46
47/***************** global vars, used only by main.c ******************/
48bool g_skip_floppies;
49long diffs;
50
51extern t_bkptype g_backup_media_type;
52extern int g_loglevel;
53
54/****************** subroutines used only by main.c ******************/
55
56
57/**
58 * Print a "don't panic" message to the log and a message about the logfile to the screen.
59 */
60void welcome_to_mondoarchive()
61{
[59]62 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
63 VERSION);
64 log_msg(0, "running on %s architecture", get_architecture());
65 log_msg(0,
66 "-----------------------------------------------------------");
67 log_msg(0,
68 "NB: Mondo logs almost everything, so don't panic if you see");
69 log_msg(0,
70 "some error messages. Please read them carefully before you");
71 log_msg(0,
72 "decide to break out in a cold sweat. Despite (or perhaps");
73 log_msg(0,
74 "because of) the wealth of messages. some users are inclined");
75 log_msg(0,
76 "to stop reading this log. If Mondo stopped for some reason,");
77 log_msg(0,
78 "chances are it's detailed here. More than likely there's a");
79 log_msg(0,
80 "message at the very end of this log that will tell you what");
81 log_msg(0,
82 "is wrong. Please read it! -Devteam");
83 log_msg(0,
84 "-----------------------------------------------------------");
[1]85
[59]86 log_msg(0, "Zero...");
87 log_msg(1, "One...");
88 log_msg(2, "Two...");
89 log_msg(3, "Three...");
90 log_msg(4, "Four...");
91 log_msg(5, "Five...");
92 log_msg(6, "Six...");
93 log_msg(7, "Seven...");
94 log_msg(8, "Eight...");
95 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[1]96}
97
98
99extern char *g_magicdev_command;
100
101/**
102 * Do whatever is necessary to insure a successful backup on the Linux distribution
103 * of the day.
104 */
105void distro_specific_kludges_at_start_of_mondoarchive()
106{
[59]107 log_msg(2, "Unmounting old ramdisks if necessary");
108 stop_magicdev_if_necessary(); // for RH+Gnome users
109 run_program_and_log_output
110 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
111 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
112 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
113 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]114}
115
116
117/**
118 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
119 */
120void distro_specific_kludges_at_end_of_mondoarchive()
121{
[59]122 log_msg(2, "Restarting magicdev if necessary");
123 sync();
124 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]125
[59]126 log_msg(2, "Restarting supermounts if necessary");
127 sync();
128 remount_supermounts_if_necessary(); // for Mandrake users
[1]129
[59]130 log_msg(2, "Unmounting /boot if necessary");
131 sync();
132 unmount_boot_if_necessary(); // for Gentoo users
[1]133}
134
135
136/**
137 * Backup/verify the user's data.
138 * What did you think it did, anyway? :-)
139 */
[59]140int main(int argc, char *argv[])
[1]141{
[59]142 struct s_bkpinfo *bkpinfo;
143 char *tmp;
[171]144 int res = 0;
145 int retval = 0;
146 char *say_at_end = NULL;
[1]147
148/* Make sure I'm root; abort if not */
[59]149 if (getuid() != 0) {
150 fprintf(stderr, "Please run as root.\r\n");
151 exit(127);
152 }
[1]153
154/* If -V, -v or --version then echo version no. and quit */
[59]155 if (argc == 2
156 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
157 || !strcmp(argv[argc - 1], "--version"))) {
158 printf("mondoarchive v%s\nSee man page for help\n", VERSION);
159 exit(0);
160 }
[1]161
162/* Initialize variables */
163
[59]164 malloc_libmondo_global_strings();
[1]165
[59]166 diffs = 0;
167 unlink("/var/log/partimagehack-debug.log");
168 printf("Initializing...\n");
169 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
170 fatal_error("Cannot malloc bkpinfo");
171 }
[1]172
173
174/* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[171]175 asprintf(&tmp, "/sbin:/usr/sbin:%s:/usr/local/sbin", getenv("PATH"));
[59]176 setenv("PATH", tmp, 1);
[171]177 paranoid_free(tmp);
[1]178
179/* Add the ARCH environment variable for ia64 purposes */
[171]180 setenv("ARCH", get_architecture(), 1);
[1]181
[59]182 unlink(MONDO_LOGFILE);
[1]183
184/* Configure the bkpinfo structure, global file paths, etc. */
[59]185 g_main_pid = getpid();
186 log_msg(9, "This");
[1]187
[59]188 register_pid(g_main_pid, "mondo");
189 set_signals(TRUE); // catch SIGTERM, etc.
[94]190 run_program_and_log_output("date", 1);
[59]191 run_program_and_log_output("dmesg -n1", TRUE);
[1]192
[59]193 log_msg(9, "Next");
194 welcome_to_mondoarchive();
195 distro_specific_kludges_at_start_of_mondoarchive();
[87]196 // BERLIOS : too early, bkpinfo is not initialized ??
197 //sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
[59]198 g_kernel_version = get_kernel_version();
[1]199
[59]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);
[1]211 }
[59]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]));
[1]217 }
[59]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);
[171]225 paranoid_free(tmp);
[59]226 finish(1);
[1]227 }
[59]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 }
[1]247
[59]248 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
249 g_loglevel = 10;
250 g_text_mode = TRUE;
251 setup_newt_stuff();
[171]252 if ((tmp = find_cdrw_device()) == NULL) {
[59]253 printf("Failed to find CDR-RW drive\n");
254 } else {
255 printf("CD-RW is at %s\n", tmp);
256 }
[171]257 paranoid_free(tmp);
258
259 if ((tmp = find_cdrom_device(FALSE)) == NULL) {
[59]260 printf("Failed to find CD-ROM drive\n");
261 } else {
262 printf("CD-ROM is at %s\n", tmp);
263 }
[171]264 paranoid_free(tmp);
[59]265 finish(0);
266 }
[1]267
[59]268 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
269 g_loglevel = 10;
270 g_text_mode = TRUE;
271 setup_newt_stuff();
[171]272 if ((tmp = find_dvd_device()) == NULL) {
[59]273 printf("Failed to find DVD drive\n");
274 } else {
275 printf("DVD is at %s\n", tmp);
276 }
[171]277 paranoid_free(tmp);
[59]278 finish(0);
279 }
[1]280
[59]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
[1]299/* Process command line, if there is one. If not, ask user for info. */
[59]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 - " MONDO_LOGFILE "\n");
314 log_msg(1, "Mondoarchive will now exit.");
315 finish(1);
316 }
317 setup_newt_stuff();
[1]318 }
319
320/* Finish configuring global structures */
[59]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 }
[1]325
[59]326 log_to_screen
327 ("BusyBox's sources are available from http://www.busybox.net");
[1]328
[59]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) {
[171]334 asprintf(&say_at_end,
[59]335 "Data archived. Please check the logs, just as a precaution. ");
336 } else {
[171]337 asprintf(&say_at_end, "Data archived OK. ");
[59]338 }
339 }
[1]340
341/* If we're meant to verify then verify */
[59]342 if (bkpinfo->verify_data) {
343 res = verify_data(bkpinfo);
344 if (res < 0) {
[171]345 asprintf(&say_at_end, "%d difference%c found.", -res,
[59]346 (-res != 1) ? 's' : ' ');
347 res = 0;
348 }
349 retval += res;
350 }
[1]351
[171]352 /* Offer to write floppy disk images to physical disks */
[59]353 if (bkpinfo->backup_data && !g_skip_floppies) {
354 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
355 retval += res;
356 }
[1]357
[171]358 /* Report result of entire operation (success? errors?) */
359 if (retval == 0) {
[59]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 }
[1]366
[59]367 if (does_file_exist("/root/images/mindi/mondorescue.iso")) {
368 log_to_screen
369 ("/root/images/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.");
370 }
[1]371
372
[59]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);
[171]388 paranoid_free(say_at_end);
389
390 asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
[59]391 run_program_and_log_output(tmp, TRUE);
[171]392 paranoid_free(tmp);
393
394 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
395 bkpinfo->scratchdir);
[59]396 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
[1]397
[59]398 run_program_and_log_output("mount", 2);
[1]399
[59]400 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
401 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
[171]402 if (retval == 0) {
[59]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);
[1]414
[59]415 unlink("/tmp/filelist.full");
416 unlink("/tmp/filelist.full.gz");
[1]417
[94]418 run_program_and_log_output("date", 1);
419
[59]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;
[1]431}
Note: See TracBrowser for help on using the repository browser.