SNode.C
Loading...
Searching...
No Matches
utils::Config Class Reference

#include <Config.h>

Collaboration diagram for utils::Config:

Public Member Functions

 Config ()=delete
 Config (const Config &)=delete
 ~Config ()=delete
Configoperator= (const Config &)=delete

Static Public Member Functions

static bool init (int argc, char *argv[])
static bool bootstrap ()
static void parse ()
static void terminate ()
static const std::string & getApplicationName ()
static int getLogLevel ()
static int getVerboseLevel ()

Static Public Attributes

static ConfigRoot configRoot

Static Private Attributes

static int argc = 0
static char ** argv = nullptr
static std::string applicationName
static std::shared_ptr< AppWithPtrconfigRootApp
static std::string configDirectory
static std::string logDirectory
static std::string pidDirectory

Friends

class ConfigRoot

Detailed Description

Definition at line 100 of file Config.h.

Constructor & Destructor Documentation

◆ Config() [1/2]

utils::Config::Config ( )
delete

◆ Config() [2/2]

utils::Config::Config ( const Config & )
delete

◆ ~Config()

utils::Config::~Config ( )
delete

Member Function Documentation

◆ bootstrap()

bool Config::bootstrap ( )
static

Definition at line 774 of file Config.cpp.

774 {
775 return configRoot.bootstrap(argc, argv);
776 }
static int argc
Definition Config.h:120
static ConfigRoot configRoot
Definition Config.h:117
static char ** argv
Definition Config.h:121

References argc, argv, utils::ConfigRoot::bootstrap(), and configRoot.

Referenced by core::EventLoop::start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getApplicationName()

const std::string & Config::getApplicationName ( )
static

Definition at line 786 of file Config.cpp.

786 {
787 return applicationName;
788 }
static std::string applicationName
Definition Config.h:123

References applicationName.

Referenced by utils::createCommandLineTemplate(), web::websocket::client::SubProtocolFactorySelector::load(), and web::websocket::server::SubProtocolFactorySelector::load().

Here is the caller graph for this function:

◆ getLogLevel()

int Config::getLogLevel ( )
static

Definition at line 790 of file Config.cpp.

790 {
791 return configRoot.logLevelOpt->as<int>();
792 }

References configRoot, and utils::ConfigRoot::logLevelOpt.

◆ getVerboseLevel()

int Config::getVerboseLevel ( )
static

Definition at line 794 of file Config.cpp.

794 {
795 return configRoot.verboseLevelOpt->as<int>();
796 }

References configRoot, and utils::ConfigRoot::verboseLevelOpt.

◆ init()

bool Config::init ( int argc,
char * argv[] )
static

Definition at line 652 of file Config.cpp.

