30 const char *
profile,
const char *cache,
32 const volatile sig_atomic_t *
running)
35 !application[0] || !input[0] || !
profile[0]) {
36 fprintf(stderr,
"IMAGE_LENS: invalid renderer request\n");
40 const char *extension = strrchr(input,
'.');
41 const char *separator = strrchr(input,
'/');
42 const size_t stem = extension && (!separator || extension > separator)
43 ? (
size_t)(extension - input) : strlen(input);
44 const char suffix[] =
".lens.jpg";
46 fprintf(stderr,
"IMAGE_LENS: derivative path too long\n");
51 fprintf(stderr,
"IMAGE_LENS: clock unavailable or capture cancelled\n");
54 for (
size_t index = 0; index < stem; ++index)
output[index] = input[index];
55 for (
size_t index = 0; index <
sizeof(suffix); ++index)
output[stem + index] = suffix[index];
56 char *arguments[9] = {(
char *)application, (
char *)
"--profile", (
char *)
profile};
59 arguments[
count++] = (
char *)
"--cache";
60 arguments[
count++] = (
char *)cache;
62 arguments[
count++] = (
char *)input;
64 arguments[
count] = NULL;
65 struct sigaction defaults = {.sa_handler = SIG_DFL};
66 sigemptyset(&defaults.sa_mask);
69 const pid_t parent = getpid();
70 const pid_t
child = fork();
74 if (sigaction(SIGTERM, &defaults, NULL) != 0 || sigaction(SIGINT, &defaults, NULL) != 0 ||
75 sigprocmask(SIG_SETMASK, &mask, NULL) != 0 ||
76 prctl(PR_SET_PDEATHSIG, SIGTERM) != 0 || getppid() != parent) _exit(1);
77 execv(application, arguments);
81 fprintf(stderr,
"IMAGE_LENS: cannot start %s: %s\n", application, strerror(errno));
87 const pid_t
result = waitpid(
child, &status, WNOHANG);
89 if (
result < 0 && errno != EINTR) {
90 fprintf(stderr,
"IMAGE_LENS: cannot wait for renderer: %s\n", strerror(errno));
97 fprintf(stderr,
"IMAGE_LENS: renderer deadline/cancellation; analysis skipped\n");
99 while (waitpid(
child, &status, 0) < 0 && errno == EINTR) {}
103 const struct timespec delay = {.tv_nsec = 10000000L};
104 nanosleep(&delay, NULL);
107 if ((
running && !*
running) || !WIFEXITED(status) || WEXITSTATUS(status) != 0 ||
108 stat(
output, &saved) != 0 || !S_ISREG(saved.st_mode) || saved.st_size <= 0) {
109 fprintf(stderr,
"IMAGE_LENS: renderer failed or derivative unavailable; analysis skipped\n");
bool image_lens_emulate(const char *application, const char *input, const char *profile, const char *cache, char *output, size_t capacity, const volatile sig_atomic_t *running)