repo
stringlengths
1
152
file
stringlengths
14
221
code
stringlengths
501
25k
file_length
int64
501
25k
avg_line_length
float64
20
99.5
max_line_length
int64
21
134
extension_type
stringclasses
2 values
null
systemd-main/src/systemctl/systemctl-whoami.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "bus-error.h" #include "bus-locator.h" #include "systemctl.h" #include "systemctl-util.h" #include "systemctl-whoami.h" #include "parse-util.h" static int lookup_pid(sd_bus *bus, pid_t pid) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_free_ char *unit = NULL; const char *path; int r; r = bus_call_method(bus, bus_systemd_mgr, "GetUnitByPID", &error, &reply, "u", (uint32_t) pid); if (r < 0) return log_error_errno(r, "Failed to get unit for ourselves: %s", bus_error_message(&error, r)); r = sd_bus_message_read(reply, "o", &path); if (r < 0) return bus_log_parse_error(r); r = unit_name_from_dbus_path(path, &unit); if (r < 0) return log_error_errno(r, "Failed to extract unit name from D-Bus object path '%s': %m", path); printf("%s\n", unit); return 0; } int verb_whoami(int argc, char *argv[], void *userdata) { sd_bus *bus; int r; r = acquire_bus(BUS_FULL, &bus); if (r < 0) return r; char **pids = strv_skip(argv, 1); if (strv_isempty(pids)) { if (arg_transport != BUS_TRANSPORT_LOCAL) return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), "Refusing to look up local PID on remote host."); return lookup_pid(bus, 0); } else { int ret = 0; STRV_FOREACH(p, pids) { pid_t pid; r = parse_pid(*p, &pid); if (r < 0) { log_error_errno(r, "Failed to parse PID: %s", *p); if (ret >= 0) ret = r; continue; } r = lookup_pid(bus, pid); if (r < 0 && ret >= 0) ret = r; } return ret; } }
2,212
30.169014
122
c
null
systemd-main/src/systemctl/systemctl.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once #include <stdbool.h> #include "bus-print-properties.h" #include "bus-util.h" #include "image-policy.h" #include "install.h" #include "output-mode.h" #include "pager.h" enum action { ACTION_SYSTEMCTL, ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT, ACTION_KEXEC, ACTION_SOFT_REBOOT, ACTION_EXIT, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE, ACTION_RUNLEVEL2, ACTION_RUNLEVEL3, ACTION_RUNLEVEL4, ACTION_RUNLEVEL5, ACTION_RESCUE, ACTION_EMERGENCY, ACTION_DEFAULT, ACTION_RELOAD, ACTION_REEXEC, ACTION_RUNLEVEL, ACTION_TELINIT, ACTION_CANCEL_SHUTDOWN, ACTION_SHOW_SHUTDOWN, _ACTION_MAX, _ACTION_INVALID = -EINVAL, }; enum dependency { DEPENDENCY_FORWARD, DEPENDENCY_REVERSE, DEPENDENCY_AFTER, DEPENDENCY_BEFORE, _DEPENDENCY_MAX }; extern char **arg_types; extern char **arg_states; extern char **arg_properties; extern bool arg_all; extern enum dependency arg_dependency; extern const char *_arg_job_mode; extern RuntimeScope arg_runtime_scope; extern bool arg_wait; extern bool arg_no_block; extern int arg_legend; extern PagerFlags arg_pager_flags; extern bool arg_no_wtmp; extern bool arg_no_sync; extern bool arg_no_wall; extern bool arg_no_reload; extern BusPrintPropertyFlags arg_print_flags; extern bool arg_show_types; extern int arg_check_inhibitors; extern bool arg_dry_run; extern bool arg_quiet; extern bool arg_no_warn; extern bool arg_full; extern bool arg_recursive; extern bool arg_with_dependencies; extern bool arg_show_transaction; extern int arg_force; extern bool arg_ask_password; extern bool arg_runtime; extern UnitFilePresetMode arg_preset_mode; extern char **arg_wall; extern const char *arg_kill_whom; extern int arg_signal; extern int arg_kill_value; extern bool arg_kill_value_set; extern char *arg_root; extern usec_t arg_when; extern const char *arg_reboot_argument; extern enum action arg_action; extern BusTransport arg_transport; extern const char *arg_host; extern unsigned arg_lines; extern OutputMode arg_output; extern bool arg_plain; extern bool arg_firmware_setup; extern usec_t arg_boot_loader_menu; extern const char *arg_boot_loader_entry; extern bool arg_now; extern bool arg_jobs_before; extern bool arg_jobs_after; extern char **arg_clean_what; extern TimestampStyle arg_timestamp_style; extern bool arg_read_only; extern bool arg_mkdir; extern bool arg_marked; extern const char *arg_drop_in; extern ImagePolicy *arg_image_policy; static inline const char* arg_job_mode(void) { return _arg_job_mode ?: "replace"; } int systemctl_dispatch_parse_argv(int argc, char *argv[]);
2,880
24.723214
58
h
null
systemd-main/src/systemd/_sd-common.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdcommonhfoo #define foosdcommonhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ /* This is a private header; never even think of including this directly! */ #if defined(__INCLUDE_LEVEL__) && __INCLUDE_LEVEL__ <= 1 && !defined(__COVERITY__) # error "Do not include _sd-common.h directly; it is a private header." #endif typedef void (*_sd_destroy_t)(void *userdata); #ifndef _sd_printf_ # if __GNUC__ >= 4 # define _sd_printf_(a,b) __attribute__((__format__(printf, a, b))) # else # define _sd_printf_(a,b) # endif #endif #ifndef _sd_sentinel_ # define _sd_sentinel_ __attribute__((__sentinel__)) #endif #ifndef _sd_packed_ # define _sd_packed_ __attribute__((__packed__)) #endif #ifndef _sd_pure_ # define _sd_pure_ __attribute__((__pure__)) #endif /* Note that strictly speaking __deprecated__ has been available before GCC 6. However, starting with GCC 6 * it also works on enum values, which we are interested in. Since this is a developer-facing feature anyway * (as opposed to build engineer-facing), let's hence conditionalize this to gcc 6, given that the developers * are probably going to use something newer anyway. */ #ifndef _sd_deprecated_ # if __GNUC__ >= 6 # define _sd_deprecated_ __attribute__((__deprecated__)) # else # define _sd_deprecated_ # endif #endif #ifndef _SD_STRINGIFY # define _SD_XSTRINGIFY(x) #x # define _SD_STRINGIFY(x) _SD_XSTRINGIFY(x) #endif #ifndef _SD_BEGIN_DECLARATIONS # ifdef __cplusplus # define _SD_BEGIN_DECLARATIONS \ extern "C" { \ struct _sd_useless_struct_to_allow_trailing_semicolon_ # else # define _SD_BEGIN_DECLARATIONS \ struct _sd_useless_struct_to_allow_trailing_semicolon_ # endif #endif #ifndef _SD_END_DECLARATIONS # ifdef __cplusplus # define _SD_END_DECLARATIONS \ } \ struct _sd_useless_cpp_struct_to_allow_trailing_semicolon_ # else # define _SD_END_DECLARATIONS \ struct _sd_useless_struct_to_allow_trailing_semicolon_ # endif #endif #ifndef _SD_ARRAY_STATIC # if __STDC_VERSION__ >= 199901L && !defined(__cplusplus) # define _SD_ARRAY_STATIC static # else # define _SD_ARRAY_STATIC # endif #endif #define _SD_DEFINE_POINTER_CLEANUP_FUNC(type, func) \ static __inline__ void func##p(type **p) { \ if (*p) \ func(*p); \ } \ struct _sd_useless_struct_to_allow_trailing_semicolon_ /* The following macro should be used in all public enums, to force 64-bit wideness on them, so that we can * freely extend them later on, without breaking compatibility. */ #define _SD_ENUM_FORCE_S64(id) \ _SD_##id##_INT64_MIN = INT64_MIN, \ _SD_##id##_INT64_MAX = INT64_MAX #endif
3,773
33.623853
109
h
null
systemd-main/src/systemd/sd-bus-protocol.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdbusprotocolhfoo #define foosdbusprotocolhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; /* Types of message */ enum { _SD_BUS_MESSAGE_TYPE_INVALID = 0, SD_BUS_MESSAGE_METHOD_CALL, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR, SD_BUS_MESSAGE_SIGNAL, _SD_BUS_MESSAGE_TYPE_MAX }; /* Primitive types */ enum { _SD_BUS_TYPE_INVALID = 0, SD_BUS_TYPE_BYTE = 'y', SD_BUS_TYPE_BOOLEAN = 'b', SD_BUS_TYPE_INT16 = 'n', SD_BUS_TYPE_UINT16 = 'q', SD_BUS_TYPE_INT32 = 'i', SD_BUS_TYPE_UINT32 = 'u', SD_BUS_TYPE_INT64 = 'x', SD_BUS_TYPE_UINT64 = 't', SD_BUS_TYPE_DOUBLE = 'd', SD_BUS_TYPE_STRING = 's', SD_BUS_TYPE_OBJECT_PATH = 'o', SD_BUS_TYPE_SIGNATURE = 'g', SD_BUS_TYPE_UNIX_FD = 'h', SD_BUS_TYPE_ARRAY = 'a', SD_BUS_TYPE_VARIANT = 'v', SD_BUS_TYPE_STRUCT = 'r', /* not actually used in signatures */ SD_BUS_TYPE_STRUCT_BEGIN = '(', SD_BUS_TYPE_STRUCT_END = ')', SD_BUS_TYPE_DICT_ENTRY = 'e', /* not actually used in signatures */ SD_BUS_TYPE_DICT_ENTRY_BEGIN = '{', SD_BUS_TYPE_DICT_ENTRY_END = '}' }; /* Well-known errors. Note that this is only a sanitized subset of the * errors that the reference implementation generates. */ #define SD_BUS_ERROR_FAILED "org.freedesktop.DBus.Error.Failed" #define SD_BUS_ERROR_NO_MEMORY "org.freedesktop.DBus.Error.NoMemory" #define SD_BUS_ERROR_SERVICE_UNKNOWN "org.freedesktop.DBus.Error.ServiceUnknown" #define SD_BUS_ERROR_NAME_HAS_NO_OWNER "org.freedesktop.DBus.Error.NameHasNoOwner" #define SD_BUS_ERROR_NO_REPLY "org.freedesktop.DBus.Error.NoReply" #define SD_BUS_ERROR_IO_ERROR "org.freedesktop.DBus.Error.IOError" #define SD_BUS_ERROR_BAD_ADDRESS "org.freedesktop.DBus.Error.BadAddress" #define SD_BUS_ERROR_NOT_SUPPORTED "org.freedesktop.DBus.Error.NotSupported" #define SD_BUS_ERROR_LIMITS_EXCEEDED "org.freedesktop.DBus.Error.LimitsExceeded" #define SD_BUS_ERROR_ACCESS_DENIED "org.freedesktop.DBus.Error.AccessDenied" #define SD_BUS_ERROR_AUTH_FAILED "org.freedesktop.DBus.Error.AuthFailed" #define SD_BUS_ERROR_NO_SERVER "org.freedesktop.DBus.Error.NoServer" #define SD_BUS_ERROR_TIMEOUT "org.freedesktop.DBus.Error.Timeout" #define SD_BUS_ERROR_NO_NETWORK "org.freedesktop.DBus.Error.NoNetwork" #define SD_BUS_ERROR_ADDRESS_IN_USE "org.freedesktop.DBus.Error.AddressInUse" #define SD_BUS_ERROR_DISCONNECTED "org.freedesktop.DBus.Error.Disconnected" #define SD_BUS_ERROR_INVALID_ARGS "org.freedesktop.DBus.Error.InvalidArgs" #define SD_BUS_ERROR_FILE_NOT_FOUND "org.freedesktop.DBus.Error.FileNotFound" #define SD_BUS_ERROR_FILE_EXISTS "org.freedesktop.DBus.Error.FileExists" #define SD_BUS_ERROR_UNKNOWN_METHOD "org.freedesktop.DBus.Error.UnknownMethod" #define SD_BUS_ERROR_UNKNOWN_OBJECT "org.freedesktop.DBus.Error.UnknownObject" #define SD_BUS_ERROR_UNKNOWN_INTERFACE "org.freedesktop.DBus.Error.UnknownInterface" #define SD_BUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty" #define SD_BUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly" #define SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN "org.freedesktop.DBus.Error.UnixProcessIdUnknown" #define SD_BUS_ERROR_INVALID_SIGNATURE "org.freedesktop.DBus.Error.InvalidSignature" #define SD_BUS_ERROR_INCONSISTENT_MESSAGE "org.freedesktop.DBus.Error.InconsistentMessage" #define SD_BUS_ERROR_TIMED_OUT "org.freedesktop.DBus.Error.TimedOut" #define SD_BUS_ERROR_MATCH_RULE_NOT_FOUND "org.freedesktop.DBus.Error.MatchRuleNotFound" #define SD_BUS_ERROR_MATCH_RULE_INVALID "org.freedesktop.DBus.Error.MatchRuleInvalid" #define SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED "org.freedesktop.DBus.Error.InteractiveAuthorizationRequired" #define SD_BUS_ERROR_INVALID_FILE_CONTENT "org.freedesktop.DBus.Error.InvalidFileContent" #define SD_BUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown" #define SD_BUS_ERROR_OBJECT_PATH_IN_USE "org.freedesktop.DBus.Error.ObjectPathInUse" /* https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-signature */ #define SD_BUS_MAXIMUM_SIGNATURE_LENGTH 255 /* https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names */ #define SD_BUS_MAXIMUM_NAME_LENGTH 255 _SD_END_DECLARATIONS; #endif
5,950
53.59633
117
h
null
systemd-main/src/systemd/sd-bus-vtable.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdbusvtablehfoo #define foosdbusvtablehfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_bus_vtable sd_bus_vtable; #include "sd-bus.h" enum { _SD_BUS_VTABLE_START = '<', _SD_BUS_VTABLE_END = '>', _SD_BUS_VTABLE_METHOD = 'M', _SD_BUS_VTABLE_SIGNAL = 'S', _SD_BUS_VTABLE_PROPERTY = 'P', _SD_BUS_VTABLE_WRITABLE_PROPERTY = 'W' }; __extension__ enum { SD_BUS_VTABLE_DEPRECATED = 1ULL << 0, SD_BUS_VTABLE_HIDDEN = 1ULL << 1, SD_BUS_VTABLE_UNPRIVILEGED = 1ULL << 2, SD_BUS_VTABLE_METHOD_NO_REPLY = 1ULL << 3, SD_BUS_VTABLE_PROPERTY_CONST = 1ULL << 4, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE = 1ULL << 5, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION = 1ULL << 6, SD_BUS_VTABLE_PROPERTY_EXPLICIT = 1ULL << 7, SD_BUS_VTABLE_SENSITIVE = 1ULL << 8, /* covers both directions: method call + reply */ SD_BUS_VTABLE_ABSOLUTE_OFFSET = 1ULL << 9, _SD_BUS_VTABLE_CAPABILITY_MASK = 0xFFFFULL << 40 }; #define SD_BUS_VTABLE_CAPABILITY(x) ((uint64_t) (((x)+1) & 0xFFFF) << 40) enum { _SD_BUS_VTABLE_PARAM_NAMES = 1 << 0 }; extern const unsigned sd_bus_object_vtable_format; /* Note: unused areas in the sd_bus_vtable[] array must be initialized to 0. The structure contains an embedded * union, and the compiler is NOT required to initialize the unused areas of the union when the rest of the * structure is initialized. Normally the array is defined as read-only data, in which case the linker places * it in the BSS section, which is always fully initialized, so this is not a concern. But if the array is * created on the stack or on the heap, care must be taken to initialize the unused areas, for examply by * first memsetting the whole region to zero before filling the data in. */ struct sd_bus_vtable { /* Please do not initialize this structure directly, use the * macros below instead */ __extension__ uint8_t type:8; __extension__ uint64_t flags:56; union { struct { size_t element_size; uint64_t features; const unsigned *vtable_format_reference; } start; struct { /* This field exists only to make sure we have something to initialize in * SD_BUS_VTABLE_END in a way that is both compatible with pedantic versions of C and * C++. It's unused otherwise. */ size_t _reserved; } end; struct { const char *member; const char *signature; const char *result; sd_bus_message_handler_t handler; size_t offset; const char *names; } method; struct { const char *member; const char *signature; const char *names; } signal; struct { const char *member; const char *signature; sd_bus_property_get_t get; sd_bus_property_set_t set; size_t offset; } property; } x; }; #define SD_BUS_VTABLE_START(_flags) \ { \ .type = _SD_BUS_VTABLE_START, \ .flags = _flags, \ .x = { \ .start = { \ .element_size = sizeof(sd_bus_vtable), \ .features = _SD_BUS_VTABLE_PARAM_NAMES, \ .vtable_format_reference = &sd_bus_object_vtable_format, \ }, \ }, \ } /* helper macro to format method and signal parameters, one at a time */ #define SD_BUS_PARAM(x) #x "\0" #define SD_BUS_METHOD_WITH_NAMES_OFFSET(_member, _signature, _in_names, _result, _out_names, _handler, _offset, _flags) \ { \ .type = _SD_BUS_VTABLE_METHOD, \ .flags = _flags, \ .x = { \ .method = { \ .member = _member, \ .signature = _signature, \ .result = _result, \ .handler = _handler, \ .offset = _offset, \ .names = _in_names _out_names, \ }, \ }, \ } #define SD_BUS_METHOD_WITH_OFFSET(_member, _signature, _result, _handler, _offset, _flags) \ SD_BUS_METHOD_WITH_NAMES_OFFSET(_member, _signature, "", _result, "", _handler, _offset, _flags) #define SD_BUS_METHOD_WITH_NAMES(_member, _signature, _in_names, _result, _out_names, _handler, _flags) \ SD_BUS_METHOD_WITH_NAMES_OFFSET(_member, _signature, _in_names, _result, _out_names, _handler, 0, _flags) #define SD_BUS_METHOD(_member, _signature, _result, _handler, _flags) \ SD_BUS_METHOD_WITH_NAMES_OFFSET(_member, _signature, "", _result, "", _handler, 0, _flags) #define SD_BUS_SIGNAL_WITH_NAMES(_member, _signature, _out_names, _flags) \ { \ .type = _SD_BUS_VTABLE_SIGNAL, \ .flags = _flags, \ .x = { \ .signal = { \ .member = _member, \ .signature = _signature, \ .names = _out_names, \ }, \ }, \ } #define SD_BUS_SIGNAL(_member, _signature, _flags) \ SD_BUS_SIGNAL_WITH_NAMES(_member, _signature, "", _flags) #define SD_BUS_PROPERTY(_member, _signature, _get, _offset, _flags) \ { \ .type = _SD_BUS_VTABLE_PROPERTY, \ .flags = _flags, \ .x = { \ .property = { \ .member = _member, \ .signature = _signature, \ .get = _get, \ .set = NULL, \ .offset = _offset, \ }, \ }, \ } #define SD_BUS_WRITABLE_PROPERTY(_member, _signature, _get, _set, _offset, _flags) \ { \ .type = _SD_BUS_VTABLE_WRITABLE_PROPERTY, \ .flags = _flags, \ .x = { \ .property = { \ .member = _member, \ .signature = _signature, \ .get = _get, \ .set = _set, \ .offset = _offset, \ }, \ }, \ } #define SD_BUS_VTABLE_END \ { \ .type = _SD_BUS_VTABLE_END, \ .flags = 0, \ .x = { \ .end = { \ ._reserved = 0, \ }, \ }, \ } #define _SD_ECHO(X) X #define _SD_CONCAT(X) #X "\0" #define _SD_VARARGS_FOREACH_SEQ(_01, _02, _03, _04, _05, _06, _07, _08, _09, _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, \ NAME, ...) NAME #define _SD_VARARGS_FOREACH_EVEN_01(FN, X) FN(X) #define _SD_VARARGS_FOREACH_EVEN_02(FN, X, Y) FN(X) #define _SD_VARARGS_FOREACH_EVEN_04(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_02(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_06(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_04(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_08(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_06(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_10(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_08(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_12(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_10(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_14(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_12(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_16(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_14(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_18(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_16(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_20(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_18(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_22(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_20(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_24(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_22(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_26(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_24(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_28(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_26(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_30(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_28(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_32(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_30(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_34(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_32(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_36(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_34(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_38(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_36(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_40(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_38(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_42(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_40(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_44(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_42(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_46(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_44(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_48(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_46(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN_50(FN, X, Y, ...) FN(X) _SD_VARARGS_FOREACH_EVEN_48(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_EVEN(FN, ...) \ _SD_VARARGS_FOREACH_SEQ(__VA_ARGS__, \ _SD_VARARGS_FOREACH_EVEN_50, _SD_VARARGS_FOREACH_EVEN_49, \ _SD_VARARGS_FOREACH_EVEN_48, _SD_VARARGS_FOREACH_EVEN_47, \ _SD_VARARGS_FOREACH_EVEN_46, _SD_VARARGS_FOREACH_EVEN_45, \ _SD_VARARGS_FOREACH_EVEN_44, _SD_VARARGS_FOREACH_EVEN_43, \ _SD_VARARGS_FOREACH_EVEN_42, _SD_VARARGS_FOREACH_EVEN_41, \ _SD_VARARGS_FOREACH_EVEN_40, _SD_VARARGS_FOREACH_EVEN_39, \ _SD_VARARGS_FOREACH_EVEN_38, _SD_VARARGS_FOREACH_EVEN_37, \ _SD_VARARGS_FOREACH_EVEN_36, _SD_VARARGS_FOREACH_EVEN_35, \ _SD_VARARGS_FOREACH_EVEN_34, _SD_VARARGS_FOREACH_EVEN_33, \ _SD_VARARGS_FOREACH_EVEN_32, _SD_VARARGS_FOREACH_EVEN_31, \ _SD_VARARGS_FOREACH_EVEN_30, _SD_VARARGS_FOREACH_EVEN_29, \ _SD_VARARGS_FOREACH_EVEN_28, _SD_VARARGS_FOREACH_EVEN_27, \ _SD_VARARGS_FOREACH_EVEN_26, _SD_VARARGS_FOREACH_EVEN_25, \ _SD_VARARGS_FOREACH_EVEN_24, _SD_VARARGS_FOREACH_EVEN_23, \ _SD_VARARGS_FOREACH_EVEN_22, _SD_VARARGS_FOREACH_EVEN_21, \ _SD_VARARGS_FOREACH_EVEN_20, _SD_VARARGS_FOREACH_EVEN_19, \ _SD_VARARGS_FOREACH_EVEN_18, _SD_VARARGS_FOREACH_EVEN_17, \ _SD_VARARGS_FOREACH_EVEN_16, _SD_VARARGS_FOREACH_EVEN_15, \ _SD_VARARGS_FOREACH_EVEN_14, _SD_VARARGS_FOREACH_EVEN_13, \ _SD_VARARGS_FOREACH_EVEN_12, _SD_VARARGS_FOREACH_EVEN_11, \ _SD_VARARGS_FOREACH_EVEN_10, _SD_VARARGS_FOREACH_EVEN_09, \ _SD_VARARGS_FOREACH_EVEN_08, _SD_VARARGS_FOREACH_EVEN_07, \ _SD_VARARGS_FOREACH_EVEN_06, _SD_VARARGS_FOREACH_EVEN_05, \ _SD_VARARGS_FOREACH_EVEN_04, _SD_VARARGS_FOREACH_EVEN_03, \ _SD_VARARGS_FOREACH_EVEN_02, _SD_VARARGS_FOREACH_EVEN_01) \ (FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_01(FN, X) #define _SD_VARARGS_FOREACH_ODD_02(FN, X, Y) FN(Y) #define _SD_VARARGS_FOREACH_ODD_04(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_02(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_06(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_04(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_08(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_06(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_10(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_08(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_12(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_10(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_14(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_12(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_16(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_14(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_18(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_16(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_20(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_18(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_22(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_20(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_24(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_22(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_26(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_24(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_28(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_26(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_30(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_28(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_32(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_30(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_34(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_32(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_36(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_34(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_38(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_36(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_40(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_38(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_42(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_40(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_44(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_42(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_46(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_44(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_48(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_46(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD_50(FN, X, Y, ...) FN(Y) _SD_VARARGS_FOREACH_ODD_48(FN, __VA_ARGS__) #define _SD_VARARGS_FOREACH_ODD(FN, ...) \ _SD_VARARGS_FOREACH_SEQ(__VA_ARGS__, \ _SD_VARARGS_FOREACH_ODD_50, _SD_VARARGS_FOREACH_ODD_49, \ _SD_VARARGS_FOREACH_ODD_48, _SD_VARARGS_FOREACH_ODD_47, \ _SD_VARARGS_FOREACH_ODD_46, _SD_VARARGS_FOREACH_ODD_45, \ _SD_VARARGS_FOREACH_ODD_44, _SD_VARARGS_FOREACH_ODD_43, \ _SD_VARARGS_FOREACH_ODD_42, _SD_VARARGS_FOREACH_ODD_41, \ _SD_VARARGS_FOREACH_ODD_40, _SD_VARARGS_FOREACH_ODD_39, \ _SD_VARARGS_FOREACH_ODD_38, _SD_VARARGS_FOREACH_ODD_37, \ _SD_VARARGS_FOREACH_ODD_36, _SD_VARARGS_FOREACH_ODD_35, \ _SD_VARARGS_FOREACH_ODD_34, _SD_VARARGS_FOREACH_ODD_33, \ _SD_VARARGS_FOREACH_ODD_32, _SD_VARARGS_FOREACH_ODD_31, \ _SD_VARARGS_FOREACH_ODD_30, _SD_VARARGS_FOREACH_ODD_29, \ _SD_VARARGS_FOREACH_ODD_28, _SD_VARARGS_FOREACH_ODD_27, \ _SD_VARARGS_FOREACH_ODD_26, _SD_VARARGS_FOREACH_ODD_25, \ _SD_VARARGS_FOREACH_ODD_24, _SD_VARARGS_FOREACH_ODD_23, \ _SD_VARARGS_FOREACH_ODD_22, _SD_VARARGS_FOREACH_ODD_21, \ _SD_VARARGS_FOREACH_ODD_20, _SD_VARARGS_FOREACH_ODD_19, \ _SD_VARARGS_FOREACH_ODD_18, _SD_VARARGS_FOREACH_ODD_17, \ _SD_VARARGS_FOREACH_ODD_16, _SD_VARARGS_FOREACH_ODD_15, \ _SD_VARARGS_FOREACH_ODD_14, _SD_VARARGS_FOREACH_ODD_13, \ _SD_VARARGS_FOREACH_ODD_12, _SD_VARARGS_FOREACH_ODD_11, \ _SD_VARARGS_FOREACH_ODD_10, _SD_VARARGS_FOREACH_ODD_09, \ _SD_VARARGS_FOREACH_ODD_08, _SD_VARARGS_FOREACH_ODD_07, \ _SD_VARARGS_FOREACH_ODD_06, _SD_VARARGS_FOREACH_ODD_05, \ _SD_VARARGS_FOREACH_ODD_04, _SD_VARARGS_FOREACH_ODD_03, \ _SD_VARARGS_FOREACH_ODD_02, _SD_VARARGS_FOREACH_ODD_01) \ (FN, __VA_ARGS__) #define SD_BUS_ARGS(...) __VA_ARGS__ #define SD_BUS_RESULT(...) __VA_ARGS__ #define SD_BUS_NO_ARGS SD_BUS_ARGS(NULL) #define SD_BUS_NO_RESULT SD_BUS_RESULT(NULL) #define SD_BUS_METHOD_WITH_ARGS(_member, _args, _result, _handler, _flags) \ SD_BUS_METHOD_WITH_NAMES(_member, \ _SD_VARARGS_FOREACH_EVEN(_SD_ECHO, _args), \ _SD_VARARGS_FOREACH_ODD(_SD_CONCAT, _args), \ _SD_VARARGS_FOREACH_EVEN(_SD_ECHO, _result), \ _SD_VARARGS_FOREACH_ODD(_SD_CONCAT, _result) "\0", \ _handler, _flags) #define SD_BUS_METHOD_WITH_ARGS_OFFSET(_member, _args, _result, _handler, _offset, _flags) \ SD_BUS_METHOD_WITH_NAMES_OFFSET(_member, \ _SD_VARARGS_FOREACH_EVEN(_SD_ECHO, _args), \ _SD_VARARGS_FOREACH_ODD(_SD_CONCAT, _args), \ _SD_VARARGS_FOREACH_EVEN(_SD_ECHO, _result), \ _SD_VARARGS_FOREACH_ODD(_SD_CONCAT, _result) "\0", \ _handler, _offset, _flags) #define SD_BUS_SIGNAL_WITH_ARGS(_member, _args, _flags) \ SD_BUS_SIGNAL_WITH_NAMES(_member, \ _SD_VARARGS_FOREACH_EVEN(_SD_ECHO, _args), \ _SD_VARARGS_FOREACH_ODD(_SD_CONCAT, _args) "\0", \ _flags) _SD_END_DECLARATIONS; #endif
23,074
64.183616
122
h
null
systemd-main/src/systemd/sd-daemon.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddaemonhfoo #define foosddaemonhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <sys/types.h> #include <sys/socket.h> #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; /* The following functionality is provided: - Support for logging with log levels on stderr - File descriptor passing for socket-based activation - Daemon startup and status notification - Detection of systemd boots See sd-daemon(3) for more information. */ /* Log levels for usage on stderr: fprintf(stderr, SD_NOTICE "Hello World!\n"); This is similar to printk() usage in the kernel. */ #define SD_EMERG "<0>" /* system is unusable */ #define SD_ALERT "<1>" /* action must be taken immediately */ #define SD_CRIT "<2>" /* critical conditions */ #define SD_ERR "<3>" /* error conditions */ #define SD_WARNING "<4>" /* warning conditions */ #define SD_NOTICE "<5>" /* normal but significant condition */ #define SD_INFO "<6>" /* informational */ #define SD_DEBUG "<7>" /* debug-level messages */ /* The first passed file descriptor is fd 3 */ #define SD_LISTEN_FDS_START 3 /* Returns how many file descriptors have been passed, or a negative errno code on failure. Optionally, removes the $LISTEN_FDS and $LISTEN_PID file descriptors from the environment (recommended, but problematic in threaded environments). If r is the return value of this function you'll find the file descriptors passed as fds SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative errno style error code on failure. This function call ensures that the FD_CLOEXEC flag is set for the passed file descriptors, to make sure they are not passed on to child processes. If FD_CLOEXEC shall not be set, the caller needs to unset it after this call for all file descriptors that are used. See sd_listen_fds(3) for more information. */ int sd_listen_fds(int unset_environment); int sd_listen_fds_with_names(int unset_environment, char ***names); /* Helper call for identifying a passed file descriptor. Returns 1 if the file descriptor is a FIFO in the file system stored under the specified path, 0 otherwise. If path is NULL a path name check will not be done and the call only verifies if the file descriptor refers to a FIFO. Returns a negative errno style error code on failure. See sd_is_fifo(3) for more information. */ int sd_is_fifo(int fd, const char *path); /* Helper call for identifying a passed file descriptor. Returns 1 if the file descriptor is a special character device on the file system stored under the specified path, 0 otherwise. If path is NULL a path name check will not be done and the call only verifies if the file descriptor refers to a special character. Returns a negative errno style error code on failure. See sd_is_special(3) for more information. */ int sd_is_special(int fd, const char *path); /* Helper call for identifying a passed file descriptor. Returns 1 if the file descriptor is a socket of the specified family (AF_INET, ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If family is 0 a socket family check will not be done. If type is 0 a socket type check will not be done and the call only verifies if the file descriptor refers to a socket. If listening is > 0 it is verified that the socket is in listening mode. (i.e. listen() has been called) If listening is == 0 it is verified that the socket is not in listening mode. If listening is < 0 no listening mode check is done. Returns a negative errno style error code on failure. See sd_is_socket(3) for more information. */ int sd_is_socket(int fd, int family, int type, int listening); /* Helper call for identifying a passed file descriptor. Returns 1 if the file descriptor is an Internet socket, of the specified family (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version check is not done. If type is 0 a socket type check will not be done. If port is 0 a socket port check will not be done. The listening flag is used the same way as in sd_is_socket(). Returns a negative errno style error code on failure. See sd_is_socket_inet(3) for more information. */ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port); /* Helper call for identifying a passed file descriptor. Returns 1 if the file descriptor is an Internet socket of the specified type (SOCK_DGRAM, SOCK_STREAM, ...), and if the address of the socket is the same as the address specified by addr. The listening flag is used the same way as in sd_is_socket(). Returns a negative errno style error code on failure. See sd_is_socket_sockaddr(3) for more information. */ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening); /* Helper call for identifying a passed file descriptor. Returns 1 if the file descriptor is an AF_UNIX socket of the specified type (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0 a socket type check will not be done. If path is NULL a socket path check will not be done. For normal AF_UNIX sockets set length to 0. For abstract namespace sockets set length to the length of the socket name (including the initial 0 byte), and pass the full socket path in path (including the initial 0 byte). The listening flag is used the same way as in sd_is_socket(). Returns a negative errno style error code on failure. See sd_is_socket_unix(3) for more information. */ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length); /* Helper call for identifying a passed file descriptor. Returns 1 if the file descriptor is a POSIX Message Queue of the specified name, 0 otherwise. If path is NULL a message queue name check is not done. Returns a negative errno style error code on failure. See sd_is_mq(3) for more information. */ int sd_is_mq(int fd, const char *path); /* Informs systemd about changed daemon state. This takes a number of newline separated environment-style variable assignments in a string. The following variables are known: MAINPID=... The main PID of a daemon, in case systemd did not fork off the process itself. Example: "MAINPID=4711" READY=1 Tells systemd that daemon startup or daemon reload is finished (only relevant for services of Type=notify). The passed argument is a boolean "1" or "0". Since there is little value in signaling non-readiness the only value daemons should send is "READY=1". RELOADING=1 Tell systemd that the daemon began reloading its configuration. When the configuration has been reloaded completely, READY=1 should be sent to inform systemd about this. STOPPING=1 Tells systemd that the daemon is about to go down. STATUS=... Passes a single-line status string back to systemd that describes the daemon state. This is free-form and can be used for various purposes: general state feedback, fsck-like programs could pass completion percentages and failing programs could pass a human readable error message. Example: "STATUS=Completed 66% of file system check..." NOTIFYACCESS=... Reset the access to the service status notification socket. Example: "NOTIFYACCESS=main" ERRNO=... If a daemon fails, the errno-style error code, formatted as string. Example: "ERRNO=2" for ENOENT. BUSERROR=... If a daemon fails, the D-Bus error-style error code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut" WATCHDOG=1 Tells systemd to update the watchdog timestamp. Services using this feature should do this in regular intervals. A watchdog framework can use the timestamps to detect failed services. Also see sd_watchdog_enabled() below. WATCHDOG_USEC=... Reset watchdog_usec value during runtime. To reset watchdog_usec value, start the service again. Example: "WATCHDOG_USEC=20000000" FDSTORE=1 Store the file descriptors passed along with the message in the per-service file descriptor store, and pass them to the main process again on next invocation. This variable is only supported with sd_pid_notify_with_fds(). FDSTOREREMOVE=1 Remove one or more file descriptors from the file descriptor store, identified by the name specified in FDNAME=, see below. FDNAME= A name to assign to new file descriptors stored in the file descriptor store, or the name of the file descriptors to remove in case of FDSTOREREMOVE=1. Daemons can choose to send additional variables. However, it is recommended to prefix variable names not listed above with X_. Returns a negative errno-style error code on failure. Returns > 0 if systemd could be notified, 0 if it couldn't possibly because systemd is not running. Example: When a daemon finished starting up, it could issue this call to notify systemd about it: sd_notify(0, "READY=1"); See sd_notifyf() for more complete examples. See sd_notify(3) for more information. */ int sd_notify(int unset_environment, const char *state); /* Similar to sd_notify() but takes a format string. Example 1: A daemon could send the following after initialization: sd_notifyf(0, "READY=1\n" "STATUS=Processing requests...\n" "MAINPID=%lu", (unsigned long) getpid()); Example 2: A daemon could send the following shortly before exiting, on failure: sd_notifyf(0, "STATUS=Failed to start up: %s\n" "ERRNO=%i", strerror_r(errnum, (char[1024]){}, 1024), errnum); See sd_notifyf(3) for more information. */ int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_(2,3); /* Similar to sd_notify(), but send the message on behalf of another process, if the appropriate permissions are available. */ int sd_pid_notify(pid_t pid, int unset_environment, const char *state); /* Similar to sd_notifyf(), but send the message on behalf of another process, if the appropriate permissions are available. */ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) _sd_printf_(3,4); /* Similar to sd_pid_notify(), but also passes the specified fd array to the service manager for storage. This is particularly useful for FDSTORE=1 messages. */ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds); /* Combination of sd_pid_notifyf() and sd_pid_notify_with_fds() */ int sd_pid_notifyf_with_fds(pid_t pid, int unset_environment, const int *fds, size_t n_fds, const char *format, ...) _sd_printf_(5,6); /* Returns > 0 if synchronization with systemd succeeded. Returns < 0 on error. Returns 0 if $NOTIFY_SOCKET was not set. Note that the timeout parameter of this function call takes the timeout in μs, and will be passed to ppoll(2), hence the behaviour will be similar to ppoll(2). This function can be called after sending a status message to systemd, if one needs to synchronize against reception of the status messages sent before this call is made. Therefore, this cannot be used to know if the status message was processed successfully, but to only synchronize against its consumption. */ int sd_notify_barrier(int unset_environment, uint64_t timeout); /* Just like sd_notify_barrier() but also takes a PID to send the barrier message from. */ int sd_pid_notify_barrier(pid_t pid, int unset_environment, uint64_t timeout); /* Returns > 0 if the system was booted with systemd. Returns < 0 on error. Returns 0 if the system was not booted with systemd. Note that all of the functions above handle non-systemd boots just fine. You should NOT protect them with a call to this function. Also note that this function checks whether the system, not the user session is controlled by systemd. However the functions above work for both user and system services. See sd_booted(3) for more information. */ int sd_booted(void); /* Returns > 0 if the service manager expects watchdog keep-alive events to be sent regularly via sd_notify(0, "WATCHDOG=1"). Returns 0 if it does not expect this. If the usec argument is non-NULL returns the watchdog timeout in μs after which the service manager will act on a process that has not sent a watchdog keep alive message. This function is useful to implement services that recognize automatically if they are being run under supervision of systemd with WatchdogSec= set. It is recommended for clients to generate keep-alive pings via sd_notify(0, "WATCHDOG=1") every half of the returned time. See sd_watchdog_enabled(3) for more information. */ int sd_watchdog_enabled(int unset_environment, uint64_t *usec); _SD_END_DECLARATIONS; #endif
14,163
39.701149
134
h
null
systemd-main/src/systemd/sd-device.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddevicehfoo #define foosddevicehfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <errno.h> #include <inttypes.h> #include <sys/stat.h> #include <sys/sysmacros.h> #include <sys/types.h> #include "sd-event.h" #include "sd-id128.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_device sd_device; typedef struct sd_device_enumerator sd_device_enumerator; typedef struct sd_device_monitor sd_device_monitor; __extension__ typedef enum sd_device_action_t { SD_DEVICE_ADD, SD_DEVICE_REMOVE, SD_DEVICE_CHANGE, SD_DEVICE_MOVE, SD_DEVICE_ONLINE, SD_DEVICE_OFFLINE, SD_DEVICE_BIND, SD_DEVICE_UNBIND, _SD_DEVICE_ACTION_MAX, _SD_DEVICE_ACTION_INVALID = -EINVAL, _SD_ENUM_FORCE_S64(DEVICE_ACTION) } sd_device_action_t; /* callback */ typedef int (*sd_device_monitor_handler_t)(sd_device_monitor *m, sd_device *device, void *userdata); /* device */ sd_device *sd_device_ref(sd_device *device); sd_device *sd_device_unref(sd_device *device); int sd_device_new_from_syspath(sd_device **ret, const char *syspath); int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum); int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *subsystem, const char *sysname); int sd_device_new_from_device_id(sd_device **ret, const char *id); int sd_device_new_from_stat_rdev(sd_device **ret, const struct stat *st); int sd_device_new_from_devname(sd_device **ret, const char *devname); int sd_device_new_from_path(sd_device **ret, const char *path); int sd_device_new_from_ifname(sd_device **ret, const char *ifname); int sd_device_new_from_ifindex(sd_device **ret, int ifindex); int sd_device_new_child(sd_device **ret, sd_device *device, const char *suffix); int sd_device_get_parent(sd_device *child, sd_device **ret); int sd_device_get_parent_with_subsystem_devtype(sd_device *child, const char *subsystem, const char *devtype, sd_device **ret); int sd_device_get_syspath(sd_device *device, const char **ret); int sd_device_get_subsystem(sd_device *device, const char **ret); int sd_device_get_devtype(sd_device *device, const char **ret); int sd_device_get_devnum(sd_device *device, dev_t *devnum); int sd_device_get_ifindex(sd_device *device, int *ifindex); int sd_device_get_driver(sd_device *device, const char **ret); int sd_device_get_devpath(sd_device *device, const char **ret); int sd_device_get_devname(sd_device *device, const char **ret); int sd_device_get_sysname(sd_device *device, const char **ret); int sd_device_get_sysnum(sd_device *device, const char **ret); int sd_device_get_action(sd_device *device, sd_device_action_t *ret); int sd_device_get_seqnum(sd_device *device, uint64_t *ret); int sd_device_get_diskseq(sd_device *device, uint64_t *ret); int sd_device_get_is_initialized(sd_device *device); int sd_device_get_usec_initialized(sd_device *device, uint64_t *ret); int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *ret); const char *sd_device_get_tag_first(sd_device *device); const char *sd_device_get_tag_next(sd_device *device); const char *sd_device_get_current_tag_first(sd_device *device); const char *sd_device_get_current_tag_next(sd_device *device); const char *sd_device_get_devlink_first(sd_device *device); const char *sd_device_get_devlink_next(sd_device *device); const char *sd_device_get_property_first(sd_device *device, const char **value); const char *sd_device_get_property_next(sd_device *device, const char **value); const char *sd_device_get_sysattr_first(sd_device *device); const char *sd_device_get_sysattr_next(sd_device *device); sd_device *sd_device_get_child_first(sd_device *device, const char **ret_suffix); sd_device *sd_device_get_child_next(sd_device *device, const char **ret_suffix); int sd_device_has_tag(sd_device *device, const char *tag); int sd_device_has_current_tag(sd_device *device, const char *tag); int sd_device_get_property_value(sd_device *device, const char *key, const char **value); int sd_device_get_trigger_uuid(sd_device *device, sd_id128_t *ret); int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **_value); int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, const char *value); int sd_device_set_sysattr_valuef(sd_device *device, const char *sysattr, const char *format, ...) _sd_printf_(3, 4); int sd_device_trigger(sd_device *device, sd_device_action_t action); int sd_device_trigger_with_uuid(sd_device *device, sd_device_action_t action, sd_id128_t *ret_uuid); int sd_device_open(sd_device *device, int flags); /* device enumerator */ int sd_device_enumerator_new(sd_device_enumerator **ret); sd_device_enumerator *sd_device_enumerator_ref(sd_device_enumerator *enumerator); sd_device_enumerator *sd_device_enumerator_unref(sd_device_enumerator *enumerator); sd_device *sd_device_enumerator_get_device_first(sd_device_enumerator *enumerator); sd_device *sd_device_enumerator_get_device_next(sd_device_enumerator *enumerator); sd_device *sd_device_enumerator_get_subsystem_first(sd_device_enumerator *enumerator); sd_device *sd_device_enumerator_get_subsystem_next(sd_device_enumerator *enumerator); int sd_device_enumerator_add_match_subsystem(sd_device_enumerator *enumerator, const char *subsystem, int match); int sd_device_enumerator_add_match_sysattr(sd_device_enumerator *enumerator, const char *sysattr, const char *value, int match); int sd_device_enumerator_add_match_property(sd_device_enumerator *enumerator, const char *property, const char *value); int sd_device_enumerator_add_match_sysname(sd_device_enumerator *enumerator, const char *sysname); int sd_device_enumerator_add_nomatch_sysname(sd_device_enumerator *enumerator, const char *sysname); int sd_device_enumerator_add_match_tag(sd_device_enumerator *enumerator, const char *tag); int sd_device_enumerator_add_match_parent(sd_device_enumerator *enumerator, sd_device *parent); int sd_device_enumerator_allow_uninitialized(sd_device_enumerator *enumerator); /* device monitor */ int sd_device_monitor_new(sd_device_monitor **ret); sd_device_monitor *sd_device_monitor_ref(sd_device_monitor *m); sd_device_monitor *sd_device_monitor_unref(sd_device_monitor *m); int sd_device_monitor_set_receive_buffer_size(sd_device_monitor *m, size_t size); int sd_device_monitor_attach_event(sd_device_monitor *m, sd_event *event); int sd_device_monitor_detach_event(sd_device_monitor *m); sd_event *sd_device_monitor_get_event(sd_device_monitor *m); sd_event_source *sd_device_monitor_get_event_source(sd_device_monitor *m); int sd_device_monitor_set_description(sd_device_monitor *m, const char *description); int sd_device_monitor_get_description(sd_device_monitor *m, const char **ret); int sd_device_monitor_start(sd_device_monitor *m, sd_device_monitor_handler_t callback, void *userdata); int sd_device_monitor_stop(sd_device_monitor *m); int sd_device_monitor_filter_add_match_subsystem_devtype(sd_device_monitor *m, const char *subsystem, const char *devtype); int sd_device_monitor_filter_add_match_tag(sd_device_monitor *m, const char *tag); int sd_device_monitor_filter_add_match_sysattr(sd_device_monitor *m, const char *sysattr, const char *value, int match); int sd_device_monitor_filter_add_match_parent(sd_device_monitor *m, sd_device *device, int match); int sd_device_monitor_filter_update(sd_device_monitor *m); int sd_device_monitor_filter_remove(sd_device_monitor *m); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_device, sd_device_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_device_enumerator, sd_device_enumerator_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_device_monitor, sd_device_monitor_unref); _SD_END_DECLARATIONS; #endif
8,375
48.857143
128
h
null
systemd-main/src/systemd/sd-dhcp-client.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddhcpclienthfoo #define foosddhcpclienthfoo /*** Copyright © 2013 Intel Corporation. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <net/ethernet.h> #include <netinet/in.h> #include <sys/types.h> #include <stdbool.h> #include "sd-device.h" #include "sd-dhcp-lease.h" #include "sd-dhcp-option.h" #include "sd-event.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; enum { SD_DHCP_CLIENT_EVENT_STOP = 0, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE = 1, SD_DHCP_CLIENT_EVENT_IP_CHANGE = 2, SD_DHCP_CLIENT_EVENT_EXPIRED = 3, SD_DHCP_CLIENT_EVENT_RENEW = 4, SD_DHCP_CLIENT_EVENT_SELECTING = 5, SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE = 6 /* Sent when we have not received a reply after the first few attempts. * The client may want to start acquiring link-local addresses. */ }; /* https://www.iana.org/assignments/bootp-dhcp-parameters/bootp-dhcp-parameters.xhtml#options */ enum { SD_DHCP_OPTION_PAD = 0, /* [RFC2132] */ SD_DHCP_OPTION_SUBNET_MASK = 1, /* [RFC2132] */ SD_DHCP_OPTION_TIME_OFFSET = 2, /* [RFC2132], deprecated by 100 and 101 */ SD_DHCP_OPTION_ROUTER = 3, /* [RFC2132] */ SD_DHCP_OPTION_TIME_SERVER = 4, /* [RFC2132] */ SD_DHCP_OPTION_NAME_SERVER = 5, /* [RFC2132] */ SD_DHCP_OPTION_DOMAIN_NAME_SERVER = 6, /* [RFC2132] */ SD_DHCP_OPTION_LOG_SERVER = 7, /* [RFC2132] */ SD_DHCP_OPTION_QUOTES_SERVER = 8, /* [RFC2132] */ SD_DHCP_OPTION_LPR_SERVER = 9, /* [RFC2132] */ SD_DHCP_OPTION_IMPRESS_SERVER = 10, /* [RFC2132] */ SD_DHCP_OPTION_RLP_SERVER = 11, /* [RFC2132] */ SD_DHCP_OPTION_HOST_NAME = 12, /* [RFC2132] */ SD_DHCP_OPTION_BOOT_FILE_SIZE = 13, /* [RFC2132] */ SD_DHCP_OPTION_MERIT_DUMP_FILE = 14, /* [RFC2132] */ SD_DHCP_OPTION_DOMAIN_NAME = 15, /* [RFC2132] */ SD_DHCP_OPTION_SWAP_SERVER = 16, /* [RFC2132] */ SD_DHCP_OPTION_ROOT_PATH = 17, /* [RFC2132] */ SD_DHCP_OPTION_EXTENSION_FILE = 18, /* [RFC2132] */ SD_DHCP_OPTION_FORWARD = 19, /* [RFC2132] */ SD_DHCP_OPTION_SOURCE_ROUTE = 20, /* [RFC2132] */ SD_DHCP_OPTION_POLICY_FILTER = 21, /* [RFC2132] */ SD_DHCP_OPTION_MAX_DATAGRAM_ASSEMBLY = 22, /* [RFC2132] */ SD_DHCP_OPTION_DEFAULT_IP_TTL = 23, /* [RFC2132] */ SD_DHCP_OPTION_MTU_TIMEOUT = 24, /* [RFC2132] */ SD_DHCP_OPTION_MTU_PLATEAU = 25, /* [RFC2132] */ SD_DHCP_OPTION_MTU_INTERFACE = 26, /* [RFC2132] */ SD_DHCP_OPTION_MTU_SUBNET = 27, /* [RFC2132] */ SD_DHCP_OPTION_BROADCAST = 28, /* [RFC2132] */ SD_DHCP_OPTION_MASK_DISCOVERY = 29, /* [RFC2132] */ SD_DHCP_OPTION_MASK_SUPPLIER = 30, /* [RFC2132] */ SD_DHCP_OPTION_ROUTER_DISCOVERY = 31, /* [RFC2132] */ SD_DHCP_OPTION_ROUTER_REQUEST = 32, /* [RFC2132] */ SD_DHCP_OPTION_STATIC_ROUTE = 33, /* [RFC2132] */ SD_DHCP_OPTION_TRAILERS = 34, /* [RFC2132] */ SD_DHCP_OPTION_ARP_TIMEOUT = 35, /* [RFC2132] */ SD_DHCP_OPTION_ETHERNET = 36, /* [RFC2132] */ SD_DHCP_OPTION_DEFAULT_TCP_TTL = 37, /* [RFC2132] */ SD_DHCP_OPTION_KEEPALIVE_TIME = 38, /* [RFC2132] */ SD_DHCP_OPTION_KEEPALIVE_DATA = 39, /* [RFC2132] */ SD_DHCP_OPTION_NIS_DOMAIN = 40, /* [RFC2132] */ SD_DHCP_OPTION_NIS_SERVER = 41, /* [RFC2132] */ SD_DHCP_OPTION_NTP_SERVER = 42, /* [RFC2132] */ SD_DHCP_OPTION_VENDOR_SPECIFIC = 43, /* [RFC2132] */ SD_DHCP_OPTION_NETBIOS_NAME_SERVER = 44, /* [RFC2132] */ SD_DHCP_OPTION_NETBIOS_DIST_SERVER = 45, /* [RFC2132] */ SD_DHCP_OPTION_NETBIOS_NODE_TYPE = 46, /* [RFC2132] */ SD_DHCP_OPTION_NETBIOS_SCOPE = 47, /* [RFC2132] */ SD_DHCP_OPTION_X_WINDOW_FONT = 48, /* [RFC2132] */ SD_DHCP_OPTION_X_WINDOW_MANAGER = 49, /* [RFC2132] */ SD_DHCP_OPTION_REQUESTED_IP_ADDRESS = 50, /* [RFC2132] */ SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME = 51, /* [RFC2132] */ SD_DHCP_OPTION_OVERLOAD = 52, /* [RFC2132] */ SD_DHCP_OPTION_MESSAGE_TYPE = 53, /* [RFC2132] */ SD_DHCP_OPTION_SERVER_IDENTIFIER = 54, /* [RFC2132] */ SD_DHCP_OPTION_PARAMETER_REQUEST_LIST = 55, /* [RFC2132] */ SD_DHCP_OPTION_ERROR_MESSAGE = 56, /* [RFC2132] */ SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE = 57, /* [RFC2132] */ SD_DHCP_OPTION_RENEWAL_TIME = 58, /* [RFC2132] */ SD_DHCP_OPTION_REBINDING_TIME = 59, /* [RFC2132] */ SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER = 60, /* [RFC2132] */ SD_DHCP_OPTION_CLIENT_IDENTIFIER = 61, /* [RFC2132] */ SD_DHCP_OPTION_NETWARE_IP_DOMAIN = 62, /* [RFC2242] */ SD_DHCP_OPTION_NETWARE_IP_OPTION = 63, /* [RFC2242] */ SD_DHCP_OPTION_NIS_DOMAIN_NAME = 64, /* [RFC2132] */ SD_DHCP_OPTION_NIS_SERVER_ADDR = 65, /* [RFC2132] */ SD_DHCP_OPTION_BOOT_SERVER_NAME = 66, /* [RFC2132] */ SD_DHCP_OPTION_BOOT_FILENAME = 67, /* [RFC2132] */ SD_DHCP_OPTION_HOME_AGENT_ADDRESSES = 68, /* [RFC2132] */ SD_DHCP_OPTION_SMTP_SERVER = 69, /* [RFC2132] */ SD_DHCP_OPTION_POP3_SERVER = 70, /* [RFC2132] */ SD_DHCP_OPTION_NNTP_SERVER = 71, /* [RFC2132] */ SD_DHCP_OPTION_WWW_SERVER = 72, /* [RFC2132] */ SD_DHCP_OPTION_FINGER_SERVER = 73, /* [RFC2132] */ SD_DHCP_OPTION_IRC_SERVER = 74, /* [RFC2132] */ SD_DHCP_OPTION_STREETTALK_SERVER = 75, /* [RFC2132] */ SD_DHCP_OPTION_STDA_SERVER = 76, /* [RFC2132] */ SD_DHCP_OPTION_USER_CLASS = 77, /* [RFC3004] */ SD_DHCP_OPTION_DIRECTORY_AGENT = 78, /* [RFC2610] */ SD_DHCP_OPTION_SERVICE_SCOPE = 79, /* [RFC2610] */ SD_DHCP_OPTION_RAPID_COMMIT = 80, /* [RFC4039] */ SD_DHCP_OPTION_FQDN = 81, /* [RFC4702] */ SD_DHCP_OPTION_RELAY_AGENT_INFORMATION = 82, /* [RFC3046] */ SD_DHCP_OPTION_ISNS = 83, /* [RFC4174] */ /* option code 84 is unassigned [RFC3679] */ SD_DHCP_OPTION_NDS_SERVER = 85, /* [RFC2241] */ SD_DHCP_OPTION_NDS_TREE_NAME = 86, /* [RFC2241] */ SD_DHCP_OPTION_NDS_CONTEXT = 87, /* [RFC2241] */ SD_DHCP_OPTION_BCMCS_CONTROLLER_DOMAIN_NAME = 88, /* [RFC4280] */ SD_DHCP_OPTION_BCMCS_CONTROLLER_ADDRESS = 89, /* [RFC4280] */ SD_DHCP_OPTION_AUTHENTICATION = 90, /* [RFC3118] */ SD_DHCP_OPTION_CLIENT_LAST_TRANSACTION_TIME = 91, /* [RFC4388] */ SD_DHCP_OPTION_ASSOCIATED_IP = 92, /* [RFC4388] */ SD_DHCP_OPTION_CLIENT_SYSTEM = 93, /* [RFC4578] */ SD_DHCP_OPTION_CLIENT_NDI = 94, /* [RFC4578] */ SD_DHCP_OPTION_LDAP = 95, /* [RFC3679] */ /* option code 96 is unassigned [RFC3679] */ SD_DHCP_OPTION_UUID = 97, /* [RFC4578] */ SD_DHCP_OPTION_USER_AUTHENTICATION = 98, /* [RFC2485] */ SD_DHCP_OPTION_GEOCONF_CIVIC = 99, /* [RFC4776] */ SD_DHCP_OPTION_POSIX_TIMEZONE = 100, /* [RFC4833] */ SD_DHCP_OPTION_TZDB_TIMEZONE = 101, /* [RFC4833] */ /* option codes 102-107 are unassigned [RFC3679] */ SD_DHCP_OPTION_IPV6_ONLY_PREFERRED = 108, /* [RFC8925] */ SD_DHCP_OPTION_DHCP4O6_SOURCE_ADDRESS = 109, /* [RFC8539] */ /* option codes 110-111 are unassigned [RFC3679] */ SD_DHCP_OPTION_NETINFO_ADDRESS = 112, /* [RFC3679] */ SD_DHCP_OPTION_NETINFO_TAG = 113, /* [RFC3679] */ SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL = 114, /* [RFC8910] */ /* option code 115 is unassigned [RFC3679] */ SD_DHCP_OPTION_AUTO_CONFIG = 116, /* [RFC2563] */ SD_DHCP_OPTION_NAME_SERVICE_SEARCH = 117, /* [RFC2937] */ SD_DHCP_OPTION_SUBNET_SELECTION = 118, /* [RFC3011] */ SD_DHCP_OPTION_DOMAIN_SEARCH = 119, /* [RFC3397] */ SD_DHCP_OPTION_SIP_SERVER = 120, /* [RFC3361] */ SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE = 121, /* [RFC3442] */ SD_DHCP_OPTION_CABLELABS_CLIENT_CONFIGURATION = 122, /* [RFC3495] */ SD_DHCP_OPTION_GEOCONF = 123, /* [RFC6225] */ SD_DHCP_OPTION_VENDOR_CLASS = 124, /* [RFC3925] */ SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION = 125, /* [RFC3925] */ /* option codes 126-127 are unassigned [RFC3679] */ /* option codes 128-135 are assigned to use by PXE, but they are vendor specific [RFC4578] */ SD_DHCP_OPTION_PANA_AGENT = 136, /* [RFC5192] */ SD_DHCP_OPTION_LOST_SERVER_FQDN = 137, /* [RFC5223] */ SD_DHCP_OPTION_CAPWAP_AC_ADDRESS = 138, /* [RFC5417] */ SD_DHCP_OPTION_MOS_ADDRESS = 139, /* [RFC5678] */ SD_DHCP_OPTION_MOS_FQDN = 140, /* [RFC5678] */ SD_DHCP_OPTION_SIP_SERVICE_DOMAIN = 141, /* [RFC6011] */ SD_DHCP_OPTION_ANDSF_ADDRESS = 142, /* [RFC6153] */ SD_DHCP_OPTION_SZTP_REDIRECT = 143, /* [RFC8572] */ SD_DHCP_OPTION_GEOLOC = 144, /* [RFC6225] */ SD_DHCP_OPTION_FORCERENEW_NONCE_CAPABLE = 145, /* [RFC6704] */ SD_DHCP_OPTION_RDNSS_SELECTION = 146, /* [RFC6731] */ SD_DHCP_OPTION_DOTS_RI = 147, /* [RFC8973] */ SD_DHCP_OPTION_DOTS_ADDRESS = 148, /* [RFC8973] */ /* option code 149 is unassigned [RFC3942] */ SD_DHCP_OPTION_TFTP_SERVER_ADDRESS = 150, /* [RFC5859] */ SD_DHCP_OPTION_STATUS_CODE = 151, /* [RFC6926] */ SD_DHCP_OPTION_BASE_TIME = 152, /* [RFC6926] */ SD_DHCP_OPTION_START_TIME_OF_STATE = 153, /* [RFC6926] */ SD_DHCP_OPTION_QUERY_START_TIME = 154, /* [RFC6926] */ SD_DHCP_OPTION_QUERY_END_TIME = 155, /* [RFC6926] */ SD_DHCP_OPTION_DHCP_STATE = 156, /* [RFC6926] */ SD_DHCP_OPTION_DATA_SOURCE = 157, /* [RFC6926] */ SD_DHCP_OPTION_PCP_SERVER = 158, /* [RFC7291] */ SD_DHCP_OPTION_PORT_PARAMS = 159, /* [RFC7618] */ /* option code 160 is unassigned [RFC7710][RFC8910] */ SD_DHCP_OPTION_MUD_URL = 161, /* [RFC8520] */ /* option codes 162-174 are unassigned [RFC3942] */ /* option codes 175-177 are temporary assigned. */ /* option codes 178-207 are unassigned [RFC3942] */ SD_DHCP_OPTION_PXELINUX_MAGIC = 208, /* [RFC5071] Deprecated */ SD_DHCP_OPTION_CONFIGURATION_FILE = 209, /* [RFC5071] */ SD_DHCP_OPTION_PATH_PREFIX = 210, /* [RFC5071] */ SD_DHCP_OPTION_REBOOT_TIME = 211, /* [RFC5071] */ SD_DHCP_OPTION_6RD = 212, /* [RFC5969] */ SD_DHCP_OPTION_ACCESS_DOMAIN = 213, /* [RFC5986] */ /* option codes 214-219 are unassigned */ SD_DHCP_OPTION_SUBNET_ALLOCATION = 220, /* [RFC6656] */ SD_DHCP_OPTION_VIRTUAL_SUBNET_SELECTION = 221, /* [RFC6607] */ /* option codes 222-223 are unassigned [RFC3942] */ /* option codes 224-254 are reserved for private use */ SD_DHCP_OPTION_PRIVATE_BASE = 224, SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE = 249, /* [RFC7844] */ SD_DHCP_OPTION_PRIVATE_PROXY_AUTODISCOVERY = 252, /* [RFC7844] */ SD_DHCP_OPTION_PRIVATE_LAST = 254, SD_DHCP_OPTION_END = 255 /* [RFC2132] */ }; /* Suboptions for SD_DHCP_OPTION_RELAY_AGENT_INFORMATION option */ enum { SD_DHCP_RELAY_AGENT_CIRCUIT_ID = 1, SD_DHCP_RELAY_AGENT_REMOTE_ID = 2 }; typedef struct sd_dhcp_client sd_dhcp_client; typedef int (*sd_dhcp_client_callback_t)(sd_dhcp_client *client, int event, void *userdata); int sd_dhcp_client_set_callback( sd_dhcp_client *client, sd_dhcp_client_callback_t cb, void *userdata); int sd_dhcp_client_set_request_option( sd_dhcp_client *client, uint8_t option); int sd_dhcp_client_set_request_address( sd_dhcp_client *client, const struct in_addr *last_address); int sd_dhcp_client_set_request_broadcast( sd_dhcp_client *client, int broadcast); int sd_dhcp_client_set_ifindex( sd_dhcp_client *client, int interface_index); int sd_dhcp_client_set_ifname( sd_dhcp_client *client, const char *interface_name); int sd_dhcp_client_get_ifname(sd_dhcp_client *client, const char **ret); int sd_dhcp_client_set_mac( sd_dhcp_client *client, const uint8_t *hw_addr, const uint8_t *bcast_addr, size_t addr_len, uint16_t arp_type); int sd_dhcp_client_set_client_id( sd_dhcp_client *client, uint8_t type, const uint8_t *data, size_t data_len); __extension__ int sd_dhcp_client_set_iaid_duid( sd_dhcp_client *client, bool iaid_set, uint32_t iaid, uint16_t duid_type, const void *duid, size_t duid_len); __extension__ int sd_dhcp_client_set_iaid_duid_llt( sd_dhcp_client *client, bool iaid_set, uint32_t iaid, uint64_t llt_time); int sd_dhcp_client_set_duid( sd_dhcp_client *client, uint16_t duid_type, const void *duid, size_t duid_len); int sd_dhcp_client_set_duid_llt( sd_dhcp_client *client, uint64_t llt_time); int sd_dhcp_client_get_client_id( sd_dhcp_client *client, uint8_t *ret_type, const uint8_t **ret_data, size_t *ret_data_len); int sd_dhcp_client_set_mtu( sd_dhcp_client *client, uint32_t mtu); int sd_dhcp_client_set_max_attempts( sd_dhcp_client *client, uint64_t attempt); int sd_dhcp_client_set_client_port( sd_dhcp_client *client, uint16_t port); int sd_dhcp_client_set_hostname( sd_dhcp_client *client, const char *hostname); int sd_dhcp_client_set_vendor_class_identifier( sd_dhcp_client *client, const char *vci); int sd_dhcp_client_set_mud_url( sd_dhcp_client *client, const char *mudurl); int sd_dhcp_client_set_user_class( sd_dhcp_client *client, char * const *user_class); int sd_dhcp_client_get_lease( sd_dhcp_client *client, sd_dhcp_lease **ret); int sd_dhcp_client_set_service_type( sd_dhcp_client *client, int type); int sd_dhcp_client_set_socket_priority( sd_dhcp_client *client, int so_priority); int sd_dhcp_client_set_fallback_lease_lifetime( sd_dhcp_client *client, uint32_t fallback_lease_lifetime); int sd_dhcp_client_add_option(sd_dhcp_client *client, sd_dhcp_option *v); int sd_dhcp_client_add_vendor_option(sd_dhcp_client *client, sd_dhcp_option *v); int sd_dhcp_client_is_running(sd_dhcp_client *client); int sd_dhcp_client_stop(sd_dhcp_client *client); int sd_dhcp_client_start(sd_dhcp_client *client); int sd_dhcp_client_send_release(sd_dhcp_client *client); int sd_dhcp_client_send_decline(sd_dhcp_client *client); int sd_dhcp_client_send_renew(sd_dhcp_client *client); sd_dhcp_client *sd_dhcp_client_ref(sd_dhcp_client *client); sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client); /* NOTE: anonymize parameter is used to initialize PRL memory with different * options when using RFC7844 Anonymity Profiles */ int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize); int sd_dhcp_client_id_to_string(const void *data, size_t len, char **ret); int sd_dhcp_client_attach_event( sd_dhcp_client *client, sd_event *event, int64_t priority); int sd_dhcp_client_detach_event(sd_dhcp_client *client); sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client); int sd_dhcp_client_attach_device(sd_dhcp_client *client, sd_device *dev); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp_client, sd_dhcp_client_unref); _SD_END_DECLARATIONS; #endif
19,387
54.236467
123
h
null
systemd-main/src/systemd/sd-dhcp-lease.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddhcpleasehfoo #define foosddhcpleasehfoo /*** Copyright © 2013 Intel Corporation. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <errno.h> #include <inttypes.h> #include <net/ethernet.h> #include <netinet/in.h> #include <sys/types.h> #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_dhcp_lease sd_dhcp_lease; typedef struct sd_dhcp_route sd_dhcp_route; sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease); sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease); __extension__ typedef enum sd_dhcp_lease_server_type_t { SD_DHCP_LEASE_DNS, SD_DHCP_LEASE_NTP, SD_DHCP_LEASE_SIP, SD_DHCP_LEASE_POP3, SD_DHCP_LEASE_SMTP, SD_DHCP_LEASE_LPR, _SD_DHCP_LEASE_SERVER_TYPE_MAX, _SD_DHCP_LEASE_SERVER_TYPE_INVALID = -EINVAL, _SD_ENUM_FORCE_S64(DHCP_LEASE_SERVER_TYPE) } sd_dhcp_lease_server_type_t; int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr); int sd_dhcp_lease_get_lifetime(sd_dhcp_lease *lease, uint32_t *lifetime); int sd_dhcp_lease_get_t1(sd_dhcp_lease *lease, uint32_t *t1); int sd_dhcp_lease_get_t2(sd_dhcp_lease *lease, uint32_t *t2); int sd_dhcp_lease_get_broadcast(sd_dhcp_lease *lease, struct in_addr *addr); int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr); int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, const struct in_addr **addr); int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr); int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr); int sd_dhcp_lease_get_servers(sd_dhcp_lease *lease, sd_dhcp_lease_server_type_t what, const struct in_addr **addr); int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr); int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr); int sd_dhcp_lease_get_sip(sd_dhcp_lease *lease, const struct in_addr **addr); int sd_dhcp_lease_get_pop3(sd_dhcp_lease *lease, const struct in_addr **addr); int sd_dhcp_lease_get_smtp(sd_dhcp_lease *lease, const struct in_addr **addr); int sd_dhcp_lease_get_lpr(sd_dhcp_lease *lease, const struct in_addr **addr); int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu); int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname); int sd_dhcp_lease_get_search_domains(sd_dhcp_lease *lease, char ***domains); int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname); int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path); int sd_dhcp_lease_get_captive_portal(sd_dhcp_lease *lease, const char **captive_portal); int sd_dhcp_lease_get_static_routes(sd_dhcp_lease *lease, sd_dhcp_route ***ret); int sd_dhcp_lease_get_classless_routes(sd_dhcp_lease *lease, sd_dhcp_route ***ret); int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, size_t *data_len); int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const void **client_id, size_t *client_id_len); int sd_dhcp_lease_get_timezone(sd_dhcp_lease *lease, const char **timezone); int sd_dhcp_lease_get_6rd( sd_dhcp_lease *lease, uint8_t *ret_ipv4masklen, uint8_t *ret_prefixlen, struct in6_addr *ret_prefix, const struct in_addr **ret_br_addresses, size_t *ret_n_br_addresses); int sd_dhcp_route_get_destination(sd_dhcp_route *route, struct in_addr *destination); int sd_dhcp_route_get_destination_prefix_length(sd_dhcp_route *route, uint8_t *length); int sd_dhcp_route_get_gateway(sd_dhcp_route *route, struct in_addr *gateway); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp_lease, sd_dhcp_lease_unref); _SD_END_DECLARATIONS; #endif
4,393
46.247312
115
h
null
systemd-main/src/systemd/sd-dhcp-option.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddhcpoptionhfoo #define foosddhcpoptionhfoo /*** Copyright © 2013 Intel Corporation. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <sys/types.h> #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_dhcp_option sd_dhcp_option; int sd_dhcp_option_new(uint8_t option, const void *data, size_t length, sd_dhcp_option **ret); sd_dhcp_option *sd_dhcp_option_ref(sd_dhcp_option *ra); sd_dhcp_option *sd_dhcp_option_unref(sd_dhcp_option *ra); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp_option, sd_dhcp_option_unref); _SD_END_DECLARATIONS; #endif
1,270
31.589744
94
h
null
systemd-main/src/systemd/sd-dhcp6-client.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddhcp6clienthfoo #define foosddhcp6clienthfoo /*** Copyright © 2014 Intel Corporation. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <net/ethernet.h> #include <sys/types.h> #include "sd-device.h" #include "sd-dhcp6-lease.h" #include "sd-dhcp6-option.h" #include "sd-event.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; enum { SD_DHCP6_CLIENT_EVENT_STOP = 0, SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE = 10, SD_DHCP6_CLIENT_EVENT_RETRANS_MAX = 11, SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE = 12, SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST = 13 }; /* https://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml#dhcpv6-parameters-2 */ enum { SD_DHCP6_OPTION_CLIENTID = 1, /* RFC 8415 */ SD_DHCP6_OPTION_SERVERID = 2, /* RFC 8415 */ SD_DHCP6_OPTION_IA_NA = 3, /* RFC 8415 */ SD_DHCP6_OPTION_IA_TA = 4, /* RFC 8415 */ SD_DHCP6_OPTION_IAADDR = 5, /* RFC 8415 */ SD_DHCP6_OPTION_ORO = 6, /* RFC 8415 */ SD_DHCP6_OPTION_PREFERENCE = 7, /* RFC 8415 */ SD_DHCP6_OPTION_ELAPSED_TIME = 8, /* RFC 8415 */ SD_DHCP6_OPTION_RELAY_MSG = 9, /* RFC 8415 */ /* option code 10 is unassigned */ SD_DHCP6_OPTION_AUTH = 11, /* RFC 8415 */ SD_DHCP6_OPTION_UNICAST = 12, /* RFC 8415 */ SD_DHCP6_OPTION_STATUS_CODE = 13, /* RFC 8415 */ SD_DHCP6_OPTION_RAPID_COMMIT = 14, /* RFC 8415 */ SD_DHCP6_OPTION_USER_CLASS = 15, /* RFC 8415 */ SD_DHCP6_OPTION_VENDOR_CLASS = 16, /* RFC 8415 */ SD_DHCP6_OPTION_VENDOR_OPTS = 17, /* RFC 8415 */ SD_DHCP6_OPTION_INTERFACE_ID = 18, /* RFC 8415 */ SD_DHCP6_OPTION_RECONF_MSG = 19, /* RFC 8415 */ SD_DHCP6_OPTION_RECONF_ACCEPT = 20, /* RFC 8415 */ SD_DHCP6_OPTION_SIP_SERVER_DOMAIN_NAME = 21, /* RFC 3319 */ SD_DHCP6_OPTION_SIP_SERVER_ADDRESS = 22, /* RFC 3319 */ SD_DHCP6_OPTION_DNS_SERVER = 23, /* RFC 3646 */ SD_DHCP6_OPTION_DOMAIN = 24, /* RFC 3646 */ SD_DHCP6_OPTION_IA_PD = 25, /* RFC 3633, RFC 8415 */ SD_DHCP6_OPTION_IA_PD_PREFIX = 26, /* RFC 3633, RFC 8415 */ SD_DHCP6_OPTION_NIS_SERVER = 27, /* RFC 3898 */ SD_DHCP6_OPTION_NISP_SERVER = 28, /* RFC 3898 */ SD_DHCP6_OPTION_NIS_DOMAIN_NAME = 29, /* RFC 3898 */ SD_DHCP6_OPTION_NISP_DOMAIN_NAME = 30, /* RFC 3898 */ SD_DHCP6_OPTION_SNTP_SERVER = 31, /* RFC 4075, deprecated */ SD_DHCP6_OPTION_INFORMATION_REFRESH_TIME = 32, /* RFC 4242, 8415, sec. 21.23 */ SD_DHCP6_OPTION_BCMCS_SERVER_D = 33, /* RFC 4280 */ SD_DHCP6_OPTION_BCMCS_SERVER_A = 34, /* RFC 4280 */ /* option code 35 is unassigned */ SD_DHCP6_OPTION_GEOCONF_CIVIC = 36, /* RFC 4776 */ SD_DHCP6_OPTION_REMOTE_ID = 37, /* RFC 4649 */ SD_DHCP6_OPTION_SUBSCRIBER_ID = 38, /* RFC 4580 */ SD_DHCP6_OPTION_CLIENT_FQDN = 39, /* RFC 4704 */ SD_DHCP6_OPTION_PANA_AGENT = 40, /* RFC 5192 */ SD_DHCP6_OPTION_POSIX_TIMEZONE = 41, /* RFC 4833 */ SD_DHCP6_OPTION_TZDB_TIMEZONE = 42, /* RFC 4833 */ SD_DHCP6_OPTION_ERO = 43, /* RFC 4994 */ SD_DHCP6_OPTION_LQ_QUERY = 44, /* RFC 5007 */ SD_DHCP6_OPTION_CLIENT_DATA = 45, /* RFC 5007 */ SD_DHCP6_OPTION_CLT_TIME = 46, /* RFC 5007 */ SD_DHCP6_OPTION_LQ_RELAY_DATA = 47, /* RFC 5007 */ SD_DHCP6_OPTION_LQ_CLIENT_LINK = 48, /* RFC 5007 */ SD_DHCP6_OPTION_MIP6_HNIDF = 49, /* RFC 6610 */ SD_DHCP6_OPTION_MIP6_VDINF = 50, /* RFC 6610 */ SD_DHCP6_OPTION_V6_LOST = 51, /* RFC 5223 */ SD_DHCP6_OPTION_CAPWAP_AC_V6 = 52, /* RFC 5417 */ SD_DHCP6_OPTION_RELAY_ID = 53, /* RFC 5460 */ SD_DHCP6_OPTION_IPV6_ADDRESS_MOS = 54, /* RFC 5678 */ SD_DHCP6_OPTION_IPV6_FQDN_MOS = 55, /* RFC 5678 */ SD_DHCP6_OPTION_NTP_SERVER = 56, /* RFC 5908 */ SD_DHCP6_OPTION_V6_ACCESS_DOMAIN = 57, /* RFC 5986 */ SD_DHCP6_OPTION_SIP_UA_CS_LIST = 58, /* RFC 6011 */ SD_DHCP6_OPTION_BOOTFILE_URL = 59, /* RFC 5970 */ SD_DHCP6_OPTION_BOOTFILE_PARAM = 60, /* RFC 5970 */ SD_DHCP6_OPTION_CLIENT_ARCH_TYPE = 61, /* RFC 5970 */ SD_DHCP6_OPTION_NII = 62, /* RFC 5970 */ SD_DHCP6_OPTION_GEOLOCATION = 63, /* RFC 6225 */ SD_DHCP6_OPTION_AFTR_NAME = 64, /* RFC 6334 */ SD_DHCP6_OPTION_ERP_LOCAL_DOMAIN_NAME = 65, /* RFC 6440 */ SD_DHCP6_OPTION_RSOO = 66, /* RFC 6422 */ SD_DHCP6_OPTION_PD_EXCLUDE = 67, /* RFC 6603 */ SD_DHCP6_OPTION_VSS = 68, /* RFC 6607 */ SD_DHCP6_OPTION_MIP6_IDINF = 69, /* RFC 6610 */ SD_DHCP6_OPTION_MIP6_UDINF = 70, /* RFC 6610 */ SD_DHCP6_OPTION_MIP6_HNP = 71, /* RFC 6610 */ SD_DHCP6_OPTION_MIP6_HAA = 72, /* RFC 6610 */ SD_DHCP6_OPTION_MIP6_HAF = 73, /* RFC 6610 */ SD_DHCP6_OPTION_RDNSS_SELECTION = 74, /* RFC 6731 */ SD_DHCP6_OPTION_KRB_PRINCIPAL_NAME = 75, /* RFC 6784 */ SD_DHCP6_OPTION_KRB_REALM_NAME = 76, /* RFC 6784 */ SD_DHCP6_OPTION_KRB_DEFAULT_REALM_NAME = 77, /* RFC 6784 */ SD_DHCP6_OPTION_KRB_KDC = 78, /* RFC 6784 */ SD_DHCP6_OPTION_CLIENT_LINKLAYER_ADDR = 79, /* RFC 6939 */ SD_DHCP6_OPTION_LINK_ADDRESS = 80, /* RFC 6977 */ SD_DHCP6_OPTION_RADIUS = 81, /* RFC 7037 */ SD_DHCP6_OPTION_SOL_MAX_RT = 82, /* RFC 7083, RFC 8415 */ SD_DHCP6_OPTION_INF_MAX_RT = 83, /* RFC 7083, RFC 8415 */ SD_DHCP6_OPTION_ADDRSEL = 84, /* RFC 7078 */ SD_DHCP6_OPTION_ADDRSEL_TABLE = 85, /* RFC 7078 */ SD_DHCP6_OPTION_V6_PCP_SERVER = 86, /* RFC 7291 */ SD_DHCP6_OPTION_DHCPV4_MSG = 87, /* RFC 7341 */ SD_DHCP6_OPTION_DHCP4_O_DHCP6_SERVER = 88, /* RFC 7341 */ SD_DHCP6_OPTION_S46_RULE = 89, /* RFC 7598 */ SD_DHCP6_OPTION_S46_BR = 90, /* RFC 7598, RFC 8539 */ SD_DHCP6_OPTION_S46_DMR = 91, /* RFC 7598 */ SD_DHCP6_OPTION_S46_V4V6BIND = 92, /* RFC 7598 */ SD_DHCP6_OPTION_S46_PORTPARAMS = 93, /* RFC 7598 */ SD_DHCP6_OPTION_S46_CONT_MAPE = 94, /* RFC 7598 */ SD_DHCP6_OPTION_S46_CONT_MAPT = 95, /* RFC 7598 */ SD_DHCP6_OPTION_S46_CONT_LW = 96, /* RFC 7598 */ SD_DHCP6_OPTION_4RD = 97, /* RFC 7600 */ SD_DHCP6_OPTION_4RD_MAP_RULE = 98, /* RFC 7600 */ SD_DHCP6_OPTION_4RD_NON_MAP_RULE = 99, /* RFC 7600 */ SD_DHCP6_OPTION_LQ_BASE_TIME = 100, /* RFC 7653 */ SD_DHCP6_OPTION_LQ_START_TIME = 101, /* RFC 7653 */ SD_DHCP6_OPTION_LQ_END_TIME = 102, /* RFC 7653 */ SD_DHCP6_OPTION_CAPTIVE_PORTAL = 103, /* RFC 8910 */ SD_DHCP6_OPTION_MPL_PARAMETERS = 104, /* RFC 7774 */ SD_DHCP6_OPTION_ANI_ATT = 105, /* RFC 7839 */ SD_DHCP6_OPTION_ANI_NETWORK_NAME = 106, /* RFC 7839 */ SD_DHCP6_OPTION_ANI_AP_NAME = 107, /* RFC 7839 */ SD_DHCP6_OPTION_ANI_AP_BSSID = 108, /* RFC 7839 */ SD_DHCP6_OPTION_ANI_OPERATOR_ID = 109, /* RFC 7839 */ SD_DHCP6_OPTION_ANI_OPERATOR_REALM = 110, /* RFC 7839 */ SD_DHCP6_OPTION_S46_PRIORITY = 111, /* RFC 8026 */ SD_DHCP6_OPTION_MUD_URL_V6 = 112, /* RFC 8520 */ SD_DHCP6_OPTION_V6_PREFIX64 = 113, /* RFC 8115 */ SD_DHCP6_OPTION_F_BINDING_STATUS = 114, /* RFC 8156 */ SD_DHCP6_OPTION_F_CONNECT_FLAGS = 115, /* RFC 8156 */ SD_DHCP6_OPTION_F_DNS_REMOVAL_INFO = 116, /* RFC 8156 */ SD_DHCP6_OPTION_F_DNS_HOST_NAME = 117, /* RFC 8156 */ SD_DHCP6_OPTION_F_DNS_ZONE_NAME = 118, /* RFC 8156 */ SD_DHCP6_OPTION_F_DNS_FLAGS = 119, /* RFC 8156 */ SD_DHCP6_OPTION_F_EXPIRATION_TIME = 120, /* RFC 8156 */ SD_DHCP6_OPTION_F_MAX_UNACKED_BNDUPD = 121, /* RFC 8156 */ SD_DHCP6_OPTION_F_MCLT = 122, /* RFC 8156 */ SD_DHCP6_OPTION_F_PARTNER_LIFETIME = 123, /* RFC 8156 */ SD_DHCP6_OPTION_F_PARTNER_LIFETIME_SENT = 124, /* RFC 8156 */ SD_DHCP6_OPTION_F_PARTNER_DOWN_TIME = 125, /* RFC 8156 */ SD_DHCP6_OPTION_F_PARTNER_RAW_CLT_TIME = 126, /* RFC 8156 */ SD_DHCP6_OPTION_F_PROTOCOL_VERSION = 127, /* RFC 8156 */ SD_DHCP6_OPTION_F_KEEPALIVE_TIME = 128, /* RFC 8156 */ SD_DHCP6_OPTION_F_RECONFIGURE_DATA = 129, /* RFC 8156 */ SD_DHCP6_OPTION_F_RELATIONSHIP_NAME = 130, /* RFC 8156 */ SD_DHCP6_OPTION_F_SERVER_FLAGS = 131, /* RFC 8156 */ SD_DHCP6_OPTION_F_SERVER_STATE = 132, /* RFC 8156 */ SD_DHCP6_OPTION_F_START_TIME_OF_STATE = 133, /* RFC 8156 */ SD_DHCP6_OPTION_F_STATE_EXPIRATION_TIME = 134, /* RFC 8156 */ SD_DHCP6_OPTION_RELAY_PORT = 135, /* RFC 8357 */ SD_DHCP6_OPTION_V6_SZTP_REDIRECT = 136, /* RFC 8572 */ SD_DHCP6_OPTION_S46_BIND_IPV6_PREFIX = 137, /* RFC 8539 */ SD_DHCP6_OPTION_IA_LL = 138, /* RFC 8947 */ SD_DHCP6_OPTION_LLADDR = 139, /* RFC 8947 */ SD_DHCP6_OPTION_SLAP_QUAD = 140, /* RFC 8948 */ SD_DHCP6_OPTION_V6_DOTS_RI = 141, /* RFC 8973 */ SD_DHCP6_OPTION_V6_DOTS_ADDRESS = 142, /* RFC 8973 */ SD_DHCP6_OPTION_IPV6_ADDRESS_ANDSF = 143 /* RFC 6153 */ /* option codes 144-65535 are unassigned */ }; typedef struct sd_dhcp6_client sd_dhcp6_client; typedef void (*sd_dhcp6_client_callback_t)(sd_dhcp6_client *client, int event, void *userdata); int sd_dhcp6_client_set_callback( sd_dhcp6_client *client, sd_dhcp6_client_callback_t cb, void *userdata); int sd_dhcp6_client_set_ifindex( sd_dhcp6_client *client, int interface_index); int sd_dhcp6_client_set_ifname( sd_dhcp6_client *client, const char *interface_name); int sd_dhcp6_client_get_ifname(sd_dhcp6_client *client, const char **ret); int sd_dhcp6_client_set_local_address( sd_dhcp6_client *client, const struct in6_addr *local_address); int sd_dhcp6_client_set_mac( sd_dhcp6_client *client, const uint8_t *addr, size_t addr_len, uint16_t arp_type); int sd_dhcp6_client_set_duid( sd_dhcp6_client *client, uint16_t duid_type, const void *duid, size_t duid_len); int sd_dhcp6_client_set_duid_llt( sd_dhcp6_client *client, uint64_t llt_time); int sd_dhcp6_client_set_iaid( sd_dhcp6_client *client, uint32_t iaid); int sd_dhcp6_client_get_iaid( sd_dhcp6_client *client, uint32_t *iaid); int sd_dhcp6_client_duid_as_string( sd_dhcp6_client *client, char **duid); int sd_dhcp6_client_set_fqdn( sd_dhcp6_client *client, const char *fqdn); int sd_dhcp6_client_set_information_request( sd_dhcp6_client *client, int enabled); int sd_dhcp6_client_get_information_request( sd_dhcp6_client *client, int *enabled); int sd_dhcp6_client_set_request_option( sd_dhcp6_client *client, uint16_t option); int sd_dhcp6_client_set_request_mud_url( sd_dhcp6_client *client, const char *mudurl); int sd_dhcp6_client_set_request_user_class( sd_dhcp6_client *client, char * const *user_class); int sd_dhcp6_client_set_request_vendor_class( sd_dhcp6_client *client, char * const *vendor_class); int sd_dhcp6_client_set_prefix_delegation_hint( sd_dhcp6_client *client, uint8_t prefixlen, const struct in6_addr *pd_prefix); int sd_dhcp6_client_get_prefix_delegation(sd_dhcp6_client *client, int *delegation); int sd_dhcp6_client_set_prefix_delegation(sd_dhcp6_client *client, int delegation); int sd_dhcp6_client_get_address_request(sd_dhcp6_client *client, int *request); int sd_dhcp6_client_set_address_request(sd_dhcp6_client *client, int request); int sd_dhcp6_client_add_vendor_option(sd_dhcp6_client *client, sd_dhcp6_option *v); int sd_dhcp6_client_set_rapid_commit(sd_dhcp6_client *client, int enable); int sd_dhcp6_client_set_send_release(sd_dhcp6_client *client, int enable); int sd_dhcp6_client_get_lease( sd_dhcp6_client *client, sd_dhcp6_lease **ret); int sd_dhcp6_client_add_option(sd_dhcp6_client *client, sd_dhcp6_option *v); int sd_dhcp6_client_stop(sd_dhcp6_client *client); int sd_dhcp6_client_start(sd_dhcp6_client *client); int sd_dhcp6_client_is_running(sd_dhcp6_client *client); int sd_dhcp6_client_attach_event( sd_dhcp6_client *client, sd_event *event, int64_t priority); int sd_dhcp6_client_detach_event(sd_dhcp6_client *client); sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client); int sd_dhcp6_client_attach_device(sd_dhcp6_client *client, sd_device *dev); sd_dhcp6_client *sd_dhcp6_client_ref(sd_dhcp6_client *client); sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client); int sd_dhcp6_client_new(sd_dhcp6_client **ret); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp6_client, sd_dhcp6_client_unref); _SD_END_DECLARATIONS; #endif
16,236
54.227891
100
h
null
systemd-main/src/systemd/sd-dhcp6-lease.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddhcp6leasehfoo #define foosddhcp6leasehfoo /*** Copyright © 2014-2015 Intel Corporation. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <netinet/in.h> #include <sys/types.h> #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_dhcp6_lease sd_dhcp6_lease; int sd_dhcp6_lease_get_timestamp(sd_dhcp6_lease *lease, clockid_t clock, uint64_t *ret); int sd_dhcp6_lease_get_server_address(sd_dhcp6_lease *lease, struct in6_addr *ret); void sd_dhcp6_lease_reset_address_iter(sd_dhcp6_lease *lease); int sd_dhcp6_lease_get_address(sd_dhcp6_lease *lease, struct in6_addr *addr, uint32_t *lifetime_preferred, uint32_t *lifetime_valid); void sd_dhcp6_lease_reset_pd_prefix_iter(sd_dhcp6_lease *lease); int sd_dhcp6_lease_get_pd(sd_dhcp6_lease *lease, struct in6_addr *prefix, uint8_t *prefix_len, uint32_t *lifetime_preferred, uint32_t *lifetime_valid); int sd_dhcp6_lease_get_dns(sd_dhcp6_lease *lease, const struct in6_addr **ret); int sd_dhcp6_lease_get_domains(sd_dhcp6_lease *lease, char ***ret); int sd_dhcp6_lease_get_ntp_addrs(sd_dhcp6_lease *lease, const struct in6_addr **ret); int sd_dhcp6_lease_get_ntp_fqdn(sd_dhcp6_lease *lease, char ***ret); int sd_dhcp6_lease_get_fqdn(sd_dhcp6_lease *lease, const char **ret); int sd_dhcp6_lease_get_captive_portal(sd_dhcp6_lease *lease, const char **ret); sd_dhcp6_lease *sd_dhcp6_lease_ref(sd_dhcp6_lease *lease); sd_dhcp6_lease *sd_dhcp6_lease_unref(sd_dhcp6_lease *lease); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp6_lease, sd_dhcp6_lease_unref); _SD_END_DECLARATIONS; #endif
2,425
38.770492
88
h
null
systemd-main/src/systemd/sd-dhcp6-option.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosddhcp6optionhfoo #define foosddhcp6optionhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <sys/types.h> #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_dhcp6_option sd_dhcp6_option; int sd_dhcp6_option_new(uint16_t option, const void *data, size_t length, uint32_t enterprise_identifier, sd_dhcp6_option **ret); sd_dhcp6_option *sd_dhcp6_option_ref(sd_dhcp6_option *ra); sd_dhcp6_option *sd_dhcp6_option_unref(sd_dhcp6_option *ra); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp6_option, sd_dhcp6_option_unref); _SD_END_DECLARATIONS; #endif
1,258
32.131579
129
h
null
systemd-main/src/systemd/sd-gpt.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdgpthfoo #define foosdgpthfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include "sd-id128.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; #define SD_GPT_ROOT_ALPHA SD_ID128_MAKE(65,23,f8,ae,3e,b1,4e,2a,a0,5a,18,b6,95,ae,65,6f) #define SD_GPT_ROOT_ARC SD_ID128_MAKE(d2,7f,46,ed,29,19,4c,b8,bd,25,95,31,f3,c1,65,34) #define SD_GPT_ROOT_ARM SD_ID128_MAKE(69,da,d7,10,2c,e4,4e,3c,b1,6c,21,a1,d4,9a,be,d3) #define SD_GPT_ROOT_ARM64 SD_ID128_MAKE(b9,21,b0,45,1d,f0,41,c3,af,44,4c,6f,28,0d,3f,ae) #define SD_GPT_ROOT_IA64 SD_ID128_MAKE(99,3d,8d,3d,f8,0e,42,25,85,5a,9d,af,8e,d7,ea,97) #define SD_GPT_ROOT_LOONGARCH64 SD_ID128_MAKE(77,05,58,00,79,2c,4f,94,b3,9a,98,c9,1b,76,2b,b6) #define SD_GPT_ROOT_MIPS_LE SD_ID128_MAKE(37,c5,8c,8a,d9,13,41,56,a2,5f,48,b1,b6,4e,07,f0) #define SD_GPT_ROOT_MIPS64_LE SD_ID128_MAKE(70,0b,da,43,7a,34,45,07,b1,79,ee,b9,3d,7a,7c,a3) #define SD_GPT_ROOT_PARISC SD_ID128_MAKE(1a,ac,db,3b,54,44,41,38,bd,9e,e5,c2,23,9b,23,46) #define SD_GPT_ROOT_PPC SD_ID128_MAKE(1d,e3,f1,ef,fa,98,47,b5,8d,cd,4a,86,0a,65,4d,78) #define SD_GPT_ROOT_PPC64 SD_ID128_MAKE(91,2a,de,1d,a8,39,49,13,89,64,a1,0e,ee,08,fb,d2) #define SD_GPT_ROOT_PPC64_LE SD_ID128_MAKE(c3,1c,45,e6,3f,39,41,2e,80,fb,48,09,c4,98,05,99) #define SD_GPT_ROOT_RISCV32 SD_ID128_MAKE(60,d5,a7,fe,8e,7d,43,5c,b7,14,3d,d8,16,21,44,e1) #define SD_GPT_ROOT_RISCV64 SD_ID128_MAKE(72,ec,70,a6,cf,74,40,e6,bd,49,4b,da,08,e8,f2,24) #define SD_GPT_ROOT_S390 SD_ID128_MAKE(08,a7,ac,ea,62,4c,4a,20,91,e8,6e,0f,a6,7d,23,f9) #define SD_GPT_ROOT_S390X SD_ID128_MAKE(5e,ea,d9,a9,fe,09,4a,1e,a1,d7,52,0d,00,53,13,06) #define SD_GPT_ROOT_TILEGX SD_ID128_MAKE(c5,0c,dd,70,38,62,4c,c3,90,e1,80,9a,8c,93,ee,2c) #define SD_GPT_ROOT_X86 SD_ID128_MAKE(44,47,95,40,f2,97,41,b2,9a,f7,d1,31,d5,f0,45,8a) #define SD_GPT_ROOT_X86_64 SD_ID128_MAKE(4f,68,bc,e3,e8,cd,4d,b1,96,e7,fb,ca,f9,84,b7,09) #define SD_GPT_USR_ALPHA SD_ID128_MAKE(e1,8c,f0,8c,33,ec,4c,0d,82,46,c6,c6,fb,3d,a0,24) #define SD_GPT_USR_ARC SD_ID128_MAKE(79,78,a6,83,63,16,49,22,bb,ee,38,bf,f5,a2,fe,cc) #define SD_GPT_USR_ARM SD_ID128_MAKE(7d,03,59,a3,02,b3,4f,0a,86,5c,65,44,03,e7,06,25) #define SD_GPT_USR_ARM64 SD_ID128_MAKE(b0,e0,10,50,ee,5f,43,90,94,9a,91,01,b1,71,04,e9) #define SD_GPT_USR_IA64 SD_ID128_MAKE(43,01,d2,a6,4e,3b,4b,2a,bb,94,9e,0b,2c,42,25,ea) #define SD_GPT_USR_LOONGARCH64 SD_ID128_MAKE(e6,11,c7,02,57,5c,4c,be,9a,46,43,4f,a0,bf,7e,3f) #define SD_GPT_USR_MIPS_LE SD_ID128_MAKE(0f,48,68,e9,99,52,47,06,97,9f,3e,d3,a4,73,e9,47) #define SD_GPT_USR_MIPS64_LE SD_ID128_MAKE(c9,7c,1f,32,ba,06,40,b4,9f,22,23,60,61,b0,8a,a8) #define SD_GPT_USR_PARISC SD_ID128_MAKE(dc,4a,44,80,69,17,42,62,a4,ec,db,93,84,94,9f,25) #define SD_GPT_USR_PPC SD_ID128_MAKE(7d,14,fe,c5,cc,71,41,5d,9d,6c,06,bf,0b,3c,3e,af) #define SD_GPT_USR_PPC64 SD_ID128_MAKE(2c,97,39,e2,f0,68,46,b3,9f,d0,01,c5,a9,af,bc,ca) #define SD_GPT_USR_PPC64_LE SD_ID128_MAKE(15,bb,03,af,77,e7,4d,4a,b1,2b,c0,d0,84,f7,49,1c) #define SD_GPT_USR_RISCV32 SD_ID128_MAKE(b9,33,fb,22,5c,3f,4f,91,af,90,e2,bb,0f,a5,07,02) #define SD_GPT_USR_RISCV64 SD_ID128_MAKE(be,ae,c3,4b,84,42,43,9b,a4,0b,98,43,81,ed,09,7d) #define SD_GPT_USR_S390 SD_ID128_MAKE(cd,0f,86,9b,d0,fb,4c,a0,b1,41,9e,a8,7c,c7,8d,66) #define SD_GPT_USR_S390X SD_ID128_MAKE(8a,4f,57,70,50,aa,4e,d3,87,4a,99,b7,10,db,6f,ea) #define SD_GPT_USR_TILEGX SD_ID128_MAKE(55,49,70,29,c7,c1,44,cc,aa,39,81,5e,d1,55,86,30) #define SD_GPT_USR_X86 SD_ID128_MAKE(75,25,0d,76,8c,c6,45,8e,bd,66,bd,47,cc,81,a8,12) #define SD_GPT_USR_X86_64 SD_ID128_MAKE(84,84,68,0c,95,21,48,c6,9c,11,b0,72,06,56,f6,9e) /* Verity partitions for the root partitions above (we only define them for the root and /usr partitions, * because only they are commonly read-only and hence suitable for verity). */ #define SD_GPT_ROOT_ALPHA_VERITY SD_ID128_MAKE(fc,56,d9,e9,e6,e5,4c,06,be,32,e7,44,07,ce,09,a5) #define SD_GPT_ROOT_ARC_VERITY SD_ID128_MAKE(24,b2,d9,75,0f,97,45,21,af,a1,cd,53,1e,42,1b,8d) #define SD_GPT_ROOT_ARM_VERITY SD_ID128_MAKE(73,86,cd,f2,20,3c,47,a9,a4,98,f2,ec,ce,45,a2,d6) #define SD_GPT_ROOT_ARM64_VERITY SD_ID128_MAKE(df,33,00,ce,d6,9f,4c,92,97,8c,9b,fb,0f,38,d8,20) #define SD_GPT_ROOT_IA64_VERITY SD_ID128_MAKE(86,ed,10,d5,b6,07,45,bb,89,57,d3,50,f2,3d,05,71) #define SD_GPT_ROOT_LOONGARCH64_VERITY SD_ID128_MAKE(f3,39,3b,22,e9,af,46,13,a9,48,9d,3b,fb,d0,c5,35) #define SD_GPT_ROOT_MIPS_LE_VERITY SD_ID128_MAKE(d7,d1,50,d2,2a,04,4a,33,8f,12,16,65,12,05,ff,7b) #define SD_GPT_ROOT_MIPS64_LE_VERITY SD_ID128_MAKE(16,b4,17,f8,3e,06,4f,57,8d,d2,9b,52,32,f4,1a,a6) #define SD_GPT_ROOT_PARISC_VERITY SD_ID128_MAKE(d2,12,a4,30,fb,c5,49,f9,a9,83,a7,fe,ef,2b,8d,0e) #define SD_GPT_ROOT_PPC64_LE_VERITY SD_ID128_MAKE(90,6b,d9,44,45,89,4a,ae,a4,e4,dd,98,39,17,44,6a) #define SD_GPT_ROOT_PPC64_VERITY SD_ID128_MAKE(92,25,a9,a3,3c,19,4d,89,b4,f6,ee,ff,88,f1,76,31) #define SD_GPT_ROOT_PPC_VERITY SD_ID128_MAKE(98,cf,e6,49,15,88,46,dc,b2,f0,ad,d1,47,42,49,25) #define SD_GPT_ROOT_RISCV32_VERITY SD_ID128_MAKE(ae,02,53,be,11,67,40,07,ac,68,43,92,6c,14,c5,de) #define SD_GPT_ROOT_RISCV64_VERITY SD_ID128_MAKE(b6,ed,55,82,44,0b,42,09,b8,da,5f,f7,c4,19,ea,3d) #define SD_GPT_ROOT_S390_VERITY SD_ID128_MAKE(7a,c6,3b,47,b2,5c,46,3b,8d,f8,b4,a9,4e,6c,90,e1) #define SD_GPT_ROOT_S390X_VERITY SD_ID128_MAKE(b3,25,bf,be,c7,be,4a,b8,83,57,13,9e,65,2d,2f,6b) #define SD_GPT_ROOT_TILEGX_VERITY SD_ID128_MAKE(96,60,61,ec,28,e4,4b,2e,b4,a5,1f,0a,82,5a,1d,84) #define SD_GPT_ROOT_X86_64_VERITY SD_ID128_MAKE(2c,73,57,ed,eb,d2,46,d9,ae,c1,23,d4,37,ec,2b,f5) #define SD_GPT_ROOT_X86_VERITY SD_ID128_MAKE(d1,3c,5d,3b,b5,d1,42,2a,b2,9f,94,54,fd,c8,9d,76) #define SD_GPT_USR_ALPHA_VERITY SD_ID128_MAKE(8c,ce,0d,25,c0,d0,4a,44,bd,87,46,33,1b,f1,df,67) #define SD_GPT_USR_ARC_VERITY SD_ID128_MAKE(fc,a0,59,8c,d8,80,45,91,8c,16,4e,da,05,c7,34,7c) #define SD_GPT_USR_ARM_VERITY SD_ID128_MAKE(c2,15,d7,51,7b,cd,46,49,be,90,66,27,49,0a,4c,05) #define SD_GPT_USR_ARM64_VERITY SD_ID128_MAKE(6e,11,a4,e7,fb,ca,4d,ed,b9,e9,e1,a5,12,bb,66,4e) #define SD_GPT_USR_IA64_VERITY SD_ID128_MAKE(6a,49,1e,03,3b,e7,45,45,8e,38,83,32,0e,0e,a8,80) #define SD_GPT_USR_LOONGARCH64_VERITY SD_ID128_MAKE(f4,6b,2c,26,59,ae,48,f0,91,06,c5,0e,d4,7f,67,3d) #define SD_GPT_USR_MIPS_LE_VERITY SD_ID128_MAKE(46,b9,8d,8d,b5,5c,4e,8f,aa,b3,37,fc,a7,f8,07,52) #define SD_GPT_USR_MIPS64_LE_VERITY SD_ID128_MAKE(3c,3d,61,fe,b5,f3,41,4d,bb,71,87,39,a6,94,a4,ef) #define SD_GPT_USR_PARISC_VERITY SD_ID128_MAKE(58,43,d6,18,ec,37,48,d7,9f,12,ce,a8,e0,87,68,b2) #define SD_GPT_USR_PPC64_LE_VERITY SD_ID128_MAKE(ee,2b,99,83,21,e8,41,53,86,d9,b6,90,1a,54,d1,ce) #define SD_GPT_USR_PPC64_VERITY SD_ID128_MAKE(bd,b5,28,a5,a2,59,47,5f,a8,7d,da,53,fa,73,6a,07) #define SD_GPT_USR_PPC_VERITY SD_ID128_MAKE(df,76,5d,00,27,0e,49,e5,bc,75,f4,7b,b2,11,8b,09) #define SD_GPT_USR_RISCV32_VERITY SD_ID128_MAKE(cb,1e,e4,e3,8c,d0,41,36,a0,a4,aa,61,a3,2e,87,30) #define SD_GPT_USR_RISCV64_VERITY SD_ID128_MAKE(8f,10,56,be,9b,05,47,c4,81,d6,be,53,12,8e,5b,54) #define SD_GPT_USR_S390_VERITY SD_ID128_MAKE(b6,63,c6,18,e7,bc,4d,6d,90,aa,11,b7,56,bb,17,97) #define SD_GPT_USR_S390X_VERITY SD_ID128_MAKE(31,74,1c,c4,1a,2a,41,11,a5,81,e0,0b,44,7d,2d,06) #define SD_GPT_USR_TILEGX_VERITY SD_ID128_MAKE(2f,b4,bf,56,07,fa,42,da,81,32,6b,13,9f,20,26,ae) #define SD_GPT_USR_X86_64_VERITY SD_ID128_MAKE(77,ff,5f,63,e7,b6,46,33,ac,f4,15,65,b8,64,c0,e6) #define SD_GPT_USR_X86_VERITY SD_ID128_MAKE(8f,46,1b,0d,14,ee,4e,81,9a,a9,04,9b,6f,b9,7a,bd) /* PKCS#7 Signatures for the Verity Root Hashes */ #define SD_GPT_ROOT_ALPHA_VERITY_SIG SD_ID128_MAKE(d4,64,95,b7,a0,53,41,4f,80,f7,70,0c,99,92,1e,f8) #define SD_GPT_ROOT_ARC_VERITY_SIG SD_ID128_MAKE(14,3a,70,ba,cb,d3,4f,06,91,9f,6c,05,68,3a,78,bc) #define SD_GPT_ROOT_ARM_VERITY_SIG SD_ID128_MAKE(42,b0,45,5f,eb,11,49,1d,98,d3,56,14,5b,a9,d0,37) #define SD_GPT_ROOT_ARM64_VERITY_SIG SD_ID128_MAKE(6d,b6,9d,e6,29,f4,47,58,a7,a5,96,21,90,f0,0c,e3) #define SD_GPT_ROOT_IA64_VERITY_SIG SD_ID128_MAKE(e9,8b,36,ee,32,ba,48,82,9b,12,0c,e1,46,55,f4,6a) #define SD_GPT_ROOT_LOONGARCH64_VERITY_SIG SD_ID128_MAKE(5a,fb,67,eb,ec,c8,4f,85,ae,8e,ac,1e,7c,50,e7,d0) #define SD_GPT_ROOT_MIPS_LE_VERITY_SIG SD_ID128_MAKE(c9,19,cc,1f,44,56,4e,ff,91,8c,f7,5e,94,52,5c,a5) #define SD_GPT_ROOT_MIPS64_LE_VERITY_SIG SD_ID128_MAKE(90,4e,58,ef,5c,65,4a,31,9c,57,6a,f5,fc,7c,5d,e7) #define SD_GPT_ROOT_PARISC_VERITY_SIG SD_ID128_MAKE(15,de,61,70,65,d3,43,1c,91,6e,b0,dc,d8,39,3f,25) #define SD_GPT_ROOT_PPC64_LE_VERITY_SIG SD_ID128_MAKE(d4,a2,36,e7,e8,73,4c,07,bf,1d,bf,6c,f7,f1,c3,c6) #define SD_GPT_ROOT_PPC64_VERITY_SIG SD_ID128_MAKE(f5,e2,c2,0c,45,b2,4f,fa,bc,e9,2a,60,73,7e,1a,af) #define SD_GPT_ROOT_PPC_VERITY_SIG SD_ID128_MAKE(1b,31,b5,aa,ad,d9,46,3a,b2,ed,bd,46,7f,c8,57,e7) #define SD_GPT_ROOT_RISCV32_VERITY_SIG SD_ID128_MAKE(3a,11,2a,75,87,29,43,80,b4,cf,76,4d,79,93,44,48) #define SD_GPT_ROOT_RISCV64_VERITY_SIG SD_ID128_MAKE(ef,e0,f0,87,ea,8d,44,69,82,1a,4c,2a,96,a8,38,6a) #define SD_GPT_ROOT_S390_VERITY_SIG SD_ID128_MAKE(34,82,38,8e,42,54,43,5a,a2,41,76,6a,06,5f,99,60) #define SD_GPT_ROOT_S390X_VERITY_SIG SD_ID128_MAKE(c8,01,87,a5,73,a3,49,1a,90,1a,01,7c,3f,a9,53,e9) #define SD_GPT_ROOT_TILEGX_VERITY_SIG SD_ID128_MAKE(b3,67,14,39,97,b0,4a,53,90,f7,2d,5a,8f,3a,d4,7b) #define SD_GPT_ROOT_X86_64_VERITY_SIG SD_ID128_MAKE(41,09,2b,05,9f,c8,45,23,99,4f,2d,ef,04,08,b1,76) #define SD_GPT_ROOT_X86_VERITY_SIG SD_ID128_MAKE(59,96,fc,05,10,9c,48,de,80,8b,23,fa,08,30,b6,76) #define SD_GPT_USR_ALPHA_VERITY_SIG SD_ID128_MAKE(5c,6e,1c,76,07,6a,45,7a,a0,fe,f3,b4,cd,21,ce,6e) #define SD_GPT_USR_ARC_VERITY_SIG SD_ID128_MAKE(94,f9,a9,a1,99,71,42,7a,a4,00,50,cb,29,7f,0f,35) #define SD_GPT_USR_ARM_VERITY_SIG SD_ID128_MAKE(d7,ff,81,2f,37,d1,49,02,a8,10,d7,6b,a5,7b,97,5a) #define SD_GPT_USR_ARM64_VERITY_SIG SD_ID128_MAKE(c2,3c,e4,ff,44,bd,4b,00,b2,d4,b4,1b,34,19,e0,2a) #define SD_GPT_USR_IA64_VERITY_SIG SD_ID128_MAKE(8d,e5,8b,c2,2a,43,46,0d,b1,4e,a7,6e,4a,17,b4,7f) #define SD_GPT_USR_LOONGARCH64_VERITY_SIG SD_ID128_MAKE(b0,24,f3,15,d3,30,44,4c,84,61,44,bb,de,52,4e,99) #define SD_GPT_USR_MIPS_LE_VERITY_SIG SD_ID128_MAKE(3e,23,ca,0b,a4,bc,4b,4e,80,87,5a,b6,a2,6a,a8,a9) #define SD_GPT_USR_MIPS64_LE_VERITY_SIG SD_ID128_MAKE(f2,c2,c7,ee,ad,cc,43,51,b5,c6,ee,98,16,b6,6e,16) #define SD_GPT_USR_PARISC_VERITY_SIG SD_ID128_MAKE(45,0d,d7,d1,32,24,45,ec,9c,f2,a4,3a,34,6d,71,ee) #define SD_GPT_USR_PPC64_LE_VERITY_SIG SD_ID128_MAKE(c8,bf,bd,1e,26,8e,45,21,8b,ba,bf,31,4c,39,95,57) #define SD_GPT_USR_PPC64_VERITY_SIG SD_ID128_MAKE(0b,88,88,63,d7,f8,4d,9e,97,66,23,9f,ce,4d,58,af) #define SD_GPT_USR_PPC_VERITY_SIG SD_ID128_MAKE(70,07,89,1d,d3,71,4a,80,86,a4,5c,b8,75,b9,30,2e) #define SD_GPT_USR_RISCV32_VERITY_SIG SD_ID128_MAKE(c3,83,6a,13,31,37,45,ba,b5,83,b1,6c,50,fe,5e,b4) #define SD_GPT_USR_RISCV64_VERITY_SIG SD_ID128_MAKE(d2,f9,00,0a,7a,18,45,3f,b5,cd,4d,32,f7,7a,7b,32) #define SD_GPT_USR_S390_VERITY_SIG SD_ID128_MAKE(17,44,0e,4f,a8,d0,46,7f,a4,6e,39,12,ae,6e,f2,c5) #define SD_GPT_USR_S390X_VERITY_SIG SD_ID128_MAKE(3f,32,48,16,66,7b,46,ae,86,ee,9b,0c,0c,6c,11,b4) #define SD_GPT_USR_TILEGX_VERITY_SIG SD_ID128_MAKE(4e,de,75,e2,6c,cc,4c,c8,b9,c7,70,33,4b,08,75,10) #define SD_GPT_USR_X86_64_VERITY_SIG SD_ID128_MAKE(e7,bb,33,fb,06,cf,4e,81,82,73,e5,43,b4,13,e2,e2) #define SD_GPT_USR_X86_VERITY_SIG SD_ID128_MAKE(97,4a,71,c0,de,41,43,c3,be,5d,5c,5c,cd,1a,d2,c0) #define SD_GPT_ESP SD_ID128_MAKE(c1,2a,73,28,f8,1f,11,d2,ba,4b,00,a0,c9,3e,c9,3b) #define SD_GPT_ESP_STR SD_ID128_MAKE_UUID_STR(c1,2a,73,28,f8,1f,11,d2,ba,4b,00,a0,c9,3e,c9,3b) #define SD_GPT_XBOOTLDR SD_ID128_MAKE(bc,13,c2,ff,59,e6,42,62,a3,52,b2,75,fd,6f,71,72) #define SD_GPT_XBOOTLDR_STR SD_ID128_MAKE_UUID_STR(bc,13,c2,ff,59,e6,42,62,a3,52,b2,75,fd,6f,71,72) #define SD_GPT_SWAP SD_ID128_MAKE(06,57,fd,6d,a4,ab,43,c4,84,e5,09,33,c8,4b,4f,4f) #define SD_GPT_SWAP_STR SD_ID128_MAKE_UUID_STR(06,57,fd,6d,a4,ab,43,c4,84,e5,09,33,c8,4b,4f,4f) #define SD_GPT_HOME SD_ID128_MAKE(93,3a,c7,e1,2e,b4,4f,13,b8,44,0e,14,e2,ae,f9,15) #define SD_GPT_HOME_STR SD_ID128_MAKE_UUID_STR(93,3a,c7,e1,2e,b4,4f,13,b8,44,0e,14,e2,ae,f9,15) #define SD_GPT_SRV SD_ID128_MAKE(3b,8f,84,25,20,e0,4f,3b,90,7f,1a,25,a7,6f,98,e8) #define SD_GPT_SRV_STR SD_ID128_MAKE_UUID_STR(3b,8f,84,25,20,e0,4f,3b,90,7f,1a,25,a7,6f,98,e8) #define SD_GPT_VAR SD_ID128_MAKE(4d,21,b0,16,b5,34,45,c2,a9,fb,5c,16,e0,91,fd,2d) #define SD_GPT_VAR_STR SD_ID128_MAKE_UUID_STR(4d,21,b0,16,b5,34,45,c2,a9,fb,5c,16,e0,91,fd,2d) #define SD_GPT_TMP SD_ID128_MAKE(7e,c6,f5,57,3b,c5,4a,ca,b2,93,16,ef,5d,f6,39,d1) #define SD_GPT_TMP_STR SD_ID128_MAKE_UUID_STR(7e,c6,f5,57,3b,c5,4a,ca,b2,93,16,ef,5d,f6,39,d1) #define SD_GPT_USER_HOME SD_ID128_MAKE(77,3f,91,ef,66,d4,49,b5,bd,83,d6,83,bf,40,ad,16) #define SD_GPT_USER_HOME_STR SD_ID128_MAKE_UUID_STR(77,3f,91,ef,66,d4,49,b5,bd,83,d6,83,bf,40,ad,16) #define SD_GPT_LINUX_GENERIC SD_ID128_MAKE(0f,c6,3d,af,84,83,47,72,8e,79,3d,69,d8,47,7d,e4) #define SD_GPT_LINUX_GENERIC_STR SD_ID128_MAKE_UUID_STR(0f,c6,3d,af,84,83,47,72,8e,79,3d,69,d8,47,7d,e4) /* Maintain same order as above */ #if defined(__alpha__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_ALPHA # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_ALPHA_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_ALPHA_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_ALPHA # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_ALPHA_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_ALPHA_VERITY_SIG #elif defined(__arc__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_ARC # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_ARC_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_ARC_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_ARC # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_ARC_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_ARC_VERITY_SIG #elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_ARM64 # define SD_GPT_ROOT_SECONDARY SD_GPT_ROOT_ARM # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_ARM64_VERITY # define SD_GPT_ROOT_SECONDARY_VERITY SD_GPT_ROOT_ARM_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_ARM64_VERITY_SIG # define SD_GPT_ROOT_SECONDARY_VERITY_SIG SD_GPT_ROOT_ARM_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_ARM64 # define SD_GPT_USR_SECONDARY SD_GPT_USR_ARM # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_ARM64_VERITY # define SD_GPT_USR_SECONDARY_VERITY SD_GPT_USR_ARM_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_ARM64_VERITY_SIG # define SD_GPT_USR_SECONDARY_VERITY_SIG SD_GPT_USR_ARM_VERITY_SIG #elif defined(__arm__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_ARM # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_ARM_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_ARM_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_ARM # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_ARM_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_ARM_VERITY_SIG #elif defined(__ia64__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_IA64 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_IA64_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_IA64_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_IA64 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_IA64_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_IA64_VERITY_SIG #elif defined(__loongarch64) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_LOONGARCH64 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_LOONGARCH64_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_LOONGARCH64_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_LOONGARCH64 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_LOONGARCH64_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_LOONGARCH64_VERITY_SIG #elif defined(__mips__) && !defined(__mips64) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_MIPS_LE # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_MIPS_LE_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_MIPS_LE_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_MIPS_LE # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_MIPS_LE_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_MIPS_LE_VERITY_SIG #elif defined(__mips64) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_MIPS64_LE # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_MIPS64_LE_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_MIPS64_LE_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_MIPS64_LE # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_MIPS64_LE_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_MIPS64_LE_VERITY_SIG #elif defined(__parisc__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_PARISC # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_PARISC_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_PARISC_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_PARISC # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_PARISC_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_PARISC_VERITY_SIG #elif defined(__powerpc__) && defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_PPC64_LE # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_PPC64_LE_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_PPC64_LE_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_PPC64_LE # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_PPC64_LE_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_PPC64_LE_VERITY_SIG #elif defined(__powerpc__) && defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_PPC64 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_PPC64_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_PPC64_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_PPC64 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_PPC64_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_PPC64_VERITY_SIG #elif defined(__powerpc__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_PPC # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_PPC_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_PPC_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_PPC # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_PPC_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_PPC_VERITY_SIG #elif defined(__riscv) && __riscv_xlen == 32 # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_RISCV32 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_RISCV32_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_RISCV32_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_RISCV32 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_RISCV32_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_RISCV32_VERITY_SIG #elif defined(__riscv) && __riscv_xlen == 64 # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_RISCV64 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_RISCV64_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_RISCV64_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_RISCV64 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_RISCV64_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_RISCV64_VERITY_SIG #elif defined(__s390__) && !defined(__s390x__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_S390 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_S390_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_S390_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_S390 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_S390_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_S390_VERITY_SIG #elif defined(__s390x__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_S390X # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_S390X_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_S390X_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_S390X # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_S390X_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_S390X_VERITY_SIG #elif defined(__tilegx__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_TILEGX # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_TILEGX_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_TILEGX_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_TILEGX # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_TILEGX_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_TILEGX_VERITY_SIG #elif defined(__x86_64__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_X86_64 # define SD_GPT_ROOT_SECONDARY SD_GPT_ROOT_X86 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_X86_64_VERITY # define SD_GPT_ROOT_SECONDARY_VERITY SD_GPT_ROOT_X86_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_X86_64_VERITY_SIG # define SD_GPT_ROOT_SECONDARY_VERITY_SIG SD_GPT_ROOT_X86_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_X86_64 # define SD_GPT_USR_SECONDARY SD_GPT_USR_X86 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_X86_64_VERITY # define SD_GPT_USR_SECONDARY_VERITY SD_GPT_USR_X86_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_X86_64_VERITY_SIG # define SD_GPT_USR_SECONDARY_VERITY_SIG SD_GPT_USR_X86_VERITY_SIG #elif defined(__i386__) # define SD_GPT_ROOT_NATIVE SD_GPT_ROOT_X86 # define SD_GPT_ROOT_NATIVE_VERITY SD_GPT_ROOT_X86_VERITY # define SD_GPT_ROOT_NATIVE_VERITY_SIG SD_GPT_ROOT_X86_VERITY_SIG # define SD_GPT_USR_NATIVE SD_GPT_USR_X86 # define SD_GPT_USR_NATIVE_VERITY SD_GPT_USR_X86_VERITY # define SD_GPT_USR_NATIVE_VERITY_SIG SD_GPT_USR_X86_VERITY_SIG #endif /* Partition attributes defined by the UEFI specification. */ #define SD_GPT_FLAG_REQUIRED_PARTITION (UINT64_C(1) << 0) #define SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL (UINT64_C(1) << 1) #define SD_GPT_FLAG_LEGACY_BIOS_BOOTABLE (UINT64_C(1) << 2) /* Flags we recognize on the root, usr, xbootldr, swap, home, srv, var, tmp partitions when doing * auto-discovery. * * The first two happen to be identical to what Microsoft defines for its own Basic Data Partitions * in "winioctl.h": GPT_BASIC_DATA_ATTRIBUTE_READ_ONLY, GPT_BASIC_DATA_ATTRIBUTE_NO_DRIVE_LETTER. */ #define SD_GPT_FLAG_READ_ONLY (UINT64_C(1) << 60) #define SD_GPT_FLAG_NO_AUTO (UINT64_C(1) << 63) #define SD_GPT_FLAG_GROWFS (UINT64_C(1) << 59) _SD_END_DECLARATIONS; #endif
23,630
67.895044
114
h
null
systemd-main/src/systemd/sd-hwdb.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdhwdbhfoo #define foosdhwdbhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_hwdb sd_hwdb; sd_hwdb *sd_hwdb_ref(sd_hwdb *hwdb); sd_hwdb *sd_hwdb_unref(sd_hwdb *hwdb); int sd_hwdb_new(sd_hwdb **ret); int sd_hwdb_new_from_path(const char *path, sd_hwdb **ret); int sd_hwdb_get(sd_hwdb *hwdb, const char *modalias, const char *key, const char **value); int sd_hwdb_seek(sd_hwdb *hwdb, const char *modalias); int sd_hwdb_enumerate(sd_hwdb *hwdb, const char **key, const char **value); /* the inverse condition avoids ambiguity of dangling 'else' after the macro */ #define SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value) \ if (sd_hwdb_seek(hwdb, modalias) < 0) { } \ else while (sd_hwdb_enumerate(hwdb, &(key), &(value)) > 0) _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_hwdb, sd_hwdb_unref); _SD_END_DECLARATIONS; #endif
1,605
33.170213
90
h
null
systemd-main/src/systemd/sd-ipv4acd.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdipv4acdfoo #define foosdipv4acdfoo /*** Copyright © 2014 Axis Communications AB. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <net/ethernet.h> #include <netinet/in.h> #include <stdbool.h> #include "sd-event.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; enum { SD_IPV4ACD_EVENT_STOP = 0, SD_IPV4ACD_EVENT_BIND = 1, SD_IPV4ACD_EVENT_CONFLICT = 2 }; typedef struct sd_ipv4acd sd_ipv4acd; typedef void (*sd_ipv4acd_callback_t)(sd_ipv4acd *acd, int event, void *userdata); typedef int (*sd_ipv4acd_check_mac_callback_t)(sd_ipv4acd *acd, const struct ether_addr *mac, void *userdata); int sd_ipv4acd_detach_event(sd_ipv4acd *acd); int sd_ipv4acd_attach_event(sd_ipv4acd *acd, sd_event *event, int64_t priority); int sd_ipv4acd_get_address(sd_ipv4acd *acd, struct in_addr *address); int sd_ipv4acd_set_callback(sd_ipv4acd *acd, sd_ipv4acd_callback_t cb, void *userdata); int sd_ipv4acd_set_check_mac_callback(sd_ipv4acd *acd, sd_ipv4acd_check_mac_callback_t cb, void *userdata); int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr); int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int interface_index); int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd); int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *interface_name); int sd_ipv4acd_get_ifname(sd_ipv4acd *acd, const char **ret); int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address); int sd_ipv4acd_is_running(sd_ipv4acd *acd); __extension__ int sd_ipv4acd_start(sd_ipv4acd *acd, bool reset_conflicts); int sd_ipv4acd_stop(sd_ipv4acd *acd); sd_ipv4acd *sd_ipv4acd_ref(sd_ipv4acd *acd); sd_ipv4acd *sd_ipv4acd_unref(sd_ipv4acd *acd); int sd_ipv4acd_new(sd_ipv4acd **ret); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_ipv4acd, sd_ipv4acd_unref); _SD_END_DECLARATIONS; #endif
2,503
38.125
110
h
null
systemd-main/src/systemd/sd-ipv4ll.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdipv4llfoo #define foosdipv4llfoo /*** Copyright © 2014 Axis Communications AB. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <net/ethernet.h> #include <netinet/in.h> #include "sd-event.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; enum { SD_IPV4LL_EVENT_STOP = 0, SD_IPV4LL_EVENT_BIND = 1, SD_IPV4LL_EVENT_CONFLICT = 2 }; typedef struct sd_ipv4ll sd_ipv4ll; typedef void (*sd_ipv4ll_callback_t)(sd_ipv4ll *ll, int event, void *userdata); typedef int (*sd_ipv4ll_check_mac_callback_t)(sd_ipv4ll *ll, const struct ether_addr *mac, void *userdata); int sd_ipv4ll_detach_event(sd_ipv4ll *ll); int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int64_t priority); int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address); int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_callback_t cb, void *userdata); int sd_ipv4ll_set_check_mac_callback(sd_ipv4ll *ll, sd_ipv4ll_check_mac_callback_t cb, void *userdata); int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr); int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int interface_index); int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll); int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *interface_name); int sd_ipv4ll_get_ifname(sd_ipv4ll *ll, const char **ret); int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address); int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed); int sd_ipv4ll_is_running(sd_ipv4ll *ll); int sd_ipv4ll_restart(sd_ipv4ll *ll); int sd_ipv4ll_start(sd_ipv4ll *ll); int sd_ipv4ll_stop(sd_ipv4ll *ll); sd_ipv4ll *sd_ipv4ll_ref(sd_ipv4ll *ll); sd_ipv4ll *sd_ipv4ll_unref(sd_ipv4ll *ll); int sd_ipv4ll_new(sd_ipv4ll **ret); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_ipv4ll, sd_ipv4ll_unref); _SD_END_DECLARATIONS; #endif
2,481
36.606061
107
h
null
systemd-main/src/systemd/sd-lldp-rx.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdlldprxhfoo #define foosdlldprxhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <errno.h> #include <inttypes.h> #include <net/ethernet.h> #include <sys/types.h> #include "sd-event.h" #include "sd-lldp.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_lldp_rx sd_lldp_rx; typedef struct sd_lldp_neighbor sd_lldp_neighbor; __extension__ typedef enum sd_lldp_rx_event_t { SD_LLDP_RX_EVENT_ADDED, SD_LLDP_RX_EVENT_REMOVED, SD_LLDP_RX_EVENT_UPDATED, SD_LLDP_RX_EVENT_REFRESHED, _SD_LLDP_RX_EVENT_MAX, _SD_LLDP_RX_EVENT_INVALID = -EINVAL, _SD_ENUM_FORCE_S64(LLDP_RX_EVENT) } sd_lldp_rx_event_t; typedef void (*sd_lldp_rx_callback_t)(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_lldp_neighbor *n, void *userdata); int sd_lldp_rx_new(sd_lldp_rx **ret); sd_lldp_rx *sd_lldp_rx_ref(sd_lldp_rx *lldp_rx); sd_lldp_rx *sd_lldp_rx_unref(sd_lldp_rx *lldp_rx); int sd_lldp_rx_start(sd_lldp_rx *lldp_rx); int sd_lldp_rx_stop(sd_lldp_rx *lldp_rx); int sd_lldp_rx_is_running(sd_lldp_rx *lldp_rx); int sd_lldp_rx_attach_event(sd_lldp_rx *lldp_rx, sd_event *event, int64_t priority); int sd_lldp_rx_detach_event(sd_lldp_rx *lldp_rx); sd_event *sd_lldp_rx_get_event(sd_lldp_rx *lldp_rx); int sd_lldp_rx_set_callback(sd_lldp_rx *lldp_rx, sd_lldp_rx_callback_t cb, void *userdata); int sd_lldp_rx_set_ifindex(sd_lldp_rx *lldp_rx, int ifindex); int sd_lldp_rx_set_ifname(sd_lldp_rx *lldp_rx, const char *ifname); int sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx, const char **ret); /* Controls how much and what to store in the neighbors database */ int sd_lldp_rx_set_neighbors_max(sd_lldp_rx *lldp_rx, uint64_t n); int sd_lldp_rx_match_capabilities(sd_lldp_rx *lldp_rx, uint16_t mask); int sd_lldp_rx_set_filter_address(sd_lldp_rx *lldp_rx, const struct ether_addr *address); int sd_lldp_rx_get_neighbors(sd_lldp_rx *lldp_rx, sd_lldp_neighbor ***neighbors); int sd_lldp_neighbor_from_raw(sd_lldp_neighbor **ret, const void *raw, size_t raw_size); sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n); sd_lldp_neighbor *sd_lldp_neighbor_unref(sd_lldp_neighbor *n); /* Access to LLDP frame metadata */ int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor *n, struct ether_addr* address); int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor *n, struct ether_addr* address); int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor *n, clockid_t clock, uint64_t *ret); int sd_lldp_neighbor_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size); /* High-level, direct, parsed out field access. These fields exist at most once, hence may be queried directly. */ int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size); int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor *n, const char **ret); int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size); int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor *n, const char **ret); int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor *n, uint16_t *ret_sec); int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor *n, const char **ret); int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor *n, const char **ret); int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor *n, const char **ret); int sd_lldp_neighbor_get_mud_url(sd_lldp_neighbor *n, const char **ret); int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor *n, uint16_t *ret); int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor *n, uint16_t *ret); /* Low-level, iterative TLV access. This is for everything else, it iteratively goes through all available TLVs * (including the ones covered with the calls above), and allows multiple TLVs for the same fields. */ int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor *n); int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor *n); int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor *n, uint8_t *type); int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor *n, uint8_t type); int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t *subtype); int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t subtype); int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_rx, sd_lldp_rx_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_neighbor, sd_lldp_neighbor_unref); _SD_END_DECLARATIONS; #endif
5,187
46.163636
122
h
null
systemd-main/src/systemd/sd-lldp-tx.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdlldptxhfoo #define foosdlldptxhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <errno.h> #include <inttypes.h> #include <net/ethernet.h> #include <sys/types.h> #include "sd-event.h" #include "sd-lldp.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_lldp_tx sd_lldp_tx; __extension__ typedef enum sd_lldp_multicast_mode_t { SD_LLDP_MULTICAST_MODE_NEAREST_BRIDGE, SD_LLDP_MULTICAST_MODE_NON_TPMR_BRIDGE, SD_LLDP_MULTICAST_MODE_CUSTOMER_BRIDGE, _SD_LLDP_MULTICAST_MODE_MAX, _SD_LLDP_MULTICAST_MODE_INVALID = -EINVAL, _SD_ENUM_FORCE_S64(LLDP_TX_MODE) } sd_lldp_multicast_mode_t; int sd_lldp_tx_new(sd_lldp_tx **ret); sd_lldp_tx *sd_lldp_tx_ref(sd_lldp_tx *lldp_tx); sd_lldp_tx *sd_lldp_tx_unref(sd_lldp_tx *lldp_tx); int sd_lldp_tx_start(sd_lldp_tx *lldp_tx); int sd_lldp_tx_stop(sd_lldp_tx *lldp_tx); int sd_lldp_tx_is_running(sd_lldp_tx *lldp_tx); int sd_lldp_tx_attach_event(sd_lldp_tx *lldp_tx, sd_event *event, int64_t priority); int sd_lldp_tx_detach_event(sd_lldp_tx *lldp_tx); int sd_lldp_tx_set_ifindex(sd_lldp_tx *lldp_tx, int ifindex); int sd_lldp_tx_set_ifname(sd_lldp_tx *lldp_tx, const char *ifname); int sd_lldp_tx_get_ifname(sd_lldp_tx *lldp_tx, const char **ret); int sd_lldp_tx_set_multicast_mode(sd_lldp_tx *lldp_tx, sd_lldp_multicast_mode_t mode); int sd_lldp_tx_set_hwaddr(sd_lldp_tx *lldp_tx, const struct ether_addr *hwaddr); int sd_lldp_tx_set_port_description(sd_lldp_tx *lldp_tx, const char *port_description); int sd_lldp_tx_set_hostname(sd_lldp_tx *lldp_tx, const char *hostname); int sd_lldp_tx_set_pretty_hostname(sd_lldp_tx *lldp_tx, const char *pretty_hostname); int sd_lldp_tx_set_mud_url(sd_lldp_tx *lldp_tx, const char *mud_url); int sd_lldp_tx_set_capabilities(sd_lldp_tx *lldp_tx, uint16_t supported, uint16_t enabled); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_tx, sd_lldp_tx_unref); _SD_END_DECLARATIONS; #endif
2,604
35.690141
91
h
null
systemd-main/src/systemd/sd-lldp.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdlldphfoo #define foosdlldphfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; /* IEEE 802.1AB-2009 Clause 8: TLV Types */ enum { SD_LLDP_TYPE_END = 0, SD_LLDP_TYPE_CHASSIS_ID = 1, SD_LLDP_TYPE_PORT_ID = 2, SD_LLDP_TYPE_TTL = 3, SD_LLDP_TYPE_PORT_DESCRIPTION = 4, SD_LLDP_TYPE_SYSTEM_NAME = 5, SD_LLDP_TYPE_SYSTEM_DESCRIPTION = 6, SD_LLDP_TYPE_SYSTEM_CAPABILITIES = 7, SD_LLDP_TYPE_MGMT_ADDRESS = 8, SD_LLDP_TYPE_PRIVATE = 127 }; /* IEEE 802.1AB-2009 Clause 8.5.2: Chassis subtypes */ enum { SD_LLDP_CHASSIS_SUBTYPE_RESERVED = 0, SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT = 1, SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS = 2, SD_LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT = 3, SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS = 4, SD_LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS = 5, SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME = 6, SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED = 7 }; /* IEEE 802.1AB-2009 Clause 8.5.3: Port subtype */ enum { SD_LLDP_PORT_SUBTYPE_RESERVED = 0, SD_LLDP_PORT_SUBTYPE_INTERFACE_ALIAS = 1, SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT = 2, SD_LLDP_PORT_SUBTYPE_MAC_ADDRESS = 3, SD_LLDP_PORT_SUBTYPE_NETWORK_ADDRESS = 4, SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME = 5, SD_LLDP_PORT_SUBTYPE_AGENT_CIRCUIT_ID = 6, SD_LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED = 7 }; /* IEEE 802.1AB-2009 Clause 8.5.8: System capabilities */ enum { SD_LLDP_SYSTEM_CAPABILITIES_OTHER = 1 << 0, SD_LLDP_SYSTEM_CAPABILITIES_REPEATER = 1 << 1, SD_LLDP_SYSTEM_CAPABILITIES_BRIDGE = 1 << 2, SD_LLDP_SYSTEM_CAPABILITIES_WLAN_AP = 1 << 3, SD_LLDP_SYSTEM_CAPABILITIES_ROUTER = 1 << 4, SD_LLDP_SYSTEM_CAPABILITIES_PHONE = 1 << 5, SD_LLDP_SYSTEM_CAPABILITIES_DOCSIS = 1 << 6, SD_LLDP_SYSTEM_CAPABILITIES_STATION = 1 << 7, SD_LLDP_SYSTEM_CAPABILITIES_CVLAN = 1 << 8, SD_LLDP_SYSTEM_CAPABILITIES_SVLAN = 1 << 9, SD_LLDP_SYSTEM_CAPABILITIES_TPMR = 1 << 10 }; #define SD_LLDP_SYSTEM_CAPABILITIES_ALL UINT16_MAX #define SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS \ ((uint16_t) \ (SD_LLDP_SYSTEM_CAPABILITIES_REPEATER | \ SD_LLDP_SYSTEM_CAPABILITIES_BRIDGE | \ SD_LLDP_SYSTEM_CAPABILITIES_WLAN_AP | \ SD_LLDP_SYSTEM_CAPABILITIES_ROUTER | \ SD_LLDP_SYSTEM_CAPABILITIES_DOCSIS | \ SD_LLDP_SYSTEM_CAPABILITIES_CVLAN | \ SD_LLDP_SYSTEM_CAPABILITIES_SVLAN | \ SD_LLDP_SYSTEM_CAPABILITIES_TPMR)) #define SD_LLDP_OUI_802_1 (const uint8_t[]) { 0x00, 0x80, 0xc2 } #define SD_LLDP_OUI_802_3 (const uint8_t[]) { 0x00, 0x12, 0x0f } #define _SD_LLDP_OUI_IANA 0x00, 0x00, 0x5E #define SD_LLDP_OUI_IANA (const uint8_t[]) { _SD_LLDP_OUI_IANA } #define SD_LLDP_OUI_IANA_SUBTYPE_MUD 0x01 #define SD_LLDP_OUI_IANA_MUD \ (const uint8_t[]) { _SD_LLDP_OUI_IANA, SD_LLDP_OUI_IANA_SUBTYPE_MUD } /* IEEE 802.1AB-2009 Annex E */ enum { SD_LLDP_OUI_802_1_SUBTYPE_PORT_VLAN_ID = 1, SD_LLDP_OUI_802_1_SUBTYPE_PORT_PROTOCOL_VLAN_ID = 2, SD_LLDP_OUI_802_1_SUBTYPE_VLAN_NAME = 3, SD_LLDP_OUI_802_1_SUBTYPE_PROTOCOL_IDENTITY = 4, SD_LLDP_OUI_802_1_SUBTYPE_VID_USAGE_DIGEST = 5, SD_LLDP_OUI_802_1_SUBTYPE_MANAGEMENT_VID = 6, SD_LLDP_OUI_802_1_SUBTYPE_LINK_AGGREGATION = 7 }; /* IEEE 802.1AB-2009 Annex F */ enum { SD_LLDP_OUI_802_3_SUBTYPE_MAC_PHY_CONFIG_STATUS = 1, SD_LLDP_OUI_802_3_SUBTYPE_POWER_VIA_MDI = 2, SD_LLDP_OUI_802_3_SUBTYPE_LINK_AGGREGATION = 3, SD_LLDP_OUI_802_3_SUBTYPE_MAXIMUM_FRAME_SIZE = 4 }; _SD_END_DECLARATIONS; #endif
4,983
39.193548
77
h
null
systemd-main/src/systemd/sd-login.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdloginhfoo #define foosdloginhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <sys/types.h> #include "_sd-common.h" /* * A few points: * * Instead of returning an empty string array or empty uid array, we * may return NULL. * * Free the data the library returns with libc free(). String arrays * are NULL terminated, and you need to free the array itself, in * addition to the strings contained. * * We return error codes as negative errno, kernel-style. On success, we * return 0 or positive. * * These functions access data in /proc, /sys/fs/cgroup, and /run. All * of these are virtual file systems; therefore, accesses are * relatively cheap. * * See sd-login(3) for more information. */ _SD_BEGIN_DECLARATIONS; /* Get session from PID. Note that 'shared' processes of a user are * not attached to a session, but only attached to a user. This will * return an error for system processes and 'shared' processes of a * user. */ int sd_pid_get_session(pid_t pid, char **session); /* Get UID of the owner of the session of the PID (or in case the * process is a 'shared' user process, the UID of that user is * returned). This will not return the UID of the process, but rather * the UID of the owner of the cgroup that the process is in. This will * return an error for system processes. */ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid); /* Get systemd non-slice unit (i.e. service) name from PID, for system * services. This will return an error for non-service processes. */ int sd_pid_get_unit(pid_t pid, char **unit); /* Get systemd non-slice unit (i.e. service) name from PID, for user * services. This will return an error for non-user-service * processes. */ int sd_pid_get_user_unit(pid_t pid, char **unit); /* Get slice name from PID. */ int sd_pid_get_slice(pid_t pid, char **slice); /* Get user slice name from PID. */ int sd_pid_get_user_slice(pid_t pid, char **slice); /* Get machine name from PID, for processes assigned to a VM or * container. This will return an error for non-machine processes. */ int sd_pid_get_machine_name(pid_t pid, char **machine); /* Get the control group from a PID, relative to the root of the * hierarchy. */ int sd_pid_get_cgroup(pid_t pid, char **cgroup); /* Equivalent to the corresponding sd_pid_get* functions, but take a * PIDFD instead of a PID, to ensure there can be no possible PID * recycle issues before/after the calls. */ int sd_pidfd_get_session(pid_t pid, char **session); int sd_pidfd_get_owner_uid(pid_t pid, uid_t *uid); int sd_pidfd_get_unit(pid_t pid, char **unit); int sd_pidfd_get_user_unit(pid_t pid, char **unit); int sd_pidfd_get_slice(pid_t pid, char **slice); int sd_pidfd_get_user_slice(pid_t pid, char **slice); int sd_pidfd_get_machine_name(pid_t pid, char **machine); int sd_pidfd_get_cgroup(pid_t pid, char **cgroup); /* Similar to sd_pid_get_session(), but retrieves data about the peer * of a connected AF_UNIX socket */ int sd_peer_get_session(int fd, char **session); /* Similar to sd_pid_get_owner_uid(), but retrieves data about the peer of * a connected AF_UNIX socket */ int sd_peer_get_owner_uid(int fd, uid_t *uid); /* Similar to sd_pid_get_unit(), but retrieves data about the peer of * a connected AF_UNIX socket */ int sd_peer_get_unit(int fd, char **unit); /* Similar to sd_pid_get_user_unit(), but retrieves data about the peer of * a connected AF_UNIX socket */ int sd_peer_get_user_unit(int fd, char **unit); /* Similar to sd_pid_get_slice(), but retrieves data about the peer of * a connected AF_UNIX socket */ int sd_peer_get_slice(int fd, char **slice); /* Similar to sd_pid_get_user_slice(), but retrieves data about the peer of * a connected AF_UNIX socket */ int sd_peer_get_user_slice(int fd, char **slice); /* Similar to sd_pid_get_machine_name(), but retrieves data about the * peer of a connected AF_UNIX socket */ int sd_peer_get_machine_name(int fd, char **machine); /* Similar to sd_pid_get_cgroup(), but retrieves data about the peer * of a connected AF_UNIX socket. */ int sd_peer_get_cgroup(int fd, char **cgroup); /* Get state from UID. Possible states: offline, lingering, online, active, closing */ int sd_uid_get_state(uid_t uid, char **state); /* Return primary session of user, if there is any */ int sd_uid_get_display(uid_t uid, char **session); /* Determine the login time of user */ int sd_uid_get_login_time(uid_t uid, uint64_t *usec); /* Return 1 if UID has session on seat. If require_active is true, this will * look for active sessions only. */ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat); /* Return sessions of user. If require_active is true, this will look for * active sessions only. Returns the number of sessions. * If sessions is NULL, this will just return the number of sessions. */ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions); /* Return seats of user is on. If require_active is true, this will look for * active seats only. Returns the number of seats. * If seats is NULL, this will just return the number of seats. */ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats); /* Return 1 if the session is active. */ int sd_session_is_active(const char *session); /* Return 1 if the session is remote. */ int sd_session_is_remote(const char *session); /* Get state from session. Possible states: online, active, closing. * This function is a more generic version of sd_session_is_active(). */ int sd_session_get_state(const char *session, char **state); /* Determine user ID of session */ int sd_session_get_uid(const char *session, uid_t *uid); /* Determine username of session */ int sd_session_get_username(const char *session, char **username); /* Determine seat of session */ int sd_session_get_seat(const char *session, char **seat); /* Determine the start time of session */ int sd_session_get_start_time(const char *session, uint64_t *usec); /* Determine the (PAM) service name this session was registered by. */ int sd_session_get_service(const char *session, char **service); /* Determine the type of this session, i.e. one of "tty", "x11", "wayland", "mir" or "unspecified". */ int sd_session_get_type(const char *session, char **type); /* Determine the class of this session, i.e. one of "user", "greeter" or "lock-screen". */ int sd_session_get_class(const char *session, char **clazz); /* Determine the desktop brand of this session, i.e. something like "GNOME", "KDE" or "systemd-console". */ int sd_session_get_desktop(const char *session, char **desktop); /* Determine the X11 display of this session. */ int sd_session_get_display(const char *session, char **display); /* Determine the leader process of this session. */ int sd_session_get_leader(const char *session, pid_t *leader); /* Determine the remote host of this session. */ int sd_session_get_remote_host(const char *session, char **remote_host); /* Determine the remote user of this session (if provided by PAM). */ int sd_session_get_remote_user(const char *session, char **remote_user); /* Determine the TTY of this session. */ int sd_session_get_tty(const char *session, char **display); /* Determine the VT number of this session. */ int sd_session_get_vt(const char *session, unsigned *vtnr); /* Return active session and user of seat */ int sd_seat_get_active(const char *seat, char **session, uid_t *uid); /* Return sessions and users on seat. Returns number of sessions. * If sessions is NULL, this returns only the number of sessions. */ int sd_seat_get_sessions( const char *seat, char ***ret_sessions, uid_t **ret_uids, unsigned *ret_n_uids); /* Return whether the seat is multi-session capable */ int sd_seat_can_multi_session(const char *seat) _sd_deprecated_; /* Return whether the seat is TTY capable, i.e. suitable for showing console UIs */ int sd_seat_can_tty(const char *seat); /* Return whether the seat is graphics capable, i.e. suitable for showing graphical UIs */ int sd_seat_can_graphical(const char *seat); /* Return the class of machine */ int sd_machine_get_class(const char *machine, char **clazz); /* Return the list if host-side network interface indices of a machine */ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices); /* Get all seats, store in *seats. Returns the number of seats. If * seats is NULL, this only returns the number of seats. */ int sd_get_seats(char ***seats); /* Get all sessions, store in *sessions. Returns the number of * sessions. If sessions is NULL, this only returns the number of sessions. */ int sd_get_sessions(char ***sessions); /* Get all logged in users, store in *users. Returns the number of * users. If users is NULL, this only returns the number of users. */ int sd_get_uids(uid_t **users); /* Get all running virtual machines/containers */ int sd_get_machine_names(char ***machines); /* Monitor object */ typedef struct sd_login_monitor sd_login_monitor; /* Create a new monitor. Category must be NULL, "seat", "session", * "uid", or "machine" to get monitor events for the specific category * (or all). */ int sd_login_monitor_new(const char *category, sd_login_monitor** ret); /* Destroys the passed monitor. Returns NULL. */ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m); /* Flushes the monitor */ int sd_login_monitor_flush(sd_login_monitor *m); /* Get FD from monitor */ int sd_login_monitor_get_fd(sd_login_monitor *m); /* Get poll() mask to monitor */ int sd_login_monitor_get_events(sd_login_monitor *m); /* Get timeout for poll(), as usec value relative to CLOCK_MONOTONIC's epoch */ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_login_monitor, sd_login_monitor_unref); _SD_END_DECLARATIONS; #endif
10,545
37.915129
107
h
null
systemd-main/src/systemd/sd-messages.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdmessageshfoo #define foosdmessageshfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include "sd-id128.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; /* Hey! If you add a new message here, you *must* also update the message catalog with an appropriate explanation */ /* And if you add a new ID here, make sure to generate a random one with "systemd-id128 new". Do not use any * other IDs, and do not count them up manually. */ #define SD_MESSAGE_JOURNAL_START SD_ID128_MAKE(f7,73,79,a8,49,0b,40,8b,be,5f,69,40,50,5a,77,7b) #define SD_MESSAGE_JOURNAL_START_STR SD_ID128_MAKE_STR(f7,73,79,a8,49,0b,40,8b,be,5f,69,40,50,5a,77,7b) #define SD_MESSAGE_JOURNAL_STOP SD_ID128_MAKE(d9,3f,b3,c9,c2,4d,45,1a,97,ce,a6,15,ce,59,c0,0b) #define SD_MESSAGE_JOURNAL_STOP_STR SD_ID128_MAKE_STR(d9,3f,b3,c9,c2,4d,45,1a,97,ce,a6,15,ce,59,c0,0b) #define SD_MESSAGE_JOURNAL_DROPPED SD_ID128_MAKE(a5,96,d6,fe,7b,fa,49,94,82,8e,72,30,9e,95,d6,1e) #define SD_MESSAGE_JOURNAL_DROPPED_STR SD_ID128_MAKE_STR(a5,96,d6,fe,7b,fa,49,94,82,8e,72,30,9e,95,d6,1e) #define SD_MESSAGE_JOURNAL_MISSED SD_ID128_MAKE(e9,bf,28,e6,e8,34,48,1b,b6,f4,8f,54,8a,d1,36,06) #define SD_MESSAGE_JOURNAL_MISSED_STR SD_ID128_MAKE_STR(e9,bf,28,e6,e8,34,48,1b,b6,f4,8f,54,8a,d1,36,06) #define SD_MESSAGE_JOURNAL_USAGE SD_ID128_MAKE(ec,38,7f,57,7b,84,4b,8f,a9,48,f3,3c,ad,9a,75,e6) #define SD_MESSAGE_JOURNAL_USAGE_STR SD_ID128_MAKE_STR(ec,38,7f,57,7b,84,4b,8f,a9,48,f3,3c,ad,9a,75,e6) #define SD_MESSAGE_COREDUMP SD_ID128_MAKE(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1) #define SD_MESSAGE_COREDUMP_STR SD_ID128_MAKE_STR(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1) #define SD_MESSAGE_TRUNCATED_CORE SD_ID128_MAKE(5a,ad,d8,e9,54,dc,4b,1a,8c,95,4d,63,fd,9e,11,37) #define SD_MESSAGE_TRUNCATED_CORE_STR SD_ID128_MAKE_STR(5a,ad,d8,e9,54,dc,4b,1a,8c,95,4d,63,fd,9e,11,37) #define SD_MESSAGE_BACKTRACE SD_ID128_MAKE(1f,4e,0a,44,a8,86,49,93,9a,ae,a3,4f,c6,da,8c,95) #define SD_MESSAGE_BACKTRACE_STR SD_ID128_MAKE_STR(1f,4e,0a,44,a8,86,49,93,9a,ae,a3,4f,c6,da,8c,95) #define SD_MESSAGE_SESSION_START SD_ID128_MAKE(8d,45,62,0c,1a,43,48,db,b1,74,10,da,57,c6,0c,66) #define SD_MESSAGE_SESSION_START_STR SD_ID128_MAKE_STR(8d,45,62,0c,1a,43,48,db,b1,74,10,da,57,c6,0c,66) #define SD_MESSAGE_SESSION_STOP SD_ID128_MAKE(33,54,93,94,24,b4,45,6d,98,02,ca,83,33,ed,42,4a) #define SD_MESSAGE_SESSION_STOP_STR SD_ID128_MAKE_STR(33,54,93,94,24,b4,45,6d,98,02,ca,83,33,ed,42,4a) #define SD_MESSAGE_SEAT_START SD_ID128_MAKE(fc,be,fc,5d,a2,3d,42,80,93,f9,7c,82,a9,29,0f,7b) #define SD_MESSAGE_SEAT_START_STR SD_ID128_MAKE_STR(fc,be,fc,5d,a2,3d,42,80,93,f9,7c,82,a9,29,0f,7b) #define SD_MESSAGE_SEAT_STOP SD_ID128_MAKE(e7,85,2b,fe,46,78,4e,d0,ac,cd,e0,4b,c8,64,c2,d5) #define SD_MESSAGE_SEAT_STOP_STR SD_ID128_MAKE_STR(e7,85,2b,fe,46,78,4e,d0,ac,cd,e0,4b,c8,64,c2,d5) #define SD_MESSAGE_MACHINE_START SD_ID128_MAKE(24,d8,d4,45,25,73,40,24,96,06,83,81,a6,31,2d,f2) #define SD_MESSAGE_MACHINE_START_STR SD_ID128_MAKE_STR(24,d8,d4,45,25,73,40,24,96,06,83,81,a6,31,2d,f2) #define SD_MESSAGE_MACHINE_STOP SD_ID128_MAKE(58,43,2b,d3,ba,ce,47,7c,b5,14,b5,63,81,b8,a7,58) #define SD_MESSAGE_MACHINE_STOP_STR SD_ID128_MAKE_STR(58,43,2b,d3,ba,ce,47,7c,b5,14,b5,63,81,b8,a7,58) #define SD_MESSAGE_TIME_CHANGE SD_ID128_MAKE(c7,a7,87,07,9b,35,4e,aa,a9,e7,7b,37,18,93,cd,27) #define SD_MESSAGE_TIME_CHANGE_STR SD_ID128_MAKE_STR(c7,a7,87,07,9b,35,4e,aa,a9,e7,7b,37,18,93,cd,27) #define SD_MESSAGE_TIMEZONE_CHANGE SD_ID128_MAKE(45,f8,2f,4a,ef,7a,4b,bf,94,2c,e8,61,d1,f2,09,90) #define SD_MESSAGE_TIMEZONE_CHANGE_STR SD_ID128_MAKE_STR(45,f8,2f,4a,ef,7a,4b,bf,94,2c,e8,61,d1,f2,09,90) #define SD_MESSAGE_TAINTED SD_ID128_MAKE(50,87,6a,9d,b0,0f,4c,40,bd,e1,a2,ad,38,1c,3a,1b) #define SD_MESSAGE_TAINTED_STR SD_ID128_MAKE_STR(50,87,6a,9d,b0,0f,4c,40,bd,e1,a2,ad,38,1c,3a,1b) #define SD_MESSAGE_STARTUP_FINISHED SD_ID128_MAKE(b0,7a,24,9c,d0,24,41,4a,82,dd,00,cd,18,13,78,ff) #define SD_MESSAGE_STARTUP_FINISHED_STR SD_ID128_MAKE_STR(b0,7a,24,9c,d0,24,41,4a,82,dd,00,cd,18,13,78,ff) #define SD_MESSAGE_USER_STARTUP_FINISHED SD_ID128_MAKE(ee,d0,0a,68,ff,d8,4e,31,88,21,05,fd,97,3a,bd,d1) #define SD_MESSAGE_USER_STARTUP_FINISHED_STR SD_ID128_MAKE_STR(ee,d0,0a,68,ff,d8,4e,31,88,21,05,fd,97,3a,bd,d1) #define SD_MESSAGE_SLEEP_START SD_ID128_MAKE(6b,bd,95,ee,97,79,41,e4,97,c4,8b,e2,7c,25,41,28) #define SD_MESSAGE_SLEEP_START_STR SD_ID128_MAKE_STR(6b,bd,95,ee,97,79,41,e4,97,c4,8b,e2,7c,25,41,28) #define SD_MESSAGE_SLEEP_STOP SD_ID128_MAKE(88,11,e6,df,2a,8e,40,f5,8a,94,ce,a2,6f,8e,bf,14) #define SD_MESSAGE_SLEEP_STOP_STR SD_ID128_MAKE_STR(88,11,e6,df,2a,8e,40,f5,8a,94,ce,a2,6f,8e,bf,14) #define SD_MESSAGE_SHUTDOWN SD_ID128_MAKE(98,26,88,66,d1,d5,4a,49,9c,4e,98,92,1d,93,bc,40) #define SD_MESSAGE_SHUTDOWN_STR SD_ID128_MAKE_STR(98,26,88,66,d1,d5,4a,49,9c,4e,98,92,1d,93,bc,40) #define SD_MESSAGE_FACTORY_RESET SD_ID128_MAKE(c1,4a,af,76,ec,28,4a,5f,a1,f1,05,f8,8d,fb,06,1c) #define SD_MESSAGE_FACTORY_RESET_STR SD_ID128_MAKE_STR(c1,4a,af,76,ec,28,4a,5f,a1,f1,05,f8,8d,fb,06,1c) /* The messages below are actually about jobs, not really about units, the macros are misleadingly named. * Moreover SD_MESSAGE_UNIT_FAILED is not actually about a failing unit but about a failed start job. A job * either finishes with SD_MESSAGE_UNIT_STARTED or with SD_MESSAGE_UNIT_FAILED hence. */ #define SD_MESSAGE_UNIT_STARTING SD_ID128_MAKE(7d,49,58,e8,42,da,4a,75,8f,6c,1c,dc,7b,36,dc,c5) #define SD_MESSAGE_UNIT_STARTING_STR SD_ID128_MAKE_STR(7d,49,58,e8,42,da,4a,75,8f,6c,1c,dc,7b,36,dc,c5) #define SD_MESSAGE_UNIT_STARTED SD_ID128_MAKE(39,f5,34,79,d3,a0,45,ac,8e,11,78,62,48,23,1f,bf) #define SD_MESSAGE_UNIT_STARTED_STR SD_ID128_MAKE_STR(39,f5,34,79,d3,a0,45,ac,8e,11,78,62,48,23,1f,bf) #define SD_MESSAGE_UNIT_FAILED SD_ID128_MAKE(be,02,cf,68,55,d2,42,8b,a4,0d,f7,e9,d0,22,f0,3d) #define SD_MESSAGE_UNIT_FAILED_STR SD_ID128_MAKE_STR(be,02,cf,68,55,d2,42,8b,a4,0d,f7,e9,d0,22,f0,3d) #define SD_MESSAGE_UNIT_STOPPING SD_ID128_MAKE(de,5b,42,6a,63,be,47,a7,b6,ac,3e,aa,c8,2e,2f,6f) #define SD_MESSAGE_UNIT_STOPPING_STR SD_ID128_MAKE_STR(de,5b,42,6a,63,be,47,a7,b6,ac,3e,aa,c8,2e,2f,6f) #define SD_MESSAGE_UNIT_STOPPED SD_ID128_MAKE(9d,1a,aa,27,d6,01,40,bd,96,36,54,38,aa,d2,02,86) #define SD_MESSAGE_UNIT_STOPPED_STR SD_ID128_MAKE_STR(9d,1a,aa,27,d6,01,40,bd,96,36,54,38,aa,d2,02,86) #define SD_MESSAGE_UNIT_RELOADING SD_ID128_MAKE(d3,4d,03,7f,ff,18,47,e6,ae,66,9a,37,0e,69,47,25) #define SD_MESSAGE_UNIT_RELOADING_STR SD_ID128_MAKE_STR(d3,4d,03,7f,ff,18,47,e6,ae,66,9a,37,0e,69,47,25) #define SD_MESSAGE_UNIT_RELOADED SD_ID128_MAKE(7b,05,eb,c6,68,38,42,22,ba,a8,88,11,79,cf,da,54) #define SD_MESSAGE_UNIT_RELOADED_STR SD_ID128_MAKE_STR(7b,05,eb,c6,68,38,42,22,ba,a8,88,11,79,cf,da,54) #define SD_MESSAGE_UNIT_RESTART_SCHEDULED SD_ID128_MAKE(5e,b0,34,94,b6,58,48,70,a5,36,b3,37,29,08,09,b3) #define SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR SD_ID128_MAKE_STR(5e,b0,34,94,b6,58,48,70,a5,36,b3,37,29,08,09,b3) #define SD_MESSAGE_UNIT_RESOURCES SD_ID128_MAKE(ae,8f,7b,86,6b,03,47,b9,af,31,fe,1c,80,b1,27,c0) #define SD_MESSAGE_UNIT_RESOURCES_STR SD_ID128_MAKE_STR(ae,8f,7b,86,6b,03,47,b9,af,31,fe,1c,80,b1,27,c0) #define SD_MESSAGE_UNIT_SUCCESS SD_ID128_MAKE(7a,d2,d1,89,f7,e9,4e,70,a3,8c,78,13,54,91,24,48) #define SD_MESSAGE_UNIT_SUCCESS_STR SD_ID128_MAKE_STR(7a,d2,d1,89,f7,e9,4e,70,a3,8c,78,13,54,91,24,48) #define SD_MESSAGE_UNIT_SKIPPED SD_ID128_MAKE(0e,42,84,a0,ca,ca,4b,fc,81,c0,bb,67,86,97,26,73) #define SD_MESSAGE_UNIT_SKIPPED_STR SD_ID128_MAKE_STR(0e,42,84,a0,ca,ca,4b,fc,81,c0,bb,67,86,97,26,73) #define SD_MESSAGE_UNIT_FAILURE_RESULT SD_ID128_MAKE(d9,b3,73,ed,55,a6,4f,eb,82,42,e0,2d,be,79,a4,9c) #define SD_MESSAGE_UNIT_FAILURE_RESULT_STR SD_ID128_MAKE_STR(d9,b3,73,ed,55,a6,4f,eb,82,42,e0,2d,be,79,a4,9c) #define SD_MESSAGE_SPAWN_FAILED SD_ID128_MAKE(64,12,57,65,1c,1b,4e,c9,a8,62,4d,7a,40,a9,e1,e7) #define SD_MESSAGE_SPAWN_FAILED_STR SD_ID128_MAKE_STR(64,12,57,65,1c,1b,4e,c9,a8,62,4d,7a,40,a9,e1,e7) #define SD_MESSAGE_UNIT_PROCESS_EXIT SD_ID128_MAKE(98,e3,22,20,3f,7a,4e,d2,90,d0,9f,e0,3c,09,fe,15) #define SD_MESSAGE_UNIT_PROCESS_EXIT_STR SD_ID128_MAKE_STR(98,e3,22,20,3f,7a,4e,d2,90,d0,9f,e0,3c,09,fe,15) #define SD_MESSAGE_FORWARD_SYSLOG_MISSED SD_ID128_MAKE(00,27,22,9c,a0,64,41,81,a7,6c,4e,92,45,8a,fa,2e) #define SD_MESSAGE_FORWARD_SYSLOG_MISSED_STR SD_ID128_MAKE_STR(00,27,22,9c,a0,64,41,81,a7,6c,4e,92,45,8a,fa,2e) #define SD_MESSAGE_OVERMOUNTING SD_ID128_MAKE(1d,ee,03,69,c7,fc,47,36,b7,09,9b,38,ec,b4,6e,e7) #define SD_MESSAGE_OVERMOUNTING_STR SD_ID128_MAKE_STR(1d,ee,03,69,c7,fc,47,36,b7,09,9b,38,ec,b4,6e,e7) #define SD_MESSAGE_UNIT_OOMD_KILL SD_ID128_MAKE(d9,89,61,1b,15,e4,4c,9d,bf,31,e3,c8,12,56,e4,ed) #define SD_MESSAGE_UNIT_OOMD_KILL_STR SD_ID128_MAKE_STR(d9,89,61,1b,15,e4,4c,9d,bf,31,e3,c8,12,56,e4,ed) #define SD_MESSAGE_UNIT_OUT_OF_MEMORY SD_ID128_MAKE(fe,6f,aa,94,e7,77,46,63,a0,da,52,71,78,91,d8,ef) #define SD_MESSAGE_UNIT_OUT_OF_MEMORY_STR SD_ID128_MAKE_STR(fe,6f,aa,94,e7,77,46,63,a0,da,52,71,78,91,d8,ef) #define SD_MESSAGE_LID_OPENED SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,6f) #define SD_MESSAGE_LID_OPENED_STR SD_ID128_MAKE_STR(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,6f) #define SD_MESSAGE_LID_CLOSED SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,70) #define SD_MESSAGE_LID_CLOSED_STR SD_ID128_MAKE_STR(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,70) #define SD_MESSAGE_SYSTEM_DOCKED SD_ID128_MAKE(f5,f4,16,b8,62,07,4b,28,92,7a,48,c3,ba,7d,51,ff) #define SD_MESSAGE_SYSTEM_DOCKED_STR SD_ID128_MAKE_STR(f5,f4,16,b8,62,07,4b,28,92,7a,48,c3,ba,7d,51,ff) #define SD_MESSAGE_SYSTEM_UNDOCKED SD_ID128_MAKE(51,e1,71,bd,58,52,48,56,81,10,14,4c,51,7c,ca,53) #define SD_MESSAGE_SYSTEM_UNDOCKED_STR SD_ID128_MAKE_STR(51,e1,71,bd,58,52,48,56,81,10,14,4c,51,7c,ca,53) #define SD_MESSAGE_POWER_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,71) #define SD_MESSAGE_POWER_KEY_STR SD_ID128_MAKE_STR(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,71) #define SD_MESSAGE_POWER_KEY_LONG_PRESS SD_ID128_MAKE(3e,01,17,10,1e,b2,43,c1,b9,a5,0d,b3,49,4a,b1,0b) #define SD_MESSAGE_POWER_KEY_LONG_PRESS_STR SD_ID128_MAKE_STR(3e,01,17,10,1e,b2,43,c1,b9,a5,0d,b3,49,4a,b1,0b) #define SD_MESSAGE_REBOOT_KEY SD_ID128_MAKE(9f,a9,d2,c0,12,13,4e,c3,85,45,1f,fe,31,6f,97,d0) #define SD_MESSAGE_REBOOT_KEY_STR SD_ID128_MAKE_STR(9f,a9,d2,c0,12,13,4e,c3,85,45,1f,fe,31,6f,97,d0) #define SD_MESSAGE_REBOOT_KEY_LONG_PRESS SD_ID128_MAKE(f1,c5,9a,58,c9,d9,43,66,89,65,c3,37,ca,ec,59,75) #define SD_MESSAGE_REBOOT_KEY_LONG_PRESS_STR SD_ID128_MAKE_STR(f1,c5,9a,58,c9,d9,43,66,89,65,c3,37,ca,ec,59,75) #define SD_MESSAGE_SUSPEND_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,72) #define SD_MESSAGE_SUSPEND_KEY_STR SD_ID128_MAKE_STR(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,72) #define SD_MESSAGE_SUSPEND_KEY_LONG_PRESS SD_ID128_MAKE(bf,da,f6,d3,12,ab,40,07,bc,1f,e4,0a,15,df,78,e8) #define SD_MESSAGE_SUSPEND_KEY_LONG_PRESS_STR SD_ID128_MAKE_STR(bf,da,f6,d3,12,ab,40,07,bc,1f,e4,0a,15,df,78,e8) #define SD_MESSAGE_HIBERNATE_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,73) #define SD_MESSAGE_HIBERNATE_KEY_STR SD_ID128_MAKE_STR(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,73) #define SD_MESSAGE_HIBERNATE_KEY_LONG_PRESS SD_ID128_MAKE(16,78,36,df,6f,7f,42,8e,98,14,72,27,b2,dc,89,45) #define SD_MESSAGE_HIBERNATE_KEY_LONG_PRESS_STR SD_ID128_MAKE_STR(16,78,36,df,6f,7f,42,8e,98,14,72,27,b2,dc,89,45) #define SD_MESSAGE_INVALID_CONFIGURATION SD_ID128_MAKE(c7,72,d2,4e,9a,88,4c,be,b9,ea,12,62,5c,30,6c,01) #define SD_MESSAGE_INVALID_CONFIGURATION_STR SD_ID128_MAKE_STR(c7,72,d2,4e,9a,88,4c,be,b9,ea,12,62,5c,30,6c,01) #define SD_MESSAGE_DNSSEC_FAILURE SD_ID128_MAKE(16,75,d7,f1,72,17,40,98,b1,10,8b,f8,c7,dc,8f,5d) #define SD_MESSAGE_DNSSEC_FAILURE_STR SD_ID128_MAKE_STR(16,75,d7,f1,72,17,40,98,b1,10,8b,f8,c7,dc,8f,5d) #define SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED SD_ID128_MAKE(4d,44,08,cf,d0,d1,44,85,91,84,d1,e6,5d,7c,8a,65) #define SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED_STR SD_ID128_MAKE_STR(4d,44,08,cf,d0,d1,44,85,91,84,d1,e6,5d,7c,8a,65) #define SD_MESSAGE_DNSSEC_DOWNGRADE SD_ID128_MAKE(36,db,2d,fa,5a,90,45,e1,bd,4a,f5,f9,3e,1c,f0,57) #define SD_MESSAGE_DNSSEC_DOWNGRADE_STR SD_ID128_MAKE_STR(36,db,2d,fa,5a,90,45,e1,bd,4a,f5,f9,3e,1c,f0,57) #define SD_MESSAGE_UNSAFE_USER_NAME SD_ID128_MAKE(b6,1f,da,c6,12,e9,4b,91,82,28,5b,99,88,43,06,1f) #define SD_MESSAGE_UNSAFE_USER_NAME_STR SD_ID128_MAKE_STR(b6,1f,da,c6,12,e9,4b,91,82,28,5b,99,88,43,06,1f) #define SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE SD_ID128_MAKE(1b,3b,b9,40,37,f0,4b,bf,81,02,8e,13,5a,12,d2,93) #define SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR SD_ID128_MAKE_STR(1b,3b,b9,40,37,f0,4b,bf,81,02,8e,13,5a,12,d2,93) #define SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE SD_ID128_MAKE(01,01,90,13,8f,49,4e,29,a0,ef,66,69,74,95,31,aa) #define SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE_STR SD_ID128_MAKE_STR(01,01,90,13,8f,49,4e,29,a0,ef,66,69,74,95,31,aa) #define SD_MESSAGE_NOBODY_USER_UNSUITABLE SD_ID128_MAKE(b4,80,32,5f,9c,39,4a,7b,80,2c,23,1e,51,a2,75,2c) #define SD_MESSAGE_NOBODY_USER_UNSUITABLE_STR SD_ID128_MAKE_STR(b4,80,32,5f,9c,39,4a,7b,80,2c,23,1e,51,a2,75,2c) #define SD_MESSAGE_SYSTEMD_UDEV_SETTLE_DEPRECATED SD_ID128_MAKE(1c,04,54,c1,bd,22,41,e0,ac,6f,ef,b4,bc,63,14,33) #define SD_MESSAGE_SYSTEMD_UDEV_SETTLE_DEPRECATED_STR SD_ID128_MAKE_STR(1c,04,54,c1,bd,22,41,e0,ac,6f,ef,b4,bc,63,14,33) #define SD_MESSAGE_TIME_SYNC SD_ID128_MAKE(7c,8a,41,f3,7b,76,49,41,a0,e1,78,0b,1b,e2,f0,37) #define SD_MESSAGE_TIME_SYNC_STR SD_ID128_MAKE_STR(7c,8a,41,f3,7b,76,49,41,a0,e1,78,0b,1b,e2,f0,37) #define SD_MESSAGE_TIME_BUMP SD_ID128_MAKE(7d,b7,3c,8a,f0,d9,4e,eb,82,2a,e0,43,23,fe,6a,b6) #define SD_MESSAGE_TIME_BUMP_STR SD_ID128_MAKE_STR(7d,b7,3c,8a,f0,d9,4e,eb,82,2a,e0,43,23,fe,6a,b6) #define SD_MESSAGE_SHUTDOWN_SCHEDULED SD_ID128_MAKE(9e,70,66,27,9d,c8,40,3d,a7,9c,e4,b1,a6,90,64,b2) #define SD_MESSAGE_SHUTDOWN_SCHEDULED_STR SD_ID128_MAKE_STR(9e,70,66,27,9d,c8,40,3d,a7,9c,e4,b1,a6,90,64,b2) #define SD_MESSAGE_SHUTDOWN_CANCELED SD_ID128_MAKE(24,9f,6f,b9,e6,e2,42,8c,96,f3,f0,87,56,81,ff,a3) #define SD_MESSAGE_SHUTDOWN_CANCELED_STR SD_ID128_MAKE_STR(24,9f,6f,b9,e6,e2,42,8c,96,f3,f0,87,56,81,ff,a3) #define SD_MESSAGE_TPM_PCR_EXTEND SD_ID128_MAKE(3f,7d,5e,f3,e5,4f,43,02,b4,f0,b1,43,bb,27,0c,ab) #define SD_MESSAGE_TPM_PCR_EXTEND_STR SD_ID128_MAKE_STR(3f,7d,5e,f3,e5,4f,43,02,b4,f0,b1,43,bb,27,0c,ab) #define SD_MESSAGE_MEMORY_TRIM SD_ID128_MAKE(f9,b0,be,46,5a,d5,40,d0,85,0a,d3,21,72,d5,7c,21) #define SD_MESSAGE_MEMORY_TRIM_STR SD_ID128_MAKE_STR(f9,b0,be,46,5a,d5,40,d0,85,0a,d3,21,72,d5,7c,21) #define SD_MESSAGE_SYSV_GENERATOR_DEPRECATED SD_ID128_MAKE(a8,fa,8d,ac,db,1d,44,3e,95,03,b8,be,36,7a,6a,db) #define SD_MESSAGE_SYSV_GENERATOR_DEPRECATED_STR SD_ID128_MAKE_STR(a8,fa,8d,ac,db,1d,44,3e,95,03,b8,be,36,7a,6a,db) _SD_END_DECLARATIONS; #endif
17,892
85.439614
120
h
null
systemd-main/src/systemd/sd-ndisc.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdndiscfoo #define foosdndiscfoo /*** Copyright © 2014 Intel Corporation. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <errno.h> #include <inttypes.h> #include <net/ethernet.h> #include <netinet/in.h> #include <sys/types.h> #include "sd-event.h" #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; /* Neighbor Discovery Options, RFC 4861, Section 4.6 and * https://www.iana.org/assignments/icmpv6-parameters/icmpv6-parameters.xhtml#icmpv6-parameters-5 */ enum { SD_NDISC_OPTION_SOURCE_LL_ADDRESS = 1, SD_NDISC_OPTION_TARGET_LL_ADDRESS = 2, SD_NDISC_OPTION_PREFIX_INFORMATION = 3, SD_NDISC_OPTION_MTU = 5, SD_NDISC_OPTION_ROUTE_INFORMATION = 24, SD_NDISC_OPTION_RDNSS = 25, SD_NDISC_OPTION_FLAGS_EXTENSION = 26, SD_NDISC_OPTION_DNSSL = 31, SD_NDISC_OPTION_CAPTIVE_PORTAL = 37 }; /* Route preference, RFC 4191, Section 2.1 */ enum { SD_NDISC_PREFERENCE_LOW = 3U, SD_NDISC_PREFERENCE_MEDIUM = 0U, SD_NDISC_PREFERENCE_HIGH = 1U }; typedef struct sd_ndisc sd_ndisc; typedef struct sd_ndisc_router sd_ndisc_router; __extension__ typedef enum sd_ndisc_event_t { SD_NDISC_EVENT_TIMEOUT, SD_NDISC_EVENT_ROUTER, _SD_NDISC_EVENT_MAX, _SD_NDISC_EVENT_INVALID = -EINVAL, _SD_ENUM_FORCE_S64(NDISC_EVENT) } sd_ndisc_event_t; typedef void (*sd_ndisc_callback_t)(sd_ndisc *nd, sd_ndisc_event_t event, sd_ndisc_router *rt, void *userdata); int sd_ndisc_new(sd_ndisc **ret); sd_ndisc *sd_ndisc_ref(sd_ndisc *nd); sd_ndisc *sd_ndisc_unref(sd_ndisc *nd); int sd_ndisc_start(sd_ndisc *nd); int sd_ndisc_stop(sd_ndisc *nd); int sd_ndisc_attach_event(sd_ndisc *nd, sd_event *event, int64_t priority); int sd_ndisc_detach_event(sd_ndisc *nd); sd_event *sd_ndisc_get_event(sd_ndisc *nd); int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t cb, void *userdata); int sd_ndisc_set_ifindex(sd_ndisc *nd, int interface_index); int sd_ndisc_set_ifname(sd_ndisc *nd, const char *interface_name); int sd_ndisc_get_ifname(sd_ndisc *nd, const char **ret); int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr); sd_ndisc_router *sd_ndisc_router_ref(sd_ndisc_router *rt); sd_ndisc_router *sd_ndisc_router_unref(sd_ndisc_router *rt); int sd_ndisc_router_get_address(sd_ndisc_router *rt, struct in6_addr *ret_addr); int sd_ndisc_router_get_timestamp(sd_ndisc_router *rt, clockid_t clock, uint64_t *ret); int sd_ndisc_router_get_raw(sd_ndisc_router *rt, const void **ret, size_t *size); int sd_ndisc_router_get_hop_limit(sd_ndisc_router *rt, uint8_t *ret); int sd_ndisc_router_get_flags(sd_ndisc_router *rt, uint64_t *ret_flags); int sd_ndisc_router_get_preference(sd_ndisc_router *rt, unsigned *ret); int sd_ndisc_router_get_lifetime(sd_ndisc_router *rt, uint16_t *ret_lifetime); int sd_ndisc_router_get_mtu(sd_ndisc_router *rt, uint32_t *ret); /* Generic option access */ int sd_ndisc_router_option_rewind(sd_ndisc_router *rt); int sd_ndisc_router_option_next(sd_ndisc_router *rt); int sd_ndisc_router_option_get_type(sd_ndisc_router *rt, uint8_t *ret); int sd_ndisc_router_option_is_type(sd_ndisc_router *rt, uint8_t type); int sd_ndisc_router_option_get_raw(sd_ndisc_router *rt, const void **ret, size_t *size); /* Specific option access: SD_NDISC_OPTION_PREFIX_INFORMATION */ int sd_ndisc_router_prefix_get_valid_lifetime(sd_ndisc_router *rt, uint32_t *ret); int sd_ndisc_router_prefix_get_preferred_lifetime(sd_ndisc_router *rt, uint32_t *ret); int sd_ndisc_router_prefix_get_flags(sd_ndisc_router *rt, uint8_t *ret); int sd_ndisc_router_prefix_get_address(sd_ndisc_router *rt, struct in6_addr *ret_addr); int sd_ndisc_router_prefix_get_prefixlen(sd_ndisc_router *rt, unsigned *prefixlen); /* Specific option access: SD_NDISC_OPTION_ROUTE_INFORMATION */ int sd_ndisc_router_route_get_lifetime(sd_ndisc_router *rt, uint32_t *ret); int sd_ndisc_router_route_get_address(sd_ndisc_router *rt, struct in6_addr *ret_addr); int sd_ndisc_router_route_get_prefixlen(sd_ndisc_router *rt, unsigned *prefixlen); int sd_ndisc_router_route_get_preference(sd_ndisc_router *rt, unsigned *ret); /* Specific option access: SD_NDISC_OPTION_RDNSS */ int sd_ndisc_router_rdnss_get_addresses(sd_ndisc_router *rt, const struct in6_addr **ret); int sd_ndisc_router_rdnss_get_lifetime(sd_ndisc_router *rt, uint32_t *ret); /* Specific option access: SD_NDISC_OPTION_DNSSL */ int sd_ndisc_router_dnssl_get_domains(sd_ndisc_router *rt, char ***ret); int sd_ndisc_router_dnssl_get_lifetime(sd_ndisc_router *rt, uint32_t *ret); /* Specific option access: SD_NDISC_OPTION_CAPTIVE_PORTAL */ int sd_ndisc_router_captive_portal_get_uri(sd_ndisc_router *rt, const char **uri, size_t *size); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_ndisc, sd_ndisc_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_ndisc_router, sd_ndisc_router_unref); _SD_END_DECLARATIONS; #endif
5,613
40.585185
111
h
null
systemd-main/src/systemd/sd-network.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdnetworkhfoo #define foosdnetworkhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <sys/stat.h> #include <sys/types.h> #include "_sd-common.h" /* * A few points: * * Instead of returning an empty string array or empty integer array, we * may return NULL. * * Free the data the library returns with libc free(). String arrays * are NULL terminated, and you need to free the array itself in * addition to the strings contained. * * We return error codes as negative errno, kernel-style. On success, we * return 0 or positive. * * These functions access data in /run. This is a virtual file system; * therefore, accesses are relatively cheap. * * See sd-network(3) for more information. */ _SD_BEGIN_DECLARATIONS; /* Get overall operational state * Possible states: down, up, dormant, carrier, degraded, routable * Possible return codes: * -ENODATA: networkd is not aware of any links */ int sd_network_get_operational_state(char **ret); int sd_network_get_carrier_state(char **ret); int sd_network_get_address_state(char **ret); int sd_network_get_ipv4_address_state(char **ret); int sd_network_get_ipv6_address_state(char **ret); int sd_network_get_online_state(char **ret); /* Get DNS entries for all links. These are string representations of * IP addresses */ int sd_network_get_dns(char ***ret); /* Get NTP entries for all links. These are domain names or string * representations of IP addresses */ int sd_network_get_ntp(char ***ret); /* Get the search domains for all links. */ int sd_network_get_search_domains(char ***ret); /* Get the search domains for all links. */ int sd_network_get_route_domains(char ***ret); /* Get setup state from ifindex. * Possible states: * pending: udev is still processing the link, we don't yet know if we will manage it * failed: networkd failed to manage the link * configuring: in the process of retrieving configuration or configuring the link * configured: link configured successfully * unmanaged: networkd is not handling the link * linger: the link is gone, but has not yet been dropped by networkd * Possible return codes: * -ENODATA: networkd is not aware of the link */ int sd_network_link_get_setup_state(int ifindex, char **ret); /* Get operational state from ifindex. * Possible states: * off: the device is powered down * no-carrier: the device is powered up, but it does not yet have a carrier * dormant: the device has a carrier, but is not yet ready for normal traffic * carrier: the link has a carrier * degraded: the link has carrier and addresses valid on the local link configured * routable: the link has carrier and routable address configured * Possible return codes: * -ENODATA: networkd is not aware of the link */ int sd_network_link_get_operational_state(int ifindex, char **ret); int sd_network_link_get_required_operstate_for_online(int ifindex, char **ret); int sd_network_link_get_required_family_for_online(int ifindex, char **ret); int sd_network_link_get_carrier_state(int ifindex, char **ret); int sd_network_link_get_address_state(int ifindex, char **ret); int sd_network_link_get_ipv4_address_state(int ifindex, char **ret); int sd_network_link_get_ipv6_address_state(int ifindex, char **ret); int sd_network_link_get_online_state(int ifindex, char **ret); /* Indicates whether the network is relevant to being online. * Possible return codes: * 0: the connection is not required * 1: the connection is required to consider the system online * <0: networkd is not aware of the link */ int sd_network_link_get_required_for_online(int ifindex); /* Get activation policy for ifindex. * Possible values are as specified for ActivationPolicy= */ int sd_network_link_get_activation_policy(int ifindex, char **ret); /* Get path to .network file applied to link */ int sd_network_link_get_network_file(int ifindex, char **ret); /* Get paths to .network file dropins applied to link */ int sd_network_link_get_network_file_dropins(int ifindex, char ***ret); /* Get DNS entries for a given link. These are string representations of * IP addresses */ int sd_network_link_get_dns(int ifindex, char ***ret); /* Get NTP entries for a given link. These are domain names or string * representations of IP addresses */ int sd_network_link_get_ntp(int ifindex, char ***ret); /* Get SIP entries for a given link. These are string * representations of IP addresses */ int sd_network_link_get_sip(int ifindex, char ***ret); /* Get the captive portal address for a given link. */ int sd_network_link_get_captive_portal(int ifindex, char **ret); /* Indicates whether or not LLMNR should be enabled for the link * Possible levels of support: yes, no, resolve * Possible return codes: * -ENODATA: networkd is not aware of the link */ int sd_network_link_get_llmnr(int ifindex, char **ret); /* Indicates whether or not MulticastDNS should be enabled for the * link. * Possible levels of support: yes, no, resolve * Possible return codes: * -ENODATA: networkd is not aware of the link */ int sd_network_link_get_mdns(int ifindex, char **ret); /* Indicates whether or not DNS-over-TLS should be enabled for the * link. * Possible levels of support: yes, no, opportunistic * Possible return codes: * -ENODATA: networkd is not aware of the link */ int sd_network_link_get_dns_over_tls(int ifindex, char **ret); /* Indicates whether or not DNSSEC should be enabled for the link * Possible levels of support: yes, no, allow-downgrade * Possible return codes: * -ENODATA: networkd is not aware of the link */ int sd_network_link_get_dnssec(int ifindex, char **ret); /* Returns the list of per-interface DNSSEC negative trust anchors * Possible return codes: * -ENODATA: networkd is not aware of the link, or has no such data */ int sd_network_link_get_dnssec_negative_trust_anchors(int ifindex, char ***ret); /* Get the search DNS domain names for a given link. */ int sd_network_link_get_search_domains(int ifindex, char ***ret); /* Get the route DNS domain names for a given link. */ int sd_network_link_get_route_domains(int ifindex, char ***ret); /* Get whether this link shall be used as 'default route' for DNS queries */ int sd_network_link_get_dns_default_route(int ifindex); /* Get the carrier interface indexes to which current link is bound to. */ int sd_network_link_get_carrier_bound_to(int ifindex, int **ret); /* Get the CARRIERS that are bound to current link. */ int sd_network_link_get_carrier_bound_by(int ifindex, int **ret); /* Get DHCPv6 client IAID for a given link. */ int sd_network_link_get_dhcp6_client_iaid_string(int ifindex, char **ret); /* Get DHCPv6 client DUID for a given link. */ int sd_network_link_get_dhcp6_client_duid_string(int ifindex, char **ret); int sd_network_link_get_stat(int ifindex, struct stat *ret); /* Monitor object */ typedef struct sd_network_monitor sd_network_monitor; /* Create a new monitor. Category must be NULL, "links" or "leases". */ int sd_network_monitor_new(sd_network_monitor **ret, const char *category); /* Destroys the passed monitor. Returns NULL. */ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m); /* Flushes the monitor */ int sd_network_monitor_flush(sd_network_monitor *m); /* Get FD from monitor */ int sd_network_monitor_get_fd(sd_network_monitor *m); /* Get poll() mask to monitor */ int sd_network_monitor_get_events(sd_network_monitor *m); /* Get timeout for poll(), as usec value relative to CLOCK_MONOTONIC's epoch */ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *ret_usec); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_network_monitor, sd_network_monitor_unref); _SD_END_DECLARATIONS; #endif
8,413
36.395556
87
h
null
systemd-main/src/systemd/sd-path.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdpathhfoo #define foosdpathhfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; enum { /* Temporary files */ SD_PATH_TEMPORARY, SD_PATH_TEMPORARY_LARGE, /* Vendor supplied data */ SD_PATH_SYSTEM_BINARIES, SD_PATH_SYSTEM_INCLUDE, SD_PATH_SYSTEM_LIBRARY_PRIVATE, SD_PATH_SYSTEM_LIBRARY_ARCH, SD_PATH_SYSTEM_SHARED, SD_PATH_SYSTEM_CONFIGURATION_FACTORY, SD_PATH_SYSTEM_STATE_FACTORY, /* System configuration, runtime, state, ... */ SD_PATH_SYSTEM_CONFIGURATION, SD_PATH_SYSTEM_RUNTIME, SD_PATH_SYSTEM_RUNTIME_LOGS, SD_PATH_SYSTEM_STATE_PRIVATE, SD_PATH_SYSTEM_STATE_LOGS, SD_PATH_SYSTEM_STATE_CACHE, SD_PATH_SYSTEM_STATE_SPOOL, /* Vendor supplied data */ SD_PATH_USER_BINARIES, SD_PATH_USER_LIBRARY_PRIVATE, SD_PATH_USER_LIBRARY_ARCH, SD_PATH_USER_SHARED, /* User configuration, state, runtime ... */ SD_PATH_USER_CONFIGURATION, SD_PATH_USER_RUNTIME, SD_PATH_USER_STATE_CACHE, /* → SD_PATH_USER_STATE_PRIVATE is added at the bottom */ /* User resources */ SD_PATH_USER, /* $HOME itself */ SD_PATH_USER_DOCUMENTS, SD_PATH_USER_MUSIC, SD_PATH_USER_PICTURES, SD_PATH_USER_VIDEOS, SD_PATH_USER_DOWNLOAD, SD_PATH_USER_PUBLIC, SD_PATH_USER_TEMPLATES, SD_PATH_USER_DESKTOP, /* Search paths */ SD_PATH_SEARCH_BINARIES, SD_PATH_SEARCH_BINARIES_DEFAULT, SD_PATH_SEARCH_LIBRARY_PRIVATE, SD_PATH_SEARCH_LIBRARY_ARCH, SD_PATH_SEARCH_SHARED, SD_PATH_SEARCH_CONFIGURATION_FACTORY, SD_PATH_SEARCH_STATE_FACTORY, SD_PATH_SEARCH_CONFIGURATION, /* Various systemd paths, generally mirroring systemd.pc — Except we drop the "dir" suffix (and * replaces "path" by "search"), since this API is about dirs/paths anyway, and contains "path" * already in the prefix */ SD_PATH_SYSTEMD_UTIL, SD_PATH_SYSTEMD_SYSTEM_UNIT, SD_PATH_SYSTEMD_SYSTEM_PRESET, SD_PATH_SYSTEMD_SYSTEM_CONF, SD_PATH_SYSTEMD_USER_UNIT, SD_PATH_SYSTEMD_USER_PRESET, SD_PATH_SYSTEMD_USER_CONF, SD_PATH_SYSTEMD_SEARCH_SYSTEM_UNIT, SD_PATH_SYSTEMD_SEARCH_USER_UNIT, SD_PATH_SYSTEMD_SYSTEM_GENERATOR, SD_PATH_SYSTEMD_USER_GENERATOR, SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR, SD_PATH_SYSTEMD_SEARCH_USER_GENERATOR, SD_PATH_SYSTEMD_SLEEP, SD_PATH_SYSTEMD_SHUTDOWN, SD_PATH_TMPFILES, SD_PATH_SYSUSERS, SD_PATH_SYSCTL, SD_PATH_BINFMT, SD_PATH_MODULES_LOAD, SD_PATH_CATALOG, /* systemd-networkd search paths */ SD_PATH_SYSTEMD_SEARCH_NETWORK, /* systemd environment generators */ SD_PATH_SYSTEMD_SYSTEM_ENVIRONMENT_GENERATOR, SD_PATH_SYSTEMD_USER_ENVIRONMENT_GENERATOR, SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR, SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR, SD_PATH_USER_STATE_PRIVATE, _SD_PATH_MAX }; int sd_path_lookup(uint64_t type, const char *suffix, char **path); int sd_path_lookup_strv(uint64_t type, const char *suffix, char ***paths); _SD_END_DECLARATIONS; #endif
4,128
30.280303
103
h
null
systemd-main/src/systemd/sd-radv.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdradvfoo #define foosdradvfoo /*** Copyright © 2017 Intel Corporation. All rights reserved. systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include <inttypes.h> #include <net/ethernet.h> #include <netinet/in.h> #include <sys/types.h> #include "_sd-common.h" #include "sd-event.h" #include "sd-ndisc.h" _SD_BEGIN_DECLARATIONS; typedef struct sd_radv sd_radv; typedef struct sd_radv_prefix sd_radv_prefix; typedef struct sd_radv_route_prefix sd_radv_route_prefix; /* Router Advertisement */ int sd_radv_new(sd_radv **ret); sd_radv *sd_radv_ref(sd_radv *ra); sd_radv *sd_radv_unref(sd_radv *ra); int sd_radv_attach_event(sd_radv *ra, sd_event *event, int64_t priority); int sd_radv_detach_event(sd_radv *nd); sd_event *sd_radv_get_event(sd_radv *ra); int sd_radv_start(sd_radv *ra); int sd_radv_stop(sd_radv *ra); int sd_radv_is_running(sd_radv *ra); int sd_radv_set_ifindex(sd_radv *ra, int interface_index); int sd_radv_set_ifname(sd_radv *ra, const char *interface_name); int sd_radv_get_ifname(sd_radv *ra, const char **ret); int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr); int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu); int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit); int sd_radv_set_router_lifetime(sd_radv *ra, uint64_t lifetime_usec); int sd_radv_set_managed_information(sd_radv *ra, int managed); int sd_radv_set_other_information(sd_radv *ra, int other); int sd_radv_set_preference(sd_radv *ra, unsigned preference); int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p); int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p); void sd_radv_remove_prefix(sd_radv *ra, const struct in6_addr *prefix, unsigned char prefixlen); int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime, const struct in6_addr *dns, size_t n_dns); int sd_radv_set_dnssl(sd_radv *ra, uint32_t lifetime, char **search_list); /* Advertised prefixes */ int sd_radv_prefix_new(sd_radv_prefix **ret); sd_radv_prefix *sd_radv_prefix_ref(sd_radv_prefix *ra); sd_radv_prefix *sd_radv_prefix_unref(sd_radv_prefix *ra); int sd_radv_prefix_set_prefix(sd_radv_prefix *p, const struct in6_addr *in6_addr, unsigned char prefixlen); int sd_radv_prefix_get_prefix(sd_radv_prefix *p, struct in6_addr *ret_in6_addr, unsigned char *ret_prefixlen); int sd_radv_prefix_set_onlink(sd_radv_prefix *p, int onlink); int sd_radv_prefix_set_address_autoconfiguration(sd_radv_prefix *p, int address_autoconfiguration); int sd_radv_prefix_set_valid_lifetime(sd_radv_prefix *p, uint64_t lifetime_usec, uint64_t valid_until); int sd_radv_prefix_set_preferred_lifetime(sd_radv_prefix *p, uint64_t lifetime_usec, uint64_t valid_until); int sd_radv_route_prefix_new(sd_radv_route_prefix **ret); sd_radv_route_prefix *sd_radv_route_prefix_ref(sd_radv_route_prefix *ra); sd_radv_route_prefix *sd_radv_route_prefix_unref(sd_radv_route_prefix *ra); int sd_radv_route_prefix_set_prefix(sd_radv_route_prefix *p, const struct in6_addr *in6_addr, unsigned char prefixlen); int sd_radv_route_prefix_set_lifetime(sd_radv_route_prefix *p, uint64_t lifetime_usec, uint64_t valid_until); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_radv, sd_radv_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_radv_prefix, sd_radv_prefix_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_radv_route_prefix, sd_radv_route_prefix_unref); _SD_END_DECLARATIONS; #endif
4,115
41.875
119
h
null
systemd-main/src/systemd/sd-utf8.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #ifndef foosdutf8hfoo #define foosdutf8hfoo /*** systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. systemd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see <https://www.gnu.org/licenses/>. ***/ #include "_sd-common.h" _SD_BEGIN_DECLARATIONS; _sd_pure_ const char *sd_utf8_is_valid(const char *s); _sd_pure_ const char *sd_ascii_is_valid(const char *s); _SD_END_DECLARATIONS; #endif
936
30.233333
74
h
null
systemd-main/src/sysupdate/sysupdate-cache.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once #include "hashmap.h" typedef struct WebCacheItem { char *url; bool verified; size_t size; uint8_t data[]; } WebCacheItem; /* A simple in-memory cache for downloaded manifests. Very likely multiple transfers will use the same * manifest URLs, hence let's make sure we only download them once within each sysupdate invocation. */ int web_cache_add_item(Hashmap **cache, const char *url, bool verified, const void *data, size_t size); WebCacheItem* web_cache_get_item(Hashmap *cache, const char *url, bool verified);
614
31.368421
103
h
null
systemd-main/src/sysupdate/sysupdate-instance.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <fcntl.h> #include <sys/stat.h> #include "sysupdate-instance.h" void instance_metadata_destroy(InstanceMetadata *m) { assert(m); free(m->version); } int instance_new( Resource *rr, const char *path, const InstanceMetadata *f, Instance **ret) { _cleanup_(instance_freep) Instance *i = NULL; _cleanup_free_ char *p = NULL, *v = NULL; assert(rr); assert(path); assert(f); assert(f->version); assert(ret); p = strdup(path); if (!p) return log_oom(); v = strdup(f->version); if (!v) return log_oom(); i = new(Instance, 1); if (!i) return log_oom(); *i = (Instance) { .resource = rr, .metadata = *f, .path = TAKE_PTR(p), .partition_info = PARTITION_INFO_NULL, }; i->metadata.version = TAKE_PTR(v); *ret = TAKE_PTR(i); return 0; } Instance *instance_free(Instance *i) { if (!i) return NULL; instance_metadata_destroy(&i->metadata); free(i->path); partition_info_destroy(&i->partition_info); return mfree(i); }
1,369
20.40625
54
c
null
systemd-main/src/sysupdate/sysupdate-instance.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once #include <inttypes.h> #include <stdbool.h> #include <sys/types.h> #include "sd-id128.h" #include "fs-util.h" #include "time-util.h" typedef struct InstanceMetadata InstanceMetadata; typedef struct Instance Instance; #include "sysupdate-resource.h" #include "sysupdate-partition.h" struct InstanceMetadata { /* Various bits of metadata for each instance, that is either derived from the filename/GPT label or * from metadata of the file/partition itself */ char *version; sd_id128_t partition_uuid; bool partition_uuid_set; uint64_t partition_flags; /* GPT partition flags */ bool partition_flags_set; usec_t mtime; mode_t mode; uint64_t size; /* uncompressed size of the file */ uint64_t tries_done, tries_left; /* for boot assessment counters */ int no_auto; int read_only; int growfs; uint8_t sha256sum[32]; /* SHA256 sum of the download (i.e. compressed) file */ bool sha256sum_set; }; #define INSTANCE_METADATA_NULL \ { \ .mtime = USEC_INFINITY, \ .mode = MODE_INVALID, \ .size = UINT64_MAX, \ .tries_done = UINT64_MAX, \ .tries_left = UINT64_MAX, \ .no_auto = -1, \ .read_only = -1, \ .growfs = -1, \ } struct Instance { /* A pointer back to the resource this belongs to */ Resource *resource; /* Metadata of this version */ InstanceMetadata metadata; /* Where we found the instance */ char *path; PartitionInfo partition_info; }; void instance_metadata_destroy(InstanceMetadata *m); int instance_new(Resource *rr, const char *path, const InstanceMetadata *f, Instance **ret); Instance *instance_free(Instance *i); DEFINE_TRIVIAL_CLEANUP_FUNC(Instance*, instance_free);
2,169
30.911765
108
h
null
systemd-main/src/sysupdate/sysupdate-partition.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/file.h> #include "alloc-util.h" #include "extract-word.h" #include "gpt.h" #include "id128-util.h" #include "parse-util.h" #include "stdio-util.h" #include "string-util.h" #include "sysupdate-partition.h" void partition_info_destroy(PartitionInfo *p) { assert(p); p->label = mfree(p->label); p->device = mfree(p->device); } static int fdisk_partition_get_attrs_as_uint64( struct fdisk_partition *pa, uint64_t *ret) { uint64_t flags = 0; const char *a; int r; assert(pa); assert(ret); /* Retrieve current flags as uint64_t mask */ a = fdisk_partition_get_attrs(pa); if (!a) { *ret = 0; return 0; } for (;;) { _cleanup_free_ char *word = NULL; r = extract_first_word(&a, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS); if (r < 0) return r; if (r == 0) break; if (streq(word, "RequiredPartition")) flags |= SD_GPT_FLAG_REQUIRED_PARTITION; else if (streq(word, "NoBlockIOProtocol")) flags |= SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL; else if (streq(word, "LegacyBIOSBootable")) flags |= SD_GPT_FLAG_LEGACY_BIOS_BOOTABLE; else { const char *e; unsigned u; /* Drop "GUID" prefix if specified */ e = startswith(word, "GUID:") ?: word; if (safe_atou(e, &u) < 0) { log_debug("Unknown partition flag '%s', ignoring.", word); continue; } if (u >= sizeof(flags)*8) { /* partition flags on GPT are 64-bit. Let's ignore any further bits should libfdisk report them */ log_debug("Partition flag above bit 63 (%s), ignoring.", word); continue; } flags |= UINT64_C(1) << u; } } *ret = flags; return 0; } static int fdisk_partition_set_attrs_as_uint64( struct fdisk_partition *pa, uint64_t flags) { _cleanup_free_ char *attrs = NULL; int r; assert(pa); for (unsigned i = 0; i < sizeof(flags) * 8; i++) { if (!FLAGS_SET(flags, UINT64_C(1) << i)) continue; r = strextendf_with_separator(&attrs, ",", "%u", i); if (r < 0) return r; } return fdisk_partition_set_attrs(pa, strempty(attrs)); } int read_partition_info( struct fdisk_context *c, struct fdisk_table *t, size_t i, PartitionInfo *ret) { _cleanup_free_ char *label_copy = NULL, *device = NULL; const char *label; struct fdisk_partition *p; uint64_t start, size, flags; sd_id128_t ptid, id; GptPartitionType type; size_t partno; int r; assert(c); assert(t); assert(ret); p = fdisk_table_get_partition(t, i); if (!p) return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m"); if (fdisk_partition_is_used(p) <= 0) { *ret = (PartitionInfo) PARTITION_INFO_NULL; return 0; /* not found! */ } if (fdisk_partition_has_partno(p) <= 0 || fdisk_partition_has_start(p) <= 0 || fdisk_partition_has_size(p) <= 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a number, position or size."); partno = fdisk_partition_get_partno(p); start = fdisk_partition_get_start(p); assert(start <= UINT64_MAX / 512U); start *= 512U; size = fdisk_partition_get_size(p); assert(size <= UINT64_MAX / 512U); size *= 512U; label = fdisk_partition_get_name(p); if (!label) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a label."); r = fdisk_partition_get_type_as_id128(p, &ptid); if (r < 0) return log_error_errno(r, "Failed to read partition type UUID: %m"); r = fdisk_partition_get_uuid_as_id128(p, &id); if (r < 0) return log_error_errno(r, "Failed to read partition UUID: %m"); r = fdisk_partition_get_attrs_as_uint64(p, &flags); if (r < 0) return log_error_errno(r, "Failed to get partition flags: %m"); r = fdisk_partition_to_string(p, c, FDISK_FIELD_DEVICE, &device); if (r != 0) return log_error_errno(r, "Failed to get partition device name: %m"); label_copy = strdup(label); if (!label_copy) return log_oom(); type = gpt_partition_type_from_uuid(ptid); *ret = (PartitionInfo) { .partno = partno, .start = start, .size = size, .flags = flags, .type = ptid, .uuid = id, .label = TAKE_PTR(label_copy), .device = TAKE_PTR(device), .no_auto = FLAGS_SET(flags, SD_GPT_FLAG_NO_AUTO) && gpt_partition_type_knows_no_auto(type), .read_only = FLAGS_SET(flags, SD_GPT_FLAG_READ_ONLY) && gpt_partition_type_knows_read_only(type), .growfs = FLAGS_SET(flags, SD_GPT_FLAG_GROWFS) && gpt_partition_type_knows_growfs(type), }; return 1; /* found! */ } int find_suitable_partition( const char *device, uint64_t space, sd_id128_t *partition_type, PartitionInfo *ret) { _cleanup_(partition_info_destroy) PartitionInfo smallest = PARTITION_INFO_NULL; _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; size_t n_partitions; int r; assert(device); assert(ret); c = fdisk_new_context(); if (!c) return log_oom(); r = fdisk_assign_device(c, device, /* readonly= */ true); if (r < 0) return log_error_errno(r, "Failed to open device '%s': %m", device); if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not suitable.", device); r = fdisk_get_partitions(c, &t); if (r < 0) return log_error_errno(r, "Failed to acquire partition table: %m"); n_partitions = fdisk_table_get_nents(t); for (size_t i = 0; i < n_partitions; i++) { _cleanup_(partition_info_destroy) PartitionInfo pinfo = PARTITION_INFO_NULL; r = read_partition_info(c, t, i, &pinfo); if (r < 0) return r; if (r == 0) /* not assigned */ continue; /* Filter out non-matching partition types */ if (partition_type && !sd_id128_equal(pinfo.type, *partition_type)) continue; if (!streq_ptr(pinfo.label, "_empty")) /* used */ continue; if (space != UINT64_MAX && pinfo.size < space) /* too small */ continue; if (smallest.partno != SIZE_MAX && smallest.size <= pinfo.size) /* already found smaller */ continue; smallest = pinfo; pinfo = (PartitionInfo) PARTITION_INFO_NULL; } if (smallest.partno == SIZE_MAX) return log_error_errno(SYNTHETIC_ERRNO(ENOSPC), "No available partition of a suitable size found."); *ret = smallest; smallest = (PartitionInfo) PARTITION_INFO_NULL; return 0; } int patch_partition( const char *device, const PartitionInfo *info, PartitionChange change) { _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *pa = NULL; _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; bool tweak_no_auto, tweak_read_only, tweak_growfs; GptPartitionType type; int r, fd; assert(device); assert(info); assert(change <= _PARTITION_CHANGE_MAX); if (change == 0) /* Nothing to do */ return 0; c = fdisk_new_context(); if (!c) return log_oom(); r = fdisk_assign_device(c, device, /* readonly= */ false); if (r < 0) return log_error_errno(r, "Failed to open device '%s': %m", device); assert_se((fd = fdisk_get_devfd(c)) >= 0); /* Make sure udev doesn't read the device while we make changes (this lock is released automatically * by the kernel when the fd is closed, i.e. when the fdisk context is freed, hence no explicit * unlock by us here anywhere.) */ if (flock(fd, LOCK_EX) < 0) return log_error_errno(errno, "Failed to lock block device '%s': %m", device); if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not suitable.", device); r = fdisk_get_partition(c, info->partno, &pa); if (r < 0) return log_error_errno(r, "Failed to read partition %zu of GPT label of '%s': %m", info->partno, device); if (change & PARTITION_LABEL) { r = fdisk_partition_set_name(pa, info->label); if (r < 0) return log_error_errno(r, "Failed to update partition label: %m"); } if (change & PARTITION_UUID) { r = fdisk_partition_set_uuid(pa, SD_ID128_TO_UUID_STRING(info->uuid)); if (r < 0) return log_error_errno(r, "Failed to update partition UUID: %m"); } type = gpt_partition_type_from_uuid(info->type); /* Tweak the read-only flag, but only if supported by the partition type */ tweak_no_auto = FLAGS_SET(change, PARTITION_NO_AUTO) && gpt_partition_type_knows_no_auto(type); tweak_read_only = FLAGS_SET(change, PARTITION_READ_ONLY) && gpt_partition_type_knows_read_only(type); tweak_growfs = FLAGS_SET(change, PARTITION_GROWFS) && gpt_partition_type_knows_growfs(type); if (change & PARTITION_FLAGS) { uint64_t flags; /* Update the full flags parameter, and import the read-only flag into it */ flags = info->flags; if (tweak_no_auto) SET_FLAG(flags, SD_GPT_FLAG_NO_AUTO, info->no_auto); if (tweak_read_only) SET_FLAG(flags, SD_GPT_FLAG_READ_ONLY, info->read_only); if (tweak_growfs) SET_FLAG(flags, SD_GPT_FLAG_GROWFS, info->growfs); r = fdisk_partition_set_attrs_as_uint64(pa, flags); if (r < 0) return log_error_errno(r, "Failed to update partition flags: %m"); } else if (tweak_no_auto || tweak_read_only || tweak_growfs) { uint64_t old_flags, new_flags; /* So we aren't supposed to update the full flags parameter, but we are supposed to update * the RO flag of it. */ r = fdisk_partition_get_attrs_as_uint64(pa, &old_flags); if (r < 0) return log_error_errno(r, "Failed to get old partition flags: %m"); new_flags = old_flags; if (tweak_no_auto) SET_FLAG(new_flags, SD_GPT_FLAG_NO_AUTO, info->no_auto); if (tweak_read_only) SET_FLAG(new_flags, SD_GPT_FLAG_READ_ONLY, info->read_only); if (tweak_growfs) SET_FLAG(new_flags, SD_GPT_FLAG_GROWFS, info->growfs); if (new_flags != old_flags) { r = fdisk_partition_set_attrs_as_uint64(pa, new_flags); if (r < 0) return log_error_errno(r, "Failed to update partition flags: %m"); } } r = fdisk_set_partition(c, info->partno, pa); if (r < 0) return log_error_errno(r, "Failed to update partition: %m"); r = fdisk_write_disklabel(c); if (r < 0) return log_error_errno(r, "Failed to write updated partition table: %m"); return 0; }
13,257
34.639785
123
c
null
systemd-main/src/sysupdate/sysupdate-partition.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once #include <inttypes.h> #include <sys/types.h> #include "sd-id128.h" #include "fdisk-util.h" #include "macro.h" typedef struct PartitionInfo PartitionInfo; typedef enum PartitionChange { PARTITION_FLAGS = 1 << 0, PARTITION_NO_AUTO = 1 << 1, PARTITION_READ_ONLY = 1 << 2, PARTITION_GROWFS = 1 << 3, PARTITION_UUID = 1 << 4, PARTITION_LABEL = 1 << 5, _PARTITION_CHANGE_MAX = (1 << 6) - 1, /* all of the above */ _PARTITION_CHANGE_INVALID = -EINVAL, } PartitionChange; struct PartitionInfo { size_t partno; uint64_t start, size; uint64_t flags; sd_id128_t type, uuid; char *label; char *device; /* Note that this might point to some non-existing path in case we operate on a loopback file */ bool no_auto:1; bool read_only:1; bool growfs:1; }; #define PARTITION_INFO_NULL \ { \ .partno = SIZE_MAX, \ .start = UINT64_MAX, \ .size = UINT64_MAX, \ } void partition_info_destroy(PartitionInfo *p); int read_partition_info(struct fdisk_context *c, struct fdisk_table *t, size_t i, PartitionInfo *ret); int find_suitable_partition(const char *device, uint64_t space, sd_id128_t *partition_type, PartitionInfo *ret); int patch_partition(const char *device, const PartitionInfo *info, PartitionChange change);
1,620
31.42
118
h
null
systemd-main/src/sysupdate/sysupdate-update-set.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "glyph-util.h" #include "string-util.h" #include "sysupdate-update-set.h" #include "terminal-util.h" UpdateSet *update_set_free(UpdateSet *us) { if (!us) return NULL; free(us->version); free(us->instances); /* The objects referenced by this array are freed via resource_free(), not us */ return mfree(us); } int update_set_cmp(UpdateSet *const*a, UpdateSet *const*b) { assert(a); assert(b); assert(*a); assert(*b); assert((*a)->version); assert((*b)->version); /* Newest version at the beginning */ return -strverscmp_improved((*a)->version, (*b)->version); } const char *update_set_flags_to_color(UpdateSetFlags flags) { if (flags == 0 || (flags & UPDATE_OBSOLETE)) return (flags & UPDATE_NEWEST) ? ansi_highlight_grey() : ansi_grey(); if (FLAGS_SET(flags, UPDATE_INSTALLED|UPDATE_NEWEST)) return ansi_highlight(); if (FLAGS_SET(flags, UPDATE_INSTALLED|UPDATE_PROTECTED)) return ansi_highlight_magenta(); if ((flags & (UPDATE_AVAILABLE|UPDATE_INSTALLED|UPDATE_NEWEST|UPDATE_OBSOLETE)) == (UPDATE_AVAILABLE|UPDATE_NEWEST)) return ansi_highlight_green(); return NULL; } const char *update_set_flags_to_glyph(UpdateSetFlags flags) { if (flags == 0 || (flags & UPDATE_OBSOLETE)) return special_glyph(SPECIAL_GLYPH_MULTIPLICATION_SIGN); if (FLAGS_SET(flags, UPDATE_INSTALLED|UPDATE_NEWEST)) return special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE); if (FLAGS_SET(flags, UPDATE_INSTALLED|UPDATE_PROTECTED)) return special_glyph(SPECIAL_GLYPH_WHITE_CIRCLE); if ((flags & (UPDATE_AVAILABLE|UPDATE_INSTALLED|UPDATE_NEWEST|UPDATE_OBSOLETE)) == (UPDATE_AVAILABLE|UPDATE_NEWEST)) return special_glyph(SPECIAL_GLYPH_CIRCLE_ARROW); return " "; }
2,045
30.96875
124
c
null
systemd-main/src/sysupdate/sysupdate-update-set.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once #include <inttypes.h> #include <stdbool.h> #include <sys/types.h> typedef struct UpdateSet UpdateSet; #include "sysupdate-instance.h" typedef enum UpdateSetFlags { UPDATE_NEWEST = 1 << 0, UPDATE_AVAILABLE = 1 << 1, UPDATE_INSTALLED = 1 << 2, UPDATE_OBSOLETE = 1 << 3, UPDATE_PROTECTED = 1 << 4, } UpdateSetFlags; struct UpdateSet { UpdateSetFlags flags; char *version; Instance **instances; size_t n_instances; }; UpdateSet *update_set_free(UpdateSet *us); int update_set_cmp(UpdateSet *const*a, UpdateSet *const*b); const char *update_set_flags_to_color(UpdateSetFlags flags); const char *update_set_flags_to_glyph(UpdateSetFlags flags);
785
22.818182
60
h
null
systemd-main/src/sysupdate/sysupdate.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once #include <inttypes.h> #include <stdbool.h> extern bool arg_sync; extern uint64_t arg_instances_max; extern char *arg_root; static inline const char* import_binary_path(void) { return secure_getenv("SYSTEMD_IMPORT_PATH") ?: SYSTEMD_IMPORT_PATH; } static inline const char* import_fs_binary_path(void) { return secure_getenv("SYSTEMD_IMPORT_FS_PATH") ?: SYSTEMD_IMPORT_FS_PATH; } static inline const char *pull_binary_path(void) { return secure_getenv("SYSTEMD_PULL_PATH") ?: SYSTEMD_PULL_PATH; }
586
25.681818
81
h
null
systemd-main/src/test/nss-test-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <dlfcn.h> #include <stdio.h> #include <unistd.h> #include "nss-test-util.h" #include "string-util.h" const char* nss_status_to_string(enum nss_status status, char *buf, size_t buf_len) { switch (status) { case NSS_STATUS_TRYAGAIN: return "NSS_STATUS_TRYAGAIN"; case NSS_STATUS_UNAVAIL: return "NSS_STATUS_UNAVAIL"; case NSS_STATUS_NOTFOUND: return "NSS_STATUS_NOTFOUND"; case NSS_STATUS_SUCCESS: return "NSS_STATUS_SUCCESS"; case NSS_STATUS_RETURN: return "NSS_STATUS_RETURN"; default: (void) snprintf(buf, buf_len, "%i", status); return buf; } }; void* nss_open_handle(const char *dir, const char *module, int flags) { const char *path = NULL; void *handle; if (dir) path = strjoina(dir, "/libnss_", module, ".so.2"); if (!path || access(path, F_OK) < 0) path = strjoina("libnss_", module, ".so.2"); log_debug("Using %s", path); handle = dlopen(path, flags); if (!handle) log_error("Failed to load module %s: %s", module, dlerror()); return handle; }
1,304
29.348837
85
c
null
systemd-main/src/test/test-acl-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <fcntl.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include "acl-util.h" #include "errno-util.h" #include "fd-util.h" #include "format-util.h" #include "fs-util.h" #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" #include "user-util.h" TEST_RET(add_acls_for_user) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-empty.XXXXXX"; _cleanup_close_ int fd = -EBADF; char *cmd; uid_t uid; int r; fd = mkostemp_safe(fn); assert_se(fd >= 0); /* Use the mode that user journal files use */ assert_se(fchmod(fd, 0640) == 0); cmd = strjoina("ls -l ", fn); assert_se(system(cmd) == 0); cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); if (getuid() == 0) { const char *nobody = NOBODY_USER_NAME; r = get_user_creds(&nobody, &uid, NULL, NULL, NULL, 0); if (r < 0) uid = 0; } else uid = getuid(); r = fd_add_uid_acl_permission(fd, uid, ACL_READ); if (ERRNO_IS_NOT_SUPPORTED(r)) return log_tests_skipped("no ACL support on /tmp"); log_info_errno(r, "fd_add_uid_acl_permission(%i, "UID_FMT", ACL_READ): %m", fd, uid); assert_se(r >= 0); cmd = strjoina("ls -l ", fn); assert_se(system(cmd) == 0); cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); /* set the acls again */ r = fd_add_uid_acl_permission(fd, uid, ACL_READ); assert_se(r >= 0); cmd = strjoina("ls -l ", fn); assert_se(system(cmd) == 0); cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); return 0; } TEST(fd_acl_make_read_only) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-empty.XXXXXX"; _cleanup_close_ int fd = -EBADF; const char *cmd; struct stat st; fd = mkostemp_safe(fn); assert_se(fd >= 0); /* make it more exciting */ (void) fd_add_uid_acl_permission(fd, 1, ACL_READ|ACL_WRITE|ACL_EXECUTE); assert_se(fstat(fd, &st) >= 0); assert_se((st.st_mode & 0200) == 0200); cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); cmd = strjoina("stat ", fn); assert_se(system(cmd) == 0); log_info("read-only"); assert_se(fd_acl_make_read_only(fd)); assert_se(fstat(fd, &st) >= 0); assert_se((st.st_mode & 0222) == 0000); cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); cmd = strjoina("stat ", fn); assert_se(system(cmd) == 0); log_info("writable"); assert_se(fd_acl_make_writable(fd)); assert_se(fstat(fd, &st) >= 0); assert_se((st.st_mode & 0222) == 0200); cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); cmd = strjoina("stat ", fn); assert_se(system(cmd) == 0); log_info("read-only"); assert_se(fd_acl_make_read_only(fd)); assert_se(fstat(fd, &st) >= 0); assert_se((st.st_mode & 0222) == 0000); cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); cmd = strjoina("stat ", fn); assert_se(system(cmd) == 0); } DEFINE_TEST_MAIN(LOG_INFO);
3,477
25.549618
93
c
null
systemd-main/src/test/test-af-list.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/socket.h> #include "macro.h" #include "string-util.h" #include "tests.h" _unused_ static const struct af_name* lookup_af(register const char *str, register GPERF_LEN_TYPE len); #include "af-from-name.h" #include "af-list.h" #include "af-to-name.h" TEST(af_list) { for (unsigned i = 0; i < ELEMENTSOF(af_names); i++) { if (af_names[i]) { assert_se(streq(af_to_name(i), af_names[i])); assert_se(af_from_name(af_names[i]) == (int) i); } } assert_se(af_to_name(af_max()) == NULL); assert_se(af_to_name(-1) == NULL); assert_se(af_from_name("huddlduddl") == -EINVAL); assert_se(af_from_name("") == -EINVAL); } DEFINE_TEST_MAIN(LOG_INFO);
830
25.806452
94
c
null
systemd-main/src/test/test-architecture.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "architecture.h" #include "errno-util.h" #include "log.h" #include "tests.h" #include "virt.h" int main(int argc, char *argv[]) { Virtualization v; Architecture a; const char *p; test_setup_logging(LOG_INFO); assert_se(architecture_from_string("") < 0); assert_se(architecture_from_string(NULL) < 0); assert_se(architecture_from_string("hoge") < 0); assert_se(architecture_to_string(-1) == NULL); assert_se(architecture_from_string(architecture_to_string(0)) == 0); assert_se(architecture_from_string(architecture_to_string(1)) == 1); v = detect_virtualization(); if (v < 0 && ERRNO_IS_PRIVILEGE(v)) return log_tests_skipped("Cannot detect virtualization"); assert_se(v >= 0); log_info("virtualization=%s id=%s", VIRTUALIZATION_IS_CONTAINER(v) ? "container" : VIRTUALIZATION_IS_VM(v) ? "vm" : "n/a", virtualization_to_string(v)); a = uname_architecture(); assert_se(a >= 0); p = architecture_to_string(a); assert_se(p); log_info("uname architecture=%s", p); assert_se(architecture_from_string(p) == a); a = native_architecture(); assert_se(a >= 0); p = architecture_to_string(a); assert_se(p); log_info("native architecture=%s", p); assert_se(architecture_from_string(p) == a); log_info("primary library architecture=" LIB_ARCH_TUPLE); return 0; }
1,617
28.962963
76
c
null
systemd-main/src/test/test-argv-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sched.h> #include <unistd.h> #if HAVE_VALGRIND_VALGRIND_H # include <valgrind/valgrind.h> #endif #include "argv-util.h" #include "missing_sched.h" #include "process-util.h" #include "tests.h" #include "virt.h" static void test_rename_process_now(const char *p, int ret) { _cleanup_free_ char *comm = NULL, *cmdline = NULL; int r; log_info("/* %s(%s) */", __func__, p); r = rename_process(p); assert_se(r == ret || (ret == 0 && r >= 0) || (ret > 0 && r > 0)); log_debug_errno(r, "rename_process(%s): %m", p); if (r < 0) return; #if HAVE_VALGRIND_VALGRIND_H /* see above, valgrind is weird, we can't verify what we are doing here */ if (RUNNING_ON_VALGRIND) return; #endif assert_se(get_process_comm(0, &comm) >= 0); log_debug("comm = <%s>", comm); assert_se(strneq(comm, p, TASK_COMM_LEN-1)); /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the * future. We'd only check the initial part, at least until we recompile, but this will still pass. */ r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline); assert_se(r >= 0); /* we cannot expect cmdline to be renamed properly without privileges */ if (geteuid() == 0) { if (r == 0 && detect_container() > 0) log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline); else { log_info("cmdline = <%s> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p); bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */ assert_se(strneq(cmdline + skip, p, strlen("test-process-util"))); assert_se(startswith(cmdline + skip, p)); } } else log_info("cmdline = <%s> (not verified)", cmdline); } static void test_rename_process_one(const char *p, int ret) { siginfo_t si; pid_t pid; log_info("/* %s(%s) */", __func__, p); pid = fork(); assert_se(pid >= 0); if (pid == 0) { /* child */ test_rename_process_now(p, ret); _exit(EXIT_SUCCESS); } assert_se(wait_for_terminate(pid, &si) >= 0); assert_se(si.si_code == CLD_EXITED); assert_se(si.si_status == EXIT_SUCCESS); } TEST(rename_process_invalid) { assert_se(rename_process(NULL) == -EINVAL); assert_se(rename_process("") == -EINVAL); } TEST(rename_process_multi) { pid_t pid; pid = fork(); assert_se(pid >= 0); if (pid > 0) { siginfo_t si; assert_se(wait_for_terminate(pid, &si) >= 0); assert_se(si.si_code == CLD_EXITED); assert_se(si.si_status == EXIT_SUCCESS); return; } /* child */ test_rename_process_now("one", 1); test_rename_process_now("more", 0); /* longer than "one", hence truncated */ (void) setresuid(99, 99, 99); /* change uid when running privileged */ test_rename_process_now("time!", 0); test_rename_process_now("0", 1); /* shorter than "one", should fit */ _exit(EXIT_SUCCESS); } TEST(rename_process) { test_rename_process_one("foo", 1); /* should always fit */ test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */ test_rename_process_one("1234567", 1); /* should always fit */ } TEST(argv_help) { assert_se(argv_looks_like_help(1, STRV_MAKE("program"))); assert_se(argv_looks_like_help(2, STRV_MAKE("program", "help"))); assert_se(argv_looks_like_help(3, STRV_MAKE("program", "arg1", "--help"))); assert_se(argv_looks_like_help(4, STRV_MAKE("program", "arg1", "arg2", "-h"))); assert_se(!argv_looks_like_help(2, STRV_MAKE("program", "arg1"))); assert_se(!argv_looks_like_help(4, STRV_MAKE("program", "arg1", "arg2", "--h"))); } static int intro(void) { log_show_color(true); return EXIT_SUCCESS; } DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
4,426
32.285714
131
c
null
systemd-main/src/test/test-arphrd-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <linux/if_arp.h> #include "arphrd-util.h" #include "string-util.h" #include "tests.h" TEST(arphrd) { for (int i = 0; i <= ARPHRD_VOID + 1; i++) { const char *name; name = arphrd_to_name(i); if (name) { log_info("%i: %s", i, name); assert_se(arphrd_from_name(name) == i); } } assert_se(arphrd_to_name(ARPHRD_VOID + 1) == NULL); assert_se(arphrd_from_name("huddlduddl") == -EINVAL); assert_se(arphrd_from_name("") == -EINVAL); } DEFINE_TEST_MAIN(LOG_INFO);
672
23.925926
63
c
null
systemd-main/src/test/test-ask-password-api.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "ask-password-api.h" #include "strv.h" #include "tests.h" TEST(ask_password) { int r; _cleanup_strv_free_ char **ret = NULL; r = ask_password_tty(-1, "hello?", "da key", 0, ASK_PASSWORD_CONSOLE_COLOR, NULL, &ret); if (r == -ECANCELED) assert_se(ret == NULL); else { assert_se(r >= 0); assert_se(strv_length(ret) == 1); log_info("Got \"%s\"", *ret); } } DEFINE_TEST_MAIN(LOG_DEBUG);
556
24.318182
96
c
null
systemd-main/src/test/test-async.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <fcntl.h> #include <sys/prctl.h> #include <sys/wait.h> #include <unistd.h> #include "async.h" #include "fs-util.h" #include "path-util.h" #include "process-util.h" #include "signal-util.h" #include "tests.h" #include "tmpfile-util.h" TEST(test_asynchronous_sync) { assert_se(asynchronous_sync(NULL) >= 0); } TEST(asynchronous_close) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-asynchronous_close.XXXXXX"; int fd, r; fd = mkostemp_safe(name); assert_se(fd >= 0); asynchronous_close(fd); sleep(1); assert_se(fcntl(fd, F_GETFD) == -1); assert_se(errno == EBADF); r = safe_fork("(subreaper)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); assert(r >= 0); if (r == 0) { /* child */ assert(make_reaper_process(true) >= 0); fd = open("/dev/null", O_RDONLY|O_CLOEXEC); assert_se(fd >= 0); asynchronous_close(fd); sleep(1); assert_se(fcntl(fd, F_GETFD) == -1); assert_se(errno == EBADF); _exit(EXIT_SUCCESS); } } TEST(asynchronous_rm_rf) { _cleanup_free_ char *t = NULL, *k = NULL; int r; assert_se(mkdtemp_malloc(NULL, &t) >= 0); assert_se(k = path_join(t, "somefile")); assert_se(touch(k) >= 0); assert_se(asynchronous_rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); /* Do this once more, form a subreaper. Which is nice, because we can watch the async child even * though detached */ r = safe_fork("(subreaper)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); assert_se(r >= 0); if (r == 0) { _cleanup_free_ char *tt = NULL, *kk = NULL; /* child */ assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0); assert_se(make_reaper_process(true) >= 0); assert_se(mkdtemp_malloc(NULL, &tt) >= 0); assert_se(kk = path_join(tt, "somefile")); assert_se(touch(kk) >= 0); assert_se(asynchronous_rm_rf(tt, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); for (;;) { siginfo_t si = {}; assert_se(waitid(P_ALL, 0, &si, WEXITED) >= 0); if (access(tt, F_OK) < 0) { assert_se(errno == ENOENT); break; } /* wasn't the rm_rf() call. let's wait longer */ } _exit(EXIT_SUCCESS); } } DEFINE_TEST_MAIN(LOG_DEBUG);
2,845
27.178218
115
c
null
systemd-main/src/test/test-barrier.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * IPC barrier tests * These tests verify the correct behavior of the IPC Barrier implementation. * Note that the tests use alarm-timers to verify dead-locks and timeouts. These * might not work on slow machines where 20ms are too short to perform specific * operations (though, very unlikely). In case that turns out true, we have to * increase it at the slightly cost of lengthen test-duration on other machines. */ #include <stdio.h> #include <sys/time.h> #include <sys/wait.h> #include <unistd.h> #include "barrier.h" #include "errno-util.h" #include "tests.h" #include "time-util.h" #include "virt.h" /* 20ms to test deadlocks; All timings use multiples of this constant as * alarm/sleep timers. If this timeout is too small for slow machines to perform * the requested operations, we have to increase it. On an i7 this works fine * with 1ms base-time, so 20ms should be just fine for everyone. */ #define BASE_TIME (20 * USEC_PER_MSEC) static void set_alarm(usec_t usecs) { struct itimerval v = { }; timeval_store(&v.it_value, usecs); assert_se(setitimer(ITIMER_REAL, &v, NULL) >= 0); } #define TEST_BARRIER(_FUNCTION, _CHILD_CODE, _WAIT_CHILD, _PARENT_CODE, _WAIT_PARENT) \ TEST(_FUNCTION) { \ Barrier b = BARRIER_NULL; \ pid_t pid1, pid2; \ \ assert_se(barrier_create(&b) >= 0); \ assert_se(b.me > 0); \ assert_se(b.them > 0); \ assert_se(b.pipe[0] > 0); \ assert_se(b.pipe[1] > 0); \ \ pid1 = fork(); \ assert_se(pid1 >= 0); \ if (pid1 == 0) { \ barrier_set_role(&b, BARRIER_CHILD); \ { _CHILD_CODE; } \ exit(42); \ } \ \ pid2 = fork(); \ assert_se(pid2 >= 0); \ if (pid2 == 0) { \ barrier_set_role(&b, BARRIER_PARENT); \ { _PARENT_CODE; } \ exit(42); \ } \ \ barrier_destroy(&b); \ set_alarm(999999); \ { _WAIT_CHILD; } \ { _WAIT_PARENT; } \ set_alarm(0); \ } #define TEST_BARRIER_WAIT_SUCCESS(_pid) \ ({ \ int pidr, status; \ pidr = waitpid(_pid, &status, 0); \ assert_se(pidr == _pid); \ assert_se(WIFEXITED(status)); \ assert_se(WEXITSTATUS(status) == 42); \ }) #define TEST_BARRIER_WAIT_ALARM(_pid) \ ({ \ int pidr, status; \ pidr = waitpid(_pid, &status, 0); \ assert_se(pidr == _pid); \ assert_se(WIFSIGNALED(status)); \ assert_se(WTERMSIG(status) == SIGALRM); \ }) /* * Test basic sync points * This places a barrier in both processes and waits synchronously for them. * The timeout makes sure the sync works as expected. The usleep_safe() on one side * makes sure the exit of the parent does not overwrite previous barriers. Due * to the usleep_safe(), we know that the parent already exited, thus there's a * pending HUP on the pipe. However, the barrier_sync() prefers reads on the * eventfd, thus we can safely wait on the barrier. */ TEST_BARRIER(barrier_sync, ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); usleep_safe(BASE_TIME * 2); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test wait_next() * This places a barrier in the parent and syncs on it. The child sleeps while * the parent places the barrier and then waits for a barrier. The wait will * succeed as the child hasn't read the parent's barrier, yet. The following * barrier and sync synchronize the exit. */ TEST_BARRIER(barrier_wait_next, ({ usleep_safe(BASE_TIME); set_alarm(BASE_TIME * 10); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME * 4); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test wait_next() multiple times * This places two barriers in the parent and waits for the child to exit. The * child sleeps 20ms so both barriers _should_ be in place. It then waits for * the parent to place the next barrier twice. The first call will fetch both * barriers and return. However, the second call will stall as the parent does * not place a 3rd barrier (the sleep caught two barriers). wait_next() is does * not look at barrier-links so this stall is expected. Thus this test times * out. */ TEST_BARRIER(barrier_wait_next_twice, ({ usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_wait_next(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); usleep_safe(BASE_TIME * 4); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test wait_next() with local barriers * This is the same as test_barrier_wait_next_twice, but places local barriers * between both waits. This does not have any effect on the wait so it times out * like the other test. */ TEST_BARRIER(barrier_wait_next_twice_local, ({ usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); assert_se(barrier_wait_next(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); usleep_safe(BASE_TIME * 4); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test wait_next() with sync_next() * This is again the same as test_barrier_wait_next_twice but uses a * synced wait as the second wait. This works just fine because the local state * has no barriers placed, therefore, the remote is always in sync. */ TEST_BARRIER(barrier_wait_next_twice_sync, ({ usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_sync_next(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test wait_next() with sync_next() and local barriers * This is again the same as test_barrier_wait_next_twice_local but uses a * synced wait as the second wait. This works just fine because the local state * is in sync with the remote. */ TEST_BARRIER(barrier_wait_next_twice_local_sync, ({ usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync_next(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test sync_next() and sync() * This tests sync_*() synchronizations and makes sure they work fine if the * local state is behind the remote state. */ TEST_BARRIER(barrier_sync_next, ({ set_alarm(BASE_TIME * 10); assert_se(barrier_sync_next(&b)); assert_se(barrier_sync(&b)); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync_next(&b)); assert_se(barrier_sync_next(&b)); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME * 10); usleep_safe(BASE_TIME); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test sync_next() and sync() with local barriers * This tests timeouts if sync_*() is used if local barriers are placed but the * remote didn't place any. */ TEST_BARRIER(barrier_sync_next_local, ({ set_alarm(BASE_TIME); assert_se(barrier_place(&b)); assert_se(barrier_sync_next(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test sync_next() and sync() with local barriers and abortion * This is the same as test_barrier_sync_next_local but aborts the sync in the * parent. Therefore, the sync_next() succeeds just fine due to the abortion. */ TEST_BARRIER(barrier_sync_next_local_abort, ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(!barrier_sync_next(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ assert_se(barrier_abort(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test matched wait_abortion() * This runs wait_abortion() with remote abortion. */ TEST_BARRIER(barrier_wait_abortion, ({ set_alarm(BASE_TIME * 10); assert_se(barrier_wait_abortion(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ assert_se(barrier_abort(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test unmatched wait_abortion() * This runs wait_abortion() without any remote abortion going on. It thus must * timeout. */ TEST_BARRIER(barrier_wait_abortion_unmatched, ({ set_alarm(BASE_TIME); assert_se(barrier_wait_abortion(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test matched wait_abortion() with local abortion * This runs wait_abortion() with local and remote abortion. */ TEST_BARRIER(barrier_wait_abortion_local, ({ set_alarm(BASE_TIME * 10); assert_se(barrier_abort(&b)); assert_se(!barrier_wait_abortion(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ assert_se(barrier_abort(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test unmatched wait_abortion() with local abortion * This runs wait_abortion() with only local abortion. This must time out. */ TEST_BARRIER(barrier_wait_abortion_local_unmatched, ({ set_alarm(BASE_TIME); assert_se(barrier_abort(&b)); assert_se(!barrier_wait_abortion(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test child exit * Place barrier and sync with the child. The child only exits()s, which should * cause an implicit abortion and wake the parent. */ TEST_BARRIER(barrier_exit, ({ }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(!barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); /* * Test child exit with sleep * Same as test_barrier_exit but verifies the test really works due to the * child-exit. We add a usleep_safe() which triggers the alarm in the parent and * causes the test to time out. */ TEST_BARRIER(barrier_no_exit, ({ usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME); assert_se(barrier_place(&b)); assert_se(!barrier_sync(&b)); }), TEST_BARRIER_WAIT_ALARM(pid2)); /* * Test pending exit against sync * The parent places a barrier *and* exits. The 20ms wait in the child * guarantees both are pending. However, our logic prefers pending barriers over * pending exit-abortions (unlike normal abortions), thus the wait_next() must * succeed, same for the sync_next() as our local barrier-count is smaller than * the remote. Once we place a barrier our count is equal, so the sync still * succeeds. Only if we place one more barrier, we're ahead of the remote, thus * we will fail due to HUP on the pipe. */ TEST_BARRIER(barrier_pending_exit, ({ set_alarm(BASE_TIME * 4); usleep_safe(BASE_TIME * 2); assert_se(barrier_wait_next(&b)); assert_se(barrier_sync_next(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync_next(&b)); assert_se(barrier_place(&b)); assert_se(!barrier_sync_next(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ assert_se(barrier_place(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); static int intro(void) { if (!slow_tests_enabled()) return log_tests_skipped("slow tests are disabled"); /* * This test uses real-time alarms and sleeps to test for CPU races explicitly. This is highly * fragile if your system is under load. We already increased the BASE_TIME value to make the tests * more robust, but that just makes the test take significantly longer. Given the recent issues when * running the test in a virtualized environments, limit it to bare metal machines only, to minimize * false-positives in CIs. */ Virtualization v = detect_virtualization(); if (v < 0 && ERRNO_IS_PRIVILEGE(v)) return log_tests_skipped("Cannot detect virtualization"); if (v != VIRTUALIZATION_NONE) return log_tests_skipped("This test requires a baremetal machine"); return EXIT_SUCCESS; } DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
16,960
37.373303
108
c
null
systemd-main/src/test/test-bitmap.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "bitmap.h" int main(int argc, const char *argv[]) { _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL; unsigned n = UINT_MAX, i = 0; b = bitmap_new(); assert_se(b); assert_se(bitmap_ensure_allocated(&b) == 0); b = bitmap_free(b); assert_se(bitmap_ensure_allocated(&b) == 0); assert_se(bitmap_isset(b, 0) == false); assert_se(bitmap_isset(b, 1) == false); assert_se(bitmap_isset(b, 256) == false); assert_se(bitmap_isclear(b) == true); assert_se(bitmap_set(b, 0) == 0); assert_se(bitmap_isset(b, 0) == true); assert_se(bitmap_isclear(b) == false); bitmap_unset(b, 0); assert_se(bitmap_isset(b, 0) == false); assert_se(bitmap_isclear(b) == true); assert_se(bitmap_set(b, 1) == 0); assert_se(bitmap_isset(b, 1) == true); assert_se(bitmap_isclear(b) == false); bitmap_unset(b, 1); assert_se(bitmap_isset(b, 1) == false); assert_se(bitmap_isclear(b) == true); assert_se(bitmap_set(b, 256) == 0); assert_se(bitmap_isset(b, 256) == true); assert_se(bitmap_isclear(b) == false); bitmap_unset(b, 256); assert_se(bitmap_isset(b, 256) == false); assert_se(bitmap_isclear(b) == true); assert_se(bitmap_set(b, 32) == 0); bitmap_unset(b, 0); assert_se(bitmap_isset(b, 32) == true); bitmap_unset(b, 32); BITMAP_FOREACH(n, NULL) assert_not_reached(); assert_se(bitmap_set(b, 0) == 0); assert_se(bitmap_set(b, 1) == 0); assert_se(bitmap_set(b, 256) == 0); BITMAP_FOREACH(n, b) { assert_se(n == i); if (i == 0) i = 1; else if (i == 1) i = 256; else if (i == 256) i = UINT_MAX; } assert_se(i == UINT_MAX); i = 0; BITMAP_FOREACH(n, b) { assert_se(n == i); if (i == 0) i = 1; else if (i == 1) i = 256; else if (i == 256) i = UINT_MAX; } assert_se(i == UINT_MAX); b2 = bitmap_copy(b); assert_se(b2); assert_se(bitmap_equal(b, b2) == true); assert_se(bitmap_equal(b, b) == true); assert_se(bitmap_equal(b, NULL) == false); assert_se(bitmap_equal(NULL, b) == false); assert_se(bitmap_equal(NULL, NULL) == true); bitmap_clear(b); assert_se(bitmap_isclear(b) == true); assert_se(bitmap_equal(b, b2) == false); b2 = bitmap_free(b2); assert_se(bitmap_set(b, UINT_MAX) == -ERANGE); b = bitmap_free(b); assert_se(bitmap_ensure_allocated(&b) == 0); assert_se(bitmap_ensure_allocated(&b2) == 0); assert_se(bitmap_equal(b, b2)); assert_se(bitmap_set(b, 0) == 0); bitmap_unset(b, 0); assert_se(bitmap_equal(b, b2)); assert_se(bitmap_set(b, 1) == 0); bitmap_clear(b); assert_se(bitmap_equal(b, b2)); assert_se(bitmap_set(b, 0) == 0); assert_se(bitmap_set(b2, 0) == 0); assert_se(bitmap_equal(b, b2)); return 0; }
3,409
28.912281
59
c
null
systemd-main/src/test/test-blockdev-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "blockdev-util.h" #include "errno-util.h" #include "tests.h" static void test_path_is_encrypted_one(const char *p, int expect) { int r; r = path_is_encrypted(p); if (r == -ENOENT || (r < 0 && ERRNO_IS_PRIVILEGE(r))) /* This might fail, if btrfs is used and we run in a container. In that case we cannot * resolve the device node paths that BTRFS_IOC_DEV_INFO returns, because the device nodes * are unlikely to exist in the container. But if we can't stat() them we cannot determine * the dev_t of them, and thus cannot figure out if they are enrypted. Hence let's just * ignore ENOENT here. Also skip the test if we lack privileges. */ return; assert_se(r >= 0); log_info("%s encrypted: %s", p, yes_no(r)); assert_se(expect < 0 || ((r > 0) == (expect > 0))); } TEST(path_is_encrypted) { int booted = sd_booted(); /* If this is run in build environments such as koji, /dev/ might be a * regular fs. Don't assume too much if not running under systemd. */ log_info("/* %s (sd_booted=%d) */", __func__, booted); test_path_is_encrypted_one("/home", -1); test_path_is_encrypted_one("/var", -1); test_path_is_encrypted_one("/", -1); test_path_is_encrypted_one("/proc", false); test_path_is_encrypted_one("/sys", false); test_path_is_encrypted_one("/dev", booted > 0 ? false : -1); } DEFINE_TEST_MAIN(LOG_INFO);
1,619
39.5
106
c
null
systemd-main/src/test/test-boot-timestamps.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/types.h> #include <unistd.h> #include "acpi-fpdt.h" #include "boot-timestamps.h" #include "efi-loader.h" #include "errno-util.h" #include "log.h" #include "tests.h" static int test_acpi_fpdt(void) { usec_t loader_start, loader_exit; int r; r = acpi_get_boot_usec(&loader_start, &loader_exit); if (r < 0) { bool ok = IN_SET(r, -ENOENT, -ENODATA, -ERANGE) || ERRNO_IS_PRIVILEGE(r); log_full_errno(ok ? LOG_DEBUG : LOG_ERR, r, "Failed to read ACPI FPDT: %m"); return ok ? 0 : r; } log_info("ACPI FPDT: loader start=%s exit=%s duration=%s", FORMAT_TIMESPAN(loader_start, USEC_PER_MSEC), FORMAT_TIMESPAN(loader_exit, USEC_PER_MSEC), FORMAT_TIMESPAN(loader_exit - loader_start, USEC_PER_MSEC)); return 1; } static int test_efi_loader(void) { usec_t loader_start, loader_exit; int r; r = efi_loader_get_boot_usec(&loader_start, &loader_exit); if (r < 0) { bool ok = IN_SET(r, -ENOENT, -EOPNOTSUPP) || ERRNO_IS_PRIVILEGE(r); log_full_errno(ok ? LOG_DEBUG : LOG_ERR, r, "Failed to read EFI loader data: %m"); return ok ? 0 : r; } log_info("EFI Loader: start=%s exit=%s duration=%s", FORMAT_TIMESPAN(loader_start, USEC_PER_MSEC), FORMAT_TIMESPAN(loader_exit, USEC_PER_MSEC), FORMAT_TIMESPAN(loader_exit - loader_start, USEC_PER_MSEC)); return 1; } static int test_boot_timestamps(void) { dual_timestamp fw, l, k; int r; dual_timestamp_from_monotonic(&k, 0); r = boot_timestamps(NULL, &fw, &l); if (r < 0) { bool ok = IN_SET(r, -ENOENT, -EOPNOTSUPP) || ERRNO_IS_PRIVILEGE(r); log_full_errno(ok ? LOG_DEBUG : LOG_ERR, r, "Failed to read variables: %m"); return ok ? 0 : r; } log_info("Firmware began %s before kernel.", FORMAT_TIMESPAN(fw.monotonic, 0)); log_info("Loader began %s before kernel.", FORMAT_TIMESPAN(l.monotonic, 0)); log_info("Firmware began %s.", FORMAT_TIMESTAMP(fw.realtime)); log_info("Loader began %s.", FORMAT_TIMESTAMP(l.realtime)); log_info("Kernel began %s.", FORMAT_TIMESTAMP(k.realtime)); return 1; } int main(int argc, char* argv[]) { int p, q, r; test_setup_logging(LOG_DEBUG); p = test_acpi_fpdt(); assert_se(p >= 0); q = test_efi_loader(); assert_se(q >= 0); r = test_boot_timestamps(); assert_se(r >= 0); if (p == 0 && q == 0 && r == 0) return log_tests_skipped("access to firmware variables not possible"); return EXIT_SUCCESS; }
2,883
31.044444
98
c
null
systemd-main/src/test/test-bootspec.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "bootspec.h" #include "fileio.h" #include "path-util.h" #include "rm-rf.h" #include "tests.h" #include "tmpfile-util.h" TEST_RET(bootspec_sort) { static const struct { const char *fname; const char *contents; } entries[] = { { .fname = "a-10.conf", .contents = "title A\n" "version 10\n" "machine-id dd235d00696545768f6f693bfd23b15f\n", }, { .fname = "a-5.conf", .contents = "title A\n" "version 5\n" "machine-id dd235d00696545768f6f693bfd23b15f\n", }, { .fname = "b.conf", .contents = "title B\n" "version 3\n" "machine-id b75451ad92f94feeab50b0b442768dbd\n", }, { .fname = "c.conf", .contents = "title C\n" "sort-key xxxx\n" "version 5\n" "machine-id 309de666fd5044268a9a26541ac93176\n", }, { .fname = "cx.conf", .contents = "title C\n" "sort-key xxxx\n" "version 10\n" "machine-id 309de666fd5044268a9a26541ac93176\n", }, { .fname = "d.conf", .contents = "title D\n" "sort-key kkkk\n" "version 100\n" "machine-id 81c6e3147cf544c19006af023e22b292\n", }, }; _cleanup_(rm_rf_physical_and_freep) char *d = NULL; _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; assert_se(mkdtemp_malloc("/tmp/bootspec-testXXXXXX", &d) >= 0); for (size_t i = 0; i < ELEMENTSOF(entries); i++) { _cleanup_free_ char *j = NULL; j = path_join(d, "/loader/entries/", entries[i].fname); assert_se(j); assert_se(write_string_file(j, entries[i].contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) >= 0); } assert_se(boot_config_load(&config, d, NULL) >= 0); assert_se(config.n_entries == 6); /* First, because has sort key, and its the lowest one */ assert_se(streq(config.entries[0].id, "d.conf")); /* These two have a sort key, and newest must be first */ assert_se(streq(config.entries[1].id, "cx.conf")); assert_se(streq(config.entries[2].id, "c.conf")); /* The following ones have no sort key, hence order by version compared ids, lowest first */ assert_se(streq(config.entries[3].id, "b.conf")); assert_se(streq(config.entries[4].id, "a-10.conf")); assert_se(streq(config.entries[5].id, "a-5.conf")); return 0; } static void test_extract_tries_one(const char *fname, int ret, const char *stripped, unsigned tries_left, unsigned tries_done) { _cleanup_free_ char *p = NULL; unsigned l, d; assert_se(boot_filename_extract_tries(fname, &p, &l, &d) == ret); if (ret < 0) return; assert_se(streq_ptr(p, stripped)); assert_se(l == tries_left); assert_se(d == tries_done); } TEST_RET(bootspec_extract_tries) { test_extract_tries_one("foo.conf", 0, "foo.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("foo+0.conf", 0, "foo.conf", 0, UINT_MAX); test_extract_tries_one("foo+1.conf", 0, "foo.conf", 1, UINT_MAX); test_extract_tries_one("foo+2.conf", 0, "foo.conf", 2, UINT_MAX); test_extract_tries_one("foo+33.conf", 0, "foo.conf", 33, UINT_MAX); assert_cc(INT_MAX == INT32_MAX); test_extract_tries_one("foo+2147483647.conf", 0, "foo.conf", 2147483647, UINT_MAX); test_extract_tries_one("foo+2147483648.conf", -ERANGE, NULL, UINT_MAX, UINT_MAX); test_extract_tries_one("foo+33-0.conf", 0, "foo.conf", 33, 0); test_extract_tries_one("foo+33-1.conf", 0, "foo.conf", 33, 1); test_extract_tries_one("foo+33-107.conf", 0, "foo.conf", 33, 107); test_extract_tries_one("foo+33-107.efi", 0, "foo.efi", 33, 107); test_extract_tries_one("foo+33-2147483647.conf", 0, "foo.conf", 33, 2147483647); test_extract_tries_one("foo+33-2147483648.conf", -ERANGE, NULL, UINT_MAX, UINT_MAX); test_extract_tries_one("foo+007-000008.conf", 0, "foo.conf", 7, 8); test_extract_tries_one("foo-1.conf", 0, "foo-1.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("foo-999.conf", 0, "foo-999.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("foo-.conf", 0, "foo-.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("foo+.conf", 0, "foo+.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("+.conf", 0, "+.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("-.conf", 0, "-.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("", 0, "", UINT_MAX, UINT_MAX); test_extract_tries_one("+1.", 0, ".", 1, UINT_MAX); test_extract_tries_one("+1-7.", 0, ".", 1, 7); test_extract_tries_one("some+name+24324-22.efi", 0, "some+name.efi", 24324, 22); test_extract_tries_one("sels+2-3+7-6.", 0, "sels+2-3.", 7, 6); test_extract_tries_one("a+1-2..", 0, "a+1-2..", UINT_MAX, UINT_MAX); test_extract_tries_one("ses.sgesge.+4-1.efi", 0, "ses.sgesge..efi", 4, 1); test_extract_tries_one("abc+0x4.conf", 0, "abc+0x4.conf", UINT_MAX, UINT_MAX); test_extract_tries_one("def+1-0x3.conf", 0, "def+1-0x3.conf", UINT_MAX, UINT_MAX); return 0; } DEFINE_TEST_MAIN(LOG_INFO);
6,163
39.287582
129
c
null
systemd-main/src/test/test-bpf-devices.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include "alloc-util.h" #include "bpf-devices.h" #include "bpf-program.h" #include "cgroup-setup.h" #include "errno-list.h" #include "fd-util.h" #include "fs-util.h" #include "path-util.h" #include "tests.h" static void test_policy_closed(const char *cgroup_path, BPFProgram **installed_prog) { _cleanup_(bpf_program_freep) BPFProgram *prog = NULL; unsigned wrong = 0; int r; log_info("/* %s */", __func__); r = bpf_devices_cgroup_init(&prog, CGROUP_DEVICE_POLICY_CLOSED, true); assert_se(r >= 0); r = bpf_devices_allow_list_static(prog, cgroup_path); assert_se(r >= 0); r = bpf_devices_apply_policy(&prog, CGROUP_DEVICE_POLICY_CLOSED, true, cgroup_path, installed_prog); assert_se(r >= 0); FOREACH_STRING(s, "/dev/null", "/dev/zero", "/dev/full", "/dev/random", "/dev/urandom", "/dev/tty", "/dev/ptmx") { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd < 0 && errno == EPERM; /* We ignore errors other than EPERM, e.g. ENOENT or ENXIO */ fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 < 0 && errno == EPERM; } assert_se(wrong == 0); } static void test_policy_strict(const char *cgroup_path, BPFProgram **installed_prog) { _cleanup_(bpf_program_freep) BPFProgram *prog = NULL; unsigned wrong = 0; int r; log_info("/* %s */", __func__); r = bpf_devices_cgroup_init(&prog, CGROUP_DEVICE_POLICY_STRICT, true); assert_se(r >= 0); r = bpf_devices_allow_list_device(prog, cgroup_path, "/dev/null", "rw"); assert_se(r >= 0); r = bpf_devices_allow_list_device(prog, cgroup_path, "/dev/random", "r"); assert_se(r >= 0); r = bpf_devices_allow_list_device(prog, cgroup_path, "/dev/zero", "w"); assert_se(r >= 0); r = bpf_devices_apply_policy(&prog, CGROUP_DEVICE_POLICY_STRICT, true, cgroup_path, installed_prog); assert_se(r >= 0); { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; const char *s = "/dev/null"; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd < 0; fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 < 0; } { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; const char *s = "/dev/random"; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd < 0; fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 >= 0; } { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; const char *s = "/dev/zero"; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd >= 0; fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 < 0; } { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; const char *s = "/dev/full"; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd >= 0; fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 >= 0; } assert_se(wrong == 0); } static void test_policy_allow_list_major(const char *pattern, const char *cgroup_path, BPFProgram **installed_prog) { _cleanup_(bpf_program_freep) BPFProgram *prog = NULL; unsigned wrong = 0; int r; log_info("/* %s(%s) */", __func__, pattern); r = bpf_devices_cgroup_init(&prog, CGROUP_DEVICE_POLICY_STRICT, true); assert_se(r >= 0); r = bpf_devices_allow_list_major(prog, cgroup_path, pattern, 'c', "rw"); assert_se(r >= 0); r = bpf_devices_apply_policy(&prog, CGROUP_DEVICE_POLICY_STRICT, true, cgroup_path, installed_prog); assert_se(r >= 0); /* /dev/null, /dev/full have major==1, /dev/tty has major==5 */ { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; const char *s = "/dev/null"; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd < 0; fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 < 0; } { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; const char *s = "/dev/full"; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd < 0; fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 < 0; } { _cleanup_close_ int fd = -EBADF, fd2 = -EBADF; const char *s = "/dev/tty"; fd = open(s, O_CLOEXEC|O_RDONLY|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd >= 0; fd2 = open(s, O_CLOEXEC|O_WRONLY|O_NOCTTY); log_debug("open(%s, \"w\") = %d/%s", s, fd2, fd2 < 0 ? errno_to_name(errno) : "-"); wrong += fd2 >= 0; } assert_se(wrong == 0); } static void test_policy_allow_list_major_star(char type, const char *cgroup_path, BPFProgram **installed_prog) { _cleanup_(bpf_program_freep) BPFProgram *prog = NULL; unsigned wrong = 0; int r; log_info("/* %s(type=%c) */", __func__, type); r = bpf_devices_cgroup_init(&prog, CGROUP_DEVICE_POLICY_STRICT, true); assert_se(r >= 0); r = bpf_devices_allow_list_major(prog, cgroup_path, "*", type, "rw"); assert_se(r >= 0); r = bpf_devices_apply_policy(&prog, CGROUP_DEVICE_POLICY_STRICT, true, cgroup_path, installed_prog); assert_se(r >= 0); { _cleanup_close_ int fd = -EBADF; const char *s = "/dev/null"; fd = open(s, O_CLOEXEC|O_RDWR|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); if (type == 'c') wrong += fd < 0; else wrong += fd >= 0; } assert_se(wrong == 0); } static void test_policy_empty(bool add_mismatched, const char *cgroup_path, BPFProgram **installed_prog) { _cleanup_(bpf_program_freep) BPFProgram *prog = NULL; unsigned wrong = 0; int r; log_info("/* %s(add_mismatched=%s) */", __func__, yes_no(add_mismatched)); r = bpf_devices_cgroup_init(&prog, CGROUP_DEVICE_POLICY_STRICT, add_mismatched); assert_se(r >= 0); if (add_mismatched) { r = bpf_devices_allow_list_major(prog, cgroup_path, "foobarxxx", 'c', "rw"); assert_se(r < 0); } r = bpf_devices_apply_policy(&prog, CGROUP_DEVICE_POLICY_STRICT, false, cgroup_path, installed_prog); assert_se(r >= 0); { _cleanup_close_ int fd = -EBADF; const char *s = "/dev/null"; fd = open(s, O_CLOEXEC|O_RDWR|O_NOCTTY); log_debug("open(%s, \"r\") = %d/%s", s, fd, fd < 0 ? errno_to_name(errno) : "-"); wrong += fd >= 0; } assert_se(wrong == 0); } int main(int argc, char *argv[]) { _cleanup_free_ char *cgroup = NULL, *parent = NULL; _cleanup_(rmdir_and_freep) char *controller_path = NULL; CGroupMask supported; struct rlimit rl; int r; test_setup_logging(LOG_DEBUG); assert_se(getrlimit(RLIMIT_MEMLOCK, &rl) >= 0); rl.rlim_cur = rl.rlim_max = MAX(rl.rlim_max, CAN_MEMLOCK_SIZE); (void) setrlimit(RLIMIT_MEMLOCK, &rl); r = cg_all_unified(); if (r <= 0) return log_tests_skipped("We don't seem to be running with unified cgroup hierarchy"); if (!can_memlock()) return log_tests_skipped("Can't use mlock()"); r = enter_cgroup_subroot(&cgroup); if (r == -ENOMEDIUM) return log_tests_skipped("cgroupfs not available"); r = bpf_devices_supported(); if (!r) return log_tests_skipped("BPF device filter not supported"); assert_se(r == 1); r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, cgroup, NULL, &controller_path); assert_se(r >= 0); _cleanup_(bpf_program_freep) BPFProgram *prog = NULL; test_policy_closed(cgroup, &prog); test_policy_strict(cgroup, &prog); test_policy_allow_list_major("mem", cgroup, &prog); test_policy_allow_list_major("1", cgroup, &prog); test_policy_allow_list_major_star('c', cgroup, &prog); test_policy_allow_list_major_star('b', cgroup, &prog); test_policy_empty(false, cgroup, &prog); test_policy_empty(true, cgroup, &prog); assert_se(path_extract_directory(cgroup, &parent) >= 0); assert_se(cg_mask_supported(&supported) >= 0); r = cg_attach_everywhere(supported, parent, 0, NULL, NULL); assert_se(r >= 0); return 0; }
10,933
34.732026
117
c
null
systemd-main/src/test/test-btrfs.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <fcntl.h> #include "btrfs-util.h" #include "fd-util.h" #include "fs-util.h" #include "fileio.h" #include "format-util.h" #include "log.h" #include "string-util.h" int main(int argc, char *argv[]) { BtrfsQuotaInfo quota; int r, fd; fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY); if (fd < 0) log_error_errno(errno, "Failed to open root directory: %m"); else { BtrfsSubvolInfo info; r = btrfs_subvol_get_info_fd(fd, 0, &info); if (r < 0) log_error_errno(r, "Failed to get subvolume info: %m"); else { log_info("otime: %s", FORMAT_TIMESTAMP(info.otime)); log_info("read-only (search): %s", yes_no(info.read_only)); } r = btrfs_qgroup_get_quota_fd(fd, 0, &quota); if (r < 0) log_error_errno(r, "Failed to get quota info: %m"); else { log_info("referenced: %s", strna(FORMAT_BYTES(quota.referenced))); log_info("exclusive: %s", strna(FORMAT_BYTES(quota.exclusive))); log_info("referenced_max: %s", strna(FORMAT_BYTES(quota.referenced_max))); log_info("exclusive_max: %s", strna(FORMAT_BYTES(quota.exclusive_max))); } r = btrfs_subvol_get_read_only_fd(fd); if (r < 0) log_error_errno(r, "Failed to get read only flag: %m"); else log_info("read-only (ioctl): %s", yes_no(r)); safe_close(fd); } r = btrfs_subvol_make("/xxxtest"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); r = write_string_file("/xxxtest/file", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE); if (r < 0) log_error_errno(r, "Failed to write file: %m"); r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxtest", AT_FDCWD, "/xxxtest2", 0); if (r < 0) log_error_errno(r, "Failed to make snapshot: %m"); r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxtest", AT_FDCWD, "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY); if (r < 0) log_error_errno(r, "Failed to make snapshot: %m"); r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxtest", AT_FDCWD, "/xxxtest4", BTRFS_SNAPSHOT_LOCK_BSD); if (r < 0) log_error_errno(r, "Failed to make snapshot: %m"); if (r >= 0) assert_se(xopenat_lock(AT_FDCWD, "/xxxtest4", 0, 0, 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN); safe_close(r); r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA); if (r < 0) log_error_errno(r, "Failed to remove subvolume: %m"); r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA); if (r < 0) log_error_errno(r, "Failed to remove subvolume: %m"); r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA); if (r < 0) log_error_errno(r, "Failed to remove subvolume: %m"); r = btrfs_subvol_remove("/xxxtest4", BTRFS_REMOVE_QUOTA); if (r < 0) log_error_errno(r, "Failed to remove subvolume: %m"); r = btrfs_subvol_snapshot_at(AT_FDCWD, "/etc", AT_FDCWD, "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY); if (r < 0) log_error_errno(r, "Failed to make snapshot: %m"); r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA); if (r < 0) log_error_errno(r, "Failed to remove subvolume: %m"); r = btrfs_subvol_make("/xxxrectest"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); r = btrfs_subvol_make("/xxxrectest/xxxrectest2"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); r = btrfs_subvol_make("/xxxrectest/xxxrectest3"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); r = btrfs_subvol_make("/xxxrectest/xxxrectest3/sub"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); if (mkdir("/xxxrectest/dir", 0755) < 0) log_error_errno(errno, "Failed to make directory: %m"); r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0) log_error_errno(errno, "Failed to make directory: %m"); r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); if (mkdir("/xxxrectest/mnt", 0755) < 0) log_error_errno(errno, "Failed to make directory: %m"); r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxrectest", AT_FDCWD, "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE); if (r < 0) log_error_errno(r, "Failed to snapshot subvolume: %m"); r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); if (r < 0) log_error_errno(r, "Failed to recursively remove subvolume: %m"); r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); if (r < 0) log_error_errno(r, "Failed to recursively remove subvolume: %m"); r = btrfs_subvol_make("/xxxquotatest"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); r = btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true); if (r < 0) log_error_errno(r, "Failed to set up auto qgroup: %m"); r = btrfs_subvol_make("/xxxquotatest/beneath"); if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); r = btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false); if (r < 0) log_error_errno(r, "Failed to set up auto qgroup: %m"); r = btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024); if (r < 0) log_error_errno(r, "Failed to set up quota limit: %m"); r = btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024); if (r < 0) log_error_errno(r, "Failed to set up quota limit: %m"); r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxquotatest", AT_FDCWD, "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA); if (r < 0) log_error_errno(r, "Failed to set up snapshot: %m"); r = btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, &quota); if (r < 0) log_error_errno(r, "Failed to query quota: %m"); if (r >= 0) assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024); r = btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, &quota); if (r < 0) log_error_errno(r, "Failed to query quota: %m"); if (r >= 0) assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024); r = btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); if (r < 0) log_error_errno(r, "Failed remove subvolume: %m"); r = btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); if (r < 0) log_error_errno(r, "Failed remove subvolume: %m"); return 0; }
7,829
38.746193
114
c
null
systemd-main/src/test/test-bus-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "bus-util.h" #include "log.h" #include "tests.h" static int callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { return 1; } static void destroy_callback(void *userdata) { int *n_called = userdata; (*n_called) ++; } TEST(destroy_callback) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; sd_bus_slot *slot = NULL; sd_bus_destroy_t t; int r, n_called = 0; r = bus_open_system_watch_bind_with_description(&bus, "test-bus"); if (r < 0) { log_error_errno(r, "Failed to connect to bus: %m"); return; } r = sd_bus_request_name_async(bus, &slot, "org.freedesktop.systemd.test-bus-util", 0, callback, &n_called); assert_se(r == 1); assert_se(sd_bus_slot_get_destroy_callback(slot, NULL) == 0); assert_se(sd_bus_slot_get_destroy_callback(slot, &t) == 0); assert_se(sd_bus_slot_set_destroy_callback(slot, destroy_callback) == 0); assert_se(sd_bus_slot_get_destroy_callback(slot, NULL) == 1); assert_se(sd_bus_slot_get_destroy_callback(slot, &t) == 1); assert_se(t == destroy_callback); /* Force cleanup so we can look at n_called */ assert_se(n_called == 0); sd_bus_slot_unref(slot); assert_se(n_called == 1); } DEFINE_TEST_MAIN(LOG_DEBUG);
1,437
28.958333
115
c
null
systemd-main/src/test/test-calendarspec.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "calendarspec.h" #include "env-util.h" #include "errno-util.h" #include "string-util.h" #include "tests.h" static void _test_one(int line, const char *input, const char *output) { _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; _cleanup_free_ char *p = NULL, *q = NULL; usec_t u; int r; r = calendar_spec_from_string(input, &c); if (r < 0) log_error_errno(r, "Failed to parse \"%s\": %m", input); assert_se(r >= 0); assert_se(calendar_spec_to_string(c, &p) >= 0); log_info("line %d: \"%s\" → \"%s\"%s%s", line, input, p, !streq(p, output) ? " expected:" : "", !streq(p, output) ? output : ""); assert_se(streq(p, output)); u = now(CLOCK_REALTIME); r = calendar_spec_next_usec(c, u, &u); log_info("Next: %s", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP(u)); c = calendar_spec_free(c); assert_se(calendar_spec_from_string(p, &c) >= 0); assert_se(calendar_spec_to_string(c, &q) >= 0); assert_se(streq(q, p)); } #define test_one(input, output) _test_one(__LINE__, input, output) static void _test_next(int line, const char *input, const char *new_tz, usec_t after, usec_t expect) { _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; usec_t u; char *old_tz; int r; old_tz = getenv("TZ"); if (old_tz) old_tz = strdupa_safe(old_tz); if (!isempty(new_tz)) new_tz = strjoina(":", new_tz); assert_se(set_unset_env("TZ", new_tz, true) == 0); tzset(); assert_se(calendar_spec_from_string(input, &c) >= 0); log_info("line %d: \"%s\" new_tz=%s", line, input, strnull(new_tz)); u = after; r = calendar_spec_next_usec(c, after, &u); log_info("At: %s", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US)); if (expect != USEC_INFINITY) assert_se(r >= 0 && u == expect); else assert_se(r == -ENOENT); assert_se(set_unset_env("TZ", old_tz, true) == 0); tzset(); } #define test_next(input, new_tz, after, expect) _test_next(__LINE__, input,new_tz,after,expect) TEST(timestamp) { char buf[FORMAT_TIMESTAMP_MAX]; _cleanup_free_ char *t = NULL; _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; usec_t x, y; /* Ensure that a timestamp is also a valid calendar specification. Convert forth and back */ x = now(CLOCK_REALTIME); assert_se(format_timestamp_style(buf, sizeof buf, x, TIMESTAMP_US)); log_info("%s", buf); assert_se(calendar_spec_from_string(buf, &c) >= 0); assert_se(calendar_spec_to_string(c, &t) >= 0); log_info("%s", t); assert_se(parse_timestamp(t, &y) >= 0); assert_se(y == x); } TEST(hourly_bug_4031) { _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; usec_t n, u, w; int r; assert_se(calendar_spec_from_string("hourly", &c) >= 0); n = now(CLOCK_REALTIME); assert_se((r = calendar_spec_next_usec(c, n, &u)) >= 0); log_info("Now: %s (%"PRIu64")", FORMAT_TIMESTAMP_STYLE(n, TIMESTAMP_US), n); log_info("Next hourly: %s (%"PRIu64")", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US), u); assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0); log_info("Next hourly: %s (%"PRIu64")", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP_STYLE(w, TIMESTAMP_US), w); assert_se(n < u); assert_se(u <= n + USEC_PER_HOUR); assert_se(u < w); assert_se(w <= u + USEC_PER_HOUR); } TEST(calendar_spec_one) { test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); test_one("Sat,Thu,Mon..Wed,Sat..Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00"); test_one("Wed *-1", "Wed *-*-01 00:00:00"); test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00"); test_one("Wed..Wed,Wed *-1", "Wed *-*-01 00:00:00"); test_one("Wed, 17:48", "Wed *-*-* 17:48:00"); test_one("Wednesday,", "Wed *-*-* 00:00:00"); test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); test_one("Wed..Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); test_one("*-*-7 0:0:0", "*-*-07 00:00:00"); test_one("10-15", "*-10-15 00:00:00"); test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00"); test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45"); test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00"); test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45"); test_one("03-05 08:05:40", "*-03-05 08:05:40"); test_one("08:05:40", "*-*-* 08:05:40"); test_one("05:40", "*-*-* 05:40:00"); test_one("Sat,Sun 12-05 08:05:40", "Sat,Sun *-12-05 08:05:40"); test_one("Sat,Sun 08:05:40", "Sat,Sun *-*-* 08:05:40"); test_one("2003-03-05 05:40", "2003-03-05 05:40:00"); test_one("2003-03-05", "2003-03-05 00:00:00"); test_one("03-05", "*-03-05 00:00:00"); test_one("hourly", "*-*-* *:00:00"); test_one("daily", "*-*-* 00:00:00"); test_one("monthly", "*-*-01 00:00:00"); test_one("weekly", "Mon *-*-* 00:00:00"); test_one("minutely", "*-*-* *:*:00"); test_one("quarterly", "*-01,04,07,10-01 00:00:00"); test_one("semi-annually", "*-01,07-01 00:00:00"); test_one("annually", "*-01-01 00:00:00"); test_one("*:2/3", "*-*-* *:02/3:00"); test_one("2015-10-25 01:00:00 uTc", "2015-10-25 01:00:00 UTC"); test_one("2015-10-25 01:00:00 Asia/Vladivostok", "2015-10-25 01:00:00 Asia/Vladivostok"); test_one("weekly Pacific/Auckland", "Mon *-*-* 00:00:00 Pacific/Auckland"); test_one("2016-03-27 03:17:00.4200005", "2016-03-27 03:17:00.420001"); test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000"); test_one("9..11,13:00,30", "*-*-* 09..11,13:00,30:00"); test_one("1..3-1..3 1..3:1..3", "*-01..03-01..03 01..03:01..03:00"); test_one("00:00:1.125..2.125", "*-*-* 00:00:01.125000..02.125000"); test_one("00:00:1.0..3.8", "*-*-* 00:00:01..03"); test_one("00:00:01..03", "*-*-* 00:00:01..03"); test_one("00:00:01/2,02..03", "*-*-* 00:00:01/2,02..03"); test_one("*:4,30:0..3", "*-*-* *:04,30:00..03"); test_one("*:4,30:0/1", "*-*-* *:04,30:*"); test_one("*:4,30:0/1,3,5", "*-*-* *:04,30:*"); test_one("*-*~1 Utc", "*-*~01 00:00:00 UTC"); test_one("*-*~05,3 ", "*-*~03,05 00:00:00"); test_one("*-*~* 00:00:00", "*-*-* 00:00:00"); test_one("Monday", "Mon *-*-* 00:00:00"); test_one("Monday *-*-*", "Mon *-*-* 00:00:00"); test_one("*-*-*", "*-*-* 00:00:00"); test_one("*:*:*", "*-*-* *:*:*"); test_one("*:*", "*-*-* *:*:00"); test_one("12:*", "*-*-* 12:*:00"); test_one("*:30", "*-*-* *:30:00"); test_one("93..00-*-*", "1993..2000-*-* 00:00:00"); test_one("00..07-*-*", "2000..2007-*-* 00:00:00"); test_one("*:20..39/5", "*-*-* *:20..35/5:00"); test_one("00:00:20..40/1", "*-*-* 00:00:20..40"); test_one("*~03/1,03..05", "*-*~03/1,03..05 00:00:00"); /* UNIX timestamps are always UTC */ test_one("@1493187147", "2017-04-26 06:12:27 UTC"); test_one("@1493187147 UTC", "2017-04-26 06:12:27 UTC"); test_one("@0", "1970-01-01 00:00:00 UTC"); test_one("@0 UTC", "1970-01-01 00:00:00 UTC"); test_one("*:05..05", "*-*-* *:05:00"); test_one("*:05..10/6", "*-*-* *:05:00"); } TEST(calendar_spec_next) { test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000); test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000); test_next("2016-03-27 03:17:00", "EET", 12345, -1); test_next("2016-03-27 03:17:00 UTC", NULL, 12345, 1459048620000000); test_next("2016-03-27 03:17:00 UTC", "", 12345, 1459048620000000); test_next("2016-03-27 03:17:00 UTC", "CET", 12345, 1459048620000000); test_next("2016-03-27 03:17:00 UTC", "EET", 12345, 1459048620000000); test_next("2016-03-27 03:17:00.420000001 UTC", "EET", 12345, 1459048620420000); test_next("2016-03-27 03:17:00.4200005 UTC", "EET", 12345, 1459048620420001); test_next("2015-11-13 09:11:23.42", "EET", 12345, 1447398683420000); test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683420000, 1447398685190000); test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683419999, 1447398683420000); test_next("Sun 16:00:00", "CET", 1456041600123456, 1456066800000000); test_next("*-04-31", "", 12345, -1); test_next("2016-02~01 UTC", "", 12345, 1456704000000000); test_next("Mon 2017-05~01..07 UTC", "", 12345, 1496016000000000); test_next("Mon 2017-05~07/1 UTC", "", 12345, 1496016000000000); test_next("*-*-01/5 04:00:00 UTC", "", 1646010000000000, 1646107200000000); test_next("*-01/7-01 04:00:00 UTC", "", 1664607600000000, 1672545600000000); test_next("2017-08-06 9,11,13,15,17:00 UTC", "", 1502029800000000, 1502031600000000); test_next("2017-08-06 9..17/2:00 UTC", "", 1502029800000000, 1502031600000000); test_next("2016-12-* 3..21/6:00 UTC", "", 1482613200000001, 1482634800000000); test_next("2017-09-24 03:30:00 Pacific/Auckland", "", 12345, 1506177000000000); /* Due to daylight saving time - 2017-09-24 02:30:00 does not exist */ test_next("2017-09-24 02:30:00 Pacific/Auckland", "", 12345, -1); test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 12345, 1491053400000000); /* Confirm that even though it's a time change here (backward) 02:30 happens only once */ test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 1491053400000000, -1); test_next("2017-04-02 03:30:00 Pacific/Auckland", "", 12345, 1491060600000000); /* Confirm that timezones in the Spec work regardless of current timezone */ test_next("2017-09-09 20:42:00 Pacific/Auckland", "", 12345, 1504946520000000); test_next("2017-09-09 20:42:00 Pacific/Auckland", "EET", 12345, 1504946520000000); /* Check that we don't start looping if mktime() moves us backwards */ test_next("Sun *-*-* 01:00:00 Europe/Dublin", "", 1616412478000000, 1617494400000000); test_next("Sun *-*-* 01:00:00 Europe/Dublin", "IST", 1616412478000000, 1617494400000000); } TEST(calendar_spec_from_string) { CalendarSpec *c; assert_se(calendar_spec_from_string("test", &c) == -EINVAL); assert_se(calendar_spec_from_string(" utc", &c) == -EINVAL); assert_se(calendar_spec_from_string(" ", &c) == -EINVAL); assert_se(calendar_spec_from_string("", &c) == -EINVAL); assert_se(calendar_spec_from_string("7", &c) == -EINVAL); assert_se(calendar_spec_from_string("121212:1:2", &c) == -EINVAL); assert_se(calendar_spec_from_string("2000-03-05.23 00:00:00", &c) == -EINVAL); assert_se(calendar_spec_from_string("2000-03-05 00:00.1:00", &c) == -EINVAL); assert_se(calendar_spec_from_string("00:00:00/0.00000001", &c) == -ERANGE); assert_se(calendar_spec_from_string("00:00:00.0..00.9", &c) == -EINVAL); assert_se(calendar_spec_from_string("2016~11-22", &c) == -EINVAL); assert_se(calendar_spec_from_string("*-*~5/5", &c) == -EINVAL); assert_se(calendar_spec_from_string("Monday.. 12:00", &c) == -EINVAL); assert_se(calendar_spec_from_string("Monday..", &c) == -EINVAL); assert_se(calendar_spec_from_string("-00:+00/-5", &c) == -EINVAL); assert_se(calendar_spec_from_string("00:+00/-5", &c) == -EINVAL); assert_se(calendar_spec_from_string("2016- 11- 24 12: 30: 00", &c) == -EINVAL); assert_se(calendar_spec_from_string("*~29", &c) == -EINVAL); assert_se(calendar_spec_from_string("*~16..31", &c) == -EINVAL); assert_se(calendar_spec_from_string("12..1/2-*", &c) == -EINVAL); assert_se(calendar_spec_from_string("20/4:00", &c) == -EINVAL); assert_se(calendar_spec_from_string("00:00/60", &c) == -EINVAL); assert_se(calendar_spec_from_string("00:00:2300", &c) == -ERANGE); assert_se(calendar_spec_from_string("00:00:18446744073709551615", &c) == -ERANGE); assert_se(calendar_spec_from_string("@88588582097858858", &c) == -ERANGE); assert_se(calendar_spec_from_string("*:4,30:*,5", &c) == -EINVAL); assert_se(calendar_spec_from_string("*:4,30:5,*", &c) == -EINVAL); assert_se(calendar_spec_from_string("*:4,30:*\n", &c) == -EINVAL); } DEFINE_TEST_MAIN(LOG_INFO);
13,026
49.492248
114
c
null
systemd-main/src/test/test-capability.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <netinet/in.h> #include <pwd.h> #include <sys/prctl.h> #include <sys/socket.h> #include <sys/wait.h> #include <unistd.h> #define TEST_CAPABILITY_C #include "alloc-util.h" #include "capability-util.h" #include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "macro.h" #include "missing_prctl.h" #include "parse-util.h" #include "process-util.h" #include "string-util.h" #include "tests.h" static uid_t test_uid = -1; static gid_t test_gid = -1; #if HAS_FEATURE_ADDRESS_SANITIZER /* Keep CAP_SYS_PTRACE when running under Address Sanitizer */ static const uint64_t test_flags = UINT64_C(1) << CAP_SYS_PTRACE; #else /* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */ static const uint64_t test_flags = UINT64_C(1) << CAP_DAC_OVERRIDE; #endif /* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */ static void test_last_cap_file(void) { _cleanup_free_ char *content = NULL; unsigned long val = 0; int r; r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); if (r == -ENOENT || (r < 0 && ERRNO_IS_PRIVILEGE(r))) /* kernel pre 3.2 or no access */ return; assert_se(r >= 0); r = safe_atolu(content, &val); assert_se(r >= 0); assert_se(val != 0); assert_se(val == cap_last_cap()); } /* verify cap_last_cap() against syscall probing */ static void test_last_cap_probe(void) { unsigned long p = (unsigned long)CAP_LAST_CAP; if (prctl(PR_CAPBSET_READ, p) < 0) { for (p--; p > 0; p --) if (prctl(PR_CAPBSET_READ, p) >= 0) break; } else { for (;; p++) if (prctl(PR_CAPBSET_READ, p+1) < 0) break; } assert_se(p != 0); assert_se(p == cap_last_cap()); } static void fork_test(void (*test_func)(void)) { pid_t pid = 0; pid = fork(); assert_se(pid >= 0); if (pid == 0) { test_func(); exit(EXIT_SUCCESS); } else if (pid > 0) { int status; assert_se(waitpid(pid, &status, 0) > 0); assert_se(WIFEXITED(status) && WEXITSTATUS(status) == 0); } } static void show_capabilities(void) { cap_t caps; char *text; caps = cap_get_proc(); assert_se(caps); text = cap_to_text(caps, NULL); assert_se(text); log_info("Capabilities:%s", text); cap_free(caps); cap_free(text); } static int setup_tests(bool *run_ambient) { struct passwd *nobody; int r; nobody = getpwnam(NOBODY_USER_NAME); if (!nobody) return log_warning_errno(SYNTHETIC_ERRNO(ENOENT), "Couldn't find 'nobody' user: %m"); test_uid = nobody->pw_uid; test_gid = nobody->pw_gid; r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0); /* There's support for PR_CAP_AMBIENT if the prctl() call succeeded or error code was something else * than EINVAL. The EINVAL check should be good enough to rule out false positives. */ *run_ambient = r >= 0 || errno != EINVAL; return 0; } static void test_drop_privileges_keep_net_raw(void) { int sock; sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); assert_se(sock >= 0); safe_close(sock); assert_se(drop_privileges(test_uid, test_gid, test_flags | (1ULL << CAP_NET_RAW)) >= 0); assert_se(getuid() == test_uid); assert_se(getgid() == test_gid); show_capabilities(); sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); assert_se(sock >= 0); safe_close(sock); } static void test_drop_privileges_dontkeep_net_raw(void) { int sock; sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); assert_se(sock >= 0); safe_close(sock); assert_se(drop_privileges(test_uid, test_gid, test_flags) >= 0); assert_se(getuid() == test_uid); assert_se(getgid() == test_gid); show_capabilities(); sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); assert_se(sock < 0); } static void test_drop_privileges_fail(void) { assert_se(drop_privileges(test_uid, test_gid, test_flags) >= 0); assert_se(getuid() == test_uid); assert_se(getgid() == test_gid); assert_se(drop_privileges(test_uid, test_gid, test_flags) < 0); assert_se(drop_privileges(0, 0, test_flags) < 0); } static void test_drop_privileges(void) { fork_test(test_drop_privileges_fail); if (have_effective_cap(CAP_NET_RAW) <= 0) /* The remaining two tests only work if we have CAP_NET_RAW * in the first place. If we are run in some restricted * container environment we might not. */ return; fork_test(test_drop_privileges_keep_net_raw); fork_test(test_drop_privileges_dontkeep_net_raw); } static void test_have_effective_cap(void) { assert_se(have_effective_cap(CAP_KILL) > 0); assert_se(have_effective_cap(CAP_CHOWN) > 0); assert_se(drop_privileges(test_uid, test_gid, test_flags | (1ULL << CAP_KILL)) >= 0); assert_se(getuid() == test_uid); assert_se(getgid() == test_gid); assert_se(have_effective_cap(CAP_KILL) > 0); assert_se(have_effective_cap(CAP_CHOWN) == 0); } static void test_update_inherited_set(void) { cap_t caps; uint64_t set = 0; cap_flag_value_t fv; caps = cap_get_proc(); assert_se(caps); set = (UINT64_C(1) << CAP_CHOWN); assert_se(!capability_update_inherited_set(caps, set)); assert_se(!cap_get_flag(caps, CAP_CHOWN, CAP_INHERITABLE, &fv)); assert_se(fv == CAP_SET); cap_free(caps); } static void test_apply_ambient_caps(void) { cap_t caps; uint64_t set = 0; cap_flag_value_t fv; assert_se(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) == 0); set = (UINT64_C(1) << CAP_CHOWN); assert_se(!capability_ambient_set_apply(set, true)); caps = cap_get_proc(); assert_se(caps); assert_se(!cap_get_flag(caps, CAP_CHOWN, CAP_INHERITABLE, &fv)); assert_se(fv == CAP_SET); cap_free(caps); assert_se(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) == 1); assert_se(!capability_ambient_set_apply(0, true)); caps = cap_get_proc(); assert_se(caps); assert_se(!cap_get_flag(caps, CAP_CHOWN, CAP_INHERITABLE, &fv)); assert_se(fv == CAP_CLEAR); cap_free(caps); assert_se(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) == 0); } static void test_ensure_cap_64_bit(void) { _cleanup_free_ char *content = NULL; unsigned long p = 0; int r; r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); if (r == -ENOENT || (r < 0 && ERRNO_IS_PRIVILEGE(r))) /* kernel pre 3.2 or no access */ return; assert_se(r >= 0); assert_se(safe_atolu(content, &p) >= 0); /* If caps don't fit into 64-bit anymore, we have a problem, fail the test. */ assert_se(p <= 63); /* Also check for the header definition */ assert_cc(CAP_LAST_CAP <= 63); } static void test_capability_get_ambient(void) { uint64_t c; int r; assert_se(capability_get_ambient(&c) >= 0); r = safe_fork("(getambient)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL); assert_se(r >= 0); if (r == 0) { int x, y; /* child */ assert_se(capability_get_ambient(&c) >= 0); x = capability_ambient_set_apply( (UINT64_C(1) << CAP_MKNOD)| (UINT64_C(1) << CAP_LINUX_IMMUTABLE), /* also_inherit= */ true); assert_se(x >= 0 || ERRNO_IS_PRIVILEGE(x)); assert_se(capability_get_ambient(&c) >= 0); assert_se(x < 0 || FLAGS_SET(c, UINT64_C(1) << CAP_MKNOD)); assert_se(x < 0 || FLAGS_SET(c, UINT64_C(1) << CAP_LINUX_IMMUTABLE)); assert_se(x < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_SETPCAP)); y = capability_bounding_set_drop( ((UINT64_C(1) << CAP_LINUX_IMMUTABLE)| (UINT64_C(1) << CAP_SETPCAP)), /* right_now= */ true); assert_se(y >= 0 || ERRNO_IS_PRIVILEGE(y)); assert_se(capability_get_ambient(&c) >= 0); assert_se(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_MKNOD)); assert_se(x < 0 || y < 0 || FLAGS_SET(c, UINT64_C(1) << CAP_LINUX_IMMUTABLE)); assert_se(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_SETPCAP)); y = capability_bounding_set_drop( (UINT64_C(1) << CAP_SETPCAP), /* right_now= */ true); assert_se(y >= 0 || ERRNO_IS_PRIVILEGE(y)); assert_se(capability_get_ambient(&c) >= 0); assert_se(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_MKNOD)); assert_se(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_LINUX_IMMUTABLE)); assert_se(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_SETPCAP)); _exit(EXIT_SUCCESS); } } int main(int argc, char *argv[]) { bool run_ambient; test_setup_logging(LOG_DEBUG); test_ensure_cap_64_bit(); test_last_cap_file(); test_last_cap_probe(); log_info("have ambient caps: %s", yes_no(ambient_capabilities_supported())); if (getuid() != 0) return log_tests_skipped("not running as root"); if (setup_tests(&run_ambient) < 0) return log_tests_skipped("setup failed"); show_capabilities(); test_drop_privileges(); test_update_inherited_set(); fork_test(test_have_effective_cap); if (run_ambient) fork_test(test_apply_ambient_caps); test_capability_get_ambient(); return 0; }
10,609
30.861862
109
c
null
systemd-main/src/test/test-cgroup-setup.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <unistd.h> #include "alloc-util.h" #include "cgroup-setup.h" #include "errno-util.h" #include "log.h" #include "proc-cmdline.h" #include "string-util.h" #include "tests.h" #include "version.h" static void test_is_wanted_print_one(bool header) { _cleanup_free_ char *cmdline = NULL; log_info("-- %s --", __func__); assert_se(proc_cmdline(&cmdline) >= 0); log_info("cmdline: %s", cmdline); if (header) { log_info("default-hierarchy=" DEFAULT_HIERARCHY_NAME); (void) system("findmnt -n /sys/fs/cgroup"); } log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted())); log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted())); log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted())); log_info(" "); } TEST(is_wanted_print) { test_is_wanted_print_one(true); test_is_wanted_print_one(false); /* run twice to test caching */ } TEST(is_wanted) { assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy", 1) >= 0); test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy=0", 1) >= 0); test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy=0 " "systemd.legacy_systemd_cgroup_controller", 1) >= 0); test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy=0 " "systemd.legacy_systemd_cgroup_controller=0", 1) >= 0); test_is_wanted_print_one(false); /* cgroup_no_v1=all implies unified cgroup hierarchy, unless otherwise * explicitly specified. */ assert_se(setenv("SYSTEMD_PROC_CMDLINE", "cgroup_no_v1=all", 1) >= 0); test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "cgroup_no_v1=all " "systemd.unified_cgroup_hierarchy=0", 1) >= 0); test_is_wanted_print_one(false); } static int intro(void) { if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno)) return log_tests_skipped("can't read /proc/cmdline"); return EXIT_SUCCESS; } DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
2,577
33.373333
80
c
null
systemd-main/src/test/test-cgroup-unit-default.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <stdio.h> #include "cgroup.h" #include "manager.h" #include "rm-rf.h" #include "tests.h" #include "unit.h" TEST_RET(default_memory_low, .sd_booted = true) { _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; _cleanup_(manager_freep) Manager *m = NULL; Unit *root, *dml, *dml_passthrough, *dml_passthrough_empty, *dml_passthrough_set_dml, *dml_passthrough_set_ml, *dml_override, *dml_override_empty, *dml_discard, *dml_discard_empty, *dml_discard_set_ml; uint64_t dml_tree_default; int r; r = enter_cgroup_subroot(NULL); if (r == -ENOMEDIUM) return log_tests_skipped("cgroupfs not available"); _cleanup_free_ char *unit_dir = NULL; assert_se(get_testdata_dir("units", &unit_dir) >= 0); assert_se(set_unit_path(unit_dir) >= 0); assert_se(runtime_dir = setup_fake_runtime_dir()); r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m); if (IN_SET(r, -EPERM, -EACCES)) { log_error_errno(r, "manager_new: %m"); return log_tests_skipped("cannot create manager"); } assert_se(r >= 0); assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); /* dml.slice has DefaultMemoryLow=50. Beyond that, individual subhierarchies look like this: * * 1. dml-passthrough.slice sets MemoryLow=100. This should not affect its children, as only * DefaultMemoryLow is propagated, not MemoryLow. As such, all leaf services should end up with * memory.low as 50, inherited from dml.slice, *except* for dml-passthrough-set-ml.service, which * should have the value of 0, as it has MemoryLow explicitly set. * * ┌───────────┐ * │ dml.slice │ * └─────┬─────┘ * MemoryLow=100 * ┌───────────┴───────────┐ * │ dml-passthrough.slice │ * └───────────┬───────────┘ * ┌───────────────────────────────────┼───────────────────────────────────┐ * no new settings DefaultMemoryLow=15 MemoryLow=0 * ┌───────────────┴───────────────┐ ┌────────────────┴────────────────┐ ┌───────────────┴────────────────┐ * │ dml-passthrough-empty.service │ │ dml-passthrough-set-dml.service │ │ dml-passthrough-set-ml.service │ * └───────────────────────────────┘ └─────────────────────────────────┘ └────────────────────────────────┘ * * 2. dml-override.slice sets DefaultMemoryLow=10. As such, dml-override-empty.service should also * end up with a memory.low of 10. dml-override.slice should still have a memory.low of 50. * * ┌───────────┐ * │ dml.slice │ * └─────┬─────┘ * DefaultMemoryLow=10 * ┌─────────┴──────────┐ * │ dml-override.slice │ * └─────────┬──────────┘ * no new settings * ┌─────────────┴──────────────┐ * │ dml-override-empty.service │ * └────────────────────────────┘ * * 3. dml-discard.slice sets DefaultMemoryLow= with no rvalue. As such, * dml-discard-empty.service should end up with a value of 0. * dml-discard-set-ml.service sets MemoryLow=15, and as such should have that override the * reset DefaultMemoryLow value. dml-discard.slice should still have an eventual memory.low of 50. * * ┌───────────┐ * │ dml.slice │ * └─────┬─────┘ * DefaultMemoryLow= * ┌─────────┴─────────┐ * │ dml-discard.slice │ * └─────────┬─────────┘ * ┌──────────────┴───────────────┐ * no new settings MemoryLow=15 * ┌─────────────┴─────────────┐ ┌─────────────┴──────────────┐ * │ dml-discard-empty.service │ │ dml-discard-set-ml.service │ * └───────────────────────────┘ └────────────────────────────┘ */ assert_se(manager_load_startable_unit_or_warn(m, "dml.slice", NULL, &dml) >= 0); assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough.slice", NULL, &dml_passthrough) >= 0); assert_se(UNIT_GET_SLICE(dml_passthrough) == dml); assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough-empty.service", NULL, &dml_passthrough_empty) >= 0); assert_se(UNIT_GET_SLICE(dml_passthrough_empty) == dml_passthrough); assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough-set-dml.service", NULL, &dml_passthrough_set_dml) >= 0); assert_se(UNIT_GET_SLICE(dml_passthrough_set_dml) == dml_passthrough); assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough-set-ml.service", NULL, &dml_passthrough_set_ml) >= 0); assert_se(UNIT_GET_SLICE(dml_passthrough_set_ml) == dml_passthrough); assert_se(manager_load_startable_unit_or_warn(m, "dml-override.slice", NULL, &dml_override) >= 0); assert_se(UNIT_GET_SLICE(dml_override) == dml); assert_se(manager_load_startable_unit_or_warn(m, "dml-override-empty.service", NULL, &dml_override_empty) >= 0); assert_se(UNIT_GET_SLICE(dml_override_empty) == dml_override); assert_se(manager_load_startable_unit_or_warn(m, "dml-discard.slice", NULL, &dml_discard) >= 0); assert_se(UNIT_GET_SLICE(dml_discard) == dml); assert_se(manager_load_startable_unit_or_warn(m, "dml-discard-empty.service", NULL, &dml_discard_empty) >= 0); assert_se(UNIT_GET_SLICE(dml_discard_empty) == dml_discard); assert_se(manager_load_startable_unit_or_warn(m, "dml-discard-set-ml.service", NULL, &dml_discard_set_ml) >= 0); assert_se(UNIT_GET_SLICE(dml_discard_set_ml) == dml_discard); assert_se(root = UNIT_GET_SLICE(dml)); assert_se(!UNIT_GET_SLICE(root)); assert_se(unit_get_ancestor_memory_low(root) == CGROUP_LIMIT_MIN); assert_se(unit_get_ancestor_memory_low(dml) == CGROUP_LIMIT_MIN); dml_tree_default = unit_get_cgroup_context(dml)->default_memory_low; assert_se(dml_tree_default == 50); assert_se(unit_get_ancestor_memory_low(dml_passthrough) == 100); assert_se(unit_get_ancestor_memory_low(dml_passthrough_empty) == dml_tree_default); assert_se(unit_get_ancestor_memory_low(dml_passthrough_set_dml) == 50); assert_se(unit_get_ancestor_memory_low(dml_passthrough_set_ml) == 0); assert_se(unit_get_ancestor_memory_low(dml_override) == dml_tree_default); assert_se(unit_get_ancestor_memory_low(dml_override_empty) == 10); assert_se(unit_get_ancestor_memory_low(dml_discard) == dml_tree_default); assert_se(unit_get_ancestor_memory_low(dml_discard_empty) == CGROUP_LIMIT_MIN); assert_se(unit_get_ancestor_memory_low(dml_discard_set_ml) == 15); return 0; } DEFINE_TEST_MAIN(LOG_DEBUG);
7,682
54.273381
130
c
null
systemd-main/src/test/test-cgroup.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <unistd.h> #include "cgroup-setup.h" #include "cgroup-util.h" #include "errno-util.h" #include "path-util.h" #include "process-util.h" #include "string-util.h" #include "tests.h" TEST(cg_split_spec) { char *c, *p; assert_se(cg_split_spec("foobar:/", &c, &p) == 0); assert_se(streq(c, "foobar")); assert_se(streq(p, "/")); c = mfree(c); p = mfree(p); assert_se(cg_split_spec("foobar:", &c, &p) == 0); c = mfree(c); p = mfree(p); assert_se(cg_split_spec("foobar:asdfd", &c, &p) < 0); assert_se(cg_split_spec(":///", &c, &p) < 0); assert_se(cg_split_spec(":", &c, &p) < 0); assert_se(cg_split_spec("", &c, &p) < 0); assert_se(cg_split_spec("fo/obar:/", &c, &p) < 0); assert_se(cg_split_spec("/", &c, &p) >= 0); assert_se(c == NULL); assert_se(streq(p, "/")); p = mfree(p); assert_se(cg_split_spec("foo", &c, &p) >= 0); assert_se(streq(c, "foo")); assert_se(p == NULL); c = mfree(c); } TEST(cg_create) { int r; r = cg_unified_cached(false); if (r == -ENOMEDIUM) { log_tests_skipped("cgroup not mounted"); return; } assert_se(r >= 0); _cleanup_free_ char *here = NULL; assert_se(cg_pid_get_path_shifted(0, NULL, &here) >= 0); const char *test_a = prefix_roota(here, "/test-a"), *test_b = prefix_roota(here, "/test-b"), *test_c = prefix_roota(here, "/test-b/test-c"), *test_d = prefix_roota(here, "/test-b/test-d"); char *path; log_info("Paths for test:\n%s\n%s", test_a, test_b); r = cg_create(SYSTEMD_CGROUP_CONTROLLER, test_a); if (IN_SET(r, -EPERM, -EACCES, -EROFS)) { log_info_errno(r, "Skipping %s: %m", __func__); return; } assert_se(r == 1); assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0); assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_b) == 1); assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_c) == 1); assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, test_b, 0) == 0); assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0); assert_se(streq(path, test_b)); free(path); assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, test_a, 0) == 0); assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0); assert_se(path_equal(path, test_a)); free(path); assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, test_d, 0) == 1); assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0); assert_se(path_equal(path, test_d)); free(path); assert_se(cg_get_path(SYSTEMD_CGROUP_CONTROLLER, test_d, NULL, &path) == 0); log_debug("test_d: %s", path); const char *full_d; if (cg_all_unified()) full_d = strjoina("/sys/fs/cgroup", test_d); else if (cg_hybrid_unified()) full_d = strjoina("/sys/fs/cgroup/unified", test_d); else full_d = strjoina("/sys/fs/cgroup/systemd", test_d); assert_se(path_equal(path, full_d)); free(path); assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, test_a) > 0); assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, test_b) > 0); assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a) > 0); assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b) == 0); assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a, 0, 0, NULL, NULL, NULL) == 0); assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b, 0, 0, NULL, NULL, NULL) > 0); assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b, SYSTEMD_CGROUP_CONTROLLER, test_a, 0) > 0); assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0); assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b) > 0); assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a, 0, 0, NULL, NULL, NULL) > 0); assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b, 0, 0, NULL, NULL, NULL) == 0); cg_trim(SYSTEMD_CGROUP_CONTROLLER, test_b, false); assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_b) == 0); assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_a) < 0); assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a, SYSTEMD_CGROUP_CONTROLLER, here, 0) > 0); assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0); } DEFINE_TEST_MAIN(LOG_DEBUG);
4,858
36.666667
117
c
null
systemd-main/src/test/test-chase-manual.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <getopt.h> #include "chase.h" #include "fd-util.h" #include "log.h" #include "main-func.h" static char *arg_root = NULL; static int arg_flags = 0; static bool arg_open = false; static int parse_argv(int argc, char *argv[]) { enum { ARG_ROOT = 0x1000, ARG_OPEN, }; static const struct option options[] = { { "help", no_argument, NULL, 'h' }, { "root", required_argument, NULL, ARG_ROOT }, { "open", no_argument, NULL, ARG_OPEN }, { "prefix-root", no_argument, NULL, CHASE_PREFIX_ROOT }, { "nonexistent", no_argument, NULL, CHASE_NONEXISTENT }, { "no_autofs", no_argument, NULL, CHASE_NO_AUTOFS }, { "safe", no_argument, NULL, CHASE_SAFE }, { "trail-slash", no_argument, NULL, CHASE_TRAIL_SLASH }, { "step", no_argument, NULL, CHASE_STEP }, { "nofollow", no_argument, NULL, CHASE_NOFOLLOW }, { "warn", no_argument, NULL, CHASE_WARN }, {} }; int c; assert_se(argc >= 0); assert_se(argv); while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) switch (c) { case 'h': printf("Syntax:\n" " %s [OPTION...] path...\n" "Options:\n" , argv[0]); for (size_t i = 0; i < ELEMENTSOF(options) - 1; i++) printf(" --%s\n", options[i].name); return 0; case ARG_ROOT: arg_root = optarg; break; case ARG_OPEN: arg_open = true; break; case CHASE_PREFIX_ROOT: case CHASE_NONEXISTENT: case CHASE_NO_AUTOFS: case CHASE_SAFE: case CHASE_TRAIL_SLASH: case CHASE_STEP: case CHASE_NOFOLLOW: case CHASE_WARN: arg_flags |= c; break; case '?': return -EINVAL; default: assert_not_reached(); } if (optind == argc) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "At least one argument is required."); return 1; } static int run(int argc, char **argv) { int r; log_setup(); r = parse_argv(argc, argv); if (r <= 0) return r; for (int i = optind; i < argc; i++) { _cleanup_free_ char *p = NULL; _cleanup_close_ int fd = -EBADF; printf("%s ", argv[i]); fflush(stdout); r = chase(argv[i], arg_root, arg_flags, &p, arg_open ? &fd : NULL); if (r < 0) log_error_errno(r, "failed: %m"); else { log_info("→ %s", p); if (arg_open) assert_se(fd >= 0); else assert_se(fd == -EBADF); } } return 0; } DEFINE_MAIN_FUNCTION(run);
3,800
31.767241
102
c
null
systemd-main/src/test/test-chown-rec.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/xattr.h> #include <unistd.h> #include "alloc-util.h" #include "chown-recursive.h" #include "log.h" #include "rm-rf.h" #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" static const uint8_t acl[] = { 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00, 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x07, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x07, 0x00, 0xff, 0xff, 0xff, 0xff, 0x10, 0x00, 0x07, 0x00, 0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff, }; static const uint8_t default_acl[] = { 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00, 0xff, 0xff, 0xff, 0xff, 0x04, 0x00, 0x07, 0x00, 0xff, 0xff, 0xff, 0xff, 0x08, 0x00, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00, 0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff, }; static bool has_xattr(const char *p) { char buffer[sizeof(acl) * 4]; if (lgetxattr(p, "system.posix_acl_access", buffer, sizeof(buffer)) < 0) return !ERRNO_IS_XATTR_ABSENT(errno); return true; } TEST(chown_recursive) { _cleanup_(rm_rf_physical_and_freep) char *t = NULL; struct stat st; const char *p; const uid_t uid = getuid(); const gid_t gid = getgid(); umask(022); assert_se(mkdtemp_malloc(NULL, &t) >= 0); p = strjoina(t, "/dir"); assert_se(mkdir(p, 0777) >= 0); assert_se(lstat(p, &st) >= 0); assert_se(S_ISDIR(st.st_mode)); assert_se((st.st_mode & 07777) == 0755); assert_se(st.st_uid == uid); assert_se(st.st_gid == gid); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/symlink"); assert_se(symlink("../../", p) >= 0); assert_se(lstat(p, &st) >= 0); assert_se(S_ISLNK(st.st_mode)); assert_se((st.st_mode & 07777) == 0777); assert_se(st.st_uid == uid); assert_se(st.st_gid == gid); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/reg"); assert_se(mknod(p, S_IFREG|0777, 0) >= 0); assert_se(lstat(p, &st) >= 0); assert_se(S_ISREG(st.st_mode)); assert_se((st.st_mode & 07777) == 0755); assert_se(st.st_uid == uid); assert_se(st.st_gid == gid); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/sock"); assert_se(mknod(p, S_IFSOCK|0777, 0) >= 0); assert_se(lstat(p, &st) >= 0); assert_se(S_ISSOCK(st.st_mode)); assert_se((st.st_mode & 07777) == 0755); assert_se(st.st_uid == uid); assert_se(st.st_gid == gid); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/fifo"); assert_se(mknod(p, S_IFIFO|0777, 0) >= 0); assert_se(lstat(p, &st) >= 0); assert_se(S_ISFIFO(st.st_mode)); assert_se((st.st_mode & 07777) == 0755); assert_se(st.st_uid == uid); assert_se(st.st_gid == gid); assert_se(!has_xattr(p)); /* We now apply an xattr to the dir, and check it again */ p = strjoina(t, "/dir"); assert_se(setxattr(p, "system.posix_acl_access", acl, sizeof(acl), 0) >= 0); assert_se(setxattr(p, "system.posix_acl_default", default_acl, sizeof(default_acl), 0) >= 0); assert_se(lstat(p, &st) >= 0); assert_se(S_ISDIR(st.st_mode)); assert_se((st.st_mode & 07777) == 0775); /* acl change changed the mode too */ assert_se(st.st_uid == uid); assert_se(st.st_gid == gid); assert_se(has_xattr(p)); assert_se(path_chown_recursive(t, 1, 2, 07777, 0) >= 0); p = strjoina(t, "/dir"); assert_se(lstat(p, &st) >= 0); assert_se(S_ISDIR(st.st_mode)); assert_se((st.st_mode & 07777) == 0775); assert_se(st.st_uid == 1); assert_se(st.st_gid == 2); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/symlink"); assert_se(lstat(p, &st) >= 0); assert_se(S_ISLNK(st.st_mode)); assert_se((st.st_mode & 07777) == 0777); assert_se(st.st_uid == 1); assert_se(st.st_gid == 2); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/reg"); assert_se(lstat(p, &st) >= 0); assert_se(S_ISREG(st.st_mode)); assert_se((st.st_mode & 07777) == 0755); assert_se(st.st_uid == 1); assert_se(st.st_gid == 2); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/sock"); assert_se(lstat(p, &st) >= 0); assert_se(S_ISSOCK(st.st_mode)); assert_se((st.st_mode & 07777) == 0755); assert_se(st.st_uid == 1); assert_se(st.st_gid == 2); assert_se(!has_xattr(p)); p = strjoina(t, "/dir/fifo"); assert_se(lstat(p, &st) >= 0); assert_se(S_ISFIFO(st.st_mode)); assert_se((st.st_mode & 07777) == 0755); assert_se(st.st_uid == 1); assert_se(st.st_gid == 2); assert_se(!has_xattr(p)); } static int intro(void) { if (geteuid() != 0) return log_tests_skipped("not running as root"); return EXIT_SUCCESS; } DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
5,264
32.322785
101
c
null
systemd-main/src/test/test-clock.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ /*** Copyright © 2016 Canonical Ltd. ***/ #include <unistd.h> #include <fcntl.h> #include "clock-util.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" #include "log.h" #include "macro.h" #include "tests.h" #include "tmpfile-util.h" TEST(clock_is_localtime) { _cleanup_(unlink_tempfilep) char adjtime[] = "/tmp/test-adjtime.XXXXXX"; _cleanup_fclose_ FILE* f = NULL; static const struct scenario { const char* contents; int expected_result; } scenarios[] = { /* adjtime configures UTC */ {"0.0 0 0\n0\nUTC\n", 0}, /* adjtime configures local time */ {"0.0 0 0\n0\nLOCAL\n", 1}, /* no final EOL */ {"0.0 0 0\n0\nUTC", 0}, {"0.0 0 0\n0\nLOCAL", 1}, /* empty value -> defaults to UTC */ {"0.0 0 0\n0\n", 0}, /* unknown value -> defaults to UTC */ {"0.0 0 0\n0\nFOO\n", 0}, /* no third line */ {"0.0 0 0", 0}, {"0.0 0 0\n", 0}, {"0.0 0 0\n0", 0}, }; /* without an adjtime file we default to UTC */ assert_se(clock_is_localtime("/nonexisting/adjtime") == 0); assert_se(fmkostemp_safe(adjtime, "w", &f) == 0); log_info("adjtime test file: %s", adjtime); for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) { log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result); log_info("%s", scenarios[i].contents); rewind(f); assert_se(ftruncate(fileno(f), 0) == 0); assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0); assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result); } } /* Test with the real /etc/adjtime */ TEST(clock_is_localtime_system) { int r; r = clock_is_localtime(NULL); if (access("/etc/adjtime", R_OK) == 0) { log_info("/etc/adjtime is readable, clock_is_localtime() == %i", r); /* if /etc/adjtime exists we expect some answer, no error or * crash */ assert_se(IN_SET(r, 0, 1)); } else /* default is UTC if there is no /etc/adjtime */ assert_se(r == 0 || ERRNO_IS_PRIVILEGE(r)); } DEFINE_TEST_MAIN(LOG_INFO);
2,554
33.066667
111
c
null
systemd-main/src/test/test-compare-operator.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "compare-operator.h" #include "tests.h" TEST(parse_compare_operator) { const char *str_a = "<>version"; assert_se(parse_compare_operator(&str_a, 0) == COMPARE_UNEQUAL); const char *str_b = "$=version"; assert_se(parse_compare_operator(&str_b, 0) == _COMPARE_OPERATOR_INVALID); assert_se(parse_compare_operator(&str_b, COMPARE_ALLOW_FNMATCH) == COMPARE_FNMATCH_EQUAL); const char *str_c = "eq oranges"; assert_se(parse_compare_operator(&str_c, 0) == _COMPARE_OPERATOR_INVALID); assert_se(parse_compare_operator(&str_c, COMPARE_ALLOW_TEXTUAL) == COMPARE_EQUAL); const char *str_d = ""; assert_se(parse_compare_operator(&str_d, 0) == _COMPARE_OPERATOR_INVALID); const char *str_e = "!=!="; /* parse_compare_operator() moves the pointer */ assert_se(parse_compare_operator(&str_e, COMPARE_EQUAL_BY_STRING) == COMPARE_STRING_UNEQUAL); assert_se(parse_compare_operator(&str_e, 0) == COMPARE_UNEQUAL); assert_se(parse_compare_operator(&str_e, 0) == _COMPARE_OPERATOR_INVALID); } TEST(test_order) { assert_se(!test_order(5, COMPARE_LOWER)); assert_se(!test_order(5, COMPARE_LOWER_OR_EQUAL)); assert_se(!test_order(5, COMPARE_EQUAL)); assert_se(test_order(5, COMPARE_UNEQUAL)); assert_se(test_order(5, COMPARE_GREATER_OR_EQUAL)); assert_se(test_order(5, COMPARE_GREATER)); assert_se(test_order(5, COMPARE_STRING_EQUAL) == -EINVAL); } TEST(version_or_fnmatch_compare) { assert_se(version_or_fnmatch_compare(COMPARE_STRING_EQUAL, "locale", "locale")); assert_se(version_or_fnmatch_compare(COMPARE_STRING_UNEQUAL, "locale", "LOCALE")); assert_se(version_or_fnmatch_compare(COMPARE_FNMATCH_EQUAL, "locaale", "loc*le")); assert_se(version_or_fnmatch_compare(COMPARE_FNMATCH_UNEQUAL, "locaale", "loc?le")); assert_se(version_or_fnmatch_compare(COMPARE_GREATER, "local512", "local256")); assert_se(version_or_fnmatch_compare(COMPARE_LOWER, "local52", "local256")); assert_se(version_or_fnmatch_compare(_COMPARE_OPERATOR_MAX, "local512", "local256") == -EINVAL); } DEFINE_TEST_MAIN(LOG_INFO);
2,263
50.454545
104
c
null
systemd-main/src/test/test-compress-benchmark.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "compress.h" #include "env-util.h" #include "macro.h" #include "memory-util.h" #include "nulstr-util.h" #include "parse-util.h" #include "process-util.h" #include "random-util.h" #include "string-util.h" #include "tests.h" typedef int (compress_t)(const void *src, uint64_t src_size, void *dst, size_t dst_alloc_size, size_t *dst_size); typedef int (decompress_t)(const void *src, uint64_t src_size, void **dst, size_t* dst_size, size_t dst_max); #if HAVE_COMPRESSION static usec_t arg_duration; static size_t arg_start; #define MAX_SIZE (1024*1024LU) #define PRIME 1048571 /* A prime close enough to one megabyte that mod 4 == 3 */ static size_t _permute(size_t x) { size_t residue; if (x >= PRIME) return x; residue = x*x % PRIME; if (x <= PRIME / 2) return residue; else return PRIME - residue; } static size_t permute(size_t x) { return _permute((_permute(x) + arg_start) % MAX_SIZE ^ 0xFF345); } static char* make_buf(size_t count, const char *type) { char *buf; size_t i; buf = malloc(count); assert_se(buf); if (streq(type, "zeros")) memzero(buf, count); else if (streq(type, "simple")) for (i = 0; i < count; i++) buf[i] = 'a' + i % ('z' - 'a' + 1); else if (streq(type, "random")) { size_t step = count / 10; random_bytes(buf, step); memzero(buf + 1*step, step); random_bytes(buf + 2*step, step); memzero(buf + 3*step, step); random_bytes(buf + 4*step, step); memzero(buf + 5*step, step); random_bytes(buf + 6*step, step); memzero(buf + 7*step, step); random_bytes(buf + 8*step, step); memzero(buf + 9*step, step); } else assert_not_reached(); return buf; } static void test_compress_decompress(const char* label, const char* type, compress_t compress, decompress_t decompress) { usec_t n, n2 = 0; float dt; _cleanup_free_ char *text = NULL, *buf = NULL; _cleanup_free_ void *buf2 = NULL; size_t skipped = 0, compressed = 0, total = 0; text = make_buf(MAX_SIZE, type); buf = calloc(MAX_SIZE + 1, 1); assert_se(text && buf); n = now(CLOCK_MONOTONIC); for (size_t i = 0; i <= MAX_SIZE; i++) { size_t j = 0, k = 0, size; int r; size = permute(i); if (size == 0) continue; log_debug("%s %zu %zu", type, i, size); memzero(buf, MIN(size + 1000, MAX_SIZE)); r = compress(text, size, buf, size, &j); /* assume compression must be successful except for small or random inputs */ assert_se(r >= 0 || (size < 2048 && r == -ENOBUFS) || streq(type, "random")); /* check for overwrites */ assert_se(buf[size] == 0); if (r < 0) { skipped += size; continue; } assert_se(j > 0); if (j >= size) log_error("%s \"compressed\" %zu -> %zu", label, size, j); r = decompress(buf, j, &buf2, &k, 0); assert_se(r == 0); assert_se(k == size); assert_se(memcmp(text, buf2, size) == 0); total += size; compressed += j; n2 = now(CLOCK_MONOTONIC); if (n2 - n > arg_duration) break; } dt = (n2-n) / 1e6; log_info("%s/%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), " "mean compression %.2f%%, skipped %zu bytes", label, type, total, dt, total / 1024. / 1024 / dt, 100 - compressed * 100. / total, skipped); } #endif int main(int argc, char *argv[]) { #if HAVE_COMPRESSION test_setup_logging(LOG_INFO); if (argc >= 2) { unsigned x; assert_se(safe_atou(argv[1], &x) >= 0); arg_duration = x * USEC_PER_SEC; } else arg_duration = slow_tests_enabled() ? 2 * USEC_PER_SEC : USEC_PER_SEC / 50; if (argc == 3) (void) safe_atozu(argv[2], &arg_start); else arg_start = getpid_cached(); NULSTR_FOREACH(i, "zeros\0simple\0random\0") { #if HAVE_XZ test_compress_decompress("XZ", i, compress_blob_xz, decompress_blob_xz); #endif #if HAVE_LZ4 test_compress_decompress("LZ4", i, compress_blob_lz4, decompress_blob_lz4); #endif #if HAVE_ZSTD test_compress_decompress("ZSTD", i, compress_blob_zstd, decompress_blob_zstd); #endif } return 0; #else return log_tests_skipped("No compression feature is enabled"); #endif }
5,332
29.129944
94
c
null
systemd-main/src/test/test-conf-parser.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "conf-parser.h" #include "fd-util.h" #include "fs-util.h" #include "log.h" #include "macro.h" #include "string-util.h" #include "strv.h" #include "tests.h" #include "tmpfile-util.h" static void test_config_parse_path_one(const char *rvalue, const char *expected) { _cleanup_free_ char *path = NULL; assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0); assert_se(streq_ptr(expected, path)); } static void test_config_parse_log_level_one(const char *rvalue, int expected) { int log_level = 0; assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL) >= 0); assert_se(expected == log_level); } static void test_config_parse_log_facility_one(const char *rvalue, int expected) { int log_facility = 0; assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0); assert_se(expected == log_facility); } static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) { size_t iec_size = 0; assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0); assert_se(expected == iec_size); } static void test_config_parse_si_uint64_one(const char *rvalue, uint64_t expected) { uint64_t si_uint64 = 0; assert_se(config_parse_si_uint64("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_uint64, NULL) >= 0); assert_se(expected == si_uint64); } static void test_config_parse_int_one(const char *rvalue, int expected) { int v = -1; assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); assert_se(expected == v); } static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) { unsigned v = 0; assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); assert_se(expected == v); } static void test_config_parse_strv_one(const char *rvalue, char **expected) { _cleanup_strv_free_ char **strv = NULL; assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0); assert_se(strv_equal(expected, strv)); } static void test_config_parse_mode_one(const char *rvalue, mode_t expected) { mode_t v = 0; assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); assert_se(expected == v); } static void test_config_parse_sec_one(const char *rvalue, usec_t expected) { usec_t v = 0; assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); assert_se(expected == v); } static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) { nsec_t v = 0; assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); assert_se(expected == v); } TEST(config_parse_path) { test_config_parse_path_one("/path", "/path"); test_config_parse_path_one("/path//////////", "/path"); test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar"); test_config_parse_path_one("/path//./////hogehoge///.", "/path/hogehoge"); test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80"); test_config_parse_path_one("not_absolute/path", NULL); test_config_parse_path_one("/path/\xc3\x7f", NULL); } TEST(config_parse_log_level) { test_config_parse_log_level_one("debug", LOG_DEBUG); test_config_parse_log_level_one("info", LOG_INFO); test_config_parse_log_level_one("garbage", 0); } TEST(config_parse_log_facility) { test_config_parse_log_facility_one("mail", LOG_MAIL); test_config_parse_log_facility_one("user", LOG_USER); test_config_parse_log_facility_one("garbage", 0); } TEST(config_parse_iec_size) { test_config_parse_iec_size_one("1024", 1024); test_config_parse_iec_size_one("2K", 2048); test_config_parse_iec_size_one("10M", 10 * 1024 * 1024); test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024); test_config_parse_iec_size_one("0G", 0); test_config_parse_iec_size_one("0", 0); test_config_parse_iec_size_one("-982", 0); test_config_parse_iec_size_one("49874444198739873000000G", 0); test_config_parse_iec_size_one("garbage", 0); } TEST(config_parse_si_uint64) { test_config_parse_si_uint64_one("1024", 1024); test_config_parse_si_uint64_one("2K", 2000); test_config_parse_si_uint64_one("10M", 10 * 1000 * 1000); test_config_parse_si_uint64_one("1G", 1 * 1000 * 1000 * 1000); test_config_parse_si_uint64_one("0G", 0); test_config_parse_si_uint64_one("0", 0); test_config_parse_si_uint64_one("-982", 0); test_config_parse_si_uint64_one("49874444198739873000000G", 0); test_config_parse_si_uint64_one("garbage", 0); } TEST(config_parse_int) { test_config_parse_int_one("1024", 1024); test_config_parse_int_one("-1024", -1024); test_config_parse_int_one("0", 0); test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1); test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1); test_config_parse_int_one("1G", -1); test_config_parse_int_one("garbage", -1); } TEST(config_parse_unsigned) { test_config_parse_unsigned_one("10241024", 10241024); test_config_parse_unsigned_one("1024", 1024); test_config_parse_unsigned_one("0", 0); test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0); test_config_parse_unsigned_one("1G", 0); test_config_parse_unsigned_one("garbage", 0); test_config_parse_unsigned_one("1000garbage", 0); } TEST(config_parse_strv) { test_config_parse_strv_one("", STRV_MAKE_EMPTY); test_config_parse_strv_one("foo", STRV_MAKE("foo")); test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo")); test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo")); test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80")); test_config_parse_strv_one("\xc3\x7f", STRV_MAKE("\xc3\x7f")); } TEST(config_parse_mode) { test_config_parse_mode_one("777", 0777); test_config_parse_mode_one("644", 0644); test_config_parse_mode_one("-777", 0); test_config_parse_mode_one("999", 0); test_config_parse_mode_one("garbage", 0); test_config_parse_mode_one("777garbage", 0); test_config_parse_mode_one("777 garbage", 0); } TEST(config_parse_sec) { test_config_parse_sec_one("1", 1 * USEC_PER_SEC); test_config_parse_sec_one("1s", 1 * USEC_PER_SEC); test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC); test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC); test_config_parse_sec_one("-1", 0); test_config_parse_sec_one("10foo", 0); test_config_parse_sec_one("garbage", 0); } TEST(config_parse_nsec) { test_config_parse_nsec_one("1", 1); test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC); test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC); test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC); test_config_parse_nsec_one("-1", 0); test_config_parse_nsec_one("10foo", 0); test_config_parse_nsec_one("garbage", 0); } TEST(config_parse_iec_uint64) { uint64_t offset = 0; assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0); assert_se(offset == 4 * 1024 * 1024); assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0); } #define x10(x) x x x x x x x x x x #define x100(x) x10(x10(x)) #define x1000(x) x10(x100(x)) static const char* const config_file[] = { "[Section]\n" "setting1=1\n", "[Section]\n" "setting1=1", /* no terminating newline */ "\n\n\n\n[Section]\n\n\n" "setting1=1", /* some whitespace, no terminating newline */ "[Section]\n" "[Section]\n" "setting1=1\n" "setting1= 2 \t\n" "setting1= 1\n", /* repeated settings */ "[Section]\n" "[Section]\n" "setting1=1\n" "setting1=2\\\n" " \n" /* empty line breaks continuation */ "setting1=1\n", /* repeated settings */ "[Section]\n" "setting1=1\\\n" /* normal continuation */ "2\\\n" "3\n", "[Section]\n" "#hogehoge\\\n" /* continuation is ignored in comment */ "setting1=1\\\n" /* normal continuation */ "2\\\n" "3\n", "[Section]\n" "setting1=1\\\n" /* normal continuation */ "#hogehoge\\\n" /* commented out line in continuation is ignored */ "2\\\n" "3\n", "[Section]\n" " #hogehoge\\\n" /* whitespaces before comments */ " setting1=1\\\n" /* whitespaces before key */ "2\\\n" "3\n", "[Section]\n" " setting1=1\\\n" /* whitespaces before key */ " #hogehoge\\\n" /* commented out line prefixed with whitespaces in continuation */ "2\\\n" "3\n", "[Section]\n" "setting1=1\\\n" /* continuation with extra trailing backslash at the end */ "2\\\n" "3\\\n", "[Section]\n" "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */ "\\\\2\n", /* note that C requires one level of escaping, so the * parser gets "…1 BS BS BS NL BS BS 2 NL", which * it translates into "…1 BS BS SP BS BS 2" */ "\n[Section]\n\n" "setting1=" /* a line above LINE_MAX length */ x1000("ABCD") "\n", "[Section]\n" "setting1=" /* a line above LINE_MAX length, with continuation */ x1000("ABCD") "\\\n" "foobar", "[Section]\n" "setting1=" /* a line above LINE_MAX length, with continuation */ x1000("ABCD") "\\\n" /* and an extra trailing backslash */ "foobar\\\n", "[Section]\n" "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */ x1000(x1000("x") x10("abcde")) "\n", "[Section]\n" "setting1=" /* many continuation lines, together above the limit */ x1000(x1000("x") x10("abcde") "\\\n") "xxx", "[Section]\n" "setting1=2\n" "[NoWarnSection]\n" "setting1=3\n" "[WarnSection]\n" "setting1=3\n" "[X-Section]\n" "setting1=3\n", }; static void test_config_parse_one(unsigned i, const char *s) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX"; _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *setting1 = NULL; int r; const ConfigTableItem items[] = { { "Section", "setting1", config_parse_string, 0, &setting1}, {} }; log_info("== %s[%u] ==", __func__, i); assert_se(fmkostemp_safe(name, "r+", &f) == 0); assert_se(fwrite(s, strlen(s), 1, f) == 1); rewind(f); /* int config_parse(const char *unit, const char *filename, FILE *f, const char *sections, ConfigItemLookup lookup, const void *table, ConfigParseFlags flags, void *userdata, struct stat *ret_stat); */ r = config_parse(NULL, name, f, "Section\0" "-NoWarnSection\0", config_item_table_lookup, items, CONFIG_PARSE_WARN, NULL, NULL); switch (i) { case 0 ... 4: assert_se(r == 1); assert_se(streq(setting1, "1")); break; case 5 ... 10: assert_se(r == 1); assert_se(streq(setting1, "1 2 3")); break; case 11: assert_se(r == 1); assert_se(streq(setting1, "1\\\\ \\\\2")); break; case 12: assert_se(r == 1); assert_se(streq(setting1, x1000("ABCD"))); break; case 13 ... 14: assert_se(r == 1); assert_se(streq(setting1, x1000("ABCD") " foobar")); break; case 15 ... 16: assert_se(r == -ENOBUFS); assert_se(setting1 == NULL); break; case 17: assert_se(r == 1); assert_se(streq(setting1, "2")); break; } } TEST(config_parse) { for (unsigned i = 0; i < ELEMENTSOF(config_file); i++) test_config_parse_one(i, config_file[i]); } DEFINE_TEST_MAIN(LOG_INFO);
13,755
33.913706
129
c
null
systemd-main/src/test/test-core-unit.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "escape.h" #include "tests.h" #include "unit.h" static void test_unit_escape_setting_one( const char *s, const char *expected_exec_env, const char *expected_exec, const char *expected_c) { _cleanup_free_ char *a = NULL, *b, *c, *d, *s_esc, *a_esc, *b_esc, *c_esc, *d_esc; const char *t; if (!expected_exec_env) expected_exec_env = s; if (!expected_exec) expected_exec = expected_exec_env; if (!expected_c) expected_c = expected_exec; assert_se(s_esc = cescape(s)); assert_se(t = unit_escape_setting(s, 0, &a)); assert_se(a_esc = cescape(t)); log_debug("%s: [%s] → [%s]", __func__, s_esc, a_esc); assert_se(a == NULL); assert_se(t == s); assert_se(t = unit_escape_setting(s, UNIT_ESCAPE_EXEC_SYNTAX_ENV, &b)); assert_se(b_esc = cescape(t)); log_debug("%s: [%s] → [%s]", __func__, s_esc, b_esc); assert_se(b == NULL || streq(b, t)); assert_se(streq(t, expected_exec_env)); assert_se(t = unit_escape_setting(s, UNIT_ESCAPE_EXEC_SYNTAX, &c)); assert_se(c_esc = cescape(t)); log_debug("%s: [%s] → [%s]", __func__, s_esc, c_esc); assert_se(c == NULL || streq(c, t)); assert_se(streq(t, expected_exec)); assert_se(t = unit_escape_setting(s, UNIT_ESCAPE_C, &d)); assert_se(d_esc = cescape(t)); log_debug("%s: [%s] → [%s]", __func__, s_esc, d_esc); assert_se(d == NULL || streq(d, t)); assert_se(streq(t, expected_c)); } TEST(unit_escape_setting) { test_unit_escape_setting_one("/sbin/sbash", NULL, NULL, NULL); test_unit_escape_setting_one("$", "$$", "$", "$"); test_unit_escape_setting_one("$$", "$$$$", "$$", "$$"); test_unit_escape_setting_one("'", "'", NULL, "\\'"); test_unit_escape_setting_one("\"", "\\\"", NULL, NULL); test_unit_escape_setting_one("\t", "\\t", NULL, NULL); test_unit_escape_setting_one(" ", NULL, NULL, NULL); test_unit_escape_setting_one("$;'\"\t\n", "$$;'\\\"\\t\\n", "$;'\\\"\\t\\n", "$;\\'\\\"\\t\\n"); } static void test_unit_concat_strv_one( char **s, const char *expected_none, const char *expected_exec_env, const char *expected_exec, const char *expected_c) { _cleanup_free_ char *a, *b, *c, *d, *s_ser, *s_esc, *a_esc, *b_esc, *c_esc, *d_esc; assert_se(s_ser = strv_join(s, "_")); assert_se(s_esc = cescape(s_ser)); if (!expected_exec_env) expected_exec_env = expected_none; if (!expected_exec) expected_exec = expected_none; if (!expected_c) expected_c = expected_none; assert_se(a = unit_concat_strv(s, 0)); assert_se(a_esc = cescape(a)); log_debug("%s: [%s] → [%s]", __func__, s_esc, a_esc); assert_se(streq(a, expected_none)); assert_se(b = unit_concat_strv(s, UNIT_ESCAPE_EXEC_SYNTAX_ENV)); assert_se(b_esc = cescape(b)); log_debug("%s: [%s] → [%s]", __func__, s_esc, b_esc); assert_se(streq(b, expected_exec_env)); assert_se(c = unit_concat_strv(s, UNIT_ESCAPE_EXEC_SYNTAX)); assert_se(c_esc = cescape(c)); log_debug("%s: [%s] → [%s]", __func__, s_esc, c_esc); assert_se(streq(c, expected_exec)); assert_se(d = unit_concat_strv(s, UNIT_ESCAPE_C)); assert_se(d_esc = cescape(d)); log_debug("%s: [%s] → [%s]", __func__, s_esc, d_esc); assert_se(streq(d, expected_c)); } TEST(unit_concat_strv) { test_unit_concat_strv_one(STRV_MAKE("a", "b", "c"), "\"a\" \"b\" \"c\"", NULL, NULL, NULL); test_unit_concat_strv_one(STRV_MAKE("a", " ", "$", "$$", ""), "\"a\" \" \" \"$\" \"$$\" \"\"", "\"a\" \" \" \"$$\" \"$$$$\" \"\"", NULL, NULL); test_unit_concat_strv_one(STRV_MAKE("\n", " ", "\t"), "\"\n\" \" \" \"\t\"", "\"\\n\" \" \" \"\\t\"", "\"\\n\" \" \" \"\\t\"", "\"\\n\" \" \" \"\\t\""); } DEFINE_TEST_MAIN(LOG_DEBUG);
4,698
37.834711
104
c
null
systemd-main/src/test/test-coredump-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <elf.h> #include "alloc-util.h" #include "coredump-util.h" #include "fileio.h" #include "fd-util.h" #include "format-util.h" #include "macro.h" #include "tests.h" TEST(coredump_filter_to_from_string) { for (CoredumpFilter i = 0; i < _COREDUMP_FILTER_MAX; i++) { const char *n; assert_se(n = coredump_filter_to_string(i)); log_info("0x%x\t%s", 1u << i, n); assert_se(coredump_filter_from_string(n) == i); uint64_t f; assert_se(coredump_filter_mask_from_string(n, &f) == 0); assert_se(f == 1u << i); } } TEST(coredump_filter_mask_from_string) { uint64_t f; assert_se(coredump_filter_mask_from_string("default", &f) == 0); assert_se(f == COREDUMP_FILTER_MASK_DEFAULT); assert_se(coredump_filter_mask_from_string("all", &f) == 0); assert_se(f == COREDUMP_FILTER_MASK_ALL); assert_se(coredump_filter_mask_from_string(" default\tdefault\tdefault ", &f) == 0); assert_se(f == COREDUMP_FILTER_MASK_DEFAULT); assert_se(coredump_filter_mask_from_string("defaulta", &f) < 0); assert_se(coredump_filter_mask_from_string("default defaulta default", &f) < 0); assert_se(coredump_filter_mask_from_string("default default defaulta", &f) < 0); assert_se(coredump_filter_mask_from_string("private-anonymous default", &f) == 0); assert_se(f == COREDUMP_FILTER_MASK_DEFAULT); assert_se(coredump_filter_mask_from_string("shared-file-backed shared-dax", &f) == 0); assert_se(f == (1 << COREDUMP_FILTER_SHARED_FILE_BACKED | 1 << COREDUMP_FILTER_SHARED_DAX)); assert_se(coredump_filter_mask_from_string("private-file-backed private-dax 0xF", &f) == 0); assert_se(f == (1 << COREDUMP_FILTER_PRIVATE_FILE_BACKED | 1 << COREDUMP_FILTER_PRIVATE_DAX | 0xF)); assert_se(coredump_filter_mask_from_string("11", &f) == 0); assert_se(f == 0x11); assert_se(coredump_filter_mask_from_string("0x1101", &f) == 0); assert_se(f == 0x1101); assert_se(coredump_filter_mask_from_string("0", &f) == 0); assert_se(f == 0); assert_se(coredump_filter_mask_from_string("all", &f) == 0); assert_se(FLAGS_SET(f, (1 << COREDUMP_FILTER_PRIVATE_ANONYMOUS | 1 << COREDUMP_FILTER_SHARED_ANONYMOUS | 1 << COREDUMP_FILTER_PRIVATE_FILE_BACKED | 1 << COREDUMP_FILTER_SHARED_FILE_BACKED | 1 << COREDUMP_FILTER_ELF_HEADERS | 1 << COREDUMP_FILTER_PRIVATE_HUGE | 1 << COREDUMP_FILTER_SHARED_HUGE | 1 << COREDUMP_FILTER_PRIVATE_DAX | 1 << COREDUMP_FILTER_SHARED_DAX))); } static void test_parse_auxv_two( uint8_t elf_class, size_t offset, const char *data, size_t data_size, int expect_at_secure, uid_t expect_uid, uid_t expect_euid, gid_t expect_gid, gid_t expect_egid) { int at_secure; uid_t uid, euid; gid_t gid, egid; assert_se(parse_auxv(LOG_ERR, elf_class, data, data_size, &at_secure, &uid, &euid, &gid, &egid) == 0); log_debug("[offset=%zu] at_secure=%d, uid="UID_FMT", euid="UID_FMT", gid="GID_FMT", egid="GID_FMT, offset, at_secure, uid, euid, gid, egid); assert_se(uid == expect_uid); assert_se(euid == expect_euid); assert_se(gid == expect_gid); assert_se(egid == expect_egid); } static void test_parse_auxv_one( uint8_t elf_class, int dir_fd, const char *filename, int expect_at_secure, uid_t expect_uid, uid_t expect_euid, gid_t expect_gid, gid_t expect_egid) { _cleanup_free_ char *buf; const char *data; size_t data_size; log_info("Parsing %s…", filename); assert_se(read_full_file_at(dir_fd, filename, &buf, &data_size) >= 0); for (size_t offset = 0; offset < 8; offset++) { _cleanup_free_ char *buf2 = NULL; if (offset == 0) data = buf; else { assert_se(buf2 = malloc(offset + data_size)); memcpy(buf2 + offset, buf, data_size); data = buf2 + offset; } test_parse_auxv_two(elf_class, offset, data, data_size, expect_at_secure, expect_uid, expect_euid, expect_gid, expect_egid); } } TEST(test_parse_auxv) { _cleanup_free_ char *dir = NULL; _cleanup_close_ int dir_fd = -EBADF; assert_se(get_testdata_dir("auxv", &dir) >= 0); dir_fd = open(dir, O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_PATH); assert_se(dir_fd >= 0); if (__BYTE_ORDER == __LITTLE_ENDIAN) { test_parse_auxv_one(ELFCLASS32, dir_fd, "resolved.arm32", 0, 193, 193, 193, 193); test_parse_auxv_one(ELFCLASS64, dir_fd, "bash.riscv64", 0, 1001, 1001, 1001, 1001); test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep.i686", 0, 1000, 1000, 1000, 1000); /* after chgrp and chmod g+s */ test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep32.i686", 1, 1000, 1000, 1000, 10); test_parse_auxv_one(ELFCLASS64, dir_fd, "sleep64.amd64", 1, 1000, 1000, 1000, 10); test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.aarch64", 1, 1494200408, 0, 1494200408, 1494200408); test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.amd64", 1, 1000, 0, 1000, 1000); /* Those run unprivileged, but start as root. */ test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.amd64", 0, 0, 0, 0, 0); test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.aarch64", 0, 0, 0, 0, 0); test_parse_auxv_one(ELFCLASS64, dir_fd, "polkitd.aarch64", 0, 0, 0, 0, 0); } else { test_parse_auxv_one(ELFCLASS64, dir_fd, "cat.s390x", 0, 3481, 3481, 3481, 3481); } } DEFINE_TEST_MAIN(LOG_INFO);
6,632
39.944444
114
c
null
systemd-main/src/test/test-cpu-set-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "cpu-set-util.h" #include "string-util.h" #include "tests.h" #include "macro.h" TEST(parse_cpu_set) { CPUSet c = {}; _cleanup_free_ char *str = NULL; int cpu; /* Single value */ assert_se(parse_cpu_set_full("0", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.set); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_ISSET_S(0, c.allocated, c.set)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 1); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "0")); str = mfree(str); cpu_set_reset(&c); /* Simple range (from CPUAffinity example) */ assert_se(parse_cpu_set_full("1 2 4", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.set); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_ISSET_S(1, c.allocated, c.set)); assert_se(CPU_ISSET_S(2, c.allocated, c.set)); assert_se(CPU_ISSET_S(4, c.allocated, c.set)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 3); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "1-2 4")); str = mfree(str); cpu_set_reset(&c); /* A more interesting range */ assert_se(parse_cpu_set_full("0 1 2 3 8 9 10 11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 8); for (cpu = 0; cpu < 4; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); for (cpu = 8; cpu < 12; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "0-3 8-11")); str = mfree(str); cpu_set_reset(&c); /* Quoted strings */ assert_se(parse_cpu_set_full("8 '9' 10 \"11\"", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 4); for (cpu = 8; cpu < 12; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "8-11")); str = mfree(str); cpu_set_reset(&c); /* Use commas as separators */ assert_se(parse_cpu_set_full("0,1,2,3 8,9,10,11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 8); for (cpu = 0; cpu < 4; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); for (cpu = 8; cpu < 12; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); cpu_set_reset(&c); /* Commas with spaces (and trailing comma, space) */ assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, 63, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 9); for (cpu = 0; cpu < 8; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(CPU_ISSET_S(63, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "0-7 63")); str = mfree(str); cpu_set_reset(&c); /* Ranges */ assert_se(parse_cpu_set_full("0-3,8-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 8); for (cpu = 0; cpu < 4; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); for (cpu = 8; cpu < 12; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); cpu_set_reset(&c); /* Ranges with trailing comma, space */ assert_se(parse_cpu_set_full("0-3 8-11, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 8); for (cpu = 0; cpu < 4; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); for (cpu = 8; cpu < 12; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "0-3 8-11")); str = mfree(str); cpu_set_reset(&c); /* Negative range (returns empty cpu_set) */ assert_se(parse_cpu_set_full("3-0", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 0); cpu_set_reset(&c); /* Overlapping ranges */ assert_se(parse_cpu_set_full("0-7 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 12); for (cpu = 0; cpu < 12; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "0-11")); str = mfree(str); cpu_set_reset(&c); /* Mix ranges and individual CPUs */ assert_se(parse_cpu_set_full("0,2 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.allocated >= DIV_ROUND_UP(sizeof(__cpu_mask), 8)); assert_se(CPU_COUNT_S(c.allocated, c.set) == 10); assert_se(CPU_ISSET_S(0, c.allocated, c.set)); assert_se(CPU_ISSET_S(2, c.allocated, c.set)); for (cpu = 4; cpu < 12; cpu++) assert_se(CPU_ISSET_S(cpu, c.allocated, c.set)); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "0 2 4-11")); str = mfree(str); cpu_set_reset(&c); /* Garbage */ assert_se(parse_cpu_set_full("0 1 2 3 garbage", &c, true, NULL, "fake", 1, "CPUAffinity") == -EINVAL); assert_se(!c.set); assert_se(c.allocated == 0); /* Range with garbage */ assert_se(parse_cpu_set_full("0-3 8-garbage", &c, true, NULL, "fake", 1, "CPUAffinity") == -EINVAL); assert_se(!c.set); assert_se(c.allocated == 0); /* Empty string */ assert_se(parse_cpu_set_full("", &c, true, NULL, "fake", 1, "CPUAffinity") == 0); assert_se(!c.set); /* empty string returns NULL */ assert_se(c.allocated == 0); /* Runaway quoted string */ assert_se(parse_cpu_set_full("0 1 2 3 \"4 5 6 7 ", &c, true, NULL, "fake", 1, "CPUAffinity") == -EINVAL); assert_se(!c.set); assert_se(c.allocated == 0); /* Maximum allocation */ assert_se(parse_cpu_set_full("8000-8191", &c, true, NULL, "fake", 1, "CPUAffinity") == 0); assert_se(CPU_COUNT_S(c.allocated, c.set) == 192); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); assert_se(streq(str, "8000-8191")); str = mfree(str); cpu_set_reset(&c); } TEST(parse_cpu_set_extend) { CPUSet c = {}; _cleanup_free_ char *s1 = NULL, *s2 = NULL; assert_se(parse_cpu_set_extend("1 3", &c, true, NULL, "fake", 1, "CPUAffinity") == 1); assert_se(CPU_COUNT_S(c.allocated, c.set) == 2); assert_se(s1 = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", s1); assert_se(parse_cpu_set_extend("4", &c, true, NULL, "fake", 1, "CPUAffinity") == 1); assert_se(CPU_COUNT_S(c.allocated, c.set) == 3); assert_se(s2 = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", s2); assert_se(parse_cpu_set_extend("", &c, true, NULL, "fake", 1, "CPUAffinity") == 0); assert_se(!c.set); assert_se(c.allocated == 0); log_info("cpu_set_to_string: (null)"); } TEST(cpu_set_to_from_dbus) { _cleanup_(cpu_set_reset) CPUSet c = {}, c2 = {}; _cleanup_free_ char *s = NULL; assert_se(parse_cpu_set_extend("1 3 8 100-200", &c, true, NULL, "fake", 1, "CPUAffinity") == 1); assert_se(s = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", s); assert_se(CPU_COUNT_S(c.allocated, c.set) == 104); _cleanup_free_ uint8_t *array = NULL; size_t allocated; static const char expected[32] = "\x0A\x01\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\xF0\xFF\xFF\xFF\xFF\xFF\xFF\xFF" "\xFF\xFF\xFF\xFF\xFF\x01"; assert_se(cpu_set_to_dbus(&c, &array, &allocated) == 0); assert_se(array); assert_se(allocated == c.allocated); assert_se(allocated <= sizeof expected); assert_se(allocated >= DIV_ROUND_UP(201u, 8u)); /* We need at least 201 bits for our mask */ assert_se(memcmp(array, expected, allocated) == 0); assert_se(cpu_set_from_dbus(array, allocated, &c2) == 0); assert_se(c2.set); assert_se(c2.allocated == c.allocated); assert_se(memcmp(c.set, c2.set, c.allocated) == 0); } TEST(cpus_in_affinity_mask) { int r; r = cpus_in_affinity_mask(); assert_se(r > 0); log_info("cpus_in_affinity_mask: %d", r); } TEST(print_cpu_alloc_size) { log_info("CPU_ALLOC_SIZE(1) = %zu", CPU_ALLOC_SIZE(1)); log_info("CPU_ALLOC_SIZE(9) = %zu", CPU_ALLOC_SIZE(9)); log_info("CPU_ALLOC_SIZE(64) = %zu", CPU_ALLOC_SIZE(64)); log_info("CPU_ALLOC_SIZE(65) = %zu", CPU_ALLOC_SIZE(65)); log_info("CPU_ALLOC_SIZE(1024) = %zu", CPU_ALLOC_SIZE(1024)); log_info("CPU_ALLOC_SIZE(1025) = %zu", CPU_ALLOC_SIZE(1025)); log_info("CPU_ALLOC_SIZE(8191) = %zu", CPU_ALLOC_SIZE(8191)); } DEFINE_TEST_MAIN(LOG_INFO);
11,923
41.434164
117
c
null
systemd-main/src/test/test-cryptolib.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "gcrypt-util.h" #include "macro.h" #include "openssl-util.h" #include "string-util.h" #include "tests.h" TEST(string_hashsum) { _cleanup_free_ char *out1 = NULL, *out2 = NULL, *out3 = NULL, *out4 = NULL; assert_se(string_hashsum("asdf", 4, OPENSSL_OR_GCRYPT(EVP_sha224(), GCRY_MD_SHA224), &out1) == 0); /* echo -n 'asdf' | sha224sum - */ assert_se(streq(out1, "7872a74bcbf298a1e77d507cd95d4f8d96131cbbd4cdfc571e776c8a")); assert_se(string_hashsum("asdf", 4, OPENSSL_OR_GCRYPT(EVP_sha256(), GCRY_MD_SHA256), &out2) == 0); /* echo -n 'asdf' | sha256sum - */ assert_se(streq(out2, "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b")); assert_se(string_hashsum("", 0, OPENSSL_OR_GCRYPT(EVP_sha224(), GCRY_MD_SHA224), &out3) == 0); /* echo -n '' | sha224sum - */ assert_se(streq(out3, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")); assert_se(string_hashsum("", 0, OPENSSL_OR_GCRYPT(EVP_sha256(), GCRY_MD_SHA256), &out4) == 0); /* echo -n '' | sha256sum - */ assert_se(streq(out4, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); } DEFINE_TEST_MAIN(LOG_INFO);
1,563
39.102564
99
c
null
systemd-main/src/test/test-daemon.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <unistd.h> #include "sd-daemon.h" #include "parse-util.h" #include "strv.h" #include "time-util.h" int main(int argc, char *argv[]) { _cleanup_strv_free_ char **l = NULL; int n, i; usec_t duration = USEC_PER_SEC / 10; if (argc >= 2) { unsigned x; assert_se(safe_atou(argv[1], &x) >= 0); duration = x * USEC_PER_SEC; } n = sd_listen_fds_with_names(false, &l); if (n < 0) { log_error_errno(n, "Failed to get listening fds: %m"); return EXIT_FAILURE; } for (i = 0; i < n; i++) log_info("fd=%i name=%s", SD_LISTEN_FDS_START + i, l[i]); sd_notify(0, "STATUS=Starting up"); usleep_safe(duration); sd_notify(0, "STATUS=Running\n" "READY=1"); usleep_safe(duration); sd_notify(0, "STATUS=Reloading\n" "RELOADING=1"); usleep_safe(duration); sd_notify(0, "STATUS=Running\n" "READY=1"); usleep_safe(duration); sd_notify(0, "STATUS=Quitting\n" "STOPPING=1"); usleep_safe(duration); return EXIT_SUCCESS; }
1,382
22.844828
73
c
null
systemd-main/src/test/test-data-fd-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include "data-fd-util.h" #include "fd-util.h" #include "memory-util.h" #include "process-util.h" #include "tests.h" #include "random-util.h" static void test_acquire_data_fd_one(unsigned flags) { char wbuffer[196*1024 - 7]; char rbuffer[sizeof(wbuffer)]; int fd; fd = acquire_data_fd("foo", 3, flags); assert_se(fd >= 0); zero(rbuffer); assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3); assert_se(streq(rbuffer, "foo")); fd = safe_close(fd); fd = acquire_data_fd("", 0, flags); assert_se(fd >= 0); zero(rbuffer); assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0); assert_se(streq(rbuffer, "")); fd = safe_close(fd); random_bytes(wbuffer, sizeof(wbuffer)); fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags); assert_se(fd >= 0); zero(rbuffer); assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer)); assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0); fd = safe_close(fd); } TEST(acquire_data_fd) { test_acquire_data_fd_one(0); test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL); test_acquire_data_fd_one(ACQUIRE_NO_MEMFD); test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD); test_acquire_data_fd_one(ACQUIRE_NO_PIPE); test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE); test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE); test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE); test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE); } static void assert_equal_fd(int fd1, int fd2) { for (;;) { uint8_t a[4096], b[4096]; ssize_t x, y; x = read(fd1, a, sizeof(a)); assert_se(x >= 0); y = read(fd2, b, sizeof(b)); assert_se(y >= 0); assert_se(x == y); if (x == 0) break; assert_se(memcmp(a, b, x) == 0); } } TEST(copy_data_fd) { _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF; _cleanup_close_pair_ int sfd[2] = PIPE_EBADF; _cleanup_(sigkill_waitp) pid_t pid = -1; int r; fd1 = open("/etc/fstab", O_RDONLY|O_CLOEXEC); if (fd1 >= 0) { fd2 = copy_data_fd(fd1); assert_se(fd2 >= 0); assert_se(lseek(fd1, 0, SEEK_SET) == 0); assert_equal_fd(fd1, fd2); } fd1 = safe_close(fd1); fd2 = safe_close(fd2); fd1 = acquire_data_fd("hallo", 6, 0); assert_se(fd1 >= 0); fd2 = copy_data_fd(fd1); assert_se(fd2 >= 0); safe_close(fd1); fd1 = acquire_data_fd("hallo", 6, 0); assert_se(fd1 >= 0); assert_equal_fd(fd1, fd2); fd1 = safe_close(fd1); fd2 = safe_close(fd2); assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sfd) >= 0); r = safe_fork("(sd-pipe)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); assert_se(r >= 0); if (r == 0) { /* child */ sfd[0] = safe_close(sfd[0]); for (uint64_t i = 0; i < 1536*1024 / sizeof(uint64_t); i++) assert_se(write(sfd[1], &i, sizeof(i)) == sizeof(i)); sfd[1] = safe_close(sfd[1]); _exit(EXIT_SUCCESS); } sfd[1] = safe_close(sfd[1]); fd2 = copy_data_fd(sfd[0]); assert_se(fd2 >= 0); uint64_t j; for (uint64_t i = 0; i < 1536*1024 / sizeof(uint64_t); i++) { assert_se(read(fd2, &j, sizeof(j)) == sizeof(j)); assert_se(i == j); } assert_se(read(fd2, &j, sizeof(j)) == 0); } DEFINE_TEST_MAIN(LOG_DEBUG);
4,105
26.557047
106
c
null
systemd-main/src/test/test-date.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "string-util.h" #include "tests.h" #include "time-util.h" static void test_should_pass(const char *p) { usec_t t, q; char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX]; log_info("Test: %s", p); assert_se(parse_timestamp(p, &t) >= 0); assert_se(format_timestamp_style(buf, sizeof(buf), t, TIMESTAMP_US)); log_info("\"%s\" → \"%s\"", p, buf); assert_se(parse_timestamp(buf, &q) >= 0); if (q != t) log_error("round-trip failed: \"%s\" → \"%s\"", buf, FORMAT_TIMESTAMP_STYLE(q, TIMESTAMP_US)); assert_se(q == t); assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t)); log_info("%s", strna(buf_relative)); } static void test_should_parse(const char *p) { usec_t t; log_info("Test: %s", p); assert_se(parse_timestamp(p, &t) >= 0); log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t); } static void test_should_fail(const char *p) { usec_t t; int r; log_info("Test: %s", p); r = parse_timestamp(p, &t); if (r >= 0) log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t); else log_info("parse_timestamp() returns %d (expected)", r); assert_se(r < 0); } static void test_one(const char *p) { _cleanup_free_ char *with_utc = NULL; with_utc = strjoin(p, " UTC"); test_should_pass(p); test_should_pass(with_utc); } static void test_one_noutc(const char *p) { _cleanup_free_ char *with_utc = NULL; with_utc = strjoin(p, " UTC"); test_should_pass(p); test_should_fail(with_utc); } int main(int argc, char *argv[]) { test_setup_logging(LOG_DEBUG); test_one("17:41"); test_one("18:42:44"); test_one("18:42:44.0"); test_one("18:42:44.999999999999"); test_one("12-10-02 12:13:14"); test_one("12-10-2 12:13:14"); test_one("12-10-03 12:13"); test_one("2012-12-30 18:42"); test_one("2012-10-02"); test_one("Mar 12 12:01:01"); test_one("Mar 12 12:01:01.687197"); test_one("Tue 2012-10-02"); test_one("yesterday"); test_one("today"); test_one("tomorrow"); test_one_noutc("16:20 UTC"); test_one_noutc("16:20 Asia/Seoul"); test_one_noutc("tomorrow Asia/Seoul"); test_one_noutc("2012-12-30 18:42 Asia/Seoul"); test_one_noutc("now"); test_one_noutc("+2d"); test_one_noutc("+2y 4d"); test_one_noutc("5months ago"); test_one_noutc("@1395716396"); test_should_parse("1970-1-1 UTC"); test_should_pass("1970-1-1 00:00:01 UTC"); test_should_fail("1969-12-31 UTC"); test_should_fail("-1000y"); test_should_fail("today UTC UTC"); test_should_fail("now Asia/Seoul"); test_should_fail("+2d Asia/Seoul"); test_should_fail("@1395716396 Asia/Seoul"); #if SIZEOF_TIME_T == 8 test_should_pass("9999-12-30 23:59:59 UTC"); test_should_fail("9999-12-31 00:00:00 UTC"); test_should_fail("10000-01-01 00:00:00 UTC"); #elif SIZEOF_TIME_T == 4 test_should_pass("2038-01-18 03:14:07 UTC"); test_should_fail("2038-01-18 03:14:08 UTC"); #endif return 0; }
3,473
30.581818
84
c
null
systemd-main/src/test/test-dev-setup.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "capability-util.h" #include "dev-setup.h" #include "fs-util.h" #include "mkdir.h" #include "path-util.h" #include "rm-rf.h" #include "tmpfile-util.h" int main(int argc, char *argv[]) { _cleanup_(rm_rf_physical_and_freep) char *p = NULL; const char *f; struct stat st; if (have_effective_cap(CAP_DAC_OVERRIDE) <= 0) return EXIT_TEST_SKIP; assert_se(mkdtemp_malloc("/tmp/test-dev-setupXXXXXX", &p) >= 0); f = prefix_roota(p, "/run/systemd"); assert_se(mkdir_p(f, 0755) >= 0); assert_se(make_inaccessible_nodes(f, 1, 1) >= 0); f = prefix_roota(p, "/run/systemd/inaccessible/reg"); assert_se(stat(f, &st) >= 0); assert_se(S_ISREG(st.st_mode)); assert_se((st.st_mode & 07777) == 0000); f = prefix_roota(p, "/run/systemd/inaccessible/dir"); assert_se(stat(f, &st) >= 0); assert_se(S_ISDIR(st.st_mode)); assert_se((st.st_mode & 07777) == 0000); f = prefix_roota(p, "/run/systemd/inaccessible/fifo"); assert_se(stat(f, &st) >= 0); assert_se(S_ISFIFO(st.st_mode)); assert_se((st.st_mode & 07777) == 0000); f = prefix_roota(p, "/run/systemd/inaccessible/sock"); assert_se(stat(f, &st) >= 0); assert_se(S_ISSOCK(st.st_mode)); assert_se((st.st_mode & 07777) == 0000); f = prefix_roota(p, "/run/systemd/inaccessible/chr"); if (stat(f, &st) < 0) assert_se(errno == ENOENT); else { assert_se(S_ISCHR(st.st_mode)); assert_se((st.st_mode & 07777) == 0000); } f = prefix_roota(p, "/run/systemd/inaccessible/blk"); if (stat(f, &st) < 0) assert_se(errno == ENOENT); else { assert_se(S_ISBLK(st.st_mode)); assert_se((st.st_mode & 07777) == 0000); } return EXIT_SUCCESS; }
1,999
30.25
72
c
null
systemd-main/src/test/test-device-nodes.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <stdio.h> #include <sys/types.h> #include "alloc-util.h" #include "device-nodes.h" #include "string-util.h" #include "tests.h" /* helpers for test_encode_devnode_name */ static char *do_encode_string(const char *in) { size_t out_len = strlen(in) * 4 + 1; char *out = malloc(out_len); assert_se(out); assert_se(encode_devnode_name(in, out, out_len) >= 0); puts(out); return out; } static bool expect_encoded_as(const char *in, const char *expected) { _cleanup_free_ char *encoded = do_encode_string(in); return streq(encoded, expected); } TEST(encode_devnode_name) { assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks")); assert_se(expect_encoded_as("pinkiepie", "pinkiepie")); assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8")); assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng")); assert_se(expect_encoded_as("/", "\\x2f")); assert_se(expect_encoded_as("!", "\\x21")); assert_se(expect_encoded_as("QEMU ", "QEMU\\x20\\x20\\x20\\x20")); } DEFINE_TEST_MAIN(LOG_DEBUG);
1,190
29.538462
77
c
null
systemd-main/src/test/test-devnum-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/stat.h> #include "devnum-util.h" #include "path-util.h" #include "stat-util.h" #include "tests.h" TEST(parse_devnum) { dev_t dev; assert_se(parse_devnum("", &dev) == -EINVAL); assert_se(parse_devnum("junk", &dev) == -EINVAL); assert_se(parse_devnum("0", &dev) == -EINVAL); assert_se(parse_devnum("5", &dev) == -EINVAL); assert_se(parse_devnum("5:", &dev) == -EINVAL); assert_se(parse_devnum(":5", &dev) == -EINVAL); assert_se(parse_devnum("-1:-1", &dev) == -EINVAL); #if SIZEOF_DEV_T < 8 assert_se(parse_devnum("4294967295:4294967295", &dev) == -EINVAL); #endif assert_se(parse_devnum("8:11", &dev) >= 0 && major(dev) == 8 && minor(dev) == 11); assert_se(parse_devnum("0:0", &dev) >= 0 && major(dev) == 0 && minor(dev) == 0); } TEST(device_major_minor_valid) { /* on glibc dev_t is 64-bit, even though in the kernel it is only 32-bit */ assert_cc(sizeof(dev_t) == sizeof(uint64_t)); assert_se(DEVICE_MAJOR_VALID(0U)); assert_se(DEVICE_MINOR_VALID(0U)); assert_se(DEVICE_MAJOR_VALID(1U)); assert_se(DEVICE_MINOR_VALID(1U)); assert_se(!DEVICE_MAJOR_VALID(-1U)); assert_se(!DEVICE_MINOR_VALID(-1U)); assert_se(DEVICE_MAJOR_VALID(1U << 10)); assert_se(DEVICE_MINOR_VALID(1U << 10)); assert_se(DEVICE_MAJOR_VALID((1U << 12) - 1)); assert_se(DEVICE_MINOR_VALID((1U << 20) - 1)); assert_se(!DEVICE_MAJOR_VALID((1U << 12))); assert_se(!DEVICE_MINOR_VALID((1U << 20))); assert_se(!DEVICE_MAJOR_VALID(1U << 25)); assert_se(!DEVICE_MINOR_VALID(1U << 25)); assert_se(!DEVICE_MAJOR_VALID(UINT32_MAX)); assert_se(!DEVICE_MINOR_VALID(UINT32_MAX)); assert_se(!DEVICE_MAJOR_VALID(UINT64_MAX)); assert_se(!DEVICE_MINOR_VALID(UINT64_MAX)); assert_se(DEVICE_MAJOR_VALID(major(0))); assert_se(DEVICE_MINOR_VALID(minor(0))); } static void test_device_path_make_canonical_one(const char *path) { _cleanup_free_ char *resolved = NULL, *raw = NULL; struct stat st; dev_t devno; mode_t mode; int r; log_debug("> %s", path); if (stat(path, &st) < 0) { assert_se(errno == ENOENT); log_notice("Path %s not found, skipping test", path); return; } r = device_path_make_canonical(st.st_mode, st.st_rdev, &resolved); if (r == -ENOENT) { /* maybe /dev/char/x:y and /dev/block/x:y are missing in this test environment, because we * run in a container or so? */ log_notice("Device %s cannot be resolved, skipping test", path); return; } assert_se(r >= 0); assert_se(path_equal(path, resolved)); assert_se(device_path_make_major_minor(st.st_mode, st.st_rdev, &raw) >= 0); assert_se(device_path_parse_major_minor(raw, &mode, &devno) >= 0); assert_se(st.st_rdev == devno); assert_se((st.st_mode & S_IFMT) == (mode & S_IFMT)); } TEST(device_path_make_canonical) { test_device_path_make_canonical_one("/dev/null"); test_device_path_make_canonical_one("/dev/zero"); test_device_path_make_canonical_one("/dev/full"); test_device_path_make_canonical_one("/dev/random"); test_device_path_make_canonical_one("/dev/urandom"); test_device_path_make_canonical_one("/dev/tty"); if (is_device_node("/run/systemd/inaccessible/blk") > 0) { test_device_path_make_canonical_one("/run/systemd/inaccessible/chr"); test_device_path_make_canonical_one("/run/systemd/inaccessible/blk"); } } static void test_devnum_format_str_one(dev_t devnum, const char *s) { dev_t x; assert_se(streq(FORMAT_DEVNUM(devnum), s)); assert_se(parse_devnum(s, &x) >= 0); assert_se(x == devnum); } TEST(devnum_format_str) { test_devnum_format_str_one(makedev(0, 0), "0:0"); test_devnum_format_str_one(makedev(1, 2), "1:2"); test_devnum_format_str_one(makedev(99, 100), "99:100"); test_devnum_format_str_one(makedev(4095, 1048575), "4095:1048575"); } DEFINE_TEST_MAIN(LOG_INFO);
4,361
33.896
106
c
null
systemd-main/src/test/test-emergency-action.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "emergency-action.h" #include "tests.h" TEST(parse_emergency_action) { EmergencyAction x; assert_se(parse_emergency_action("none", RUNTIME_SCOPE_USER, &x) == 0); assert_se(x == EMERGENCY_ACTION_NONE); assert_se(parse_emergency_action("reboot", RUNTIME_SCOPE_USER, &x) == -EOPNOTSUPP); assert_se(parse_emergency_action("reboot-force", RUNTIME_SCOPE_USER, &x) == -EOPNOTSUPP); assert_se(parse_emergency_action("reboot-immediate", RUNTIME_SCOPE_USER, &x) == -EOPNOTSUPP); assert_se(parse_emergency_action("poweroff", RUNTIME_SCOPE_USER, &x) == -EOPNOTSUPP); assert_se(parse_emergency_action("poweroff-force", RUNTIME_SCOPE_USER, &x) == -EOPNOTSUPP); assert_se(parse_emergency_action("poweroff-immediate", RUNTIME_SCOPE_USER, &x) == -EOPNOTSUPP); assert_se(x == EMERGENCY_ACTION_NONE); assert_se(parse_emergency_action("exit", RUNTIME_SCOPE_USER, &x) == 0); assert_se(x == EMERGENCY_ACTION_EXIT); assert_se(parse_emergency_action("exit-force", RUNTIME_SCOPE_USER, &x) == 0); assert_se(x == EMERGENCY_ACTION_EXIT_FORCE); assert_se(parse_emergency_action("exit-forcee", RUNTIME_SCOPE_USER, &x) == -EINVAL); assert_se(parse_emergency_action("none", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(x == EMERGENCY_ACTION_NONE); assert_se(parse_emergency_action("reboot", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(x == EMERGENCY_ACTION_REBOOT); assert_se(parse_emergency_action("reboot-force", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(x == EMERGENCY_ACTION_REBOOT_FORCE); assert_se(parse_emergency_action("reboot-immediate", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(x == EMERGENCY_ACTION_REBOOT_IMMEDIATE); assert_se(parse_emergency_action("poweroff", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(x == EMERGENCY_ACTION_POWEROFF); assert_se(parse_emergency_action("poweroff-force", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(x == EMERGENCY_ACTION_POWEROFF_FORCE); assert_se(parse_emergency_action("poweroff-immediate", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(parse_emergency_action("exit", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(parse_emergency_action("exit-force", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(parse_emergency_action("exit-forcee", RUNTIME_SCOPE_SYSTEM, &x) == -EINVAL); assert_se(x == EMERGENCY_ACTION_EXIT_FORCE); assert_se(parse_emergency_action("kexec", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(parse_emergency_action("kexec-force", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(parse_emergency_action("kexec-forcee", RUNTIME_SCOPE_SYSTEM, &x) == -EINVAL); assert_se(x == EMERGENCY_ACTION_KEXEC_FORCE); assert_se(parse_emergency_action("halt", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(parse_emergency_action("halt-force", RUNTIME_SCOPE_SYSTEM, &x) == 0); assert_se(parse_emergency_action("halt-forcee", RUNTIME_SCOPE_SYSTEM, &x) == -EINVAL); assert_se(x == EMERGENCY_ACTION_HALT_FORCE); } DEFINE_TEST_MAIN(LOG_INFO);
3,214
60.826923
103
c
null
systemd-main/src/test/test-env-file.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "env-file.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" #include "macro.h" #include "strv.h" #include "tests.h" #include "tmpfile-util.h" /* In case of repeating keys, later entries win. */ #define env_file_1 \ "a=a\n" \ "a=b\n" \ "a=b\n" \ "a=a\n" \ "b=b\\\n" \ "c\n" \ "d= d\\\n" \ "e \\\n" \ "f \n" \ "g=g\\ \n" \ "h= ąęół\\ śćńźżμ \n" \ "i=i\\" #define env_file_2 \ "a=a\\\n" #define env_file_3 \ "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \ "#--nouser-config \\\n" \ "normal1=line\\\n" \ "111\n" \ ";normal=ignored \\\n" \ "normal2=line222\n" \ "normal ignored \\\n" #define env_file_4 \ "# Generated\n" \ "\n" \ "HWMON_MODULES=\"coretemp f71882fg\"\n" \ "\n" \ "# For compatibility reasons\n" \ "\n" \ "MODULE_0=coretemp\n" \ "MODULE_1=f71882fg" #define env_file_5 \ "a=\n" \ "b=" #define env_file_6 \ "a=\\ \\n \\t \\x \\y \\' \n" \ "b= \\$' \n" \ "c= ' \\n\\t\\$\\`\\\\\n" \ "' \n" \ "d= \" \\n\\t\\$\\`\\\\\n" \ "\" \n" TEST(load_env_file_1) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, env_file_1) == 0); _cleanup_strv_free_ char **data = NULL; assert_se(load_env_file(NULL, name, &data) == 0); assert_se(streq(data[0], "a=a")); assert_se(streq(data[1], "b=bc")); assert_se(streq(data[2], "d=de f")); assert_se(streq(data[3], "g=g ")); assert_se(streq(data[4], "h=ąęół śćńźżμ")); assert_se(streq(data[5], "i=i")); assert_se(data[6] == NULL); } TEST(load_env_file_2) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, env_file_2) == 0); _cleanup_strv_free_ char **data = NULL; assert_se(load_env_file(NULL, name, &data) == 0); assert_se(streq(data[0], "a=a")); assert_se(data[1] == NULL); } TEST(load_env_file_3) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, env_file_3) == 0); _cleanup_strv_free_ char **data = NULL; assert_se(load_env_file(NULL, name, &data) == 0); assert_se(streq(data[0], "normal1=line111")); assert_se(streq(data[1], "normal2=line222")); assert_se(data[2] == NULL); } TEST(load_env_file_4) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, env_file_4) == 0); _cleanup_strv_free_ char **data = NULL; assert_se(load_env_file(NULL, name, &data) == 0); assert_se(streq(data[0], "HWMON_MODULES=coretemp f71882fg")); assert_se(streq(data[1], "MODULE_0=coretemp")); assert_se(streq(data[2], "MODULE_1=f71882fg")); assert_se(data[3] == NULL); } TEST(load_env_file_5) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, env_file_5) == 0); _cleanup_strv_free_ char **data = NULL; assert_se(load_env_file(NULL, name, &data) == 0); assert_se(streq(data[0], "a=")); assert_se(streq(data[1], "b=")); assert_se(data[2] == NULL); } TEST(load_env_file_6) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, env_file_6) == 0); _cleanup_strv_free_ char **data = NULL; assert_se(load_env_file(NULL, name, &data) == 0); assert_se(streq(data[0], "a= n t x y '")); assert_se(streq(data[1], "b=$'")); assert_se(streq(data[2], "c= \\n\\t\\$\\`\\\\\n")); assert_se(streq(data[3], "d= \\n\\t$`\\\n")); assert_se(data[4] == NULL); } TEST(load_env_file_invalid_utf8) { /* Test out a couple of assignments where the key/value has an invalid * UTF-8 character ("noncharacter") * * See: https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Non-characters */ FOREACH_STRING(s, "fo\ufffeo=bar", "foo=b\uffffar", "baz=hello world\ufffe") { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, s) == 0); _cleanup_strv_free_ char **data = NULL; assert_se(load_env_file(NULL, name, &data) == -EINVAL); assert_se(!data); } } TEST(write_and_load_env_file) { /* Make sure that our writer, parser and the shell agree on what our env var files mean */ FOREACH_STRING(v, "obbardc-laptop", "obbardc\\-laptop", "obbardc-lap\\top", "obbardc-lap\\top", "obbardc-lap\\\\top", "double\"quote", "single\'quote", "dollar$dollar", "newline\nnewline") { _cleanup_(unlink_and_freep) char *p = NULL; _cleanup_strv_free_ char **l = NULL; _cleanup_free_ char *j = NULL, *w = NULL, *cmd = NULL, *from_shell = NULL; _cleanup_pclose_ FILE *f = NULL; size_t sz; assert_se(tempfn_random_child(NULL, NULL, &p) >= 0); assert_se(j = strjoin("TEST=", v)); assert_se(write_env_file(p, STRV_MAKE(j)) >= 0); assert_se(cmd = strjoin(". ", p, " && /bin/echo -n \"$TEST\"")); assert_se(f = popen(cmd, "re")); assert_se(read_full_stream(f, &from_shell, &sz) >= 0); assert_se(sz == strlen(v)); assert_se(streq(from_shell, v)); assert_se(load_env_file(NULL, p, &l) >= 0); assert_se(strv_equal(l, STRV_MAKE(j))); assert_se(parse_env_file(NULL, p, "TEST", &w) >= 0); assert_se(streq_ptr(w, v)); } } DEFINE_TEST_MAIN(LOG_INFO);
7,416
37.630208
98
c
null
systemd-main/src/test/test-errno-list.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <errno.h> #include "errno-list.h" #include "errno-to-name.h" #include "macro.h" #include "string-util.h" #include "tests.h" TEST(errno_list) { for (size_t i = 0; i < ELEMENTSOF(errno_names); i++) { if (errno_names[i]) { assert_se(streq(errno_to_name(i), errno_names[i])); assert_se(errno_from_name(errno_names[i]) == (int) i); } } #ifdef ECANCELLED /* ECANCELLED is an alias of ECANCELED. */ assert_se(streq(errno_to_name(ECANCELLED), "ECANCELED")); #endif assert_se(streq(errno_to_name(ECANCELED), "ECANCELED")); #ifdef EREFUSED /* EREFUSED is an alias of ECONNREFUSED. */ assert_se(streq(errno_to_name(EREFUSED), "ECONNREFUSED")); #endif assert_se(streq(errno_to_name(ECONNREFUSED), "ECONNREFUSED")); } DEFINE_TEST_MAIN(LOG_INFO);
941
27.545455
78
c
null
systemd-main/src/test/test-errno-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "errno-util.h" #include "stdio-util.h" #include "string-util.h" #include "tests.h" TEST(strerror_not_threadsafe) { /* Just check that strerror really is not thread-safe. */ log_info("strerror(%d) → %s", 200, strerror(200)); log_info("strerror(%d) → %s", 201, strerror(201)); log_info("strerror(%d) → %s", INT_MAX, strerror(INT_MAX)); log_info("strerror(%d), strerror(%d) → %p, %p", 200, 201, strerror(200), strerror(201)); /* This call is not allowed, because the first returned string becomes invalid when * we call strerror the second time: * * log_info("strerror(%d), strerror(%d) → %s, %s", 200, 201, strerror(200), strerror(201)); */ } TEST(STRERROR) { /* Just check that STRERROR really is thread-safe. */ log_info("STRERROR(%d) → %s", 200, STRERROR(200)); log_info("STRERROR(%d) → %s", 201, STRERROR(201)); log_info("STRERROR(%d), STRERROR(%d) → %s, %s", 200, 201, STRERROR(200), STRERROR(201)); const char *a = STRERROR(200), *b = STRERROR(201); assert_se(strstr(a, "200")); assert_se(strstr(b, "201")); /* Check with negative values */ assert_se(streq(a, STRERROR(-200))); assert_se(streq(b, STRERROR(-201))); const char *c = STRERROR(INT_MAX); char buf[DECIMAL_STR_MAX(int)]; xsprintf(buf, "%d", INT_MAX); /* INT_MAX is hexadecimal, use printf to convert to decimal */ log_info("STRERROR(%d) → %s", INT_MAX, c); assert_se(strstr(c, buf)); } TEST(STRERROR_OR_ELSE) { log_info("STRERROR_OR_ELSE(0, \"EOF\") → %s", STRERROR_OR_EOF(0)); log_info("STRERROR_OR_ELSE(EPERM, \"EOF\") → %s", STRERROR_OR_EOF(EPERM)); log_info("STRERROR_OR_ELSE(-EPERM, \"EOF\") → %s", STRERROR_OR_EOF(-EPERM)); } TEST(PROTECT_ERRNO) { errno = 12; { PROTECT_ERRNO; errno = 11; } assert_se(errno == 12); } static void test_unprotect_errno_inner_function(void) { PROTECT_ERRNO; errno = 2222; } TEST(UNPROTECT_ERRNO) { errno = 4711; PROTECT_ERRNO; errno = 815; UNPROTECT_ERRNO; assert_se(errno == 4711); test_unprotect_errno_inner_function(); assert_se(errno == 4711); } DEFINE_TEST_MAIN(LOG_INFO);
2,422
28.54878
101
c
null
systemd-main/src/test/test-escape.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "escape.h" #include "macro.h" #include "tests.h" TEST(cescape) { _cleanup_free_ char *t = NULL; assert_se(t = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313")); assert_se(streq(t, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313")); } TEST(xescape) { _cleanup_free_ char *t = NULL; assert_se(t = xescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "")); assert_se(streq(t, "abc\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb")); } static void test_xescape_full_one(bool eight_bits) { const char* escaped = !eight_bits ? "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb" : "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\177\234\313"; const unsigned full_fit = !eight_bits ? 55 : 46; XEscapeFlags flags = eight_bits * XESCAPE_8_BIT; log_info("/* %s */", __func__); for (unsigned i = 0; i < 60; i++) { _cleanup_free_ char *t = NULL, *q = NULL; assert_se(t = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, flags)); log_info("%02u: <%s>", i, t); if (i >= full_fit) assert_se(streq(t, escaped)); else if (i >= 3) { /* We need up to four columns, so up to three columns may be wasted */ assert_se(strlen(t) == i || strlen(t) == i - 1 || strlen(t) == i - 2 || strlen(t) == i - 3); assert_se(strneq(t, escaped, i - 3) || strneq(t, escaped, i - 4) || strneq(t, escaped, i - 5) || strneq(t, escaped, i - 6)); assert_se(endswith(t, "...")); } else { assert_se(strlen(t) == i); assert_se(strneq(t, "...", i)); } assert_se(q = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, flags | XESCAPE_FORCE_ELLIPSIS)); log_info("%02u: <%s>", i, q); if (i > 0) assert_se(endswith(q, ".")); assert_se(strlen(q) <= i); assert_se(strlen(q) + 3 >= strlen(t)); } } TEST(test_xescape_full) { test_xescape_full_one(false); test_xescape_full_one(true); } TEST(cunescape) { _cleanup_free_ char *unescaped = NULL; assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0); assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00")); unescaped = mfree(unescaped); /* incomplete sequences */ assert_se(cunescape("\\x0", 0, &unescaped) < 0); assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\x0")); unescaped = mfree(unescaped); assert_se(cunescape("\\x", 0, &unescaped) < 0); assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\x")); unescaped = mfree(unescaped); assert_se(cunescape("\\", 0, &unescaped) < 0); assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\")); unescaped = mfree(unescaped); assert_se(cunescape("\\11", 0, &unescaped) < 0); assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\11")); unescaped = mfree(unescaped); assert_se(cunescape("\\1", 0, &unescaped) < 0); assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\1")); unescaped = mfree(unescaped); assert_se(cunescape("\\u0000", 0, &unescaped) < 0); assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "ßßΠA")); unescaped = mfree(unescaped); assert_se(cunescape("\\073", 0, &unescaped) >= 0); assert_se(streq_ptr(unescaped, ";")); unescaped = mfree(unescaped); assert_se(cunescape("A=A\\\\x0aB", 0, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "A=A\\x0aB")); unescaped = mfree(unescaped); assert_se(cunescape("A=A\\\\x0aB", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "A=A\\x0aB")); unescaped = mfree(unescaped); assert_se(cunescape("\\x00\\x00\\x00", UNESCAPE_ACCEPT_NUL, &unescaped) == 3); assert_se(memcmp(unescaped, "\0\0\0", 3) == 0); unescaped = mfree(unescaped); assert_se(cunescape("\\u0000\\u0000\\u0000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3); assert_se(memcmp(unescaped, "\0\0\0", 3) == 0); unescaped = mfree(unescaped); assert_se(cunescape("\\U00000000\\U00000000\\U00000000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3); assert_se(memcmp(unescaped, "\0\0\0", 3) == 0); unescaped = mfree(unescaped); assert_se(cunescape("\\000\\000\\000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3); assert_se(memcmp(unescaped, "\0\0\0", 3) == 0); } static void test_shell_escape_one(const char *s, const char *bad, const char *expected) { _cleanup_free_ char *r = NULL; assert_se(r = shell_escape(s, bad)); log_debug("%s → %s (expected %s)", s, r, expected); assert_se(streq_ptr(r, expected)); } TEST(shell_escape) { test_shell_escape_one("", "", ""); test_shell_escape_one("\\", "", "\\\\"); test_shell_escape_one("foobar", "", "foobar"); test_shell_escape_one("foobar", "o", "f\\o\\obar"); test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz"); test_shell_escape_one("foo\nbar\nbaz", ",:", "foo\\nbar\\nbaz"); } static void test_shell_maybe_quote_one(const char *s, ShellEscapeFlags flags, const char *expected) { _cleanup_free_ char *ret = NULL; assert_se(ret = shell_maybe_quote(s, flags)); log_debug("[%s] → [%s] (%s)", s, ret, expected); assert_se(streq(ret, expected)); } TEST(shell_maybe_quote) { test_shell_maybe_quote_one("", 0, ""); test_shell_maybe_quote_one("", SHELL_ESCAPE_EMPTY, "\"\""); test_shell_maybe_quote_one("", SHELL_ESCAPE_POSIX, ""); test_shell_maybe_quote_one("", SHELL_ESCAPE_POSIX | SHELL_ESCAPE_EMPTY, "\"\""); test_shell_maybe_quote_one("\\", 0, "\"\\\\\""); test_shell_maybe_quote_one("\\", SHELL_ESCAPE_POSIX, "$'\\\\'"); test_shell_maybe_quote_one("\"", 0, "\"\\\"\""); test_shell_maybe_quote_one("\"", SHELL_ESCAPE_POSIX, "$'\"'"); test_shell_maybe_quote_one("foobar", 0, "foobar"); test_shell_maybe_quote_one("foobar", SHELL_ESCAPE_POSIX, "foobar"); test_shell_maybe_quote_one("foo bar", 0, "\"foo bar\""); test_shell_maybe_quote_one("foo bar", SHELL_ESCAPE_POSIX, "$'foo bar'"); test_shell_maybe_quote_one("foo\tbar", 0, "\"foo\\tbar\""); test_shell_maybe_quote_one("foo\tbar", SHELL_ESCAPE_POSIX, "$'foo\\tbar'"); test_shell_maybe_quote_one("foo\nbar", 0, "\"foo\\nbar\""); test_shell_maybe_quote_one("foo\nbar", SHELL_ESCAPE_POSIX, "$'foo\\nbar'"); test_shell_maybe_quote_one("foo \"bar\" waldo", 0, "\"foo \\\"bar\\\" waldo\""); test_shell_maybe_quote_one("foo \"bar\" waldo", SHELL_ESCAPE_POSIX, "$'foo \"bar\" waldo'"); test_shell_maybe_quote_one("foo$bar", 0, "\"foo\\$bar\""); test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_EMPTY, "\"foo\\$bar\""); test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_POSIX, "$'foo$bar'"); test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_POSIX | SHELL_ESCAPE_EMPTY, "$'foo$bar'"); /* Exclamation mark is special in the interactive shell, but we don't treat it so. */ test_shell_maybe_quote_one("foo!bar", 0, "\"foo!bar\""); test_shell_maybe_quote_one("foo!bar", SHELL_ESCAPE_POSIX, "$'foo!bar'"); /* Control characters and unicode */ test_shell_maybe_quote_one("a\nb\001", 0, "\"a\\nb\\001\""); test_shell_maybe_quote_one("a\nb\001", SHELL_ESCAPE_POSIX, "$'a\\nb\\001'"); test_shell_maybe_quote_one("głąb", 0, "głąb"); test_shell_maybe_quote_one("głąb", SHELL_ESCAPE_POSIX, "głąb"); test_shell_maybe_quote_one("głąb\002\003", 0, "\"głąb\\002\\003\""); test_shell_maybe_quote_one("głąb\002\003", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003'"); test_shell_maybe_quote_one("głąb\002\003rząd", 0, "\"głąb\\002\\003rząd\""); test_shell_maybe_quote_one("głąb\002\003rząd", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003rząd'"); /* Bogus UTF-8 strings */ test_shell_maybe_quote_one("\250\350", 0, "\"\\250\\350\""); test_shell_maybe_quote_one("\250\350", SHELL_ESCAPE_POSIX, "$'\\250\\350'"); } static void test_quote_command_line_one(char **argv, const char *expected) { _cleanup_free_ char *s = NULL; assert_se(s = quote_command_line(argv, SHELL_ESCAPE_EMPTY)); log_info("%s", s); assert_se(streq(s, expected)); } TEST(quote_command_line) { test_quote_command_line_one(STRV_MAKE("true", "true"), "true true"); test_quote_command_line_one(STRV_MAKE("true", "with a space"), "true \"with a space\""); test_quote_command_line_one(STRV_MAKE("true", "with a 'quote'"), "true \"with a 'quote'\""); test_quote_command_line_one(STRV_MAKE("true", "with a \"quote\""), "true \"with a \\\"quote\\\"\""); test_quote_command_line_one(STRV_MAKE("true", "$dollar"), "true \"\\$dollar\""); } static void test_octescape_one(const char *s, const char *expected) { _cleanup_free_ char *ret = NULL; assert_se(ret = octescape(s, strlen_ptr(s))); log_debug("octescape(\"%s\") → \"%s\" (expected: \"%s\")", strnull(s), ret, expected); assert_se(streq(ret, expected)); } TEST(octescape) { test_octescape_one(NULL, ""); test_octescape_one("", ""); test_octescape_one("foo", "foo"); test_octescape_one("\"\\\"", "\\042\\134\\042"); test_octescape_one("\123\213\222", "\123\\213\\222"); } DEFINE_TEST_MAIN(LOG_DEBUG);
10,748
43.234568
128
c
null
systemd-main/src/test/test-execve.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "exec-util.h" #include "fd-util.h" #include "log.h" #include "main-func.h" #include "strv.h" #include "tests.h" /* This program can be used to call programs through fexecve / execveat(…, "", …, AT_EMPTY_PATH), * when compiled with -Dfexecve=true, and the fallback paths, when -Dfexecve=false. * * Example: * $ strace -e execveat build/test-execve /bin/grep Name /proc/self/status * execveat(3, "", ["/bin/grep", "Name", "/proc/self/status"], NULL, AT_EMPTY_PATH) = 0 * Name: 3 * * FIXME: use the new kernel api to set COMM properly when the kernel makes that available. * C.f. ceedbf8185fc7593366679f02d31da63af8c4bd1. */ static int run(int argc, char **argv) { _cleanup_close_ int fd = -EBADF; char **args = strv_skip(argv, 1); int r; test_setup_logging(LOG_DEBUG); args = !strv_isempty(args) ? args : STRV_MAKE("/bin/true"); fd = open(args[0], O_RDONLY | O_CLOEXEC); if (fd < 0) return log_error_errno(errno, "open(%s) failed: %m", args[0]); r = fexecve_or_execve(fd, args[0], args, NULL); assert_se(r < 0); return log_error_errno(r, "fexecve_or_execve(%s) failed: %m", args[0]); } DEFINE_MAIN_FUNCTION(run);
1,285
30.365854
97
c
null
systemd-main/src/test/test-exit-status.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "exit-status.h" #include "string-util.h" #include "tests.h" TEST(exit_status_to_string) { for (int i = -1; i <= 256; i++) { const char *s, *class; s = exit_status_to_string(i, EXIT_STATUS_FULL); class = exit_status_class(i); log_info("%d: %s%s%s%s", i, s ?: "-", class ? " (" : "", strempty(class), class ? ")" : ""); if (s) assert_se(exit_status_from_string(s) == i); } } TEST(exit_status_from_string) { assert_se(exit_status_from_string("11") == 11); assert_se(exit_status_from_string("-1") == -ERANGE); assert_se(exit_status_from_string("256") == -ERANGE); assert_se(exit_status_from_string("foo") == -EINVAL); assert_se(exit_status_from_string("SUCCESS") == 0); assert_se(exit_status_from_string("FAILURE") == 1); } TEST(exit_status_NUMA_POLICY) { assert_se(streq(exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_FULL), "NUMA_POLICY")); assert_se(streq(exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_SYSTEMD), "NUMA_POLICY")); assert_se(!exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_BSD)); assert_se(!exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_LSB)); } DEFINE_TEST_MAIN(LOG_DEBUG);
1,425
35.564103
102
c
null
systemd-main/src/test/test-fdset.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <fcntl.h> #include <unistd.h> #include "fd-util.h" #include "fdset.h" #include "fs-util.h" #include "macro.h" #include "tests.h" #include "tmpfile-util.h" TEST(fdset_new_fill) { int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; log_close(); log_set_open_when_needed(true); fd = open("/dev/null", O_CLOEXEC|O_RDONLY); assert_se(fd >= 0); assert_se(fdset_new_fill(/* filter_cloexec= */ -1, &fdset) >= 0); assert_se(fdset_contains(fdset, fd)); fdset = fdset_free(fdset); assert_se(fcntl(fd, F_GETFD) < 0); assert_se(errno == EBADF); fd = open("/dev/null", O_CLOEXEC|O_RDONLY); assert_se(fd >= 0); assert_se(fdset_new_fill(/* filter_cloexec= */ 0, &fdset) >= 0); assert_se(!fdset_contains(fdset, fd)); fdset = fdset_free(fdset); assert_se(fcntl(fd, F_GETFD) >= 0); assert_se(fdset_new_fill(/* filter_cloexec= */ 1, &fdset) >= 0); assert_se(fdset_contains(fdset, fd)); fdset = fdset_free(fdset); assert_se(fcntl(fd, F_GETFD) < 0); assert_se(errno == EBADF); fd = open("/dev/null", O_RDONLY); assert_se(fd >= 0); assert_se(fdset_new_fill(/* filter_cloexec= */ 1, &fdset) >= 0); assert_se(!fdset_contains(fdset, fd)); fdset = fdset_free(fdset); assert_se(fcntl(fd, F_GETFD) >= 0); assert_se(fdset_new_fill(/* filter_cloexec= */ 0, &fdset) >= 0); assert_se(fdset_contains(fdset, fd)); fdset = fdset_free(fdset); assert_se(fcntl(fd, F_GETFD) < 0); assert_se(errno == EBADF); log_open(); } TEST(fdset_put_dup) { _cleanup_close_ int fd = -EBADF; int copyfd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_put_dup.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); fdset = fdset_new(); assert_se(fdset); copyfd = fdset_put_dup(fdset, fd); assert_se(copyfd >= 0 && copyfd != fd); assert_se(fdset_contains(fdset, copyfd)); assert_se(!fdset_contains(fdset, fd)); } TEST(fdset_cloexec) { int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; int flags = -1; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_cloexec.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); fdset = fdset_new(); assert_se(fdset); assert_se(fdset_put(fdset, fd)); assert_se(fdset_cloexec(fdset, false) >= 0); flags = fcntl(fd, F_GETFD); assert_se(flags >= 0); assert_se(!(flags & FD_CLOEXEC)); assert_se(fdset_cloexec(fdset, true) >= 0); flags = fcntl(fd, F_GETFD); assert_se(flags >= 0); assert_se(flags & FD_CLOEXEC); } TEST(fdset_close_others) { int fd = -EBADF; int copyfd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; int flags = -1; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_close_others.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); fdset = fdset_new(); assert_se(fdset); copyfd = fdset_put_dup(fdset, fd); assert_se(copyfd >= 0); assert_se(fdset_close_others(fdset) >= 0); flags = fcntl(fd, F_GETFD); assert_se(flags < 0); flags = fcntl(copyfd, F_GETFD); assert_se(flags >= 0); } TEST(fdset_remove) { _cleanup_close_ int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_remove.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); fdset = fdset_new(); assert_se(fdset); assert_se(fdset_put(fdset, fd) >= 0); assert_se(fdset_remove(fdset, fd) >= 0); assert_se(!fdset_contains(fdset, fd)); assert_se(fcntl(fd, F_GETFD) >= 0); } TEST(fdset_iterate) { int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_iterate.XXXXXX"; int c = 0; int a; fd = mkostemp_safe(name); assert_se(fd >= 0); fdset = fdset_new(); assert_se(fdset); assert_se(fdset_put(fdset, fd) >= 0); assert_se(fdset_put(fdset, fd) >= 0); assert_se(fdset_put(fdset, fd) >= 0); FDSET_FOREACH(a, fdset) { c++; assert_se(a == fd); } assert_se(c == 1); } TEST(fdset_isempty) { int fd; _cleanup_fdset_free_ FDSet *fdset = NULL; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_isempty.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); fdset = fdset_new(); assert_se(fdset); assert_se(fdset_isempty(fdset)); assert_se(fdset_put(fdset, fd) >= 0); assert_se(!fdset_isempty(fdset)); } TEST(fdset_steal_first) { int fd; _cleanup_fdset_free_ FDSet *fdset = NULL; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_steal_first.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); fdset = fdset_new(); assert_se(fdset); assert_se(fdset_steal_first(fdset) < 0); assert_se(fdset_put(fdset, fd) >= 0); assert_se(fdset_steal_first(fdset) == fd); assert_se(fdset_steal_first(fdset) < 0); assert_se(fdset_put(fdset, fd) >= 0); } TEST(fdset_new_array) { int fds[] = {10, 11, 12, 13}; _cleanup_fdset_free_ FDSet *fdset = NULL; assert_se(fdset_new_array(&fdset, fds, 4) >= 0); assert_se(fdset_size(fdset) == 4); assert_se(fdset_contains(fdset, 10)); assert_se(fdset_contains(fdset, 11)); assert_se(fdset_contains(fdset, 12)); assert_se(fdset_contains(fdset, 13)); } DEFINE_TEST_MAIN(LOG_INFO);
6,093
28.019048
88
c
null
systemd-main/src/test/test-fstab-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <stdio.h> #include "alloc-util.h" #include "fstab-util.h" #include "log.h" #include "string-util.h" #include "strv.h" #include "tests.h" /* int fstab_filter_options( const char *opts, const char *names, const char **ret_namefound, const char **ret_value, const char **ret_values, char **ret_filtered); */ static void do_fstab_filter_options(const char *opts, const char *remove, int r_expected, int r_values_expected, const char *name_expected, const char *value_expected, const char *values_expected, const char *filtered_expected) { int r; const char *name; _cleanup_free_ char *value = NULL, *filtered = NULL, *joined = NULL; _cleanup_strv_free_ char **values = NULL; /* test mode which returns the last value */ r = fstab_filter_options(opts, remove, &name, &value, NULL, &filtered); log_info("1: \"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"", opts, r, strnull(name), value, filtered, r_expected, strnull(name_expected), strnull(value_expected), filtered_expected ?: opts); assert_se(r == r_expected); assert_se(streq_ptr(name, name_expected)); assert_se(streq_ptr(value, value_expected)); assert_se(streq_ptr(filtered, filtered_expected ?: opts)); /* test mode which returns all the values */ r = fstab_filter_options(opts, remove, &name, NULL, &values, NULL); assert_se(joined = strv_join(values, ":")); log_info("2: \"%s\" → %d, \"%s\", \"%s\", expected %d, \"%s\", \"%s\"", opts, r, strnull(name), joined, r_values_expected, strnull(name_expected), strnull(values_expected)); assert_se(r == r_values_expected); assert_se(streq_ptr(name, r_values_expected > 0 ? name_expected : NULL)); assert_se(streq_ptr(joined, values_expected)); /* also test the malloc-less mode */ r = fstab_filter_options(opts, remove, &name, NULL, NULL, NULL); log_info("3: \"%s\" → %d, \"%s\", expected %d, \"%s\"\n-", opts, r, strnull(name), r_expected, strnull(name_expected)); assert_se(r == r_expected); assert_se(streq_ptr(name, name_expected)); } TEST(fstab_filter_options) { do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, 1, "opt", "0", "0", ""); do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, 1, "opt", "0", "0", ""); do_fstab_filter_options("opt", "opt\0x-opt\0", 1, 0, "opt", NULL, "", ""); do_fstab_filter_options("opt", "x-opt\0opt\0", 1, 0, "opt", NULL, "", ""); do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, 0, "x-opt", NULL, "", ""); do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "other"); do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, 1, "opt", "0", "0", "other"); do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "other"); do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, 0, "opt", NULL, "", "other"); do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, 0, "x-opt", NULL, "", "other"); do_fstab_filter_options("opt=0\\,1,other", "opt\0x-opt\0", 1, 1, "opt", "0,1", "0,1", "other"); do_fstab_filter_options("opt=0,other,x-opt\\,foobar", "x-opt\0opt\0", 1, 1, "opt", "0", "0", "other,x-opt\\,foobar"); do_fstab_filter_options("opt,other,x-opt\\,part", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "other,x-opt\\,part"); do_fstab_filter_options("opt,other,part\\,x-opt", "x-opt\0opt\0", 1, 0, "opt", NULL, "", "other,part\\,x-opt"); do_fstab_filter_options("opt,other\\,\\,\\,opt,x-part", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "other\\,\\,\\,opt,x-part"); do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL); do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL); do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL); do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "first"); do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "first=1"); do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, 1, "opt", "", "", "first"); do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "first=1"); do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, 0, "x-opt", NULL, "", "first=1"); do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "first,last=1"); do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, 1, "opt", "0", "0", "first=1,last=2"); do_fstab_filter_options("first,opt,last", "opt\0", 1, 0, "opt", NULL, "", "first,last"); do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, 0, "opt", NULL, "", "first=1,last"); do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, 0, "opt", NULL, "", "first=,last"); /* check repeated options */ do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, 1, "noopt", "1", "0:1", "first,last=1"); do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, 1, "opt", "1", "0:1", "first=1,last=2"); do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, 1, "x-opt", "1", "0:1", ""); do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, 1, "x-opt", "1", "0:1", ""); do_fstab_filter_options("opt=0,opt=1,opt=,opt=,opt=2", "opt\0noopt\0", 1, 1, "opt", "2", "0:1:::2", ""); /* check that semicolons are not misinterpreted */ do_fstab_filter_options("opt=0;", "opt\0", 1, 1, "opt", "0;", "0;", ""); do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, 0, NULL, NULL, "", NULL); do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL); /* check that spaces are not misinterpreted */ do_fstab_filter_options("opt=0 ", "opt\0", 1, 1, "opt", "0 ", "0 ", ""); do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, 0, NULL, NULL, "", NULL); do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL); /* check function with NULL args */ do_fstab_filter_options(NULL, "opt\0", 0, 0, NULL, NULL, "", ""); do_fstab_filter_options("", "opt\0", 0, 0, NULL, NULL, "", ""); /* unnecessary comma separators */ do_fstab_filter_options("opt=x,,,,", "opt\0", 1, 1, "opt", "x", "x", ""); do_fstab_filter_options(",,,opt=x,,,,", "opt\0", 1, 1, "opt", "x", "x", ""); /* escaped characters */ do_fstab_filter_options("opt1=\\\\,opt2=\\xff", "opt1\0", 1, 1, "opt1", "\\", "\\", "opt2=\\xff"); do_fstab_filter_options("opt1=\\\\,opt2=\\xff", "opt2\0", 1, 1, "opt2", "\\xff", "\\xff", "opt1=\\"); } TEST(fstab_find_pri) { int pri = -1; assert_se(fstab_find_pri("pri", &pri) == 0); assert_se(pri == -1); assert_se(fstab_find_pri("pri=11", &pri) == 1); assert_se(pri == 11); assert_se(fstab_find_pri("pri=-2", &pri) == 1); assert_se(pri == -2); assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1); assert_se(pri == 12); assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1); assert_se(pri == 13); } TEST(fstab_yes_no_option) { assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true); assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false); assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false); assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true); assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false); } TEST(fstab_node_to_udev_node) { char *n; n = fstab_node_to_udev_node("LABEL=applé/jack"); puts(n); assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack")); free(n); n = fstab_node_to_udev_node("PARTLABEL=pinkié pie"); puts(n); assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie")); free(n); n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535"); puts(n); assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535")); free(n); n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535"); puts(n); assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535")); free(n); n = fstab_node_to_udev_node("PONIES=awesome"); puts(n); assert_se(streq(n, "PONIES=awesome")); free(n); n = fstab_node_to_udev_node("/dev/xda1"); puts(n); assert_se(streq(n, "/dev/xda1")); free(n); } DEFINE_TEST_MAIN(LOG_INFO);
9,439
47.911917
131
c
null
systemd-main/src/test/test-glob-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <fcntl.h> #include <sys/stat.h> #include <unistd.h> #include "alloc-util.h" #include "dirent-util.h" #include "fs-util.h" #include "glob-util.h" #include "macro.h" #include "rm-rf.h" #include "tests.h" #include "tmpfile-util.h" TEST(glob_first) { char *first, name[] = "/tmp/test-glob_first.XXXXXX"; int fd = -EBADF; int r; fd = mkostemp_safe(name); assert_se(fd >= 0); close(fd); r = glob_first("/tmp/test-glob_first*", &first); assert_se(r == 1); assert_se(streq(name, first)); first = mfree(first); r = unlink(name); assert_se(r == 0); r = glob_first("/tmp/test-glob_first*", &first); assert_se(r == 0); assert_se(first == NULL); } TEST(glob_exists) { char name[] = "/tmp/test-glob_exists.XXXXXX"; int fd = -EBADF; int r; fd = mkostemp_safe(name); assert_se(fd >= 0); close(fd); r = glob_exists("/tmp/test-glob_exists*"); assert_se(r == 1); r = unlink(name); assert_se(r == 0); r = glob_exists("/tmp/test-glob_exists*"); assert_se(r == 0); } static void closedir_wrapper(void* v) { (void) closedir(v); } TEST(glob_no_dot) { char template[] = "/tmp/test-glob-util.XXXXXXX"; const char *fn; _cleanup_globfree_ glob_t g = { .gl_closedir = closedir_wrapper, .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot, .gl_opendir = (void *(*)(const char *)) opendir, .gl_lstat = lstat, .gl_stat = stat, }; int r; assert_se(mkdtemp(template)); fn = strjoina(template, "/*"); r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); assert_se(r == GLOB_NOMATCH); fn = strjoina(template, "/.*"); r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); assert_se(r == GLOB_NOMATCH); (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); } TEST(safe_glob) { char template[] = "/tmp/test-glob-util.XXXXXXX"; const char *fn, *fn2, *fname; _cleanup_globfree_ glob_t g = {}; int r; assert_se(mkdtemp(template)); fn = strjoina(template, "/*"); r = safe_glob(fn, 0, &g); assert_se(r == -ENOENT); fn2 = strjoina(template, "/.*"); r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g); assert_se(r == -ENOENT); fname = strjoina(template, "/.foobar"); assert_se(touch(fname) == 0); r = safe_glob(fn, 0, &g); assert_se(r == -ENOENT); r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g); assert_se(r == 0); assert_se(g.gl_pathc == 1); assert_se(streq(g.gl_pathv[0], fname)); assert_se(g.gl_pathv[1] == NULL); (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); } static void test_glob_non_glob_prefix_one(const char *path, const char *expected) { _cleanup_free_ char *t; assert_se(glob_non_glob_prefix(path, &t) == 0); assert_se(streq(t, expected)); } TEST(glob_non_glob) { test_glob_non_glob_prefix_one("/tmp/.X11-*", "/tmp/"); test_glob_non_glob_prefix_one("/tmp/*", "/tmp/"); test_glob_non_glob_prefix_one("/tmp*", "/"); test_glob_non_glob_prefix_one("/tmp/*/whatever", "/tmp/"); test_glob_non_glob_prefix_one("/tmp/*/whatever?", "/tmp/"); test_glob_non_glob_prefix_one("/?", "/"); char *x; assert_se(glob_non_glob_prefix("?", &x) == -ENOENT); } DEFINE_TEST_MAIN(LOG_INFO);
3,716
25.934783
83
c
null
systemd-main/src/test/test-gpt.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "architecture.h" #include "glyph-util.h" #include "gpt.h" #include "log.h" #include "pretty-print.h" #include "strv.h" #include "terminal-util.h" #include "tests.h" TEST(gpt_types_against_architectures) { int r; /* Dumps a table indicating for which architectures we know we have matching GPT partition * types. Also validates whether we can properly categorize the entries. */ FOREACH_STRING(prefix, "root-", "usr-") for (Architecture a = 0; a < _ARCHITECTURE_MAX; a++) FOREACH_STRING(suffix, "", "-verity", "-verity-sig") { _cleanup_free_ char *joined = NULL; GptPartitionType type; joined = strjoin(prefix, architecture_to_string(a), suffix); if (!joined) return (void) log_oom(); r = gpt_partition_type_from_string(joined, &type); if (r < 0) { printf("%s %s\n", RED_CROSS_MARK(), joined); continue; } printf("%s %s\n", GREEN_CHECK_MARK(), joined); if (streq(prefix, "root-") && streq(suffix, "")) assert_se(type.designator == PARTITION_ROOT); if (streq(prefix, "root-") && streq(suffix, "-verity")) assert_se(type.designator == PARTITION_ROOT_VERITY); if (streq(prefix, "usr-") && streq(suffix, "")) assert_se(type.designator == PARTITION_USR); if (streq(prefix, "usr-") && streq(suffix, "-verity")) assert_se(type.designator == PARTITION_USR_VERITY); assert_se(type.arch == a); } } TEST(verity_mappings) { for (PartitionDesignator p = 0; p < _PARTITION_DESIGNATOR_MAX; p++) { PartitionDesignator q; q = partition_verity_of(p); assert_se(q < 0 || partition_verity_to_data(q) == p); q = partition_verity_sig_of(p); assert_se(q < 0 || partition_verity_sig_to_data(q) == p); q = partition_verity_to_data(p); assert_se(q < 0 || partition_verity_of(q) == p); q = partition_verity_sig_to_data(p); assert_se(q < 0 || partition_verity_sig_of(q) == p); } } TEST(type_alias_same) { /* Check that the partition type table is consistent, i.e. all aliases of the same partition type * carry the same metadata */ for (const GptPartitionType *t = gpt_partition_type_table; t->name; t++) { GptPartitionType x, y; x = gpt_partition_type_from_uuid(t->uuid); /* search first by uuid */ assert_se(gpt_partition_type_from_string(t->name, &y) >= 0); /* search first by name */ assert_se(t->arch == x.arch); assert_se(t->arch == y.arch); assert_se(t->designator == x.designator); assert_se(t->designator == y.designator); } } TEST(override_architecture) { GptPartitionType x, y; assert_se(gpt_partition_type_from_string("root-x86-64", &x) >= 0); assert_se(x.arch == ARCHITECTURE_X86_64); assert_se(gpt_partition_type_from_string("root-arm64", &y) >= 0); assert(y.arch == ARCHITECTURE_ARM64); x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64); assert_se(x.arch == y.arch); assert_se(x.designator == y.designator); assert_se(sd_id128_equal(x.uuid, y.uuid)); assert_se(streq(x.name, y.name)); /* If the partition type does not have an architecture, nothing should change. */ assert_se(gpt_partition_type_from_string("esp", &x) >= 0); y = x; x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64); assert_se(x.arch == y.arch); assert_se(x.designator == y.designator); assert_se(sd_id128_equal(x.uuid, y.uuid)); assert_se(streq(x.name, y.name)); } DEFINE_TEST_MAIN(LOG_INFO);
4,527
39.428571
105
c
null
systemd-main/src/test/test-gunicode.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "gunicode.h" #include "tests.h" #include "utf8.h" TEST(unichar_iswide) { char32_t c; int r; /* FIXME: the cats are wide, but we get this wrong */ for (const char *narrow = "abX_…ąęµ!" "😼😿🙀😸😻"; *narrow; narrow += r) { r = utf8_encoded_to_unichar(narrow, &c); bool w = unichar_iswide(c); assert_se(r > 0); assert_se(!w); } for (const char *wide = "🐱/¥"; *wide; wide += r) { r = utf8_encoded_to_unichar(wide, &c); bool w = unichar_iswide(c); assert_se(r > 0); assert_se(w); } } DEFINE_TEST_MAIN(LOG_INFO);
750
25.821429
78
c
null
systemd-main/src/test/test-hash-funcs.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "tests.h" #include "hash-funcs.h" #include "set.h" TEST(path_hash_set) { /* The goal is to make sure that non-simplified path are hashed as expected, * and that we don't need to simplify them beforehand. */ /* No freeing of keys, we operate on static strings here… */ _cleanup_set_free_ Set *set = NULL; assert_se(set_isempty(set)); assert_se(set_ensure_put(&set, &path_hash_ops, "foo") == 1); assert_se(set_ensure_put(&set, &path_hash_ops, "foo") == 0); assert_se(set_ensure_put(&set, &path_hash_ops, "bar") == 1); assert_se(set_ensure_put(&set, &path_hash_ops, "bar") == 0); assert_se(set_ensure_put(&set, &path_hash_ops, "/foo") == 1); assert_se(set_ensure_put(&set, &path_hash_ops, "/bar") == 1); assert_se(set_ensure_put(&set, &path_hash_ops, "/foo/.") == 0); assert_se(set_ensure_put(&set, &path_hash_ops, "/./bar/./.") == 0); assert_se(set_contains(set, "foo")); assert_se(set_contains(set, "bar")); assert_se(set_contains(set, "./foo")); assert_se(set_contains(set, "./foo/.")); assert_se(set_contains(set, "./bar")); assert_se(set_contains(set, "./bar/.")); assert_se(set_contains(set, "/foo")); assert_se(set_contains(set, "/bar")); assert_se(set_contains(set, "//./foo")); assert_se(set_contains(set, "///./foo/.")); assert_se(set_contains(set, "////./bar")); assert_se(set_contains(set, "/////./bar/.")); assert_se(set_contains(set, "foo/")); assert_se(set_contains(set, "bar/")); assert_se(set_contains(set, "./foo/")); assert_se(set_contains(set, "./foo/./")); assert_se(set_contains(set, "./bar/")); assert_se(set_contains(set, "./bar/./")); assert_se(set_contains(set, "/foo/")); assert_se(set_contains(set, "/bar/")); assert_se(set_contains(set, "//./foo/")); assert_se(set_contains(set, "///./foo/./")); assert_se(set_contains(set, "////./bar/")); assert_se(set_contains(set, "/////./bar/./")); assert_se(!set_contains(set, "foo.")); assert_se(!set_contains(set, ".bar")); assert_se(!set_contains(set, "./foo.")); assert_se(!set_contains(set, "./.foo/.")); assert_se(!set_contains(set, "../bar")); assert_se(!set_contains(set, "./bar/..")); assert_se(!set_contains(set, "./foo..")); assert_se(!set_contains(set, "/..bar")); assert_se(!set_contains(set, "//../foo")); assert_se(!set_contains(set, "///../foo/.")); assert_se(!set_contains(set, "////../bar")); assert_se(!set_contains(set, "/////../bar/.")); assert_se(!set_contains(set, "foo./")); assert_se(!set_contains(set, ".bar/")); assert_se(!set_contains(set, "./foo./")); assert_se(!set_contains(set, "./.foo/./")); assert_se(!set_contains(set, "../bar/")); assert_se(!set_contains(set, "./bar/../")); assert_se(!set_contains(set, "./foo../")); assert_se(!set_contains(set, "/..bar/")); assert_se(!set_contains(set, "//../foo/")); assert_se(!set_contains(set, "///../foo/./")); assert_se(!set_contains(set, "////../bar/")); assert_se(!set_contains(set, "/////../bar/./")); } DEFINE_TEST_MAIN(LOG_INFO);
3,433
43.025641
84
c
null
systemd-main/src/test/test-hashmap.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "hashmap.h" #include "string-util.h" #include "tests.h" unsigned custom_counter = 0; static void custom_destruct(void* p) { custom_counter--; free(p); } DEFINE_HASH_OPS_FULL(boring_hash_ops, char, string_hash_func, string_compare_func, free, char, free); DEFINE_HASH_OPS_FULL(custom_hash_ops, char, string_hash_func, string_compare_func, custom_destruct, char, custom_destruct); TEST(ordered_hashmap_next) { _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL; int i; assert_se(m = ordered_hashmap_new(NULL)); for (i = -2; i <= 2; i++) assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1); for (i = -2; i <= 1; i++) assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11)); assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2))); assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1))); assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3))); } TEST(uint64_compare_func) { const uint64_t a = 0x100, b = 0x101; assert_se(uint64_compare_func(&a, &a) == 0); assert_se(uint64_compare_func(&a, &b) == -1); assert_se(uint64_compare_func(&b, &a) == 1); } TEST(trivial_compare_func) { assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0); assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1); assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1); } TEST(string_compare_func) { assert_se(string_compare_func("fred", "wilma") != 0); assert_se(string_compare_func("fred", "fred") == 0); } static void compare_cache(Hashmap *map, IteratedCache *cache) { const void **keys = NULL, **values = NULL; unsigned num, idx; void *k, *v; assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0); assert_se(num == 0 || keys); assert_se(num == 0 || values); idx = 0; HASHMAP_FOREACH_KEY(v, k, map) { assert_se(v == values[idx]); assert_se(k == keys[idx]); idx++; } assert_se(idx == num); } TEST(iterated_cache) { Hashmap *m; IteratedCache *c; assert_se(m = hashmap_new(NULL)); assert_se(c = hashmap_iterated_cache_new(m)); compare_cache(m, c); for (int stage = 0; stage < 100; stage++) { for (int i = 0; i < 100; i++) { int foo = stage * 1000 + i; assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1); } compare_cache(m, c); if (!(stage % 10)) { for (int i = 0; i < 100; i++) { int foo = stage * 1000 + i; assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777)); } compare_cache(m, c); } } hashmap_clear(m); compare_cache(m, c); assert_se(hashmap_free(m) == NULL); assert_se(iterated_cache_free(c) == NULL); } TEST(hashmap_put_strdup) { _cleanup_hashmap_free_ Hashmap *m = NULL; char *s; /* We don't have ordered_hashmap_put_strdup() yet. If it is added, * these tests should be moved to test-hashmap-plain.c. */ assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1); assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); assert_se(hashmap_put_strdup(&m, "foo", "BAR") == -EEXIST); assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); assert_se(hashmap_contains(m, "foo")); s = hashmap_get(m, "foo"); assert_se(streq(s, "bar")); assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 1); assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 0); assert_se(hashmap_put_strdup(&m, "xxx", "BAR") == -EEXIST); assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 0); assert_se(hashmap_contains(m, "xxx")); s = hashmap_get(m, "xxx"); assert_se(streq(s, "bar")); } TEST(hashmap_put_strdup_null) { _cleanup_hashmap_free_ Hashmap *m = NULL; char *s; assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1); assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); assert_se(hashmap_put_strdup(&m, "foo", NULL) == -EEXIST); assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); assert_se(hashmap_contains(m, "foo")); s = hashmap_get(m, "foo"); assert_se(streq(s, "bar")); assert_se(hashmap_put_strdup(&m, "xxx", NULL) == 1); assert_se(hashmap_put_strdup(&m, "xxx", "bar") == -EEXIST); assert_se(hashmap_put_strdup(&m, "xxx", NULL) == 0); assert_se(hashmap_contains(m, "xxx")); s = hashmap_get(m, "xxx"); assert_se(s == NULL); } /* This file tests in test-hashmap-plain.c, and tests in test-hashmap-ordered.c, which is generated * from test-hashmap-plain.c. Hashmap tests should be added to test-hashmap-plain.c, and here only if * they don't apply to ordered hashmaps. */ /* This variable allows us to assert that the tests from different compilation units were actually run. */ int n_extern_tests_run = 0; static int intro(void) { assert_se(n_extern_tests_run == 0); return EXIT_SUCCESS; } static int outro(void) { /* Ensure hashmap and ordered_hashmap were tested. */ assert_se(n_extern_tests_run == 2); return EXIT_SUCCESS; } DEFINE_TEST_MAIN_FULL(LOG_INFO, intro, outro);
5,717
32.244186
123
c
null
systemd-main/src/test/test-hexdecoct.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <errno.h> #include "alloc-util.h" #include "hexdecoct.h" #include "macro.h" #include "random-util.h" #include "string-util.h" #include "tests.h" TEST(hexchar) { assert_se(hexchar(0xa) == 'a'); assert_se(hexchar(0x0) == '0'); } TEST(unhexchar) { assert_se(unhexchar('a') == 0xA); assert_se(unhexchar('A') == 0xA); assert_se(unhexchar('0') == 0x0); } TEST(base32hexchar) { assert_se(base32hexchar(0) == '0'); assert_se(base32hexchar(9) == '9'); assert_se(base32hexchar(10) == 'A'); assert_se(base32hexchar(31) == 'V'); } TEST(unbase32hexchar) { assert_se(unbase32hexchar('0') == 0); assert_se(unbase32hexchar('9') == 9); assert_se(unbase32hexchar('A') == 10); assert_se(unbase32hexchar('V') == 31); assert_se(unbase32hexchar('=') == -EINVAL); } TEST(base64char) { assert_se(base64char(0) == 'A'); assert_se(base64char(26) == 'a'); assert_se(base64char(63) == '/'); } TEST(unbase64char) { assert_se(unbase64char('A') == 0); assert_se(unbase64char('Z') == 25); assert_se(unbase64char('a') == 26); assert_se(unbase64char('z') == 51); assert_se(unbase64char('0') == 52); assert_se(unbase64char('9') == 61); assert_se(unbase64char('+') == 62); assert_se(unbase64char('/') == 63); assert_se(unbase64char('=') == -EINVAL); } TEST(octchar) { assert_se(octchar(00) == '0'); assert_se(octchar(07) == '7'); } TEST(unoctchar) { assert_se(unoctchar('0') == 00); assert_se(unoctchar('7') == 07); } TEST(decchar) { assert_se(decchar(0) == '0'); assert_se(decchar(9) == '9'); } TEST(undecchar) { assert_se(undecchar('0') == 0); assert_se(undecchar('9') == 9); } static void test_hexmem_one(const char *in, const char *expected) { _cleanup_free_ char *result = NULL; _cleanup_free_ void *mem = NULL; size_t len; assert_se(result = hexmem(in, strlen_ptr(in))); log_debug("hexmem(\"%s\") → \"%s\" (expected: \"%s\")", strnull(in), result, expected); assert_se(streq(result, expected)); assert_se(unhexmem(result, SIZE_MAX, &mem, &len) >= 0); assert_se(memcmp_safe(mem, in, len) == 0); } TEST(hexmem) { test_hexmem_one(NULL, ""); test_hexmem_one("", ""); test_hexmem_one("foo", "666f6f"); } static void test_unhexmem_one(const char *s, size_t l, int retval) { _cleanup_free_ char *hex = NULL; _cleanup_free_ void *mem = NULL; size_t len; assert_se(unhexmem(s, l, &mem, &len) == retval); if (retval == 0) { char *answer; if (l == SIZE_MAX) l = strlen(s); assert_se(hex = hexmem(mem, len)); answer = strndupa_safe(strempty(s), l); assert_se(streq(delete_chars(answer, WHITESPACE), hex)); } } TEST(unhexmem) { const char *hex = "efa2149213"; const char *hex_space = " e f a\n 2\r 14\n\r\t9\t2 \n1\r3 \r\r\t"; const char *hex_invalid = "efa214921o"; test_unhexmem_one(NULL, 0, 0); test_unhexmem_one("", 0, 0); test_unhexmem_one("", SIZE_MAX, 0); test_unhexmem_one(" \n \t\r \t\t \n\n\n", SIZE_MAX, 0); test_unhexmem_one(hex_invalid, strlen(hex_invalid), -EINVAL); test_unhexmem_one(hex_invalid, (size_t) - 1, -EINVAL); test_unhexmem_one(hex, strlen(hex) - 1, -EPIPE); test_unhexmem_one(hex, strlen(hex), 0); test_unhexmem_one(hex, SIZE_MAX, 0); test_unhexmem_one(hex_space, strlen(hex_space), 0); test_unhexmem_one(hex_space, SIZE_MAX, 0); } /* https://tools.ietf.org/html/rfc4648#section-10 */ TEST(base32hexmem) { char *b32; b32 = base32hexmem("", STRLEN(""), true); assert_se(b32); assert_se(streq(b32, "")); free(b32); b32 = base32hexmem("f", STRLEN("f"), true); assert_se(b32); assert_se(streq(b32, "CO======")); free(b32); b32 = base32hexmem("fo", STRLEN("fo"), true); assert_se(b32); assert_se(streq(b32, "CPNG====")); free(b32); b32 = base32hexmem("foo", STRLEN("foo"), true); assert_se(b32); assert_se(streq(b32, "CPNMU===")); free(b32); b32 = base32hexmem("foob", STRLEN("foob"), true); assert_se(b32); assert_se(streq(b32, "CPNMUOG=")); free(b32); b32 = base32hexmem("fooba", STRLEN("fooba"), true); assert_se(b32); assert_se(streq(b32, "CPNMUOJ1")); free(b32); b32 = base32hexmem("foobar", STRLEN("foobar"), true); assert_se(b32); assert_se(streq(b32, "CPNMUOJ1E8======")); free(b32); b32 = base32hexmem("", STRLEN(""), false); assert_se(b32); assert_se(streq(b32, "")); free(b32); b32 = base32hexmem("f", STRLEN("f"), false); assert_se(b32); assert_se(streq(b32, "CO")); free(b32); b32 = base32hexmem("fo", STRLEN("fo"), false); assert_se(b32); assert_se(streq(b32, "CPNG")); free(b32); b32 = base32hexmem("foo", STRLEN("foo"), false); assert_se(b32); assert_se(streq(b32, "CPNMU")); free(b32); b32 = base32hexmem("foob", STRLEN("foob"), false); assert_se(b32); assert_se(streq(b32, "CPNMUOG")); free(b32); b32 = base32hexmem("fooba", STRLEN("fooba"), false); assert_se(b32); assert_se(streq(b32, "CPNMUOJ1")); free(b32); b32 = base32hexmem("foobar", STRLEN("foobar"), false); assert_se(b32); assert_se(streq(b32, "CPNMUOJ1E8")); free(b32); } static void test_unbase32hexmem_one(const char *hex, bool padding, int retval, const char *ans) { _cleanup_free_ void *mem = NULL; size_t len; assert_se(unbase32hexmem(hex, SIZE_MAX, padding, &mem, &len) == retval); if (retval == 0) { char *str; str = strndupa_safe(mem, len); assert_se(streq(str, ans)); } } TEST(unbase32hexmem) { test_unbase32hexmem_one("", true, 0, ""); test_unbase32hexmem_one("CO======", true, 0, "f"); test_unbase32hexmem_one("CPNG====", true, 0, "fo"); test_unbase32hexmem_one("CPNMU===", true, 0, "foo"); test_unbase32hexmem_one("CPNMUOG=", true, 0, "foob"); test_unbase32hexmem_one("CPNMUOJ1", true, 0, "fooba"); test_unbase32hexmem_one("CPNMUOJ1E8======", true, 0, "foobar"); test_unbase32hexmem_one("A", true, -EINVAL, NULL); test_unbase32hexmem_one("A=======", true, -EINVAL, NULL); test_unbase32hexmem_one("AAA=====", true, -EINVAL, NULL); test_unbase32hexmem_one("AAAAAA==", true, -EINVAL, NULL); test_unbase32hexmem_one("AB======", true, -EINVAL, NULL); test_unbase32hexmem_one("AAAB====", true, -EINVAL, NULL); test_unbase32hexmem_one("AAAAB===", true, -EINVAL, NULL); test_unbase32hexmem_one("AAAAAAB=", true, -EINVAL, NULL); test_unbase32hexmem_one("XPNMUOJ1", true, -EINVAL, NULL); test_unbase32hexmem_one("CXNMUOJ1", true, -EINVAL, NULL); test_unbase32hexmem_one("CPXMUOJ1", true, -EINVAL, NULL); test_unbase32hexmem_one("CPNXUOJ1", true, -EINVAL, NULL); test_unbase32hexmem_one("CPNMXOJ1", true, -EINVAL, NULL); test_unbase32hexmem_one("CPNMUXJ1", true, -EINVAL, NULL); test_unbase32hexmem_one("CPNMUOX1", true, -EINVAL, NULL); test_unbase32hexmem_one("CPNMUOJX", true, -EINVAL, NULL); test_unbase32hexmem_one("", false, 0, ""); test_unbase32hexmem_one("CO", false, 0, "f"); test_unbase32hexmem_one("CPNG", false, 0, "fo"); test_unbase32hexmem_one("CPNMU", false, 0, "foo"); test_unbase32hexmem_one("CPNMUOG", false, 0, "foob"); test_unbase32hexmem_one("CPNMUOJ1", false, 0, "fooba"); test_unbase32hexmem_one("CPNMUOJ1E8", false, 0, "foobar"); test_unbase32hexmem_one("CPNMUOG=", false, -EINVAL, NULL); test_unbase32hexmem_one("CPNMUOJ1E8======", false, -EINVAL, NULL); test_unbase32hexmem_one("A", false, -EINVAL, NULL); test_unbase32hexmem_one("AAA", false, -EINVAL, NULL); test_unbase32hexmem_one("AAAAAA", false, -EINVAL, NULL); test_unbase32hexmem_one("AB", false, -EINVAL, NULL); test_unbase32hexmem_one("AAAB", false, -EINVAL, NULL); test_unbase32hexmem_one("AAAAB", false, -EINVAL, NULL); test_unbase32hexmem_one("AAAAAAB", false, -EINVAL, NULL); } /* https://tools.ietf.org/html/rfc4648#section-10 */ TEST(base64mem) { char *b64; assert_se(base64mem("", STRLEN(""), &b64) == 0); assert_se(streq(b64, "")); free(b64); assert_se(base64mem("f", STRLEN("f"), &b64) == 4); assert_se(streq(b64, "Zg==")); free(b64); assert_se(base64mem("fo", STRLEN("fo"), &b64) == 4); assert_se(streq(b64, "Zm8=")); free(b64); assert_se(base64mem("foo", STRLEN("foo"), &b64) == 4); assert_se(streq(b64, "Zm9v")); free(b64); assert_se(base64mem("foob", STRLEN("foob"), &b64) == 8); assert_se(streq(b64, "Zm9vYg==")); free(b64); assert_se(base64mem("fooba", STRLEN("fooba"), &b64) == 8); assert_se(streq(b64, "Zm9vYmE=")); free(b64); assert_se(base64mem("foobar", STRLEN("foobar"), &b64) == 8); assert_se(streq(b64, "Zm9vYmFy")); free(b64); } TEST(base64mem_linebreak) { uint8_t data[4096]; for (size_t i = 0; i < 20; i++) { _cleanup_free_ char *encoded = NULL; _cleanup_free_ void *decoded = NULL; size_t decoded_size; uint64_t n, m; ssize_t l; /* Try a bunch of differently sized blobs */ n = random_u64_range(sizeof(data)); random_bytes(data, n); /* Break at various different columns */ m = 1 + random_u64_range(n + 5); l = base64mem_full(data, n, m, &encoded); assert_se(l >= 0); assert_se(encoded); assert_se((size_t) l == strlen(encoded)); assert_se(unbase64mem(encoded, SIZE_MAX, &decoded, &decoded_size) >= 0); assert_se(decoded_size == n); assert_se(memcmp(data, decoded, n) == 0); /* Also try in secure mode */ decoded = mfree(decoded); decoded_size = 0; assert_se(unbase64mem_full(encoded, SIZE_MAX, /* secure= */ true, &decoded, &decoded_size) >= 0); assert_se(decoded_size == n); assert_se(memcmp(data, decoded, n) == 0); for (size_t j = 0; j < (size_t) l; j++) assert_se((encoded[j] == '\n') == (j % (m + 1) == m)); } } static void test_base64_append_one(char **buf, size_t *len, const char *in, const char *expected) { ssize_t new_len; new_len = base64_append(buf, *len, in, strlen_ptr(in), 8, 12); assert_se(new_len >= 0); log_debug("base64_append_one(\"%s\")\nresult:\n%s\nexpected:\n%s", in, strnull(*buf), strnull(expected)); assert_se((size_t) new_len == strlen_ptr(*buf)); assert_se(streq_ptr(*buf, expected)); *len = new_len; } TEST(base64_append) { _cleanup_free_ char *buf = NULL; size_t len = 0; test_base64_append_one(&buf, &len, "", NULL); test_base64_append_one(&buf, &len, "f", "Zg=="); test_base64_append_one(&buf, &len, "fo", "Zg== Zm8="); test_base64_append_one(&buf, &len, "foo", "Zg== Zm8=\n" " Zm9v"); test_base64_append_one(&buf, &len, "foob", "Zg== Zm8=\n" " Zm9v\n" " Zm9v\n" " Yg=="); test_base64_append_one(&buf, &len, "fooba", "Zg== Zm8=\n" " Zm9v\n" " Zm9v\n" " Yg==\n" " Zm9v\n" " YmE="); test_base64_append_one(&buf, &len, "foobar", "Zg== Zm8=\n" " Zm9v\n" " Zm9v\n" " Yg==\n" " Zm9v\n" " YmE=\n" " Zm9v\n" " YmFy"); assert_se(free_and_strdup(&buf, "hogehogehogehoge") >= 0); len = strlen(buf); test_base64_append_one(&buf, &len, "", "hogehogehogehoge"); test_base64_append_one(&buf, &len, "f", "hogehogehogehoge\n" " Zg=="); test_base64_append_one(&buf, &len, "fo", "hogehogehogehoge\n" " Zg==\n" " Zm8="); test_base64_append_one(&buf, &len, "foo", "hogehogehogehoge\n" " Zg==\n" " Zm8=\n" " Zm9v"); test_base64_append_one(&buf, &len, "foob", "hogehogehogehoge\n" " Zg==\n" " Zm8=\n" " Zm9v\n" " Zm9v\n" " Yg=="); test_base64_append_one(&buf, &len, "fooba", "hogehogehogehoge\n" " Zg==\n" " Zm8=\n" " Zm9v\n" " Zm9v\n" " Yg==\n" " Zm9v\n" " YmE="); test_base64_append_one(&buf, &len, "foobar", "hogehogehogehoge\n" " Zg==\n" " Zm8=\n" " Zm9v\n" " Zm9v\n" " Yg==\n" " Zm9v\n" " YmE=\n" " Zm9v\n" " YmFy"); assert_se(free_and_strdup(&buf, "hogehogehogehoge") >= 0); len = strlen(buf); test_base64_append_one(&buf, &len, "foobarfoobarfoobarfoobar", "hogehogehogehoge\n" " Zm9v\n" " YmFy\n" " Zm9v\n" " YmFy\n" " Zm9v\n" " YmFy\n" " Zm9v\n" " YmFy"); assert_se(free_and_strdup(&buf, "aaa") >= 0); len = strlen(buf); test_base64_append_one(&buf, &len, "foobarfoobarfoobarfoobar", "aaa Zm9vYmFy\n" " Zm9vYmFy\n" " Zm9vYmFy\n" " Zm9vYmFy"); } static void test_unbase64mem_one(const char *input, const char *output, int ret) { _cleanup_free_ void *buffer = NULL; size_t size = 0; assert_se(unbase64mem(input, SIZE_MAX, &buffer, &size) == ret); if (ret >= 0) { assert_se(size == strlen(output)); assert_se(memcmp(buffer, output, size) == 0); assert_se(((char*) buffer)[size] == 0); } /* also try in secure mode */ buffer = mfree(buffer); size = 0; assert_se(unbase64mem_full(input, SIZE_MAX, /* secure=*/ true, &buffer, &size) == ret); if (ret >= 0) { assert_se(size == strlen(output)); assert_se(memcmp(buffer, output, size) == 0); assert_se(((char*) buffer)[size] == 0); } } TEST(unbase64mem) { test_unbase64mem_one("", "", 0); test_unbase64mem_one("Zg==", "f", 0); test_unbase64mem_one("Zm8=", "fo", 0); test_unbase64mem_one("Zm9v", "foo", 0); test_unbase64mem_one("Zm9vYg==", "foob", 0); test_unbase64mem_one("Zm9vYmE=", "fooba", 0); test_unbase64mem_one("Zm9vYmFy", "foobar", 0); test_unbase64mem_one(" ", "", 0); test_unbase64mem_one(" \n\r ", "", 0); test_unbase64mem_one(" Zg\n== ", "f", 0); test_unbase64mem_one(" Zm 8=\r", "fo", 0); test_unbase64mem_one(" Zm9\n\r\r\nv ", "foo", 0); test_unbase64mem_one(" Z m9vYg==\n\r", "foob", 0); test_unbase64mem_one(" Zm 9vYmE= ", "fooba", 0); test_unbase64mem_one(" Z m9v YmFy ", "foobar", 0); test_unbase64mem_one("A", NULL, -EPIPE); test_unbase64mem_one("A====", NULL, -EINVAL); test_unbase64mem_one("AAB==", NULL, -EINVAL); test_unbase64mem_one(" A A A B = ", NULL, -EINVAL); test_unbase64mem_one(" Z m 8 = q u u x ", NULL, -ENAMETOOLONG); } TEST(hexdump) { uint8_t data[146]; unsigned i; hexdump(stdout, NULL, 0); hexdump(stdout, "", 0); hexdump(stdout, "", 1); hexdump(stdout, "x", 1); hexdump(stdout, "x", 2); hexdump(stdout, "foobar", 7); hexdump(stdout, "f\nobar", 7); hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23); for (i = 0; i < ELEMENTSOF(data); i++) data[i] = i*2; hexdump(stdout, data, sizeof(data)); } DEFINE_TEST_MAIN(LOG_INFO);
18,654
34.875
113
c
null
systemd-main/src/test/test-hostname-setup.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <unistd.h> #include "alloc-util.h" #include "fileio.h" #include "fs-util.h" #include "hostname-setup.h" #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" TEST(read_etc_hostname) { _cleanup_(unlink_tempfilep) char path[] = "/tmp/hostname.XXXXXX"; char *hostname; int fd; fd = mkostemp_safe(path); assert_se(fd > 0); close(fd); /* simple hostname */ assert_se(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE) == 0); assert_se(read_etc_hostname(path, &hostname) == 0); assert_se(streq(hostname, "foo")); hostname = mfree(hostname); /* with comment */ assert_se(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE) == 0); assert_se(read_etc_hostname(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); hostname = mfree(hostname); /* with comment and extra whitespace */ assert_se(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE) == 0); assert_se(read_etc_hostname(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); hostname = mfree(hostname); /* cleans up name */ assert_se(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE) == 0); assert_se(read_etc_hostname(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foobar.com")); hostname = mfree(hostname); /* no value set */ hostname = (char*) 0x1234; assert_se(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE) == 0); assert_se(read_etc_hostname(path, &hostname) == -ENOENT); assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ /* nonexisting file */ assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT); assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ } TEST(hostname_setup) { hostname_setup(false); } DEFINE_TEST_MAIN(LOG_DEBUG);
2,203
32.907692
96
c
null
systemd-main/src/test/test-install-file.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "fileio.h" #include "install-file.h" #include "path-util.h" #include "rm-rf.h" #include "tests.h" #include "tmpfile-util.h" #include "umask-util.h" TEST(install_file) { _cleanup_(rm_rf_physical_and_freep) char *p = NULL; _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL; struct stat stat1, stat2; assert_se(mkdtemp_malloc(NULL, &p) >= 0); assert_se(a = path_join(p, "foo")); assert_se(b = path_join(p, "bar")); WITH_UMASK(0077) assert_se(write_string_file(a, "wups", WRITE_STRING_FILE_CREATE) >= 0); assert_se(lstat(a, &stat1) >= 0); assert_se(S_ISREG(stat1.st_mode)); assert_se(install_file(AT_FDCWD, a, AT_FDCWD, b, 0) >= 0); assert_se(install_file(AT_FDCWD, b, AT_FDCWD, a, INSTALL_FSYNC) >= 0); assert_se(write_string_file(b, "ttss", WRITE_STRING_FILE_CREATE) >= 0); assert_se(install_file(AT_FDCWD, a, AT_FDCWD, b, INSTALL_FSYNC_FULL) == -EEXIST); assert_se(install_file(AT_FDCWD, a, AT_FDCWD, b, INSTALL_FSYNC_FULL|INSTALL_REPLACE) >= 0); assert_se(stat(b, &stat2) >= 0); assert_se(stat1.st_dev == stat2.st_dev); assert_se(stat1.st_ino == stat2.st_ino); assert_se((stat2.st_mode & 0222) != 0); /* writable */ assert_se(install_file(AT_FDCWD, b, AT_FDCWD, a, INSTALL_FSYNC_FULL|INSTALL_REPLACE|INSTALL_READ_ONLY) >= 0); assert_se(stat(a, &stat2) >= 0); assert_se(stat1.st_dev == stat2.st_dev); assert_se(stat1.st_ino == stat2.st_ino); assert_se((stat2.st_mode & 0222) == 0); /* read-only */ assert_se(mkdir(b, 0755) >= 0); assert_se(c = path_join(b, "dir")); assert_se(mkdir(c, 0755) >= 0); free(c); assert_se(c = path_join(b, "reg")); assert_se(mknod(c, S_IFREG|0755, 0) >= 0); free(c); assert_se(c = path_join(b, "fifo")); assert_se(mknod(c, S_IFIFO|0755, 0) >= 0); assert_se(install_file(AT_FDCWD, b, AT_FDCWD, a, INSTALL_FSYNC_FULL) == -EEXIST); assert_se(install_file(AT_FDCWD, b, AT_FDCWD, a, INSTALL_FSYNC_FULL|INSTALL_REPLACE) == 0); assert_se(write_string_file(b, "ttss", WRITE_STRING_FILE_CREATE) >= 0); assert_se(install_file(AT_FDCWD, b, AT_FDCWD, a, INSTALL_FSYNC_FULL) == -EEXIST); assert_se(install_file(AT_FDCWD, b, AT_FDCWD, a, INSTALL_FSYNC_FULL|INSTALL_REPLACE) == 0); } DEFINE_TEST_MAIN(LOG_INFO);
2,511
37.646154
117
c
null
systemd-main/src/test/test-install.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <stdio.h> #include <string.h> #include "install.h" #include "tests.h" static void dump_changes(InstallChange *c, unsigned n) { unsigned i; assert_se(n == 0 || c); for (i = 0; i < n; i++) { if (c[i].type == INSTALL_CHANGE_UNLINK) printf("rm '%s'\n", c[i].path); else if (c[i].type == INSTALL_CHANGE_SYMLINK) printf("ln -s '%s' '%s'\n", c[i].source, c[i].path); } } int main(int argc, char* argv[]) { _cleanup_hashmap_free_ Hashmap *h = NULL; UnitFileList *p; int r; const char *const files[] = { "avahi-daemon.service", NULL }; const char *const files2[] = { "/home/lennart/test.service", NULL }; InstallChange *changes = NULL; size_t n_changes = 0; UnitFileState state = 0; test_setup_logging(LOG_DEBUG); h = hashmap_new(&unit_file_list_hash_ops_free); r = unit_file_get_list(RUNTIME_SCOPE_SYSTEM, NULL, h, NULL, NULL); assert_se(r == 0); HASHMAP_FOREACH(p, h) { UnitFileState s = _UNIT_FILE_STATE_INVALID; r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(p->path), &s); assert_se((r < 0 && p->state == UNIT_FILE_BAD) || (p->state == s)); fprintf(stderr, "%s (%s)\n", p->path, unit_file_state_to_string(p->state)); } log_info("/*** enable **/"); r = unit_file_enable(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); log_info("/*** enable2 **/"); r = unit_file_enable(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, files[0], &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_ENABLED); log_info("/*** disable ***/"); changes = NULL; n_changes = 0; r = unit_file_disable(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, files[0], &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_DISABLED); log_info("/*** mask ***/"); changes = NULL; n_changes = 0; r = unit_file_mask(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); log_info("/*** mask2 ***/"); r = unit_file_mask(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, files[0], &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_MASKED); log_info("/*** unmask ***/"); changes = NULL; n_changes = 0; r = unit_file_unmask(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); log_info("/*** unmask2 ***/"); r = unit_file_unmask(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, files[0], &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_DISABLED); log_info("/*** mask ***/"); changes = NULL; n_changes = 0; r = unit_file_mask(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, files[0], &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_MASKED); log_info("/*** disable ***/"); changes = NULL; n_changes = 0; r = unit_file_disable(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); log_info("/*** disable2 ***/"); r = unit_file_disable(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, files[0], &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_MASKED); log_info("/*** umask ***/"); changes = NULL; n_changes = 0; r = unit_file_unmask(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, files[0], &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_DISABLED); log_info("/*** enable files2 ***/"); changes = NULL; n_changes = 0; r = unit_file_enable(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files2[0]), &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_ENABLED); log_info("/*** disable files2 ***/"); changes = NULL; n_changes = 0; r = unit_file_disable(RUNTIME_SCOPE_SYSTEM, 0, NULL, STRV_MAKE(basename(files2[0])), &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files2[0]), &state); assert_se(r < 0); log_info("/*** link files2 ***/"); changes = NULL; n_changes = 0; r = unit_file_link(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files2[0]), &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_LINKED); log_info("/*** disable files2 ***/"); changes = NULL; n_changes = 0; r = unit_file_disable(RUNTIME_SCOPE_SYSTEM, 0, NULL, STRV_MAKE(basename(files2[0])), &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files2[0]), &state); assert_se(r < 0); log_info("/*** link files2 ***/"); changes = NULL; n_changes = 0; r = unit_file_link(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files2[0]), &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_LINKED); log_info("/*** reenable files2 ***/"); changes = NULL; n_changes = 0; r = unit_file_reenable(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files2[0]), &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_ENABLED); log_info("/*** disable files2 ***/"); changes = NULL; n_changes = 0; r = unit_file_disable(RUNTIME_SCOPE_SYSTEM, 0, NULL, STRV_MAKE(basename(files2[0])), &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files2[0]), &state); assert_se(r < 0); log_info("/*** preset files ***/"); changes = NULL; n_changes = 0; r = unit_file_preset(RUNTIME_SCOPE_SYSTEM, 0, NULL, (char**) files, UNIT_FILE_PRESET_FULL, &changes, &n_changes); assert_se(r >= 0); dump_changes(changes, n_changes); install_changes_free(changes, n_changes); r = unit_file_get_state(RUNTIME_SCOPE_SYSTEM, NULL, basename(files[0]), &state); assert_se(r >= 0); assert_se(state == UNIT_FILE_ENABLED); return 0; }
9,160
32.804428
121
c
null
systemd-main/src/test/test-io-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <fcntl.h> #include <stdlib.h> #include <unistd.h> #include "alloc-util.h" #include "fd-util.h" #include "io-util.h" #include "macro.h" #include "tests.h" static void test_sparse_write_one(int fd, const char *buffer, size_t n) { char check[n]; assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(ftruncate(fd, 0) >= 0); assert_se(sparse_write(fd, buffer, n, 4) == (ssize_t) n); assert_se(lseek(fd, 0, SEEK_CUR) == (off_t) n); assert_se(ftruncate(fd, n) >= 0); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(read(fd, check, n) == (ssize_t) n); assert_se(memcmp(buffer, check, n) == 0); } TEST(sparse_write) { const char test_a[] = "test"; const char test_b[] = "\0\0\0\0test\0\0\0\0"; const char test_c[] = "\0\0test\0\0\0\0"; const char test_d[] = "\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0\0\0\0"; const char test_e[] = "test\0\0\0\0test"; _cleanup_close_ int fd = -EBADF; char fn[] = "/tmp/sparseXXXXXX"; fd = mkostemp(fn, O_CLOEXEC); assert_se(fd >= 0); (void) unlink(fn); test_sparse_write_one(fd, test_a, sizeof(test_a)); test_sparse_write_one(fd, test_b, sizeof(test_b)); test_sparse_write_one(fd, test_c, sizeof(test_c)); test_sparse_write_one(fd, test_d, sizeof(test_d)); test_sparse_write_one(fd, test_e, sizeof(test_e)); } DEFINE_TEST_MAIN(LOG_INFO);
1,557
30.16
115
c
null
systemd-main/src/test/test-ip-protocol-list.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <netinet/in.h> #include "macro.h" #include "ip-protocol-list.h" #include "stdio-util.h" #include "string-util.h" #include "tests.h" static void test_int(int i) { char str[DECIMAL_STR_MAX(int)]; assert_se(ip_protocol_from_name(ip_protocol_to_name(i)) == i); xsprintf(str, "%i", i); assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i); } static void test_int_fail(int i) { char str[DECIMAL_STR_MAX(int)]; assert_se(!ip_protocol_to_name(i)); xsprintf(str, "%i", i); assert_se(parse_ip_protocol(str) == -EINVAL); } static void test_str(const char *s) { assert_se(streq(ip_protocol_to_name(ip_protocol_from_name(s)), s)); assert_se(streq(ip_protocol_to_name(parse_ip_protocol(s)), s)); } static void test_str_fail(const char *s) { assert_se(ip_protocol_from_name(s) == -EINVAL); assert_se(parse_ip_protocol(s) == -EINVAL); } static void test_parse_ip_protocol_one(const char *s, int expected) { assert_se(parse_ip_protocol(s) == expected); } TEST(integer) { test_int(IPPROTO_TCP); test_int(IPPROTO_DCCP); test_int_fail(-1); test_int_fail(1024 * 1024); } TEST(string) { test_str("sctp"); test_str("udp"); test_str_fail("hoge"); test_str_fail("-1"); test_str_fail("1000000000"); } TEST(parse_ip_protocol) { test_parse_ip_protocol_one("sctp", IPPROTO_SCTP); test_parse_ip_protocol_one("ScTp", IPPROTO_SCTP); test_parse_ip_protocol_one("ip", IPPROTO_IP); test_parse_ip_protocol_one("", IPPROTO_IP); test_parse_ip_protocol_one("1", 1); test_parse_ip_protocol_one("0", 0); test_parse_ip_protocol_one("-10", -EINVAL); test_parse_ip_protocol_one("100000000", -EINVAL); } DEFINE_TEST_MAIN(LOG_INFO);
1,930
26.585714
91
c
null
systemd-main/src/test/test-ipcrm.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "clean-ipc.h" #include "errno-util.h" #include "main-func.h" #include "tests.h" #include "user-util.h" static int run(int argc, char *argv[]) { uid_t uid; int r; const char* name = argv[1] ?: NOBODY_USER_NAME; test_setup_logging(LOG_INFO); r = get_user_creds(&name, &uid, NULL, NULL, NULL, 0); if (r == -ESRCH) return log_tests_skipped("Failed to resolve user"); if (r < 0) return log_error_errno(r, "Failed to resolve \"%s\": %m", name); r = clean_ipc_by_uid(uid); if (ERRNO_IS_PRIVILEGE(r)) return log_tests_skipped("No privileges"); return r; } DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
790
25.366667
80
c
null
systemd-main/src/test/test-journal-importer.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include "alloc-util.h" #include "log.h" #include "journal-importer.h" #include "path-util.h" #include "string-util.h" #include "tests.h" static void assert_iovec_entry(const struct iovec *iovec, const char* content) { assert_se(strlen(content) == iovec->iov_len); assert_se(memcmp(content, iovec->iov_base, iovec->iov_len) == 0); } #define COREDUMP_PROC_GROUP \ "COREDUMP_PROC_CGROUP=1:name=systemd:/\n" \ "0::/user.slice/user-1002.slice/[email protected]/gnome-terminal-server.service\n" TEST(basic_parsing) { _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1); _cleanup_free_ char *journal_data_path = NULL; int r; assert_se(get_testdata_dir("journal-data/journal-1.txt", &journal_data_path) >= 0); imp.fd = open(journal_data_path, O_RDONLY|O_CLOEXEC); assert_se(imp.fd >= 0); do r = journal_importer_process_data(&imp); while (r == 0 && !journal_importer_eof(&imp)); assert_se(r == 1); /* We read one entry, so we should get EOF on next read, but not yet */ assert_se(!journal_importer_eof(&imp)); assert_se(imp.iovw.count == 6); assert_iovec_entry(&imp.iovw.iovec[0], "_BOOT_ID=1531fd22ec84429e85ae888b12fadb91"); assert_iovec_entry(&imp.iovw.iovec[1], "_TRANSPORT=journal"); assert_iovec_entry(&imp.iovw.iovec[2], COREDUMP_PROC_GROUP); assert_iovec_entry(&imp.iovw.iovec[3], "COREDUMP_RLIMIT=-1"); assert_iovec_entry(&imp.iovw.iovec[4], COREDUMP_PROC_GROUP); assert_iovec_entry(&imp.iovw.iovec[5], "_SOURCE_REALTIME_TIMESTAMP=1478389147837945"); /* Let's check if we get EOF now */ r = journal_importer_process_data(&imp); assert_se(r == 0); assert_se(journal_importer_eof(&imp)); } TEST(bad_input) { _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1); _cleanup_free_ char *journal_data_path = NULL; int r; assert_se(get_testdata_dir("journal-data/journal-1.txt", &journal_data_path) >= 0); imp.fd = open(journal_data_path, O_RDONLY|O_CLOEXEC); assert_se(imp.fd >= 0); do r = journal_importer_process_data(&imp); while (!journal_importer_eof(&imp)); assert_se(r == 0); /* If we don't have enough input, 0 is returned */ assert_se(journal_importer_eof(&imp)); } DEFINE_TEST_MAIN(LOG_DEBUG);
2,676
36.180556
94
c
null
systemd-main/src/test/test-kbd-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "kbd-util.h" #include "log.h" #include "strv.h" #include "tests.h" int main(int argc, char *argv[]) { _cleanup_strv_free_ char **maps = NULL; int r; log_show_color(true); test_setup_logging(LOG_DEBUG); r = get_keymaps(&maps); if (r < 0) { log_error_errno(r, "Failed to acquire keymaps: %m"); return 0; } STRV_FOREACH(m, maps) { log_info("Found keymap: %s", *m); assert_se(keymap_exists(*m) > 0); } return 0; }
618
21.107143
68
c
null
systemd-main/src/test/test-libmount.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "alloc-util.h" #include "fd-util.h" #include "escape.h" #include "libmount-util.h" #include "tests.h" static void test_libmount_unescaping_one( const char *title, const char *string, bool may_fail, const char *expected_source, const char *expected_target) { /* A test for libmount really */ int r; log_info("/* %s %s */", __func__, title); _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; _cleanup_fclose_ FILE *f = NULL; f = fmemopen((char*) string, strlen(string), "r"); assert_se(f); assert_se(libmount_parse(title, f, &table, &iter) >= 0); struct libmnt_fs *fs; const char *source, *target; _cleanup_free_ char *x = NULL, *cs = NULL, *s = NULL, *ct = NULL, *t = NULL; /* We allow this call and the checks below to fail in some cases. See the case definitions below. */ r = mnt_table_next_fs(table, iter, &fs); if (r != 0 && may_fail) { log_error_errno(r, "mnt_table_next_fs failed: %m"); return; } assert_se(r == 0); assert_se(x = cescape(string)); assert_se(source = mnt_fs_get_source(fs)); assert_se(target = mnt_fs_get_target(fs)); assert_se(cs = cescape(source)); assert_se(ct = cescape(target)); assert_se(cunescape(source, UNESCAPE_RELAX, &s) >= 0); assert_se(cunescape(target, UNESCAPE_RELAX, &t) >= 0); log_info("from '%s'", x); log_info("source: '%s'", source); log_info("source: '%s'", cs); log_info("source: '%s'", s); log_info("expected: '%s'", strna(expected_source)); log_info("target: '%s'", target); log_info("target: '%s'", ct); log_info("target: '%s'", t); log_info("expected: '%s'", strna(expected_target)); assert_se(may_fail || streq(source, expected_source)); assert_se(may_fail || streq(target, expected_target)); assert_se(mnt_table_next_fs(table, iter, &fs) == 1); } TEST(libmount_unescaping) { test_libmount_unescaping_one( "escaped space + utf8", "729 38 0:59 / /tmp/„zupa\\040zębowa” rw,relatime shared:395 - tmpfs die\\040Brühe rw,seclabel", false, "die Brühe", "/tmp/„zupa zębowa”" ); test_libmount_unescaping_one( "escaped newline", "729 38 0:59 / /tmp/x\\012y rw,relatime shared:395 - tmpfs newline rw,seclabel", false, "newline", "/tmp/x\ny" ); /* The result of "mount -t tmpfs '' /tmp/emptysource". * This will fail with libmount <= v2.33. * See https://github.com/karelzak/util-linux/commit/18a52a5094. */ test_libmount_unescaping_one( "empty source", "760 38 0:60 / /tmp/emptysource rw,relatime shared:410 - tmpfs rw,seclabel", true, "", "/tmp/emptysource" ); /* The kernel leaves \r as is. * Also see https://github.com/karelzak/util-linux/issues/780. */ test_libmount_unescaping_one( "foo\\rbar", "790 38 0:61 / /tmp/foo\rbar rw,relatime shared:425 - tmpfs tmpfs rw,seclabel", true, "tmpfs", "/tmp/foo\rbar" ); } DEFINE_TEST_MAIN(LOG_DEBUG);
3,841
33.612613
120
c
null
systemd-main/src/test/test-limits-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "format-util.h" #include "limits-util.h" #include "tests.h" TEST(physical_memory) { uint64_t p; p = physical_memory(); assert_se(p > 0); assert_se(p < UINT64_MAX); assert_se(p % page_size() == 0); log_info("Memory: %s (%" PRIu64 ")", FORMAT_BYTES(p), p); } TEST(physical_memory_scale) { uint64_t p; p = physical_memory(); assert_se(physical_memory_scale(0, 100) == 0); assert_se(physical_memory_scale(100, 100) == p); log_info("Memory original: %" PRIu64, physical_memory()); log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100)); log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2); log_info("Page size: %zu", page_size()); /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */ assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size()); assert_se(physical_memory_scale(200, 100) == p*2); assert_se(physical_memory_scale(0, 1) == 0); assert_se(physical_memory_scale(1, 1) == p); assert_se(physical_memory_scale(2, 1) == p*2); assert_se(physical_memory_scale(0, 2) == 0); assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size()); assert_se(physical_memory_scale(2, 2) == p); assert_se(physical_memory_scale(4, 2) == p*2); assert_se(physical_memory_scale(0, UINT32_MAX) == 0); assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p); /* overflow */ assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); } TEST(system_tasks_max) { uint64_t t; t = system_tasks_max(); assert_se(t > 0); assert_se(t < UINT64_MAX); log_info("Max tasks: %" PRIu64, t); } TEST(system_tasks_max_scale) { uint64_t t; t = system_tasks_max(); assert_se(system_tasks_max_scale(0, 100) == 0); assert_se(system_tasks_max_scale(100, 100) == t); assert_se(system_tasks_max_scale(0, 1) == 0); assert_se(system_tasks_max_scale(1, 1) == t); assert_se(system_tasks_max_scale(2, 1) == 2*t); assert_se(system_tasks_max_scale(0, 2) == 0); assert_se(system_tasks_max_scale(1, 2) == t/2); assert_se(system_tasks_max_scale(2, 2) == t); assert_se(system_tasks_max_scale(3, 2) == (3*t)/2); assert_se(system_tasks_max_scale(4, 2) == t*2); assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0); assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2); assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t); /* overflow */ assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); } DEFINE_TEST_MAIN(LOG_INFO);
2,941
31.688889
112
c
null
systemd-main/src/test/test-list.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "list.h" int main(int argc, const char *argv[]) { size_t i; typedef struct list_item { LIST_FIELDS(struct list_item, item_list); } list_item; LIST_HEAD(list_item, head); LIST_HEAD(list_item, head2); list_item items[4]; LIST_HEAD_INIT(head); LIST_HEAD_INIT(head2); assert_se(head == NULL); assert_se(head2 == NULL); for (i = 0; i < ELEMENTSOF(items); i++) { LIST_INIT(item_list, &items[i]); assert_se(LIST_JUST_US(item_list, &items[i])); assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]); } i = 0; LIST_FOREACH_OTHERS(item_list, cursor, &items[2]) { i++; assert_se(cursor != &items[2]); } assert_se(i == ELEMENTSOF(items)-1); i = 0; LIST_FOREACH_OTHERS(item_list, cursor, &items[0]) { i++; assert_se(cursor != &items[0]); } assert_se(i == ELEMENTSOF(items)-1); i = 0; LIST_FOREACH_OTHERS(item_list, cursor, &items[3]) { i++; assert_se(cursor != &items[3]); } assert_se(i == ELEMENTSOF(items)-1); assert_se(!LIST_JUST_US(item_list, head)); assert_se(items[0].item_list_next == NULL); assert_se(items[1].item_list_next == &items[0]); assert_se(items[2].item_list_next == &items[1]); assert_se(items[3].item_list_next == &items[2]); assert_se(items[0].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[2]); assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); list_item *cursor = LIST_FIND_HEAD(item_list, &items[0]); assert_se(cursor == &items[3]); cursor = LIST_FIND_TAIL(item_list, &items[3]); assert_se(cursor == &items[0]); assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); assert_se(LIST_JUST_US(item_list, &items[1])); assert_se(items[0].item_list_next == NULL); assert_se(items[2].item_list_next == &items[0]); assert_se(items[3].item_list_next == &items[2]); assert_se(items[0].item_list_prev == &items[2]); assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]) == &items[1]); assert_se(items[0].item_list_next == NULL); assert_se(items[2].item_list_next == &items[0]); assert_se(items[1].item_list_next == &items[2]); assert_se(items[3].item_list_next == &items[1]); assert_se(items[0].item_list_prev == &items[2]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); assert_se(LIST_JUST_US(item_list, &items[1])); assert_se(items[0].item_list_next == NULL); assert_se(items[2].item_list_next == &items[0]); assert_se(items[3].item_list_next == &items[2]); assert_se(items[0].item_list_prev == &items[2]); assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]) == &items[1]); assert_se(items[0].item_list_next == NULL); assert_se(items[2].item_list_next == &items[0]); assert_se(items[1].item_list_next == &items[2]); assert_se(items[3].item_list_next == &items[1]); assert_se(items[0].item_list_prev == &items[2]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); assert_se(LIST_JUST_US(item_list, &items[0])); assert_se(items[2].item_list_next == NULL); assert_se(items[1].item_list_next == &items[2]); assert_se(items[3].item_list_next == &items[1]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]) == &items[0]); assert_se(items[2].item_list_next == NULL); assert_se(items[1].item_list_next == &items[2]); assert_se(items[3].item_list_next == &items[1]); assert_se(items[0].item_list_next == &items[3]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == &items[0]); assert_se(items[0].item_list_prev == NULL); assert_se(head == &items[0]); assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); assert_se(LIST_JUST_US(item_list, &items[0])); assert_se(items[2].item_list_next == NULL); assert_se(items[1].item_list_next == &items[2]); assert_se(items[3].item_list_next == &items[1]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]) == &items[0]); assert_se(items[0].item_list_next == NULL); assert_se(items[2].item_list_next == &items[0]); assert_se(items[1].item_list_next == &items[2]); assert_se(items[3].item_list_next == &items[1]); assert_se(items[0].item_list_prev == &items[2]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); assert_se(LIST_JUST_US(item_list, &items[0])); assert_se(items[2].item_list_next == NULL); assert_se(items[1].item_list_next == &items[2]); assert_se(items[3].item_list_next == &items[1]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); assert_se(LIST_JUST_US(item_list, &items[1])); assert_se(items[2].item_list_next == NULL); assert_se(items[3].item_list_next == &items[2]); assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_REMOVE(item_list, head, &items[2]) == &items[2]); assert_se(LIST_JUST_US(item_list, &items[2])); assert_se(LIST_JUST_US(item_list, head)); assert_se(LIST_REMOVE(item_list, head, &items[3]) == &items[3]); assert_se(LIST_JUST_US(item_list, &items[3])); assert_se(head == NULL); for (i = 0; i < ELEMENTSOF(items); i++) { assert_se(LIST_JUST_US(item_list, &items[i])); assert_se(LIST_APPEND(item_list, head, &items[i]) == &items[i]); } assert_se(!LIST_JUST_US(item_list, head)); assert_se(items[0].item_list_next == &items[1]); assert_se(items[1].item_list_next == &items[2]); assert_se(items[2].item_list_next == &items[3]); assert_se(items[3].item_list_next == NULL); assert_se(items[0].item_list_prev == NULL); assert_se(items[1].item_list_prev == &items[0]); assert_se(items[2].item_list_prev == &items[1]); assert_se(items[3].item_list_prev == &items[2]); for (i = 0; i < ELEMENTSOF(items); i++) assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]); assert_se(head == NULL); for (i = 0; i < ELEMENTSOF(items) / 2; i++) { LIST_INIT(item_list, &items[i]); assert_se(LIST_JUST_US(item_list, &items[i])); assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]); } for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) { LIST_INIT(item_list, &items[i]); assert_se(LIST_JUST_US(item_list, &items[i])); assert_se(LIST_PREPEND(item_list, head2, &items[i]) == &items[i]); } assert_se(items[0].item_list_next == NULL); assert_se(items[1].item_list_next == &items[0]); assert_se(items[2].item_list_next == NULL); assert_se(items[3].item_list_next == &items[2]); assert_se(items[0].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == NULL); assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_JOIN(item_list, head2, head) == head2); assert_se(head == NULL); assert_se(items[0].item_list_next == NULL); assert_se(items[1].item_list_next == &items[0]); assert_se(items[2].item_list_next == &items[1]); assert_se(items[3].item_list_next == &items[2]); assert_se(items[0].item_list_prev == &items[1]); assert_se(items[1].item_list_prev == &items[2]); assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); assert_se(LIST_JOIN(item_list, head, head2) == head); assert_se(head2 == NULL); assert_se(head); for (i = 0; i < ELEMENTSOF(items); i++) assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]); assert_se(head == NULL); assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0); assert_se(LIST_PREPEND(item_list, head, items + 1) == items + 1); assert_se(LIST_PREPEND(item_list, head, items + 2) == items + 2); assert_se(LIST_POP(item_list, head) == items + 2); assert_se(LIST_POP(item_list, head) == items + 1); assert_se(LIST_POP(item_list, head) == items + 0); assert_se(LIST_POP(item_list, head) == NULL); return 0; }
10,373
39.054054
90
c
null
systemd-main/src/test/test-local-addresses.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include <stdio.h> #include "af-list.h" #include "alloc-util.h" #include "in-addr-util.h" #include "local-addresses.h" #include "tests.h" static void print_local_addresses(struct local_address *a, unsigned n) { for (unsigned i = 0; i < n; i++) { _cleanup_free_ char *b = NULL; assert_se(in_addr_to_string(a[i].family, &a[i].address, &b) >= 0); log_debug("%s if%i scope=%i metric=%u address=%s", af_to_name(a[i].family), a[i].ifindex, a[i].scope, a[i].metric, b); } } TEST(local_addresses) { struct local_address *a = NULL; int n; n = local_addresses(NULL, 0, AF_INET, &a); assert_se(n >= 0); log_debug("/* Local Addresses(ifindex:0, AF_INET) */"); print_local_addresses(a, (unsigned) n); a = mfree(a); n = local_addresses(NULL, 0, AF_INET6, &a); assert_se(n >= 0); log_debug("/* Local Addresses(ifindex:0, AF_INET6) */"); print_local_addresses(a, (unsigned) n); a = mfree(a); n = local_addresses(NULL, 0, AF_UNSPEC, &a); assert_se(n >= 0); log_debug("/* Local Addresses(ifindex:0, AF_UNSPEC) */"); print_local_addresses(a, (unsigned) n); a = mfree(a); n = local_addresses(NULL, 1, AF_INET, &a); assert_se(n >= 0); log_debug("/* Local Addresses(ifindex:1, AF_INET) */"); print_local_addresses(a, (unsigned) n); a = mfree(a); n = local_addresses(NULL, 1, AF_INET6, &a); assert_se(n >= 0); log_debug("/* Local Addresses(ifindex:1, AF_INET6) */"); print_local_addresses(a, (unsigned) n); a = mfree(a); n = local_addresses(NULL, 1, AF_UNSPEC, &a); assert_se(n >= 0); log_debug("/* Local Addresses(ifindex:1, AF_UNSPEC) */"); print_local_addresses(a, (unsigned) n); a = mfree(a); n = local_gateways(NULL, 0, AF_UNSPEC, &a); assert_se(n >= 0); log_debug("/* Local Gateways */"); print_local_addresses(a, (unsigned) n); a = mfree(a); n = local_outbounds(NULL, 0, AF_UNSPEC, &a); assert_se(n >= 0); log_debug("/* Local Outbounds */"); print_local_addresses(a, (unsigned) n); free(a); } DEFINE_TEST_MAIN(LOG_DEBUG);
2,369
31.027027
134
c
null
systemd-main/src/test/test-locale-util.c
/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "glyph-util.h" #include "kbd-util.h" #include "locale-util.h" #include "macro.h" #include "strv.h" #include "tests.h" TEST(get_locales) { _cleanup_strv_free_ char **locales = NULL; int r; r = get_locales(&locales); assert_se(r >= 0); assert_se(locales); STRV_FOREACH(p, locales) { puts(*p); assert_se(locale_is_valid(*p)); } } TEST(locale_is_valid) { assert_se(locale_is_valid("en_EN.utf8")); assert_se(locale_is_valid("fr_FR.utf8")); assert_se(locale_is_valid("fr_FR@euro")); assert_se(locale_is_valid("fi_FI")); assert_se(locale_is_valid("POSIX")); assert_se(locale_is_valid("C")); assert_se(!locale_is_valid("")); assert_se(!locale_is_valid("/usr/bin/foo")); assert_se(!locale_is_valid("\x01gar\x02 bage\x03")); } TEST(locale_is_installed) { /* Always available */ assert_se(locale_is_installed("POSIX") > 0); assert_se(locale_is_installed("C") > 0); /* Might, or might not be installed. */ assert_se(locale_is_installed("en_EN.utf8") >= 0); assert_se(locale_is_installed("fr_FR.utf8") >= 0); assert_se(locale_is_installed("fr_FR@euro") >= 0); assert_se(locale_is_installed("fi_FI") >= 0); /* Definitely not valid */ assert_se(locale_is_installed("") == 0); assert_se(locale_is_installed("/usr/bin/foo") == 0); assert_se(locale_is_installed("\x01gar\x02 bage\x03") == 0); /* Definitely not installed */ assert_se(locale_is_installed("zz_ZZ") == 0); } TEST(keymaps) { _cleanup_strv_free_ char **kmaps = NULL; int r; assert_se(!keymap_is_valid("")); assert_se(!keymap_is_valid("/usr/bin/foo")); assert_se(!keymap_is_valid("\x01gar\x02 bage\x03")); r = get_keymaps(&kmaps); if (r == -ENOENT) return; /* skip test if no keymaps are installed */ assert_se(r >= 0); assert_se(kmaps); STRV_FOREACH(p, kmaps) { puts(*p); assert_se(keymap_is_valid(*p)); } assert_se(keymap_is_valid("uk")); assert_se(keymap_is_valid("de-nodeadkeys")); assert_se(keymap_is_valid("ANSI-dvorak")); assert_se(keymap_is_valid("unicode")); } #define dump_glyph(x) log_info(STRINGIFY(x) ": %s", special_glyph(x)) TEST(dump_special_glyphs) { assert_cc(SPECIAL_GLYPH_WARNING_SIGN + 1 == _SPECIAL_GLYPH_MAX); log_info("is_locale_utf8: %s", yes_no(is_locale_utf8())); dump_glyph(SPECIAL_GLYPH_TREE_VERTICAL); dump_glyph(SPECIAL_GLYPH_TREE_BRANCH); dump_glyph(SPECIAL_GLYPH_TREE_RIGHT); dump_glyph(SPECIAL_GLYPH_TREE_SPACE); dump_glyph(SPECIAL_GLYPH_TREE_TOP); dump_glyph(SPECIAL_GLYPH_VERTICAL_DOTTED); dump_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET); dump_glyph(SPECIAL_GLYPH_BLACK_CIRCLE); dump_glyph(SPECIAL_GLYPH_WHITE_CIRCLE); dump_glyph(SPECIAL_GLYPH_MULTIPLICATION_SIGN); dump_glyph(SPECIAL_GLYPH_CIRCLE_ARROW); dump_glyph(SPECIAL_GLYPH_BULLET); dump_glyph(SPECIAL_GLYPH_ARROW_LEFT); dump_glyph(SPECIAL_GLYPH_ARROW_RIGHT); dump_glyph(SPECIAL_GLYPH_ARROW_UP); dump_glyph(SPECIAL_GLYPH_ARROW_DOWN); dump_glyph(SPECIAL_GLYPH_ELLIPSIS); dump_glyph(SPECIAL_GLYPH_MU); dump_glyph(SPECIAL_GLYPH_CHECK_MARK); dump_glyph(SPECIAL_GLYPH_CROSS_MARK); dump_glyph(SPECIAL_GLYPH_EXTERNAL_LINK); dump_glyph(SPECIAL_GLYPH_ECSTATIC_SMILEY); dump_glyph(SPECIAL_GLYPH_HAPPY_SMILEY); dump_glyph(SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY); dump_glyph(SPECIAL_GLYPH_NEUTRAL_SMILEY); dump_glyph(SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY); dump_glyph(SPECIAL_GLYPH_UNHAPPY_SMILEY); dump_glyph(SPECIAL_GLYPH_DEPRESSED_SMILEY); dump_glyph(SPECIAL_GLYPH_LOCK_AND_KEY); dump_glyph(SPECIAL_GLYPH_TOUCH); dump_glyph(SPECIAL_GLYPH_RECYCLING); dump_glyph(SPECIAL_GLYPH_DOWNLOAD); dump_glyph(SPECIAL_GLYPH_SPARKLES); dump_glyph(SPECIAL_GLYPH_LOW_BATTERY); dump_glyph(SPECIAL_GLYPH_WARNING_SIGN); } DEFINE_TEST_MAIN(LOG_INFO);
4,365
33.377953
72
c