I stole the printk_once() macro.
I always wanted to be able to print some error directly if there is a
buffer to dump, however we can't use error_report() where the code path
can be triggered by DDOS attack. To avoid that, we can introduce a
print-once-like function for it. Meanwhile, we also introduce the
corresponding helper for warn_report().
CC: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
include/qemu/error-report.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h
index e1c8ae1a52..3e6e84801f 100644
--- a/include/qemu/error-report.h
+++ b/include/qemu/error-report.h
@@ -44,6 +44,32 @@ void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
void warn_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
void info_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
+/* Similar to error_report(), but it only prints the message once. */
+#define error_report_once(fmt, ...) \
+ ({ \
+ static bool __print_once; \
+ bool __ret_print_once = !__print_once; \
+ \
+ if (!__print_once) { \
+ __print_once = true; \
+ error_report(fmt, ##__VA_ARGS__); \
+ } \
+ unlikely(__ret_print_once); \
+ })
+
+/* Similar to warn_report(), but it only prints the message once. */
+#define warn_report_once(fmt, ...) \
+ ({ \
+ static bool __print_once; \
+ bool __ret_print_once = !__print_once; \
+ \
+ if (!__print_once) { \
+ __print_once = true; \
+ warn_report(fmt, ##__VA_ARGS__); \
+ } \
+ unlikely(__ret_print_once); \
+ })
+
const char *error_get_progname(void);
extern bool enable_timestamp_msg;
--
2.17.0
On 05/21/2018 10:56 PM, Peter Xu wrote:
> I stole the printk_once() macro.
>
> I always wanted to be able to print some error directly if there is a
> buffer to dump, however we can't use error_report() where the code path
> can be triggered by DDOS attack. To avoid that, we can introduce a
> print-once-like function for it. Meanwhile, we also introduce the
> corresponding helper for warn_report().
>
> CC: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
> include/qemu/error-report.h | 26 ++++++++++++++++++++++++++
> 1 file changed, 26 insertions(+)
>
> diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h
> index e1c8ae1a52..3e6e84801f 100644
> --- a/include/qemu/error-report.h
> +++ b/include/qemu/error-report.h
> @@ -44,6 +44,32 @@ void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
> void warn_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
> void info_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
>
> +/* Similar to error_report(), but it only prints the message once. */
> +#define error_report_once(fmt, ...) \
> + ({ \
> + static bool __print_once; \
You're still using the reserved namespace. I asked you to fix that on
v1; it's disappointing when a later revision doesn't make changes
requested by a reviewers, without also offering a counter-argument for
why the request does not need to be considered.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
On Wed, May 23, 2018 at 10:47:29AM -0500, Eric Blake wrote:
> On 05/21/2018 10:56 PM, Peter Xu wrote:
> > I stole the printk_once() macro.
> >
> > I always wanted to be able to print some error directly if there is a
> > buffer to dump, however we can't use error_report() where the code path
> > can be triggered by DDOS attack. To avoid that, we can introduce a
> > print-once-like function for it. Meanwhile, we also introduce the
> > corresponding helper for warn_report().
> >
> > CC: Markus Armbruster <armbru@redhat.com>
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> > include/qemu/error-report.h | 26 ++++++++++++++++++++++++++
> > 1 file changed, 26 insertions(+)
> >
> > diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h
> > index e1c8ae1a52..3e6e84801f 100644
> > --- a/include/qemu/error-report.h
> > +++ b/include/qemu/error-report.h
> > @@ -44,6 +44,32 @@ void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
> > void warn_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
> > void info_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
> > +/* Similar to error_report(), but it only prints the message once. */
> > +#define error_report_once(fmt, ...) \
> > + ({ \
> > + static bool __print_once; \
>
> You're still using the reserved namespace. I asked you to fix that on v1;
> it's disappointing when a later revision doesn't make changes requested by a
> reviewers, without also offering a counter-argument for why the request does
> not need to be considered.
I am sorry. It's totally because I forgot that one! (It rarely
happens since I note them down in most cases, but there must be
something that even interrupted the procedure when I took notes...)
I will repost with another one soon. Sorry again.
--
Peter Xu
© 2016 - 2026 Red Hat, Inc.