Coverage Report

Created: 2026-11-12 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/netcdf-c/libdispatch/dudfplugins.c
Line
Count
Source
1
/* Copyright 2026, UCAR/Unidata.
2
   See the COPYRIGHT file for more information. */
3
4
/**
5
 * @file
6
 * @internal This file contains functions for loading UDF plugins from RC files.
7
 *
8
 * @author Ed Hartnett
9
 * @date 2/2/26
10
 */
11
12
#include "config.h"
13
#include <stdlib.h>
14
#include <string.h>
15
#include <stdio.h>
16
#include "netcdf.h"
17
#include "netcdf_dispatch.h"
18
#include "nclog.h"
19
#include "ncrc.h"
20
#include "ncudfplugins.h"
21
22
/* Platform-specific dynamic loading headers */
23
#ifdef _WIN32
24
#include <windows.h>
25
#else
26
#include <dlfcn.h>
27
#endif
28
29
/**
30
 * Load a dynamic library (platform-specific).
31
 *
32
 * @param path Full path to the library file.
33
 * @return Handle to the loaded library, or NULL on failure.
34
 *
35
 * @author Edward Hartnett
36
 * @date 2/2/26
37
 */
38
static void*
39
load_library(const char* path)
40
0
{
41
0
    void* handle = NULL;
42
    
43
#ifdef _WIN32
44
    handle = (void*)LoadLibraryA(path);
45
    if (!handle) {
46
        DWORD err = GetLastError();
47
        nclog(NCLOGERR, "LoadLibrary failed for %s: error %lu", path, err);
48
    }
49
#else
50
0
    handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
51
0
    if (!handle) {
52
0
        nclog(NCLOGERR, "dlopen failed for %s: %s", path, dlerror());
53
0
    }
54
0
#endif
55
    
56
0
    return handle;
57
0
}
58
59
/**
60
 * Get a symbol from a loaded library (platform-specific).
61
 *
62
 * @param handle Handle to the loaded library.
63
 * @param symbol Name of the symbol to retrieve.
64
 * @return Pointer to the symbol, or NULL on failure.
65
 *
66
 * @author Edward Hartnett
67
 * @date 2/2/26
68
 */
69
static void*
70
get_symbol(void* handle, const char* symbol)
71
0
{
72
0
    void* sym = NULL;
73
    
74
#ifdef _WIN32
75
    sym = (void*)GetProcAddress((HMODULE)handle, symbol);
76
    if (!sym) {
77
        DWORD err = GetLastError();
78
        nclog(NCLOGERR, "GetProcAddress failed for %s: error %lu", symbol, err);
79
    }
80
#else
81
0
    sym = dlsym(handle, symbol);
82
0
    if (!sym) {
83
0
        nclog(NCLOGERR, "dlsym failed for %s: %s", symbol, dlerror());
84
0
    }
85
0
#endif
86
    
87
0
    return sym;
88
0
}
89
90
/**
91
 * Load a single UDF plugin library and call its initialization function.
92
 *
93
 * @param udf_number UDF slot number (0 to NC_MAX_UDF_FORMATS - 1).
94
 * @param library_path Full path to the plugin library.
95
 * @param init_func Name of the initialization function.
96
 * @param magic Optional magic number string (can be NULL).
97
 * @return NC_NOERR on success, error code on failure.
98
 *
99
 * @author Edward Hartnett
100
 * @date 2/2/26
101
 */
102
static int
103
load_udf_plugin(int udf_number, const char* library_path,
104
                const char* init_func, const char* magic)
