• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *                 Copyright Lingxi Li 2015.
3  *              Copyright Andrey Semashev 2015.
4  * Distributed under the Boost Software License, Version 1.0.
5  *    (See accompanying file LICENSE_1_0.txt or copy at
6  *          http://www.boost.org/LICENSE_1_0.txt)
7  */
8 /*!
9  * \file   permissions.hpp
10  * \author Lingxi Li
11  * \author Andrey Semashev
12  * \date   14.10.2015
13  *
14  * The header contains an abstraction wrapper for security permissions.
15  */
16 
17 #ifndef BOOST_LOG_UTILITY_PERMISSIONS_HPP_INCLUDED_
18 #define BOOST_LOG_UTILITY_PERMISSIONS_HPP_INCLUDED_
19 
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/detail/header.hpp>
22 
23 #ifdef BOOST_HAS_PRAGMA_ONCE
24 #pragma once
25 #endif
26 
27 #ifdef BOOST_WINDOWS
28 extern "C" {
29 struct _SECURITY_ATTRIBUTES;
30 }
31 #endif // BOOST_WINDOWS
32 
33 namespace boost {
34 
35 #ifdef BOOST_WINDOWS
36 #if defined(BOOST_GCC) && BOOST_GCC >= 40600
37 #pragma GCC diagnostic push
38 // type attributes ignored after type is already defined
39 #pragma GCC diagnostic ignored "-Wattributes"
40 #endif
41 namespace winapi {
42 struct BOOST_LOG_MAY_ALIAS _SECURITY_ATTRIBUTES;
43 }
44 #if defined(BOOST_GCC) && BOOST_GCC >= 40600
45 #pragma GCC diagnostic pop
46 #endif
47 #endif
48 
49 namespace interprocess {
50 class permissions;
51 } // namespace interprocess
52 
53 BOOST_LOG_OPEN_NAMESPACE
54 
55 /*!
56  * \brief Access permissions wrapper.
57  *
58  * On Windows platforms, it represents a pointer to \c SECURITY_ATTRIBUTES. The user is responsible
59  * for allocating and reclaiming resources associated with the pointer, \c permissions instance does
60  * not own them.
61  *
62  * On POSIX platforms, it represents a \c mode_t value.
63  */
64 class permissions
65 {
66 public:
67 #if defined(BOOST_LOG_DOXYGEN_PASS)
68     //! The type of security permissions, specific to the operating system
69     typedef implementation_defined native_type;
70 #elif defined(BOOST_WINDOWS)
71     typedef ::_SECURITY_ATTRIBUTES* native_type;
72 #else
73     // Equivalent to POSIX mode_t
74     typedef unsigned int native_type;
75 #endif
76 
77 #if !defined(BOOST_LOG_DOXYGEN_PASS)
78 private:
79     native_type m_perms;
80 #endif
81 
82 public:
83     /*!
84      * Default constructor. The method constructs an object that represents
85      * a null \c SECURITY_ATTRIBUTES pointer on Windows platforms, and a
86      * \c mode_t value \c 0644 on POSIX platforms.
87      */
permissions()88     permissions() BOOST_NOEXCEPT
89     {
90         set_default();
91     }
92 
93     /*!
94      * Copy constructor.
95      */
permissions(permissions const & that)96     permissions(permissions const& that) BOOST_NOEXCEPT : m_perms(that.m_perms)
97     {
98     }
99 
100     /*!
101      * Copy assignment.
102      */
operator =(permissions const & that)103     permissions& operator=(permissions const& that) BOOST_NOEXCEPT
104     {
105         m_perms = that.m_perms;
106         return *this;
107     }
108 
109     /*!
110      * Initializing constructor.
111      */
permissions(native_type perms)112     permissions(native_type perms) BOOST_NOEXCEPT : m_perms(perms)
113     {
114     }
115 
116 #ifdef BOOST_WINDOWS
permissions(boost::winapi::_SECURITY_ATTRIBUTES * perms)117     permissions(boost::winapi::_SECURITY_ATTRIBUTES* perms) BOOST_NOEXCEPT : m_perms(reinterpret_cast< native_type >(perms))
118     {
119     }
120 #endif
121 
122     /*!
123      * Initializing constructor.
124      */
125     BOOST_LOG_API permissions(boost::interprocess::permissions const& perms) BOOST_NOEXCEPT;
126 
127 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
128     /*!
129      * Move constructor.
130      */
permissions(permissions && that)131     permissions(permissions&& that) BOOST_NOEXCEPT : m_perms(that.m_perms)
132     {
133         that.set_default();
134     }
135 
136     /*!
137      * Move assignment.
138      */
operator =(permissions && that)139     permissions& operator=(permissions&& that) BOOST_NOEXCEPT
140     {
141         m_perms = that.m_perms;
142         that.set_default();
143         return *this;
144     }
145 #endif
146 
147     /*!
148      * Sets permissions from the OS-specific permissions.
149      */
set_native(native_type perms)150     void set_native(native_type perms) BOOST_NOEXCEPT
151     {
152         m_perms = perms;
153     }
154 
155     /*!
156      * Returns the underlying OS-specific permissions.
157      */
get_native() const158     native_type get_native() const BOOST_NOEXCEPT
159     {
160         return m_perms;
161     }
162 
163     /*!
164      * Sets the default permissions, which are equivalent to \c NULL \c SECURITY_ATTRIBUTES
165      * on Windows and \c 0644 on POSIX platforms.
166      */
set_default()167     void set_default() BOOST_NOEXCEPT
168     {
169 #if defined(BOOST_WINDOWS)
170         m_perms = 0;
171 #else
172         m_perms = 0644;
173 #endif
174     }
175 
176     /*!
177      * Sets unrestricted permissions, which are equivalent to \c SECURITY_ATTRIBUTES with \c NULL DACL
178      * on Windows and \c 0666 on POSIX platforms.
179      */
set_unrestricted()180     void set_unrestricted()
181     {
182 #if defined(BOOST_WINDOWS)
183         m_perms = get_unrestricted_security_attributes();
184 #else
185         m_perms = 0666;
186 #endif
187     }
188 
189     /*!
190      * The method swaps the object with \a that.
191      *
192      * \param that The other object to swap with.
193      */
swap(permissions & that)194     void swap(permissions& that) BOOST_NOEXCEPT
195     {
196         native_type perms = m_perms;
197         m_perms = that.m_perms;
198         that.m_perms = perms;
199     }
200 
201     //! Swaps the two \c permissions objects.
swap(permissions & a,permissions & b)202     friend void swap(permissions& a, permissions& b) BOOST_NOEXCEPT
203     {
204         a.swap(b);
205     }
206 
207 #if !defined(BOOST_LOG_DOXYGEN_PASS) && defined(BOOST_WINDOWS)
208 private:
209     static BOOST_LOG_API native_type get_unrestricted_security_attributes();
210 #endif
211 };
212 
213 BOOST_LOG_CLOSE_NAMESPACE // namespace log
214 
215 } // namespace boost
216 
217 #include <boost/log/detail/footer.hpp>
218 
219 #endif // BOOST_LOG_UTILITY_PERMISSIONS_HPP_INCLUDED_
220