aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/fmgr/dfmgr.c
blob: e835d9451d6d5d9460c9a5664d6f1852e5847605 (plain)
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
/*-------------------------------------------------------------------------
 *
 * dfmgr.c--
 *    Dynamic function manager code.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.1.1.1 1996/07/09 06:22:07 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>

#include "postgres.h"

#include "fmgr.h"		/* generated by Gen_fmgrtab.sh */
#include "utils/dynamic_loader.h"
#include "utils/elog.h"
#include "utils/builtins.h"
#include "access/heapam.h"
#include "nodes/pg_list.h"

#include "port-protos.h"     	/* system specific function prototypes */

#include "catalog/catname.h"
#include "utils/syscache.h"
#include "catalog/pg_proc.h"

static DynamicFileList *file_list = (DynamicFileList *) NULL;
static DynamicFileList *file_tail = (DynamicFileList *) NULL;

#define NOT_EQUAL(A, B) (((A).st_ino != (B).inode) \
                      || ((A).st_dev != (B).device))

static Oid procedureId_save = -1;
static int pronargs_save;
static func_ptr user_fn_save = (func_ptr) NULL;
static func_ptr handle_load(char *filename, char *funcname);

func_ptr
fmgr_dynamic(Oid procedureId, int *pronargs)
{
    HeapTuple   procedureTuple;
    Form_pg_proc procedureStruct;
    char        *proname;
    char        *probinattr, *probinstring;
    func_ptr    user_fn, handle_load();
    Relation    rdesc;
    bool     isnull;
    
    if (procedureId == procedureId_save) {
        *pronargs = pronargs_save;
        return(user_fn_save);
    }
    
    /*
     * The procedure isn't a builtin, so we'll have to do a catalog
     * lookup to find its pg_proc entry.
     */
    procedureTuple = SearchSysCacheTuple(PROOID, ObjectIdGetDatum(procedureId),
					 0,0,0);
    if (!HeapTupleIsValid(procedureTuple)) {
        elog(WARN, "fmgr: Cache lookup failed for procedure %d\n",
             procedureId);
        return((func_ptr) NULL);
    }
    
    procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
    proname = procedureStruct->proname.data;
    pronargs_save = *pronargs = procedureStruct->pronargs;
    
    /*
     * Extract the procedure info from the pg_proc tuple.
     * Since probin is varlena, do a amgetattr() on the procedure
     * tuple.  To do that, we need the rdesc for the procedure
     * relation, so...
     */
    
    /* open pg_procedure */
    
    rdesc = heap_openr(ProcedureRelationName);
    if (!RelationIsValid(rdesc)) {
        elog(WARN, "fmgr: Could not open relation %s",
             ProcedureRelationName);
        return((func_ptr) NULL);
    }
    probinattr = heap_getattr(procedureTuple, (Buffer) 0,
			      Anum_pg_proc_probin,
			      RelationGetTupleDescriptor(rdesc), &isnull);
    if (!PointerIsValid(probinattr) /*|| isnull*/) {
        heap_close(rdesc);
        elog(WARN, "fmgr: Could not extract probin for %d from %s",
             procedureId, ProcedureRelationName);
        return((func_ptr) NULL);
    }
    probinstring = textout((struct varlena *) probinattr);
    
    user_fn = handle_load(probinstring, proname);
    
    procedureId_save = procedureId;
    user_fn_save = user_fn;
    
    return(user_fn);
}

