21#include <radcli/radcli.h>
32static char const * months[] =
34 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
35 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
43void rc_str2tm (
char const *valstr,
struct tm *tm)
48 for (i = 0; i < 12; i++)
50 if (strncmp (months[i], valstr, 3) == 0)
58 tm->tm_mday = atoi (&valstr[4]);
61 tm->tm_year = atoi (&valstr[7]) - 1900;
69double rc_getmtime(
void)
71#ifdef HAVE_CLOCK_GETTIME
72 struct timespec timespec;
74 if (clock_gettime(CLOCK_MONOTONIC, ×pec) != 0)
77 return timespec.tv_sec + ((double)timespec.tv_nsec) / 1000000000.0;
81 if (gettimeofday(&timev, NULL) == -1)
84 return timev.tv_sec + ((double)timev.tv_usec) / 1000000.0;
97 static unsigned short int cnt = 0;
98 snprintf (buf,
sizeof(buf),
"%08lX%04X%02hX",
99 (
unsigned long int) time (NULL),
100 (
unsigned int) getpid (),
131rc_strlcpy(
char *dst,
char const *src,
size_t siz)
138 if (n != 0 && --n != 0) {
140 if ((*d++ = *s++) == 0)
165int rc_set_netns(
const char *net_namespace,
int *prev_ns_handle)
170 static const char* crt_nsnet =
"/proc/self/ns/net";
172 char sock_nsnet[NSNET_SZ];
174 if (NULL == net_namespace) {
175 rc_log(LOG_ERR,
"Namespace not provided");
178 if (NULL == prev_ns_handle) {
179 rc_log(LOG_ERR,
"NULL NS handle for: %s", net_namespace);
182 *prev_ns_handle = -1;
183 snprintf(sock_nsnet, NSNET_SZ,
"/var/run/netns/%s", net_namespace);
185 *prev_ns_handle = open(crt_nsnet, O_RDONLY);
186 if (*prev_ns_handle < 0) {
187 rc_log(LOG_ERR,
"Cannot open %s errno=%s(%d)", crt_nsnet, strerror (errno), errno);
190 sock_ns_fd = open(sock_nsnet, O_RDONLY);
191 if (sock_ns_fd < 0) {
192 rc_log(LOG_ERR,
"Cannot open %s errno=%s(%d)", sock_nsnet, strerror (errno), errno);
195 if (setns(sock_ns_fd, CLONE_NEWNET) < 0) {
196 rc_log(LOG_ERR,
"'setns' set failed for %s errno=%s(%d)", sock_nsnet,
197 strerror(errno), errno);
202 if (sock_ns_fd >= 0) {
203 (void)close(sock_ns_fd);
206 if (*prev_ns_handle >=0 ) {
207 (void)close(*prev_ns_handle);
208 *prev_ns_handle = -1;
212 rc_log(LOG_ERR,
"Not a Linux system. No operation performed");
224int rc_reset_netns(
int *prev_ns_handle)
228 if (NULL == prev_ns_handle) {
229 rc_log(LOG_ERR,
"NULL NS handle");
232 if (setns(*prev_ns_handle, CLONE_NEWNET) < 0) {
233 rc_log(LOG_ERR,
"'setns' - reset failed errno=%s(%d)", strerror (errno), errno);
235 if (close(*prev_ns_handle) != 0) {
236 rc_log(LOG_ERR,
"Close error fd=%d errno=%s(%d)", *prev_ns_handle, strerror (errno), errno);
239 *prev_ns_handle = -1;
241 rc_log(LOG_ERR,
"Not a Linux system. No operation performed");