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