/* * Guillaume Cottenceau (gc@mandrakesoft.com) * * Copyright 2000 MandrakeSoft * * This software may be freely redistributed under the terms of the GNU * public license. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * Portions from Erik Troan (ewt@redhat.com) * * Copyright 1996 Red Hat Software * */ #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <stdarg.h> #include <stdio.h> #include <string.h> #include <time.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include "stage1.h" #include "log.h" static FILE * logfile = NULL; void vlog_message(const char * s, va_list args) { fprintf(logfile, "* "); vfprintf(logfile, s, args); fprintf(logfile, "\n"); fflush(logfile); } void log_message(const char * s, ...) { va_list args; if (!logfile) { fprintf(stderr, "Log is not open!\n"); return; } va_start(args, s); vlog_message(s, args); va_end(args); return; } void log_perror(char *msg) { log_message("%s: %s", msg, strerror(errno)); } void open_log(void) { if (!IS_TESTING) { mknod("/dev/ttyprintk", S_IFCHR, MKDEV(5, 3)); logfile = fopen("/dev/ttyprintk", "w"); if (!logfile) logfile = fopen("/tmp/install.log", "a"); } else logfile = fopen("debug.log", "w"); } void close_log(void) { if (logfile) { log_message("stage1: disconnecting life support systems"); fclose(logfile); } } int redirect2log(int fd) { int rc; if (!logfile) { fprintf(stderr, "%s: log file is not open", __func__); return -1; } rc = dup2(fileno(logfile), fd); if (rc < 0) { log_message("%s: dup2 %d: error: %s", __func__, fd, strerror(errno)); } return rc; } int redirect2null(int fd) { int nullfd = -1, newfd = -1; nullfd = open("/dev/null", O_RDONLY); if (nullfd < 0) { log_message("%s: open /dev/null: %s", __func__, strerror(errno)); goto out; } newfd = dup2(nullfd, fd); if (newfd < 0) { log_message("%s: dup2: %s", __func__, strerror(errno)); goto out; } out: if (nullfd >= 0) { close(nullfd); } return newfd; }