Commit 87c158ca authored by Seblu's avatar Seblu
Browse files

global improvment

command reload
parent 85966783
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
Improvments
command reload
command update

BUG:
+86 −44
Original line number Diff line number Diff line
@@ -18,6 +18,11 @@

#include "sld.hh"

const string VERSION = "1";
const int MAX_LINE_SIZE = 512;
const char *RCV_DATA = "<< ";
const char *SND_DATA = ">> ";

SLDaemon *SLDaemon::instance_ = 0;

SLDaemon::SLDaemon() : socket_fs_(0) {}
@@ -169,6 +174,7 @@ void SLDaemon::run() {
  auth();
  while (1) {
    line = recvln();
    tee(line, RCV_DATA);
    // call right handler
    try {
      if (!strcmp(line, "EXIT\n"))
@@ -274,7 +280,7 @@ char *SLDaemon::recv(size_t size) {
  return data;
}

void SLDaemon::recv(size_t size, const string filename) {
void SLDaemon::recv(size_t size, const string &filename) {
  char *data = recv(size);
  FILE *fs = fopen(filename.c_str(), "w");
  if (fs == 0)
@@ -339,8 +345,9 @@ void SLDaemon::auth() {
  return;
}

void SLDaemon::tee(const string msg) {
  if (verbose()) std::cout << msg;
void SLDaemon::tee(const string &msg, const string &local_prefix) {
  if (verbose())
    std::cout << local_prefix << msg;
  send(msg);
}

@@ -382,11 +389,21 @@ void SLDaemon::cmd_job(const char *line) {
  sscanf(line, "JOB %s\n", filename);
  string path = options_.scriptdir + "/" +  filename;

  // Check if file exist and exec flag
  struct stat buf;
  int ret = lstat(path.c_str(), &buf);
  // Check if file exist
  struct stat st;
  int ret = lstat(path.c_str(), &st);
  if (ret == ENOENT || errno == EACCES) {
    tee(strerror(errno));
    tee(strerror(errno), SND_DATA);
    return;
  }
  // check for exec flag
  if (st.st_mode | S_IXUSR != S_IXUSR) {
    tee("JOB: no exec flag.\n", SND_DATA);
    return;
  }
  // check file owner
  if (st.st_uid != getuid()) {
    tee("JOB: Bad file owner.\n", SND_DATA);
    return;
  }

@@ -403,7 +420,7 @@ void SLDaemon::cmd_job(const char *line) {
    jobs_.insert(j);
    
    // show job info
    tee((string) "EXEC " + filename + ", pid: ");
    tee((string) "JOB: " + filename + ", pid: ", SND_DATA);
    tee(pid);
    tee(", start at: ");
    tee(j.start_time);
@@ -511,11 +528,9 @@ void SLDaemon::cmd_update(const char *line) {
}

void SLDaemon::cmd_list() {
  if (verbose())
    std::cout << "LIST requested." << std::endl;
  FILE *fls = popen(string("ls -1A " + options_.scriptdir).c_str(), "r");
  if (fls == 0) {
    tee("Unable to list " + options_.scriptdir + ".");
    tee("LIST: Unable to list " + options_.scriptdir + ".\n", SND_DATA);
    return;
  }

@@ -534,20 +549,19 @@ void SLDaemon::cmd_list() {

void SLDaemon::cmd_status() {
  t_job::iterator i;

  if (verbose())
    std::cout << "STATUS requested." << std::endl;
  time_t t = time(0);

  tee("STATUS of \n", SND_DATA);
  for (i = jobs_.begin(); i != jobs_.end(); ++i) {
    send("EXEC: ");
    send(i->name);
    send(", pid: ");
    send(i->pid);
    send(", start at: ");
    send(i->start_time);
    send(", since: ");
    send(t - i->start_time);
    send(" seconds.\n");
    tee("job: ", SND_DATA);
    tee(i->name);
    tee(", pid: ");
    tee(i->pid);
    tee(", start at: ");
    tee(i->start_time);
    tee(", since: ");
    tee(t - i->start_time);
    tee(" seconds.\n");
  }
  flush();
}
@@ -562,10 +576,6 @@ void SLDaemon::cmd_kill(const char *line) {
    return;
  }

  if (verbose())
    std::cout << "KILL " << pid
	      <<" requested." << std::endl;

  for (i = jobs_.begin(); i != jobs_.end(); ++i)
    if (pid == i->pid) {
      int ret = kill(i->pid, SIGKILL);
@@ -586,9 +596,6 @@ void SLDaemon::cmd_kill(const char *line) {
void SLDaemon::cmd_killall() {
  t_job::iterator i;

  if (verbose())
    std::cout << "KILLALL requested." << std::endl;

  for (i = jobs_.begin(); i != jobs_.end(); ++i) {
    int ret = kill(i->pid, SIGKILL);
    send("kill -SIGKILL ");
@@ -606,15 +613,46 @@ void SLDaemon::cmd_killall() {
}

void SLDaemon::cmd_reload() {
  if (verbose())
    std::cout << "RELOAD requested." << std::endl;
  const char *path = binpath();

  if (path == NULL) {
    tee("RELOAD: Unable to locate sld binary.\n", SND_DATA);
    return;
  }
  
  // Get bin info
  struct stat st;
  if (lstat(path, &st)) {
    tee((string)"RELOAD: lstat: " + strerror(errno) + "\n", SND_DATA);
    return;
  }
  
  // FIXME: check re run right (exec + file owner)
  
  int pid = fork();
  if (pid == -1) {
    tee((string)"RELOAD: " + strerror(errno) + "\n", SND_DATA);
    return;
  }
  // father process
  if (pid) {
    disconnect();
    exit(0);
  }
  // son process
  else {
    execl(path, basename(path));
    tee((string) "RELOAD: " + strerror(errno) + "\n", SND_DATA);
    flush();
    exit(ERR_UNKNOWN);
  }
}

//******************************************************************************
// others functions
//******************************************************************************

string SLDaemon::md5(const string file) const {
string SLDaemon::md5(const string &file) const {
  MD5_CTX ctx;
  FILE *fs;
  size_t len;
@@ -638,6 +676,20 @@ string SLDaemon::md5(const string file) const {
  return string(digest);
}

//******************************************************************************
// Class Job functions
//******************************************************************************

SLDaemon::Job::Job(int p, string s, time_t t) : pid(p), name(s), start_time(t) {}

bool SLDaemon::Job::operator()(const SLDaemon::Job &a, const SLDaemon::Job &b) {
  return a.pid < b.pid;
}

//******************************************************************************
// Friends Class functions
//******************************************************************************

void sigchild(int) {
  int status;
  SLDaemon &d = SLDaemon::Instance();
@@ -664,13 +716,3 @@ void sigchild(int) {
    }
  }
}

//******************************************************************************
// Class Job functions
//******************************************************************************

SLDaemon::Job::Job(int p, string s, time_t t) : pid(p), name(s), start_time(t) {}

bool SLDaemon::Job::operator()(const SLDaemon::Job &a, const SLDaemon::Job &b) {
  return a.pid < b.pid;
}
+12 −5
Original line number Diff line number Diff line
@@ -35,8 +35,12 @@ enum {
// Gonstant
// -----------------------------------------------------------------------------

const string VERSION = "1";
const int MAX_LINE_SIZE = 512;
extern const string VERSION;
extern const int MAX_LINE_SIZE;
extern const char *RCV_DATA;
extern const char *SND_DATA;



void sigchild(int);

@@ -73,12 +77,15 @@ 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;
  void applyoptions(const Options *opt);
  void run();

  // friends functions

  friend void sigchild(int);

protected:
@@ -104,13 +111,13 @@ protected:
  void send(const string str, bool buf = true);
  void send(long int i);
  char *recv(size_t size);
  void recv(size_t size, const string filename);
  void recv(size_t size, const string &filename);
  char *recvln();
  void flush();
  
  // protocol functions
  void auth();
  void tee(const string msg);
  void tee(const string &msg, const string &local_prefix = "");
  void tee(long int);
  void proto_violation();
  
@@ -127,7 +134,7 @@ protected:
  void cmd_file(const char *line);

  // others functions
  string md5(const string file) const;
  string md5(const string &file) const;
  
private:
  static SLDaemon *instance_;