David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 1 | /* |
David Hendricks | 80cbf94 | 2015-10-30 18:24:40 -0700 | [diff] [blame] | 2 | * Copyright 2015, The Chromium OS Authors |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following |
| 13 | * disclaimer in the documentation and/or other materials provided |
| 14 | * with the distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | * scanft() is derived from mosys source,s which were released under the |
| 32 | * BSD license |
| 33 | */ |
| 34 | |
| 35 | #include <dirent.h> |
| 36 | #include <errno.h> |
David Hendricks | 80cbf94 | 2015-10-30 18:24:40 -0700 | [diff] [blame] | 37 | #include <glob.h> |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 38 | #include <limits.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <unistd.h> |
David Hendricks | 80cbf94 | 2015-10-30 18:24:40 -0700 | [diff] [blame] | 43 | #include <arpa/inet.h> |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 44 | #include <sys/fcntl.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <sys/types.h> |
| 47 | |
Nikolai Artemiev | 7ecd7c4 | 2021-02-12 11:43:14 +1100 | [diff] [blame] | 48 | #include "file.h" |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 49 | #include "flash.h" |
| 50 | |
Brian Norris | 0938894 | 2016-03-28 12:57:01 -0700 | [diff] [blame] | 51 | /* Like strstr(), but allowing NULL bytes within haystack */ |
| 52 | static int __find_string(const char *haystack, size_t hlen, const char *needle) |
| 53 | { |
| 54 | const char *p = haystack; |
| 55 | const char *end = haystack + hlen; |
| 56 | |
| 57 | while (p < end) { |
| 58 | if (strstr(p, needle)) |
| 59 | return 1; |
| 60 | p = memchr(p, '\0', end - p); |
| 61 | if (!p) |
| 62 | /* Not found? We're at the end */ |
| 63 | return 0; |
| 64 | else |
| 65 | /* Skip past the NULL separator */ |
| 66 | p++; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
David Hendricks | 1d2f41b | 2015-11-03 15:48:20 -0800 | [diff] [blame] | 72 | /* returns 1 if string if found, 0 if not, and <0 to indicate error */ |
| 73 | static int find_string(const char *path, const char *str) |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 74 | { |
Brian Norris | 0938894 | 2016-03-28 12:57:01 -0700 | [diff] [blame] | 75 | char contents[4096]; |
| 76 | int fd, ret; |
| 77 | ssize_t len; |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 78 | |
Brian Norris | 0938894 | 2016-03-28 12:57:01 -0700 | [diff] [blame] | 79 | msg_pdbg("%s: checking path \"%s\" for contents \"%s\"\n", |
| 80 | __func__, path, str); |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 81 | |
David Hendricks | 1d2f41b | 2015-11-03 15:48:20 -0800 | [diff] [blame] | 82 | fd = open(path, O_RDONLY, 0); |
| 83 | if (fd < 0) { |
| 84 | msg_gerr("Cannot open file \"%s\"\n", path); |
| 85 | ret = -1; |
| 86 | goto find_string_exit_0; |
| 87 | } |
| 88 | |
Brian Norris | 0938894 | 2016-03-28 12:57:01 -0700 | [diff] [blame] | 89 | /* mmap() (or even read() with a file length) would be nice but these |
| 90 | * might not be implemented for files in sysfs and procfs. |
| 91 | * Let's also leave room for a terminator. */ |
| 92 | len = read(fd, contents, sizeof(contents) - 1); |
| 93 | if (len == -1) { |
| 94 | msg_gerr("Cannot read file \"%s\"\n", path); |
| 95 | ret = -1; |
| 96 | goto find_string_exit_1; |
David Hendricks | 1d2f41b | 2015-11-03 15:48:20 -0800 | [diff] [blame] | 97 | } |
Brian Norris | 0938894 | 2016-03-28 12:57:01 -0700 | [diff] [blame] | 98 | /* Terminate the contents, in case they weren't terminated for us */ |
| 99 | contents[len++] = '\0'; |
| 100 | ret = __find_string(contents, len, str); |
David Hendricks | 1d2f41b | 2015-11-03 15:48:20 -0800 | [diff] [blame] | 101 | |
| 102 | find_string_exit_1: |
| 103 | close(fd); |
| 104 | find_string_exit_0: |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
Brian Norris | 0938894 | 2016-03-28 12:57:01 -0700 | [diff] [blame] | 108 | /* |
David Hendricks | 1d2f41b | 2015-11-03 15:48:20 -0800 | [diff] [blame] | 109 | * scanft - scan filetree for file with option to search for content |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 110 | * |
| 111 | * @root: Where to begin search |
| 112 | * @filename: Name of file to search for |
David Hendricks | 1d2f41b | 2015-11-03 15:48:20 -0800 | [diff] [blame] | 113 | * @str: Optional NULL terminated string to search for |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 114 | * @symdepth: Maximum depth of symlinks to follow. A negative value means |
| 115 | * follow indefinitely. Zero means do not follow symlinks. |
| 116 | * |
| 117 | * The caller should be specific enough with root and symdepth arguments |
| 118 | * to avoid finding duplicate information (especially in sysfs). |
| 119 | * |
Brian Norris | 0938894 | 2016-03-28 12:57:01 -0700 | [diff] [blame] | 120 | * Also, note that we may only scan a bounded portion of the beginning of the |
| 121 | * file for a match. |
| 122 | * |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 123 | * returns allocated string with path of matching file if successful |
| 124 | * returns NULL to indicate failure |
| 125 | */ |
| 126 | const char *scanft(const char *root, const char *filename, |
| 127 | const char *str, int symdepth) |
| 128 | { |
| 129 | DIR *dp; |
| 130 | struct dirent *d; |
| 131 | struct stat s; |
| 132 | const char *ret = NULL; |
| 133 | |
| 134 | if (lstat(root, &s) < 0) { |
| 135 | msg_pdbg("%s: Error stat'ing %s: %s\n", |
| 136 | __func__, root, strerror(errno)); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | if (S_ISLNK(s.st_mode)) { |
| 141 | if (symdepth == 0) /* Leaf has been reached */ |
| 142 | return NULL; |
| 143 | else if (symdepth > 0) /* Follow if not too deep in */ |
| 144 | symdepth--; |
David Hendricks | 0cc34e7 | 2013-11-12 15:06:42 -0800 | [diff] [blame] | 145 | } else if (!S_ISDIR(s.st_mode)) { |
| 146 | return NULL; |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if ((dp = opendir(root)) == NULL) |
| 150 | return NULL; |
| 151 | |
| 152 | while (!ret && (d = readdir(dp))) { |
| 153 | char newpath[PATH_MAX]; |
| 154 | |
| 155 | /* Skip "." and ".." */ |
| 156 | if (!(strcmp(d->d_name, ".")) || |
| 157 | !(strcmp(d->d_name, ".."))) |
| 158 | continue; |
| 159 | |
| 160 | snprintf(newpath, sizeof(newpath), "%s/%s", root, d->d_name); |
| 161 | |
| 162 | if (!strcmp(d->d_name, filename)) { |
David Hendricks | 1d2f41b | 2015-11-03 15:48:20 -0800 | [diff] [blame] | 163 | if (!str || (find_string(newpath, str) == 1)) |
David Hendricks | 9e9fca2 | 2012-05-22 20:55:29 -0700 | [diff] [blame] | 164 | ret = strdup(newpath); |
| 165 | } |
| 166 | |
| 167 | if (!ret) |
| 168 | ret = scanft(newpath, filename, str, symdepth); |
| 169 | } |
| 170 | |
| 171 | closedir(dp); |
| 172 | return ret; |
| 173 | } |