forked from kristrev/data-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_helpers.c
188 lines (151 loc) · 4.91 KB
/
system_helpers.c
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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (c) 2015, Celerway, Kristian Evensen <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <inttypes.h>
#ifdef OPENWRT
#include <uci.h>
#endif
#include "system_helpers.h"
#include "metadata_exporter.h"
#include "metadata_exporter_log.h"
#ifdef OPENWRT
uint32_t system_helpers_get_nodeid()
{
char *opt;
struct uci_ptr uciptr;
int retval;
long node_id;
struct uci_context *ctx = uci_alloc_context();
if(ctx == NULL)
return 0;
opt = malloc(strlen(NODEIDPATH)+1);
strcpy(opt, NODEIDPATH);
memset(&uciptr, 0, sizeof(uciptr));
retval = uci_lookup_ptr(ctx, &uciptr, opt, true);
if (retval != UCI_OK || uciptr.o == NULL) {
free(opt);
uci_free_context(ctx);
return 0;
}
node_id = atol(uciptr.o->v.string);
free(opt);
uci_free_context(ctx);
if (node_id <= 0 || node_id > UINT32_MAX)
return 0;
return (uint32_t) node_id;
}
#elif MONROE
uint32_t system_helpers_get_nodeid(const char* nodeid_file)
{
char num_buf[255];
long node_id;
size_t retval;
//Check if file exists
FILE *node_file = fopen(nodeid_file, "r");
if (node_file == NULL)
return 0;
memset(num_buf, 0, sizeof(num_buf));
retval = fread(num_buf, sizeof(char), 255, node_file);
fclose(node_file);
if (!retval)
return 0;
node_id = atol(num_buf);
if (node_id <= 0 || node_id > UINT32_MAX)
return 0;
return (uint32_t) node_id;
}
#else
uint32_t system_helpers_get_nodeid(){
return 0;
}
#endif
uint8_t system_helpers_check_address(const char *addr)
{
struct addrinfo hint, *res;
int32_t retval;
memset(&hint, 0, sizeof(struct addrinfo));
//ZeroMQ implementation does not use v6 yet
hint.ai_family = AF_INET;
retval = getaddrinfo(addr, NULL, &hint, &res);
//If address can't be resolved or there is something wrong with address,
//getaddrinfo will fail. We don't care about the actual result.
if (retval)
return RETVAL_FAILURE;
freeaddrinfo(res);
return RETVAL_SUCCESS;
}
//Reads uptime and stores value in uptime.
uint8_t system_helpers_read_uint64_from_file(const char *filename,
uint64_t *value)
{
FILE *file_to_read = fopen(filename, "r");
int retval;
if (!file_to_read)
return RETVAL_FAILURE;
retval = fscanf(file_to_read, "%"PRIu64, value);
fclose(file_to_read);
if (retval != 1 || retval == EOF)
return RETVAL_FAILURE;
return RETVAL_SUCCESS;
}
uint8_t system_helpers_write_uint64_to_file(const char *filename,
uint64_t value)
{
//The only user (so far) of this function only uses this value for help.
//Thus, it is not critical if writing fails. Therefore, no need to write to
//partial first, link, etc.
FILE *file_to_write = fopen(filename, "w");
int retval;
if (!file_to_write)
return RETVAL_FAILURE;
retval = fprintf(file_to_write, "%"PRIu64, value);
fclose(file_to_write);
if (retval < 0)
return RETVAL_FAILURE;
else
return RETVAL_SUCCESS;
}
uint8_t system_helpers_read_session_id(const char *path, uint64_t *session_id,
uint64_t *session_id_multip)
{
int32_t retval = 0;
FILE *session_id_file = fopen(path, "r");
if (!session_id_file)
return RETVAL_FAILURE;
retval = fscanf(session_id_file, "%"PRIu64" %"PRIu64, session_id,
session_id_multip);
fclose(session_id_file);
if (retval != 2)
return RETVAL_FAILURE;
else
return RETVAL_SUCCESS;
}