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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* 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
* (at your option) 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/>.
*
* Author: Gris Ge <fge@redhat.com>
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
/* ^ For strerror_r() */
#endif
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "libopeniscsiusr/libopeniscsiusr.h"
#include "misc.h"
#include "node.h"
/* ptr is both input and output pointer.
* count is both input and output pointer.
* When success, both count and ptr will be updated.
* If fail, return LIBISCSI_ERR_NOMEM and no touch to old memory.
*/
static int _grow_node_array(struct iscsi_context *ctx,
struct iscsi_node ***nodes, uint32_t *count)
{
int rc = LIBISCSI_OK;
struct iscsi_node **tmp = NULL;
uint32_t i = 0;
_debug(ctx, "Growing node array from size %" PRIu32 " to %" PRIu32,
*count, *count * 2);
tmp = realloc(*nodes, *count * 2 * sizeof(struct iscsi_node *));
_alloc_null_check(ctx, tmp, rc, out);
for (i = *count; i < *count * 2; ++i)
tmp[i] = NULL;
*count *= 2;
*nodes = tmp;
out:
return rc;
}
static int nodes_append(struct iscsi_context *ctx, struct iscsi_node ***nodes,
uint32_t *real_node_count, uint32_t *array_size,
struct iscsi_node *node)
{
int rc = LIBISCSI_OK;
if (*real_node_count >= *array_size)
_good(_grow_node_array(ctx, nodes, array_size), rc, out);
(*nodes)[(*real_node_count)++] = node;
out:
return rc;
}
int iscsi_nodes_get(struct iscsi_context *ctx, struct iscsi_node ***nodes,
uint32_t *node_count)
{
int rc = LIBISCSI_OK;
struct dirent **namelist = NULL;
int n = 0;
int i = 0;
int j = 0;
int k = 0;
struct iscsi_node *node = NULL;
uint32_t real_node_count = 0;
const char *target_name = NULL;
const char *portal = NULL;
const char *iface_name = NULL;
struct dirent **namelist_portals = NULL;
int p = 0;
struct dirent **namelist_ifaces = NULL;
int f = 0;
char *target_path = NULL;
char *path = NULL;
struct stat path_stat;
char strerr_buff[_STRERR_BUFF_LEN];
assert(ctx != NULL);
assert(nodes != NULL);
assert(node_count != NULL);
*nodes = NULL;
*node_count = 0;
_good(_idbm_lock(ctx), rc, out);
_good(_scandir(ctx, NODE_CONFIG_DIR, &namelist, &n), rc, out);
_debug(ctx, "Got %d target from %s nodes folder", n, NODE_CONFIG_DIR);
*node_count = n & UINT32_MAX;
*nodes = (struct iscsi_node **) calloc(*node_count,
sizeof(struct iscsi_node *));
_alloc_null_check(ctx, *nodes, rc, out);
// New style of nodes folder:
// <target_name>/<address>,<port>,<tpgt>/<iface_name>
// Old style of nodes folder:
// <target_name>/<address>,<port>
for (i = 0; i < n; ++i) {
target_name = namelist[i]->d_name;
_good(_asprintf(&target_path, "%s/%s", NODE_CONFIG_DIR,
target_name), rc, out);
_good(_scandir(ctx, target_path, &namelist_portals, &p),
rc, out);
_debug(ctx, "Got %d portals from %s folder", p, target_path);
free(target_path);
target_path = NULL;
for (j = 0; j < p; ++j) {
portal = namelist_portals[j]->d_name;
_good(_asprintf(&path, "%s/%s/%s", NODE_CONFIG_DIR,
target_name, portal), rc, out);
if (stat(path, &path_stat) != 0) {
_warn(ctx, "Cannot stat path '%s': %d, %s",
path, errno,
_strerror(errno, strerr_buff));
continue;
}
if (S_ISREG(path_stat.st_mode)) {
// Old style of node
_good(_idbm_node_get(ctx, target_name, portal,
NULL, &node),
rc, out);
_good(nodes_append(ctx, nodes,
&real_node_count,
node_count, node),
rc, out);
continue;
}
if (! S_ISDIR(path_stat.st_mode)) {
_warn(ctx, "Invalid iSCSI node configuration "
"file %s, it should be a file or "
"directory.", path);
rc = LIBISCSI_ERR_IDBM;
goto out;
}
_good(_scandir(ctx, path, &namelist_ifaces, &f), rc,
out);
_debug(ctx, "Got %d ifaces from %s folder", f, path);
for (k = 0; k < f; ++k) {
iface_name = namelist_ifaces[k]->d_name;
_good(_idbm_node_get(ctx, target_name, portal,
iface_name, &node),
rc, out);
_good(nodes_append(ctx, nodes,
&real_node_count,
node_count, node),
rc, out);
}
free(path);
path = NULL;
_scandir_free(namelist_ifaces, f);
namelist_ifaces = NULL;
f = 0;
}
_scandir_free(namelist_portals, p);
namelist_portals = NULL;
p = 0;
}
*node_count = real_node_count;
out:
free(path);
free(target_path);
_scandir_free(namelist, n);
_scandir_free(namelist_portals, p);
_scandir_free(namelist_ifaces, f);
_idbm_unlock(ctx);
if (rc != LIBISCSI_OK) {
iscsi_nodes_free(*nodes, *node_count);
*nodes = NULL;
*node_count = 0;
}
return rc;
}
void iscsi_nodes_free(struct iscsi_node **nodes, uint32_t node_count)
{
uint32_t i = 0;
if ((nodes == NULL) || (node_count == 0))
return;
for (i = 0; i < node_count; ++i)
iscsi_node_free(nodes[i]);
free (nodes);
}
void iscsi_node_free(struct iscsi_node *node)
{
free(node);
}
const char *iscsi_node_dump_config(struct iscsi_node *node, bool show_secret)
{
FILE *f = NULL;
char *buff = NULL;
assert(node != NULL);
buff = calloc(1, IDBM_DUMP_SIZE);
if (buff == NULL)
return NULL;
f = fmemopen(buff, IDBM_DUMP_SIZE - 1, "w");
if (f == NULL) {
free(buff);
return NULL;
}
_idbm_node_print(node, f, show_secret);
fclose(f);
return buff;
}
void iscsi_node_print_config(struct iscsi_node *node, bool show_secret)
{
assert(node != NULL);
_idbm_node_print(node, stdout, show_secret);
}
// TODO(Gris Ge): Convert below duplicated codes to macros.
bool iscsi_node_conn_is_ipv6(struct iscsi_node *node)
{
assert(node != NULL);
return node->conn.is_ipv6;
}
const char *iscsi_node_conn_address_get(struct iscsi_node *node)
{
assert(node != NULL);
return node->conn.address;
}
uint32_t iscsi_node_conn_port_get(struct iscsi_node *node)
{
assert(node != NULL);
return node->conn.port;
}
int32_t iscsi_node_tpgt_get(struct iscsi_node *node)
{
assert(node != NULL);
return node->tpgt;
}
const char *iscsi_node_target_name_get(struct iscsi_node *node)
{
assert(node != NULL);
return node->target_name;
}
const char *iscsi_node_iface_name_get(struct iscsi_node *node)
{
assert(node != NULL);
return node->iface.name;
}
const char *iscsi_node_portal_get(struct iscsi_node *node)
{
assert(node != NULL);
return node->portal;
}
|