aboutsummaryrefslogtreecommitdiff
path: root/paludis/repository_so_loader.cc
blob: e3cc96008d93879faea7fcc435518cee65792d93 (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
124
125
126
127
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/about.hh>
#include <paludis/util/system.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/is_file_with_extension.hh>

#include <dlfcn.h>
#include <list>
#include <algorithm>

using namespace paludis;

namespace
{
    class PaludisRepositorySoDirNotADirectoryError :
        public Exception
    {
        public:
            PaludisRepositorySoDirNotADirectoryError() throw () :
                Exception("PALUDIS_REPOSITORY_SO_DIR not a directory")
            {
            }
    };

    class PaludisRepositorySoDirCannotDlopenError :
        public Exception
    {
        private:
            std::string _file, _dlerr;
            mutable std::string _what;

        public:
            PaludisRepositorySoDirCannotDlopenError(const std::string & file,
                    const std::string & e) throw () :
                Exception("Cannot dlopen a repository. so file"),
                _file(file),
                _dlerr(e)
            {
            }

            ~PaludisRepositorySoDirCannotDlopenError() throw ()
            {
            }

            const char * what() const throw ()
            {
                if (_what.empty())
                    _what = std::string(Exception::what()) +
                        ": Cannot dlopen repository .so file '" + _file + "': '" + _dlerr + "'";
                return _what.c_str();
            }
    };

    struct RepositorySoLoader
    {
        RepositorySoLoader();
        ~RepositorySoLoader();

        void load_dir(const FSEntry &);

        std::list<void *> dl_opened;
    };

    static RepositorySoLoader repository_so_loader;
}

RepositorySoLoader::RepositorySoLoader()
{
    FSEntry so_dir(getenv_with_default("PALUDIS_REPOSITORY_SO_DIR",
                LIBDIR "/paludis/repositories"));

    if (! so_dir.is_directory())
        throw PaludisRepositorySoDirNotADirectoryError();

    load_dir(so_dir);
}

void
RepositorySoLoader::load_dir(const FSEntry & so_dir)
{
    for (DirIterator d(so_dir), d_end ; d != d_end ; ++d)
    {
        if (d->is_directory())
            load_dir(*d);

        if (! IsFileWithExtension(".so." + stringify(100 * PALUDIS_VERSION_MAJOR +
                        PALUDIS_VERSION_MINOR))(*d))
            continue;

        /* don't use RTLD_LOCAL, g++ is over happy about template instantiations, and it
         * can lead to multiple singleton instances. */
        void * dl(dlopen(stringify(*d).c_str(), RTLD_GLOBAL | RTLD_NOW));

        if (dl)
            dl_opened.push_back(dl);
        else
            throw PaludisRepositorySoDirCannotDlopenError(stringify(*d), dlerror());
    }

    if ((so_dir / ".libs").is_directory())
        load_dir(so_dir / ".libs");
}

RepositorySoLoader::~RepositorySoLoader()
{
    std::for_each(dl_opened.begin(), dl_opened.end(), &::dlclose);
}