DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
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"
24
26
28 m_user_id = ::getuid();
29} // User
30
31OksSystem::User::User(uid_t user_id) throw() {
32 m_user_id = user_id;
33} // User
34
35OksSystem::User::User(const std::string &s_name) {
36
37 errno = 0;
38 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
39
40 if(bufSize == -1) {
41 if(errno == 0) {
42 bufSize = 1024;
43 } else {
44 std::string message = "with argument _SC_GETPW_R_SIZE_MAX while getting info about user " + s_name;
45 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "sysconf", message.c_str());
46 }
47 }
48
49 struct passwd pwd;
50 struct passwd *result;
51 char* buf = new char[bufSize];
52 errno = 0;
53 int res = ::getpwnam_r(s_name.c_str(), &pwd, buf, bufSize, &result);
54 if(res == 0) {
55 if(result != 0) {
56 m_user_id = result->pw_uid;
57 } else {
58 delete[] buf;
59 std::string eMsg = "User " + s_name + " not found";
60 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "getpwnam_r", eMsg.c_str());
61 }
62 } else {
63 delete[] buf;
64 std::string message = "while getting info about user " + s_name;
65 throw OksSystem::OksSystemCallIssue(ERS_HERE, res, "getpwnam_r", message.c_str());
66 }
67 delete[] buf;
68
69 m_user_name = s_name;
70} // User
71
72OksSystem::User::User(const User &other) throw() {
73 m_user_id = other.m_user_id;
74 m_user_name = other.m_user_name;
75} // User
76
77// Operators
78// =========
79
80
81OksSystem::User::operator uid_t() const throw() {
82 return m_user_id;
83} // uid_t
84
85OksSystem::User::operator std::string() const {
86 return name();
87} // string
88
89OksSystem::User::operator const char *() const {
90 if (m_user_name.empty()) { resolve();}
91 return m_user_name.c_str();
92} // char*
93
94// Methods
95// =========
96
100
101uid_t OksSystem::User::identity() const throw() {
102 return m_user_id;
103} // identity
104
110
112
113 errno = 0;
114 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
115
116 if(bufSize == -1) {
117 if(errno == 0) {
118 bufSize = 1024;
119 } else {
120 std::ostringstream message;
121 message << "with argument _SC_GETPW_R_SIZE_MAX while getting info about user with id " << m_user_id;
122 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "sysconf", message.str().c_str());
123 }
124 }
125
126 struct passwd pwd;
127 struct passwd *result;
128 char* buf = new char[bufSize];
129 errno = 0;
130 int res = ::getpwuid_r(m_user_id, &pwd, buf, bufSize, &result);
131 if(res == 0) {
132 if(result != 0) {
133 m_user_name = std::string(result->pw_name);
134 m_user_home = std::string(result->pw_dir);
135 m_user_real_name = std::string(result->pw_gecos);
136 } else {
137 delete[] buf;
138 std::ostringstream eMsg;
139 eMsg << "User " << m_user_id << " not found";
140 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "getpwuid_r", eMsg.str().c_str());
141 }
142 } else {
143 delete[] buf;
144 std::ostringstream message;
145 message << "while getting info about user with id " << m_user_id;
146 throw OksSystem::OksSystemCallIssue(ERS_HERE, res, "getpwnam_r", message.str().c_str());
147 }
148 delete[] buf;
149
150} //
151
156
157void OksSystem::User::resolve_safe() const throw() {
158
159 errno = 0;
160 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
161
162 if(bufSize == -1) {
163 if(errno == 0) {
164 bufSize = 1024;
165 } else {
166 m_user_name = "unknown";
167 m_user_home = "unknown";
168 m_user_real_name = "unknown";
169 return;
170 }
171 }
172
173 struct passwd pwd;
174 struct passwd *result;
175 char* buf = new char[bufSize];
176 int res = ::getpwuid_r(m_user_id, &pwd, buf, bufSize, &result);
177 if(res == 0) {
178 if(result != 0) {
179 m_user_name = std::string(result->pw_name);
180 m_user_home = std::string(result->pw_dir);
181 m_user_real_name = std::string(result->pw_gecos);
182 } else {
183 m_user_name = "unknown";
184 m_user_home = "unknown";
185 m_user_real_name = "unknown";
186 }
187 } else {
188 m_user_name = "unknown";
189 m_user_home = "unknown";
190 m_user_real_name = "unknown";
191 }
192 delete[] buf;
193
194} // resolve_safe
195
199
200const std::string & OksSystem::User::name() const {
201 if (m_user_name.empty()) { resolve();}
202 return m_user_name;
203} // name
204
208
209const std::string & OksSystem::User::name_safe() const throw() {
210 if (m_user_name.empty()) { resolve_safe(); }
211 return m_user_name;
212} // name
213
217
218const std::string & OksSystem::User::home() const {
219 if (m_user_home.empty()) { resolve();}
220 return m_user_home;
221} // home
222
226
227const std::string & OksSystem::User::real_name() const {
228 if (m_user_real_name.empty()) { resolve();}
229 return m_user_real_name;
230} // real_name
231
236
238 const int status = ::setuid(m_user_id);
239 if (status<0) {
240 std::ostringstream message;
241 message << "while setting the effective user ID to " << m_user_id << "(" << this->name_safe() << ")";
242 throw OksSystem::OksSystemCallIssue( ERS_HERE, errno, "setuid", message.str().c_str() );
243 }
244} // setuid
245
246
252
253std::ostream& operator<<(std::ostream& stream, const OksSystem::User& user) throw() {
254 unsigned int i = (unsigned int) user.identity();
255 const std::string name = user.name_safe();
256 stream << name << '(' << i << ')';
257 return stream;
258} // operator<<
259
265
266bool operator ==(const OksSystem::User &a, const OksSystem::User &b) throw() {
267 return a.identity() == b.identity();
268} //
269
275
276bool operator !=(const OksSystem::User &a, const OksSystem::User &b) throw() {
277 return a.identity() != b.identity();
278} //
279
280
#define ERS_HERE
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition Logger.hxx:102
void resolve() const
resolve information from user-id
Definition User.cpp:111
void resolve_safe() const
resolve without throwing exceptions
Definition User.cpp:157
static const User ROOT
constant for root user
Definition User.hpp:40
const std::string & name() const
username
Definition User.cpp:200
uid_t identity() const
user-id
Definition User.cpp:101
const std::string & home() const
home directory name
Definition User.cpp:218
std::string m_user_real_name
cached real user name
Definition User.hpp:36
const std::string & real_name() const
real name
Definition User.cpp:227
uid_t m_user_id
the actual user id
Definition User.hpp:33
const std::string & name_safe() const
username - no exception
Definition User.cpp:209
void setuid() const
tries to set the uid of current process
Definition User.cpp:237
std::string m_user_home
cached user home directory
Definition User.hpp:35
std::string m_user_name
cached user name
Definition User.hpp:34
User()
constructor for current user
Definition User.cpp:27
bool operator!=(const Host &a, const Host &b)
inequality operator
Definition Host.cpp:188
bool operator==(const Host &a, const Host &b)
equality operator
Definition Host.cpp:184