utils.cpp 3.37 KB
Newer Older
Steffen Vogel's avatar
Steffen Vogel committed
1
2
3
/** Utilities.
 *
 * @author Daniel Krebs <github@daniel-krebs.net>
Steffen Vogel's avatar
Steffen Vogel committed
4
 * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
Steffen Vogel's avatar
Steffen Vogel committed
5
6
 * @license GNU General Public License (version 3)
 *
7
 * VILLAScommon
Steffen Vogel's avatar
Steffen Vogel committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#include <vector>
#include <string>
Steffen Vogel's avatar
Steffen Vogel committed
25
#include <iostream>
Steffen Vogel's avatar
Steffen Vogel committed
26

Steffen Vogel's avatar
Steffen Vogel committed
27
28
29
30
#include <fcntl.h>
#include <unistd.h>

#include <villas/utils.h>
Steffen Vogel's avatar
Steffen Vogel committed
31
#include <villas/utils.hpp>
Steffen Vogel's avatar
Steffen Vogel committed
32
#include <villas/config.h>
Steffen Vogel's avatar
Steffen Vogel committed
33
#include <villas/log.hpp>
Steffen Vogel's avatar
Steffen Vogel committed
34
35
36
37

namespace villas {
namespace utils {

38
39
static pthread_t main_thread;

Steffen Vogel's avatar
Steffen Vogel committed
40
41
42
43
44
45
46
47
48
49
50
51
std::vector<std::string>
tokenize(std::string s, std::string delimiter)
{
	std::vector<std::string> tokens;

	size_t lastPos = 0;
	size_t curentPos;

	while((curentPos = s.find(delimiter, lastPos)) != std::string::npos) {
		const size_t tokenLength = curentPos - lastPos;
		tokens.push_back(s.substr(lastPos, tokenLength));

52
		/* Advance in string */
Steffen Vogel's avatar
Steffen Vogel committed
53
54
55
		lastPos = curentPos + delimiter.length();
	}

56
	/* Check if there's a last token behind the last delimiter. */
Steffen Vogel's avatar
Steffen Vogel committed
57
58
59
60
61
62
63
64
	if(lastPos != s.length()) {
		const size_t lastTokenLength = s.length() - lastPos;
		tokens.push_back(s.substr(lastPos, lastTokenLength));
	}

	return tokens;
}

Steffen Vogel's avatar
Steffen Vogel committed
65
66
67
ssize_t read_random(char *buf, size_t len)
{
	int fd;
68
	ssize_t bytes = -1;
Steffen Vogel's avatar
Steffen Vogel committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

	fd = open("/dev/urandom", O_RDONLY);
	if (fd < 0)
		return -1;

	while (len) {
		bytes = read(fd, buf, len);
		if (bytes < 0)
			break;

		len -= bytes;
		buf += bytes;
	}

	close(fd);

	return bytes;
}

/* Setup exit handler */
int signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx))
{
	int ret;

Steffen Vogel's avatar
Steffen Vogel committed
93
	Logger logger = logging.get("signals");
Steffen Vogel's avatar
Steffen Vogel committed
94
95

	logger->info("Initialize subsystem");
Steffen Vogel's avatar
Steffen Vogel committed
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

	struct sigaction sa_quit;
	sa_quit.sa_flags = SA_SIGINFO | SA_NODEFER;
	sa_quit.sa_sigaction = cb;

	struct sigaction sa_chld;
	sa_chld.sa_flags = 0;
	sa_chld.sa_handler = SIG_IGN;

	main_thread = pthread_self();

	sigemptyset(&sa_quit.sa_mask);

	ret = sigaction(SIGINT, &sa_quit, NULL);
	if (ret)
		return ret;

	ret = sigaction(SIGTERM, &sa_quit, NULL);
	if (ret)
		return ret;

	ret = sigaction(SIGALRM, &sa_quit, NULL);
	if (ret)
		return ret;

	ret = sigaction(SIGCHLD, &sa_chld, NULL);
	if (ret)
		return ret;

	return 0;
}

char * decolor(char *str)
{
	char *p, *q;
	bool inseq = false;

	for (p = q = str; *p; p++) {
		switch (*p) {
			case 0x1b:
				if (*(++p) == '[') {
					inseq = true;
					continue;
				}
				break;

			case 'm':
				if (inseq) {
					inseq = false;
					continue;
				}
				break;
		}

		if (!inseq) {
			*q = *p;
			q++;
		}
	}

	*q = '\0';

	return str;
}

161
162
163
164
165
166
167
168
169
170
171
172
173
extern "C" {

void killme(int sig)
{
	/* Send only to main thread in case the ID was initilized by signals_init() */
	if (main_thread)
		pthread_kill(main_thread, sig);
	else
		kill(0, sig);
}

}

174
175
} /* namespace utils */
} /* namespace villas */