652 {
653 bool proceed = true;
654
657
658 applicationName = std::filesystem::path(argv[0]).filename();
659
660 uid_t euid = 0;
661 const struct passwd* pw = nullptr;
662 const struct group* gr = nullptr;
663
664 if ((pw = getpwuid(getuid())) == nullptr) {
665 proceed = false;
666 } else if ((gr = getgrgid(pw->pw_gid)) == nullptr) {
667 proceed = false;
668 } else if ((euid = geteuid()) == 0) {
669 configDirectory = "/etc/snode.c";
670 logDirectory = "/var/log/snode.c";
671 pidDirectory = "/var/run/snode.c";
672 } else {
673 const char* homedir = nullptr;
674 if ((homedir = std::getenv("XDG_CONFIG_HOME")) == nullptr) {
675 if ((homedir = std::getenv("HOME")) == nullptr) {
676 homedir = pw->pw_dir;
677 }
678 }
679
680 if (homedir != nullptr) {
681 configDirectory = std::string(homedir) + "/.config/snode.c";
682 logDirectory = std::string(homedir) + "/.local/log/snode.c";
683 pidDirectory = std::string(homedir) + "/.local/run/snode.c";
684 } else {
685 proceed = false;
686 }
687 }
688
689 if (proceed && !std::filesystem::exists(configDirectory)) {
690 if (std::filesystem::create_directories(configDirectory)) {
691 std::filesystem::permissions(
693 (std::filesystem::perms::owner_all | std::filesystem::perms::group_read | std::filesystem::perms::group_exec) &
694 ~std::filesystem::perms::others_all);
695 if (geteuid() == 0) {
696 const struct group* gr = nullptr;
697 if ((gr = getgrnam(XSTR(GROUP_NAME))) != nullptr) {
698 if (chown(configDirectory.c_str(), euid, gr->gr_gid) < 0) {
699 std::cout << "Warning: Can not set group ownership of '" << configDirectory
700 << "' to 'snodec':" << strerror(errno) << std::endl;
701 }
702 } else {
703 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup" << std::endl;
704 std::cout << " and add the current user to this group." << std::endl;
705 std::filesystem::remove(configDirectory);
706 proceed = false;
707 }
708 }
709 } else {
710 std::cout << "Error: Can not create directory '" << configDirectory << "'" << std::endl;
711 proceed = false;
712 }
713 }
714
715 if (proceed && !std::filesystem::exists(logDirectory)) {
716 if (std::filesystem::create_directories(logDirectory)) {
717 std::filesystem::permissions(logDirectory,
718 (std::filesystem::perms::owner_all | std::filesystem::perms::group_all) &
719 ~std::filesystem::perms::others_all);
720 if (geteuid() == 0) {
721 const struct group* gr = nullptr;
722 if ((gr = getgrnam(XSTR(GROUP_NAME))) != nullptr) {
723 if (chown(logDirectory.c_str(), euid, gr->gr_gid) < 0) {
724 std::cout << "Warning: Can not set group ownership of '" << logDirectory << "' to 'snodec':" << strerror(errno)
725 << std::endl;
726 }
727 } else {
728 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup" << std::endl;
729 std::cout << " and add the current user to this group." << std::endl;
730 std::filesystem::remove(configDirectory);
731 proceed = false;
732 }
733 }
734 } else {
735 std::cout << "Error: Can not create directory '" << logDirectory << "'" << std::endl;
736 proceed = false;
737 }
738 }
739
740 if (proceed && !std::filesystem::exists(pidDirectory)) {
741 if (std::filesystem::create_directories(pidDirectory)) {
742 std::filesystem::permissions(pidDirectory,
743 (std::filesystem::perms::owner_all | std::filesystem::perms::group_all) &
744 ~std::filesystem::perms::others_all);
745 if (geteuid() == 0) {
746 const struct group* gr = nullptr;
747 if ((gr = getgrnam(XSTR(GROUP_NAME))) != nullptr) {
748 if (chown(pidDirectory.c_str(), euid, gr->gr_gid) < 0) {
749 std::cout << "Warning: Can not set group ownership of '" << pidDirectory << "' to 'snodec':" << strerror(errno)
750 << std::endl;
751 }
752 } else {
753 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup." << std::endl;
754 std::cout << " and add the current user to this group." << std::endl;
755 std::filesystem::remove(configDirectory);
756 proceed = false;
757 }
758 }
759 } else {
760 std::cout << "Error: Can not create directory '" << pidDirectory << "'" << std::endl;
761 proceed = false;
762 }
763 }
764
765 if (proceed) {
766 configRoot.addRootOptions(applicationName, pw->pw_name, gr->gr_name, configDirectory, logDirectory, pidDirectory);
767
768 proceed = configRoot.parse1(argc, argv);
769 }
770
771 return proceed;
772 }
#define XSTR(s)
static std::string configDirectory
Definition Config.h:127
static std::string pidDirectory
Definition Config.h:129
static std::string logDirectory
Definition Config.h:128

References utils::ConfigRoot::addRootOptions(), applicationName, argc, argv, configDirectory, configRoot, logDirectory, utils::ConfigRoot::parse1(), and pidDirectory.

Referenced by core::EventLoop::init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

Config & utils::Config::operator= ( const Config & )
delete

◆ parse()

void Config::parse ( )
static

Definition at line 778 of file Config.cpp.

778 {
779 configRoot.parse(argc, argv);
780 }

References argc, argv, configRoot, and utils::SubCommand::parse().

Here is the call graph for this function:

◆ terminate()

void Config::terminate ( )
static

Definition at line 782 of file Config.cpp.

782 {
783 configRoot.terminate();
784 }

References configRoot, and utils::ConfigRoot::terminate().

Referenced by core::EventLoop::free().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConfigRoot

friend class ConfigRoot
friend

Definition at line 131 of file Config.h.

Member Data Documentation

◆ applicationName

std::string Config::applicationName
staticprivate

Definition at line 123 of file Config.h.

Referenced by getApplicationName(), and init().

◆ argc

int Config::argc = 0
staticprivate

Definition at line 120 of file Config.h.

Referenced by bootstrap(), init(), and parse().

◆ argv

char ** Config::argv = nullptr
staticprivate

Definition at line 121 of file Config.h.

Referenced by bootstrap(), init(), and parse().

◆ configDirectory

std::string Config::configDirectory
staticprivate

Definition at line 127 of file Config.h.

Referenced by init().

◆ configRoot

◆ configRootApp

std::shared_ptr< AppWithPtr > Config::configRootApp
staticprivate

Definition at line 125 of file Config.h.

Referenced by utils::ConfigRoot::ConfigRoot().

◆ logDirectory

std::string Config::logDirectory
staticprivate

Definition at line 128 of file Config.h.

Referenced by init().

◆ pidDirectory

std::string Config::pidDirectory
staticprivate

Definition at line 129 of file Config.h.

Referenced by init().


The documentation for this class was generated from the following files: