Commit 153a1634 authored by Seblu's avatar Seblu
Browse files

Ajout de l'option -c (closefd) qui ferme stdin,stdout,stderr en passage en daemon

Ajout de l'option -F qui write le pid dans un fichier au demarrage
parent 32c1601e
Loading
Loading
Loading
Loading
+38 −17
Original line number Diff line number Diff line
@@ -8,13 +8,14 @@ Options::Options() {
}

void Options::usage(const char *argv0) const {
  std::cerr << "usage: "
  std::cerr
    << "usage: "
    << argv0
    << " [-f conffile] [-d scriptdir] [-h] [-v] [-l login]"
    << "[-p pass] [-H host] [-P port] [-r] [-V]"
    << std::endl
	    << "  -f conffile  : read and load conf file (def: /etc/sldrc)." << std::endl
	    << "  -d scriptdir : Scripts directory." << std::endl
    << "  -f file   : read and load conf file (def: /etc/sldrc)." << std::endl
    << "  -d dir    : Scripts directory." << std::endl
    << "  -h        : Print this usage." << std::endl
    << "  -v        : Verbose mode." << std::endl
    << "  -q        : Quiet mode." << std::endl
@@ -23,7 +24,9 @@ void Options::usage(const char *argv0) const {
    << "  -p secret : Set pass to secret." << std::endl
    << "  -H name   : Set server host to name." << std::endl
    << "  -P number : Set server port to number (def: 18136)." << std::endl
	    << "  -r val       : Set retry delay to val in seconds (def: 60)." << std::endl
    << "  -r number : Set retry delay to val in seconds (def: 60)." << std::endl
    << "  -F file   : Set file where pid is written." << std::endl
    << "  -c        : Close stdin,stdout,stderr in daemon mode." << std::endl
    << "  -V        : Print version and exit." << std::endl;
}

@@ -37,6 +40,8 @@ void Options::merge(const Options &opt) {
  if (opt.scriptdir != "") scriptdir = opt.scriptdir;
  if (opt.retrydelay >= 0) retrydelay = opt.retrydelay;
  if (opt.daemon >= 0) daemon = opt.daemon;
  if (opt.closefd >= 0) closefd = opt.closefd;
  if (opt.pidfile != "") pidfile = opt.pidfile;
}

Options &Options::getoptions(int argc, char *argv[]) {
@@ -96,6 +101,16 @@ Options &Options::getoptions(int argc, char *argv[]) {
	throw Error(ERR_USAGE, "No enough argument for option -r");
      this->retrydelay = atoi(argv[i]);
    }
    else if (!strcmp(argv[i], "-c")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -c");
      this->closefd = atoi(argv[i]);
    }
    else if (!strcmp(argv[i], "-F")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -F");
      this->pidfile = argv[i];
    }
    else
      throw Error(ERR_USAGE, (string) "Invalid options : " + argv[i]);
  }
@@ -136,6 +151,10 @@ Options Options::getoptions(const string file) {
      this->retrydelay = atoi(value);
    else if (!strcasecmp(name, "daemon"))
      this->daemon = atoi(value);
    else if (!strcasecmp(name, "closefd"))
      this->closefd = atoi(value);
    else if (!strcasecmp(name, "pidfile"))
      this->pidfile = value;
    else
      std::cerr << "Invalid line " << i << ": " << line << std::endl;
  }
@@ -154,6 +173,8 @@ ostream &operator<< (ostream &s, const Options &o) {
	   << "configuration file: " << o.conffile << std::endl
	   << "retry delay: " << o.retrydelay << std::endl
	   << "daemon mode: " << o.daemon << std::endl
	   << "pid file: " << o.pidfile << std::endl
	   << "close fd: " << o.closefd << std::endl
	   << "verbose level: " << o.verbose << std::endl;
}
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ struct Options {
  string conffile;
  int retrydelay; //-1 undef
  int daemon;
  int closefd; //-1 undel
  string pidfile;
};

#endif
+12 −1
Original line number Diff line number Diff line
@@ -49,8 +49,18 @@ int main(int argc, char *argv[])
    return ERR_UNKNOWN;
  }

  // Switch in daemon mode
  if (daemon.options.daemon == 1)
    ::daemon(0, 1);
    ::daemon(0, !(daemon.options.closefd == 1));

  // Write pid in a file
  if (daemon.options.pidfile != "") {
    FILE *fs = fopen(daemon.options.pidfile.c_str(), "w");
    if (fs) {
      fprintf(fs, "%d", getpid());
      fclose(fs);
    } else perror("sld: fopen");
  }

  // run daemon
  try { daemon.run(); }
@@ -78,6 +88,7 @@ static Options get_default_options() {
  options.conffile = "/etc/sldrc";
  options.retrydelay = 60;
  options.daemon = 0;
  options.closefd = 1;
  return options;
}