[LTP] [PATCH v4 1/2] tst_device.c: Add tst_is_mounted_ro/w check mount option

Li Wang liwang@redhat.com
Fri Aug 22 09:01:10 CEST 2025


On Fri, Aug 22, 2025 at 2:33 PM Wei Gao <wegao@suse.com> wrote:

> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
>  include/tst_device.h |  2 ++
>  lib/tst_device.c     | 48 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 50 insertions(+)
>
> diff --git a/include/tst_device.h b/include/tst_device.h
> index 2597fb4e2..3ea7b5500 100644
> --- a/include/tst_device.h
> +++ b/include/tst_device.h
> @@ -34,6 +34,8 @@ int tst_umount(const char *path);
>   */
>  int tst_is_mounted(const char *path);
>  int tst_is_mounted_at_tmpdir(const char *path);
> +int tst_is_mounted_ro(const char *path);
> +int tst_is_mounted_rw(const char *path);
>
>  /*
>   * Clears a first few blocks of the device. This is needed when device has
> diff --git a/lib/tst_device.c b/lib/tst_device.c
> index 6d1abf065..7f9a2b2f6 100644
> --- a/lib/tst_device.c
> +++ b/lib/tst_device.c
> @@ -473,6 +473,54 @@ int tst_is_mounted_at_tmpdir(const char *path)
>         return tst_is_mounted(mpath);
>  }
>
> +static int tst_mount_has_opt(const char *path, const char *opt)
> +{
> +       char line[PATH_MAX];
> +       FILE *file;
> +       int ret = 0;
> +       char abspath[PATH_MAX];
> +
> +       if (!realpath(path, abspath)) {
> +               tst_resm(TWARN | TERRNO, "realpath(%s) failed", path);
> +               return 0;
> +       }
> +
> +       file = SAFE_FOPEN(NULL, "/proc/mounts", "r");
> +
> +       while (fgets(line, sizeof(line), file)) {
> +               char mount_point[PATH_MAX], options[PATH_MAX];
> +
> +               if (sscanf(line, "%*s %s %*s %s", mount_point, options) <
> 2)
> +                       continue;
> +
> +               if (strcmp(mount_point, abspath) != 0)
> +                       continue;
> +
> +               char *tok = strtok(options, ",");
> +               while (tok) {
> +                       if (strcmp(tok, opt) == 0) {
> +                               ret = 1;
> +                               break;
> +                       }
> +                       tok = strtok(NULL, ",");
> +               }
> +               if (ret)
> +                       break;
> +       }
> +
>

Patch set pushed with minor adjustments, thank you.

+       SAFE_FCLOSE(NULL, file);

+       return ret;
> +}
> +
> +int tst_is_mounted_ro(const char *path)
> +{
> +       return tst_mount_has_opt(path, "ro");
> +}
> +
> +int tst_is_mounted_rw(const char *path)
> +{
> +       return tst_mount_has_opt(path, "rw");
> +}
> +
>  static int find_stat_file(const char *dev, char *path, size_t path_len)
>  {
>         const char *devname = strrchr(dev, '/') + 1;
> --
> 2.43.0
>
>

-- 
Regards,
Li Wang


More information about the ltp mailing list