Skip to content
daemon.cc 16.5 KiB
Newer Older
Seblu's avatar
Seblu committed
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
Seblu's avatar
Seblu committed
#include <stdio.h>
Seblu's avatar
Seblu committed
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
Seblu's avatar
Seblu committed
#include <sys/stat.h>
#include <dirent.h>
Seblu's avatar
Seblu committed
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
Seblu's avatar
Seblu committed
#include <openssl/md5.h>
Seblu's avatar
Seblu committed

#include "sld.hh"

SLDaemon *SLDaemon::instance_ = 0;

SLDaemon::SLDaemon() : socket_fs_(0) {}

SLDaemon &SLDaemon::Instance() {
  if (instance_ == 0)
    instance_ = new SLDaemon();
  return *instance_;
}
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
void SLDaemon::check_options() const {

  // Check validy of arguement
Seblu's avatar
Seblu committed
  if (options.server == "")
    throw Error(ERR_BADPARAM, "No valid server address specified");
Seblu's avatar
Seblu committed
  if (options.port <= 0)
    throw Error(ERR_BADPARAM, "No valid server port specified");
Seblu's avatar
Seblu committed
  if (options.login == "")
    throw Error(ERR_BADPARAM, "No valid login specified");
Seblu's avatar
Seblu committed
  if (options.pass == "")
    throw Error(ERR_BADPARAM, "No valid pass specified");
Seblu's avatar
Seblu committed
  if (options.scriptdir == "")
    throw Error(ERR_BADPARAM, "No valid scripts directory specified");
Seblu's avatar
Seblu committed
  if (options.retrydelay < 1)
    throw Error(ERR_BADPARAM, "No valid retry delay specified");
Seblu's avatar
Seblu committed

  // Check dir script exist
Seblu's avatar
Seblu committed
  if (euidaccess(options.scriptdir.c_str(), W_OK))
    throw Error(ERR_BADPARAM, "Unable to write into script directory");
Seblu's avatar
Seblu committed
}
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
void SLDaemon::run() {
  char *line;

Seblu's avatar
Seblu committed
  if (verbose())
Seblu's avatar
Seblu committed
    std::cout << options;
Seblu's avatar
Seblu committed
  check_options();
Seblu's avatar
Seblu committed
  clean_dir(options.scriptdir);
 net_connect:
  try {
    connect();
    auth();
    while (1) {
      line = recvln();
      // call right handler
      try {
	if (!strcmp(line, "EXIT\n"))
	  cmd_exit();
	else if (!strcmp(line, "VERSION\n"))
	  cmd_version();
	else if (!strcmp(line, "STATUS\n"))
	  cmd_status();
	else if (!strcmp(line, "LIST\n"))
	  cmd_list();
	else if (!strcmp(line, "KILLALL\n"))
	  cmd_killall();
	else if (!strcmp(line, "KILL\n"))
	  cmd_kill(line);
	else if (!strncmp(line, "EXEC ", 4))
	  cmd_exec(line);
	else if (!strncmp(line, "FILE ", 5))
	  cmd_file(line);
	else if (!strcmp(line, "UPDATE\n"))
	  cmd_update();
	else
	  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;
Seblu's avatar
Seblu committed
    }
  }
  // recupere les erreurs de reseaux.
  catch (const Error &e) {
    if (e.code() != ERR_NET)
      throw;
Seblu's avatar
Seblu committed
    sleep(options.retrydelay);
    goto net_connect;
Seblu's avatar
Seblu committed
  }
}

//******************************************************************************
// network functions
//******************************************************************************
Seblu's avatar
Seblu committed

void SLDaemon::connect() {
  struct sockaddr_in daddr;
  struct hostent *h;

Seblu's avatar
Seblu committed
  // close existing connexion
  if (socket_fs_ != 0)
Seblu's avatar
Seblu committed
    disconnect();

  // retrieve remote host info
Seblu's avatar
Seblu committed
  h = gethostbyname(options.server.c_str());
  if (h == 0)
Seblu's avatar
Seblu committed
    throw Error(ERR_NET, hstrerror(h_errno));
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  // create socket
  socket_fd_ = socket(PF_INET, SOCK_STREAM, 0);
  if (socket_fd_ == -1)
    throw Error(ERR_NET, strerror(errno));
Seblu's avatar
Seblu committed

  daddr.sin_family = AF_INET;
Seblu's avatar
Seblu committed
  daddr.sin_port = htons(options.port);
Seblu's avatar
Seblu committed
  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)
Seblu's avatar
Seblu committed
	      << ") on port " << options.port << "..." << std::endl;
Seblu's avatar
Seblu committed
  // connect
  if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1)
    throw Error(ERR_NET, strerror(errno));
