Fix wsssht launch to be non-blocking

- Replace popen with pipe/fork to capture port output without blocking
- Parent reads port from pipe then closes it, allowing daemon to continue running
- Removes blocking pclose that was causing hangs
parent ad46667f
...@@ -113,8 +113,7 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons ...@@ -113,8 +113,7 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons
return -1; return -1;
} }
// Build command // Build target
char cmd[1024];
char target[512]; char target[512];
if (domain) { if (domain) {
snprintf(target, sizeof(target), "%s://%s@%s", service, client_id, domain); snprintf(target, sizeof(target), "%s://%s@%s", service, client_id, domain);
...@@ -127,18 +126,40 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons ...@@ -127,18 +126,40 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons
wsssht_path = "wsssht"; wsssht_path = "wsssht";
} }
snprintf(cmd, sizeof(cmd), "%s --daemon %s", wsssht_path, target); // Create pipe for output
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("[WEB-PROXY] Failed to create pipe");
return -1;
}
// Launch process // Fork process
FILE *fp = popen(cmd, "r"); pid_t pid = fork();
if (!fp) { if (pid == -1) {
printf("[WEB-PROXY] Failed to launch wsssht\n"); perror("[WEB-PROXY] Failed to fork");
close(pipefd[0]);
close(pipefd[1]);
return -1; return -1;
} }
if (pid == 0) {
// Child process
close(pipefd[0]); // Close read end
dup2(pipefd[1], STDOUT_FILENO); // Redirect stdout to pipe
close(pipefd[1]); // Close write end after dup
execl(wsssht_path, "wsssht", "--daemon", target, NULL);
perror("[WEB-PROXY] Failed to execl wsssht");
exit(1);
} else {
// Parent process
close(pipefd[1]); // Close write end
// Read output to find port // Read output to find port
char line[256]; char line[256];
int port = -1; int port = -1;
FILE *fp = fdopen(pipefd[0], "r");
if (fp) {
while (fgets(line, sizeof(line), fp)) { while (fgets(line, sizeof(line), fp)) {
printf("[WEB-PROXY] wsssht output: %s", line); printf("[WEB-PROXY] wsssht output: %s", line);
if (strstr(line, "Listening on port")) { if (strstr(line, "Listening on port")) {
...@@ -151,8 +172,10 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons ...@@ -151,8 +172,10 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons
} }
} }
} }
fclose(fp);
pclose(fp); } else {
close(pipefd[0]);
}
if (port <= 0) { if (port <= 0) {
printf("[WEB-PROXY] Failed to parse port from wsssht output\n"); printf("[WEB-PROXY] Failed to parse port from wsssht output\n");
...@@ -161,6 +184,7 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons ...@@ -161,6 +184,7 @@ static int launch_wsssht_daemon(const char *service, const char *client_id, cons
printf("[WEB-PROXY] wsssht daemon listening on port %d\n", port); printf("[WEB-PROXY] wsssht daemon listening on port %d\n", port);
return port; return port;
}
} }
// Find or create wsssht process for hostname // Find or create wsssht process for hostname
...@@ -219,8 +243,6 @@ static wsssht_process_t *find_or_create_wsssht_process(const char *hostname, con ...@@ -219,8 +243,6 @@ static wsssht_process_t *find_or_create_wsssht_process(const char *hostname, con
} else if (pid > 0) { } else if (pid > 0) {
proc->pid = pid; proc->pid = pid;
printf("[WEB-PROXY] Launched wsssht daemon for %s (PID: %d, Port: %d)\n", hostname, pid, proc->port); printf("[WEB-PROXY] Launched wsssht daemon for %s (PID: %d, Port: %d)\n", hostname, pid, proc->port);
// Give wsssht time to start listening
sleep(1);
} else { } else {
proc->active = false; proc->active = false;
wsssht_processes_count--; wsssht_processes_count--;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment