123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <sys/wait.h>
- #include "util.h"
- #include "postprocess.h"
- static char socket_path[100];
- struct Job {
- pid_t pid;
- char burstdir[255];
- char target[255];
- int savedng;
- };
- pid_t
- start_processing(struct Job job)
- {
- pid_t child_pid = fork();
- if (child_pid < 0) {
- err("fork failed");
- } else if (child_pid > 0) {
- // parent process
- return child_pid;
- } else {
- // child process
- postprocess_internal(job.burstdir, job.target);
- exit(0);
- }
- return -1;
- }
- int
- listen_on_socket()
- {
- int sock;
- struct sockaddr_un addr;
- // Clean existing socket
- if (remove(socket_path) == -1 && errno != ENOENT) {
- err("could not clean up old socket");
- }
- // Make new unix domain socket to listen on
- sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
- if (sock < 0) {
- err("could not make socket fd");
- return 0;
- }
- if (bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
- err("failed to bind socket");
- return 0;
- }
- if (listen(sock, 20) < 0) {
- err("failed to listen");
- return 0;
- }
- return sock;
- }
- int
- is_alive()
- {
- int sock;
- struct sockaddr_un addr;
- sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
- if (sock < 0) {
- err("could not make socket fd");
- return 0;
- }
- memset(&addr, 0, sizeof(struct sockaddr_un));
- addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
- if (connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
- err("failed to open socket");
- return 0;
- }
- return 0;
- }
- void
- queue_job(struct Job job)
- {
- }
- void
- make_socket_path()
- {
- char fname[80];
- char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
- char *user = getenv("USER");
- snprintf(fname, sizeof(fname), "postprocessd-%s.sock", user);
- if (xdg_runtime_dir) {
- snprintf(socket_path, sizeof(socket_path), "%s/%s", xdg_runtime_dir, fname);
- } else {
- snprintf(socket_path, sizeof(socket_path), "/tmp/%s", fname);
- }
- }
- int
- main(int argc, char *argv[])
- {
- struct Job job;
- if (argc != 4) {
- printf("usage: %s burst-dir target-name save-dng\n", argv[0]);
- exit(1);
- }
- postprocess_setup();
- // Parse command line arguments into the job struct
- job.pid = 0;
- strncpy(job.burstdir, argv[1], sizeof(job.burstdir));
- strncpy(job.target, argv[2], sizeof(job.target));
- if (strcmp(argv[3], "0") == 0) {
- job.savedng = 0;
- } else {
- job.savedng = 1;
- }
- make_socket_path();
- // Check if an existing instance is running, if not start a listener
- if (is_alive() == 0) {
- printf("first instance, listening\n");
- listen_on_socket();
- } else {
- // A processing job is already running, queue this job on the existing process and block
- printf("second instance, running\n");
- queue_job(job);
- }
- start_processing(job);
- wait(NULL);
- return 0;
- }
|