summaryrefslogtreecommitdiff
path: root/usr/iscsi_util.c
blob: 2f1de3e61dc3e0a6b0f22bb6c5f023234df5ce57 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Misc helpers
 *
 * Copyright (C) 2004 Dmitry Yusupov, Alex Aizman
 * Copyright (C) 2006 - 2010 Mike Christie
 * Copyright (C) 2006 - 2010 Red Hat, Inc. All rights reserved.
 * maintained by open-iscsi@googlegroups.com
 *
 * 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 2 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.
 *
 * See the file COPYING included with this distribution for more details.
 */
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>

#include "sysdeps.h"
#include "log.h"
#include "iscsi_settings.h"
#include "iface.h"
#include "session_info.h"
#include "iscsi_util.h"

int setup_abstract_addr(struct sockaddr_un *addr, char *unix_sock_name)
{
	memset(addr, 0, sizeof(*addr));
	addr->sun_family = AF_LOCAL;
	strlcpy(addr->sun_path + 1, unix_sock_name, sizeof(addr->sun_path) - 1);
	return offsetof(struct sockaddr_un, sun_path) +
		strlen(addr->sun_path + 1) + 1;
}

void daemon_init(void)
{
	int fd;

	fd = open("/dev/null", O_RDWR);
	if (fd == -1) {
		exit(-1);
	}

	dup2(fd, 0);
	dup2(fd, 1);
	dup2(fd, 2);
	setsid();
	if (chdir("/") < 0)
		log_debug(1, "Could not chdir to /: %s", strerror(errno));
	close(fd);
}

/*
 * make a best effort at ajusting our nice
 * score and our OOM score, but it's not considered
 * fatal if either adjustment fails
 *
 * return 0 on success of OOM adjustment
 */
int oom_adjust(void)
{
	int fd;
	int res = 0;

	errno = 0;
	if (nice(-10) == -1 && errno != 0)
		log_warning("Could not increase process priority: %s",
			  strerror(errno));

	/*
	 * try the modern method of adjusting our OOM score,
	 * then try the old one, if that fails
	 */
	if ((fd = open("/proc/self/oom_score_adj", O_WRONLY)) >= 0) {
		if ((res = write(fd, "-1000", 5)) < 0)
			log_warning("Could not set /proc/self/oom_score_adj to -1000: %s",
				strerror(errno));
	} else if ((fd = open("/proc/self/oom_adj", O_WRONLY)) >= 0) {
		if ((res = write(fd, "-17", 3)) < 0)
			log_warning("Could not set /proc/self/oom_adj to -16: %s",
				strerror(errno));
	} else
		return -1;

	close(fd);
	if (res < 0)
		return res;
	else
		return 0;
}

char*
str_to_ipport(char *str, int *port, int *tpgt)
{
	char *stpgt, *sport = str, *ip = str;

	if (!strchr(ip, '.')) {
		if (*ip == '[') {
			/* IPv6 with [] */
			if (!(sport = strchr(ip, ']')))
				return NULL;
			*sport++ = '\0';
			ip++;
			str = sport;
		} else {
			/* hostname or ipv6 */
			sport = strchr(ip, ':');
			if (sport) {
				if (strchr(sport + 1, ':'))
					/* ipv6 */
					sport = NULL;
				else
					/* hostname:port */
					str = sport;
			}
		}
	}

	if (sport && (sport = strchr(str, ':'))) {
		*sport++ = '\0';
		*port = strtoul(sport, NULL, 10);
		str = sport;
	}

	if ((stpgt = strchr(str, ','))) {
		*stpgt++ = '\0';
		*tpgt = strtoul(stpgt, NULL, 10);
	} else
		*tpgt = PORTAL_GROUP_TAG_UNKNOWN;

	log_debug(2, "ip %s, port %d, tgpt %d", ip, *port, *tpgt);
	return ip;
}

#define ISCSI_MAX_FILES 16384

int increase_max_files(void)
{
	struct rlimit rl;
	int err;

	err = getrlimit(RLIMIT_NOFILE, &rl);
	if (err) {
		log_debug(1, "Could not get file limit (err %d)", errno);
		return errno;
	}
	log_debug(1, "Max file limits %lu %lu",
		        (long unsigned)rl.rlim_cur,
			(long unsigned)rl.rlim_max);

	if (rl.rlim_cur < ISCSI_MAX_FILES)
		rl.rlim_cur = ISCSI_MAX_FILES;
	if (rl.rlim_max < ISCSI_MAX_FILES)
		rl.rlim_max = ISCSI_MAX_FILES;

	err = setrlimit(RLIMIT_NOFILE, &rl);
	if (err) {
		log_debug(1, "Could not set file limit to %lu/%lu (err %d)",
			  (long unsigned)rl.rlim_cur,
			  (long unsigned)rl.rlim_max, errno);
		return errno;
	}

	return 0;
}

/*
 * from linux kernel
 */
char *strstrip(char *s)
{
	size_t size;
	char *end;

	size = strlen(s);
	if (!size)
		return s;

	end = s + size - 1;
	while (end >= s && isspace(*end))
		end--;
	*(end + 1) = '\0';

	while (*s && isspace(*s))
		s++;

	return s;
}

