
June 9th, 2003, 10:12 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Just to get you started, here are my (admittedly very terse) notes from "Linux Programming by Example":
Quote:
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);
}
|