1
0
mirror of https://github.com/systemd/systemd synced 2025-10-07 12:44:45 +02:00

Compare commits

..

No commits in common. "e2e40e9a9e4074eaca7984b70a5d1cd7a7f0cbe0" and "d6bf675f0bd047a1f02d9079444efaed1cc3ef8b" have entirely different histories.

7 changed files with 132 additions and 83 deletions

View File

@ -33,7 +33,7 @@ static inline int negative_errno(void) {
static inline const char *strerror_safe(int error) { static inline const char *strerror_safe(int error) {
/* 'safe' here does NOT mean thread safety. */ /* 'safe' here does NOT mean thread safety. */
return strerror(abs(error)); /* lgtm [cpp/potentially-dangerous-function] */ return strerror(abs(error));
} }
static inline int errno_or_else(int fallback) { static inline int errno_or_else(int fallback) {

View File

@ -51,7 +51,7 @@ bool in4_addr_is_link_local(const struct in_addr *a) {
bool in6_addr_is_link_local(const struct in6_addr *a) { bool in6_addr_is_link_local(const struct in6_addr *a) {
assert(a); assert(a);
return IN6_IS_ADDR_LINKLOCAL(a); /* lgtm [cpp/potentially-dangerous-function] */ return IN6_IS_ADDR_LINKLOCAL(a);
} }
int in_addr_is_link_local(int family, const union in_addr_union *u) { int in_addr_is_link_local(int family, const union in_addr_union *u) {
@ -116,7 +116,7 @@ int in_addr_is_localhost(int family, const union in_addr_union *u) {
return in4_addr_is_localhost(&u->in); return in4_addr_is_localhost(&u->in);
if (family == AF_INET6) if (family == AF_INET6)
return IN6_IS_ADDR_LOOPBACK(&u->in6); /* lgtm [cpp/potentially-dangerous-function] */ return IN6_IS_ADDR_LOOPBACK(&u->in6);
return -EAFNOSUPPORT; return -EAFNOSUPPORT;
} }

View File

@ -105,7 +105,6 @@ static int open_parent_block_device(dev_t devnum, int *ret_fd) {
} }
static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) { static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) {
#if HAVE_LIBCRYPTSETUP
_cleanup_free_ char *e = NULL, *n = NULL, *d = NULL; _cleanup_free_ char *e = NULL, *n = NULL, *d = NULL;
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
int r; int r;
@ -183,9 +182,6 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
} }
return 0; return 0;
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Partition is encrypted, but the project was compiled without libcryptsetup support");
#endif
} }
static int add_mount( static int add_mount(

View File

@ -1893,11 +1893,10 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
return r; return r;
path = prefix_roota(syspath, sysattr); path = prefix_roota(syspath, sysattr);
if (lstat(path, &statbuf) < 0) { r = lstat(path, &statbuf);
if (r < 0) {
int k; int k;
r = -errno;
/* remember that we could not access the sysattr */ /* remember that we could not access the sysattr */
k = device_cache_sysattr_value(device, sysattr, NULL); k = device_cache_sysattr_value(device, sysattr, NULL);
if (k < 0) if (k < 0)

View File

@ -428,20 +428,6 @@ static void test_extract_first_word(void) {
assert_se(streq(t, "c")); assert_se(streq(t, "c"));
free(t); free(t);
assert_se(p == NULL); assert_se(p == NULL);
p = original = "foobar=\"waldo\"maldo, baldo";
assert_se(extract_first_word(&p, &t, "=\", ", 0) > 0);
assert_se(streq(t, "foobar"));
free(t);
assert_se(extract_first_word(&p, &t, "=\", ", 0) > 0);
assert_se(streq(t, "waldo"));
free(t);
assert_se(extract_first_word(&p, &t, "=\", ", 0) > 0);
assert_se(streq(t, "maldo"));
free(t);
assert_se(extract_first_word(&p, &t, "=\", ", 0) > 0);
assert_se(streq(t, "baldo"));
free(t);
} }
static void test_extract_first_word_and_warn(void) { static void test_extract_first_word_and_warn(void) {

View File

@ -801,7 +801,6 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error
static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *error) { static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *error) {
sd_bus *bus = sd_bus_message_get_bus(m); sd_bus *bus = sd_bus_message_get_bus(m);
char buf[FORMAT_TIMESTAMP_MAX];
int relative, interactive, r; int relative, interactive, r;
Context *c = userdata; Context *c = userdata;
int64_t utc; int64_t utc;
@ -887,7 +886,7 @@ static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *erro
log_struct(LOG_INFO, log_struct(LOG_INFO,
"MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR, "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR,
"REALTIME="USEC_FMT, timespec_load(&ts), "REALTIME="USEC_FMT, timespec_load(&ts),
LOG_MESSAGE("Changed local time to %s", strnull(format_timestamp(buf, sizeof(buf), timespec_load(&ts))))); LOG_MESSAGE("Changed local time to %s", ctime(&ts.tv_sec)));
return sd_bus_reply_method_return(m, NULL); return sd_bus_reply_method_return(m, NULL);
} }

View File

@ -19,11 +19,9 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "build.h" #include "build.h"
#include "device-nodes.h" #include "device-nodes.h"
#include "extract-word.h"
#include "fd-util.h" #include "fd-util.h"
#include "scsi_id.h" #include "scsi_id.h"
#include "string-util.h" #include "string-util.h"
#include "strv.h"
#include "strxcpyx.h" #include "strxcpyx.h"
#include "udev-util.h" #include "udev-util.h"
@ -92,6 +90,50 @@ static void set_type(const char *from, char *to, size_t len) {
strscpy(to, len, type); strscpy(to, len, type);
} }
/*
* get_value:
*
* buf points to an '=' followed by a quoted string ("foo") or a string ending
* with a space or ','.
*
* Return a pointer to the NUL terminated string, returns NULL if no
* matches.
*/
static char *get_value(char **buffer) {
static const char *quote_string = "\"\n";
static const char *comma_string = ",\n";
char *val;
const char *end;
if (**buffer == '"') {
/*
* skip leading quote, terminate when quote seen
*/
(*buffer)++;
end = quote_string;
} else
end = comma_string;
val = strsep(buffer, end);
if (val && end == quote_string)
/*
* skip trailing quote
*/
(*buffer)++;
while (isspace(**buffer))
(*buffer)++;
return val;
}
static int argc_count(char *opts) {
int i = 0;
while (*opts != '\0')
if (*opts++ == ' ')
i++;
return i;
}
/* /*
* get_file_options: * get_file_options:
* *
@ -103,12 +145,14 @@ static void set_type(const char *from, char *to, size_t len) {
*/ */
static int get_file_options(const char *vendor, const char *model, static int get_file_options(const char *vendor, const char *model,
int *argc, char ***newargv) { int *argc, char ***newargv) {
_cleanup_free_ char *buffer = NULL, _cleanup_free_ char *buffer = NULL;
*vendor_in = NULL, *model_in = NULL, *options_in = NULL; /* read in from file */
_cleanup_strv_free_ char **options_argv = NULL;
_cleanup_fclose_ FILE *f; _cleanup_fclose_ FILE *f;
const char *buf; char *buf;
int lineno, r; char *str1;
char *vendor_in, *model_in, *options_in; /* read in from file */
int lineno;
int c;
int retval = 0;
f = fopen(config_file, "re"); f = fopen(config_file, "re");
if (!f) { if (!f) {
@ -132,16 +176,16 @@ static int get_file_options(const char *vendor, const char *model,
*newargv = NULL; *newargv = NULL;
lineno = 0; lineno = 0;
for (;;) { for (;;) {
_cleanup_free_ char *key = NULL, *value = NULL;
vendor_in = model_in = options_in = NULL; vendor_in = model_in = options_in = NULL;
buf = fgets(buffer, MAX_BUFFER_LEN, f); buf = fgets(buffer, MAX_BUFFER_LEN, f);
if (!buf) if (!buf)
break; break;
lineno++; lineno++;
if (buf[strlen(buffer) - 1] != '\n') if (buf[strlen(buffer) - 1] != '\n') {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Config file line %d too long", lineno); log_error("Config file line %d too long", lineno);
break;
}
while (isspace(*buf)) while (isspace(*buf))
buf++; buf++;
@ -154,36 +198,44 @@ static int get_file_options(const char *vendor, const char *model,
if (*buf == '#') if (*buf == '#')
continue; continue;
r = extract_many_words(&buf, "=\",\n", 0, &key, &value, NULL); str1 = strsep(&buf, "=");
if (r < 2) if (str1 && strcaseeq(str1, "VENDOR")) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error parsing config file line %d '%s'", lineno, buffer); str1 = get_value(&buf);
if (!str1) {
retval = log_oom();
break;
}
vendor_in = str1;
if (strcaseeq(key, "VENDOR")) { str1 = strsep(&buf, "=");
vendor_in = TAKE_PTR(value); if (str1 && strcaseeq(str1, "MODEL")) {
str1 = get_value(&buf);
key = mfree(key); if (!str1) {
r = extract_many_words(&buf, "=\",\n", 0, &key, &value, NULL); retval = log_oom();
if (r < 2) break;
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error parsing config file line %d '%s'", lineno, buffer); }
model_in = str1;
if (strcaseeq(key, "MODEL")) { str1 = strsep(&buf, "=");
model_in = TAKE_PTR(value);
key = mfree(key);
r = extract_many_words(&buf, "=\",\n", 0, &key, &value, NULL);
if (r < 2)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error parsing config file line %d '%s'", lineno, buffer);
} }
} }
if (strcaseeq(key, "OPTIONS")) if (str1 && strcaseeq(str1, "OPTIONS")) {
options_in = TAKE_PTR(value); str1 = get_value(&buf);
if (!str1) {
retval = log_oom();
break;
}
options_in = str1;
}
/* /*
* Only allow: [vendor=foo[,model=bar]]options=stuff * Only allow: [vendor=foo[,model=bar]]options=stuff
*/ */
if (!options_in || (!vendor_in && model_in)) if (!options_in || (!vendor_in && model_in)) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error parsing config file line %d '%s'", lineno, buffer); log_error("Error parsing config file line %d '%s'", lineno, buffer);
retval = -1;
break;
}
if (!vendor) { if (!vendor) {
if (!vendor_in) if (!vendor_in)
break; break;
@ -199,30 +251,39 @@ static int get_file_options(const char *vendor, const char *model,
*/ */
break; break;
} }
vendor_in = mfree(vendor_in);
model_in = mfree(model_in);
options_in = mfree(options_in);
} }
if (vendor_in == NULL && model_in == NULL && options_in == NULL) if (retval == 0) {
return 1; /* No matches */ if (vendor_in != NULL || model_in != NULL ||
options_in != NULL) {
/* /*
* Something matched. Allocate newargv, and store * Something matched. Allocate newargv, and store
* values found in options_in. * values found in options_in.
*/ */
options_argv = strv_split(options_in, " \t"); strcpy(buffer, options_in);
if (!options_argv) c = argc_count(buffer) + 2;
return log_oom(); *newargv = calloc(c, sizeof(**newargv));
r = strv_prepend(&options_argv, ""); /* getopt skips over argv[0] */ if (!*newargv)
if (r < 0) retval = log_oom();
return r; else {
*newargv = TAKE_PTR(options_argv); *argc = c;
*argc = strv_length(*newargv); c = 0;
/*
return 0; * argv[0] at 0 is skipped by getopt, but
* store the buffer address there for
* later freeing
*/
(*newargv)[c] = buffer;
for (c = 1; c < *argc; c++)
(*newargv)[c] = strsep(&buffer, " \t");
buffer = NULL;
}
} else {
/* No matches */
retval = 1;
}
}
return retval;
} }
static void help(void) { static void help(void) {
@ -330,9 +391,9 @@ static int set_options(int argc, char **argv,
} }
static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *page_code) { static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *page_code) {
_cleanup_strv_free_ char **newargv = NULL;
int retval; int retval;
int newargc; int newargc;
char **newargv = NULL;
int option; int option;
*good_bad = all_good; *good_bad = all_good;
@ -375,6 +436,10 @@ static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *
} }
} }
if (newargv) {
free(newargv[0]);
free(newargv);
}
return retval; return retval;
} }
@ -478,10 +543,10 @@ out:
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
_cleanup_strv_free_ char **newargv = NULL;
int retval = 0; int retval = 0;
char maj_min_dev[MAX_PATH_LEN]; char maj_min_dev[MAX_PATH_LEN];
int newargc; int newargc;
char **newargv = NULL;
log_set_target(LOG_TARGET_AUTO); log_set_target(LOG_TARGET_AUTO);
udev_parse_config(); udev_parse_config();
@ -520,6 +585,10 @@ int main(int argc, char **argv) {
retval = scsi_id(maj_min_dev); retval = scsi_id(maj_min_dev);
exit: exit:
if (newargv) {
free(newargv[0]);
free(newargv);
}
log_close(); log_close();
return retval; return retval;
} }