You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#include <dirent.h>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>
#include <vector>
#include <thread>
#include <db/db.h>
#include <db/create.h>
#include <db/update.h>
#include <db/fetch_alarms.h>
#include <db/FSWatcher.h>
#include <util/notifications.h>
#include <util/calendar_parsing.h>
#include <ical/IcalObject.h>
#include <io/debug.h>
const int MINUTE = 60; const int SLEEP_INTERVAL = 60 * MINUTE;
sql::Database* init_database(std::string calendar) { return db::init(calendar + ".db"); }
void update_database(std::string directory, sql::Database *db) { std::vector<ical::IcalObject*> objects = util::parse_cal_dir(directory); for(auto const& obj : objects) { if(obj->empty()) continue; db::insert_events(db, obj); } }
void init_fswatcher(std::string directory, sql::Database *db) { db::FSWatcher fswatcher(db, directory); fswatcher.run(); }
void do_heartbeat(std::string directory, sql::Database *db) { db::fetch_alarms(db, SLEEP_INTERVAL); }
// For security purposes, we don't allow any arguments to be passed into the daemon
int main(void) { // Define variables
//pid_t pid, sid;
//// Fork the current process
//pid = fork();
//// The parent process continues with a process ID greater than 0
//if(pid > 0)
//{
// exit(EXIT_SUCCESS);
//}
//// A process ID lower than 0 indicates a failure in either process
//else if(pid < 0)
//{
// exit(EXIT_FAILURE);
//}
//// The parent process has now terminated, and the forked child process will continue
//// (the pid of the child process was 0)
//// Since the child process is a daemon, the umask needs to be set so files and logs can be written
//umask(0);
//// Open system logs for the child process
//openlog("daemon-named", LOG_NOWAIT | LOG_PID, LOG_USER);
//syslog(LOG_NOTICE, "Successfully started daemon-name");
//// Generate a session ID for the child process
//sid = setsid();
//// Ensure a valid SID for the child process
//if(sid < 0)
//{
// // Log failure and exit
// syslog(LOG_ERR, "Could not generate session ID for child process");
// // If a new session ID could not be generated, we must terminate the child process
// // or it will be orphaned
// exit(EXIT_FAILURE);
//}
//// Change the current working directory to a directory guaranteed to exist
//if((chdir("/")) < 0)
//{
// // Log failure and exit
// syslog(LOG_ERR, "Could not change working directory to /");
// // If our guaranteed directory does not exist, terminate the child process to ensure
// // the daemon has not been hijacked
// exit(EXIT_FAILURE);
//}
//// A daemon cannot use the terminal, so close standard file descriptors for security reasons
//close(STDIN_FILENO);
//close(STDOUT_FILENO);
//close(STDERR_FILENO);
// Daemon-specific intialization should go here
std::string calendar = "dummy"; std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/"; sql::Database* db = init_database(calendar); update_database(directory, db);
std::thread thread([&](){ init_fswatcher(directory, db); });
// Enter daemon loop
while(1) { do_heartbeat(directory, db); sleep(SLEEP_INTERVAL); }
// Close system logs for the child process
//syslog(LOG_NOTICE, "Stopping daemon-name");
//closelog();
// Terminate the child process when the daemon completes
exit(EXIT_SUCCESS); }
|