source: MondoRescue/branches/2.2.9/mondo/src/mondoarchive/mondoarchive.c@ 2230

Last change on this file since 2230 was 2211, checked in by Bruno Cornec, 15 years ago

r3089@localhost: bruno | 2009-05-18 06:41:05 +0200

  • move call to asprintf to call to mr_asprintf (suppress a compiler warning)
  • remove all the most obvious bad call to strcat and replace by mr_strcat as appropriate
  • Property svn:keywords set to Id
File size: 12.4 KB
Line 
1/***************************************************************************
2$Id: mondoarchive.c 2211 2009-06-03 17:10:19Z bruno $
3* The main file for mondoarchive.
4*/
5
6/************************* #include statements *************************/
7#include <pthread.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include "my-stuff.h"
11#include "mr_mem.h"
12#include "../common/mondostructures.h"
13#include "../common/libmondo.h"
14#include "../common/libmondo-cli-EXT.h"
15#include "../common/libmondo-tools-EXT.h"
16#include "mondoarchive.h"
17
18// for CVS
19//static char cvsid[] = "$Id: mondoarchive.c 2211 2009-06-03 17:10:19Z bruno $";
20
21/************************* external variables *************************/
22extern void set_signals(int);
23extern int g_current_media_number;
24extern int g_currentY;
25extern bool g_text_mode;
26extern char *g_boot_mountpt;
27extern bool g_remount_floppy_at_end;
28extern char *g_cdrw_drive_is_here;
29static char *g_cdrom_drive_is_here = NULL;
30static char *g_dvd_drive_is_here = NULL;
31extern double g_kernel_version;
32
33/***************** global vars, used only by main.c ******************/
34long diffs;
35
36extern t_bkptype g_backup_media_type;
37extern int g_loglevel;
38
39/**
40 * Whether we're restoring from ISOs. Obviously not, since this is the
41 * backup program.
42 * @note You @b MUST declare this variable somewhere in your program if
43 * you use libmondo. Otherwise the link will fail.
44 * @ingroup globalGroup
45 */
46bool g_ISO_restore_mode = FALSE;
47
48/* Do we use extended attributes and acl ?
49 * * By default no, use --acl & --attr options to force their usage */
50char *g_getfacl = NULL;
51char *g_getfattr = NULL;
52
53/* Reference to global bkpinfo */
54struct s_bkpinfo *bkpinfo;
55
56/* No cleanup for the moment */
57void (*mr_cleanup)(void) = NULL;
58
59/* To be coded */
60void free_MR_global_filenames(void) {
61}
62
63/****************** subroutines used only by main.c ******************/
64
65
66/**
67 * Print a "don't panic" message to the log and a message about the logfile to the screen.
68 */
69void welcome_to_mondoarchive(void)
70{
71 char *tmp = NULL;
72
73 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
74 PACKAGE_VERSION);
75 log_msg(0, "running %s binaries", get_architecture());
76 tmp = get_uname_m();
77 log_msg(0, "running on %s architecture", tmp);
78 free(tmp);
79 log_msg(0,
80 "-----------------------------------------------------------");
81 log_msg(0,
82 "NB: Mondo logs almost everything, so don't panic if you see");
83 log_msg(0,
84 "some error messages. Please read them carefully before you");
85 log_msg(0,
86 "decide to break out in a cold sweat. Despite (or perhaps");
87 log_msg(0,
88 "because of) the wealth of messages. some users are inclined");
89 log_msg(0,
90 "to stop reading this log. If Mondo stopped for some reason,");
91 log_msg(0,
92 "chances are it's detailed here. More than likely there's a");
93 log_msg(0,
94 "message at the very end of this log that will tell you what");
95 log_msg(0,
96 "is wrong. Please read it! -Devteam");
97 log_msg(0,
98 "-----------------------------------------------------------");
99
100 log_msg(0, "Zero...");
101 log_msg(1, "One...");
102 log_msg(2, "Two...");
103 log_msg(3, "Three...");
104 log_msg(4, "Four...");
105 log_msg(5, "Five...");
106 log_msg(6, "Six...");
107 log_msg(7, "Seven...");
108 log_msg(8, "Eight...");
109 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
110}
111
112
113extern char *g_magicdev_command;
114
115/**
116 * Do whatever is necessary to insure a successful backup on the Linux distribution
117 * of the day.
118 */
119void distro_specific_kludges_at_start_of_mondoarchive(void)
120{
121 log_msg(2, "Unmounting old ramdisks if necessary");
122 stop_magicdev_if_necessary(); // for RH+Gnome users
123 /*
124 run_program_and_log_output
125 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
126 */
127 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
128 // stop_autofs_if_necessary(); // for Xandros users
129 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
130 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
131}
132
133
134
135/**
136 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
137 */
138void distro_specific_kludges_at_end_of_mondoarchive(void)
139{
140// char tmp[500];
141 log_msg(2, "Restarting magicdev if necessary");
142 sync();
143 restart_magicdev_if_necessary(); // for RH+Gnome users
144
145 log_msg(2, "Restarting autofs if necessary");
146 sync();
147 // restart_autofs_if_necessary(); // for Xandros users
148
149 log_msg(2, "Restarting supermounts if necessary");
150 sync();
151 remount_supermounts_if_necessary(); // for Mandrake users
152
153 log_msg(2, "Unmounting /boot if necessary");
154 sync();
155 unmount_boot_if_necessary(); // for Gentoo users
156
157// log_msg( 2, "Cleaning up KDE desktop");
158// clean_up_KDE_desktop_if_necessary();
159}
160
161
162/**
163 * Backup/verify the user's data.
164 * What did you think it did, anyway? :-)
165 */
166int main(int argc, char *argv[])
167{
168 char *tmp;
169 char *tmp1 = NULL;
170 int res, retval;
171 char *say_at_end = NULL;
172
173/* Make sure I'm root; abort if not */
174 if (getuid() != 0) {
175 fprintf(stderr, "Please run as root.\r\n");
176 exit(127);
177 }
178
179/* If -V, -v or --version then echo version no. and quit */
180 if (argc == 2
181 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
182 || !strcmp(argv[argc - 1], "--version"))) {
183 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
184 exit(0);
185 }
186
187/* Initialize variables */
188
189 printf("Initializing...\n");
190 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
191 fatal_error("Cannot malloc bkpinfo");
192 }
193 reset_bkpinfo();
194
195 res = 0;
196 retval = 0;
197 diffs = 0;
198 malloc_string(tmp);
199 malloc_libmondo_global_strings();
200
201 /* initialize log file with time stamp */
202 unlink(MONDO_LOGFILE);
203 log_msg(0, "Time started: %s", mr_date());
204
205 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
206 mr_asprintf(&tmp1,"%s:/sbin:/usr/sbin:/usr/local/sbin",getenv("PATH"));
207 setenv("PATH", tmp1, 1);
208 paranoid_free(tmp1);
209
210 /* Add the ARCH environment variable for ia64 purposes */
211 mr_asprintf(&tmp1,"%s",get_architecture());
212 setenv("ARCH", tmp1, 1);
213 paranoid_free(tmp1);
214
215 /* Add MONDO_SHARE environment variable for mindi */
216 setenv_mondo_share();
217
218 /* Configure the bkpinfo structure, global file paths, etc. */
219 g_main_pid = getpid();
220 log_msg(9, "This");
221
222 set_signals(TRUE); // catch SIGTERM, etc.
223 run_program_and_log_output("dmesg -n1", TRUE);
224
225 log_msg(9, "Next");
226 make_hole_for_dir(MONDO_CACHE);
227
228 welcome_to_mondoarchive();
229 distro_specific_kludges_at_start_of_mondoarchive();
230 g_kernel_version = get_kernel_version();
231
232 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
233 g_loglevel = 10;
234 g_text_mode = TRUE;
235 setup_newt_stuff();
236 if (!strstr(argv[2], "filelist")) {
237 printf("Sorry - filelist goes first\n");
238 finish(1);
239 } else {
240 finish(get_fattr_list(argv[2], argv[3]));
241 }
242 finish(0);
243 }
244 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
245 g_loglevel = 10;
246// chdir("/tmp");
247 g_text_mode = TRUE;
248 setup_newt_stuff();
249 finish(set_fattr_list(argv[2], argv[3]));
250 }
251
252 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
253 g_loglevel = 10;
254 g_text_mode = TRUE;
255 setup_newt_stuff();
256 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
257 printf("in=%s; out=%s\n", argv[2], tmp);
258 finish(1);
259 }
260
261 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
262 g_loglevel = 10;
263 g_text_mode = TRUE;
264 setup_newt_stuff();
265 if (!strstr(argv[2], "filelist")) {
266 printf("Sorry - filelist goes first\n");
267 finish(1);
268 } else {
269 finish(get_acl_list(argv[2], argv[3]));
270 }
271 finish(0);
272 }
273 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
274 g_loglevel = 10;
275// chdir("/tmp");
276 g_text_mode = TRUE;
277 setup_newt_stuff();
278 finish(set_acl_list(argv[2], argv[3]));
279 }
280
281 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
282 g_loglevel = 10;
283 g_text_mode = TRUE;
284 setup_newt_stuff();
285 if (find_cdrw_device(tmp)) {
286 printf("Failed to find CDR-RW drive\n");
287 } else {
288 printf("CD-RW is at %s\n", tmp);
289 }
290 tmp[0] = '\0';
291 if (find_cdrom_device(tmp, atoi(argv[2]))) {
292 printf("Failed to find CD-ROM drive\n");
293 } else {
294 printf("CD-ROM is at %s\n", tmp);
295 }
296 finish(0);
297 }
298
299 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
300 g_loglevel = 10;
301 g_text_mode = TRUE;
302 setup_newt_stuff();
303 if (find_dvd_device(tmp, atoi(argv[2]))) {
304 printf("Failed to find DVD drive\n");
305 } else {
306 printf("DVD is at %s\n", tmp);
307 }
308 finish(0);
309 }
310
311 if (argc > 2 && !strcmp(argv[1], "disksize")) {
312 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
313 finish(0);
314 }
315 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
316 if (is_dev_an_NTFS_dev(argv[2])) {
317 printf("%s is indeed an NTFS dev\n", argv[2]);
318 } else {
319 printf("%s is _not_ an NTFS dev\n", argv[2]);
320 }
321 finish(0);
322 }
323
324 if (pre_param_configuration()) {
325 fatal_error
326 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
327 }
328
329/* Process command line, if there is one. If not, ask user for info. */
330 if (argc == 1) {
331 g_text_mode = FALSE;
332 setup_newt_stuff();
333 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
334 if (res) {
335 fatal_error
336 ("Syntax error. Please review the parameters you have supplied and try again.");
337 }
338 } else {
339 res = handle_incoming_parameters(argc, argv);
340 if (res) {
341 printf
342 ("Errors were detected in the command line you supplied.\n");
343 printf("Please review the log file - %s\n", MONDO_LOGFILE );
344 log_msg(1, "Mondoarchive will now exit.");
345 finish(1);
346 }
347 setup_newt_stuff();
348 }
349
350/* Finish configuring global structures */
351 if (post_param_configuration()) {
352 fatal_error
353 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
354 }
355
356 log_to_screen
357 ("BusyBox's sources are available from http://www.busybox.net");
358
359 /* If we're meant to backup then backup */
360 if (bkpinfo->backup_data) {
361 res = backup_data();
362 retval += res;
363 if (res) {
364 mr_asprintf(&say_at_end,
365 "Data archived. Please check the logs, just as a precaution. ");
366 } else {
367 mr_asprintf(&say_at_end, "Data archived OK. ");
368 }
369 }
370
371/* If we're meant to verify then verify */
372 if (bkpinfo->verify_data) {
373 res = verify_data();
374 if (res < 0) {
375 sprintf(tmp, "%d difference%c found.", -res,
376 (-res != 1) ? 's' : ' ');
377 mr_asprintf(&say_at_end, tmp);
378 log_to_screen(tmp);
379 res = 0;
380 }
381 retval += res;
382 }
383
384/* Report result of entire operation (success? errors?) */
385 if (!retval) {
386 mvaddstr_and_log_it(g_currentY++, 0,
387 "Backup and/or verify ran to completion. Everything appears to be fine.");
388 } else {
389 mvaddstr_and_log_it(g_currentY++, 0,
390 "Backup and/or verify ran to completion. However, errors did occur.");
391 }
392
393 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
394 log_to_screen
395 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
396 }
397
398 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
399 if (g_text_mode) {
400 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
401 } else {
402 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
403 log_msg(2, "Calling popup_changelist_from_file()");
404 popup_changelist_from_file(MONDO_CACHE"/changed.files");
405 log_msg(2, "Returned from popup_changelist_from_file()");
406 }
407 } else {
408 unlink(MONDO_CACHE"/changed.files");
409 }
410 if (say_at_end != NULL) {
411 log_to_screen(say_at_end);
412 paranoid_free(say_at_end);
413 }
414 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
415 run_program_and_log_output(tmp, TRUE);
416 if (bkpinfo->backup_media_type == usb) {
417 log_msg(1, "Unmounting USB device.");
418 sprintf(tmp, "umount %s1", bkpinfo->media_device);
419 run_program_and_log_output(tmp, TRUE);
420 }
421
422 run_program_and_log_output("mount", 2);
423
424 system("rm -f "MONDO_CACHE"/last-backup.aborted");
425 if (!retval) {
426 printf("Mondoarchive ran OK.\n");
427 } else {
428 printf("Errors occurred during backup. Please check logfile.\n");
429 }
430 distro_specific_kludges_at_end_of_mondoarchive();
431 set_signals(FALSE);
432
433 free_libmondo_global_strings();
434
435
436 if (!g_cdrom_drive_is_here) {
437 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
438 }
439 if (!g_dvd_drive_is_here) {
440 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
441 }
442
443 /* finalize log file with time stamp */
444 log_msg(0, "Time finished: %s", mr_date());
445
446 chdir("/tmp");
447
448 if (!g_text_mode) {
449 popup_and_OK
450 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
451 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
452 } else {
453 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
454 }
455 finish(retval);
456
457 return EXIT_SUCCESS;
458}
Note: See TracBrowser for help on using the repository browser.