Back to index

im-sdk  12.3.91
segvlogger.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /*
00003  * Copyright (c) Red Hat, Inc. All rights reserved. This copyrighted material
00004  * is made available to anyone wishing to use, modify, copy, or
00005  * redistribute it subject to the terms and conditions of the GNU General
00006  * Public License v.2.
00007  *
00008  * You should have received a copy of the GNU General Public License
00009  * along with this program; if not, write to the Free Software
00010  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00011  *
00012  */
00013 #include <stdio.h>
00014 #include <limits.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <sys/types.h>
00018 #include <sys/stat.h>
00019 #include <pwd.h>
00020 #include <unistd.h>
00021 #include <errno.h>
00022 #include <time.h>
00023 #include <dirent.h>
00024 
00025 int
00026 main(int argc, char **argv)
00027 {
00028     char buffer[1024], buf[1024], logdir[PATH_MAX], homedir[PATH_MAX];
00029     FILE *fpr, *fpw;
00030     pid_t ppid;
00031     size_t size;
00032     struct stat st;
00033     struct passwd *pass;
00034     uid_t uid;
00035 
00036     uid = getuid();
00037     pass = getpwuid(uid);
00038     snprintf(homedir, PATH_MAX, "%s/.iiim", pass->pw_dir);
00039     snprintf(logdir, PATH_MAX, "%s/.iiim/logs", pass->pw_dir);
00040     if (argc < 3) {
00041        printf("Usage: %s <pid> <signum>\n"
00042               "store the gdb debugging logs for <pid> to %s\n", argv[0], LOGDIR "/segvYYYYMMDDhhmmss.log");
00043        exit(1);
00044     }
00045     ppid = getppid();
00046     if (ppid != atoi(argv[1])) {
00047        fprintf(stderr, "the parent process id %d and the given pid %s is different.\n",
00048               ppid, argv[1]);
00049        exit(1);
00050     }
00051     if (stat(DATADIR "/gdbcmd", &st) == -1) {
00052        fprintf(stderr, "cannot find the command list file `%s' for gdb.\n", DATADIR "/gdbcmd");
00053        exit(1);
00054     }
00055 
00056     if (getenv("IIIMF_DEBUG") != NULL) {
00057        /* nothing to do debug with this */
00058        fprintf(stderr, "According to your request, won't run gdb at this time.\n");
00059        fprintf(stderr, "please kill %s after debugging has been done.\n", argv[0]);
00060        fprintf(stderr, "For your information, iiimd should be running as pid %d, and it has received signal %s\n", ppid, argv[2]);
00061        while (1) {
00062            sleep(1);
00063        }
00064        exit(1);
00065     }
00066     snprintf(buffer, 1024, "gdb --batch --quiet --command %s/gdbcmd --pid %d",
00067             DATADIR, ppid);
00068 
00069     if ((fpr = popen(buffer, "r")) != NULL) {
00070        char logfile[PATH_MAX];
00071        time_t t = time(NULL);
00072        struct tm tm;
00073 
00074        if (stat(LOGDIR, &st) == -1) {
00075            if (errno == ENOENT) {
00076               /* make the log directory */
00077               mkdir(LOGDIR, 0755);
00078            }
00079        }
00080        if (stat(homedir, &st) == -1) {
00081            if (errno == ENOENT) {
00082               mkdir(homedir, 0700);
00083            }
00084        }
00085        if (stat(logdir, &st) == -1) {
00086            if (errno == ENOENT) {
00087               mkdir(logdir, 0700);
00088            }
00089        }
00090 
00091        tm = *localtime(&t);
00092        snprintf(logfile, PATH_MAX, "%s/segv%04d%02d%02d%02d%02d%02d.log", LOGDIR, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
00093 
00094        fpw = fopen(logfile, "w");
00095        if (fpw == NULL) {
00096            snprintf(logfile, PATH_MAX, "%s/segv%04d%02d%02d%02d%02d%02d.log", logdir, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
00097            fpw = fopen(logfile, "w");
00098        }
00099        if (fpw != NULL) {
00100            fprintf(fpw, "==================================================\nProcess %d received signal %s\n==================================================\n",
00101                   ppid, argv[2]);
00102            while (1) {
00103               size = fread(buf, sizeof (char), 1024, fpr);
00104               fwrite(buf, sizeof (char), size, fpw);
00105               if (size < 1024) {
00106                   fputs("\n", fpw);
00107                   break;
00108               }
00109            }
00110 
00111            fclose(fpw);
00112        } else {
00113            fprintf(stderr, "failed to open the log file `%s'.\n", logfile);
00114        }
00115        pclose(fpr);
00116     } else {
00117        fprintf(stderr, "failed to fork\n");
00118     }
00119 
00120     return 0;
00121 }