chardev: Avoid adding duplicate chardev
PATCH v2 1/1 chardev: Avoid adding duplicate chardev
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
RH BZ 1660831:
# (host) ls -lt /tmp/helloworld*
srwxr-xr-x. /tmp/helloworld1
srwxr-xr-x. /tmp/helloworld2
Before this patch:
hotplug existed chardev(channel1) in qmp:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket", "data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate property 'charchannel1' to object (type 'container')"}}
# ls -lt /tmp/helloworld* srwxr-xr-x. 1 root root 0 Dec 19 16:39 /tmp/helloworld2
After this patch:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket", "data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}} {"error": {"class": "GenericError", "desc": "Chardev 'charchannel1' already exists"}}
# ls -lt /tmp/helloworld* srwxr-xr-x. 1 /tmp/helloworld1 srwxr-xr-x. 1 /tmp/helloworld2
---
v1->v2
Correct error message - Eric
chardev/char.c | 6 ++++++ 1 file changed, 6 insertions(+)
chardev/char.c +6 -0
@@ -985,6 +985,12 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,ChardevReturn *ret;Chardev *chr; + chr =qemu_chr_find (id); +if (chr) { +error_setg (errp,"Chardev '%s' already exists" , id); +return NULL ; + } + cc =char_get_class (ChardevBackendKind_str (backend->type), errp);if (!cc) {return NULL ;
Signature · 1 line
2.14.3
Quoted 34 lines · Pankaj Gupta
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
RH BZ 1660831:
# (host) ls -lt
/tmp/helloworld*
srwxr-xr-x./tmp/helloworld1
srwxr-xr-x./tmp/helloworld2Before this patch:
hotplug existed chardev(channel1) in qmp:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket", "data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}{"error": {"class": "GenericError", "desc": "attempt to add duplicate property 'charchannel1' to object (type 'container')"}}
# ls -lt /tmp/helloworld* srwxr-xr-x. 1 root root 0 Dec 19 16:39 /tmp/helloworld2After this patch:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket", "data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}} {"error": {"class": "GenericError", "desc": "Chardev 'charchannel1' already exists"}}# ls -lt /tmp/helloworld* srwxr-xr-x. 1 /tmp/helloworld1 srwxr-xr-x. 1 /tmp/helloworld2Reported-by Xiaohui Li <[email protected]>Signed-off-by Pankaj Gupta <[email protected]>
Quoted 7 lines
---
v1->v2
Correct error message - Ericchardev/char.c | 6 ++++++ 1 file changed, 6 insertions(+)
Hi
Quoted 4 lines · Pankaj Gupta
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
As you pointed out, if you attempt to add a chardev with an existing ID, you get an error:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}
But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other later error path could have the same "bug" effect of removing existing chardev because of the overwrite socket creation.
Daniel, you may want to comment (we had a similar discussion about Spice server unix sockets recently)
thanks
Quoted 60 lines
RH BZ 1660831:
# (host) ls -lt
/tmp/helloworld*
srwxr-xr-x./tmp/helloworld1
srwxr-xr-x./tmp/helloworld2Before this patch:
hotplug existed chardev(channel1) in qmp:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket", "data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}{"error": {"class": "GenericError", "desc": "attempt to add duplicate property 'charchannel1' to object (type 'container')"}}
# ls -lt /tmp/helloworld* srwxr-xr-x. 1 root root 0 Dec 19 16:39 /tmp/helloworld2After this patch:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket", "data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}} {"error": {"class": "GenericError", "desc": "Chardev 'charchannel1' already exists"}}# ls -lt /tmp/helloworld* srwxr-xr-x. 1 /tmp/helloworld1 srwxr-xr-x. 1 /tmp/helloworld2Reported-by Xiaohui Li <[email protected]>Signed-off-by Pankaj Gupta <[email protected]>---
v1->v2
Correct error message - Ericchardev/char.c | 6 ++++++ 1 file changed, 6 insertions(+)chardev/char.c +6
-1@@ -985,6 +985,12 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,ChardevReturn *ret;Chardev *chr; + chr =qemu_chr_find (id); +if (chr) { +error_setg (errp,"Chardev '%s' already exists" , id); +return NULL ; + } + cc =char_get_class (ChardevBackendKind_str (backend->type), errp);if (!cc) {return NULL ; --2.14.3
Signature · 1 line
Marc-André Lureau
Quoted 28 lines · Marc-André Lureau
Hi
Quoted 4 lines · Pankaj Gupta
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
As you pointed out, if you attempt to add a chardev with an existing ID, you get an error:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.
Quoted 5 lines
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other later error path could have the same "bug" effect of removing existing chardev because of the overwrite socket creation.
Checking the ID is not a useful fix IMHO. Someone could just as easily send 2 commands with different IDs and the same socket path.
A more accurate fix would be to iterate over existing chardevs and check whether any of them clash, but even that is useless if you have two separate QEMU instances and both try to use the same UNIX socket path. To deal with that you need to start taking out fcntl locks to ensure real mutual exclusion.
I think I'd really just call this user error and do nothing
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Hi Daniel, Marc-Andre,
Thanks for your reply. Please find my reply inline.
Quoted 30 lines
Quoted 24 lines
Quoted 3 lines
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
As you pointed out, if you attempt to add a chardev with an existing ID, you get an error:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.Quoted 5 lines
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other later error path could have the same "bug" effect of removing existing chardev because of the overwrite socket creation.
Quoted 8 lines
Checking the ID is not a useful fix IMHO. Someone could just as easily send 2 commands with different IDs and the same socket path.
A more accurate fix would be to iterate over existing chardevs and check whether any of them clash, but even that is useless if you have two separate QEMU instances and both try to use the same UNIX socket path. To deal with that you need to start taking out fcntl locks to ensure real mutual exclusion.
The reason we are already throwing error "attempt to add duplicate property" implies we are considering "id" as primary key? Even if we throw the error existing chardev should work as before. But this is not the case right now, it just deletes the existing chardev after error.
Thanks,
Pankaj
Quoted 13 lines
I think I'd really just call this user error and do nothing
Regards,
Daniel
--|: https://berrange.com -o- https://www.flickr.com/photos/dberrange |:| |: https://libvirt.org -o- https://fstop138.berrange.com |:| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange |:|
Hi
Quoted 45 lines · Pankaj Gupta
Hi Daniel, Marc-Andre,
Thanks for your reply. Please find my reply inline.
Quoted 30 lines
Quoted 24 lines
Quoted 3 lines
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
As you pointed out, if you attempt to add a chardev with an existing ID, you get an error:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.Quoted 5 lines
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other later error path could have the same "bug" effect of removing existing chardev because of the overwrite socket creation.
Quoted 8 lines
Checking the ID is not a useful fix IMHO. Someone could just as easily send 2 commands with different IDs and the same socket path.
A more accurate fix would be to iterate over existing chardevs and check whether any of them clash, but even that is useless if you have two separate QEMU instances and both try to use the same UNIX socket path. To deal with that you need to start taking out fcntl locks to ensure real mutual exclusion.
Quoted 4 lines
The reason we are already throwing error "attempt to add duplicate property" implies we are considering "id" as primary key? Even if we throw the error existing chardev should work as before. But this is not the case right now, it just deletes the existing chardev after error.
It deletes the socket "file" (since it overwrites it on chardev creation). The existing chardev is not deleted:
qemu-system-x86_64 -qmp stdio
{"QMP": {"version": {"qemu": {"micro": 0, "minor": 0, "major": 3},
"package": "qemu-3.0.0-3.fc29"}, "capabilities": []}}
{"execute":"qmp_capabilities"}
{"return": {}}
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix",
"data": {"path": "/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}
{"execute":"query-chardev"}
{"return": [{"frontend-open": true, "filename": "vc", "label":
"serial0"}, {"frontend-open": true, "filename": "stdio", "label":
"compat_monitor0"}, {"frontend-open": false, "filename":
"disconnected:unix:/tmp/helloworld1,server", "label": "charchannel1"},
{"frontend-open": true, "filename": "vc", "label": "parallel0"}]}
Quoted 17 lines
Thanks,
PankajQuoted 13 lines
I think I'd really just call this user error and do nothing
Regards,
Daniel
--|: https://berrange.com -o- https://www.flickr.com/photos/dberrange |:| |: https://libvirt.org -o- https://fstop138.berrange.com |:| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange |:|
Signature · 1 line
Marc-André Lureau
Quoted 48 lines · Marc-André Lureau
Hi
Quoted 45 lines · Pankaj Gupta
Hi Daniel, Marc-Andre,
Thanks for your reply. Please find my reply inline.
Quoted 38 lines
Quoted 24 lines
Quoted 3 lines
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
As you pointed out, if you attempt to add a chardev with an existing ID, you get an error:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.Quoted 5 lines
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other later error path could have the same "bug" effect of removing existing chardev because of the overwrite socket creation.
Checking the ID is not a useful fix IMHO. Someone could just as easily send 2 commands with different IDs and the same socket path.
A more accurate fix would be to iterate over existing chardevs and check whether any of them clash, but even that is useless if you have two separate QEMU instances and both try to use the same UNIX socket path. To deal with that you need to start taking out fcntl locks to ensure real mutual exclusion.
Quoted 7 lines
Quoted 4 lines
The reason we are already throwing error "attempt to add duplicate property" implies we are considering "id" as primary key? Even if we throw the error existing chardev should work as before. But this is not the case right now, it just deletes the existing chardev after error.
It deletes the socket "file" (since it overwrites it on chardev creation). The existing chardev is not deleted:
I think this is yet another example of why it is a bad idea for the qemu_chardev_new API to also open the backend. We should have a qemu_chardev_new that only does the arg parsing, object creation and registration. Then have a separate API for actually opening it.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
Thanks for your reply. Please find my reply inline.
Quoted 34 lines
Quoted 24 lines
Quoted 3 lines
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
As you pointed out, if you attempt to add a chardev with an existing ID, you get an error:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.Quoted 6 lines
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other later error path could have the same "bug" effect of removing existing
chardev because of the overwrite socket creation.Checking the ID is not a useful fix IMHO. Someone could just as easily send 2 commands with different IDs and the same socket path.
Quoted 6 lines
A more accurate fix would be to iterate over existing chardevs and check
whether any of them clash, but even that is useless if you have two separate QEMU instances and both try to use the same UNIX socket path. To deal with that you need to start taking out fcntl locks to ensure real mutual exclusion.The reason we are already throwing error "attempt to add duplicate property"
implies we are considering "id" as primary key? Even if we throw the error
existing chardev should work as before. But this is not the case right now,
it just deletes the existing chardev after error.
Quoted 8 lines
Quoted 2 lines
It deletes the socket "file" (since it overwrites it on chardev creation). The existing chardev is not deleted:
I think this is yet another example of why it is a bad idea for the
qemu_chardev_newAPI to also open the backend. We should have aqemu_chardev_newthat only does the arg parsing, object creation and registration. Then have a separate API for actually opening it.
Agree. This looks bigger fix. Till we fix that. Can we accept this patch to avoid deleting/corrupting existing socket "file" if user tries to add chardev with existing same "id"?
Thanks,
Pankaj
Hi
Quoted 62 lines · Pankaj Gupta
Quoted 53 lines
Quoted 53 lines
Quoted 52 lines
Thanks for your reply. Please find my reply inline.
Quoted 34 lines
Quoted 24 lines
Quoted 3 lines
Hotplugging existing char chardev with qmp, dereferences(removes) existing chardev. This patch avoids adding a chardev if a chardev with same id exists.
As you pointed out, if you attempt to add a chardev with an existing ID, you get an error:
{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add duplicate
property 'charchannel1' to object (type 'container')"}}But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.Quoted 6 lines
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other later error path could have the same "bug" effect of removing existing
chardev because of the overwrite socket creation.Checking the ID is not a useful fix IMHO. Someone could just as easily send 2 commands with different IDs and the same socket path.
Quoted 6 lines
A more accurate fix would be to iterate over existing chardevs and check
whether any of them clash, but even that is useless if you have two separate QEMU instances and both try to use the same UNIX socket path. To deal with that you need to start taking out fcntl locks to ensure real mutual exclusion.The reason we are already throwing error "attempt to add duplicate property"
implies we are considering "id" as primary key? Even if we throw the error
existing chardev should work as before. But this is not the case right now,
it just deletes the existing chardev after error.Quoted 8 lines
Quoted 2 lines
It deletes the socket "file" (since it overwrites it on chardev creation). The existing chardev is not deleted:
I think this is yet another example of why it is a bad idea for the
qemu_chardev_newAPI to also open the backend. We should have aqemu_chardev_newthat only does the arg parsing, object creation and registration. Then have a separate API for actually opening it.
Quoted 3 lines
Agree. This looks bigger fix. Till we fix that. Can we accept this patch to avoid deleting/corrupting existing socket "file" if user tries to add chardev with existing same "id"?
It's not a proper fix. As I and Daniel explained, there are many other cases where a similar effect of deleting existing socket file can happen.
I would rather not merge this, but if it's critical, I would add a FIXME comment around.
Signature · 1 line
Marc-André Lureau
Quoted 80 lines
Hi
Quoted 71 lines · Pankaj Gupta
Quoted 62 lines
Quoted 62 lines
Quoted 61 lines
Thanks for your reply. Please find my reply inline.
Quoted 40 lines
Quoted 28 lines
Quoted 5 lines
Hotplugging existing char chardev with qmp, dereferences(removes)
existing chardev. This patch avoids adding a chardev if a chardev
with same id exists.As you pointed out, if you attempt to add a chardev with an existing
ID, you get an error:{"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "
/tmp/helloworld1"}}}}}}
{"return": {}} {"execute":"chardev-add","arguments":{"id":"charchannel1","backend":{"type":"socket","data":{"addr":{"type":"unix", "data": {"path": "/tmp/helloworld1"}}}}}}
{"error": {"class": "GenericError", "desc": "attempt to add
duplicate
property 'charchannel1' to object (type 'container')"}}But the existing chardev is left untouched.
However, since unix socket chardev will delete existing file and rebind (this is not always a good idea, but people seem to prefer that)
the rebound socket is removed on error cleanup.Quoted 7 lines
I am not sure this is a bug tbh.
Your solution to check for duplicate ID upfront is ok. But any other
later error path could have the same "bug" effect of removing existing
chardev because of the overwrite socket creation.Checking the ID is not a useful fix IMHO. Someone could just as easily
send 2 commands with different IDs and the same socket path.Quoted 8 lines
A more accurate fix would be to iterate over existing chardevs and check
whether any of them clash, but even that is useless if you have two separate QEMU instances and both try to use the same UNIX socket path.
To deal with that you need to start taking out fcntl locks to ensure
real mutual exclusion.The reason we are already throwing error "attempt to add duplicate property"
implies we are considering "id" as primary key? Even if we throw the error
existing chardev should work as before. But this is not the case right
now,
it just deletes the existing chardev after error.Quoted 8 lines
Quoted 2 lines
It deletes the socket "file" (since it overwrites it on chardev creation). The existing chardev is not deleted:
I think this is yet another example of why it is a bad idea for the
qemu_chardev_newAPI to also open the backend. We should have aqemu_chardev_newthat only does the arg parsing, object creation and registration. Then have a separate API for actually opening it.Quoted 3 lines
Agree. This looks bigger fix. Till we fix that. Can we accept this patch to avoid deleting/corrupting existing socket "file" if user tries to add chardev with existing same "id"?
Quoted 7 lines
It's not a proper fix. As I and Daniel explained, there are many other cases where a similar effect of deleting existing socket file can happen.
I would rather not merge this, but if it's critical, I would add a FIXME comment around.
Sure. Sounds good.
Thanks,
Pankaj