Skip to content

Commit f15b385

Browse files
committed
Add a declaration for XSI-compliant strerror_r
- This is to ensure that compilation will not be dependent on GNU extensions being enabled. - A new source file has to be added, because one cannot simply undef _GNU_SOURCE in Systemd.cpp - it will cause the a failure to compile - something in the logging framework depends on GNU extensions.
1 parent 469361d commit f15b385

File tree

5 files changed

+95
-10
lines changed

5 files changed

+95
-10
lines changed

configure.ac

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,8 @@ AC_ARG_ENABLE(
755755

756756
have_libsystemd="no"
757757
AS_IF([test "x$enable_systemd" != "xno"],
758-
[PKG_CHECK_MODULES([libsystemd], [libsystemd >= 38], [have_libsystemd="yes"])
758+
[PKG_CHECK_MODULES([libsystemd], [libsystemd >= 38], [have_libsystemd="yes"],
759+
[AC_MSG_WARN([disabling systemd support: can't find libsystemd])])
759760
AC_CHECK_HEADER([systemd/sd-daemon.h], [],
760761
[have_libsystemd="no"
761762
AC_MSG_WARN([disabling systemd support: can't find systemd/sd-daemon.h])])

olad/Makefile.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ ola_server_additional_libs += common/http/libolahttp.la
4343
endif
4444

4545
if HAVE_LIBSYSTEMD
46-
ola_server_sources += olad/Systemd.cpp olad/Systemd.h
46+
ola_server_sources += olad/Strerror_r.cpp olad/Strerror_r.h \
47+
olad/Systemd.cpp olad/Systemd.h
4748
ola_server_additional_libs += $(libsystemd_LIBS)
4849
endif
4950

olad/Strerror_r.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU Library General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15+
*
16+
* Strerror_r.cpp
17+
* Definition of strerror_r that is XSI-compliant.
18+
* Copyright (C) 2018 Shenghao Yang
19+
*/
20+
21+
#if HAVE_CONFIG_H
22+
#include <config.h>
23+
#endif // HAVE_CONFIG_H
24+
25+
// Required for string.h to declare the standards-compliant version of
26+
// strerror_r(), when compiling under glibc. Must come before the inclusion
27+
// of string.h.
28+
// These are conditional to avoid errors when not compiling with glibc, or
29+
// when compiling with a compiler that does not define _POSIX_C_SOURCE.
30+
#ifdef _GNU_SOURCE
31+
#undef _GNU_SOURCE
32+
#endif
33+
34+
#ifdef _POSIX_C_SOURCE
35+
#undef _POSIX_C_SOURCE
36+
#endif
37+
#define _POSIX_C_SOURCE 200112L
38+
39+
#include <string.h>
40+
41+
#include "olad/Strerror_r.h"
42+
43+
namespace ola {
44+
45+
int Strerror_r(int errnum, char* buf, size_t buflen) {
46+
return strerror_r(errnum, buf, buflen);
47+
}
48+
49+
} // namespace ola

olad/Strerror_r.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU Library General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15+
*
16+
* Strerror_r.h
17+
* Declaration of strerror_r that is XSI-compliant.
18+
* Copyright (C) 2018 Shenghao Yang
19+
*/
20+
21+
#ifndef OLAD_STRERROR_R_H_
22+
#define OLAD_STRERROR_R_H_
23+
24+
namespace ola {
25+
26+
/*
27+
* @brief XSI-compliant version of @ref strerror_r()
28+
29+
* See strerror(3) for more details.
30+
*/
31+
int Strerror_r(int errnum, char* buf, size_t buflen);
32+
33+
} // namespace ola
34+
35+
#endif // OLAD_STRERROR_R_H_

olad/Systemd.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424

2525
#if HAVE_LIBSYSTEMD
2626
#include <systemd/sd-daemon.h>
27-
#include <errno.h>
28-
#include <string.h>
2927
#endif // HAVE_LIBSYSTEMD
3028

3129
#include "ola/Logging.h"
3230

31+
#include "olad/Strerror_r.h"
3332
#include "olad/Systemd.h"
3433

3534
namespace ola {
@@ -38,12 +37,12 @@ int SystemdNotify(int unset_environment, const char *state) {
3837
int rtn = sd_notify(unset_environment, state);
3938
if (rtn < 0) {
4039
char buf[1024];
41-
// olad is compiled under GNU extensions: the return value of
42-
// strerror_r() is not an error code but a pointer to a character array
43-
// containing the error description. The array returned is always NUL
44-
// terminated, even in case of errors, so blindly using it is not a problem.
45-
OLA_WARN << "Error sending notification to systemd: "
46-
<< strerror_r(-rtn, buf, sizeof(buf));
40+
OLA_WARN << "Error sending notification to systemd: ";
41+
if (ola::Strerror_r(-rtn, buf, sizeof(buf))) {
42+
OLA_WARN << "errno = " << -rtn;
43+
} else {
44+
OLA_WARN << buf;
45+
}
4746
}
4847
return rtn;
4948
}

0 commit comments

Comments
 (0)