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.

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