arch, passt: Use executable link to form AVX2 binary path
...instead of argv[0], which might or might not contain a valid path to the executable itself. Instead of mangling argv[0], use the same link to find out if we're already running the AVX2 build where supported. Alternatively, we could use execvpe(), but that might result in running a different installed version, in case e.g. the set of binaries is present in both /usr/bin and /usr/local/bin, with both being in $PATH. Reported-by: Wenli Quan <wquan@redhat.com> Link: https://bugzilla.redhat.com/show_bug.cgi?id=2101310 Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
25
arch.c
25
arch.c
@@ -14,26 +14,31 @@
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* arch_avx2_exec() - Run AVX2 build if supported, drop suffix from argv[0]
|
||||
* arch_avx2_exec() - Switch to AVX2 build if supported
|
||||
* @argv: Arguments from command line
|
||||
*/
|
||||
#ifdef __x86_64__
|
||||
static char avx2_path[PATH_MAX];
|
||||
|
||||
void arch_avx2_exec(char **argv)
|
||||
{
|
||||
char *p = strstr(argv[0], ".avx2");
|
||||
char exe[PATH_MAX] = { 0 }, new_path[PATH_MAX + sizeof(".avx2")], *p;
|
||||
|
||||
if (p) {
|
||||
*p = 0;
|
||||
} else if (__builtin_cpu_supports("avx2")) {
|
||||
snprintf(avx2_path, PATH_MAX, "%s.avx2", argv[0]);
|
||||
argv[0] = avx2_path;
|
||||
execve(avx2_path, argv, environ);
|
||||
if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) {
|
||||
perror("readlink /proc/self/exe");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
p = strstr(exe, ".avx2");
|
||||
if (p && strlen(p) == strlen(".avx2"))
|
||||
return;
|
||||
|
||||
if (__builtin_cpu_supports("avx2")) {
|
||||
snprintf(new_path, PATH_MAX + sizeof(".avx2"), "%s.avx2", exe);
|
||||
execve(new_path, argv, environ);
|
||||
perror("Can't run AVX2 build, using non-AVX2 version");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user