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{
29 m_user_id = ::getuid();
30} // User
31
32OksSystem::User::User(uid_t user_id) throw()
33{
34 m_user_id = user_id;
35} // User
36
37OksSystem::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
75OksSystem::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
84OksSystem::User::operator uid_t() const throw()
85{
86 return m_user_id;
87} // uid_t
88
89OksSystem::User::operator std::string() const
90{
91 return name();
92} // string
93
94OksSystem::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
109uid_t
111{
112 return m_user_id;
113} // identity
114
120
121void
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
169void
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
214const std::string&
216{
217 if (m_user_name.empty()) {
218 resolve();
219 }
220 return m_user_name;
221} // name
222
226
227const std::string&
229{
230 if (m_user_name.empty()) {
231 resolve_safe();
232 }
233 return m_user_name;
234} // name
235
239
240const std::string&
242{
243 if (m_user_home.empty()) {
244 resolve();
245 }
246 return m_user_home;
247} // home
248
252
253const std::string&
255{
256 if (m_user_real_name.empty()) {
257 resolve();
258 }
259 return m_user_real_name;
260} // real_name
261
266
267void
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
284std::ostream&
285operator<<(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
299bool
300operator==(const OksSystem::User& a, const OksSystem::User& b) throw()
301{
302 return a.identity() == b.identity();
303} //
304
310
311bool
312operator!=(const OksSystem::User& a, const OksSystem::User& b) throw()
313{
314 return a.identity() != b.identity();
315} //
bool operator==(const OksSystem::File &a, const OksSystem::File &b)
Definition File.cpp:956
bool operator!=(const OksSystem::File &a, const OksSystem::File &b)
Definition File.cpp:962
#define ERS_HERE
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition Logger.hxx:128
void resolve() const
resolve information from user-id
Definition User.cpp:122
void resolve_safe() const
resolve without throwing exceptions
Definition User.cpp:170
static const User ROOT
constant for root user
Definition User.hpp:41
const std::string & name() const
username
Definition User.cpp:215
uid_t identity() const
user-id
Definition User.cpp:110
const std::string & home() const
home directory name
Definition User.cpp:241
std::string m_user_real_name
cached real user name
Definition User.hpp:37
const std::string & real_name() const
real name
Definition User.cpp:254
uid_t m_user_id
the actual user id
Definition User.hpp:34
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
std::string m_user_home
cached user home directory
Definition User.hpp:36
std::string m_user_name
cached user name
Definition User.hpp:35
User()
constructor for current user
Definition User.cpp:27