Skip to content
sld.cc 1.87 KiB
Newer Older
#include <signal.h>
#include <sys/types.h>
Seblu's avatar
Seblu committed
#include <unistd.h>
#include <string>
Seblu's avatar
Seblu committed
#include "sld.hh"

Seblu's avatar
Seblu committed
string binpath;

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

Seblu's avatar
Seblu committed
  try {
    //parse arg line options
    lineopt = daemon.getoptions(argc, argv);
Seblu's avatar
Seblu committed
    assert(lineopt);
    if (lineopt->conffile != "") {
      //parse conf file options
      fileopt = daemon.getoptions(lineopt->conffile);
    }
  }
  catch (const Error &e) {
    e.print();
    return e.code();
  }
  try {
    //apply config file option
    if (fileopt)
      daemon.applyoptions(fileopt);
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    //apply parsed option
    daemon.applyoptions(lineopt);
Seblu's avatar
Seblu committed

    // register sigchild handler
    if (signal(SIGCHLD, sigchild) == SIG_ERR) {
      perror("sld: error");
      return ERR_UNKNOWN;
    }

Seblu's avatar
Seblu committed
    //execute receive command
    daemon.run();
  }
  catch (const Error &e) {
    std::cerr <<"sld: error: " << e.message() << "." << std::endl;
Seblu's avatar
Seblu committed
    return e.code();
  }
Seblu's avatar
Seblu committed
  return ERR_UNKNOWN;
Seblu's avatar
Seblu committed
}
Seblu's avatar
Seblu committed

string getbinpath() {
  int pid = getpid();
Seblu's avatar
Seblu committed
  char buf[PATH_MAX];
  char buf2[PATH_MAX] = "";

  snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
  if (readlink(buf, buf2, PATH_MAX) == 0) {
    perror("sld: getbinpath: readlink: ");
    return (string) "";
  }
  return (string) buf2;
}

bool cp(const char *src, const char *dst) {
  assert(src);
  assert(dst);

  //open source file
  FILE *fsrc = fopen(src, "r");
  if (!fsrc)
    return false;

  //open destination file
  FILE *fdst = fopen(dst, "w");
  if (!fdst) {
    fclose(fsrc);
    return false;
  }

  //do copy
  char buf[512];
  size_t nread;
  size_t nwrite;
  while ((nread = fread(buf, 1, 512, fsrc)) > 0) {
    nwrite = fwrite(buf, 1, nread, fdst);
    if (nwrite != nread) {
      fclose(fsrc);
      fclose(fdst);
      return false;
    }
  }
  fclose(fsrc);
  fclose(fdst);
  return true;
Seblu's avatar
Seblu committed
}