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
RevLine 
[1]1/***************************************************************************
[2211]2$Id: mondoarchive.c 2421 2009-09-24 13:50:38Z bruno $
3* The main file for mondoarchive.
4*/
[1]5
6/************************* #include statements *************************/
7#include <pthread.h>
8#include <stdio.h>
9#include <stdlib.h>
[1930]10#include "my-stuff.h"
[2211]11#include "mr_mem.h"
[2421]12#include "mr_str.h"
[1]13#include "../common/mondostructures.h"
14#include "../common/libmondo.h"
[1917]15#include "../common/libmondo-cli-EXT.h"
[1383]16#include "../common/libmondo-tools-EXT.h"
[1315]17#include "mondoarchive.h"
[1]18
19// for CVS
[128]20//static char cvsid[] = "$Id: mondoarchive.c 2421 2009-09-24 13:50:38Z bruno $";
[1]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;
[1736]28extern bool g_remount_floppy_at_end;
[1]29extern char *g_cdrw_drive_is_here;
[128]30static char *g_cdrom_drive_is_here = NULL;
31static char *g_dvd_drive_is_here = NULL;
[1]32extern double g_kernel_version;
33
34/***************** global vars, used only by main.c ******************/
[2322]35long diffs = 0L;
[1]36
37extern t_bkptype g_backup_media_type;
38extern int g_loglevel;
[2340]39extern char *g_magicdev_command;
[1]40
[1917]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
[1645]55/* Reference to global bkpinfo */
56struct s_bkpinfo *bkpinfo;
57
[2320]58/* Make cleanup the finish function */
59void (*mr_cleanup)(int) = finish;
[2211]60
[1967]61/* To be coded */
62void free_MR_global_filenames(void) {
63}
64
[1]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 */
[1236]71void welcome_to_mondoarchive(void)
[1]72{
[1375]73 char *tmp = NULL;
74
[2340]75 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
[1375]76 log_msg(0, "running %s binaries", get_architecture());
77 tmp = get_uname_m();
78 log_msg(0, "running on %s architecture", tmp);
[2241]79 mr_free(tmp);
[2340]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, "-----------------------------------------------------------");
[1]90
[128]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...");
[1]100}
101
102
103/**
104 * Do whatever is necessary to insure a successful backup on the Linux distribution
105 * of the day.
106 */
[1236]107void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]108{
[128]109 log_msg(2, "Unmounting old ramdisks if necessary");
110 stop_magicdev_if_necessary(); // for RH+Gnome users
[2030]111 /*
[128]112 run_program_and_log_output
113 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
[2030]114 */
[128]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
[1]119}
120
121
122
123/**
124 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
125 */
[1236]126void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]127{
[128]128 log_msg(2, "Restarting magicdev if necessary");
129 sync();
130 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]131
[128]132 log_msg(2, "Restarting autofs if necessary");
133 sync();
134 // restart_autofs_if_necessary(); // for Xandros users
[1]135
[128]136 log_msg(2, "Restarting supermounts if necessary");
137 sync();
138 remount_supermounts_if_necessary(); // for Mandrake users
[1]139
[128]140 log_msg(2, "Unmounting /boot if necessary");
141 sync();
142 unmount_boot_if_necessary(); // for Gentoo users
[1]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 */
[128]153int main(int argc, char *argv[])
[1]154{
[2241]155 char *tmp = NULL;
[2113]156 char *tmp1 = NULL;
[2322]157 int res = 0;
158 int i = 0;
159 int retval = 0;
[2211]160 char *say_at_end = NULL;
[1]161
[2321]162 printf("Initializing...\n");
163
[810]164 /* initialize log file with time stamp */
165 unlink(MONDO_LOGFILE);
[2421]166 tmp = mr_date();
167 log_msg(0, "Time started: %s", tmp);
168 mr_free(tmp);
[1]169
[2421]170 init_bkpinfo();
171
[2321]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;
[2322]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 }
[2321]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
[807]199 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[2323]200 mr_asprintf(tmp1,"%s:/sbin:/usr/sbin:/usr/local/sbin",getenv("PATH"));
[2113]201 setenv("PATH", tmp1, 1);
[2321]202 mr_free(tmp1);
[1]203
[807]204 /* Add the ARCH environment variable for ia64 purposes */
[2323]205 mr_asprintf(tmp1,"%s",get_architecture());
[2113]206 setenv("ARCH", tmp1, 1);
[2321]207 mr_free(tmp1);
[1]208
[424]209 /* Add MONDO_SHARE environment variable for mindi */
210 setenv_mondo_share();
[258]211
[807]212 /* Configure the bkpinfo structure, global file paths, etc. */
[128]213 g_main_pid = getpid();
214 log_msg(9, "This");
[1]215
[128]216 set_signals(TRUE); // catch SIGTERM, etc.
217 run_program_and_log_output("dmesg -n1", TRUE);
[1]218
[128]219 log_msg(9, "Next");
[1747]220 make_hole_for_dir(MONDO_CACHE);
221
[128]222 welcome_to_mondoarchive();
223 distro_specific_kludges_at_start_of_mondoarchive();
224 g_kernel_version = get_kernel_version();
[1]225
[128]226 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
227 g_loglevel = 10;
228 if (!strstr(argv[2], "filelist")) {
[541]229 printf("Sorry - filelist goes first\n");
[128]230 finish(1);
231 } else {
232 finish(get_fattr_list(argv[2], argv[3]));
233 }
234 finish(0);
[1]235 }
[128]236 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
237 g_loglevel = 10;
238 finish(set_fattr_list(argv[2], argv[3]));
[1]239 }
[128]240
241 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
242 g_loglevel = 10;
[2413]243 tmp = mr_stresc(argv[2], "[]*?", '\\');
[128]244 printf("in=%s; out=%s\n", argv[2], tmp);
[2413]245 mr_free(tmp);
[128]246 finish(1);
[1]247 }
[128]248
249 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
250 g_loglevel = 10;
251 if (!strstr(argv[2], "filelist")) {
[541]252 printf("Sorry - filelist goes first\n");
[128]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 }
[1]263
[128]264 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
265 g_loglevel = 10;
[2325]266 if ((tmp = find_cdrw_device()) == NULL) {
[541]267 printf("Failed to find CDR-RW drive\n");
[128]268 } else {
[541]269 printf("CD-RW is at %s\n", tmp);
[128]270 }
[2325]271 mr_free(tmp);
272 if ((tmp = find_cdrom_device(FALSE)) == NULL) {
[541]273 printf("Failed to find CD-ROM drive\n");
[128]274 } else {
[541]275 printf("CD-ROM is at %s\n", tmp);
[128]276 }
[2325]277 mr_free(tmp);
[128]278 finish(0);
279 }
[1]280
[128]281 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
282 g_loglevel = 10;
[2325]283 if ((tmp = find_dvd_device()) == NULL) {
[541]284 printf("Failed to find DVD drive\n");
[128]285 } else {
[541]286 printf("DVD is at %s\n", tmp);
[128]287 }
[2325]288 mr_free(tmp);
[128]289 finish(0);
290 }
[1]291
[128]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])) {
[541]298 printf("%s is indeed an NTFS dev\n", argv[2]);
[128]299 } else {
[541]300 printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]301 }
302 finish(0);
303 }
304
[1645]305 if (pre_param_configuration()) {
[2340]306 fatal_error("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
[128]307 }
308
[2340]309 /* Process command line, if there is one. If not, ask user for info. */
[128]310 if (argc == 1) {
[1645]311 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
[128]312 if (res) {
[2340]313 fatal_error("Syntax error. Please review the parameters you have supplied and try again.");
[128]314 }
315 } else {
[1645]316 res = handle_incoming_parameters(argc, argv);
[128]317 if (res) {
[2340]318 printf("Errors were detected in the command line you supplied.\n");
[1319]319 printf("Please review the log file - %s\n", MONDO_LOGFILE );
[128]320 log_msg(1, "Mondoarchive will now exit.");
321 finish(1);
322 }
[1]323 }
324
325/* Finish configuring global structures */
[1645]326 if (post_param_configuration()) {
[2321]327 fatal_error("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
[128]328 }
[1]329
[2321]330 log_to_screen("BusyBox's sources are available from http://www.busybox.net");
[1]331
[128]332 /* If we're meant to backup then backup */
333 if (bkpinfo->backup_data) {
[1645]334 res = backup_data();
[128]335 retval += res;
336 if (res) {
[2323]337 mr_asprintf(say_at_end, "Data archived. Please check the logs, just as a precaution. ");
[128]338 } else {
[2323]339 mr_asprintf(say_at_end, "Data archived OK. ");
[128]340 }
341 }
[1]342
343/* If we're meant to verify then verify */
[128]344 if (bkpinfo->verify_data) {
[1645]345 res = verify_data();
[128]346 if (res < 0) {
[2324]347 mr_asprintf(tmp, "%d difference%c found.", -res, (-res != 1) ? 's' : ' ');
[2323]348 mr_asprintf(say_at_end, "%s", tmp);
[128]349 log_to_screen(tmp);
[2241]350 mr_free(tmp);
[128]351 res = 0;
352 }
353 retval += res;
354 }
[1]355
356/* Report result of entire operation (success? errors?) */
[128]357 if (!retval) {
358 mvaddstr_and_log_it(g_currentY++, 0,
[541]359 "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]360 } else {
361 mvaddstr_and_log_it(g_currentY++, 0,
[541]362 "Backup and/or verify ran to completion. However, errors did occur.");
[128]363 }
[1]364
[1437]365 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
[128]366 log_to_screen
[1437]367 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]368 }
[1]369
[1747]370 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
[128]371 if (g_text_mode) {
[1747]372 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]373 } else {
[1747]374 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]375 log_msg(2, "Calling popup_changelist_from_file()");
[1747]376 popup_changelist_from_file(MONDO_CACHE"/changed.files");
[128]377 log_msg(2, "Returned from popup_changelist_from_file()");
378 }
379 } else {
[1747]380 unlink(MONDO_CACHE"/changed.files");
[128]381 }
[2211]382 if (say_at_end != NULL) {
383 log_to_screen(say_at_end);
[2321]384 mr_free(say_at_end);
[2211]385 }
[2323]386 mr_asprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
[128]387 run_program_and_log_output(tmp, TRUE);
[2241]388 mr_free(tmp);
[1697]389 if (bkpinfo->backup_media_type == usb) {
390 log_msg(1, "Unmounting USB device.");
[2325]391 if (bkpinfo->media_device == NULL) {
392 fatal_error("USB device set to NULL");
393 }
[2323]394 mr_asprintf(tmp, "umount %s1", bkpinfo->media_device);
[1697]395 run_program_and_log_output(tmp, TRUE);
[2241]396 mr_free(tmp);
[1697]397 }
[1]398
[128]399 run_program_and_log_output("mount", 2);
[1]400
[1747]401 system("rm -f "MONDO_CACHE"/last-backup.aborted");
[128]402 if (!retval) {
[541]403 printf("Mondoarchive ran OK.\n");
[128]404 } else {
[541]405 printf("Errors occurred during backup. Please check logfile.\n");
[128]406 }
407 distro_specific_kludges_at_end_of_mondoarchive();
408 set_signals(FALSE);
[1644]409
[128]410 free_libmondo_global_strings();
[1703]411
[1]412
[128]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 }
[1]419
[810]420 /* finalize log file with time stamp */
[2421]421 tmp = mr_date();
422 log_msg(0, "Time finished: %s", tmp);
423 mr_free(tmp);
[810]424
[1708]425 chdir("/tmp");
[1644]426
[128]427 if (!g_text_mode) {
[2322]428 popup_and_OK("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
[541]429 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
[128]430 } else {
[541]431 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]432 }
[1703]433 finish(retval);
[128]434
435 return EXIT_SUCCESS;
[1]436}
Note: See TracBrowser for help on using the repository browser.