Added the "Allow this time." option.

Now the user can choose the "Allow this time." option when prompted.
`ask_access` will return 2 if this option is selected. So far, only the
GUI for this feature is implemented.
This commit is contained in:
BritishTeapot 2025-03-10 18:01:04 +01:00
parent 13830ead87
commit 64289b2786

View File

@ -28,12 +28,12 @@ int init_ui_socket(const char *filename) {
}
}
/*
* This function is called from the FUSE operations functions. Those are called
* from separate threads. Therefore, there can be multiple threads that try to
* ask for access at the same time, but we have to
/**
* @param filename: The file that the process is trying to access
* @pram pi: The process information
* @return: 0 if access is denied, 1 if access is allowed, 2 if access is allwed
* for the runtime of the process
*/
int ask_access(const char *filename, struct process_info pi) {
FILE *fp;
@ -41,16 +41,31 @@ int ask_access(const char *filename, struct process_info pi) {
139 + sizeof(pid_t) * 8 + strlen(pi.name) + strlen(filename);
char *command = (char *)malloc(command_len);
snprintf(command, command_len,
"zenity --question --title \"Allow Access?\" --text \"Allow process "
"zenity --question --extra-button \"Allow this time\" --title "
"\"Allow Access?\" --text \"Allow process "
"<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"",
pi.name, pi.PID, filename);
// Zenity Question Message Popup
fp = popen(command, "r");
free(command);
if (fp == NULL) {
perror("Pipe returned a error");
return -1;
} else {
return WEXITSTATUS(pclose(fp));
}
// 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[1024];
while (fgets(buffer, sizeof(buffer), fp)) {
if (strcmp(buffer, "Allow this time.\n") == 0) {
pclose(fp);
return 2;
}
}
int zenity_exit_code = WEXITSTATUS(pclose(fp));
return zenity_exit_code;
}