aboutsummaryrefslogtreecommitdiff
path: root/paludis/broken_linkage_configuration.cc
blob: 86f1bb56025d0c7752eae835e0a17bcec5ab27b8 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 David Leverton
 *
 * 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/broken_linkage_configuration.hh>
#include <paludis/util/realpath.hh>
#include <paludis/util/wildcard_expander.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/join.hh>
#include <paludis/util/log.hh>
#include <paludis/util/options.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/system.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/create_iterator-impl.hh>
#include <paludis/util/fs_iterator.hh>
#include <paludis/util/fs_path.hh>
#include <paludis/util/fs_stat.hh>
#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>
#include <cctype>

using namespace paludis;

namespace paludis
{
    template <>
    struct Imp<BrokenLinkageConfiguration>
    {
        std::vector<std::string> ld_library_mask;
        std::vector<FSPath> search_dirs;
        std::vector<FSPath> search_dirs_mask;
        std::vector<FSPath> ld_so_conf;

        void load_from_environment();
        void load_from_etc_revdep_rebuild(const FSPath &);
        void load_from_etc_profile_env(const FSPath &);
        void load_from_etc_ld_so_conf(const FSPath &);
        void add_defaults();
    };

    template <>
    struct WrappedForwardIteratorTraits<BrokenLinkageConfiguration::DirsIteratorTag>
    {
        typedef std::vector<FSPath>::const_iterator UnderlyingIterator;
    };
}

namespace
{
    struct IsGarbageFile : std::unary_function<const FSPath &, bool>
    {
        bool operator() (const FSPath & file)
        {
            std::string basename(file.basename());
            return '#' == basename[0] || '~' == basename[basename.length() - 1];
        }
    };

    template <typename T_>
    void
    from_colon_string(const std::function<std::string (const std::string &)> & source,
                const std::string & varname, std::vector<T_> & vec)
    {
        std::string str(source.operator() (varname)); /* silly 4.3 ICE */
        if (! str.empty())
        {
            Log::get_instance()->message("broken_linkage_finder.config", ll_debug, lc_context)
                << "Got " << varname << "=\"" + str << "\"";
            tokenise<delim_kind::AnyOfTag, delim_mode::DelimiterTag>(str, ":", "", create_inserter<T_>(std::back_inserter(vec)));
        }
    }

    template <typename T_>
    void
    from_string(const std::function<std::string (const std::string &)> & source,
                const std::string & varname, std::vector<T_> & vec)
    {
        std::string str(source.operator() (varname)); /* silly 4.3 ICE */
        if (! str.empty())
        {
            Log::get_instance()->message("broken_linkage_finder.config", ll_debug, lc_context)
                << "Got " << varname << "=\"" << str << "\"";
            tokenise_whitespace(str, create_inserter<T_>(std::back_inserter(vec)));
        }
    }

    inline void
    do_wildcards(std::vector<std::string> &, const FSPath &)
    {
    }

    inline void
    do_wildcards(std::vector<FSPath> & vec, const FSPath & root)
    {
        std::vector<FSPath> scratch;

        for (std::vector<FSPath>::const_iterator it(vec.begin()), it_end(vec.end()); it_end != it; ++it)
            std::copy(WildcardExpander(stringify(*it), root), WildcardExpander(),
                      std::back_inserter(scratch));

        using std::swap;
        swap(vec, scratch);
    }

    template <typename T_, typename C_>
    void
    cleanup(const std::string & varname, std::vector<T_> & vec, const FSPath & root, const C_ & comparator)
    {
        vec.erase(std::find(vec.begin(), vec.end(), T_("-*")), vec.end());

        do_wildcards(vec, root);

        std::sort(vec.begin(), vec.end(), comparator);
        vec.erase(std::unique(vec.begin(), vec.end()), vec.end());

        Log::get_instance()->message("broken_linkage_finder.config",
                ll_debug, lc_context) << "Final " << varname << "=\"" <<
            join(vec.begin(), vec.end(),  " ") << "\"";
    }

