Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4534568
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:17:49+00:00 2026-05-21T14:17:49+00:00

Hi there Stackoverflow hackers! I have a very minor case of serious problem, or

  • 0

Hi there Stackoverflow hackers!

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

The code compiles just fine, and I can see its doing its “dummy job” (pinging 8.8.8.8), but the defined log just does not append. I am completly puzzled by this, and have no idea what could be wrong. Already SMAO (Searched My Ass Off – trying to popularize that) but just can’t get it working properly.

Code 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 highly appreciated!

estol


The solution:

Added the following lines 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 that contains the MD: sequence, will be redirected to the management.log file.
Works like a charm. Thanks again for pointing me in the right direction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:17:50+00:00Added an answer on May 21, 2026 at 2:17 pm

    The first argument to openlog() is a program identifier, not a log file name. That explains why you won’t find anything in /var/log/management.log.

    The name of the log file is usually set in the logger daemon’s configuration file. The name and location of that file depend on the daemon you’re using (e.g. it’s /etc/syslog-ng/syslog-ng.conf on my machine).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just like stackoverflow, there is a many-to-many relationship between Question and Tag. After running
I tryied on Stackoverflow to post my code, but I think there are too
There have been a number of posts on stackoverflow and other places detailing how
There have been a few questions asked along this line stackoverflow such as What
SO i have searched on stackoverflow and there does not seem to be any
Why hello there Stackoverflow, I have a nutcracker for ya, is there an good
For stackoverflow.com there is a stat for how many views for a question. How
There is a question here in stackoverflow with the same title but that is
There are a lot of questions on Stackoverflow about curl but I could not
There are a lot of SQL Top N questions on stackoverflow but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.