summaryrefslogtreecommitdiff
path: root/mysql-test/lib/My/SafeProcess/wsrep_check_version.c
blob: cec760738be7f697501a9be564b5e3d13cfc6941 (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
/* Copyright (c) 2009, 2019, MariaDB

   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; version 2 of the License.

   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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef _WIN32
#include <windows.h>
#define dlsym(lib, name) GetProcAddress((HMODULE)lib, name)
#define dlopen(libname, unused) LoadLibraryEx(libname, NULL, 0)
#define dlclose(lib) FreeLibrary((HMODULE)lib)
#elif defined(HAVE_DLFCN_H)
#include <dlfcn.h>
#else
#define NO_DLL
#endif

#ifndef NO_DLL

#include "../../../../wsrep/wsrep_api.h"

/**************************************************************************
 * Library loader
 **************************************************************************/

static int wsrep_check_iface_version(const char *found, const char *iface_ver)
{
    if (strcmp(found, iface_ver)) {
        return ERANGE;
    }
    return 0;
}

typedef int (*wsrep_loader_fun)(wsrep_t*);

static wsrep_loader_fun wsrep_dlf(void *dlh, const char *sym)
{
    union {
        wsrep_loader_fun dlfun;
        void *obj;
    } alias;
    alias.obj = dlsym(dlh, sym);
    return alias.dlfun;
}

static int wsrep_check_version_symbol(void *dlh)
{
    char** dlversion = NULL;
    dlversion = (char**) dlsym(dlh, "wsrep_interface_version");
    if (dlversion == NULL)
        return EINVAL;
    return wsrep_check_iface_version(*dlversion, WSREP_INTERFACE_VERSION);
}

static int wsrep_print_version(void *dlh)
{
    char** dlversion = NULL;
    dlversion = (char**) dlsym(dlh, "wsrep_interface_version");
    if (dlversion == NULL)
        return EINVAL;
    printf("found: %s, need: %s\n", *dlversion, WSREP_INTERFACE_VERSION);
    return 0;
}

int main(int argc, char **argv)
{
    int rc = EINVAL;
    void *dlh;
    wsrep_loader_fun dlfun;

    if (!(dlh = dlopen(getenv("WSREP_PROVIDER"), RTLD_NOW | RTLD_LOCAL))) {
       goto err;
    }

    if (!(dlfun = wsrep_dlf(dlh, "wsrep_loader"))) {
       goto err;
    }

    if (argc < 2 || strcmp(argv[1], "-p")) {
        rc = wsrep_check_version_symbol(dlh);
    }
    else {
        rc = wsrep_print_version(dlh);
    }

err:
    if (dlh) dlclose(dlh);

    if (rc == 0)
        return 0;
    else if (rc == ERANGE)
        return 2;
    else
        return 1;
}

#else

int main(void)
{
    return 1;
}

#endif