ChimeraTK-DeviceAccess  03.18.00
ProcessManagement.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #include "ProcessManagement.h"
5 
6 #include <sys/types.h>
7 
8 #include <cerrno>
9 #include <csignal>
10 #include <cstring>
11 #include <iostream>
12 #include <pwd.h>
13 #include <string>
14 #include <unistd.h>
15 
16 bool processExists(unsigned pid) {
17  return !kill((pid_t)pid, 0);
18 }
19 
20 unsigned getOwnPID() {
21  return (unsigned)getpid();
22 }
23 
24 std::string getUserName() {
25  errno = 0;
26  auto* pwent = getpwuid(geteuid());
27  auto savedErr = errno;
28 
29  if(pwent == nullptr) {
30  // pwent == nullptr and errno == 0 is a valid scenario - There was no error, but the system also
31  // could not find the corresponding user name.
32  if(savedErr != 0) {
33  std::cout << "Failed to lookup user name, expect issues: " << strerror(savedErr) << std::endl;
34  }
35  return {"**unknown*user*name**"};
36  }
37  return {pwent->pw_name};
38 }
getUserName
std::string getUserName()
Definition: ProcessManagement.cpp:24
processExists
bool processExists(unsigned pid)
Definition: ProcessManagement.cpp:16
getOwnPID
unsigned getOwnPID()
Definition: ProcessManagement.cpp:20
ProcessManagement.h