Commit e15f75d1 authored by Seblu's avatar Seblu

Add daemon code

Add log file printing
parent 0119af1d
......@@ -47,7 +47,10 @@ void Database::open(const string &path) {
load_users_();
load_scripts_();
load_aliases_();
}
bool Database::opened() const {
return !!db_;
}
void Database::close() {
......
......@@ -19,16 +19,71 @@
#ifndef DATABASE_HH
# define DATABASE_HH
# include <list>
# include <sqlite3.h>
# include "sls.hh"
// Data container
// -----------------------------------------------------------------------------
// Classes
// -----------------------------------------------------------------------------
// Host
struct Host {
string login;
string pass;
string ipv4;
int level;
};
typedef std::list<Host> Hosts;
// Group
struct Group {
string name;
Hosts hosts;
};
typedef std::list<Group> Groups;
// User
struct User {
string login;
string pass;
int level;
bool priv;
};
typedef std::list<User> Users;
// Script
struct Script {
string name;
string path;
int level;
};
typedef std::list<Script> Scripts;
// Alias
struct Alias {
string name;
string val;
};
typedef std::list<Alias> Aliases;
// Data container
class Database {
public:
Database();
void open(const string &path);
bool opened() const;
void close();
protected:
......
......@@ -29,9 +29,10 @@ Option::Option() {
verbose = false;
database = "/var/db/sls.db";
port = 18136;
maxthread = 200;
daemon = true;
closefd = true;
logfile = "/var/log/syslog/sls";
logfile = "/var/log/sls";
pidfile = "/var/lock/sls.pid";
}
......@@ -42,12 +43,13 @@ void Option::usage(const char *name, ostream &out) const {
out << "usage: " << name
<< " [-h|--help] [-d|--database file] [-v|--verbose] [-p port]" << std::endl
<< "-h | --help : Print this help." << std::endl
<< "-d | --database dir : Define database directory." << std::endl
<< "-d | --database file : Define database (def: /var/db/sls.db)." << std::endl
<< "-v | --verbose : Set verbose mode." << std::endl
<< "-V | --version : Print version and exit." << std::endl
<< "-p | --port p : sls listen on port p." << std::endl
<< "-P | --pid-file : Path to pid file." << std::endl
<< "-l | --log-file : Path to log file." << std::endl
<< "-p | --port p : sls listen on port p (def: 18136)." << std::endl
<< "-t | --max-thread p : Maximun number of thread (def: 200)." << std::endl
<< "-P | --pid-file : Path to pid file (def: /var/lock/sls.pid)." << std::endl
<< "-l | --log-file : Path to log file (def: /var/log/sls)." << std::endl
<< "-D | --debug : Run with verbose and not in daemon mode." << std::endl
<< "-n | --no-close : Don't close stdin,stdout and stderr." << std::endl;
}
......@@ -67,6 +69,7 @@ Option &Option::load(int argc, char *argv[]) {
{"verbose", 0, 0, 'v'},
{"version", 0, 0, 'V'},
{"port", 0, 0, 'p'},
{"max-thread", 0, 0, 't'},
{"pid-file", 0, 0, 'P'},
{"log-file", 0, 0, 'l'},
{"debug", 0, 0, 'D'},
......@@ -75,7 +78,7 @@ Option &Option::load(int argc, char *argv[]) {
};
int option;
while ((option = getopt_long(argc, argv, "hvnVDd:p:P:l:", lopt, 0)) != -1) {
while ((option = getopt_long(argc, argv, "hvnVDd:p:P:l:t:", lopt, 0)) != -1) {
switch (option) {
case 'h':
usage(*argv, std::cout);
......@@ -83,6 +86,9 @@ Option &Option::load(int argc, char *argv[]) {
case 'p':
port = atoi(optarg);
break;
case 't':
maxthread = atoi(optarg);
break;
case 'd':
database = optarg;
break;
......
......@@ -34,6 +34,7 @@ struct Option {
// socket option
int port;
size_t maxthread;
// daemon options
bool daemon;
......
......@@ -18,3 +18,36 @@
#include "sls.hh"
#include "server.hh"
/*!
* Constructor
*/
Server::Server() : socket_(0) {}
/*!
* Start server. Creating socket
*/
void Server::start() {
}
/*!
* @return if server is started or not
*/
bool Server::started() const {
return socket_ != 0;
}
/*!
* Start server. Destroy socket
*/
void Server::stop() {
}
/*!
* Server listen routine. This create new thead.
*/
void Server::listen(size_t max_thread) {
assert(max_thread > 0);
}
......@@ -19,4 +19,27 @@
#ifndef SERVER_HH
# define SERVER_HH
# include <set>
class Server {
public:
typedef std::set<string> stringset;
public:
Server();
void start();
void stop();
void listen(size_t max_thread);
bool started() const;
private:
stringset logged_hosts_;
stringset logged_users_;
int socket_;
size_t max_thread_;
};
#endif
......@@ -22,28 +22,63 @@
#include "server.hh"
#include "error.hh"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
Option O;
Database D;
Server S;
int main(int argc, char *argv[])
{
static Option O;
static Database D;
// static Server S;
try {
O.load(argc, argv);
// Load options
O.load(argc, argv);
O.loadenv();
D.open(O.database);
// Daemonize
if (O.daemon) {
// open log file
int logfd = open(O.logfile.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0640);
if (logfd < 0)
throw Error(ERR_FILE, "Cannot open " + O.logfile + ": " + strerror(errno));
// S.start();
// redirect stdout and stderr
if (dup2(logfd, STDOUT_FILENO) == -1)
throw Error(ERR_FILE, (string) "Unable to redirect STDOUT into " + O.logfile + ": " + strerror(errno));
if (dup2(logfd, STDERR_FILENO) == -1)
throw Error(ERR_FILE, (string) "Unable to redirect STDERR into " + O.logfile + ": " + strerror(errno));
// close unused fd
if (close(STDIN_FILENO) == -1)
throw Error(ERR_FILE, (string) "Unable to close STDIN: " + strerror(errno));
if (close(logfd) == -1)
throw Error(ERR_FILE, (string) "Unable to close: " + O.logfile + ": " + strerror(errno));
// goto hell
daemon(0, 1);
}
// Open DB
D.open(O.database);
// S.listen();
// start network
S.start();
S.listen(O.maxthread);
// Close DB
D.close();
return ERR_NO;
}
catch (const Error &e) {
if (S.started()) S.stop();
if (D.opened()) D.close();
e.print();
return e.code();
}
......
......@@ -26,8 +26,6 @@
# include <iostream>
# include <fstream>
# include <sstream>
# include <map>
# include <list>
typedef std::string string;
typedef std::stringstream sstream;
......@@ -52,61 +50,7 @@ enum {
// Gonstant
// -----------------------------------------------------------------------------
static const string VERSION = "1";
static const string VERSION = "1.0";
static const int MAX_LINE_SIZE = 512;
// -----------------------------------------------------------------------------
// Classes
// -----------------------------------------------------------------------------
// Host
struct Host {
string login;
string pass;
string ipv4;
int level;
};
typedef std::list<Host> Hosts;
// Group
struct Group {
string name;
Hosts hosts;
};
typedef std::list<Group> Groups;
// User
struct User {
string login;
string pass;
int level;
bool priv;
};
typedef std::list<User> Users;
// Script
struct Script {
string name;
string path;
int level;
};
typedef std::list<Script> Scripts;
// Alias
struct Alias {
string name;
string val;
};
typedef std::list<Alias> Aliases;
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment