MDEV-34340 move get_exepath to mysys as my_get_exepath

Original implementation was from MDEV-13466
(commit d471469bd2)
This commit is contained in:
Daniel Black 2024-09-24 08:58:16 +10:00
parent e9c71f3a25
commit 171c4aa479
4 changed files with 62 additions and 29 deletions

View file

@ -6792,7 +6792,6 @@ void handle_options(int argc, char **argv, char ***argv_server,
}
static int main_low(char** argv);
static int get_exepath(char *buf, size_t size, const char *argv0);
/* ================= main =================== */
int main(int argc, char **argv)
@ -6803,8 +6802,8 @@ int main(int argc, char **argv)
my_getopt_prefix_matching= 0;
if (get_exepath(mariabackup_exe,FN_REFLEN, argv[0]))
strncpy(mariabackup_exe,argv[0], FN_REFLEN-1);
if (my_get_exepath(mariabackup_exe, FN_REFLEN, argv[0]))
strncpy(mariabackup_exe, argv[0], FN_REFLEN-1);
if (argc > 1 )
@ -7101,32 +7100,6 @@ static int main_low(char** argv)
}
static int get_exepath(char *buf, size_t size, const char *argv0)
{
#ifdef _WIN32
DWORD ret = GetModuleFileNameA(NULL, buf, (DWORD)size);
if (ret > 0)
return 0;
#elif defined(__linux__)
ssize_t ret = readlink("/proc/self/exe", buf, size-1);
if(ret > 0)
return 0;
#elif defined(__APPLE__)
size_t ret = proc_pidpath(getpid(), buf, static_cast<uint32_t>(size));
if (ret > 0) {
buf[ret] = 0;
return 0;
}
#elif defined(__FreeBSD__)
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
if (sysctl(mib, 4, buf, &size, NULL, 0) == 0) {
return 0;
}
#endif
return my_realpath(buf, argv0, 0);
}
#if defined (__SANITIZE_ADDRESS__) && defined (__linux__)
/* Avoid LeakSanitizer's false positives. */

View file

@ -640,6 +640,8 @@ extern size_t my_fwrite(FILE *stream,const uchar *Buffer,size_t Count,
extern my_off_t my_fseek(FILE *stream,my_off_t pos,int whence,myf MyFlags);
extern my_off_t my_ftell(FILE *stream,myf MyFlags);
extern int my_get_exepath(char *buf, size_t size, const char *argv0);
/* implemented in my_memmem.c */
extern void *my_memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen);

View file

@ -27,6 +27,7 @@ SET(MYSYS_SOURCES array.c charset-def.c charset.c my_default.c
my_alloc.c my_bit.c my_bitmap.c my_chsize.c
my_compress.c my_copy.c my_create.c my_delete.c
my_div.c my_error.c my_file.c my_fopen.c my_fstream.c
my_getexe.c
my_gethwaddr.c my_getopt.c my_getsystime.c my_getwd.c my_compare.c my_init.c
my_lib.c my_lock.c my_malloc.c my_mess.c
my_mkdir.c my_mmap.c my_once.c my_open.c my_pread.c my_pthread.c

57
mysys/my_getexe.c Normal file
View file

@ -0,0 +1,57 @@
/* Copyright (c) 2017, MariaDB Plc
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
#include <my_global.h>
#include <my_sys.h>
#ifdef _WIN32
#elif defined(__linux__)
#include <unistd.h>
#elif defined(__APPLE__)
#include <libproc.h>
#elif defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif
/** Fill the buffer with the executable program name
@param[out] buf buffer to place obtained executable name.
@param[in] size size available in the buffer.
@param[in] argv0 the argv0 from which the executable argv might be used.
@return 0, for successful filling of buf, non-zero for failure. */
int my_get_exepath(char *buf, size_t size, const char *argv0)
{
#ifdef _WIN32
DWORD ret = GetModuleFileNameA(NULL, buf, (DWORD)size);
if (ret > 0)
return 0;
#elif defined(__linux__)
ssize_t ret = readlink("/proc/self/exe", buf, size-1);
if(ret > 0)
return 0;
#elif defined(__APPLE__)
size_t ret = proc_pidpath(getpid(), buf, (uint32_t)size);
if (ret > 0) {
buf[ret] = 0;
return 0;
}
#elif defined(__FreeBSD__)
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
if (sysctl(mib, 4, buf, &size, NULL, 0) == 0) {
return 0;
}
#endif
return my_realpath(buf, argv0, 0);
}