From 8cb7721e392b1673ad0c5aef37a44a42f63ccfa0 Mon Sep 17 00:00:00 2001 From: fedir Date: Thu, 1 May 2025 20:55:05 +0200 Subject: [PATCH] Updated ui-socket to use the new dialogue --- src/ui-socket.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/ui-socket.c b/src/ui-socket.c index ad2549e..f383b77 100644 --- a/src/ui-socket.c +++ b/src/ui-socket.c @@ -11,6 +11,7 @@ #include #include #define _GNU_SOURCE +#include "cc.h" #include "perm_permissions_table.h" #include "real_filename.h" #include "temp_permissions_table.h" @@ -23,7 +24,9 @@ #include #include -#define ZENITY_TEMP_ALLOW_MESSAGE "Allow this time\n" +#define ZENITY_YES 0 +#define ZENITY_NO 1 +#define ZENITY_PERM 2 int init_ui_socket(const char *perm_permissions_db_filename) { FILE *fp = NULL; @@ -66,12 +69,8 @@ void destroy_ui_socket(void) { access_t ask_access(const char *filename, struct process_info proc_info) { FILE *fp = NULL; char *command = NULL; - int ret = - asprintf(&command, - "zenity --question --extra-button \"Allow this time\" --title " - "\"Allow Access?\" --text \"Allow process " - "%s with PID %d to access %s\"", - proc_info.name, proc_info.PID, filename, get_mountpoint()); + int ret = asprintf(&command, "zenity \"%d\" \"%s\" \"%s\" \"%s\"", + proc_info.PID, proc_info.name, filename, get_mountpoint()); if (ret < 0) { // If asprintf fails, the contents of command are undefined (see man @@ -92,24 +91,30 @@ access_t ask_access(const char *filename, struct process_info proc_info) { return DENY; } - // if the user clicks the "Allow this time" button, `zenity` will only - // write it to `stdout`, but the exit code will still be `1`. So, we need - // to manually check the output. - char buffer[sizeof(ZENITY_TEMP_ALLOW_MESSAGE) + 1]; - while (fgets(buffer, sizeof(buffer), fp)) { - printf("%s", buffer); - if (strcmp(buffer, ZENITY_TEMP_ALLOW_MESSAGE) == 0) { - pclose(fp); - return ALLOW_TEMP; - } + str(char) zenity_output; + init(&zenity_output); + + size_t total_read = 0; + char line[1024]; // Buffer to read individual lines + + // Read the command output line by line + while (fgets(line, sizeof(line), fp)) { + size_t line_len = strlen(line); + push_fmt(&zenity_output, line); } int zenity_exit_code = WEXITSTATUS(pclose(fp)); + fprintf(stderr, "zenity wrote out %s\n", first(&zenity_output)); fprintf(stderr, "zenity returned %d\n", zenity_exit_code); - // zenity returns 1 on "No" >:( - if (zenity_exit_code == 0) { + + cleanup(&zenity_output); + + if (zenity_exit_code == (ZENITY_YES | ZENITY_PERM)) { return ALLOW; } + if (zenity_exit_code == ZENITY_YES) { + return ALLOW_TEMP; + } return DENY; }