linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Leo Yan <leo.yan@linaro.org>
Cc: Jiri Olsa <jolsa@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>, Jin Yao <yao.jin@linux.intel.com>,
	John Garry <john.garry@huawei.com>,
	Yonatan Goldschmidt <yonatan.goldschmidt@granulate.io>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH v1 1/2] perf namespaces: Add helper nsinfo__is_in_root_namespace()
Date: Mon, 13 Dec 2021 15:20:30 -0300	[thread overview]
Message-ID: <YbeO7sHHL0AvDl5g@kernel.org> (raw)
In-Reply-To: <20211212134721.1721245-2-leo.yan@linaro.org>

Em Sun, Dec 12, 2021 at 09:47:20PM +0800, Leo Yan escreveu:
> Refactors code for gathering PID infos, it creates the function
> nsinfo__get_nspid() to parse process 'status' node in folder '/proc'.
> 
> Base on the refactoring, this patch introduces a new helper
> nsinfo__is_in_root_namespace(), it returns true when the caller runs in
> the root PID namespace.

Thanks, applied.

- Arnaldo

 
> Signed-off-by: Leo Yan <leo.yan@linaro.org>
> ---
>  tools/perf/util/namespaces.c | 76 ++++++++++++++++++++++--------------
>  tools/perf/util/namespaces.h |  2 +
>  2 files changed, 48 insertions(+), 30 deletions(-)
> 
> diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
> index 608b20c72a5c..48aa3217300b 100644
> --- a/tools/perf/util/namespaces.c
> +++ b/tools/perf/util/namespaces.c
> @@ -60,17 +60,49 @@ void namespaces__free(struct namespaces *namespaces)
>  	free(namespaces);
>  }
>  
> +static int nsinfo__get_nspid(struct nsinfo *nsi, const char *path)
> +{
> +	FILE *f = NULL;
> +	char *statln = NULL;
> +	size_t linesz = 0;
> +	char *nspid;
> +
> +	f = fopen(path, "r");
> +	if (f == NULL)
> +		return -1;
> +
> +	while (getline(&statln, &linesz, f) != -1) {
> +		/* Use tgid if CONFIG_PID_NS is not defined. */
> +		if (strstr(statln, "Tgid:") != NULL) {
> +			nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
> +						     NULL, 10);
> +			nsi->nstgid = nsi->tgid;
> +		}
> +
> +		if (strstr(statln, "NStgid:") != NULL) {
> +			nspid = strrchr(statln, '\t');
> +			nsi->nstgid = (pid_t)strtol(nspid, NULL, 10);
> +			/*
> +			 * If innermost tgid is not the first, process is in a different
> +			 * PID namespace.
> +			 */
> +			nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
> +			break;
> +		}
> +	}
> +
> +	fclose(f);
> +	free(statln);
> +	return 0;
> +}
> +
>  int nsinfo__init(struct nsinfo *nsi)
>  {
>  	char oldns[PATH_MAX];
>  	char spath[PATH_MAX];
>  	char *newns = NULL;
> -	char *statln = NULL;
> -	char *nspid;
>  	struct stat old_stat;
>  	struct stat new_stat;
> -	FILE *f = NULL;
> -	size_t linesz = 0;
>  	int rv = -1;
>  
>  	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
> @@ -100,34 +132,9 @@ int nsinfo__init(struct nsinfo *nsi)
>  	if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
>  		goto out;
>  
> -	f = fopen(spath, "r");
> -	if (f == NULL)
> -		goto out;
> -
> -	while (getline(&statln, &linesz, f) != -1) {
> -		/* Use tgid if CONFIG_PID_NS is not defined. */
> -		if (strstr(statln, "Tgid:") != NULL) {
> -			nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
> -						     NULL, 10);
> -			nsi->nstgid = nsi->tgid;
> -		}
> -
> -		if (strstr(statln, "NStgid:") != NULL) {
> -			nspid = strrchr(statln, '\t');
> -			nsi->nstgid = (pid_t)strtol(nspid, NULL, 10);
> -			/* If innermost tgid is not the first, process is in a different
> -			 * PID namespace.
> -			 */
> -			nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
> -			break;
> -		}
> -	}
> -	rv = 0;
> +	rv = nsinfo__get_nspid(nsi, spath);
>  
>  out:
> -	if (f != NULL)
> -		(void) fclose(f);
> -	free(statln);
>  	free(newns);
>  	return rv;
>  }
> @@ -299,3 +306,12 @@ int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi)
>  
>  	return ret;
>  }
> +
> +bool nsinfo__is_in_root_namespace(void)
> +{
> +	struct nsinfo nsi;
> +
> +	memset(&nsi, 0x0, sizeof(nsi));
> +	nsinfo__get_nspid(&nsi, "/proc/self/status");
> +	return !nsi.in_pidns;
> +}
> diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
> index ad9775db7b9c..9ceea9643507 100644
> --- a/tools/perf/util/namespaces.h
> +++ b/tools/perf/util/namespaces.h
> @@ -59,6 +59,8 @@ void nsinfo__mountns_exit(struct nscookie *nc);
>  char *nsinfo__realpath(const char *path, struct nsinfo *nsi);
>  int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi);
>  
> +bool nsinfo__is_in_root_namespace(void);
> +
>  static inline void __nsinfo__zput(struct nsinfo **nsip)
>  {
>  	if (nsip) {
> -- 
> 2.25.1

-- 

- Arnaldo

  reply	other threads:[~2021-12-13 18:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-12 13:47 [PATCH v1 0/2] perf: Fix perf in non-root PID namespace Leo Yan
2021-12-12 13:47 ` [PATCH v1 1/2] perf namespaces: Add helper nsinfo__is_in_root_namespace() Leo Yan
2021-12-13 18:20   ` Arnaldo Carvalho de Melo [this message]
2021-12-12 13:47 ` [PATCH v1 2/2] perf evlist: Don't run perf in non-root PID namespace when launch workload Leo Yan
2021-12-13 13:54   ` James Clark
2021-12-14  3:24     ` Leo Yan

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=YbeO7sHHL0AvDl5g@kernel.org \
    --to=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=john.garry@huawei.com \
    --cc=jolsa@redhat.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=leo.yan@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=songliubraving@fb.com \
    --cc=yao.jin@linux.intel.com \
    --cc=yhs@fb.com \
    --cc=yonatan.goldschmidt@granulate.io \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).