#include #include #include #include #include #include #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; int ret; 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[]) { int sock; 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"); sock = 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; }