Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 352 lines (291 sloc) 8.389 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
/*
* PL/R - PostgreSQL support for R as a
* procedural language (PL)
*
* Copyright (c) 2003 by Joseph E. Conway
* ALL RIGHTS RESERVED
*
* Joe Conway <mail@joeconway.com>
*
* Based on pltcl by Jan Wieck
* and inspired by REmbeddedPostgres by
* Duncan Temple Lang <duncan@research.bell-labs.com>
* http://www.omegahat.org/RSPostgres/
*
* License: GPL version 2 or newer. http://www.gnu.org/copyleft/gpl.html
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* pg_backend_support.c - Postgres backend support functions
*/
#include "plr.h"

/* Example format: "/usr/local/pgsql/lib" */
#ifndef PKGLIBDIR
#error "PKGLIBDIR needs to be defined to compile this file."
#endif

/* GUC variable */
extern char *Dynamic_library_path;

/*
* static declarations
*/
static char *get_lib_pathstr(Oid funcid);
static char *expand_dynamic_library_name(const char *name);
static char *substitute_libpath_macro(const char *name);
static char *find_in_dynamic_libpath(const char *basename);
static bool file_exists(const char *name);

static char *
get_lib_pathstr(Oid funcid)
{
HeapTuple procedureTuple;
Form_pg_proc procedureStruct;
Oid language;
HeapTuple languageTuple;
Form_pg_language languageStruct;
Oid lang_funcid;
Datum tmp;
bool isnull;
char *raw_path;
char *cooked_path;

/* get the pg_proc entry */
procedureTuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(funcid),
0, 0, 0);
if (!HeapTupleIsValid(procedureTuple))
elog(ERROR, "get_lib_pathstr: cache lookup for function %u failed",
funcid);
procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);

/* now get the pg_language entry */
language = procedureStruct->prolang;
ReleaseSysCache(procedureTuple);

languageTuple = SearchSysCache(LANGOID,
ObjectIdGetDatum(language),
0, 0, 0);
if (!HeapTupleIsValid(languageTuple))
elog(ERROR, "get_lib_pathstr: cache lookup for language %u failed",
language);
languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
lang_funcid = languageStruct->lanplcallfoid;
ReleaseSysCache(languageTuple);

/* finally, get the pg_proc entry for the language handler */
procedureTuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(lang_funcid),
0, 0, 0);
if (!HeapTupleIsValid(procedureTuple))
elog(ERROR, "get_lib_pathstr: cache lookup for function %u failed",
lang_funcid);
procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);

tmp = SysCacheGetAttr(PROCOID, procedureTuple, Anum_pg_proc_probin, &isnull);
raw_path = DatumGetCString(DirectFunctionCall1(byteaout, tmp));
cooked_path = expand_dynamic_library_name(raw_path);

ReleaseSysCache(procedureTuple);

return cooked_path;
}

char *
get_load_self_ref_cmd(Oid funcid)
{
char *libstr = get_lib_pathstr(funcid);
char *buf = (char *) palloc(strlen(libstr) + 12 + 1);

sprintf(buf, "dyn.load(\"%s\")", libstr);
return buf;
}

void
perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
{
fmgr_info_cxt(functionId, finfo, TopMemoryContext);
finfo->fn_mcxt = QueryContext;
}

void
system_cache_lookup(Oid element_type,
bool input,
int *typlen,
bool *typbyval,
char *typdelim,
Oid *typelem,
Oid *proc,
char *typalign)
{
HeapTuple typeTuple;
Form_pg_type typeStruct;

typeTuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(element_type),
0, 0, 0);
if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "cache lookup failed for type %u", element_type);
typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);

*typlen = typeStruct->typlen;
*typbyval = typeStruct->typbyval;
*typdelim = typeStruct->typdelim;
*typelem = typeStruct->typelem;
*typalign = typeStruct->typalign;
if (input)
*proc = typeStruct->typinput;
else
*proc = typeStruct->typoutput;
ReleaseSysCache(typeTuple);
}

