Commit 64d5ba12 authored by Seblu's avatar Seblu
Browse files

cmd_update

cmd_reload
parent 4812fb2e
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
Improvments
command update
IMPROVMENTS:
remove static magic MAX_LINE_SIZE in sscanf

BUG:
SLDaemon::binpath() ne recupere pas le bon nom pour le binaire en cours d'execution
RELOAD return bad address during execl
+79 −18
Original line number Diff line number Diff line
@@ -190,8 +190,8 @@ void SLDaemon::run() {
	cmd_job(line);
      else if (!strncmp(line, "FILE ", 5))
	cmd_file(line);
      else if (!strncmp(line, "UPDATE ", 6))
	cmd_update(line);
      else if (!strcmp(line, "UPDATE"))
	cmd_update();
      else
	proto_violation();
    }
@@ -353,13 +353,12 @@ void SLDaemon::answer(long int i, bool prefix) {

void SLDaemon::warn(const string &msg, const string &prefix) {
  if (verbose())
    std::cout << SND_DATA << prefix << msg;
  send(msg);
    std::cout << prefix << msg;
}

void SLDaemon::warn(long int i, const string &prefix) {
  if (verbose()) std::cout << prefix << i;
  send(i);
  if (verbose())
    std::cout << prefix << i;
}

void SLDaemon::proto_violation() {
@@ -387,7 +386,7 @@ void SLDaemon::cmd_job(const char *line) {

  // build path
  char buf[MAX_LINE_SIZE];
  sscanf(line, "JOB %512s\n", buf);  //FIXME: Bad static magic number
  sscanf(line, "EXEC %512s\n", buf);  //FIXME: Bad static magic number
  string path = options_.scriptdir + "/" +  buf;

  // Check if file exist
@@ -398,7 +397,7 @@ void SLDaemon::cmd_job(const char *line) {
    return;
  }
  // check for exec flag
  if (st.st_mode | S_IXUSR != S_IXUSR) { //FIXME: mask error !
  if (st.st_mode & S_IXUSR != S_IXUSR) {
    answer("JOB: no exec flag.\n");
    return;
  }
@@ -439,8 +438,7 @@ void SLDaemon::cmd_job(const char *line) {
      exit(ERR_UNKNOWN);
    }
    execl(path.c_str(), basename(path.c_str()), 0);
    printf("\"%s\"\n", path.c_str());
    perror("sld: execl");
    perror(string("sld: execl " + path).c_str());
    exit(ERR_UNKNOWN);
  }
}
@@ -517,8 +515,69 @@ void SLDaemon::cmd_file(const char *line) {
  answer("FILE: Transfer of " + target + ": OK.\n"); 
}
 
void SLDaemon::cmd_update(const char *line) {
  assert(line);
void SLDaemon::cmd_update() {
  char *buf;
  int ret;

  // get filename
  const string &target = binpath;

  //get size
  int size;  
  buf = recvln();
  warn(buf);
  ret = sscanf(buf, "SIZE %i\n", &size);
  delete buf;
  if (ret != 1) {
    answer("UPDATE: Invalid size parameter.\n"); 
    return;
  }
  
  //get md5
  char md5[MAX_LINE_SIZE]; 
  buf = recvln();
  warn(buf);
  ret = sscanf(buf, "MD5 %512s\n", md5); //FIXME: bad magic size
  delete buf;
  if (ret != 1) {
    answer("UPDATE: Invalid md5 parameter.\n");
    return;
  }

  //get data
  try {
    recv(size, target);
  }
  catch (const Error &err) {
    if (err.code() == ERR_FILE) {
      answer("UPDATE: Data transfer error.\n");
      return;
    }
    throw;
  }

  // check MD5
  if (SLDaemon::md5(target) != string(md5)) {
    answer("UPDATE: file " + target + ": Invalid MD5.\n");
    unlink(target.c_str());
    return;
 }

  // proceed chown
  if (chown(target.c_str(), getuid(), getgid())) {
    answer("FILE: chown of " + target + ": Unable to chown.\n");
    unlink(target.c_str());
    return;
  }

  // proceed chmod
  if (chmod(target.c_str(), S_IRUSR | S_IWUSR | S_IXUSR)) {
    answer("FILE: chmod of " + target + ": Unable to chmod.\n");
    unlink(target.c_str());
    return;
  }

  answer("UPDATE: Transfer of " + target + ": OK.\n"); 
}

void SLDaemon::cmd_list() {
@@ -592,25 +651,27 @@ void SLDaemon::cmd_killall() {
}

void SLDaemon::cmd_reload() {
  const char *path = binpath();
  const string &path = binpath;

  if (path == NULL) {
  if (path == "") {
    answer("RELOAD: Unable to locate sld binary.\n");
    return;
  }
  
  // Get bin info
  struct stat st;
  if (lstat(path, &st)) {
  if (lstat(path.c_str(), &st)) {
    answer((string)"RELOAD: lstat: " + strerror(errno) + ".\n");
    return;
  }
  
  // FIXME: check re run right (exec + file owner)

  answer((string) "RELOAD: Trying to reload `" + path.c_str() + "'.\n");
  
  int pid = fork();
  if (pid == -1) {
    answer((string)"RELOAD: " + strerror(errno) + ".\n");
    answer((string)"RELOAD: fork: " + strerror(errno) + ".\n");
    return;
  }
  // father process
@@ -620,8 +681,8 @@ void SLDaemon::cmd_reload() {
  }
  // son process
  else {
    execl(path, basename(path));
    answer((string) "RELOAD: " + strerror(errno) + ".\n");
    execl(path.c_str(), path.c_str());
    answer((string) "RELOAD: execl: " + strerror(errno) + ".\n");
    flush();
    exit(ERR_UNKNOWN);
  }
+17 −0
Original line number Diff line number Diff line
@@ -9,15 +9,21 @@
//

#include <signal.h>
#include <unistd.h>
#include <string>

#include "sld.hh"

string binpath;

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

  binpath = getbinpath(*argv);

  try {
    //parse arg line options
    lineopt = daemon.getoptions(argc, argv);
@@ -55,3 +61,14 @@ int main(int argc, char *argv[])
  }
  return ERR_UNKNOWN;
}

string getbinpath(const char *argv0) {
  // check if is absolute pathname
  if (*argv0 == '/')
    return string(argv0);

  //contatence current dir with argv0
  char buf[PATH_MAX];
  getcwd(buf, PATH_MAX);
  return (string) buf + "/" + argv0;
}
+4 −2
Original line number Diff line number Diff line
@@ -40,6 +40,9 @@ static const int MAX_LINE_SIZE = 512;
static const string RCV_DATA = "<< ";
static const string SND_DATA = ">> ";

extern string binpath;
string getbinpath(const char *argv0);

void sigchild(int);

// -----------------------------------------------------------------------------
@@ -75,7 +78,6 @@ public:
  };

  inline bool verbose() const { return options_.verbose == 1; }
  inline const char *binpath() const { return getenv("_"); }

  Options *getoptions(int argc, char *argv[]) const;
  Options *getoptions(const string file) const;
@@ -129,8 +131,8 @@ protected:
  void cmd_status();
  void cmd_killall();
  void cmd_kill(const char *line);
  void cmd_update(const char *line);
  void cmd_job(const char *line);
  void cmd_update();
  void cmd_file(const char *line);

  // others functions