[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240627-fix-help-issue-v3-1-85318a3974e4@gmail.com>
Date: Thu, 27 Jun 2024 09:49:37 +0200
From: Roman Storozhenko <romeusmeister@...il.com>
To: Thomas Renninger <trenn@...e.com>, Shuah Khan <shuah@...nel.org>
Cc: Javier Carrasco <javier.carrasco.cruz@...il.com>,
linux-pm@...r.kernel.org, linux-kernel@...r.kernel.org,
Roman Storozhenko <romeusmeister@...il.com>
Subject: [PATCH v3] cpupower: Make help command available for custom
install dir
When the 'cpupower' utility installed in the custom dir, it fails to
render appropriate help info for a particular subcommand:
$ LD_LIBRARY_PATH=lib64/ bin/cpupower help monitor
with error message like 'No manual entry for cpupower-monitor.1'
The issue is that under the hood it calls 'exec' function with
the following args: 'man cpupower-monitor.1'. In turn, 'man' search
path is defined in '/etc/manpath.config'. Of course it contains only
standard system man paths.
Make subcommands help available for a user by setting up 'MANPATH'
environment variable to the custom installation man pages dir. That
variable value will be prepended to the man pages standard search paths
as described in 'SEARCH PATH' section of MANPATH(5).
Signed-off-by: Roman Storozhenko <romeusmeister@...il.com>
---
Changes in v3:
- fix: append the previous value of "MANPATH" to a new one instead of
its replacement
- Link to v2: https://lore.kernel.org/r/20240622-fix-help-issue-v2-1-6c19e28a4ec1@gmail.com
Changes in v2:
- Fixed spelling errors
- Simplified man pages search approach by the 'MANPATH' variable usage
- Link to v1: https://lore.kernel.org/r/20240621-fix-help-issue-v1-1-7906998d46eb@gmail.com
---
tools/power/cpupower/utils/cpupower.c | 43 ++++++++++++++++++++++++++++++-----
1 file changed, 37 insertions(+), 6 deletions(-)
diff --git a/tools/power/cpupower/utils/cpupower.c b/tools/power/cpupower/utils/cpupower.c
index 9ec973165af1..a7777e693fa7 100644
--- a/tools/power/cpupower/utils/cpupower.c
+++ b/tools/power/cpupower/utils/cpupower.c
@@ -12,6 +12,8 @@
#include <unistd.h>
#include <errno.h>
#include <sched.h>
+#include <libgen.h>
+#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
@@ -80,14 +82,17 @@ static void print_help(void)
static int print_man_page(const char *subpage)
{
- int len;
- char *page;
+ char *page, *man_path, *exec_dir, *man_env;
+ char exec_path[PATH_MAX];
+ int subpage_len;
- len = 10; /* enough for "cpupower-" */
- if (subpage != NULL)
- len += strlen(subpage);
+ if (!subpage)
+ return -EINVAL;
- page = malloc(len);
+ subpage_len = 10; /* enough for "cpupower-" */
+ subpage_len += strlen(subpage);
+
+ page = malloc(subpage_len);
if (!page)
return -ENOMEM;
@@ -97,6 +102,32 @@ static int print_man_page(const char *subpage)
strcat(page, subpage);
}
+ /* Get current process image name full path */
+ if (readlink("/proc/self/exe", exec_path, PATH_MAX) > 0) {
+ exec_dir = strdup(exec_path);
+ if (!exec_dir) {
+ free(page);
+ free(man_path);
+ return -ENOMEM;
+ }
+
+ /* Prepend standard search path for man pages with relative path
+ * to custom install man directory
+ */
+ man_env = getenv("MANPATH");
+ if (asprintf(&man_path, "%s/../man:%s",
+ dirname(exec_dir), (man_env ? man_env : "")) > 0) {
+ setenv("MANPATH", man_path, 1);
+ free(exec_dir);
+ free(man_path);
+ } else {
+ free(page);
+ free(exec_dir);
+ free(man_path);
+ return -ENOMEM;
+ }
+ }
+
execlp("man", "man", page, NULL);
/* should not be reached */
---
base-commit: 2102cb0d050d34d50b9642a3a50861787527e922
change-id: 20240619-fix-help-issue-573c40bb6427
Best regards,
--
Roman Storozhenko <romeusmeister@...il.com>
Powered by blists - more mailing lists