Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 1 | /* |
| 2 | * kmod - interface to kernel module operations |
| 3 | * |
| 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
| 5 | * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com> |
| 6 | * Copyright (C) 2013-2014 Intel Corporation. All rights reserved. |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
Lucas De Marchi | dea2dfe | 2014-12-25 23:32:03 -0200 | [diff] [blame] | 19 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <assert.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 23 | #include <ctype.h> |
| 24 | #include <errno.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <stddef.h> |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 29 | #include <string.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 30 | #include <unistd.h> |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 31 | |
Natanael Copa | efb5bfa | 2014-11-17 09:36:29 +0100 | [diff] [blame] | 32 | #include <shared/missing.h> |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 33 | #include <shared/util.h> |
| 34 | |
| 35 | #define USEC_PER_SEC 1000000ULL |
| 36 | #define NSEC_PER_USEC 1000ULL |
| 37 | |
Lucas De Marchi | f4e8c16 | 2014-10-09 01:14:16 -0300 | [diff] [blame] | 38 | static const struct kmod_ext { |
| 39 | const char *ext; |
| 40 | size_t len; |
| 41 | } kmod_exts[] = { |
| 42 | {KMOD_EXTENSION_UNCOMPRESSED, sizeof(KMOD_EXTENSION_UNCOMPRESSED) - 1}, |
| 43 | #ifdef ENABLE_ZLIB |
| 44 | {".ko.gz", sizeof(".ko.gz") - 1}, |
| 45 | #endif |
| 46 | #ifdef ENABLE_XZ |
| 47 | {".ko.xz", sizeof(".ko.xz") - 1}, |
| 48 | #endif |
| 49 | { } |
| 50 | }; |
| 51 | |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 52 | /* string handling functions and memory allocations */ |
| 53 | /* ************************************************************************ */ |
| 54 | |
| 55 | void *memdup(const void *p, size_t n) |
| 56 | { |
| 57 | void *r = malloc(n); |
| 58 | |
| 59 | if (r == NULL) |
| 60 | return NULL; |
| 61 | |
| 62 | return memcpy(r, p, n); |
| 63 | } |
| 64 | |
Lucas De Marchi | 9a43753 | 2014-10-09 11:03:29 -0300 | [diff] [blame] | 65 | char *strchr_replace(char *s, char c, char r) |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 66 | { |
| 67 | char *p; |
| 68 | |
| 69 | for (p = s; *p != '\0'; p++) { |
| 70 | if (*p == c) |
| 71 | *p = r; |
| 72 | } |
| 73 | |
| 74 | return s; |
| 75 | } |
| 76 | |
Lucas De Marchi | 2b0104f | 2014-10-09 00:43:01 -0300 | [diff] [blame] | 77 | /* module-related functions */ |
| 78 | /* ************************************************************************ */ |
| 79 | int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) |
| 80 | { |
| 81 | size_t i; |
| 82 | |
| 83 | for (i = 0; i < PATH_MAX - 1; i++) { |
| 84 | const char c = alias[i]; |
| 85 | switch (c) { |
| 86 | case '-': |
| 87 | buf[i] = '_'; |
| 88 | break; |
| 89 | case ']': |
| 90 | return -EINVAL; |
| 91 | case '[': |
| 92 | while (alias[i] != ']' && alias[i] != '\0') { |
| 93 | buf[i] = alias[i]; |
| 94 | i++; |
| 95 | } |
| 96 | |
| 97 | if (alias[i] != ']') |
| 98 | return -EINVAL; |
| 99 | |
| 100 | buf[i] = alias[i]; |
| 101 | break; |
| 102 | case '\0': |
| 103 | goto finish; |
| 104 | default: |
| 105 | buf[i] = c; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | finish: |
| 110 | buf[i] = '\0'; |
| 111 | if (len) |
| 112 | *len = i; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Lucas De Marchi | 52c9c99 | 2014-10-09 10:59:08 -0300 | [diff] [blame] | 117 | /* |
| 118 | * Replace dashes with underscores. |
| 119 | * Dashes inside character range patterns (e.g. [0-9]) are left unchanged. |
| 120 | * |
| 121 | * For convenience, it returns error if @s is NULL |
| 122 | */ |
| 123 | int underscores(char *s) |
| 124 | { |
| 125 | unsigned int i; |
| 126 | |
| 127 | if (!s) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | for (i = 0; s[i]; i++) { |
| 131 | switch (s[i]) { |
| 132 | case '-': |
| 133 | s[i] = '_'; |
| 134 | break; |
| 135 | case ']': |
| 136 | return -EINVAL; |
| 137 | case '[': |
| 138 | i += strcspn(&s[i], "]"); |
| 139 | if (!s[i]) |
| 140 | return -EINVAL; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Lucas De Marchi | f4e8c16 | 2014-10-09 01:14:16 -0300 | [diff] [blame] | 148 | char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) |
| 149 | { |
| 150 | size_t s; |
| 151 | |
| 152 | for (s = 0; s < PATH_MAX - 1; s++) { |
| 153 | const char c = modname[s]; |
| 154 | if (c == '-') |
| 155 | buf[s] = '_'; |
| 156 | else if (c == '\0' || c == '.') |
| 157 | break; |
| 158 | else |
| 159 | buf[s] = c; |
| 160 | } |
| 161 | |
| 162 | buf[s] = '\0'; |
| 163 | |
| 164 | if (len) |
| 165 | *len = s; |
| 166 | |
| 167 | return buf; |
| 168 | } |
| 169 | |
| 170 | char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) |
| 171 | { |
| 172 | char *modname; |
| 173 | |
| 174 | modname = basename(path); |
| 175 | if (modname == NULL || modname[0] == '\0') |
| 176 | return NULL; |
| 177 | |
| 178 | return modname_normalize(modname, buf, len); |
| 179 | } |
| 180 | |
| 181 | bool path_ends_with_kmod_ext(const char *path, size_t len) |
| 182 | { |
| 183 | const struct kmod_ext *eitr; |
| 184 | |
| 185 | for (eitr = kmod_exts; eitr->ext != NULL; eitr++) { |
| 186 | if (len <= eitr->len) |
| 187 | continue; |
| 188 | if (streq(path + len - eitr->len, eitr->ext)) |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | return false; |
| 193 | } |
| 194 | |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 195 | /* read-like and fread-like functions */ |
| 196 | /* ************************************************************************ */ |
| 197 | ssize_t read_str_safe(int fd, char *buf, size_t buflen) |
| 198 | { |
| 199 | size_t todo = buflen - 1; |
| 200 | size_t done = 0; |
| 201 | |
| 202 | do { |
| 203 | ssize_t r = read(fd, buf + done, todo); |
| 204 | |
| 205 | if (r == 0) |
| 206 | break; |
| 207 | else if (r > 0) { |
| 208 | todo -= r; |
| 209 | done += r; |
| 210 | } else { |
| 211 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 212 | errno == EINTR) |
| 213 | continue; |
| 214 | else |
| 215 | return -errno; |
| 216 | } |
| 217 | } while (todo > 0); |
| 218 | |
| 219 | buf[done] = '\0'; |
| 220 | return done; |
| 221 | } |
| 222 | |
| 223 | ssize_t write_str_safe(int fd, const char *buf, size_t buflen) |
| 224 | { |
| 225 | size_t todo = buflen; |
| 226 | size_t done = 0; |
| 227 | |
| 228 | do { |
| 229 | ssize_t r = write(fd, buf + done, todo); |
| 230 | |
| 231 | if (r == 0) |
| 232 | break; |
| 233 | else if (r > 0) { |
| 234 | todo -= r; |
| 235 | done += r; |
| 236 | } else { |
| 237 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 238 | errno == EINTR) |
| 239 | continue; |
| 240 | else |
| 241 | return -errno; |
| 242 | } |
| 243 | } while (todo > 0); |
| 244 | |
| 245 | return done; |
| 246 | } |
| 247 | |
| 248 | int read_str_long(int fd, long *value, int base) |
| 249 | { |
| 250 | char buf[32], *end; |
| 251 | long v; |
| 252 | int err; |
| 253 | |
| 254 | *value = 0; |
| 255 | err = read_str_safe(fd, buf, sizeof(buf)); |
| 256 | if (err < 0) |
| 257 | return err; |
| 258 | errno = 0; |
| 259 | v = strtol(buf, &end, base); |
| 260 | if (end == buf || !isspace(*end)) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | *value = v; |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int read_str_ulong(int fd, unsigned long *value, int base) |
| 268 | { |
| 269 | char buf[32], *end; |
| 270 | long v; |
| 271 | int err; |
| 272 | |
| 273 | *value = 0; |
| 274 | err = read_str_safe(fd, buf, sizeof(buf)); |
| 275 | if (err < 0) |
| 276 | return err; |
| 277 | errno = 0; |
| 278 | v = strtoul(buf, &end, base); |
| 279 | if (end == buf || !isspace(*end)) |
| 280 | return -EINVAL; |
| 281 | *value = v; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Read one logical line from a configuration file. |
| 287 | * |
| 288 | * Line endings may be escaped with backslashes, to form one logical line from |
| 289 | * several physical lines. No end of line character(s) are included in the |
| 290 | * result. |
| 291 | * |
| 292 | * If linenum is not NULL, it is incremented by the number of physical lines |
| 293 | * which have been read. |
| 294 | */ |
Lucas De Marchi | aafd383 | 2014-10-03 03:25:06 -0300 | [diff] [blame] | 295 | char *freadline_wrapped(FILE *fp, unsigned int *linenum) |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 296 | { |
| 297 | int size = 256; |
| 298 | int i = 0, n = 0; |
| 299 | _cleanup_free_ char *buf = malloc(size); |
| 300 | |
| 301 | if (buf == NULL) |
| 302 | return NULL; |
| 303 | |
| 304 | for(;;) { |
| 305 | int ch = getc_unlocked(fp); |
| 306 | |
| 307 | switch(ch) { |
| 308 | case EOF: |
| 309 | if (i == 0) |
| 310 | return NULL; |
| 311 | /* else fall through */ |
| 312 | |
| 313 | case '\n': |
| 314 | n++; |
| 315 | |
| 316 | { |
Caio Marcelo de Oliveira Filho | 88f6ffe | 2015-01-21 09:37:23 -0200 | [diff] [blame] | 317 | char *ret = buf; |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 318 | ret[i] = '\0'; |
| 319 | buf = NULL; |
| 320 | if (linenum) |
| 321 | *linenum += n; |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | case '\\': |
| 326 | ch = getc_unlocked(fp); |
| 327 | |
| 328 | if (ch == '\n') { |
| 329 | n++; |
| 330 | continue; |
| 331 | } |
| 332 | /* else fall through */ |
| 333 | |
| 334 | default: |
| 335 | buf[i++] = ch; |
| 336 | |
| 337 | if (i == size) { |
| 338 | char *tmp; |
| 339 | size *= 2; |
| 340 | tmp = realloc(buf, size); |
| 341 | if (!tmp) |
| 342 | return NULL; |
| 343 | buf = tmp; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /* path handling functions */ |
| 350 | /* ************************************************************************ */ |
| 351 | |
| 352 | bool path_is_absolute(const char *p) |
| 353 | { |
| 354 | assert(p != NULL); |
| 355 | |
| 356 | return p[0] == '/'; |
| 357 | } |
| 358 | |
| 359 | char *path_make_absolute_cwd(const char *p) |
| 360 | { |
| 361 | _cleanup_free_ char *cwd = NULL; |
| 362 | size_t plen, cwdlen; |
| 363 | char *r; |
| 364 | |
| 365 | if (path_is_absolute(p)) |
| 366 | return strdup(p); |
| 367 | |
| 368 | cwd = get_current_dir_name(); |
| 369 | if (!cwd) |
| 370 | return NULL; |
| 371 | |
| 372 | plen = strlen(p); |
| 373 | cwdlen = strlen(cwd); |
| 374 | |
| 375 | /* cwd + '/' + p + '\0' */ |
| 376 | r = realloc(cwd, cwdlen + 1 + plen + 1); |
| 377 | if (r == NULL) |
| 378 | return NULL; |
| 379 | |
| 380 | cwd = NULL; |
| 381 | r[cwdlen] = '/'; |
| 382 | memcpy(&r[cwdlen + 1], p, plen + 1); |
| 383 | |
| 384 | return r; |
| 385 | } |
| 386 | |
| 387 | static inline int is_dir(const char *path) |
| 388 | { |
| 389 | struct stat st; |
| 390 | |
| 391 | if (stat(path, &st) >= 0) |
| 392 | return S_ISDIR(st.st_mode); |
| 393 | |
| 394 | return -errno; |
| 395 | } |
| 396 | |
| 397 | int mkdir_p(const char *path, int len, mode_t mode) |
| 398 | { |
| 399 | char *start, *end; |
| 400 | |
| 401 | start = strndupa(path, len); |
| 402 | end = start + len; |
| 403 | |
| 404 | /* |
| 405 | * scan backwards, replacing '/' with '\0' while the component doesn't |
| 406 | * exist |
| 407 | */ |
| 408 | for (;;) { |
| 409 | int r = is_dir(start); |
| 410 | if (r > 0) { |
| 411 | end += strlen(end); |
| 412 | |
| 413 | if (end == start + len) |
| 414 | return 0; |
| 415 | |
| 416 | /* end != start, since it would be caught on the first |
| 417 | * iteration */ |
| 418 | *end = '/'; |
| 419 | break; |
| 420 | } else if (r == 0) |
| 421 | return -ENOTDIR; |
| 422 | |
| 423 | if (end == start) |
| 424 | break; |
| 425 | |
| 426 | *end = '\0'; |
| 427 | |
| 428 | /* Find the next component, backwards, discarding extra '/'*/ |
| 429 | while (end > start && *end != '/') |
| 430 | end--; |
| 431 | |
| 432 | while (end > start && *(end - 1) == '/') |
| 433 | end--; |
| 434 | } |
| 435 | |
| 436 | for (; end < start + len;) { |
| 437 | if (mkdir(start, mode) < 0 && errno != EEXIST) |
| 438 | return -errno; |
| 439 | |
| 440 | end += strlen(end); |
| 441 | *end = '/'; |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | int mkdir_parents(const char *path, mode_t mode) |
| 448 | { |
| 449 | char *end = strrchr(path, '/'); |
| 450 | |
| 451 | /* no parent directories */ |
| 452 | if (end == NULL) |
| 453 | return 0; |
| 454 | |
| 455 | return mkdir_p(path, end - path, mode); |
| 456 | } |
| 457 | |
| 458 | unsigned long long ts_usec(const struct timespec *ts) |
| 459 | { |
| 460 | return (unsigned long long) ts->tv_sec * USEC_PER_SEC + |
| 461 | (unsigned long long) ts->tv_nsec / NSEC_PER_USEC; |
| 462 | } |
| 463 | |
| 464 | unsigned long long stat_mstamp(const struct stat *st) |
| 465 | { |
| 466 | #ifdef HAVE_STRUCT_STAT_ST_MTIM |
| 467 | return ts_usec(&st->st_mtim); |
| 468 | #else |
| 469 | return (unsigned long long) st->st_mtime; |
| 470 | #endif |
| 471 | } |