Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2008 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

extern "C" {
#include <sys/types.h>
#include <sys/wait.h>

#include <fcntl.h>
#include <signal.h>
}

#include <cassert>
#include <cstdarg>
#include <cerrno>
#include <cstring>
#include <iostream>

#include "exceptions.hpp"
#include "text.hpp"
#include "process.hpp"

namespace detail = tools::process::detail;
namespace impl = tools::process;
#define IMPL_NAME "tools::process"

// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------

template< class C >
tools::auto_array< const char* >
collection_to_argv(const C& c)
{
    tools::auto_array< const char* > argv(new const char*[c.size() + 1]);

    std::size_t pos = 0;
    for (typename C::const_iterator iter = c.begin(); iter != c.end();
         iter++) {
        argv[pos] = (*iter).c_str();
        pos++;
    }
    assert(pos == c.size());
    argv[pos] = NULL;

    return argv;
}

template< class C >
C
argv_to_collection(const char* const* argv)
{
    C c;

    for (const char* const* iter = argv; *iter != NULL; iter++)
        c.push_back(std::string(*iter));

    return c;
}

static
void
safe_dup(const int oldfd, const int newfd)
{
    if (oldfd != newfd) {
        if (dup2(oldfd, newfd) == -1) {
            throw tools::system_error(IMPL_NAME "::safe_dup",
                                      "Could not allocate file descriptor",
                                      errno);
        } else {
            ::close(oldfd);
        }
    }
}

static
int
const_execvp(const char *file, const char *const *argv)
{
#define UNCONST(a) ((void *)(unsigned long)(const void *)(a))
    return ::execvp(file, (char* const*)(UNCONST(argv)));
#undef UNCONST
}

void
detail::do_exec(void *v)
{
    struct exec_args *ea = reinterpret_cast<struct exec_args *>(v);

    if (ea->m_prehook != NULL)
        ea->m_prehook();

    const int ret = const_execvp(ea->m_prog.c_str(), ea->m_argv.exec_argv());
    const int errnocopy = errno;
    assert(ret == -1);
    std::cerr << "exec(" << ea->m_prog.str() << ") failed: "
              << std::strerror(errnocopy) << "\n";
    std::exit(EXIT_FAILURE);
}

// ------------------------------------------------------------------------
// The "argv_array" type.
// ------------------------------------------------------------------------

impl::argv_array::argv_array(void) :
    m_exec_argv(collection_to_argv(m_args))
{
}

impl::argv_array::argv_array(const char* arg1, ...)
{
    m_args.push_back(arg1);

    {
        va_list ap;
        const char* nextarg;

        va_start(ap, arg1);
        while ((nextarg = va_arg(ap, const char*)) != NULL)
            m_args.push_back(nextarg);
        va_end(ap);
    }

    ctor_init_exec_argv();
}

impl::argv_array::argv_array(const char* const* ca) :
    m_args(argv_to_collection< args_vector >(ca)),
    m_exec_argv(collection_to_argv(m_args))
{
}

impl::argv_array::argv_array(const argv_array& a) :
    m_args(a.m_args),
    m_exec_argv(collection_to_argv(m_args))
{
}

void
impl::argv_array::ctor_init_exec_argv(void)
{
    m_exec_argv = collection_to_argv(m_args);
}

const char* const*
impl::argv_array::exec_argv(void)
    const
{
    return m_exec_argv.get();
}

impl::argv_array::size_type
impl::argv_array::size(void)
    const
{
    return m_args.size();
}

const char*
impl::argv_array::operator[](int idx)
    const
{
    return m_args[idx].c_str();
}

impl::argv_array::const_iterator
impl::argv_array::begin(void)
    const
{
    return m_args.begin();
}

impl::argv_array::const_iterator
impl::argv_array::end(void)
    const
{
    return m_args.end();
}

impl::argv_array&
impl::argv_array::operator=(const argv_array& a)
{
    if (this != &a) {
        m_args = a.m_args;
        m_exec_argv = collection_to_argv(m_args);
    }
    return *this;
}

// ------------------------------------------------------------------------
// The "stream" types.
// ------------------------------------------------------------------------

impl::stream_capture::stream_capture(void)
{
    for (int i = 0; i < 2; i++)
        m_pipefds[i] = -1;
}

impl::stream_capture::~stream_capture(void)
{
    for (int i = 0; i < 2; i++)
        if (m_pipefds[i] != -1)
            ::close(m_pipefds[i]);
}

void
impl::stream_capture::prepare(void)
{
    if (pipe(m_pipefds) == -1)
        throw system_error(IMPL_NAME "::stream_capture::prepare",
                           "Failed to create pipe", errno);
}

int
impl::stream_capture::connect_parent(void)
{
    ::close(m_pipefds[1]); m_pipefds[1] = -1;
    const int fd = m_pipefds[0];
    m_pipefds[0] = -1;
    return fd;
}

void
impl::stream_capture::connect_child(const int fd)
{
    ::close(m_pipefds[0]); m_pipefds[0] = -1;
    if (m_pipefds[1] != fd) {
        safe_dup(m_pipefds[1], fd);
    }
    m_pipefds[1] = -1;
}

