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::string configDirectory
static std::string logDirectory
static std::string pidDirectory

Friends

class ConfigRoot

Detailed Description

Definition at line 99 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 779 of file Config.cpp.

779 {
780 return configRoot.bootstrap(argc, argv);
781 }
static int argc
Definition Config.h:119
static ConfigRoot configRoot
Definition Config.h:116
static char ** argv
Definition Config.h:120

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 791 of file Config.cpp.

791 {
792 return applicationName;
793 }
static std::string applicationName
Definition Config.h:122

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 795 of file Config.cpp.

795 {
796 return configRoot.logLevelOpt->as<int>();
797 }

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

◆ getVerboseLevel()

int Config::getVerboseLevel ( )
static

Definition at line 799 of file Config.cpp.

799 {
800 return configRoot.verboseLevelOpt->as<int>();
801 }

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

◆ init()

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

Definition at line 657 of file Config.cpp.

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

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 783 of file Config.cpp.

783 {
784 configRoot.parse(argc, argv);
785 }

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

Here is the call graph for this function:

◆ terminate()

void Config::terminate ( )
static

Definition at line 787 of file Config.cpp.

787 {
788 configRoot.terminate();
789 }

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 128 of file Config.h.

Member Data Documentation

◆ applicationName

std::string Config::applicationName
staticprivate

Definition at line 122 of file Config.h.

Referenced by getApplicationName(), and init().

◆ argc

int Config::argc = 0
staticprivate

Definition at line 119 of file Config.h.

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

◆ argv

char ** Config::argv = nullptr
staticprivate

Definition at line 120 of file Config.h.

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

◆ configDirectory

std::string Config::configDirectory
staticprivate

Definition at line 124 of file Config.h.

Referenced by init().

◆ configRoot

ConfigRoot Config::configRoot
static

◆ logDirectory

std::string Config::logDirectory
staticprivate

Definition at line 125 of file Config.h.

Referenced by init().

◆ pidDirectory

std::string Config::pidDirectory
staticprivate

Definition at line 126 of file Config.h.

Referenced by init().


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