mardi 28 novembre 2017

rsyslog writing to synchronous file does not block

I am on ubuntu 16.04 with rsyslog version 7.4.4. I am writing some tests for an C++ syslog wrapper. In the wrapper I just call syslog(3).

There is only one rule in the config file.

user.*    /var/log/user.log

I turned filter duplicate messages off and I can see all the messages in the log files.

During the test I noticed that the syslog call is not blocking.

TEST(BlockingTest, block)
{
  ifstream file;
  long oriPos=0;
  long newPos=0;

  int offset = strlen("Nov 28 13:07:01 4dac2c62ebe7 logTest: blockinglogger: blocking call")+1;

  file.open("/var/log/user.log");
  if(file.is_open())
  {
    file.seekg(0,ios::end);
    oriPos = file.tellg();
  }
  file.close();

  Syslogging::Logger logger("blockinglogger",Syslogging::Level::DEBUG);
  logger.debug("blocking call");

  // This needs to be here else undefined behavior.
  this_thread::sleep_for(chrono::milliseconds(2));

  file.open("/var/log/user.log");
  if(file.is_open())
  {
    file.seekg(0,ios::end);
    newPos = file.tellg();
  }
  file.close();

  EXPECT_EQ(newPos, oriPos+offset);
}

I thought that using the above config it would block for each syslog call till it was written to the file. But I need a small timeout or I get undefined behavior (sometimes it passes, sometimes it fails).

Do I need another setting or anybody that can explain this behavior more clearly pls?

Aucun commentaire:

Enregistrer un commentaire