Skip to content

Commit f3e2352

Browse files
refactor(driver): reorg get_first_runpath -> get_runpath -> get_origin
* On any platform, we only need implement get_origin() and get_runpath() two functions. * These two functions both return a char * allocated by malloc(). * If no way to get RUNPATH, only implementing get_origin() is acceptable. Signed-off-by: Coelacanthus <uwu@coelacanthus.name>
1 parent eb11aef commit f3e2352

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

driver/driver.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static char *get_origin() {
187187
*/
188188
#if defined(RTLD_DI_LINKMAP) || defined(__GLIBC__) || defined(__linux__) || defined(__FreeBSD__) \
189189
|| (defined(__sun) && defined(__SVR4))
190-
static const char *get_runpath() {
190+
static char *get_runpath() {
191191
void *handle = dlopen(NULL, RTLD_NOW);
192192
if (handle == NULL) {
193193
return NULL;
@@ -210,14 +210,14 @@ static const char *get_runpath() {
210210
}
211211
}
212212
if (runpath != NULL && strtab != NULL) {
213-
return strtab + runpath->d_un.d_val;
213+
return strdup(strtab + runpath->d_un.d_val);
214214
} else {
215215
return NULL;
216216
}
217217
}
218218
// If DT_RUNPATH is not accessable, use the first search path as alternative.
219-
#elif defined(__unix__) || defined(__unix)
220-
static const char *get_runpath() {
219+
#elif defined(RTLD_DI_SERINFO) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
220+
static char *get_runpath() {
221221
void *handle = dlopen(NULL, RTLD_NOW);
222222
if (handle == NULL) {
223223
return NULL;
@@ -248,19 +248,15 @@ static const char *get_runpath() {
248248
return runpath;
249249
}
250250
#else
251-
// FIXME: I still didn't find the way to determine RUNPATH on non-Glibc/Musl/Soloris.
252-
// So use $ORIGIN as a temporary solution
253-
static char *get_runpath() { return get_origin(); }
251+
static char *get_runpath() { return strdup("$ORIGIN"); }
254252
#endif
255253

256-
static char *get_first_runpath(const char *runpath) {
254+
static char *get_first_runpath() {
255+
_cleanup_free_ char *runpath = get_runpath();
257256
if (runpath == NULL)
258257
return NULL;
259-
_cleanup_free_ char *runpath1 = strdup(runpath);
260-
if (runpath1 == NULL)
261-
return NULL;
262258
// runpath is not empty, so strtok shouldn't return NULL when first call.
263-
char *tmp = strtok(runpath1, ":");
259+
char *tmp = strtok(runpath, ":");
264260
if (!strcmp(tmp, "$ORIGIN") || !strcmp(tmp, "@loader_path")) {
265261
return get_origin();
266262
} else {
@@ -269,7 +265,7 @@ static char *get_first_runpath(const char *runpath) {
269265
}
270266

271267
static char *get_driver_path() {
272-
_cleanup_free_ char *LPAC_DRIVER_HOME = get_first_runpath(get_runpath());
268+
_cleanup_free_ char *LPAC_DRIVER_HOME = get_first_runpath();
273269
if (LPAC_DRIVER_HOME == NULL)
274270
return NULL;
275271
return path_concat(LPAC_DRIVER_HOME, "driver");

0 commit comments

Comments
 (0)