    bool
    char_equals_ci(char a, char b)
    {
        return std::tolower(a) == std::tolower(b);
    }

    bool
    equals_ci(const std::string & a, const std::string & b)
    {
        return a.length() == b.length() && std::equal(a.begin(), a.end(), b.begin(), char_equals_ci);
    }

    void
    parse_ld_so_conf(const FSPath & root, const FSPath & file, const LineConfigFileOptions & opts, std::vector<std::string> & res)
    {
        FSStat file_stat(file);

        if (file_stat.is_regular_file_or_symlink_to_regular_file())
        {
            LineConfigFile lines(file, opts);
            for (auto it(lines.begin()), it_end(lines.end()); it_end != it; ++it)
            {
                std::vector<std::string> tokens;
                tokenise_whitespace(*it, std::back_inserter(tokens));

                if ("include" == tokens.at(0))
                {
                    for (auto it2(next(tokens.begin())), it2_end(tokens.end()); it2_end != it2; ++it2)
                    {
                        FSPath rel('/' == it2->at(0) ? root : file.dirname());
                        for (WildcardExpander it3(*it2, rel), it3_end; it3_end != it3; ++it3)
                        {
                            Context ctx("When reading included file '" + stringify(rel / *it3) + "':");
                            parse_ld_so_conf(root, rel / *it3, opts, res);
                        }
                    }
                }

                else if (equals_ci("hwdep", tokens.at(0))) // XXX
                    Log::get_instance()->message("broken_linkage_finder.etc_ld_so_conf.hwdep", ll_warning, lc_context)
                        << "'hwdep' line in '" << file << "' is not supported";
                else if (std::string::npos != it->find('='))
                    Log::get_instance()->message("broken_linkage_finder.etc_ld_so_conf.equals", ll_warning, lc_context)
                        << "'=' line in '" << file << "' is not supported";

                else
                    res.push_back(*it);
            }
        }

        else if (file_stat.exists())
            Log::get_instance()->message("broken_linkage_finder.etc_ld_so_conf.not_a_file", ll_warning, lc_context)
                << "'" << file << "' exists but is not a regular file";
    }
}

BrokenLinkageConfiguration::BrokenLinkageConfiguration(const FSPath & root) :
    _imp()
{
    Context ctx("When loading broken linkage checker configuration for '" + stringify(root) + "':");

    _imp->load_from_environment();
    _imp->load_from_etc_revdep_rebuild(root);
    _imp->load_from_etc_profile_env(root);
    _imp->load_from_etc_ld_so_conf(root);
    _imp->add_defaults();

    cleanup("LD_LIBRARY_MASK",  _imp->ld_library_mask,  root, std::less<std::string>());
    cleanup("SEARCH_DIRS",      _imp->search_dirs,      root, FSPathComparator());
    cleanup("SEARCH_DIRS_MASK", _imp->search_dirs_mask, root, FSPathComparator());

    // don't need the extra cleanup here
    std::sort(_imp->ld_so_conf.begin(), _imp->ld_so_conf.end(), FSPathComparator());
    _imp->ld_so_conf.erase(std::unique(_imp->ld_so_conf.begin(), _imp->ld_so_conf.end()),
                           _imp->ld_so_conf.end());
    Log::get_instance()->message("broken_linkage_finder.config",
            ll_debug, lc_context) << "Final ld.so.conf contents is \"" <<
        join(_imp->ld_so_conf.begin(), _imp->ld_so_conf.end(), " ") << "\"";
}

BrokenLinkageConfiguration::~BrokenLinkageConfiguration()
{
}

void
Imp<BrokenLinkageConfiguration>::load_from_environment()
{
    using namespace std::placeholders;

    Context ctx("When checking environment variables:");

    std::function<std::string (const std::string &)> fromenv(
        std::bind(getenv_with_default, _1, ""));

    from_string(fromenv, "LD_LIBRARY_MASK",  ld_library_mask);
    from_string(fromenv, "SEARCH_DIRS",      search_dirs);
    from_string(fromenv, "SEARCH_DIRS_MASK", search_dirs_mask);
}

