qemu-file: qemu_file_get_fd() cleanup
cover qemu-file: qemu_file_get_fd() cleanup
Hi all!
Here are some generic improvements around qemu_file_get_fd() interface and failure path.
Vladimir Sementsov-Ogievskiy (5):
vmstate-types: get_fd(): handle qemu_file_get_fd() failure
qemu-file: qemu_file_get_fd(): fail if no expected fd come
qemu-file: qemu_file_get_fd(): get byte in one shot
qemu-file: qemu_file_get_fd(): check service byte itself
qemu-file: qemu_file_get_fd(): improve interface
migration/qemu-file.c | 41 ++++++++++++++++++++++++--------------- migration/qemu-file.h | 2 +- migration/vmstate-types.c | 5 +++-- 3 files changed, 29 insertions(+), 19 deletions(-)
Signature · 1 line
2.48.1
PATCH 1/5 vmstate-types: get_fd(): handle qemu_file_get_fd() failure
qemu_file_get_fd() may fail, and return -1 in this case. We don't support passing "invalid" file descriptors (like -1), so we should not interpret error here like invalid descriptor.
--- migration/vmstate-types.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
migration/vmstate-types.c +10 -1
@@ -321,12 +321,21 @@ const VMStateInfo vmstate_info_uint64 = {static int get_fd (QEMUFile *f,void *pv,size_t size,const VMStateField *field) { +int fd;int32_t *v = pv; +if (migrate_mode () ==MIG_MODE_CPR_EXEC ) {qemu_get_sbe32s (f, v);return 0 ; } - *v =qemu_file_get_fd (f); + + fd =qemu_file_get_fd (f); +if (fd <0 ) { +return -1 ; + } + + *v = fd; +return 0 ; }
Signature · 1 line
2.48.1
Re: [PATCH 1/5] vmstate-types: get_fd(): handle qemu_file_get_fd() failure
Quoted 25 lines · Vladimir Sementsov-Ogievskiy
qemu_file_get_fd()may fail, and return -1 in this case. We don't support passing "invalid" file descriptors (like -1), so we should not interpret error here like invalid descriptor.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- migration/vmstate-types.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)migration/vmstate-types.c +2
-1@@ -321,12 +321,21 @@ const VMStateInfo vmstate_info_uint64 = {static int get_fd (QEMUFile *f,void *pv,size_t size,const VMStateField *field) { +int fd;int32_t *v = pv; +if (migrate_mode () ==MIG_MODE_CPR_EXEC ) {qemu_get_sbe32s (f, v);return 0 ; } - *v =qemu_file_get_fd (f);
migration/vmstate-types.c const VMStateInfo vmstate_info_uint64 = { · line 340
Quoted 10 lines
migration/vmstate-types.c +8
-0+ + fd =qemu_file_get_fd (f); +if (fd <0 ) { +return -1 ; + } + + *v = fd; +return 0 ; }
PATCH 2/5 qemu-file: qemu_file_get_fd(): fail if no expected fd come
In reply to Vladimir Sementsov-OgievskiyIn _put() we don't actually allow send a service byte without fd. So on _get() it's unexpected. Let's be strict.
--- migration/qemu-file.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
migration/qemu-file.c +16 -10
@@ -389,28 +389,34 @@ int qemu_file_get_fd(QEMUFile *f) {int fd =-1 ;FdEntry *fde; +Error *err =NULL ;if (!f->can_pass_fd) { -Error *err =NULL ;error_setg (&err,"%s does not support fd passing" , f->ioc->name); -error_report_err (error_copy (err)); -qemu_file_set_error_obj (f, -EIO , err); - goto out; + goto fail; }/* Force the dummy byte and its fd passenger to appear. */ qemu_peek_byte (f,0 ); fde =QTAILQ_FIRST (&f->fds); -if (fde) { -qemu_get_byte (f);/* Drop the dummy byte */ - fd = fde->fd; -QTAILQ_REMOVE (&f->fds, fde, entry); -g_free (fde); +if (!fde) { +error_setg (&err,"%s no FD come with service byte" , f->ioc->name); + goto fail; } -out: + +qemu_get_byte (f);/* Drop the dummy byte */ + fd = fde->fd; +QTAILQ_REMOVE (&f->fds, fde, entry); +g_free (fde); +trace_qemu_file_get_fd (f->ioc->name, fd);return fd; + +fail: +error_report_err (error_copy (err)); +qemu_file_set_error_obj (f, -EIO , err); +return -1 ; }/** Closes the file
Signature · 1 line
2.48.1
Re: [PATCH 2/5] qemu-file: qemu_file_get_fd(): fail if no expected fd come
Quoted 47 lines · Vladimir Sementsov-Ogievskiy
In
_put()we don't actually allow send a service byte without fd. So on_get()it's unexpected. Let's be strict.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- migration/qemu-file.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)migration/qemu-file.c +11
-10@@ -389,28 +389,34 @@ int qemu_file_get_fd(QEMUFile *f) {int fd =-1 ;FdEntry *fde; +Error *err =NULL ;if (!f->can_pass_fd) { -Error *err =NULL ;error_setg (&err,"%s does not support fd passing" , f->ioc->name); -error_report_err (error_copy (err)); -qemu_file_set_error_obj (f, -EIO , err); - goto out; + goto fail; }/* Force the dummy byte and its fd passenger to appear. */ qemu_peek_byte (f,0 ); fde =QTAILQ_FIRST (&f->fds); -if (fde) { -qemu_get_byte (f);/* Drop the dummy byte */ - fd = fde->fd; -QTAILQ_REMOVE (&f->fds, fde, entry); -g_free (fde); +if (!fde) { +error_setg (&err,"%s no FD come with service byte" , f->ioc->name); + goto fail; } -out: + +qemu_get_byte (f);/* Drop the dummy byte */ + fd = fde->fd; +QTAILQ_REMOVE (&f->fds, fde, entry); +g_free (fde); +
migration/qemu-file.c int qemu_file_get_fd(QEMUFile *f) · line 422
Quoted 10 lines
migration/qemu-file.c +5
-0trace_qemu_file_get_fd (f->ioc->name, fd);return fd; + +fail: +error_report_err (error_copy (err)); +qemu_file_set_error_obj (f, -EIO , err); +return -1 ; }/** Closes the file
PATCH 3/5 qemu-file: qemu_file_get_fd(): get byte in one shot
In reply to Vladimir Sementsov-OgievskiyNo reason to split into peek + get.
--- migration/qemu-file.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
migration/qemu-file.c +1 -3
@@ -396,8 +396,7 @@ int qemu_file_get_fd(QEMUFile *f) goto fail; } -/* Force the dummy byte and its fd passenger to appear. */ -qemu_peek_byte (f,0 ); +qemu_get_byte (f); fde =QTAILQ_FIRST (&f->fds);if (!fde) {
@@ -405,7 +404,6 @@ int qemu_file_get_fd(QEMUFile *f) goto fail; } -qemu_get_byte (f);/* Drop the dummy byte */ fd = fde->fd;QTAILQ_REMOVE (&f->fds, fde, entry);g_free (fde);
Signature · 1 line
2.48.1
Re: [PATCH 3/5] qemu-file: qemu_file_get_fd(): get byte in one shot
Quoted 22 lines · Vladimir Sementsov-Ogievskiy
No reason to split into peek + get.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- migration/qemu-file.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)migration/qemu-file.c +1
-2@@ -396,8 +396,7 @@ int qemu_file_get_fd(QEMUFile *f) goto fail; } -/* Force the dummy byte and its fd passenger to appear. */ -qemu_peek_byte (f,0 ); +qemu_get_byte (f); fde =QTAILQ_FIRST (&f->fds);if (!fde) {
migration/qemu-file.c int qemu_file_get_fd(QEMUFile *f) · line 409
Quoted 7 lines
migration/qemu-file.c +0
-1@@ -405,7 +404,6 @@ int qemu_file_get_fd(QEMUFile *f) goto fail; } -qemu_get_byte (f);/* Drop the dummy byte */ fd = fde->fd;QTAILQ_REMOVE (&f->fds, fde, entry);g_free (fde);
PATCH 4/5 qemu-file: qemu_file_get_fd(): check service byte itself
In reply to Vladimir Sementsov-OgievskiyWe can check it, so, why not.
--- migration/qemu-file.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
migration/qemu-file.c +7 -1
@@ -390,13 +390,19 @@ int qemu_file_get_fd(QEMUFile *f)int fd =-1 ;FdEntry *fde;Error *err =NULL ; +int service_byte;if (!f->can_pass_fd) {error_setg (&err,"%s does not support fd passing" , f->ioc->name); goto fail; } -qemu_get_byte (f); + service_byte =qemu_get_byte (f); +if (service_byte !=' ' ) { +error_setg (&err,"%s unexpected service byte: %d(%c)" , f->ioc->name, + service_byte, service_byte); + goto fail; + } fde =QTAILQ_FIRST (&f->fds);if (!fde) {
Signature · 1 line
2.48.1
Re: [PATCH 4/5] qemu-file: qemu_file_get_fd(): check service byte itself
Quoted 22 lines · Vladimir Sementsov-Ogievskiy
We can check it, so, why not.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- migration/qemu-file.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)migration/qemu-file.c +1
-0@@ -390,13 +390,19 @@ int qemu_file_get_fd(QEMUFile *f)int fd =-1 ;FdEntry *fde;Error *err =NULL ; +int service_byte;if (!f->can_pass_fd) {error_setg (&err,"%s does not support fd passing" , f->ioc->name); goto fail; }
migration/qemu-file.c int qemu_file_get_fd(QEMUFile *f) · line 408
Quoted 10 lines
migration/qemu-file.c +6
-1-qemu_get_byte (f); + service_byte =qemu_get_byte (f); +if (service_byte !=' ' ) { +error_setg (&err,"%s unexpected service byte: %d(%c)" , f->ioc->name, + service_byte, service_byte); + goto fail; + } fde =QTAILQ_FIRST (&f->fds);if (!fde) {
PATCH 5/5 qemu-file: qemu_file_get_fd(): improve interface
In reply to Vladimir Sementsov-OgievskiyMake it symmetrical with qemu_file_put_fd() and simplify callers.
--- migration/qemu-file.c | 9 ++++----- migration/qemu-file.h | 2 +- migration/vmstate-types.c | 10 +--------- 3 files changed, 6 insertions(+), 15 deletions(-)
migration/qemu-file.c +4 -5
@@ -385,9 +385,8 @@ int qemu_file_put_fd(QEMUFile *f, int fd)return ret; } -int qemu_file_get_fd (QEMUFile *f) +int qemu_file_get_fd (QEMUFile *f,int *fd) { -int fd =-1 ;FdEntry *fde;Error *err =NULL ;int service_byte;
@@ -410,12 +409,12 @@ int qemu_file_get_fd(QEMUFile *f) goto fail; } - fd = fde->fd; + *fd = fde->fd;QTAILQ_REMOVE (&f->fds, fde, entry);g_free (fde); -trace_qemu_file_get_fd (f->ioc->name, fd); -return fd; +trace_qemu_file_get_fd (f->ioc->name, *fd); +return 0 ; fail:error_report_err (error_copy (err));
migration/qemu-file.h +1 -1
@@ -82,6 +82,6 @@ size_t qemu_get_buffer_at(QEMUFile *f, const uint8_t *buf, size_t buflen,QIOChannel *qemu_file_get_ioc (QEMUFile *file);int qemu_file_put_fd (QEMUFile *f,int fd); -int qemu_file_get_fd (QEMUFile *f); +int qemu_file_get_fd (QEMUFile *f,int *fd);#endif
migration/vmstate-types.c +1 -9
@@ -321,7 +321,6 @@ const VMStateInfo vmstate_info_uint64 = {static int get_fd (QEMUFile *f,void *pv,size_t size,const VMStateField *field) { -int fd;int32_t *v = pv;if (migrate_mode () ==MIG_MODE_CPR_EXEC ) {
@@ -329,14 +328,7 @@ static int get_fd(QEMUFile *f, void *pv, size_t size,return 0 ; } - fd =qemu_file_get_fd (f); -if (fd <0 ) { -return -1 ; - } - - *v = fd; - -return 0 ; +return qemu_file_get_fd (f, v); } static int put_fd(QEMUFile *f, void *pv, size_t size,
Signature · 1 line
2.48.1
Re: [PATCH 5/5] qemu-file: qemu_file_get_fd(): improve interface
Quoted 70 lines · Vladimir Sementsov-Ogievskiy
Make it symmetrical with
qemu_file_put_fd()and simplify callers.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- migration/qemu-file.c | 9 ++++----- migration/qemu-file.h | 2 +- migration/vmstate-types.c | 10 +--------- 3 files changed, 6 insertions(+), 15 deletions(-)migration/qemu-file.c +4
-5@@ -385,9 +385,8 @@ int qemu_file_put_fd(QEMUFile *f, int fd)return ret; } -int qemu_file_get_fd (QEMUFile *f) +int qemu_file_get_fd (QEMUFile *f,int *fd) { -int fd =-1 ;FdEntry *fde;Error *err =NULL ;int service_byte;@@ -410,12 +409,12 @@ int qemu_file_get_fd(QEMUFile *f) goto fail; } - fd = fde->fd; + *fd = fde->fd;QTAILQ_REMOVE (&f->fds, fde, entry);g_free (fde); -trace_qemu_file_get_fd (f->ioc->name, fd); -return fd; +trace_qemu_file_get_fd (f->ioc->name, *fd); +return 0 ; fail:error_report_err (error_copy (err));migration/qemu-file.h +1
-1@@ -82,6 +82,6 @@ size_t qemu_get_buffer_at(QEMUFile *f, const uint8_t *buf, size_t buflen,QIOChannel *qemu_file_get_ioc (QEMUFile *file);int qemu_file_put_fd (QEMUFile *f,int fd); -int qemu_file_get_fd (QEMUFile *f); +int qemu_file_get_fd (QEMUFile *f,int *fd);#endif migration/vmstate-types.c +0
-3@@ -321,7 +321,6 @@ const VMStateInfo vmstate_info_uint64 = {static int get_fd (QEMUFile *f,void *pv,size_t size,const VMStateField *field) { -int fd;int32_t *v = pv;if (migrate_mode () ==MIG_MODE_CPR_EXEC ) {@@ -329,14 +328,7 @@ static int get_fd(QEMUFile *f, void *pv, size_t size,return 0 ; } - fd =qemu_file_get_fd (f); -if (fd <0 ) {
migration/vmstate-types.c static int get_fd(QEMUFile *f, void *pv, size_t size, · line 334
Quoted 10 lines
migration/vmstate-types.c +1
-6-return -1 ; - } - - *v = fd; - -return 0 ; +return qemu_file_get_fd (f, v); } static int put_fd(QEMUFile *f, void *pv, size_t size,
Fabiano Rosas
AppliedQuoted 12 lines · Vladimir Sementsov-Ogievskiy
Hi all!
Here are some generic improvements around
qemu_file_get_fd()interface and failure path.Vladimir Sementsov-Ogievskiy (5):
vmstate-types:get_fd(): handleqemu_file_get_fd()failure
qemu-file:qemu_file_get_fd(): fail if no expected fd come
qemu-file:qemu_file_get_fd(): get byte in one shot
qemu-file:qemu_file_get_fd(): check service byte itself
qemu-file:qemu_file_get_fd(): improve interface
Quoted 4 lines
migration/qemu-file.c | 41 ++++++++++++++++++++++++--------------- migration/qemu-file.h | 2 +- migration/vmstate-types.c | 5 +++-- 3 files changed, 29 insertions(+), 19 deletions(-)
Queued, thanks
Quoted 14 lines · Fabiano Rosas
Quoted 12 lines · Vladimir Sementsov-Ogievskiy
Hi all!
Here are some generic improvements around
qemu_file_get_fd()interface and failure path.Vladimir Sementsov-Ogievskiy (5):
vmstate-types:get_fd(): handleqemu_file_get_fd()failure
qemu-file:qemu_file_get_fd(): fail if no expected fd come
qemu-file:qemu_file_get_fd(): get byte in one shot
qemu-file:qemu_file_get_fd(): check service byte itself
qemu-file:qemu_file_get_fd(): improve interface
Quoted 6 lines
Quoted 4 lines
migration/qemu-file.c | 41 ++++++++++++++++++++++++--------------- migration/qemu-file.h | 2 +- migration/vmstate-types.c | 5 +++-- 3 files changed, 29 insertions(+), 19 deletions(-)Queued, thanks
Thanks a lot!
Signature · 2 lines
Best regards, Vladimir