* [Qemu-devel] [PATCH 1/1] Support abstract socket namespace in AF_UNIX socket family.
@ 2013-01-07 13:15 Alexander Barabash
2013-01-07 14:07 ` Anthony Liguori
0 siblings, 1 reply; 4+ messages in thread
From: Alexander Barabash @ 2013-01-07 13:15 UTC (permalink / raw)
To: qemu-devel; +Cc: Alexander Barabash
The abstract socket namespace is a nonportable Linux extension.
The sockets' names in this namespace have no connection
with file system pathnames. To specify a named AF_UNIX socket
in the abstract socket namespace, start the socket's path option
with a backslash followed by zero: \0.
Signed-off-by: Alexander Barabash <alexander_barabash@mentor.com>
---
qemu-sockets.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 55 insertions(+), 3 deletions(-)
diff --git a/qemu-sockets.c b/qemu-sockets.c
index 3537bf3..70c8ad5 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -663,11 +663,50 @@ int inet_nonblocking_connect(const char *str,
#ifndef _WIN32
+/*
+ * The abstract socket namespace is a nonportable Linux extension.
+ * The sockets' names in this namespace have no connection
+ * with file system pathnames. To specify a named AF_UNIX socket
+ * in the abstract socket namespace, start the socket's path option
+ * with a backslash followed by zero: \0.
+ */
+static
+char *get_abstract_namespace_path(const char *path, int *abstract_path_length)
+{
+ char *abstract_path;
+ char *inp;
+ char *outp;
+ char c;
+
+ *abstract_path_length = 0;
+ if ((path == NULL) || (path[0] != '\\') || (path[1] != '0')) {
+ return NULL;
+ }
+ abstract_path = g_strdup(path + 2);
+ for (inp = abstract_path, outp = abstract_path; (c = *inp); ++inp, ++outp) {
+ if (c == '\\') {
+ c = *++inp;
+ if (c == '\\') {
+ *outp = c;
+ } else if (c == '0') {
+ *outp = '\0';
+ }
+ } else {
+ *outp = c;
+ }
+ }
+ *abstract_path_length = outp - abstract_path;
+ return abstract_path;
+}
+
int unix_listen_opts(QemuOpts *opts, Error **errp)
{
struct sockaddr_un un;
const char *path = qemu_opt_get(opts, "path");
+ int abstract_path_length;
+ char *abstract_path;
int sock, fd;
+ socklen_t addrlen = sizeof(un);
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
@@ -675,9 +714,18 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
return -1;
}
+ abstract_path = get_abstract_namespace_path(path, &abstract_path_length);
+
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
- if (path && strlen(path)) {
+ if (abstract_path != NULL) {
+ if (abstract_path_length > sizeof(un.sun_path) - 1) {
+ abstract_path_length = sizeof(un.sun_path) - 1;
+ }
+ memcpy(un.sun_path + 1, abstract_path, abstract_path_length);
+ addrlen =
+ offsetof(struct sockaddr_un, sun_path) + 1 + abstract_path_length;
+ } else if (path && strlen(path)) {
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
} else {
char *tmpdir = getenv("TMPDIR");
@@ -694,8 +742,10 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
qemu_opt_set(opts, "path", un.sun_path);
}
- unlink(un.sun_path);
- if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
+ if (abstract_path == NULL) {
+ unlink(un.sun_path);
+ }
+ if (bind(sock, (struct sockaddr *) &un, addrlen) < 0) {
error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
goto err;
}
@@ -704,10 +754,12 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
goto err;
}
+ g_free(abstract_path);
return sock;
err:
closesocket(sock);
+ g_free(abstract_path);
return -1;
}
--
1.7.9.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH 1/1] Support abstract socket namespace in AF_UNIX socket family.
2013-01-07 13:15 [Qemu-devel] [PATCH 1/1] Support abstract socket namespace in AF_UNIX socket family Alexander Barabash
@ 2013-01-07 14:07 ` Anthony Liguori
2013-01-08 10:55 ` Alexander Barabash
2013-01-08 18:53 ` [Qemu-devel] [PATCH v2 1/1] chardev: " Alexander Barabash
0 siblings, 2 replies; 4+ messages in thread
From: Anthony Liguori @ 2013-01-07 14:07 UTC (permalink / raw)
To: Alexander Barabash, qemu-devel
Alexander Barabash <alexander_barabash@mentor.com> writes:
> The abstract socket namespace is a nonportable Linux extension.
> The sockets' names in this namespace have no connection
> with file system pathnames. To specify a named AF_UNIX socket
> in the abstract socket namespace, start the socket's path option
> with a backslash followed by zero: \0.
>
> Signed-off-by: Alexander Barabash <alexander_barabash@mentor.com>
Magic interpretation of path names is a bad thing in general.
I think it would be better to add a flag 'abstract=on' which
indicates to use the abstract namespace. As an example:
qemu -chardev foo,path=bar,abstract=on
vs:
qemu -chardev foo,path="\\\\0bar"
I think the former is quite a bit more user friendly.
Regards,
Anthony Liguori
> ---
> qemu-sockets.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
> 1 file changed, 55 insertions(+), 3 deletions(-)
>
> diff --git a/qemu-sockets.c b/qemu-sockets.c
> index 3537bf3..70c8ad5 100644
> --- a/qemu-sockets.c
> +++ b/qemu-sockets.c
> @@ -663,11 +663,50 @@ int inet_nonblocking_connect(const char *str,
>
> #ifndef _WIN32
>
> +/*
> + * The abstract socket namespace is a nonportable Linux extension.
> + * The sockets' names in this namespace have no connection
> + * with file system pathnames. To specify a named AF_UNIX socket
> + * in the abstract socket namespace, start the socket's path option
> + * with a backslash followed by zero: \0.
> + */
> +static
> +char *get_abstract_namespace_path(const char *path, int *abstract_path_length)
> +{
> + char *abstract_path;
> + char *inp;
> + char *outp;
> + char c;
> +
> + *abstract_path_length = 0;
> + if ((path == NULL) || (path[0] != '\\') || (path[1] != '0')) {
> + return NULL;
> + }
> + abstract_path = g_strdup(path + 2);
> + for (inp = abstract_path, outp = abstract_path; (c = *inp); ++inp, ++outp) {
> + if (c == '\\') {
> + c = *++inp;
> + if (c == '\\') {
> + *outp = c;
> + } else if (c == '0') {
> + *outp = '\0';
> + }
> + } else {
> + *outp = c;
> + }
> + }
> + *abstract_path_length = outp - abstract_path;
> + return abstract_path;
> +}
> +
> int unix_listen_opts(QemuOpts *opts, Error **errp)
> {
> struct sockaddr_un un;
> const char *path = qemu_opt_get(opts, "path");
> + int abstract_path_length;
> + char *abstract_path;
> int sock, fd;
> + socklen_t addrlen = sizeof(un);
>
> sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
> if (sock < 0) {
> @@ -675,9 +714,18 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
> return -1;
> }
>
> + abstract_path = get_abstract_namespace_path(path, &abstract_path_length);
> +
> memset(&un, 0, sizeof(un));
> un.sun_family = AF_UNIX;
> - if (path && strlen(path)) {
> + if (abstract_path != NULL) {
> + if (abstract_path_length > sizeof(un.sun_path) - 1) {
> + abstract_path_length = sizeof(un.sun_path) - 1;
> + }
> + memcpy(un.sun_path + 1, abstract_path, abstract_path_length);
> + addrlen =
> + offsetof(struct sockaddr_un, sun_path) + 1 + abstract_path_length;
> + } else if (path && strlen(path)) {
> snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
> } else {
> char *tmpdir = getenv("TMPDIR");
> @@ -694,8 +742,10 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
> qemu_opt_set(opts, "path", un.sun_path);
> }
>
> - unlink(un.sun_path);
> - if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
> + if (abstract_path == NULL) {
> + unlink(un.sun_path);
> + }
> + if (bind(sock, (struct sockaddr *) &un, addrlen) < 0) {
> error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
> goto err;
> }
> @@ -704,10 +754,12 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
> goto err;
> }
>
> + g_free(abstract_path);
> return sock;
>
> err:
> closesocket(sock);
> + g_free(abstract_path);
> return -1;
> }
>
> --
> 1.7.9.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH 1/1] Support abstract socket namespace in AF_UNIX socket family.
2013-01-07 14:07 ` Anthony Liguori
@ 2013-01-08 10:55 ` Alexander Barabash
2013-01-08 18:53 ` [Qemu-devel] [PATCH v2 1/1] chardev: " Alexander Barabash
1 sibling, 0 replies; 4+ messages in thread
From: Alexander Barabash @ 2013-01-08 10:55 UTC (permalink / raw)
To: Anthony Liguori; +Cc: qemu-devel
Hi,
On 01/07/2013 04:07 PM, Anthony Liguori wrote:
> Alexander Barabash <alexander_barabash@mentor.com> writes:
>
>> The abstract socket namespace is a nonportable Linux extension.
>> The sockets' names in this namespace have no connection
>> with file system pathnames. To specify a named AF_UNIX socket
>> in the abstract socket namespace, start the socket's path option
>> with a backslash followed by zero: \0.
>>
>> Signed-off-by: Alexander Barabash <alexander_barabash@mentor.com>
> Magic interpretation of path names is a bad thing in general.
>
> I think it would be better to add a flag 'abstract=on' which
> indicates to use the abstract namespace. As an example:
>
> qemu -chardev foo,path=bar,abstract=on
>
> vs:
>
> qemu -chardev foo,path="\\\\0bar"
>
> I think the former is quite a bit more user friendly.
OK
>
> Regards,
>
> Anthony Liguori
Regards,
Alex
>
>> ---
>> qemu-sockets.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
>> 1 file changed, 55 insertions(+), 3 deletions(-)
>>
>> diff --git a/qemu-sockets.c b/qemu-sockets.c
>> index 3537bf3..70c8ad5 100644
>> --- a/qemu-sockets.c
>> +++ b/qemu-sockets.c
>> @@ -663,11 +663,50 @@ int inet_nonblocking_connect(const char *str,
>>
>> #ifndef _WIN32
>>
>> +/*
>> + * The abstract socket namespace is a nonportable Linux extension.
>> + * The sockets' names in this namespace have no connection
>> + * with file system pathnames. To specify a named AF_UNIX socket
>> + * in the abstract socket namespace, start the socket's path option
>> + * with a backslash followed by zero: \0.
>> + */
>> +static
>> +char *get_abstract_namespace_path(const char *path, int *abstract_path_length)
>> +{
>> + char *abstract_path;
>> + char *inp;
>> + char *outp;
>> + char c;
>> +
>> + *abstract_path_length = 0;
>> + if ((path == NULL) || (path[0] != '\\') || (path[1] != '0')) {
>> + return NULL;
>> + }
>> + abstract_path = g_strdup(path + 2);
>> + for (inp = abstract_path, outp = abstract_path; (c = *inp); ++inp, ++outp) {
>> + if (c == '\\') {
>> + c = *++inp;
>> + if (c == '\\') {
>> + *outp = c;
>> + } else if (c == '0') {
>> + *outp = '\0';
>> + }
>> + } else {
>> + *outp = c;
>> + }
>> + }
>> + *abstract_path_length = outp - abstract_path;
>> + return abstract_path;
>> +}
>> +
>> int unix_listen_opts(QemuOpts *opts, Error **errp)
>> {
>> struct sockaddr_un un;
>> const char *path = qemu_opt_get(opts, "path");
>> + int abstract_path_length;
>> + char *abstract_path;
>> int sock, fd;
>> + socklen_t addrlen = sizeof(un);
>>
>> sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
>> if (sock < 0) {
>> @@ -675,9 +714,18 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
>> return -1;
>> }
>>
>> + abstract_path = get_abstract_namespace_path(path, &abstract_path_length);
>> +
>> memset(&un, 0, sizeof(un));
>> un.sun_family = AF_UNIX;
>> - if (path && strlen(path)) {
>> + if (abstract_path != NULL) {
>> + if (abstract_path_length > sizeof(un.sun_path) - 1) {
>> + abstract_path_length = sizeof(un.sun_path) - 1;
>> + }
>> + memcpy(un.sun_path + 1, abstract_path, abstract_path_length);
>> + addrlen =
>> + offsetof(struct sockaddr_un, sun_path) + 1 + abstract_path_length;
>> + } else if (path && strlen(path)) {
>> snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
>> } else {
>> char *tmpdir = getenv("TMPDIR");
>> @@ -694,8 +742,10 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
>> qemu_opt_set(opts, "path", un.sun_path);
>> }
>>
>> - unlink(un.sun_path);
>> - if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
>> + if (abstract_path == NULL) {
>> + unlink(un.sun_path);
>> + }
>> + if (bind(sock, (struct sockaddr *) &un, addrlen) < 0) {
>> error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
>> goto err;
>> }
>> @@ -704,10 +754,12 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
>> goto err;
>> }
>>
>> + g_free(abstract_path);
>> return sock;
>>
>> err:
>> closesocket(sock);
>> + g_free(abstract_path);
>> return -1;
>> }
>>
>> --
>> 1.7.9.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Qemu-devel] [PATCH v2 1/1] chardev: Support abstract socket namespace in AF_UNIX socket family.
2013-01-07 14:07 ` Anthony Liguori
2013-01-08 10:55 ` Alexander Barabash
@ 2013-01-08 18:53 ` Alexander Barabash
1 sibling, 0 replies; 4+ messages in thread
From: Alexander Barabash @ 2013-01-08 18:53 UTC (permalink / raw)
To: qemu-devel, anthony; +Cc: Alexander Barabash
The abstract socket namespace is a nonportable Linux extension.
The sockets' names in this namespace have no connection
with file system pathnames. To specify a named AF_UNIX socket
in the abstract socket namespace, pass true for the boolean flag
"abstract", e.g.:
qemu -chardev socket,path=NAME_OF_THE_SOCKET,abstract=on
Signed-off-by: Alexander Barabash <alexander_barabash@mentor.com>
---
qemu-config.c | 3 +++
qemu-sockets.c | 35 ++++++++++++++++++++++++++++++-----
2 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/qemu-config.c b/qemu-config.c
index 2188c3e..5b0f71e 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -182,6 +182,9 @@ static QemuOptsList qemu_chardev_opts = {
.name = "ipv6",
.type = QEMU_OPT_BOOL,
},{
+ .name = "abstract",
+ .type = QEMU_OPT_BOOL,
+ },{
.name = "wait",
.type = QEMU_OPT_BOOL,
},{
diff --git a/qemu-sockets.c b/qemu-sockets.c
index 3537bf3..b1c3a79 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -56,6 +56,9 @@ static QemuOptsList dummy_opts = {
},{
.name = "ipv6",
.type = QEMU_OPT_BOOL,
+ },{
+ .name = "abstract",
+ .type = QEMU_OPT_BOOL,
},
{ /* end if list */ }
},
@@ -667,7 +670,9 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
{
struct sockaddr_un un;
const char *path = qemu_opt_get(opts, "path");
+ bool abstract = qemu_opt_get_bool(opts, "abstract", false);
int sock, fd;
+ socklen_t addrlen = sizeof(un);
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
@@ -678,7 +683,15 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
if (path && strlen(path)) {
- snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
+ if (!abstract) {
+ snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
+ } else {
+ int printed_chars;
+ printed_chars = snprintf(un.sun_path + 1, sizeof(un.sun_path) - 1,
+ "%s", path);
+ addrlen =
+ offsetof(struct sockaddr_un, sun_path) + 1 + printed_chars;
+ }
} else {
char *tmpdir = getenv("TMPDIR");
snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
@@ -694,8 +707,10 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
qemu_opt_set(opts, "path", un.sun_path);
}
- unlink(un.sun_path);
- if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
+ if (!abstract) {
+ unlink(un.sun_path);
+ }
+ if (bind(sock, (struct sockaddr *) &un, addrlen) < 0) {
error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
goto err;
}
@@ -716,8 +731,10 @@ int unix_connect_opts(QemuOpts *opts, Error **errp,
{
struct sockaddr_un un;
const char *path = qemu_opt_get(opts, "path");
+ bool abstract = qemu_opt_get_bool(opts, "abstract", false);
ConnectState *connect_state = NULL;
int sock, rc;
+ socklen_t addrlen = sizeof(un);
if (NULL == path) {
error_setg(errp, "unix connect: no path specified\n");
@@ -738,12 +755,20 @@ int unix_connect_opts(QemuOpts *opts, Error **errp,
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
- snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
+ if (!abstract) {
+ snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
+ } else {
+ int printed_chars;
+ printed_chars = snprintf(un.sun_path + 1, sizeof(un.sun_path) - 1,
+ "%s", path);
+ addrlen =
+ offsetof(struct sockaddr_un, sun_path) + 1 + printed_chars;
+ }
/* connect to peer */
do {
rc = 0;
- if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
+ if (connect(sock, (struct sockaddr *) &un, addrlen) < 0) {
rc = -socket_error();
}
} while (rc == -EINTR);
--
1.7.9.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-01-08 18:54 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-07 13:15 [Qemu-devel] [PATCH 1/1] Support abstract socket namespace in AF_UNIX socket family Alexander Barabash
2013-01-07 14:07 ` Anthony Liguori
2013-01-08 10:55 ` Alexander Barabash
2013-01-08 18:53 ` [Qemu-devel] [PATCH v2 1/1] chardev: " Alexander Barabash
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).