File: impl_linux.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (101 lines) | stat: -rw-r--r-- 1,758 bytes parent folder | download | duplicates (3)
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
#include "utils/platform/unix/interface.h"
#include "utils/platform/platform.h"

#include <csignal>
#include <sys/inotify.h>
#include <sys/prctl.h>
#include <unistd.h>

namespace TangoTest::platform
{

std::vector<std::string> default_env()
{
    return {};
}

namespace unix
{
struct FileWatcher::Impl
{
    explicit Impl(const char *filename)
    {
        fd = inotify_init();
        if(fd == -1)
        {
            throw_strerror("inotify_init()");
        }

        if(inotify_add_watch(fd, filename, IN_MODIFY) == -1)
        {
            throw_strerror("inotify_add_watch(\"", filename, "\")");
        }
    }

    ~Impl()
    {
        ::close(fd);
    }

    void pop_event()
    {
        struct inotify_event ev;
        while(true)
        {
            if(read(fd, &ev, sizeof(ev)) == -1)
            {
                if(errno == EINTR)
                {
                    continue;
                }

                throw_strerror("read()");
            }

            break;
        }
    }

    int fd = -1;
};

FileWatcher::FileWatcher(const char *filename) :
    m_pimpl{std::make_unique<FileWatcher::Impl>(filename)}
{
}

FileWatcher::~FileWatcher() { }

void FileWatcher::start_watching() { }

void FileWatcher::stop_watching() { }

int FileWatcher::get_file_descriptor()
{
    return m_pimpl->fd;
}

void FileWatcher::cleanup_in_child()
{
    m_pimpl.reset(nullptr);
}

void FileWatcher::pop_event()
{
    m_pimpl->pop_event();
}

void kill_self_on_parent_death(pid_t ppid)
{
    prctl(PR_SET_PDEATHSIG, SIGTERM);

    // Our parent might have died after the call to fork(), but before the call
    // to prctl()
    if(getppid() != ppid)
    {
        exit(0);
    }
}

} // namespace unix
} // namespace TangoTest::platform