All of lore.kernel.org
 help / color / mirror / Atom feed
From: James Carter <jwcart2@gmail.com>
To: Vit Mojzis <vmojzis@redhat.com>
Cc: selinux@vger.kernel.org
Subject: Re: [PATCH] python: Hide error message when modifying non-local fcontext
Date: Wed, 19 Oct 2022 15:06:58 -0400	[thread overview]
Message-ID: <CAP+JOzSmdtMH=3kHowRpvXwYVhELXvnyz2y6arD6Amh3ziSRtA@mail.gmail.com> (raw)
In-Reply-To: <20220914220337.165813-1-vmojzis@redhat.com>

On Wed, Sep 14, 2022 at 6:08 PM Vit Mojzis <vmojzis@redhat.com> wrote:
>
> Fixes:
>   # semanage fcontext -f f -m -t passwd_file_t /etc/security/opasswd
>   libsemanage.dbase_llist_query: could not query record value (No such file or directory).
>
> Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
> ---
>  python/semanage/seobject.py | 19 ++++++++++++++++++-
>  1 file changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py
> index 0782c082..2d52f53c 100644
> --- a/python/semanage/seobject.py
> +++ b/python/semanage/seobject.py
> @@ -177,6 +177,22 @@ except (OSError, ImportError):
>              for l in self.log_list:
>                  syslog.syslog(syslog.LOG_INFO, message + l)
>
> +# Define a context manager to suppress stderr.
> +class suppress_stderr(object):
> +    def __init__(self):
> +        # Open a /dev/null file to be used as stderr
> +        self.null =  os.open(os.devnull,os.O_RDWR)
> +        self.save_fd = os.dup(2)
> +
> +    def __enter__(self):
> +        # Set stderr to the null file
> +        os.dup2(self.null,2)
> +
> +    def __exit__(self, *_):
> +        # Restore stderr
> +        os.dup2(self.save_fd,2)
> +        os.close(self.null)
> +
>
>  class nulllogger:
>
> @@ -2510,7 +2526,8 @@ class fcontextRecords(semanageRecords):
>                  raise ValueError(_("File context for %s is not defined") % target)
>
>          try:
> -            (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
> +            with suppress_stderr():
> +                (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
>          except OSError:
>              try:
>                  (rc, fcontext) = semanage_fcontext_query(self.sh, k)
> --
> 2.35.3
>

I think it is better to rework semanage/seobject.py so that there is
no error. I sent a patch to the list that should fix this.
Thanks for the report. Sorry that it took so long to get to.
Jim

  reply	other threads:[~2022-10-19 19:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-14 22:03 [PATCH] python: Hide error message when modifying non-local fcontext Vit Mojzis
2022-10-19 19:06 ` James Carter [this message]
2022-10-20  8:16   ` Vit Mojzis

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='CAP+JOzSmdtMH=3kHowRpvXwYVhELXvnyz2y6arD6Amh3ziSRtA@mail.gmail.com' \
    --to=jwcart2@gmail.com \
    --cc=selinux@vger.kernel.org \
    --cc=vmojzis@redhat.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.