Commit 7fe62539 authored by Seblu's avatar Seblu
Browse files

Correction de l'ordre d'evaluation des options

parent 083366c7
Loading
Loading
Loading
Loading
+24 −33
Original line number Diff line number Diff line
@@ -21,17 +21,7 @@

SLDaemon *SLDaemon::instance_ = 0;

SLDaemon::SLDaemon() : socket_fs_(0) {
  char buf[128];

// set default options
  if (gethostname(buf, 128) == 0) {
    options_.login = buf;
  }
  options_.port = 18136;
  options_.conffile = "/etc/sldrc";
  options_.retrydelay = 60;
}
SLDaemon::SLDaemon() : socket_fs_(0) {}

SLDaemon &SLDaemon::Instance() {
  if (instance_ == 0)
@@ -58,8 +48,8 @@ void SLDaemon::usage(const char *argv0) const {
	    << "  -V           : Print version and exit." << std::endl;
}

SLDaemon::Options *SLDaemon::getoptions(int argc, char *argv[]) const {
  Options opt, *mopt;
SLDaemon::Options SLDaemon::getoptions(int argc, char *argv[]) const {
  Options opt;

  for (int i = 1; i < argc; ++i) {
    if (!strcmp(argv[i], "-h")) {
@@ -117,12 +107,10 @@ SLDaemon::Options *SLDaemon::getoptions(int argc, char *argv[]) const {
    else
      throw Error(ERR_USAGE, (string) "Invalid options : " + argv[i]);
  }
  mopt = new Options();
  *mopt = opt;
  return mopt;
  return opt;
}

SLDaemon::Options *SLDaemon::getoptions(const string file) const {
SLDaemon::Options SLDaemon::getoptions(const string file) const {
  ifstream fs;
  char line[MAX_CONF_LINE_SIZE];
  int i=0;
@@ -131,13 +119,13 @@ SLDaemon::Options *SLDaemon::getoptions(const string file) const {

  fs.open(file.c_str(), ifstream::in);

  if (!fs.is_open())
    return 0;
  if (fs.fail())
    throw Error(ERR_FILE, "Unable to open configuration file");

  while (fs.good()) {
  while (!fs.eof()) {
    fs.getline(line, MAX_CONF_LINE_SIZE);
    if (fs.eof())
      break;
//     if (fs.eof())
//       break;
    sscanf(line, "%10[^=]=%2039s\n", name, value); //TODO: fix magic number

    if (!strcasecmp(name, "host"))
@@ -160,19 +148,11 @@ SLDaemon::Options *SLDaemon::getoptions(const string file) const {

  fs.close();

  return new SLDaemon::Options(o);
  return o;
}

void SLDaemon::applyoptions(const Options *opt) {
  assert(opt);
  if (opt->server != "") options_.server = opt->server;
  if (opt->port >= 0) options_.port = opt->port;
  if (opt->login != "") options_.login = opt->login;
  if (opt->pass != "") options_.pass = opt->pass;
  if (opt->verbose >= 0) options_.verbose = opt->verbose;
  if (opt->conffile != "") options_.conffile = opt->conffile;
  if (opt->scriptdir != "") options_.scriptdir = opt->scriptdir;
  if (opt->retrydelay >= 0) options_.retrydelay = opt->retrydelay;
void SLDaemon::applyoptions(const Options &opt) {
  options_.merge(opt);
}

void SLDaemon::check_options() const {
@@ -805,6 +785,17 @@ SLDaemon::Options::Options() {
  retrydelay = -1;
}

void SLDaemon::Options::merge(const SLDaemon::Options &opt) {
  if (opt.server != "") server = opt.server;
  if (opt.port >= 0) port = opt.port;
  if (opt.login != "") login = opt.login;
  if (opt.pass != "") pass = opt.pass;
  if (opt.verbose >= 0) verbose = opt.verbose;
  if (opt.conffile != "") conffile = opt.conffile;
  if (opt.scriptdir != "") scriptdir = opt.scriptdir;
  if (opt.retrydelay >= 0) retrydelay = opt.retrydelay;
}

void SLDaemon::Options::print() {
  std::cout << "server: " << server << std::endl;
  std::cout << "port: " << port << std::endl;
+4 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ public:

  struct Options {
    Options();
    void merge(const Options &);
    void print();
    string server;
    int port; //-1 undef
@@ -42,9 +43,9 @@ public:

  inline bool verbose() const { return options_.verbose == 1; }

  Options *getoptions(int argc, char *argv[]) const;
  Options *getoptions(const string file) const;
  void applyoptions(const Options *opt);
  Options getoptions(int argc, char *argv[]) const;
  Options getoptions(const string file) const;
  void applyoptions(const Options &opt);
  void run();

  // friends functions
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ Error::Error(const Error &e)
void Error::print() const
{
  if (msg_.str() != "")
    std::cerr << msg_.str() << std::endl;
    std::cerr << msg_.str() << "." << std::endl;
}

string Error::message() const
+38 −25
Original line number Diff line number Diff line
@@ -5,35 +5,30 @@

#include "sld.hh"

string binpath;
static SLDaemon::Options get_default_options();

int main(int argc, char *argv[])
{
  SLDaemon &daemon = SLDaemon::Instance();
  SLDaemon::Options *lineopt;
  SLDaemon::Options *fileopt = NULL;
  SLDaemon::Options lineopt;

  try {
    //parse arg line options
    lineopt = daemon.getoptions(argc, argv);
    // retrieve arg line option and default options
    // order is very important
    lineopt = get_default_options();
    lineopt.merge(daemon.getoptions(argc, argv));

    assert(lineopt);
    if (lineopt->conffile != "") {
      //parse conf file options
      fileopt = daemon.getoptions(lineopt->conffile);
    }
    // Get and apply options if conffile is specified
    if (!euidaccess(lineopt.conffile.c_str(), R_OK))
      daemon.applyoptions(daemon.getoptions(lineopt.conffile));

    //apply arg line options
    daemon.applyoptions(lineopt);
  }
  catch (const Error &e) {
    e.print();
    return e.code();
  }
  try {
    //apply config file option
    if (fileopt)
      daemon.applyoptions(fileopt);

    //apply parsed option
    daemon.applyoptions(lineopt);

  // register sigchild handler
  if (signal(SIGCHLD, sigchild) == SIG_ERR) {
@@ -41,9 +36,8 @@ int main(int argc, char *argv[])
    return ERR_UNKNOWN;
  }

    //execute receive command
    daemon.run();
  }
  // run daemon
  try { daemon.run(); }
  catch (const Error &e) {
    std::cerr <<"sld: error: " << e.message() << "." << std::endl;
    return e.code();
@@ -52,6 +46,25 @@ int main(int argc, char *argv[])
  return ERR_UNKNOWN;
}

/*!
** Set default options
**
** @param opt option struc to set
**
*/
static SLDaemon::Options get_default_options() {
  char buf[128];
  SLDaemon::Options options;

  if (gethostname(buf, 128) == 0) {
    options.login = buf;
  }
  options.port = 18136;
  options.conffile = "/etc/sldrc";
  options.retrydelay = 60;
  return options;
}

string getbinpath() {
  int pid = getpid();
  char buf[PATH_MAX];