/**
 * cfg_get_string_param - return param value
 * @pathname: pathname and filename of config file
 * @key: param name
 *
 * Assumes the delim is a "=". "#" comments a line, but if
 * the "#" is after the key= then it is a valid value.
*/
char *cfg_get_string_param(char *pathname, const char *key)
{
	FILE *f = NULL;
	char *line, buffer[1024];
	char *value = NULL, *param, *comment;

	if (!pathname) {
		log_error("No pathname to load %s from", key);
		return NULL;
	}

	if ((f = fopen(pathname, "r"))) {
		while ((line = fgets(buffer, sizeof (buffer), f))) {
			param = strstr(line, key);
			if (!param)
				continue;

			/* make sure it is not commented out */
			comment = strchr(line, '#');
			if (comment) {
				if (comment < param)
					continue;
			}

			param = strchr(param, '=');
			if (!param) {
				log_error("Invalid config line for %s. "
					  "Missing '='.", key);
				continue;
			}

			param++;
			if (!strlen(param)) {
				log_error("Invalid config line for %s. "
					  "Missing value", key);
				continue;
			}

			param = strstrip(param);
			if (!strlen(param)) {
				log_error("Invalid config line for %s. "
					  "Missing value", key);
				continue;
			}

			value = strdup(param);
			break;
		}
		fclose(f);
		if (value)
			log_debug(5, "%s=%s", key, value);
	} else
		log_error("can't open %s configuration file %s", key, pathname);

	return value;
}

/**
 * iscsi_addr_match - check if the addrs are to the same ip
 * @address1: pattern
 * @address2: address to check
 *
 * If address1 is blank then it matches any string passed in.
 */
static int iscsi_addr_match(char *address1, char *address2)
{
	struct addrinfo hints1, hints2, *res1, *res2;
	int rc;

	if (!strlen(address1))
		return 1;

	if (!strcmp(address1, address2))
		return 1;

	memset(&hints1, 0, sizeof(struct addrinfo));
	hints1.ai_family = AF_UNSPEC;
	hints1.ai_socktype = SOCK_STREAM;

	memset(&hints2, 0, sizeof(struct addrinfo));
	hints2.ai_family = AF_UNSPEC;
	hints2.ai_socktype = SOCK_STREAM;

	/*
	 * didn't match so we have to resolve to see if one is a dnsname
	 * that matches an ip address.
	 */
	rc = getaddrinfo(address1, NULL, &hints1, &res1);
	if (rc) {
		log_debug(1, "Match error. Could not resolve %s: %s", address1,
			  gai_strerror(rc));
		return 0;

	}

	rc = getaddrinfo(address2, NULL, &hints2, &res2);
	if (rc) {
		log_debug(1, "Match error. Could not resolve %s: %s", address2,
			  gai_strerror(rc));
		rc = 0;
		goto free_res1;
	}

	if ((res1->ai_addrlen != res2->ai_addrlen) ||
	    memcmp(res1->ai_addr, res2->ai_addr, res2->ai_addrlen))
		rc = 0;
	else
		rc = 1;

	freeaddrinfo(res2);
free_res1:
	freeaddrinfo(res1);
	return rc;
}

int __iscsi_match_session(node_rec_t *rec, char *targetname,
			  char *address, int port, struct iface_rec *iface,
			  unsigned sid)
{
	if (!rec) {
		log_debug(6, "no rec info to match");
		return 1;
	}

	log_debug(6, "match session [%s,%s,%d][%s %s,%s,%s]:%u",
		  rec->name, rec->conn[0].address, rec->conn[0].port,
		  rec->iface.name, rec->iface.transport_name,
		  rec->iface.hwaddress, rec->iface.ipaddress,
		  rec->session.sid);

	if (iface)
		log_debug(6, "to [%s,%s,%d][%s %s,%s,%s]:%u",
			  targetname, address, port, iface->name,
			  iface->transport_name, iface->hwaddress,
			  iface->ipaddress, sid);

	if (rec->session.sid && sid && rec->session.sid != sid)
		return 0;

	if (strlen(rec->name) && strcmp(rec->name, targetname))
		return 0;

	if (!iscsi_addr_match(rec->conn[0].address, address))
		return 0;

	if (rec->conn[0].port != -1 && port != rec->conn[0].port)
		return 0;

	if (!iface_match(&rec->iface, iface))
		return 0;

	return 1;
}

int iscsi_match_session(void *data, struct session_info *info)
{
	return __iscsi_match_session(data, info->targetname,
				     info->persistent_address,
				     info->persistent_port, &info->iface,
				     info->sid);
}

int iscsi_match_session_count(void *data, struct session_info *info)
{
	/*
	 * iscsi_sysfs_for_each_session expects:
	 *   0==match -1==nomatch >0==error
	 * but iscsi_match_session returns:
	 *   1==match 0==nomatch
	 */
	if (iscsi_match_session(data, info))
		return 0;
	return -1;
}

int iscsi_match_target(void *data, struct session_info *info)
{
	return __iscsi_match_session(data, info->targetname,
				     info->persistent_address,
				     info->persistent_port, NULL,
				     MATCH_ANY_SID);
}