Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / pyuno / source / module / pyuno_dlopenwrapper.c
blob44e711653e35290d3e9cddce28da6a0d4839ecb0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /* make Python.h go first as a hack to work around _POSIX_C_SOURCE redefinition
21 warnings: */
22 #include <Python.h>
24 #include <sal/config.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #if defined LINUX && !defined __USE_GNU
30 #define __USE_GNU
31 #endif
32 #include <dlfcn.h>
34 #include <rtl/string.h>
36 /* A wrapper around libpyuno.so, making sure the latter is loaded RTLD_GLOBAL
37 so that C++ exception handling works with old GCC versions (that determine
38 RTTI identity by comparing string addresses rather than string content).
41 static void * load(void const * address, char const * symbol) {
42 Dl_info dl_info;
43 char * slash;
44 size_t len;
45 char * libname;
46 void * h;
47 void * func;
48 if (dladdr(address, &dl_info) == 0) {
49 abort();
51 slash = strrchr(dl_info.dli_fname, '/');
52 if (slash == NULL) {
53 abort();
55 len = slash - dl_info.dli_fname + 1;
56 libname = malloc(
57 len + RTL_CONSTASCII_LENGTH(SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION)
58 + 1);
59 if (libname == NULL) {
60 abort();
62 strncpy(libname, dl_info.dli_fname, len);
63 strcpy(libname + len, SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION);
64 h = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
65 free(libname);
66 if (h == NULL) {
67 fprintf(stderr, "failed to load pyuno: '%s'\n", dlerror());
68 abort();
70 func = dlsym(h, symbol);
71 if (func == NULL) {
72 dlclose(h);
73 abort();
75 // coverity[leaked_storage] - this is on purpose
76 return func;
79 #if PY_MAJOR_VERSION >= 3
81 SAL_DLLPUBLIC_EXPORT PyObject * PyInit_pyuno(void) {
82 return
83 ((PyObject * (*)(void)) load((void *) &PyInit_pyuno, "PyInit_pyuno"))();
86 #else
88 SAL_DLLPUBLIC_EXPORT void initpyuno(void) {
89 ((void (*)(void)) load((void *) &initpyuno, "initpyuno"))();
92 #endif
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */