main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. struct sockaddr_un addr;
  37. // Clean existing socket
  38. if (remove(socket_path) == -1 && errno != ENOENT) {
  39. err("could not clean up old socket");
  40. }
  41. // Make new unix domain socket to listen on
  42. sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  43. if (sock < 0) {
  44. err("could not make socket fd");
  45. return 0;
  46. }
  47. if (bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
  48. err("failed to bind socket");
  49. return 0;
  50. }
  51. if (listen(sock, 20) < 0) {
  52. err("failed to listen");
  53. return 0;
  54. }
  55. return sock;
  56. }
  57. int
  58. is_alive()
  59. {
  60. int sock;
  61. struct sockaddr_un addr;
  62. sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  63. if (sock < 0) {
  64. err("could not make socket fd");
  65. return 0;
  66. }
  67. memset(&addr, 0, sizeof(struct sockaddr_un));
  68. addr.sun_family = AF_UNIX;
  69. strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
  70. if (connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
  71. err("failed to open socket");
  72. return 0;
  73. }
  74. return 0;
  75. }
  76. void
  77. queue_job(struct Job job)
  78. {
  79. }
  80. void
  81. make_socket_path()
  82. {
  83. char fname[80];
  84. char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
  85. char *user = getenv("USER");
  86. snprintf(fname, sizeof(fname), "postprocessd-%s.sock", user);
  87. if (xdg_runtime_dir) {
  88. snprintf(socket_path, sizeof(socket_path), "%s/%s", xdg_runtime_dir, fname);
  89. } else {
  90. snprintf(socket_path, sizeof(socket_path), "/tmp/%s", fname);
  91. }
  92. }
  93. int
  94. main(int argc, char *argv[])
  95. {
  96. struct Job job;
  97. if (argc != 4) {
  98. printf("usage: %s burst-dir target-name save-dng\n", argv[0]);
  99. exit(1);
  100. }
  101. postprocess_setup();
  102. // Parse command line arguments into the job struct
  103. job.pid = 0;
  104. strncpy(job.burstdir, argv[1], sizeof(job.burstdir));
  105. strncpy(job.target, argv[2], sizeof(job.target));
  106. if (strcmp(argv[3], "0") == 0) {
  107. job.savedng = 0;
  108. } else {
  109. job.savedng = 1;
  110. }
  111. make_socket_path();
  112. // Check if an existing instance is running, if not start a listener
  113. if (is_alive() == 0) {
  114. printf("first instance, listening\n");
  115. listen_on_socket();
  116. } else {
  117. // A processing job is already running, queue this job on the existing process and block
  118. printf("second instance, running\n");
  119. queue_job(job);
  120. }
  121. start_processing(job);
  122. wait(NULL);
  123. return 0;
  124. }