Re: [PATCH 05/24] perf daemon: Add client socket support
From: Jiri Olsa
Date: Thu Feb 11 2021 - 12:36:22 EST
On Thu, Feb 11, 2021 at 09:52:05AM -0300, Arnaldo Carvalho de Melo wrote:
> Em Mon, Feb 08, 2021 at 09:08:49PM +0100, Jiri Olsa escreveu:
> > +__maybe_unused
> > +static int send_cmd(struct daemon *daemon, union cmd *cmd)
> > +{
> > + int ret = -1, fd;
> > + char *line = NULL;
> > + size_t len = 0;
> > + ssize_t nread;
> > + FILE *in = NULL;
> > +
> > + if (setup_client_config(daemon))
> > + return -1;
> > +
> > + fd = setup_client_socket(daemon);
> > + if (fd < 0)
> > + return -1;
> > +
> > + if (sizeof(*cmd) != writen(fd, cmd, sizeof(*cmd))) {
> > + perror("failed: write");
> > + goto out;
> > + }
> > +
> > + in = fdopen(fd, "r");
> > + if (!in) {
> > + perror("failed: fdopen");
> > + goto out;
> > + }
> > +
> > + while ((nread = getline(&line, &len, in)) != -1) {
> > + fwrite(line, nread, 1, stdout);
> > + fflush(stdout);
> > + }
> > +
> > + ret = 0;
> > + fclose(in);
> > +out:
> > + /* If in is defined, then fd is closed via fclose. */
> > + if (!in)
> > + close(fd);
> > + return ret;
> > +}
> > +
>
> So I had to add the patch below to deal with this in some systems:
>
> cc1: warnings being treated as errors
> builtin-daemon.c: In function 'send_cmd': MKDIR /tmp/build/perf/bench/
>
> builtin-daemon.c:1368: error: ignoring return value of 'fwrite', declared with attribute warn_unused_result
> MKDIR /tmp/build/perf/tests/
> make[3]: *** [/tmp/build/perf/builtin-daemon.o] Error 1
>
> And also to not leak the 'line' buffer allocated by getline(), since you
> initialized line to NULL and len to zero, man page says:
>
> If *lineptr is set to NULL and *n is set 0 before the call,
> then getline() will allocate a buffer for storing the line.
> This buffer should be freed by the user program even if
> getline() failed.
>
>
> diff --git a/tools/perf/builtin-daemon.c b/tools/perf/builtin-daemon.c
> index e0880c5ee39c89bd..0a282c4e23a9fd9a 100644
> --- a/tools/perf/builtin-daemon.c
> +++ b/tools/perf/builtin-daemon.c
> @@ -344,12 +344,15 @@ static int send_cmd(struct daemon *daemon, union cmd *cmd)
> }
>
> while ((nread = getline(&line, &len, in)) != -1) {
> - fwrite(line, nread, 1, stdout);
> + if (fwrite(line, nread, 1, stdout) != 1)
> + goto out_fclose;
> fflush(stdout);
> }
>
> ret = 0;
> +out_fclose:
> fclose(in);
> + free(line);
> out:
> /* If in is defined, then fd is closed via fclose. */
> if (!in)
>
ok, thanks
jirka