summaryrefslogtreecommitdiff
path: root/src/common/include/mount-monitor.h
blob: 3226e6b556620d59e70a1ca7e645b167aeac27d8 (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
/*
 * Copyright (c) 2017-2020 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * This file is licensed under the terms of MIT License or the Apache License
 * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
 * See the LICENSE file or the notice below for Apache License Version 2.0
 * details.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * @file        mount-monitor.h
 * @author      Rafal Krypa <r.krypa@samsung.com>
 * @version     1.0
 * @brief       Monitor mount/umount events
 */

#pragma once

#include <libmount.h>

#include <atomic>
#include <functional>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

namespace SecurityManager {

class MntFS {
public:
    MntFS(const libmnt_fs *fs);
    ~MntFS();

    MntFS(const MntFS &) = delete;
    MntFS(MntFS &&other);
    MntFS & operator=(const MntFS &) = delete;
    MntFS && operator=(MntFS &&other);

    std::string getSource() const;
    std::string getTarget() const;

private:
    struct libmnt_fs *m_fs = nullptr;
};

class MntDiffEntry
{
public:

    enum class Type {
        MOUNT = MNT_TABDIFF_MOUNT,
        UMOUNT = MNT_TABDIFF_UMOUNT,
        MOVE = MNT_TABDIFF_MOVE,
        REMOUNT = MNT_TABDIFF_REMOUNT
    };

    Type m_type;
    MntFS m_oldFS, m_newFS;

    MntDiffEntry(int type, const libmnt_fs *oldFS, const libmnt_fs *newFS) :
        m_type(static_cast<Type>(type)), m_oldFS(oldFS), m_newFS(newFS)
    {}
};

class MntTable {
public:
    MntTable(const std::string &file = "/proc/self/mountinfo");
    ~MntTable();

    MntTable(const MntTable &) = delete;
    MntTable(MntTable &&other);
    MntTable & operator=(const MntTable &) = delete;
    MntTable & operator=(MntTable &&other);

    std::vector<MntDiffEntry> diff(const MntTable &other);

private:
    libmnt_table *m_table = nullptr;
};

class MntMonitor
{
public:
    MntMonitor(const std::function<void(MntDiffEntry &)> &callback);
    ~MntMonitor();

private:
    void threadNotifyPut();
    void threadNotifyGet();
    void run();

    MntTable m_table;
    struct libmnt_monitor *m_monitor;
    int m_monitorFd;

    std::function<void(MntDiffEntry &)> m_callback;

    std::mutex m_monitorMut;
    std::thread m_thread;
    std::atomic<bool> m_terminate;
    const int m_eventFd;
};

} // namespace SecurityManager