* Cycles through hugepage directories and looks for hugepage * files associated with a given prefix. Depending on value of * action, the hugepages are checked if they exist, checked if * they can be locked, or are simply deleted. * * Returns 1 if it finds at least one hugepage matching the action * Returns 0 if no matching hugepages were found * Returns -1 if it encounters an error */
| 163 | * Returns -1 if it encounters an error |
| 164 | */ |
| 165 | static int |
| 166 | process_hugefiles(const char * prefix, enum hugepage_action action) |
| 167 | { |
| 168 | FILE * hugedir_handle = NULL; |
| 169 | DIR * hugepage_dir = NULL; |
| 170 | struct dirent *dirent = NULL; |
| 171 | |
| 172 | char hugefile_prefix[PATH_MAX] = {0}; |
| 173 | char hugedir[PATH_MAX] = {0}; |
| 174 | char line[PATH_MAX] = {0}; |
| 175 | |
| 176 | int fd, lck_result, result = 0; |
| 177 | |
| 178 | const int prefix_len = snprintf(hugefile_prefix, |
| 179 | sizeof(hugefile_prefix), "%smap_", prefix); |
| 180 | if (prefix_len <= 0 || prefix_len >= (int)sizeof(hugefile_prefix) |
| 181 | || prefix_len >= (int)sizeof(dirent->d_name)) { |
| 182 | printf("Error creating hugefile filename prefix\n"); |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | /* get hugetlbfs mountpoints from /proc/mounts */ |
| 187 | hugedir_handle = fopen("/proc/mounts", "r"); |
| 188 | |
| 189 | if (hugedir_handle == NULL) { |
| 190 | printf("Error parsing /proc/mounts!\n"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | /* read and parse script output */ |
| 195 | while (fgets(line, sizeof(line), hugedir_handle) != NULL) { |
| 196 | |
| 197 | /* check if we have a hugepage filesystem path */ |
| 198 | if (!get_hugepage_path(line, sizeof(line), hugedir, sizeof(hugedir))) |
| 199 | continue; |
| 200 | |
| 201 | /* check if directory exists */ |
| 202 | if ((hugepage_dir = opendir(hugedir)) == NULL) { |
| 203 | fclose(hugedir_handle); |
| 204 | printf("Error reading %s: %s\n", hugedir, strerror(errno)); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | while ((dirent = readdir(hugepage_dir)) != NULL) { |
| 209 | if (memcmp(dirent->d_name, hugefile_prefix, prefix_len) != 0) |
| 210 | continue; |
| 211 | |
| 212 | switch (action) { |
| 213 | case HUGEPAGE_CHECK_EXISTS: |
| 214 | { |
| 215 | /* file exists, return */ |
| 216 | closedir(hugepage_dir); |
| 217 | result = 1; |
| 218 | goto end; |
| 219 | } |
| 220 | break; |
| 221 | case HUGEPAGE_DELETE: |
| 222 | { |
no test coverage detected