Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-04-30 9f2ef60949ced4fab4d077a7e1c6d913f3620c9b
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
/*
 * PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as
 *             procedural languages (PL)
 *
 *
 * Copyright (c) 2023 Brick Abode
 *
 * This code is subject to the terms of the PostgreSQL License.
 * The full text of the license can be found in the LICENSE file
 * at the top level of the pldotnet repository.
 *
 * pldotnet_hostfxr.c
 *
 */
 
#include "pldotnet_hostfxr.h"
 
#include <assert.h>
#include <dlfcn.h>
#include <limits.h>
#include <postgres.h>
#include <stdio.h>
 
#define nullptr ((void *)0)
#define MAX_PATH PATH_MAX
 
/*
 * START: declaring functions
 */
 
/** @brief Loads dotnet using the HostFXR.  HostFXR "finds and resolves
 * the runtime and all the frameworks the app needs", which in our
 * case is via `nethost`, "which is used by native apps (any app
 * which is not .NET Core) to load .NET Core code dynamically"
 *
 * See these URLs for more background:
 * https://github.com/dotnet/runtime/blob/main/docs/design/features/host-components.md
 * https://github.com/dotnet/runtime/blob/main/docs/design/features/sharedfx-lookup.md
 *
 * @return 1 on success, 0 on failure
 */
extern int pldotnet_LoadHostfxr(void);
 
/*
 * END: declaring functions
 */
 
/*
 * START: declaring variables
 */
 
static hostfxr_initialize_for_runtime_config_fn init_fptr;
static hostfxr_set_runtime_property_value_fn set_runtime_properties_ptr;
static hostfxr_get_runtime_delegate_fn get_delegate_fptr;
static hostfxr_close_fn close_fptr;
 
/*
 * END: declaring variables
 */
 
/*
 * START: implementing functions
 */
 
static void *pldotnet_dlopen(const char *path) {
    /* Allows us to add windows support later */
    return dlopen(path, RTLD_LAZY | RTLD_LOCAL);
}
 
static void *pldotnet_dlsym(void *handle, const char *symbol) {
    /* Allows us to add windows support later */
    return dlsym(handle, symbol);
}
 
// Returns 0 on success, (other) on error
static int pldotnet_GetHostFxrPath(char_t *buffer, size_t bufferSize) {
    FILE *out;
    char cmd[256];
    const char *aux = "/libhostfxr.so";
    size_t currentLength;
    int written;
 
    snprintf(cmd, sizeof(cmd),
        "dpkg -L dotnet-hostfxr-%s | grep libhostfxr.so | head -1 | xargs dirname",
        DOTNET_VERSION);
 
    out = popen(cmd, "r");
    if (NULL == out) {
        return -11;
    }
 
    while (fgets(buffer, bufferSize, out) != NULL) puts(buffer);
    buffer[strlen(buffer) - 1] = '\0';  // Removing the newline character
    currentLength = strlen(buffer);
 
    // Using snprintf to append the `aux` string
    written = snprintf(buffer + currentLength,
                       bufferSize - currentLength,
                       "%s",
                       aux);
    pclose(out);
    if ((written < 0) || (written >= bufferSize - currentLength)) {
        return -2;  // Indicate an snprintf error
    }
    return 0;
}
 
 
int pldotnet_LoadHostfxr(void) {
    void *lib;
    /* Pre-allocate a large buffer for the path to hostfxr */
    char_t hostfxr_path[MAX_PATH];
    size_t buffer_size = sizeof(hostfxr_path) / sizeof(char_t);
 
    if (get_hostfxr_path(hostfxr_path, &buffer_size, nullptr) != 0)
        if (pldotnet_GetHostFxrPath(hostfxr_path, buffer_size) != 0)
            return 0;
 
    /* Load hostfxr and get desired exports */
    lib = pldotnet_dlopen(hostfxr_path);
    init_fptr = (hostfxr_initialize_for_runtime_config_fn)pldotnet_dlsym(
        lib, "hostfxr_initialize_for_runtime_config");
    get_delegate_fptr = (hostfxr_get_runtime_delegate_fn)pldotnet_dlsym(
        lib, "hostfxr_get_runtime_delegate");
    set_runtime_properties_ptr =
        (hostfxr_set_runtime_property_value_fn)pldotnet_dlsym(
            lib, "hostfxr_set_runtime_property_value");
    close_fptr = (hostfxr_close_fn)pldotnet_dlsym(lib, "hostfxr_close");
 
    return (init_fptr && get_delegate_fptr && set_runtime_properties_ptr &&
            close_fptr);
}
 
load_assembly_and_get_function_pointer_fn GetNetLoadAssemblySetup(
    const char_t *config_path, const char_t *host_base_path) {
    int rc;
    static void *load_assembly_and_get_function_pointer = nullptr;
    hostfxr_handle cxt = nullptr;
 
    if (load_assembly_and_get_function_pointer != nullptr)
        return load_assembly_and_get_function_pointer;
 
    rc = init_fptr(config_path, nullptr, &cxt);
 
    if (rc > 1 || rc < 0 || cxt == nullptr) {
        fprintf(stderr, "Init failed: %x\n", rc);
        close_fptr(cxt);
        return nullptr;
    }
 
    if (nullptr != host_base_path)
        set_runtime_properties_ptr(cxt, "APP_CONTEXT_BASE_DIRECTORY",
                                   (char *)host_base_path);
 
    /* Get the load assembly function pointer */
    rc = get_delegate_fptr(cxt, hdt_load_assembly_and_get_function_pointer,
                           &load_assembly_and_get_function_pointer);
    close_fptr(cxt);
 
    if (rc != 0 || load_assembly_and_get_function_pointer == nullptr)
        elog(ERROR, "Get delegate failed: %x\n", rc);
 
    return (load_assembly_and_get_function_pointer_fn)
        load_assembly_and_get_function_pointer;
}
 
bool pldotnet_LoadHostFxrIfNeeded(void) {
    static bool hostfxr_loaded = false;
 
    if (!hostfxr_loaded) {
        hostfxr_loaded = pldotnet_LoadHostfxr();
    }
 
    return hostfxr_loaded;
}
 
/*
 * END: implementing functions
 */