Skip to content

Commit 2729d79

Browse files
zippy2bonzini
authored andcommitted
pr-helper: Rework socket path handling
When reviewing Paolo's pr-helper patches I've noticed couple of problems: 1) socket_path needs to be calculated at two different places (one for printing out help, the other if socket activation is NOT used), 2) even though the default socket_path is allocated in compute_default_paths() it is the only default path the function handles. For instance, pidfile is allocated outside of this function. And yet again, at different places than 1) Signed-off-by: Michal Privoznik <[email protected]> Message-Id: <c791ba035f26ea957e8f3602e3009b621769b1ba.1530611283.git.mprivozn@redhat.com> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent ee8c13b commit 2729d79

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

scsi/qemu-pr-helper.c

+10-26
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ static int gid = -1;
7676

7777
static void compute_default_paths(void)
7878
{
79-
if (!socket_path) {
80-
socket_path = qemu_get_local_state_pathname("run/qemu-pr-helper.sock");
81-
}
79+
socket_path = qemu_get_local_state_pathname("run/qemu-pr-helper.sock");
80+
pidfile = qemu_get_local_state_pathname("run/qemu-pr-helper.pid");
8281
}
8382

8483
static void usage(const char *name)
8584
{
86-
compute_default_paths();
8785
(printf) (
8886
"Usage: %s [OPTIONS] FILE\n"
8987
"Persistent Reservation helper program for QEMU\n"
@@ -841,19 +839,6 @@ static gboolean accept_client(QIOChannel *ioc, GIOCondition cond, gpointer opaqu
841839
return TRUE;
842840
}
843841

844-
845-
/*
846-
* Check socket parameters compatibility when socket activation is used.
847-
*/
848-
static const char *socket_activation_validate_opts(void)
849-
{
850-
if (socket_path != NULL) {
851-
return "Unix socket can't be set when using socket activation";
852-
}
853-
854-
return NULL;
855-
}
856-
857842
static void termsig_handler(int signum)
858843
{
859844
atomic_cmpxchg(&state, RUNNING, TERMINATE);
@@ -927,6 +912,7 @@ int main(int argc, char **argv)
927912
char *trace_file = NULL;
928913
bool daemonize = false;
929914
bool pidfile_specified = false;
915+
bool socket_path_specified = false;
930916
unsigned socket_activation;
931917

932918
struct sigaction sa_sigterm;
@@ -943,12 +929,14 @@ int main(int argc, char **argv)
943929
qemu_add_opts(&qemu_trace_opts);
944930
qemu_init_exec_dir(argv[0]);
945931

946-
pidfile = qemu_get_local_state_pathname("run/qemu-pr-helper.pid");
932+
compute_default_paths();
947933

948934
while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
949935
switch (ch) {
950936
case 'k':
951-
socket_path = optarg;
937+
g_free(socket_path);
938+
socket_path = g_strdup(optarg);
939+
socket_path_specified = true;
952940
if (socket_path[0] != '/') {
953941
error_report("socket path must be absolute");
954942
exit(EXIT_FAILURE);
@@ -1039,23 +1027,20 @@ int main(int argc, char **argv)
10391027
socket_activation = check_socket_activation();
10401028
if (socket_activation == 0) {
10411029
SocketAddress saddr;
1042-
compute_default_paths();
10431030
saddr = (SocketAddress){
10441031
.type = SOCKET_ADDRESS_TYPE_UNIX,
1045-
.u.q_unix.path = g_strdup(socket_path)
1032+
.u.q_unix.path = socket_path,
10461033
};
10471034
server_ioc = qio_channel_socket_new();
10481035
if (qio_channel_socket_listen_sync(server_ioc, &saddr, &local_err) < 0) {
10491036
object_unref(OBJECT(server_ioc));
10501037
error_report_err(local_err);
10511038
return 1;
10521039
}
1053-
g_free(saddr.u.q_unix.path);
10541040
} else {
10551041
/* Using socket activation - check user didn't use -p etc. */
1056-
const char *err_msg = socket_activation_validate_opts();
1057-
if (err_msg != NULL) {
1058-
error_report("%s", err_msg);
1042+
if (socket_path_specified) {
1043+
error_report("Unix socket can't be set when using socket activation");
10591044
exit(EXIT_FAILURE);
10601045
}
10611046

@@ -1072,7 +1057,6 @@ int main(int argc, char **argv)
10721057
error_get_pretty(local_err));
10731058
exit(EXIT_FAILURE);
10741059
}
1075-
socket_path = NULL;
10761060
}
10771061

10781062
if (qemu_init_main_loop(&local_err)) {

0 commit comments

Comments
 (0)