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

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

Addition of mr_mkdir in library and usage in main for MONDO_CACHE creation

  • Property svn:keywords set to Id
File size: 12.9 KB
RevLine 
[1]1/***************************************************************************
[1080]2* $Id: main.c 1100 2007-02-05 16:00:16Z 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
[1087]24#include "mr_str.h"
25
[1]26// for CVS
[128]27//static char cvsid[] = "$Id: main.c 1100 2007-02-05 16:00:16Z bruno $";
[1]28
29/************************* external variables *************************/
30extern void set_signals(int);
31extern int g_current_media_number;
[128]32extern void register_pid(pid_t, char *);
[1]33extern int g_currentY;
34extern bool g_text_mode;
35extern char *g_boot_mountpt;
36extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
37extern char *g_mondo_home;
38extern char *g_tmpfs_mountpt;
39extern char *g_erase_tmpdir_and_scratchdir;
40extern char *g_cdrw_drive_is_here;
[128]41static char *g_cdrom_drive_is_here = NULL;
42static char *g_dvd_drive_is_here = NULL;
[1]43extern double g_kernel_version;
44
45/***************** global vars, used only by main.c ******************/
46bool g_skip_floppies;
47long diffs;
[792]48char *ps_options = "auxww";
[928]49char *ps_proc_id = "$2";
[1]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{
[128]62 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
[251]63 PACKAGE_VERSION);
[128]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
[128]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{
[128]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 // stop_autofs_if_necessary(); // for Xandros users
113 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
114 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]115}
116
117
118
119/**
120 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
121 */
122void distro_specific_kludges_at_end_of_mondoarchive()
123{
124// char tmp[500];
[128]125 log_msg(2, "Restarting magicdev if necessary");
126 sync();
127 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]128
[128]129 log_msg(2, "Restarting autofs if necessary");
130 sync();
131 // restart_autofs_if_necessary(); // for Xandros users
[1]132
[128]133 log_msg(2, "Restarting supermounts if necessary");
134 sync();
135 remount_supermounts_if_necessary(); // for Mandrake users
[1]136
[128]137 log_msg(2, "Unmounting /boot if necessary");
138 sync();
139 unmount_boot_if_necessary(); // for Gentoo users
[1]140
141// log_msg( 2, "Cleaning up KDE desktop");
142// clean_up_KDE_desktop_if_necessary();
143}
144
145/*-----------------------------------------------------------*/
146
147
148
149/**
150 * Backup/verify the user's data.
151 * What did you think it did, anyway? :-)
152 */
[128]153int main(int argc, char *argv[])
[1]154{
[128]155 struct s_bkpinfo *bkpinfo;
[1100]156 struct stat stbuf;
[128]157 char *tmp;
158 int res, retval;
159 char *say_at_end;
[1]160
161/* Make sure I'm root; abort if not */
[128]162 if (getuid() != 0) {
[541]163 fprintf(stderr, "Please run as root.\r\n");
[128]164 exit(127);
165 }
[1]166
167/* If -V, -v or --version then echo version no. and quit */
[128]168 if (argc == 2
169 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
170 || !strcmp(argv[argc - 1], "--version"))) {
[541]171 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
[128]172 exit(0);
173 }
[1]174
175/* Initialize variables */
176
[128]177 malloc_libmondo_global_strings();
178 malloc_string(tmp);
179 malloc_string(say_at_end);
[1]180
[128]181 res = 0;
182 retval = 0;
183 diffs = 0;
184 say_at_end[0] = '\0';
[541]185 printf("Initializing...\n");
[1080]186 bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
[1]187
[810]188 /* initialize log file with time stamp */
189 unlink(MONDO_LOGFILE);
[813]190 log_msg(0, "Time started: %s", mr_date());
[1]191
[807]192 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[128]193 strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
194 tmp[MAX_STR_LEN - 1] = '\0';
195 if (strlen(tmp) >= MAX_STR_LEN - 33) {
196 fatal_error
197 ("Your PATH environmental variable is too long. Please shorten it.");
198 }
199 strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
200 setenv("PATH", tmp, 1);
[1]201
[807]202 /* Add the ARCH environment variable for ia64 purposes */
[128]203 strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
204 tmp[MAX_STR_LEN - 1] = '\0';
205 setenv("ARCH", tmp, 1);
[1]206
[1100]207 /* Add MONDO_SHARE + others environment variable for mindi */
[424]208 setenv_mondo_share();
[258]209
[1100]210 if (stat(MONDO_CACHE, &stbuf) != 0) {
211 mr_mkdir(MONDO_CACHE,0x755);
212 }
213
[807]214 /* Configure the bkpinfo structure, global file paths, etc. */
[128]215 g_main_pid = getpid();
216 log_msg(9, "This");
[1]217
[128]218 register_pid(g_main_pid, "mondo");
219 set_signals(TRUE); // catch SIGTERM, etc.
220 run_program_and_log_output("dmesg -n1", TRUE);
[1]221
[128]222 log_msg(9, "Next");
223 welcome_to_mondoarchive();
224 distro_specific_kludges_at_start_of_mondoarchive();
225 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
226 bkpinfo->scratchdir);
227 g_kernel_version = get_kernel_version();
[1]228
[128]229 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
230 g_loglevel = 10;
231 g_text_mode = TRUE;
232 setup_newt_stuff();
233 if (!strstr(argv[2], "filelist")) {
[541]234 printf("Sorry - filelist goes first\n");
[128]235 finish(1);
236 } else {
237 finish(get_fattr_list(argv[2], argv[3]));
238 }
239 finish(0);
[1]240 }
[128]241 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
242 g_loglevel = 10;
[1]243// chdir("/tmp");
[128]244 g_text_mode = TRUE;
245 setup_newt_stuff();
246 finish(set_fattr_list(argv[2], argv[3]));
[1]247 }
[128]248
249 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
250 g_loglevel = 10;
251 g_text_mode = TRUE;
252 setup_newt_stuff();
253 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
254 printf("in=%s; out=%s\n", argv[2], tmp);
255 finish(1);
[1]256 }
[128]257
258 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
259 g_loglevel = 10;
260 g_text_mode = TRUE;
261 setup_newt_stuff();
262 if (!strstr(argv[2], "filelist")) {
[541]263 printf("Sorry - filelist goes first\n");
[128]264 finish(1);
265 } else {
266 finish(get_acl_list(argv[2], argv[3]));
267 }
268 finish(0);
269 }
270 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
271 g_loglevel = 10;
[1]272// chdir("/tmp");
[128]273 g_text_mode = TRUE;
274 setup_newt_stuff();
275 finish(set_acl_list(argv[2], argv[3]));
276 }
[1]277
[128]278 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
279 g_loglevel = 10;
280 g_text_mode = TRUE;
281 setup_newt_stuff();
282 if (find_cdrw_device(tmp)) {
[541]283 printf("Failed to find CDR-RW drive\n");
[128]284 } else {
[541]285 printf("CD-RW is at %s\n", tmp);
[128]286 }
287 tmp[0] = '\0';
288 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[541]289 printf("Failed to find CD-ROM drive\n");
[128]290 } else {
[541]291 printf("CD-ROM is at %s\n", tmp);
[128]292 }
293 finish(0);
294 }
[1]295
[128]296 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
297 g_loglevel = 10;
298 g_text_mode = TRUE;
299 setup_newt_stuff();
300 if (find_dvd_device(tmp, atoi(argv[2]))) {
[541]301 printf("Failed to find DVD drive\n");
[128]302 } else {
[541]303 printf("DVD is at %s\n", tmp);
[128]304 }
305 finish(0);
306 }
[1]307
[128]308 if (argc > 2 && !strcmp(argv[1], "disksize")) {
309 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
310 finish(0);
311 }
312 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
313 if (is_dev_an_NTFS_dev(argv[2])) {
[541]314 printf("%s is indeed an NTFS dev\n", argv[2]);
[128]315 } else {
[541]316 printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]317 }
318 finish(0);
319 }
320
321 if (pre_param_configuration(bkpinfo)) {
322 fatal_error
323 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
324 }
325
[1]326/* Process command line, if there is one. If not, ask user for info. */
[128]327 if (argc == 1) {
328 g_text_mode = FALSE;
329 setup_newt_stuff();
330 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
331 if (res) {
332 fatal_error
333 ("Syntax error. Please review the parameters you have supplied and try again.");
334 }
335 } else {
336 res = handle_incoming_parameters(argc, argv, bkpinfo);
337 if (res) {
338 printf
[541]339 ("Errors were detected in the command line you supplied.\n");
340 printf("Please review the log file - " MONDO_LOGFILE "\n");
[128]341 log_msg(1, "Mondoarchive will now exit.");
342 finish(1);
343 }
344 setup_newt_stuff();
[1]345 }
346
347/* Finish configuring global structures */
[128]348 if (post_param_configuration(bkpinfo)) {
349 fatal_error
350 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
351 }
[1]352
[128]353 log_to_screen
[541]354 ("BusyBox's sources are available from http://www.busybox.net");
[128]355 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
356 bkpinfo->scratchdir);
[1]357
[128]358 /* If we're meant to backup then backup */
359 if (bkpinfo->backup_data) {
[541]360 res = backup_data(bkpinfo);
[128]361 retval += res;
362 if (res) {
363 strcat(say_at_end,
[541]364 "Data archived. Please check the logs, just as a precaution. ");
[128]365 } else {
[541]366 strcat(say_at_end, "Data archived OK. ");
[128]367 }
368 }
[1]369
370/* If we're meant to verify then verify */
[128]371 if (bkpinfo->verify_data) {
372 res = verify_data(bkpinfo);
373 if (res < 0) {
[541]374 sprintf(tmp, "%d difference%c found.", -res,
[128]375 (-res != 1) ? 's' : ' ');
376 strcat(say_at_end, tmp);
377 log_to_screen(tmp);
378 res = 0;
379 }
380 retval += res;
381 }
[1]382
383/* Offer to write floppy disk images to physical disks */
[128]384 if (bkpinfo->backup_data && !g_skip_floppies) {
385 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
386 retval += res;
[1]387// res = offer_to_write_boot_ISO_to_physical_CD(bkpinfo);
388// retval += res;
[128]389 }
[1]390
391/* Report result of entire operation (success? errors?) */
[128]392 if (!retval) {
393 mvaddstr_and_log_it(g_currentY++, 0,
[541]394 "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]395 } else {
396 mvaddstr_and_log_it(g_currentY++, 0,
[541]397 "Backup and/or verify ran to completion. However, errors did occur.");
[128]398 }
[1]399
[1044]400 if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
[128]401 log_to_screen
[1044]402 ("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]403 }
[1]404
405
[128]406 if (length_of_file("/tmp/changed.files") > 2) {
407 if (g_text_mode) {
408 log_to_screen
[541]409 ("Type 'less /tmp/changed.files' to see which files don't match the archives");
[128]410 } else {
411 log_msg(1,
[541]412 "Type 'less /tmp/changed.files' to see which files don't match the archives");
[128]413 log_msg(2, "Calling popup_changelist_from_file()");
414 popup_changelist_from_file("/tmp/changed.files");
415 log_msg(2, "Returned from popup_changelist_from_file()");
416 }
417 } else {
418 unlink("/tmp/changed.files");
419 }
420 log_to_screen(say_at_end);
421 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
422 run_program_and_log_output(tmp, TRUE);
423 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
[1]424
[128]425 run_program_and_log_output("mount", 2);
[1]426
[128]427 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
428 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
429 if (!retval) {
[541]430 printf("Mondoarchive ran OK.\n");
[128]431 } else {
[541]432 printf("Errors occurred during backup. Please check logfile.\n");
[128]433 }
434 distro_specific_kludges_at_end_of_mondoarchive();
435 register_pid(0, "mondo");
436 set_signals(FALSE);
437 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
438 system(g_erase_tmpdir_and_scratchdir);
439 free_libmondo_global_strings();
[1080]440 mr_free(say_at_end);
441 mr_free(tmp);
442 mr_free(bkpinfo);
[1]443
[128]444 unlink("/tmp/filelist.full");
445 unlink("/tmp/filelist.full.gz");
[1]446
[128]447 if (!g_cdrom_drive_is_here) {
448 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
449 }
450 if (!g_dvd_drive_is_here) {
451 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
452 }
[1]453
[810]454 /* finalize log file with time stamp */
[813]455 log_msg(0, "Time finished: %s", mr_date());
[810]456
[128]457 if (!g_text_mode) {
458 popup_and_OK
[541]459 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
460 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
[128]461 finish(retval);
462 } else {
[541]463 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]464 exit(retval);
465 }
466
467 return EXIT_SUCCESS;
[1]468}
Note: See TracBrowser for help on using the repository browser.