Commit 083366c7 authored by Seblu's avatar Seblu
Browse files

Ajout de l'option retry delay dans le fichier de conf

Ajout de l'option quiet en ligne de commande (-q)
Ajout de message d'erreur a la connexion / deconnexion
Gestion de la reconnection
parent 599e087c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,4 +3,5 @@ pass=sex
server=localhost
port=18136
scriptdir=/tmp/sld
retrydelay=5
verbose=1
+80 −41
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ void SLDaemon::usage(const char *argv0) const {
	    << "  -d scriptdir : Scripts directory." << std::endl
	    << "  -h           : Print this usage." << std::endl
	    << "  -v           : Verbose mode." << std::endl
	    << "  -q           : Quiet mode." << std::endl
	    << "  -l host      : Set login to host. (def: check hostname)" << std::endl
	    << "  -p secret    : Set pass to secret." << std::endl
	    << "  -H name      : Set server host to name." << std::endl
@@ -68,6 +69,9 @@ SLDaemon::Options *SLDaemon::getoptions(int argc, char *argv[]) const {
    else if (!strcmp(argv[i], "-v")) {
      opt.verbose = 1;
    }
    else if (!strcmp(argv[i], "-q")) {
      opt.verbose = 0;
    }
    else if (!strcmp(argv[i], "-V")) {
      std::cout << "sl daemon version : " << VERSION << std::endl;
      exit(ERR_OK);
@@ -105,6 +109,11 @@ SLDaemon::Options *SLDaemon::getoptions(int argc, char *argv[]) const {
	throw Error(ERR_USAGE, "No enough argument for option -p");
      opt.pass = string(argv[i]);
    }
    else if (!strcmp(argv[i], "-r")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -r");
      opt.retrydelay = atoi(argv[i]);
    }
    else
      throw Error(ERR_USAGE, (string) "Invalid options : " + argv[i]);
  }
@@ -143,6 +152,8 @@ SLDaemon::Options *SLDaemon::getoptions(const string file) const {
      o.scriptdir = value;
    else if (!strcasecmp(name, "verbose"))
      o.verbose = atoi(value);
    else if (!strcasecmp(name, "retrydelay"))
      o.retrydelay = atoi(value);
    else
      std::cerr << "Invalid line " << i << ": " << line;
  }
@@ -168,17 +179,17 @@ void SLDaemon::check_options() const {

  // Check validy of arguement
  if (options_.server == "")
    throw Error(ERR_BADPARAM, "No server address specified");
  if (options_.port == 0)
    throw Error(ERR_BADPARAM, "No server port specified");
  if (options_.port < 1 || options_.port > 65535)
    throw Error(ERR_BADPARAM, "Bad server port number (1 - 65535)");
    throw Error(ERR_BADPARAM, "No valid server address specified");
  if (options_.port <= 0)
    throw Error(ERR_BADPARAM, "No valid server port specified");
  if (options_.login == "")
    throw Error(ERR_BADPARAM, "No login specified");
    throw Error(ERR_BADPARAM, "No valid login specified");
  if (options_.pass == "")
    throw Error(ERR_BADPARAM, "No pass specified");
    throw Error(ERR_BADPARAM, "No valid pass specified");
  if (options_.scriptdir == "")
    throw Error(ERR_BADPARAM, "No scripts directory specified");
    throw Error(ERR_BADPARAM, "No valid scripts directory specified");
  if (options_.retrydelay < 1)
    throw Error(ERR_BADPARAM, "No valid retry delay specified");

  // Check dir script exist
  if (euidaccess(options_.scriptdir.c_str(), W_OK))
@@ -191,6 +202,8 @@ void SLDaemon::run() {
    options_.print();
  check_options();
  clean_dir(options_.scriptdir);
 net_connect:
  try {
    connect();
    auth();
    while (1) {
@@ -219,12 +232,25 @@ void SLDaemon::run() {
	  sendln("Invalid command.");
      }
      catch (const Error &e) {
	delete [] line;
	if (e.code() == ERR_NET)
	  throw;
	else {
	  sendln(e.message());
	  std::cerr << "!! " << e.message() << std::endl;
	}
      }
      delete[] line;
    }
  }
  // recupere les erreurs de reseaux.
  catch (const Error &e) {
    if (e.code() != ERR_NET)
      throw;
    sleep(options_.retrydelay);
    goto net_connect;
  }
}

//******************************************************************************
// network functions
@@ -235,7 +261,7 @@ void SLDaemon::connect() {
  struct hostent *h;

  // close existing connexion
  if (socket_fs_ == 0)
  if (socket_fs_ != 0)
    disconnect();

  // retrieve remote host info
@@ -253,10 +279,18 @@ void SLDaemon::connect() {
  daddr.sin_addr = *((struct in_addr *) h->h_addr);
  memset(daddr.sin_zero, '\0', sizeof daddr.sin_zero);

  if (verbose())
    std::cout << "Connecting to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr)
	      << ") on port " << options_.port << "..." << std::endl;

  // connect
  if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1)
    throw Error(ERR_NET, strerror(errno));

  if (verbose())
    std::cout << "Connected to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr)
	      << ") on port " << options_.port << "..." << std::endl;

  // initialize socket stream
  if ((socket_fs_ = fdopen(socket_fd_, "r+")) == 0)
    throw Error(ERR_NET, strerror(errno));
@@ -267,6 +301,8 @@ void SLDaemon::disconnect() {
    return;
  if (fclose(socket_fs_))
    throw Error(ERR_NET, strerror(errno));
  if (verbose())
    std::cout << "Connection closed." << std::endl;
  socket_fs_ = 0;
}

@@ -318,8 +354,11 @@ void SLDaemon::recv(size_t size, const string &filename) {
char *SLDaemon::recvln() {
  char *line = new char[MAX_LINE_SIZE];

  errno=0;
  if (fgets(line, MAX_LINE_SIZE, socket_fs_) == 0) {
    delete[] line;
    if(feof(socket_fs_))
      throw Error(ERR_NET, (string) "recvln: Connexion close");
    throw Error(ERR_FILE, (string) "recvln: " + strerror(errno));
  }
  if (verbose())
@@ -748,7 +787,7 @@ void SLDaemon::clean_dir(const string &dir) const {
  while ((de = readdir(ds))) {
    if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
      continue;
    if (remove(de->d_name)) {
    if (remove(string(dir + "/" + de->d_name).c_str())) {
      closedir(ds);
      throw Error(ERR_FILE,(string) "clear_dir: remove("+ de->d_name + "): " + strerror(errno));
    }