From: Cyril Hrubis <chrubis@suse.cz>
To: Jan Stancek <jstancek@redhat.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 1/3] lib: introduce safe_write_fully()
Date: Mon, 3 Oct 2022 15:16:41 +0200 [thread overview]
Message-ID: <YzrguY3J3YLJhU//@yuki> (raw)
In-Reply-To: <43d65409eb3290b09e1c3a21cb0dc079c5f4849c.1664801307.git.jstancek@redhat.com>
Hi!
> In case there is a short (but otherwise successful) write(),
> safe_write_fully() repeats write() and attempts to resume
> with the remainder of the buffer.
>
> Signed-off-by: Jan Stancek <jstancek@redhat.com>
> ---
> include/tst_safe_macros.h | 5 +++++
> lib/tst_safe_macros.c | 19 +++++++++++++++++++
> 2 files changed, 24 insertions(+)
>
> diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
> index 81c4b0844267..caee0e9cf842 100644
> --- a/include/tst_safe_macros.h
> +++ b/include/tst_safe_macros.h
> @@ -645,4 +645,9 @@ int safe_sysinfo(const char *file, const int lineno, struct sysinfo *info);
> #define SAFE_SYSINFO(info) \
> safe_sysinfo(__FILE__, __LINE__, (info))
>
> +ssize_t safe_write_fully(const char *file, const int lineno,
> + int fildes, const void *buf, size_t nbyte);
> +#define SAFE_WRITE_FULLY(fildes, buf, nbyte) \
> + safe_write_fully(__FILE__, __LINE__, (fildes), (buf), (nbyte))
We already have a flag for SAFE_WRITE() to fail/not-fail on partial
write, what about turning that into three way switch?
Something as:
enum safe_write_opts {
SAFE_WRITE_PARTIAL = 0,
SAFE_WRITE_FULL = 1,
SAFE_WRITE_RETRY = 2,
};
Or maybe just rename the SAFE_WRITE_FULLY() to SAFE_WRITE_RETRY().
> #endif /* SAFE_MACROS_H__ */
> diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
> index c4cdc87e7346..e4d4ef4269a4 100644
> --- a/lib/tst_safe_macros.c
> +++ b/lib/tst_safe_macros.c
> @@ -591,3 +591,22 @@ void safe_cmd(const char *file, const int lineno, const char *const argv[],
> tst_brk_(file, lineno, TBROK, "%s failed (%d)", argv[0], rval);
> }
> }
> +
> +ssize_t safe_write_fully(const char *file, const int lineno,
> + int fildes, const void *buf, size_t nbyte)
> +{
> + ssize_t rval;
> + size_t len = nbyte;
> +
> + do {
> + rval = write(fildes, buf, len);
> + if (rval == -1) {
> + tst_brk_(file, lineno, TBROK | TERRNO,
> + "write(%d,%p,%zu) failed", fildes, buf, len);
I guess that we may print potentionally confusing output here since we
modify the buf and len in the loop. I guess that we should store the buf
pointer at the start just for a case of this message and use the nbyte
and possibly write how many bytes we have managed to write before the
failure.
> + }
> + buf += rval;
> + len -= rval;
> + } while (len > 0);
> +
> + return nbyte;
> +}
> --
> 2.27.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2022-10-03 13:15 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-03 12:48 [LTP] [PATCH 1/3] lib: introduce safe_write_fully() Jan Stancek
2022-10-03 12:48 ` [LTP] [PATCH 2/3] syscalls/preadv203: don't treat short write() as failure Jan Stancek
2022-10-03 12:48 ` [LTP] [PATCH 3/3] mmapstress04: use SAFE_WRITE_FULLY from LTP library Jan Stancek
2022-10-03 13:16 ` Cyril Hrubis [this message]
2022-10-03 13:56 ` [LTP] [PATCH 1/3] lib: introduce safe_write_fully() Jan Stancek
2022-10-03 14:00 ` Cyril Hrubis
2022-10-04 8:31 ` [LTP] [PATCH v2] lib: introduce safe_write() retry Jan Stancek
2022-10-04 12:20 ` Petr Vorel
2022-10-04 12:34 ` Jan Stancek
2022-10-04 18:45 ` Petr Vorel
2022-10-05 9:32 ` Cyril Hrubis
2022-10-05 12:02 ` Jan Stancek
2022-10-17 14:15 ` Richard Palethorpe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YzrguY3J3YLJhU//@yuki \
--to=chrubis@suse.cz \
--cc=jstancek@redhat.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.