DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::User Class Reference

User. More...

#include <User.hpp>

Collaboration diagram for OksSystem::User:
[legend]

Public Member Functions

 User () throw ()
 constructor for current user
 User (uid_t user_id) throw ()
 constructor from uid
 User (const std::string &name)
 constructor from name
 User (const User &other) throw ()
 copy constructor
 operator uid_t () const throw ()
 cast to user id
 operator std::string () const
 cast to string
 operator const char * () const
 cast to c-string
uid_t identity () const throw ()
 user-id
const std::string & name () const
 username
const std::string & name_safe () const throw ()
 username - no exception
const std::string & home () const
 home directory name
const std::string & real_name () const
 real name
void setuid () const
 tries to set the uid of current process

Static Public Attributes

static const User ROOT
 constant for root user

Protected Member Functions

void resolve () const
 resolve information from user-id
void resolve_safe () const throw ()
 resolve without throwing exceptions

Protected Attributes

uid_t m_user_id
 the actual user id
std::string m_user_name
 cached user name
std::string m_user_home
 cached user home directory
std::string m_user_real_name
 cached real user name

Detailed Description

User.

This class represents an user

Author
Matthias Wiesmann
Version
1.0

Definition at line 30 of file User.hpp.

Constructor & Destructor Documentation

◆ User() [1/4]

OksSystem::User::User ( )
throw ( )

constructor for current user

Definition at line 27 of file User.cpp.

28{
29 m_user_id = ::getuid();
30} // User
uid_t m_user_id
the actual user id
Definition User.hpp:34

◆ User() [2/4]

OksSystem::User::User ( uid_t user_id)
throw ( )

constructor from uid

Definition at line 32 of file User.cpp.

33{
34 m_user_id = user_id;
35} // User

◆ User() [3/4]

OksSystem::User::User ( const std::string & name)

constructor from name

Definition at line 37 of file User.cpp.

38{
39
40 errno = 0;
41 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
42
43 if (bufSize == -1) {
44 if (errno == 0) {
45 bufSize = 1024;
46 } else {
47 std::string message = "with argument _SC_GETPW_R_SIZE_MAX while getting info about user " + s_name;
48 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "sysconf", message.c_str());
49 }
50 }
51
52 struct passwd pwd;
53 struct passwd* result;
54 char* buf = new char[bufSize];
55 errno = 0;
56 int res = ::getpwnam_r(s_name.c_str(), &pwd, buf, bufSize, &result);
57 if (res == 0) {
58 if (result != 0) {
59 m_user_id = result->pw_uid;
60 } else {
61 delete[] buf;
62 std::string eMsg = "User " + s_name + " not found";
63 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "getpwnam_r", eMsg.c_str());
64 }
65 } else {
66 delete[] buf;
67 std::string message = "while getting info about user " + s_name;
68 throw OksSystem::OksSystemCallIssue(ERS_HERE, res, "getpwnam_r", message.c_str());
69 }
70 delete[] buf;
71
72 m_user_name = s_name;
73} // User
#define ERS_HERE
std::string m_user_name
cached user name
Definition User.hpp:35
message(message)
Definition __init__.py:84

◆ User() [4/4]

OksSystem::User::User ( const User & other)
throw ( )

copy constructor

Definition at line 75 of file User.cpp.

76{
77 m_user_id = other.m_user_id;
78 m_user_name = other.m_user_name;
79} // User

Member Function Documentation

◆ home()

const std::string & OksSystem::User::home ( ) const

home directory name

Gets the home directory of the user

Returns
path to home directory

Definition at line 241 of file User.cpp.

242{
243 if (m_user_home.empty()) {
244 resolve();
245 }
246 return m_user_home;
247} // home
void resolve() const
resolve information from user-id
Definition User.cpp:122
std::string m_user_home
cached user home directory
Definition User.hpp:36

◆ identity()

uid_t OksSystem::User::identity ( ) const
throw ( )

user-id

Returns the user_id for user

Returns
user-id

Definition at line 110 of file User.cpp.

111{
112 return m_user_id;
113} // identity

◆ name()

const std::string & OksSystem::User::name ( ) const

username

Gets the (short) name of the user

Returns
the name of user

Definition at line 215 of file User.cpp.

216{
217 if (m_user_name.empty()) {
218 resolve();
219 }
220 return m_user_name;
221} // name

◆ name_safe()

const std::string & OksSystem::User::name_safe ( ) const
throw ( )

username - no exception

Gets the (short) name of the user - no error is thrown in case of problem

Returns
the name of user

Definition at line 228 of file User.cpp.

229{
230 if (m_user_name.empty()) {
231 resolve_safe();
232 }
233 return m_user_name;
234} // name
void resolve_safe() const
resolve without throwing exceptions
Definition User.cpp:170

◆ operator const char *()

OksSystem::User::operator const char * ( ) const

cast to c-string

Definition at line 94 of file User.cpp.

95{
96 if (m_user_name.empty()) {
97 resolve();
98 }
99 return m_user_name.c_str();
100} // char*

◆ operator std::string()

OksSystem::User::operator std::string ( ) const

cast to string

Definition at line 89 of file User.cpp.

