All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Bobrowski via ltp <ltp@lists.linux.it>
To: Amir Goldstein <amir73il@gmail.com>
Cc: Jan Kara <jack@suse.cz>, LTP List <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode
Date: Tue, 2 Nov 2021 08:16:18 +1100	[thread overview]
Message-ID: <YYBZIpBiUYPTMdXK@google.com> (raw)
In-Reply-To: <CAOQ4uxgnKLO7UuzyGsKr1Nz035tAOrm_JDyqbvdyOE5vbdc=MQ@mail.gmail.com>

On Wed, Oct 27, 2021 at 01:35:53PM +0300, Amir Goldstein wrote:
> On Wed, Oct 27, 2021 at 12:45 PM Matthew Bobrowski <repnop@google.com> wrote:
> >
> > A new test that performs verification on the values returned within the
> > struct fanotify_event_info_pidfd record when notification group intialized
> > in FAN_REPORT_PIDFD mode.
> >
> > Signed-off-by: Matthew Bobrowski <repnop@google.com>
> > ---
> >  testcases/kernel/syscalls/fanotify/.gitignore |   1 +
> >  .../kernel/syscalls/fanotify/fanotify21.c     | 363 ++++++++++++++++++
> >  2 files changed, 364 insertions(+)
> >  create mode 100644 testcases/kernel/syscalls/fanotify/fanotify21.c
> >
> > diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore
> > index c99e6fff7..35e73b91e 100644
> > --- a/testcases/kernel/syscalls/fanotify/.gitignore
> > +++ b/testcases/kernel/syscalls/fanotify/.gitignore
> > @@ -18,4 +18,5 @@
> >  /fanotify18
> >  /fanotify19
> >  /fanotify20
> > +/fanotify21
> >  /fanotify_child
> > diff --git a/testcases/kernel/syscalls/fanotify/fanotify21.c b/testcases/kernel/syscalls/fanotify/fanotify21.c
> > new file mode 100644
> > index 000000000..f64f8fef4
> > --- /dev/null
> > +++ b/testcases/kernel/syscalls/fanotify/fanotify21.c
> > @@ -0,0 +1,363 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*
> > + * Copyright (c) 2021 Google. All Rights Reserved.
> > + *
> > + * Started by Matthew Bobrowski <repnop@google.com>
> > + */
> > +
> > +/*\
> > + * [Description]
> > + *
> > + * A test which verifies whether the returned struct
> > + * fanotify_event_info_pidfd in FAN_REPORT_PIDFD mode contains the
> > + * expected set of information.
> > + */
> > +
> > +#define _GNU_SOURCE
> > +#include <stdio.h>
> > +#include <ctype.h>
> > +#include <stdlib.h>
> > +#include <string.h>
> > +#include "tst_test.h"
> > +#include "tst_safe_stdio.h"
> > +#include "lapi/pidfd_open.h"
> > +
> > +#ifdef HAVE_SYS_FANOTIFY_H
> > +#include "fanotify.h"
> > +
> > +#define BUF_SZ         4096
> > +#define MOUNT_PATH     "fs_mnt"
> > +#define TEST_FILE      MOUNT_PATH "/testfile"
> > +
> > +struct pidfd_fdinfo_t {
> > +       int pos;
> > +       int flags;
> > +       int mnt_id;
> > +       int pid;
> > +       int ns_pid;
> > +};
> > +
> > +struct test_case_t {
> > +       char *name;
> > +       int fork;
> > +       int want_pidfd_err;
> > +} test_cases[] = {
> > +       {
> > +               "return a valid pidfd for event created by self",
> > +               0,
> > +               0,
> > +       },
> > +       {
> > +               "return invalid pidfd for event created by terminated child",
> > +               1,
> > +               FAN_NOPIDFD,
> > +       },
> > +};
> > +
> > +static int fanotify_fd;
> > +static char event_buf[BUF_SZ];
> > +static struct pidfd_fdinfo_t *self_pidfd_fdinfo = NULL;
> > +
> > +static char *trim(char *line)
> > +{
> > +       char *start = line;
> > +       char *end = line + strlen(line);
> > +
> > +       while(*start && isspace(*start))
> > +               start++;
> > +
> > +       while(end > start && isspace(*(end - 1)))
> > +               end--;
> > +
> > +       *end = '\0';
> > +       return start;
> > +}
> > +
> > +static int parse_pidfd_fdinfo_line(char *line,
> > +                                  struct pidfd_fdinfo_t *pidfd_fdinfo)
> > +{
> > +       char *ptr, *key, *value;
> > +
> > +       ptr = strchr(line, ':');
> > +       if (ptr == NULL)
> > +               return -1;
> > +
> > +       *ptr++ = '\0';
> > +       key = trim(line);
> > +       value = trim(ptr);
> > +
> > +       /*
> > +        * Ensure to cover all keys of interest that may be found within the
> > +        * pidfd fdinfo. If we encounter an unexpected key, skip it.
> > +        */
> > +       if (strcmp(key, "pos") == 0)
> > +               pidfd_fdinfo->pos = atoi(value);
> > +       else if (strcmp(key, "flags") == 0)
> > +               pidfd_fdinfo->flags = (int)strtol(value, NULL, 16);
> > +       else if (strcmp(key, "mnt_id") == 0)
> > +               pidfd_fdinfo->mnt_id = atoi(value);
> > +       else if (strcmp(key, "Pid") == 0)
> > +               pidfd_fdinfo->pid = atoi(value);
> > +       else if (strcmp(key, "NSpid") == 0)
> > +               pidfd_fdinfo->ns_pid = atoi(value);
> > +
> > +       return 0;
> > +}
> 
> Please use existing LTP parsing utilities.
> It's never a good idea to re-implement these sort of things.
> With a quick grep I found:
> 
> SAFE_FILE_LINES_SCANF("/proc/meminfo", "SwapCached: %ld",
> 
> Otherwise, test looks fine to me.

Ah, wonderful. I wasn't aware that this helper had existed. A failure
on my part for not properly checking. I think this will work the
treat!

/M

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

      reply	other threads:[~2021-11-01 21:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-27  9:44 [LTP] [PATCH 0/2] Test support for new fanotify FAN_REPORT_PIDFD feature Matthew Bobrowski via ltp
2021-10-27  9:44 ` [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD Matthew Bobrowski via ltp
2021-10-27 10:24   ` Amir Goldstein
2021-10-29 15:04     ` Gabriel Krisman Bertazi
2021-11-01 21:09       ` Matthew Bobrowski via ltp
2021-11-01 21:05     ` Matthew Bobrowski via ltp
2021-10-27  9:45 ` [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode Matthew Bobrowski via ltp
2021-10-27 10:35   ` Amir Goldstein
2021-11-01 21:16     ` Matthew Bobrowski via ltp [this message]

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=YYBZIpBiUYPTMdXK@google.com \
    --to=ltp@lists.linux.it \
    --cc=amir73il@gmail.com \
    --cc=jack@suse.cz \
    --cc=repnop@google.com \
    /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.