19 #ifndef ROSTLAB_CXXGRP
20 #define ROSTLAB_CXXGRP 1
22 #include <boost/format.hpp>
27 #include <sys/types.h>
44 inline cxx_group(
const std::string& __gr_name );
45 inline cxx_group(
const std::string& __gr_name,
const std::string& __gr_passwd, gid_t __gr_gid,
const std::vector<std::string>& __gr_mem ) :
47 inline cxx_group(
const std::string& __gr_name,
const std::string& __gr_passwd, gid_t __gr_gid,
char** __gr_mem ) :
57 inline gid_t
getgrnam_r(
const std::string& __gname );
71 size_t buflen = sysconf( _SC_GETGR_R_SIZE_MAX );
72 char* buf =
static_cast<char *
>( malloc(buflen) );
if(!buf)
throw runtime_error( bo::str( bo::format(
"failed to allocate %d bytes") % buflen ) );
77 _errno =
::getgrgid_r( __gid, &gbuf, buf, buflen, &gbufp );
78 if( _errno == ERANGE )
81 buf =
static_cast<char *
>( realloc( buf, buflen ) );
if(!buf)
throw runtime_error( bo::str( bo::format(
"failed to allocate %d bytes") % buflen ) );
83 else if( _errno )
throw runtime_error( bo::str( bo::format(
"failed to getgrgid_r '%d': %s") % __gid % strerror( errno ) ) );
86 if( gbufp == NULL )
throw gid_not_found_error( bo::str( bo::format(
"gid '%d' not found") % __gid ) );
88 __group =
cxx_group( gbuf.gr_name, gbuf.gr_passwd, gbuf.gr_gid, gbuf.gr_mem );
95 free(buf); buf = NULL;
112 size_t buflen = sysconf( _SC_GETGR_R_SIZE_MAX );
113 char* buf =
static_cast<char *
>( malloc(buflen) );
if(!buf)
throw runtime_error( bo::str( bo::format(
"failed to allocate %d bytes") % buflen ) );
118 _errno =
::getgrnam_r( __gname.c_str(), &gbuf, buf, buflen, &gbufp );
119 if( _errno == ERANGE )
122 buf =
static_cast<char *
>( realloc( buf, buflen ) );
if(!buf)
throw runtime_error( bo::str( bo::format(
"failed to allocate %d bytes") % buflen ) );
124 else if( _errno )
throw runtime_error( bo::str( bo::format(
"failed to getgrnam_r '%s': %s") % __gname % strerror( errno ) ) );
127 if( gbufp == NULL )
throw gname_not_found_error( bo::str( bo::format(
"gname '%s' not found") % __gname ) );
129 __group =
cxx_group( gbuf.gr_name, gbuf.gr_passwd, gbuf.gr_gid, gbuf.gr_mem );
136 free(buf); buf = NULL;
gid_not_found_error(const std::string &what)
gname_not_found_error(const std::string &what)
runtime_error(const std::string &__msg)
std::string getgrgid_r(gid_t __gid)
gid_t getgrnam_r(const std::string &__gname)
std::vector< std::string > gr_mem
cxx_group(const std::string &__gr_name, const std::string &__gr_passwd, gid_t __gr_gid, char **__gr_mem)
cxx_group(const std::string &__gr_name, const std::string &__gr_passwd, gid_t __gr_gid, const std::vector< std::string > &__gr_mem)