Line data Source code
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 :
41 0 : static void * load(void * 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 0 : if (dladdr(address, &dl_info) == 0) {
49 0 : abort();
50 : }
51 0 : slash = strrchr(dl_info.dli_fname, '/');
52 0 : if (slash == NULL) {
53 0 : abort();
54 : }
55 0 : len = slash - dl_info.dli_fname + 1;
56 0 : libname = malloc(
57 : len + RTL_CONSTASCII_LENGTH(SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION)
58 0 : + 1);
59 0 : if (libname == 0) {
60 0 : abort();
61 : }
62 0 : strncpy(libname, dl_info.dli_fname, len);
63 0 : strcpy(libname + len, SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION);
64 0 : h = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
65 0 : free(libname);
66 0 : if (h == NULL) {
67 0 : abort();
68 : }
69 0 : func = dlsym(h, symbol);
70 0 : if (func == NULL) {
71 0 : abort();
72 : }
73 0 : return func;
74 : }
75 :
76 : #if PY_MAJOR_VERSION >= 3
77 :
78 0 : SAL_DLLPUBLIC_EXPORT PyObject * PyInit_pyuno(void) {
79 0 : return
80 0 : ((PyObject * (*)(void)) load((void *) &PyInit_pyuno, "PyInit_pyuno"))();
81 : }
82 :
83 : #else
84 :
85 : SAL_DLLPUBLIC_EXPORT void initpyuno(void) {
86 : ((void (*)(void)) load((void *) &initpyuno, "initpyuno"))();
87 : }
88 :
89 : #endif
90 :
91 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|