void
Imp<BrokenLinkageConfiguration>::load_from_etc_revdep_rebuild(const FSPath & root)
{
    using namespace std::placeholders;

    FSPath etc_revdep_rebuild(root / "etc" / "revdep-rebuild");
    FSStat etc_revdep_rebuild_stat(etc_revdep_rebuild);
    Context ctx("When reading '" + stringify(etc_revdep_rebuild) + "':");

    if (etc_revdep_rebuild_stat.is_directory_or_symlink_to_directory())
    {
        std::vector<FSPath> conf_files = std::vector<FSPath>(FSIterator(etc_revdep_rebuild, { }), FSIterator());
        conf_files.erase(std::remove_if(conf_files.begin(), conf_files.end(), IsGarbageFile()),
                         conf_files.end());
        std::sort(conf_files.begin(), conf_files.end(), FSPathComparator());

        KeyValueConfigFileOptions opts;
        opts += kvcfo_disallow_space_around_equals;
        opts += kvcfo_disallow_space_inside_unquoted_values;

        for (std::vector<FSPath>::iterator it(conf_files.begin()),
                 it_end(conf_files.end()); it_end != it; ++it)
        {
            Context ctx_file("When reading '" + stringify(*it) + "':");

            if (it->stat().is_regular_file_or_symlink_to_regular_file())
            {
                KeyValueConfigFile kvs(*it, opts,
                        &KeyValueConfigFile::no_defaults, &KeyValueConfigFile::no_transformation);

                std::function<std::string (const std::string &)> fromfile(
                    std::bind(&KeyValueConfigFile::get, std::cref(kvs), _1));

                from_string(fromfile, "LD_LIBRARY_MASK",  ld_library_mask);
                from_string(fromfile, "SEARCH_DIRS",      search_dirs);
                from_string(fromfile, "SEARCH_DIRS_MASK", search_dirs_mask);
            }
            else
                Log::get_instance()->message("broken_linkage_finder.failure", ll_warning, lc_context)
                    << "'" << *it << "' is not a regular file";
        }
    }
    else if (etc_revdep_rebuild_stat.exists())
        Log::get_instance()->message("broken_linkage_finder.etc_revdep_rebuild.not_a_directory", ll_warning, lc_context)
            << "'" << etc_revdep_rebuild << "' exists but is not a directory";
}

void
Imp<BrokenLinkageConfiguration>::load_from_etc_profile_env(const FSPath & root)
{
    using namespace std::placeholders;

    FSPath etc_profile_env(root / "etc" / "profile.env");
    FSStat etc_profile_env_stat(etc_profile_env);
    Context ctx("When reading '" + stringify(etc_profile_env) + "':");

    if (etc_profile_env_stat.is_regular_file_or_symlink_to_regular_file())
    {
        KeyValueConfigFileOptions opts;
        opts += kvcfo_disallow_space_around_equals;
        opts += kvcfo_disallow_space_inside_unquoted_values;
        opts += kvcfo_ignore_export;

        KeyValueConfigFile kvs(etc_profile_env, opts,
                &KeyValueConfigFile::no_defaults, &KeyValueConfigFile::no_transformation);

        std::function<std::string (const std::string &)> fromfile(
            std::bind(&KeyValueConfigFile::get, std::cref(kvs), _1));

        from_colon_string(fromfile, "PATH",     search_dirs);
        from_colon_string(fromfile, "ROOTPATH", search_dirs);
    }
    else if (etc_profile_env_stat.exists())
        Log::get_instance()->message("broken_linkage_finder.etc_profile_env.not_a_file", ll_warning, lc_context)
            << "'" << etc_profile_env << "' exists but is not a regular file";
}

