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.

131 lines
3.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <dirent.h>
  2. #include <iterator>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <sys/stat.h>
  10. #include <syslog.h>
  11. #include <unistd.h>
  12. #include <vector>
  13. #include <thread>
  14. #include <db/db.h>
  15. #include <db/create.h>
  16. #include <db/update.h>
  17. #include <db/fetch_alarms.h>
  18. #include <db/FSWatcher.h>
  19. #include <util/notifications.h>
  20. #include <util/calendar_parsing.h>
  21. #include <ical/IcalObject.h>
  22. #include <io/debug.h>
  23. const int MINUTE = 60;
  24. const int SLEEP_INTERVAL = 60 * MINUTE;
  25. sql::Database* init_database(std::string calendar) {
  26. return db::init(calendar + ".db");
  27. }
  28. void update_database(std::string directory, sql::Database *db) {
  29. std::vector<ical::IcalObject*> objects = util::parse_cal_dir(directory);
  30. for(auto const& obj : objects) {
  31. if(obj->empty()) continue;
  32. db::insert_events(db, obj);
  33. }
  34. }
  35. void init_fswatcher(std::string directory, sql::Database *db) {
  36. db::FSWatcher fswatcher(db, directory);
  37. fswatcher.run();
  38. }
  39. void do_heartbeat(std::string directory, sql::Database *db) {
  40. db::fetch_alarms(db, SLEEP_INTERVAL);
  41. }
  42. // For security purposes, we don't allow any arguments to be passed into the daemon
  43. int main(void)
  44. {
  45. // Define variables
  46. //pid_t pid, sid;
  47. //// Fork the current process
  48. //pid = fork();
  49. //// The parent process continues with a process ID greater than 0
  50. //if(pid > 0)
  51. //{
  52. // exit(EXIT_SUCCESS);
  53. //}
  54. //// A process ID lower than 0 indicates a failure in either process
  55. //else if(pid < 0)
  56. //{
  57. // exit(EXIT_FAILURE);
  58. //}
  59. //// The parent process has now terminated, and the forked child process will continue
  60. //// (the pid of the child process was 0)
  61. //// Since the child process is a daemon, the umask needs to be set so files and logs can be written
  62. //umask(0);
  63. //// Open system logs for the child process
  64. //openlog("daemon-named", LOG_NOWAIT | LOG_PID, LOG_USER);
  65. //syslog(LOG_NOTICE, "Successfully started daemon-name");
  66. //// Generate a session ID for the child process
  67. //sid = setsid();
  68. //// Ensure a valid SID for the child process
  69. //if(sid < 0)
  70. //{
  71. // // Log failure and exit
  72. // syslog(LOG_ERR, "Could not generate session ID for child process");
  73. // // If a new session ID could not be generated, we must terminate the child process
  74. // // or it will be orphaned
  75. // exit(EXIT_FAILURE);
  76. //}
  77. //// Change the current working directory to a directory guaranteed to exist
  78. //if((chdir("/")) < 0)
  79. //{
  80. // // Log failure and exit
  81. // syslog(LOG_ERR, "Could not change working directory to /");
  82. // // If our guaranteed directory does not exist, terminate the child process to ensure
  83. // // the daemon has not been hijacked
  84. // exit(EXIT_FAILURE);
  85. //}
  86. //// A daemon cannot use the terminal, so close standard file descriptors for security reasons
  87. //close(STDIN_FILENO);
  88. //close(STDOUT_FILENO);
  89. //close(STDERR_FILENO);
  90. // Daemon-specific intialization should go here
  91. setenv("TZ", "/usr/share/zoneinfo/Europe/Vienna", 1); // POSIX-specific
  92. std::string calendar = "dummy";
  93. std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/";
  94. sql::Database* db = init_database(calendar);
  95. update_database(directory, db);
  96. std::thread thread([&](){ init_fswatcher(directory, db); });
  97. // Enter daemon loop
  98. while(1)
  99. {
  100. do_heartbeat(directory, db);
  101. sleep(SLEEP_INTERVAL);
  102. }
  103. // Close system logs for the child process
  104. //syslog(LOG_NOTICE, "Stopping daemon-name");
  105. //closelog();
  106. // Terminate the child process when the daemon completes
  107. exit(EXIT_SUCCESS);
  108. }