networkd: add support for bridge fdb destination address.
@@
constant s;
@@
+(
+#define STRLEN
+&
- sizeof(s)-1
+ STRLEN(s)
+)
@@
constant s;
@@
@@
@@
+(
+#define DEBUG_LOGGING
+&
- _unlikely_(log_get_max_level() >= LOG_DEBUG)
+ DEBUG_LOGGING
+)
@@
@@
+(
+#define DEBUG_LOGGING
+&
- log_get_max_level() >= LOG_DEBUG
+ DEBUG_LOGGING
+)
@@
+/* We want to stick with dup() in test-fd-util.c */
+position p : script:python() { p[0].file != "src/test/test-fd-util.c" };
expression fd;
@@
-- dup(fd)
+- dup@p(fd)
+ fcntl(fd, F_DUPFD, 3)
@@
+/* Avoid running this transformation on the empty_to_null function itself */
+position p : script:python() { p[0].current_element != "empty_to_null" };
expression s;
@@
-- isempty(s) ? NULL : s
+
+- isempty@p(s) ? NULL : s
+ empty_to_null(s)
@@
+/* Disable this transformation for the securebits-util.h, as it makes
+ * the expression there confusing. */
+position p : script:python() { p[0].file != "src/shared/securebits-util.h" };
expression x, y;
@@
-- ((x) & (y)) == (y)
+(
+- ((x@p) & (y)) == (y)
+ FLAGS_SET(x, y)
-@@
-expression x, y;
-@@
-- (x & (y)) == (y)
+|
+- (x@p & (y)) == (y)
+ FLAGS_SET(x, y)
-@@
-expression x, y;
-@@
-- ((x) & y) == y
+|
+- ((x@p) & y) == y
+ FLAGS_SET(x, y)
+)
@@
expression e;
-constant n0, n1, n2, n3, n4, n5, n6, n7, n8, n9;
-@@
+/* Exclude JsonVariant * from the transformation, as it can't work with the
+ * current version of the IN_SET macro */
+typedef JsonVariant;
+type T != JsonVariant*;
+constant T n0, n1, n2, n3, n4, n5, n6, n7, n8, n9;
+@@
+
+(
- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6 || e == n7 || e == n8 || e == n9
+ IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5, n6, n7, n8;
-@@
+|
- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6 || e == n7 || e == n8
+ IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5, n6, n7;
-@@
+|
- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6 || e == n7
+ IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5, n6;
-@@
+|
- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6
+ IN_SET(e, n0, n1, n2, n3, n4, n5, n6)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5;
-@@
+|
- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5
+ IN_SET(e, n0, n1, n2, n3, n4, n5)
-@@
-expression e;
-constant n0, n1, n2, n3, n4;
-@@
+|
- e == n0 || e == n1 || e == n2 || e == n3 || e == n4
+ IN_SET(e, n0, n1, n2, n3, n4)
-@@
-expression e;
-constant n0, n1, n2, n3;
-@@
+|
- e == n0 || e == n1 || e == n2 || e == n3
+ IN_SET(e, n0, n1, n2, n3)
-@@
-expression e;
-constant n0, n1, n2;
-@@
+|
- e == n0 || e == n1 || e == n2
+ IN_SET(e, n0, n1, n2)
-@@
-expression e;
-constant n0, n1;
-@@
+|
- e == n0 || e == n1
+ IN_SET(e, n0, n1)
+)
@@
+/* Disable this transformation for the test-string-util.c */
+position p : script:python() { p[0].file != "src/test/test-string-util.c" };
expression s;
@@
-- strv_length(s) == 0
+(
+- strv_length@p(s) == 0
+ strv_isempty(s)
-@@
-expression s;
-@@
-- strv_length(s) <= 0
+|
+- strv_length@p(s) <= 0
+ strv_isempty(s)
-@@
-expression s;
-@@
-- strv_length(s) > 0
+|
+- strv_length@p(s) > 0
+ !strv_isempty(s)
-@@
-expression s;
-@@
-- strv_length(s) != 0
+|
+- strv_length@p(s) != 0
+ !strv_isempty(s)
-@@
-expression s;
-@@
-- strlen(s) == 0
+|
+- strlen@p(s) == 0
+ isempty(s)
-@@
-expression s;
-@@
-- strlen(s) <= 0
+|
+- strlen@p(s) <= 0
+ isempty(s)
-@@
-expression s;
-@@
-- strlen(s) > 0
+|
+- strlen@p(s) > 0
+ !isempty(s)
-@@
-expression s;
-@@
-- strlen(s) != 0
+|
+- strlen@p(s) != 0
+ !isempty(s)
-@@
-expression s;
-@@
-- strlen_ptr(s) == 0
+|
+- strlen_ptr@p(s) == 0
+ isempty(s)
-@@
-expression s;
-@@
-- strlen_ptr(s) <= 0
+|
+- strlen_ptr@p(s) <= 0
+ isempty(s)
-@@
-expression s;
-@@
-- strlen_ptr(s) > 0
+|
+- strlen_ptr@p(s) > 0
+ !isempty(s)
-@@
-expression s;
-@@
-- strlen_ptr(s) != 0
+|
+- strlen_ptr@p(s) != 0
+ !isempty(s)
+)
@@
expression a, b;
@@
+(
+#define memzero
+&
- memset(a, 0, b)
+ memzero(a, b)
+)
@@
expression a, b;
@@
+(
+#define memzero
+&
- bzero(a, b)
+ memzero(a, b)
+)
@@
-expression p;
+/* Avoid running this transformation on the mfree function itself */
+position p : script:python() { p[0].current_element != "mfree" };
+expression e;
@@
-- free(p);
+- free@p(e);
- return NULL;
-+ return mfree(p);
++ return mfree(e);
@@
expression e;
-constant n0, n1, n2, n3, n4, n5, n6, n7, n8, n9;
+typedef JsonVariant;
+type T != JsonVariant*;
+constant T n0, n1, n2, n3, n4, n5, n6, n7, n8, n9;
@@
+(
- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6 && e != n7 && e != n8 && e != n9
+ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5, n6, n7, n8;
-@@
+|
- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6 && e != n7 && e != n8
+ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5, n6, n7;
-@@
+|
- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6 && e != n7
+ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5, n6;
-@@
+|
- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6
+ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6)
-@@
-expression e;
-constant n0, n1, n2, n3, n4, n5;
-@@
+|
- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5
+ !IN_SET(e, n0, n1, n2, n3, n4, n5)
-@@
-expression e;
-constant n0, n1, n2, n3, n4;
-@@
+|
- e != n0 && e != n1 && e != n2 && e != n3 && e != n4
+ !IN_SET(e, n0, n1, n2, n3, n4)
-@@
-expression e;
-constant n0, n1, n2, n3;
-@@
+|
- e != n0 && e != n1 && e != n2 && e != n3
+ !IN_SET(e, n0, n1, n2, n3)
-@@
-expression e;
-constant n0, n1, n2;
-@@
+|
- e != n0 && e != n1 && e != n2
+ !IN_SET(e, n0, n1, n2)
-@@
-expression e;
-constant n0, n1;
-@@
+|
- e != n0 && e != n1
+ !IN_SET(e, n0, n1)
+)
#!/bin/bash -e
+# Exclude following paths from the Coccinelle transformations
+EXCLUDED_PATHS=(
+ "src/boot/efi/*"
+ "src/shared/linux/*"
+ "src/basic/linux/*"
+ # Symlinked to test-bus-vtable-cc.cc, which causes issues with the IN_SET macro
+ "src/libsystemd/sd-bus/test-bus-vtable.c"
+)
+
top="$(git rev-parse --show-toplevel)"
-files="$(git ls-files ':/*.[ch]')"
+iso_defs="$top/coccinelle/systemd-definitions.iso"
args=
+# Create an array from files tracked by git...
+mapfile -t files < <(git ls-files ':/*.[ch]')
+# ...and filter everything that matches patterns from EXCLUDED_PATHS
+for excl in "${EXCLUDED_PATHS[@]}"; do
+ files=(${files[@]//$excl})
+done
+
case "$1" in
-i)
args="$args --in-place"
TMPFILE=`mktemp`
echo "+ spatch --sp-file $SCRIPT $args ..."
parallel --halt now,fail=1 --keep-order --noswap --max-args=20 \
- spatch --sp-file $SCRIPT $args ::: $files \
+ spatch --iso-file $iso_defs --sp-file $SCRIPT $args ::: "${files[@]}" \
2>"$TMPFILE" || cat "$TMPFILE"
echo -e "--x-- Processed $SCRIPT --x--\n"
done
@@
+/* Avoid running this transformation on the strempty function itself */
+position p : script:python() { p[0].current_element != "strempty" };
expression s;
@@
-- s ?: ""
+(
+- s@p ?: ""
+ strempty(s)
-@@
-expression s;
-@@
-- s ? s : ""
+|
+- s@p ? s : ""
+ strempty(s)
+)
+
@@
+position p : script:python() { p[0].current_element != "strempty" };
expression s;
@@
-- if (!s)
+- if (!s@p)
- s = "";
+ s = strempty(s);
+
@@
+position p : script:python() { p[0].current_element != "strnull" };
expression s;
@@
-- s ?: "(null)"
+(
+- s@p ?: "(null)"
+ strnull(s)
-@@
-expression s;
-@@
-- s ? s : "(null)"
+|
+- s@p ? s : "(null)"
+ strnull(s)
+)
+
@@
+position p : script:python() { p[0].current_element != "strnull" };
expression s;
@@
-- if (!s)
+- if (!s@p)
- s = "(null)";
+ s = strnull(s);
+
@@
+position p : script:python() { p[0].current_element != "strna" };
expression s;
@@
-- s ?: "n/a"
+(
+- s@p ?: "n/a"
+ strna(s)
-@@
-expression s;
-@@
-- s ? s : "n/a"
+|
+- s@p ? s : "n/a"
+ strna(s)
+)
+
@@
+position p : script:python() { p[0].current_element != "strna" };
expression s;
@@
-- if (!s)
+- if (!s@p)
- s = "n/a";
+ s = strna(s);
expression e;
expression list args;
@@
+(
+/* Ignore one specific case in src/shared/bootspec.c where we want to stick
+ * with the log_debug() + return pattern */
+log_debug("Found no default boot entry :(");
+|
- log_debug(args);
- return -e;
+ return log_debug_errno(SYNTHETIC_ERRNO(e), args);
+)
@@
expression e;
expression list args;
--- /dev/null
+/* Statement isomorphisms - replace explicit checks against NULL with a
+ * shorter variant, which relies on C's downgrade-to-bool feature.
+ * The expression metavariables should be declared as pointers, however,
+ * that doesn't work well with complex expressions like:
+ * if (UNIT(p)->default_dependencies != NULL)
+ */
+
+Statement
+@@
+expression X;
+statement S;
+@@
+if (X == NULL) S => if (!X) S
+
+Statement
+@@
+expression X;
+statement S;
+@@
+if (X != NULL) S => if (X) S
- q = -1;
- return p;
+ return TAKE_FD(q);
+
+/* The ideal solution would use 'local idexpression' to avoid matching errno,
+ * which is a global variable. However, 'idexpression' nor 'identifier'
+ * would match, for example, "x->fd", which is considered 'expression' in
+ * the SmPL grammar
+ */
@@
-expression p, q;
+expression p != errno;
+expression q;
@@
- p = q;
- q = -1;
vtable,
&object));
- while (true) {
+ for (;;) {
check(sd_bus_wait(bus, UINT64_MAX));
check(sd_bus_process(bus, NULL));
}
'src/mount/mount-tool.c',
include_directories : includes,
link_with : [libshared],
+ dependencies: [libmount],
install_rpath : rootlibexecdir,
install : true)
public_programs += exe
/* SPDX-License-Identifier: LGPL-2.1+ */
-#include <malloc.h>
#include <stdint.h>
#include <string.h>
return NULL;
*p = q;
- *allocated = _unlikely_(size == 0) ? newalloc : malloc_usable_size(q) / size;
+ *allocated = newalloc;
return q;
}
assert(fd >= 0);
- /* This returns the first error we run into, but nevertheless
- * tries to go on. This closes the passed fd. */
+ /* This returns the first error we run into, but nevertheless tries to go on. This closes the passed
+ * fd, in all cases, including on failure.. */
if (!(flags & REMOVE_PHYSICAL)) {
is_dir = de->d_type == DT_DIR;
if (is_dir) {
- int subdir_fd;
+ _cleanup_close_ int subdir_fd = -1;
/* if root_dev is set, remove subdirectories only if device is same */
if (root_dev && st.st_dev != root_dev->st_dev)
if (ret == 0 && r != -ENOENT)
ret = r;
- safe_close(subdir_fd);
continue;
}
- if (r) {
- safe_close(subdir_fd);
+ if (r > 0)
continue;
- }
if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
if (ret == 0)
ret = r;
- safe_close(subdir_fd);
continue;
}
- /* ENOTTY, then it wasn't a
- * btrfs subvolume, continue
- * below. */
- } else {
+ /* ENOTTY, then it wasn't a btrfs subvolume, continue below. */
+ } else
/* It was a subvolume, continue. */
- safe_close(subdir_fd);
continue;
- }
}
- /* We pass REMOVE_PHYSICAL here, to avoid
- * doing the fstatfs() to check the file
+ /* We pass REMOVE_PHYSICAL here, to avoid doing the fstatfs() to check the file
* system type again for each directory */
- r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev);
+ r = rm_rf_children(TAKE_FD(subdir_fd), flags | REMOVE_PHYSICAL, root_dev);
if (r < 0 && ret == 0)
ret = r;
#include "macro.h"
static inline void umaskp(mode_t *u) {
- umask(*u);
+ umask(*u & 0777);
}
#define _cleanup_umask_ _cleanup_(umaskp)
-struct _umask_struct_ {
- mode_t mask;
- bool quit;
-};
+/* We make use of the fact here that the umask() concept is using only the lower 9 bits of mode_t, although
+ * mode_t has space for the file type in the bits further up. We simply OR in the file type mask S_IFMT to
+ * distinguish the first and the second iteration of the RUN_WITH_UMASK() loop, so that we can run the first
+ * one, and exit on the second. */
-static inline void _reset_umask_(struct _umask_struct_ *s) {
- umask(s->mask);
-};
+assert_cc((S_IFMT & 0777) == 0);
#define RUN_WITH_UMASK(mask) \
- for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
- !_saved_umask_.quit ; \
- _saved_umask_.quit = true)
+ for (_cleanup_umask_ mode_t _saved_umask_ = umask(mask) | S_IFMT; \
+ FLAGS_SET(_saved_umask_, S_IFMT); \
+ _saved_umask_ &= 0777)
if (streq(name, "SocketProtocol"))
return bus_set_transient_socket_protocol(u, name, &s->socket_protocol, message, flags, error);
- if ((ci = socket_exec_command_from_string(name)) >= 0)
- return bus_set_transient_exec_command(u, name, &s->exec_command[ci], message, flags, error);
+ ci = socket_exec_command_from_string(name);
+ if (ci >= 0)
+ return bus_set_transient_exec_command(u, name,
+ &s->exec_command[ci],
+ message, flags, error);
+
if (streq(name, "Symlinks")) {
_cleanup_strv_free_ char **l = NULL;
}
static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
- _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
- _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
+ _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+ _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
int r;
assert(m);
- t = mnt_new_table();
- i = mnt_new_iter(MNT_ITER_FORWARD);
- if (!t || !i)
- return log_oom();
-
- r = mnt_table_parse_mtab(t, NULL);
+ r = libmount_parse(NULL, NULL, &table, &iter);
if (r < 0)
return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
for (;;) {
struct libmnt_fs *fs;
const char *device, *path, *options, *fstype;
- int k;
- k = mnt_table_next_fs(t, i, &fs);
- if (k == 1)
+ r = mnt_table_next_fs(table, iter, &fs);
+ if (r == 1)
break;
- if (k < 0)
- return log_error_errno(k, "Failed to get next entry from /proc/self/mountinfo: %m");
+ if (r < 0)
+ return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
device = mnt_fs_get_source(fs);
path = mnt_fs_get_target(fs);
* (truncated) copy of what we want to send, and the second one
* contains the trailing dots. */
copy[0] = iovec[i];
- copy[1] = (struct iovec) {
- .iov_base = (char[]) { '.', '.', '.' },
- .iov_len = 3,
- };
+ copy[1] = IOVEC_MAKE(((char[]){'.', '.', '.'}), 3);
mh.msg_iov = copy;
mh.msg_iovlen = 2;
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
- fclose(f);
+ f = safe_fclose(f);
r = generator_open_unit_file(dest, "/etc/fstab", automount_name, &f);
if (r < 0)
[['src/fuzz/fuzz-nspawn-settings.c'],
[libshared,
libnspawn_core],
- []],
+ [libseccomp]],
[['src/fuzz/fuzz-nspawn-oci.c'],
[libshared,
libnspawn_core],
- []],
+ [libseccomp]],
[['src/fuzz/fuzz-calendarspec.c'],
[libshared],
n = strndup(t->log_message, e - t->log_message);
/* Skip over NUL and newlines */
- while (e < t->log_message + t->log_message_size && (*e == 0 || *e == '\n'))
+ while (e < t->log_message + t->log_message_size && IN_SET(*e, 0, '\n'))
e++;
memmove(t->log_message, e, t->log_message + sizeof(t->log_message) - e);
return log_warning_errno(r, "Failed to get writer for source %s: %m",
name);
- if (s->sources[fd] == NULL) {
+ if (!s->sources[fd]) {
s->sources[fd] = source_new(fd, false, name, writer);
if (!s->sources[fd]) {
writer_unref(writer);
assert(data);
hex_buf = hexmem(data, size);
- if (hex_buf == NULL)
+ if (!hex_buf)
return -ENOMEM;
fprintf(f, "%s=%s\n", key, hex_buf);
static void *adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) {
- if (p == NULL)
+ if (!p)
return NULL;
if (old_base == new_base)
return false;
if (m->handle_lid_switch != HANDLE_IGNORE)
return false;
- if (m->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
- m->handle_lid_switch_ep != HANDLE_IGNORE)
+ if (!IN_SET(m->handle_lid_switch_ep, _HANDLE_ACTION_INVALID, HANDLE_IGNORE))
return false;
if (m->handle_lid_switch_docked != HANDLE_IGNORE)
return false;
assert(!u->timer_event_source);
- if (u->manager->user_stop_delay == 0 || u->manager->user_stop_delay == USEC_INFINITY)
+ if (IN_SET(u->manager->user_stop_delay, 0, USEC_INFINITY))
return;
if (sd_event_get_state(u->manager->event) == SD_EVENT_FINISHED) {
#include "format-util.h"
#include "fs-util.h"
#include "fstab-util.h"
+#include "libmount-util.h"
#include "main-func.h"
#include "mount-util.h"
#include "mountpoint-util.h"
}
static int find_mount_points(const char *what, char ***list) {
- _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
+ _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+ _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
_cleanup_strv_free_ char **l = NULL;
size_t bufsize = 0, n = 0;
+ int r;
assert(what);
assert(list);
/* Returns all mount points obtained from /proc/self/mountinfo in *list,
* and the number of mount points as return value. */
- proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
- if (!proc_self_mountinfo)
- return log_error_errno(errno, "Can't open /proc/self/mountinfo: %m");
+ r = libmount_parse(NULL, NULL, &table, &iter);
+ if (r < 0)
+ return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
for (;;) {
- _cleanup_free_ char *path = NULL, *where = NULL, *dev = NULL;
- int r;
+ struct libmnt_fs *fs;
+ const char *source, *target;
- r = fscanf(proc_self_mountinfo,
- "%*s " /* (1) mount id */
- "%*s " /* (2) parent id */
- "%*s " /* (3) major:minor */
- "%*s " /* (4) root */
- "%ms " /* (5) mount point */
- "%*s" /* (6) mount options */
- "%*[^-]" /* (7) optional fields */
- "- " /* (8) separator */
- "%*s " /* (9) file system type */
- "%ms" /* (10) mount source */
- "%*s" /* (11) mount options 2 */
- "%*[^\n]", /* some rubbish at the end */
- &path, &dev);
- if (r != 2) {
- if (r == EOF)
- break;
-
- continue;
- }
+ r = mnt_table_next_fs(table, iter, &fs);
+ if (r == 1)
+ break;
+ if (r < 0)
+ return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
- if (!streq(what, dev))
+ source = mnt_fs_get_source(fs);
+ target = mnt_fs_get_target(fs);
+ if (!source || !target)
continue;
- r = cunescape(path, UNESCAPE_RELAX, &where);
- if (r < 0)
+ if (!path_equal(source, what))
continue;
/* one extra slot is needed for the terminating NULL */
- if (!GREEDY_REALLOC(l, bufsize, n + 2))
+ if (!GREEDY_REALLOC0(l, bufsize, n + 2))
return log_oom();
- l[n++] = TAKE_PTR(where);
+ l[n] = strdup(target);
+ if (!l[n])
+ return log_oom();
+ n++;
}
- if (!GREEDY_REALLOC(l, bufsize, n + 1))
+ if (!GREEDY_REALLOC0(l, bufsize, n + 1))
return log_oom();
- l[n] = NULL;
*list = TAKE_PTR(l);
-
return n;
}
strnull(assigned_buf),
strnull(buf), pd_prefix_len);
- if (assigned_link == NULL)
+ if (!assigned_link)
continue;
} else
if (link->network->router_dns) {
dns = newdup(struct in6_addr, link->network->router_dns,
link->network->n_router_dns);
- if (dns == NULL)
+ if (!dns)
return -ENOMEM;
n_dns = link->network->n_router_dns;
goto set_dns;
if (uplink) {
- if (uplink->network == NULL) {
+ if (!uplink->network) {
log_link_debug(uplink, "Cannot fetch DNS servers as uplink interface is not managed by us");
return 0;
}
goto set_domains;
if (uplink) {
- if (uplink->network == NULL) {
+ if (!uplink->network) {
log_link_debug(uplink, "Cannot fetch DNS search domains as uplink interface is not managed by us");
return 0;
}
[['src/nspawn/test-nspawn-tables.c'],
[libnspawn_core,
libshared],
- []],
+ [libseccomp]],
[['src/nspawn/test-patch-uid.c'],
[libnspawn_core,
d = dest;
for (;;) {
- if (*n == 0 || *n == '.') {
+ if (IN_SET(*n, 0, '.')) {
if (FLAGS_SET(flags, DNS_LABEL_LDH) && last_char == '-')
/* Trailing dash */
return -EINVAL;
if (path)
*path = TAKE_PTR(p);
if (active)
- *active = !!(header->attr & LOAD_OPTION_ACTIVE);
+ *active = header->attr & LOAD_OPTION_ACTIVE;
return 0;
}
c = memchr(imp->buf + start, '\n',
imp->filled - start);
- if (c != NULL)
+ if (c)
break;
}
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
+#include <stdio.h>
+
/* This needs to be after sys/mount.h */
#include <libmount.h>
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
+
+static inline int libmount_parse(
+ const char *path,
+ FILE *source,
+ struct libmnt_table **ret_table,
+ struct libmnt_iter **ret_iter) {
+
+ _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+ _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
+ int r;
+
+ /* Older libmount seems to require this. */
+ assert(!source || path);
+
+ table = mnt_new_table();
+ iter = mnt_new_iter(MNT_ITER_FORWARD);
+ if (!table || !iter)
+ return -ENOMEM;
+
+ /* If source or path are specified, we use on the functions which ignore utab.
+ * Only if both are empty, we use mnt_table_parse_mtab(). */
+
+ if (source)
+ r = mnt_table_parse_stream(table, source, path);
+ else if (path)
+ r = mnt_table_parse_file(table, path);
+ else
+ r = mnt_table_parse_mtab(table, NULL);
+ if (r < 0)
+ return r;
+
+ *ret_table = TAKE_PTR(table);
+ *ret_iter = TAKE_PTR(iter);
+ return 0;
+}
if (r < 0)
return r;
- if (size != 0 && size != UINT64_MAX)
+ if (!IN_SET(size, 0, UINT64_MAX))
(*ret)->usage = (*ret)->usage_exclusive = (*ret)->limit = (*ret)->limit_exclusive = size;
return 0;
#include <sys/statvfs.h>
#include <unistd.h>
-/* Include later */
-#include <libmount.h>
-
#include "alloc-util.h"
-#include "escape.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "hashmap.h"
+#include "libmount-util.h"
#include "mount-util.h"
#include "mountpoint-util.h"
#include "parse-util.h"
* unmounting them until they are gone. */
do {
- _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
+ _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+ _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
again = false;
- r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
+ r = libmount_parse("/proc/self/mountinfo", NULL, &table, &iter);
if (r < 0)
- return r;
+ return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
for (;;) {
- _cleanup_free_ char *path = NULL, *p = NULL;
- int k;
-
- k = fscanf(proc_self_mountinfo,
- "%*s " /* (1) mount id */
- "%*s " /* (2) parent id */
- "%*s " /* (3) major:minor */
- "%*s " /* (4) root */
- "%ms " /* (5) mount point */
- "%*s" /* (6) mount options */
- "%*[^-]" /* (7) optional fields */
- "- " /* (8) separator */
- "%*s " /* (9) file system type */
- "%*s" /* (10) mount source */
- "%*s" /* (11) mount options 2 */
- "%*[^\n]", /* some rubbish at the end */
- &path);
- if (k != 1) {
- if (k == EOF)
- break;
+ struct libmnt_fs *fs;
+ const char *path;
- continue;
- }
+ r = mnt_table_next_fs(table, iter, &fs);
+ if (r == 1)
+ break;
+ if (r < 0)
+ return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
- k = cunescape(path, UNESCAPE_RELAX, &p);
- if (k < 0)
- return k;
+ path = mnt_fs_get_target(fs);
+ if (!path)
+ continue;
- if (!path_startswith(p, prefix))
+ if (!path_startswith(path, prefix))
continue;
- if (umount2(p, flags) < 0) {
- r = log_debug_errno(errno, "Failed to umount %s: %m", p);
+ if (umount2(path, flags) < 0) {
+ r = log_debug_errno(errno, "Failed to umount %s: %m", path);
continue;
}
- log_debug("Successfully unmounted %s", p);
+ log_debug("Successfully unmounted %s", path);
again = true;
n++;
} while (again);
- return r < 0 ? r : n;
+ return n;
}
static int get_mount_flags(const char *path, unsigned long *flags) {
for (;;) {
_cleanup_set_free_free_ Set *todo = NULL;
+ _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+ _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
bool top_autofs = false;
char *x;
unsigned long orig_flags;
rewind(proc_self_mountinfo);
- for (;;) {
- _cleanup_free_ char *path = NULL, *p = NULL, *type = NULL;
- int k;
-
- k = fscanf(proc_self_mountinfo,
- "%*s " /* (1) mount id */
- "%*s " /* (2) parent id */
- "%*s " /* (3) major:minor */
- "%*s " /* (4) root */
- "%ms " /* (5) mount point */
- "%*s" /* (6) mount options (superblock) */
- "%*[^-]" /* (7) optional fields */
- "- " /* (8) separator */
- "%ms " /* (9) file system type */
- "%*s" /* (10) mount source */
- "%*s" /* (11) mount options (bind mount) */
- "%*[^\n]", /* some rubbish at the end */
- &path,
- &type);
- if (k != 2) {
- if (k == EOF)
- break;
+ r = libmount_parse("/proc/self/mountinfo", proc_self_mountinfo, &table, &iter);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
- continue;
- }
+ for (;;) {
+ struct libmnt_fs *fs;
+ const char *path, *type;
- r = cunescape(path, UNESCAPE_RELAX, &p);
+ r = mnt_table_next_fs(table, iter, &fs);
+ if (r == 1)
+ break;
if (r < 0)
- return r;
+ return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
- if (!path_startswith(p, cleaned))
+ path = mnt_fs_get_target(fs);
+ type = mnt_fs_get_fstype(fs);
+ if (!path || !type)
continue;
- /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount we shall
- * operate on. */
- if (!path_equal(cleaned, p)) {
+ if (!path_startswith(path, cleaned))
+ continue;
+
+ /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount
+ * we shall operate on. */
+ if (!path_equal(path, cleaned)) {
bool blacklisted = false;
char **i;
STRV_FOREACH(i, blacklist) {
-
if (path_equal(*i, cleaned))
continue;
if (!path_startswith(*i, cleaned))
continue;
- if (path_startswith(p, *i)) {
+ if (path_startswith(path, *i)) {
blacklisted = true;
- log_debug("Not remounting %s blacklisted by %s, called for %s", p, *i, cleaned);
+ log_debug("Not remounting %s blacklisted by %s, called for %s",
+ path, *i, cleaned);
break;
}
}
* already triggered, then we will find
* another entry for this. */
if (streq(type, "autofs")) {
- top_autofs = top_autofs || path_equal(cleaned, p);
+ top_autofs = top_autofs || path_equal(path, cleaned);
continue;
}
- if (!set_contains(done, p)) {
- r = set_consume(todo, p);
- p = NULL;
- if (r == -EEXIST)
- continue;
+ if (!set_contains(done, path)) {
+ r = set_put_strdup(todo, path);
if (r < 0)
return r;
}
if (fstat(old_root_fd, &rb) < 0)
log_warning_errno(errno, "Failed to stat old root directory, leaving: %m");
- else {
- (void) rm_rf_children(old_root_fd, 0, &rb);
- old_root_fd = -1;
- }
+ else
+ (void) rm_rf_children(TAKE_FD(old_root_fd), 0, &rb); /* takes possession of the dir fd, even on failure */
}
return 0;
}
int mount_points_list_get(const char *mountinfo, MountPoint **head) {
- _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
- _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
+ _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+ _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
int r;
assert(head);
- t = mnt_new_table();
- i = mnt_new_iter(MNT_ITER_FORWARD);
- if (!t || !i)
- return log_oom();
-
- r = mnt_table_parse_mtab(t, mountinfo);
+ r = libmount_parse(mountinfo, NULL, &table, &iter);
if (r < 0)
return log_error_errno(r, "Failed to parse %s: %m", mountinfo);
bool try_remount_ro;
_cleanup_free_ MountPoint *m = NULL;
- r = mnt_table_next_fs(t, i, &fs);
+ r = mnt_table_next_fs(table, iter, &fs);
if (r == 1)
break;
if (r < 0)
[],
[]],
+ [['src/test/test-umask-util.c'],
+ [],
+ []],
+
[['src/test/test-proc-cmdline.c'],
[],
[]],
_cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
_cleanup_fclose_ FILE *f = NULL;
- assert_se(table = mnt_new_table());
- assert_se(iter = mnt_new_iter(MNT_ITER_FORWARD));
-
f = fmemopen((char*) string, strlen(string), "re");
assert_se(f);
- assert_se(mnt_table_parse_stream(table, f, title) >= 0);
+ assert_se(libmount_parse(title, f, &table, &iter) >= 0);
struct libmnt_fs *fs;
const char *source, *target;
device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
udev_list_entry_get_name(list_entry));
- if (device != NULL) {
+ if (device) {
log_info("device: '%s' (%s)",
udev_device_get_syspath(device),
udev_device_get_subsystem(device));
log_info("enumerate '%s'", subsystem == NULL ? "<all>" : subsystem);
udev_enumerate = udev_enumerate_new(udev);
- if (udev_enumerate == NULL)
+ if (!udev_enumerate)
return -1;
udev_enumerate_add_match_subsystem(udev_enumerate, subsystem);
udev_enumerate_scan_devices(udev_enumerate);
log_info("enumerate 'net' + duplicated scan + null + zero");
udev_enumerate = udev_enumerate_new(udev);
- if (udev_enumerate == NULL)
+ if (!udev_enumerate)
return -1;
udev_enumerate_add_match_subsystem(udev_enumerate, "net");
udev_enumerate_scan_devices(udev_enumerate);
log_info("enumerate 'block'");
udev_enumerate = udev_enumerate_new(udev);
- if (udev_enumerate == NULL)
+ if (!udev_enumerate)
return -1;
udev_enumerate_add_match_subsystem(udev_enumerate,"block");
r = udev_enumerate_add_match_is_initialized(udev_enumerate);
log_info("enumerate 'not block'");
udev_enumerate = udev_enumerate_new(udev);
- if (udev_enumerate == NULL)
+ if (!udev_enumerate)
return -1;
udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
udev_enumerate_scan_devices(udev_enumerate);
log_info("enumerate 'pci, mem, vc'");
udev_enumerate = udev_enumerate_new(udev);
- if (udev_enumerate == NULL)
+ if (!udev_enumerate)
return -1;
udev_enumerate_add_match_subsystem(udev_enumerate, "pci");
udev_enumerate_add_match_subsystem(udev_enumerate, "mem");
log_info("enumerate 'subsystem'");
udev_enumerate = udev_enumerate_new(udev);
- if (udev_enumerate == NULL)
+ if (!udev_enumerate)
return -1;
udev_enumerate_scan_subsystems(udev_enumerate);
enumerate_print_list(udev_enumerate);
log_info("enumerate 'property IF_FS_*=filesystem'");
udev_enumerate = udev_enumerate_new(udev);
- if (udev_enumerate == NULL)
+ if (!udev_enumerate)
return -1;
udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem");
udev_enumerate_scan_devices(udev_enumerate);
if (it->scopeid == 0)
goto numerical_index;
- if (if_indextoname(it->scopeid, ifname) == NULL) {
+ if (!if_indextoname(it->scopeid, ifname)) {
log_warning_errno(errno, "if_indextoname(%d) failed: %m", it->scopeid);
numerical_index:
xsprintf(ifname, "%i", it->scopeid);
--- /dev/null
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include "tests.h"
+#include "umask-util.h"
+
+int main(int argc, char *argv[]) {
+ size_t n;
+ mode_t u;
+
+ test_setup_logging(LOG_DEBUG);
+
+ u = umask(0111);
+
+ n = 0;
+ RUN_WITH_UMASK(0123) {
+ assert_se(umask(000) == 0123);
+ n++;
+ }
+
+ assert_se(n == 1);
+ assert_se(umask(u) == 0111);
+
+ RUN_WITH_UMASK(0135) {
+ assert_se(umask(000) == 0135);
+ n++;
+ }
+
+ assert_se(n == 2);
+ assert_se(umask(0111) == u);
+
+ RUN_WITH_UMASK(0315) {
+ assert_se(umask(000) == 0315);
+ n++;
+ break;
+ }
+
+ assert_se(n == 3);
+ assert_se(umask(u) == 0111);
+
+ return EXIT_SUCCESS;
+}
sd_network_monitor_flush(m->network_monitor);
/* When manager_network_read_link_servers() failed, we assume that the servers are changed. */
- changed = !!manager_network_read_link_servers(m);
+ changed = manager_network_read_link_servers(m);
/* check if the machine is online */
online = network_is_online();
assert(i);
- if (i->argument == NULL)
+ if (!i->argument)
return 0;
switch (i->type) {
}
out:
- if (out_is_packet_device != NULL)
+ if (out_is_packet_device)
*out_is_packet_device = is_packet_device;
return ret;
}
}
node = argv[optind];
- if (node == NULL) {
+ if (!node) {
log_error("no node specified");
return 1;
}
return false;
fp = fopen("/proc/self/mountinfo", "re");
- if (fp == NULL)
+ if (!fp)
return false;
while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
if (makedev(maj, min) == statbuf.st_rdev) {
}
static void info_scsi_cmd_err(const char *cmd, int err) {
- if (err == -1) {
+ if (err == -1)
log_debug("%s failed", cmd);
- return;
- }
- log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
+ else
+ log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
}
struct scsi_cmd {
cmd->sg_io.dxferp = buf;
cmd->sg_io.dxfer_len = bufsize;
cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
- } else {
+ } else
cmd->sg_io.dxfer_direction = SG_DXFER_NONE;
- }
+
if (ioctl(fd, SG_IO, &cmd->sg_io))
return -1;
scsi_cmd_set(&sc, 4, 0x02);
scsi_cmd_set(&sc, 5, 0);
err = scsi_cmd_run(&sc, fd, NULL, 0);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("START_STOP_UNIT", err);
return -1;
}
int capability;
capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
- if (capability < 0) {
- log_debug("CDROM_GET_CAPABILITY failed");
- return -1;
- }
+ if (capability < 0)
+ return log_debug_errno(errno, "CDROM_GET_CAPABILITY failed");
if (capability & CDC_CD_R)
cd_cd_r = 1;
}
static int cd_media_compat(int fd) {
- if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
- log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK");
- return -1;
- }
+ if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK)
+ return log_debug_errno(errno, "CDROM_DRIVE_STATUS != CDS_DISC_OK");
+
cd_media = 1;
return 0;
}
scsi_cmd_set(&sc, 4, 36);
scsi_cmd_set(&sc, 5, 0);
err = scsi_cmd_run(&sc, fd, inq, 36);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("INQUIRY", err);
return -1;
}
- if ((inq[0] & 0x1F) != 5) {
- log_debug("not an MMC unit");
- return -1;
- }
+ if ((inq[0] & 0x1F) != 5)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "not an MMC unit");
log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32);
return 0;
scsi_cmd_set(&sc, 8, sizeof(header));
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DISC INFORMATION", err);
if (cd_media == 1) {
log_debug("no current profile, but disc is present; assuming CD-ROM");
cd_media_track_count = 1;
cd_media_track_count_data = 1;
return 0;
- } else {
- log_debug("no current profile, assuming no media");
- return -1;
- }
+ } else
+ return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
+ "no current profile, assuming no media");
};
cd_media = 1;
scsi_cmd_set(&sc, 8, 8);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, features, 8);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("GET CONFIGURATION", err);
/* handle pre-MMC2 drives which do not support GET CONFIGURATION */
if (SK(err) == 0x5 && IN_SET(ASC(err), 0x20, 0x24)) {
log_debug("current profile 0x%02x", cur_profile);
feature_profile_media(cur_profile);
ret = 0; /* we have media */
- } else {
+ } else
log_debug("no current profile, assuming no media");
- }
len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);
scsi_cmd_set(&sc, 8, len & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, features, len);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("GET CONFIGURATION", err);
return -1;
}
scsi_cmd_set(&sc, 8, sizeof(header) & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DISC INFORMATION", err);
return -1;
};
scsi_cmd_set(&sc, 9, sizeof(dvdstruct));
scsi_cmd_set(&sc, 11, 0);
err = scsi_cmd_run(&sc, fd, dvdstruct, sizeof(dvdstruct));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DVD STRUCTURE", err);
return -1;
}
scsi_cmd_set(&sc, 8, sizeof(format));
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, format, sizeof(format));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DVD FORMAT CAPACITIES", err);
return -1;
}
len = format[3];
- if (len & 7 || len < 16) {
- log_debug("invalid format capacities length");
- return -1;
- }
+ if (len & 7 || len < 16)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "invalid format capacities length");
switch(format[8] & 3) {
case 1:
case 3:
cd_media = 0; //return no media
- log_debug("format capacities returned no media");
- return -1;
+ return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
+ "format capacities returned no media");
}
}
scsi_cmd_set(&sc, 8, 32);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, buffer, sizeof(buffer));
- if ((err != 0)) {
+ if (err != 0) {
cd_media = 0;
info_scsi_cmd_err("READ FIRST 32 BLOCKS", err);
return -1;
scsi_cmd_set(&sc, 8, sizeof(header) & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ TOC", err);
return -1;
}
scsi_cmd_set(&sc, 8, len & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, toc, len);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ TOC (tracks)", err);
return -1;
}
scsi_cmd_set(&sc, 8, sizeof(header));
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ TOC (multi session)", err);
return -1;
}
if (cd_media_hddvd_rw)
printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
- if (cd_media_state != NULL)
+ if (cd_media_state)
printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
if (cd_media_session_next > 0)
printf("ID_CDROM_MEDIA_SESSION_NEXT=%u\n", cd_media_session_next);
int retval = 0;
f = fopen(config_file, "re");
- if (f == NULL) {
+ if (!f) {
if (errno == ENOENT)
return 1;
else {
vendor_in = model_in = options_in = NULL;
buf = fgets(buffer, MAX_BUFFER_LEN, f);
- if (buf == NULL)
+ if (!buf)
break;
lineno++;
if (buf[strlen(buffer) - 1] != '\n') {
break;
}
if (vendor == NULL) {
- if (vendor_in == NULL)
+ if (!vendor_in)
break;
} else if (vendor_in &&
startswith(vendor, vendor_in) &&
default_page_code = PAGE_83;
else if (streq(optarg, "pre-spc3-83"))
default_page_code = PAGE_83_PRE_SPC3;
- else {
- log_error("Unknown page code '%s'", optarg);
- return -1;
- }
+ else
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "Unknown page code '%s'",
+ optarg);
break;
case 's':
sg_version = atoi(optarg);
- if (sg_version < 3 || sg_version > 4) {
- log_error("Unknown SG version '%s'", optarg);
- return -1;
- }
+ if (sg_version < 3 || sg_version > 4)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "Unknown SG version '%s'",
+ optarg);
break;
case 'u':
* we'll retry the command.
*/
- if (sb_len < 1) {
- log_debug("%s: sense buffer empty", dev_scsi->kernel);
- return -1;
- }
+ if (sb_len < 1)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: sense buffer empty",
+ dev_scsi->kernel);
sense_class = (sense_buffer[0] >> 4) & 0x07;
code = sense_buffer[0] & 0xf;
* extended sense data.
*/
s = sense_buffer[7] + 8;
- if (sb_len < s) {
- log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
- dev_scsi->kernel, sb_len, s - sb_len);
- return -1;
- }
+ if (sb_len < s)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: sense buffer too small %d bytes, %d bytes too short",
+ dev_scsi->kernel, sb_len,
+ s - sb_len);
+
if (IN_SET(code, 0x0, 0x1)) {
sense_key = sense_buffer[2] & 0xf;
- if (s < 14) {
+ if (s < 14)
/*
* Possible?
*/
- log_debug("%s: sense result too" " small %d bytes",
- dev_scsi->kernel, s);
- return -1;
- }
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: sense result too small %d bytes",
+ dev_scsi->kernel, s);
+
asc = sense_buffer[12];
ascq = sense_buffer[13];
} else if (IN_SET(code, 0x2, 0x3)) {
sense_key = sense_buffer[1] & 0xf;
asc = sense_buffer[2];
ascq = sense_buffer[3];
- } else {
- log_debug("%s: invalid sense code 0x%x",
- dev_scsi->kernel, code);
- return -1;
- }
+ } else
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: invalid sense code 0x%x",
+ dev_scsi->kernel, code);
+
log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x",
dev_scsi->kernel, sense_key, asc, ascq);
} else {
- if (sb_len < 4) {
- log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
- dev_scsi->kernel, sb_len, 4 - sb_len);
- return -1;
- }
+ if (sb_len < 4)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: sense buffer too small %d bytes, %d bytes too short",
+ dev_scsi->kernel, sb_len,
+ 4 - sb_len);
if (sense_buffer[0] < 15)
log_debug("%s: old sense key: 0x%x", dev_scsi->kernel, sense_buffer[0] & 0x0f);
static int scsi_dump(struct scsi_id_device *dev_scsi, struct sg_io_hdr *io) {
if (!io->status && !io->host_status && !io->msg_status &&
- !io->driver_status) {
+ !io->driver_status)
/*
* Impossible, should not be called.
*/
- log_debug("%s: called with no error", __FUNCTION__);
- return -1;
- }
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: called with no error",
+ __FUNCTION__);
log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x",
dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status);
static int scsi_dump_v4(struct scsi_id_device *dev_scsi, struct sg_io_v4 *io) {
if (!io->device_status && !io->transport_status &&
- !io->driver_status) {
+ !io->driver_status)
/*
* Impossible, should not be called.
*/
- log_debug("%s: called with no error", __FUNCTION__);
- return -1;
- }
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: called with no error",
+ __FUNCTION__);
log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x",
dev_scsi->kernel, io->driver_status, io->transport_status, io->device_status);
int retry = 3; /* rather random */
int retval;
- if (buflen > SCSI_INQ_BUFF_LEN) {
- log_debug("buflen %d too long", buflen);
- return -1;
- }
+ if (buflen > SCSI_INQ_BUFF_LEN)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "buflen %d too long", buflen);
resend:
if (dev_scsi->use_sg == 4) {
* This is not a complete check, since we are using strncat/cpy
* above, ind will never be too large.
*/
- if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) {
- log_debug("%s: expected length %d, got length %d",
- dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind);
- return -1;
- }
+ if (ind != (VENDOR_LENGTH + MODEL_LENGTH))
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "%s: expected length %d, got length %d",
+ dev_scsi->kernel,
+ (VENDOR_LENGTH + MODEL_LENGTH), ind);
return ind;
}
if (id_search->id_type == SCSI_ID_NAA && wwn != NULL) {
strncpy(wwn, &serial[s], 16);
- if (wwn_vendor_extension != NULL)
+ if (wwn_vendor_extension)
strncpy(wwn_vendor_extension, &serial[s + 16], 16);
}
* specific type where we prepend '0' + vendor + model.
*/
len = buf[3];
- if (serial != NULL) {
+ if (serial) {
serial[0] = 'S';
ser_ind = prepend_vendor_model(dev_scsi, &serial[1]);
if (ser_ind < 0)
for (i = 4; i < len + 4; i++, ser_ind++)
serial[ser_ind] = buf[i];
}
- if (serial_short != NULL) {
+ if (serial_short) {
memcpy(serial_short, &buf[4], len);
serial_short[len] = '\0';
}
desc->bInterfaceProtocol) != 7)
continue;
- if (strstr(ifs_str, if_str) != NULL)
+ if (strstr(ifs_str, if_str))
continue;
memcpy(&ifs_str[strpos], if_str, 8),
}
device = argv[optind];
- if (device == NULL)
+ if (!device)
return 2;
fd = open(device, O_RDONLY);