public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libbpf: Explicitly call write to append content to file
@ 2023-03-10 15:02 liupan
  2023-03-14 20:24 ` Andrii Nakryiko
  0 siblings, 1 reply; 2+ messages in thread
From: liupan @ 2023-03-10 15:02 UTC (permalink / raw)
  To: bpf; +Cc: liupan

Write data to fd by calling "vdprintf", in most implementations
of the standard library, the data is finally written by the writev syscall.
But "uprobe_events/kprobe_events" does not allow segmented writes,
so switch the "append_to_file" function to explicit write() call.

Signed-off-by: liupan <patteliu@gmail.com>
---
 tools/lib/bpf/libbpf.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index a557718401e4..7d865ca95c81 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9912,16 +9912,20 @@ static int append_to_file(const char *file, const char *fmt, ...)
 {
 	int fd, n, err = 0;
 	va_list ap;
+	char buf[1024];
+
+	va_start(ap, fmt);
+	n = vsnprintf(buf, sizeof(buf), fmt, ap);
+	va_end(ap);
+
+	if (n < 0 || n >= sizeof(buf))
+		return -EINVAL;
 
 	fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0);
 	if (fd < 0)
 		return -errno;
 
-	va_start(ap, fmt);
-	n = vdprintf(fd, fmt, ap);
-	va_end(ap);
-
-	if (n < 0)
+	if (write(fd, buf, n) < 0)
 		err = -errno;
 
 	close(fd);
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] libbpf: Explicitly call write to append content to file
  2023-03-10 15:02 [PATCH] libbpf: Explicitly call write to append content to file liupan
@ 2023-03-14 20:24 ` Andrii Nakryiko
  0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2023-03-14 20:24 UTC (permalink / raw)
  To: liupan; +Cc: bpf

On Fri, Mar 10, 2023 at 7:13 AM liupan <patteliu@gmail.com> wrote:
>
> Write data to fd by calling "vdprintf", in most implementations
> of the standard library, the data is finally written by the writev syscall.
> But "uprobe_events/kprobe_events" does not allow segmented writes,
> so switch the "append_to_file" function to explicit write() call.
>
> Signed-off-by: liupan <patteliu@gmail.com>

please specify your real full name in Signed-off-by tag

> ---
>  tools/lib/bpf/libbpf.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a557718401e4..7d865ca95c81 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -9912,16 +9912,20 @@ static int append_to_file(const char *file, const char *fmt, ...)
>  {
>         int fd, n, err = 0;
>         va_list ap;
> +       char buf[1024];
> +
> +       va_start(ap, fmt);
> +       n = vsnprintf(buf, sizeof(buf), fmt, ap);
> +       va_end(ap);
> +
> +       if (n < 0 || n >= sizeof(buf))
> +               return -EINVAL;

if n < 0 we should return -errno here

and if n >= sizeof(buf) let's return -E2BIG

>
>         fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0);
>         if (fd < 0)
>                 return -errno;
>
> -       va_start(ap, fmt);
> -       n = vdprintf(fd, fmt, ap);
> -       va_end(ap);
> -
> -       if (n < 0)
> +       if (write(fd, buf, n) < 0)
>                 err = -errno;
>
>         close(fd);
> --
> 2.20.1
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-14 20:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-10 15:02 [PATCH] libbpf: Explicitly call write to append content to file liupan
2023-03-14 20:24 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox