# common/src/PionAdminRights.cpp

    00001 // -----------------------------------------------------------------------
    00002 // pion-common: a collection of common libraries used by the Pion Platform
    00003 // -----------------------------------------------------------------------
    00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
    00005 //
    00006 // Distributed under the Boost Software License, Version 1.0.
    00007 // See http://www.boost.org/LICENSE_1_0.txt
    00008 //
    00009
    00010 #include <pion/PionAdminRights.hpp>
    00011
    00012 #ifndef _MSC_VER
    00013     #include <sys/types.h>
    00014     #include <unistd.h>
    00015 #endif
    00016
    00017
    00018 namespace pion {    // begin namespace pion
    00019
    00020
    00021 // static members of PionAdminRights
    00022
    00023 const boost::int16_t            PionAdminRights::ADMIN_USER_ID = 0;
    00024 boost::mutex                    PionAdminRights::m_mutex;
    00025
    00026
    00027 // PionAdminRights member functions
    00028
    00029 PionAdminRights::PionAdminRights(bool use_log)
    00030     : m_logger(PION_GET_LOGGER("pion.PionAdminRights")),
    00031     m_lock(m_mutex), m_user_id(-1), m_has_rights(false), m_use_log(use_log)
    00032 {
    00033 #ifndef _MSC_VER
    00034     m_user_id = geteuid();
    00035     if ( seteuid(ADMIN_USER_ID) != 0 ) {
    00036         if (m_use_log)
    00037             PION_LOG_ERROR(m_logger, "Unable to upgrade to administrative rights");
    00038         m_lock.unlock();
    00039         return;
    00040     } else {
    00041         m_has_rights = true;
    00042         if (m_use_log)
    00043             PION_LOG_DEBUG(m_logger, "Upgraded to administrative rights");
    00044     }
    00045 #endif
    00046 }
    00047
    00048 void PionAdminRights::release(void)
    00049 {
    00050 #ifndef _MSC_VER
    00051     if (m_has_rights) {
    00052         if ( seteuid(m_user_id) == 0 ) {
    00053             if (m_use_log)
    00054                 PION_LOG_DEBUG(m_logger, "Released administrative rights");
    00055         } else {
    00056             if (m_use_log)
    00057                 PION_LOG_ERROR(m_logger, "Unable to release administrative rights");
    00058         }
    00059         m_has_rights = false;
    00060         m_lock.unlock();
    00061     }
    00062 #endif
    00063 }
    00064
    00065
    00066 }   // end namespace pion
    00067