1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00

Added propper daemonization

This commit is contained in:
Ralf Ramsauer 2015-05-14 15:33:40 +02:00
parent de6008e28b
commit 7c063e77d3
5 changed files with 46 additions and 13 deletions

10
CMakeLists.txt Executable file → Normal file
View File

@ -29,6 +29,7 @@ door.cpp
epaper.cpp epaper.cpp
logic.cpp logic.cpp
util.cpp util.cpp
daemon.cpp
epaper/Display_COG_Process.c epaper/Display_COG_Process.c
epaper/Display_Controller.c epaper/Display_Controller.c
epaper/Display_Hardware_Driver.c epaper/Display_Hardware_Driver.c
@ -38,9 +39,10 @@ epaper/bsp.c
add_executable(doorlockd ${SRCS}) add_executable(doorlockd ${SRCS})
target_link_libraries(doorlockd wiringPi jsoncpp ldap ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(doorlockd wiringPi jsoncpp ldap ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
install(TARGETS doorlockd DESTINATION sbin) install(TARGETS doorlockd DESTINATION sbin/)
install(FILES img/template.png DESTINATION share/doorlockd) install(FILES img/template.png DESTINATION share/doorlockd/)
install(DIRECTORY scripts/ DESTINATION share/doorlockd install(DIRECTORY scripts/ DESTINATION share/doorlockd/
FILES_MATCHING PATTERN "scripts/doorlockd-*" FILES_MATCHING PATTERN "scripts/doorlockd-*"
PERMISSIONS WORLD_EXECUTE WORLD_READ OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE OWNER_WRITE) PERMISSIONS WORLD_EXECUTE WORLD_READ OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE OWNER_WRITE)
install(FILES scripts/doorlockd.service DESTINATION /etc/systemd/system) install(FILES scripts/doorlockd.service DESTINATION /etc/systemd/system/)
install(FILES scripts/doorlockd DESTINATION /etc/sysconfig/)

2
config.h.in Executable file → Normal file
View File

@ -14,6 +14,8 @@
#define DEFAULT_WEB_PREFIX "https://lock.binary.kitchen/" #define DEFAULT_WEB_PREFIX "https://lock.binary.kitchen/"
#define DEFAULT_LDAP_SERVER "ldaps://ldap.binary.kitchen" #define DEFAULT_LDAP_SERVER "ldaps://ldap.binary.kitchen"
#define DEFAULT_BINDDN "cn=%s,ou=Users,dc=binary-kitchen,dc=de" #define DEFAULT_BINDDN "cn=%s,ou=Users,dc=binary-kitchen,dc=de"
#define DEFAULT_LOG_FILE "/var/log/doorlockd.log"
#define DEFAULT_ALLOWED_IP_PREFIX "172.23.3." #define DEFAULT_ALLOWED_IP_PREFIX "172.23.3."
#define DEFAULT_PID_FILE "/var/run/doorlockd.pid"
#endif #endif

10
logic.cpp Executable file → Normal file
View File

@ -56,7 +56,7 @@ Logic::Response Logic::parseRequest(const string &str)
{ {
unique_lock<mutex> l(_mutex); unique_lock<mutex> l(_mutex);
_logger("Parsing request..."); _logger(LogLevel::info, "Incoming request...");
Json::Reader reader; Json::Reader reader;
Json::Value root; Json::Value root;
Response retval = Fail; Response retval = Fail;
@ -66,7 +66,7 @@ Logic::Response Logic::parseRequest(const string &str)
bool suc = reader.parse(str, root, false); bool suc = reader.parse(str, root, false);
if (!suc) if (!suc)
{ {
_logger(LogLevel::error, "Request ist not valid JSON!"); _logger(LogLevel::warning, "Request ist not valid JSON!");
retval = NotJson; retval = NotJson;
goto out; goto out;
} }
@ -89,8 +89,10 @@ Logic::Response Logic::parseRequest(const string &str)
goto out; goto out;
} }
printf("Action: %s\nAuthenticate: %d\nIP: %s\n",action.c_str(), authenticate, ip.c_str()); _logger(" Action: " + action, LogLevel::notice);
printf("User: %s\nPassword: XXXXXXXXXX\nToken: %s\n",user.c_str(), token.c_str()); _logger(" User : " + user, LogLevel::notice);
_logger(" IP : " + ip, LogLevel::notice);
_logger(" Token : " + token, LogLevel::notice);
if (authenticate == true) if (authenticate == true)
{ {

0
logic.h Executable file → Normal file
View File

37
main.cpp Executable file → Normal file
View File

@ -3,10 +3,12 @@
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <csignal>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "daemon.h"
#include "config.h" #include "config.h"
#include "logic.h" #include "logic.h"
@ -19,6 +21,15 @@ const static Logger &l = Logger::get();
static unique_ptr<Logic> logic = nullptr; static unique_ptr<Logic> logic = nullptr;
boost::asio::io_service io_service;
void signal_handler(int signum)
{
(void)signum;
io_service.stop();
logic.reset();
}
class session class session
: public std::enable_shared_from_this<session> : public std::enable_shared_from_this<session>
{ {
@ -96,6 +107,9 @@ int main(int argc, char** argv)
string bindDN; string bindDN;
string lockPagePrefix; string lockPagePrefix;
string allowedIpPrefix; string allowedIpPrefix;
string logfile;
string pidFile;
bool foreground = false;
l(LogLevel::notice, "Starting doorlockd"); l(LogLevel::notice, "Starting doorlockd");
@ -106,10 +120,13 @@ int main(int argc, char** argv)
("help,h", "print help") ("help,h", "print help")
("tokentimeout,t", po::value<unsigned int>(&timeout)->default_value(DEFAULT_TOKEN_TIMEOUT), "Token timeout in seconds") ("tokentimeout,t", po::value<unsigned int>(&timeout)->default_value(DEFAULT_TOKEN_TIMEOUT), "Token timeout in seconds")
("port,p", po::value<short>(&port)->default_value(DEFAULT_PORT), "Port") ("port,p", po::value<short>(&port)->default_value(DEFAULT_PORT), "Port")
("ldap,l", po::value<string>(&ldapServer)->default_value(DEFAULT_LDAP_SERVER), "Ldap Server") ("ldap,s", po::value<string>(&ldapServer)->default_value(DEFAULT_LDAP_SERVER), "Ldap Server")
("bidndn,b", po::value<string>(&bindDN)->default_value(DEFAULT_BINDDN), "Bind DN, %s means username") ("bidndn,b", po::value<string>(&bindDN)->default_value(DEFAULT_BINDDN), "Bind DN, %s means username")
("web,w", po::value<string>(&lockPagePrefix)->default_value(DEFAULT_WEB_PREFIX), "Prefix of the webpage") ("web,w", po::value<string>(&lockPagePrefix)->default_value(DEFAULT_WEB_PREFIX), "Prefix of the webpage")
("ip,i", po::value<string>(&allowedIpPrefix)->default_value(DEFAULT_ALLOWED_IP_PREFIX), "Default allowed IP Prefix"); ("ip,i", po::value<string>(&allowedIpPrefix)->default_value(DEFAULT_ALLOWED_IP_PREFIX), "Default allowed IP Prefix")
("foreground,f", po::bool_switch(&foreground)->default_value(false), "Run in foreground")
("logfile,l", po::value<string>(&logfile)->default_value(DEFAULT_LOG_FILE), "Log file")
("pid,z", po::value<string>(&pidFile)->default_value(DEFAULT_PID_FILE), "PID file");
po::variables_map vm; po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm); po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
@ -131,6 +148,19 @@ int main(int argc, char** argv)
goto out; goto out;
} }
daemonize(!foreground,
"/",
"/dev/null",
logfile,
logfile,
pidFile);
signal(SIGINT, signal_handler);
signal(SIGKILL, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler);
logic = unique_ptr<Logic>(new Logic(tokenTimeout, logic = unique_ptr<Logic>(new Logic(tokenTimeout,
ldapServer, ldapServer,
bindDN, bindDN,
@ -138,11 +168,8 @@ int main(int argc, char** argv)
allowedIpPrefix)); allowedIpPrefix));
try { try {
boost::asio::io_service io_service;
server s(io_service, port); server s(io_service, port);
io_service.run(); io_service.run();
retval = 0;
} }
catch (const char* const &ex) { catch (const char* const &ex) {
ostringstream str; ostringstream str;