xref: /linux/tools/perf/util/namespaces.h (revision 58f6259b7a08f8d47d4629609703d358b042f0fd)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *
4  * Copyright (C) 2017 Hari Bathini, IBM Corporation
5  */
6 
7 #ifndef __PERF_NAMESPACES_H
8 #define __PERF_NAMESPACES_H
9 
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <linux/stddef.h>
13 #include <linux/perf_event.h>
14 #include <linux/refcount.h>
15 #include <linux/types.h>
16 #include <internal/rc_check.h>
17 
18 #ifndef HAVE_SETNS_SUPPORT
19 int setns(int fd, int nstype);
20 #endif
21 
22 struct perf_record_namespaces;
23 
24 struct namespaces {
25 	struct list_head list;
26 	u64 end_time;
27 	struct perf_ns_link_info link_info[];
28 };
29 
30 struct namespaces *namespaces__new(struct perf_record_namespaces *event);
31 void namespaces__free(struct namespaces *namespaces);
32 
33 DECLARE_RC_STRUCT(nsinfo) {
34 	pid_t			pid;
35 	pid_t			tgid;
36 	pid_t			nstgid;
37 	bool			need_setns;
38 	bool			in_pidns;
39 	char			*mntns_path;
40 	refcount_t		refcnt;
41 };
42 
43 struct nscookie {
44 	int			oldns;
45 	int			newns;
46 	char			*oldcwd;
47 };
48 
49 int nsinfo__init(struct nsinfo *nsi);
50 struct nsinfo *nsinfo__new(pid_t pid);
51 struct nsinfo *nsinfo__copy(const struct nsinfo *nsi);
52 
53 struct nsinfo *nsinfo__get(struct nsinfo *nsi);
54 void nsinfo__put(struct nsinfo *nsi);
55 
56 bool nsinfo__need_setns(const struct nsinfo *nsi);
57 void nsinfo__clear_need_setns(struct nsinfo *nsi);
58 pid_t nsinfo__tgid(const struct nsinfo  *nsi);
59 pid_t nsinfo__nstgid(const struct nsinfo  *nsi);
60 pid_t nsinfo__pid(const struct nsinfo  *nsi);
61 pid_t nsinfo__in_pidns(const struct nsinfo  *nsi);
62 
63 void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc);
64 void nsinfo__mountns_exit(struct nscookie *nc);
65 
66 char *nsinfo__realpath(const char *path, struct nsinfo *nsi);
67 int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi);
68 
69 bool nsinfo__is_in_root_namespace(void);
70 
71 static inline void __nsinfo__zput(struct nsinfo **nsip)
72 {
73 	if (nsip) {
74 		nsinfo__put(*nsip);
75 		*nsip = NULL;
76 	}
77 }
78 
79 #define nsinfo__zput(nsi) __nsinfo__zput(&nsi)
80 
81 const char *perf_ns__name(unsigned int id);
82 
83 #endif  /* __PERF_NAMESPACES_H */
84