main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. memset(&addr, 0, sizeof(struct sockaddr_un));
  48. addr.sun_family = AF_UNIX;
  49. strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
  50. if (bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
  51. err("failed to bind socket");
  52. return 0;
  53. }
  54. if (listen(sock, 20) < 0) {
  55. err("failed to listen");
  56. return 0;
  57. }
  58. return sock;
  59. }
  60. int
  61. queue_job(struct Job job)
  62. {
  63. int sock;
  64. struct sockaddr_un addr;
  65. sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  66. if (sock < 0) {
  67. err("could not make socket fd");
  68. return 0;
  69. }
  70. memset(&addr, 0, sizeof(struct sockaddr_un));
  71. addr.sun_family = AF_UNIX;
  72. strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
  73. if (connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
  74. err("failed to open socket");
  75. return 0;
  76. }
  77. if (write(sock, &job, sizeof(job)) < 0) {
  78. err("failed to write");
  79. return 0;
  80. }
  81. close(sock);
  82. return 1;
  83. }
  84. int
  85. listen_and_convert()
  86. {
  87. int sock, fd;
  88. struct sockaddr_un cli_addr;
  89. unsigned int clilen;
  90. struct Job j;
  91. clilen = sizeof(cli_addr);
  92. postprocess_setup();
  93. sock = listen_on_socket();
  94. while(1) {
  95. fd = accept(sock, (struct sockaddr *)&cli_addr, &clilen);
  96. if (fd < 0) {
  97. err("failed to accept");
  98. return 0;
  99. }
  100. if(read(fd, &j, sizeof(j)) < 0) {
  101. err("failed to read");
  102. return 0;
  103. }
  104. printf("%s -> %s\n", j.burstdir, j.target);
  105. close(fd);
  106. }
  107. }
  108. void
  109. make_socket_path()
  110. {
  111. char fname[80];
  112. char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
  113. char *user = getenv("USER");
  114. snprintf(fname, sizeof(fname), "postprocessd-%s.sock", user);
  115. if (xdg_runtime_dir) {
  116. snprintf(socket_path, sizeof(socket_path), "%s/%s", xdg_runtime_dir, fname);
  117. } else {
  118. snprintf(socket_path, sizeof(socket_path), "/tmp/%s", fname);
  119. }
  120. }
  121. int
  122. main(int argc, char *argv[])
  123. {
  124. struct Job job;
  125. make_socket_path();
  126. if (argc != 4) {
  127. printf("usage: %s burst-dir target-name save-dng\n", argv[0]);
  128. listen_and_convert();
  129. exit(1);
  130. }
  131. // Parse command line arguments into the job struct
  132. job.pid = 0;
  133. strncpy(job.burstdir, argv[1], sizeof(job.burstdir));
  134. strncpy(job.target, argv[2], sizeof(job.target));
  135. if (strcmp(argv[3], "0") == 0) {
  136. job.savedng = 0;
  137. } else {
  138. job.savedng = 1;
  139. }
  140. queue_job(job);
  141. return 0;
  142. }