bug-sysutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-sysutils] RFC dmesgd


From: Barry deFreese
Subject: [Bug-sysutils] RFC dmesgd
Date: Sun, 23 Jan 2005 09:29:19 -0800
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20050105 Debian/1.7.5-1

Howdy folks,

Here is the dmesgd that I was working on. If any of you have time I would appreciate some comments/suggestions.

One of the problems I am not sure what to do with is if dmesgd is restarted without rebooting. Obviosly klog is empty at that point so what should I do, somehow keep the same log file?

Thanks!

--
Barry deFreese
Debian 3.0r1 "Woody"
GNU/Hurd
Registered Linux "Newbie" #302256 - Hurd H4XX0r wannabe

"Programming today is a race between software engineers striving
to build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots. So far, the Universe is
winning." Rich Cook.



/* dmesgd.c -- daemon to capture the kernel ring buffer
 *
 * Copyright (C) 2004 Barry deFreese
 * Copyright (C) 2004 Free Software Foundation, Inc.
 *
 *  This file is part of GNU Sysutils
 *
 *  GNU Sysutils is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  GNU Sysutils is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <paths.h>
#include <syslog.h>

#ifndef _PATH_KLOG
#define _PATH_KLOG "/dev/klog"
#endif

#define PRG_NAME "dmesg"
#define KBUFSIZE 16384
#define _PATH_DMLOG "/var/log/dmesg.log"
extern const char *progname;

struct stat statbuf;

/**
 * The program's main-function
 */
int main(void)
{
        pid_t sid;

        /* File desc for the kernel buffer and dmesg log */
        int fdkbuf, fdlog;

        int retval;

        char kbuf[KBUFSIZE];

        if (getuid() == 0) {

                /* Open KLOG */
                fdkbuf = open(_PATH_KLOG, O_RDONLY);

                if (fdkbuf < 0) {
                        syslog(LOG_ERR, "open");
                        exit(EXIT_FAILURE);
                }

                /* Open dmesg log */
                fdlog = open(_PATH_DMLOG, 
                  O_CREAT | O_WRONLY | O_TRUNC, 0644);

                if (fdlog < 0) {
                        syslog(LOG_ERR, "open");
                        exit(EXIT_FAILURE);
                }

                switch (fork()) {
                case 0:
                        break;

                case -1:
                        perror("fork");
                        exit(EXIT_FAILURE);

                default:
                        _exit(0);
                }
        } else {
                fprintf(stderr, "%s: failure, you must be root\n", PRG_NAME);
                exit(EXIT_FAILURE);
        }

        /* Start a new session in the child process */
        sid = setsid();
        if (sid < 0) {
                perror("setsid");
                exit(EXIT_FAILURE);
        }

        /* Make / the current directory */
        if ((chdir("/")) < 0) {
                perror("chdir");
                exit(EXIT_FAILURE);
        }

        /* Reset the file mode */
        umask (0);

        openlog("dmesg", LOG_PERROR, LOG_DAEMON);

        /* Close standard input/outputs */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        freopen("/dev/null", "r", stdin);
        freopen(_PATH_DMLOG, "a", stdout);
        freopen(_PATH_DMLOG, "a", stderr);
syslog(LOG_DEBUG, "dmesgd: reopened std*\n");

        while(1) {
                retval = read(fdkbuf, kbuf, KBUFSIZE);

                if (retval > 0) {
                        retval = write(fdlog, kbuf, retval);
                                if (retval < 0) {
                                        syslog(LOG_ERR, "write");
                                        exit(EXIT_FAILURE);
                                }
                } else if (retval < 0) {
                        syslog(LOG_ERR, "read");
                        exit(EXIT_FAILURE);
                }
        }

        close(fdkbuf);
        close(fdlog);
        unlink(_PATH_DMLOG);
        exit(EXIT_SUCCESS);
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]