Seblu's avatar
Seblu committed

  if (verbose())
    std::cout << "Connected to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr)
Seblu's avatar
Seblu committed
	      << ") on port " << options.port << "..." << std::endl;
Seblu's avatar
Seblu committed
  // initialize socket stream
  if ((socket_fs_ = fdopen(socket_fd_, "r+")) == 0)
Seblu's avatar
Seblu committed
    throw Error(ERR_NET, strerror(errno));
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
void SLDaemon::disconnect() {
  if (socket_fs_ == 0)
Seblu's avatar
Seblu committed
    return;
Seblu's avatar
Seblu committed
  if (fclose(socket_fs_))
    throw Error(ERR_NET, strerror(errno));
  if (verbose())
    std::cout << "Connection closed." << std::endl;
  socket_fs_ = 0;
}

void SLDaemon::send(long int i) {
  fprintf(socket_fs_, "%li", i);
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
void SLDaemon::send(const string str, bool buf) {
  send(str.c_str(), str.length(), buf);
}

void SLDaemon::send(const char *data, size_t len, bool buf) {
Seblu's avatar
Seblu committed
  if (len == 0)
    return;
Seblu's avatar
Seblu committed
  if (fwrite(data, 1, len, socket_fs_) != len)
    throw Error(ERR_NET, strerror(errno));
  if (!buf && fflush(socket_fs_))
    throw Error(ERR_NET, strerror(errno));
Seblu's avatar
Seblu committed
}

void SLDaemon::sendln(const string &s) {
  if (!fprintf(socket_fs_, "%s\n", s.c_str()))
    throw Error(ERR_NET, strerror(errno));
  if (verbose())
    std::cout << SND_DATA << s << std::endl;
}

Seblu's avatar
Seblu committed
char *SLDaemon::recv(size_t size) {
  char *data = new char[size];

  if (fread(data, 1, size, socket_fs_) != size) {
    delete[] data;
Seblu's avatar
Seblu committed
    throw Error(ERR_NET, strerror(errno));
Seblu's avatar
Seblu committed
  }
Seblu's avatar
Seblu committed
  return data;
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
void SLDaemon::recv(size_t size, const string &filename) {
Seblu's avatar
Seblu committed
  char *data = recv(size);
  FILE *fs = fopen(filename.c_str(), "w");
  if (fs == 0)
    throw Error(ERR_FILE, (string) "recv: fopen: " + strerror(errno));
Seblu's avatar
Seblu committed
  if (fwrite(data, 1, size, fs) != size)
    throw Error(ERR_FILE, (string) "recv: fwrite: " + strerror(errno));
Seblu's avatar
Seblu committed
  if (fclose(fs))
    throw Error(ERR_FILE, (string) "recv: fclose: " + strerror(errno));
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
char *SLDaemon::recvln() {
  char *line = new char[MAX_LINE_SIZE];

  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));
Seblu's avatar
Seblu committed
  }
  if (verbose())
    std::cout << RCV_DATA << line;
Seblu's avatar
Seblu committed
  return line;
}

void SLDaemon::flush() {
  if (fflush(socket_fs_))
    throw Error(ERR_NET, strerror(errno));
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
//******************************************************************************
// protocol functions
//******************************************************************************

Seblu's avatar
Seblu committed
void SLDaemon::auth() {
  unsigned char md[SHA_DIGEST_LENGTH];
  char buf[MAX_LINE_SIZE];
  char *buf2 = "";
Seblu's avatar
Seblu committed
  BIO *bmem, *b64;
  BUF_MEM *bptr;

Seblu's avatar
Seblu committed
  SHA1((const unsigned char *) options.pass.c_str(), options.pass.length(), md);
Seblu's avatar
Seblu committed

  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_write(b64, md, SHA_DIGEST_LENGTH);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);

  if (bptr->length > 0) {
    buf2 = new char[bptr->length];
    memcpy(buf2, bptr->data, bptr->length-1);
    buf2[bptr->length-1] = 0;
  }
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  snprintf(buf, MAX_LINE_SIZE, "HOST %s", options.login.c_str());
  sendln(buf);
Seblu's avatar
Seblu committed

  snprintf(buf, MAX_LINE_SIZE, "PASS %s", buf2);
  sendln(buf);
Seblu's avatar
Seblu committed

  if (bptr->length > 0)
    delete[] buf2;
Seblu's avatar
Seblu committed

  BIO_free_all(b64);
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  return;
}

Seblu's avatar
Seblu committed
//******************************************************************************
// command functions
//******************************************************************************

