main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <sys/un.h>
  6. #include <sys/wait.h>
  7. #include "util.h"
  8. #include "postprocess.h"
  9. static char socket_path[100];
  10. struct Job {
  11. pid_t pid;
  12. char burstdir[255];
  13. char target[255];
  14. int savedng;
  15. };
  16. pid_t
  17. start_processing(struct Job job)
  18. {
  19. pid_t child_pid = fork();
  20. if (child_pid < 0) {
  21. err("fork failed");
  22. } else if (child_pid > 0) {
  23. // parent process
  24. return child_pid;
  25. } else {
  26. // child process
  27. postprocess_internal(job.burstdir, job.target);
  28. exit(0);
  29. }
  30. return -1;
  31. }
  32. int
  33. listen_on_socket()
  34. {
  35. int sock;
  36. int ret;
  37. struct sockaddr_un addr;
  38. // Clean existing socket
  39. if (remove(socket_path) == -1 && errno != ENOENT) {
  40. err("could not clean up old socket");
  41. }
  42. // Make new unix domain socket to listen on
  43. sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  44. if (sock < 0) {
  45. err("could not make socket fd");
  46. return 0;
  47. }
  48. if (bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
  49. err("failed to bind socket");
  50. return 0;
  51. }
  52. if (listen(sock, 20) < 0) {
  53. err("failed to listen");
  54. return 0;
  55. }
  56. return sock;
  57. }
  58. int
  59. is_alive()
  60. {
  61. int sock;
  62. struct sockaddr_un addr;
  63. sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  64. if (sock < 0) {
  65. err("could not make socket fd");
  66. return 0;
  67. }
  68. memset(&addr, 0, sizeof(struct sockaddr_un));
  69. addr.sun_family = AF_UNIX;
  70. strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
  71. if (connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
  72. err("failed to open socket");
  73. return 0;
  74. }
  75. return 0;
  76. }
  77. void
  78. queue_job(struct Job job)
  79. {
  80. }
  81. void
  82. make_socket_path()
  83. {
  84. char fname[80];
  85. char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
  86. char *user = getenv("USER");
  87. snprintf(fname, sizeof(fname), "postprocessd-%s.sock", user);
  88. if (xdg_runtime_dir) {
  89. snprintf(socket_path, sizeof(socket_path), "%s/%s", xdg_runtime_dir, fname);
  90. } else {
  91. snprintf(socket_path, sizeof(socket_path), "/tmp/%s", fname);
  92. }
  93. }
  94. int
  95. main(int argc, char *argv[])
  96. {
  97. int sock;
  98. struct Job job;
  99. if (argc != 4) {
  100. printf("usage: %s burst-dir target-name save-dng\n", argv[0]);
  101. exit(1);
  102. }
  103. postprocess_setup();
  104. // Parse command line arguments into the job struct
  105. job.pid = 0;
  106. strncpy(job.burstdir, argv[1], sizeof(job.burstdir));
  107. strncpy(job.target, argv[2], sizeof(job.target));
  108. if (strcmp(argv[3], "0") == 0) {
  109. job.savedng = 0;
  110. } else {
  111. job.savedng = 1;
  112. }
  113. make_socket_path();
  114. // Check if an existing instance is running, if not start a listener
  115. if (is_alive() == 0) {
  116. printf("first instance, listening\n");
  117. sock = listen_on_socket();
  118. } else {
  119. // A processing job is already running, queue this job on the existing process and block
  120. printf("second instance, running\n");
  121. queue_job(job);
  122. }
  123. start_processing(job);
  124. wait(NULL);
  125. return 0;
  126. }