summaryrefslogtreecommitdiff
path: root/libopeniscsiusr/sysfs.c
blob: 6f590f428efa3419a809fc0d72f1f1da0fcd23be (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright (C) 2017 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>
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <regex.h>
#include <dirent.h>
#include <errno.h>

#include "libopeniscsiusr/libopeniscsiusr_common.h"
#include "sysfs.h"
#include "misc.h"

#define _INT32_STR_MAX_LEN		12
/* ^ The max uint32_t is 4294967296 which requires 11 bytes for string.
 *   The max/min in32_t is 2147483647 or -2147483646 which requires 12 bytes.
 */

#define _SYS_NULL_STR			"(null)"

static int sysfs_read_file(const char *path, uint8_t *buff, size_t buff_size);
static int iscsi_sysfs_prop_get_ll(struct iscsi_context *ctx,
				   const char *dir_path, const char *prop_name,
				   long long int *val,
				   long long int default_value);
static int sysfs_get_dev_path(struct iscsi_context *ctx, const char *path,
			      char *dev_path);

/*
 * dev_path should be char[PATH_MAX].
 */
static int sysfs_read_file(const char *path, uint8_t *buff, size_t buff_size)
{
	int fd = -1;
	int errno_save = 0;
	ssize_t readed = 0;
	ssize_t i = 0;

	assert(path != NULL);
	assert(buff != NULL);
	assert(buff_size != 0);

	memset(buff, 0, buff_size);

	fd = open(path, O_RDONLY);
	if (fd < 0)
		return errno;
	readed = read(fd, buff, buff_size);
	errno_save = errno;
	close(fd);

	if (readed < 0) {
		buff[0] = '\0';
		return errno_save;
	}

	buff[buff_size - 1] = '\0';
	/* Remove the trailing \n */
	for (i = readed - 1; i >= 0; --i) {
		if (buff[i] == '\n') {
			buff[i] = '\0';
			break;
		}
	}

	if (strcmp((char *) buff, _SYS_NULL_STR) == 0)
		buff[0] = '\0';

	return 0;
}

int _sysfs_prop_get_str(struct iscsi_context *ctx, const char *dir_path,
			const char *prop_name, char *buff, size_t buff_size,
			const char *default_value)
{
	char file_path[PATH_MAX];
	int rc = LIBISCSI_OK;
	int errno_save = 0;

	assert(dir_path != NULL);
	assert(prop_name != NULL);
	assert(buff != NULL);

	snprintf(file_path, PATH_MAX, "%s/%s", dir_path, prop_name);

	errno_save = sysfs_read_file(file_path, (uint8_t *) buff, buff_size);
	if (errno_save != 0) {
		if (errno_save == ENOENT) {
			if (default_value == NULL) {
				rc = LIBISCSI_ERR_SYSFS_LOOKUP;
				_error(ctx, "Failed to read '%s': "
				       "file '%s' does not exists", prop_name,
				       file_path);
			} else {
				_info(ctx, "Failed to read '%s': "
				      "file '%s' does not exists, "
				      "using default value %s", prop_name,
				      file_path, default_value);
				memcpy(buff, (void *) default_value,
				       strlen(default_value) + 1);
			}
		} else if (errno_save == EACCES) {
			rc = LIBISCSI_ERR_ACCESS;
			_error(ctx, "Failed to read '%s': "
			       "permission deny when reading '%s'", prop_name,
			       file_path);
		} else {
			rc = LIBISCSI_ERR_BUG;
			_error(ctx, "Failed to read '%s': "
			       "error when reading '%s': %d", prop_name,
			       file_path, errno_save);
		}
	} else {
		_debug(ctx, "Open '%s', got '%s'", file_path, buff);
	}
	return rc;
}

static int iscsi_sysfs_prop_get_ll(struct iscsi_context *ctx,
				 const char *dir_path, const char *prop_name,
				 long long int *val,
				 long long int default_value)
{
	char file_path[PATH_MAX];
	int rc = LIBISCSI_OK;
	int errno_save = 0;
	uint8_t buff[_INT32_STR_MAX_LEN];
	long long int tmp_val = 0;

	assert(dir_path != NULL);
	assert(prop_name != NULL);
	assert(val != NULL);

	*val = 0;

	snprintf(file_path, PATH_MAX, "%s/%s", dir_path, prop_name);

	errno_save = sysfs_read_file(file_path, buff, _INT32_STR_MAX_LEN);
	if (errno_save != 0) {
		if (errno_save == ENOENT) {
			if (default_value == LLONG_MAX) {
				rc = LIBISCSI_ERR_SYSFS_LOOKUP;
				_error(ctx, "Failed to read '%s': "
				       "file '%s' does not exists",
				       prop_name, file_path);
				return rc;
			} else {
				_info(ctx,
				       "Failed to read '%s': "
				      "File '%s' does not exists, using ",
				      "default value %lld",
				      file_path, default_value);
				*val = default_value;
				return rc;
			}
		} else if (errno_save == EACCES) {
			rc = LIBISCSI_ERR_ACCESS;
			_error(ctx, "Permission deny when reading '%s'",
			       file_path);
			return rc;
		} else {
			rc = LIBISCSI_ERR_BUG;
			_error(ctx, "Error when reading '%s': %d", file_path,
			       errno_save);
			return rc;
		}
	}

	tmp_val = strtoll((const char *) buff, NULL, 10 /* base */);
	errno_save = errno;
	if ((errno_save != 0) && (tmp_val == LONG_MAX)) {
		rc = LIBISCSI_ERR_BUG;
		_error(ctx, "Sysfs: %s: Error when converting '%s' "
		       "to number", file_path,  (char *) buff, errno_save);
		return rc;
	}

	*val = tmp_val;

	_debug(ctx, "Open '%s', got %lld", file_path, tmp_val);

	return rc;
}

int _sysfs_prop_get_u32(struct iscsi_context *ctx, const char *dir_path,
			const char *prop_name, uint32_t *val,
			uint32_t default_value)
{
	long long int tmp_val = 0;
	int rc = LIBISCSI_OK;
	long long int dv = default_value;

	if (default_value == UINT32_MAX)
		dv = LLONG_MAX;

	rc = iscsi_sysfs_prop_get_ll(ctx, dir_path, prop_name, &tmp_val,
				     (long long int) dv);
	if (rc == LIBISCSI_OK)
		*val = tmp_val & UINT32_MAX;
	return rc;
}

int _sysfs_prop_get_i32(struct iscsi_context *ctx, const char *dir_path,
			const char *prop_name, int32_t *val,
			int32_t default_value)
{
	long long int tmp_val = 0;
	int rc = LIBISCSI_OK;
	long long int dv = default_value;

	if (default_value == INT32_MAX)
		dv = LLONG_MAX;

	rc = iscsi_sysfs_prop_get_ll(ctx, dir_path, prop_name, &tmp_val,
				     (long long int) dv);

	if (rc == LIBISCSI_OK)
		*val = tmp_val & INT32_MAX;
	return rc;
}

static int sysfs_get_dev_path(struct iscsi_context *ctx, const char *path,
			      char *dev_path)
{
	int rc = LIBISCSI_OK;
	int errno_save = 0;
	regex_t regex;
	regmatch_t reg_match[2];
	int reg_rc = 0;
	int need_free_reg = 0;

	assert(ctx != NULL);
	assert(path != NULL);
	assert(dev_path != NULL);

	memset(dev_path, 0, PATH_MAX);

	if (realpath(path, dev_path) == NULL) {
		errno_save = errno;
		rc = LIBISCSI_ERR_SYSFS_LOOKUP;
		_error(ctx, "realpath() failed on %s with error %d", path,
		       errno_save);
		goto out;
	}

	reg_rc = regcomp(&regex,
			 "\\(.\\{1,\\}/devices/.\\{1,\\}/host[0-9]\\{1,\\}\\)/"
			 "session[0-9]\\{1,\\}/iscsi_session/",
			 0 /* no flag */);
	/* ^ BUG(Gris Ge): This is based on GUESS, should check linux kernel
	 *		   code on this
	 */
	if (reg_rc != 0) {
		rc = LIBISCSI_ERR_SYSFS_LOOKUP;
		_error(ctx, "regcomp() failed %d", reg_rc);
		goto out;
	}
	need_free_reg = 1;
	if (regexec(&regex, dev_path, 2 /* count of max matches */,
		    reg_match, 0 /* no flags */) != 0) {
		rc = LIBISCSI_ERR_SYSFS_LOOKUP;
		_error(ctx, "regexec() not match for %s", dev_path);
		goto out;
	}

	*(dev_path + reg_match[1].rm_eo ) = '\0';

	_debug(ctx, "Got dev path of '%s': '%s'", path, dev_path);

out:
	if (need_free_reg)
		regfree(&regex);
	if (rc != LIBISCSI_OK)
		memset(dev_path, 0, PATH_MAX);
	return rc;
}

int _iscsi_host_id_of_session(struct iscsi_context *ctx, uint32_t sid,
			      uint32_t *host_id)
{
	int rc = LIBISCSI_OK;
	char sys_se_dir_path[PATH_MAX];
	char sys_dev_path[PATH_MAX];
	char sys_scsi_host_dir_path[PATH_MAX];
	struct dirent **namelist = NULL;
	int n = 0;
	const char *host_id_str = NULL;
	int i = 0;
	const char iscsi_host_dir_str[] = "/iscsi_host/";
	const unsigned int iscsi_host_dir_strlen = strlen(iscsi_host_dir_str);


	assert(ctx != NULL);
	assert(sid != 0);
	assert(host_id != NULL);

	snprintf(sys_se_dir_path, PATH_MAX, "%s/session%" PRIu32,
		 _ISCSI_SYS_SESSION_DIR, sid);

	*host_id = 0;

	_good(sysfs_get_dev_path(ctx, sys_se_dir_path, sys_dev_path), rc, out);

	if ((strlen(sys_dev_path) + iscsi_host_dir_strlen) >= PATH_MAX) {
		rc = LIBISCSI_ERR_SYSFS_LOOKUP;
		_error(ctx, "Pathname too long: %s%s",
		       sys_dev_path, iscsi_host_dir_str);
		goto out;
	}

	strncpy(sys_scsi_host_dir_path, sys_dev_path, PATH_MAX);
	strncat(sys_scsi_host_dir_path, iscsi_host_dir_str,
		PATH_MAX - iscsi_host_dir_strlen);

	n = scandir(sys_scsi_host_dir_path, &namelist, _scan_filter_skip_dot,
		    alphasort);
	if (n != 1) {
		rc = LIBISCSI_ERR_SYSFS_LOOKUP;
		_error(ctx, "Got unexpected(should be 1) file in folder %s",
		       sys_scsi_host_dir_path);
		goto out;
	}
	host_id_str = namelist[0]->d_name;

	if (sscanf(host_id_str, "host%" SCNu32, host_id) != 1) {
		rc = LIBISCSI_ERR_SYSFS_LOOKUP;
		_error(ctx, "sscanf() failed on string %s", host_id_str);
		goto out;
	}

out:
	for (i = n - 1; i >= 0; --i)
		free(namelist[i]);
	free(namelist);

	return rc;
}