Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

PwGrp.C

Go to the documentation of this file.
00001 // Copyright (C) 2005, Vesta Free Software Project
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 #if defined (__linux__)
00020 // Needed to get PTHREAD_MUTEX_RECURSIVE on older platforms (RH7)
00021 #define _XOPEN_SOURCE 600
00022 #include <features.h>
00023 #endif
00024 
00025 #include <pwd.h>
00026 #include <grp.h>
00027 #include <sys/types.h>
00028 
00029 #include "PwGrp.H"
00030 
00031 // Define mutex for wrappers around following functions: getpwent(),
00032 // getgrent(), getpwuid(), getpwnam(), getgrgid(), getgrnam()
00033 static Basics::mutex pwGrp_mu(PTHREAD_MUTEX_RECURSIVE);
00034 
00035 // Group class 
00036 // functions implementations 
00037 void OS::Group::clear()
00038 { // clear group members sequence
00039   int gr_mem_num = members.size();
00040   for(int i = 0; i < gr_mem_num; i++) {
00041     members.remhi();
00042   }  
00043 }
00044 
00045 // PasswdIter class
00046 // functions implementations
00047 OS::PasswdIter::PasswdIter()
00048 {
00049   pwGrp_mu.lock();
00050   setpwent();
00051 }
00052 
00053 OS::PasswdIter::~PasswdIter()
00054 {
00055   endpwent();
00056   pwGrp_mu.unlock();
00057 }
00058 
00059 bool OS::PasswdIter::Next(/*OUT*/OS::Passwd& passwd)
00060 {
00061   struct passwd* pwent;
00062   pwent = getpwent();
00063   if(pwent != 0) {
00064     passwd.name = pwent->pw_name;
00065     passwd.uid = pwent->pw_uid;
00066     passwd.gid = pwent->pw_gid;
00067     return true;
00068   }
00069   return false;
00070 }
00071 
00072 // GroupIter class
00073 // functions implementations
00074 OS::GroupIter::GroupIter()
00075 {
00076   pwGrp_mu.lock();
00077   setgrent();
00078 }
00079 
00080 OS::GroupIter::~GroupIter()
00081 {
00082   endgrent();
00083   pwGrp_mu.unlock();
00084 }
00085 
00086 bool OS::GroupIter::Next(/*OUT*/OS::Group& group)
00087 {
00088   struct group* grent;
00089   grent = getgrent();
00090   if(grent != 0) {
00091     group.clear();
00092     group.name = grent->gr_name;
00093     group.gid = grent->gr_gid;
00094     char** grmem;
00095     for(grmem = grent->gr_mem; *grmem != NULL; grmem++) {
00096       group.members.addhi(*grmem);
00097     }
00098     return true;
00099   }
00100   return false;
00101 }
00102 
00103 // getpwnam(3) wrapper
00104 bool OS::getPwNam(const Text& user_name, /*OUT*/OS::Passwd& passwd)
00105 {
00106   pwGrp_mu.lock();
00107   bool result = false;
00108   struct passwd* pwent;
00109   pwent = getpwnam(user_name.cchars());
00110   if(pwent != 0) {
00111     passwd.name = pwent->pw_name;
00112     passwd.uid = pwent->pw_uid;
00113     passwd.gid = pwent->pw_gid;
00114     result = true;
00115   }
00116   pwGrp_mu.unlock();
00117   return result;
00118 }
00119 
00120 // getpwuid(3) wrapper
00121 bool OS::getPwUid(uid_t uid, /*OUT*/OS::Passwd& passwd)
00122 {
00123   pwGrp_mu.lock();
00124   bool result = false;
00125   struct passwd* pwent;
00126   pwent = getpwuid(uid);
00127   if(pwent != 0) {
00128     passwd.name = pwent->pw_name;
00129     passwd.uid = pwent->pw_uid;
00130     passwd.gid = pwent->pw_gid;
00131     result = true;
00132   }
00133   pwGrp_mu.unlock();
00134   return result;
00135 }
00136 
00137 // getgrnam(3) wrapper
00138 bool OS::getGrNam(const Text& user_name, /*OUT*/OS::Group& group)
00139 {
00140   pwGrp_mu.lock();
00141   bool result = false;
00142   struct group* grent;
00143   grent = getgrnam(user_name.cchars());
00144   if(grent != 0) {
00145     group.clear();
00146     group.name = grent->gr_name;
00147     group.gid = grent->gr_gid;
00148     char** grmem;
00149     for(grmem = grent->gr_mem; *grmem != NULL; grmem++) {
00150       group.members.addhi(*grmem);
00151     }
00152     result = true;
00153   }
00154   pwGrp_mu.unlock();
00155   return result;
00156 }
00157 
00158 // getgrgid(3) wrapper
00159 bool OS::getGrGid(gid_t gid, /*OUT*/OS::Group& group)
00160 {
00161   pwGrp_mu.lock();
00162   bool result = false;
00163   struct group* grent;
00164   grent = getgrgid(gid);
00165   if(grent != 0) {
00166     group.clear();
00167     group.name = grent->gr_name;
00168     group.gid = grent->gr_gid;
00169     char** grmem;
00170     for(grmem = grent->gr_mem; *grmem != NULL; grmem++) {
00171       group.members.addhi(*grmem);
00172     }
00173     result = true;
00174   }
00175   pwGrp_mu.unlock();
00176   return result;
00177 }
00178 

Generated on Mon May 8 00:48:30 2006 for Vesta by  doxygen 1.4.2