Skip to content
sld.cc 2.21 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"

static SLDaemon::Options get_default_options();
Seblu's avatar
Seblu committed

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

Seblu's avatar
Seblu committed
  try {
    // retrieve arg line option and default options
    // order is very important
    lineopt = get_default_options();
    lineopt.merge(daemon.getoptions(argc, argv));
    // Get and apply options if conffile is specified
    if (!euidaccess(lineopt.conffile.c_str(), R_OK))
      daemon.applyoptions(daemon.getoptions(lineopt.conffile));

    //apply arg line options
    daemon.applyoptions(lineopt);
Seblu's avatar
Seblu committed
  }
  catch (const Error &e) {
    e.print();
    return e.code();
  }
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
  }

  // run daemon
  try { daemon.run(); }
Seblu's avatar
Seblu committed
  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

/*!
** Set default options
**
** @param opt option struc to set
**
*/
static SLDaemon::Options get_default_options() {
  char buf[128];
  SLDaemon::Options options;

  if (gethostname(buf, 128) == 0) {
    options.login = buf;
  }
  options.port = 18136;
  options.conffile = "/etc/sldrc";
  options.retrydelay = 60;
  return options;
}

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
}