90{
91 return name();
92} // string
const std::string & name() const
username
Definition User.cpp:215

◆ operator uid_t()

OksSystem::User::operator uid_t ( ) const
throw ( )

cast to user id

Definition at line 84 of file User.cpp.

85{
86 return m_user_id;
87} // uid_t

◆ real_name()

const std::string & OksSystem::User::real_name ( ) const

real name

Gets the 'real name' of the user

Returns
real name

Definition at line 254 of file User.cpp.

255{
256 if (m_user_real_name.empty()) {
257 resolve();
258 }
259 return m_user_real_name;
260} // real_name
std::string m_user_real_name
cached real user name
Definition User.hpp:37

◆ resolve()

void OksSystem::User::resolve ( ) const
protected

resolve information from user-id

This method is responsible for filling in the mutable fields of the class when needed, this is done by calling the getpwuid function, all fields of the object are then filled in.

Exceptions
OksSystem::OksSystemCallIssueif the user information cannot be found

Definition at line 122 of file User.cpp.

123{
124
125 errno = 0;
126 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
127
128 if (bufSize == -1) {
129 if (errno == 0) {
130 bufSize = 1024;
131 } else {
132 std::ostringstream message;
133 message << "with argument _SC_GETPW_R_SIZE_MAX while getting info about user with id " << m_user_id;
134 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "sysconf", message.str().c_str());
135 }
136 }
137
138 struct passwd pwd;
139 struct passwd* result;
140 char* buf = new char[bufSize];
141 errno = 0;
142 int res = ::getpwuid_r(m_user_id, &pwd, buf, bufSize, &result);
143 if (res == 0) {
144 if (result != 0) {
145 m_user_name = std::string(result->pw_name);
146 m_user_home = std::string(result->pw_dir);
147 m_user_real_name = std::string(result->pw_gecos);
148 } else {
149 delete[] buf;
150 std::ostringstream eMsg;
151 eMsg << "User " << m_user_id << " not found";
152 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "getpwuid_r", eMsg.str().c_str());
153 }
154 } else {
155 delete[] buf;
156 std::ostringstream message;
157 message << "while getting info about user with id " << m_user_id;
158 throw OksSystem::OksSystemCallIssue(ERS_HERE, res, "getpwnam_r", message.str().c_str());
159 }
160 delete[] buf;
161
162} //

◆ resolve_safe()

void OksSystem::User::resolve_safe ( ) const
throw ( )
protected

resolve without throwing exceptions

This method is the same as resolve() but does not throw exceptions. Instead the method silently fails

See also
OksSystem::User::resolve()

Definition at line 170 of file User.cpp.

171{
172
173 errno = 0;
174 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
175
176 if (bufSize == -1) {
177 if (errno == 0) {
178 bufSize = 1024;
179 } else {
180 m_user_name = "unknown";
181 m_user_home = "unknown";
182 m_user_real_name = "unknown";
183 return;
184 }
185 }
186
187 struct passwd pwd;
188 struct passwd* result;
189 char* buf = new char[bufSize];
190 int res = ::getpwuid_r(m_user_id, &pwd, buf, bufSize, &result);
191 if (res == 0) {
192 if (result != 0) {
193 m_user_name = std::string(result->pw_name);
194 m_user_home = std::string(result->pw_dir);
195 m_user_real_name = std::string(result->pw_gecos);
196 } else {
197 m_user_name = "unknown";
198 m_user_home = "unknown";
199 m_user_real_name = "unknown";
200 }
201 } else {
202 m_user_name = "unknown";
203 m_user_home = "unknown";
204 m_user_real_name = "unknown";
205 }
206 delete[] buf;
207
208} // resolve_safe

◆ setuid()

void OksSystem::User::setuid ( ) const

tries to set the uid of current process

Sets the user-identity of the current process to this user. This method will only succeed if the current user has sufficient privileges to changed uids (typically because the current user is root).

Definition at line 268 of file User.cpp.

269{
270 const int status = ::setuid(m_user_id);
271 if (status < 0) {
272 std::ostringstream message;
273 message << "while setting the effective user ID to " << m_user_id << "(" << this->name_safe() << ")";
274 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "setuid", message.str().c_str());
275 }
276} // setuid
const std::string & name_safe() const
username - no exception
Definition User.cpp:228
void setuid() const
tries to set the uid of current process
Definition User.cpp:268

Member Data Documentation

◆ m_user_home

std::string OksSystem::User::m_user_home
mutableprotected

cached user home directory

Definition at line 36 of file User.hpp.

◆ m_user_id

uid_t OksSystem::User::m_user_id
protected

the actual user id

Definition at line 34 of file User.hpp.

◆ m_user_name

std::string OksSystem::User::m_user_name
mutableprotected

cached user name

Definition at line 35 of file User.hpp.

◆ m_user_real_name

std::string OksSystem::User::m_user_real_name
mutableprotected

cached real user name

Definition at line 37 of file User.hpp.

◆ ROOT

const OksSystem::User OksSystem::User::ROOT
static

constant for root user

Definition at line 41 of file User.hpp.


The documentation for this class was generated from the following files: