diff options
author | Andrew G. Morgan <agm@google.com> | 2021-10-01 18:16:57 +0000 |
---|---|---|
committer | Andrew G. Morgan <morgan@kernel.org> | 2021-10-01 19:13:36 -0700 |
commit | 6c0e37a5dc19842ba7c8111611789b0297e2ea1a (patch) | |
tree | c25d83b5645f8ad51639fa6611948c69b1c8f75a | |
parent | d07df10aaa02cf8cfe8047610d2f56dc0be68f1e (diff) | |
download | libcap-6c0e37a5dc19842ba7c8111611789b0297e2ea1a.tar.gz |
Lower the start up memory allocation overhead.
In the vast majority of cases, code will not need to
override the "/proc" root directory, so treat NULL
as equivalent to "/proc".
Signed-off-by: Andrew G. Morgan <agm@google.com>
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
-rw-r--r-- | libcap/cap_alloc.c | 1 | ||||
-rw-r--r-- | libcap/cap_test.c | 2 | ||||
-rw-r--r-- | libcap/cap_text.c | 10 | ||||
-rw-r--r-- | libcap/include/sys/capability.h | 10 |
4 files changed, 16 insertions, 7 deletions
diff --git a/libcap/cap_alloc.c b/libcap/cap_alloc.c index 4dabe27..f77dec3 100644 --- a/libcap/cap_alloc.c +++ b/libcap/cap_alloc.c @@ -19,7 +19,6 @@ __attribute__((constructor (300))) static void _initialize_libcap(void) } cap_set_syscall(NULL, NULL); _binary_search(_cap_max_bits, cap_get_bound, 0, __CAP_MAXBITS, __CAP_BITS); - cap_proc_root("/proc"); } cap_value_t cap_max_bits(void) diff --git a/libcap/cap_test.c b/libcap/cap_test.c index 2c068aa..b523091 100644 --- a/libcap/cap_test.c +++ b/libcap/cap_test.c @@ -171,7 +171,7 @@ static int test_alloc(void) } old_root = cap_proc_root("blah"); - if (old_root == NULL || strcmp(old_root, "/proc") != 0) { + if (old_root != NULL) { printf("bad initial proc_root [%s]\n", old_root); fflush(stdout); retval = -1; diff --git a/libcap/cap_text.c b/libcap/cap_text.c index 1004aae..40bd59a 100644 --- a/libcap/cap_text.c +++ b/libcap/cap_text.c @@ -669,7 +669,7 @@ static __u32 _parse_vec_string(__u32 *vals, const char *c, int invert) /* * libcap believes this is the root of the mounted "/proc" - * filesystem + * filesystem. (NULL == "/proc".) */ static char *_cap_proc_dir; @@ -695,6 +695,8 @@ __attribute__((destructor (300))) static void _cleanup_libcap(void) * memory for storing the replacement root, and it is this memory that * is returned. So, when changing the value, the caller should * cap_free(the-return-value) when done with it. + * + * A return value of NULL implies the default is in effect "/proc". */ char *cap_proc_root(const char *root) { @@ -717,8 +719,12 @@ cap_iab_t cap_iab_get_pid(pid_t pid) char *path; FILE *file; char line[PROC_LINE_MAX]; + const char *proc_root = _cap_proc_dir; - if (asprintf(&path, "%s/%d/status", _cap_proc_dir, pid) <= 0) { + if (proc_root == NULL) { + proc_root = "/proc"; + } + if (asprintf(&path, "%s/%d/status", proc_root, pid) <= 0) { return NULL; } file = fopen(path, "r"); diff --git a/libcap/include/sys/capability.h b/libcap/include/sys/capability.h index 59f9377..80171b0 100644 --- a/libcap/include/sys/capability.h +++ b/libcap/include/sys/capability.h @@ -55,8 +55,9 @@ extern cap_value_t cap_max_bits(void); /* * cap_proc_root reads and (optionally: when root != NULL) changes - * libcap's notion of where the "/proc" filesystem is mounted. It - * defaults to the value "/proc". + * libcap's notion of where the "/proc" filesystem is mounted. When + * the return value is NULL, it should be interpreted as the + * value "/proc". * * Note, this is a global value and not considered thread safe to * write - so the client should take suitable care when changing @@ -65,7 +66,10 @@ extern cap_value_t cap_max_bits(void); * Further, libcap will allocate a memory copy for storing the * replacement root, and it is this kind of memory that is returned. * So, when changing the value, the caller should - * cap_free(the-return-value) when done with it. + * cap_free(the-return-value) else cause a memory leak. + * + * Note, the library uses a destructor to clean up the live allocated + * value of the working setting. */ extern char *cap_proc_root(const char *root); |