summaryrefslogtreecommitdiffstats
path: root/lib.c
blob: 2b09293cfaded961d73fd318c737163f7138f2b4 (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
 * mdadm - manage Linux "md" devices aka RAID arrays.
 *
 * Copyright (C) 2011  Neil Brown <neilb@suse.de>
 *
 *
 *    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.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    Author: Neil Brown
 *    Email: <neilb@suse.de>
 */

#include	"mdadm.h"
#include	"dlink.h"
#include	<ctype.h>
#include	<limits.h>

/**
 * is_string_lq() - Check if string length with NULL byte is lower or equal to requested.
 * @str: string to check.
 * @max_len: max length.
 *
 * @str length must be bigger than 0 and be lower or equal @max_len, including termination byte.
 */
bool is_string_lq(const char * const str, size_t max_len)
{
	assert(str);

	size_t _len = strnlen(str, max_len);

	if (_len > 0 && _len < max_len)
		return true;
	return false;
}

bool is_dev_alive(char *path)
{
	if (!path)
		return false;

	if (access(path, R_OK) == 0)
		return true;

	return false;
}

/* This fill contains various 'library' style function.  They
 * have no dependency on anything outside this file.
 */

int get_mdp_major(void)
{
	static int mdp_major = -1;
	FILE *fl;
	char *w;
	int have_block = 0;
	int have_devices = 0;
	int last_num = -1;

	if (mdp_major != -1)
		return mdp_major;

	fl = fopen("/proc/devices", "r");
	if (!fl)
		return -1;

	while ((w = conf_word(fl, 1))) {
		if (have_block && strcmp(w, "devices:") == 0)
			have_devices = 1;
		have_block =  (strcmp(w, "Block") == 0);
		if (isdigit(w[0]))
			last_num = atoi(w);
		if (have_devices && strcmp(w, "mdp") == 0)
			mdp_major = last_num;
		free(w);
	}
	fclose(fl);

	return mdp_major;
}

char *devid2kname(dev_t devid)
{
	char path[30];
	char link[PATH_MAX];
	static char devnm[32];
	char *cp;
	int n;

	/* Look at the
	 * /sys/dev/block/%d:%d link which must look like
	 * and take the last component.
	 */
	sprintf(path, "/sys/dev/block/%d:%d", major(devid), minor(devid));
	n = readlink(path, link, sizeof(link) - 1);
	if (n > 0) {
		link[n] = 0;
		cp = strrchr(link, '/');
		if (cp) {
			strcpy(devnm, cp + 1);
			return devnm;
		}
	}
	return NULL;
}

char *stat2kname(struct stat *st)
{
	if ((S_IFMT & st->st_mode) != S_IFBLK)
		return NULL;

	return devid2kname(st->st_rdev);
}

char *fd2kname(int fd)
{
	struct stat stb;

	if (fstat(fd, &stb) == 0)
		return stat2kname(&stb);

	return NULL;
}

char *devid2devnm(dev_t devid)
{
	char path[30];
	char link[200];
	static char devnm[32];
	char *cp, *ep;
	int n;

	/* Might be an extended-minor partition or a
	 * named md device. Look at the
	 * /sys/dev/block/%d:%d link which must look like
	 *    ../../block/mdXXX/mdXXXpYY
	 * or
	 *    ...../block/md_FOO
	 */
	sprintf(path, "/sys/dev/block/%d:%d", major(devid), minor(devid));
	n = readlink(path, link, sizeof(link) - 1);
	if (n > 0) {
		link[n] = 0;
		cp = strstr(link, "/block/");
		if (cp) {
			cp += 7;
			ep = strchr(cp, '/');
			if (ep)
				*ep = 0;
			strcpy(devnm, cp);
			return devnm;
		}
	}
	if (major(devid) == MD_MAJOR)
		sprintf(devnm,"md%d", minor(devid));
	else if (major(devid) == (unsigned)get_mdp_major())
		sprintf(devnm,"md_d%d",
			(minor(devid)>>MdpMinorShift));
	else
		return NULL;

	return devnm;
}

char *stat2devnm(struct stat *st)
{
	if ((S_IFMT & st->st_mode) != S_IFBLK)
		return NULL;

	return devid2devnm(st->st_rdev);
}

bool stat_is_md_dev(struct stat *st)
{
	if ((S_IFMT & st->st_mode) != S_IFBLK)
		return false;
	if (major(st->st_rdev) == MD_MAJOR)
		return true;
	if (major(st->st_rdev) == (unsigned)get_mdp_major())
		return true;

	return false;
}

char *fd2devnm(int fd)
{
	struct stat stb;

	if (fstat(fd, &stb) == 0)
		return stat2devnm(&stb);

	return NULL;
}

/*
 * convert a major/minor pair for a block device into a name in /dev, if possible.
 * On the first call, walk /dev collecting name.
 * Put them in a simple linked listfor now.
 */
struct devmap {
	int major, minor;
	char *name;
	struct devmap *next;
} *devlist = NULL;
int devlist_ready = 0;

int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
{
	struct stat st;

	if (S_ISLNK(stb->st_mode)) {
		if (stat(name, &st) != 0)
			return 0;
		stb = &st;
	}

	if ((stb->st_mode&S_IFMT)== S_IFBLK) {
		char *n = xstrdup(name);
		struct devmap *dm = xmalloc(sizeof(*dm));
		if (strncmp(n, "/dev/./", 7) == 0)
			strcpy(n + 4, name + 6);
		if (dm) {
			dm->major = major(stb->st_rdev);
			dm->minor = minor(stb->st_rdev);
			dm->name = n;
			dm->next = devlist;
			devlist = dm;
		}
	}

	return 0;
}

#ifndef HAVE_NFTW
#ifdef HAVE_FTW
int add_dev_1(const char *name, const struct stat *stb, int flag)
{
	return add_dev(name, stb, flag, NULL);
}
int nftw(const char *path,
	 int (*han)(const char *name, const struct stat *stb,
		    int flag, struct FTW *s), int nopenfd, int flags)
{
	return ftw(path, add_dev_1, nopenfd);
}
#else
int nftw(const char *path,
	 int (*han)(const char *name, const struct stat *stb,
		    int flag, struct FTW *s), int nopenfd, int flags)
{
	return 0;
}
#endif /* HAVE_FTW */
#endif /* HAVE_NFTW */

/*
 * Find a block device with the right major/minor number.
 * If we find multiple names, choose the shortest.
 * If we find a name in /dev/md/, we prefer that.
 * This applies only to names for MD devices.
 * If 'prefer' is set (normally to e.g. /by-path/)
 * then we prefer a name which contains that string.
 */
char *map_dev_preferred(int major, int minor, int create,
			char *prefer)
{
	struct devmap *p;
	char *regular = NULL, *preferred=NULL;
	int did_check = 0;

	if (major == 0 && minor == 0)
		return NULL;

 retry:
	if (!devlist_ready) {
		char *dev = "/dev";
		struct stat stb;
		while(devlist) {
			struct devmap *d = devlist;
			devlist = d->next;
			free(d->name);
			free(d);
		}
		if (lstat(dev, &stb) == 0 && S_ISLNK(stb.st_mode))
			dev = "/dev/.";
		nftw(dev, add_dev, 10, FTW_PHYS);
		devlist_ready=1;
		did_check = 1;
	}

	for (p = devlist; p; p = p->next)
		if (p->major == major && p->minor == minor) {
			if (strncmp(p->name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 ||
			    (prefer && strstr(p->name, prefer))) {
				if (preferred == NULL ||
				    strlen(p->name) < strlen(preferred))
					preferred = p->name;
			} else {
				if (regular == NULL ||
				    strlen(p->name) < strlen(regular))
					regular = p->name;
			}
		}
	if (!regular && !preferred && !did_check) {
		devlist_ready = 0;
		goto retry;
	}
	if (create && !regular && !preferred) {
		static char buf[30];
		snprintf(buf, sizeof(buf), "%d:%d", major, minor);
		regular = buf;
	}

	return preferred ? preferred : regular;
}

/* conf_word gets one word from the conf file.
 * if "allow_key", then accept words at the start of a line,
 * otherwise stop when such a word is found.
 * We assume that the file pointer is at the end of a word, so the
 * next character is a space, or a newline.  If not, it is the start of a line.
 */

char *conf_word(FILE *file, int allow_key)
{
	int wsize = 100;
	int len = 0;
	int c;
	int quote;
	int wordfound = 0;
	char *word = xmalloc(wsize);

	while (wordfound == 0) {
		/* at the end of a word.. */
		c = getc(file);
		if (c == '#')
			while (c != EOF && c != '\n')
				c = getc(file);
		if (c == EOF)
			break;
		if (c == '\n')
			continue;

		if (c != ' ' && c != '\t' && ! allow_key) {
			ungetc(c, file);
			break;
		}
		/* looks like it is safe to get a word here, if there is one */
		quote = 0;
		/* first, skip any spaces */
		while (c == ' ' || c == '\t')
			c = getc(file);
		if (c != EOF && c != '\n' && c != '#') {
			/* we really have a character of a word, so start saving it */
			while (c != EOF && c != '\n' &&
			       (quote || (c != ' ' && c != '\t'))) {
				wordfound = 1;
				if (quote && c == quote)
					quote = 0;
				else if (quote == 0 && (c == '\'' || c == '"'))
					quote = c;
				else {
					if (len == wsize-1) {
						wsize += 100;
						word = xrealloc(word, wsize);
					}
					word[len++] = c;
				}
				c = getc(file);
				/* Hack for broken kernels (2.6.14-.24) that put
				 *        "active(auto-read-only)"
				 * in /proc/mdstat instead of
				 *        "active (auto-read-only)"
				 */
				if (c == '(' && len >= 6 &&
				    strncmp(word + len - 6, "active", 6) == 0)
					c = ' ';
			}
		}
		if (c != EOF)
			ungetc(c, file);
	}
	word[len] = 0;

	/* Further HACK for broken kernels.. 2.6.14-2.6.24 */
	if (strcmp(word, "auto-read-only)") == 0)
		strcpy(word, "(auto-read-only)");

/*    printf("word is <%s>\n", word); */
	if (!wordfound) {
		free(word);
		word = NULL;
	}
	return word;
}

void print_quoted(char *str)
{
	/* Printf the string with surrounding quotes
	 * iff needed.
	 * If no space, tab, or quote - leave unchanged.
	 * Else print surrounded by " or ', swapping quotes
	 * when we find one that will cause confusion.
	 */

	char first_quote = 0, q;
	char *c;

	for (c = str; *c; c++) {
		switch(*c) {
		case '\'':
		case '"':
			first_quote = *c;
			break;
		case ' ':
		case '\t':
			first_quote = *c;
			continue;
		default:
			continue;
		}
		break;
	}
	if (!first_quote) {
		printf("%s", str);
		return;
	}

	if (first_quote == '"')
		q = '\'';
	else
		q = '"';
	putchar(q);
	for (c = str; *c; c++) {
		if (*c == q) {
			putchar(q);
			q ^= '"' ^ '\'';
			putchar(q);
		}
		putchar(*c);
	}
	putchar(q);
}

/**
 * is_alphanum() - Check if sign is letter or digit.
 * @c: char to analyze.
 *
 * Similar to isalnum() but additional locales are excluded.
 *
 * Return: %true on success, %false otherwise.
 */
bool is_alphanum(const char c)
{
	if (isupper(c) || islower(c) || isdigit(c) != 0)
		return true;
	return false;
}

/**
 * is_name_posix_compatible() - Check if name is POSIX compatible.
 * @name: name to check.
 *
 *  POSIX portable file name character set contains ASCII letters,
 *  digits, '_', '.', and '-'. Also forbid leading '-'.
 *  The length of the name cannot exceed NAME_MAX - 1 (ensure NULL ending).
 *
 * Return: %true on success, %false otherwise.
 */
bool is_name_posix_compatible(const char * const name)
{
	assert(name);

	char allowed_symbols[] = "-_.";
	const char *n = name;

	if (!is_string_lq(name, NAME_MAX))
		return false;

	if (*n == '-')
		return false;

	while (*n != '\0') {
		if (!is_alphanum(*n) && !strchr(allowed_symbols, *n))
			return false;
		n++;
	}
	return true;
}

int check_env(char *name)
{
	char *val = getenv(name);

	if (val && atoi(val) == 1)
		return 1;

	return 0;
}

unsigned long GCD(unsigned long a, unsigned long b)
{
	while (a != b) {
		if (a < b)
			b -= a;
		if (b < a)
			a -= b;
	}
	return a;
}

/*
 * conf_line reads one logical line from the conffile or mdstat.
 * It skips comments and continues until it finds a line that starts
 * with a non blank/comment.  This character is pushed back for the next call
 * A doubly linked list of words is returned.
 * the first word will be a keyword.  Other words will have had quotes removed.
 */

char *conf_line(FILE *file)
{
	char *w;
	char *list;

	w = conf_word(file, 1);
	if (w == NULL)
		return NULL;

	list = dl_strdup(w);
	free(w);
	dl_init(list);

	while ((w = conf_word(file, 0))){
		char *w2 = dl_strdup(w);
		free(w);
		dl_add(list, w2);
	}
/*    printf("got a line\n");*/
	return list;
}

void free_line(char *line)
{
	char *w;
	for (w = dl_next(line); w != line; w = dl_next(line)) {
		dl_del(w);
		dl_free(w);
	}
	dl_free(line);
}

/**
 * parse_num() - Parse int from string.
 * @dest: Pointer to destination.
 * @num: Pointer to string that is going to be parsed.
 *
 * If string contains anything after a number, error code is returned.
 * The same happens when number is bigger than INT_MAX or smaller than 0.
 * Writes to destination only if successfully read the number.
 *
 * Return: 0 on success, 1 otherwise.
 */
int parse_num(int *dest, const char *num)
{
	char *c = NULL;
	long temp;

	if (!num)
		return 1;

	errno = 0;
	temp = strtol(num, &c, 10);
	if (temp < 0 || temp > INT_MAX || *c || errno != 0 || num == c)
		return 1;
	*dest = temp;
	return 0;
}

/**
 * s_gethostname() - secure get hostname. Assure null-terminated string.
 *
 * @buf: buffer for hostname.
 * @buf_len: buffer length.
 *
 * Return: gethostname() result.
 */
int s_gethostname(char *buf, int buf_len)
{
	assert(buf);

	int ret = gethostname(buf, buf_len);

	buf[buf_len - 1] = 0;

	return ret;
}