Skip to content

Commit 5d7f6ef

Browse files
committed
daemon.cc: Clean up PeerInfo by using std::optional
1 parent 0db70b8 commit 5d7f6ef

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/nix/unix/daemon.cc

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,17 @@ static bool matchUser(const std::string & user, const std::string & group, const
198198

199199
struct PeerInfo
200200
{
201-
bool pidKnown;
202-
pid_t pid;
203-
bool uidKnown;
204-
uid_t uid;
205-
bool gidKnown;
206-
gid_t gid;
201+
std::optional<pid_t> pid;
202+
std::optional<uid_t> uid;
203+
std::optional<gid_t> gid;
207204
};
208205

209206
/**
210207
* Get the identity of the caller, if possible.
211208
*/
212209
static PeerInfo getPeerInfo(int remote)
213210
{
214-
PeerInfo peer = {false, 0, false, 0, false, 0};
211+
PeerInfo peer;
215212

216213
#if defined(SO_PEERCRED)
217214

@@ -221,9 +218,11 @@ static PeerInfo getPeerInfo(int remote)
221218
ucred cred;
222219
# endif
223220
socklen_t credLen = sizeof(cred);
224-
if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == -1)
225-
throw SysError("getting peer credentials");
226-
peer = {true, cred.pid, true, cred.uid, true, cred.gid};
221+
if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == 0) {
222+
peer.pid = cred.pid;
223+
peer.uid = cred.uid;
224+
peer.gid = cred.gid;
225+
}
227226

228227
#elif defined(LOCAL_PEERCRED)
229228

@@ -233,9 +232,8 @@ static PeerInfo getPeerInfo(int remote)
233232

234233
xucred cred;
235234
socklen_t credLen = sizeof(cred);
236-
if (getsockopt(remote, SOL_LOCAL, LOCAL_PEERCRED, &cred, &credLen) == -1)
237-
throw SysError("getting peer credentials");
238-
peer = {false, 0, true, cred.cr_uid, false, 0};
235+
if (getsockopt(remote, SOL_LOCAL, LOCAL_PEERCRED, &cred, &credLen) == 0)
236+
peer.uid = cred.cr_uid;
239237

240238
#endif
241239

@@ -270,11 +268,11 @@ static std::pair<TrustedFlag, std::string> authPeer(const PeerInfo & peer)
270268
{
271269
TrustedFlag trusted = NotTrusted;
272270

273-
struct passwd * pw = peer.uidKnown ? getpwuid(peer.uid) : 0;
274-
std::string user = pw ? pw->pw_name : std::to_string(peer.uid);
271+
auto pw = peer.uid ? getpwuid(*peer.uid) : nullptr;
272+
std::string user = pw ? pw->pw_name : peer.uid ? std::to_string(*peer.uid) : "";
275273

276-
struct group * gr = peer.gidKnown ? getgrgid(peer.gid) : 0;
277-
std::string group = gr ? gr->gr_name : std::to_string(peer.gid);
274+
auto gr = peer.gid ? getgrgid(*peer.gid) : 0;
275+
std::string group = gr ? gr->gr_name : peer.gid ? std::to_string(*peer.gid) : "";
278276

279277
const Strings & trustedUsers = authorizationSettings.trustedUsers;
280278
const Strings & allowedUsers = authorizationSettings.allowedUsers;
@@ -360,9 +358,9 @@ static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
360358

361359
unix::closeOnExec(remote.get());
362360

363-
PeerInfo peer{.pidKnown = false};
361+
PeerInfo peer;
364362
TrustedFlag trusted;
365-
std::string user;
363+
std::string user = "<unknown>";
366364

367365
if (forceTrustClientOpt)
368366
trusted = *forceTrustClientOpt;
@@ -375,8 +373,8 @@ static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
375373

376374
printInfo(
377375
(std::string) "accepted connection from pid %1%, user %2%" + (trusted ? " (trusted)" : ""),
378-
peer.pidKnown ? std::to_string(peer.pid) : "<unknown>",
379-
peer.uidKnown ? user : "<unknown>");
376+
peer.pid ? std::to_string(*peer.pid) : "<unknown>",
377+
user);
380378

381379
// Fork a child to handle the connection.
382380
ProcessOptions options;
@@ -396,8 +394,8 @@ static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
396394
setSigChldAction(false);
397395

398396
// For debugging, stuff the pid into argv[1].
399-
if (peer.pidKnown && savedArgv[1]) {
400-
auto processName = std::to_string(peer.pid);
397+
if (peer.pid && savedArgv[1]) {
398+
auto processName = std::to_string(*peer.pid);
401399
strncpy(savedArgv[1], processName.c_str(), strlen(savedArgv[1]));
402400
}
403401

0 commit comments

Comments
 (0)