DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
okssystem
src
User.cpp
Go to the documentation of this file.
1
// DUNE DAQ modification notice:
2
// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3
// Fork baseline commit: system-00-00-20 (2020-09-25).
4
// Renamed since fork: yes (from src/User.cxx to src/User.cpp).
5
6
/*
7
* User.cxx
8
* OksSystem
9
*
10
* Created by Matthias Wiesmann on 03.02.05.
11
* Copyright 2005 CERN. All rights reserved.
12
*
13
*/
14
15
#include <pwd.h>
16
17
#include <iostream>
18
#include <sstream>
19
20
#include "
ers/ers.hpp
"
21
22
#include "
okssystem/User.hpp
"
23
#include "
okssystem/exceptions.hpp
"
24
25
const
OksSystem::User
OksSystem::User::ROOT
(0);
26
27
OksSystem::User::User
() throw()
28
{
29
m_user_id
= ::getuid();
30
}
// User
31
32
OksSystem::User::User
(uid_t user_id)
throw
()
33
{
34
m_user_id
= user_id;
35
}
// User
36
37
OksSystem::User::User
(
const
std::string& s_name)
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
74
75
OksSystem::User::User
(
const
User
& other)
throw
()
76
{
77
m_user_id
= other.m_user_id;
78
m_user_name
= other.m_user_name;
79
}
// User
80
81
// Operators
82
// =========
83
84
OksSystem::User::operator uid_t()
const
throw()
85
{
86
return
m_user_id
;
87
}
// uid_t
88
89
OksSystem::User::operator std::string()
const
90
{
91
return
name
();
92
}
// string
93
94
OksSystem::User::operator
const
char
*()
const
95
{
96
if
(
m_user_name
.empty()) {
97
resolve
();
98
}
99
return
m_user_name
.c_str();
100
}
// char*
101
102
// Methods
103
// =========
104
108
109
uid_t
110
OksSystem::User::identity
()
const
throw()
111
{
112
return
m_user_id
;
113
}
// identity
114
120
121
void
122
OksSystem::User::resolve
()
const
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
}
//
163
168
169
void
170
OksSystem::User::resolve_safe
()
const
throw()
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
209
213
214
const
std::string&
215
OksSystem::User::name
()
const
216
{
217
if
(
m_user_name
.empty()) {
218
resolve
();
219
}
220
return
m_user_name
;
221
}
// name
222
226
227
const
std::string&
228
OksSystem::User::name_safe
()
const
throw()
229
{
230
if
(
m_user_name
.empty()) {
231
resolve_safe();
232
}
233
return
m_user_name
;
234
}
// name
235
239
240
const
std::string&
241
OksSystem::User::home
()
const
242
{
243
if
(
m_user_home
.empty()) {
244
resolve
();
245
}
246
return
m_user_home
;
247
}
// home
248
252
253
const
std::string&
254
OksSystem::User::real_name
()
const
255
{
256
if
(
m_user_real_name
.empty()) {
257
resolve
();
258
}
259
return
m_user_real_name
;
260
}
// real_name
261
266
267
void
268
OksSystem::User::setuid
()
const
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
277
283
284
std::ostream&
285
operator<<
(std::ostream& stream,
const
OksSystem::User
& user)
throw
()
286
{
287
unsigned
int
i = (
unsigned
int)user.identity();
288
const
std::string name = user.name_safe();
289
stream << name <<
'('
<< i <<
')'
;
290
return
stream;
291
}
// operator<<
292
298
299
bool
300
operator==
(
const
OksSystem::User
& a,
const
OksSystem::User
& b)
throw
()
301
{
302
return
a.identity() == b.identity();
303
}
//
304
310
311
bool
312
operator!=
(
const
OksSystem::User
& a,
const
OksSystem::User
& b)
throw
()
313
{
314
return
a.identity() != b.identity();
315
}
//
operator==
bool operator==(const OksSystem::File &a, const OksSystem::File &b)
Definition
File.cpp:956
operator!=
bool operator!=(const OksSystem::File &a, const OksSystem::File &b)
Definition
File.cpp:962
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:127
operator<<
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition
Logger.hxx:128
User.hpp
OksSystem::User
User.
Definition
User.hpp:31
OksSystem::User::resolve
void resolve() const
resolve information from user-id
Definition
User.cpp:122
OksSystem::User::resolve_safe
void resolve_safe() const
resolve without throwing exceptions
Definition
User.cpp:170
OksSystem::User::ROOT
static const User ROOT
constant for root user
Definition
User.hpp:41
OksSystem::User::name
const std::string & name() const
username
Definition
User.cpp:215
OksSystem::User::identity
uid_t identity() const
user-id
Definition
User.cpp:110
OksSystem::User::home
const std::string & home() const
home directory name
Definition
User.cpp:241
OksSystem::User::m_user_real_name
std::string m_user_real_name
cached real user name
Definition
User.hpp:37
OksSystem::User::real_name
const std::string & real_name() const
real name
Definition
User.cpp:254
OksSystem::User::m_user_id
uid_t m_user_id
the actual user id
Definition
User.hpp:34
OksSystem::User::name_safe
const std::string & name_safe() const
username - no exception
Definition
User.cpp:228
OksSystem::User::setuid
void setuid() const
tries to set the uid of current process
Definition
User.cpp:268
OksSystem::User::m_user_home
std::string m_user_home
cached user home directory
Definition
User.hpp:36
OksSystem::User::m_user_name
std::string m_user_name
cached user name
Definition
User.hpp:35
OksSystem::User::User
User()
constructor for current user
Definition
User.cpp:27
ers.hpp
exceptions.hpp
Generated on
for DUNE-DAQ by
1.18.0