impl::stream_connect::stream_connect(const int src_fd, const int tgt_fd) :
    m_src_fd(src_fd), m_tgt_fd(tgt_fd)
{
}

void
impl::stream_connect::prepare(void)
{
}

int
impl::stream_connect::connect_parent(void)
{
    return -1;
}

void
impl::stream_connect::connect_child(const int fd __attribute__((__unused__)))
{
    safe_dup(m_tgt_fd, m_src_fd);
}

impl::stream_inherit::stream_inherit(void)
{
}

void
impl::stream_inherit::prepare(void)
{
}

int
impl::stream_inherit::connect_parent(void)
{
    return -1;
}

void
impl::stream_inherit::connect_child(const int fd __attribute__((__unused__)))
{
}

impl::stream_redirect_fd::stream_redirect_fd(const int fd) :
    m_fd(fd)
{
}

void
impl::stream_redirect_fd::prepare(void)
{
}

int
impl::stream_redirect_fd::connect_parent(void)
{
    return -1;
}

void
impl::stream_redirect_fd::connect_child(const int fd)
{
    safe_dup(m_fd, fd);
}

impl::stream_redirect_path::stream_redirect_path(const tools::fs::path& p) :
    m_path(p)
{
}

void
impl::stream_redirect_path::prepare(void)
{
}

int
impl::stream_redirect_path::connect_parent(void)
{
    return -1;
}

void
impl::stream_redirect_path::connect_child(const int fd)
{
    const int aux = ::open(m_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (aux == -1)
        throw system_error(IMPL_NAME "::stream_redirect_path::connect_child",
                           "Could not create " + m_path.str(), errno);
    else
        safe_dup(aux, fd);
}

// ------------------------------------------------------------------------
// The "status" type.
// ------------------------------------------------------------------------

impl::status::status(int s) :
    m_status(s)
{
}

impl::status::~status(void)
{
}

std::string
impl::status::str(void)
     const
{
    int mutable_status = m_status;
    std::stringstream rv;
    if (WIFEXITED(mutable_status))
	rv << "exit("  << WEXITSTATUS(mutable_status);
    else if (WIFSTOPPED(mutable_status))
	rv << "stopped("  << WSTOPSIG(mutable_status);
    else if (WIFSIGNALED(mutable_status))
	rv << "terminated("  << WTERMSIG(mutable_status);
    if (WCOREDUMP(mutable_status))
	rv << "/core)";
    else
	rv << ")";
    return rv.str();
}

bool
impl::status::exited(void)
    const
{
    int mutable_status = m_status;
    return WIFEXITED(mutable_status);
}

int
impl::status::exitstatus(void)
    const
{
    assert(exited());
    int mutable_status = m_status;
    return WEXITSTATUS(mutable_status);
}

bool
impl::status::signaled(void)
    const
{
    int mutable_status = m_status;
    return WIFSIGNALED(mutable_status);
}

int
impl::status::termsig(void)
    const
{
    assert(signaled());
    int mutable_status = m_status;
    return WTERMSIG(mutable_status);
}

bool
impl::status::coredump(void)
    const
{
    assert(signaled());
    int mutable_status = m_status;
    return WCOREDUMP(mutable_status);
}

// ------------------------------------------------------------------------
// The "child" type.
// ------------------------------------------------------------------------

impl::child::child(const pid_t pid_arg, const int stdout_fd_arg,
                   const int stderr_fd_arg) :
    m_pid(pid_arg),
    m_stdout(stdout_fd_arg),
    m_stderr(stderr_fd_arg),
    m_waited(false)
{
}

impl::child::~child(void)
{
    if (!m_waited) {
        ::kill(m_pid, SIGTERM);
        (void)wait();

        if (m_stdout != -1) {
            ::close(m_stdout); m_stdout = -1;
        }
        if (m_stderr != -1) {
            ::close(m_stderr); m_stderr = -1;
        }
    }
}

impl::status
impl::child::wait(void)
{
    int s;

    if (::waitpid(m_pid, &s, 0) == -1)
        throw system_error(IMPL_NAME "::child::wait", "Failed waiting for "
                           "process " + text::to_string(m_pid), errno);

    if (m_stdout != -1) {
        ::close(m_stdout); m_stdout = -1;
    }
    if (m_stderr != -1) {
        ::close(m_stderr); m_stderr = -1;
    }

    m_waited = true;
    return status(s);
}

pid_t
impl::child::pid(void)
    const
{
    return m_pid;
}

int
impl::child::stdout_fd(void)
{
    return m_stdout;
}

int
impl::child::stderr_fd(void)
{
    return m_stderr;
}

// ------------------------------------------------------------------------
// Free functions.
// ------------------------------------------------------------------------

void
detail::flush_streams(void)
{
    // This is a weird hack to ensure that the output of the parent process
    // is flushed before executing a child which prevents, for example, the
    // output of the atf-run hooks to appear before the output of atf-run
    // itself.
    //
    // TODO: This should only be executed when inheriting the stdout or
    // stderr file descriptors.  However, the flushing is specific to the
    // iostreams, so we cannot do it from the C library where all the process
    // logic is performed.  Come up with a better design.
    std::cout.flush();
    std::cerr.flush();
}