Seblu's avatar
Seblu committed
void SLDaemon::cmd_exit() {
  sendln("Bye.");
Seblu's avatar
Seblu committed
  disconnect();
Seblu's avatar
Seblu committed
  exit(ERR_OK);
}

void SLDaemon::cmd_version() {
  sendln(VERSION);
Seblu's avatar
Seblu committed
}

void SLDaemon::cmd_exec(const char *line) {
Seblu's avatar
Seblu committed
  assert(line);

  // build path
Seblu's avatar
Seblu committed
  char buf[MAX_LINE_SIZE];
  if (sscanf(line, "EXEC %512s\n", buf) != 1) {  //FIXME: Bad static magic number
    sendln("EXEC: Syntax error.");
    return;
  }
Seblu's avatar
Seblu committed
  string path = options.scriptdir + "/" +  buf;
  // Check transversal path attack
  if (strchr(buf, '/')) {
    sendln("EXEC: Invalid character in job name.");
    return;
  }

Seblu's avatar
Seblu committed
  // Check if file exist
  struct stat st;
  int ret = lstat(path.c_str(), &st);
  if (ret) {
    char msg[MAX_LINE_SIZE];
    snprintf(msg, MAX_LINE_SIZE, "EXEC: %s: %s.", buf, strerror(errno));
    sendln(msg);
Seblu's avatar
Seblu committed
    return;
  }
  // check for exec flag
Seblu's avatar
Seblu committed
  if (st.st_mode & S_IXUSR != S_IXUSR) {
    sendln("EXEC: no exec flag.");
Seblu's avatar
Seblu committed
    return;
  }
  // check file owner
  if (st.st_uid != getuid()) {
    sendln("EXEC: Bad file owner.");
Seblu's avatar
Seblu committed
    return;
  }

  // flush before fork
  flush();

  //Create new process
Seblu's avatar
Seblu committed
  pid_t pid = fork();
Seblu's avatar
Seblu committed
  if (pid == -1) {
    sendln((string) "EXEC: Unable to fork: " + strerror(errno) + ".");
Seblu's avatar
Seblu committed
    return;
  }
Seblu's avatar
Seblu committed

  // Father job
Seblu's avatar
Seblu committed
  if (pid > 0) {
    // reg job
Seblu's avatar
Seblu committed
    SLDaemon::Job j(pid, buf, time(0));
    jobs_.insert(j);
Seblu's avatar
Seblu committed
    // send job info
    snprintf(buf, MAX_LINE_SIZE,
	     "EXEC: %s, pid: %d, start at: %ld.",
Seblu's avatar
Seblu committed
	     j.name.c_str(), j.pid, j.start_time);
    sendln(buf);

    // allow sun to start
    if (kill(pid, SIGCONT)) {
      perror("sld: cmd_exec");
      jobs_.erase(j);
    }
Seblu's avatar
Seblu committed
  }
  // Son job
Seblu's avatar
Seblu committed
  else if (pid == 0) {
    // Wait father registration stuff before doing something else
    // so we suspend execution
    if (kill(getpid(), SIGSTOP)) {
      perror("sld: cmd_exec");
      return;
    }

    // try to run job
Seblu's avatar
Seblu committed
    if (dup2(socket_fd_, STDOUT_FILENO) == -1) {
Seblu's avatar
Seblu committed
      perror(">> dup2");
Seblu's avatar
Seblu committed
      exit(ERR_UNKNOWN);
Seblu's avatar
Seblu committed
    }
Seblu's avatar
Seblu committed
    if (dup2(socket_fd_, STDERR_FILENO) == -1) {
Seblu's avatar
Seblu committed
      perror(">> dup2");
Seblu's avatar
Seblu committed
      exit(ERR_UNKNOWN);
Seblu's avatar
Seblu committed
    }
    execl(path.c_str(), basename(path.c_str()), 0);
Seblu's avatar
Seblu committed
    perror(string("sld: execl " + path).c_str());
    exit(ERR_UNKNOWN);
Seblu's avatar
Seblu committed
  }
}

