snowid_checkpoint.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. *
  3. * MIT License
  4. *
  5. * Copyright (c) 2022 beyonddream
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include "snowid_checkpoint.h"
  27. #include "snowid_util.h"
  28. #define CHECKPOINT_PERIOD_SECS 2
  29. static void snow_checkpoint_periodic(char *timestamp_path);
  30. static void snow_checkpoint_periodic(char *timestamp_path)
  31. {
  32. int fd;
  33. uint64_t buf[1];
  34. if ((fd = open(timestamp_path, O_WRONLY)) == -1) {
  35. _exit(1);
  36. }
  37. uint64_t checkpoint;
  38. for(;;) {
  39. /* check if child is reparented to init(1), if so then exit. */
  40. /* XXX - may not guarenteed to work if reparent is not pid == 1 */
  41. if (getppid() == 1) {
  42. _exit(0);
  43. }
  44. if (get_current_ts(&checkpoint) == false) {
  45. continue;
  46. }
  47. buf[0] = checkpoint;
  48. /* XXX - ignore lseek() errors - it may recover during next try (: */
  49. if (lseek(fd, 0, SEEK_SET) == -1) {
  50. continue;
  51. }
  52. /* XXX - ignore write() errors - it may recover during next try (: */
  53. if (write(fd, buf, 1) != -1) {
  54. /* XXX - ignore fsync() errors - it may recover during next try (: */
  55. fsync(fd);
  56. }
  57. sleep(CHECKPOINT_PERIOD_SECS);
  58. }
  59. _exit(1);
  60. }
  61. bool snow_checkpoint_start(char *timestamp_path)
  62. {
  63. if (timestamp_path == NULL) {
  64. fprintf(stderr, "snow_checkpoint_start():timestamp_path is NULL.");
  65. return false;
  66. }
  67. if (access(timestamp_path, W_OK) != 0) {
  68. perror("snow_checkpoint_start():error while checking access to `timestamp_path`.");
  69. return false;
  70. }
  71. int pid = fork();
  72. if (pid < 0) {
  73. perror("snow_checkpoint_start():Call to fork failed.");
  74. return false;
  75. }
  76. /* child */
  77. if (pid == 0) {
  78. snow_checkpoint_periodic(timestamp_path);
  79. }
  80. return true;
  81. }