105
0
{
106
0
    int stat = NC_NOERR;
107
0
    void* handle = NULL;
108
0
    int mode_flag;
109
#ifndef HAVE_NETCDF_UDF_SELF_REGISTRATION
110
    NC_Dispatch* dispatch_table = NULL;
111
    char magic_check[NC_MAX_MAGIC_NUMBER_LEN + 1];
112
#endif
113
    
114
    /* Determine mode flag from UDF number. */
115
0
    mode_flag = NC_UDF(udf_number);
116
    
117
    /* Load the library */
118
0
    handle = load_library(library_path);
119
0
    if (!handle) {
120
0
        stat = NC_ENOTNC;
121
0
        goto done;
122
0
    }
123
    
124
0
#ifdef HAVE_NETCDF_UDF_SELF_REGISTRATION
125
    /* Self-registration mode: init function returns NC_Dispatch* */
126
0
    {
127
0
        NC_Dispatch* (*init_function)(void) = NULL;
128
0
        NC_Dispatch* table = NULL;
129
        
130
        /* Get the initialization function */
131
0
        init_function = (NC_Dispatch* (*)(void))get_symbol(handle, init_func);
132
0
        if (!init_function) {
133
0
            stat = NC_ENOTNC;
134
0
            goto done;
135
0
        }
136
        
137
        /* Call the initialization function to get the dispatch table */
138
0
        table = init_function();
139
0
        if (!table) {
140
0
            nclog(NCLOGERR, "Plugin init function %s returned NULL", init_func);
141
0
            stat = NC_ENOTNC;
142
0
            goto done;
143
0
        }
144
        
145
        /* Verify dispatch ABI version */
146
0
        if (table->dispatch_version != NC_DISPATCH_VERSION) {
147
0
            nclog(NCLOGERR, "Plugin dispatch ABI mismatch for UDF%d: expected %d, got %d",
148
0
                  udf_number, NC_DISPATCH_VERSION, table->dispatch_version);
149
0
            stat = NC_EINVAL;
150
0
            goto done;
151
0
        }
152
        
153
        /* Register the dispatch table returned by the plugin */
154
0
        if ((stat = nc_def_user_format(mode_flag, table, (char*)magic))) {
155
0
            nclog(NCLOGERR, "Failed to register dispatch table for UDF%d: %d",
156
0
                  udf_number, stat);
157
0
            goto done;
158
0
        }
159
0
    }
160
#else
161
    /* Legacy mode: init function returns int and registers itself */
162
    {
163
        int (*init_function)(void) = NULL;
164
        
165
        /* Get the initialization function */
166
        init_function = (int (*)(void))get_symbol(handle, init_func);
167
        if (!init_function) {
168
            stat = NC_ENOTNC;
169
            goto done;
170
        }
171
        
172
        /* Call the initialization function */
173
        if ((stat = init_function())) {
174
            nclog(NCLOGERR, "Plugin init function %s failed: %d", init_func, stat);
175
            goto done;
176
        }
177
        
178
        /* Verify the dispatch table was registered */
179
        memset(magic_check, 0, sizeof(magic_check));
180
        if ((stat = nc_inq_user_format(mode_flag, &dispatch_table, magic_check))) {
181
            nclog(NCLOGERR, "Plugin did not register dispatch table for UDF%d", udf_number);
182
            goto done;
183
        }
184
        
185
        if (dispatch_table == NULL) {
186
            nclog(NCLOGERR, "Plugin registered NULL dispatch table for UDF%d", udf_number);
187
            stat = NC_EINVAL;
188
            goto done;
189
        }
190
        
191
        /* Verify dispatch ABI version */
192
        if (dispatch_table->dispatch_version != NC_DISPATCH_VERSION) {
193
            nclog(NCLOGERR, "Plugin dispatch ABI mismatch for UDF%d: expected %d, got %d",
194
                  udf_number, NC_DISPATCH_VERSION, dispatch_table->dispatch_version);
195
            stat = NC_EINVAL;
196
            goto done;
197
        }
198
        
199
        /* Optionally verify magic number matches */
200
        if (magic != NULL && strlen(magic_check) > 0) {
201
            if (strcmp(magic, magic_check) != 0) {
202
                nclog(NCLOGWARN, "Plugin magic number mismatch for UDF%d: expected %s, got %s",
203
                      udf_number, magic, magic_check);
204
            }
205
        }
206
    }
207
#endif
208
    
209
0
    nclog(NCLOGNOTE, "Successfully loaded UDF%d plugin from %s", 
210
0
          udf_number, library_path);
211
    
212
0
done:
213
    /* Handles are intentionally not closed; the OS will reclaim at process exit.
214
     * The dispatch table and its functions must remain accessible. */
215
0
    return stat;
216
0
}
217
218
/**
219
 * Load and initialize all UDF plugins from RC file configuration.
220
 *
221
 * This function loops through all NC_MAX_UDF_FORMATS UDF slots and
222
 * checks for corresponding RC file entries. If both LIBRARY and INIT
223
 * keys are present for a slot, it attempts to load that plugin.
224
 *
225
 * @return NC_NOERR (always succeeds, even if plugins fail to load).
226
 *
227
 * @author Edward Hartnett
228
 * @date 2/2/26
229
 */
230
int
231
NC_udf_load_plugins(void)
232
1
{
233
1
    int stat = NC_NOERR;
234
    
235
    /* Loop through all UDF slots */
236
65
    for (int i = 0; i < NC_MAX_UDF_FORMATS; i++) {
237
64
        char key_lib[64], key_init[64], key_magic[64];
238
64
        const char* lib = NULL;
239
64
        const char* init = NULL;
240
64
        const char* magic = NULL;
241
        
242
        /* Build RC key names for this UDF slot */
243
64
        snprintf(key_lib, sizeof(key_lib), "NETCDF.UDF%d.LIBRARY", i);
244
64
        snprintf(key_init, sizeof(key_init), "NETCDF.UDF%d.INIT", i);
245
64
        snprintf(key_magic, sizeof(key_magic), "NETCDF.UDF%d.MAGIC", i);
246
        
247
        /* Look up RC values */
248
64
        lib = NC_rclookup(key_lib, NULL, NULL);
249
64
        init = NC_rclookup(key_init, NULL, NULL);
250
64
        magic = NC_rclookup(key_magic, NULL, NULL);
251
        
252
        /* If both LIBRARY and INIT are present, try to load the plugin */
253
64
        if (lib && init) {
254
0
            if ((stat = load_udf_plugin(i, lib, init, magic))) {
255
0
                nclog(NCLOGWARN, "Failed to load UDF%d plugin from %s: %d", i, lib, stat);
256
0
            }
257
64
        } else if (lib || init) {
258
            /* Warn about partial configuration */
259
0
            nclog(NCLOGWARN, "Ignoring partial UDF%d configuration "
260
0
                  "(both NETCDF.UDF%d.LIBRARY and NETCDF.UDF%d.INIT are required)",
261
0
                  i, i, i);
262
0
        }
263
64
    }
264
    
265
    /* Always return success - plugin loading failures are not fatal */
266
1
    return NC_NOERR;
267
1
}