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

Last change on this file since 783 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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