Thread: syslog help

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12

    syslog help


    Hi there,

    I am just curious if anyone can tell me where I can find information about syslog. I need to know how I can manipulate the syslog, for instance, when people connect thru' my proxy, I want to be able to log their details, etc....

    Geekoid
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Just to get you started, here are my (admittedly very terse) notes from "Linux Programming by Example":
    need to log errors, etc, to a system log:
    <syslog.h>
    void openlog(char *ident, int option, int facility);
    void closelog(void);
    void syslog(int priority, char *format, ...);

    OR options for openlog:
    LOG_CONS
    LOG_NDELAY
    LOG_PERROR
    LOG_PID

    facility is one of:
    LOG_AUTHPRIV, LOG_CRON, LOG_DAEMON, LOG_KERN, LOG_LOCAL[0-7], LOG_LPR,
    LOG_MAIL, LOG_NEWS, LOG_SYSLOG, LOG_USER, LOG_UUCP

    priority is one of:
    LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG
    The example given was (downloaded from their site):
    Code:
    int main(void)
    {
        pid_t pid, sid;
        time_t timebuf;
        int fd, len;
         
        /* Open the system log */
        openlog("lpedated", LOG_PID, LOG_DAEMON);
    
        pid = fork();
        if(pid < 0) {
    	syslog(LOG_ERR, "%s\n", perror);
    	exit(EXIT_FAILURE);
        }
        if(pid > 0) 
    	/* In the parent, let's bail */
    	exit(EXIT_SUCCESS);
    
        /* In the child... */
        /* First, start a new session */
        if((sid = setsid()) < 0) {
    	syslog(LOG_ERR, "%s\n", "setsid");
    	exit(EXIT_FAILURE);
        }
        /* Next, make / the current directory */
        if((chdir("/")) < 0) {
    	syslog(LOG_ERR, "%s\n", "chdir");
    	exit(EXIT_FAILURE);
        }
        /* Reset the file mode */
        umask(0);
    
        /* Close stdin, etc. */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    
        /*  Finally, do our work */
        len = strlen(ctime(&timebuf));
        while(1) {
    	char *buf = malloc(sizeof(char) * (len + 1));
    	
    	if(buf == NULL) {
    	    syslog(LOG_ERR, "malloc");
    	    exit(EXIT_FAILURE);
    	}
    	if((fd = open("/var/log/lpedated.log",
    		      O_CREAT | O_WRONLY | O_APPEND, 0600)) < 0) {
    	    syslog(LOG_ERR, "open");
    	    exit(EXIT_FAILURE);
    	}
    	time(&timebuf);
    	strncpy(buf, ctime(&timebuf), len + 1);
    	write(fd, buf, len + 1);
    	close(fd);
    	sleep(60);
        }
        closelog();
        exit(EXIT_SUCCESS);
    }

IMN logo majestic logo threadwatch logo seochat tools logo