Oid
get_typelem(Oid element_type)
{
Oid typelem;
HeapTuple typeTuple;
Form_pg_type typeStruct;

typeTuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(element_type),
0, 0, 0);
if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "cache lookup failed for type %u", element_type);
typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);

typelem = typeStruct->typelem;

ReleaseSysCache(typeTuple);

return typelem;
}

static bool
file_exists(const char *name)
{
struct stat st;

AssertArg(name != NULL);

if (stat(name, &st) == 0)
return S_ISDIR(st.st_mode) ? false : true;
else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
elog(ERROR, "stat failed on %s: %s", name, strerror(errno));

return false;
}

/*
* If name contains a slash, check if the file exists, if so return
* the name. Else (no slash) try to expand using search path (see
* find_in_dynamic_libpath below); if that works, return the fully
* expanded file name. If the previous failed, append DLSUFFIX and
* try again. If all fails, return NULL.
*
* A non-NULL result will always be freshly palloc'd.
*/
static char *
expand_dynamic_library_name(const char *name)
{
bool have_slash;
char *new;
char *full;

AssertArg(name);

have_slash = (strchr(name, '/') != NULL);

if (!have_slash)
{
full = find_in_dynamic_libpath(name);
if (full)
return full;
}
else
{
full = substitute_libpath_macro(name);
if (file_exists(full))
return full;
pfree(full);
}

new = palloc(strlen(name) + strlen(DLSUFFIX) + 1);
strcpy(new, name);
strcat(new, DLSUFFIX);

if (!have_slash)
{
full = find_in_dynamic_libpath(new);
pfree(new);
if (full)
return full;
}
else
{
full = substitute_libpath_macro(new);
pfree(new);
if (file_exists(full))
return full;
pfree(full);
}

return NULL;
}


/*
* Substitute for any macros appearing in the given string.
* Result is always freshly palloc'd.
*/
static char *
substitute_libpath_macro(const char *name)
{
size_t macroname_len;
char *replacement = NULL;

AssertArg(name != NULL);

if (name[0] != '$')
return pstrdup(name);

macroname_len = strcspn(name + 1, "/") + 1;

if (strncmp(name, "$libdir", macroname_len) == 0)
replacement = PKGLIBDIR;
else
elog(ERROR, "invalid macro name in dynamic library path");

if (name[macroname_len] == '\0')
return pstrdup(replacement);
else
{
char *new;

new = palloc(strlen(replacement) + (strlen(name) - macroname_len) + 1);

strcpy(new, replacement);
strcat(new, name + macroname_len);

return new;
}
}


/*
* Search for a file called 'basename' in the colon-separated search
* path Dynamic_library_path. If the file is found, the full file name
* is returned in freshly palloc'd memory. If the file is not found,
* return NULL.
*/
static char *
find_in_dynamic_libpath(const char *basename)
{
const char *p;
size_t baselen;

AssertArg(basename != NULL);
AssertArg(strchr(basename, '/') == NULL);
AssertState(Dynamic_library_path != NULL);

p = Dynamic_library_path;
if (strlen(p) == 0)
return NULL;

baselen = strlen(basename);

for (;;)
{
size_t len;
char *piece;
char *mangled;
char *full;

len = strcspn(p, ":");

if (len == 0)
elog(ERROR, "zero length dynamic_library_path component");

piece = palloc(len + 1);
strncpy(piece, p, len);
piece[len] = '\0';

mangled = substitute_libpath_macro(piece);
pfree(piece);

/* only absolute paths */
if (mangled[0] != '/')
elog(ERROR, "dynamic_library_path component is not absolute");

full = palloc(strlen(mangled) + 1 + baselen + 1);
sprintf(full, "%s/%s", mangled, basename);
pfree(mangled);

elog(DEBUG2, "find_in_dynamic_libpath: trying %s", full);

if (file_exists(full))
return full;

pfree(full);

if (p[len] == '\0')
break;
else
p += len + 1;
}

return NULL;
}
Something went wrong with that request. Please try again.