Merge pull request #18651 from poettering/einval-followup

two follow-up fixes for the enum einvalification
This commit is contained in:
Lennart Poettering
2021-02-17 23:15:50 +01:00
committed by GitHub
11 changed files with 126 additions and 102 deletions
+1 -1
View File
@@ -2388,7 +2388,7 @@ int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, Un
while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
/* We expect only "success" changes to be sent over the bus.
Hence, reject anything negative. */
UnitFileChangeType ch = unit_file_change_type_from_string(type);
int ch = unit_file_change_type_from_string(type);
if (ch < 0) {
log_notice_errno(ch, "Manager reported unknown change type \"%s\" for path \"%s\", ignoring.",
type, path);
+34 -22
View File
@@ -14,6 +14,7 @@
#include "conf-parser.h"
#include "def.h"
#include "dirent-util.h"
#include "errno-list.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
@@ -261,7 +262,7 @@ static const char* config_path_from_flags(const LookupPaths *paths, UnitFileFlag
int unit_file_changes_add(
UnitFileChange **changes,
size_t *n_changes,
int type,
int type_or_errno, /* UNIT_FILE_SYMLINK, _UNLINK, _IS_MASKED, _IS_DANGLING if positive or errno if negative */
const char *path,
const char *source) {
@@ -271,6 +272,11 @@ int unit_file_changes_add(
assert(path);
assert(!changes == !n_changes);
if (type_or_errno >= 0)
assert(type_or_errno < _UNIT_FILE_CHANGE_TYPE_MAX);
else
assert(type_or_errno >= -ERRNO_MAX);
if (!changes)
return 0;
@@ -280,19 +286,25 @@ int unit_file_changes_add(
*changes = c;
p = strdup(path);
if (source)
s = strdup(source);
if (!p || (source && !s))
if (!p)
return -ENOMEM;
path_simplify(p, false);
if (s)
path_simplify(s, false);
c[*n_changes] = (UnitFileChange) { type, p, s };
p = s = NULL;
(*n_changes) ++;
if (source) {
s = strdup(source);
if (!s)
return -ENOMEM;
path_simplify(s, false);
}
c[(*n_changes)++] = (UnitFileChange) {
.type_or_errno = type_or_errno,
.path = TAKE_PTR(p),
.source = TAKE_PTR(s),
};
return 0;
}
@@ -315,9 +327,9 @@ void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *chang
assert(verb || r >= 0);
for (size_t i = 0; i < n_changes; i++) {
assert(verb || changes[i].type >= 0);
assert(verb || changes[i].type_or_errno >= 0);
switch(changes[i].type) {
switch(changes[i].type_or_errno) {
case UNIT_FILE_SYMLINK:
if (!quiet)
log_info("Created symlink %s %s %s.",
@@ -340,45 +352,45 @@ void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *chang
break;
case -EEXIST:
if (changes[i].source)
log_error_errno(changes[i].type,
log_error_errno(changes[i].type_or_errno,
"Failed to %s unit, file %s already exists and is a symlink to %s.",
verb, changes[i].path, changes[i].source);
else
log_error_errno(changes[i].type,
log_error_errno(changes[i].type_or_errno,
"Failed to %s unit, file %s already exists.",
verb, changes[i].path);
logged = true;
break;
case -ERFKILL:
log_error_errno(changes[i].type, "Failed to %s unit, unit %s is masked.",
log_error_errno(changes[i].type_or_errno, "Failed to %s unit, unit %s is masked.",
verb, changes[i].path);
logged = true;
break;
case -EADDRNOTAVAIL:
log_error_errno(changes[i].type, "Failed to %s unit, unit %s is transient or generated.",
log_error_errno(changes[i].type_or_errno, "Failed to %s unit, unit %s is transient or generated.",
verb, changes[i].path);
logged = true;
break;
case -EUCLEAN:
log_error_errno(changes[i].type,
log_error_errno(changes[i].type_or_errno,
"Failed to %s unit, \"%s\" is not a valid unit name.",
verb, changes[i].path);
logged = true;
break;
case -ELOOP:
log_error_errno(changes[i].type, "Failed to %s unit, refusing to operate on linked unit file %s",
log_error_errno(changes[i].type_or_errno, "Failed to %s unit, refusing to operate on linked unit file %s",
verb, changes[i].path);
logged = true;
break;
case -ENOENT:
log_error_errno(changes[i].type, "Failed to %s unit, unit %s does not exist.", verb, changes[i].path);
log_error_errno(changes[i].type_or_errno, "Failed to %s unit, unit %s does not exist.", verb, changes[i].path);
logged = true;
break;
default:
assert(changes[i].type < 0);
log_error_errno(changes[i].type, "Failed to %s unit, file %s: %m.",
assert(changes[i].type_or_errno < 0);
log_error_errno(changes[i].type_or_errno, "Failed to %s unit, file %s: %m.",
verb, changes[i].path);
logged = true;
}
@@ -3464,7 +3476,7 @@ static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX]
[UNIT_FILE_IS_DANGLING] = "dangling",
};
DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, int);
static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MAX] = {
[UNIT_FILE_PRESET_FULL] = "full",
+11 -11
View File
@@ -25,7 +25,9 @@ enum UnitFilePresetMode {
_UNIT_FILE_PRESET_INVALID = -EINVAL,
};
enum UnitFileChangeType {
/* This enum type is anonymous, since we usually store it in an 'int', as we overload it with negative errno
* values. */
enum {
UNIT_FILE_SYMLINK,
UNIT_FILE_UNLINK,
UNIT_FILE_IS_MASKED,
@@ -42,20 +44,18 @@ enum UnitFileFlags {
_UNIT_FILE_FLAGS_MASK_PUBLIC = UNIT_FILE_RUNTIME|UNIT_FILE_PORTABLE|UNIT_FILE_FORCE,
};
/* type can either one of the UnitFileChangeTypes listed above, or a negative error.
* If source is specified, it should be the contents of the path symlink.
* In case of an error, source should be the existing symlink contents or NULL
*/
/* type can either one of the UNIT_FILE_SYMLINK, UNIT_FILE_UNLINK, … listed above, or a negative errno value.
* If source is specified, it should be the contents of the path symlink. In case of an error, source should
* be the existing symlink contents or NULL. */
struct UnitFileChange {
int type; /* UnitFileChangeType or bust */
int type_or_errno; /* UNIT_FILE_SYMLINK, … if positive, errno if negative */
char *path;
char *source;
};
static inline bool unit_file_changes_have_modification(const UnitFileChange* changes, size_t n_changes) {
size_t i;
for (i = 0; i < n_changes; i++)
if (IN_SET(changes[i].type, UNIT_FILE_SYMLINK, UNIT_FILE_UNLINK))
for (size_t i = 0; i < n_changes; i++)
if (IN_SET(changes[i].type_or_errno, UNIT_FILE_SYMLINK, UNIT_FILE_UNLINK))
return true;
return false;
}
@@ -206,8 +206,8 @@ const char *unit_file_state_to_string(UnitFileState s) _const_;
UnitFileState unit_file_state_from_string(const char *s) _pure_;
/* from_string conversion is unreliable because of the overlap between -EPERM and -1 for error. */
const char *unit_file_change_type_to_string(UnitFileChangeType s) _const_;
UnitFileChangeType unit_file_change_type_from_string(const char *s) _pure_;
const char *unit_file_change_type_to_string(int s) _const_;
int unit_file_change_type_from_string(const char *s) _pure_;
const char *unit_file_preset_mode_to_string(UnitFilePresetMode m) _const_;
UnitFilePresetMode unit_file_preset_mode_from_string(const char *s) _pure_;