pion-net  4.0.9
PionAdminRights.cpp
1 // -----------------------------------------------------------------------
2 // pion-common: a collection of common libraries used by the Pion Platform
3 // -----------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #include <pion/PionAdminRights.hpp>
11 
12 #ifndef _MSC_VER
13  #include <sys/types.h>
14  #include <unistd.h>
15 #endif
16 
17 
18 namespace pion { // begin namespace pion
19 
20 
21 // static members of PionAdminRights
22 
23 const boost::int16_t PionAdminRights::ADMIN_USER_ID = 0;
24 boost::mutex PionAdminRights::m_mutex;
25 
26 
27 // PionAdminRights member functions
28 
30  : m_logger(PION_GET_LOGGER("pion.PionAdminRights")),
31  m_lock(m_mutex), m_user_id(-1), m_has_rights(false), m_use_log(use_log)
32 {
33 #ifndef _MSC_VER
34  m_user_id = geteuid();
35  if ( seteuid(ADMIN_USER_ID) != 0 ) {
36  if (m_use_log)
37  PION_LOG_ERROR(m_logger, "Unable to upgrade to administrative rights");
38  m_lock.unlock();
39  return;
40  } else {
41  m_has_rights = true;
42  if (m_use_log)
43  PION_LOG_DEBUG(m_logger, "Upgraded to administrative rights");
44  }
45 #endif
46 }
47 
49 {
50 #ifndef _MSC_VER
51  if (m_has_rights) {
52  if ( seteuid(m_user_id) == 0 ) {
53  if (m_use_log)
54  PION_LOG_DEBUG(m_logger, "Released administrative rights");
55  } else {
56  if (m_use_log)
57  PION_LOG_ERROR(m_logger, "Unable to release administrative rights");
58  }
59  m_has_rights = false;
60  m_lock.unlock();
61  }
62 #endif
63 }
64 
65 
66 } // end namespace pion
67