linux_systemutil.cpp
Go to the documentation of this file.
1 /**
2  * @author: Daniel Fuchs
3  * @contact: fuxeysolutions@gmail.com
4  *
5  * distributed under the MIT License (MIT).
6  * Copyright (c) Daniel Fuchs
7  *
8  */
9 #include <cstdlib>
10 #include <unistd.h>
11 #include <cstdio>
12 #include <dirent.h>
13 #include <fstream>
14 #include <sys/types.h>
15 #include <csignal>
16 #include <cstring>
17 #include <netinet/in.h>
18 #include <sys/statvfs.h>
19 #include <sys/stat.h>
20 #include <pwd.h>
21 #include <thread>
22 
23 #include "linux_systemutil.hpp"
24 #include <cinttypes>
25 
26 int64_t linuxUtil::getTemperature(const std::string &thermalZone) {
27  std::ifstream temperatureFile;
28  const std::string parsePath = "/sys/class/thermal/" + thermalZone + "/temp";
29  temperatureFile.open(parsePath);
30 
31  int64_t temperature;
32  std::string line;
33  while (std::getline(temperatureFile, line)) {
34  auto unused = scanf(line.c_str(), "%ld", &temperature);
35  (void) unused;
36  }
37  temperatureFile.close();
38  return temperature;
39 }
40 
41 int linuxUtil::getProcIdByName(const std::string &procName) {
42  int pid = -1;
43  auto dp = opendir("/proc");
44  if (dp != nullptr) {
45  struct dirent *dirp;
46  while (pid < 0 && (dirp = readdir(dp))) {
47  int id = atoi(dirp->d_name);
48  if (id > 0) {
49  std::string cmdPath{"/proc/"};
50  cmdPath.append(dirp->d_name);
51  cmdPath.append("/cmdline");
52  std::ifstream cmdFile(cmdPath.c_str());
53  std::string cmdLine;
54  getline(cmdFile, cmdLine);
55  if (!cmdLine.empty()) {
56  size_t pos = cmdLine.find('\0');
57  if (pos != std::string::npos)
58  cmdLine = cmdLine.substr(0, pos);
59  pos = cmdLine.rfind('/');
60  if (pos != std::string::npos)
61  cmdLine = cmdLine.substr(pos + 1);
62  if (strcmp(procName.c_str(), cmdLine.c_str()) == 0) {
63  pid = id;
64  }
65  }
66  }
67  }
68  }
69  closedir(dp);
70  return pid;
71 }
72 
73 int linuxUtil::killProcessById(int pid, const std::string &procName) {
74  if (pid == -1) {
75  throw std::runtime_error(
76  "Nothing to Kill, no Process " + procName + " PID " + std::to_string(pid));
77  }
78  int ret = kill(pid, 9);
79  if (ret == -1) {
80  throw std::runtime_error("killing " + procName + " was not successful!");
81  }
82  return ret;
83 }
84 
86  std::ifstream upTimeFile;
87  upTimeFile.open("/proc/uptime");
88 
89  if (!upTimeFile.is_open()) {
90  return 0;
91  }
92 
93  uint64_t beforeBootTime;
94  uint64_t sysUptime = 0;
95  std::string line;
96  while (std::getline(upTimeFile, line)) {
97  sscanf(line.c_str(), "%" PRIu64 "%" PRIu64, &sysUptime, &beforeBootTime);
98  }
99  upTimeFile.close();
100  return sysUptime;
101 }
102 
103 
105  pid_t pid = fork();
106  if (pid < 0) {
107  return false;
108  }
109 
110  if (pid > 0)
111  std::exit(EXIT_SUCCESS);
112 
113  if (setsid() < 0)
114  std::exit(EXIT_FAILURE);
115 
116  //TODO: Implement a working signal handler */
117  std::signal(SIGCHLD, SIG_IGN);
118  std::signal(SIGHUP, SIG_IGN);
119 
120  pid = fork();
121  if (pid < 0)
122  std::exit(EXIT_FAILURE);
123 
124  if (pid > 0)
125  std::exit(EXIT_SUCCESS);
126 
127  umask(0);
128  auto retval = chdir("/test");
129  if (retval < 0) {
130  return false;
131  }
132 
133  for (int x = sysconf(_SC_OPEN_MAX); x >= 0; x--) {
134  close(x);
135  }
136  return true;
137 }
138 
139 uint64_t linuxUtil::getFreeDiskSpace(std::string absoluteFilePath) {
140  struct statvfs buf;
141 
142  if (!statvfs(absoluteFilePath.c_str(), &buf)) {
143  uint64_t blksize, blocks, freeblks, disk_size, used, free;
144  std::cout << "blksize :" << buf.f_bsize << std::endl;
145  std::cout << "blocks : " << buf.f_blocks;
146  std::cout << "bfree : " << buf.f_bfree;
147  std::cout << "bavail: " << buf.f_bavail;
148  std::cout << "f_frsize: " << buf.f_frsize;
149  blksize = buf.f_bsize;
150  blocks = buf.f_blocks;
151  freeblks = buf.f_bfree;
152  disk_size = blocks * blksize;
153  free = freeblks * blksize;
154  used = disk_size - free;
155 
156  std::cout << "disk " << absoluteFilePath
157  << " disksize: " << disk_size
158  << " free: " << free
159  << " used: " << used
160  << std::endl;
161  return free;
162  } else {
163  return 0;
164  }
165  return 0;
166 }
167 
169  struct statvfs stat;
170  struct passwd *pw = getpwuid(getuid());
171  if (nullptr != pw && 0 == statvfs(pw->pw_dir, &stat)) {
172  std::cout << "path " << pw->pw_dir << std::endl;
173  uint64_t freeBytes = stat.f_bavail * stat.f_frsize;
174  return freeBytes;
175  }
176  return 0ULL;
177 }
178 
180  std::ifstream versionFile;
181  versionFile.open("/proc/version_signature");
182 
183  if (!versionFile.is_open()) {
184  return std::string();
185  }
186  std::string line;
187  std::getline(versionFile, line);
188 
189  versionFile.close();
190  return line;
191 }
192 
193 std::string linuxUtil::getOsVersionString(void) {
194  std::ifstream versionFile;
195  versionFile.open("/proc/version");
196 
197  if (!versionFile.is_open()) {
198  return std::string();
199  }
200  std::string line;
201  std::getline(versionFile, line);
202 
203  versionFile.close();
204  return line;
205 }
206 
207 bool linuxUtil::isDeviceOnline(std::string address) {
208  const std::string processPrefix = {"ping -c 1 -w 1 "};
209  const std::string processPostfix = {" 2>&1"};
210  auto fd = popen((processPrefix + address + processPostfix).c_str(), "r");
211  std::this_thread::sleep_for(std::chrono::seconds(2));
212  if (fd == nullptr) {
213  return false;
214  }
215  char buff[1000];
216  char *ptr = buff;
217  size_t sz = sizeof(buff);
218  while (getline(&ptr, &sz, fd) != -1) {
219  std::string line(buff);
220  if (line.find(" 1 received") != std::string::npos) {
221  pclose(fd);
222  return true;
223  }
224  if (line.find("100% packet loss") != std::string::npos) {
225  pclose(fd);
226  return false;
227  }
228  }
229  return false;
230 }
231 
233  uint32_t Threads = 0;
234  std::ifstream memoryFile;
235  memoryFile.open("/proc/self/status");
236  std::string line;
237  while (std::getline(memoryFile, line)) {
238  sscanf(line.c_str(), "Threads: %u", &Threads);
239  }
240  return Threads;
241 }
242 
244  uint32_t Threads = 0;
245  std::ifstream memoryFile;
246  memoryFile.open("/proc/self/" + std::to_string(Pid));
247  std::string line;
248  while (std::getline(memoryFile, line)) {
249  sscanf(line.c_str(), "Threads: %u", &Threads);
250  }
251  return Threads;
252 }
253 
254 std::string linuxUtil::getIFaceMacAddress(std::string deviceName) {
255  const std::string sysClassPath = "/sys/class/net/";
256 
257  std::ifstream IFaceFile;
258  IFaceFile.open(sysClassPath + deviceName + "/address");
259  std::string line;
260  std::getline(IFaceFile, line);
261  return line;
262 }
linuxUtil::getFreeDiskSpace
static uint64_t getFreeDiskSpace(std::string absoluteFilePath)
Definition: linux_systemutil.cpp:139
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
linuxUtil::getIFaceMacAddress
static std::string getIFaceMacAddress(std::string deviceName)
Definition: linux_systemutil.cpp:254
linuxUtil::userAvailableFreeSpace
static uint64_t userAvailableFreeSpace()
Definition: linux_systemutil.cpp:168
linuxUtil::getNumOfThreadsByPID
static uint32_t getNumOfThreadsByPID(int Pid)
Definition: linux_systemutil.cpp:243
linuxUtil::getOsVersionString
static std::string getOsVersionString(void)
Definition: linux_systemutil.cpp:193
linux_systemutil.hpp
linuxUtil::getNumOfThreadsByThisProcess
static uint32_t getNumOfThreadsByThisProcess()
Definition: linux_systemutil.cpp:232
linuxUtil::startAppAsDaemon
static bool startAppAsDaemon()
Definition: linux_systemutil.cpp:104
linuxUtil::getOSVersion_Signature
static std::string getOSVersion_Signature(void)
Definition: linux_systemutil.cpp:179
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
linuxUtil::getProcIdByName
static int getProcIdByName(const std::string &procName)
Definition: linux_systemutil.cpp:41
linuxUtil::getTemperature
static int64_t getTemperature(const std::string &thermalZone="thermal_zone0")
Definition: linux_systemutil.cpp:26
linuxUtil::isDeviceOnline
static bool isDeviceOnline(std::string address)
Definition: linux_systemutil.cpp:207
linuxUtil::getSysUpTime
static uint64_t getSysUpTime()
Definition: linux_systemutil.cpp:85
linuxUtil::killProcessById
static int killProcessById(int pid, const std::string &procName)
Definition: linux_systemutil.cpp:73