void
Imp<BrokenLinkageConfiguration>::load_from_etc_ld_so_conf(const FSPath & root)
{
    FSPath etc_ld_so_conf(root / "etc" / "ld.so.conf");
    Context ctx("When reading '" + stringify(etc_ld_so_conf) + "':");

    LineConfigFileOptions opts;
    opts += lcfo_disallow_continuations;
    opts += lcfo_allow_inline_comments;

    std::vector<std::string> res;
    parse_ld_so_conf(root, etc_ld_so_conf, opts, res);

    if (res.begin() != res.end())
    {
        Log::get_instance()->message("broken_linkage_finder.got", ll_debug, lc_context)
                << "Got " << join(res.begin(), res.end(), " ");
        std::copy(res.begin(), res.end(), create_inserter<FSPath>(std::back_inserter(search_dirs)));
        std::copy(res.begin(), res.end(), create_inserter<FSPath>(std::back_inserter(ld_so_conf)));
    }
}

void
Imp<BrokenLinkageConfiguration>::add_defaults()
{
    Context ctx("When adding default settings:");

    static const std::string default_ld_library_mask(
        "libodbcinst.so libodbc.so libjava.so libjvm.so");
    static const std::string default_search_dirs(
        "/bin /sbin /usr/bin /usr/sbin /lib* /usr/lib*");
    static const std::string default_search_dirs_mask(
        "/lib*/modules");
    static const std::string default_ld_so_conf("/lib /usr/lib");

    Log::get_instance()->message("broken_linkage_finder.config", ll_debug, lc_context)
        << "Got LD_LIBRARY_MASK=\"" << default_ld_library_mask << "\"";
    tokenise_whitespace(
            default_ld_library_mask, std::back_inserter(ld_library_mask));

    Log::get_instance()->message("broken_linkage_finder.config", ll_debug, lc_context)
        << "Got SEARCH_DIRS=\"" << default_search_dirs << "\"";
    tokenise_whitespace(
            default_search_dirs, create_inserter<FSPath>(std::back_inserter(search_dirs)));

    Log::get_instance()->message("broken_linkage_finder.config", ll_debug, lc_context)
        << "Got SEARCH_DIRS_MASK=\"" << default_search_dirs_mask << "\"";
    tokenise_whitespace(
            default_search_dirs_mask, create_inserter<FSPath>(std::back_inserter(search_dirs_mask)));

    Log::get_instance()->message("broken_linkage_finder.config", ll_debug, lc_context)
        << "Default ld.so.conf contents is \"" << default_ld_so_conf << "\"";
    tokenise_whitespace(
            default_ld_so_conf, create_inserter<FSPath>(std::back_inserter(ld_so_conf)));
}

BrokenLinkageConfiguration::DirsIterator
BrokenLinkageConfiguration::begin_search_dirs() const
{
    return DirsIterator(_imp->search_dirs.begin());
}

BrokenLinkageConfiguration::DirsIterator
BrokenLinkageConfiguration::end_search_dirs() const
{
    return DirsIterator(_imp->search_dirs.end());
}

BrokenLinkageConfiguration::DirsIterator
BrokenLinkageConfiguration::begin_ld_so_conf() const
{
    return DirsIterator(_imp->ld_so_conf.begin());
}

BrokenLinkageConfiguration::DirsIterator
BrokenLinkageConfiguration::end_ld_so_conf() const
{
    return DirsIterator(_imp->ld_so_conf.end());
}

bool
BrokenLinkageConfiguration::dir_is_masked(const FSPath & dir) const
{
    return std::binary_search(_imp->search_dirs_mask.begin(), _imp->search_dirs_mask.end(), dir, FSPathComparator());
}

bool
BrokenLinkageConfiguration::lib_is_masked(const std::string & lib) const
{
    return std::binary_search(_imp->ld_library_mask.begin(), _imp->ld_library_mask.end(), lib);
}

namespace paludis
{
    template class WrappedForwardIterator<BrokenLinkageConfiguration::DirsIteratorTag, const FSPath>;
}