2001-08-28 07:47:48 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 2001 Institut National de Recherche en Informatique et */
|
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
2001-08-28 07:47:48 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
2004-01-03 12:55:41 -08:00
|
|
|
/* $Id$ */
|
2001-08-28 07:47:48 -07:00
|
|
|
|
|
|
|
/* Unix-specific stuff */
|
|
|
|
|
2007-11-10 08:32:20 -08:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
/* Helps finding RTLD_DEFAULT in glibc */
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "config.h"
|
|
|
|
#ifdef SUPPORT_DYNAMIC_LINKING
|
2008-04-22 05:40:14 -07:00
|
|
|
#ifdef __CYGWIN32__
|
2007-11-06 07:16:56 -08:00
|
|
|
#include "flexdll.h"
|
2003-01-09 00:42:13 -08:00
|
|
|
#else
|
2001-08-28 07:47:48 -07:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2003-01-09 00:42:13 -08:00
|
|
|
#endif
|
2002-04-26 06:33:25 -07:00
|
|
|
#ifdef HAS_UNISTD
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2003-03-03 09:16:15 -08:00
|
|
|
#ifdef HAS_DIRENT
|
|
|
|
#include <dirent.h>
|
|
|
|
#else
|
|
|
|
#include <sys/dir.h>
|
|
|
|
#endif
|
2001-08-28 07:47:48 -07:00
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "osdeps.h"
|
|
|
|
|
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
char * caml_decompose_path(struct ext_table * tbl, char * path)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
char * p, * q;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (path == NULL) return NULL;
|
2003-12-31 06:20:40 -08:00
|
|
|
p = caml_stat_alloc(strlen(path) + 1);
|
2001-08-28 07:47:48 -07:00
|
|
|
strcpy(p, path);
|
|
|
|
q = p;
|
|
|
|
while (1) {
|
|
|
|
for (n = 0; q[n] != 0 && q[n] != ':'; n++) /*nothing*/;
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_ext_table_add(tbl, q);
|
2001-08-28 07:47:48 -07:00
|
|
|
q = q + n;
|
|
|
|
if (*q == 0) break;
|
|
|
|
*q = 0;
|
|
|
|
q += 1;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
char * caml_search_in_path(struct ext_table * path, char * name)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
char * p, * fullname;
|
|
|
|
int i;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
for (p = name; *p != 0; p++) {
|
|
|
|
if (*p == '/') goto not_found;
|
|
|
|
}
|
|
|
|
for (i = 0; i < path->size; i++) {
|
2003-12-31 06:20:40 -08:00
|
|
|
fullname = caml_stat_alloc(strlen((char *)(path->contents[i])) +
|
|
|
|
strlen(name) + 2);
|
2001-08-28 07:47:48 -07:00
|
|
|
strcpy(fullname, (char *)(path->contents[i]));
|
|
|
|
if (fullname[0] != 0) strcat(fullname, "/");
|
|
|
|
strcat(fullname, name);
|
|
|
|
if (stat(fullname, &st) == 0 && S_ISREG(st.st_mode)) return fullname;
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_free(fullname);
|
2001-08-28 07:47:48 -07:00
|
|
|
}
|
|
|
|
not_found:
|
2003-12-31 06:20:40 -08:00
|
|
|
fullname = caml_stat_alloc(strlen(name) + 1);
|
2001-08-28 07:47:48 -07:00
|
|
|
strcpy(fullname, name);
|
|
|
|
return fullname;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __CYGWIN32__
|
|
|
|
|
|
|
|
/* Cygwin needs special treatment because of the implicit ".exe" at the
|
|
|
|
end of executable file names */
|
|
|
|
|
|
|
|
static int cygwin_file_exists(char * name)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
/* Cannot use stat() here because it adds ".exe" implicitly */
|
|
|
|
fd = open(name, O_RDONLY);
|
|
|
|
if (fd == -1) return 0;
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * cygwin_search_exe_in_path(struct ext_table * path, char * name)
|
|
|
|
{
|
|
|
|
char * p, * fullname;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (p = name; *p != 0; p++) {
|
|
|
|
if (*p == '/' || *p == '\\') goto not_found;
|
|
|
|
}
|
|
|
|
for (i = 0; i < path->size; i++) {
|
2003-12-31 06:20:40 -08:00
|
|
|
fullname = caml_stat_alloc(strlen((char *)(path->contents[i])) +
|
|
|
|
strlen(name) + 6);
|
2001-08-28 07:47:48 -07:00
|
|
|
strcpy(fullname, (char *)(path->contents[i]));
|
|
|
|
strcat(fullname, "/");
|
|
|
|
strcat(fullname, name);
|
|
|
|
if (cygwin_file_exists(fullname)) return fullname;
|
|
|
|
strcat(fullname, ".exe");
|
|
|
|
if (cygwin_file_exists(fullname)) return fullname;
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_free(fullname);
|
2001-08-28 07:47:48 -07:00
|
|
|
}
|
|
|
|
not_found:
|
2003-12-31 06:20:40 -08:00
|
|
|
fullname = caml_stat_alloc(strlen(name) + 5);
|
2001-08-28 07:47:48 -07:00
|
|
|
strcpy(fullname, name);
|
|
|
|
if (cygwin_file_exists(fullname)) return fullname;
|
|
|
|
strcat(fullname, ".exe");
|
|
|
|
if (cygwin_file_exists(fullname)) return fullname;
|
|
|
|
strcpy(fullname, name);
|
|
|
|
return fullname;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
char * caml_search_exe_in_path(char * name)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
struct ext_table path;
|
|
|
|
char * tofree;
|
|
|
|
char * res;
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_ext_table_init(&path, 8);
|
2004-01-01 08:42:43 -08:00
|
|
|
tofree = caml_decompose_path(&path, getenv("PATH"));
|
2001-08-28 07:47:48 -07:00
|
|
|
#ifndef __CYGWIN32__
|
2004-01-01 08:42:43 -08:00
|
|
|
res = caml_search_in_path(&path, name);
|
2001-08-28 07:47:48 -07:00
|
|
|
#else
|
|
|
|
res = cygwin_search_exe_in_path(&path, name);
|
|
|
|
#endif
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_free(tofree);
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_ext_table_free(&path, 0);
|
2001-08-28 07:47:48 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
char * caml_search_dll_in_path(struct ext_table * path, char * name)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
char * dllname = caml_stat_alloc(strlen(name) + 4);
|
2001-08-28 07:47:48 -07:00
|
|
|
char * res;
|
|
|
|
strcpy(dllname, name);
|
|
|
|
strcat(dllname, ".so");
|
2004-01-01 08:42:43 -08:00
|
|
|
res = caml_search_in_path(path, dllname);
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_free(dllname);
|
2001-08-28 07:47:48 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SUPPORT_DYNAMIC_LINKING
|
2008-04-22 05:40:14 -07:00
|
|
|
#ifdef __CYGWIN32__
|
2007-11-06 07:16:56 -08:00
|
|
|
/* Use flexdll */
|
|
|
|
|
2008-04-22 05:24:10 -07:00
|
|
|
void * caml_dlopen(char * libname, int for_execution, int global)
|
2007-11-06 07:16:56 -08:00
|
|
|
{
|
2008-04-22 05:24:10 -07:00
|
|
|
int flags = (global ? FLEXDLL_RTLD_GLOBAL : 0);
|
2007-11-06 07:16:56 -08:00
|
|
|
if (!for_execution) flags |= FLEXDLL_RTLD_NOEXEC;
|
|
|
|
return flexdll_dlopen(libname, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void caml_dlclose(void * handle)
|
|
|
|
{
|
|
|
|
flexdll_dlclose(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * caml_dlsym(void * handle, char * name)
|
|
|
|
{
|
|
|
|
return flexdll_dlsym(handle, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * caml_globalsym(char * name)
|
|
|
|
{
|
2008-04-22 05:24:10 -07:00
|
|
|
return flexdll_dlsym(flexdll_dlopen(NULL,0,1), name);
|
2007-11-06 07:16:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
char * caml_dlerror(void)
|
|
|
|
{
|
|
|
|
return flexdll_dlerror();
|
|
|
|
}
|
|
|
|
|
2003-01-09 00:42:13 -08:00
|
|
|
#else
|
|
|
|
/* Use normal dlopen */
|
2001-08-28 07:47:48 -07:00
|
|
|
|
2001-09-09 01:35:55 -07:00
|
|
|
#ifndef RTLD_GLOBAL
|
|
|
|
#define RTLD_GLOBAL 0
|
|
|
|
#endif
|
2002-04-18 00:27:47 -07:00
|
|
|
#ifndef RTLD_NODELETE
|
|
|
|
#define RTLD_NODELETE 0
|
|
|
|
#endif
|
2001-09-09 01:35:55 -07:00
|
|
|
|
2008-04-22 05:24:10 -07:00
|
|
|
void * caml_dlopen(char * libname, int for_execution, int global)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
2008-04-22 05:24:10 -07:00
|
|
|
return dlopen(libname, RTLD_NOW | (global ? RTLD_GLOBAL : 0) | RTLD_NODELETE);
|
2006-09-29 01:18:22 -07:00
|
|
|
/* Could use RTLD_LAZY if for_execution == 0, but needs testing */
|
2001-08-28 07:47:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void caml_dlclose(void * handle)
|
|
|
|
{
|
|
|
|
dlclose(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * caml_dlsym(void * handle, char * name)
|
|
|
|
{
|
2002-11-14 11:01:26 -08:00
|
|
|
#ifdef DL_NEEDS_UNDERSCORE
|
2002-11-12 09:52:57 -08:00
|
|
|
char _name[1000] = "_";
|
|
|
|
strncat (_name, name, 998);
|
|
|
|
name = _name;
|
|
|
|
#endif
|
2001-08-28 07:47:48 -07:00
|
|
|
return dlsym(handle, name);
|
|
|
|
}
|
|
|
|
|
2007-11-06 07:16:56 -08:00
|
|
|
void * caml_globalsym(char * name)
|
|
|
|
{
|
2007-11-10 08:32:20 -08:00
|
|
|
#ifdef RTLD_DEFAULT
|
2007-11-08 02:55:57 -08:00
|
|
|
return caml_dlsym(RTLD_DEFAULT, name);
|
2007-11-10 08:32:20 -08:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2007-11-06 07:16:56 -08:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
char * caml_dlerror(void)
|
|
|
|
{
|
2007-11-08 02:55:57 -08:00
|
|
|
return (char*) dlerror();
|
2001-08-28 07:47:48 -07:00
|
|
|
}
|
|
|
|
|
2003-01-09 00:42:13 -08:00
|
|
|
#endif
|
2001-08-28 07:47:48 -07:00
|
|
|
#else
|
|
|
|
|
2008-04-22 05:24:10 -07:00
|
|
|
void * caml_dlopen(char * libname, int for_execution, int global)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void caml_dlclose(void * handle)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void * caml_dlsym(void * handle, char * name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-06 07:16:56 -08:00
|
|
|
void * caml_globalsym(char * name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
char * caml_dlerror(void)
|
|
|
|
{
|
|
|
|
return "dynamic loading not supported on this platform";
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-03-03 09:16:15 -08:00
|
|
|
/* Add to [contents] the (short) names of the files contained in
|
|
|
|
the directory named [dirname]. No entries are added for [.] and [..].
|
|
|
|
Return 0 on success, -1 on error; set errno in the case of error. */
|
|
|
|
|
|
|
|
int caml_read_directory(char * dirname, struct ext_table * contents)
|
|
|
|
{
|
|
|
|
DIR * d;
|
|
|
|
#ifdef HAS_DIRENT
|
|
|
|
struct dirent * e;
|
|
|
|
#else
|
|
|
|
struct direct * e;
|
|
|
|
#endif
|
|
|
|
char * p;
|
|
|
|
|
|
|
|
d = opendir(dirname);
|
|
|
|
if (d == NULL) return -1;
|
|
|
|
while (1) {
|
|
|
|
e = readdir(d);
|
|
|
|
if (e == NULL) break;
|
|
|
|
if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) continue;
|
2003-12-31 06:20:40 -08:00
|
|
|
p = caml_stat_alloc(strlen(e->d_name) + 1);
|
2003-03-03 09:16:15 -08:00
|
|
|
strcpy(p, e->d_name);
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_ext_table_add(contents, p);
|
2003-03-03 09:16:15 -08:00
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-03 06:56:36 -07:00
|
|
|
/* Recover executable name from /proc/self/exe if possible */
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
int caml_executable_name(char * name, int name_len)
|
2002-09-03 06:56:36 -07:00
|
|
|
{
|
|
|
|
int retcode;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
retcode = readlink("/proc/self/exe", name, name_len);
|
|
|
|
if (retcode == -1 || retcode >= name_len) return -1;
|
|
|
|
name[retcode] = 0;
|
|
|
|
/* Make sure that the contents of /proc/self/exe is a regular file.
|
|
|
|
(Old Linux kernels return an inode number instead.) */
|
|
|
|
if (stat(name, &st) != 0) return -1;
|
|
|
|
if (! S_ISREG(st.st_mode)) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|