Syslog.h does not write a log

Hi Stackoverflow Hackers!

I have a very small case of a serious problem or misunderstanding between me and the C syslog () function.

The code compiles just fine, and I see how it performs its "dummy task" (pinging 8.8.8.8), but a specific log is simply not added. I am completely puzzled by this and have no idea what might be wrong. It’s already SMAO (Searched My Ass Off - trying to popularize this), but simply cannot make it work properly.

The code is here:

#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <syslog.h> #include <string.h> // Application settings # TODO: import these from a .ini file #define WORKDIR "/var/run/management" #define LOGDIR "/var/log/management" #define LOGFILE "/var/log/management.log" #define SCRIPTDIR "/var/spool/management" #define PIDFILE "/var/run/management/daemon.pid" int main(void) { printf("Management Daemon\nInitializing..."); pid_t pid, sid; setlogmask(LOG_UPTO (LOG_NOTICE)); openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog(LOG_NOTICE, "Management Daemon started by User %d", getuid()); closelog(); printf("Done.\nForking...\n"); pid = fork(); if(pid < 0) { printf("Fork failed! Exiting...\n"); // TODO: syslog facility syslog(LOG_EMERG, "Forking failed, exiting."); closelog(); exit(EXIT_FAILURE); } if(pid > 0) { FILE *pidfile; pidfile = fopen(PIDFILE, "w"); fprintf(pidfile, "%d\n", pid); fclose(pidfile); printf("PID written to %s\nUsing log file %s\nGoing silent...\n", PIDFILE, LOGFILE); // TODO: syslog facility openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog(LOG_NOTICE, "Fork returned with valid PID: %d. PID file: %s", pid, PIDFILE); exit(EXIT_SUCCESS); } umask(0); sid = setsid(); if(sid < 0) { // TODO: syslog facility printf("SID is corrupt\n"); exit(EXIT_FAILURE); } if(sid > 0) { printf("Acquired valid SID!\n"); } if((chdir(WORKDIR)) < 0) { // TODO: syslog facility printf("Directory change failed. Got permissions?\n"); exit(EXIT_FAILURE); } // Going Silent close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); // daemon init here FILE *fp; // The big loop closelog(); while(1) { // Do your thing... // TODO: implement daemon executing mechanics. ret = system("ping 8.8.8.8 -c 1"); fp = fopen("/var/run/management.output", "a"); if(ret == 0) { fprintf(fp, "Success!\n"); fclose(fp); } if(ret == 512) { fprintf(fp, "Failure!\n"); fclose(fp); } // Sleep till the next heartbeat // TODO: notice level log about heartbeats if verbosity is set to high sleep(30); } exit(EXIT_SUCCESS); } 

All help would be greatly appreciated!

Estol


Decision:

The following lines have been added to syslog-ng.conf:

 destination d_management { file("/var/log/management/management.log"); }; filter f_management { match("MD:" value("MESSAGE")); }; log { source(src); filter(f_management); destination(d_management); }; 

All log messages containing the MD: sequence will be redirected to the management.log file. It works like a charm. Thanks again for pointing me in the right direction.

+7
source share
1 answer

The first argument to openlog () is the program identifier, not the log file name. This explains why you will not find anything in /var/log/management.log .

The log file name is usually set in the logging daemon configuration file. The name and location of this file depends on the daemon you are using (for example, /etc/syslog-ng/syslog-ng.conf on my machine).

+5
source

All Articles