]>
Commit | Line | Data |
---|---|---|
78c0a3b1 N |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2011 Neil Brown <neilb@suse.de> | |
5 | * | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
22 | * Email: <neilb@suse.de> | |
23 | */ | |
24 | ||
25 | #include "mdadm.h" | |
d0c017a6 | 26 | #include "dlink.h" |
78c0a3b1 | 27 | #include <ctype.h> |
60815698 | 28 | #include <limits.h> |
78c0a3b1 | 29 | |
67417d92 MT |
30 | /** |
31 | * is_string_lq() - Check if string length with NULL byte is lower or equal to requested. | |
32 | * @str: string to check. | |
33 | * @max_len: max length. | |
34 | * | |
35 | * @str length must be bigger than 0 and be lower or equal @max_len, including termination byte. | |
36 | */ | |
37 | bool is_string_lq(const char * const str, size_t max_len) | |
38 | { | |
39 | assert(str); | |
40 | ||
41 | size_t _len = strnlen(str, max_len); | |
42 | ||
43 | if (_len > 0 && _len < max_len) | |
44 | return true; | |
45 | return false; | |
46 | } | |
47 | ||
a35aa68f XN |
48 | bool is_dev_alive(char *path) |
49 | { | |
50 | if (!path) | |
51 | return false; | |
52 | ||
53 | if (access(path, R_OK) == 0) | |
54 | return true; | |
55 | ||
56 | return false; | |
57 | } | |
58 | ||
78c0a3b1 N |
59 | /* This fill contains various 'library' style function. They |
60 | * have no dependency on anything outside this file. | |
61 | */ | |
62 | ||
63 | int get_mdp_major(void) | |
64 | { | |
ad7ac9ac | 65 | static int mdp_major = -1; |
78c0a3b1 N |
66 | FILE *fl; |
67 | char *w; | |
68 | int have_block = 0; | |
69 | int have_devices = 0; | |
70 | int last_num = -1; | |
71 | ||
72 | if (mdp_major != -1) | |
73 | return mdp_major; | |
ad7ac9ac | 74 | |
78c0a3b1 N |
75 | fl = fopen("/proc/devices", "r"); |
76 | if (!fl) | |
77 | return -1; | |
ad7ac9ac | 78 | |
78c0a3b1 | 79 | while ((w = conf_word(fl, 1))) { |
ad7ac9ac | 80 | if (have_block && strcmp(w, "devices:") == 0) |
78c0a3b1 | 81 | have_devices = 1; |
ad7ac9ac | 82 | have_block = (strcmp(w, "Block") == 0); |
78c0a3b1 N |
83 | if (isdigit(w[0])) |
84 | last_num = atoi(w); | |
ad7ac9ac | 85 | if (have_devices && strcmp(w, "mdp") == 0) |
78c0a3b1 N |
86 | mdp_major = last_num; |
87 | free(w); | |
88 | } | |
89 | fclose(fl); | |
ad7ac9ac | 90 | |
78c0a3b1 N |
91 | return mdp_major; |
92 | } | |
93 | ||
d3c40fab | 94 | char *devid2kname(dev_t devid) |
bc17158d N |
95 | { |
96 | char path[30]; | |
52a94085 | 97 | char link[PATH_MAX]; |
bc17158d N |
98 | static char devnm[32]; |
99 | char *cp; | |
100 | int n; | |
101 | ||
102 | /* Look at the | |
103 | * /sys/dev/block/%d:%d link which must look like | |
104 | * and take the last component. | |
105 | */ | |
d3c40fab | 106 | sprintf(path, "/sys/dev/block/%d:%d", major(devid), minor(devid)); |
ad7ac9ac | 107 | n = readlink(path, link, sizeof(link) - 1); |
bc17158d N |
108 | if (n > 0) { |
109 | link[n] = 0; | |
110 | cp = strrchr(link, '/'); | |
111 | if (cp) { | |
ad7ac9ac | 112 | strcpy(devnm, cp + 1); |
bc17158d N |
113 | return devnm; |
114 | } | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
f96b1302 AP |
119 | char *stat2kname(struct stat *st) |
120 | { | |
121 | if ((S_IFMT & st->st_mode) != S_IFBLK) | |
122 | return NULL; | |
ad7ac9ac | 123 | |
f96b1302 AP |
124 | return devid2kname(st->st_rdev); |
125 | } | |
126 | ||
127 | char *fd2kname(int fd) | |
128 | { | |
129 | struct stat stb; | |
ad7ac9ac | 130 | |
f96b1302 AP |
131 | if (fstat(fd, &stb) == 0) |
132 | return stat2kname(&stb); | |
ad7ac9ac | 133 | |
f96b1302 AP |
134 | return NULL; |
135 | } | |
136 | ||
13db17bd | 137 | char *devid2devnm(dev_t devid) |
78c0a3b1 N |
138 | { |
139 | char path[30]; | |
140 | char link[200]; | |
4dd2df09 N |
141 | static char devnm[32]; |
142 | char *cp, *ep; | |
78c0a3b1 N |
143 | int n; |
144 | ||
4dd2df09 N |
145 | /* Might be an extended-minor partition or a |
146 | * named md device. Look at the | |
147 | * /sys/dev/block/%d:%d link which must look like | |
148 | * ../../block/mdXXX/mdXXXpYY | |
149 | * or | |
150 | * ...../block/md_FOO | |
151 | */ | |
ad7ac9ac JS |
152 | sprintf(path, "/sys/dev/block/%d:%d", major(devid), minor(devid)); |
153 | n = readlink(path, link, sizeof(link) - 1); | |
4dd2df09 | 154 | if (n > 0) { |
78c0a3b1 | 155 | link[n] = 0; |
4dd2df09 N |
156 | cp = strstr(link, "/block/"); |
157 | if (cp) { | |
158 | cp += 7; | |
159 | ep = strchr(cp, '/'); | |
160 | if (ep) | |
161 | *ep = 0; | |
162 | strcpy(devnm, cp); | |
163 | return devnm; | |
164 | } | |
78c0a3b1 | 165 | } |
4dd2df09 N |
166 | if (major(devid) == MD_MAJOR) |
167 | sprintf(devnm,"md%d", minor(devid)); | |
168 | else if (major(devid) == (unsigned)get_mdp_major()) | |
169 | sprintf(devnm,"md_d%d", | |
170 | (minor(devid)>>MdpMinorShift)); | |
171 | else | |
172 | return NULL; | |
ad7ac9ac | 173 | |
4dd2df09 N |
174 | return devnm; |
175 | } | |
78c0a3b1 | 176 | |
4dd2df09 N |
177 | char *stat2devnm(struct stat *st) |
178 | { | |
179 | if ((S_IFMT & st->st_mode) != S_IFBLK) | |
180 | return NULL; | |
ad7ac9ac | 181 | |
4dd2df09 | 182 | return devid2devnm(st->st_rdev); |
78c0a3b1 N |
183 | } |
184 | ||
27ad4900 LG |
185 | bool stat_is_md_dev(struct stat *st) |
186 | { | |
187 | if ((S_IFMT & st->st_mode) != S_IFBLK) | |
188 | return false; | |
189 | if (major(st->st_rdev) == MD_MAJOR) | |
190 | return true; | |
191 | if (major(st->st_rdev) == (unsigned)get_mdp_major()) | |
192 | return true; | |
193 | ||
194 | return false; | |
195 | } | |
196 | ||
4dd2df09 | 197 | char *fd2devnm(int fd) |
78c0a3b1 N |
198 | { |
199 | struct stat stb; | |
ad7ac9ac | 200 | |
78c0a3b1 | 201 | if (fstat(fd, &stb) == 0) |
4dd2df09 | 202 | return stat2devnm(&stb); |
ad7ac9ac | 203 | |
4dd2df09 | 204 | return NULL; |
78c0a3b1 N |
205 | } |
206 | ||
78c0a3b1 N |
207 | /* |
208 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
209 | * On the first call, walk /dev collecting name. | |
210 | * Put them in a simple linked listfor now. | |
211 | */ | |
212 | struct devmap { | |
5d500228 N |
213 | int major, minor; |
214 | char *name; | |
215 | struct devmap *next; | |
78c0a3b1 N |
216 | } *devlist = NULL; |
217 | int devlist_ready = 0; | |
218 | ||
219 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
220 | { | |
221 | struct stat st; | |
222 | ||
223 | if (S_ISLNK(stb->st_mode)) { | |
224 | if (stat(name, &st) != 0) | |
225 | return 0; | |
226 | stb = &st; | |
227 | } | |
228 | ||
229 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
503975b9 N |
230 | char *n = xstrdup(name); |
231 | struct devmap *dm = xmalloc(sizeof(*dm)); | |
ad7ac9ac JS |
232 | if (strncmp(n, "/dev/./", 7) == 0) |
233 | strcpy(n + 4, name + 6); | |
78c0a3b1 N |
234 | if (dm) { |
235 | dm->major = major(stb->st_rdev); | |
236 | dm->minor = minor(stb->st_rdev); | |
237 | dm->name = n; | |
238 | dm->next = devlist; | |
239 | devlist = dm; | |
240 | } | |
241 | } | |
ad7ac9ac | 242 | |
78c0a3b1 N |
243 | return 0; |
244 | } | |
245 | ||
246 | #ifndef HAVE_NFTW | |
247 | #ifdef HAVE_FTW | |
248 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
249 | { | |
250 | return add_dev(name, stb, flag, NULL); | |
251 | } | |
ad7ac9ac JS |
252 | int nftw(const char *path, |
253 | int (*han)(const char *name, const struct stat *stb, | |
254 | int flag, struct FTW *s), int nopenfd, int flags) | |
78c0a3b1 N |
255 | { |
256 | return ftw(path, add_dev_1, nopenfd); | |
257 | } | |
258 | #else | |
ad7ac9ac JS |
259 | int nftw(const char *path, |
260 | int (*han)(const char *name, const struct stat *stb, | |
261 | int flag, struct FTW *s), int nopenfd, int flags) | |
78c0a3b1 N |
262 | { |
263 | return 0; | |
264 | } | |
265 | #endif /* HAVE_FTW */ | |
266 | #endif /* HAVE_NFTW */ | |
267 | ||
268 | /* | |
269 | * Find a block device with the right major/minor number. | |
270 | * If we find multiple names, choose the shortest. | |
271 | * If we find a name in /dev/md/, we prefer that. | |
272 | * This applies only to names for MD devices. | |
c2ecf5f6 N |
273 | * If 'prefer' is set (normally to e.g. /by-path/) |
274 | * then we prefer a name which contains that string. | |
78c0a3b1 | 275 | */ |
c2ecf5f6 N |
276 | char *map_dev_preferred(int major, int minor, int create, |
277 | char *prefer) | |
78c0a3b1 N |
278 | { |
279 | struct devmap *p; | |
280 | char *regular = NULL, *preferred=NULL; | |
281 | int did_check = 0; | |
282 | ||
283 | if (major == 0 && minor == 0) | |
781f7efb | 284 | return NULL; |
78c0a3b1 N |
285 | |
286 | retry: | |
287 | if (!devlist_ready) { | |
288 | char *dev = "/dev"; | |
289 | struct stat stb; | |
290 | while(devlist) { | |
291 | struct devmap *d = devlist; | |
292 | devlist = d->next; | |
293 | free(d->name); | |
294 | free(d); | |
295 | } | |
ad7ac9ac | 296 | if (lstat(dev, &stb) == 0 && S_ISLNK(stb.st_mode)) |
78c0a3b1 N |
297 | dev = "/dev/."; |
298 | nftw(dev, add_dev, 10, FTW_PHYS); | |
299 | devlist_ready=1; | |
300 | did_check = 1; | |
301 | } | |
302 | ||
ad7ac9ac JS |
303 | for (p = devlist; p; p = p->next) |
304 | if (p->major == major && p->minor == minor) { | |
b9ce7ab0 | 305 | if (strncmp(p->name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 || |
ad7ac9ac | 306 | (prefer && strstr(p->name, prefer))) { |
78c0a3b1 N |
307 | if (preferred == NULL || |
308 | strlen(p->name) < strlen(preferred)) | |
309 | preferred = p->name; | |
310 | } else { | |
311 | if (regular == NULL || | |
312 | strlen(p->name) < strlen(regular)) | |
313 | regular = p->name; | |
314 | } | |
315 | } | |
316 | if (!regular && !preferred && !did_check) { | |
317 | devlist_ready = 0; | |
318 | goto retry; | |
319 | } | |
320 | if (create && !regular && !preferred) { | |
321 | static char buf[30]; | |
322 | snprintf(buf, sizeof(buf), "%d:%d", major, minor); | |
323 | regular = buf; | |
324 | } | |
325 | ||
326 | return preferred ? preferred : regular; | |
327 | } | |
328 | ||
78c0a3b1 N |
329 | /* conf_word gets one word from the conf file. |
330 | * if "allow_key", then accept words at the start of a line, | |
331 | * otherwise stop when such a word is found. | |
332 | * We assume that the file pointer is at the end of a word, so the | |
333 | * next character is a space, or a newline. If not, it is the start of a line. | |
334 | */ | |
335 | ||
336 | char *conf_word(FILE *file, int allow_key) | |
337 | { | |
338 | int wsize = 100; | |
339 | int len = 0; | |
340 | int c; | |
341 | int quote; | |
342 | int wordfound = 0; | |
503975b9 | 343 | char *word = xmalloc(wsize); |
78c0a3b1 | 344 | |
ad7ac9ac | 345 | while (wordfound == 0) { |
78c0a3b1 N |
346 | /* at the end of a word.. */ |
347 | c = getc(file); | |
348 | if (c == '#') | |
349 | while (c != EOF && c != '\n') | |
350 | c = getc(file); | |
ad7ac9ac JS |
351 | if (c == EOF) |
352 | break; | |
353 | if (c == '\n') | |
354 | continue; | |
78c0a3b1 N |
355 | |
356 | if (c != ' ' && c != '\t' && ! allow_key) { | |
357 | ungetc(c, file); | |
358 | break; | |
359 | } | |
360 | /* looks like it is safe to get a word here, if there is one */ | |
361 | quote = 0; | |
362 | /* first, skip any spaces */ | |
363 | while (c == ' ' || c == '\t') | |
364 | c = getc(file); | |
365 | if (c != EOF && c != '\n' && c != '#') { | |
366 | /* we really have a character of a word, so start saving it */ | |
ad7ac9ac JS |
367 | while (c != EOF && c != '\n' && |
368 | (quote || (c != ' ' && c != '\t'))) { | |
78c0a3b1 | 369 | wordfound = 1; |
ad7ac9ac JS |
370 | if (quote && c == quote) |
371 | quote = 0; | |
78c0a3b1 N |
372 | else if (quote == 0 && (c == '\'' || c == '"')) |
373 | quote = c; | |
374 | else { | |
375 | if (len == wsize-1) { | |
376 | wsize += 100; | |
503975b9 | 377 | word = xrealloc(word, wsize); |
78c0a3b1 N |
378 | } |
379 | word[len++] = c; | |
380 | } | |
381 | c = getc(file); | |
382 | /* Hack for broken kernels (2.6.14-.24) that put | |
383 | * "active(auto-read-only)" | |
384 | * in /proc/mdstat instead of | |
385 | * "active (auto-read-only)" | |
386 | */ | |
ad7ac9ac JS |
387 | if (c == '(' && len >= 6 && |
388 | strncmp(word + len - 6, "active", 6) == 0) | |
78c0a3b1 N |
389 | c = ' '; |
390 | } | |
391 | } | |
ad7ac9ac JS |
392 | if (c != EOF) |
393 | ungetc(c, file); | |
78c0a3b1 N |
394 | } |
395 | word[len] = 0; | |
396 | ||
397 | /* Further HACK for broken kernels.. 2.6.14-2.6.24 */ | |
398 | if (strcmp(word, "auto-read-only)") == 0) | |
399 | strcpy(word, "(auto-read-only)"); | |
400 | ||
401 | /* printf("word is <%s>\n", word); */ | |
402 | if (!wordfound) { | |
403 | free(word); | |
404 | word = NULL; | |
405 | } | |
406 | return word; | |
407 | } | |
7103b9b8 N |
408 | |
409 | void print_quoted(char *str) | |
410 | { | |
411 | /* Printf the string with surrounding quotes | |
412 | * iff needed. | |
413 | * If no space, tab, or quote - leave unchanged. | |
414 | * Else print surrounded by " or ', swapping quotes | |
415 | * when we find one that will cause confusion. | |
416 | */ | |
417 | ||
418 | char first_quote = 0, q; | |
419 | char *c; | |
420 | ||
421 | for (c = str; *c; c++) { | |
422 | switch(*c) { | |
423 | case '\'': | |
424 | case '"': | |
425 | first_quote = *c; | |
426 | break; | |
427 | case ' ': | |
428 | case '\t': | |
429 | first_quote = *c; | |
430 | continue; | |
431 | default: | |
432 | continue; | |
433 | } | |
434 | break; | |
435 | } | |
436 | if (!first_quote) { | |
437 | printf("%s", str); | |
438 | return; | |
439 | } | |
440 | ||
441 | if (first_quote == '"') | |
442 | q = '\''; | |
443 | else | |
444 | q = '"'; | |
445 | putchar(q); | |
446 | for (c = str; *c; c++) { | |
447 | if (*c == q) { | |
448 | putchar(q); | |
449 | q ^= '"' ^ '\''; | |
450 | putchar(q); | |
451 | } | |
452 | putchar(*c); | |
453 | } | |
454 | putchar(q); | |
455 | } | |
456 | ||
e2eb503b MT |
457 | /** |
458 | * is_alphanum() - Check if sign is letter or digit. | |
459 | * @c: char to analyze. | |
460 | * | |
461 | * Similar to isalnum() but additional locales are excluded. | |
462 | * | |
463 | * Return: %true on success, %false otherwise. | |
464 | */ | |
465 | bool is_alphanum(const char c) | |
7103b9b8 | 466 | { |
e2eb503b MT |
467 | if (isupper(c) || islower(c) || isdigit(c) != 0) |
468 | return true; | |
469 | return false; | |
470 | } | |
471 | ||
472 | /** | |
473 | * is_name_posix_compatible() - Check if name is POSIX compatible. | |
474 | * @name: name to check. | |
475 | * | |
476 | * POSIX portable file name character set contains ASCII letters, | |
477 | * digits, '_', '.', and '-'. Also forbid leading '-'. | |
478 | * The length of the name cannot exceed NAME_MAX - 1 (ensure NULL ending). | |
479 | * | |
480 | * Return: %true on success, %false otherwise. | |
481 | */ | |
482 | bool is_name_posix_compatible(const char * const name) | |
483 | { | |
484 | assert(name); | |
485 | ||
486 | char allowed_symbols[] = "-_."; | |
487 | const char *n = name; | |
488 | ||
489 | if (!is_string_lq(name, NAME_MAX)) | |
490 | return false; | |
491 | ||
492 | if (*n == '-') | |
493 | return false; | |
494 | ||
495 | while (*n != '\0') { | |
496 | if (!is_alphanum(*n) && !strchr(allowed_symbols, *n)) | |
497 | return false; | |
498 | n++; | |
7103b9b8 | 499 | } |
e2eb503b | 500 | return true; |
7103b9b8 | 501 | } |
06d2ffc3 | 502 | |
f8fcf7a1 BS |
503 | int check_env(char *name) |
504 | { | |
505 | char *val = getenv(name); | |
506 | ||
507 | if (val && atoi(val) == 1) | |
508 | return 1; | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
2eba8496 N |
513 | unsigned long GCD(unsigned long a, unsigned long b) |
514 | { | |
515 | while (a != b) { | |
516 | if (a < b) | |
517 | b -= a; | |
518 | if (b < a) | |
519 | a -= b; | |
520 | } | |
521 | return a; | |
522 | } | |
d0c017a6 N |
523 | |
524 | /* | |
525 | * conf_line reads one logical line from the conffile or mdstat. | |
526 | * It skips comments and continues until it finds a line that starts | |
527 | * with a non blank/comment. This character is pushed back for the next call | |
528 | * A doubly linked list of words is returned. | |
529 | * the first word will be a keyword. Other words will have had quotes removed. | |
530 | */ | |
531 | ||
532 | char *conf_line(FILE *file) | |
533 | { | |
534 | char *w; | |
535 | char *list; | |
536 | ||
537 | w = conf_word(file, 1); | |
781f7efb JS |
538 | if (w == NULL) |
539 | return NULL; | |
d0c017a6 N |
540 | |
541 | list = dl_strdup(w); | |
542 | free(w); | |
543 | dl_init(list); | |
544 | ||
ad7ac9ac | 545 | while ((w = conf_word(file, 0))){ |
d0c017a6 N |
546 | char *w2 = dl_strdup(w); |
547 | free(w); | |
548 | dl_add(list, w2); | |
549 | } | |
550 | /* printf("got a line\n");*/ | |
551 | return list; | |
552 | } | |
553 | ||
554 | void free_line(char *line) | |
555 | { | |
556 | char *w; | |
ad7ac9ac | 557 | for (w = dl_next(line); w != line; w = dl_next(line)) { |
d0c017a6 N |
558 | dl_del(w); |
559 | dl_free(w); | |
560 | } | |
561 | dl_free(line); | |
562 | } | |
60815698 MG |
563 | |
564 | /** | |
565 | * parse_num() - Parse int from string. | |
566 | * @dest: Pointer to destination. | |
567 | * @num: Pointer to string that is going to be parsed. | |
568 | * | |
569 | * If string contains anything after a number, error code is returned. | |
570 | * The same happens when number is bigger than INT_MAX or smaller than 0. | |
571 | * Writes to destination only if successfully read the number. | |
572 | * | |
573 | * Return: 0 on success, 1 otherwise. | |
574 | */ | |
25aa7329 | 575 | int parse_num(int *dest, const char *num) |
60815698 MG |
576 | { |
577 | char *c = NULL; | |
578 | long temp; | |
579 | ||
580 | if (!num) | |
581 | return 1; | |
582 | ||
583 | errno = 0; | |
584 | temp = strtol(num, &c, 10); | |
585 | if (temp < 0 || temp > INT_MAX || *c || errno != 0 || num == c) | |
586 | return 1; | |
587 | *dest = temp; | |
588 | return 0; | |
589 | } | |
21e622f2 BK |
590 | |
591 | /** | |
592 | * s_gethostname() - secure get hostname. Assure null-terminated string. | |
593 | * | |
594 | * @buf: buffer for hostname. | |
595 | * @buf_len: buffer length. | |
596 | * | |
597 | * Return: gethostname() result. | |
598 | */ | |
599 | int s_gethostname(char *buf, int buf_len) | |
600 | { | |
601 | assert(buf); | |
602 | ||
603 | int ret = gethostname(buf, buf_len); | |
604 | ||
605 | buf[buf_len - 1] = 0; | |
606 | ||
607 | return ret; | |
608 | } |