Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:31

0001 /* boost uuid/detail/random_provider_posix implementation
0002 *
0003 * Copyright Jens Maurer 2000
0004 * Copyright 2007 Andy Tompkins.
0005 * Copyright Steven Watanabe 2010-2011
0006 * Copyright 2017 James E. King III
0007 *
0008 * Distributed under the Boost Software License, Version 1.0. (See
0009 * accompanying file LICENSE_1_0.txt or copy at
0010 * https://www.boost.org/LICENSE_1_0.txt)
0011 *
0012 * $Id$
0013 */
0014 
0015 #include <boost/config.hpp>
0016 #include <boost/core/ignore_unused.hpp>
0017 #include <boost/move/core.hpp>
0018 #include <boost/throw_exception.hpp>
0019 #include <boost/uuid/entropy_error.hpp>
0020 #include <cerrno>
0021 #include <cstddef>
0022 #include <fcntl.h>    // open
0023 #include <sys/stat.h>
0024 #include <sys/types.h>
0025 #if defined(BOOST_HAS_UNISTD_H)
0026 #include <unistd.h>
0027 #endif
0028 
0029 #ifndef BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_CLOSE
0030 #define BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_CLOSE ::close
0031 #endif
0032 #ifndef BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_OPEN
0033 #define BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_OPEN ::open
0034 #endif
0035 #ifndef BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_READ
0036 #define BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_READ ::read
0037 #endif
0038 
0039 namespace boost {
0040 namespace uuids {
0041 namespace detail {
0042 
0043 class random_provider_base
0044 {
0045     BOOST_MOVABLE_BUT_NOT_COPYABLE(random_provider_base)
0046 
0047 public:
0048     random_provider_base()
0049       : fd_(-1)
0050     {
0051         int flags = O_RDONLY;
0052 #if defined(O_CLOEXEC)
0053         flags |= O_CLOEXEC;
0054 #endif
0055         fd_ = BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_OPEN("/dev/urandom", flags);
0056 
0057         if (BOOST_UNLIKELY(-1 == fd_))
0058         {
0059             int err = errno;
0060             BOOST_THROW_EXCEPTION(entropy_error(err, "open /dev/urandom"));
0061         }
0062     }
0063 
0064     random_provider_base(BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT : fd_(that.fd_)
0065     {
0066         that.fd_ = -1;
0067     }
0068 
0069     random_provider_base& operator= (BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT
0070     {
0071         destroy();
0072         fd_ = that.fd_;
0073         that.fd_ = -1;
0074         return *this;
0075     }
0076 
0077     ~random_provider_base() BOOST_NOEXCEPT
0078     {
0079         destroy();
0080     }
0081 
0082     //! Obtain entropy and place it into a memory location
0083     //! \param[in]  buf  the location to write entropy
0084     //! \param[in]  siz  the number of bytes to acquire
0085     void get_random_bytes(void *buf, std::size_t siz)
0086     {
0087         std::size_t offset = 0;
0088         while (offset < siz)
0089         {
0090             ssize_t sz = BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_READ(
0091                 fd_, static_cast<char *>(buf) + offset, siz - offset);
0092 
0093             if (BOOST_UNLIKELY(sz < 0))
0094             {
0095                 int err = errno;
0096                 if (err == EINTR)
0097                     continue;
0098                 BOOST_THROW_EXCEPTION(entropy_error(err, "read"));
0099             }
0100 
0101             offset += sz;
0102         }
0103     }
0104 
0105 private:
0106     void destroy() BOOST_NOEXCEPT
0107     {
0108         if (fd_ >= 0)
0109         {
0110             boost::ignore_unused(BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_CLOSE(fd_));
0111         }
0112     }
0113 
0114 private:
0115     int fd_;
0116 };
0117 
0118 } // detail
0119 } // uuids
0120 } // boost