static func_ptr
handle_load(char *filename, char *funcname)
{
    DynamicFileList     *file_scanner = (DynamicFileList *) NULL;
    func_ptr            retval = (func_ptr) NULL;
    char                *load_error;
#ifdef WIN32
    struct _stat        stat_buf;
#else
    struct stat	stat_buf;
#endif     /* WIN32 */

    /*
     * Do this because loading files may screw up the dynamic function
     * manager otherwise.
     */
    procedureId_save = -1;

    /*
     * Scan the list of loaded FILES to see if the function
     * has been loaded.  
     */
    
    if (filename != (char *) NULL) {
        for (file_scanner = file_list;
             file_scanner != (DynamicFileList *) NULL
             && file_scanner->filename != (char *) NULL
             && strcmp(filename, file_scanner->filename) != 0;
             file_scanner = file_scanner->next)
	    ;
        if (file_scanner == (DynamicFileList *) NULL) {
            if (stat(filename, &stat_buf) == -1) {
                elog(WARN, "stat failed on file %s", filename);
            }
	    
            for (file_scanner = file_list;
                 file_scanner != (DynamicFileList *) NULL
                 && (NOT_EQUAL(stat_buf, *file_scanner));
                 file_scanner = file_scanner->next)
		;
            /*
             * Same files - different paths (ie, symlink or link)
             */
            if (file_scanner != (DynamicFileList *) NULL)
		(void) strcpy(file_scanner->filename, filename);
	    
        }
    } else {
        file_scanner = (DynamicFileList *) NULL;
    }
    
    /*
     * File not loaded yet.
     */
    
    if (file_scanner == (DynamicFileList *) NULL) {
        if (file_list == (DynamicFileList *) NULL) {
            file_list = (DynamicFileList *)
		malloc(sizeof(DynamicFileList));
            file_scanner = file_list;
        } else {
            file_tail->next = (DynamicFileList *)
		malloc(sizeof(DynamicFileList));
            file_scanner = file_tail->next;
        }
	memset((char *) file_scanner, 0, sizeof(DynamicFileList));
	
        (void) strcpy(file_scanner->filename, filename);
#ifndef WIN32
        file_scanner->device = stat_buf.st_dev;
        file_scanner->inode = stat_buf.st_ino;
#endif /* WIN32 */
        file_scanner->next = (DynamicFileList *) NULL;
	
	file_scanner->handle = pg_dlopen(filename);
        if (file_scanner->handle == (void *)NULL) {
	    load_error = pg_dlerror();
            if (file_scanner == file_list) {
                file_list = (DynamicFileList *) NULL;
            } else {
                file_tail->next = (DynamicFileList *) NULL;
            }
	    
            free((char *) file_scanner);
            elog(WARN, "Load of file %s failed: %s", filename, load_error);
        }
	
	/*
	 * Just load the file - we are done with that so return.
	 */
        file_tail = file_scanner;
	
        if (funcname == (char *) NULL)
	    return((func_ptr) NULL);
    }
    
    retval = pg_dlsym(file_scanner->handle, funcname);
    
    if (retval == (func_ptr) NULL) {
	elog(WARN, "Can't find function %s in file %s", funcname, filename);
    }
    
    return(retval);
}

/*
 * This function loads files by the following:
 *
 * If the file is already loaded:
 * o  Zero out that file's loaded space (so it doesn't screw up linking)
 * o  Free all space associated with that file 
 * o  Free that file's descriptor.
 *
 * Now load the file by calling handle_load with a NULL argument as the
 * function.
 */
void
load_file(char *filename)
{
    DynamicFileList *file_scanner, *p;
#ifdef WIN32
    struct _stat stat_buf;
#else
     struct stat stat_buf;
#endif /* WIN32 */

    int done = 0;
    
    if (stat(filename, &stat_buf) == -1) {
        elog(WARN, "stat failed on file %s", filename);
    }
    
    if (file_list != (DynamicFileList *) NULL
	&& !NOT_EQUAL(stat_buf, *file_list)) {
        file_scanner = file_list;
        file_list = file_list->next;
	pg_dlclose(file_scanner->handle);
        free((char *) file_scanner);
    } else if (file_list != (DynamicFileList *) NULL) {
        file_scanner = file_list;
        while (!done) {
            if (file_scanner->next == (DynamicFileList *) NULL) {
                done = 1;
            } else if (!NOT_EQUAL(stat_buf, *(file_scanner->next))) {
                done = 1;
            } else {
                file_scanner = file_scanner->next;
            }
        }
	
        if (file_scanner->next != (DynamicFileList *) NULL) {
            p = file_scanner->next;
            file_scanner->next = file_scanner->next->next;
	    pg_dlclose(file_scanner->handle);
            free((char *)p);
        }
    }
    handle_load(filename, (char *) NULL);
}