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
|
/*
* util - Utility function library
*
* UDEV helper functions
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib/util_exit_code.h"
#include "lib/util_file.h"
#include "lib/util_libc.h"
#include "lib/util_list.h"
#include "lib/util_path.h"
#include "lib/util_udev.h"
/* Create a newly allocated udev entry. */
static struct util_udev_entry_node *util_udev_entry_node_new(const char *key,
const char *op,
const char *value)
{
struct util_udev_entry_node *entry;
entry = util_zalloc(sizeof(struct util_udev_entry_node));
entry->key = util_strdup(key);
entry->op = util_strdup(op);
entry->value = util_strdup(value);
return entry;
}
/* Release resources associated with udev entry. */
static void util_udev_entry_node_free(struct util_udev_entry_node *entry)
{
if (!entry)
return;
free(entry->key);
free(entry->op);
free(entry->value);
free(entry);
}
/* Create a newly allocated udev line. */
static struct util_udev_line_node *util_udev_line_node_new(void)
{
struct util_udev_line_node *line;
line = util_zalloc(sizeof(struct util_udev_line_node));
util_list_init(&line->entries, struct util_udev_entry_node, node);
return line;
}
/* Release resources associated with udev line. */
static void util_udev_line_node_free(struct util_udev_line_node *line)
{
struct util_udev_entry_node *e, *n;
if (!line)
return;
util_list_iterate_safe(&line->entries, e, n) {
util_list_remove(&line->entries, e);
util_udev_entry_node_free(e);
}
free(line->line);
free(line);
}
/* Create a newly allocated udev file. */
static struct util_udev_file *util_udev_file_new(void)
{
struct util_udev_file *file;
file = util_zalloc(sizeof(struct util_udev_file));
util_list_init(&file->lines, struct util_udev_line_node, node);
return file;
}
/**
* Release resources associated with udev file.
*
* @param[in, out] file Udev file structure to be freed
*/
void util_udev_free_file(struct util_udev_file *file)
{
struct util_udev_line_node *l, *n;
if (!file)
return;
util_list_iterate_safe(&file->lines, l, n) {
util_list_remove(&file->lines, l);
util_udev_line_node_free(l);
}
free(file);
}
/**
* Print the contents of a udev file to stdout. Used for debugging.
*
* @param[in] file Udev file structure to print
*/
void util_udev_file_print(struct util_udev_file *file)
{
struct util_udev_line_node *l;
struct util_udev_entry_node *e;
printf("util_udev_file at %p\n", (void *) file);
if (!file)
return;
util_list_iterate(&file->lines, l) {
printf(" util_udev_line_node at %p\n", (void *) l);
printf(" line='%s'\n", l->line);
util_list_iterate(&l->entries, e) {
printf(" util_udev_entry at %p\n", (void *) e);
printf(" '%s' '%s' '%s'\n", e->key, e->op,
e->value);
}
}
}
static void skip_whitespace(const char **s_ptr)
{
const char *s = *s_ptr;
while (*s && isspace(*s))
s++;
*s_ptr = s;
}
static char *parse_key(const char **s_ptr)
{
const char *s, *e;
char *key;
s = *s_ptr;
/* Parse \w+(\{[^\}]*\})? */
e = s;
while (*e && (isalnum(*e) || *e == '_'))
e++;
if (*e == '{') {
while (*e && *e != '}')
e++;
if (*e == '}')
e++;
}
if (e == s)
return NULL;
/* s points to key start, e to character after key end. */
key = util_zalloc(e - s + 1);
memcpy(key, s, e - s);
*s_ptr = e;
return key;
}
static char *parse_op(const char **s_ptr)
{
const char *ops[] = { "==", "!=", "=", "+=", ":=", NULL };
const char *entry;
size_t len;
int i;
entry = *s_ptr;
for (i = 0; ops[i]; i++) {
len = strlen(ops[i]);
if (strncmp(entry, ops[i], len) == 0) {
*s_ptr += len;
return util_strdup(ops[i]);
}
}
return NULL;
}
static char *parse_value(const char **s_ptr)
{
const char *s, *e;
char *value;
/* Parse: ^\s*(.*)\s*$ */
s = *s_ptr;
skip_whitespace(&s);
e = s;
while (*e)
e++;
e--;
while (e > s && isspace(*e))
e--;
e++;
*s_ptr = e;
/* Remove quotes. */
if ((*s == '"' && *(e - 1) == '"') ||
(*s == '\'' && *(e - 1) == '\'')) {
s++;
e--;
}
/* s points to value start, e to character after value end. */
value = util_zalloc(e - s + 1);
memcpy(value, s, e - s);
return value;
}
static bool parse_util_udev_entry(struct util_udev_line_node *line,
const char *entry)
{
char *key = NULL, *op = NULL, *value = NULL;
struct util_udev_entry_node *e;
bool rc = false;
/* Parse: ^\s*(\w+)\s*(==|!=|=|\+=|:=)\s*"?([^"]*)"\s*$ */
/* Parse key. */
skip_whitespace(&entry);
key = parse_key(&entry);
if (!key)
goto out;
/* Parse operator. */
skip_whitespace(&entry);
op = parse_op(&entry);
if (!op)
goto out;
/* Parse value. */
skip_whitespace(&entry);
value = parse_value(&entry);
if (!value)
goto out;
skip_whitespace(&entry);
/* Check for unrecognized characters at end of entry. */
if (*entry != 0)
goto out;
/* Add entry to list. */
e = util_udev_entry_node_new(key, op, value);
util_list_add_tail(&line->entries, e);
rc = true;
out:
free(key);
free(op);
free(value);
return rc;
}
static void replace_unquoted(char *s, char from, char to)
{
char quoted = 0;
for (; *s; s++) {
if (quoted) {
/* Skip until quote end is found. */
if (*s == quoted)
quoted = 0;
continue;
}
if (*s == '"' || *s == '\'') {
quoted = *s;
continue;
}
if (*s == from)
*s = to;
}
}
static bool parse_util_udev_line(struct util_udev_file *file, const char *line)
{
char *copy, *curr, *next;
struct util_udev_line_node *l;
int i;
bool result = true;
l = util_udev_line_node_new();
l->line = util_strdup(line);
/* Check for empty lines and comment lines. */
for (i = 0; line[i] && isspace(line[i]); i++);
if (line[i] == 0 || line[i] == '#')
goto ok;
/* Parse each comma-separated entry. */
copy = util_strdup(line);
/* A hack to differentiate between quoted and unquoted commas. */
replace_unquoted(copy, ',', 1);
next = copy;
while ((curr = strsep(&next, "\1"))) {
if (!parse_util_udev_entry(l, curr)) {
result = false;
break;
}
}
free(copy);
ok:
if (result)
util_list_add_tail(&file->lines, l);
else
util_udev_line_node_free(l);
return result;
}
/**
* Create a new util_udev_file structure and read the contents of a specified
* udev file into that structure.
*
* @param[in] path Path to the udev file that will be read in
* @param[in, out] file_ptr A buffer to store resulting udev file structure
*
* @retval 0 Udev file read successfully
* @retval UTIL_EXIT_RUNTIME_ERROR Error reading the udev file
*/
util_exit_code_t util_udev_read_file(const char *path,
struct util_udev_file **file_ptr)
{
char *text, *curr, *next;
struct util_udev_file *file;
int
text = util_file_read_text_file(path, 0);
if (!text)
return UTIL_EXIT_RUNTIME_ERROR;
file = util_udev_file_new();
/* Iterate over each line. */
next = text;
while ((curr = strsep(&next, "\n"))) {
if (parse_util_udev_line(file, curr))
continue;
if (!once) {
fprintf(stderr, "Unrecognized udev rule in %s:\n",
path);
}
fprintf(stderr, "%s\n", curr);
}
free(text);
*file_ptr = file;
return UTIL_EXIT_OK;
}
|