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.

120 lines
3.1 KiB

  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 <db/db.h>
  14. #include <db/create.h>
  15. #include <util/notifications.h>
  16. #include <util/calendar_parsing.h>
  17. #include <io/debug.h>
  18. const int MINUTE = 1000000;
  19. void init_database(std::string calendar) {
  20. db::init(calendar + ".db");
  21. }
  22. void update_database(std::string directory) {
  23. std::vector<ical::ical_object*> objects = util::parse_cal_dir(directory);
  24. }
  25. void do_heartbeat(std::string directory)
  26. {
  27. util::notify("Updating files from " + directory, "");
  28. update_database(directory);
  29. }
  30. // For security purposes, we don't allow any arguments to be passed into the daemon
  31. int main(void)
  32. {
  33. // Define variables
  34. //pid_t pid, sid;
  35. //// Fork the current process
  36. //pid = fork();
  37. //// The parent process continues with a process ID greater than 0
  38. //if(pid > 0)
  39. //{
  40. // exit(EXIT_SUCCESS);
  41. //}
  42. //// A process ID lower than 0 indicates a failure in either process
  43. //else if(pid < 0)
  44. //{
  45. // exit(EXIT_FAILURE);
  46. //}
  47. //// The parent process has now terminated, and the forked child process will continue
  48. //// (the pid of the child process was 0)
  49. //// Since the child process is a daemon, the umask needs to be set so files and logs can be written
  50. //umask(0);
  51. //// Open system logs for the child process
  52. //openlog("daemon-named", LOG_NOWAIT | LOG_PID, LOG_USER);
  53. //syslog(LOG_NOTICE, "Successfully started daemon-name");
  54. //// Generate a session ID for the child process
  55. //sid = setsid();
  56. //// Ensure a valid SID for the child process
  57. //if(sid < 0)
  58. //{
  59. // // Log failure and exit
  60. // syslog(LOG_ERR, "Could not generate session ID for child process");
  61. // // If a new session ID could not be generated, we must terminate the child process
  62. // // or it will be orphaned
  63. // exit(EXIT_FAILURE);
  64. //}
  65. //// Change the current working directory to a directory guaranteed to exist
  66. //if((chdir("/")) < 0)
  67. //{
  68. // // Log failure and exit
  69. // syslog(LOG_ERR, "Could not change working directory to /");
  70. // // If our guaranteed directory does not exist, terminate the child process to ensure
  71. // // the daemon has not been hijacked
  72. // exit(EXIT_FAILURE);
  73. //}
  74. //// A daemon cannot use the terminal, so close standard file descriptors for security reasons
  75. //close(STDIN_FILENO);
  76. //close(STDOUT_FILENO);
  77. //close(STDERR_FILENO);
  78. // Daemon-specific intialization should go here
  79. const int SLEEP_INTERVAL = 5 * MINUTE;
  80. std::string calendar = "dummy";
  81. std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/";
  82. init_database(calendar);
  83. // Enter daemon loop
  84. while(1)
  85. {
  86. // Execute daemon heartbeat, where your recurring activity occurs
  87. do_heartbeat(directory);
  88. exit(0);
  89. // Sleep for a period of time
  90. sleep(SLEEP_INTERVAL);
  91. }
  92. // Close system logs for the child process
  93. //syslog(LOG_NOTICE, "Stopping daemon-name");
  94. //closelog();
  95. // Terminate the child process when the daemon completes
  96. exit(EXIT_SUCCESS);
  97. }