Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 01 Sep 2024 08:56:53 +0000
From:      bugzilla-noreply@freebsd.org
To:        bugs@FreeBSD.org
Subject:   [Bug 268436] [libc] syslog() and vsyslog() do not preserve the errno at the time of the call.
Message-ID:  <bug-268436-227-BYpiaVeGDP@https.bugs.freebsd.org/bugzilla/>
In-Reply-To: <bug-268436-227@https.bugs.freebsd.org/bugzilla/>
References:  <bug-268436-227@https.bugs.freebsd.org/bugzilla/>

next in thread | previous in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D268436

--- Comment #4 from Hodong <hodong@nimfsoft.art> ---
#include <syslog.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

void* c_realloc (void* ptr, size_t size)
{
  if (!size)
  {
    free (ptr);
    return NULL;
  }

  void* mem =3D realloc (ptr, size);

  if (mem)
    return mem;

  perror (__PRETTY_FUNCTION__);
  abort ();
}

char* c_str_join (const char *str, ...)
{
  va_list ap;
  size_t  offset =3D 0;
  char*   result =3D NULL;

  va_start (ap, str);

  for (const char* s =3D str; s !=3D NULL; s =3D va_arg (ap, const char*))
  {
    size_t len =3D strlen (s);
    result =3D c_realloc (result, offset + len + 1);
    memcpy (result + offset, s, len);
    offset =3D offset + len;
  }

  va_end (ap);

  if (result)
    result[offset] =3D '\0';

  return result;
}

void c_log (int priority, const char* format, ...)
{
  const char* prefix;
  char*       new_format;
  va_list     args;

  switch (priority)
  {
    case LOG_EMERG:
      prefix =3D "EMERGENCY";
      break;
    case LOG_ALERT:
      prefix =3D "ALERT";
      break;
    case LOG_CRIT:
      prefix =3D "CRITICAL";
      break;
    case LOG_ERR:
      prefix =3D "ERROR";
      break;
    case LOG_WARNING:
      prefix =3D "WARNING";
      break;
    case LOG_NOTICE:
      prefix =3D "NOTICE";
      break;
    case LOG_INFO:
      prefix =3D "INFO";
      break;
    case LOG_DEBUG:
      prefix =3D "DEBUG";
      break;
    default:
      prefix =3D "NOTICE";
      break;
  }

  openlog (getprogname(), LOG_PID | LOG_PERROR, LOG_DAEMON);
  new_format =3D c_str_join (prefix, ": ", format, NULL);

  /*
   * 2022.12.18
   * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D268436
   * The freebsd libc syslog, vsyslog does not preserve the errno at the ti=
me
of
   * the call, so errno may change after the syslog call.
   */
  //int saved_errno =3D errno;
  va_start (args, format);
  vsyslog  (priority, new_format, args);
  va_end   (args);
  //errno =3D saved_errno;

  free (new_format);
  closelog ();
}

#define c_log_critical(format, ...) \
  c_log (LOG_CRIT, __FILE__ ":%d:%s: " format, \
         __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__)

#define c_log_warning(format, ...) \
  c_log (LOG_WARNING, __FILE__ ":%d:%s: " format, \
         __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__)

#define c_log_info(format, ...) \
  c_log (LOG_INFO, format, ## __VA_ARGS__)

int main ()
{
  printf ("before: %d\n", errno);
  c_log_info ("Example: %d", errno);
  c_log_warning ("Example: %d", errno);
  c_log_critical ("Example: %d", errno);
  printf ("after: %d\n", errno);
  return 0;
}

When I tested this code in the past, there was an issue where errno was cha=
nged
after calling vsyslog().
So I used this:

  int saved_errno =3D errno;
  va_start (args, format);
  vsyslog  (priority, new_format, args);
  va_end   (args);
  errno =3D saved_errno;

But now that I've tested it again, errno doesn't change after calling
vsyslog().

  //int saved_errno =3D errno;
  va_start (args, format);
  vsyslog  (priority, new_format, args);
  va_end   (args);
  //errno =3D saved_errno;

So this issue seems to be resolved and I think we can close this issue.

--=20
You are receiving this mail because:
You are the assignee for the bug.=



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-268436-227-BYpiaVeGDP>