LibreOffice Module pyuno (master) 1
pyuno_dlopenwrapper.c
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20/* make Python.h go first as a hack to work around _POSIX_C_SOURCE redefinition
21 warnings: */
22#include <Python.h>
23
24#include <sal/config.h>
25
26#include <stdlib.h>
27#include <string.h>
28
29#if defined LINUX && !defined __USE_GNU
30#define __USE_GNU
31#endif
32#include <dlfcn.h>
33
34#include <rtl/string.h>
35
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).
39*/
40
41static 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();
50 }
51 slash = strrchr(dl_info.dli_fname, '/');
52 if (slash == NULL) {
53 abort();
54 }
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();
61 }
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();
69 }
70 func = dlsym(h, symbol);
71 if (func == NULL) {
72 dlclose(h);
73 abort();
74 }
75 // coverity[leaked_storage] - this is on purpose
76 return func;
77}
78
79SAL_DLLPUBLIC_EXPORT PyObject * PyInit_pyuno(void) {
80 return
81 ((PyObject * (*)(void)) load((void *) &PyInit_pyuno, "PyInit_pyuno"))();
82}
83
84/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DLLPREFIX
#define SAL_DLLEXTENSION
return NULL
sal_Int32 h
static void * load(void const *address, char const *symbol)
SAL_DLLPUBLIC_EXPORT PyObject * PyInit_pyuno(void)
function called by the python runtime to initialize the pyuno module.