Skip to content

Commit f8d9343

Browse files
committed
qrexec-client: Factor some duplicated code
No functional change intended.
1 parent 347a9bc commit f8d9343

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

daemon/qrexec-client.c

+20-22
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static void set_remote_domain(const char *src_domain_name) {
7979
}
8080

8181
/* initialize data_protocol_version */
82-
static int handle_agent_handshake(libvchan_t *vchan, int remote_send_first)
82+
static int handle_agent_handshake(libvchan_t *vchan, bool remote_send_first)
8383
{
8484
struct msg_header hdr;
8585
struct peer_info info;
@@ -534,6 +534,23 @@ static size_t compute_service_length(const char *const remote_cmdline, const cha
534534
return service_length;
535535
}
536536

537+
static void handshake_and_go(libvchan_t *data_vchan,
538+
struct buffer *stdin_buffer,
539+
bool remote_send_first,
540+
int prepare_ret)
541+
{
542+
if (data_vchan == NULL || !libvchan_is_open(data_vchan)) {
543+
LOG(ERROR, "Failed to open data vchan connection");
544+
exit(1);
545+
}
546+
int data_protocol_version = handle_agent_handshake(data_vchan, remote_send_first);
547+
if (data_protocol_version < 0)
548+
exit(1);
549+
if (prepare_ret < 0)
550+
handle_failed_exec(data_vchan);
551+
select_loop(data_vchan, data_protocol_version, stdin_buffer);
552+
}
553+
537554
int main(int argc, char **argv)
538555
{
539556
int opt;
@@ -551,7 +568,6 @@ int main(int argc, char **argv)
551568
int src_domain_id = 0; /* if not -c given, the process is run in dom0 */
552569
int connection_timeout = 5;
553570
struct service_params svc_params;
554-
int data_protocol_version;
555571
int prepare_ret;
556572
bool kill = false;
557573

@@ -665,16 +681,7 @@ int main(int argc, char **argv)
665681
data_vchan = libvchan_client_init(data_domain, data_port);
666682
alarm(0);
667683
signal(SIGALRM, old_handler);
668-
if (!data_vchan || !libvchan_is_open(data_vchan)) {
669-
LOG(ERROR, "Failed to open data vchan connection");
670-
exit(1);
671-
}
672-
data_protocol_version = handle_agent_handshake(data_vchan, true);
673-
if (data_protocol_version < 0)
674-
exit(1);
675-
if (prepare_ret < 0)
676-
handle_failed_exec(data_vchan);
677-
select_loop(data_vchan, data_protocol_version, &stdin_buffer);
684+
handshake_and_go(data_vchan, &stdin_buffer, true, prepare_ret);
678685
} else {
679686
s = connect_unix_socket(domname);
680687
negotiate_connection_params(s,
@@ -713,16 +720,7 @@ int main(int argc, char **argv)
713720
exit(1);
714721
}
715722
wait_for_vchan_client_with_timeout(data_vchan, connection_timeout);
716-
if (!libvchan_is_open(data_vchan)) {
717-
LOG(ERROR, "Failed to open data vchan connection");
718-
exit(1);
719-
}
720-
data_protocol_version = handle_agent_handshake(data_vchan, 0);
721-
if (data_protocol_version < 0)
722-
exit(1);
723-
if (prepare_ret < 0)
724-
handle_failed_exec(data_vchan);
725-
select_loop(data_vchan, data_protocol_version, &stdin_buffer);
723+
handshake_and_go(data_vchan, &stdin_buffer, false, prepare_ret);
726724
}
727725
}
728726

0 commit comments

Comments
 (0)