-
Notifications
You must be signed in to change notification settings - Fork 216
Add functionality to notify systemd on olad startup and config reload #1444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a30e440
a5e57fe
183ac8d
693fe33
0257949
50c9bda
29556d0
b7afd38
83807d4
469361d
f15b385
d674930
bf8deff
175b393
17d917c
1cd1202
64400b8
d62a8f6
ff34724
f6f5813
15efd6f
18735c4
a21f0a4
c714ee8
527b115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* StrError_R.cpp | ||
* Helper function for StrError_R_XSI(). | ||
* Copyright (C) 2018 Shenghao Yang | ||
*/ | ||
|
||
#if HAVE_CONFIG_H | ||
#include <config.h> | ||
#endif // HAVE_CONFIG_H | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <limits> | ||
#include <string> | ||
#include <sstream> | ||
|
||
#include "ola/base/StrError_R.h" | ||
#include "ola/strings/Format.h" | ||
|
||
namespace ola { | ||
|
||
const int StrError_R_BufSize = 1024; | ||
|
||
std::string StrError_R(int errnum) { | ||
// Pre-allocate to prevent re-allocation. | ||
std::string out(StrError_R_BufSize, '\0'); | ||
if (StrError_R_XSI(errnum, &(out[0]), out.size())) { | ||
out.assign(std::string("errno = ") + ola::strings::IntToString(errnum)); | ||
} else { | ||
out.resize(strlen(&(out[0]))); | ||
} | ||
return out; | ||
} | ||
|
||
} // namespace ola |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* StrError_R_XSI.cpp | ||
* Definition of strerror_r() that is XSI-compliant. | ||
* This extra file is required because the header declaring the symbols | ||
* for the C++ string library conflicts with the macros used to expose | ||
* the XSI-compliant declaration of strerror_r(). | ||
* Copyright (C) 2018 Shenghao Yang | ||
*/ | ||
|
||
#if HAVE_CONFIG_H | ||
#include <config.h> | ||
#endif // HAVE_CONFIG_H | ||
|
||
// Required for string.h to declare the standards-compliant version of | ||
// strerror_r(), when compiling under glibc. Must come before the inclusion | ||
// of string.h. | ||
// These are conditional to avoid errors when not compiling with glibc, or | ||
// when compiling with a compiler that does not define _POSIX_C_SOURCE. | ||
// See strerror(3) as to why the macros are defined this way. | ||
#ifdef _GNU_SOURCE | ||
#undef _GNU_SOURCE | ||
#endif | ||
|
||
#ifdef _POSIX_C_SOURCE | ||
#undef _POSIX_C_SOURCE | ||
#endif | ||
#define _POSIX_C_SOURCE 200112L | ||
|
||
#include <string.h> | ||
|
||
namespace ola { | ||
|
||
int StrError_R_XSI(int errnum, char* buf, size_t buflen) { | ||
return strerror_r(errnum, buf, buflen); | ||
} | ||
|
||
} // namespace ola |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* StrError_R.h | ||
* Helper functions for strerror_r and a declaration of strerror_r that is | ||
* XSI-compliant. | ||
* Copyright (C) 2018 Shenghao Yang | ||
*/ | ||
|
||
/** | ||
* @defgroup strerror Description of system error codes | ||
* @brief Error descriptions | ||
* @details Convenience functions to obtain descriptions of system error codes. | ||
* The functions and variables in this group are only defined if \c strerror_r() | ||
* is available. | ||
*/ | ||
|
||
/** | ||
* @addtogroup strerror | ||
* @{ | ||
* @file StrError_R.h | ||
* @} | ||
*/ | ||
|
||
#ifndef INCLUDE_OLA_BASE_STRERROR_R_H_ | ||
#define INCLUDE_OLA_BASE_STRERROR_R_H_ | ||
|
||
#include <string> | ||
|
||
namespace ola { | ||
|
||
/** | ||
* @addtogroup strerror | ||
* @{ | ||
*/ | ||
|
||
/** | ||
* @brief Length of the internal buffer used for @ref StrError_R(). | ||
* | ||
* If the length of the system-provided error description exceeds the length | ||
* of this buffer minus one, then the output of @ref StrError_R() will only | ||
* include the numerical error value provided. | ||
*/ | ||
extern const int StrError_R_BufSize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One for @nomis52 then as to why we've gone for static const over extern const? |
||
|
||
/** | ||
* @brief XSI-compliant version of \c strerror_r(). | ||
* | ||
* @see https://linux.die.net/man/3/strerror for more details. | ||
*/ | ||
int StrError_R_XSI(int errnum, char* buf, size_t buflen); | ||
|
||
/** | ||
* @brief Convenience function that wraps @ref StrError_R_XSI(). | ||
* | ||
* @param errnum error value to generate the textual description for. | ||
* @return Textual description of the error value. If the description is | ||
* truncated due to an insufficient buffer length, the description will | ||
* be in the form: "errno = errnum". | ||
* @sa StrError_R_BufSize for information regarding truncation. | ||
*/ | ||
std::string StrError_R(int errnum); | ||
|
||
/**@}*/ | ||
} // namespace ola | ||
|
||
#endif // INCLUDE_OLA_BASE_STRERROR_R_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Library General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Systemd.cpp | ||
* Provides wrapped access to systemd interfaces. | ||
* Copyright (C) 2018 Shenghao Yang | ||
*/ | ||
|
||
#if HAVE_CONFIG_H | ||
#include <config.h> | ||
#endif // HAVE_CONFIG_H | ||
|
||
#if HAVE_LIBSYSTEMD | ||
#include <systemd/sd-daemon.h> | ||
#endif // HAVE_LIBSYSTEMD | ||
|
||
#include "ola/Logging.h" | ||
#include "ola/base/StrError_R.h" | ||
|
||
#include "olad/Systemd.h" | ||
|
||
namespace ola { | ||
|
||
int SystemdNotify(int unset_environment, const char *state) { | ||
int rtn = sd_notify(unset_environment, state); | ||
if (rtn < 0) { | ||
OLA_WARN << "Error sending notification to systemd: " | ||
<< ola::StrError_R(-rtn); | ||
} | ||
return rtn; | ||
} | ||
|
||
bool SystemdNotifyAvailable() { | ||
return (sd_notify(0, "") != 0); | ||
} | ||
|
||
} // namespace ola |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can drop the x's and square brackets here, looking at HAVE_KQUEUE just above, which will have had broader OS testing.