aboutsummaryrefslogtreecommitdiff
path: root/paludis/ipc_output_manager.cc
blob: 5bb7d31e903e50e30a72b8ffdb247a2c7b69da9a (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010 Ciaran McCreesh
 *
 * 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/ipc_output_manager.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/safe_ofstream.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/set.hh>
#include <paludis/util/map.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/system.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/log.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/join.hh>
#include <paludis/util/thread.hh>
#include <paludis/util/pipe.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/options.hh>
#include <paludis/standard_output_manager.hh>
#include <paludis/create_output_manager_info.hh>
#include <paludis/serialise.hh>
#include <paludis/environment.hh>
#include <functional>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

using namespace paludis;

namespace paludis
{
    template <>
    struct Imp<IPCOutputManager>
    {
        std::shared_ptr<SafeOFStream> stdout_stream;
        std::shared_ptr<SafeOFStream> stderr_stream;

        std::shared_ptr<SafeIFStream> pipe_command_read_stream;
        std::shared_ptr<SafeOFStream> pipe_command_write_stream;

        Imp(int r, int w) :
            pipe_command_write_stream(std::make_shared<SafeOFStream>(w))
        {
            *pipe_command_write_stream << "PING 1 GOAT" << '\0' << std::flush;

            pipe_command_read_stream = std::make_shared<SafeIFStream>(r);
            std::string response;
            if (! std::getline(*pipe_command_read_stream, response, '\0'))
                throw InternalError(PALUDIS_HERE, "couldn't get a pipe command response");
            if (response != "OPONG GOAT")
                throw InternalError(PALUDIS_HERE, "got response '" + response + "'");
        }
    };
}

IPCOutputManager::IPCOutputManager(const int r, const int w, const CreateOutputManagerInfo & i) :
    Pimp<IPCOutputManager>(r, w)
{
    std::stringstream ser_stream;
    Serialiser ser(ser_stream);
    i.serialise(ser);
    *_imp->pipe_command_write_stream << "CREATE 1 " << ser_stream.str() << '\0' << std::flush;

    std::string response;
    if (! std::getline(*_imp->pipe_command_read_stream, response, '\0'))
        throw InternalError(PALUDIS_HERE, "couldn't get a pipe command response");

    std::vector<std::string> tokens;
    tokenise_whitespace(response, std::back_inserter(tokens));

    if (tokens.size() != 4 || tokens[0] != "O" || tokens[1] != "1")
        throw InternalError(PALUDIS_HERE, "got response '" + response + "'");

    int stdout_fd(destringify<int>(tokens[2])), stderr_fd(destringify<int>(tokens[3]));
    _imp->stdout_stream = std::make_shared<SafeOFStream>(stdout_fd);
    _imp->stderr_stream = std::make_shared<SafeOFStream>(stderr_fd);

    if (0 != ::fcntl(stdout_fd, F_SETFD, FD_CLOEXEC))
        throw InternalError(PALUDIS_HERE, "fcntl failed");
    if (0 != ::fcntl(stderr_fd, F_SETFD, FD_CLOEXEC))
        throw InternalError(PALUDIS_HERE, "fcntl failed");
}

IPCOutputManager::~IPCOutputManager()
{
    *_imp->pipe_command_write_stream << "FINISHED 1" << '\0' << std::flush;

    std::string response;
    if (! std::getline(*_imp->pipe_command_read_stream, response, '\0'))
        throw InternalError(PALUDIS_HERE, "couldn't get a pipe command response");
    if (response != "O")
        throw InternalError(PALUDIS_HERE, "got response '" + response + "'");
}

std::ostream &
IPCOutputManager::stdout_stream()
{
    return *_imp->stdout_stream;
}

std::ostream &
IPCOutputManager::stderr_stream()
{
    return *_imp->stderr_stream;
}

void
IPCOutputManager::message(const MessageType t, const std::string & s)
{
    *_imp->pipe_command_write_stream << "MESSAGE 1 " << t << " " << s << '\0' << std::flush;

    std::string response;
    if (! std::getline(*_imp->pipe_command_read_stream, response, '\0'))
        throw InternalError(PALUDIS_HERE, "couldn't get a pipe command response");
    if (response != "O")
        throw InternalError(PALUDIS_HERE, "got response '" + response + "'");
}

void
IPCOutputManager::succeeded()
{
    *_imp->pipe_command_write_stream << "SUCCEEDED 1" << '\0' << std::flush;

    std::string response;
    if (! std::getline(*_imp->pipe_command_read_stream, response, '\0'))
        throw InternalError(PALUDIS_HERE, "couldn't get a pipe command response");
    if (response != "O")
        throw InternalError(PALUDIS_HERE, "got response '" + response + "'");
}

void
IPCOutputManager::flush()
{
}

bool
IPCOutputManager::want_to_flush() const
{
    return false;
}

void
IPCOutputManager::nothing_more_to_come()
{
    *_imp->pipe_command_write_stream << "NOTHING_MORE_TO_COME 1" << '\0' << std::flush;

    std::string response;
    if (! std::getline(*_imp->pipe_command_read_stream, response, '\0'))
        throw InternalError(PALUDIS_HERE, "couldn't get a pipe command response");
    if (response != "O")
        throw InternalError(PALUDIS_HERE, "got response '" + response + "'");
}

namespace paludis
{
    template <>
    struct Imp<IPCInputManager>
    {
        const Environment * const env;
        const std::function<void (const std::shared_ptr<OutputManager> &)> on_create;

        mutable Mutex mutex;
        std::shared_ptr<OutputManager> output_manager;

        Pipe stdout_pipe, stderr_pipe, finished_pipe;

        std::shared_ptr<Thread> copy_thread;

        Imp(const Environment * const e,
                const std::function<void (const std::shared_ptr<OutputManager> &)> & c) :
            env(e),
            on_create(c)
        {
            if (0 != ::fcntl(finished_pipe.read_fd(), F_SETFD, FD_CLOEXEC))
                throw InternalError(PALUDIS_HERE, "fcntl failed");
            if (0 != ::fcntl(finished_pipe.write_fd(), F_SETFD, FD_CLOEXEC))
                throw InternalError(PALUDIS_HERE, "fcntl failed");
            if (0 != ::fcntl(stdout_pipe.read_fd(), F_SETFD, FD_CLOEXEC))
                throw InternalError(PALUDIS_HERE, "fcntl failed");
            if (0 != ::fcntl(stderr_pipe.read_fd(), F_SETFD, FD_CLOEXEC))
                throw InternalError(PALUDIS_HERE, "fcntl failed");
        }
    };
}

IPCInputManager::IPCInputManager(const Environment * const e,
        const std::function<void (const std::shared_ptr<OutputManager> &)> & c) :
    Pimp<IPCInputManager>(e, c)
{
}

IPCInputManager::~IPCInputManager()
{
    char c('x');
    if (1 != write(_imp->finished_pipe.write_fd(), &c, 1))
        throw InternalError(PALUDIS_HERE, "write failed");
}

const std::function<std::string (const std::string &)>
IPCInputManager::pipe_command_handler()
{
    return std::bind(&IPCInputManager::_pipe_command_handler, this, std::placeholders::_1);
}

std::string
IPCInputManager::_pipe_command_handler(const std::string & s)
{
    std::vector<std::string> tokens;
    tokenise_whitespace(s, std::back_inserter(tokens));

    if (tokens.empty())
        throw InternalError(PALUDIS_HERE, "tokens is empty");

    if (tokens[0] == "PING")
    {
        if (tokens.size() != 3 || tokens[1] != "1")
            return "Ebad PING subcommand";
        return "OPONG " + tokens[2];
    }
    else if (tokens[0] == "CREATE")
    {
        if (tokens.size() != 3 || tokens[1] != "1")
            return "Ebad CREATE subcommand";

        std::stringstream stream(tokens[2]);
        Deserialiser deserialiser(_imp->env, stream);
        Deserialisation deserialisation("CreateOutputManagerInfo", deserialiser);
        const std::shared_ptr<CreateOutputManagerInfo> i(CreateOutputManagerInfo::deserialise(deserialisation));

        {
            Lock lock(_imp->mutex);
            if (_imp->output_manager)
                return "Ealready constructed";

            _imp->output_manager = _imp->env->create_output_manager(*i);
            if (_imp->on_create)
                _imp->on_create(_imp->output_manager);
        }

        _imp->copy_thread = std::make_shared<Thread>(std::bind(&IPCInputManager::_copy_thread, this));

        return "O 1 " + stringify(_imp->stdout_pipe.write_fd()) + " " + stringify(_imp->stderr_pipe.write_fd());
    }
    else if (tokens[0] == "MESSAGE")
    {
        if (tokens.size() < 3 || tokens[1] != "1")
            return "Ebad MESSAGE subcommand";

        MessageType t(destringify<MessageType>(tokens[2]));
        std::string m(join(next(tokens.begin(), 3), tokens.end(), " "));
        _imp->output_manager->message(t, m);

        return "O";
    }
    else if (tokens[0] == "SUCCEEDED")
    {
        if (tokens.size() != 2 || tokens[1] != "1")
            return "Ebad SUCCEEDED subcommand";
        _imp->output_manager->succeeded();

        return "O";
    }
    else if (tokens[0] == "NOTHING_MORE_TO_COME")
    {
        if (tokens.size() != 2 || tokens[1] != "1")
            return "Ebad NOTHING_MORE_TO_COME subcommand";
        _imp->output_manager->nothing_more_to_come();

        return "O";
    }
    else if (tokens[0] == "FINISHED")
    {
        if (tokens.size() != 2 || tokens[1] != "1")
            return "Ebad FINISHED subcommand";

        char c('x');
        if (1 != write(_imp->finished_pipe.write_fd(), &c, 1))
            throw InternalError(PALUDIS_HERE, "write failed");

        return "O";
    }
    else
        return "Eunknown pipe command";
}

void
IPCInputManager::_copy_thread()
{
    bool done(false);
    while (! done)
    {
        fd_set read_fds;
        FD_ZERO(&read_fds);
        int max_fd(0);

        FD_SET(_imp->stdout_pipe.read_fd(), &read_fds);
        max_fd = std::max(max_fd, _imp->stdout_pipe.read_fd());

        FD_SET(_imp->stderr_pipe.read_fd(), &read_fds);
        max_fd = std::max(max_fd, _imp->stderr_pipe.read_fd());

        FD_SET(_imp->finished_pipe.read_fd(), &read_fds);
        max_fd = std::max(max_fd, _imp->finished_pipe.read_fd());

        int retval(pselect(max_fd + 1, &read_fds, 0, 0, 0, 0));
        if (-1 == retval)
            throw InternalError(PALUDIS_HERE, "pselect failed: " + stringify(strerror(errno)));

        bool got_output(false);
        char buf[1024];

        if (FD_ISSET(_imp->stdout_pipe.read_fd(), &read_fds))
        {
            got_output = true;
            int r;
            if (((r = read(_imp->stdout_pipe.read_fd(), buf, 1024))) > 0)
                _imp->output_manager->stdout_stream() <<  std::string(buf, r);
        }

        if (FD_ISSET(_imp->stderr_pipe.read_fd(), &read_fds))
        {
            got_output = true;
            int r;
            if (((r = read(_imp->stderr_pipe.read_fd(), buf, 1024))) > 0)
                _imp->output_manager->stderr_stream() <<  std::string(buf, r);
        }

        if (got_output)
            continue;

        if (FD_ISSET(_imp->finished_pipe.read_fd(), &read_fds))
        {
            int r;
            if (((r = read(_imp->finished_pipe.read_fd(), buf, 1024))) > 0)
                done = true;
        }
    }
}

const std::shared_ptr<OutputManager>
IPCInputManager::underlying_output_manager_if_constructed() const
{
    Lock lock(_imp->mutex);
    return _imp->output_manager;
}

namespace paludis
{
    template <>
    struct Imp<OutputManagerFromIPC>
    {
        const Environment * const env;
        const std::shared_ptr<const PackageID> id;
        const OutputExclusivity exclusivity;
        const ClientOutputFeatures client_output_features;

        int read_fd, write_fd;

        std::shared_ptr<OutputManager> result;

        Imp(const Environment * const e, const std::shared_ptr<const PackageID> & i,
                const OutputExclusivity x, const ClientOutputFeatures & c) :
            env(e),
            id(i),
            exclusivity(x),
            client_output_features(c),
            read_fd(destringify<int>(getenv_with_default("PALUDIS_IPC_READ_FD", "-1"))),
            write_fd(destringify<int>(getenv_with_default("PALUDIS_IPC_WRITE_FD", "-1")))
        {
            if (-1 == read_fd || -1 == write_fd)
                throw InternalError(PALUDIS_HERE, "no pipe command handler available");
            unsetenv("PALUDIS_IPC_READ_FD");
            unsetenv("PALUDIS_IPC_WRITE_FD");

            if (0 != ::fcntl(read_fd, F_SETFD, FD_CLOEXEC))
                throw InternalError(PALUDIS_HERE, "fcntl failed");
            if (0 != ::fcntl(write_fd, F_SETFD, FD_CLOEXEC))
                throw InternalError(PALUDIS_HERE, "fcntl failed");
        }
    };
}

OutputManagerFromIPC::OutputManagerFromIPC(const Environment * const e,
        const std::shared_ptr<const PackageID> & i,
        const OutputExclusivity x,
        const ClientOutputFeatures & c) :
    Pimp<OutputManagerFromIPC>(e, i, x, c)
{
}

OutputManagerFromIPC::~OutputManagerFromIPC()
{
}

const std::shared_ptr<OutputManager>
OutputManagerFromIPC::operator() (const Action & a)
{
    if (! _imp->result)
    {
        CreateOutputManagerForPackageIDActionInfo info(_imp->id, a, _imp->exclusivity, _imp->client_output_features);
        _imp->result = std::make_shared<IPCOutputManager>(_imp->read_fd, _imp->write_fd, info);
    }
    return _imp->result;
}

const std::shared_ptr<OutputManager>
OutputManagerFromIPC::output_manager_if_constructed()
{
    return _imp->result;
}

void
OutputManagerFromIPC::construct_standard_if_unconstructed()
{
    if (! _imp->result)
    {
        Log::get_instance()->message("output_manager_from_ipc.constructed_standard", ll_warning, lc_context)
            << "No output manager available, creating a standard output manager. This is probably a bug.";
        _imp->result = std::make_shared<StandardOutputManager>();
    }
}

template class Pimp<IPCOutputManager>;
template class Pimp<IPCInputManager>;
template class Pimp<OutputManagerFromIPC>;