Repository navigation
Expand file tree
/
Copy pathhelper.cpp
More file actions
133 lines (113 loc) · 3.02 KB
/
Copy pathhelper.cpp
File metadata and controls
133 lines (113 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* this file contains various helper routines */
#include <signal.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "helper.h"
using std::string;
// check if it is already running
int isRunning(char *prgname)
{
int num = 0;
char s[256];
sprintf(s,"ps -e | grep %s",prgname);
FILE *fp = popen(s,"r");
if(fp)
{
// gets the output of the system command
while (fgets(s, sizeof(s)-1, fp) != NULL)
{
if(strstr(s,prgname) && !strstr(s,"grep"))
{
if(++num == 2)
{
printf("%s is already running, do not start twice !\n",prgname);
pclose(fp);
return 1;
}
}
}
pclose(fp);
}
return 0;
}
void (*sigfunc)();
// signal handler
void sighandler(int signum)
{
printf("program stopped by signal\n");
(*sigfunc)();
}
void install_signal_handler(void (*signalfunction)())
{
sigfunc = signalfunction;
// signal handler, mainly used if the user presses Ctrl-C
struct sigaction sigact;
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
sigaction(SIGABRT, &sigact, NULL); // assert() error
//sigaction(SIGSEGV, &sigact, NULL);
// switch off signal 13 (broken pipe)
// instead handle the return value of the write or send function
signal(SIGPIPE, SIG_IGN);
}
// get own IP adress
char* ownIP()
{
static char ip[20] = {0};
struct ifaddrs* ifaddr, *ifa;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
exit(1);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
if (ifa->ifa_addr->sa_family == AF_INET
&& strcmp(ifa->ifa_name, "wlan0") == 0)
{
int s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
printf("getnameinfo() failed: %s\n", gai_strerror(s));
continue;
}
strcpy(ip, host);
break;
}
}
freeifaddrs(ifaddr);
return ip;
}
float get_cpu_temperature()
{
string line;
float temp;
std::ifstream file("/sys/class/thermal/thermal_zone0/temp");
if (file.is_open()) {
getline(file, line);
temp = stof(line) / 1000.0; // Convert from millidegrees Celsius to degrees Celsius
file.close();
return temp;
} else {
std::cerr << "Error: Unable to open file." << std::endl;
return -1.0; // Return -1.0 if error occurs
}
}