void SLDaemon::cmd_file(const char *line) {
Seblu's avatar
Seblu committed
  char *buf;
  int ret;

Seblu's avatar
Seblu committed
  assert(line);
Seblu's avatar
Seblu committed

  // get filename
Seblu's avatar
Seblu committed
  char filename[MAX_LINE_SIZE];
  if (sscanf(line, "FILE %512s\n", filename) != 1) { //FIXME: bad magic size
    sendln("FILE: Syntax error.");
    return;
  }

  // Check transversal path attack
  if (strchr(filename, '/')) {
    sendln("FILE: Invalid character in filename.");
Seblu's avatar
Seblu committed
    return;
  }
Seblu's avatar
Seblu committed
  string target = options.scriptdir + "/" + filename;
Seblu's avatar
Seblu committed

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

  //get data
  try {
    recv(size, target);
  }
  catch (const Error &err) {
    if (err.code() == ERR_FILE) {
      sendln("FILE: Data transfer error.");
Seblu's avatar
Seblu committed
      return;
    }
    throw;
  }

  // check MD5
  if (SLDaemon::md5(target) != string(md5)) {
    sendln("FILE: file " + target + ": Invalid MD5.");
Seblu's avatar
Seblu committed
    unlink(target.c_str());
    return;
Seblu's avatar
Seblu committed
 }
Seblu's avatar
Seblu committed

  // proceed chown
  if (chown(target.c_str(), getuid(), getgid())) {
    sendln("FILE: chown of " + target + ": Unable to chown.");
Seblu's avatar
Seblu committed
    unlink(target.c_str());
    return;
  }

  // proceed chmod
  if (chmod(target.c_str(), S_IRUSR | S_IWUSR | S_IXUSR)) {
    sendln("FILE: chmod of " + target + ": Unable to chmod.");
Seblu's avatar
Seblu committed
    unlink(target.c_str());
    return;
  }

  sendln("FILE: Transfer of " + target + ": OK.");
Seblu's avatar
Seblu committed
}
Seblu's avatar
Seblu committed
void SLDaemon::cmd_update() {
  char *buf;
  int ret;

  // get filename
  const string &target = getbinpath();
Seblu's avatar
Seblu committed

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

  // find tempory filename
  char tempsld[PATH_MAX] = "/tmp/sldXXXXXX";
  int tempsld_fd = mkstemp(tempsld);

  if (tempsld_fd == 0) {
    sendln((string) "UPDATE: mkstemp: " + strerror(errno) + ".");
    return;
  }
  close(tempsld_fd);

Seblu's avatar
Seblu committed
  //get data
  try {
    recv(size, tempsld);
Seblu's avatar
Seblu committed
  }
  catch (const Error &err) {
    if (err.code() == ERR_FILE) {
      sendln("UPDATE: " + err.message() + ".");
      unlink(tempsld);
Seblu's avatar
Seblu committed
      return;
    }
    throw;
  }

  // check MD5
  if (SLDaemon::md5(tempsld) != string(md5)) {
Seblu's avatar
Seblu committed
    sendln((string) "UPDATE: file " + tempsld + ": Invalid MD5.");
    unlink(tempsld);
    return;
 }

  // copy file to its right destination
  unlink(target.c_str());
  cp(tempsld, target.c_str());


  // check MD5
  if (SLDaemon::md5(target.c_str()) != string(md5)) {
    sendln("UPDATE: file " + target + ": Invalid MD5.");
    unlink(tempsld);
Seblu's avatar
Seblu committed
    return;
 }

Seblu's avatar
Seblu committed
  // proceed chown
  if (chown(target.c_str(), getuid(), getgid())) {
    sendln("FILE: chown of " + target + ": Unable to chown.");
Seblu's avatar
Seblu committed
    unlink(target.c_str());
    return;
  }

  // proceed chmod
  if (chmod(target.c_str(), S_IRUSR | S_IWUSR | S_IXUSR)) {
    sendln("FILE: chmod of " + target + ": Unable to chmod.");
Seblu's avatar
Seblu committed
    unlink(target.c_str());
    return;
  }

  sendln("UPDATE: Transfer of " + target + ": OK.");
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
void SLDaemon::cmd_list() {
Seblu's avatar
Seblu committed
  FILE *fls = popen(string("ls -1A " + options.scriptdir).c_str(), "r");
  if (fls == 0) {
Seblu's avatar
Seblu committed
    sendln("LIST: Unable to list " + options.scriptdir + ".");
Seblu's avatar
Seblu committed
    return;
  }

  char buf[255];
  size_t len;
  try {
    while ((len = fread(buf, 1, 255, fls)) > 0)
      send(buf, len);
    sendln("LIST: data send.");
Seblu's avatar
Seblu committed
  }
  catch (...) {
    pclose(fls);
    throw;
  }
}

void SLDaemon::cmd_status() {
  t_job::iterator i;
  time_t t = time(0);
Seblu's avatar
Seblu committed
  char buf[MAX_LINE_SIZE];
Seblu's avatar
Seblu committed

  if (jobs_.size() == 0)
    return;

  sendln("STATUS of");
  for (i = jobs_.begin(); i != jobs_.end(); ++i) {
Seblu's avatar
Seblu committed
    snprintf(buf, MAX_LINE_SIZE,
	     " job: %s, pid: %d, start at: %ld, since: %ld seconds.",
Seblu's avatar
Seblu committed
	     i->name.c_str(), i->pid, i->start_time,t - i->start_time);
    sendln(buf);
  }
}

void SLDaemon::cmd_kill(const char *line) {
  t_job::iterator i;
Seblu's avatar
Seblu committed
  char buf[MAX_LINE_SIZE];

  // retrieve pid
  int pid;
  if (sscanf(line, "KILL %i\n", &pid) != 1) {
    sendln("KILL: Syntax error.");
    return;
  }

  for (i = jobs_.begin(); i != jobs_.end(); ++i)
    if (pid == i->pid) {
      int ret = kill(i->pid, SIGKILL);
      snprintf(buf, MAX_LINE_SIZE, "KILL: kill -SIGKILL %d (%s), return %d (%s).",
Seblu's avatar
Seblu committed
	       i->pid, i->name.c_str(), ret, ((ret == -1) ? strerror(errno) : "OK" ));
      sendln(buf);
    }
}

void SLDaemon::cmd_killall() {
  t_job::iterator i;
Seblu's avatar
Seblu committed
  char buf[MAX_LINE_SIZE];

  for (i = jobs_.begin(); i != jobs_.end(); ++i) {
    int ret = kill(i->pid, SIGKILL);
    snprintf(buf, MAX_LINE_SIZE, "KILL: kill -SIGKILL %d (%s), return %d (%s).",
Seblu's avatar
Seblu committed
	     i->pid, i->name.c_str(), ret, ((ret == -1) ? strerror(errno) : "OK" ));
    sendln(buf);
Seblu's avatar
Seblu committed
  }
Seblu's avatar
Seblu committed
}

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

Seblu's avatar
Seblu committed
string SLDaemon::md5(const string &file) const {
Seblu's avatar
Seblu committed
  MD5_CTX ctx;
  FILE *fs;
  size_t len;
  char buf[512];
  char md[MD5_DIGEST_LENGTH];
  char digest[MD5_DIGEST_LENGTH * 2 + 1];

  if (!MD5_Init(&ctx))
    return "";
  if ((fs = fopen(file.c_str(), "r")) == 0)
Seblu's avatar
Seblu committed
    return "";
  while ((len = fread(buf, 1, 512, fs)) > 0)
    if (!MD5_Update(&ctx, buf, len))
      break;
  if (!MD5_Final((unsigned char*)md, &ctx))
    return "";
  for(len = 0; len < MD5_DIGEST_LENGTH; ++len) {
    sprintf(digest + (len * 2), "%02x", (unsigned char) md[len]);
  }
  digest[MD5_DIGEST_LENGTH * 2] = 0;
  return string(digest);
Seblu's avatar
Seblu committed
}
void SLDaemon::clean_dir(const string &dir) const {
  DIR *ds;
  struct dirent *de;

  ds = opendir(dir.c_str());
  if (ds == 0)
    throw Error(ERR_FILE, "clear_dir: Unable to read dir script");

  while ((de = readdir(ds))) {
    if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
      continue;
    if (remove(string(dir + "/" + de->d_name).c_str())) {
      closedir(ds);
      throw Error(ERR_FILE,(string) "clear_dir: remove("+ de->d_name + "): " + strerror(errno));
    }
  }
  closedir(ds);
}

Seblu's avatar
Seblu committed
//******************************************************************************
// Class Job functions
//******************************************************************************

Seblu's avatar
Seblu committed
SLDaemon::Job::Job(int p, const string &s, time_t t) : pid(p), name(s), start_time(t) {}
Seblu's avatar
Seblu committed

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();
  typedef SLDaemon::t_job t_job;
  t_job::iterator i;
  t_job &jobs_ = d.jobs_;
  pid_t pid;
  // Retrieve a pid
  while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
    time_t t = time(0);
    for (i = jobs_.begin(); i != jobs_.end(); ++i) {
      if (i->pid == pid) {
	char buf[MAX_LINE_SIZE];
	snprintf(buf, MAX_LINE_SIZE,
		 "JOB: %s, pid: %d, end at: %ld, since: %ld, return: %d\n",
		 i->name.c_str(), i->pid,  t, t - i->start_time,
		 WEXITSTATUS(status));
	write(d.socket_fd_, buf, strlen(buf));
Seblu's avatar
Seblu committed
	if (d.verbose())
	  std::cout << SND_DATA << buf;
	jobs_.erase(i);
	break;
      }
    }
  }
}