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

Last change on this file since 171 was 171, checked in by bcornec, 18 years ago

memory management continues:

  • mondoarchive handled completely
  • bkpinfo, begining of dyn. alloc.
  • lot of changes around memory everywhere

=> even if it compiles, i'm pretty sure it doesn't work yet (even not tried)

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