source: MondoRescue/branches/2.2.10/mondo/src/mondoarchive/mondoarchive.c@ 2421

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