/*.tar.xz
/Makefile
/TAGS
-/accelerometer
+/GPATH
+/GRTAGS
+/GSYMS
+/GTAGS
/ata_id
/bootctl
/build-aux
/defined
/exported
/exported-*
-/gtk-doc.make
/hostnamectl
/install-tree
/journalctl
/systemd-cgls
/systemd-cgroups-agent
/systemd-cgtop
-/systemd-consoled
/systemd-coredump
/systemd-cryptsetup
/systemd-cryptsetup-generator
/systemd-debug-generator
/systemd-delta
/systemd-detect-virt
-/systemd-efi-boot-generator
/systemd-escape
-/systemd-evcat
/systemd-export
/systemd-firstboot
/systemd-fsck
-/systemd-fsckd
/systemd-fstab-generator
/systemd-getty-generator
/systemd-gnome-ask-password-agent
/systemd-machine-id-commit
/systemd-machine-id-setup
/systemd-machined
-/systemd-modeset
/systemd-modules-load
/systemd-networkd
/systemd-networkd-wait-online
/systemd-rfkill
/systemd-run
/systemd-shutdown
-/systemd-shutdownd
/systemd-sleep
/systemd-socket-proxyd
/systemd-stdio-bridge
-/systemd-subterm
/systemd-sysctl
/systemd-system-update-generator
/systemd-sysusers
/systemd-vconsole-setup
/tags
/test-architecture
+/test-audit-type
+/test-af-list
+/test-arphrd-list
/test-async
/test-barrier
+/test-bitmap
/test-boot-timestamp
/test-btrfs
+/test-bus-benchmark
/test-bus-chat
/test-bus-cleanup
/test-bus-creds
/test-bus-gvariant
/test-bus-introspect
/test-bus-kernel
-/test-bus-kernel-benchmark
/test-bus-kernel-bloom
/test-bus-marshal
/test-bus-match
/test-bus-objects
/test-bus-policy
+/test-bus-proxy
/test-bus-server
/test-bus-signature
/test-bus-zero-copy
/test-compress-benchmark
/test-condition
/test-conf-files
+/test-conf-parser
/test-copy
/test-coredump-vacuum
/test-daemon
/test-fdset
/test-fileio
/test-fstab-util
-/test-fw-util
+/test-firewall-util
/test-hashmap
/test-hostname
+/test-hostname-util
/test-icmp6-rs
/test-id128
/test-inhibit
/test-path-util
/test-pppoe
/test-prioq
+/test-process-util
/test-pty
/test-qcow2
/test-ratelimit
/test-replace-var
/test-resolve
/test-ring
-/test-rtnl
-/test-rtnl-manual
+/test-netlink
+/test-netlink-manual
/test-sched-prio
/test-set
/test-sigbus
/test-strv
/test-strxcpyx
/test-tables
-/test-term-page
-/test-term-parser
+/test-terminal-util
/test-time
/test-tmpfiles
/test-udev
config.status
configure
stamp-*
+/pwx_*
+/cleanup_*.sh
+/elogind
+/elogind-cgroups-agent
+/rebuild_all.sh
+/src/libelogind/libelogind.pc
+/elogind-inhibit
+/check_tree.sh
+/get_build_file_diff.sh
+elogind_patches.lst
+/patches/
+/elogind.cbp
+/elogind.layout*
+*.bak
+/FIXME
+/create_dist.sh
+
+++ /dev/null
-language: c
-compiler:
- - gcc
-before_install:
- - sudo apt-get update -qq
- - sudo apt-get install autotools-dev automake autoconf libtool libdbus-1-dev libcap-dev libblkid-dev libmount-dev libpam-dev libcryptsetup-dev libaudit-dev libacl1-dev libattr1-dev libselinux-dev liblzma-dev libgcrypt-dev libqrencode-dev libmicrohttpd-dev gtk-doc-tools gperf python2.7-dev
-script: ./autogen.sh && ./configure --enable-gtk-doc --enable-gtk-doc-pdf && make V=1 && sudo ./systemd-machine-id-setup && make check && make distcheck
-after_failure: cat test-suite.log
-notifications:
- irc:
- channels:
- - "irc.freenode.org#systemd"
- on_success: change
- on_failure: always
- 8ch indent, no tabs, except for files in man/ which are 2ch indent,
and still no tabs
+- We prefer /* comments */ over // comments, please. This is not C++, after
+ all. (Yes we know that C99 supports both kinds of comments, but still,
+ please!)
+
- Don't break code lines too eagerly. We do *not* force line breaks at
80ch, all of today's screens should be much larger than that. But
then again, don't overdo it, ~140ch should be enough really.
no speed benefit, and on calls like printf() "float"s get promoted
to "double"s anyway, so there is no point.
-- Do not invoke functions when you allocate variables on the stack. Wrong:
+- Do not mix function invocations with variable definitions in one
+ line. Wrong:
{
int a = foobar();
- Do not use types like "short". They *never* make sense. Use ints,
longs, long longs, all in unsigned+signed fashion, and the fixed
- size types uint32_t and so on, as well as size_t, but nothing else.
+ size types uint32_t and so on, as well as size_t, but nothing
+ else. Do not use kernel types like u32 and so on, leave that to the
+ kernel.
- Public API calls (i.e. functions exported by our shared libraries)
must be marked "_public_" and need to be prefixed with "sd_". No
c) recvmsg() must get MSG_CMSG_CLOEXEC set
d) F_DUPFD_CLOEXEC should be used instead of F_DUPFD, and so on
-- We never use the XDG version of basename(). glibc defines it in
- libgen.h. The only reason to include that file is because dirname()
+- We never use the POSIX version of basename() (which glibc defines it in
+ libgen.h), only the GNU version (which glibc defines in string.h).
+ The only reason to include libgen.h is because dirname()
is needed. Everytime you need that please immediately undefine
basename(), and add a comment about it, so that no code ever ends up
- using the XDG version!
+ using the POSIX version!
- Use the bool type for booleans, not integers. One exception: in public
headers (i.e those in src/systemd/sd-*.h) use integers after all, as "bool"
2, i.e. stdin, stdout, stderr, should those fds be closed. Given the
special semantics of those fds, it's probably a good idea to avoid
them. F_DUPFD_CLOEXEC with "3" as parameter avoids them.
+
+- When you define a destructor or unref() call for an object, please
+ accept a NULL object and simply treat this as NOP. This is similar
+ to how libc free() works, which accepts NULL pointers and becomes a
+ NOP for them. By following this scheme a lot of if checks can be
+ removed before invoking your destructor, which makes the code
+ substantially more readable and robust.
+
+- Related to this: when you define a destructor or unref() call for an
+ object, please make it return the same type it takes and always
+ return NULL from it. This allows writing code like this:
+
+ p = foobar_unref(p);
+
+ which will always work regardless if p is initialized or not, and
+ guarantees that p is NULL afterwards, all in just one line.
+
+- Use alloca(), but never forget that it is not OK to invoke alloca()
+ within a loop or within function call parameters. alloca() memory is
+ released at the end of a function, and not at the end of a {}
+ block. Thus, if you invoke it in a loop, you keep increasing the
+ stack pointer without ever releasing memory again. (VLAs have better
+ behaviour in this case, so consider using them as an alternative.)
+ Regarding not using alloca() within function parameters, see the
+ BUGS section of the alloca(3) man page.
+
+- Use memzero() or even better zero() instead of memset(..., 0, ...)
+
+- Instead of using memzero()/memset() to initialize structs allocated
+ on the stack, please try to use c99 structure initializers. It's
+ short, prettier and actually even faster at execution. Hence:
+
+ struct foobar t = {
+ .foo = 7,
+ .bar = "bazz",
+ };
+
+ instead of:
+
+ struct foobar t;
+ zero(t);
+ t.foo = 7;
+ t.bar = "bazz";
+
+- When returning a return code from main(), please preferably use
+ EXIT_FAILURE and EXIT_SUCCESS as defined by libc.
+
+- The order in which header files are included doesn't matter too
+ much. systemd-internal headers must not rely on an include order, so
+ it is safe to include them in any order possible.
+ However, to not clutter global includes, and to make sure internal
+ definitions will not affect global headers, please always include the
+ headers of external components first (these are all headers enclosed
+ in <>), followed by our own exported headers (usually everything
+ that's prefixed by "sd-"), and then followed by internal headers.
+ Furthermore, in all three groups, order all includes alphabetically
+ so duplicate includes can easily be detected.
+
+- To implement an endless loop, use "for (;;)" rather than "while
+ (1)". The latter is a bit ugly anyway, since you probably really
+ meant "while (true)"... To avoid the discussion what the right
+ always-true expression for an infinite while() loop is our
+ recommendation is to simply write it without any such expression by
+ using "for (;;)".
+# Do not edit. Generated by make-man-rules.py.
+# To regenerate:
+# 1. Create, update, or remove source .xml files in man/
+# 2. Run 'make update-man-list'
+# 3. Run 'make man' to generate manpages
+#
+# To make a man page conditional on a configure switch add
+# attribute conditional="ENABLE_WHAT" or conditional="WITH_WHAT"
+# to <refentry> element.
MANPAGES += \
+ man/elogind.8 \
man/loginctl.1 \
man/logind.conf.5 \
- man/logind.8
+ man/sd_id128_get_machine.3 \
+ man/sd_id128_randomize.3 \
+ man/sd_id128_to_string.3 \
+ man/sd_machine_get_class.3 \
+ man/sd_notify.3 \
+ man/sd_watchdog_enabled.3
MANPAGES_ALIAS += \
- man/logind.conf.d.5
-man/logind.conf.d.5: man/logind.conf.5
-man/logind.conf.d.html: man/logind.conf.html
+ man/sd_id128_from_string.3 \
+ man/sd_id128_get_boot.3 \
+ man/sd_machine_get_ifindices.3 \
+ man/sd_notifyf.3 \
+ man/sd_pid_notify.3 \
+ man/sd_pid_notify_with_fds.3 \
+ man/sd_pid_notifyf.3
+man/sd_id128_from_string.3: man/sd_id128_to_string.3
+man/sd_id128_get_boot.3: man/sd_id128_get_machine.3
+man/sd_machine_get_ifindices.3: man/sd_machine_get_class.3
+man/sd_notifyf.3: man/sd_notify.3
+man/sd_pid_notify.3: man/sd_notify.3
+man/sd_pid_notify_with_fds.3: man/sd_notify.3
+man/sd_pid_notifyf.3: man/sd_notify.3
+man/sd_id128_from_string.html: man/sd_id128_to_string.html
$(html-alias)
+man/sd_id128_get_boot.html: man/sd_id128_get_machine.html
+ $(html-alias)
+
+man/sd_machine_get_ifindices.html: man/sd_machine_get_class.html
+ $(html-alias)
+
+man/sd_notifyf.html: man/sd_notify.html
+ $(html-alias)
+
+man/sd_pid_notify.html: man/sd_notify.html
+ $(html-alias)
+
+man/sd_pid_notify_with_fds.html: man/sd_notify.html
+ $(html-alias)
+
+man/sd_pid_notifyf.html: man/sd_notify.html
+ $(html-alias)
+
+
if HAVE_PAM
MANPAGES += \
man/pam_elogind.8 \
- man/sd-login.3 \
man/sd_get_seats.3 \
- man/sd_login_monitor_new.3 \
- man/sd_pid_get_session.3 \
man/sd_seat_get_active.3 \
- man/sd_session_is_active.3 \
- man/sd_uid_get_state.3
+ man/sd_session_is_active.3
MANPAGES_ALIAS += \
man/sd_get_machine_names.3 \
man/sd_get_sessions.3 \
man/sd_get_uids.3 \
- man/sd_login_monitor.3 \
- man/sd_login_monitor_flush.3 \
- man/sd_login_monitor_get_events.3 \
- man/sd_login_monitor_get_fd.3 \
- man/sd_login_monitor_get_timeout.3 \
- man/sd_login_monitor_unref.3 \
- man/sd_peer_get_machine_name.3 \
- man/sd_peer_get_owner_uid.3 \
- man/sd_peer_get_session.3 \
- man/sd_peer_get_slice.3 \
- man/sd_peer_get_unit.3 \
- man/sd_peer_get_user_unit.3 \
- man/sd_pid_get_machine_name.3 \
- man/sd_pid_get_owner_uid.3 \
- man/sd_pid_get_slice.3 \
- man/sd_pid_get_unit.3 \
- man/sd_pid_get_user_unit.3 \
man/sd_seat_can_graphical.3 \
man/sd_seat_can_multi_session.3 \
man/sd_seat_can_tty.3 \
man/sd_session_get_type.3 \
man/sd_session_get_uid.3 \
man/sd_session_get_vt.3 \
- man/sd_session_is_remote.3 \
- man/sd_uid_get_display.3 \
- man/sd_uid_get_seats.3 \
- man/sd_uid_get_sessions.3 \
- man/sd_uid_is_on_seat.3
+ man/sd_session_is_remote.3
man/sd_get_machine_names.3: man/sd_get_seats.3
man/sd_get_sessions.3: man/sd_get_seats.3
man/sd_get_uids.3: man/sd_get_seats.3
-man/sd_login_monitor.3: man/sd_login_monitor_new.3
-man/sd_login_monitor_flush.3: man/sd_login_monitor_new.3
-man/sd_login_monitor_get_events.3: man/sd_login_monitor_new.3
-man/sd_login_monitor_get_fd.3: man/sd_login_monitor_new.3
-man/sd_login_monitor_get_timeout.3: man/sd_login_monitor_new.3
-man/sd_login_monitor_unref.3: man/sd_login_monitor_new.3
-man/sd_peer_get_machine_name.3: man/sd_pid_get_session.3
-man/sd_peer_get_owner_uid.3: man/sd_pid_get_session.3
-man/sd_peer_get_session.3: man/sd_pid_get_session.3
-man/sd_peer_get_slice.3: man/sd_pid_get_session.3
-man/sd_peer_get_unit.3: man/sd_pid_get_session.3
-man/sd_peer_get_user_unit.3: man/sd_pid_get_session.3
-man/sd_pid_get_machine_name.3: man/sd_pid_get_session.3
-man/sd_pid_get_owner_uid.3: man/sd_pid_get_session.3
-man/sd_pid_get_slice.3: man/sd_pid_get_session.3
-man/sd_pid_get_unit.3: man/sd_pid_get_session.3
-man/sd_pid_get_user_unit.3: man/sd_pid_get_session.3
man/sd_seat_can_graphical.3: man/sd_seat_get_active.3
man/sd_seat_can_multi_session.3: man/sd_seat_get_active.3
man/sd_seat_can_tty.3: man/sd_seat_get_active.3
man/sd_session_get_uid.3: man/sd_session_is_active.3
man/sd_session_get_vt.3: man/sd_session_is_active.3
man/sd_session_is_remote.3: man/sd_session_is_active.3
-man/sd_uid_get_display.3: man/sd_uid_get_state.3
-man/sd_uid_get_seats.3: man/sd_uid_get_state.3
-man/sd_uid_get_sessions.3: man/sd_uid_get_state.3
-man/sd_uid_is_on_seat.3: man/sd_uid_get_state.3
man/sd_get_machine_names.html: man/sd_get_seats.html
$(html-alias)
man/sd_get_uids.html: man/sd_get_seats.html
$(html-alias)
-man/sd_login_monitor.html: man/sd_login_monitor_new.html
- $(html-alias)
-
-man/sd_login_monitor_flush.html: man/sd_login_monitor_new.html
- $(html-alias)
-
-man/sd_login_monitor_get_events.html: man/sd_login_monitor_new.html
- $(html-alias)
-
-man/sd_login_monitor_get_fd.html: man/sd_login_monitor_new.html
- $(html-alias)
-
-man/sd_login_monitor_get_timeout.html: man/sd_login_monitor_new.html
- $(html-alias)
-
-man/sd_login_monitor_unref.html: man/sd_login_monitor_new.html
- $(html-alias)
-
-man/sd_peer_get_machine_name.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_peer_get_owner_uid.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_peer_get_session.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_peer_get_slice.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_peer_get_unit.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_peer_get_user_unit.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_pid_get_machine_name.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_pid_get_owner_uid.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_pid_get_slice.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_pid_get_unit.html: man/sd_pid_get_session.html
- $(html-alias)
-
-man/sd_pid_get_user_unit.html: man/sd_pid_get_session.html
- $(html-alias)
-
man/sd_seat_can_graphical.html: man/sd_seat_get_active.html
$(html-alias)
man/sd_session_is_remote.html: man/sd_session_is_active.html
$(html-alias)
-man/sd_uid_get_display.html: man/sd_uid_get_state.html
- $(html-alias)
-
-man/sd_uid_get_seats.html: man/sd_uid_get_state.html
- $(html-alias)
+endif
-man/sd_uid_get_sessions.html: man/sd_uid_get_state.html
- $(html-alias)
+if HAVE_PYTHON
+MANPAGES += \
+ man/elogind.index.7
+MANPAGES_ALIAS += \
+ #
-man/sd_uid_is_on_seat.html: man/sd_uid_get_state.html
- $(html-alias)
endif
+# Really, do not edit this file.
+
EXTRA_DIST += \
+ man/elogind.xml \
+ man/less-variables.xml \
+ man/libelogind-pkgconfig.xml \
man/loginctl.xml \
- man/logind.xml \
man/logind.conf.xml \
man/pam_elogind.xml \
man/sd_get_seats.xml \
- man/sd_login_monitor_new.xml \
- man/sd_pid_get_session.xml \
+ man/sd_id128_get_machine.xml \
+ man/sd_id128_randomize.xml \
+ man/sd_id128_to_string.xml \
+ man/sd_machine_get_class.xml \
+ man/sd_notify.xml \
man/sd_seat_get_active.xml \
man/sd_session_is_active.xml \
- man/sd_uid_get_state.xml
+ man/sd_watchdog_enabled.xml \
+ man/standard-conf.xml \
+ man/standard-options.xml \
+ man/user-system-options.xml
# Keep the test-suite.log
.PRECIOUS: $(TEST_SUITE_LOG) Makefile
-LIBELOGIND_CURRENT=6
+LIBELOGIND_CURRENT=11
LIBELOGIND_REVISION=0
-LIBELOGIND_AGE=6
+LIBELOGIND_AGE=11
# Dirs of external packages
dbuspolicydir=@dbuspolicydir@
-dbussessionservicedir=@dbussessionservicedir@
dbussystemservicedir=@dbussystemservicedir@
pamlibdir=@pamlibdir@
pamconfdir=@pamconfdir@
+pkgconfigdatadir=$(datadir)/pkgconfig
pkgconfiglibdir=$(libdir)/pkgconfig
polkitpolicydir=$(datadir)/polkit-1/actions
bashcompletiondir=@bashcompletiondir@
zshcompletiondir=@zshcompletiondir@
-varlogdir=$(localstatedir)/log
-elogindstatedir=$(localstatedir)/lib/elogind
systemsleepdir=$(pkglibexecdir)/sleep.d
systemshutdowndir=$(pkglibexecdir)/shutdown.d
-udevrulesdir=@udevrulesdir@
-udevbindir=@udevbindir@
-
+CGROUP_CONTROLLER=@cgroup_controller@
PKTTYAGENT=$(bindir)/pkttyagent
# Our own, non-special dirs
pkgsysconfdir=$(sysconfdir)/elogind
pkgincludedir=$(includedir)/elogind
-factory_etcdir = $(prefix)/share/factory/etc
-factory_pamdir = $(prefix)/share/factory/etc/pam.d
+udevrulesdir=@udevrulesdir@
+udevbindir=@udevbindir@
+udevlibexecdir=$(udevbindir)
+udevhomedir=$(udevlibexecdir)
+factory_pamdir = $(datadir)/factory/etc/pam.d
+
+# And these are the special ones for /
+rootprefix=@rootprefix@
+rootlibdir=@rootlibdir@
+rootbindir=$(rootprefix)/bin
+
EXTRA_DIST =
BUILT_SOURCES =
pkginclude_HEADERS =
noinst_LTLIBRARIES =
lib_LTLIBRARIES =
-include_HEADERS =
noinst_DATA =
pkgconfiglib_DATA =
-polkitpolicy_in_in_files =
polkitpolicy_in_files =
polkitpolicy_files =
+dist_udevrules_DATA =
+nodist_udevrules_DATA =
dist_pkgsysconf_DATA =
-nodist_pkgsysconf_DATA =
-dist_pkgdata_DATA =
dist_dbuspolicy_DATA =
dist_dbussystemservice_DATA =
check_PROGRAMS =
check_DATA =
tests=
manual_tests =
-TEST_EXTENSIONS = .py
-PY_LOG_COMPILER = $(PYTHON)
if ENABLE_TESTS
noinst_PROGRAMS = $(manual_tests) $(tests)
TESTS = $(tests)
AM_CPPFLAGS = \
-include $(top_builddir)/config.h \
-DPKGSYSCONFDIR=\"$(pkgsysconfdir)\" \
+ -DSYSTEMD_CGROUP_CONTROLLER=\"$(CGROUP_CONTROLLER)\" \
+ -DELOGIND_CGROUP_AGENT_PATH=\"$(pkglibexecdir)/elogind-cgroups-agent\" \
+ -DUDEVLIBEXECDIR=\"$(udevlibexecdir)\" \
-DPOLKIT_AGENT_BINARY_PATH=\"$(PKTTYAGENT)\" \
- -DSYSTEM_CONFIG_UNIT_PATH=\"$(pkgsysconfdir)/system\" \
- -DSYSTEM_DATA_UNIT_PATH=\"$(systemunitdir)\" \
- -DSYSTEMD_CGROUP_AGENT_PATH=\"$(pkglibexecdir)/logind-cgroups-agent\" \
- -DUSER_CONFIG_UNIT_PATH=\"$(pkgsysconfdir)/user\" \
- -DUSER_DATA_UNIT_PATH=\"$(userunitdir)\" \
- -DROOTPREFIX=\"$(rootprefix)\" \
-DSYSTEM_SLEEP_PATH=\"$(systemsleepdir)\" \
-DSYSTEM_SHUTDOWN_PATH=\"$(systemshutdowndir)\" \
-DHALT=\"$(HALT)\" \
-DLIBDIR=\"$(libdir)\" \
-DTEST_DIR=\"$(abs_top_srcdir)/test\" \
-I $(top_srcdir)/src \
- -I $(top_builddir)/src/shared \
+ -I $(top_builddir)/src/basic \
+ -I $(top_srcdir)/src/basic \
+ -I $(top_srcdir)/src/core \
-I $(top_srcdir)/src/shared \
+ -I $(top_builddir)/src/shared \
-I $(top_srcdir)/src/login \
-I $(top_srcdir)/src/systemd \
-I $(top_srcdir)/src/libelogind/sd-bus \
-I $(top_srcdir)/src/libelogind/sd-event \
- -I $(top_srcdir)/src/libelogind/sd-device \
- $(UDEV_CFLAGS) \
+ -I $(top_srcdir)/src/libelogind/sd-login \
$(OUR_CPPFLAGS)
AM_CFLAGS = $(OUR_CFLAGS)
AM_LDFLAGS = $(OUR_LDFLAGS)
# ------------------------------------------------------------------------------
-SHUTDOWN_TARGET_WANTS =
-LOCAL_FS_TARGET_WANTS =
-MULTI_USER_TARGET_WANTS =
-GRAPHICAL_TARGET_WANTS =
-RESCUE_TARGET_WANTS =
-SYSINIT_TARGET_WANTS =
-SOCKETS_TARGET_WANTS =
-BUSNAMES_TARGET_WANTS =
-TIMERS_TARGET_WANTS =
-USER_SOCKETS_TARGET_WANTS =
-USER_DEFAULT_TARGET_WANTS =
-USER_BUSNAMES_TARGET_WANTS =
-
-SYSTEM_UNIT_ALIASES =
-USER_UNIT_ALIASES =
-GENERAL_ALIASES =
-
-install-target-wants-hook:
- what="$(SHUTDOWN_TARGET_WANTS)" && wants=shutdown.target && dir=$(systemunitdir) && $(add-wants)
- what="$(LOCAL_FS_TARGET_WANTS)" && wants=local-fs.target && dir=$(systemunitdir) && $(add-wants)
- what="$(MULTI_USER_TARGET_WANTS)" && wants=multi-user.target && dir=$(systemunitdir) && $(add-wants)
- what="$(GRAPHICAL_TARGET_WANTS)" && wants=graphical.target && dir=$(systemunitdir) && $(add-wants)
- what="$(RESCUE_TARGET_WANTS)" && wants=rescue.target && dir=$(systemunitdir) && $(add-wants)
- what="$(SYSINIT_TARGET_WANTS)" && wants=sysinit.target && dir=$(systemunitdir) && $(add-wants)
- what="$(SOCKETS_TARGET_WANTS)" && wants=sockets.target && dir=$(systemunitdir) && $(add-wants)
- what="$(TIMERS_TARGET_WANTS)" && wants=timers.target && dir=$(systemunitdir) && $(add-wants)
- what="$(SLICES_TARGET_WANTS)" && wants=slices.target && dir=$(systemunitdir) && $(add-wants)
- what="$(USER_SOCKETS_TARGET_WANTS)" && wants=sockets.target && dir=$(userunitdir) && $(add-wants)
- what="$(USER_DEFAULT_TARGET_WANTS)" && wants=default.target && dir=$(userunitdir) && $(add-wants)
-
-install-busnames-target-wants-hook:
- what="$(BUSNAMES_TARGET_WANTS)" && wants=busnames.target && dir=$(systemunitdir) && $(add-wants)
- what="$(USER_BUSNAMES_TARGET_WANTS)" && wants=busnames.target && dir=$(userunitdir) && $(add-wants)
-
-define add-wants
- [ -z "$$what" ] || ( \
- dir=$(DESTDIR)$$dir/$$wants.wants && \
- $(MKDIR_P) -m 0755 $$dir && \
- cd $$dir && \
- rm -f $$what && \
- for i in $$what; do $(LN_S) ../$$i . || exit $$? ; done )
-endef
-
-install-aliases-hook:
- set -- $(GENERAL_ALIASES) && \
- dir= && $(install-relative-aliases)
-
-define install-aliases
- while [ -n "$$1" ]; do \
- $(MKDIR_P) `dirname $(DESTDIR)$$dir/$$2` && \
- rm -f $(DESTDIR)$$dir/$$2 && \
- $(LN_S) $$1 $(DESTDIR)$$dir/$$2 && \
- shift 2 || exit $$?; \
- done
-endef
-
-define install-relative-aliases
- while [ -n "$$1" ]; do \
- $(MKDIR_P) `dirname $(DESTDIR)$$dir/$$2` && \
- rm -f $(DESTDIR)$$dir/$$2 && \
- $(LN_S) --relative $(DESTDIR)$$1 $(DESTDIR)$$dir/$$2 && \
- shift 2 || exit $$?; \
- done
+define move-to-rootlibdir
+ if test "$(libdir)" != "$(rootlibdir)"; then \
+ $(MKDIR_P) $(DESTDIR)$(rootlibdir) && \
+ so_img_name=$$(readlink $(DESTDIR)$(libdir)/$$libname) && \
+ rm -f $(DESTDIR)$(libdir)/$$libname && \
+ $(LN_S) --relative -f $(DESTDIR)$(rootlibdir)/$$so_img_name $(DESTDIR)$(libdir)/$$libname && \
+ mv $(DESTDIR)$(libdir)/$$libname.* $(DESTDIR)$(rootlibdir); \
+ fi
endef
install-touch-usr-hook:
touch -c $(DESTDIR)/$(prefix)
INSTALL_EXEC_HOOKS += \
- install-target-wants-hook \
- install-aliases-hook \
install-touch-usr-hook
-if ENABLE_KDBUS
-INSTALL_EXEC_HOOKS += \
- install-busnames-target-wants-hook
-endif
# ------------------------------------------------------------------------------
AM_V_M4 = $(AM_V_M4_$(V))
AM_V_RM_0 = @echo " RM " $@;
# ------------------------------------------------------------------------------
-bin_PROGRAMS =
+rootbin_PROGRAMS =
pkglibexec_PROGRAMS =
-dist_bashcompletion_DATA =
-dist_zshcompletion_DATA =
dist_doc_DATA = \
README \
NEWS \
LICENSE.LGPL2.1 \
- LICENSE.GPL2 \
- LICENSE.MIT \
- src/libelogind/sd-bus/PORTING-DBUS1 \
- src/libelogind/sd-bus/DIFFERENCES \
- src/libelogind/sd-bus/GVARIANT-SERIALIZATION
+ LICENSE.GPL2
@INTLTOOL_POLICY_RULE@
CLEANFILES += \
docs/html/man
+if HAVE_PYTHON
+man/index.html: man/elogind.index.html
+ $(AM_V_LN)$(LN_S) -f elogind.index.html $@
+
+noinst_DATA += \
+ man/index.html
+
+CLEANFILES += \
+ man/index.html
+
+XML_GLOB = $(wildcard $(top_srcdir)/man/*.xml)
+NON_INDEX_XML_FILES = $(filter-out man/elogind.index.xml,$(XML_FILES))
+SOURCE_XML_FILES = ${patsubst %,$(top_srcdir)/%,$(filter-out man/elogind.directives.xml,$(NON_INDEX_XML_FILES))}
+
+# This target should only be run manually. It recreates Makefile-man.am
+# file in the source directory based on all man/*.xml files. Run it after
+# adding, removing, or changing the conditional in a man page.
+update-man-list: $(top_srcdir)/tools/make-man-rules.py $(XML_GLOB)
+ $(AM_V_GEN)$(PYTHON) $^ > $(top_srcdir)/Makefile-man.tmp
+ $(AM_V_at)mv $(top_srcdir)/Makefile-man.tmp $(top_srcdir)/Makefile-man.am
+ @echo "Makefile-man.am has been regenerated"
+
+man/elogind.index.xml: $(top_srcdir)/tools/make-man-index.py $(NON_INDEX_XML_FILES)
+ $(AM_V_at)$(MKDIR_P) $(dir $@)
+ $(AM_V_GEN)$(PYTHON) $< $@ $(filter-out $<,$^)
+
+man/elogind.directives.xml: $(top_srcdir)/tools/make-directive-index.py man/custom-entities.ent $(SOURCE_XML_FILES)
+ $(AM_V_at)$(MKDIR_P) $(dir $@)
+ $(AM_V_GEN)$(PYTHON) $< $@ $(SOURCE_XML_FILES)
+
+CLEANFILES += \
+ man/elogind.index.xml \
+ man/elogind.directives.xml
+
+EXTRA_DIST += \
+ tools/make-man-rules.py
+
+endif
+
endif
EXTRA_DIST += \
- $(XML_FILES) \
+ $(filter-out man/elogind.directives.xml,$(XML_FILES)) \
$(HTML_FILES) \
$(HTML_ALIAS) \
$(man_MANS) \
- tools/make-man-index.py \
- tools/make-directive-index.py \
- tools/xml_helper.py
+ $(NULL)
# ------------------------------------------------------------------------------
noinst_LTLIBRARIES += \
- libelogind-shared.la
-
-libelogind_shared_la_SOURCES = \
- src/shared/capability.c \
- src/shared/capability.h \
- src/shared/linux/auto_dev-ioctl.h \
- src/shared/ioprio.h \
- src/shared/missing.h \
- src/shared/initreq.h \
- src/shared/securebits.h \
- src/shared/special.h \
- src/shared/list.h \
- src/shared/unaligned.h \
- src/shared/macro.h \
- src/shared/def.h \
- src/shared/sparse-endian.h \
- src/shared/refcnt.h \
- src/shared/udev-util.h \
- src/shared/device-nodes.c \
- src/shared/device-nodes.h \
- src/shared/util.c \
- src/shared/util.h \
- src/shared/virt.c \
- src/shared/virt.h \
- src/shared/architecture.c \
- src/shared/architecture.h \
- src/shared/fstab-util.c \
- src/shared/fstab-util.h \
- src/shared/path-util.c \
- src/shared/path-util.h \
- src/shared/time-util.c \
- src/shared/time-util.h \
- src/shared/locale-util.c \
- src/shared/locale-util.h \
- src/shared/mempool.c \
- src/shared/mempool.h \
- src/shared/hashmap.c \
- src/shared/hashmap.h \
- src/shared/siphash24.c \
- src/shared/siphash24.h \
- src/shared/set.h \
- src/shared/fdset.c \
- src/shared/fdset.h \
- src/shared/prioq.c \
- src/shared/prioq.h \
+ libbasic.la
+
+libbasic_la_SOURCES = \
+ src/basic/missing.h \
+ src/basic/musl_missing.h \
+ src/basic/musl_missing.c \
+ src/basic/capability.c \
+ src/basic/capability.h \
+ src/basic/conf-files.c \
+ src/basic/conf-files.h \
+ src/basic/hostname-util.h \
+ src/basic/hostname-util.c \
+ src/basic/unit-name.c \
+ src/basic/unit-name.h \
+ src/basic/util.c \
+ src/basic/util.h \
+ src/basic/path-util.c \
+ src/basic/path-util.h \
+ src/basic/time-util.c \
+ src/basic/time-util.h \
+ src/basic/signal-util.c \
+ src/basic/signal-util.h \
+ src/basic/mempool.c \
+ src/basic/mempool.h \
+ src/basic/hashmap.c \
+ src/basic/hashmap.h \
+ src/basic/siphash24.c \
+ src/basic/siphash24.h \
+ src/basic/prioq.c \
+ src/basic/prioq.h \
+ src/basic/strv.c \
+ src/basic/strv.h \
+ src/basic/log.c \
+ src/basic/log.h \
+ src/basic/bus-label.c \
+ src/basic/bus-label.h \
+ src/basic/virt.c \
+ src/basic/virt.h \
+ src/basic/smack-util.c \
+ src/basic/smack-util.h \
+ src/basic/utf8.c \
+ src/basic/utf8.h \
+ src/basic/gunicode.c \
+ src/basic/gunicode.h \
+ src/basic/fileio.c \
+ src/basic/fileio.h \
+ src/basic/mkdir.c \
+ src/basic/mkdir.h \
+ src/basic/cgroup-util.c \
+ src/basic/cgroup-util.h \
+ src/basic/errno-list.c \
+ src/basic/errno-list.h \
+ src/basic/terminal-util.c \
+ src/basic/terminal-util.h \
+ src/basic/login-util.h \
+ src/basic/login-util.c \
+ src/basic/audit.c \
+ src/basic/audit.h \
+ src/basic/memfd-util.c \
+ src/basic/memfd-util.h \
+ src/basic/process-util.c \
+ src/basic/process-util.h \
+ src/basic/random-util.c \
+ src/basic/random-util.h \
+ src/basic/verbs.c \
+ src/basic/verbs.h \
+ src/basic/label.c \
+ src/basic/label.h \
+ src/basic/selinux-util.c \
+ src/basic/selinux-util.h \
+ src/basic/mkdir-label.c \
+ src/basic/fileio-label.c \
+ src/basic/fileio-label.h \
+ src/basic/rm-rf.c \
+ src/basic/rm-rf.h \
+ src/basic/copy.c \
+ src/basic/copy.h \
+ src/basic/parse-printf-format.c \
+ src/basic/parse-printf-format.h
+
+nodist_libbasic_la_SOURCES = \
+ src/basic/errno-from-name.h \
+ src/basic/errno-to-name.h
+
+libbasic_la_CFLAGS = \
+ $(AM_CFLAGS) \
+ $(SELINUX_CFLAGS) \
+ $(CAP_CFLAGS) \
+ -pthread
+
+libbasic_la_LDFLAGS = \
+ $(CAP_LDFLAGS)
+
+libbasic_la_LIBADD = \
+ $(SELINUX_LIBS) \
+ $(CAP_LIBS) \
+ -ldl \
+ -lm
+
+# -----------------------------------------------------------------------------
+noinst_LTLIBRARIES += \
+ libshared.la
+
+libshared_la_SOURCES = \
src/shared/sleep-config.c \
src/shared/sleep-config.h \
- src/shared/strv.c \
- src/shared/strv.h \
- src/shared/env-util.c \
- src/shared/env-util.h \
- src/shared/strbuf.c \
- src/shared/strbuf.h \
- src/shared/strxcpyx.c \
- src/shared/strxcpyx.h \
src/shared/conf-parser.c \
src/shared/conf-parser.h \
- src/shared/log.c \
- src/shared/log.h \
- src/shared/ratelimit.h \
- src/shared/ratelimit.c \
- src/shared/exit-status.c \
- src/shared/exit-status.h \
- src/shared/utf8.c \
- src/shared/utf8.h \
- src/shared/gunicode.c \
- src/shared/gunicode.h \
src/shared/pager.c \
src/shared/pager.h \
- src/shared/socket-util.c \
- src/shared/socket-util.h \
- src/shared/in-addr-util.c \
- src/shared/in-addr-util.h \
- src/shared/ether-addr-util.h \
- src/shared/conf-files.c \
- src/shared/conf-files.h \
- src/shared/cgroup-util.c \
- src/shared/cgroup-util.h \
- src/shared/cgroup-show.c \
- src/shared/cgroup-show.h \
- src/shared/logs-show.h \
- src/shared/unit-name.c \
- src/shared/unit-name.h \
- src/shared/watchdog.c \
- src/shared/watchdog.h \
- src/shared/replace-var.c \
- src/shared/replace-var.h \
src/shared/spawn-polkit-agent.c \
src/shared/spawn-polkit-agent.h \
- src/shared/clock-util.c \
- src/shared/clock-util.h \
- src/shared/calendarspec.c \
- src/shared/calendarspec.h \
- src/shared/fileio.c \
- src/shared/fileio.h \
- src/shared/output-mode.h \
- src/shared/MurmurHash2.c \
- src/shared/MurmurHash2.h \
- src/shared/acpi-fpdt.h \
- src/shared/acpi-fpdt.c \
- src/shared/mkdir.c \
- src/shared/mkdir.h \
- src/shared/smack-util.c \
- src/shared/smack-util.h \
- src/shared/apparmor-util.c \
- src/shared/apparmor-util.h \
- src/shared/ima-util.c \
- src/shared/ima-util.h \
- src/shared/ptyfwd.c \
- src/shared/ptyfwd.h \
- src/shared/errno-list.c \
- src/shared/errno-list.h \
- src/shared/af-list.c \
- src/shared/af-list.h \
- src/shared/arphrd-list.c \
- src/shared/arphrd-list.h \
- src/shared/cap-list.c \
- src/shared/cap-list.h \
- src/shared/audit.c \
- src/shared/audit.h \
- src/shared/xml.c \
- src/shared/xml.h \
- src/shared/json.c \
- src/shared/json.h \
- src/shared/selinux-util.c \
- src/shared/selinux-util.h \
- src/shared/label.c \
- src/shared/label.h \
- src/shared/socket-label.c \
- src/shared/mkdir-label.c \
- src/shared/fileio-label.h \
- src/shared/fileio-label.c \
- src/shared/bus-label.c \
- src/shared/bus-label.h \
- src/shared/gpt.h \
- src/shared/clean-ipc.h \
src/shared/clean-ipc.c \
- src/shared/login-shared.c \
- src/shared/login-shared.h \
- src/shared/ring.c \
- src/shared/ring.h \
- src/shared/barrier.c \
- src/shared/barrier.h \
- src/shared/pty.c \
- src/shared/pty.h \
- src/shared/async.c \
- src/shared/async.h \
- src/shared/base-filesystem.c \
- src/shared/base-filesystem.h \
- src/shared/memfd-util.c \
- src/shared/memfd-util.h \
- src/shared/uid-range.c \
- src/shared/uid-range.h \
- src/shared/nss-util.h \
- src/shared/verbs.c \
- src/shared/verbs.h \
- src/shared/sigbus.c \
- src/shared/sigbus.h \
- src/shared/build.h \
- src/shared/import-util.c \
- src/shared/import-util.h \
- src/shared/sysctl-util.c \
- src/shared/sysctl-util.h
-
-nodist_libelogind_shared_la_SOURCES = \
- src/shared/errno-from-name.h \
- src/shared/errno-to-name.h \
- src/shared/af-from-name.h \
- src/shared/af-to-name.h \
- src/shared/arphrd-from-name.h \
- src/shared/arphrd-to-name.h \
- src/shared/cap-from-name.h \
- src/shared/cap-to-name.h
-
-libelogind_shared_la_CFLAGS = \
- $(AM_CFLAGS) \
- $(CAP_CFLAGS) \
- $(SECCOMP_CFLAGS) \
- -pthread
-
-libelogind_shared_la_LIBADD = \
- $(CAP_LIBS) \
- -lm
-
-libelogind_shared_la_LDFLAGS = \
- $(CAP_LDFLAGS)
+ src/shared/clean-ipc.h \
+ src/shared/cgroup-show.c \
+ src/shared/cgroup-show.h \
+ src/shared/utmp-wtmp.h \
+ src/shared/bus-util.c \
+ src/shared/bus-util.h
-# ------------------------------------------------------------------------------
if HAVE_ACL
-noinst_LTLIBRARIES += \
- libelogind-acl.la
-
-libelogind_acl_la_SOURCES = \
+libshared_la_SOURCES += \
src/shared/acl-util.c \
src/shared/acl-util.h
+endif
-libelogind_acl_la_CFLAGS = \
+libshared_la_CFLAGS = \
$(AM_CFLAGS) \
$(ACL_CFLAGS)
-libelogind_acl_la_LIBADD = \
+libshared_la_LIBADD = \
+ libelogind-internal.la \
+ $(UDEV_LIBS) \
$(ACL_LIBS)
-endif
gperf_txt_sources = \
- src/shared/errno-list.txt \
- src/shared/af-list.txt \
- src/shared/arphrd-list.txt \
- src/shared/cap-list.txt
+ src/basic/errno-list.txt
BUILT_SOURCES += \
+ $(gperf_gperf_m4_sources:-gperf.gperf.m4=-gperf.c) \
$(gperf_gperf_sources:-gperf.gperf=-gperf.c) \
$(gperf_txt_sources:-list.txt=-from-name.h) \
$(gperf_txt_sources:-list.txt=-to-name.h)
EXTRA_DIST += \
$(gperf_gperf_m4_sources) \
- $(gperf_gperf_sources) \
- $(gperf_txt_sources:-list.txt=-from-name.gperf)
+ $(gperf_gperf_sources)
CLEANFILES += \
$(gperf_txt_sources)
%-from-name.gperf: %-list.txt
+ $(AM_V_at)$(MKDIR_P) $(dir $@)
$(AM_V_GEN)$(AWK) 'BEGIN{ print "struct $(notdir $*)_name { const char* name; int id; };"; print "%null-strings"; print "%%";} { printf "%s, %s\n", $$1, $$1 }' <$< >$@
%-from-name.h: %-from-name.gperf
+ $(AM_V_at)$(MKDIR_P) $(dir $@)
$(AM_V_GPERF)$(GPERF) -L ANSI-C -t --ignore-case -N lookup_$(notdir $*) -H hash_$(notdir $*)_name -p -C <$< >$@
-src/shared/errno-list.txt:
+src/basic/errno-list.txt:
$(AM_V_at)$(MKDIR_P) $(dir $@)
$(AM_V_GEN)$(CPP) $(CFLAGS) $(AM_CPPFLAGS) $(CPPFLAGS) -dM -include errno.h - </dev/null | $(AWK) '/^#define[ \t]+E[^ _]+[ \t]+/ { print $$2; }' >$@
-src/shared/errno-to-name.h: src/shared/errno-list.txt
- $(AM_V_GEN)$(AWK) 'BEGIN{ print "static const char* const errno_names[] = { "} !/EDEADLOCK/ && !/EWOULDBLOCK/ && !/ENOTSUP/ { printf "[%s] = \"%s\",\n", $$1, $$1 } END{print "};"}' <$< >$@
-
-
-src/shared/af-list.txt:
- $(AM_V_at)$(MKDIR_P) $(dir $@)
- $(AM_V_GEN)$(CPP) $(CFLAGS) $(AM_CPPFLAGS) $(CPPFLAGS) -dM -include sys/socket.h - </dev/null | grep -v AF_UNSPEC | grep -v AF_MAX | $(AWK) '/^#define[ \t]+AF_[^ \t]+[ \t]+PF_[^ \t]/ { print $$2; }' >$@
-
-src/shared/af-to-name.h: src/shared/af-list.txt
- $(AM_V_GEN)$(AWK) 'BEGIN{ print "static const char* const af_names[] = { "} !/AF_FILE/ && !/AF_ROUTE/ && !/AF_LOCAL/ { printf "[%s] = \"%s\",\n", $$1, $$1 } END{print "};"}' <$< >$@
-
-
-src/shared/arphrd-list.txt:
- $(AM_V_at)$(MKDIR_P) $(dir $@)
- $(AM_V_GEN)$(CPP) $(CFLAGS) $(AM_CPPFLAGS) $(CPPFLAGS) -dM -include net/if_arp.h - </dev/null | $(AWK) '/^#define[ \t]+ARPHRD_[^ \t]+[ \t]+[^ \t]/ { print $$2; }' | sed -e 's/ARPHRD_//' >$@
-
-src/shared/arphrd-to-name.h: src/shared/arphrd-list.txt
- $(AM_V_GEN)$(AWK) 'BEGIN{ print "static const char* const arphrd_names[] = { "} !/CISCO/ { printf "[ARPHRD_%s] = \"%s\",\n", $$1, $$1 } END{print "};"}' <$< >$@
-
-src/shared/arphrd-from-name.gperf: src/shared/arphrd-list.txt
- $(AM_V_GEN)$(AWK) 'BEGIN{ print "struct arphrd_name { const char* name; int id; };"; print "%null-strings"; print "%%";} { printf "%s, ARPHRD_%s\n", $$1, $$1 }' <$< >$@
-
-
-src/shared/cap-list.txt:
+src/basic/errno-to-name.h: src/basic/errno-list.txt
$(AM_V_at)$(MKDIR_P) $(dir $@)
- $(AM_V_GEN)$(CPP) $(CFLAGS) $(AM_CPPFLAGS) $(CPPFLAGS) -dM -include linux/capability.h -include missing.h - </dev/null | $(AWK) '/^#define[ \t]+CAP_[A-Z_]+[ \t]+/ { print $$2; }' | grep -v CAP_LAST_CAP >$@
-
-src/shared/cap-to-name.h: src/shared/cap-list.txt
- $(AM_V_GEN)$(AWK) 'BEGIN{ print "static const char* const capability_names[] = { "} { printf "[%s] = \"%s\",\n", $$1, tolower($$1) } END{print "};"}' <$< >$@
-
-src/shared/cap-from-name.gperf: src/shared/cap-list.txt
- $(AM_V_GEN)$(AWK) 'BEGIN{ print "struct capability_name { const char* name; int id; };"; print "%null-strings"; print "%%";} { printf "%s, %s\n", $$1, $$1 }' <$< >$@
+ $(AM_V_GEN)$(AWK) 'BEGIN{ print "static const char* const errno_names[] = { "} !/EDEADLOCK/ && !/EWOULDBLOCK/ && !/ENOTSUP/ { printf "[%s] = \"%s\",\n", $$1, $$1 } END{print "};"}' <$< >$@
-src/shared/cap-from-name.h: src/shared/cap-from-name.gperf
- $(AM_V_GPERF)$(GPERF) -L ANSI-C -t --ignore-case -N lookup_capability -H hash_capability_name -p -C <$< >$@
# ------------------------------------------------------------------------------
-dist_factory_etc_DATA =
if HAVE_PAM
dist_factory_pam_DATA = \
endif
# ------------------------------------------------------------------------------
-noinst_LTLIBRARIES += \
- libelogind-internal.la
+pkglibexec_PROGRAMS += \
+ elogind-cgroups-agent
+
+elogind_cgroups_agent_SOURCES = \
+ src/cgroups-agent/cgroups-agent.c
+elogind_cgroups_agent_LDADD = \
+ libshared.la
+
+# ------------------------------------------------------------------------------
libelogind_internal_la_SOURCES = \
src/systemd/sd-bus.h \
src/systemd/sd-bus-protocol.h \
src/systemd/sd-bus-vtable.h \
- src/systemd/sd-utf8.h \
src/systemd/sd-event.h \
- src/systemd/sd-rtnl.h \
- src/systemd/sd-resolve.h \
src/systemd/sd-login.h \
src/systemd/sd-id128.h \
src/systemd/sd-daemon.h \
- src/systemd/sd-network.h \
- src/systemd/sd-device.h \
+ src/libelogind/libelogind.sym \
src/libelogind/sd-bus/sd-bus.c \
src/libelogind/sd-bus/bus-control.c \
src/libelogind/sd-bus/bus-control.h \
src/libelogind/sd-bus/bus-convenience.c \
src/libelogind/sd-bus/bus-track.c \
src/libelogind/sd-bus/bus-track.h \
- src/libelogind/sd-bus/bus-util.c \
- src/libelogind/sd-bus/bus-util.h \
src/libelogind/sd-bus/bus-slot.c \
src/libelogind/sd-bus/bus-slot.h \
src/libelogind/sd-bus/bus-protocol.h \
- src/libelogind/sd-bus/kdbus.h \
- src/libelogind/sd-utf8/sd-utf8.c \
src/libelogind/sd-event/sd-event.c \
- src/libelogind/sd-event/event-util.h \
- src/libelogind/sd-rtnl/sd-rtnl.c \
- src/libelogind/sd-rtnl/rtnl-internal.h \
- src/libelogind/sd-rtnl/rtnl-message.c \
- src/libelogind/sd-rtnl/rtnl-types.h \
- src/libelogind/sd-rtnl/rtnl-types.c \
- src/libelogind/sd-rtnl/rtnl-util.h \
- src/libelogind/sd-rtnl/rtnl-util.c \
- src/libelogind/sd-rtnl/local-addresses.h \
- src/libelogind/sd-rtnl/local-addresses.c \
src/libelogind/sd-id128/sd-id128.c \
src/libelogind/sd-daemon/sd-daemon.c \
- src/libelogind/sd-login/sd-login.c \
- src/libelogind/sd-network/sd-network.c \
- src/libelogind/sd-network/network-util.h \
- src/libelogind/sd-network/network-util.c \
- src/libelogind/sd-device/device-internal.h \
- src/libelogind/sd-device/device-util.h \
- src/libelogind/sd-device/sd-device.c \
- src/libelogind/sd-device/device-private.c \
- src/libelogind/sd-device/device-private.h
-
-nodist_libelogind_internal_la_SOURCES = \
- src/libelogind/libelogind.sym
-
-libelogind_internal_la_CFLAGS = \
- $(AM_CFLAGS) \
- -pthread
-
-BUILT_SOURCES += \
- src/libelogind/libelogind.sym
-
+ src/libelogind/sd-login/sd-login.c
libelogind_internal_la_LIBADD = \
- libelogind-shared.la
+ libbasic.la \
+ -lresolv
-libelogind_internal_la_LDFLAGS = \
- $(AM_LDFLAGS)
+noinst_LTLIBRARIES += \
+ libelogind-internal.la
-lib_LTLIBRARIES += \
- libelogind.la
+EXTRA_DIST += \
+ src/libelogind/libelogind.pc.in \
+ src/libelogind/sd-bus/DIFFERENCES \
+ src/libelogind/sd-bus/GVARIANT-SERIALIZATION
-libelogind_la_SOURCES =
+libelogind_la_SOURCES = \
+ $(libelogind_internal_la_SOURCES)
+
+nodist_libelogind_la_SOURCES = \
+ $(nodist_libelogind_internal_la_SOURCES)
libelogind_la_CFLAGS = \
- $(AM_CFLAGS)
+ $(libelogind_internal_la_CFLAGS)
libelogind_la_LDFLAGS = \
$(AM_LDFLAGS) \
-version-info $(LIBELOGIND_CURRENT):$(LIBELOGIND_REVISION):$(LIBELOGIND_AGE) \
- -Wl,--version-script=src/libelogind/libelogind.sym
+ -Wl,--version-script=$(top_srcdir)/src/libelogind/libelogind.sym
libelogind_la_LIBADD = \
- libelogind-internal.la
+ $(libelogind_internal_la_LIBADD)
+
+libelogind-install-hook:
+ libname=libelogind.so && $(move-to-rootlibdir)
+
+libelogind-uninstall-hook:
+ rm -f $(DESTDIR)$(rootlibdir)/libelogind.so*
+
+INSTALL_EXEC_HOOKS += libelogind-install-hook
+UNINSTALL_EXEC_HOOKS += libelogind-uninstall-hook
pkgconfiglib_DATA += \
src/libelogind/libelogind.pc
pkginclude_HEADERS += \
src/systemd/sd-login.h \
src/systemd/sd-messages.h \
- src/systemd/sd-journal.h \
+ src/systemd/sd-id128.h \
src/systemd/_sd-common.h
-EXTRA_DIST += \
- src/libelogind/libelogind.sym.m4 \
- src/libelogind/libelogind.pc.in \
- src/libelogind/sd-bus/DIFFERENCES \
- src/libelogind/sd-bus/GVARIANT-SERIALIZATION
-
-CLEANFILES += \
- src/libelogind/libelogind.sym
-
-BUILT_SOURCES += \
- src/libelogind/libelogind.sym
+lib_LTLIBRARIES += \
+ libelogind.la
# ------------------------------------------------------------------------------
elogind_SOURCES = \
src/login/logind-session-dbus.c \
src/login/logind-seat-dbus.c \
src/login/logind-user-dbus.c \
- src/login/logind-acl.h
+ src/login/logind-utmp.c \
+ src/login/logind-acl.h \
+ src/core/cgroup.h \
+ src/core/cgroup.c \
+ src/core/mount-setup.h \
+ src/core/mount-setup.c
libelogind_core_la_LIBADD = \
- $(UDEV_LIBS) \
- libelogind-internal.la
+ libshared.la
if HAVE_ACL
libelogind_core_la_SOURCES += \
src/login/logind-acl.c
-
-libelogind_core_la_LIBADD += \
- libelogind-acl.la
endif
noinst_LTLIBRARIES += \
libelogind-core.la
-pkglibexec_PROGRAMS += elogind
+pkglibexec_PROGRAMS += \
+ elogind
loginctl_SOURCES = \
src/login/loginctl.c \
src/login/sysfs-show.c
loginctl_LDADD = \
- $(UDEV_LIBS) \
- libelogind-internal.la \
- libelogind-shared.la
+ libshared.la
-bin_PROGRAMS += \
+rootbin_PROGRAMS += \
loginctl
-logind_cgroups_agent_SOURCES = \
- src/cgroups-agent/cgroups-agent.c
-
-logind_cgroups_agent_LDADD = \
- libelogind-internal.la \
- libelogind-shared.la
-
-pkglibexec_PROGRAMS += \
- logind-cgroups-agent
-
-dist_bashcompletion_DATA += \
+if ENABLE_BASH_COMPLETION
+dist_bashcompletion_DATA = \
shell-completion/bash/loginctl
+endif
-dist_zshcompletion_DATA += \
- shell-completion/zsh/_loginctl
+if ENABLE_ZSH_COMPLETION
+dist_zshcompletion_DATA = \
+ shell-completion/zsh/_loginctl \
+ shell-completion/zsh/_elogind-inhibit
+endif
-systemd_inhibit_SOURCES = \
+elogind_inhibit_SOURCES = \
src/login/inhibit.c
-systemd_inhibit_LDADD = \
- libelogind-internal.la \
- libelogind-shared.la
+elogind_inhibit_LDADD = \
+ libshared.la
-bin_PROGRAMS += \
- systemd-inhibit
+rootbin_PROGRAMS += \
+ elogind-inhibit
test_login_SOURCES = \
src/libelogind/sd-login/test-login.c
test_login_LDADD = \
- libelogind-internal.la \
- libelogind-shared.la
+ libshared.la
test_login_shared_SOURCES = \
src/login/test-login-shared.c
test_login_shared_LDADD = \
- libelogind-internal.la \
- libelogind-shared.la
+ libshared.la
test_inhibit_SOURCES = \
src/login/test-inhibit.c
test_inhibit_LDADD = \
- libelogind-internal.la \
- libelogind-shared.la
+ libshared.la
test_login_tables_SOURCES = \
- src/login/test-login-tables.c \
- src/shared/test-tables.h
+ src/login/test-login-tables.c
test_login_tables_LDADD = \
libelogind-core.la
pam_elogind_la_CFLAGS = \
$(AM_CFLAGS) \
- $(PAM_CFLAGS) \
- -fvisibility=hidden
+ $(PAM_CFLAGS)
pam_elogind_la_LDFLAGS = \
$(AM_LDFLAGS) \
-Wl,--version-script=$(top_srcdir)/src/login/pam_elogind.sym
pam_elogind_la_LIBADD = \
- libelogind-internal.la \
+ libshared.la \
$(PAM_LIBS)
pamlib_LTLIBRARIES = \
polkitpolicy_files += \
src/login/org.freedesktop.login1.policy
-SYSTEM_UNIT_ALIASES += \
- elogind.service dbus-org.freedesktop.login1.service
-
-dist_udevrules_DATA = \
+dist_udevrules_DATA += \
src/login/70-uaccess.rules \
src/login/70-power-switch.rules
-nodist_udevrules_DATA = \
+nodist_udevrules_DATA += \
src/login/71-seat.rules \
src/login/73-seat-late.rules
# ------------------------------------------------------------------------------
substitutions = \
+ '|rootbindir=$(rootbindir)|' \
'|bindir=$(bindir)|' \
- '|udevbindir=$(udevbindir)|' \
'|pkgsysconfdir=$(pkgsysconfdir)|' \
'|pkgdatadir=$(pkgdatadir)|' \
+ '|udevrulesdir=$(udevrulesdir)|' \
'|PACKAGE_VERSION=$(PACKAGE_VERSION)|' \
'|PACKAGE_NAME=$(PACKAGE_NAME)|' \
'|PACKAGE_URL=$(PACKAGE_URL)|' \
'|includedir=$(includedir)|' \
'|VERSION=$(VERSION)|' \
'|rootprefix=$(rootprefix)|' \
+ '|udevlibexecdir=$(udevlibexecdir)|' \
+ '|KILL=$(KILL)|' \
'|MKDIR_P=$(MKDIR_P)|' \
'|systemuidmax=$(SYSTEM_UID_MAX)|' \
- '|systemgidmax=$(SYSTEM_GID_MAX)|'
+ '|systemgidmax=$(SYSTEM_GID_MAX)|' \
+ '|TTY_GID=$(TTY_GID)|'
SED_PROCESS = \
$(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
$(SED) $(subst '|,-e 's|@,$(subst =,\@|,$(subst |',|g',$(substitutions)))) \
< $< > $@
-units/%: units/%.in
- $(SED_PROCESS)
-
man/%: man/%.in
$(SED_PROCESS)
%.pc: %.pc.in
$(SED_PROCESS)
-%.conf: %.conf.in
- $(SED_PROCESS)
-
-src/%.policy.in: src/%.policy.in.in
- $(SED_PROCESS)
-
shell-completion/%: shell-completion/%.in
$(SED_PROCESS)
if ENABLE_POLKIT
nodist_polkitpolicy_DATA = \
- $(polkitpolicy_files) \
- $(polkitpolicy_in_in_files:.policy.in.in=.policy)
+ $(polkitpolicy_files)
endif
EXTRA_DIST += \
- $(polkitpolicy_in_files) \
- $(polkitpolicy_in_in_files)
+ $(polkitpolicy_in_files)
# ------------------------------------------------------------------------------
if ENABLE_MANPAGES
printf '$(subst '|,<!ENTITY ,$(subst =, ",$(subst |',">\n,$(substitutions))))') \
> $@ # '
-DISTCLEANFILES += \
+CLEANFILES += \
man/custom-entities.ent
XSLTPROC_FLAGS = \
clean-local: $(CLEAN_LOCAL_HOOKS)
rm -rf $(abs_srcdir)/install-tree
+ rm -f $(abs_srcdir)/hwdb/usb.ids $(abs_srcdir)/hwdb/pci.ids $(abs_srcdir)/hwdb/oui.txt \
+ $(abs_srcdir)/hwdb/iab.txt
DISTCHECK_CONFIGURE_FLAGS = \
--with-dbuspolicydir=$$dc_install_base/$(dbuspolicydir) \
- --with-dbussessionservicedir=$$dc_install_base/$(dbussessionservicedir) \
--with-dbussystemservicedir=$$dc_install_base/$(dbussystemservicedir) \
--with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir) \
--with-zshcompletiondir=$$dc_install_base/$(zshcompletiondir) \
--with-pamlibdir=$$dc_install_base/$(pamlibdir) \
--with-pamconfdir=$$dc_install_base/$(pamconfdir) \
- --enable-kdbus \
- --with-udevrulesdir=$$dc_install_base/$(udevrulesdir)
+ --with-rootprefix=$$dc_install_base
-if ENABLE_GTK_DOC
+if ENABLE_SPLIT_USR
DISTCHECK_CONFIGURE_FLAGS += \
- --enable-gtk-doc
+ --enable-split-usr
+else
+DISTCHECK_CONFIGURE_FLAGS += \
+ --disable-split-usr
endif
+
+.PHONY: dist-check-help
+
+dist-check-help: $(rootbin_PROGRAMS)
+ for i in $(abspath $^); do \
+ if $$i --help | grep -v 'default:' | grep -E -q '.{80}.' ; then \
+ echo "$(basename $$i) --help output is too wide:"; \
+ $$i --help | awk 'length > 80' | grep -E --color=yes '.{80}'; \
+ exit 1; \
+ fi; done
+
+dist:
+
+.PHONY: git-tag
+git-tag:
+ git tag -s "v$(VERSION)" -m "elogind $(VERSION)"
+
+.PHONY: git-tar
+git-tar:
+ $(AM_V_at)git archive --format=tar --prefix=elogind-$(VERSION)-raw/ HEAD | \
+ (cd /var/tmp/ && tar xf -)
+ $(AM_V_GEN)(cd /var/tmp && rm -rf elogind-$(VERSION) && $(MKDIR_P) elogind-$(VERSION) && \
+ for f in `find elogind-$(VERSION)-raw/ -type f | cut -d '/' -f 2-` ; do \
+ $(MKDIR_P) elogind-$(VERSION)/`dirname $$f` ; \
+ $(AWK) 'BEGIN { i=0;e=0 } \
+ /^#if\s+0\s*$$/ { i=1;next } \
+ /^#else\s*$$/ { \
+ { if ( i==1 ) e=1; else print }{next} \
+ } \
+ /^#endif\s*\/\/\s*0\s*$$/ { \
+ { if ( i==1 ) { i=0;e=0 } else print }{next} \
+ } \
+ /^\s*\/\/\/.*elogind.*$$/{ next } \
+ /^\s*\/\/\s*#include.+$$/{ next } \
+ { if ( (i==0) || (e==1) ) print }' \
+ elogind-$(VERSION)-raw/$$f \
+ > elogind-$(VERSION)/$$f ; \
+ chmod `stat -c '%a' elogind-$(VERSION)-raw/$$f` \
+ elogind-$(VERSION)/$$f ; \
+ done ; \
+ for l in `find elogind-$(VERSION)-raw/ -type l | cut -d '/' -f 2-` ; do \
+ $(MKDIR_P) elogind-$(VERSION)/`dirname $$l` ; \
+ cp -P elogind-$(VERSION)-raw/$$l elogind-$(VERSION)/$$l ; \
+ done ; \
+ tar czf elogind-$(VERSION).tar.gz elogind-$(VERSION) ; \
+ rm -rf elogind-$(VERSION) elogind-$(VERSION)-raw) ; \
+ $(AM_V_at)mv /var/tmp/elogind-$(VERSION).tar.gz ./
+
+.PHONY: install-tree
+install-tree: all
+ rm -rf $(abs_srcdir)/install-tree
+ $(MAKE) install DESTDIR=$(abs_srcdir)/install-tree
+ tree $(abs_srcdir)/install-tree
+
+# Let's run all tests of the test suite, but under valgrind. Let's
+# exclude the one perl script we have in there
+.PHONY: valgrind-tests
+valgrind-tests: $(TESTS)
+ $(AM_V_GEN)for f in $(filter-out %.pl, $^); do \
+ if file $$f | grep -q shell; then \
+ echo -e "$${x}Skipping non-binary $$f"; else \
+ echo -e "$${x}Running $$f"; \
+ libtool --mode=execute valgrind -q --leak-check=full --max-stackframe=5242880 --error-exitcode=55 $(builddir)/$$f ; fi; \
+ x="\n\n"; \
+ done
+
+exported-%: %
+ $(AM_V_GEN)$(NM) -g --defined-only $(builddir)/.libs/$(<:.la=.so) 2>&1 /dev/null | grep " T " | cut -d" " -f3 > $@
+
+exported: $(addprefix exported-, $(lib_LTLIBRARIES))
+ $(AM_V_GEN)cat $^ > $@
+
+.PHONY: check-api-docs
+check-api-docs: exported man
+ $(AM_V_GEN)for symbol in `cat exported` ; do \
+ if test -f $(builddir)/man/$$symbol.html ; then \
+ echo " Symbol $$symbol() is documented." ; \
+ else \
+ echo "‣ Symbol $$symbol() lacks documentation." ; \
+ fi ; \
+ done
+
+OBJECT_VARIABLES:=$(filter %_OBJECTS,$(.VARIABLES))
+ALL_OBJECTS:=$(foreach v,$(OBJECT_VARIABLES),$($(v)))
+
+undefined defined: $(ALL_OBJECTS)
+ $(AM_V_GEN)for f in $(ALL_OBJECTS) ; do \
+ $(NM) -g --$@-only `echo $(builddir)/"$$f" | sed -e 's,\([^/]*\).lo$$,.libs/\1.o,'` ; \
+ done | cut -c 20- | cut -d @ -f 1 | sort -u > $@
+
+CLEANFILES += \
+ defined \
+ undefined
+
+.PHONY: check-api-unused
+check-api-unused: defined undefined exported
+ ( cat exported undefined ) | sort -u | diff -u - defined | grep ^+ | grep -v ^+++ | cut -c2-
+
+.PHONY: check-includes
+check-includes: $(top_srcdir)/tools/check-includes.pl
+ $(AM_V_GEN) find * -name '*.[hcS]' -type f -print | sort -u \
+ | xargs $(top_srcdir)/tools/check-includes.pl
+
+EXTRA_DIST += \
+ $(top_srcdir)/tools/check-includes.pl
+
+# Stupid test that everything purported to be exported really is
+define generate-sym-test
+ $(AM_V_at)$(MKDIR_P) $(dir $@)
+ $(AM_V_at)printf '#include <stdio.h>\n' > $@
+ $(AM_V_at)printf '#include "%s"\n' $(notdir $(filter %.h, $^)) >> $@
+ $(AM_V_at)printf 'void* functions[] = {\n' >> $@
+ $(AM_V_GEN)sed -r -n 's/^ +([a-zA-Z0-9_]+);/\1,/p' $< >> $@
+ $(AM_V_at)printf '};\nint main(void) {\n' >> $@
+ $(AM_V_at)printf 'unsigned i; for (i=0;i<sizeof(functions)/sizeof(void*);i++) printf("%%p\\n", functions[i]);\n' >> $@
+ $(AM_V_at)printf 'return 0; }\n' >> $@
+endef
+
+test-libelogind-sym.c: \
+ $(top_builddir)/src/libelogind/libelogind.sym \
+ src/systemd/sd-daemon.h \
+ src/systemd/sd-login.h \
+ src/systemd/sd-bus.h \
+ src/systemd/sd-event.h
+ $(generate-sym-test)
+
+test_libelogind_sym_SOURCES = \
+ test-libelogind-sym.c
+test_libelogind_sym_LDADD = \
+ libelogind.la
+
+BUILT_SOURCES += \
+ $(test_libelogind_sym_SOURCES)
+
+tests += \
+ test-libelogind-sym
+
+.PHONY: cppcheck
+cppcheck:
+ cppcheck --enable=all -q $(top_srcdir)
+
+# Used to extract compile flags for YCM.
+print-%:
+ @echo $($*)
+
+git-contrib:
+ @git shortlog -s `git describe --abbrev=0`.. | cut -c8- | awk '{ print $$0 "," }' | sort -u
+
+EXTRA_DIST += \
+ tools/gdb-sd_dump_hashmaps.py
+
+list-keys:
+ gpg --verbose --no-options --no-default-keyring --no-auto-key-locate --batch --trust-model=always --keyring=$(srcdir)/src/import/import-pubring.gpg --list-keys
+
+add-key:
+ gpg --verbose --no-options --no-default-keyring --no-auto-key-locate --batch --trust-model=always --keyring=$(srcdir)/src/import/import-pubring.gpg --import -
systemd System and Service Manager
+CHANGES WITH 226:
+
+ * The DHCP implementation of systemd-networkd gained a set of
+ new features:
+
+ - The DHCP server now supports emitting DNS and NTP
+ information. It may be enabled and configured via
+ EmitDNS=, DNS=, EmitNTP=, and NTP=. If transmission of DNS
+ and NTP information is enabled, but no servers are
+ configured, the corresponding uplink information (if there
+ is any) is propagated.
+
+ - Server and client now support transmission and reception
+ of timezone information. It can be configured via the
+ newly introduced network options UseTimezone=,
+ EmitTimezone=, and Timezone=. Transmission of timezone
+ information is enabled between host and containers by
+ default now: the container will change its local timezone
+ to what the host has set.
+
+ - Lease timeouts can now be configured via
+ MaxLeaseTimeSec= and DefaultLeaseTimeSec=.
+
+ - The DHCP server improved on the stability of
+ leases. Clients are more likely to get the same lease
+ information back, even if the server loses state.
+
+ - The DHCP server supports two new configuration options to
+ control the lease address pool metrics, PoolOffset= and
+ PoolSize=.
+
+ * The encapsulation limit of tunnels in systemd-networkd may
+ now be configured via 'EncapsulationLimit='. It allows
+ modifying the maximum additional levels of encapsulation
+ that are permitted to be prepended to a packet.
+
+ * systemd now supports the concept of user buses replacing
+ session buses, if used with dbus-1.10 (and enabled via dbus
+ --enable-user-session). It previously only supported this on
+ kdbus-enabled systems, and this release expands this to
+ 'dbus-daemon' systems.
+
+ * systemd-networkd now supports predictable interface names
+ for virtio devices.
+
+ * systemd now optionally supports the new Linux kernel
+ "unified" control group hierarchy. If enabled via the kernel
+ command-line option 'systemd.unified_cgroup_hierarchy=1',
+ systemd will try to mount the unified cgroup hierarchy
+ directly on /sys/fs/cgroup. If not enabled, or not
+ available, systemd will fall back to the legacy cgroup
+ hierarchy setup, as before. Host system and containers can
+ mix and match legacy and unified hierarchies as they
+ wish. nspawn understands the $UNIFIED_CROUP_HIERARCHY
+ environment variable to individually select the hierarchy to
+ use for executed containers. By default, nspawn will use the
+ unified hierarchy for the containers if the host uses the
+ unified hierarchy, and the legacy hierarchy otherwise.
+ Please note that at this point the unified hierarchy is an
+ experimental kernel feature and is likely to change in one
+ of the next kernel releases. Therefore, it should not be
+ enabled by default in downstream distributions yet. The
+ minimum required kernel version for the unified hierarchy to
+ work is 4.2. Note that when the unified hierarchy is used
+ for the first time delegated access to controllers is
+ safe. Because of this systemd-nspawn containers will get
+ access to controllers now, as will systemd user
+ sessions. This means containers and user sessions may now
+ manage their own resources, partitioning up what the system
+ grants them.
+
+ * A new special scope unit "init.scope" has been introduced
+ that encapsulates PID 1 of the system. It may be used to
+ determine resource usage and enforce resource limits on PID
+ 1 itself. PID 1 hence moved out of the root of the control
+ group tree.
+
+ * The cgtop tool gained support for filtering out kernel
+ threads when counting tasks in a control group. Also, the
+ count of processes is now recursively summed up by
+ default. Two options -k and --recursive= have been added to
+ revert to old behaviour. The tool has also been updated to
+ work correctly in containers now.
+
+ * systemd-nspawn's --bind= and --bind-ro= options have been
+ extended to allow creation of non-recursive bind mounts.
+
+ * libsystemd gained two new calls sd_pid_get_cgroup() and
+ sd_peer_get_cgroup() which return the control group path of
+ a process or peer of a connected AF_UNIX socket. This
+ function call is particularly useful when implementing
+ delegated subtrees support in the control group hierarchy.
+
+ * The "sd-event" event loop API of libsystemd now supports
+ correct dequeuing of real-time signals, without losing
+ signal events.
+
+ * When systemd requests a PolicyKit decision when managing
+ units it will now add additional fields to the request,
+ including unit name and desired operation. This enables more
+ powerful PolicyKit policies, that make decisions depending
+ on these parameters.
+
+ * nspawn learnt support for .nspawn settings files, that may
+ accompany the image files or directories of containers, and
+ may contain additional settings for the container. This is
+ an alternative to configuring container parameters via the
+ nspawn command line.
+
+ Contributions from: Cristian Rodríguez, Daniel Mack, David
+ Herrmann, Eugene Yakubovich, Evgeny Vereshchagin, Filipe
+ Brandenburger, Hans de Goede, Jan Alexander Steffens, Jan
+ Synacek, Kay Sievers, Lennart Poettering, Mangix, Marcel
+ Holtmann, Martin Pitt, Michael Biebl, Michael Chapman, Michal
+ Sekletar, Peter Hutterer, Piotr Drąg, reverendhomer, Robin
+ Hack, Susant Sahani, Sylvain Pasche, Thomas Hindoe Paaboel
+ Andersen, Tom Gundersen, Torstein Husebø
+
+ -- Berlin, 2015-09-08
+
+CHANGES WITH 225:
+
+ * machinectl gained a new verb 'shell' which opens a fresh
+ shell on the target container or the host. It is similar to
+ the existing 'login' command of machinectl, but spawns the
+ shell directly without prompting for username or
+ password. The pseudo machine '.host' now refers to the local
+ host and is used by default. Hence, 'machinectl shell' can
+ be used as replacement for 'su -' which spawns a session as
+ a fresh systemd unit in a way that is fully isolated from
+ the originating session.
+
+ * systemd-networkd learned to cope with private-zone DHCP
+ options and allows other programs to query the values.
+
+ * SELinux access control when enabling/disabling units is no
+ longer enforced with this release. The previous
+ implementation was incorrect, and a new corrected
+ implementation is not yet available. As unit file operations
+ are still protected via PolicyKit and D-Bus policy this is
+ not a security problem. Yet, distributions which care about
+ optimal SELinux support should probably not stabilize on
+ this release.
+
+ * sd-bus gained support for matches of type "arg0has=", that
+ test for membership of strings in string arrays sent in bus
+ messages.
+
+ * systemd-resolved now dumps the contents of its DNS and LLMNR
+ caches to the logs on reception of the SIGUSR1 signal. This
+ is useful to debug DNS behaviour.
+
+ * The coredumpctl tool gained a new --directory= option to
+ operate on journal files in a specific directory.
+
+ * "systemctl reboot" and related commands gained a new
+ "--message=" option which may be used to set a free-text
+ wall message when shutting down or rebooting the
+ system. This message is also logged, which is useful for
+ figuring out the reason for a reboot or shutdown a
+ posteriori.
+
+ * The "systemd-resolve-host" tool's -i switch now takes
+ network interface numbers as alternative to interface names.
+
+ * A new unit file setting for services has been introduced:
+ UtmpMode= allows configuration of how precisely systemd
+ handles utmp and wtmp entries for the service if this is
+ enabled. This allows writing services that appear similar to
+ user sessions in the output of the "w", "who", "last" and
+ "lastlog" tools.
+
+ * systemd-resolved will now locally synthesize DNS resource
+ records for the "localhost" and "gateway" domains as well as
+ the local hostname. This should ensure that clients querying
+ RRs via resolved will get similar results as those going via
+ NSS, if nss-myhostname is enabled.
+
+ Contributions from: Alastair Hughes, Alex Crawford, Daniel
+ Mack, David Herrmann, Dimitri John Ledkov, Eric Kostrowski,
+ Evgeny Vereshchagin, Felipe Sateler, HATAYAMA Daisuke, Jan
+ Pokorný, Jan Synacek, Johnny Robeson, Karel Zak, Kay Sievers,
+ Kefeng Wang, Lennart Poettering, Major Hayden, Marcel
+ Holtmann, Markus Elfring, Martin Mikkelsen, Martin Pitt, Matt
+ Turner, Maxim Mikityanskiy, Michael Biebl, Namhyung Kim,
+ Nicolas Cornu, Owen W. Taylor, Patrik Flykt, Peter Hutterer,
+ reverendhomer, Richard Maw, Ronny Chevalier, Seth Jennings,
+ Stef Walter, Susant Sahani, Thomas Blume, Thomas Hindoe
+ Paaboel Andersen, Thomas Meyer, Tom Gundersen, Vincent Batts,
+ WaLyong Cho, Zbigniew Jędrzejewski-Szmek
+
+ -- Berlin, 2015-08-27
+
+CHANGES WITH 224:
+
+ * The systemd-efi-boot-generator functionality was merged into
+ systemd-gpt-auto-generator.
+
+ * systemd-networkd now supports Group Policy for vxlan
+ devices. It can be enabled via the new boolean configuration
+ option called 'GroupPolicyExtension='.
+
+ Contributions from: Andreas Kempf, Christian Hesse, Daniel Mack, David
+ Herrmann, Herman Fries, Johannes Nixdorf, Kay Sievers, Lennart
+ Poettering, Peter Hutterer, Susant Sahani, Tom Gundersen
+
+ -- Berlin, 2015-07-31
+
+CHANGES WITH 223:
+
+ * The python-systemd code has been removed from the systemd repository.
+ A new repository has been created which accommodates the code from
+ now on, and we kindly ask distributions to create a separate package
+ for this: https://github.com/systemd/python-systemd
+
+ * The systemd daemon will now reload its main configuration
+ (/etc/systemd/system.conf) on daemon-reload.
+
+ * sd-dhcp now exposes vendor specific extensions via
+ sd_dhcp_lease_get_vendor_specific().
+
+ * systemd-networkd gained a number of new configuration options.
+
+ - A new boolean configuration option for TAP devices called
+ 'VNetHeader='. If set, the IFF_VNET_HDR flag is set for the
+ device, thus allowing to send and receive GSO packets.
+
+ - A new tunnel configuration option called 'CopyDSCP='.
+ If enabled, the DSCP field of ip6 tunnels is copied into the
+ decapsulated packet.
+
+ - A set of boolean bridge configuration options were added.
+ 'UseBPDU=', 'HairPin=', 'FastLeave=', 'AllowPortToBeRoot=',
+ and 'UnicastFlood=' are now parsed by networkd and applied to the
+ respective bridge link device via the respective IFLA_BRPORT_*
+ netlink attribute.
+
+ - A new string configuration option to override the hostname sent
+ to a DHCP server, called 'Hostname='. If set and 'SendHostname='
+ is true, networkd will use the configured hostname instead of the
+ system hostname when sending DHCP requests.
+
+ - A new tunnel configuration option called 'IPv6FlowLabel='. If set,
+ networkd will configure the IPv6 flow-label of the tunnel device
+ according to RFC2460.
+
+ - The 'macvtap' virtual network devices are now supported, similar to
+ the already supported 'macvlan' devices.
+
+ * systemd-resolved now implements RFC5452 to improve resilience against
+ cache poisoning. Additionally, source port randomization is enabled
+ by default to further protect against DNS spoofing attacks.
+
+ * nss-mymachines now supports translating UIDs and GIDs of running
+ containers with user-namespaces enabled. If a container 'foo'
+ translates a host uid 'UID' to the container uid 'TUID', then
+ nss-mymachines will also map uid 'UID' to/from username 'vu-foo-TUID'
+ (with 'foo' and 'TUID' replaced accordingly). Similarly, groups are
+ mapped as 'vg-foo-TGID'.
+
+ Contributions from: Beniamino Galvani, cee1, Christian Hesse, Daniel
+ Buch, Daniel Mack, daurnimator, David Herrmann, Dimitri John Ledkov,
+ HATAYAMA Daisuke, Ivan Shapovalov, Jan Alexander Steffens (heftig),
+ Johan Ouwerkerk, Jose Carlos Venegas Munoz, Karel Zak, Kay Sievers,
+ Lennart Poettering, Lidong Zhong, Martin Pitt, Michael Biebl, Michael
+ Olbrich, Michal Schmidt, Michal Sekletar, Mike Gilbert, Namhyung Kim,
+ Nick Owens, Peter Hutterer, Richard Maw, Steven Allen, Sungbae Yoo,
+ Susant Sahani, Thomas Blume, Thomas Hindoe Paaboel Andersen, Tom
+ Gundersen, Torstein Husebø, Umut Tezduyar Lindskog, Vito Caputo,
+ Vivenzio Pagliari, Zbigniew Jędrzejewski-Szmek
+
+ -- Berlin, 2015-07-29
+
+CHANGES WITH 222:
+
+ * udev does not longer support the WAIT_FOR_SYSFS= key in udev rules.
+ There are no known issues with current sysfs, and udev does not need
+ or should be used to work around such bugs.
+
+ * udev does no longer enable USB HID power management. Several reports
+ indicate, that some devices cannot handle that setting.
+
+ * The udev accelerometer helper was removed. The functionality
+ is now fully included in iio-sensor-proxy. But this means,
+ older iio-sensor-proxy versions will no longer provide
+ accelerometer/orientation data with this systemd version.
+ Please upgrade iio-sensor-proxy to version 1.0.
+
+ * networkd gained a new configuration option IPv6PrivacyExtensions=
+ which enables IPv6 privacy extensions (RFC 4941, "Privacy Extensions
+ for Stateless Address") on selected networks.
+
+ * For the sake of fewer build-time dependencies and less code in the
+ main repository, the python bindings are about to be removed in the
+ next release. A new repository has been created which accommodates
+ the code from now on, and we kindly ask distributions to create a
+ separate package for this. The removal will take place in v223.
+
+ https://github.com/systemd/python-systemd
+
+ Contributions from: Abdo Roig-Maranges, Andrew Eikum, Bastien Nocera,
+ Cédric Delmas, Christian Hesse, Christos Trochalakis, Daniel Mack,
+ daurnimator, David Herrmann, Dimitri John Ledkov, Eric Biggers, Eric
+ Cook, Felipe Sateler, Geert Jansen, Gerd Hoffmann, Gianpaolo Macario,
+ Greg Kroah-Hartman, Iago López Galeiras, Jan Alexander Steffens
+ (heftig), Jan Engelhardt, Jay Strict, Kay Sievers, Lennart Poettering,
+ Markus Knetschke, Martin Pitt, Michael Biebl, Michael Marineau, Michal
+ Sekletar, Miguel Bernal Marin, Peter Hutterer, Richard Maw, rinrinne,
+ Susant Sahani, Thomas Hindoe Paaboel Andersen, Tom Gundersen, Torstein
+ Husebø, Vedran Miletić, WaLyong Cho, Zbigniew Jędrzejewski-Szmek
+
+ -- Berlin, 2015-07-07
+
+CHANGES WITH 221:
+
+ * The sd-bus.h and sd-event.h APIs have now been declared
+ stable and have been added to the official interface of
+ libsystemd.so. sd-bus implements an alternative D-Bus client
+ library, that is relatively easy to use, very efficient and
+ supports both classic D-Bus as well as kdbus as transport
+ backend. sd-event is a generic event loop abstraction that
+ is built around Linux epoll, but adds features such as event
+ prioritization or efficient timer handling. Both APIs are good
+ choices for C programs looking for a bus and/or event loop
+ implementation that is minimal and does not have to be
+ portable to other kernels.
+
+ * kdbus support is no longer compile-time optional. It is now
+ always built-in. However, it can still be disabled at
+ runtime using the kdbus=0 kernel command line setting, and
+ that setting may be changed to default to off, by specifying
+ --disable-kdbus at build-time. Note though that the kernel
+ command line setting has no effect if the kdbus.ko kernel
+ module is not installed, in which case kdbus is (obviously)
+ also disabled. We encourage all downstream distributions to
+ begin testing kdbus by adding it to the kernel images in the
+ development distributions, and leaving kdbus support in
+ systemd enabled.
+
+ * The minimal required util-linux version has been bumped to
+ 2.26.
+
+ * Support for chkconfig (--enable-chkconfig) was removed in
+ favor of calling an abstraction tool
+ /lib/systemd/systemd-sysv-install. This needs to be
+ implemented for your distribution. See "SYSV INIT.D SCRIPTS"
+ in README for details.
+
+ * If there's a systemd unit and a SysV init script for the
+ same service name, and the user executes "systemctl enable"
+ for it (or a related call), then this will now enable both
+ (or execute the related operation on both), not just the
+ unit.
+
+ * The libudev API documentation has been converted from gtkdoc
+ into man pages.
+
+ * gudev has been removed from the systemd tree, it is now an
+ external project.
+
+ * The systemd-cgtop tool learnt a new --raw switch to generate
+ "raw" (machine parsable) output.
+
+ * networkd's IPForwarding= .network file setting learnt the
+ new setting "kernel", which ensures that networkd does not
+ change the IP forwarding sysctl from the default kernel
+ state.
+
+ * The systemd-logind bus API now exposes a new boolean
+ property "Docked" that reports whether logind considers the
+ system "docked", i.e. connected to a docking station or not.
+
+ Contributions from: Alex Crawford, Andreas Pokorny, Andrei
+ Borzenkov, Charles Duffy, Colin Guthrie, Cristian Rodríguez,
+ Daniele Medri, Daniel Hahler, Daniel Mack, David Herrmann,
+ David Mohr, Dimitri John Ledkov, Djalal Harouni, dslul, Ed
+ Swierk, Eric Cook, Filipe Brandenburger, Gianpaolo Macario,
+ Harald Hoyer, Iago López Galeiras, Igor Vuk, Jan Synacek,
+ Jason Pleau, Jason S. McMullan, Jean Delvare, Jeff Huang,
+ Jonathan Boulle, Karel Zak, Kay Sievers, kloun, Lennart
+ Poettering, Marc-Antoine Perennou, Marcel Holtmann, Mario
+ Limonciello, Martin Pitt, Michael Biebl, Michael Olbrich,
+ Michal Schmidt, Mike Gilbert, Nick Owens, Pablo Lezaeta Reyes,
+ Patrick Donnelly, Pavel Odvody, Peter Hutterer, Philip
+ Withnall, Ronny Chevalier, Simon McVittie, Susant Sahani,
+ Thomas Hindoe Paaboel Andersen, Tom Gundersen, Torstein
+ Husebø, Umut Tezduyar Lindskog, Viktar Vauchkevich, Werner
+ Fink, Zbigniew Jędrzejewski-Szmek
+
+ -- Berlin, 2015-06-19
+
+CHANGES WITH 220:
+
+ * The gudev library has been extracted into a separate repository
+ available at: https://git.gnome.org/browse/libgudev/
+ It is now managed as part of the Gnome project. Distributions
+ are recommended to pass --disable-gudev to systemd and use
+ gudev from the Gnome project instead. gudev is still included
+ in systemd, for now. It will be removed soon, though. Please
+ also see the announcement-thread on systemd-devel:
+ http://lists.freedesktop.org/archives/systemd-devel/2015-May/032070.html
+
+ * systemd now exposes a CPUUsageNSec= property for each
+ service unit on the bus, that contains the overall consumed
+ CPU time of a service (the sum of what each process of the
+ service consumed). This value is only available if
+ CPUAccounting= is turned on for a service, and is then shown
+ in the "systemctl status" output.
+
+ * Support for configuring alternative mappings of the old SysV
+ runlevels to systemd targets has been removed. They are now
+ hardcoded in a way that runlevels 2, 3, 4 all map to
+ multi-user.target and 5 to graphical.target (which
+ previously was already the default behaviour).
+
+ * The auto-mounter logic gained support for mount point
+ expiry, using a new TimeoutIdleSec= setting in .automount
+ units. (Also available as x-systemd.idle-timeout= in /etc/fstab).
+
+ * The EFI System Partition (ESP) as mounted to /boot by
+ systemd-efi-boot-generator will now be unmounted
+ automatically after 2 minutes of not being used. This should
+ minimize the risk of ESP corruptions.
+
+ * New /etc/fstab options x-systemd.requires= and
+ x-systemd.requires-mounts-for= are now supported to express
+ additional dependencies for mounts. This is useful for
+ journalling file systems that support external journal
+ devices or overlay file systems that require underlying file
+ systems to be mounted.
+
+ * systemd does not support direct live-upgrades (via systemctl
+ daemon-reexec) from versions older than v44 anymore. As no
+ distribution we are aware of shipped such old versions in a
+ stable release this should not be problematic.
+
+ * When systemd forks off a new per-connection service instance
+ it will now set the $REMOTE_ADDR environment variable to the
+ remote IP address, and $REMOTE_PORT environment variable to
+ the remote IP port. This behaviour is similar to the
+ corresponding environment variables defined by CGI.
+
+ * systemd-networkd gained support for uplink failure
+ detection. The BindCarrier= option allows binding interface
+ configuration dynamically to the link sense of other
+ interfaces. This is useful to achieve behaviour like in
+ network switches.
+
+ * systemd-networkd gained support for configuring the DHCP
+ client identifier to use when requesting leases.
+
+ * systemd-networkd now has a per-network UseNTP= option to
+ configure whether NTP server information acquired via DHCP
+ is passed on to services like systemd-timesyncd.
+
+ * systemd-networkd gained support for vti6 tunnels.
+
+ * Note that systemd-networkd manages the sysctl variable
+ /proc/sys/net/ipv[46]/conf/*/forwarding for each interface
+ it is configured for since v219. The variable controls IP
+ forwarding, and is a per-interface alternative to the global
+ /proc/sys/net/ipv[46]/ip_forward. This setting is
+ configurable in the IPForward= option, which defaults to
+ "no". This means if networkd is used for an interface it is
+ no longer sufficient to set the global sysctl option to turn
+ on IP forwarding! Instead, the .network file option
+ IPForward= needs to be turned on! Note that the
+ implementation of this behaviour was broken in v219 and has
+ been fixed in v220.
+
+ * Many bonding and vxlan options are now configurable in
+ systemd-networkd.
+
+ * systemd-nspawn gained a new --property= setting to set unit
+ properties for the container scope. This is useful for
+ setting resource parameters (e.g "CPUShares=500") on
+ containers started from the command line.
+
+ * systemd-nspawn gained a new --private-users= switch to make
+ use of user namespacing available on recent Linux kernels.
+
+ * systemd-nspawn may now be called as part of a shell pipeline
+ in which case the pipes used for stdin and stdout are passed
+ directly to the process invoked in the container, without
+ indirection via a pseudo tty.
+
+ * systemd-nspawn gained a new switch to control the UNIX
+ signal to use when killing the init process of the container
+ when shutting down.
+
+ * systemd-nspawn gained a new --overlay= switch for mounting
+ overlay file systems into the container using the new kernel
+ overlayfs support.
+
+ * When a container image is imported via systemd-importd and
+ the host file system is not btrfs, a loopback block device
+ file is created in /var/lib/machines.raw with a btrfs file
+ system inside. It is then mounted to /var/lib/machines to
+ enable btrfs features for container management. The loopback
+ file and btrfs file system is grown as needed when container
+ images are imported via systemd-importd.
+
+ * systemd-machined/systemd-importd gained support for btrfs
+ quota, to enforce container disk space limits on disk. This
+ is exposed in "machinectl set-limit".
+
+ * systemd-importd now can import containers from local .tar,
+ .raw and .qcow2 images, and export them to .tar and .raw. It
+ can also import dkr v2 images now from the network (on top
+ of v1 as before).
+
+ * systemd-importd gained support for verifying downloaded
+ images with gpg2 (previously only gpg1 was supported).
+
+ * systemd-machined, systemd-logind, systemd: most bus calls
+ are now accessible to unprivileged processes via
+ PolicyKit. Also, systemd-logind will now allow users to kill
+ their own sessions without further privileges or
+ authorization.
+
+ * systemd-shutdownd has been removed. This service was
+ previously responsible for implementing scheduled shutdowns
+ as exposed in /usr/bin/shutdown's time parameter. This
+ functionality has now been moved into systemd-logind and is
+ accessible via a bus interface.
+
+ * "systemctl reboot" gained a new switch --firmware-setup that
+ can be used to reboot into the EFI firmware setup, if that
+ is available. systemd-logind now exposes an API on the bus
+ to trigger such reboots, in case graphical desktop UIs want
+ to cover this functionality.
+
+ * "systemctl enable", "systemctl disable" and "systemctl mask"
+ now support a new "--now" switch. If specified the units
+ that are enabled will also be started, and the ones
+ disabled/masked also stopped.
+
+ * The Gummiboot EFI boot loader tool has been merged into
+ systemd, and renamed to "systemd-boot". The bootctl tool has been
+ updated to support systemd-boot.
+
+ * An EFI kernel stub has been added that may be used to create
+ kernel EFI binaries that contain not only the actual kernel,
+ but also an initrd, boot splash, command line and OS release
+ information. This combined binary can then be signed as a
+ single image, so that the firmware can verify it all in one
+ step. systemd-boot has special support for EFI binaries created
+ like this and can extract OS release information from them
+ and show them in the boot menu. This functionality is useful
+ to implement cryptographically verified boot schemes.
+
+ * Optional support has been added to systemd-fsck to pass
+ fsck's progress report to an AF_UNIX socket in the file
+ system.
+
+ * udev will no longer create device symlinks for all block
+ devices by default. A blacklist for excluding special block
+ devices from this logic has been turned into a whitelist
+ that requires picking block devices explicitly that require
+ device symlinks.
+
+ * A new (currently still internal) API sd-device.h has been
+ added to libsystemd. This modernized API is supposed to
+ replace libudev eventually. In fact, already much of libudev
+ is now just a wrapper around sd-device.h.
+
+ * A new hwdb database for storing metadata about pointing
+ stick devices has been added.
+
+ * systemd-tmpfiles gained support for setting file attributes
+ similar to the "chattr" tool with new 'h' and 'H' lines.
+
+ * systemd-journald will no longer unconditionally set the
+ btrfs NOCOW flag on new journal files. This is instead done
+ with tmpfiles snippet using the new 'h' line type. This
+ allows easy disabling of this logic, by masking the
+ journal-nocow.conf tmpfiles file.
+
+ * systemd-journald will now translate audit message types to
+ human readable identifiers when writing them to the
+ journal. This should improve readability of audit messages.
+
+ * The LUKS logic gained support for the offset= and skip=
+ options in /etc/crypttab, as previously implemented by
+ Debian.
+
+ * /usr/lib/os-release gained a new optional field VARIANT= for
+ distributions that support multiple variants (such as a
+ desktop edition, a server edition, ...)
+
+ Contributions from: Aaro Koskinen, Adam Goode, Alban Crequy,
+ Alberto Fanjul Alonso, Alexander Sverdlin, Alex Puchades, Alin
+ Rauta, Alison Chaiken, Andrew Jones, Arend van Spriel,
+ Benedikt Morbach, Benjamin Franzke, Benjamin Tissoires, Blaž
+ Tomažič, Chris Morgan, Chris Morin, Colin Walters, Cristian
+ Rodríguez, Daniel Buch, Daniel Drake, Daniele Medri, Daniel
+ Mack, Daniel Mustieles, daurnimator, Davide Bettio, David
+ Herrmann, David Strauss, Didier Roche, Dimitri John Ledkov,
+ Eric Cook, Gavin Li, Goffredo Baroncelli, Hannes Reinecke,
+ Hans de Goede, Hans-Peter Deifel, Harald Hoyer, Iago López
+ Galeiras, Ivan Shapovalov, Jan Engelhardt, Jan Janssen, Jan
+ Pazdziora, Jan Synacek, Jasper St. Pierre, Jay Faulkner, John
+ Paul Adrian Glaubitz, Jonathon Gilbert, Karel Zak, Kay
+ Sievers, Koen Kooi, Lennart Poettering, Lubomir Rintel, Lucas
+ De Marchi, Lukas Nykryn, Lukas Rusak, Lukasz Skalski, Łukasz
+ Stelmach, Mantas Mikulėnas, Marc-Antoine Perennou, Marcel
+ Holtmann, Martin Pitt, Mathieu Chevrier, Matthew Garrett,
+ Michael Biebl, Michael Marineau, Michael Olbrich, Michal
+ Schmidt, Michal Sekletar, Mirco Tischler, Nir Soffer, Patrik
+ Flykt, Pavel Odvody, Peter Hutterer, Peter Lemenkov, Peter
+ Waller, Piotr Drąg, Raul Gutierrez S, Richard Maw, Ronny
+ Chevalier, Ross Burton, Sebastian Rasmussen, Sergey Ptashnick,
+ Seth Jennings, Shawn Landden, Simon Farnsworth, Stefan Junker,
+ Stephen Gallagher, Susant Sahani, Sylvain Plantefève, Thomas
+ Haller, Thomas Hindoe Paaboel Andersen, Tobias Hunger, Tom
+ Gundersen, Torstein Husebø, Umut Tezduyar Lindskog, Will
+ Woods, Zachary Cook, Zbigniew Jędrzejewski-Szmek
+
+ -- Berlin, 2015-05-22
+
CHANGES WITH 219:
* Introduce a new API "sd-hwdb.h" for querying the hardware
decompress bz2, xz, gzip compressed downloads if necessary,
and restore sparse files on disk. The daemon uses privilege
separation to ensure the actual download logic runs with
- fewer privileges than the deamon itself. machinectl has
+ fewer privileges than the daemon itself. machinectl has
gained new commands "pull-tar", "pull-raw" and "pull-dkr" to
make the functionality of importd available to the
user. With this in place the Fedora and Ubuntu "Cloud"
* systemd now provides a way to store file descriptors
per-service in PID 1.This is useful for daemons to ensure
that fds they require are not lost during a daemon
- restart. The fds are passed to the deamon on the next
+ restart. The fds are passed to the daemon on the next
invocation in the same way socket activation fds are
passed. This is now used by journald to ensure that the
various sockets connected to all the system's stdout/stderr
* When querying unit file enablement status (for example via
"systemctl is-enabled"), a new state "indirect" is now known
which indicates that a unit might not be enabled itself, but
- another unit listed in its Alias= setting might be.
+ another unit listed in its Also= setting might be.
* Similar to the various existing ConditionXYZ= settings for
units there are now matching AssertXYZ= settings. While
* HandleSleepKey= in logind.conf has been split up into
HandleSuspendKey= and HandleHibernateKey=. The old setting
is not available anymore. X11 and the kernel are
- distuingishing between these keys and we should too. This
+ distinguishing between these keys and we should too. This
also means the inhibition lock for these keys has been split
into two.
* Various functionality updates to libsystemd-login.so
- * Track class of PAM logins to distuingish greeters from
+ * Track class of PAM logins to distinguish greeters from
normal user logins.
Contributions from: Kay Sievers, Lennart Poettering, Michael
To contribute to elogind, fork the current source code from github:
- https://github.com/wingo/elogind
+ https://github.com/elogind/elogind
Send a pull request for the changes you like.
Finally, bug reports:
- https://github.com/wingo/elogind/issues
+ https://github.com/elogind/elogind/issues
Why bother?
-----------
Elogind does monitor power button and the lid switch, like systemd,
but instead of doing RPC to systemd to suspend, poweroff, or restart
-the machine, elogind just does this directly. For suspend, hybernate,
+the machine, elogind just does this directly. For suspend, hibernate,
and hybrid-sleep, elogind uses the same code as systemd-sleep.
Instead of using a separate sleep.conf file to configure the sleep
behavior, this is included in the [Sleep] section of
automount points even when the original .automount file did not exist
anymore. Only the .mount unit was still around.
-* ExecStart with unicode characters fails in strv_split_quoted:
+* ExecStart with unicode characters fails in strv_split_extract:
[Service]
Environment=ONE='one' "TWO='two two' too" THREE=
ExecStart=/bin/python3 -c 'import sys;print(sys.argv)' $ONE $TWO $THREE
-* MEMORY return code is overloaded for syntax errors in the command line.
- str_split_quoted() should return a real return code, so spawn_child can
- report the failure properly.
-
* When systemctl --host is used, underlying ssh connection can remain open.
bus_close does not kill children?
* Fedora: add an rpmlint check that verifies that all unit files in the RPM are listed in %systemd_post macros.
-* Fedora: move kernel image to /usr/lib/modules/, kernel-install will take care of populating /boot
-
-* Fedora: remove /etc/resolv.conf tmpfiles hack
-
* wiki: update journal format documentation for lz4 additions
* When lz4 gets an API for lz4 command output, make use of it to
compress coredumps in a way compatible with /usr/bin/lz4.
-Before 220:
+Features:
-* rework fsckd/fsck's connection logic or remove fsck entirely
+* sd-event: maybe add support for inotify events
-* fix userns support in nspawn, or remove it entirely
+* PID 1 should send out sd_notify("WATCHDOG=1") messages (for usage in the --user mode, and when run via nspawn)
-* make unmount-on-eject work again
+* nspawn should send out sd_notify("WATCHDOG=1") messages
-* bus-proxy: GetConnectionSELinuxSecurityContext() is completely broken
+* nspawn should optionally support receiving WATCHDOG=1 messages from its payload PID 1...
-* logind: make sure the syncrhonous polkit checks are not interactive, i.e. supporess client side interactive bus message header flag for them
+* consider throwing a warning if a service declares it wants to be "Before=" a .device unit.
-* timer units triggering services with failing conditions run busy:
- http://lists.freedesktop.org/archives/systemd-devel/2015-April/030095.html
+* "systemctl edit" should know a mode to create a new unit file
-Features:
+* there's probably something wrong with having user mounts below /sys,
+ as we have for debugfs. for exmaple, src/core/mount.c handles mounts
+ prefixed with /sys generally special.
+ http://lists.freedesktop.org/archives/systemd-devel/2015-June/032962.html
-* the default stop timeout for units is not documented anywhere.
+* Add PassEnvironment= setting to service units, to import select env vars from PID 1 into the service env block
-* .timer units should optionally support CLOCK_BOOTTIME in addition to CLOCK_MONOTONIC
+* nspawn: fix logic always print a final newline on output.
+ https://github.com/systemd/systemd/pull/272#issuecomment-113153176
+
+* make nspawn's --network-veth switch more powerful:
+ http://lists.freedesktop.org/archives/systemd-devel/2015-June/033121.html
+
+* man: document that unless you use StandardError=null the shell >/dev/stderr won't work in shell scripts in services
+
+* man: clarify that "machinectl show" shows different information than "machinectl status" (no cgroup tree, no IP addresses, ...)
+
+* "systemctl daemon-reload" should result in /etc/systemd/system.conf being reloaded by systemd
+
+* install: include generator dirs in unit file search paths
+
+* stop using off_t, it's a crazy type. Use uint64_t instead.
+
+* logind: follow PropertiesChanged state more closely, to deal with quick logouts and relogins
+
+* invent a better systemd-run scheme for naming scopes, that works with remoting
+
+* add journalctl -H that talks via ssh to a remote peer and passes through binary logs data
+
+* change journalctl -M to acquire fd to journal directory via machined, and then operate on that via openat() instead of absolute paths
+
+* add a version of --merge which also merges /var/log/journal/remote
+
+* log accumulated resource usage after each service invocation
+
+* nspawn: a nice way to boot up without machine id set, so that it is set at boot automatically for supporting --ephemeral. Maybe hash the host machine id together with the machine name to generate the machine id for the container
+
+* logind: rename session scope so that it includes the UID. THat way
+ the session scope can be arranged freely in slices and we don't have
+ make assumptions about their slice anymore.
+
+* journalctl: -m should access container journals directly by enumerating them via machined, and also watch containers coming and going. Benefit: nspawn --ephemeral would start working nicely with the journal.
+
+* nspawn: don't copy /etc/resolv.conf from host into container unless we are in shared-network mode
+
+* nspawn: optionally automatically add FORWARD rules to iptables whenever nspawn is running, remove them when shut down.
-* rm_rf() should be able to remove subvolumes
+* importd: generate a nice warning if mkfs.btrfs is missing
-* systemd-run should support a mode where we wait for the unit to be started up
+* nspawn: add a logic for cleaning up read-only, hidden container images in /var/lib/machines that are not ancestors of any non-hidden containers
+
+* nspawn: Improve error message when --bind= is used on a non-existing source directory
+
+* nspawn: maybe make copying of /etc/resolv.conf optional, and skip it if --read-only is used
+
+* man: document how update dkr images works with machinectl
+ http://lists.freedesktop.org/archives/systemd-devel/2015-February/028630.html
+
+* nspawn: as soon as networkd has a bus interface, hook up --network-interface=, --network-bridge= with networkd, to trigger netdev creation should an interface be missing
+
+* rework C11 utf8.[ch] to use char32_t instead of uint32_t when referring
+ to unicode chars, to make things more expressive.
+
+* "machinectl migrate" or similar to copy a container from or to a
+ difference host, via ssh
+
+* tmpfiles: creating new directories/subvolumes/fifos/device nodes
+ should not follow symlinks. None of the other adjustment or creation
+ calls follow symlinks.
+
+* fstab-generator: default to tmpfs-as-root if only usr= is specified on the kernel cmdline
+
+* docs: bring http://www.freedesktop.org/wiki/Software/systemd/MyServiceCantGetRealtime up to date
+
+* mounting and unmounting mount points manually with different source
+ devices will result in collected collected on all devices used.
+ http://lists.freedesktop.org/archives/systemd-devel/2015-April/030225.html
+
+* add a job mode that will fail if a transaction would mean stopping
+ running units. Use this in timedated to manage the NTP service
+ state.
+ http://lists.freedesktop.org/archives/systemd-devel/2015-April/030229.html
+
+* Maybe add support for the equivalent of "ethtool advertise" to .link files?
+ http://lists.freedesktop.org/archives/systemd-devel/2015-April/030112.html
+
+* .timer units should optionally support CLOCK_BOOTTIME in addition to CLOCK_MONOTONIC
* create a btrfs qgroup for /var/lib/machines, and add all container
subvolumes we create to it.
-* nspawn: add --overlay= to support overlay file systems, similar to
- --tmpfs= and --bind=.
-
* When logging about multiple units (stopping BoundTo units, conflicts, etc.),
log both units as UNIT=, so that journalctl -u triggers on both.
that are not supported...
http://lists.freedesktop.org/archives/systemd-devel/2015-February/028076.html
-* PID 1: when invoking systemctl preset-all on first boots, operate in
- an exclusively additive way, i.e. never remove any pre-existing
- symlinks, only add new ones.
-
* Introduce $LISTEN_NAMES to complement $LISTEN_FDS, containing a
colon separated list of identifiers for the fds passed.
-* when the fstab-generator runs in the initrd, it should create a /dev/null mask for systemd-fsck-root.service, to avoid that the the root fs is fsck'ed twice.
-
* maybe introduce WantsMountsFor=? Usecase:
http://lists.freedesktop.org/archives/systemd-devel/2015-January/027729.html
* logind: maybe allow configuration of the StopTimeout for session scopes
-* Set NoNewPriviliges= on all of our own services, where that makes sense
+* Set NoNewPrivileges= on all of our own services, where that makes sense
* Rework systemctl's GetAll property parsing to use the generic bus_map_all_properties() API
* "machinectl list-images" should show os-release data, as well as machine-info data (including deployment level)
-* nspawn: when start a container "foobar" look for its configuration in a file "foobar.nspawn" in /etc/systemd/nspawn/ as well as next to the actualy directory or image to boot
-
* Port various tools to make use of verbs.[ch], where applicable
* "machinectl history"
* figure out when we can use the coarse timers
-* sd-resolve: drop res_query wrapping, people should call via the bus to resolved instead
-
* add "systemctl start -v foobar.service" that shows logs of a service
while the start command runs. This is non-trivial to do without
races though, since we should flush out all journal messages before
* maybe add support for specifier expansion in user.conf, specifically DefaultEnvironment=
-* code cleanup: retire FOREACH_WORD_QUOTED, port to unquote_first_word() loops instead
+* code cleanup: retire FOREACH_WORD_QUOTED, port to extract_first_word() loops instead
* introduce systemd-timesync-wait.service or so to sync on an NTP fix?
* exponential backoff in timesyncd and resolved when we cannot reach a server
-* unquote_many_words() should probably be used by a lot of code that
+* extract_many_words() should probably be used by a lot of code that
currently uses FOREACH_WORD and friends. For example, most conf
parsing callbacks should use it.
- add LLDP client side support
- the DHCP lease data (such as NTP/DNS) is still made available when
a carrier is lost on a link. It should be removed instantly.
- - .network setting that allows overriding of the hostname to send to the dhcp server
- http://lists.freedesktop.org/archives/systemd-devel/2014-July/021550.html
- expose in the API the following bits:
- option 15, domain name and/or option 119, search list
- option 12, host name and/or option 81, fqdn
- - option 100, 101, timezone
- option 123, 144, geolocation
- option 252, configure http proxy (PAC/wpad)
- - networkd's dhcp server should transparently pass on the DNS and
- NTP server list it got from user configuration and its dhcp client
- to clients. It should also pass on its own timezone information.
- provide a way to define a per-network interface default metric value
for all routes to it. possibly a second default for DHCP routes.
- allow Name= to be specified repeatedly in the [Match] section. Maybe also
support Name=foo*|bar*|baz ?
- duplicate address check for static IPs (like ARPCHECK in network-scripts)
+ - allow DUID/IAID to be customized, see issue #394.
+ - support configuration option for TSO (tcp segmentation offload)
+ - networkd: whenever uplink info changes, make DHCP server send out FORCERENEW
* resolved:
- put networkd events and rtnl events at a higher priority, so that
we always process them before we process client requests
- DNSSEC
- - use base64 for key presentation?
- add display of private key types (http://tools.ietf.org/html/rfc4034#appendix-A.1.1)?
- - add nice formatting of DNS timestamps
- DNS
- search paths
- mDNS/DNS-SD
- avahi compat
- DNS-SD service registration from socket units
- edns0
- - dname
+ - dname: Not necessary for plain DNS as synthesized cname is handed out instead if we do not
+ announce dname support. However, for DNSSEC it is necessary as the synthesized cname
+ will not be signed.
- cname on PTR (?)
- - maybe randomize DNS UDP source ports
- - maybe compare query section of DNS replies
* Allow multiple ExecStart= for all Type= settings, so that we can cover rescue.service nicely
ReadOnlyDirectories=... for whitelisting files for a service.
* sd-bus:
+ - EBADSLT handling
- GetAllProperties() on a non-existing object does not result in a failure currently
- kdbus: process fd=-1 for incoming msgs
- port to sd-resolve for connecting to TCP dbus servers
* maybe do not install getty@tty1.service symlink in /etc but in /usr?
-* re-enable "make check" for gtk-doc (broken for unknown reason)
-
* fstab: add new mount option x-systemd-after=/foobar/waldo to allow manual dependencies to other mount points
https://bugzilla.redhat.com/show_bug.cgi?id=812826
* If we show an error about a unit (such as not showing up) and it has no Description string, then show a description string generated form the reverse of unit_name_mangle().
-* fedup: add --unit to systemctl switch-root somehow
-* fedup: do not delete initrd on switch-root
-* fedup: generator
-
* clean up date formatting and parsing so that all absolute/relative timestamps we format can also be parsed
* on shutdown: move utmp, wall, audit logic all into PID 1 (or logind?), get rid of systemd-update-utmp-runlevel
* when breaking cycles drop sysv services first, then services from /run, then from /etc, then from /usr
-* automount: implement expire:
- - set superblock timeout AUTOFS_DEV_IOCTL_TIMEOUT_CMD
- - periodically run AUTOFS_DEV_IOCTL_EXPIRE_CMD
- - every timeout/4 (original autofs logic)
- - blocking, needs a thread
- - run until -EAGAIN
- - receive expire packet on pipe if kernel tells the timeout is over
- - call umount
- - answer expire packet on pipe with AUTOFS_DEV_IOCTL_{READY,FAIL}_CMD
- - AUTOFS_DEV_IOCTL_EXPIRE_CMD returns
-
* ExecOnFailure=/usr/bin/foo
* udev:
* dhcp:
- figure out how much we can increase Maximum Message Size
- - export timezone information
- support RFC4702 (pass FQDN)
* dhcp6:
topdir=$(dirname $0)
cd $topdir
-if [ -f .git/hooks/pre-commit.sample ] && [ ! -f .git/hooks/pre-commit ]; then
- # This part is allowed to fail
- cp -p .git/hooks/pre-commit.sample .git/hooks/pre-commit && \
- chmod +x .git/hooks/pre-commit && \
- echo "Activated pre-commit hook." || :
-fi
-
-if which gtkdocize >/dev/null 2>/dev/null; then
- gtkdocize --docdir docs/ --flavour no-tmpl
- gtkdocargs=--enable-gtk-doc
-else
- echo "You don't have gtk-doc installed, and thus won't be able to generate the documentation."
- rm -f docs/gtk-doc.make
- echo 'EXTRA_DIST =' > docs/gtk-doc.make
-fi
-
+# We do not need this, we are not systemd upstream!
+#if [ -f .git/hooks/pre-commit.sample ] && [ ! -f .git/hooks/pre-commit ]; then
+# # This part is allowed to fail
+# cp -p .git/hooks/pre-commit.sample .git/hooks/pre-commit && \
+# chmod +x .git/hooks/pre-commit && \
+# echo "Activated pre-commit hook." || :
+#fi
intltoolize --force --automake
autoreconf --force --install --symlink
+
+cd $oldpwd
#
-# This file is part of systemd.
+# This file is part of elogind.
#
# Copyright 2010-2012 Lennart Poettering
# Copyright 2010-2012 Kay Sievers
#
-# systemd is free software; you can redistribute it and/or modify it
+# elogind 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
+# elogind 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.
AC_PREREQ([2.64])
AC_INIT([elogind],
- [219.14],
- [https://github.com/wingo/elogind/issues],
+ [226.3],
+ [https://github.com/elogind/elogind/issues],
[elogind],
- [https://github.com/wingo/elogind])
+ [https://github.com/elogind/elogind])
AC_CONFIG_SRCDIR([src/login/logind.c])
AC_CONFIG_MACRO_DIR([m4])
AM_SILENT_RULES([yes])
AC_CANONICAL_HOST
AC_DEFINE_UNQUOTED([CANONICAL_HOST], "$host", [Canonical host string.])
+
+AC_PROG_CC_C99
+
+AX_COMPILER_VENDOR
+AS_IF([test "x$ax_cv_c_compiler_vendor" = "xgnu"], [
+ AC_CHECK_TOOLS([AR], [gcc-ar ar], [:])
+ AC_CHECK_TOOLS([NM], [gcc-nm nm], [:])
+ AC_CHECK_TOOLS([RANLIB], [gcc-ranlib ranlib], [:])
+])
+
LT_PREREQ(2.2)
LT_INIT([disable-static])
SET_ARCH(X86_64, x86_64*)
SET_ARCH(IA32, i*86*)
SET_ARCH(MIPS, mips*)
+SET_ARCH(AARCH64, aarch64*)
# i18n stuff for the PolicyKit policy files, heck whether intltool can be found, disable NLS otherwise
AC_CHECK_PROG(intltool_found, [intltool-merge], [yes], [no])
AC_PROG_GREP
AC_PROG_AWK
-AC_PROG_CC_C99
-
AC_PATH_PROG([M4], [m4])
AC_PATH_PROG([XSLTPROC], [xsltproc])
M4_DEFINES=
-# gtkdocize greps for '^GTK_DOC_CHECK', so it needs to be on its own line
-m4_ifdef([GTK_DOC_CHECK], [
-GTK_DOC_CHECK([1.18],[--flavour no-tmpl])],
- [AM_CONDITIONAL([ENABLE_GTK_DOC], [false])
- enable_gtk_doc=no])
-
-AS_IF([test "x$enable_gtk_doc" = "xyes" -a "x$XSLTPROC" = x], [
- AC_MSG_ERROR([*** GTK doc requested but xsltproc not found])
-])
-
+AC_CHECK_TOOL(OBJCOPY, objcopy)
AC_CHECK_TOOL(GPERF, gperf)
if test -z "$GPERF" ; then
AC_MSG_ERROR([*** gperf not found])
fi
+
+# ------------------------------------------------------------------------------
+# Find running cgroup controller
+with_cgroupctrl=
+AS_IF( [test -f /proc/self/cgroup], [
+ # If the init system is a cgroup controler, it will be position 1.
+ # Secondary controllers, like cgmanager, do not work.
+ with_cgroupctrl=`grep "^1:name=" /proc/self/cgroup | cut -d ':' -f 2`
+ AS_IF( [test -z "$with_cgroupctrl"], [
+ # Try to be our own cgroup controller
+ with_cgroupctrl="name=elogind"
+ ])
+])
+AS_IF( [test -z "$with_cgroupctrl"],
+ AC_MSG_ERROR([No running cgroup controller found]))
+
+
# ------------------------------------------------------------------------------
address_sanitizer_cflags=
address_sanitizer_cppflags=
-pipe \
-Wall \
-Wextra \
- -Wno-inline \
-Wundef \
"-Wformat=2 -Wformat-security -Wformat-nonliteral" \
-Wlogical-op \
- -Wsign-compare \
-Wmissing-include-dirs \
-Wold-style-definition \
-Wpointer-arith \
-Wdeclaration-after-statement \
-Wfloat-equal \
-Wsuggest-attribute=noreturn \
- -Wmissing-prototypes \
+ -Werror=missing-prototypes \
+ -Werror=implicit-function-declaration \
+ -Werror=missing-declarations \
+ -Werror=return-type \
+ -Werror=shadow \
-Wstrict-prototypes \
-Wredundant-decls \
- -Wmissing-declarations \
-Wmissing-noreturn \
-Wshadow \
-Wendif-labels \
-Wstrict-aliasing=2 \
-Wwrite-strings \
- -Wno-long-long \
- -Wno-overlength-strings \
-Wno-unused-parameter \
-Wno-missing-field-initializers \
-Wno-unused-result \
-fdiagnostics-show-option \
-fno-strict-aliasing \
-fvisibility=hidden \
- -ffunction-sections \
- -fdata-sections \
-fstack-protector \
-fstack-protector-strong \
-fPIE \
dnl AS_CASE([$CFLAGS], [*-O[[12345sz\ ]]*],
dnl [CC_CHECK_FLAGS_APPEND([with_cflags], [CFLAGS], [\
-dnl -flto -ffat-lto-objects])],
+dnl -flto])],
dnl [AC_MSG_RESULT([skipping -flto, optimization not enabled])])
-AC_SUBST([OUR_CFLAGS], "$with_cflags $sanitizer_cflags")
+dnl AC_SUBST([OUR_CFLAGS], "$with_cflags $sanitizer_cflags")
AS_CASE([$CFLAGS], [*-O[[12345sz\ ]]*],
[CC_CHECK_FLAGS_APPEND([with_cppflags], [CPPFLAGS], [\
[AC_MSG_RESULT([skipping -D_FORTIFY_SOURCE, optimization not enabled])])
AC_SUBST([OUR_CPPFLAGS], "$with_cppflags $sanitizer_cppflags")
+AS_CASE([$CFLAGS], [*-O[[12345sz\ ]]*],
+ [CC_CHECK_FLAGS_APPEND([with_ldflags], [LDFLAGS], [\
+ -Wl,--gc-sections])],
+ [AC_MSG_RESULT([skipping --gc-sections, optimization not enabled])])
+AC_SUBST([OUR_CFLAGS], "$with_ldflags $sanitizer_cflags")
+
+AS_CASE([$CFLAGS], [*-O[[12345sz\ ]]*],
+ [CC_CHECK_FLAGS_APPEND([with_cflags], [CFLAGS], [\
+ -ffunction-sections -fdata-sections])],
+ [AC_MSG_RESULT([skipping -ffunction/data-section, optimization not enabled])])
+AC_SUBST([OUR_CFLAGS], "$with_cflags $sanitizer_cflags")
+
CC_CHECK_FLAGS_APPEND([with_ldflags], [LDFLAGS], [\
-Wl,--as-needed \
-Wl,--no-undefined \
- -Wl,--gc-sections \
-Wl,-z,relro \
-Wl,-z,now \
-pie \
#include <sys/resource.h>
])
+GPERF_TEST="$(echo foo,bar | ${GPERF} -L ANSI-C)"
+save_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -Wno-error"
+AC_COMPILE_IFELSE(
+ [AC_LANG_PROGRAM([
+ #include <string.h>
+ const char * in_word_set(const char *, size_t);
+ $GPERF_TEST]
+ )],
+ [GPERF_LEN_TYPE=size_t],
+ [AC_COMPILE_IFELSE(
+ [AC_LANG_PROGRAM([
+ #include <string.h>
+ const char * in_word_set(const char *, unsigned);
+ $GPERF_TEST]
+ )],
+ [GPERF_LEN_TYPE=unsigned],
+ [AC_MSG_ERROR([** unable to determine gperf len type])]
+ )]
+)
+CFLAGS="$save_CFLAGS"
+
+AC_DEFINE_UNQUOTED([GPERF_LEN_TYPE], [$GPERF_LEN_TYPE], [gperf len type])
+
+# ------------------------------------------------------------------------------
+# we use python to build the man page index
+have_python=no
+AC_ARG_WITH([python],
+ [AS_HELP_STRING([--without-python], [Disable building the man page index (default: test)])])
+
+have_lxml=no
+AS_IF([test "x$with_python" != "xno"], [
+ AM_PATH_PYTHON(,, [:])
+ AS_IF([test "x$PYTHON" != "x:"], [
+ AC_MSG_CHECKING([for python lxml module])
+ AS_IF(["$PYTHON" -c 'import lxml' 2>/dev/null], [have_lxml=yes])
+ AC_MSG_RESULT([$have_lxml])
+ AS_IF([test "x$have_lxml" = "xyes"], [have_python=yes],
+ [AC_MSG_WARN([*** python support requires python-lxml module installed])])
+ ])
+])
+AS_IF([test "$have_python" != "yes"], [
+ AS_IF([test "$with_python" = "yes"],
+ [AC_MSG_ERROR([*** python support requested but python support not found])])
+ AS_IF([test "$with_python" != "no"],
+ [AC_MSG_WARN([*** python support not found, some documentation cannot be built])])
+])
+
+AM_CONDITIONAL([HAVE_PYTHON], [test "x$have_python" = "xyes"])
+
# ------------------------------------------------------------------------------
AC_CHECK_HEADERS([sys/capability.h], [], [AC_MSG_ERROR([*** POSIX caps headers not found])])
AC_CHECK_HEADERS([linux/btrfs.h], [], [])
AC_CHECK_HEADERS([linux/memfd.h], [], [])
+AC_CHECK_HEADERS([printf.h], [have_printf_h=yes], [have_printf_h=no])
+AS_IF([test x$have_printf_h = xyes], [
+ AC_DEFINE(HAVE_PRINTF_H, 1, [Define if printf.h was found])
+])
+
+
+
# unconditionally pull-in librt with old glibc versions
-AC_SEARCH_LIBS([clock_gettime], [rt], [], [])
-AC_SEARCH_LIBS([mq_unlink], [rt], [], [])
+dnl AC_SEARCH_LIBS([clock_gettime], [rt], [], [])
+dnl AC_SEARCH_LIBS([mq_unlink], [rt], [], [])
AC_ARG_WITH([libcap],
AS_HELP_STRING([--with-libcap=DIR], [Prefix for libcap]),
IFLA_PHYS_PORT_ID,
IFLA_BOND_AD_INFO,
IFLA_VLAN_PROTOCOL,
+ IFLA_VXLAN_REMCSUM_NOPARTIAL,
IFLA_VXLAN_LOCAL6,
- IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
+ IFLA_IPTUN_ENCAP_DPORT,
+ IFLA_GRE_ENCAP_DPORT,
IFLA_BRIDGE_VLAN_INFO,
- IFLA_BRPORT_UNICAST_FLOOD,
- NDA_IFINDEX],
+ IFLA_BRPORT_LEARNING_SYNC,
+ NDA_IFINDEX,
+ IFA_FLAGS],
[], [], [[
#include <inttypes.h>
#include <netinet/in.h>
#include <linux/if_tunnel.h>
#include <linux/if_link.h>
#include <linux/if_bridge.h>
+#include <linux/if_addr.h>
#include <linux/neighbour.h>
]])
# This makes sure pkg.m4 is available.
m4_pattern_forbid([^_?PKG_[A-Z_]+$],[*** pkg.m4 missing, please install pkg-config])
-# ------------------------------------------------------------------------------
-have_dbus=no
-AC_ARG_ENABLE(dbus, AS_HELP_STRING([--disable-dbus], [disable usage of dbus-1 in tests]))
-AS_IF([test "x$enable_dbus" != "xno"], [
- PKG_CHECK_MODULES(DBUS, [dbus-1 >= 1.3.2],
- [AC_DEFINE(HAVE_DBUS, 1, [Define if dbus-1 library is available]) have_dbus=yes],
- [have_dbus=no])
- AS_IF([test "x$have_dbus" = "xno" -a "x$enable_dbus" = "xyes"],
- [AC_MSG_ERROR([*** dbus-1 support requested but libraries not found])])])
-AM_CONDITIONAL(HAVE_DBUS, [test "$have_dbus" = "yes"])
# ------------------------------------------------------------------------------
PKG_CHECK_MODULES(UDEV, [libudev])
-
+dnl
AC_ARG_WITH([udevrulesdir],
AS_HELP_STRING([--with-udevrulesdir=DIR], [Directory for udev rules files]),
[],
[with_udevrulesdir=$($PKG_CONFIG --variable=udevdir udev)/rules.d])
AC_SUBST([udevrulesdir], [$with_udevrulesdir])
-AC_SUBST([udevbindir], [$($PKG_CONFIG --variable=udevdir udev)/../bin])
+
+AC_ARG_WITH([udevbindir],
+ AS_HELP_STRING([--with-udevbindir=DIR], [Directory for udev binary files]),
+ [],
+ [with_udevbindir=$($PKG_CONFIG --variable=udevdir udev)])
+AC_SUBST([udevbindir], [$with_udevbindir])
# ------------------------------------------------------------------------------
have_coverage=no
fi
AM_CONDITIONAL(ENABLE_COVERAGE, [test "$have_coverage" = "yes"])
-# ------------------------------------------------------------------------------
-have_blkid=no
-AC_ARG_ENABLE(blkid, AS_HELP_STRING([--disable-blkid], [disable blkid support]))
-if test "x$enable_blkid" != "xno"; then
- PKG_CHECK_MODULES(BLKID, [ blkid >= 2.24 ],
- [AC_DEFINE(HAVE_BLKID, 1, [Define if blkid is available]) have_blkid=yes], have_blkid=no)
- if test "x$have_blkid" = xno -a "x$enable_blkid" = xyes; then
- AC_MSG_ERROR([*** blkid support requested but libraries not found])
- fi
-fi
-AM_CONDITIONAL(HAVE_BLKID, [test "$have_blkid" = "yes"])
-
-# ------------------------------------------------------------------------------
-have_seccomp=no
-AC_ARG_ENABLE(seccomp, AS_HELP_STRING([--disable-seccomp], [Disable optional SECCOMP support]))
-if test "x$enable_seccomp" != "xno"; then
- PKG_CHECK_MODULES(SECCOMP, [libseccomp >= 1.0.0],
- [AC_DEFINE(HAVE_SECCOMP, 1, [Define if seccomp is available])
- have_seccomp=yes
- M4_DEFINES="$M4_DEFINES -DHAVE_SECCOMP"],
- [have_seccomp=no])
- if test "x$have_seccomp" = "xno" -a "x$enable_seccomp" = "xyes"; then
- AC_MSG_ERROR([*** seccomp support requested but libraries not found])
- fi
-fi
-AM_CONDITIONAL(HAVE_SECCOMP, [test "$have_seccomp" = "yes"])
-
# ------------------------------------------------------------------------------
have_selinux=no
AC_ARG_ENABLE(selinux, AS_HELP_STRING([--disable-selinux], [Disable optional SELINUX support]))
fi
AM_CONDITIONAL(HAVE_SELINUX, [test "$have_selinux" = "yes"])
-have_apparmor=no
-AC_ARG_ENABLE(apparmor, AS_HELP_STRING([--disable-apparmor], [Disable optional AppArmor support]))
-if test "x$enable_apparmor" != "xno"; then
- PKG_CHECK_MODULES([APPARMOR], [libapparmor],
- [AC_DEFINE(HAVE_APPARMOR, 1, [Define if AppArmor is available])
- have_apparmor=yes
- M4_DEFINES="$M4_DEFINES -DHAVE_APPARMOR"],
- [have_apparmor=no])
- if test "x$have_apparmor" = xno -a "x$enable_apparmor" = xyes; then
- AC_MSG_ERROR([*** AppArmor support requested but libraries not found])
- fi
-fi
-AM_CONDITIONAL(HAVE_APPARMOR, [test "$have_apparmor" = "yes"])
-
# ------------------------------------------------------------------------------
AC_ARG_ENABLE([pam],
AS_HELP_STRING([--disable-pam],[Disable optional PAM support]),
AC_ARG_WITH(smack-run-label,
AS_HELP_STRING([--with-smack-run-label=STRING],
- [run systemd --system with a specific SMACK label]),
- [AC_DEFINE_UNQUOTED(SMACK_RUN_LABEL, ["$withval"], [Run with a smack label])],
+ [run elogind --system itself with a specific SMACK label]),
+ [AC_DEFINE_UNQUOTED(SMACK_RUN_LABEL, ["$withval"], [Run elogind itself with SMACK label])],
+ [])
+
+AC_ARG_WITH(smack-default-process-label,
+AS_HELP_STRING([--with-smack-default-process-label=STRING],
+ [default SMACK label for executed processes]),
+ [AC_DEFINE_UNQUOTED(SMACK_DEFAULT_PROCESS_LABEL, ["$withval"], [Default SMACK label for executed processes])],
[])
if test "x${have_smack}" = xyes ; then
# ------------------------------------------------------------------------------
have_kdbus=no
-AC_ARG_ENABLE(kdbus, AS_HELP_STRING([--enable-kdbus], [do connect to kdbus by default]))
-if test "x$enable_kdbus" = "xyes"; then
- AC_DEFINE(ENABLE_KDBUS, 1, [Define if kdbus support is to be enabled])
+AC_ARG_ENABLE(kdbus, AS_HELP_STRING([--disable-kdbus], [do not connect to kdbus by default]))
+if test "x$enable_kdbus" != "xno"; then
+ AC_DEFINE(ENABLE_KDBUS, 1, [Define if kdbus is to be connected to by default])
have_kdbus=yes
M4_DEFINES="$M4_DEFINES -DENABLE_KDBUS"
fi
AS_HELP_STRING([--with-dbuspolicydir=DIR], [D-Bus policy directory]),
[],
[with_dbuspolicydir=${sysconfdir}/dbus-1/system.d])
-
-AC_ARG_WITH([dbussessionservicedir],
- AS_HELP_STRING([--with-dbussessionservicedir=DIR], [D-Bus session service directory]),
- [],
- [with_dbussessionservicedir=${datadir}/dbus-1/services])
+AX_NORMALIZE_PATH([with_dbuspolicydir])
AC_ARG_WITH([dbussystemservicedir],
AS_HELP_STRING([--with-dbussystemservicedir=DIR], [D-Bus system service directory]),
[],
[with_dbussystemservicedir=${datadir}/dbus-1/system-services])
+AX_NORMALIZE_PATH([with_dbussystemservicedir])
AC_ARG_WITH([bashcompletiondir],
AS_HELP_STRING([--with-bashcompletiondir=DIR], [Bash completions directory]),
] , [
with_bashcompletiondir=${datadir}/bash-completion/completions
])])
+AM_CONDITIONAL(ENABLE_BASH_COMPLETION, [test "$with_bashcompletiondir" != "no"])
+AX_NORMALIZE_PATH([with_bashcompletiondir])
AC_ARG_WITH([zshcompletiondir],
AS_HELP_STRING([--with-zshcompletiondir=DIR], [Zsh completions directory]),
[], [with_zshcompletiondir=${datadir}/zsh/site-functions])
+AM_CONDITIONAL(ENABLE_ZSH_COMPLETION, [test "$with_zshcompletiondir" != "no"])
+AX_NORMALIZE_PATH([with_zshcompletiondir])
+
+AC_ARG_WITH([rootprefix],
+ AS_HELP_STRING([--with-rootprefix=DIR], [rootfs directory prefix for config files and kernel modules]),
+ [], [with_rootprefix=${ac_default_prefix}])
+# --with-rootprefix= (empty) should default to "/" but AX_NORMALIZE_PATH
+# defaults those to ".", solve that here for now until we can find a suitable
+# fix for AX_NORMALIZE_PATH upstream at autoconf-archive.
+# See: https://github.com/systemd/systemd/issues/54
+if test "x${with_rootprefix}" = "x"; then
+ with_rootprefix="/"
+fi
+AX_NORMALIZE_PATH([with_rootprefix])
AC_ARG_WITH([rootlibdir],
AS_HELP_STRING([--with-rootlibdir=DIR], [Root directory for libraries necessary for boot]),
[],
[with_rootlibdir=${libdir}])
+AX_NORMALIZE_PATH([with_rootlibdir])
AC_ARG_WITH([pamlibdir],
AS_HELP_STRING([--with-pamlibdir=DIR], [Directory for PAM modules]),
[],
[with_pamlibdir=${with_rootlibdir}/security])
+AX_NORMALIZE_PATH([with_pamlibdir])
AC_ARG_WITH([pamconfdir],
AS_HELP_STRING([--with-pamconfdir=DIR], [Directory for PAM configuration]),
[],
[with_pamconfdir=${sysconfdir}/pam.d])
+AX_NORMALIZE_PATH([with_pamconfdir])
AC_ARG_ENABLE([split-usr],
AS_HELP_STRING([--enable-split-usr], [Assume that /bin, /sbin aren\'t symlinks into /usr]),
AS_IF([test "x${enable_split_usr}" = "xyes"], [
AC_DEFINE(HAVE_SPLIT_USR, 1, [Define if /bin, /sbin aren't symlinks into /usr])
])
+AM_CONDITIONAL(ENABLE_SPLIT_USR, [test "x${enable_split_usr}" = "xyes"])
-# Work around intltoolize and gtk-doc problems in VPATH builds
-AM_CONDITIONAL([ENABLE_GTK_DOC_TESTS], [test "x$0" = "x./configure"],
- [Define to do gtk-doc tests])
+# work around intltool-update issues during 'make distcheck'
AS_IF([test "x$0" != "x./configure"], [
AC_SUBST([INTLTOOL_UPDATE], [/usr/bin/env true])
])
AM_CONDITIONAL(ENABLE_TESTS, [test x$enable_tests = xyes])
AC_ARG_ENABLE(debug,
- [AC_HELP_STRING([--enable-debug@<:@=LIST@:>@], [enable extra debugging (hashmap,mmap-cache)])],
+ [AC_HELP_STRING([--enable-debug@<:@=LIST@:>@], [enable extra debugging (elogind,hashmap,mmap-cache)])],
[if test "x$enableval" = "xyes"; then
- enableval="hashmap,mmap-cache"
+ enableval="elogind,hashmap,mmap-cache"
fi
saved_ifs="$IFS"
IFS="$IFS$PATH_SEPARATOR,"
for name in $enableval; do
case $name in
+ elogind)
+ enable_debug_elogind=yes
+ ;;
hashmap)
enable_debug_hashmap=yes
;;
IFS="$saved_ifs"],[])
enable_debug=""
+AS_IF([test x$enable_debug_elogind = xyes], [
+ AC_DEFINE(ENABLE_DEBUG_ELOGIND, 1, [Define if elogind debugging is to be enabled])
+ enable_debug="elogind $enable_debug"
+])
AS_IF([test x$enable_debug_hashmap = xyes], [
AC_DEFINE(ENABLE_DEBUG_HASHMAP, 1, [Define if hashmap debugging is to be enabled])
enable_debug="hashmap $enable_debug"
test -z "$enable_debug" && enable_debug="none"
AC_SUBST([dbuspolicydir], [$with_dbuspolicydir])
-AC_SUBST([dbussessionservicedir], [$with_dbussessionservicedir])
AC_SUBST([dbussystemservicedir], [$with_dbussystemservicedir])
AC_SUBST([bashcompletiondir], [$with_bashcompletiondir])
AC_SUBST([zshcompletiondir], [$with_zshcompletiondir])
AC_SUBST([pamlibdir], [$with_pamlibdir])
AC_SUBST([pamconfdir], [$with_pamconfdir])
+AC_SUBST([rootprefix], [$with_rootprefix])
AC_SUBST([rootlibdir], [$with_rootlibdir])
+AC_SUBST([cgroup_controller], [$with_cgroupctrl])
AC_CONFIG_FILES([
- Makefile po/Makefile.in
+ Makefile
+ po/Makefile.in
])
AC_OUTPUT
$PACKAGE_NAME $VERSION
PAM: ${have_pam}
- AppArmor: ${have_apparmor}
SELinux: ${have_selinux}
- SECCOMP: ${have_seccomp}
SMACK: ${have_smack}
ACL: ${have_acl}
polkit: ${have_polkit}
- blkid: ${have_blkid}
- dbus: ${have_dbus}
kdbus: ${have_kdbus}
+ Python: ${have_python}
man pages: ${have_manpages}
- gtk-doc: ${enable_gtk_doc}
test coverage: ${have_coverage}
Split /usr: ${enable_split_usr}
extra debugging: ${enable_debug}
+ cgroup controller: ${with_cgroupctrl}
prefix: ${prefix}
+ rootprefix: ${with_rootprefix}
sysconf dir: ${sysconfdir}
datarootdir: ${datarootdir}
includedir: ${includedir}
PAM modules dir: ${with_pamlibdir}
PAM configuration dir: ${with_pamconfdir}
D-Bus policy dir: ${with_dbuspolicydir}
- D-Bus session dir: ${with_dbussessionservicedir}
D-Bus system dir: ${with_dbussystemservicedir}
Bash completions dir: ${with_bashcompletiondir}
Zsh completions dir: ${with_zshcompletiondir}
--- /dev/null
+../src/Makefile
\ No newline at end of file
--- /dev/null
+/usr/share/gtk-doc/data/gtk-doc.notmpl.make
\ No newline at end of file
-# This file is part of systemd.
+# This file is part of elogind.
auth include system-auth
account include system-auth
-# This file is part of systemd.
+# This file is part of elogind.
auth sufficient pam_unix.so nullok try_first_pass
password sufficient pam_unix.so nullok sha512 shadow try_first_pass try_authtok
-session optional pam_loginuid.so
--session optional pam_systemd.so
+-session optional pam_elogind.so
session sufficient pam_unix.so
ltsugar.m4
ltversion.m4
lt~obsolete.m4
-gtk-doc.m4
--- /dev/null
+# ===========================================================================
+# http://www.gnu.org/software/autoconf-archive/ax_normalize_path.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+# AX_NORMALIZE_PATH(VARNAME, [REFERENCE_STRING])
+#
+# DESCRIPTION
+#
+# Perform some cleanups on the value of $VARNAME (interpreted as a path):
+#
+# - empty paths are changed to '.'
+# - trailing slashes are removed
+# - repeated slashes are squeezed except a leading doubled slash '//'
+# (which might indicate a networked disk on some OS).
+#
+# REFERENCE_STRING is used to turn '/' into '\' and vice-versa: if
+# REFERENCE_STRING contains some backslashes, all slashes and backslashes
+# are turned into backslashes, otherwise they are all turned into slashes.
+#
+# This makes processing of DOS filenames quite easier, because you can
+# turn a filename to the Unix notation, make your processing, and turn it
+# back to original notation.
+#
+# filename='A:\FOO\\BAR\'
+# old_filename="$filename"
+# # Switch to the unix notation
+# AX_NORMALIZE_PATH([filename], ["/"])
+# # now we have $filename = 'A:/FOO/BAR' and we can process it as if
+# # it was a Unix path. For instance let's say that you want
+# # to append '/subpath':
+# filename="$filename/subpath"
+# # finally switch back to the original notation
+# AX_NORMALIZE_PATH([filename], ["$old_filename"])
+# # now $filename equals to 'A:\FOO\BAR\subpath'
+#
+# One good reason to make all path processing with the unix convention is
+# that backslashes have a special meaning in many cases. For instance
+#
+# expr 'A:\FOO' : 'A:\Foo'
+#
+# will return 0 because the second argument is a regex in which
+# backslashes have to be backslashed. In other words, to have the two
+# strings to match you should write this instead:
+#
+# expr 'A:\Foo' : 'A:\\Foo'
+#
+# Such behavior makes DOS filenames extremely unpleasant to work with. So
+# temporary turn your paths to the Unix notation, and revert them to the
+# original notation after the processing. See the macro
+# AX_COMPUTE_RELATIVE_PATHS for a concrete example of this.
+#
+# REFERENCE_STRING defaults to $VARIABLE, this means that slashes will be
+# converted to backslashes if $VARIABLE already contains some backslashes
+# (see $thirddir below).
+#
+# firstdir='/usr/local//share'
+# seconddir='C:\Program Files\\'
+# thirddir='C:\home/usr/'
+# AX_NORMALIZE_PATH([firstdir])
+# AX_NORMALIZE_PATH([seconddir])
+# AX_NORMALIZE_PATH([thirddir])
+# # $firstdir = '/usr/local/share'
+# # $seconddir = 'C:\Program Files'
+# # $thirddir = 'C:\home\usr'
+#
+# LICENSE
+#
+# Copyright (c) 2008 Alexandre Duret-Lutz <adl@gnu.org>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program 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 General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# As a special exception, the respective Autoconf Macro's copyright owner
+# gives unlimited permission to copy, distribute and modify the configure
+# scripts that are the output of Autoconf when processing the Macro. You
+# need not follow the terms of the GNU General Public License when using
+# or distributing such scripts, even though portions of the text of the
+# Macro appear in them. The GNU General Public License (GPL) does govern
+# all other use of the material that constitutes the Autoconf Macro.
+#
+# This special exception to the GPL applies to versions of the Autoconf
+# Macro released by the Autoconf Archive. When you make and distribute a
+# modified version of the Autoconf Macro, you may extend this special
+# exception to the GPL to apply to your modified version as well.
+
+#serial 5
+
+AU_ALIAS([ADL_NORMALIZE_PATH], [AX_NORMALIZE_PATH])
+AC_DEFUN([AX_NORMALIZE_PATH],
+[case ":[$]$1:" in
+# change empty paths to '.'
+ ::) $1='.' ;;
+# strip trailing slashes
+ :*[[\\/]]:) $1=`echo "[$]$1" | sed 's,[[\\/]]*[$],,'` ;;
+ :*:) ;;
+esac
+# squeze repeated slashes
+case ifelse($2,,"[$]$1",$2) in
+# if the path contains any backslashes, turn slashes into backslashes
+ *\\*) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1\\\\,g'` ;;
+# if the path contains slashes, also turn backslashes into slashes
+ *) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1/,g'` ;;
+esac])
--- /dev/null
+dnl -*- mode: autoconf -*-
+
+# serial 2
+
+dnl Usage:
+dnl GTK_DOC_CHECK([minimum-gtk-doc-version])
+AC_DEFUN([GTK_DOC_CHECK],
+[
+ AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+ AC_BEFORE([AC_PROG_LIBTOOL],[$0])dnl setup libtool first
+ AC_BEFORE([AM_PROG_LIBTOOL],[$0])dnl setup libtool first
+
+ ifelse([$1],[],[gtk_doc_requires="gtk-doc"],[gtk_doc_requires="gtk-doc >= $1"])
+ AC_MSG_CHECKING([for gtk-doc])
+ PKG_CHECK_EXISTS([$gtk_doc_requires],[have_gtk_doc=yes],[have_gtk_doc=no])
+ AC_MSG_RESULT($have_gtk_doc)
+
+ if test "$have_gtk_doc" = "no"; then
+ AC_MSG_WARN([
+ You will not be able to create source packages with 'make dist'
+ because $gtk_doc_requires is not found.])
+ fi
+
+ dnl check for tools we added during development
+ dnl Use AC_CHECK_PROG to avoid the check target using an absolute path that
+ dnl may not be writable by the user. Currently, automake requires that the
+ dnl test name must end in '.test'.
+ dnl https://bugzilla.gnome.org/show_bug.cgi?id=701638
+ AC_CHECK_PROG([GTKDOC_CHECK],[gtkdoc-check],[gtkdoc-check.test])
+ AC_PATH_PROG([GTKDOC_CHECK_PATH],[gtkdoc-check])
+ AC_PATH_PROGS([GTKDOC_REBASE],[gtkdoc-rebase],[true])
+ AC_PATH_PROG([GTKDOC_MKPDF],[gtkdoc-mkpdf])
+
+ dnl for overriding the documentation installation directory
+ AC_ARG_WITH([html-dir],
+ AS_HELP_STRING([--with-html-dir=PATH], [path to installed docs]),,
+ [with_html_dir='${datadir}/gtk-doc/html'])
+ HTML_DIR="$with_html_dir"
+ AC_SUBST([HTML_DIR])
+
+ dnl enable/disable documentation building
+ AC_ARG_ENABLE([gtk-doc],
+ AS_HELP_STRING([--enable-gtk-doc],
+ [use gtk-doc to build documentation [[default=no]]]),,
+ [enable_gtk_doc=no])
+
+ AC_MSG_CHECKING([whether to build gtk-doc documentation])
+ AC_MSG_RESULT($enable_gtk_doc)
+
+ if test "x$enable_gtk_doc" = "xyes" && test "$have_gtk_doc" = "no"; then
+ AC_MSG_ERROR([
+ You must have $gtk_doc_requires installed to build documentation for
+ $PACKAGE_NAME. Please install gtk-doc or disable building the
+ documentation by adding '--disable-gtk-doc' to '[$]0'.])
+ fi
+
+ dnl don't check for glib if we build glib
+ if test "x$PACKAGE_NAME" != "xglib"; then
+ dnl don't fail if someone does not have glib
+ PKG_CHECK_MODULES(GTKDOC_DEPS, glib-2.0 >= 2.10.0 gobject-2.0 >= 2.10.0,,[:])
+ fi
+
+ dnl enable/disable output formats
+ AC_ARG_ENABLE([gtk-doc-html],
+ AS_HELP_STRING([--enable-gtk-doc-html],
+ [build documentation in html format [[default=yes]]]),,
+ [enable_gtk_doc_html=yes])
+ AC_ARG_ENABLE([gtk-doc-pdf],
+ AS_HELP_STRING([--enable-gtk-doc-pdf],
+ [build documentation in pdf format [[default=no]]]),,
+ [enable_gtk_doc_pdf=no])
+
+ if test -z "$GTKDOC_MKPDF"; then
+ enable_gtk_doc_pdf=no
+ fi
+
+ if test -z "$AM_DEFAULT_VERBOSITY"; then
+ AM_DEFAULT_VERBOSITY=1
+ fi
+ AC_SUBST([AM_DEFAULT_VERBOSITY])
+
+ AM_CONDITIONAL([HAVE_GTK_DOC], [test x$have_gtk_doc = xyes])
+ AM_CONDITIONAL([ENABLE_GTK_DOC], [test x$enable_gtk_doc = xyes])
+ AM_CONDITIONAL([GTK_DOC_BUILD_HTML], [test x$enable_gtk_doc_html = xyes])
+ AM_CONDITIONAL([GTK_DOC_BUILD_PDF], [test x$enable_gtk_doc_pdf = xyes])
+ AM_CONDITIONAL([GTK_DOC_USE_LIBTOOL], [test -n "$LIBTOOL"])
+ AM_CONDITIONAL([GTK_DOC_USE_REBASE], [test -n "$GTKDOC_REBASE"])
+])
-/systemd.directives.xml
-/systemd.index.xml
+/elogind.directives.xml
+/elogind.index.xml
/*.[13578]
/custom-entities.ent
--- /dev/null
+# This file is part of elogind.
+#
+# Copyright 2010 Lennart Poettering
+#
+# elogind 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.
+#
+# elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+
+# This file is a dirty trick to simplify compilation from within
+# emacs. This file is not intended to be distributed. So, don't touch
+# it, even better ignore it!
+
+all:
+ $(MAKE) -C ..
+
+clean:
+ $(MAKE) -C .. clean
+
+.PHONY: all clean
<?xml version='1.0'?> <!--*-nxml-*-->
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2011 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl"/>
+<!--
+ - The docbook stylesheet injects empty anchor tags into generated HTML, identified by an auto-generated ID.
+ - Ask the docbook stylesheet to generate reproducible output when generating (these) ID values.
+ - This makes the output of this stylesheet reproducible across identical invocations on the same input,
+ - which is an easy and significant win for achieving reproducible builds.
+ -
+ - It may be even better to strip the empty anchors from the document output in addition to turning on consistent IDs,
+ - for this stylesheet contains its own custom ID logic (for generating permalinks) already.
+ -->
+<xsl:param name="generate.consistent.ids" select="1"/>
<!-- translate man page references to links to html pages -->
<xsl:template match="citerefentry[not(@project)]">
</a>
</xsl:template>
-<xsl:template match="refsect1/title|refsect1/info/title">
- <!-- the ID is output in the block.object call for refsect1 -->
- <h2>
+<!--
+ - helper template to do conflict resolution between various headings with the same inferred ID attribute/tag from the headerlink template
+ - this conflict resolution is necessary to prevent malformed HTML output (multiple id attributes with the same value)
+ - and it fixes xsltproc warnings during compilation of HTML man pages
+ -
+ - A simple top-to-bottom numbering scheme is implemented for nodes with the same ID value to derive unique ID values for HTML output.
+ - It uses two parameters:
+ templateID the proposed ID string to use which must be checked for conflicts
+ keyNode the context node which 'produced' the given templateID.
+ -
+ - Conflicts are detected solely based on keyNode, templateID is not taken into account for that purpose.
+ -->
+<xsl:template name="generateID">
+ <!-- node which generatedID needs to assume as the 'source' of the ID -->
+ <xsl:param name="keyNode"/>
+ <!-- suggested value for generatedID output, a contextually meaningful ID string -->
+ <xsl:param name="templateID"/>
+ <xsl:variable name="conflictSource" select="preceding::refsect1/title|preceding::refsect1/info/title|
+ preceding::refsect2/title|preceding::refsect2/info/title|
+ preceding::varlistentry/term[1]"/>
+ <xsl:variable name="conflictCount" select="count($conflictSource[. = $keyNode])"/>
+ <xsl:choose>
+ <!-- special case conflictCount = 0 to preserve compatibility with URLs generated by previous versions of this XSL stylesheet where possible -->
+ <xsl:when test="$conflictCount = 0">
+ <xsl:value-of select="$templateID"/>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="concat($templateID, $conflictCount)"/>
+ </xsl:otherwise>
+ </xsl:choose>
+</xsl:template>
+
+<!--
+ - a helper template to abstract over the structure of generated subheading + permalink HTML output
+ - It helps reduce tedious repetition and groups all actual markup output (as opposed to URL/ID logic) in a single location.
+ -->
+<xsl:template name="permalink">
+ <xsl:param name="nodeType"/> <!-- local name of the element node to generate, e.g. 'h2' for <h2></h2> -->
+ <xsl:param name="nodeContent"/> <!-- nodeset to apply further templates to obtain the content of the subheading/term -->
+ <xsl:param name="linkTitle"/> <!-- value for title attribute of generated permalink, e.g. 'this is a permalink' -->
+
+ <!-- parameters passed to generateID template, otherwise unused. -->
+ <xsl:param name="keyNode"/>
+ <xsl:param name="templateID"/>
+
+ <!--
+ - If stable URLs with fragment markers (references to the ID) turn out not to be important:
+ - generatedID could simply take the value of generate-id(), and various other helper templates may be dropped entirely.
+ - Alternatively if xsltproc is patched to generate reproducible generate-id() output the same simplifications can be
+ - applied at the cost of breaking compatibility with URLs generated from output of previous versions of this stylesheet.
+ -->
+ <xsl:variable name="generatedID">
+ <xsl:call-template name="generateID">
+ <xsl:with-param name="keyNode" select="$keyNode"/>
+ <xsl:with-param name="templateID" select="$templateID"/>
+ </xsl:call-template>
+ </xsl:variable>
+
+ <xsl:element name="{$nodeType}">
<xsl:attribute name="id">
- <xsl:call-template name="inline.charseq"/>
+ <xsl:value-of select="$generatedID"/>
</xsl:attribute>
- <xsl:apply-templates/>
- <a>
- <xsl:attribute name="class">
- <xsl:text>headerlink</xsl:text>
- </xsl:attribute>
- <xsl:attribute name="title">
- <xsl:text>Permalink to this headline</xsl:text>
- </xsl:attribute>
- <xsl:attribute name="href">
- <xsl:text>#</xsl:text>
- <xsl:call-template name="inline.charseq"/>
- </xsl:attribute>
- <xsl:text>¶</xsl:text>
- </a>
- </h2>
+ <xsl:apply-templates select="$nodeContent"/>
+ <a class="headerlink" title="{$linkTitle}" href="#{$generatedID}">¶</a>
+ </xsl:element>
</xsl:template>
-<xsl:template match="refsect2/title|refsect2/info/title">
- <h3>
- <xsl:attribute name="id">
+<!-- simple wrapper around permalink to avoid repeating common info for each level of subheading covered by the permalink logic (h2, h3) -->
+<xsl:template name="headerlink">
+ <xsl:param name="nodeType"/>
+ <xsl:call-template name="permalink">
+ <xsl:with-param name="nodeType" select="$nodeType"/>
+ <xsl:with-param name="linkTitle" select="'Permalink to this headline'"/>
+ <xsl:with-param name="nodeContent" select="node()"/>
+ <xsl:with-param name="keyNode" select="."/>
+ <!--
+ - To retain compatibility with IDs generated by previous versions of the template, inline.charseq must be called.
+ - The purpose of that template is to generate markup (according to docbook documentation its purpose is to mark/format something as plain text).
+ - The only reason to call this template is to get the auto-generated text such as brackets ([]) before flattening it.
+ -->
+ <xsl:with-param name="templateID">
<xsl:call-template name="inline.charseq"/>
- </xsl:attribute>
- <xsl:apply-templates/>
- <a>
- <xsl:attribute name="class">
- <xsl:text>headerlink</xsl:text>
- </xsl:attribute>
- <xsl:attribute name="title">
- <xsl:text>Permalink to this headline</xsl:text>
- </xsl:attribute>
- <xsl:attribute name="href">
- <xsl:text>#</xsl:text>
- <xsl:call-template name="inline.charseq"/>
- </xsl:attribute>
- <xsl:text>¶</xsl:text>
- </a>
- </h3>
+ </xsl:with-param>
+ </xsl:call-template>
+</xsl:template>
+
+<xsl:template match="refsect1/title|refsect1/info/title">
+ <!-- the ID is output in the block.object call for refsect1 -->
+ <xsl:call-template name="headerlink">
+ <xsl:with-param name="nodeType" select="'h2'"/>
+ </xsl:call-template>
+</xsl:template>
+
+<xsl:template match="refsect2/title|refsect2/info/title">
+ <xsl:call-template name="headerlink">
+ <xsl:with-param name="nodeType" select="'h3'"/>
+ </xsl:call-template>
</xsl:template>
<xsl:template match="varlistentry">
- <dt>
- <xsl:attribute name="id">
+ <xsl:call-template name="permalink">
+ <xsl:with-param name="nodeType" select="'dt'"/>
+ <xsl:with-param name="linkTitle" select="'Permalink to this term'"/>
+ <xsl:with-param name="nodeContent" select="term"/>
+ <xsl:with-param name="keyNode" select="term[1]"/>
+ <!--
+ - To retain compatibility with IDs generated by previous versions of the template, inline.charseq must be called.
+ - The purpose of that template is to generate markup (according to docbook documentation its purpose is to mark/format something as plain text).
+ - The only reason to call this template is to get the auto-generated text such as brackets ([]) before flattening it.
+ -->
+ <xsl:with-param name="templateID">
<xsl:call-template name="inline.charseq">
- <xsl:with-param name="content">
- <xsl:copy-of select="term[position()=1]" />
- </xsl:with-param>
+ <xsl:with-param name="content" select="term[1]"/>
</xsl:call-template>
- </xsl:attribute>
- <xsl:apply-templates select="term"/>
- <a>
- <xsl:attribute name="class">
- <xsl:text>headerlink</xsl:text>
- </xsl:attribute>
- <xsl:attribute name="title">
- <xsl:text>Permalink to this term</xsl:text>
- </xsl:attribute>
- <xsl:attribute name="href">
- <!-- <xsl:call-template name="href.target.uri" /> -->
- <xsl:text>#</xsl:text>
- <xsl:call-template name="inline.charseq">
- <xsl:with-param name="content">
- <xsl:copy-of select="term[position()=1]" />
- </xsl:with-param>
- </xsl:call-template>
- </xsl:attribute>
- <xsl:text>¶</xsl:text>
- </a>
- </dt>
+ </xsl:with-param>
+ </xsl:call-template>
<dd>
<xsl:apply-templates select="listitem"/>
</dd>
<xsl:text>index.html</xsl:text>
</xsl:attribute>
<xsl:text>Index </xsl:text>
+ </a>·
+ <a>
+ <xsl:attribute name="href">
+ <xsl:text>elogind.directives.html</xsl:text>
+ </xsl:attribute>
+ <xsl:text>Directives </xsl:text>
</a>
<span style="float:right">
<?xml version='1.0'?> <!--*-nxml-*-->
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2013 Zbigniew Jędrzejewski-Szmek
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2010 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
-<refentry id="logind.service">
+<refentry id="elogind">
<refentryinfo>
- <title>logind.service</title>
- <productname>systemd</productname>
+ <title>elogind</title>
+ <productname>elogind</productname>
<authorgroup>
<author>
</refentryinfo>
<refmeta>
- <refentrytitle>logind.service</refentrytitle>
+ <refentrytitle>elogind</refentrytitle>
<manvolnum>8</manvolnum>
</refmeta>
<refnamediv>
- <refname>logind.service</refname>
- <refname>logind</refname>
+ <refname>elogind</refname>
<refpurpose>Login manager</refpurpose>
</refnamediv>
<refsynopsisdiv>
- <para><filename>logind.service</filename></para>
- <para><filename>/usr/lib/systemd/logind</filename></para>
+ <para><filename>/usr/libexec/elogind/elogind</filename></para>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
- <para><command>logind</command> is a system service that
+ <para><command>elogind</command> is a system service that
manages user logins. It is responsible for:</para>
<itemizedlist>
<listitem><para>Device access management for
users</para></listitem>
- <listitem><para>Automatic spawning of text logins (gettys) on
- virtual console activation and user runtime directory
- management</para></listitem>
</itemizedlist>
- <para>User sessions are registered in logind via the
- <citerefentry><refentrytitle>pam_systemd</refentrytitle><manvolnum>8</manvolnum></citerefentry>
+ <para>User sessions are registered in elogind via the
+ <citerefentry><refentrytitle>pam_elogind</refentrytitle><manvolnum>8</manvolnum></citerefentry>
PAM module.</para>
<para>See
<refsect1>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>systemd-user-sessions.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>loginctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>logind.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>pam_systemd</refentrytitle><manvolnum>8</manvolnum></citerefentry>
+ <citerefentry><refentrytitle>pam_elogind</refentrytitle><manvolnum>8</manvolnum></citerefentry>
</para>
</refsect1>
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2010 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="loginctl" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>loginctl</title>
- <productname>systemd</productname>
+ <productname>elogind</productname>
<authorgroup>
<author>
<refnamediv>
<refname>loginctl</refname>
- <refpurpose>Control the systemd login manager</refpurpose>
+ <refpurpose>Control the elogind login manager</refpurpose>
</refnamediv>
<refsynopsisdiv>
<para><command>loginctl</command> may be used to introspect and
control the state of the
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>
login manager
<citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>logind.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2010 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="logind.conf" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>logind.conf</title>
- <productname>systemd</productname>
+ <productname>elogind</productname>
<authorgroup>
<author>
<refnamediv>
<refname>logind.conf</refname>
- <refname>logind.conf.d</refname>
<refpurpose>Login manager configuration files</refpurpose>
</refnamediv>
<refsynopsisdiv>
- <para><filename>/etc/systemd/logind.conf</filename></para>
- <para><filename>/etc/systemd/logind.conf.d/*.conf</filename></para>
- <para><filename>/run/systemd/logind.conf.d/*.conf</filename></para>
- <para><filename>/usr/lib/systemd/logind.conf.d/*.conf</filename></para>
+ <para><filename>/etc/elogind/logind.conf</filename></para>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
- <para>These files configure various parameters of the systemd
+ <para>These files configure various parameters of the elogind
login manager,
<citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
</para>
<term><varname>HoldoffTimeoutSec=</varname></term>
<listitem><para>Specifies the timeout after system startup or
- system resume in which systemd will hold off on reacting to
+ system resume in which elogind will hold off on reacting to
LID events. This is required for the system to properly
- detect any hotplugged devices so systemd can ignore LID events
+ detect any hotplugged devices so elogind can ignore LID events
if external monitors, or docks, are connected. If set to 0,
- systemd will always react immediately, possibly before the
+ elogind will always react immediately, possibly before the
kernel fully probed all hotplugged devices. This is safe, as
- long as you do not care for systemd to account for devices
+ long as you do not care for elogind to account for devices
that have been plugged or unplugged while the system was off.
Defaults to 30s.</para></listitem>
</varlistentry>
<refsect1>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>loginctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+ <citerefentry><refentrytitle>elogind-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>
</para>
</refsect1>
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2010 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="pam_elogind" conditional='HAVE_PAM'>
<para><command>pam_elogind</command> registers user sessions with
the elogind login manager
- <citerefentry><refentrytitle>logind</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
and hence the elogind control group hierarchy.</para>
<para>On login, this module ensures the following:</para>
<refsect1>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>logind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>logind.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>loginctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>pam.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>pam.d</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>pam</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>pam_loginuid</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind.scope</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
</para>
</refsect1>
+++ /dev/null
-<?xml version='1.0'?> <!--*-nxml-*-->
-<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
- "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
-
-<!--
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
--->
-
-<refentry id="sd-login" conditional='HAVE_PAM'
- xmlns:xi="http://www.w3.org/2001/XInclude">
-
- <refentryinfo>
- <title>sd-login</title>
- <productname>systemd</productname>
-
- <authorgroup>
- <author>
- <contrib>Developer</contrib>
- <firstname>Lennart</firstname>
- <surname>Poettering</surname>
- <email>lennart@poettering.net</email>
- </author>
- </authorgroup>
- </refentryinfo>
-
- <refmeta>
- <refentrytitle>sd-login</refentrytitle>
- <manvolnum>3</manvolnum>
- </refmeta>
-
- <refnamediv>
- <refname>sd-login</refname>
- <refpurpose>APIs for
- tracking logins</refpurpose>
- </refnamediv>
-
- <refsynopsisdiv>
- <funcsynopsis>
- <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
- </funcsynopsis>
-
- <cmdsynopsis>
- <command>pkg-config --cflags --libs libelogind</command>
- </cmdsynopsis>
- </refsynopsisdiv>
-
- <refsect1>
- <title>Description</title>
-
- <para><filename>sd-login.h</filename> provides APIs to introspect
- and monitor seat, login session and user status information on the
- local system. </para>
-
- <para>See <ulink
- url="http://www.freedesktop.org/wiki/Software/systemd/multiseat">Multi-Seat
- on Linux</ulink> for an introduction into multi-seat support on
- Linux, the background for this set of APIs.</para>
-
- <para>Note that these APIs only allow purely passive access and
- monitoring of seats, sessions and users. To actively make changes
- to the seat configuration, terminate login sessions, or switch
- session on a seat you need to utilize the D-Bus API of
- logind, instead.</para>
-
- <para>These functions synchronously access data in
- <filename>/proc</filename>, <filename>/sys/fs/cgroup</filename>
- and <filename>/run</filename>. All of these are virtual file
- systems, hence the runtime cost of the accesses is relatively
- cheap.</para>
-
- <para>It is possible (and often a very good choice) to mix calls
- to the synchronous interface of <filename>sd-login.h</filename>
- with the asynchronous D-Bus interface of logind. However,
- if this is done you need to think a bit about possible races since
- the stream of events from D-Bus and from
- <filename>sd-login.h</filename> interfaces such as the login
- monitor are asynchronous and not ordered against each
- other.</para>
-
- <para>If the functions return string arrays, these are generally
- <constant>NULL</constant> terminated and need to be freed by the
- caller with the libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use, including the strings referenced therein.
- Similarly, individual strings returned need to be freed, as
- well.</para>
-
- <para>As a special exception, instead of an empty string array
- <constant>NULL</constant> may be returned, which should be treated
- equivalent to an empty string array.</para>
-
- <para>See
- <citerefentry><refentrytitle>sd_pid_get_session</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_uid_get_state</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_session_is_active</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_seat_get_active</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_login_monitor_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- for more information about the functions
- implemented.</para>
- </refsect1>
-
- <xi:include href="libelogind-pkgconfig.xml" />
-
- <refsect1>
- <title>See Also</title>
- <para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_pid_get_session</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_uid_get_state</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_session_is_active</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_seat_get_active</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_login_monitor_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
- </para>
- </refsect1>
-
-</refentry>
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2010 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="sd_get_seats" conditional='HAVE_PAM'>
<refentryinfo>
<title>sd_get_seats</title>
- <productname>systemd</productname>
+ <productname>elogind</productname>
<authorgroup>
<author>
<refsynopsisdiv>
<funcsynopsis>
- <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
+ <funcsynopsisinfo>#include <elogind/sd-login.h></funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_get_seats</function></funcdef>
errno-style error code.</para>
</refsect1>
+
+ <refsect1>
+ <title>Errors</title>
+
+ <para>Returned errors may indicate the following problems:</para>
+
+ <variablelist>
+
+ <varlistentry>
+ <term><constant>-EINVAL</constant></term>
+
+ <listitem><para>An input parameter was invalid (out of range,
+ or NULL, where that's not accepted).</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-ENOMEM</constant></term>
+
+ <listitem><para>Memory allocation failed.</para></listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
<refsect1>
<title>Notes</title>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_session_get_seat</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
--- /dev/null
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+
+<!--
+ This file is part of elogind.
+
+ Copyright 2012 Lennart Poettering
+
+ elogind 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.
+
+ elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<refentry id="sd_id128_get_machine">
+
+ <refentryinfo>
+ <title>sd_id128_get_machine</title>
+ <productname>elogind</productname>
+
+ <authorgroup>
+ <author>
+ <contrib>Developer</contrib>
+ <firstname>Lennart</firstname>
+ <surname>Poettering</surname>
+ <email>lennart@poettering.net</email>
+ </author>
+ </authorgroup>
+ </refentryinfo>
+
+ <refmeta>
+ <refentrytitle>sd_id128_get_machine</refentrytitle>
+ <manvolnum>3</manvolnum>
+ </refmeta>
+
+ <refnamediv>
+ <refname>sd_id128_get_machine</refname>
+ <refname>sd_id128_get_boot</refname>
+ <refpurpose>Retrieve 128-bit IDs</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+ <funcsynopsis>
+ <funcsynopsisinfo>#include <elogind/sd-id128.h></funcsynopsisinfo>
+
+ <funcprototype>
+ <funcdef>int <function>sd_id128_get_machine</function></funcdef>
+ <paramdef>sd_id128_t *<parameter>ret</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_id128_get_boot</function></funcdef>
+ <paramdef>sd_id128_t *<parameter>ret</parameter></paramdef>
+ </funcprototype>
+
+ </funcsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+ <title>Description</title>
+
+ <para><function>sd_id128_get_machine()</function> returns the
+ machine ID of the executing host. This reads and parses the
+ <citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+ file. This function caches the machine ID internally to make
+ retrieving the machine ID a cheap operation.</para>
+
+ <para><function>sd_id128_get_boot()</function> returns the boot ID
+ of the executing kernel. This reads and parses the
+ <filename>/proc/sys/kernel/random/boot_id</filename> file exposed
+ by the kernel. It is randomly generated early at boot and is
+ unique for every running kernel instance. See
+ <citerefentry project='man-pages'><refentrytitle>random</refentrytitle><manvolnum>4</manvolnum></citerefentry>
+ for more information. This function also internally caches the
+ returned ID to make this call a cheap operation.</para>
+
+ <para>Note that <function>sd_id128_get_boot()</function> always
+ returns a UUID v4 compatible ID.
+ <function>sd_id128_get_machine()</function> will also return a
+ UUID v4-compatible ID on new installations but might not on older.
+ It is possible to convert the machine ID into a UUID v4-compatible
+ one. For more information, see
+ <citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
+
+ <para>For more information about the <literal>sd_id128_t</literal>
+ type see
+ <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Return Value</title>
+
+ <para>The two calls return 0 on success (in which case
+ <parameter>ret</parameter> is filled in), or a negative
+ errno-style error code.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Notes</title>
+
+ <para>The <function>sd_id128_get_machine()</function> and
+ <function>sd_id128_get_boot()</function> interfaces are available
+ as a shared library, which can be compiled and linked to with the
+ <literal>libelogind</literal> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+ file.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+
+ <para>
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>random</refentrytitle><manvolnum>4</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd_id128_randomize</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ </para>
+ </refsect1>
+
+</refentry>
--- /dev/null
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+
+<!--
+ This file is part of elogind.
+
+ Copyright 2012 Lennart Poettering
+
+ elogind 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.
+
+ elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<refentry id="sd_id128_randomize">
+
+ <refentryinfo>
+ <title>sd_id128_randomize</title>
+ <productname>elogind</productname>
+
+ <authorgroup>
+ <author>
+ <contrib>Developer</contrib>
+ <firstname>Lennart</firstname>
+ <surname>Poettering</surname>
+ <email>lennart@poettering.net</email>
+ </author>
+ </authorgroup>
+ </refentryinfo>
+
+ <refmeta>
+ <refentrytitle>sd_id128_randomize</refentrytitle>
+ <manvolnum>3</manvolnum>
+ </refmeta>
+
+ <refnamediv>
+ <refname>sd_id128_randomize</refname>
+ <refpurpose>Generate 128-bit IDs</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+ <funcsynopsis>
+ <funcsynopsisinfo>#include <elogind/sd-id128.h></funcsynopsisinfo>
+
+ <funcprototype>
+ <funcdef>int <function>sd_id128_randomize</function></funcdef>
+ <paramdef>sd_id128_t *<parameter>ret</parameter></paramdef>
+ </funcprototype>
+
+ </funcsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+ <title>Description</title>
+
+ <para><function>sd_id128_randomize()</function> generates a new
+ randomized 128-bit ID and returns it in
+ <parameter>ret</parameter>. Every invocation returns a new
+ randomly generated ID. This uses the
+ <filename>/dev/urandom</filename> kernel random number
+ generator.</para>
+
+ <para>Note that <function>sd_id128_randomize()</function> always
+ returns a UUID v4-compatible ID.</para>
+
+ <para>For more information about the <literal>sd_id128_t</literal>
+ type, see
+ <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
+
+ <para><citerefentry><refentrytitle>journalctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
+ <option>--new-id</option> option may be used as a command line
+ front-end for <function>sd_id128_randomize()</function>.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Return Value</title>
+
+ <para>The call returns 0 on success (in which case
+ <parameter>ret</parameter> is filled in), or a negative
+ errno-style error code.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Notes</title>
+
+ <para>The <function>sd_id128_randomize()</function> interface is
+ available as a shared library, which can be compiled and linked to
+ with the
+ <literal>libelogind</literal> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+ file.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+
+ <para>
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>random</refentrytitle><manvolnum>4</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd_id128_get_machine</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ </para>
+ </refsect1>
+
+</refentry>
--- /dev/null
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+
+<!--
+ This file is part of elogind.
+
+ Copyright 2012 Lennart Poettering
+
+ elogind 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.
+
+ elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<refentry id="sd_id128_to_string">
+
+ <refentryinfo>
+ <title>sd_id128_to_string</title>
+ <productname>elogind</productname>
+
+ <authorgroup>
+ <author>
+ <contrib>Developer</contrib>
+ <firstname>Lennart</firstname>
+ <surname>Poettering</surname>
+ <email>lennart@poettering.net</email>
+ </author>
+ </authorgroup>
+ </refentryinfo>
+
+ <refmeta>
+ <refentrytitle>sd_id128_to_string</refentrytitle>
+ <manvolnum>3</manvolnum>
+ </refmeta>
+
+ <refnamediv>
+ <refname>sd_id128_to_string</refname>
+ <refname>sd_id128_from_string</refname>
+ <refpurpose>Format or parse 128-bit IDs as strings</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+ <funcsynopsis>
+ <funcsynopsisinfo>#include <elogind/sd-id128.h></funcsynopsisinfo>
+
+ <funcprototype>
+ <funcdef>char *<function>sd_id128_to_string</function></funcdef>
+ <paramdef>sd_id128_t <parameter>id</parameter>, char <parameter>s</parameter>[33]</paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_id128_from_string</function></funcdef>
+ <paramdef>const char *<parameter>s</parameter>, sd_id128_t *<parameter>ret</parameter></paramdef>
+ </funcprototype>
+
+ </funcsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+ <title>Description</title>
+
+ <para><function>sd_id128_to_string()</function> formats a 128-bit
+ ID as a character string. It expects the ID and a string array
+ capable of storing 33 characters. The ID will be formatted as 32
+ lowercase hexadecimal digits and be terminated by a
+ <constant>NUL</constant> byte.</para>
+
+ <para><function>sd_id128_from_string()</function> implements the
+ reverse operation: it takes a 33 character string with 32
+ hexadecimal digits (either lowercase or uppercase, terminated by
+ <constant>NUL</constant>) and parses them back into a 128-bit ID
+ returned in <parameter>ret</parameter>. Alternatively, this call
+ can also parse a 37-character string with a 128-bit ID formatted
+ as RFC UUID.</para>
+
+ <para>For more information about the <literal>sd_id128_t</literal>
+ type see
+ <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
+ Note that these calls operate the same way on all architectures,
+ i.e. the results do not depend on endianness.</para>
+
+ <para>When formatting a 128-bit ID into a string, it is often
+ easier to use a format string for
+ <citerefentry project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
+ This is easily done using the
+ <function>SD_ID128_FORMAT_STR</function> and
+ <function>SD_ID128_FORMAT_VAL()</function> macros. For more
+ information see
+ <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Return Value</title>
+
+ <para><function>sd_id128_to_string()</function> always succeeds
+ and returns a pointer to the string array passed in.
+ <function>sd_id128_from_string</function> returns 0 on success, in
+ which case <parameter>ret</parameter> is filled in, or a negative
+ errno-style error code.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Notes</title>
+
+ <para>The <function>sd_id128_to_string()</function> and
+ <function>sd_id128_from_string()</function> interfaces are
+ available as a shared library, which can be compiled and linked to
+ with the
+ <literal>libelogind</literal> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+ file.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+
+ <para>
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ </para>
+ </refsect1>
+
+</refentry>
+++ /dev/null
-<?xml version='1.0'?> <!--*-nxml-*-->
-<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
- "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
-
-<!--
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
--->
-
-<refentry id="sd_login_monitor_new" conditional='HAVE_PAM'>
-
- <refentryinfo>
- <title>sd_login_monitor_new</title>
- <productname>systemd</productname>
-
- <authorgroup>
- <author>
- <contrib>Developer</contrib>
- <firstname>Lennart</firstname>
- <surname>Poettering</surname>
- <email>lennart@poettering.net</email>
- </author>
- </authorgroup>
- </refentryinfo>
-
- <refmeta>
- <refentrytitle>sd_login_monitor_new</refentrytitle>
- <manvolnum>3</manvolnum>
- </refmeta>
-
- <refnamediv>
- <refname>sd_login_monitor_new</refname>
- <refname>sd_login_monitor_unref</refname>
- <refname>sd_login_monitor_flush</refname>
- <refname>sd_login_monitor_get_fd</refname>
- <refname>sd_login_monitor_get_events</refname>
- <refname>sd_login_monitor_get_timeout</refname>
- <refname>sd_login_monitor</refname>
- <refpurpose>Monitor login sessions, seats, users and virtual machines/containers</refpurpose>
- </refnamediv>
-
- <refsynopsisdiv>
- <funcsynopsis>
- <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
-
- <funcprototype>
- <funcdef>int <function>sd_login_monitor_new</function></funcdef>
- <paramdef>const char *<parameter>category</parameter></paramdef>
- <paramdef>sd_login_monitor **<parameter>ret</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>sd_login_monitor *<function>sd_login_monitor_unref</function></funcdef>
- <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_login_monitor_flush</function></funcdef>
- <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_login_monitor_get_fd</function></funcdef>
- <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_login_monitor_get_events</function></funcdef>
- <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_login_monitor_get_timeout</function></funcdef>
- <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
- <paramdef>uint64_t *<parameter>timeout_usec</parameter></paramdef>
- </funcprototype>
-
- </funcsynopsis>
- </refsynopsisdiv>
-
- <refsect1>
- <title>Description</title>
-
- <para><function>sd_login_monitor_new()</function> may be used to
- monitor login sessions, users, seats, and virtual
- machines/containers. Via a monitor object a file descriptor can be
- integrated into an application defined event loop which is woken
- up each time a user logs in, logs out or a seat is added or
- removed, or a session, user, seat or virtual machine/container
- changes state otherwise. The first parameter takes a string which
- can be <literal>seat</literal> (to get only notifications about
- seats being added, removed or changed), <literal>session</literal>
- (to get only notifications about sessions being created or removed
- or changed), <literal>uid</literal> (to get only notifications
- when a user changes state in respect to logins) or
- <literal>machine</literal> (to get only notifications when a
- virtual machine or container is started or stopped). If
- notifications shall be generated in all these conditions,
- <constant>NULL</constant> may be passed. Note that in the future
- additional categories may be defined. The second parameter returns
- a monitor object and needs to be freed with the
- <function>sd_login_monitor_unref()</function> call after
- use.</para>
-
- <para><function>sd_login_monitor_unref()</function> may be used to
- destroy a monitor object. Note that this will invalidate any file
- descriptor returned by
- <function>sd_login_monitor_get_fd()</function>.</para>
-
- <para><function>sd_login_monitor_flush()</function> may be used to
- reset the wakeup state of the monitor object. Whenever an event
- causes the monitor to wake up the event loop via the file
- descriptor this function needs to be called to reset the wake-up
- state. If this call is not invoked, the file descriptor will
- immediately wake up the event loop again.</para>
-
- <para><function>sd_login_monitor_get_fd()</function> may be used
- to retrieve the file descriptor of the monitor object that may be
- integrated in an application defined event loop, based around
- <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>
- or a similar interface. The application should include the
- returned file descriptor as wake-up source for the events mask
- returned by <function>sd_login_monitor_get_events()</function>. It
- should pass a timeout value as returned by
- <function>sd_login_monitor_get_timeout()</function>. Whenever a
- wake-up is triggered the file descriptor needs to be reset via
- <function>sd_login_monitor_flush()</function>. An application
- needs to reread the login state with a function like
- <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- or similar to determine what changed.</para>
-
- <para><function>sd_login_monitor_get_events()</function> will
- return the <function>poll()</function> mask to wait for. This
- function will return a combination of <constant>POLLIN</constant>,
- <constant>POLLOUT</constant> and similar to fill into the
- <literal>.events</literal> field of <varname>struct
- pollfd</varname>.</para>
-
- <para><function>sd_login_monitor_get_timeout()</function> will
- return a timeout value for usage in <function>poll()</function>.
- This returns a value in microseconds since the epoch of
- <constant>CLOCK_MONOTONIC</constant> for timing out
- <function>poll()</function> in <varname>timeout_usec</varname>.
- See
- <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
- for details about <constant>CLOCK_MONOTONIC</constant>. If there
- is no timeout to wait for this will fill in <constant>(uint64_t)
- -1</constant> instead. Note that <function>poll()</function> takes
- a relative timeout in milliseconds rather than an absolute timeout
- in microseconds. To convert the absolute 'us' timeout into
- relative 'ms', use code like the following:</para>
-
- <programlisting>uint64_t t;
-int msec;
-sd_login_monitor_get_timeout(m, &t);
-if (t == (uint64_t) -1)
- msec = -1;
-else {
- struct timespec ts;
- uint64_t n;
- clock_getttime(CLOCK_MONOTONIC, &ts);
- n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
- msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
-}</programlisting>
-
- <para>The code above does not do any error checking for brevity's
- sake. The calculated <varname>msec</varname> integer can be passed
- directly as <function>poll()</function>'s timeout
- parameter.</para>
- </refsect1>
-
- <refsect1>
- <title>Return Value</title>
-
- <para>On success,
- <function>sd_login_monitor_new()</function>,
- <function>sd_login_monitor_flush()</function> and
- <function>sd_login_monitor_get_timeout()</function>
- return 0 or a positive integer. On success,
- <function>sd_login_monitor_get_fd()</function> returns
- a Unix file descriptor. On success,
- <function>sd_login_monitor_get_events()</function>
- returns a combination of <constant>POLLIN</constant>,
- <constant>POLLOUT</constant> and suchlike. On failure,
- these calls return a negative errno-style error
- code.</para>
-
- <para><function>sd_login_monitor_unref()</function>
- always returns <constant>NULL</constant>.</para>
- </refsect1>
-
- <refsect1>
- <title>Notes</title>
-
- <para>The <function>sd_login_monitor_new()</function>,
- <function>sd_login_monitor_unref()</function>,
- <function>sd_login_monitor_flush()</function>,
- <function>sd_login_monitor_get_fd()</function>,
- <function>sd_login_monitor_get_events()</function> and
- <function>sd_login_monitor_get_timeout()</function>
- interfaces are available as a shared library, which can be
- compiled and linked to with the
- <constant>libelogind</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
- file.</para>
- </refsect1>
-
- <refsect1>
- <title>See Also</title>
-
- <para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
- </para>
- </refsect1>
-
-</refentry>
--- /dev/null
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+
+<!--
+ This file is part of elogind.
+
+ Copyright 2014 Lennart Poettering
+
+ elogind 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.
+
+ elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<refentry id="sd_machine_get_class">
+
+ <refentryinfo>
+ <title>sd_machine_get_class</title>
+ <productname>elogind</productname>
+
+ <authorgroup>
+ <author>
+ <contrib>Developer</contrib>
+ <firstname>Lennart</firstname>
+ <surname>Poettering</surname>
+ <email>lennart@poettering.net</email>
+ </author>
+ </authorgroup>
+ </refentryinfo>
+
+ <refmeta>
+ <refentrytitle>sd_machine_get_class</refentrytitle>
+ <manvolnum>3</manvolnum>
+ </refmeta>
+
+ <refnamediv>
+ <refname>sd_machine_get_class</refname>
+ <refname>sd_machine_get_ifindices</refname>
+ <refpurpose>Determine the class and network interface indices of a
+ locally running virtual machine or container.</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+ <funcsynopsis>
+ <funcsynopsisinfo>#include <elogind/sd-login.h></funcsynopsisinfo>
+
+ <funcprototype>
+ <funcdef>int <function>sd_machine_get_class</function></funcdef>
+ <paramdef>const char* <parameter>machine</parameter></paramdef>
+ <paramdef>char **<parameter>class</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_machine_get_ifindices</function></funcdef>
+ <paramdef>const char* <parameter>machine</parameter></paramdef>
+ <paramdef>int **<parameter>ifindices</parameter></paramdef>
+ </funcprototype>
+ </funcsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+ <title>Description</title>
+
+ <para><function>sd_machine_get_class()</function> may be used to
+ determine the class of a locally running virtual machine or
+ container that is registered with
+ <citerefentry><refentrytitle>elogind-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
+ The string returned is either <literal>vm</literal> or
+ <literal>container</literal>. The returned string needs to be
+ freed with the libc <citerefentry
+ project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ call after use.</para>
+
+ <para><function>sd_machine_get_ifindices()</function> may be used
+ to determine the numeric indices of the network interfaces on the
+ host that are pointing towards the specified locally running
+ virtual machine or container that is registered with
+ <citerefentry><refentrytitle>elogind-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
+ The returned array needs to be freed with the libc <citerefentry
+ project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ call after use.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Return Value</title>
+
+ <para>On success, these calls return 0 or a positive integer. On
+ failure, these calls return a negative errno-style error
+ code.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Errors</title>
+
+ <para>Returned errors may indicate the following problems:</para>
+
+ <variablelist>
+
+ <varlistentry>
+ <term><constant>-ENXIO</constant></term>
+
+ <listitem><para>The specified machine does not exist or is currently not running.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-EINVAL</constant></term>
+
+ <listitem><para>An input parameter was invalid (out of range,
+ or NULL, where that's not accepted).</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-ENOMEM</constant></term>
+
+ <listitem><para>Memory allocation failed.</para></listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
+ <refsect1>
+ <title>Notes</title>
+
+ <para>The <function>sd_machine_get_class()</function> and
+ <function>sd_machine_get_ifindices()</function> interfaces are
+ available as a shared library, which can be compiled and linked to
+ with the
+ <constant>libelogind</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+ file.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+
+ <para>
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd_pid_get_machine_name</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ </para>
+ </refsect1>
+
+</refentry>
--- /dev/null
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+
+<!--
+ This file is part of elogind.
+
+ Copyright 2010 Lennart Poettering
+
+ elogind 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.
+
+ elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<refentry id="sd_notify"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+
+ <refentryinfo>
+ <title>sd_notify</title>
+ <productname>elogind</productname>
+
+ <authorgroup>
+ <author>
+ <contrib>Developer</contrib>
+ <firstname>Lennart</firstname>
+ <surname>Poettering</surname>
+ <email>lennart@poettering.net</email>
+ </author>
+ </authorgroup>
+ </refentryinfo>
+
+ <refmeta>
+ <refentrytitle>sd_notify</refentrytitle>
+ <manvolnum>3</manvolnum>
+ </refmeta>
+
+ <refnamediv>
+ <refname>sd_notify</refname>
+ <refname>sd_notifyf</refname>
+ <refname>sd_pid_notify</refname>
+ <refname>sd_pid_notifyf</refname>
+ <refname>sd_pid_notify_with_fds</refname>
+ <refpurpose>Notify service manager about start-up completion and other service status changes</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+ <funcsynopsis>
+ <funcsynopsisinfo>#include <elogind/sd-daemon.h></funcsynopsisinfo>
+
+ <funcprototype>
+ <funcdef>int <function>sd_notify</function></funcdef>
+ <paramdef>int <parameter>unset_environment</parameter></paramdef>
+ <paramdef>const char *<parameter>state</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_notifyf</function></funcdef>
+ <paramdef>int <parameter>unset_environment</parameter></paramdef>
+ <paramdef>const char *<parameter>format</parameter></paramdef>
+ <paramdef>...</paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_pid_notify</function></funcdef>
+ <paramdef>pid_t <parameter>pid</parameter></paramdef>
+ <paramdef>int <parameter>unset_environment</parameter></paramdef>
+ <paramdef>const char *<parameter>state</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_pid_notifyf</function></funcdef>
+ <paramdef>pid_t <parameter>pid</parameter></paramdef>
+ <paramdef>int <parameter>unset_environment</parameter></paramdef>
+ <paramdef>const char *<parameter>format</parameter></paramdef>
+ <paramdef>...</paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_pid_notify_with_fds</function></funcdef>
+ <paramdef>pid_t <parameter>pid</parameter></paramdef>
+ <paramdef>int <parameter>unset_environment</parameter></paramdef>
+ <paramdef>const char *<parameter>state</parameter></paramdef>
+ <paramdef>const int *<parameter>fds</parameter></paramdef>
+ <paramdef>unsigned <parameter>n_fds</parameter></paramdef>
+ </funcprototype>
+ </funcsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+ <title>Description</title>
+ <para><function>sd_notify()</function> may be called by a service
+ to notify the service manager about state changes. It can be used
+ to send arbitrary information, encoded in an
+ environment-block-like string. Most importantly it can be used for
+ start-up completion notification.</para>
+
+ <para>If the <parameter>unset_environment</parameter> parameter is
+ non-zero, <function>sd_notify()</function> will unset the
+ <varname>$NOTIFY_SOCKET</varname> environment variable before
+ returning (regardless of whether the function call itself
+ succeeded or not). Further calls to
+ <function>sd_notify()</function> will then fail, but the variable
+ is no longer inherited by child processes.</para>
+
+ <para>The <parameter>state</parameter> parameter should contain a
+ newline-separated list of variable assignments, similar in style
+ to an environment block. A trailing newline is implied if none is
+ specified. The string may contain any kind of variable
+ assignments, but the following shall be considered
+ well-known:</para>
+
+ <variablelist>
+ <varlistentry>
+ <term>READY=1</term>
+
+ <listitem><para>Tells the service manager that service startup
+ is finished. This is only used by elogind if the service
+ definition file has Type=notify set. Since there is little
+ value in signaling non-readiness, the only value services
+ should send is <literal>READY=1</literal> (i.e.
+ <literal>READY=0</literal> is not defined).</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>RELOADING=1</term>
+
+ <listitem><para>Tells the service manager that the service is
+ reloading its configuration. This is useful to allow the
+ service manager to track the service's internal state, and
+ present it to the user. Note that a service that sends this
+ notification must also send a <literal>READY=1</literal>
+ notification when it completed reloading its
+ configuration.</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>STOPPING=1</term>
+
+ <listitem><para>Tells the service manager that the service is
+ beginning its shutdown. This is useful to allow the service
+ manager to track the service's internal state, and present it
+ to the user.</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>STATUS=...</term>
+
+ <listitem><para>Passes a single-line UTF-8 status string back
+ to the service manager that describes the service 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: <literal>STATUS=Completed 66% of file
+ system check...</literal></para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>ERRNO=...</term>
+
+ <listitem><para>If a service fails, the errno-style error
+ code, formatted as string. Example: <literal>ERRNO=2</literal>
+ for ENOENT.</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>BUSERROR=...</term>
+
+ <listitem><para>If a service fails, the D-Bus error-style
+ error code. Example:
+ <literal>BUSERROR=org.freedesktop.DBus.Error.TimedOut</literal></para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>MAINPID=...</term>
+
+ <listitem><para>The main process ID (PID) of the service, in
+ case the service manager did not fork off the process itself.
+ Example: <literal>MAINPID=4711</literal></para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>WATCHDOG=1</term>
+
+ <listitem><para>Tells the service manager to update the
+ watchdog timestamp. This is the keep-alive ping that services
+ need to issue in regular intervals if
+ <varname>WatchdogSec=</varname> is enabled for it. See
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+ for information how to enable this functionality and
+ <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ for the details of how the service can check whether the
+ watchdog is enabled. </para></listitem>
+ </varlistentry>
+
+
+ <varlistentry>
+ <term>FDSTORE=1</term>
+
+ <listitem><para>Stores additional file descriptors in the
+ service manager. File descriptors sent this way will be
+ maintained per-service by the service manager and be passed
+ again using the usual file descriptor passing logic on the
+ next invocation of the service (see
+ <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
+ This is useful for implementing service restart schemes where
+ services serialize their state to <filename>/run</filename>,
+ push their file descriptors to the system manager, and are
+ then restarted, retrieving their state again via socket
+ passing and <filename>/run</filename>. Note that the service
+ manager will accept messages for a service only if
+ <varname>FileDescriptorStoreMax=</varname> is set to non-zero
+ for it (defaults to zero). See
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+ for details. Multiple arrays of file descriptors may be sent
+ in separate messages, in which case the arrays are combined.
+ Note that the service manager removes duplicate file
+ descriptors before passing them to the service. Use
+ <function>sd_pid_notify_with_fds()</function> to send messages
+ with <literal>FDSTORE=1</literal>, see
+ below.</para></listitem>
+ </varlistentry>
+
+ </variablelist>
+
+ <para>It is recommended to prefix variable names that are not
+ listed above with <varname>X_</varname> to avoid namespace
+ clashes.</para>
+
+ <para>Note that elogind will accept status data sent from a
+ service only if the <varname>NotifyAccess=</varname> option is
+ correctly set in the service definition file. See
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+ for details.</para>
+
+ <para><function>sd_notifyf()</function> is similar to
+ <function>sd_notify()</function> but takes a
+ <function>printf()</function>-like format string plus
+ arguments.</para>
+
+ <para><function>sd_pid_notify()</function> and
+ <function>sd_pid_notifyf()</function> are similar to
+ <function>sd_notify()</function> and
+ <function>sd_notifyf()</function> but take a process ID (PID) to
+ use as originating PID for the message as first argument. This is
+ useful to send notification messages on behalf of other processes,
+ provided the appropriate privileges are available. If the PID
+ argument is specified as 0 the process ID of the calling process
+ is used, in which case the calls are fully equivalent to
+ <function>sd_notify()</function> and
+ <function>sd_notifyf()</function>.</para>
+
+ <para><function>sd_pid_notify_with_fds()</function> is similar to
+ <function>sd_pid_notify()</function> but takes an additional array
+ of file descriptors. These file descriptors are sent along the
+ notification message to the service manager. This is particularly
+ useful for sending <literal>FDSTORE=1</literal> messages, as
+ described above. The additional arguments are a pointer to the
+ file descriptor array plus the number of file descriptors in the
+ array. If the number of file descriptors is passed as 0, the call
+ is fully equivalent to <function>sd_pid_notify()</function>, i.e.
+ no file descriptors are passed. Note that sending file descriptors
+ to the service manager on messages that do not expect them (i.e.
+ without <literal>FDSTORE=1</literal>) they are immediately closed
+ on reception.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Return Value</title>
+
+ <para>On failure, these calls return a negative errno-style error
+ code. If <varname>$NOTIFY_SOCKET</varname> was not set and hence
+ no status data could be sent, 0 is returned. If the status was
+ sent, these functions return with a positive return value. In
+ order to support both, init systems that implement this scheme and
+ those which do not, it is generally recommended to ignore the
+ return value of this call.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Notes</title>
+
+ <xi:include href="libelogind-pkgconfig.xml" xpointer="pkgconfig-text"/>
+
+ <para>Internally, these functions send a single datagram with the
+ state string as payload to the <constant>AF_UNIX</constant> socket
+ referenced in the <varname>$NOTIFY_SOCKET</varname> environment
+ variable. If the first character of
+ <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the
+ string is understood as Linux abstract namespace socket. The
+ datagram is accompanied by the process credentials of the sending
+ service, using SCM_CREDENTIALS.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Environment</title>
+
+ <variablelist class='environment-variables'>
+ <varlistentry>
+ <term><varname>$NOTIFY_SOCKET</varname></term>
+
+ <listitem><para>Set by the service manager for supervised
+ processes for status and start-up completion notification.
+ This environment variable specifies the socket
+ <function>sd_notify()</function> talks to. See above for
+ details.</para></listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
+ <refsect1>
+ <title>Examples</title>
+
+ <example>
+ <title>Start-up Notification</title>
+
+ <para>When a service finished starting up, it might issue the
+ following call to notify the service manager:</para>
+
+ <programlisting>sd_notify(0, "READY=1");</programlisting>
+ </example>
+
+ <example>
+ <title>Extended Start-up Notification</title>
+
+ <para>A service could send the following after completing
+ initialization:</para>
+
+ <programlisting>sd_notifyf(0, "READY=1\n"
+ "STATUS=Processing requests...\n"
+ "MAINPID=%lu",
+ (unsigned long) getpid());</programlisting>
+ </example>
+
+ <example>
+ <title>Error Cause Notification</title>
+
+ <para>A service could send the following shortly before exiting, on failure:</para>
+
+ <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n"
+ "ERRNO=%i",
+ strerror(errno),
+ errno);</programlisting>
+ </example>
+
+ <example>
+ <title>Store a File Descriptor in the Service Manager</title>
+
+ <para>To store an open file descriptor in the service manager,
+ in order to continue operation after a service restart without
+ losing state use <literal>FDSTORE=1</literal>:</para>
+
+ <programlisting>sd_pid_notify_with_fds(0, 0, "FDSTORE=1", &fd, 1);</programlisting>
+ </example>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+ <para>
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ </para>
+ </refsect1>
+
+</refentry>
+++ /dev/null
-<?xml version='1.0'?> <!--*-nxml-*-->
-<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
- "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
-
-<!--
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
--->
-
-<refentry id="sd_pid_get_session" conditional='HAVE_PAM'>
-
- <refentryinfo>
- <title>sd_pid_get_session</title>
- <productname>systemd</productname>
-
- <authorgroup>
- <author>
- <contrib>Developer</contrib>
- <firstname>Lennart</firstname>
- <surname>Poettering</surname>
- <email>lennart@poettering.net</email>
- </author>
- </authorgroup>
- </refentryinfo>
-
- <refmeta>
- <refentrytitle>sd_pid_get_session</refentrytitle>
- <manvolnum>3</manvolnum>
- </refmeta>
-
- <refnamediv>
- <refname>sd_pid_get_session</refname>
- <refname>sd_pid_get_unit</refname>
- <refname>sd_pid_get_user_unit</refname>
- <refname>sd_pid_get_owner_uid</refname>
- <refname>sd_pid_get_machine_name</refname>
- <refname>sd_pid_get_slice</refname>
- <refname>sd_peer_get_session</refname>
- <refname>sd_peer_get_unit</refname>
- <refname>sd_peer_get_user_unit</refname>
- <refname>sd_peer_get_owner_uid</refname>
- <refname>sd_peer_get_machine_name</refname>
- <refname>sd_peer_get_slice</refname>
- <refpurpose>Determine session, service, owner of a
- session, container/VM or slice of a specific
- PID or socket peer</refpurpose>
- </refnamediv>
-
- <refsynopsisdiv>
- <funcsynopsis>
- <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
-
- <funcprototype>
- <funcdef>int <function>sd_pid_get_session</function></funcdef>
- <paramdef>pid_t <parameter>pid</parameter></paramdef>
- <paramdef>char **<parameter>session</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_pid_get_unit</function></funcdef>
- <paramdef>pid_t <parameter>pid</parameter></paramdef>
- <paramdef>char **<parameter>unit</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_pid_get_user_unit</function></funcdef>
- <paramdef>pid_t <parameter>pid</parameter></paramdef>
- <paramdef>char **<parameter>unit</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_pid_get_owner_uid</function></funcdef>
- <paramdef>pid_t <parameter>pid</parameter></paramdef>
- <paramdef>uid_t *<parameter>uid</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_pid_get_machine_name</function></funcdef>
- <paramdef>pid_t <parameter>pid</parameter></paramdef>
- <paramdef>char **<parameter>name</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_pid_get_slice</function></funcdef>
- <paramdef>pid_t <parameter>pid</parameter></paramdef>
- <paramdef>char **<parameter>slice</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_peer_get_session</function></funcdef>
- <paramdef>int <parameter>fd</parameter></paramdef>
- <paramdef>char **<parameter>session</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_peer_get_unit</function></funcdef>
- <paramdef>int <parameter>fd</parameter></paramdef>
- <paramdef>char **<parameter>unit</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_peer_get_user_unit</function></funcdef>
- <paramdef>int <parameter>fd</parameter></paramdef>
- <paramdef>char **<parameter>unit</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_peer_get_owner_uid</function></funcdef>
- <paramdef>int <parameter>fd</parameter></paramdef>
- <paramdef>uid_t *<parameter>uid</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_peer_get_machine_name</function></funcdef>
- <paramdef>int <parameter>fd</parameter></paramdef>
- <paramdef>char **<parameter>name</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_peer_get_slice</function></funcdef>
- <paramdef>int <parameter>fd</parameter></paramdef>
- <paramdef>char **<parameter>slice</parameter></paramdef>
- </funcprototype>
- </funcsynopsis>
- </refsynopsisdiv>
-
- <refsect1>
- <title>Description</title>
-
- <para><function>sd_pid_get_session()</function> may be used to
- determine the login session identifier of a process identified by
- the specified process identifier. The session identifier is a
- short string, suitable for usage in file system paths. Note that
- not all processes are part of a login session (e.g. system service
- processes, user processes that are shared between multiple
- sessions of the same user, or kernel threads). For processes not
- being part of a login session this function will fail. The
- returned string needs to be freed with the libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use.</para>
-
- <para><function>sd_pid_get_unit()</function> may be used to
- determine the systemd system unit (i.e. system service) identifier
- of a process identified by the specified PID. The unit name is a
- short string, suitable for usage in file system paths. Note that
- not all processes are part of a system unit/service (e.g. user
- processes, or kernel threads). For processes not being part of a
- systemd system unit this function will fail. (More specifically:
- this call will not work for processes that are part of user units,
- use <function>sd_pid_get_user_unit()</function> for that.) The
- returned string needs to be freed with the libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use.</para>
-
- <para><function>sd_pid_get_user_unit()</function> may be used to
- determine the systemd user unit (i.e. user service) identifier of
- a process identified by the specified PID. This is similar to
- <function>sd_pid_get_unit()</function> but applies to user units
- instead of system units.</para>
-
- <para><function>sd_pid_get_owner_uid()</function> may be used to
- determine the Unix user identifier of the owner of the session of
- a process identified the specified PID. Note that this function
- will succeed for user processes which are shared between multiple
- login sessions of the same user, where
- <function>sd_pid_get_session()</function> will fail. For processes
- not being part of a login session and not being a shared process
- of a user this function will fail.</para>
-
- <para><function>sd_pid_get_machine_name()</function> may be used
- to determine the name of the VM or container is a member of. The
- machine name is a short string, suitable for usage in file system
- paths. The returned string needs to be freed with the libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use.</para>
-
- <para><function>sd_pid_get_slice()</function> may be used to
- determine the slice unit the process is a member of. See
- <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>
- for details about slices. The returned string needs to be freed
- with the libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use.</para>
-
- <para>If the <varname>pid</varname> parameter of any of these
- functions is passed as 0, the operation is executed for the
- calling process.</para>
-
- <para>The <function>sd_peer_get_session()</function>,
- <function>sd_peer_get_unit()</function>,
- <function>sd_peer_get_user_unit()</function>,
- <function>sd_peer_get_owner_uid()</function>,
- <function>sd_peer_get_machine_name()</function> and
- <function>sd_peer_get_slice()</function> calls operate similar to
- their PID counterparts, but operate on a connected AF_UNIX socket
- and retrieve information about the connected peer process.</para>
- </refsect1>
-
- <refsect1>
- <title>Return Value</title>
-
- <para>On success, these calls return 0 or a positive integer. On
- failure, these calls return a negative errno-style error
- code.</para>
- </refsect1>
-
- <refsect1>
- <title>Notes</title>
-
- <para>The <function>sd_pid_get_session()</function>,
- <function>sd_pid_get_unit()</function>,
- <function>sd_pid_get_user_unit()</function>,
- <function>sd_pid_get_owner_uid()</function>,
- <function>sd_pid_get_machine_name()</function>,
- <function>sd_pid_get_slice()</function>,
- <function>sd_peer_get_session()</function>,
- <function>sd_peer_get_unit()</function>,
- <function>sd_peer_get_user_unit()</function>,
- <function>sd_peer_get_owner_uid()</function>,
- <function>sd_peer_get_machine_name()</function> and
- <function>sd_peer_get_slice()</function> interfaces are
- available as a shared library, which can be compiled
- and linked to with the
- <constant>libelogind</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
- file.</para>
-
- <para>Note that the login session identifier as
- returned by <function>sd_pid_get_session()</function>
- is completely unrelated to the process session
- identifier as returned by
- <citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry>.</para>
- </refsect1>
-
- <refsect1>
- <title>See Also</title>
-
- <para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_session_is_active</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
- </para>
- </refsect1>
-
-</refentry>
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2010 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="sd_seat_get_active" conditional='HAVE_PAM'>
<refentryinfo>
<title>sd_seat_get_active</title>
- <productname>systemd</productname>
+ <productname>elogind</productname>
<authorgroup>
<author>
<refsynopsisdiv>
<funcsynopsis>
- <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
+ <funcsynopsisinfo>#include <elogind/sd-login.h></funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_seat_get_active</function></funcdef>
errno-style error code.</para>
</refsect1>
+ <refsect1>
+ <title>Errors</title>
+
+ <para>Returned errors may indicate the following problems:</para>
+
+ <variablelist>
+
+ <varlistentry>
+ <term><constant>-ENODATA</constant></term>
+
+ <listitem><para>Given field is not specified for the described
+ seat.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-ENXIO</constant></term>
+
+ <listitem><para>The specified seat is unknown.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-EINVAL</constant></term>
+
+ <listitem><para>An input parameter was invalid (out of range,
+ or NULL, where that's not accepted).</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-ENOMEM</constant></term>
+
+ <listitem><para>Memory allocation failed.</para></listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
<refsect1>
<title>Notes</title>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_session_get_seat</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
- This file is part of systemd.
+ This file is part of elogind.
Copyright 2010 Lennart Poettering
- systemd is free software; you can redistribute it and/or modify it
+ elogind 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
+ elogind 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 <http://www.gnu.org/licenses/>.
+ along with elogind; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="sd_session_is_active" conditional='HAVE_PAM'>
<refentryinfo>
<title>sd_session_is_active</title>
- <productname>systemd</productname>
+ <productname>elogind</productname>
<authorgroup>
<author>
<refsynopsisdiv>
<funcsynopsis>
- <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
+ <funcsynopsisinfo>#include <elogind/sd-login.h></funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_session_is_active</function></funcdef>
negative errno-style error code.</para>
</refsect1>
+ <refsect1>
+ <title>Errors</title>
+
+ <para>Returned errors may indicate the following problems:</para>
+
+ <variablelist>
+
+ <varlistentry>
+ <term><constant>-ENXIO</constant></term>
+
+ <listitem><para>The specified session does not exist.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-ENODATA</constant></term>
+
+ <listitem><para>Given field is not specified for the described
+ session.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-EINVAL</constant></term>
+
+ <listitem><para>An input parameter was invalid (out of range,
+ or NULL, where that's not accepted).</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><constant>-ENOMEM</constant></term>
+
+ <listitem><para>Memory allocation failed.</para></listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
<refsect1>
<title>Notes</title>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_pid_get_session</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
+++ /dev/null
-<?xml version='1.0'?> <!--*-nxml-*-->
-<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
- "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
-
-<!--
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
--->
-
-<refentry id="sd_uid_get_state" conditional='HAVE_PAM'>
-
- <refentryinfo>
- <title>sd_uid_get_state</title>
- <productname>systemd</productname>
-
- <authorgroup>
- <author>
- <contrib>Developer</contrib>
- <firstname>Lennart</firstname>
- <surname>Poettering</surname>
- <email>lennart@poettering.net</email>
- </author>
- </authorgroup>
- </refentryinfo>
-
- <refmeta>
- <refentrytitle>sd_uid_get_state</refentrytitle>
- <manvolnum>3</manvolnum>
- </refmeta>
-
- <refnamediv>
- <refname>sd_uid_get_state</refname>
- <refname>sd_uid_is_on_seat</refname>
- <refname>sd_uid_get_sessions</refname>
- <refname>sd_uid_get_seats</refname>
- <refname>sd_uid_get_display</refname>
- <refpurpose>Determine login state of a specific Unix user ID</refpurpose>
- </refnamediv>
-
- <refsynopsisdiv>
- <funcsynopsis>
- <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
-
- <funcprototype>
- <funcdef>int <function>sd_uid_get_state</function></funcdef>
- <paramdef>uid_t <parameter>uid</parameter></paramdef>
- <paramdef>char **<parameter>state</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_uid_is_on_seat</function></funcdef>
- <paramdef>uid_t <parameter>uid</parameter></paramdef>
- <paramdef>int <parameter>require_active</parameter></paramdef>
- <paramdef>const char *<parameter>seat</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_uid_get_sessions</function></funcdef>
- <paramdef>uid_t <parameter>uid</parameter></paramdef>
- <paramdef>int <parameter>require_active</parameter></paramdef>
- <paramdef>char ***<parameter>sessions</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_uid_get_seats</function></funcdef>
- <paramdef>uid_t <parameter>uid</parameter></paramdef>
- <paramdef>int <parameter>require_active</parameter></paramdef>
- <paramdef>char ***<parameter>seats</parameter></paramdef>
- </funcprototype>
-
- <funcprototype>
- <funcdef>int <function>sd_uid_get_display</function></funcdef>
- <paramdef>uid_t <parameter>uid</parameter></paramdef>
- <paramdef>char **<parameter>session</parameter></paramdef>
- </funcprototype>
- </funcsynopsis>
- </refsynopsisdiv>
-
- <refsect1>
- <title>Description</title>
-
- <para><function>sd_uid_get_state()</function> may be used to
- determine the login state of a specific Unix user identifier. The
- following states are currently known: <literal>offline</literal>
- (user not logged in at all), <literal>lingering</literal> (user
- not logged in, but some user services running),
- <literal>online</literal> (user logged in, but not active, i.e.
- has no session in the foreground), <literal>active</literal> (user
- logged in, and has at least one active session, i.e. one session
- in the foreground), <literal>closing</literal> (user not logged
- in, and not lingering, but some processes are still around). In
- the future additional states might be defined, client code should
- be written to be robust in regards to additional state strings
- being returned. The returned string needs to be freed with the
- libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use.</para>
-
- <para><function>sd_uid_is_on_seat()</function> may be used to
- determine whether a specific user is logged in or active on a
- specific seat. Accepts a Unix user identifier and a seat
- identifier string as parameters. The
- <parameter>require_active</parameter> parameter is a boolean
- value. If non-zero (true), this function will test if the user is
- active (i.e. has a session that is in the foreground and accepting
- user input) on the specified seat, otherwise (false) only if the
- user is logged in (and possibly inactive) on the specified
- seat.</para>
-
- <para><function>sd_uid_get_sessions()</function> may be used to
- determine the current sessions of the specified user. Accepts a
- Unix user identifier as parameter. The
- <parameter>require_active</parameter> parameter controls whether
- the returned list shall consist of only those sessions where the
- user is currently active (> 0), where the user is currently
- online but possibly inactive (= 0), or logged in at all but
- possibly closing the session (< 0). The call returns a
- <constant>NULL</constant> terminated string array of session
- identifiers in <parameter>sessions</parameter> which needs to be
- freed by the caller with the libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use, including all the strings referenced. If the
- string array parameter is passed as <constant>NULL</constant>, the
- array will not be filled in, but the return code still indicates
- the number of current sessions. Note that instead of an empty
- array <constant>NULL</constant> may be returned and should be
- considered equivalent to an empty array.</para>
-
- <para>Similarly, <function>sd_uid_get_seats()</function> may be
- used to determine the list of seats on which the user currently
- has sessions. Similar semantics apply, however note that the user
- may have multiple sessions on the same seat as well as sessions
- with no attached seat and hence the number of entries in the
- returned array may differ from the one returned by
- <function>sd_uid_get_sessions()</function>.</para>
-
- <para><function>sd_uid_get_display()</function> returns the name
- of the "primary" session of a user. If the user has graphical
- sessions, it will be the oldest graphical session. Otherwise, it
- will be the oldest open session.</para>
- </refsect1>
-
- <refsect1>
- <title>Return Value</title>
-
- <para>On success, <function>sd_uid_get_state()</function> returns
- 0 or a positive integer. If the test succeeds,
- <function>sd_uid_is_on_seat()</function> returns a positive
- integer; if it fails, 0.
- <function>sd_uid_get_sessions()</function> and
- <function>sd_uid_get_seats()</function> return the number of
- entries in the returned arrays.
- <function>sd_uid_get_display()</function> returns a non-negative
- code on success. On failure, these calls return a negative
- errno-style error code.</para>
- </refsect1>
-
- <refsect1>
- <title>Notes</title>
-
- <para>Functions described here are available as a shared library,
- and can be compiled and linked to using the
- <constant>libelogind</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
- entry.</para>
- </refsect1>
-
- <refsect1>
- <title>History</title>
-
- <para><function>sd_uid_get_state()</function>,
- <function>sd_uid_is_on_seat()</function>,
- <function>sd_uid_get_sessions()</function>, and
- <function>sd_uid_get_seats()</function> functions were added in
- systemd-31.</para>
-
- <para><function>sd_uid_get_display()</function> was added in
- systemd-213.</para>
- </refsect1>
-
- <refsect1>
- <title>See Also</title>
-
- <para>
- <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_pid_get_owner_uid</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- </para>
- </refsect1>
-
-</refentry>
--- /dev/null
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+
+<!--
+ This file is part of elogind.
+
+ Copyright 2013 Lennart Poettering
+
+ elogind 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.
+
+ elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<refentry id="sd_watchdog_enabled"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+
+ <refentryinfo>
+ <title>sd_watchdog_enabled</title>
+ <productname>elogind</productname>
+
+ <authorgroup>
+ <author>
+ <contrib>Developer</contrib>
+ <firstname>Lennart</firstname>
+ <surname>Poettering</surname>
+ <email>lennart@poettering.net</email>
+ </author>
+ </authorgroup>
+ </refentryinfo>
+
+ <refmeta>
+ <refentrytitle>sd_watchdog_enabled</refentrytitle>
+ <manvolnum>3</manvolnum>
+ </refmeta>
+
+ <refnamediv>
+ <refname>sd_watchdog_enabled</refname>
+ <refpurpose>Check whether the service manager expects watchdog keep-alive notifications from a service</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+ <funcsynopsis>
+ <funcsynopsisinfo>#include <elogind/sd-daemon.h></funcsynopsisinfo>
+
+ <funcprototype>
+ <funcdef>int <function>sd_watchdog_enabled</function></funcdef>
+ <paramdef>int <parameter>unset_environment</parameter></paramdef>
+ <paramdef>uint64_t *<parameter>usec</parameter></paramdef>
+ </funcprototype>
+ </funcsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+ <title>Description</title>
+ <para><function>sd_watchdog_enabled()</function> may be called by
+ a service to detect whether the service manager expects regular
+ keep-alive watchdog notification events from it, and the timeout
+ after which the manager will act on the service if it did not get
+ such a notification.</para>
+
+ <para>If the <varname>$WATCHDOG_USEC</varname> environment
+ variable is set, and the <varname>$WATCHDOG_PID</varname> variable
+ is unset or set to the PID of the current process, the service
+ manager expects notifications from this process. The manager will
+ usually terminate a service when it does not get a notification
+ message within the specified time after startup and after each
+ previous message. It is recommended that a daemon sends a
+ keep-alive notification message to the service manager every half
+ of the time returned here. Notification messages may be sent with
+ <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ with a message string of <literal>WATCHDOG=1</literal>.</para>
+
+ <para>If the <parameter>unset_environment</parameter> parameter is
+ non-zero, <function>sd_watchdog_enabled()</function> will unset
+ the <varname>$WATCHDOG_USEC</varname> and
+ <varname>$WATCHDOG_PID</varname> environment variables before
+ returning (regardless of whether the function call itself
+ succeeded or not). Those variables are no longer inherited by
+ child processes. Further calls to
+ <function>sd_watchdog_enabled()</function> will also return with
+ zero.</para>
+
+ <para>If the <parameter>usec</parameter> parameter is non-NULL,
+ <function>sd_watchdog_enabled()</function> will write the timeout
+ in µs for the watchdog logic to it.</para>
+
+ <para>To enable service supervision with the watchdog logic, use
+ <varname>WatchdogSec=</varname> in service files. See
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+ for details.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Return Value</title>
+
+ <para>On failure, this call returns a negative errno-style error
+ code. If the service manager expects watchdog keep-alive
+ notification messages to be sent, > 0 is returned, otherwise 0
+ is returned. Only if the return value is > 0, the
+ <parameter>usec</parameter> parameter is valid after the
+ call.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Notes</title>
+
+ <xi:include href="libelogind-pkgconfig.xml" xpointer="pkgconfig-text"/>
+
+ <para>Internally, this functions parses the
+ <varname>$WATCHDOG_PID</varname> and
+ <varname>$WATCHDOG_USEC</varname> environment variable. The call
+ will ignore these variables if <varname>$WATCHDOG_PID</varname>
+ does not contain the PID of the current process, under the
+ assumption that in that case, the variables were set for a
+ different process further up the process tree.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>Environment</title>
+
+ <variablelist class='environment-variables'>
+ <varlistentry>
+ <term><varname>$WATCHDOG_PID</varname></term>
+
+ <listitem><para>Set by the system manager for supervised
+ process for which watchdog support is enabled, and contains
+ the PID of that process. See above for
+ details.</para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><varname>$WATCHDOG_USEC</varname></term>
+
+ <listitem><para>Set by the system manager for supervised
+ process for which watchdog support is enabled, and contains
+ the watchdog timeout in µs See above for
+ details.</para></listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
+ <refsect1>
+ <title>History</title>
+
+ <para>The watchdog functionality and the
+ <varname>$WATCHDOG_USEC</varname> variable were added in
+ elogind-41.</para>
+
+ <para><function>sd_watchdog_enabled()</function> function was
+ added in elogind-209. Since that version the
+ <varname>$WATCHDOG_PID</varname> variable is also set.</para>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+ <para>
+ <citerefentry><refentrytitle>elogind</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>logind.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ </para>
+ </refsect1>
+
+</refentry>
the vendor, the recommended way is to place a symlink to
<filename>/dev/null</filename> in the configuration directory in
<filename>/etc/</filename>, with the same filename as the vendor
- configuration file.</para>
+ configuration file. If the vendor configuration file is included in
+ the initrd image, the image has to be regenerated.</para>
+
</refsection>
<refsection id='main-conf'>
<para>Default configuration is defined during compilation, so a
configuration file is only needed when it is necessary to deviate
from those defaults. By default the configuration file in
- <filename>/etc/systemd/</filename> contains commented out entries
+ <filename>/etc/elogind/</filename> contains commented out entries
showing the defaults as a guide to the administrator. This file
can be edited to create local overrides.
</para>
<para>When packages need to customize the configuration, they can
install configuration snippets in
- <filename>/usr/lib/systemd/*.conf.d/</filename>. Files in
+ <filename>/usr/lib/elogind/*.conf.d/</filename>. Files in
<filename>/etc/</filename> are reserved for the local
administrator, who may use this logic to override the
configuration files installed by vendor packages. The main
uk
sv
es
+zh_TW
+be
+be@latin
--- /dev/null
+# Belarusian translation for systemd.
+# Copyright (C) 2015 systemd's COPYRIGHT HOLDER
+# This file is distributed under the same license as the systemd package.
+#
+# Viktar Vaŭčkievič <victorenator@gmail.com>, 2015.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: systemd master\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
+"PO-Revision-Date: 2015-06-14 11:17+0300\n"
+"Last-Translator: Viktar Vaŭčkievič <victorenator@gmail.com>\n"
+"Language-Team: \n"
+"Language: be\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
+"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
+"X-Generator: Lokalize 1.5\n"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
+msgid "Send passphrase back to system"
+msgstr "Адправіць пароль назад сістэме"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
+msgstr "Неабходна аўтэнтыфікацыя для адпраўкі пароля назад сістэме."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
+msgid "Manage system services or other units"
+msgstr "Кіраваць сэрвісамі і іншымі сістэмнымі адзінкамі"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
+msgid "Authentication is required to manage system services or other units."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для кіравання сэрвісамі і іншымі сістэмнымі "
+"адзінкамі."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
+msgid "Manage system service or unit files"
+msgstr "Кіраваць файламі сэрвісаў і іншых сістэмных адзінак"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
+msgid "Authentication is required to manage system service or unit files."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для кіравання файламі сэрвісаў і іншых сістэмных "
+"адзінак."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
+msgid "Set or unset system and service manager environment variables"
+msgstr "Усталяваць або скінуць зменныя асяроддзя сістэмнага мэнэджэра"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для ўсталявання або скіду зменных асяроддзя "
+"сістэмнага мэнэджэра."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
+msgid "Reload the systemd state"
+msgstr "Перачытаць стан systemd"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
+msgid "Authentication is required to reload the systemd state."
+msgstr "Неабходна аўтэнтыфікацыя для перачытання стану systemd."
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
+msgid "Set host name"
+msgstr "Усталяваць імя вузла"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:2
+msgid "Authentication is required to set the local host name."
+msgstr "Неабходна аўтэнтыфікацыя для ўсталявання імя вузла."
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:3
+msgid "Set static host name"
+msgstr "Усталяваць статычнае імя вузла"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:4
+msgid ""
+"Authentication is required to set the statically configured local host name, "
+"as well as the pretty host name."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для ўсталявання як статычнага так і прыгожага імя "
+"вузла."
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:5
+msgid "Set machine information"
+msgstr "Усталяваць інфармацыю аб машыне"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:6
+msgid "Authentication is required to set local machine information."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для ўсталявання інфармацыі аб лакальнай машыне."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr "Імпартаваць вобраз ВМ або кантэйнера"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+msgid "Authentication is required to import a VM or container image"
+msgstr "Неабходна аўтэнтыфікацыя для імпарту вобраза ВМ або кантэйнера"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr "Экспартаваць вобраз ВМ або кантэйнера"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+msgid "Authentication is required to export a VM or container image"
+msgstr "Неабходна аўтэнтыфікацыя для экспарту вобраза ВМ або кантэйнера"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr "Спампаваць вобраз ВМ або кантэйнера"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
+msgid "Authentication is required to download a VM or container image"
+msgstr "Неабходна аўтэнтыфікацыя для спампоўкі вобраза ВМ або кантэйнера"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
+msgid "Set system locale"
+msgstr "Усталяваць сістэмную лакаль"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:2
+msgid "Authentication is required to set the system locale."
+msgstr "Неабходна аўтэнтыфікацыя для ўсталявання сістэмнай лакалі."
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:3
+msgid "Set system keyboard settings"
+msgstr "Усталяваць сістэмныя налады клавіятуры"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:4
+msgid "Authentication is required to set the system keyboard settings."
+msgstr "Неабходна аўтэнтыфікацыя для ўсталявання сістэмных налад клавіятуры."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:1
+msgid "Allow applications to inhibit system shutdown"
+msgstr "Дазволіць праграмам перашкаджаць выключэнню сістэмы"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:2
+msgid ""
+"Authentication is required for an application to inhibit system shutdown."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам перашкаджаць выключэнню "
+"сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:3
+msgid "Allow applications to delay system shutdown"
+msgstr "Дазволіць праграмам затрымліваць выключэнне сістэмы"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:4
+msgid "Authentication is required for an application to delay system shutdown."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам затрымліваць выключэнне "
+"сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:5
+msgid "Allow applications to inhibit system sleep"
+msgstr "Дазволіць праграмам перашкаджаць засыпанню сістэмы"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:6
+msgid "Authentication is required for an application to inhibit system sleep."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам перашкаджаць засыпанню "
+"сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:7
+msgid "Allow applications to delay system sleep"
+msgstr "Дазволіць праграмам затрымліваць засыпанне сістэмы"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:8
+msgid "Authentication is required for an application to delay system sleep."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам затрымліваць засыпанне "
+"сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:9
+msgid "Allow applications to inhibit automatic system suspend"
+msgstr "Дазволіць праграмам перашкаджаць аўтаматычнаму прыпыненню сістэмы"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:10
+msgid ""
+"Authentication is required for an application to inhibit automatic system "
+"suspend."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам перашкаджаць аўтаматычнаму "
+"прыпыненню сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:11
+msgid "Allow applications to inhibit system handling of the power key"
+msgstr ""
+"Дазволіць праграмам перашкаджаць сістэме апрацоўваць клавішу выключэння"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:12
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the power key."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам перашкаджаць сістэме "
+"апрацоўваць клавішу выключэння."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:13
+msgid "Allow applications to inhibit system handling of the suspend key"
+msgstr ""
+"Дазволіць праграмам перашкаджаць сістэме апрацоўваць клавішу прыпынення"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:14
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the suspend key."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам перашкаджаць сістэме "
+"апрацоўваць клавішу прыпынення."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:15
+msgid "Allow applications to inhibit system handling of the hibernate key"
+msgstr ""
+"Дазволіць праграмам перашкаджаць сістэме апрацоўваць клавішу гібернацыі"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:16
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the hibernate key."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам перашкаджаць сістэме "
+"апрацоўваць клавішу гібернацыі."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:17
+msgid "Allow applications to inhibit system handling of the lid switch"
+msgstr ""
+"Дазволіць праграмам перашкаджаць сістэме апрацоўваць закрыццё крышкі ноўтбука"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:18
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the lid switch."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для дазволу праграмам перашкаджаць сістэме "
+"апрацоўваць закрыццё крышкі ноўтбука."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:19
+msgid "Allow non-logged-in users to run programs"
+msgstr ""
+"Дазволіць карыстальнікам, якія яшчэ не ўвайшлі ў сістэму, выконваць праграмы"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:20
+msgid "Authentication is required to run programs as a non-logged-in user."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для выканання праграм карыстальніка, які яшчэ не "
+"ўвайшоў у сістэму."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:21
+msgid "Allow attaching devices to seats"
+msgstr "Дазволіць далучаць прылады да працоўных месцаў"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:22
+msgid "Authentication is required for attaching a device to a seat."
+msgstr "Неабходна аўтэнтыфікацыя для далучэння прылад да працоўных месцаў."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:23
+msgid "Flush device to seat attachments"
+msgstr "Адключаць прылады ад працоўных месцаў"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:24
+msgid ""
+"Authentication is required for resetting how devices are attached to seats."
+msgstr "Неабходна аўтэнтыфікацыя для адключэння прылад ад працоўных месцаў."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:25
+msgid "Power off the system"
+msgstr "Выключыць сістэму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:26
+msgid "Authentication is required for powering off the system."
+msgstr "Неабходна аўтэнтыфікацыя для выключэння сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:27
+msgid "Power off the system while other users are logged in"
+msgstr "Выключыць сістэму пры прысутнасці іншых карыстальнікаў"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:28
+msgid ""
+"Authentication is required for powering off the system while other users are "
+"logged in."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для выключэння сістэмы пры прысутнасці іншых "
+"карыстальнікаў."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:29
+msgid "Power off the system while an application asked to inhibit it"
+msgstr "Выключыць сістэму, калі праграмы перашкаджаюць гэтаму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:30
+msgid ""
+"Authentication is required for powering off the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для выключэння сістэмы, калі праграмы перашкаджаюць "
+"гэтаму."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:31
+msgid "Reboot the system"
+msgstr "Перазагрузіць сістэму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:32
+msgid "Authentication is required for rebooting the system."
+msgstr "Неабходна аўтэнтыфікацыя для перазагрузкі сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:33
+msgid "Reboot the system while other users are logged in"
+msgstr "Перазагрузіць сістэму пры прысутнасці іншых карыстальнікаў"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:34
+msgid ""
+"Authentication is required for rebooting the system while other users are "
+"logged in."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для перазагрузкі сістэмы пры прысутнасці іншых "
+"карыстальнікаў."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:35
+msgid "Reboot the system while an application asked to inhibit it"
+msgstr "Перазагрузіць сістэму, калі праграмы перашкаджаюць гэтаму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:36
+msgid ""
+"Authentication is required for rebooting the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для перазагрузкі сістэмы, калі праграмы "
+"перашкаджаюць гэтаму."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:37
+msgid "Suspend the system"
+msgstr "Прыпыніць сістэму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:38
+msgid "Authentication is required for suspending the system."
+msgstr "Неабходна аўтэнтыфікацыя для прыпынення сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:39
+msgid "Suspend the system while other users are logged in"
+msgstr "Прыпыніць сістэму пры прысутнасці іншых карыстальнікаў"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:40
+msgid ""
+"Authentication is required for suspending the system while other users are "
+"logged in."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для прыпынення сістэмы пры прысутнасці іншых "
+"карыстальнікаў."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:41
+msgid "Suspend the system while an application asked to inhibit it"
+msgstr "Прыпыніць сістэму, калі праграмы перашкаджаюць гэтаму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:42
+msgid ""
+"Authentication is required for suspending the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для прыпынення сістэмы, калі праграмы перашкаджаюць "
+"гэтаму."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:43
+msgid "Hibernate the system"
+msgstr "Гіберніраваць сістэму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:44
+msgid "Authentication is required for hibernating the system."
+msgstr "Неабходна аўтэнтыфікацыя для гібернацыі сістэмы."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:45
+msgid "Hibernate the system while other users are logged in"
+msgstr "Гіберніраваць сістэму пры прысутнасці іншых карыстальнікаў"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:46
+msgid ""
+"Authentication is required for hibernating the system while other users are "
+"logged in."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для гібернацыі сістэмы пры прысутнасці іншых "
+"карыстальнікаў."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:47
+msgid "Hibernate the system while an application asked to inhibit it"
+msgstr "Гіберніраваць сістэму, калі праграмы перашкаджаюць гэтаму"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:48
+msgid ""
+"Authentication is required for hibernating the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для гібернацыі сістэмы, калі праграмы перашкаджаюць "
+"гэтаму."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr "Кіраваць актыўнымі сесіямі, карыстальнікамі і працоўнымі месцамі"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для кіравання актыўнымі сесіямі, карыстальнікамі і "
+"месцамі."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr "Блакіраваць або разблакіраваць актыўную сесію"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для блакіроўкі або разблакіроўкі актыўнай сесіі."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr "Дазволіць указанне прашыўцы на загрузку інтэрфейсу налад"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для ўказання прашыўцы на загрузку інтэрфейсу налад."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
+msgid "Log into a local container"
+msgstr "Увайсці ў лакальны кантэйнер"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
+msgid "Authentication is required to log into a local container."
+msgstr "Неабходна аўтэнтыфікацыя для ўваходу ў лакальны кантэйнер."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr "Кіраваць лакальнымі віртуальнымі машынамі або кантэйнерамі"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для кіравання лакальнымі віртуальнымі машынамі і "
+"кантэйнерамі."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr "Кіраваць вобразамі лакальных віртуальных машын і кантэйнераў"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для кіравання вобразамі лакальных віртуальных машын "
+"і кантэйнераў."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
+msgid "Set system time"
+msgstr "Усталяваць сістэмны час"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:2
+msgid "Authentication is required to set the system time."
+msgstr "Неабходна аўтэнтыфікацыя для ўсталявання сістэмнага часу."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:3
+msgid "Set system timezone"
+msgstr "Усталяваць сістэмны часавы пояс"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:4
+msgid "Authentication is required to set the system timezone."
+msgstr "Неабходна аўтэнтыфікацыя для ўсталявання сістэмнага часавога поясу."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:5
+msgid "Set RTC to local timezone or UTC"
+msgstr "Усталяваць часавы пояс (мясцовы або UTC), у якім RTC захоўвае час"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:6
+msgid ""
+"Authentication is required to control whether the RTC stores the local or "
+"UTC time."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для ўсталявання часавога поясу (мясцовы або UTC), у "
+"якім захоўваецца час у RTC."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:7
+msgid "Turn network time synchronization on or off"
+msgstr "Уключыць або выключыць сінхранізацыю часу па сетцы"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:8
+msgid ""
+"Authentication is required to control whether network time synchronization "
+"shall be enabled."
+msgstr ""
+"Неабходна аўтэнтыфікацыя для ўключэння або выключэння сінхранізацыі часу па "
+"сетцы."
--- /dev/null
+# Belarusian Latin translation for systemd.
+# Copyright (C) 2015 systemd's COPYRIGHT HOLDER
+# This file is distributed under the same license as the systemd package.
+#
+# Viktar Vaŭčkievič <victorenator@gmail.com>, 2015.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: systemd master\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
+"PO-Revision-Date: 2015-06-14 11:17+0300\n"
+"Last-Translator: Viktar Vaŭčkievič <victorenator@gmail.com>\n"
+"Language-Team: \n"
+"Language: be\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
+"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
+"X-Generator: Lokalize 1.5\n"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
+msgid "Send passphrase back to system"
+msgstr "Adpravić paroĺ nazad sistemie"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
+msgstr "Nieabchodna aŭtentyfikacyja dlia adpraŭki parolia nazad sistemie."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
+msgid "Manage system services or other units"
+msgstr "Kiravać servisami i inšymi sistemnymi adzinkami"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
+msgid "Authentication is required to manage system services or other units."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia kiravannia servisami i inšymi sistemnymi "
+"adzinkami."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
+msgid "Manage system service or unit files"
+msgstr "Kiravać fajlami servisaŭ i inšych sistemnych adzinak"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
+msgid "Authentication is required to manage system service or unit files."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia kiravannia fajlami servisaŭ i inšych "
+"sistemnych adzinak."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
+msgid "Set or unset system and service manager environment variables"
+msgstr "Ustaliavać abo skinuć zmiennyja asiaroddzia sistemnaha menedžera"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia abo skidu zmiennych "
+"asiaroddzia sistemnaha menedžera."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
+msgid "Reload the systemd state"
+msgstr "Pieračytać stan systemd"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
+msgid "Authentication is required to reload the systemd state."
+msgstr "Nieabchodna aŭtentyfikacyja dlia pieračytannia stanu systemd."
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
+msgid "Set host name"
+msgstr "Ustaliavać imia vuzla"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:2
+msgid "Authentication is required to set the local host name."
+msgstr "Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia imia vuzla."
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:3
+msgid "Set static host name"
+msgstr "Ustaliavać statyčnaje imia vuzla"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:4
+msgid ""
+"Authentication is required to set the statically configured local host name, "
+"as well as the pretty host name."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia jak statyčnaha tak i "
+"pryhožaha imia vuzla."
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:5
+msgid "Set machine information"
+msgstr "Ustaliavać infarmacyju ab mašynie"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:6
+msgid "Authentication is required to set local machine information."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia infarmacyi ab lakaĺnaj "
+"mašynie."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr "Impartavać vobraz VM abo kantejniera"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+msgid "Authentication is required to import a VM or container image"
+msgstr "Nieabchodna aŭtentyfikacyja dlia impartu vobraza VM abo kantejniera"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr "Ekspartavać vobraz VM abo kantejniera"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+msgid "Authentication is required to export a VM or container image"
+msgstr "Nieabchodna aŭtentyfikacyja dlia ekspartu vobraza VM abo kantejniera"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr "Spampavać vobraz VM abo kantejniera"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
+msgid "Authentication is required to download a VM or container image"
+msgstr "Nieabchodna aŭtentyfikacyja dlia spampoŭki vobraza VM abo kantejniera"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
+msgid "Set system locale"
+msgstr "Ustaliavać sistemnuju lakaĺ"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:2
+msgid "Authentication is required to set the system locale."
+msgstr "Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia sistemnaj lakali."
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:3
+msgid "Set system keyboard settings"
+msgstr "Ustaliavać sistemnyja nalady klavijatury"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:4
+msgid "Authentication is required to set the system keyboard settings."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia sistemnych nalad klavijatury."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:1
+msgid "Allow applications to inhibit system shutdown"
+msgstr "Dazvolić prahramam pieraškadžać vykliučenniu sistemy"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:2
+msgid ""
+"Authentication is required for an application to inhibit system shutdown."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam pieraškadžać vykliučenniu "
+"sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:3
+msgid "Allow applications to delay system shutdown"
+msgstr "Dazvolić prahramam zatrymlivać vykliučennie sistemy"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:4
+msgid "Authentication is required for an application to delay system shutdown."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam zatrymlivać vykliučennie "
+"sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:5
+msgid "Allow applications to inhibit system sleep"
+msgstr "Dazvolić prahramam pieraškadžać zasypanniu sistemy"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:6
+msgid "Authentication is required for an application to inhibit system sleep."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam pieraškadžać zasypanniu "
+"sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:7
+msgid "Allow applications to delay system sleep"
+msgstr "Dazvolić prahramam zatrymlivać zasypannie sistemy"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:8
+msgid "Authentication is required for an application to delay system sleep."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam zatrymlivać zasypannie "
+"sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:9
+msgid "Allow applications to inhibit automatic system suspend"
+msgstr "Dazvolić prahramam pieraškadžać aŭtamatyčnamu prypynienniu sistemy"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:10
+msgid ""
+"Authentication is required for an application to inhibit automatic system "
+"suspend."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam pieraškadžać "
+"aŭtamatyčnamu prypynienniu sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:11
+msgid "Allow applications to inhibit system handling of the power key"
+msgstr ""
+"Dazvolić prahramam pieraškadžać sistemie apracoŭvać klavišu vykliučennia"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:12
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the power key."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam pieraškadžać sistemie "
+"apracoŭvać klavišu vykliučennia."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:13
+msgid "Allow applications to inhibit system handling of the suspend key"
+msgstr ""
+"Dazvolić prahramam pieraškadžać sistemie apracoŭvać klavišu prypyniennia"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:14
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the suspend key."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam pieraškadžać sistemie "
+"apracoŭvać klavišu prypyniennia."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:15
+msgid "Allow applications to inhibit system handling of the hibernate key"
+msgstr ""
+"Dazvolić prahramam pieraškadžać sistemie apracoŭvać klavišu hibiernacyi"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:16
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the hibernate key."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam pieraškadžać sistemie "
+"apracoŭvać klavišu hibiernacyi."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:17
+msgid "Allow applications to inhibit system handling of the lid switch"
+msgstr ""
+"Dazvolić prahramam pieraškadžać sistemie apracoŭvać zakryccio kryški noŭtbuka"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:18
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the lid switch."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dazvolu prahramam pieraškadžać sistemie "
+"apracoŭvać zakryccio kryški noŭtbuka."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:19
+msgid "Allow non-logged-in users to run programs"
+msgstr ""
+"Dazvolić karystaĺnikam, jakija jašče nie ŭvajšli ŭ sistemu, vykonvać prahramy"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:20
+msgid "Authentication is required to run programs as a non-logged-in user."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia vykanannia prahram karystaĺnika, jaki jašče "
+"nie ŭvajšoŭ u sistemu."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:21
+msgid "Allow attaching devices to seats"
+msgstr "Dazvolić dalučać prylady da pracoŭnych miescaŭ"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:22
+msgid "Authentication is required for attaching a device to a seat."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia dalučennia prylad da pracoŭnych miescaŭ."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:23
+msgid "Flush device to seat attachments"
+msgstr "Adkliučać prylady ad pracoŭnych miescaŭ"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:24
+msgid ""
+"Authentication is required for resetting how devices are attached to seats."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia adkliučennia prylad ad pracoŭnych miescaŭ."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:25
+msgid "Power off the system"
+msgstr "Vykliučyć sistemu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:26
+msgid "Authentication is required for powering off the system."
+msgstr "Nieabchodna aŭtentyfikacyja dlia vykliučennia sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:27
+msgid "Power off the system while other users are logged in"
+msgstr "Vykliučyć sistemu pry prysutnasci inšych karystaĺnikaŭ"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:28
+msgid ""
+"Authentication is required for powering off the system while other users are "
+"logged in."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia vykliučennia sistemy pry prysutnasci inšych "
+"karystaĺnikaŭ."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:29
+msgid "Power off the system while an application asked to inhibit it"
+msgstr "Vykliučyć sistemu, kali prahramy pieraškadžajuć hetamu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:30
+msgid ""
+"Authentication is required for powering off the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia vykliučennia sistemy, kali prahramy "
+"pieraškadžajuć hetamu."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:31
+msgid "Reboot the system"
+msgstr "Pierazahruzić sistemu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:32
+msgid "Authentication is required for rebooting the system."
+msgstr "Nieabchodna aŭtentyfikacyja dlia pierazahruzki sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:33
+msgid "Reboot the system while other users are logged in"
+msgstr "Pierazahruzić sistemu pry prysutnasci inšych karystaĺnikaŭ"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:34
+msgid ""
+"Authentication is required for rebooting the system while other users are "
+"logged in."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia pierazahruzki sistemy pry prysutnasci "
+"inšych karystaĺnikaŭ."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:35
+msgid "Reboot the system while an application asked to inhibit it"
+msgstr "Pierazahruzić sistemu, kali prahramy pieraškadžajuć hetamu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:36
+msgid ""
+"Authentication is required for rebooting the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia pierazahruzki sistemy, kali prahramy "
+"pieraškadžajuć hetamu."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:37
+msgid "Suspend the system"
+msgstr "Prypynić sistemu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:38
+msgid "Authentication is required for suspending the system."
+msgstr "Nieabchodna aŭtentyfikacyja dlia prypyniennia sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:39
+msgid "Suspend the system while other users are logged in"
+msgstr "Prypynić sistemu pry prysutnasci inšych karystaĺnikaŭ"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:40
+msgid ""
+"Authentication is required for suspending the system while other users are "
+"logged in."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia prypyniennia sistemy pry prysutnasci inšych "
+"karystaĺnikaŭ."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:41
+msgid "Suspend the system while an application asked to inhibit it"
+msgstr "Prypynić sistemu, kali prahramy pieraškadžajuć hetamu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:42
+msgid ""
+"Authentication is required for suspending the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia prypyniennia sistemy, kali prahramy "
+"pieraškadžajuć hetamu."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:43
+msgid "Hibernate the system"
+msgstr "Hibierniravać sistemu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:44
+msgid "Authentication is required for hibernating the system."
+msgstr "Nieabchodna aŭtentyfikacyja dlia hibiernacyi sistemy."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:45
+msgid "Hibernate the system while other users are logged in"
+msgstr "Hibierniravać sistemu pry prysutnasci inšych karystaĺnikaŭ"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:46
+msgid ""
+"Authentication is required for hibernating the system while other users are "
+"logged in."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia hibiernacyi sistemy pry prysutnasci inšych "
+"karystaĺnikaŭ."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:47
+msgid "Hibernate the system while an application asked to inhibit it"
+msgstr "Hibierniravać sistemu, kali prahramy pieraškadžajuć hetamu"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:48
+msgid ""
+"Authentication is required for hibernating the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia hibiernacyi sistemy, kali prahramy "
+"pieraškadžajuć hetamu."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr "Kiravać aktyŭnymi siesijami, karystaĺnikami i pracoŭnymi miescami"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia kiravannia aktyŭnymi siesijami, "
+"karystaĺnikami i miescami."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr "Blakiravać abo razblakiravać aktyŭnuju siesiju"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia blakiroŭki abo razblakiroŭki aktyŭnaj "
+"siesii."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr "Dazvolić ukazannie prašyŭcy na zahruzku interfiejsu nalad"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭkazannia prašyŭcy na zahruzku interfiejsu "
+"nalad."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
+msgid "Log into a local container"
+msgstr "Uvajsci ŭ lakaĺny kantejnier"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
+msgid "Authentication is required to log into a local container."
+msgstr "Nieabchodna aŭtentyfikacyja dlia ŭvachodu ŭ lakaĺny kantejnier."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr "Kiravać lakaĺnymi virtuaĺnymi mašynami abo kantejnierami"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia kiravannia lakaĺnymi virtuaĺnymi mašynami i "
+"kantejnierami."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr "Kiravać vobrazami lakaĺnych virtuaĺnych mašyn i kantejnieraŭ"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia kiravannia vobrazami lakaĺnych virtuaĺnych "
+"mašyn i kantejnieraŭ."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
+msgid "Set system time"
+msgstr "Ustaliavać sistemny čas"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:2
+msgid "Authentication is required to set the system time."
+msgstr "Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia sistemnaha času."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:3
+msgid "Set system timezone"
+msgstr "Ustaliavać sistemny časavy pojas"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:4
+msgid "Authentication is required to set the system timezone."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia sistemnaha časavoha pojasu."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:5
+msgid "Set RTC to local timezone or UTC"
+msgstr "Ustaliavać časavy pojas (miascovy abo UTC), u jakim RTC zachoŭvaje čas"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:6
+msgid ""
+"Authentication is required to control whether the RTC stores the local or "
+"UTC time."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭstaliavannia časavoha pojasu (miascovy abo "
+"UTC), u jakim zachoŭvajecca čas u RTC."
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:7
+msgid "Turn network time synchronization on or off"
+msgstr "Ukliučyć abo vykliučyć sinchranizacyju času pa sietcy"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:8
+msgid ""
+"Authentication is required to control whether network time synchronization "
+"shall be enabled."
+msgstr ""
+"Nieabchodna aŭtentyfikacyja dlia ŭkliučennia abo vykliučennia sinchranizacyi "
+"času pa sietcy."
msgid ""
msgstr ""
"Project-Id-Version: systemd master\n"
-"Report-Msgid-Bugs-To: https://bugs.freedesktop.org/enter_bug.cgi?"
-"product=systemd&keywords=I18N+L10N&component=general\n"
-"POT-Creation-Date: 2015-01-22 14:27+0000\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2015-02-18 17:08+0100\n"
"Last-Translator: Martin Pitt <martin.pitt@ubuntu.com>\n"
"Language-Team: German <gnome-de@gnome.org>\n"
"notwendig."
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
-msgid "Manage system services or units"
+#, fuzzy
+msgid "Manage system services or other units"
msgstr "Systemdienste und Einheiten verwalten"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
-#| msgid "Authentication is required to access the system and service manager."
-msgid "Authentication is required to manage system services or units."
+#, fuzzy
+msgid "Authentication is required to manage system services or other units."
msgstr ""
"Legitimierung ist notwendig für die Verwaltung von Systemdiensten und "
"Einheiten"
msgstr "Systemdienste und Einheitendateien verwalten"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
-#| msgid "Authentication is required to access the system and service manager."
msgid "Authentication is required to manage system service or unit files."
msgstr ""
"Legitimierung ist notwendig für die Verwaltung von Systemdiensten und "
"Einheitendateien."
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
-#| msgid "Reboot the system"
+#, fuzzy
+msgid "Set or unset system and service manager environment variables"
+msgstr "Privilegierter Zugriff auf die System- und Dienstverwaltung"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+#, fuzzy
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr ""
+"Legitimierung ist notwendig für die Verwaltung von Systemdiensten und "
+"Einheitendateien."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
msgid "Reload the systemd state"
msgstr "Den systemd-Zustand neu laden"
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
-#| msgid "Authentication is required to set the system time."
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
msgid "Authentication is required to reload the systemd state."
msgstr "Legitimierung ist zum erneuten Laden des systemd-Zustands notwendig."
"erforderlich."
#: ../src/import/org.freedesktop.import1.policy.in.h:1
-msgid "Download a VM or container image"
+#, fuzzy
+msgid "Import a VM or container image"
msgstr "Abbild einer VM oder eines Containers herunterladen"
#: ../src/import/org.freedesktop.import1.policy.in.h:2
-#| msgid "Authentication is required to set local machine information."
+#, fuzzy
+msgid "Authentication is required to import a VM or container image"
+msgstr ""
+"Legitimierung ist zum Herunterladen eines VM- oder Containerabbilds "
+"erforderlich"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+#, fuzzy
+msgid "Export a VM or container image"
+msgstr "Abbild einer VM oder eines Containers herunterladen"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+#, fuzzy
+msgid "Authentication is required to export a VM or container image"
+msgstr ""
+"Legitimierung ist zum Herunterladen eines VM- oder Containerabbilds "
+"erforderlich"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr "Abbild einer VM oder eines Containers herunterladen"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
msgid "Authentication is required to download a VM or container image"
msgstr ""
"Legitimierung ist zum Herunterladen eines VM- oder Containerabbilds "
"Legitimierung ist zum Versetzen des System in den Ruhezustand notwendig, "
"während eine Anwendung wünscht dies zu verhindern."
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr ""
+
+# www.freedesktop.org/wiki/Software/systemd/multiseat/
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+#, fuzzy
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr ""
+"Legitimierung ist zum Anschließen eines Geräts an eine Arbeitsstation "
+"notwendig."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+#, fuzzy
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr "Legitimierung ist zum Anmelden in einem lokalen Container notwendig"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+#, fuzzy
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "Legitimierung ist zum Festlegen des lokalen Rechnernamens notwendig"
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "In einem lokalen Container anmelden"
#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
-#| msgid "Authentication is required to set the local host name."
-msgid "Authentication is required to log into a local container"
+#, fuzzy
+msgid "Authentication is required to log into a local container."
msgstr "Legitimierung ist zum Anmelden in einem lokalen Container notwendig"
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr ""
+"Legitimierung ist zum Festlegen der lokalen Maschinen-Information "
+"erforderlich."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr ""
+"Legitimierung ist zum Herunterladen eines VM- oder Containerabbilds "
+"erforderlich"
+
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
msgid "Set system time"
msgstr "Die Systemzeit festlegen"
"Legitimierung ist zum Festlegen, ob Netzwerkzeitabgeich eingeschaltet sein "
"soll, erforderlich."
-#: ../src/fsckd/fsckd.c:186
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr "Strl+C drücken um laufende Dateisystem-Prüfungen abzubrechen"
-
-#: ../src/fsckd/fsckd.c:227
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] "Prüfe %d Laufwerk (%3.1f%% fertig)"
-msgstr[1] "Prüfe %d Laufwerke (%3.1f%% fertig)"
+#~ msgid "Press Ctrl+C to cancel all filesystem checks in progress"
+#~ msgstr "Strl+C drücken um laufende Dateisystem-Prüfungen abzubrechen"
-#~ msgid "Privileged system and service manager access"
-#~ msgstr "Privilegierter Zugriff auf die System- und Dienstverwaltung"
+#~ msgid "Checking in progress on %d disk (%3.1f%% complete)"
+#~ msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
+#~ msgstr[0] "Prüfe %d Laufwerk (%3.1f%% fertig)"
+#~ msgstr[1] "Prüfe %d Laufwerke (%3.1f%% fertig)"
msgid ""
msgstr ""
"Project-Id-Version: systemd master\n"
-"Report-Msgid-Bugs-To: https://bugs.freedesktop.org/enter_bug.cgi?product=sys"
-"temd&keywords=I18N+L10N&component=general\n"
-"POT-Creation-Date: 2014-04-25 15:51+0000\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2014-04-29 09:17+0300\n"
"Last-Translator: Dimitris Spingos (Δημήτρης Σπίγγος) <dmtrs32@gmail.com>\n"
"Language-Team: team@lists.gnome.gr\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Virtaal 0.7.0\n"
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
+msgid "Send passphrase back to system"
+msgstr "Αποστολή του συνθηματικού πίσω στο σύστημα"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
+msgstr ""
+"Απαιτείται πιστοποίηση για αποστολή του εισερχόμενου συνθηματικού πίσω στο "
+"σύστημα."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
+msgid "Manage system services or other units"
+msgstr ""
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
+#, fuzzy
+msgid "Authentication is required to manage system services or other units."
+msgstr ""
+"Απαιτείται πιστοποίηση για να προσπελάσετε τον διαχειριστή συστήματος και "
+"υπηρεσιών."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
+msgid "Manage system service or unit files"
+msgstr ""
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
+#, fuzzy
+msgid "Authentication is required to manage system service or unit files."
+msgstr ""
+"Απαιτείται πιστοποίηση για να προσπελάσετε τον διαχειριστή συστήματος και "
+"υπηρεσιών."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
+#, fuzzy
+msgid "Set or unset system and service manager environment variables"
+msgstr "Προνομιούχος πρόσβαση διαχειριστή συστήματος και υπηρεσίας"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+#, fuzzy
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr ""
+"Απαιτείται πιστοποίηση για να προσπελάσετε τον διαχειριστή συστήματος και "
+"υπηρεσιών."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
+#, fuzzy
+msgid "Reload the systemd state"
+msgstr "Επανεκκίνηση του συστήματος"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
+#, fuzzy
+msgid "Authentication is required to reload the systemd state."
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε την ώρα του συστήματος."
+
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
msgid "Set host name"
msgstr "Ορισμός ονόματος οικοδεσπότη"
msgid "Authentication is required to set local machine information."
msgstr "Απαιτείται πιστοποίηση για να ορίσετε πληροφορίες τοπικής μηχανής."
+#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+#, fuzzy
+msgid "Authentication is required to import a VM or container image"
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε την ώρα του συστήματος."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+#, fuzzy
+msgid "Authentication is required to export a VM or container image"
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε την ώρα του συστήματος."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
+#, fuzzy
+msgid "Authentication is required to download a VM or container image"
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε πληροφορίες τοπικής μηχανής."
+
#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
msgid "Set system locale"
msgstr "Ορισμός τοπικών ρυθμίσεων συστήματος"
#: ../src/login/org.freedesktop.login1.policy.in.h:2
msgid ""
-"Authentication is required for an application to inhibit system "
-"shutdown."
+"Authentication is required for an application to inhibit system shutdown."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να αποτρέψει τον "
"τερματισμό του συστήματος."
"Να επιτρέπεται στις εφαρμογές να καθυστερούν τον τερματισμό του συστήματος"
#: ../src/login/org.freedesktop.login1.policy.in.h:4
-msgid ""
-"Authentication is required for an application to delay system shutdown."
+msgid "Authentication is required for an application to delay system shutdown."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να καθυστερήσει "
"τον τερματισμό του συστήματος."
msgstr "Να επιτρέπεται στις εφαρμογές να αποτρέπουν την ύπνωση του συστήματος"
#: ../src/login/org.freedesktop.login1.policy.in.h:6
-msgid ""
-"Authentication is required for an application to inhibit system sleep."
+msgid "Authentication is required for an application to inhibit system sleep."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να αποτρέψει την "
"ύπνωση του συστήματος."
msgstr "Να επιτρέπεται στις εφαρμογές να καθυστερούν την ύπνωση του συστήματος"
#: ../src/login/org.freedesktop.login1.policy.in.h:8
-msgid ""
-"Authentication is required for an application to delay system sleep."
+msgid "Authentication is required for an application to delay system sleep."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να καθυστερήσει "
"την ύπνωση του συστήματος."
#: ../src/login/org.freedesktop.login1.policy.in.h:10
msgid ""
-"Authentication is required for an application to inhibit automatic "
-"system suspend."
+"Authentication is required for an application to inhibit automatic system "
+"suspend."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να αποτρέψει την "
"αυτόματη αναστολή του συστήματος."
#: ../src/login/org.freedesktop.login1.policy.in.h:12
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the power key."
+"Authentication is required for an application to inhibit system handling of "
+"the power key."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να αποτρέψει την "
"διαχείριση του πλήκτρου ενεργοποίησης του συστήματος."
#: ../src/login/org.freedesktop.login1.policy.in.h:14
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the suspend key."
+"Authentication is required for an application to inhibit system handling of "
+"the suspend key."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να αποτρέψει την "
"διαχείριση του πλήκτρου αναστολής του συστήματος."
#: ../src/login/org.freedesktop.login1.policy.in.h:16
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the hibernate key."
+"Authentication is required for an application to inhibit system handling of "
+"the hibernate key."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να αποτρέψει την "
"διαχείριση του πλήκτρου αδρανοποίησης του συστήματος."
#: ../src/login/org.freedesktop.login1.policy.in.h:18
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the lid switch."
+"Authentication is required for an application to inhibit system handling of "
+"the lid switch."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μια εφαρμογή να αποτρέψει την "
"διαχείριση του διακόπτη καλύμματος του συστήματος."
msgstr "Να επιτρέπεται σε μη συνδεμένους χρήστες να εκτελούν προγράμματα"
#: ../src/login/org.freedesktop.login1.policy.in.h:20
-msgid ""
-"Authentication is required to run programs as a non-logged-in user."
+msgid "Authentication is required to run programs as a non-logged-in user."
msgstr ""
"Απαιτείται πιστοποίηση για να επιτρέπεται σε μη συνδεμένους χρήστες να "
"εκτελούν προγράμματα."
"Απαιτείται πιστοποίηση για αδρανοποίηση του συστήματος ενώ μια εφαρμογή "
"ζήτησε να αποτραπεί."
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+#, fuzzy
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr ""
+"Απαιτείται πιστοποίηση για προσάρτηση μιας συσκευής σε έναν σταθμό εργασίας."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+#, fuzzy
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε πληροφορίες τοπικής μηχανής."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+#, fuzzy
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε τοπικά όνομα οικοδεσπότη."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
+msgid "Log into a local container"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
+#, fuzzy
+msgid "Authentication is required to log into a local container."
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε τοπικά όνομα οικοδεσπότη."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε πληροφορίες τοπικής μηχανής."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr "Απαιτείται πιστοποίηση για να ορίσετε πληροφορίες τοπικής μηχανής."
+
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
msgid "Set system time"
msgstr "Ορισμός ώρας συστήματος"
msgstr ""
"Απαιτείται πιστοποίηση για να ελέγξετε αν ο συγχρονισμός ώρας δικτύου θα "
"ενεργοποιηθεί."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
-msgid "Send passphrase back to system"
-msgstr "Αποστολή του συνθηματικού πίσω στο σύστημα"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
-msgid ""
-"Authentication is required to send the entered passphrase back to the system."
-msgstr ""
-"Απαιτείται πιστοποίηση για αποστολή του εισερχόμενου συνθηματικού πίσω στο "
-"σύστημα."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
-msgid "Privileged system and service manager access"
-msgstr "Προνομιούχος πρόσβαση διαχειριστή συστήματος και υπηρεσίας"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
-msgid "Authentication is required to access the system and service manager."
-msgstr ""
-"Απαιτείται πιστοποίηση για να προσπελάσετε τον διαχειριστή συστήματος και "
-"υπηρεσιών."
-
-#: ../src/fsckd/fsckd.c:186
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr ""
-
-#: ../src/fsckd/fsckd.c:227
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] ""
-msgstr[1] ""
# This file is distributed under the same license as the systemd package.
# Alex Puchades <alex94puchades@gmail.com>, 2015.
# Daniel Mustieles <daniel.mustieles@gmail.com>, 2015.
+# Álex Puchades <alex94puchades@gmail.com>, 2015.
#
msgid ""
msgstr ""
"Project-Id-Version: systemd master\n"
-"Report-Msgid-Bugs-To: https://bugs.freedesktop.org/enter_bug.cgi?"
-"product=systemd&keywords=I18N+L10N&component=general\n"
-"POT-Creation-Date: 2015-03-11 23:57+0000\n"
-"PO-Revision-Date: 2015-03-12 12:27+0100\n"
-"Last-Translator: Daniel Mustieles <daniel.mustieles@gmail.com>\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
+"PO-Revision-Date: 2015-04-24 13:26+0200\n"
+"Last-Translator: Álex Puchades <alex94puchades@gmail.com>\n"
"Language-Team: Español; Castellano <gnome-es-list@gnome.org>\n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
-"X-Generator: Gtranslator 2.91.6\n"
+"X-Generator: Gtranslator 2.91.7\n"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
msgid "Send passphrase back to system"
msgid "Authentication is required to lock or unlock active sessions."
msgstr "Se requiere autenticación para bloquear/desbloquear sesiones activas."
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+"Permitir indicación al firmware para arrancar la interfaz de configuración"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr ""
+"Se requiere autenticación para indicar al firmware que arranque la interfaz "
+"de configuración."
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Conectarse a un contenedor local"
msgid ""
"Authentication is required to manage local virtual machines and containers."
msgstr ""
-"Se requiere autenticación para administrar máquinas virtuales y contenedores "
-"locales."
+"Se requiere autenticación para administrar las máquinas virtuales y los "
+"contenedores locales."
#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
msgid "Manage local virtual machine and container images"
"Se requiere autenticación para activar/desactivar la sincronización de hora "
"por red."
-#: ../src/fsckd/fsckd.c:291
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr ""
-"Presione Ctrl+C para cancelar todas las comprobaciones del sistema de "
-"archivos en curso"
+#~ msgid "Press Ctrl+C to cancel all filesystem checks in progress"
+#~ msgstr ""
+#~ "Presione Ctrl+C para cancelar todas las comprobaciones del sistema de "
+#~ "archivos en curso"
-#: ../src/fsckd/fsckd.c:336
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] "Comprobando progreso en %d disco (%3.1f%% completado)"
-msgstr[1] "Comprobando progreso en %d discos (%3.1f%% completado)"
+#~ msgid "Checking in progress on %d disk (%3.1f%% complete)"
+#~ msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
+#~ msgstr[0] "Comprobando progreso en %d disco (%3.1f %% completado)"
+#~ msgstr[1] "Comprobando progreso en %d discos (%3.1f %% completado)"
msgstr ""
"Project-Id-Version: systemd\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2015-03-09 18:30+0100\n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2014-12-28 13:04+0100\n"
"Last-Translator: Sylvain Plantefève <sylvain.plantefeve@gmail.com>\n"
"Language-Team: French\n"
"Authentification requise pour verrouiller ou déverrouiller des sessions "
"actives."
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+"Permet d'indiquer au micrologiciel de démarrer sur l'interface de "
+"configuration"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr ""
+"Authentification requise pour indiquer au micrologiciel de démarrer sur "
+"l'interface de configuration."
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Connexion dans un conteneur local"
"Authentification requise pour activer ou désactiver la synchronisation de "
"l'heure avec le réseau."
-#: ../src/fsckd/fsckd.c:186
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr ""
-"Appuyez sur Ctrl+C pour annuler toutes vérifications en cours du système de "
-"fichiers"
+#~ msgid "Press Ctrl+C to cancel all filesystem checks in progress"
+#~ msgstr ""
+#~ "Appuyez sur Ctrl+C pour annuler toutes vérifications en cours du système "
+#~ "de fichiers"
-#: ../src/fsckd/fsckd.c:227
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] "Vérification en cours sur %d disque (%3.1f%% complété)"
-msgstr[1] "Vérification en cours sur %d disques (%3.1f%% complété)"
+#~ msgid "Checking in progress on %d disk (%3.1f%% complete)"
+#~ msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
+#~ msgstr[0] "Vérification en cours sur %d disque (%3.1f%% complété)"
+#~ msgstr[1] "Vérification en cours sur %d disques (%3.1f%% complété)"
msgstr ""
"Project-Id-Version: systemd master\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2015-01-02 22:59+0100\n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2015-01-02 22:58+0100\n"
"Last-Translator: Gabor Kelemen <kelemeng at ubuntu dot com>\n"
"Language-Team: Hungarian <openscope at googlegroups dot com>\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Lokalize 1.5\n"
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
+msgid "Send passphrase back to system"
+msgstr "Jelmondat visszaküldése a rendszernek"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
+msgstr ""
+"Hitelesítés szükséges a bevitt jelmondat visszaküldéséhez a rendszernek."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
+#, fuzzy
+msgid "Manage system services or other units"
+msgstr "Rendszerszolgáltatások vagy -egységek kezelése"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
+#, fuzzy
+msgid "Authentication is required to manage system services or other units."
+msgstr ""
+"Hitelesítés szükséges a rendszerszolgáltatások vagy -egységek kezeléséhez."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
+msgid "Manage system service or unit files"
+msgstr "Rendszerszolgáltatás- vagy egységfájlok kezelése"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
+msgid "Authentication is required to manage system service or unit files."
+msgstr ""
+"Hitelesítés szükséges a rendszerszolgáltatás- vagy egységfájlok kezeléséhez."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
+msgid "Set or unset system and service manager environment variables"
+msgstr ""
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+#, fuzzy
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr ""
+"Hitelesítés szükséges a rendszerszolgáltatás- vagy egységfájlok kezeléséhez."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
+msgid "Reload the systemd state"
+msgstr "A systemd állapotának újratöltése"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
+msgid "Authentication is required to reload the systemd state."
+msgstr "Hitelesítés szükséges a systemd állapotának újratöltéséhez."
+
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
msgid "Set host name"
msgstr "Gépnév beállítása"
msgid "Authentication is required to set local machine information."
msgstr "Hitelesítés szükséges a helyi gép információinak beállításához."
+#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+#, fuzzy
+msgid "Authentication is required to import a VM or container image"
+msgstr "Hitelesítés szükséges a bejelentkezéshez egy helyi konténerbe."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+#, fuzzy
+msgid "Authentication is required to export a VM or container image"
+msgstr "Hitelesítés szükséges a bejelentkezéshez egy helyi konténerbe."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
+#, fuzzy
+msgid "Authentication is required to download a VM or container image"
+msgstr "Hitelesítés szükséges a bejelentkezéshez egy helyi konténerbe."
+
#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
msgid "Set system locale"
msgstr "Területi beállítás megadása"
"Hitelesítés szükséges a rendszer hibernálásához miközben egy alkalmazás "
"ennek meggátlását kérte."
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+#, fuzzy
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr ""
+"Hitelesítés szükséges eszköz csatolásának engedélyezéséhez egy "
+"munkaállomáshoz"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+#, fuzzy
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr "Hitelesítés szükséges a bejelentkezéshez egy helyi konténerbe."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+#, fuzzy
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "Hitelesítés szükséges a helyi gépnév beállításához."
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Bejelentkezés helyi konténerbe"
#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
-msgid "Authentication is required to log into a local container"
+#, fuzzy
+msgid "Authentication is required to log into a local container."
msgstr "Hitelesítés szükséges a bejelentkezéshez egy helyi konténerbe."
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr "Hitelesítés szükséges a helyi gép információinak beállításához."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr "Hitelesítés szükséges a helyi gép információinak beállításához."
+
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
msgid "Set system time"
msgstr "Rendszeridő beállítása"
"Authentication is required to control whether network time synchronization "
"shall be enabled."
msgstr "Hitelesítés szükséges a hálózati időszinkronizáció engedélyezéséhez."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
-msgid "Send passphrase back to system"
-msgstr "Jelmondat visszaküldése a rendszernek"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
-msgid ""
-"Authentication is required to send the entered passphrase back to the system."
-msgstr ""
-"Hitelesítés szükséges a bevitt jelmondat visszaküldéséhez a rendszernek."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
-msgid "Manage system services or units"
-msgstr "Rendszerszolgáltatások vagy -egységek kezelése"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
-msgid "Authentication is required to manage system services or units."
-msgstr ""
-"Hitelesítés szükséges a rendszerszolgáltatások vagy -egységek kezeléséhez."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
-msgid "Manage system service or unit files"
-msgstr "Rendszerszolgáltatás- vagy egységfájlok kezelése"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
-msgid "Authentication is required to manage system service or unit files."
-msgstr ""
-"Hitelesítés szükséges a rendszerszolgáltatás- vagy egységfájlok kezeléséhez."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
-msgid "Reload the systemd state"
-msgstr "A systemd állapotának újratöltése"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
-msgid "Authentication is required to reload the systemd state."
-msgstr "Hitelesítés szükséges a systemd állapotának újratöltéséhez."
-
-#: ../src/fsckd/fsckd.c:186
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr ""
-
-#: ../src/fsckd/fsckd.c:227
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] ""
-msgstr[1] ""
msgstr ""
"Project-Id-Version: systemd\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2015-02-19 16:03+0100\n"
-"PO-Revision-Date: 2015-02-19 16:34+0100\n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
+"PO-Revision-Date: 2015-06-10 23:10+0100\n"
"Last-Translator: Daniele Medri <dmedri@gmail.com>\n"
"Language-Team: Italian\n"
"Language: it\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
-"X-Generator: Poedit 1.7.3\n"
+"X-Generator: Poedit 1.7.6\n"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
msgid "Send passphrase back to system"
"locale."
#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr "Importa un'immagine VM o un container"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+msgid "Authentication is required to import a VM or container image"
+msgstr "Autenticazione richiesta per importare un'immagine VM o un container"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr "Esporta un'immagine VM o un container"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+msgid "Authentication is required to export a VM or container image"
+msgstr "Autenticazione richiesta per esportare un'immagine VM o un container"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
msgid "Download a VM or container image"
msgstr "Scarica un'immagine VM o un container"
-#: ../src/import/org.freedesktop.import1.policy.in.h:2
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
msgid "Authentication is required to download a VM or container image"
msgstr "Autenticazione richiesta per scaricare un'immagine VM o un container"
msgid "Authentication is required to lock or unlock active sessions."
msgstr "Autenticazione richiesta per bloccare o sbloccare le sessioni attive."
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+"Permette indicazioni per il firmware per avviare l'interfaccia di "
+"configurazione"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr ""
+"Autenticazione richiesta per indicare al firmware di avviare l'interfaccia "
+"di configurazione."
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Accedi in un container locale"
msgstr ""
"Autenticazione richiesta per verificare se la sincronizzazione dell'orario "
"in rete possa essere attivata."
-
-#: ../src/fsckd/fsckd.c:186
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr "Premere Ctrl+C per annullare i controlli in corso sul filesystem"
-
-#: ../src/fsckd/fsckd.c:227
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] "Controllo in corso sul disco %d (%3.1f%% completato)"
-msgstr[1] "Controllo in corso sui dischi %d (%3.1f%% completato)"
msgstr ""
"Project-Id-Version: systemd\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2015-03-15 22:59+0100\n"
-"PO-Revision-Date: 2015-03-15 23:00+0100\n"
+"POT-Creation-Date: 2015-09-06 20:39+0200\n"
+"PO-Revision-Date: 2015-09-06 20:40+0200\n"
"Last-Translator: Piotr Drąg <piotrdrag@gmail.com>\n"
"Language-Team: Polish <trans-pl@lists.fedoraproject.org>\n"
"Language: pl\n"
msgstr ""
"Wymagane jest uwierzytelnienie, aby zablokować lub odblokować aktywne sesje."
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr "Wskazanie oprogramowaniu sprzętowemu, aby uruchomić interfejs ustawień"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby wskazać oprogramowaniu sprzętowemu, że "
+"należy uruchomić interfejs ustawień."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:55
+msgid "Set a wall message"
+msgstr "Ustawienie komunikatu wall"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:56
+msgid "Authentication is required to set a wall message"
+msgstr "Wymagane jest uwierzytelnienie, aby ustawić komunikat wall"
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Logowanie do lokalnego kontenera"
"Wymagane jest uwierzytelnienie, aby zalogować się do lokalnego kontenera."
#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Log into the local host"
+msgstr "Logowanie do lokalnego komputera"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+msgid "Authentication is required to log into the local host."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby zalogować się do lokalnego komputera."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Acquire a shell in a local container"
+msgstr "Uzyskanie powłoki w lokalnym kontenerze"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+msgid "Authentication is required to acquire a shell in a local container."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby uzyskać powłokę w lokalnym kontenerze."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:7
+msgid "Acquire a shell on the local host"
+msgstr "Uzyskanie powłoki na lokalnym komputerze"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:8
+msgid "Authentication is required to acquire a shell on the local host."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby uzyskać powłokę na lokalnym komputerze."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:9
+msgid "Acquire a pseudo TTY in a local container"
+msgstr "Uzyskanie pseudo-TTY w lokalnym kontenerze"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:10
+msgid ""
+"Authentication is required to acquire a pseudo TTY in a local container."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby uzyskać pseudo-TTY w lokalnym kontenerze."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:11
+msgid "Acquire a pseudo TTY on the local host"
+msgstr "Uzyskanie pseudo-TTY na lokalnym komputerze"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:12
+msgid "Authentication is required to acquire a pseudo TTY on the local host."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby uzyskać pseudo-TTY na lokalnym "
+"komputerze."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:13
msgid "Manage local virtual machines and containers"
msgstr "Zarządzanie lokalnymi maszynami wirtualnymi i kontenerami"
-#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:14
msgid ""
"Authentication is required to manage local virtual machines and containers."
msgstr ""
"Wymagane jest uwierzytelnienie, aby zarządzać lokalnymi maszynami "
"wirtualnymi i kontenerami."
-#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:15
msgid "Manage local virtual machine and container images"
msgstr "Zarządzanie lokalnymi obrazami maszyn wirtualnych i kontenerów"
-#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:16
msgid ""
"Authentication is required to manage local virtual machine and container "
"images."
"Wymagane jest uwierzytelnienie, aby kontrolować, czy włączyć synchronizację "
"czasu przez sieć."
-#: ../src/fsckd/fsckd.c:291
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
+#: ../src/core/dbus-unit.c:428
+msgid "Authentication is required to start '$(unit)'."
+msgstr "Wymagane jest uwierzytelnienie, aby uruchomić jednostkę „$(unit)”."
+
+#: ../src/core/dbus-unit.c:429
+msgid "Authentication is required to stop '$(unit)'."
+msgstr "Wymagane jest uwierzytelnienie, aby zatrzymać jednostkę „$(unit)”."
+
+#: ../src/core/dbus-unit.c:430
+msgid "Authentication is required to reload '$(unit)'."
msgstr ""
-"Naciśnięcie klawiszy Ctrl+C anuluje wszystkie trwające procesy sprawdzania "
-"systemów plików"
+"Wymagane jest uwierzytelnienie, aby ponownie wczytać jednostkę „$(unit)”."
+
+#: ../src/core/dbus-unit.c:431 ../src/core/dbus-unit.c:432
+msgid "Authentication is required to restart '$(unit)'."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby ponownie uruchomić jednostkę „$(unit)”."
+
+#: ../src/core/dbus-unit.c:535
+msgid "Authentication is required to kill '$(unit)'."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby wymusić wyłączenie jednostki „$(unit)”."
+
+#: ../src/core/dbus-unit.c:565
+msgid "Authentication is required to reset the \"failed\" state of '$(unit)'."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby przywrócić stan „failed” (niepowodzenia) "
+"jednostki „$(unit)”."
+
+#: ../src/core/dbus-unit.c:597
+msgid "Authentication is required to set properties on '$(unit)'."
+msgstr ""
+"Wymagane jest uwierzytelnienie, aby ustawić właściwości jednostki „$(unit)”."
+
+#~ msgid "Press Ctrl+C to cancel all filesystem checks in progress"
+#~ msgstr ""
+#~ "Naciśnięcie klawiszy Ctrl+C anuluje wszystkie trwające procesy "
+#~ "sprawdzania systemów plików"
-#: ../src/fsckd/fsckd.c:336
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] "Trwa sprawdzanie %d dysku (ukończono %3.1f%%)"
-msgstr[1] "Trwa sprawdzanie %d dysków (ukończono %3.1f%%)"
-msgstr[2] "Trwa sprawdzanie %d dysków (ukończono %3.1f%%)"
+#~ msgid "Checking in progress on %d disk (%3.1f%% complete)"
+#~ msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
+#~ msgstr[0] "Trwa sprawdzanie %d dysku (ukończono %3.1f%%)"
+#~ msgstr[1] "Trwa sprawdzanie %d dysków (ukończono %3.1f%%)"
+#~ msgstr[2] "Trwa sprawdzanie %d dysków (ukończono %3.1f%%)"
msgid ""
msgstr ""
"Project-Id-Version: systemd master\n"
-"Report-Msgid-Bugs-To: https://bugs.freedesktop.org/enter_bug.cgi?"
-"product=systemd&keywords=I18N+L10N&component=general\n"
-"POT-Creation-Date: 2015-01-01 21:18+0000\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2015-01-10 12:23-0300\n"
"Last-Translator: Rafael Ferreira <rafael.f.f1@gmail.com>\n"
"Language-Team: Brazilian Portuguese <gnome-pt_br-list@gnome.org>\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Poedit 1.7.1\n"
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
+msgid "Send passphrase back to system"
+msgstr "Enviar frase secreta de volta ao sistema"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
+msgstr ""
+"É necessária autenticação para enviar a frase secreta informada de volta ao "
+"sistema."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
+#, fuzzy
+msgid "Manage system services or other units"
+msgstr "Gerenciar unidades e serviços do sistema"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
+#, fuzzy
+msgid "Authentication is required to manage system services or other units."
+msgstr ""
+"É necessária autenticação para gerenciar unidades e serviços do sistema."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
+msgid "Manage system service or unit files"
+msgstr "Gerenciar arquivos de unidades e serviços do sistema"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
+msgid "Authentication is required to manage system service or unit files."
+msgstr ""
+"É necessária autenticação para gerenciar arquivos \"unit\" e \"service\" do "
+"sistema."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
+#, fuzzy
+msgid "Set or unset system and service manager environment variables"
+msgstr "Acesso privilegiado ao gerenciador de serviço e de sistema"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+#, fuzzy
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr ""
+"É necessária autenticação para gerenciar arquivos \"unit\" e \"service\" do "
+"sistema."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
+msgid "Reload the systemd state"
+msgstr "Recarregar o estado do sistema"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
+msgid "Authentication is required to reload the systemd state."
+msgstr "É necessária autenticação para recarregar o estado do sistema."
+
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
msgid "Set host name"
msgstr "Definir nome de máquina"
msgid "Authentication is required to set local machine information."
msgstr "É necessária autenticação para definir informações de máquina local."
+#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+#, fuzzy
+msgid "Authentication is required to import a VM or container image"
+msgstr "É necessária autenticação para se conectar a um contêiner local."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+#, fuzzy
+msgid "Authentication is required to export a VM or container image"
+msgstr "É necessária autenticação para se conectar a um contêiner local."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
+#, fuzzy
+msgid "Authentication is required to download a VM or container image"
+msgstr "É necessária autenticação para se conectar a um contêiner local."
+
#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
msgid "Set system locale"
msgstr "Definir configurações regionais do sistema"
"É necessária autenticação para hibernar o sistema enquanto um aplicativo "
"solicitou inibição."
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+#, fuzzy
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr "É necessária autenticação para conectar um dispositivo em uma estação."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+#, fuzzy
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr "É necessária autenticação para se conectar a um contêiner local."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+#, fuzzy
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "É necessária autenticação para definir nome de máquina local."
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Conectar a um contêiner local"
#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
-msgid "Authentication is required to log into a local container"
+#, fuzzy
+msgid "Authentication is required to log into a local container."
msgstr "É necessária autenticação para se conectar a um contêiner local."
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr "É necessária autenticação para definir informações de máquina local."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr "É necessária autenticação para definir informações de máquina local."
+
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
msgid "Set system time"
msgstr "Definir horário do sistema"
msgstr ""
"É necessária autenticação para controlar se deve ser habilitada, ou não, a "
"sincronização de horário através de rede."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
-msgid "Send passphrase back to system"
-msgstr "Enviar frase secreta de volta ao sistema"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
-msgid ""
-"Authentication is required to send the entered passphrase back to the system."
-msgstr ""
-"É necessária autenticação para enviar a frase secreta informada de volta ao "
-"sistema."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
-msgid "Manage system services or units"
-msgstr "Gerenciar unidades e serviços do sistema"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
-msgid "Authentication is required to manage system services or units."
-msgstr ""
-"É necessária autenticação para gerenciar unidades e serviços do sistema."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
-msgid "Manage system service or unit files"
-msgstr "Gerenciar arquivos de unidades e serviços do sistema"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
-msgid "Authentication is required to manage system service or unit files."
-msgstr ""
-"É necessária autenticação para gerenciar arquivos \"unit\" e \"service\" do "
-"sistema."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
-msgid "Reload the systemd state"
-msgstr "Recarregar o estado do sistema"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
-msgid "Authentication is required to reload the systemd state."
-msgstr "É necessária autenticação para recarregar o estado do sistema."
-
-#~ msgid "Privileged system and service manager access"
-#~ msgstr "Acesso privilegiado ao gerenciador de serviço e de sistema"
-
-#: ../src/fsckd/fsckd.c:186
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr ""
-
-#: ../src/fsckd/fsckd.c:227
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] ""
-msgstr[1] ""
msgid ""
msgstr ""
"Project-Id-Version: systemd\n"
-"Report-Msgid-Bugs-To: 0comffdiz@inbox.ru\n"
-"POT-Creation-Date: 2013-03-24 19:22+0300\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2015-03-22 21:53+0300\n"
"Last-Translator: Sergey Ptashnick <0comffdiz@inbox.ru>\n"
"Language: ru\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
-"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
+"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
+"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
msgid "Send passphrase back to system"
msgstr "Отправить пароль системе"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
-msgid "Authentication is required to send the entered passphrase back to the system."
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
msgstr "Чтобы отправить пароль системе, необходимо пройти аутентификацию."
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
-msgid "Manage system services or units"
+#, fuzzy
+msgid "Manage system services or other units"
msgstr "Управление системными службами и юнитами"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
-msgid "Authentication is required to manage system services or units."
-msgstr "Для управления системными службами и юнитами, необходимо пройти аутентификацию."
+#, fuzzy
+msgid "Authentication is required to manage system services or other units."
+msgstr ""
+"Для управления системными службами и юнитами, необходимо пройти "
+"аутентификацию."
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
msgid "Manage system service or unit files"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
msgid "Authentication is required to manage system service or unit files."
-msgstr "Для управления файлами конфигурации системных служб и юнитов, необходимо пройти аутентификацию."
+msgstr ""
+"Для управления файлами конфигурации системных служб и юнитов, необходимо "
+"пройти аутентификацию."
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
msgid "Set or unset system and service manager environment variables"
msgstr "Настроить переменные окружения для системного менеджера"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
-msgid "Authentication is required to set or unset system and service manager environment variables."
-msgstr "Чтобы настроить переменные окружения для системного менеджера, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr ""
+"Чтобы настроить переменные окружения для системного менеджера, необходимо "
+"пройти аутентификацию."
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
msgid "Reload the systemd state"
#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
msgid "Authentication is required to reload the systemd state."
-msgstr "Чтобы заставить systemd перечитать конфигурацию, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы заставить systemd перечитать конфигурацию, необходимо пройти "
+"аутентификацию."
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
msgid "Set host name"
msgstr "Настроить статическое имя компьютера"
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:4
-msgid "Authentication is required to set the statically configured local host name, as well as the pretty host name."
-msgstr "Чтобы настроить статическое имя компьютера, а также его «красивое» имя, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required to set the statically configured local host name, "
+"as well as the pretty host name."
+msgstr ""
+"Чтобы настроить статическое имя компьютера, а также его «красивое» имя, "
+"необходимо пройти аутентификацию."
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:5
msgid "Set machine information"
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:6
msgid "Authentication is required to set local machine information."
-msgstr "Чтобы настроить информацию о компьютере, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы настроить информацию о компьютере, необходимо пройти аутентификацию."
#: ../src/import/org.freedesktop.import1.policy.in.h:1
msgid "Import a VM or container image"
#: ../src/import/org.freedesktop.import1.policy.in.h:2
msgid "Authentication is required to import a VM or container image"
-msgstr "Чтобы импортировать образ виртуальной машины или контейнера, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы импортировать образ виртуальной машины или контейнера, необходимо "
+"пройти аутентификацию."
#: ../src/import/org.freedesktop.import1.policy.in.h:3
msgid "Export a VM or container image"
#: ../src/import/org.freedesktop.import1.policy.in.h:4
msgid "Authentication is required to export a VM or container image"
-msgstr "Чтобы экспортировать образ виртуальной машины или контейнера, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы экспортировать образ виртуальной машины или контейнера, необходимо "
+"пройти аутентификацию."
#: ../src/import/org.freedesktop.import1.policy.in.h:5
msgid "Download a VM or container image"
#: ../src/import/org.freedesktop.import1.policy.in.h:6
msgid "Authentication is required to download a VM or container image"
-msgstr "Чтобы загрузить образ виртуальной машины или контейнера, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы загрузить образ виртуальной машины или контейнера, необходимо пройти "
+"аутентификацию."
#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
msgid "Set system locale"
#: ../src/locale/org.freedesktop.locale1.policy.in.h:4
msgid "Authentication is required to set the system keyboard settings."
-msgstr "Чтобы настроить параметры клавиатуры, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы настроить параметры клавиатуры, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:1
msgid "Allow applications to inhibit system shutdown"
msgstr "Разрешить приложениям устанавливать блокировку на выключение системы"
#: ../src/login/org.freedesktop.login1.policy.in.h:2
-msgid "Authentication is required for an application to inhibit system shutdown."
-msgstr "Чтобы разрешить приложениям устанавливать блокировку на выключение системы, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for an application to inhibit system shutdown."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать блокировку на выключение системы, "
+"необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:3
msgid "Allow applications to delay system shutdown"
#: ../src/login/org.freedesktop.login1.policy.in.h:4
msgid "Authentication is required for an application to delay system shutdown."
-msgstr "Чтобы разрешить приложениям устанавливать задержку на выключение системы, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать задержку на выключение системы, "
+"необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:5
msgid "Allow applications to inhibit system sleep"
#: ../src/login/org.freedesktop.login1.policy.in.h:6
msgid "Authentication is required for an application to inhibit system sleep."
-msgstr "Чтобы разрешить приложениям устанавливать блокировку на засыпание системы, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать блокировку на засыпание системы, "
+"необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:7
msgid "Allow applications to delay system sleep"
#: ../src/login/org.freedesktop.login1.policy.in.h:8
msgid "Authentication is required for an application to delay system sleep."
-msgstr "Чтобы разрешить приложениям устанавливать задержку на засыпание системы, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать задержку на засыпание системы, "
+"необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:9
msgid "Allow applications to inhibit automatic system suspend"
-msgstr "Разрешить приложениям устанавливать блокировку на автоматический переход системы в ждущий режим"
+msgstr ""
+"Разрешить приложениям устанавливать блокировку на автоматический переход "
+"системы в ждущий режим"
#: ../src/login/org.freedesktop.login1.policy.in.h:10
-msgid "Authentication is required for an application to inhibit automatic system suspend."
-msgstr "Чтобы разрешить приложениям устанавливать блокировку на автоматический переход системы в ждущий режим, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for an application to inhibit automatic system "
+"suspend."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать блокировку на автоматический "
+"переход системы в ждущий режим, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:11
msgid "Allow applications to inhibit system handling of the power key"
-msgstr "Разрешить приложениям устанавливать блокировку обработки нажатий на кнопку выключения"
+msgstr ""
+"Разрешить приложениям устанавливать блокировку обработки нажатий на кнопку "
+"выключения"
#: ../src/login/org.freedesktop.login1.policy.in.h:12
-msgid "Authentication is required for an application to inhibit system handling of the power key."
-msgstr "Чтобы разрешить приложениям устанавливать блокировку обработки нажатий на кнопку выключения, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the power key."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать блокировку обработки нажатий на "
+"кнопку выключения, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:13
msgid "Allow applications to inhibit system handling of the suspend key"
-msgstr "Разрешить приложениям устанавливать блокировку обработки нажатий на кнопку перехода в ждущий режим"
+msgstr ""
+"Разрешить приложениям устанавливать блокировку обработки нажатий на кнопку "
+"перехода в ждущий режим"
#: ../src/login/org.freedesktop.login1.policy.in.h:14
-msgid "Authentication is required for an application to inhibit system handling of the suspend key."
-msgstr "Чтобы разрешить приложениям устанавливать блокировку обработки нажатий на кнопку перехода в ждущий режим, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the suspend key."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать блокировку обработки нажатий на "
+"кнопку перехода в ждущий режим, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:15
msgid "Allow applications to inhibit system handling of the hibernate key"
-msgstr "Разрешить приложениям устанавливать блокировку обработки нажатий на кнопку перехода в спящий режим"
+msgstr ""
+"Разрешить приложениям устанавливать блокировку обработки нажатий на кнопку "
+"перехода в спящий режим"
#: ../src/login/org.freedesktop.login1.policy.in.h:16
-msgid "Authentication is required for an application to inhibit system handling of the hibernate key."
-msgstr "Чтобы разрешить приложениям устанавливать блокировку обработки нажатий на кнопку перехода в спящий режим, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the hibernate key."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать блокировку обработки нажатий на "
+"кнопку перехода в спящий режим, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:17
msgid "Allow applications to inhibit system handling of the lid switch"
-msgstr "Разрешить приложениям устанавливать блокировку на обработку закрытия крышки ноутбука"
+msgstr ""
+"Разрешить приложениям устанавливать блокировку на обработку закрытия крышки "
+"ноутбука"
#: ../src/login/org.freedesktop.login1.policy.in.h:18
-msgid "Authentication is required for an application to inhibit system handling of the lid switch."
-msgstr "Чтобы разрешить приложениям устанавливать блокировку на обработку закрытия крышки ноутбука, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the lid switch."
+msgstr ""
+"Чтобы разрешить приложениям устанавливать блокировку на обработку закрытия "
+"крышки ноутбука, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:19
msgid "Allow non-logged-in users to run programs"
-msgstr "Разрешить пользователям оставлять программы в фоновом режиме после завершения сеанса"
+msgstr ""
+"Разрешить пользователям оставлять программы в фоновом режиме после "
+"завершения сеанса"
#: ../src/login/org.freedesktop.login1.policy.in.h:20
msgid "Authentication is required to run programs as a non-logged-in user."
-msgstr "Чтобы разрешить пользователям оставлять программы в фоновом режиме после завершения сеанса, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы разрешить пользователям оставлять программы в фоновом режиме после "
+"завершения сеанса, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:21
msgid "Allow attaching devices to seats"
#: ../src/login/org.freedesktop.login1.policy.in.h:22
msgid "Authentication is required for attaching a device to a seat."
-msgstr "Чтобы разрешить подключение устройств к рабочим местам, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы разрешить подключение устройств к рабочим местам, необходимо пройти "
+"аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:23
msgid "Flush device to seat attachments"
msgstr "Сбросить привязки устройств к рабочим местам"
#: ../src/login/org.freedesktop.login1.policy.in.h:24
-msgid "Authentication is required for resetting how devices are attached to seats."
-msgstr "Чтобы сбросить привязки устройств к рабочим местам, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for resetting how devices are attached to seats."
+msgstr ""
+"Чтобы сбросить привязки устройств к рабочим местам, необходимо пройти "
+"аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:25
msgid "Power off the system"
#: ../src/login/org.freedesktop.login1.policy.in.h:27
msgid "Power off the system while other users are logged in"
-msgstr "Выключить систему, несмотря на то, что в ней работают другие пользователи"
+msgstr ""
+"Выключить систему, несмотря на то, что в ней работают другие пользователи"
#: ../src/login/org.freedesktop.login1.policy.in.h:28
-msgid "Authentication is required for powering off the system while other users are logged in."
-msgstr "Чтобы выключить систему, несмотря на то, что в ней работают другие пользователи, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for powering off the system while other users are "
+"logged in."
+msgstr ""
+"Чтобы выключить систему, несмотря на то, что в ней работают другие "
+"пользователи, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:29
msgid "Power off the system while an application asked to inhibit it"
-msgstr "Выключить систему, несмотря на то, что приложение запросило блокировку выключения"
+msgstr ""
+"Выключить систему, несмотря на то, что приложение запросило блокировку "
+"выключения"
#: ../src/login/org.freedesktop.login1.policy.in.h:30
-msgid "Authentication is required for powering off the system while an application asked to inhibit it."
-msgstr "Чтобы выключить систему, несмотря на то, что приложение запросило блокировку выключения, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for powering off the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Чтобы выключить систему, несмотря на то, что приложение запросило блокировку "
+"выключения, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:31
msgid "Reboot the system"
#: ../src/login/org.freedesktop.login1.policy.in.h:33
msgid "Reboot the system while other users are logged in"
-msgstr "Перезагрузить систему, несмотря на то, что в ней работают другие пользователи"
+msgstr ""
+"Перезагрузить систему, несмотря на то, что в ней работают другие пользователи"
#: ../src/login/org.freedesktop.login1.policy.in.h:34
-msgid "Authentication is required for rebooting the system while other users are logged in."
-msgstr "Чтобы перезагрузить систему, несмотря на то, что в ней работают другие пользователи, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for rebooting the system while other users are "
+"logged in."
+msgstr ""
+"Чтобы перезагрузить систему, несмотря на то, что в ней работают другие "
+"пользователи, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:35
msgid "Reboot the system while an application asked to inhibit it"
-msgstr "Перезагрузить систему, несмотря на то, что приложение запросило блокировку выключения"
+msgstr ""
+"Перезагрузить систему, несмотря на то, что приложение запросило блокировку "
+"выключения"
#: ../src/login/org.freedesktop.login1.policy.in.h:36
-msgid "Authentication is required for rebooting the system while an application asked to inhibit it."
-msgstr "Чтобы перезагрузить систему, несмотря на то, что приложение запросило блокировку выключения, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for rebooting the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Чтобы перезагрузить систему, несмотря на то, что приложение запросило "
+"блокировку выключения, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:37
msgid "Suspend the system"
#: ../src/login/org.freedesktop.login1.policy.in.h:38
msgid "Authentication is required for suspending the system."
-msgstr "Чтобы перевести систему в ждущий режим, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы перевести систему в ждущий режим, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:39
msgid "Suspend the system while other users are logged in"
-msgstr "Перевести систему в ждущий режим, несмотря на то, что в ней работают другие пользователи"
+msgstr ""
+"Перевести систему в ждущий режим, несмотря на то, что в ней работают другие "
+"пользователи"
#: ../src/login/org.freedesktop.login1.policy.in.h:40
-msgid "Authentication is required for suspending the system while other users are logged in."
-msgstr "Чтобы перевести систему в ждущий режим, несмотря на то, что в ней работают другие пользователи, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for suspending the system while other users are "
+"logged in."
+msgstr ""
+"Чтобы перевести систему в ждущий режим, несмотря на то, что в ней работают "
+"другие пользователи, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:41
msgid "Suspend the system while an application asked to inhibit it"
-msgstr "Перевести систему в ждущий режим, несмотря на то, что приложение запросило блокировку"
+msgstr ""
+"Перевести систему в ждущий режим, несмотря на то, что приложение запросило "
+"блокировку"
#: ../src/login/org.freedesktop.login1.policy.in.h:42
-msgid "Authentication is required for suspending the system while an application asked to inhibit it."
-msgstr "Чтобы перевести систему в ждущий режим, несмотря на то, что приложение запросило блокировку, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for suspending the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Чтобы перевести систему в ждущий режим, несмотря на то, что приложение "
+"запросило блокировку, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:43
msgid "Hibernate the system"
#: ../src/login/org.freedesktop.login1.policy.in.h:44
msgid "Authentication is required for hibernating the system."
-msgstr "Чтобы перевести систему в спящий режим, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы перевести систему в спящий режим, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:45
msgid "Hibernate the system while other users are logged in"
-msgstr "Перевести систему в спящий режим, несмотря на то, что в ней работают другие пользователи"
+msgstr ""
+"Перевести систему в спящий режим, несмотря на то, что в ней работают другие "
+"пользователи"
#: ../src/login/org.freedesktop.login1.policy.in.h:46
-msgid "Authentication is required for hibernating the system while other users are logged in."
-msgstr "Чтобы перевести систему в спящий режим, несмотря на то, что в ней работают другие пользователи, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for hibernating the system while other users are "
+"logged in."
+msgstr ""
+"Чтобы перевести систему в спящий режим, несмотря на то, что в ней работают "
+"другие пользователи, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:47
msgid "Hibernate the system while an application asked to inhibit it"
-msgstr "Перевести систему в спящий режим, несмотря на то, что приложение запросило блокировку"
+msgstr ""
+"Перевести систему в спящий режим, несмотря на то, что приложение запросило "
+"блокировку"
#: ../src/login/org.freedesktop.login1.policy.in.h:48
-msgid "Authentication is required for hibernating the system while an application asked to inhibit it."
-msgstr "Чтобы перевести систему в спящий режим, несмотря на то, что приложение запросило блокировку, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for hibernating the system while an application "
+"asked to inhibit it."
+msgstr ""
+"Чтобы перевести систему в спящий режим, несмотря на то, что приложение "
+"запросило блокировку, необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:49
msgid "Manage active sessions, users and seats"
msgstr "Управление текущими сеансами, пользователями и рабочими местами"
#: ../src/login/org.freedesktop.login1.policy.in.h:50
-msgid "Authentication is required for managing active sessions, users and seats."
-msgstr "Для управления текущими сеансами, пользователями и рабочими местами, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr ""
+"Для управления текущими сеансами, пользователями и рабочими местами, "
+"необходимо пройти аутентификацию."
#: ../src/login/org.freedesktop.login1.policy.in.h:51
msgid "Lock or unlock active sessions"
#: ../src/login/org.freedesktop.login1.policy.in.h:52
msgid "Authentication is required to lock or unlock active sessions."
-msgstr "Чтобы заблокировать или разблокировать текущие сеансы, необходимо пройти аутентификацию."
+msgstr ""
+"Чтобы заблокировать или разблокировать текущие сеансы, необходимо пройти "
+"аутентификацию."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+#, fuzzy
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "Чтобы настроить имя компьютера, необходимо пройти аутентификацию."
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Управление виртуальными машинами и контейнерами"
#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
-msgid "Authentication is required to manage local virtual machines and containers."
-msgstr "Для управления виртуальными машинами и контейнерами, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr ""
+"Для управления виртуальными машинами и контейнерами, необходимо пройти "
+"аутентификацию."
#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
msgid "Manage local virtual machine and container images"
msgstr "Управление образами виртуальных машин и контейнеров"
#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
-msgid "Authentication is required to manage local virtual machine and container images."
-msgstr "Для управления образами виртуальных машин и контейнеров, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr ""
+"Для управления образами виртуальных машин и контейнеров, необходимо пройти "
+"аутентификацию."
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
msgid "Set system time"
msgstr "Установить аппаратные часы по местному времени или по Гринвичу"
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:6
-msgid "Authentication is required to control whether the RTC stores the local or UTC time."
-msgstr "Чтобы контролировать, установлены аппаратные часы по местному времени или по Гринвичу, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required to control whether the RTC stores the local or "
+"UTC time."
+msgstr ""
+"Чтобы контролировать, установлены аппаратные часы по местному времени или по "
+"Гринвичу, необходимо пройти аутентификацию."
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:7
msgid "Turn network time synchronization on or off"
msgstr "Включить или выключить синхронизацию времени по сети"
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:8
-msgid "Authentication is required to control whether network time synchronization shall be enabled."
-msgstr "Чтобы включить или выключить синхронизацию времени по сети, необходимо пройти аутентификацию."
+msgid ""
+"Authentication is required to control whether network time synchronization "
+"shall be enabled."
+msgstr ""
+"Чтобы включить или выключить синхронизацию времени по сети, необходимо "
+"пройти аутентификацию."
-#: ../src/fsckd/fsckd.c:297
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr "Чтобы прервать все запущенные проверки файловых систем, нажмите Ctrl+C"
+#~ msgid "Press Ctrl+C to cancel all filesystem checks in progress"
+#~ msgstr ""
+#~ "Чтобы прервать все запущенные проверки файловых систем, нажмите Ctrl+C"
# There is no difference between "на 2 дисках" (plural==1) and "на 5 дисках" (plural==2)
-#: ../src/fsckd/fsckd.c:343
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] "Проверяется целостность файловой системы на %d диске (выполнено %3.1f%%)"
-msgstr[1] "Проверяется целостность файловых систем на %d дисках (выполнено %3.1f%%)"
-msgstr[2] "Проверяется целостность файловых систем на %d дисках (выполнено %3.1f%%)"
+#~ msgid "Checking in progress on %d disk (%3.1f%% complete)"
+#~ msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
+#~ msgstr[0] ""
+#~ "Проверяется целостность файловой системы на %d диске (выполнено %3.1f%%)"
+#~ msgstr[1] ""
+#~ "Проверяется целостность файловых систем на %d дисках (выполнено %3.1f%%)"
+#~ msgstr[2] ""
+#~ "Проверяется целостность файловых систем на %d дисках (выполнено %3.1f%%)"
msgid ""
msgstr ""
"Project-Id-Version: systemd master\n"
-"Report-Msgid-Bugs-To: https://bugs.freedesktop.org/enter_bug.cgi?"
-"product=systemd&keywords=I18N+L10N&component=general\n"
-"POT-Creation-Date: 2015-03-13 23:56+0000\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2015-03-14 11:09+0100\n"
"Last-Translator: Sebastian Rasmussen <sebras@gmail.com>\n"
"Language-Team: Swedish\n"
msgid "Authentication is required to lock or unlock active sessions."
msgstr "Autentisering krävs för att låsa eller låsa upp aktiva sessioner."
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+#, fuzzy
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "Autentisering krävs för att ange lokalt värdnamn."
+
#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
msgid "Log into a local container"
msgstr "Logga till en lokal behållare"
"Autentisering krävs för att kontrollera huruvida synkronisering av "
"nätverkstid ska vara aktiverat."
-#: ../src/fsckd/fsckd.c:291
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr "Tryck Ctrl+C för att avbryta alla pågående filsystemskontroller."
+#~ msgid "Press Ctrl+C to cancel all filesystem checks in progress"
+#~ msgstr "Tryck Ctrl+C för att avbryta alla pågående filsystemskontroller."
-#: ../src/fsckd/fsckd.c:336
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] "Kontroll pågår på %d disk (%3.1f%% klart)"
-msgstr[1] "Kontroll pågår på %d diskar (%3.1f%% klart)"
+#~ msgid "Checking in progress on %d disk (%3.1f%% complete)"
+#~ msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
+#~ msgstr[0] "Kontroll pågår på %d disk (%3.1f%% klart)"
+#~ msgstr[1] "Kontroll pågår på %d diskar (%3.1f%% klart)"
msgid ""
msgstr ""
"Project-Id-Version: systemd master\n"
-"Report-Msgid-Bugs-To: https://bugs.freedesktop.org/enter_bug.cgi?product=sys"
-"temd&keywords=I18N+L10N&component=general\n"
-"POT-Creation-Date: 2014-06-06 22:16+0000\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
"PO-Revision-Date: 2014-07-16 19:13+0300\n"
"Last-Translator: Daniel Korostil <ted.korostiled@gmail.com>\n"
"Language-Team: linux.org.ua\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
-"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%"
-"10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
+"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
+"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"X-Generator: Virtaal 0.7.1\n"
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
+msgid "Send passphrase back to system"
+msgstr "Надіслати пароль назад у систему"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
+msgstr "Засвідчення потрібно, щоб надіслати введений пароль назад у систему."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
+msgid "Manage system services or other units"
+msgstr ""
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
+#, fuzzy
+msgid "Authentication is required to manage system services or other units."
+msgstr "Засвідчення потрібно, щоб доступитись до менеджера системи і служб."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
+msgid "Manage system service or unit files"
+msgstr ""
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
+#, fuzzy
+msgid "Authentication is required to manage system service or unit files."
+msgstr "Засвідчення потрібно, щоб доступитись до менеджера системи і служб."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
+#, fuzzy
+msgid "Set or unset system and service manager environment variables"
+msgstr "Привілейований доступ до менеджера системи і служб"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+#, fuzzy
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr "Засвідчення потрібно, щоб доступитись до менеджера системи і служб."
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
+#, fuzzy
+msgid "Reload the systemd state"
+msgstr "Перезавантажити систему"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
+#, fuzzy
+msgid "Authentication is required to reload the systemd state."
+msgstr "Засвідчення потрібно, щоб вказати системний час."
+
#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
msgid "Set host name"
msgstr "Встановити назву вузла"
msgid "Authentication is required to set local machine information."
msgstr "Засвідчення потрібно, щоб вказати локальну інформацію про машини."
+#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+#, fuzzy
+msgid "Authentication is required to import a VM or container image"
+msgstr "Засвідчення потрібно, щоб вказати системний час."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+#, fuzzy
+msgid "Authentication is required to export a VM or container image"
+msgstr "Засвідчення потрібно, щоб вказати системний час."
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr ""
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
+#, fuzzy
+msgid "Authentication is required to download a VM or container image"
+msgstr "Засвідчення потрібно, щоб вказати локальну інформацію про машини."
+
#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
msgid "Set system locale"
msgstr "Вказати системну локаль"
#: ../src/login/org.freedesktop.login1.policy.in.h:2
msgid ""
-"Authentication is required for an application to inhibit system "
-"shutdown."
+"Authentication is required for an application to inhibit system shutdown."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам перешкоджати вимкненню системи."
msgstr "Дозволити програмам затримувати вимкнення системи"
#: ../src/login/org.freedesktop.login1.policy.in.h:4
-msgid ""
-"Authentication is required for an application to delay system shutdown."
+msgid "Authentication is required for an application to delay system shutdown."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам затримувати вимкнення системи."
msgstr "Дозволити програмам перешкоджати засинанню системи"
#: ../src/login/org.freedesktop.login1.policy.in.h:6
-msgid ""
-"Authentication is required for an application to inhibit system sleep."
+msgid "Authentication is required for an application to inhibit system sleep."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам перешкоджати засинанню системи."
msgstr "Дозволити програмами затримувати засинання системи"
#: ../src/login/org.freedesktop.login1.policy.in.h:8
-msgid ""
-"Authentication is required for an application to delay system sleep."
+msgid "Authentication is required for an application to delay system sleep."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам затримувати засинання системи."
#: ../src/login/org.freedesktop.login1.policy.in.h:10
msgid ""
-"Authentication is required for an application to inhibit automatic "
-"system suspend."
+"Authentication is required for an application to inhibit automatic system "
+"suspend."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам перешкоджати автоматичному "
"призупиненню системи."
#: ../src/login/org.freedesktop.login1.policy.in.h:12
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the power key."
+"Authentication is required for an application to inhibit system handling of "
+"the power key."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам перешкоджати обробленню "
"системою клавіші живлення."
#: ../src/login/org.freedesktop.login1.policy.in.h:14
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the suspend key."
+"Authentication is required for an application to inhibit system handling of "
+"the suspend key."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам перешкоджати обробленню "
"системою клавіші призупинення."
#: ../src/login/org.freedesktop.login1.policy.in.h:15
msgid "Allow applications to inhibit system handling of the hibernate key"
-msgstr "Дозволити програмам перешкоджати обробленню системою клавіші присипання"
+msgstr ""
+"Дозволити програмам перешкоджати обробленню системою клавіші присипання"
#: ../src/login/org.freedesktop.login1.policy.in.h:16
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the hibernate key."
+"Authentication is required for an application to inhibit system handling of "
+"the hibernate key."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам перешкоджати обробленню "
"системою клавіші присипання."
#: ../src/login/org.freedesktop.login1.policy.in.h:18
msgid ""
-"Authentication is required for an application to inhibit system "
-"handling of the lid switch."
+"Authentication is required for an application to inhibit system handling of "
+"the lid switch."
msgstr ""
"Засвідчення потрібно, щоб дозволити програмам перешкоджати обробленню "
"системою клавіші перемикання кришки."
msgstr "Дозволити незареєстрованим користувачам запускати програми"
#: ../src/login/org.freedesktop.login1.policy.in.h:20
-msgid ""
-"Authentication is required to run programs as a non-logged-in user."
+msgid "Authentication is required to run programs as a non-logged-in user."
msgstr ""
"Засвідчення потрібно, щоб дозволити незареєстрованим користувачам запускати "
"програми."
"Authentication is required for rebooting the system while other users are "
"logged in."
msgstr ""
-"Засвідчення потрібно, щоб перезапустити систему, коли інші користувачі в "
-"ній."
+"Засвідчення потрібно, щоб перезапустити систему, коли інші користувачі в ній."
#: ../src/login/org.freedesktop.login1.policy.in.h:35
msgid "Reboot the system while an application asked to inhibit it"
"Засвідчення потрібно, щоб приспати систему, коли програми намагаються "
"першкодити цьому."
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+#, fuzzy
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr "Засвідчення потрібно, щоб під'єднувати пристрої до місць."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+#, fuzzy
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr "Засвідчення потрібно, щоб вказати локальну інформацію про машини."
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr ""
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+#, fuzzy
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "Засвідчення потрібне, щоб встановити назву локального вузла."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
+msgid "Log into a local container"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
+#, fuzzy
+msgid "Authentication is required to log into a local container."
+msgstr "Засвідчення потрібне, щоб встановити назву локального вузла."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr "Засвідчення потрібно, щоб вказати локальну інформацію про машини."
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr ""
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+#, fuzzy
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr "Засвідчення потрібно, щоб вказати локальну інформацію про машини."
+
#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
msgid "Set system time"
msgstr "Вказати системний час"
msgstr ""
"Засвідчення потрібно, щоб контролювати, чи синхронізування часу через мережу "
"запущено."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
-msgid "Send passphrase back to system"
-msgstr "Надіслати пароль назад у систему"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
-msgid ""
-"Authentication is required to send the entered passphrase back to the system."
-msgstr "Засвідчення потрібно, щоб надіслати введений пароль назад у систему."
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
-msgid "Privileged system and service manager access"
-msgstr "Привілейований доступ до менеджера системи і служб"
-
-#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
-msgid "Authentication is required to access the system and service manager."
-msgstr "Засвідчення потрібно, щоб доступитись до менеджера системи і служб."
-
-#: ../src/fsckd/fsckd.c:186
-msgid "Press Ctrl+C to cancel all filesystem checks in progress"
-msgstr ""
-
-#: ../src/fsckd/fsckd.c:227
-#, c-format
-msgid "Checking in progress on %d disk (%3.1f%% complete)"
-msgid_plural "Checking in progress on %d disks (%3.1f%% complete)"
-msgstr[0] ""
-msgstr[1] ""
-msgstr[2] ""
--- /dev/null
+# Traditional Chinese translation for systemd.
+# Copyright (C) 2015 systemd's COPYRIGHT HOLDER
+# This file is distributed under the same license as the systemd package.
+# Jeff Huang <s8321414@gmail.com>, 2015.
+msgid ""
+msgstr ""
+"Project-Id-Version: \n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-06-18 00:53+0200\n"
+"PO-Revision-Date: 2015-06-11 12:44+0800\n"
+"Last-Translator: Jeff Huang <s8321414@gmail.com>\n"
+"Language-Team: Chinese Traditional <kde-i18n-doc@kde.org>\n"
+"Language: zh_TW\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=n != 1;\n"
+"X-Generator: Lokalize 1.5\n"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:1
+msgid "Send passphrase back to system"
+msgstr "傳回密碼片語到系統"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:2
+msgid ""
+"Authentication is required to send the entered passphrase back to the system."
+msgstr "傳回已輸入的密碼片語到系統需要驗證。"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:3
+msgid "Manage system services or other units"
+msgstr "管理系統服務或其他單位"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:4
+msgid "Authentication is required to manage system services or other units."
+msgstr "管理系統服務或其他單位需要驗證。"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:5
+msgid "Manage system service or unit files"
+msgstr "管理系統服務或單位檔案"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:6
+msgid "Authentication is required to manage system service or unit files."
+msgstr "管理系統服務或單位檔案需要驗證。"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:7
+msgid "Set or unset system and service manager environment variables"
+msgstr "設定或取消設定系統及服務管理員環境變量"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:8
+msgid ""
+"Authentication is required to set or unset system and service manager "
+"environment variables."
+msgstr "設定或取消設定系統及服務管理員環境變量時需要驗證"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:9
+msgid "Reload the systemd state"
+msgstr "重新載入 systemd 狀態"
+
+#: ../src/core/org.freedesktop.systemd1.policy.in.in.h:10
+msgid "Authentication is required to reload the systemd state."
+msgstr "重新載入 systemd 狀態需要驗證。"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:1
+msgid "Set host name"
+msgstr "設定主機名稱"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:2
+msgid "Authentication is required to set the local host name."
+msgstr "設定主機名稱需要驗證。"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:3
+msgid "Set static host name"
+msgstr "設定靜態主機名稱"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:4
+msgid ""
+"Authentication is required to set the statically configured local host name, "
+"as well as the pretty host name."
+msgstr "設定靜態設定的本機主機名稱時需要驗證,同時也需要漂亮的主機名稱"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:5
+msgid "Set machine information"
+msgstr "設定機器資訊"
+
+#: ../src/hostname/org.freedesktop.hostname1.policy.in.h:6
+msgid "Authentication is required to set local machine information."
+msgstr "設定機器資訊需要驗證"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:1
+msgid "Import a VM or container image"
+msgstr "匯入虛擬機器或容器映像"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:2
+msgid "Authentication is required to import a VM or container image"
+msgstr "匯入虛擬機器或容器映像需要驗證"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:3
+msgid "Export a VM or container image"
+msgstr "匯出虛擬機器或容器映像"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:4
+msgid "Authentication is required to export a VM or container image"
+msgstr "匯出虛擬機器或容器映像需要驗證"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:5
+msgid "Download a VM or container image"
+msgstr "下載虛擬機器或容器映像"
+
+#: ../src/import/org.freedesktop.import1.policy.in.h:6
+msgid "Authentication is required to download a VM or container image"
+msgstr "下載虛擬機器或容器映像需要驗證"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:1
+msgid "Set system locale"
+msgstr "設定系統語系"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:2
+msgid "Authentication is required to set the system locale."
+msgstr "設定系統語系需要驗證。"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:3
+msgid "Set system keyboard settings"
+msgstr "設定系統鍵盤設定"
+
+#: ../src/locale/org.freedesktop.locale1.policy.in.h:4
+msgid "Authentication is required to set the system keyboard settings."
+msgstr "設定系統鍵盤設定需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:1
+msgid "Allow applications to inhibit system shutdown"
+msgstr "允許應用程式阻止系統關機"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:2
+msgid ""
+"Authentication is required for an application to inhibit system shutdown."
+msgstr "要讓應用程式阻止系統關機需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:3
+msgid "Allow applications to delay system shutdown"
+msgstr "允許應用程式延遲系統關機"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:4
+msgid "Authentication is required for an application to delay system shutdown."
+msgstr "要讓應用程式延遲系統關機需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:5
+msgid "Allow applications to inhibit system sleep"
+msgstr "允許應用程式阻止系統睡眠"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:6
+msgid "Authentication is required for an application to inhibit system sleep."
+msgstr "要讓應用程式阻止系統睡眠需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:7
+msgid "Allow applications to delay system sleep"
+msgstr "允許應用程式延遲系統睡眠"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:8
+msgid "Authentication is required for an application to delay system sleep."
+msgstr "要讓應用程式延遲系統睡眠需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:9
+msgid "Allow applications to inhibit automatic system suspend"
+msgstr "允許應用程式阻止自動系統暫停"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:10
+msgid ""
+"Authentication is required for an application to inhibit automatic system "
+"suspend."
+msgstr "要讓應用程式阻止自動系統暫停需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:11
+msgid "Allow applications to inhibit system handling of the power key"
+msgstr "允許應用程式阻止系統處理電源鍵"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:12
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the power key."
+msgstr "要讓應用程式阻止系統處理電源鍵需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:13
+msgid "Allow applications to inhibit system handling of the suspend key"
+msgstr "允許應用程式阻止系統處理暫停鍵"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:14
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the suspend key."
+msgstr "要讓應用程式阻止系統處理暫停鍵需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:15
+msgid "Allow applications to inhibit system handling of the hibernate key"
+msgstr "允許應用程式阻止系統處理冬眠鍵"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:16
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the hibernate key."
+msgstr "要讓應用程式阻止系統處理冬眠鍵需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:17
+msgid "Allow applications to inhibit system handling of the lid switch"
+msgstr "允許應用程式阻止系統處理上蓋開關"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:18
+msgid ""
+"Authentication is required for an application to inhibit system handling of "
+"the lid switch."
+msgstr "要讓應用程式阻止系統處理上蓋開關需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:19
+msgid "Allow non-logged-in users to run programs"
+msgstr "允許未登入的使用者執行程式"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:20
+msgid "Authentication is required to run programs as a non-logged-in user."
+msgstr "要讓未登入的使用者執行程式需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:21
+msgid "Allow attaching devices to seats"
+msgstr "允許將設備連接到座位"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:22
+msgid "Authentication is required for attaching a device to a seat."
+msgstr "將設備連接到座位需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:23
+msgid "Flush device to seat attachments"
+msgstr "暴露裝置以安裝附件"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:24
+msgid ""
+"Authentication is required for resetting how devices are attached to seats."
+msgstr "要重置裝置如何連接到座位需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:25
+msgid "Power off the system"
+msgstr "關閉系統電源"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:26
+msgid "Authentication is required for powering off the system."
+msgstr "關閉系統電源需要驗證"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:27
+msgid "Power off the system while other users are logged in"
+msgstr "在有其他使用者登入時關閉系統電源"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:28
+msgid ""
+"Authentication is required for powering off the system while other users are "
+"logged in."
+msgstr "在有其他使用者登入時關閉系統電源需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:29
+msgid "Power off the system while an application asked to inhibit it"
+msgstr "當應用程式阻止系統電源關閉時將其關閉"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:30
+msgid ""
+"Authentication is required for powering off the system while an application "
+"asked to inhibit it."
+msgstr "當應用程式阻止系統電源關閉時將系統電源關閉需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:31
+msgid "Reboot the system"
+msgstr "重新啟動系統"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:32
+msgid "Authentication is required for rebooting the system."
+msgstr "重新啟動系統需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:33
+msgid "Reboot the system while other users are logged in"
+msgstr "在有其他使用者登入時重新啟動系統"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:34
+msgid ""
+"Authentication is required for rebooting the system while other users are "
+"logged in."
+msgstr "在有其他使用者登入時重新啟動系統需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:35
+msgid "Reboot the system while an application asked to inhibit it"
+msgstr "當應用程式阻止重新啟動系統時將系統重新啟動"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:36
+msgid ""
+"Authentication is required for rebooting the system while an application "
+"asked to inhibit it."
+msgstr "當應用程式阻止系統重新啟動時將系統重新啟動需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:37
+msgid "Suspend the system"
+msgstr "暫停系統"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:38
+msgid "Authentication is required for suspending the system."
+msgstr "暫停系統需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:39
+msgid "Suspend the system while other users are logged in"
+msgstr "在有其他使用者登入時暫停系統"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:40
+msgid ""
+"Authentication is required for suspending the system while other users are "
+"logged in."
+msgstr "在有其他使用者登入時暫停系統需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:41
+msgid "Suspend the system while an application asked to inhibit it"
+msgstr "當應用程式阻止暫停系統時將系統暫停"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:42
+msgid ""
+"Authentication is required for suspending the system while an application "
+"asked to inhibit it."
+msgstr "當應用程式阻止系統暫停時將系統暫停需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:43
+msgid "Hibernate the system"
+msgstr "系統冬眠"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:44
+msgid "Authentication is required for hibernating the system."
+msgstr "系統冬眠需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:45
+msgid "Hibernate the system while other users are logged in"
+msgstr "在有其他使用者登入時冬眠系統"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:46
+msgid ""
+"Authentication is required for hibernating the system while other users are "
+"logged in."
+msgstr "在有其他使用者登入時冬眠系統需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:47
+msgid "Hibernate the system while an application asked to inhibit it"
+msgstr "當應用程式阻止冬眠系統時將系統冬眠"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:48
+msgid ""
+"Authentication is required for hibernating the system while an application "
+"asked to inhibit it."
+msgstr "當應用程式阻止系統冬眠時將系統冬眠需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:49
+msgid "Manage active sessions, users and seats"
+msgstr "管理活躍的工作階段、使用者與座位"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:50
+msgid ""
+"Authentication is required for managing active sessions, users and seats."
+msgstr "管理活躍的工作階段、使用者與座位需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:51
+msgid "Lock or unlock active sessions"
+msgstr "鎖定或解鎖活躍的工作階段"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:52
+msgid "Authentication is required to lock or unlock active sessions."
+msgstr "鎖定或解鎖活躍的工作階段需要驗證。"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:53
+msgid "Allow indication to the firmware to boot to setup interface"
+msgstr "允許對韌體的指示以開始設定介面"
+
+#: ../src/login/org.freedesktop.login1.policy.in.h:54
+msgid ""
+"Authentication is required to indicate to the firmware to boot to setup "
+"interface."
+msgstr "對韌體的指示以開始設定介面需要驗證。"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:1
+msgid "Log into a local container"
+msgstr "登入到本機容器"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:2
+msgid "Authentication is required to log into a local container."
+msgstr "登入到本機容器需要驗證。"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:3
+msgid "Manage local virtual machines and containers"
+msgstr "管理本機虛擬機器及容器"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:4
+msgid ""
+"Authentication is required to manage local virtual machines and containers."
+msgstr "管理本機虛擬機器及容器需要驗證。"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:5
+msgid "Manage local virtual machine and container images"
+msgstr "管理本機虛擬機器及容器映像"
+
+#: ../src/machine/org.freedesktop.machine1.policy.in.h:6
+msgid ""
+"Authentication is required to manage local virtual machine and container "
+"images."
+msgstr "管理本機虛擬機器及容器映像需要驗證。"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:1
+msgid "Set system time"
+msgstr "設定系統時間"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:2
+msgid "Authentication is required to set the system time."
+msgstr "設定系統時間需要驗證。"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:3
+msgid "Set system timezone"
+msgstr "設定系統時區"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:4
+msgid "Authentication is required to set the system timezone."
+msgstr "設定系統時區需要驗證。"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:5
+msgid "Set RTC to local timezone or UTC"
+msgstr "將 RTC 設定為本地時區或 UTC"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:6
+msgid ""
+"Authentication is required to control whether the RTC stores the local or "
+"UTC time."
+msgstr "控制 RTC 儲存本地或 UTC 時間需要驗證。"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:7
+msgid "Turn network time synchronization on or off"
+msgstr "打開或關閉網路時間同步"
+
+#: ../src/timedate/org.freedesktop.timedate1.policy.in.h:8
+msgid ""
+"Authentication is required to control whether network time synchronization "
+"shall be enabled."
+msgstr "控制網路時間同步是否啟用需要驗證。"
+++ /dev/null
-# udevadm(8) completion -*- shell-script -*-
-#
-# This file is part of systemd.
-#
-# Copyright 2010 Ran Benita
-#
-# 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
-# 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 <http://www.gnu.org/licenses/>.
-
-__contains_word () {
- local w word=$1; shift
- for w in "$@"; do
- [[ $w = "$word" ]] && return
- done
-}
-
-__get_all_sysdevs() {
- local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/)
- printf '%s\n' "${devs[@]%/}"
-}
-
-_udevadm() {
- local i verb comps
- local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
- local OPTS='-h --help --version --debug'
-
- local verbs=(info trigger settle control monitor hwdb test-builtin test)
-
- for ((i=0; i < COMP_CWORD; i++)); do
- if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}" &&
- ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
- verb=${COMP_WORDS[i]}
- break
- fi
- done
-
- if [[ -z $verb ]]; then
- COMPREPLY=( $(compgen -W '${OPTS[*]} ${verbs[*]}' -- "$cur") )
- return 0
- fi
-
- case $verb in
- 'info')
- if [[ $cur = -* ]]; then
- comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db'
- else
- comps=$( __get_all_sysdevs )
- fi
- ;;
- 'trigger')
- comps='--help --verbose --dry-run --type= --action= --subsystem-match=
- --subsystem-nomatch= --attr-match= --attr-nomatch= --property-match=
- --tag-match= --sysname-match= --parent-match='
- ;;
- 'settle')
- comps='--help --timeout= --seq-start= --seq-end= --exit-if-exists= --quiet'
- ;;
- 'control')
- comps='--help --exit --log-priority= --stop-exec-queue --start-exec-queue
- --reload --property= --children-max= --timeout='
- ;;
- 'monitor')
- comps='--help --kernel --udev --property --subsystem-match= --tag-match='
- ;;
- 'hwdb')
- comps='--help --update --test='
- ;;
- 'test')
- if [[ $cur = -* ]]; then
- comps='--help --action='
- else
- comps=$( __get_all_sysdevs )
- fi
- ;;
- 'test-builtin')
- comps='blkid btrfs hwdb input_id keyboard kmod net_id net_setup_link path_id usb_id uaccess'
- ;;
- *)
- comps=${VERBS[*]}
- ;;
- esac
-
- COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
- return 0
-}
-
-complete -F _udevadm udevadm
--- /dev/null
+#compdef systemd-inhibit
+
+_systemd_inhibit_command(){
+ if (( CURRENT == 1 )); then
+ compset -q
+ _normal
+ else
+ local n=${words[(b:2:i)[^-]*]}
+ if (( n <= CURRENT )); then
+ compset -n $n
+ _alternative \
+ 'files:file:_files' \
+ 'commands:command:_normal' && return 0
+ fi
+ _default
+ fi
+}
+
+_inhibit_what() {
+ local _inhibit
+ _inhibit=(shutdown sleep idle handle-power-key handle-suspend-key handle-hibernate-key handle-lid-switch)
+ _values -s : "${_inhibit[@]}"
+}
+
+_arguments \
+ {-h,--help}'[Show this help]' \
+ '--version[Show package version]' \
+ '--what=[Operations to inhibit]:options:_inhibit_what' \
+ '--who=[A descriptive string who is inhibiting]:who is inhibiting:' \
+ '--why=[A descriptive string why is being inhibited]:reason for the lock:' \
+ '--mode=[One of block or delay]:lock mode:( block delay )' \
+ '--list[List active inhibitors]' \
+ '*:commands:_systemd_inhibit_command'
#compdef loginctl
-_loginctl_all_sessions(){_sys_all_sessions=($(loginctl --no-legend list-sessions | { while read a b; do echo " $a"; done; }) )}
-_loginctl_all_users() {_sys_all_users=( $(loginctl --no-legend list-users | { while read a b; do echo " $a"; done; }) )}
-_loginctl_all_seats() {_sys_all_seats=( $(loginctl --no-legend list-seats | { while read a b; do echo " $a"; done; }) )}
+_loginctl_all_sessions() {
+ local session description
+ loginctl --no-legend list-sessions | while read -r session description; do
+ _sys_all_sessions+=( "$session" )
+ _sys_all_sessions_descr+=( "${session}:$description" )
+ done
+}
+
+_loginctl_all_users() {
+ local uid description
+ loginctl --no-legend list-users | while read -r uid description; do
+ _sys_all_users+=( "$uid" )
+ _sys_all_users_descr+=( "${uid}:$description" )
+ done
+}
+_loginctl_all_seats() {
+ local seat description
+ loginctl --no-legend list-seats | while read -r seat description; do
+ _sys_all_seats+=( "$seat" )
+ _sys_all_seats_descr+=( "${seat}:$description" )
+ done
+}
+
+local fun
# Completion functions for SESSIONS
for fun in session-status show-session activate lock-session unlock-session terminate-session kill-session ; do
(( $+functions[_loginctl_$fun] )) || _loginctl_$fun()
{
+ local -a _sys_all_sessions{,_descr}
+
_loginctl_all_sessions
- compadd "$@" -a - _sys_all_sessions
+ for _ignore in $words[2,-1]; do
+ _sys_all_sessions[(i)$_ignore]=()
+ _sys_all_sessions_descr[(i)$_ignore:*]=()
+ done
+
+ if zstyle -T ":completion:${curcontext}:systemd-sessions" verbose; then
+ _describe -t systemd-sessions session _sys_all_sessions_descr _sys_all_sessions "$@"
+ else
+ local expl
+ _wanted systemd-sessions expl session compadd "$@" -a _sys_all_sessions
+ fi
}
done
for fun in user-status show-user enable-linger disable-linger terminate-user kill-user ; do
(( $+functions[_loginctl_$fun] )) || _loginctl_$fun()
{
- _loginctl_all_users
- compadd "$@" -a - _sys_all_users
+ local -a _sys_all_users{,_descr}
+ zstyle -a ":completion:${curcontext}:users" users _sys_all_users
+
+ if ! (( $#_sys_all_users )); then
+ _loginctl_all_users
+ fi
+
+ for _ignore in $words[2,-1]; do
+ _sys_all_users[(i)$_ignore]=()
+ _sys_all_users_descr[(i)$_ignore:*]=()
+ done
+
+ # using the common tag `users' here, not rolling our own `systemd-users' tag
+ if zstyle -T ":completion:${curcontext}:users" verbose; then
+ _describe -t users user ${_sys_all_users_descr:+_sys_all_users_descr} _sys_all_users "$@"
+ else
+ local expl
+ _wanted users expl user compadd "$@" -a _sys_all_users
+ fi
}
done
# Completion functions for SEATS
(( $+functions[_loginctl_seats] )) || _loginctl_seats()
{
+ local -a _sys_all_seats{,_descr}
+
_loginctl_all_seats
- compadd "$@" -a - _sys_all_seats
+ for _ignore in $words[2,-1]; do
+ _sys_all_seats[(i)$_ignore]=()
+ _sys_all_seats_descr[(i)$_ignore:*]=()
+ done
+
+ if zstyle -T ":completion:${curcontext}:systemd-seats" verbose; then
+ _describe -t systemd-seats seat _sys_all_seats_descr _sys_all_seats "$@"
+ else
+ local expl
+ _wanted systemd-seats expl seat compadd "$@" -a _sys_all_seats
+ fi
}
for fun in seat-status show-seat terminate-seat ; do
(( $+functions[_loginctl_$fun] )) || _loginctl_$fun()
# Completion functions for ATTACH
(( $+functions[_loginctl_attach] )) || _loginctl_attach()
{
- _loginctl_all_seats
-
_arguments -w -C -S -s \
':seat:_loginctl_seats' \
'*:device:_files'
if (( CURRENT == 1 )); then
_describe -t commands 'loginctl command' _loginctl_cmds || compadd "$@"
else
- local curcontext="$curcontext"
+ local curcontext="$curcontext" _ignore
cmd="${${_loginctl_cmds[(r)$words[1]:*]%%:*}}"
'--kill-who=[Who to send signal to]:killwho:(main control all)' \
{-s+,--signal=}'[Which signal to send]:signal:_signals' \
{-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
- {-M+,--machine=}'[Operate on local container]:machine' \
- {-P,--privileged}'[Acquire privileges before execution]' \
+ {-M+,--machine=}'[Operate on local container]:machine:_sd_machines' \
{-l,--full}'[Do not ellipsize output]' \
'--no-pager[Do not pipe output into a pager]' \
'--no-legend[Do not show the headers and footers]' \
'--no-ask-password[Do not ask for system passwords]' \
+ {-n+,--lines=}'[Number of journal entries to show]' \
+ {-o+,--output=}'[Change journal output mode]:output modes:_sd_outputmodes' \
'*::loginctl command:_loginctl_command'
+++ /dev/null
-#compdef udevadm
-
-_udevadm_info(){
- _arguments \
- '--query=[Query the database for specified type of device data. It needs the --path or --name to identify the specified device.]:type:(name symlink path property all)' \
- '--path=[The devpath of the device to query.]:sys files:_files -P /sys/ -W /sys' \
- '--name=[The name of the device node or a symlink to query]:device files:_files -P /dev/ -W /dev' \
- '--root[Print absolute paths in name or symlink query.]' \
- '--attribute-walk[Print all sysfs properties of the specified device that can be used in udev rules to match the specified device]' \
- '--export[Print output as key/value pairs.]' \
- '--export-prefix=[Add a prefix to the key name of exported values.]:prefix' \
- '--device-id-of-file=[Print major/minor numbers of the underlying device, where the file lives on.]:files:_udevadm_mounts' \
- '--export-db[Export the content of the udev database.]' \
- '--cleanup-db[Cleanup the udev database.]'
-}
-
-_udevadm_trigger(){
- _arguments \
- '--verbose[Print the list of devices which will be triggered.]' \
- '--dry-run[Do not actually trigger the event.]' \
- '--type=[Trigger a specific type of devices.]:types:(devices subsystems failed)' \
- '--action=[Type of event to be triggered.]:actions:(add change remove)' \
- '--subsystem-match=[Trigger events for devices which belong to a matching subsystem.]' \
- '--subsystem-nomatch=[Do not trigger events for devices which belong to a matching subsystem.]' \
- '--attr-match=attribute=[Trigger events for devices with a matching sysfs attribute.]' \
- '--attr-nomatch=attribute=[Do not trigger events for devices with a matching sysfs attribute.]' \
- '--property-match=[Trigger events for devices with a matching property value.]' \
- '--tag-match=property[Trigger events for devices with a matching tag.]' \
- '--sysname-match=[Trigger events for devices with a matching sys device name.]' \
- '--parent-match=[Trigger events for all children of a given device.]'
-}
-
-_udevadm_settle(){
- _arguments \
- '--timeout=[Maximum number of seconds to wait for the event queue to become empty.]' \
- '--seq-start=[Wait only for events after the given sequence number.]' \
- '--seq-end=[Wait only for events before the given sequence number.]' \
- '--exit-if-exists=[Stop waiting if file exists.]:files:_files' \
- '--quiet[Do not print any output, like the remaining queue entries when reaching the timeout.]' \
- '--help[Print help text.]'
-}
-
-_udevadm_control(){
- _arguments \
- '--exit[Signal and wait for systemd-udevd to exit.]' \
- '--log-priority=[Set the internal log level of systemd-udevd.]:priorities:(err info debug)' \
- '--stop-exec-queue[Signal systemd-udevd to stop executing new events. Incoming events will be queued.]' \
- '--start-exec-queue[Signal systemd-udevd to enable the execution of events.]' \
- '--reload[Signal systemd-udevd to reload the rules files and other databases like the kernel module index.]' \
- '--property=[Set a global property for all events.]' \
- '--children-max=[Set the maximum number of events.]' \
- '--timeout=[The maximum number of seconds to wait for a reply from systemd-udevd.]' \
- '--help[Print help text.]'
-}
-
-_udevadm_monitor(){
- _arguments \
- '--kernel[Print the kernel uevents.]' \
- '--udev[Print the udev event after the rule processing.]' \
- '--property[Also print the properties of the event.]' \
- '--subsystem-match=[Filter events by subsystem/\[devtype\].]' \
- '--tag-match=[Filter events by property.]' \
- '--help[Print help text.]'
-}
-
-_udevadm_test(){
- _arguments \
- '--action=[The action string.]:actions:(add change remove)' \
- '--subsystem=[The subsystem string.]' \
- '--help[Print help text.]' \
- '*::devpath:_files -P /sys/ -W /sys'
-}
-
-_udevadm_test-builtin(){
- if (( CURRENT == 2 )); then
- _arguments \
- '--help[Print help text]' \
- '*::builtins:(blkid btrfs hwdb input_id net_id net_setup_link kmod path_id usb_id uaccess)'
- elif (( CURRENT == 3 )); then
- _arguments \
- '--help[Print help text]' \
- '*::syspath:_files -P /sys -W /sys'
- else
- _arguments \
- '--help[Print help text]'
- fi
-}
-
-_udevadm_mounts(){
- local dev_tmp dpath_tmp mp_tmp mline
-
- tmp=( "${(@f)$(< /etc/mtab)}" )
- dev_tmp=( "${(@)${(@)tmp%% *}:#none}" )
- mp_tmp=( "${(@)${(@)tmp#* }%% *}" )
-
- local MATCH
- mp_tmp=("${(@q)mp_tmp//(#m)\\[0-7](#c3)/${(#)$(( 8#${MATCH[2,-1]} ))}}")
- dpath_tmp=( "${(@Mq)dev_tmp:#/*}" )
- dev_tmp=( "${(@q)dev_tmp:#/*}" )
-
- _alternative \
- 'device-paths: device path:compadd -a dpath_tmp' \
- 'directories:mount point:compadd -a mp_tmp'
-}
-
-
-_udevadm_command(){
- local -a _udevadm_cmds
- _udevadm_cmds=(
- 'info:query sysfs or the udev database'
- 'trigger:request events from the kernel'
- 'settle:wait for the event queue to finish'
- 'control:control the udev daemon'
- 'monitor:listen to kernel and udev events'
- 'test:test an event run'
- 'test-builtin:test a built-in command'
- )
-
- if ((CURRENT == 1)); then
- _describe -t commands 'udevadm commands' _udevadm_cmds
- else
- local curcontext="$curcontext"
- cmd="${${_udevadm_cmds[(r)$words[1]:*]%%:*}}"
- if (($#cmd)); then
- if (( $+functions[_udevadm_$cmd] )); then
- _udevadm_$cmd
- else
- _message "no options for $cmd"
- fi
- else
- _message "no more options"
- fi
- fi
-}
-
-
-_arguments \
- '--debug[Print debug messages to stderr]' \
- '--version[Print version number]' \
- '--help[Print help text]' \
- '*::udevadm commands:_udevadm_command'
#include "macro.h"
#include "audit.h"
#include "util.h"
+#include "process-util.h"
#include "fileio.h"
int audit_session_from_pid(pid_t pid, uint32_t *id) {
assert(id);
+ /* We don't convert ENOENT to ESRCH here, since we can't
+ * really distuingish between "audit is not available in the
+ * kernel" and "the process does not exist", both which will
+ * result in ENOENT. */
+
p = procfs_file_alloca(pid, "sessionid");
r = read_one_line_file(p, &s);
if (r < 0)
return r;
- if (u == (uint32_t) -1 || u <= 0)
- return -ENXIO;
+ if (u == AUDIT_SESSION_INVALID || u <= 0)
+ return -ENODATA;
*id = u;
return 0;
return r;
r = parse_uid(s, &u);
+ if (r == -ENXIO) /* the UID was -1 */
+ return -ENODATA;
if (r < 0)
return r;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
bool use_audit(void) {
static int cached_use = -1;
return cached_use;
}
+#endif // 0
#include <stdbool.h>
#include <sys/types.h>
+#define AUDIT_SESSION_INVALID ((uint32_t) -1)
int audit_session_from_pid(pid_t pid, uint32_t *id);
int audit_loginuid_from_pid(pid_t pid, uid_t *uid);
-bool use_audit(void);
+// UNNEEDED bool use_audit(void);
#define _SELINUX_FEATURE_ "-SELINUX"
#endif
-#ifdef HAVE_APPARMOR
-#define _APPARMOR_FEATURE_ "+APPARMOR"
-#else
-#define _APPARMOR_FEATURE_ "-APPARMOR"
-#endif
-
#ifdef HAVE_SMACK
#define _SMACK_FEATURE_ "+SMACK"
#else
#define _ACL_FEATURE_ "-ACL"
#endif
-#ifdef HAVE_SECCOMP
-#define _SECCOMP_FEATURE_ "+SECCOMP"
-#else
-#define _SECCOMP_FEATURE_ "-SECCOMP"
-#endif
-
-#ifdef HAVE_BLKID
-#define _BLKID_FEATURE_ "+BLKID"
-#else
-#define _BLKID_FEATURE_ "-BLKID"
-#endif
-
#define SYSTEMD_FEATURES \
_PAM_FEATURE_ " " \
_SELINUX_FEATURE_ " " \
- _APPARMOR_FEATURE_ " " \
_SMACK_FEATURE_ " " \
- _ACL_FEATURE_ " " \
- _SECCOMP_FEATURE_ " " \
- _BLKID_FEATURE_
+ _ACL_FEATURE_ " "
return r;
}
-char *bus_label_unescape(const char *f) {
+char *bus_label_unescape_n(const char *f, size_t l) {
char *r, *t;
+ size_t i;
assert_return(f, NULL);
/* Special case for the empty string */
- if (streq(f, "_"))
+ if (l == 1 && *f == '_')
return strdup("");
- r = new(char, strlen(f) + 1);
+ r = new(char, l + 1);
if (!r)
return NULL;
- for (t = r; *f; f++) {
-
- if (*f == '_') {
+ for (i = 0, t = r; i < l; ++i) {
+ if (f[i] == '_') {
int a, b;
- if ((a = unhexchar(f[1])) < 0 ||
- (b = unhexchar(f[2])) < 0) {
+ if (l - i < 3 ||
+ (a = unhexchar(f[i + 1])) < 0 ||
+ (b = unhexchar(f[i + 2])) < 0) {
/* Invalid escape code, let's take it literal then */
*(t++) = '_';
} else {
*(t++) = (char) ((a << 4) | b);
- f += 2;
+ i += 2;
}
} else
- *(t++) = *f;
+ *(t++) = f[i];
}
*t = 0;
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <stdlib.h>
+#include <string.h>
+
char *bus_label_escape(const char *s);
-char *bus_label_unescape(const char *f);
+char *bus_label_unescape_n(const char *f, size_t l);
+
+static inline char *bus_label_unescape(const char *f) {
+ return bus_label_unescape_n(f, f ? strlen(f) : 0);
+}
#include "fileio.h"
#include "capability.h"
+/// UNNEEDED by elogind
+#if 0
int have_effective_cap(int value) {
_cleanup_cap_free_ cap_t cap;
cap_flag_value_t fv;
else
return fv == CAP_SET;
}
+#endif // 0
unsigned long cap_last_cap(void) {
static thread_local unsigned long saved;
return p;
}
+/// UNNEEDED by elogind
+#if 0
int capability_bounding_set_drop(uint64_t drop, bool right_now) {
_cleanup_cap_free_ cap_t after_cap = NULL;
cap_flag_value_t fv;
if (asprintf(&p, "%u %u", lo, hi) < 0)
return -ENOMEM;
- r = write_string_file(fn, p);
+ r = write_string_file(fn, p, WRITE_STRING_FILE_CREATE);
free(p);
return r;
return 0;
}
+#endif // 0
#include "util.h"
unsigned long cap_last_cap(void);
-int have_effective_cap(int value);
-int capability_bounding_set_drop(uint64_t drop, bool right_now);
-int capability_bounding_set_drop_usermode(uint64_t drop);
+// UNNEEDED int have_effective_cap(int value);
+// UNNEEDED int capability_bounding_set_drop(uint64_t drop, bool right_now);
+// UNNEEDED int capability_bounding_set_drop_usermode(uint64_t drop);
-int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilites);
+// UNNEEDED int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities);
-int drop_capability(cap_value_t cv);
+// UNNEEDED int drop_capability(cap_value_t cv);
DEFINE_TRIVIAL_CLEANUP_FUNC(cap_t, cap_free);
#define _cleanup_cap_free_ _cleanup_(cap_freep)
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <errno.h>
+#include <unistd.h>
+#include <signal.h>
+#include <string.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <ftw.h>
+
+#include "set.h"
+#include "macro.h"
+#include "util.h"
+#include "formats-util.h"
+#include "process-util.h"
+#include "path-util.h"
+// #include "unit-name.h"
+#include "fileio.h"
+// #include "special.h"
+#include "mkdir.h"
+#include "login-util.h"
+#include "cgroup-util.h"
+
+int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
+ _cleanup_free_ char *fs = NULL;
+ FILE *f;
+ int r;
+
+ assert(_f);
+
+ r = cg_get_path(controller, path, "cgroup.procs", &fs);
+ if (r < 0)
+ return r;
+
+ f = fopen(fs, "re");
+ if (!f)
+ return -errno;
+
+ *_f = f;
+ return 0;
+}
+
+int cg_read_pid(FILE *f, pid_t *_pid) {
+ unsigned long ul;
+
+ /* Note that the cgroup.procs might contain duplicates! See
+ * cgroups.txt for details. */
+
+ assert(f);
+ assert(_pid);
+
+ errno = 0;
+ if (fscanf(f, "%lu", &ul) != 1) {
+
+ if (feof(f))
+ return 0;
+
+ return errno ? -errno : -EIO;
+ }
+
+ if (ul <= 0)
+ return -EIO;
+
+ *_pid = (pid_t) ul;
+ return 1;
+}
+
+int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
+ _cleanup_free_ char *fs = NULL;
+ int r;
+ DIR *d;
+
+ assert(_d);
+
+ /* This is not recursive! */
+
+ r = cg_get_path(controller, path, NULL, &fs);
+ if (r < 0)
+ return r;
+
+ d = opendir(fs);
+ if (!d)
+ return -errno;
+
+ *_d = d;
+ return 0;
+}
+
+int cg_read_subgroup(DIR *d, char **fn) {
+ struct dirent *de;
+
+ assert(d);
+ assert(fn);
+
+ FOREACH_DIRENT_ALL(de, d, return -errno) {
+ char *b;
+
+ if (de->d_type != DT_DIR)
+ continue;
+
+ if (streq(de->d_name, ".") ||
+ streq(de->d_name, ".."))
+ continue;
+
+ b = strdup(de->d_name);
+ if (!b)
+ return -ENOMEM;
+
+ *fn = b;
+ return 1;
+ }
+
+ return 0;
+}
+
+int cg_rmdir(const char *controller, const char *path) {
+ _cleanup_free_ char *p = NULL;
+ int r;
+
+ r = cg_get_path(controller, path, NULL, &p);
+ if (r < 0)
+ return r;
+
+ r = rmdir(p);
+ if (r < 0 && errno != ENOENT)
+ return -errno;
+
+ return 0;
+}
+
+int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
+ _cleanup_set_free_ Set *allocated_set = NULL;
+ bool done = false;
+ int r, ret = 0;
+ pid_t my_pid;
+
+ assert(sig >= 0);
+
+ /* This goes through the tasks list and kills them all. This
+ * is repeated until no further processes are added to the
+ * tasks list, to properly handle forking processes */
+
+ if (!s) {
+ s = allocated_set = set_new(NULL);
+ if (!s)
+ return -ENOMEM;
+ }
+
+ my_pid = getpid();
+
+ do {
+ _cleanup_fclose_ FILE *f = NULL;
+ pid_t pid = 0;
+ done = true;
+
+ r = cg_enumerate_processes(controller, path, &f);
+ if (r < 0) {
+ if (ret >= 0 && r != -ENOENT)
+ return r;
+
+ return ret;
+ }
+
+ while ((r = cg_read_pid(f, &pid)) > 0) {
+
+ if (ignore_self && pid == my_pid)
+ continue;
+
+ if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
+ continue;
+
+ /* If we haven't killed this process yet, kill
+ * it */
+ if (kill(pid, sig) < 0) {
+ if (ret >= 0 && errno != ESRCH)
+ ret = -errno;
+ } else {
+ if (sigcont && sig != SIGKILL)
+ (void) kill(pid, SIGCONT);
+
+ if (ret == 0)
+ ret = 1;
+ }
+
+ done = false;
+
+ r = set_put(s, PID_TO_PTR(pid));
+ if (r < 0) {
+ if (ret >= 0)
+ return r;
+
+ return ret;
+ }
+ }
+
+ if (r < 0) {
+ if (ret >= 0)
+ return r;
+
+ return ret;
+ }
+
+ /* To avoid racing against processes which fork
+ * quicker than we can kill them we repeat this until
+ * no new pids need to be killed. */
+
+ } while (!done);
+
+ return ret;
+}
+
+int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
+ _cleanup_set_free_ Set *allocated_set = NULL;
+ _cleanup_closedir_ DIR *d = NULL;
+ int r, ret;
+ char *fn;
+
+ assert(path);
+ assert(sig >= 0);
+
+ if (!s) {
+ s = allocated_set = set_new(NULL);
+ if (!s)
+ return -ENOMEM;
+ }
+
+ ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
+
+ r = cg_enumerate_subgroups(controller, path, &d);
+ if (r < 0) {
+ if (ret >= 0 && r != -ENOENT)
+ return r;
+
+ return ret;
+ }
+
+ while ((r = cg_read_subgroup(d, &fn)) > 0) {
+ _cleanup_free_ char *p = NULL;
+
+ p = strjoin(path, "/", fn, NULL);
+ free(fn);
+ if (!p)
+ return -ENOMEM;
+
+ r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
+ if (r != 0 && ret >= 0)
+ ret = r;
+ }
+
+ if (ret >= 0 && r < 0)
+ ret = r;
+
+ if (rem) {
+ r = cg_rmdir(controller, path);
+ if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
+ return r;
+ }
+
+ return ret;
+}
+
+int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
+ bool done = false;
+ _cleanup_set_free_ Set *s = NULL;
+ int r, ret = 0;
+ pid_t my_pid;
+
+ assert(cfrom);
+ assert(pfrom);
+ assert(cto);
+ assert(pto);
+
+ s = set_new(NULL);
+ if (!s)
+ return -ENOMEM;
+
+ my_pid = getpid();
+
+ log_debug_elogind("Migrating \"%s\"/\"%s\" to \"%s\"/\"%s\" (%s)",
+ cfrom, pfrom, cto, pto,
+ ignore_self ? "ignoring self" : "watching self");
+
+ do {
+ _cleanup_fclose_ FILE *f = NULL;
+ pid_t pid = 0;
+ done = true;
+
+ r = cg_enumerate_processes(cfrom, pfrom, &f);
+ if (r < 0) {
+ if (ret >= 0 && r != -ENOENT)
+ return r;
+
+ return ret;
+ }
+
+ while ((r = cg_read_pid(f, &pid)) > 0) {
+
+ /* This might do weird stuff if we aren't a
+ * single-threaded program. However, we
+ * luckily know we are not */
+ if (ignore_self && pid == my_pid)
+ continue;
+
+ if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
+ continue;
+
+ /* Ignore kernel threads. Since they can only
+ * exist in the root cgroup, we only check for
+ * them there. */
+ if (cfrom &&
+ (isempty(pfrom) || path_equal(pfrom, "/")) &&
+ is_kernel_thread(pid) > 0)
+ continue;
+
+ r = cg_attach(cto, pto, pid);
+ if (r < 0) {
+ if (ret >= 0 && r != -ESRCH)
+ ret = r;
+ } else if (ret == 0)
+ ret = 1;
+
+ done = false;
+
+ r = set_put(s, PID_TO_PTR(pid));
+ if (r < 0) {
+ if (ret >= 0)
+ return r;
+
+ return ret;
+ }
+ }
+
+ if (r < 0) {
+ if (ret >= 0)
+ return r;
+
+ return ret;
+ }
+ } while (!done);
+
+ return ret;
+}
+
+int cg_migrate_recursive(
+ const char *cfrom,
+ const char *pfrom,
+ const char *cto,
+ const char *pto,
+ bool ignore_self,
+ bool rem) {
+
+ _cleanup_closedir_ DIR *d = NULL;
+ int r, ret = 0;
+ char *fn;
+
+ assert(cfrom);
+ assert(pfrom);
+ assert(cto);
+ assert(pto);
+
+ ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
+
+ r = cg_enumerate_subgroups(cfrom, pfrom, &d);
+ if (r < 0) {
+ if (ret >= 0 && r != -ENOENT)
+ return r;
+
+ return ret;
+ }
+
+ while ((r = cg_read_subgroup(d, &fn)) > 0) {
+ _cleanup_free_ char *p = NULL;
+
+ p = strjoin(pfrom, "/", fn, NULL);
+ free(fn);
+ if (!p)
+ return -ENOMEM;
+
+ r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
+ if (r != 0 && ret >= 0)
+ ret = r;
+ }
+
+ if (r < 0 && ret >= 0)
+ ret = r;
+
+ if (rem) {
+ r = cg_rmdir(cfrom, pfrom);
+ if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
+ return r;
+ }
+
+ return ret;
+}
+
+int cg_migrate_recursive_fallback(
+ const char *cfrom,
+ const char *pfrom,
+ const char *cto,
+ const char *pto,
+ bool ignore_self,
+ bool rem) {
+
+ int r;
+
+ assert(cfrom);
+ assert(pfrom);
+ assert(cto);
+ assert(pto);
+
+ r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
+ if (r < 0) {
+ char prefix[strlen(pto) + 1];
+
+ /* This didn't work? Then let's try all prefixes of the destination */
+
+ PATH_FOREACH_PREFIX(prefix, pto) {
+ int q;
+
+ q = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
+ if (q >= 0)
+ return q;
+ }
+ }
+
+ return r;
+}
+
+static const char *controller_to_dirname(const char *controller) {
+ const char *e;
+
+ assert(controller);
+
+ /* Converts a controller name to the directory name below
+ * /sys/fs/cgroup/ we want to mount it to. Effectively, this
+ * just cuts off the name= prefixed used for named
+ * hierarchies, if it is specified. */
+
+ e = startswith(controller, "name=");
+ if (e)
+ return e;
+
+ return controller;
+}
+
+static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) {
+ const char *dn;
+ char *t = NULL;
+
+ assert(fs);
+ assert(controller);
+
+ dn = controller_to_dirname(controller);
+
+ if (isempty(path) && isempty(suffix))
+ t = strappend("/sys/fs/cgroup/", dn);
+ else if (isempty(path))
+ t = strjoin("/sys/fs/cgroup/", dn, "/", suffix, NULL);
+ else if (isempty(suffix))
+ t = strjoin("/sys/fs/cgroup/", dn, "/", path, NULL);
+ else
+ t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix, NULL);
+ if (!t)
+ return -ENOMEM;
+
+ *fs = t;
+ return 0;
+ }
+
+static int join_path_unified(const char *path, const char *suffix, char **fs) {
+ char *t;
+
+ assert(fs);
+
+ if (isempty(path) && isempty(suffix))
+ t = strdup("/sys/fs/cgroup");
+ else if (isempty(path))
+ t = strappend("/sys/fs/cgroup/", suffix);
+ else if (isempty(suffix))
+ t = strappend("/sys/fs/cgroup/", path);
+ else
+ t = strjoin("/sys/fs/cgroup/", path, "/", suffix, NULL);
+ if (!t)
+ return -ENOMEM;
+
+ *fs = t;
+ return 0;
+}
+
+int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
+ int unified, r;
+
+ assert(fs);
+
+ if (!controller) {
+ char *t;
+
+ /* If no controller is specified, we return the path
+ * *below* the controllers, without any prefix. */
+
+ if (!path && !suffix)
+ return -EINVAL;
+
+ if (!suffix)
+ t = strdup(path);
+ else if (!path)
+ t = strdup(suffix);
+ else
+ t = strjoin(path, "/", suffix, NULL);
+ if (!t)
+ return -ENOMEM;
+
+ *fs = path_kill_slashes(t);
+ return 0;
+ }
+
+ if (!cg_controller_is_valid(controller))
+ return -EINVAL;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+
+ if (unified > 0)
+ r = join_path_unified(path, suffix, fs);
+ else
+ r = join_path_legacy(controller, path, suffix, fs);
+
+ if (r < 0)
+ return r;
+
+ path_kill_slashes(*fs);
+ return 0;
+}
+
+static int controller_is_accessible(const char *controller) {
+ int unified;
+
+ assert(controller);
+
+ /* Checks whether a specific controller is accessible,
+ * i.e. its hierarchy mounted. In the unified hierarchy all
+ * controllers are considered accessible, except for the named
+ * hierarchies */
+
+ if (!cg_controller_is_valid(controller))
+ return -EINVAL;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified > 0) {
+ /* We don't support named hierarchies if we are using
+ * the unified hierarchy. */
+
+ if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
+ return 0;
+
+ if (startswith(controller, "name="))
+ return -EOPNOTSUPP;
+
+ } else {
+ const char *cc, *dn;
+
+ dn = controller_to_dirname(controller);
+ cc = strjoina("/sys/fs/cgroup/", dn);
+
+ if (laccess(cc, F_OK) < 0)
+ return -errno;
+ }
+
+ return 0;
+}
+
+int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
+ int r;
+
+ assert(controller);
+ assert(fs);
+
+ /* Check if the specified controller is actually accessible */
+ r = controller_is_accessible(controller);
+ if (r < 0)
+ return r;
+
+ return cg_get_path(controller, path, suffix, fs);
+}
+
+static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
+ assert(path);
+ assert(sb);
+ assert(ftwbuf);
+
+ if (typeflag != FTW_DP)
+ return 0;
+
+ if (ftwbuf->level < 1)
+ return 0;
+
+ (void) rmdir(path);
+ return 0;
+}
+
+int cg_trim(const char *controller, const char *path, bool delete_root) {
+ _cleanup_free_ char *fs = NULL;
+ int r = 0;
+
+ assert(path);
+
+ r = cg_get_path(controller, path, NULL, &fs);
+ if (r < 0)
+ return r;
+
+ errno = 0;
+ if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) {
+ if (errno == ENOENT)
+ r = 0;
+ else if (errno != 0)
+ r = -errno;
+ else
+ r = -EIO;
+ }
+
+ if (delete_root) {
+ if (rmdir(fs) < 0 && errno != ENOENT)
+ return -errno;
+ }
+
+ return r;
+}
+
+int cg_create(const char *controller, const char *path) {
+ _cleanup_free_ char *fs = NULL;
+ int r;
+
+ r = cg_get_path_and_check(controller, path, NULL, &fs);
+ if (r < 0)
+ return r;
+
+ r = mkdir_parents(fs, 0755);
+ if (r < 0)
+ return r;
+
+ if (mkdir(fs, 0755) < 0) {
+
+ if (errno == EEXIST)
+ return 0;
+
+ return -errno;
+ }
+
+ return 1;
+}
+
+int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
+ int r, q;
+
+ assert(pid >= 0);
+
+ r = cg_create(controller, path);
+ if (r < 0)
+ return r;
+
+ q = cg_attach(controller, path, pid);
+ if (q < 0)
+ return q;
+
+ /* This does not remove the cgroup on failure */
+ return r;
+}
+
+int cg_attach(const char *controller, const char *path, pid_t pid) {
+ _cleanup_free_ char *fs = NULL;
+ char c[DECIMAL_STR_MAX(pid_t) + 2];
+ int r;
+
+ assert(path);
+ assert(pid >= 0);
+
+ r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
+ if (r < 0)
+ return r;
+
+ if (pid == 0)
+ pid = getpid();
+
+ snprintf(c, sizeof(c), PID_FMT"\n", pid);
+
+ return write_string_file(fs, c, 0);
+}
+
+int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
+ int r;
+
+ assert(controller);
+ assert(path);
+ assert(pid >= 0);
+
+ r = cg_attach(controller, path, pid);
+ if (r < 0) {
+ char prefix[strlen(path) + 1];
+
+ /* This didn't work? Then let's try all prefixes of
+ * the destination */
+
+ PATH_FOREACH_PREFIX(prefix, path) {
+ int q;
+
+ q = cg_attach(controller, prefix, pid);
+ if (q >= 0)
+ return q;
+ }
+ }
+
+ return r;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int cg_set_group_access(
+ const char *controller,
+ const char *path,
+ mode_t mode,
+ uid_t uid,
+ gid_t gid) {
+
+ _cleanup_free_ char *fs = NULL;
+ int r;
+
+ if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
+ return 0;
+
+ if (mode != MODE_INVALID)
+ mode &= 0777;
+
+ r = cg_get_path(controller, path, NULL, &fs);
+ if (r < 0)
+ return r;
+
+ return chmod_and_chown(fs, mode, uid, gid);
+}
+
+int cg_set_task_access(
+ const char *controller,
+ const char *path,
+ mode_t mode,
+ uid_t uid,
+ gid_t gid) {
+
+ _cleanup_free_ char *fs = NULL, *procs = NULL;
+ int r, unified;
+
+ assert(path);
+
+ if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
+ return 0;
+
+ if (mode != MODE_INVALID)
+ mode &= 0666;
+
+ r = cg_get_path(controller, path, "cgroup.procs", &fs);
+ if (r < 0)
+ return r;
+
+ r = chmod_and_chown(fs, mode, uid, gid);
+ if (r < 0)
+ return r;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified)
+ return 0;
+
+ /* Compatibility, Always keep values for "tasks" in sync with
+ * "cgroup.procs" */
+ if (cg_get_path(controller, path, "tasks", &procs) >= 0)
+ (void) chmod_and_chown(procs, mode, uid, gid);
+
+ return 0;
+}
+#endif // 0
+
+int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char line[LINE_MAX];
+ const char *fs;
+ size_t cs = 0;
+ int unified;
+
+ assert(path);
+ assert(pid >= 0);
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified == 0) {
+ if (controller) {
+ if (!cg_controller_is_valid(controller))
+ return -EINVAL;
+ } else
+ controller = SYSTEMD_CGROUP_CONTROLLER;
+
+ cs = strlen(controller);
+ }
+
+ fs = procfs_file_alloca(pid, "cgroup");
+ log_debug_elogind("Searching for PID %u in \"%s\" (controller \"%s\")",
+ pid, fs, controller);
+ f = fopen(fs, "re");
+ if (!f)
+ return errno == ENOENT ? -ESRCH : -errno;
+
+ FOREACH_LINE(line, f, return -errno) {
+ char *e, *p;
+
+ truncate_nl(line);
+
+ if (unified) {
+ e = startswith(line, "0:");
+ if (!e)
+ continue;
+
+ e = strchr(e, ':');
+ if (!e)
+ continue;
+ } else {
+ char *l;
+ size_t k;
+ const char *word, *state;
+ bool found = false;
+
+ l = strchr(line, ':');
+ if (!l)
+ continue;
+
+ l++;
+ e = strchr(l, ':');
+ if (!e)
+ continue;
+
+ *e = 0;
+ FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
+ if (k == cs && memcmp(word, controller, cs) == 0) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ continue;
+ }
+
+ log_debug_elogind("Found %s:%s", line, e+1);
+ p = strdup(e + 1);
+ if (!p)
+ return -ENOMEM;
+
+ *path = p;
+ return 0;
+ }
+
+ return -ENODATA;
+}
+
+int cg_install_release_agent(const char *controller, const char *agent) {
+ _cleanup_free_ char *fs = NULL, *contents = NULL;
+ const char *sc;
+ int r, unified;
+
+ assert(agent);
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified) /* doesn't apply to unified hierarchy */
+ return -EOPNOTSUPP;
+
+ r = cg_get_path(controller, NULL, "release_agent", &fs);
+ if (r < 0)
+ return r;
+
+ r = read_one_line_file(fs, &contents);
+ if (r < 0)
+ return r;
+
+ sc = strstrip(contents);
+ if (isempty(sc)) {
+ r = write_string_file(fs, agent, 0);
+ if (r < 0)
+ return r;
+ } else if (!path_equal(sc, agent))
+ return -EEXIST;
+
+ fs = mfree(fs);
+ r = cg_get_path(controller, NULL, "notify_on_release", &fs);
+ if (r < 0)
+ return r;
+
+ contents = mfree(contents);
+ r = read_one_line_file(fs, &contents);
+ if (r < 0)
+ return r;
+
+ sc = strstrip(contents);
+ if (streq(sc, "0")) {
+ r = write_string_file(fs, "1", 0);
+ if (r < 0)
+ return r;
+
+ return 1;
+ }
+
+ if (!streq(sc, "1"))
+ return -EIO;
+
+ return 0;
+}
+
+int cg_uninstall_release_agent(const char *controller) {
+ _cleanup_free_ char *fs = NULL;
+ int r, unified;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified) /* Doesn't apply to unified hierarchy */
+ return -EOPNOTSUPP;
+
+ r = cg_get_path(controller, NULL, "notify_on_release", &fs);
+ if (r < 0)
+ return r;
+
+ r = write_string_file(fs, "0", 0);
+ if (r < 0)
+ return r;
+
+ fs = mfree(fs);
+
+ r = cg_get_path(controller, NULL, "release_agent", &fs);
+ if (r < 0)
+ return r;
+
+ r = write_string_file(fs, "", 0);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+int cg_is_empty(const char *controller, const char *path) {
+ _cleanup_fclose_ FILE *f = NULL;
+ pid_t pid;
+ int r;
+
+ assert(path);
+
+ r = cg_enumerate_processes(controller, path, &f);
+ if (r == -ENOENT)
+ return 1;
+ if (r < 0)
+ return r;
+
+ r = cg_read_pid(f, &pid);
+ if (r < 0)
+ return r;
+
+ return r == 0;
+}
+
+int cg_is_empty_recursive(const char *controller, const char *path) {
+ int unified, r;
+
+ assert(path);
+
+ /* The root cgroup is always populated */
+ if (controller && (isempty(path) || path_equal(path, "/")))
+ return false;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+
+ if (unified > 0) {
+ _cleanup_free_ char *populated = NULL, *t = NULL;
+
+ /* On the unified hierarchy we can check empty state
+ * via the "cgroup.populated" attribute. */
+
+ r = cg_get_path(controller, path, "cgroup.populated", &populated);
+ if (r < 0)
+ return r;
+
+ r = read_one_line_file(populated, &t);
+ if (r == -ENOENT)
+ return 1;
+ if (r < 0)
+ return r;
+
+ return streq(t, "0");
+ } else {
+ _cleanup_closedir_ DIR *d = NULL;
+ char *fn;
+
+ r = cg_is_empty(controller, path);
+ if (r <= 0)
+ return r;
+
+ r = cg_enumerate_subgroups(controller, path, &d);
+ if (r == -ENOENT)
+ return 1;
+ if (r < 0)
+ return r;
+
+ while ((r = cg_read_subgroup(d, &fn)) > 0) {
+ _cleanup_free_ char *p = NULL;
+
+ p = strjoin(path, "/", fn, NULL);
+ free(fn);
+ if (!p)
+ return -ENOMEM;
+
+ r = cg_is_empty_recursive(controller, p);
+ if (r <= 0)
+ return r;
+ }
+ if (r < 0)
+ return r;
+
+ return true;
+ }
+}
+
+int cg_split_spec(const char *spec, char **controller, char **path) {
+ char *t = NULL, *u = NULL;
+ const char *e;
+
+ assert(spec);
+
+ if (*spec == '/') {
+ if (!path_is_safe(spec))
+ return -EINVAL;
+
+ if (path) {
+ t = strdup(spec);
+ if (!t)
+ return -ENOMEM;
+
+ *path = path_kill_slashes(t);
+ }
+
+ if (controller)
+ *controller = NULL;
+
+ return 0;
+ }
+
+ e = strchr(spec, ':');
+ if (!e) {
+ if (!cg_controller_is_valid(spec))
+ return -EINVAL;
+
+ if (controller) {
+ t = strdup(spec);
+ if (!t)
+ return -ENOMEM;
+
+ *controller = t;
+ }
+
+ if (path)
+ *path = NULL;
+
+ return 0;
+ }
+
+ t = strndup(spec, e-spec);
+ if (!t)
+ return -ENOMEM;
+ if (!cg_controller_is_valid(t)) {
+ free(t);
+ return -EINVAL;
+ }
+
+ if (isempty(e+1))
+ u = NULL;
+ else {
+ u = strdup(e+1);
+ if (!u) {
+ free(t);
+ return -ENOMEM;
+ }
+
+ if (!path_is_safe(u) ||
+ !path_is_absolute(u)) {
+ free(t);
+ free(u);
+ return -EINVAL;
+ }
+
+ path_kill_slashes(u);
+ }
+
+ if (controller)
+ *controller = t;
+ else
+ free(t);
+
+ if (path)
+ *path = u;
+ else
+ free(u);
+
+ return 0;
+}
+
+int cg_mangle_path(const char *path, char **result) {
+ _cleanup_free_ char *c = NULL, *p = NULL;
+ char *t;
+ int r;
+
+ assert(path);
+ assert(result);
+
+ /* First, check if it already is a filesystem path */
+ if (path_startswith(path, "/sys/fs/cgroup")) {
+
+ t = strdup(path);
+ if (!t)
+ return -ENOMEM;
+
+ *result = path_kill_slashes(t);
+ return 0;
+ }
+
+ /* Otherwise, treat it as cg spec */
+ r = cg_split_spec(path, &c, &p);
+ if (r < 0)
+ return r;
+
+ return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
+}
+
+int cg_get_root_path(char **path) {
+/// elogind does not support systemd scopes and slices
+#if 0
+ char *p, *e;
+ int r;
+
+ assert(path);
+
+ r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
+ if (r < 0)
+ return r;
+
+ e = endswith(p, "/" SPECIAL_INIT_SCOPE);
+ if (!e)
+ e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
+ if (!e)
+ e = endswith(p, "/system"); /* even more legacy */
+ if (e)
+ *e = 0;
+
+ *path = p;
+ return 0;
+#else
+ assert(path);
+ return cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, path);
+#endif // 0
+}
+
+int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
+ _cleanup_free_ char *rt = NULL;
+ char *p;
+ int r;
+
+ assert(cgroup);
+ assert(shifted);
+
+ if (!root) {
+ /* If the root was specified let's use that, otherwise
+ * let's determine it from PID 1 */
+
+ r = cg_get_root_path(&rt);
+ if (r < 0)
+ return r;
+
+ root = rt;
+ log_debug_elogind("Determined root path: \"%s\"", root);
+ }
+
+ p = path_startswith(cgroup, root);
+ if (p && p[0] && (p > cgroup))
+ *shifted = p - 1;
+ else
+ *shifted = cgroup;
+
+ return 0;
+}
+
+int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
+ _cleanup_free_ char *raw = NULL;
+ const char *c;
+ int r;
+
+ assert(pid >= 0);
+ assert(cgroup);
+
+ r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
+ if (r < 0)
+ return r;
+
+ log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
+ raw, pid, root ? root : "NULL");
+ r = cg_shift_path(raw, root, &c);
+ if (r < 0)
+ return r;
+
+ if (c == raw) {
+ *cgroup = raw;
+ raw = NULL;
+ } else {
+ char *n;
+
+ n = strdup(c);
+ if (!n)
+ return -ENOMEM;
+
+ *cgroup = n;
+ }
+ log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
+
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int cg_path_decode_unit(const char *cgroup, char **unit){
+ char *c, *s;
+ size_t n;
+
+ assert(cgroup);
+ assert(unit);
+
+ n = strcspn(cgroup, "/");
+ if (n < 3)
+ return -ENXIO;
+
+ c = strndupa(cgroup, n);
+ c = cg_unescape(c);
+
+ if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
+ return -ENXIO;
+
+ s = strdup(c);
+ if (!s)
+ return -ENOMEM;
+
+ *unit = s;
+ return 0;
+}
+
+static bool valid_slice_name(const char *p, size_t n) {
+
+ if (!p)
+ return false;
+
+ if (n < strlen("x.slice"))
+ return false;
+
+ if (memcmp(p + n - 6, ".slice", 6) == 0) {
+ char buf[n+1], *c;
+
+ memcpy(buf, p, n);
+ buf[n] = 0;
+
+ c = cg_unescape(buf);
+
+ return unit_name_is_valid(c, UNIT_NAME_PLAIN);
+ }
+
+ return false;
+}
+
+static const char *skip_slices(const char *p) {
+ assert(p);
+
+ /* Skips over all slice assignments */
+
+ for (;;) {
+ size_t n;
+
+ p += strspn(p, "/");
+
+ n = strcspn(p, "/");
+ if (!valid_slice_name(p, n))
+ return p;
+
+ p += n;
+ }
+}
+
+int cg_path_get_unit(const char *path, char **ret) {
+ const char *e;
+ char *unit;
+ int r;
+
+ assert(path);
+ assert(ret);
+
+ e = skip_slices(path);
+
+ r = cg_path_decode_unit(e, &unit);
+ if (r < 0)
+ return r;
+
+ /* We skipped over the slices, don't accept any now */
+ if (endswith(unit, ".slice")) {
+ free(unit);
+ return -ENXIO;
+ }
+
+ *ret = unit;
+ return 0;
+}
+
+int cg_pid_get_unit(pid_t pid, char **unit) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ assert(unit);
+
+ r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ return cg_path_get_unit(cgroup, unit);
+}
+
+/**
+ * Skip session-*.scope, but require it to be there.
+ */
+static const char *skip_session(const char *p) {
+ size_t n;
+
+ if (isempty(p))
+ return NULL;
+
+ p += strspn(p, "/");
+
+ n = strcspn(p, "/");
+ if (n < strlen("session-x.scope"))
+ return NULL;
+
+ if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
+ char buf[n - 8 - 6 + 1];
+
+ memcpy(buf, p + 8, n - 8 - 6);
+ buf[n - 8 - 6] = 0;
+
+ /* Note that session scopes never need unescaping,
+ * since they cannot conflict with the kernel's own
+ * names, hence we don't need to call cg_unescape()
+ * here. */
+
+ if (!session_id_valid(buf))
+ return false;
+
+ p += n;
+ p += strspn(p, "/");
+ return p;
+ }
+
+ return NULL;
+}
+
+/**
+ * Skip user@*.service, but require it to be there.
+ */
+static const char *skip_user_manager(const char *p) {
+ size_t n;
+
+ if (isempty(p))
+ return NULL;
+
+ p += strspn(p, "/");
+
+ n = strcspn(p, "/");
+ if (n < strlen("user@x.service"))
+ return NULL;
+
+ if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
+ char buf[n - 5 - 8 + 1];
+
+ memcpy(buf, p + 5, n - 5 - 8);
+ buf[n - 5 - 8] = 0;
+
+ /* Note that user manager services never need unescaping,
+ * since they cannot conflict with the kernel's own
+ * names, hence we don't need to call cg_unescape()
+ * here. */
+
+ if (parse_uid(buf, NULL) < 0)
+ return NULL;
+
+ p += n;
+ p += strspn(p, "/");
+
+ return p;
+ }
+
+ return NULL;
+}
+
+static const char *skip_user_prefix(const char *path) {
+ const char *e, *t;
+
+ assert(path);
+
+ /* Skip slices, if there are any */
+ e = skip_slices(path);
+
+ /* Skip the user manager, if it's in the path now... */
+ t = skip_user_manager(e);
+ if (t)
+ return t;
+
+ /* Alternatively skip the user session if it is in the path... */
+ return skip_session(e);
+}
+
+int cg_path_get_user_unit(const char *path, char **ret) {
+ const char *t;
+
+ assert(path);
+ assert(ret);
+
+ t = skip_user_prefix(path);
+ if (!t)
+ return -ENXIO;
+
+ /* And from here on it looks pretty much the same as for a
+ * system unit, hence let's use the same parser from here
+ * on. */
+ return cg_path_get_unit(t, ret);
+}
+
+int cg_pid_get_user_unit(pid_t pid, char **unit) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ assert(unit);
+
+ r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ return cg_path_get_user_unit(cgroup, unit);
+}
+
+int cg_path_get_machine_name(const char *path, char **machine) {
+ _cleanup_free_ char *u = NULL;
+ const char *sl;
+ int r;
+
+ r = cg_path_get_unit(path, &u);
+ if (r < 0)
+ return r;
+
+ sl = strjoina("/run/systemd/machines/unit:", u);
+ return readlink_malloc(sl, machine);
+}
+
+int cg_pid_get_machine_name(pid_t pid, char **machine) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ assert(machine);
+
+ r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ return cg_path_get_machine_name(cgroup, machine);
+}
+#endif // 0
+
+int cg_path_get_session(const char *path, char **session) {
+ /* Elogind uses a flat hierarchy, just "/SESSION". The only
+ wrinkle is that SESSION might be escaped. */
+#if 0
+ _cleanup_free_ char *unit = NULL;
+ char *start, *end;
+ int r;
+
+ assert(path);
+
+ r = cg_path_get_unit(path, &unit);
+ if (r < 0)
+ return r;
+
+ start = startswith(unit, "session-");
+ if (!start)
+ return -ENXIO;
+ end = endswith(start, ".scope");
+ if (!end)
+ return -ENXIO;
+
+ *end = 0;
+ if (!session_id_valid(start))
+ return -ENXIO;
+#else
+ const char *e, *n, *start;
+
+ assert(path);
+ log_debug_elogind("path is \"%s\"", path);
+ assert(path[0] == '/');
+
+ e = path + 1;
+ n = strchrnul(e, '/');
+ if (e == n)
+ return -ENOENT;
+
+ start = strndupa(e, n - e);
+ start = cg_unescape(start);
+
+ if (!start[0])
+ return -ENOENT;
+#endif // 0
+
+ if (session) {
+ char *rr;
+
+ log_debug_elogind("found session: \"%s\"", start);
+ rr = strdup(start);
+ if (!rr)
+ return -ENOMEM;
+
+ *session = rr;
+ }
+
+ return 0;
+}
+
+int cg_pid_get_session(pid_t pid, char **session) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ return cg_path_get_session(cgroup, session);
+}
+
+/// UNNEEDED by elogind
+#if 0
+int cg_path_get_owner_uid(const char *path, uid_t *uid) {
+ _cleanup_free_ char *slice = NULL;
+ char *start, *end;
+ int r;
+
+ assert(path);
+
+ r = cg_path_get_slice(path, &slice);
+ if (r < 0)
+ return r;
+
+ start = startswith(slice, "user-");
+ if (!start)
+ return -ENXIO;
+ end = endswith(start, ".slice");
+ if (!end)
+ return -ENXIO;
+
+ *end = 0;
+ if (parse_uid(start, uid) < 0)
+ return -ENXIO;
+
+ return 0;
+}
+
+int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ return cg_path_get_owner_uid(cgroup, uid);
+}
+
+int cg_path_get_slice(const char *p, char **slice) {
+ const char *e = NULL;
+
+ assert(p);
+ assert(slice);
+
+ /* Finds the right-most slice unit from the beginning, but
+ * stops before we come to the first non-slice unit. */
+
+ for (;;) {
+ size_t n;
+
+ p += strspn(p, "/");
+
+ n = strcspn(p, "/");
+ if (!valid_slice_name(p, n)) {
+
+ if (!e) {
+ char *s;
+
+ s = strdup("-.slice");
+ if (!s)
+ return -ENOMEM;
+
+ *slice = s;
+ return 0;
+ }
+
+ return cg_path_decode_unit(e, slice);
+ }
+
+ e = p;
+ p += n;
+ }
+}
+
+int cg_pid_get_slice(pid_t pid, char **slice) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ assert(slice);
+
+ r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ return cg_path_get_slice(cgroup, slice);
+}
+
+int cg_path_get_user_slice(const char *p, char **slice) {
+ const char *t;
+ assert(p);
+ assert(slice);
+
+ t = skip_user_prefix(p);
+ if (!t)
+ return -ENXIO;
+
+ /* And now it looks pretty much the same as for a system
+ * slice, so let's just use the same parser from here on. */
+ return cg_path_get_slice(t, slice);
+}
+
+int cg_pid_get_user_slice(pid_t pid, char **slice) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ assert(slice);
+
+ r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ return cg_path_get_user_slice(cgroup, slice);
+}
+#endif // 0
+
+char *cg_escape(const char *p) {
+ bool need_prefix = false;
+
+ /* This implements very minimal escaping for names to be used
+ * as file names in the cgroup tree: any name which might
+ * conflict with a kernel name or is prefixed with '_' is
+ * prefixed with a '_'. That way, when reading cgroup names it
+ * is sufficient to remove a single prefixing underscore if
+ * there is one. */
+
+ /* The return value of this function (unlike cg_unescape())
+ * needs free()! */
+
+ if (p[0] == 0 ||
+ p[0] == '_' ||
+ p[0] == '.' ||
+ streq(p, "notify_on_release") ||
+ streq(p, "release_agent") ||
+ streq(p, "tasks") ||
+ startswith(p, "cgroup."))
+ need_prefix = true;
+ else {
+ const char *dot;
+
+ dot = strrchr(p, '.');
+ if (dot) {
+ CGroupController c;
+ size_t l = dot - p;
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+ const char *n;
+
+ n = cgroup_controller_to_string(c);
+
+ if (l != strlen(n))
+ continue;
+
+ if (memcmp(p, n, l) != 0)
+ continue;
+
+ need_prefix = true;
+ break;
+ }
+ }
+ }
+
+ if (need_prefix)
+ return strappend("_", p);
+
+ return strdup(p);
+}
+
+char *cg_unescape(const char *p) {
+ assert(p);
+
+ /* The return value of this function (unlike cg_escape())
+ * doesn't need free()! */
+
+ if (p[0] == '_')
+ return (char*) p+1;
+
+ return (char*) p;
+}
+
+#define CONTROLLER_VALID \
+ DIGITS LETTERS \
+ "_"
+
+bool cg_controller_is_valid(const char *p) {
+ const char *t, *s;
+
+ if (!p)
+ return false;
+
+ s = startswith(p, "name=");
+ if (s)
+ p = s;
+
+ if (*p == 0 || *p == '_')
+ return false;
+
+ for (t = p; *t; t++)
+ if (!strchr(CONTROLLER_VALID, *t))
+ return false;
+
+ if (t - p > FILENAME_MAX)
+ return false;
+
+ return true;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int cg_slice_to_path(const char *unit, char **ret) {
+ _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
+ const char *dash;
+ int r;
+
+ assert(unit);
+ assert(ret);
+
+ if (streq(unit, "-.slice")) {
+ char *x;
+
+ x = strdup("");
+ if (!x)
+ return -ENOMEM;
+ *ret = x;
+ return 0;
+ }
+
+ if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
+ return -EINVAL;
+
+ if (!endswith(unit, ".slice"))
+ return -EINVAL;
+
+ r = unit_name_to_prefix(unit, &p);
+ if (r < 0)
+ return r;
+
+ dash = strchr(p, '-');
+
+ /* Don't allow initial dashes */
+ if (dash == p)
+ return -EINVAL;
+
+ while (dash) {
+ _cleanup_free_ char *escaped = NULL;
+ char n[dash - p + sizeof(".slice")];
+
+ /* Don't allow trailing or double dashes */
+ if (dash[1] == 0 || dash[1] == '-')
+ return -EINVAL;
+
+ strcpy(stpncpy(n, p, dash - p), ".slice");
+ if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
+ return -EINVAL;
+
+ escaped = cg_escape(n);
+ if (!escaped)
+ return -ENOMEM;
+
+ if (!strextend(&s, escaped, "/", NULL))
+ return -ENOMEM;
+
+ dash = strchr(dash+1, '-');
+ }
+
+ e = cg_escape(unit);
+ if (!e)
+ return -ENOMEM;
+
+ if (!strextend(&s, e, NULL))
+ return -ENOMEM;
+
+ *ret = s;
+ s = NULL;
+
+ return 0;
+}
+#endif // 0
+
+int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
+ _cleanup_free_ char *p = NULL;
+ int r;
+
+ r = cg_get_path(controller, path, attribute, &p);
+ if (r < 0)
+ return r;
+
+ return write_string_file(p, value, 0);
+}
+
+/// UNNEEDED by elogind
+#if 0
+int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
+ _cleanup_free_ char *p = NULL;
+ int r;
+
+ r = cg_get_path(controller, path, attribute, &p);
+ if (r < 0)
+ return r;
+
+ return read_one_line_file(p, ret);
+}
+
+int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
+ CGroupController c;
+ int r, unified;
+
+ /* This one will create a cgroup in our private tree, but also
+ * duplicate it in the trees specified in mask, and remove it
+ * in all others */
+
+ /* First create the cgroup in our own hierarchy. */
+ r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
+ if (r < 0)
+ return r;
+
+ /* If we are in the unified hierarchy, we are done now */
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified > 0)
+ return 0;
+
+ /* Otherwise, do the same in the other hierarchies */
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+ CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
+ const char *n;
+
+ n = cgroup_controller_to_string(c);
+
+ if (mask & bit)
+ (void) cg_create(n, path);
+ else if (supported & bit)
+ (void) cg_trim(n, path, true);
+ }
+
+ return 0;
+}
+
+int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
+ CGroupController c;
+ int r, unified;
+
+ r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
+ if (r < 0)
+ return r;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified > 0)
+ return 0;
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+ CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
+ const char *p = NULL;
+
+ if (!(supported & bit))
+ continue;
+
+ if (path_callback)
+ p = path_callback(bit, userdata);
+
+ if (!p)
+ p = path;
+
+ (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
+ }
+
+ return 0;
+}
+
+int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
+ Iterator i;
+ void *pidp;
+ int r = 0;
+
+ SET_FOREACH(pidp, pids, i) {
+ pid_t pid = PTR_TO_PID(pidp);
+ int q;
+
+ q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
+ if (q < 0 && r >= 0)
+ r = q;
+ }
+
+ return r;
+}
+
+int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
+ CGroupController c;
+ int r = 0, unified;
+
+ if (!path_equal(from, to)) {
+ r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
+ if (r < 0)
+ return r;
+ }
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified > 0)
+ return r;
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+ CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
+ const char *p = NULL;
+
+ if (!(supported & bit))
+ continue;
+
+ if (to_callback)
+ p = to_callback(bit, userdata);
+
+ if (!p)
+ p = to;
+
+ (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, false, false);
+ }
+
+ return 0;
+}
+
+int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
+ CGroupController c;
+ int r, unified;
+
+ r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
+ if (r < 0)
+ return r;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified > 0)
+ return r;
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+ CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
+
+ if (!(supported & bit))
+ continue;
+
+ (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
+ }
+
+ return 0;
+}
+#endif // 0
+
+int cg_mask_supported(CGroupMask *ret) {
+ CGroupMask mask = 0;
+ int r, unified;
+
+ /* Determines the mask of supported cgroup controllers. Only
+ * includes controllers we can make sense of and that are
+ * actually accessible. */
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (unified > 0) {
+ _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
+ const char *c;
+
+ /* In the unified hierarchy we can read the supported
+ * and accessible controllers from a the top-level
+ * cgroup attribute */
+
+ r = cg_get_root_path(&root);
+ if (r < 0)
+ return r;
+
+ r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
+ if (r < 0)
+ return r;
+
+ r = read_one_line_file(path, &controllers);
+ if (r < 0)
+ return r;
+
+ c = controllers;
+ for (;;) {
+ _cleanup_free_ char *n = NULL;
+ CGroupController v;
+
+ r = extract_first_word(&c, &n, NULL, 0);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
+
+ v = cgroup_controller_from_string(n);
+ if (v < 0)
+ continue;
+
+ mask |= CGROUP_CONTROLLER_TO_MASK(v);
+ }
+
+ /* Currently, we only support the memory controller in
+ * the unified hierarchy, mask everything else off. */
+ mask &= CGROUP_MASK_MEMORY;
+
+ } else {
+ CGroupController c;
+
+ /* In the legacy hierarchy, we check whether which
+ * hierarchies are mounted. */
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+ const char *n;
+
+ n = cgroup_controller_to_string(c);
+ if (controller_is_accessible(n) >= 0)
+ mask |= CGROUP_CONTROLLER_TO_MASK(c);
+ }
+ }
+
+ *ret = mask;
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int cg_kernel_controllers(Set *controllers) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char buf[LINE_MAX];
+ int r;
+
+ assert(controllers);
+
+ /* Determines the full list of kernel-known controllers. Might
+ * include controllers we don't actually support, arbitrary
+ * named hierarchies and controllers that aren't currently
+ * accessible (because not mounted). */
+
+ f = fopen("/proc/cgroups", "re");
+ if (!f) {
+ if (errno == ENOENT)
+ return 0;
+ return -errno;
+ }
+
+ /* Ignore the header line */
+ (void) fgets(buf, sizeof(buf), f);
+
+ for (;;) {
+ char *controller;
+ int enabled = 0;
+
+ errno = 0;
+ if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
+
+ if (feof(f))
+ break;
+
+ if (ferror(f) && errno != 0)
+ return -errno;
+
+ return -EBADMSG;
+ }
+
+ if (!enabled) {
+ free(controller);
+ continue;
+ }
+
+ if (!cg_controller_is_valid(controller)) {
+ free(controller);
+ return -EBADMSG;
+ }
+
+ r = set_consume(controllers, controller);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+#endif // 0
+
+static thread_local int unified_cache = -1;
+
+int cg_unified(void) {
+ struct statfs fs;
+
+ /* Checks if we support the unified hierarchy. Returns an
+ * error when the cgroup hierarchies aren't mounted yet or we
+ * have any other trouble determining if the unified hierarchy
+ * is supported. */
+
+ if (unified_cache >= 0)
+ return unified_cache;
+
+ if (statfs("/sys/fs/cgroup/", &fs) < 0)
+ return -errno;
+
+/// elogind can not support the unified hierarchy as a controller,
+/// so always assume a classical hierarchy.
+/// If, ond only *if*, someone really wants to substitute systemd-login
+/// in an environment managed by systemd with elogin, we might have to
+/// add such a support.
+#if 0
+ if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC))
+ unified_cache = true;
+ else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
+#else
+ if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
+#endif // 0
+ unified_cache = false;
+ else
+ return -ENOEXEC;
+
+ return unified_cache;
+}
+
+/// UNNEEDED by elogind
+#if 0
+void cg_unified_flush(void) {
+ unified_cache = -1;
+}
+
+int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
+ _cleanup_free_ char *fs = NULL;
+ CGroupController c;
+ int r, unified;
+
+ assert(p);
+
+ if (supported == 0)
+ return 0;
+
+ unified = cg_unified();
+ if (unified < 0)
+ return unified;
+ if (!unified) /* on the legacy hiearchy there's no joining of controllers defined */
+ return 0;
+
+ r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
+ if (r < 0)
+ return r;
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+ CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
+ const char *n;
+
+ if (!(supported & bit))
+ continue;
+
+ n = cgroup_controller_to_string(c);
+ {
+ char s[1 + strlen(n) + 1];
+
+ s[0] = mask & bit ? '+' : '-';
+ strcpy(s + 1, n);
+
+ r = write_string_file(fs, s, 0);
+ if (r < 0)
+ log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
+ }
+ }
+
+ return 0;
+}
+
+bool cg_is_unified_wanted(void) {
+ static thread_local int wanted = -1;
+ int r, unified;
+
+ /* If the hierarchy is already mounted, then follow whatever
+ * was chosen for it. */
+ unified = cg_unified();
+ if (unified >= 0)
+ return unified;
+
+ /* Otherwise, let's see what the kernel command line has to
+ * say. Since checking that is expensive, let's cache the
+ * result. */
+ if (wanted >= 0)
+ return wanted;
+
+ r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy", NULL);
+ if (r > 0)
+ return (wanted = true);
+ else {
+ _cleanup_free_ char *value = NULL;
+
+ r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy=", &value);
+ if (r < 0)
+ return false;
+ if (r == 0)
+ return (wanted = false);
+
+ return (wanted = parse_boolean(value) > 0);
+ }
+}
+
+bool cg_is_legacy_wanted(void) {
+ return !cg_is_unified_wanted();
+}
+#else
+bool cg_is_legacy_wanted(void) {
+ return true;
+}
+#endif // 0
+
+static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
+ [CGROUP_CONTROLLER_CPU] = "cpu",
+ [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
+ [CGROUP_CONTROLLER_BLKIO] = "blkio",
+ [CGROUP_CONTROLLER_MEMORY] = "memory",
+ [CGROUP_CONTROLLER_DEVICE] = "devices",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);
#include "set.h"
#include "def.h"
+/* An enum of well known cgroup controllers */
+typedef enum CGroupController {
+ CGROUP_CONTROLLER_CPU,
+ CGROUP_CONTROLLER_CPUACCT,
+ CGROUP_CONTROLLER_BLKIO,
+ CGROUP_CONTROLLER_MEMORY,
+ CGROUP_CONTROLLER_DEVICE,
+ _CGROUP_CONTROLLER_MAX,
+ _CGROUP_CONTROLLER_INVALID = -1,
+} CGroupController;
+
+#define CGROUP_CONTROLLER_TO_MASK(c) (1 << (c))
+
/* A bit mask of well known cgroup controllers */
-typedef enum CGroupControllerMask {
- CGROUP_CPU = 1,
- CGROUP_CPUACCT = 2,
- CGROUP_BLKIO = 4,
- CGROUP_MEMORY = 8,
- CGROUP_DEVICE = 16,
- _CGROUP_CONTROLLER_MASK_ALL = 31
-} CGroupControllerMask;
+typedef enum CGroupMask {
+ CGROUP_MASK_CPU = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_CPU),
+ CGROUP_MASK_CPUACCT = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_CPUACCT),
+ CGROUP_MASK_BLKIO = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BLKIO),
+ CGROUP_MASK_MEMORY = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_MEMORY),
+ CGROUP_MASK_DEVICE = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_DEVICE),
+ _CGROUP_MASK_ALL = CGROUP_CONTROLLER_TO_MASK(_CGROUP_CONTROLLER_MAX) - 1
+} CGroupMask;
/*
* General rules:
int cg_trim(const char *controller, const char *path, bool delete_root);
int cg_rmdir(const char *controller, const char *path);
-int cg_delete(const char *controller, const char *path);
int cg_create(const char *controller, const char *path);
int cg_attach(const char *controller, const char *path, pid_t pid);
int cg_create_and_attach(const char *controller, const char *path, pid_t pid);
int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value);
-int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret);
+// UNNEEDED int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret);
-int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid);
-int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid);
+// UNNEEDED int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid);
+// UNNEEDED int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid);
int cg_install_release_agent(const char *controller, const char *agent);
int cg_uninstall_release_agent(const char *controller);
-int cg_is_empty(const char *controller, const char *path, bool ignore_self);
-int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self);
+int cg_is_empty(const char *controller, const char *path);
+int cg_is_empty_recursive(const char *controller, const char *path);
int cg_get_root_path(char **path);
int cg_path_get_session(const char *path, char **session);
+// UNNEEDED int cg_path_get_owner_uid(const char *path, uid_t *uid);
+// UNNEEDED int cg_path_get_unit(const char *path, char **unit);
+// UNNEEDED int cg_path_get_user_unit(const char *path, char **unit);
+// UNNEEDED int cg_path_get_machine_name(const char *path, char **machine);
+// UNNEEDED int cg_path_get_slice(const char *path, char **slice);
+// UNNEEDED int cg_path_get_user_slice(const char *path, char **slice);
int cg_shift_path(const char *cgroup, const char *cached_root, const char **shifted);
int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **cgroup);
int cg_pid_get_session(pid_t pid, char **session);
+// UNNEEDED int cg_pid_get_owner_uid(pid_t pid, uid_t *uid);
+// UNNEEDED int cg_pid_get_unit(pid_t pid, char **unit);
+// UNNEEDED int cg_pid_get_user_unit(pid_t pid, char **unit);
+// UNNEEDED int cg_pid_get_machine_name(pid_t pid, char **machine);
+// UNNEEDED int cg_pid_get_slice(pid_t pid, char **slice);
+// UNNEEDED int cg_pid_get_user_slice(pid_t pid, char **slice);
+
+// UNNEEDED int cg_path_decode_unit(const char *cgroup, char **unit);
char *cg_escape(const char *p);
char *cg_unescape(const char *p) _pure_;
-bool cg_controller_is_valid(const char *p, bool allow_named);
+bool cg_controller_is_valid(const char *p);
+
+// UNNEEDED int cg_slice_to_path(const char *unit, char **ret);
+
+typedef const char* (*cg_migrate_callback_t)(CGroupMask mask, void *userdata);
+
+// UNNEEDED int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path);
+// UNNEEDED int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t callback, void *userdata);
+// UNNEEDED int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t callback, void *userdata);
+// UNNEEDED int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t callback, void *userdata);
+// UNNEEDED int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root);
+// UNNEEDED int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p);
+
+int cg_mask_supported(CGroupMask *ret);
-typedef const char* (*cg_migrate_callback_t)(CGroupControllerMask mask, void *userdata);
+// UNNEEDED int cg_kernel_controllers(Set *controllers);
-int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path);
-int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t callback, void *userdata);
-int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t callback, void *userdata);
-int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t callback, void *userdata);
-int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root);
+int cg_unified(void);
+// UNNEEDED void cg_unified_flush(void);
-CGroupControllerMask cg_mask_supported(void);
+// UNNEEDED bool cg_is_unified_wanted(void);
+bool cg_is_legacy_wanted(void);
-int cg_kernel_controllers(Set *controllers);
+const char* cgroup_controller_to_string(CGroupController c) _const_;
+CGroupController cgroup_controller_from_string(const char *s) _pure_;
static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
_cleanup_closedir_ DIR *dir = NULL;
- char *dirpath;
+ const char *dirpath;
+ int r;
assert(path);
assert(suffix);
- dirpath = strjoina(root ? root : "", path);
+ dirpath = prefix_roota(root, path);
dir = opendir(dirpath);
if (!dir) {
for (;;) {
struct dirent *de;
char *p;
- int r;
errno = 0;
de = readdir(dir);
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-
-int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...);
-int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char* const* dirs);
-int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *dirs);
+int conf_files_list(char ***ret, const char *suffix, const char *root, const char *dir, ...);
+int conf_files_list_strv(char ***ret, const char *suffix, const char *root, const char* const* dirs);
+int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, const char *dirs);
#include <sys/xattr.h>
#include "util.h"
-#include "btrfs-util.h"
+// #include "btrfs-util.h"
+#include "strv.h"
#include "copy.h"
#define COPY_BUFFER_SIZE (16*1024)
int copy_bytes(int fdf, int fdt, off_t max_bytes, bool try_reflink) {
- bool try_sendfile = true;
+ bool try_sendfile = true, try_splice = true;
int r;
assert(fdf >= 0);
assert(fdt >= 0);
-
+#if 0
/* Try btrfs reflinks first. */
if (try_reflink && max_bytes == (off_t) -1) {
r = btrfs_reflink(fdf, fdt);
if (r >= 0)
return r;
}
-
+#endif // 0
for (;;) {
size_t m = COPY_BUFFER_SIZE;
ssize_t n;
} else if (n == 0) /* EOF */
break;
else if (n > 0)
- /* Succcess! */
+ /* Success! */
+ goto next;
+ }
+
+ /* The try splice, unless we already tried */
+ if (try_splice) {
+ n = splice(fdf, NULL, fdt, NULL, m, 0);
+ if (n < 0) {
+ if (errno != EINVAL && errno != ENOSYS)
+ return -errno;
+
+ try_splice = false;
+ /* use fallback below */
+ } else if (n == 0) /* EOF */
+ break;
+ else if (n > 0)
+ /* Success! */
goto next;
}
return 0;
}
+// UNNEEDED by elogind
+#if 0
static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
_cleanup_free_ char *target = NULL;
int r;
(void) copy_xattr(dirfd(d), fdt);
}
- FOREACH_DIRENT(de, d, return -errno) {
+ FOREACH_DIRENT_ALL(de, d, return -errno) {
struct stat buf;
int q;
+ if (STR_IN_SET(de->d_name, ".", ".."))
+ continue;
+
if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
r = -errno;
continue;
}
if (chattr_flags != 0)
- (void) chattr_fd(fdt, true, chattr_flags);
+ (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
r = copy_file_fd(from, fdt, true);
if (r < 0) {
assert(from);
assert(to);
- r = tempfn_random(to, &t);
+ r = tempfn_random(to, NULL, &t);
if (r < 0)
return r;
sza *= 2;
- free(bufa);
- bufa = NULL;
+ bufa = mfree(bufa);
}
p = bufa;
if (m < 0) {
if (errno == ERANGE) {
szb *= 2;
- free(bufb);
- bufb = NULL;
+ bufb = mfree(bufb);
continue;
}
return ret;
}
+#endif // 0
#include <stdbool.h>
#include <sys/types.h>
-int copy_file_fd(const char *from, int to, bool try_reflink);
-int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags);
-int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags);
-int copy_tree(const char *from, const char *to, bool merge);
-int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge);
-int copy_directory_fd(int dirfd, const char *to, bool merge);
+// UNNEEDED int copy_file_fd(const char *from, int to, bool try_reflink);
+// UNNEEDED int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags);
+// UNNEEDED int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags);
+// UNNEEDED int copy_tree(const char *from, const char *to, bool merge);
+// UNNEEDED int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge);
+// UNNEEDED int copy_directory_fd(int dirfd, const char *to, bool merge);
int copy_bytes(int fdf, int fdt, off_t max_bytes, bool try_reflink);
-int copy_times(int fdf, int fdt);
-int copy_xattr(int fdf, int fdt);
+// UNNEEDED int copy_times(int fdf, int fdt);
+// UNNEEDED int copy_xattr(int fdf, int fdt);
* the watchdog pings will keep the loop busy. */
#define DEFAULT_EXIT_USEC (30*USEC_PER_SEC)
-#define SYSTEMD_CGROUP_CONTROLLER "name=elogind"
+#ifndef SYSTEMD_CGROUP_CONTROLLER
+# define SYSTEMD_CGROUP_CONTROLLER "name=elogind"
+#endif
#define SIGNALS_CRASH_HANDLER SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT
#define SIGNALS_IGNORE SIGPIPE
#define UNIX_SYSTEM_BUS_ADDRESS "unix:path=/var/run/dbus/system_bus_socket"
#define KERNEL_SYSTEM_BUS_ADDRESS "kernel:path=/sys/fs/kdbus/0-system/bus"
-
-#ifdef ENABLE_KDBUS
-# define DEFAULT_SYSTEM_BUS_ADDRESS KERNEL_SYSTEM_BUS_ADDRESS ";" UNIX_SYSTEM_BUS_ADDRESS
-#else
-# define DEFAULT_SYSTEM_BUS_ADDRESS UNIX_SYSTEM_BUS_ADDRESS
-#endif
-
+#define DEFAULT_SYSTEM_BUS_ADDRESS KERNEL_SYSTEM_BUS_ADDRESS ";" UNIX_SYSTEM_BUS_ADDRESS
#define UNIX_USER_BUS_ADDRESS_FMT "unix:path=%s/bus"
#define KERNEL_USER_BUS_ADDRESS_FMT "kernel:path=/sys/fs/kdbus/"UID_FMT"-user/bus"
.un.sun_path = "\0/org/freedesktop/plymouthd", \
}
+#ifndef TTY_GID
+#define TTY_GID 5
+#endif
+
#define NOTIFY_FD_MAX 768
#define NOTIFY_BUFFER_MAX PIPE_BUF
#include <string.h>
+#include "config.h"
#include "util.h"
#include "errno-list.h"
static const struct errno_name* lookup_errno(register const char *str,
- register unsigned int len);
+ register GPERF_LEN_TYPE len);
+
#include "errno-to-name.h"
#include "errno-from-name.h"
return sc->id;
}
+/// UNNEEDED by elogind
+#if 0
int errno_max(void) {
return ELEMENTSOF(errno_names);
}
+#endif // 0
const char *errno_to_name(int id);
int errno_from_name(const char *name);
-int errno_max(void);
+// UNNEEDED int errno_max(void);
if (r < 0)
return r;
- r = write_string_file_atomic(fn, line);
+ r = write_string_file(fn, line, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
mac_selinux_create_file_clear();
return r;
}
+/// UNNEEDED by elogind
+#if 0
int write_env_file_label(const char *fname, char **l) {
int r;
return r;
}
+#endif // 0
#include "fileio.h"
int write_string_file_atomic_label(const char *fn, const char *line);
-int write_env_file_label(const char *fname, char **l);
-int fopen_temporary_label(const char *target,
- const char *path, FILE **f, char **temp_path);
+// UNNEEDED int write_env_file_label(const char *fname, char **l);
+// UNNEEDED int fopen_temporary_label(const char *target,
+// const char *path, FILE **f, char **temp_path);
#include "ctype.h"
#include "fileio.h"
-int write_string_stream(FILE *f, const char *line) {
+int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
+
assert(f);
assert(line);
- errno = 0;
-
fputs(line, f);
- if (!endswith(line, "\n"))
+ if (enforce_newline && !endswith(line, "\n"))
fputc('\n', f);
- fflush(f);
-
- if (ferror(f))
- return errno ? -errno : -EIO;
-
- return 0;
+ return fflush_and_check(f);
}
-int write_string_file(const char *fn, const char *line) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(fn);
- assert(line);
-
- f = fopen(fn, "we");
- if (!f)
- return -errno;
-
- return write_string_stream(f, line);
-}
-
-int write_string_file_no_create(const char *fn, const char *line) {
- _cleanup_fclose_ FILE *f = NULL;
- int fd;
-
- assert(fn);
- assert(line);
-
- /* We manually build our own version of fopen(..., "we") that
- * works without O_CREAT */
- fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
- return -errno;
-
- f = fdopen(fd, "we");
- if (!f) {
- safe_close(fd);
- return -errno;
- }
-
- return write_string_stream(f, line);
-}
-
-int write_string_file_atomic(const char *fn, const char *line) {
+static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int r;
fchmod_umask(fileno(f), 0644);
- r = write_string_stream(f, line);
+ r = write_string_stream(f, line, enforce_newline);
if (r >= 0) {
if (rename(p, fn) < 0)
r = -errno;
return r;
}
+int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
+ _cleanup_fclose_ FILE *f = NULL;
+
+ assert(fn);
+ assert(line);
+
+ if (flags & WRITE_STRING_FILE_ATOMIC) {
+ assert(flags & WRITE_STRING_FILE_CREATE);
+
+ return write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
+ }
+
+ if (flags & WRITE_STRING_FILE_CREATE) {
+ f = fopen(fn, "we");
+ if (!f)
+ return -errno;
+ } else {
+ int fd;
+
+ /* We manually build our own version of fopen(..., "we") that
+ * works without O_CREAT */
+ fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0)
+ return -errno;
+
+ f = fdopen(fd, "we");
+ if (!f) {
+ safe_close(fd);
+ return -errno;
+ }
+ }
+
+ return write_string_stream(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
+}
+
int read_one_line_file(const char *fn, char **line) {
_cleanup_fclose_ FILE *f = NULL;
char t[LINE_MAX], *c;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
+int verify_one_line_file(const char *fn, const char *line) {
+ _cleanup_free_ char *value = NULL;
+ int r;
+
+ r = read_one_line_file(fn, &value);
+ if (r < 0)
+ return r;
+
+ return streq(value, line);
+}
+#endif // 0
+
int read_full_stream(FILE *f, char **contents, size_t *size) {
size_t n, l;
_cleanup_free_ char *buf = NULL;
return 0;
}
+/// UNNEDED by elogind
+#if 0
static int load_env_file_push_pairs(
const char *filename, unsigned line,
const char *key, char *value,
*rl = m;
return 0;
}
+#endif // 0
static void write_env_var(FILE *f, const char *v) {
const char *p;
return r;
}
+/// UNNEEDED by elogind
+#if 0
int executable_is_script(const char *path, char **interpreter) {
int r;
_cleanup_free_ char *line = NULL;
*interpreter = ans;
return 1;
}
+#endif // 0
/**
* Retrieve one field from a file like /proc/self/status. pattern
*/
int get_status_field(const char *filename, const char *pattern, char **field) {
_cleanup_free_ char *status = NULL;
- char *t;
+ char *t, *f;
size_t len;
int r;
len = strcspn(t, WHITESPACE);
- *field = strndup(t, len);
- if (!*field)
+ f = strndup(t, len);
+ if (!f)
return -ENOMEM;
+ *field = f;
return 0;
}
#include "macro.h"
-int write_string_stream(FILE *f, const char *line);
-int write_string_file(const char *fn, const char *line);
-int write_string_file_no_create(const char *fn, const char *line);
-int write_string_file_atomic(const char *fn, const char *line);
+typedef enum {
+ WRITE_STRING_FILE_CREATE = 1,
+ WRITE_STRING_FILE_ATOMIC = 2,
+ WRITE_STRING_FILE_AVOID_NEWLINE = 4,
+} WriteStringFileFlags;
+
+int write_string_stream(FILE *f, const char *line, bool enforce_newline);
+int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags);
int read_one_line_file(const char *fn, char **line);
int read_full_file(const char *fn, char **contents, size_t *size);
int read_full_stream(FILE *f, char **contents, size_t *size);
+// UNNEEDED int verify_one_line_file(const char *fn, const char *line);
+
int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
int load_env_file(FILE *f, const char *fname, const char *separator, char ***l);
-int load_env_file_pairs(FILE *f, const char *fname, const char *separator, char ***l);
+// UNNEEDED int load_env_file_pairs(FILE *f, const char *fname, const char *separator, char ***l);
int write_env_file(const char *fname, char **l);
-int executable_is_script(const char *path, char **interpreter);
+// UNNEEDED int executable_is_script(const char *path, char **interpreter);
int get_status_field(const char *filename, const char *pattern, char **field);
#include <stdlib.h>
#include <errno.h>
+#include <pthread.h>
#include "util.h"
#include "hashmap.h"
#include "siphash24.h"
#include "strv.h"
#include "mempool.h"
+#include "random-util.h"
#ifdef ENABLE_DEBUG_HASHMAP
#include "list.h"
/* Distance from Initial Bucket */
typedef uint8_t dib_raw_t;
-#define DIB_RAW_OVERFLOW ((dib_raw_t)0xfdU) /* indicates DIB value is greater than representable */
-#define DIB_RAW_REHASH ((dib_raw_t)0xfeU) /* entry yet to be rehashed during in-place resize */
-#define DIB_RAW_FREE ((dib_raw_t)0xffU) /* a free bucket */
-#define DIB_RAW_INIT ((char)DIB_RAW_FREE) /* a byte to memset a DIB store with when initializing */
+#define DIB_RAW_OVERFLOW ((dib_raw_t)0xfdU) /* indicates DIB value is greater than representable */
+#define DIB_RAW_REHASH ((dib_raw_t)0xfeU) /* entry yet to be rehashed during in-place resize */
+#define DIB_RAW_FREE ((dib_raw_t)0xffU) /* a free bucket */
+#define DIB_RAW_INIT ((char)DIB_RAW_FREE) /* a byte to memset a DIB store with when initializing */
#define DIB_FREE UINT_MAX
/* Tracks all existing hashmaps. Get at it from gdb. See sd_dump_hashmaps.py */
static LIST_HEAD(struct hashmap_debug_info, hashmap_debug_list);
+static pthread_mutex_t hashmap_debug_list_mutex = PTHREAD_MUTEX_INITIALIZER;
#define HASHMAP_DEBUG_FIELDS struct hashmap_debug_info debug;
: hashmap_iterate_in_internal_order(h, i);
}
-void *internal_hashmap_iterate(HashmapBase *h, Iterator *i, const void **key) {
+bool internal_hashmap_iterate(HashmapBase *h, Iterator *i, void **value, const void **key) {
struct hashmap_base_entry *e;
void *data;
unsigned idx;
idx = hashmap_iterate_entry(h, i);
if (idx == IDX_NIL) {
+ if (value)
+ *value = NULL;
if (key)
*key = NULL;
- return NULL;
+ return false;
}
e = bucket_at(h, idx);
data = entry_value(h, e);
+ if (value)
+ *value = data;
if (key)
*key = e->key;
- return data;
+ return true;
}
-void *set_iterate(Set *s, Iterator *i) {
- return internal_hashmap_iterate(HASHMAP_BASE(s), i, NULL);
+bool set_iterate(Set *s, Iterator *i, void **value) {
+ return internal_hashmap_iterate(HASHMAP_BASE(s), i, value, NULL);
}
#define HASHMAP_FOREACH_IDX(idx, h, i) \
memset(p, DIB_RAW_INIT, sizeof(dib_raw_t) * hi->n_direct_buckets);
}
-static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
+static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
HashmapBase *h;
const struct hashmap_type_info *hi = &hashmap_type_info[type];
bool use_pool;
}
#ifdef ENABLE_DEBUG_HASHMAP
- LIST_PREPEND(debug_list, hashmap_debug_list, &h->debug);
h->debug.func = func;
h->debug.file = file;
h->debug.line = line;
+ assert_se(pthread_mutex_lock(&hashmap_debug_list_mutex) == 0);
+ LIST_PREPEND(debug_list, hashmap_debug_list, &h->debug);
+ assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
#endif
return h;
}
Hashmap *internal_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
- return (Hashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
+ return (Hashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
}
OrderedHashmap *internal_ordered_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
- return (OrderedHashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
+ return (OrderedHashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
}
Set *internal_set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
- return (Set*) hashmap_base_new(hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
+ return (Set*) hashmap_base_new(hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
}
static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops *hash_ops,
- enum HashmapType type HASHMAP_DEBUG_PARAMS) {
+ enum HashmapType type HASHMAP_DEBUG_PARAMS) {
HashmapBase *q;
assert(h);
if (*h)
return 0;
- q = hashmap_base_new(hash_ops, type HASHMAP_DEBUG_PASS_ARGS);
+ q = hashmap_base_new(hash_ops, type HASHMAP_DEBUG_PASS_ARGS);
if (!q)
return -ENOMEM;
}
int internal_hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
- return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
+ return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
}
int internal_ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
- return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
+ return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
}
int internal_set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
- return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
+ return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
}
static void hashmap_free_no_clear(HashmapBase *h) {
assert(!h->n_direct_entries);
#ifdef ENABLE_DEBUG_HASHMAP
+ assert_se(pthread_mutex_lock(&hashmap_debug_list_mutex) == 0);
LIST_REMOVE(debug_list, hashmap_debug_list, &h->debug);
+ assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
#endif
if (h->from_pool)
free(h);
}
-void internal_hashmap_free(HashmapBase *h) {
+HashmapBase *internal_hashmap_free(HashmapBase *h) {
/* Free the hashmap, but nothing in it */
- if (!h)
- return;
+ if (h) {
+ internal_hashmap_clear(h);
+ hashmap_free_no_clear(h);
+ }
- internal_hashmap_clear(h);
- hashmap_free_no_clear(h);
+ return NULL;
}
-void internal_hashmap_free_free(HashmapBase *h) {
+HashmapBase *internal_hashmap_free_free(HashmapBase *h) {
/* Free the hashmap and all data objects in it, but not the
* keys */
- if (!h)
- return;
+ if (h) {
+ internal_hashmap_clear_free(h);
+ hashmap_free_no_clear(h);
+ }
- internal_hashmap_clear_free(h);
- hashmap_free_no_clear(h);
+ return NULL;
}
-void hashmap_free_free_free(Hashmap *h) {
+Hashmap *hashmap_free_free_free(Hashmap *h) {
/* Free the hashmap and all data and key objects in it */
- if (!h)
- return;
+ if (h) {
+ hashmap_clear_free_free(h);
+ hashmap_free_no_clear(HASHMAP_BASE(h));
+ }
- hashmap_clear_free_free(h);
- hashmap_free_no_clear(HASHMAP_BASE(h));
+ return NULL;
}
void internal_hashmap_clear(HashmapBase *h) {
if (dib < distance) {
/* Found a wealthier entry. Go Robin Hood! */
-
bucket_set_dib(h, idx, distance);
/* swap the entries */
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int set_remove_and_put(Set *s, const void *old_key, const void *new_key) {
struct swap_entries swap;
struct hashmap_base_entry *e;
return 0;
}
+#endif // 0
int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value) {
struct swap_entries swap;
return sv;
}
+/// UNNEEDED by elogind
+#if 0
void *ordered_hashmap_next(OrderedHashmap *h, const void *key) {
struct ordered_hashmap_entry *e;
unsigned hash, idx;
- assert(key);
-
if (!h)
return NULL;
return NULL;
return ordered_bucket_at(h, e->iterate_next)->p.value;
}
-
+#endif // 0
int set_consume(Set *s, void *value) {
int r;
return r;
}
+/// UNNEEDED by elogind
+#if 0
int set_put_strdupv(Set *s, char **l) {
int n = 0, r;
char **i;
return n;
}
+#endif // 0
#define hashmap_new(ops) internal_hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS)
#define ordered_hashmap_new(ops) internal_ordered_hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS)
-void internal_hashmap_free(HashmapBase *h);
-static inline void hashmap_free(Hashmap *h) {
- internal_hashmap_free(HASHMAP_BASE(h));
+HashmapBase *internal_hashmap_free(HashmapBase *h);
+static inline Hashmap *hashmap_free(Hashmap *h) {
+ return (void*)internal_hashmap_free(HASHMAP_BASE(h));
}
-static inline void ordered_hashmap_free(OrderedHashmap *h) {
- internal_hashmap_free(HASHMAP_BASE(h));
+static inline OrderedHashmap *ordered_hashmap_free(OrderedHashmap *h) {
+ return (void*)internal_hashmap_free(HASHMAP_BASE(h));
}
-void internal_hashmap_free_free(HashmapBase *h);
-static inline void hashmap_free_free(Hashmap *h) {
- internal_hashmap_free_free(HASHMAP_BASE(h));
+HashmapBase *internal_hashmap_free_free(HashmapBase *h);
+static inline Hashmap *hashmap_free_free(Hashmap *h) {
+ return (void*)internal_hashmap_free_free(HASHMAP_BASE(h));
}
-static inline void ordered_hashmap_free_free(OrderedHashmap *h) {
- internal_hashmap_free_free(HASHMAP_BASE(h));
+static inline OrderedHashmap *ordered_hashmap_free_free(OrderedHashmap *h) {
+ return (void*)internal_hashmap_free_free(HASHMAP_BASE(h));
}
-void hashmap_free_free_free(Hashmap *h);
-static inline void ordered_hashmap_free_free_free(OrderedHashmap *h) {
- hashmap_free_free_free(PLAIN_HASHMAP(h));
+Hashmap *hashmap_free_free_free(Hashmap *h);
+static inline OrderedHashmap *ordered_hashmap_free_free_free(OrderedHashmap *h) {
+ return (void*)hashmap_free_free_free(PLAIN_HASHMAP(h));
}
HashmapBase *internal_hashmap_copy(HashmapBase *h);
return internal_hashmap_buckets(HASHMAP_BASE(h));
}
-void *internal_hashmap_iterate(HashmapBase *h, Iterator *i, const void **key);
-static inline void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key) {
- return internal_hashmap_iterate(HASHMAP_BASE(h), i, key);
+bool internal_hashmap_iterate(HashmapBase *h, Iterator *i, void **value, const void **key);
+static inline bool hashmap_iterate(Hashmap *h, Iterator *i, void **value, const void **key) {
+ return internal_hashmap_iterate(HASHMAP_BASE(h), i, value, key);
}
-static inline void *ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, const void **key) {
- return internal_hashmap_iterate(HASHMAP_BASE(h), i, key);
+static inline bool ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, void **value, const void **key) {
+ return internal_hashmap_iterate(HASHMAP_BASE(h), i, value, key);
}
void internal_hashmap_clear(HashmapBase *h);
}
/* no hashmap_next */
-void *ordered_hashmap_next(OrderedHashmap *h, const void *key);
+// UNNEEDED void *ordered_hashmap_next(OrderedHashmap *h, const void *key);
char **internal_hashmap_get_strv(HashmapBase *h);
static inline char **hashmap_get_strv(Hashmap *h) {
* It is safe to remove the current entry.
*/
#define HASHMAP_FOREACH(e, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); \
- (e); \
- (e) = hashmap_iterate((h), &(i), NULL))
+ for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), NULL); )
#define ORDERED_HASHMAP_FOREACH(e, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), NULL); \
- (e); \
- (e) = ordered_hashmap_iterate((h), &(i), NULL))
+ for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), NULL); )
#define HASHMAP_FOREACH_KEY(e, k, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); \
- (e); \
- (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
+ for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
#define ORDERED_HASHMAP_FOREACH_KEY(e, k, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)); \
- (e); \
- (e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)))
+ for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free);
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2015 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/utsname.h>
+#include <ctype.h>
+
+#include "util.h"
+#include "hostname-util.h"
+
+/// UNNEEDED by elogind
+#if 0
+bool hostname_is_set(void) {
+ struct utsname u;
+
+ assert_se(uname(&u) >= 0);
+
+ if (isempty(u.nodename))
+ return false;
+
+ /* This is the built-in kernel default host name */
+ if (streq(u.nodename, "(none)"))
+ return false;
+
+ return true;
+}
+
+char* gethostname_malloc(void) {
+ struct utsname u;
+
+ assert_se(uname(&u) >= 0);
+
+ if (isempty(u.nodename) || streq(u.nodename, "(none)"))
+ return strdup(u.sysname);
+
+ return strdup(u.nodename);
+}
+#endif // 0
+
+static bool hostname_valid_char(char c) {
+ return
+ (c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') ||
+ c == '-' ||
+ c == '_' ||
+ c == '.';
+}
+
+/**
+ * Check if s looks like a valid host name or FQDN. This does not do
+ * full DNS validation, but only checks if the name is composed of
+ * allowed characters and the length is not above the maximum allowed
+ * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
+ * allow_trailing_dot is true and at least two components are present
+ * in the name. Note that due to the restricted charset and length
+ * this call is substantially more conservative than
+ * dns_domain_is_valid().
+ */
+bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
+ unsigned n_dots = 0;
+ const char *p;
+ bool dot;
+
+ if (isempty(s))
+ return false;
+
+ /* Doesn't accept empty hostnames, hostnames with
+ * leading dots, and hostnames with multiple dots in a
+ * sequence. Also ensures that the length stays below
+ * HOST_NAME_MAX. */
+
+ for (p = s, dot = true; *p; p++) {
+ if (*p == '.') {
+ if (dot)
+ return false;
+
+ dot = true;
+ n_dots ++;
+ } else {
+ if (!hostname_valid_char(*p))
+ return false;
+
+ dot = false;
+ }
+ }
+
+ if (dot && (n_dots < 2 || !allow_trailing_dot))
+ return false;
+
+ if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on
+ * Linux, but DNS allows domain names
+ * up to 255 characters */
+ return false;
+
+ return true;
+}
+
+/// UNNEEDED by elogind
+#if 0
+char* hostname_cleanup(char *s) {
+ char *p, *d;
+ bool dot;
+
+ assert(s);
+
+ for (p = s, d = s, dot = true; *p; p++) {
+ if (*p == '.') {
+ if (dot)
+ continue;
+
+ *(d++) = '.';
+ dot = true;
+ } else if (hostname_valid_char(*p)) {
+ *(d++) = *p;
+ dot = false;
+ }
+
+ }
+
+ if (dot && d > s)
+ d[-1] = 0;
+ else
+ *d = 0;
+
+ strshorten(s, HOST_NAME_MAX);
+
+ return s;
+}
+#endif // 0
+
+bool is_localhost(const char *hostname) {
+ assert(hostname);
+
+ /* This tries to identify local host and domain names
+ * described in RFC6761 plus the redhatism of .localdomain */
+
+ return strcaseeq(hostname, "localhost") ||
+ strcaseeq(hostname, "localhost.") ||
+ strcaseeq(hostname, "localdomain.") ||
+ strcaseeq(hostname, "localdomain") ||
+ endswith_no_case(hostname, ".localhost") ||
+ endswith_no_case(hostname, ".localhost.") ||
+ endswith_no_case(hostname, ".localdomain") ||
+ endswith_no_case(hostname, ".localdomain.");
+}
+
+/// UNNEEDED by elogind
+#if 0
+bool is_gateway_hostname(const char *hostname) {
+ assert(hostname);
+
+ /* This tries to identify the valid syntaxes for the our
+ * synthetic "gateway" host. */
+
+ return
+ strcaseeq(hostname, "gateway") ||
+ strcaseeq(hostname, "gateway.");
+}
+
+int sethostname_idempotent(const char *s) {
+ char buf[HOST_NAME_MAX + 1] = {};
+
+ assert(s);
+
+ if (gethostname(buf, sizeof(buf)) < 0)
+ return -errno;
+
+ if (streq(buf, s))
+ return 0;
+
+ if (sethostname(s, strlen(s)) < 0)
+ return -errno;
+
+ return 1;
+}
+
+int read_hostname_config(const char *path, char **hostname) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char l[LINE_MAX];
+ char *name = NULL;
+
+ assert(path);
+ assert(hostname);
+
+ f = fopen(path, "re");
+ if (!f)
+ return -errno;
+
+ /* may have comments, ignore them */
+ FOREACH_LINE(l, f, return -errno) {
+ truncate_nl(l);
+ if (l[0] != '\0' && l[0] != '#') {
+ /* found line with value */
+ name = hostname_cleanup(l);
+ name = strdup(name);
+ if (!name)
+ return -ENOMEM;
+ break;
+ }
+ }
+
+ if (!name)
+ /* no non-empty line found */
+ return -ENOENT;
+
+ *hostname = name;
+ return 0;
+}
+#endif // 0
/***
This file is part of systemd.
- Copyright 2010-2013 Lennart Poettering
+ Copyright 2010-2015 Lennart Poettering
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
#include <stdbool.h>
-#include "sd-event.h"
+#include "macro.h"
-typedef struct PTYForward PTYForward;
+// UNNEEDED bool hostname_is_set(void);
-int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, bool read_only, PTYForward **f);
-PTYForward *pty_forward_free(PTYForward *f);
+// UNNEEDED char* gethostname_malloc(void);
-int pty_forward_get_last_char(PTYForward *f, char *ch);
+bool hostname_is_valid(const char *s, bool allow_trailing_dot) _pure_;
+// UNNEEDED char* hostname_cleanup(char *s);
-int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup);
-int pty_forward_get_ignore_vhangup(PTYForward *f);
+#define machine_name_is_valid(s) hostname_is_valid(s, false)
-DEFINE_TRIVIAL_CLEANUP_FUNC(PTYForward*, pty_forward_free);
+bool is_localhost(const char *hostname);
+// UNNEEDED bool is_gateway_hostname(const char *hostname);
+
+// UNNEEDED int sethostname_idempotent(const char *s);
+
+// UNNEEDED int read_hostname_config(const char *path, char **hostname);
return mac_smack_fix(path, false, false);
}
+/// UNNEEDED by elogind
+#if 0
int symlink_label(const char *old_path, const char *new_path) {
int r;
return mac_smack_fix(new_path, false, false);
}
+#endif // 0
int label_fix(const char *path, bool ignore_enoent, bool ignore_erofs);
int mkdir_label(const char *path, mode_t mode);
-int symlink_label(const char *old_path, const char *new_path);
+// UNNEEDED int symlink_label(const char *old_path, const char *new_path);
} \
} while(false)
+/* Insert an item before another one (a = where, b = what) */
+#define LIST_INSERT_BEFORE(name,head,a,b) \
+ do { \
+ typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
+ assert(_b); \
+ if (!_a) { \
+ if (!*_head) { \
+ _b->name##_next = NULL; \
+ _b->name##_prev = NULL; \
+ *_head = _b; \
+ } else { \
+ typeof(*(head)) *_tail = (head); \
+ while (_tail->name##_next) \
+ _tail = _tail->name##_next; \
+ _b->name##_next = NULL; \
+ _b->name##_prev = _tail; \
+ _tail->name##_next = _b; \
+ } \
+ } else { \
+ if ((_b->name##_prev = _a->name##_prev)) \
+ _b->name##_prev->name##_next = _b; \
+ _b->name##_next = _a; \
+ _a->name##_prev = _b; \
+ } \
+ } while(false)
+
#define LIST_JUST_US(name,item) \
(!(item)->name##_prev && !(item)->name##_next) \
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>
-#include <printf.h>
+#include "parse-printf-format.h"
+#include "sd-messages.h"
#include "log.h"
#include "util.h"
#include "missing.h"
#include "macro.h"
#include "socket-util.h"
+#include "formats-util.h"
+#include "process-util.h"
+#include "terminal-util.h"
+#include "signal-util.h"
#define SNDBUF_SIZE (8*1024*1024)
static bool show_color = false;
static bool show_location = false;
-static bool upgrade_syslog_to_journal = false;
+/// UNNEEDED by elogind static bool upgrade_syslog_to_journal = false;
/* Akin to glibc's __abort_msg; which is private and we hence cannot
* use here. */
return r;
}
+/// UNNEEDED by elogind
+#if 0
void log_close_journal(void) {
journal_fd = safe_close(journal_fd);
}
log_close_journal();
return r;
}
+#endif // 0
int log_open(void) {
int r;
* because there is no reason to close it. */
if (log_target == LOG_TARGET_NULL) {
- log_close_journal();
+ /// UNNEEDED by elogind log_close_journal();
log_close_syslog();
log_close_console();
return 0;
getpid() == 1 ||
isatty(STDERR_FILENO) <= 0) {
+/// elogind does not support logging to systemd-journald
+#if 0
if (log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
log_target == LOG_TARGET_JOURNAL) {
return r;
}
}
-
+#endif // 0
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_SYSLOG) {
r = log_open_syslog();
if (r >= 0) {
- log_close_journal();
+ /// UNNEEDED by elogind log_close_journal();
log_close_console();
return r;
}
if (log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_SAFE ||
- log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
+ /// UNNEEDED by elogind log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_KMSG) {
r = log_open_kmsg();
if (r >= 0) {
- log_close_journal();
+ /// UNNEEDED by elogind log_close_journal();
log_close_syslog();
log_close_console();
return r;
}
}
- log_close_journal();
+ /// UNNEEDED by elogind log_close_journal();
log_close_syslog();
return log_open_console();
assert(target >= 0);
assert(target < _LOG_TARGET_MAX);
+/// elogind does not support logging to systemd-journald
+#if 0
if (upgrade_syslog_to_journal) {
if (target == LOG_TARGET_SYSLOG)
target = LOG_TARGET_JOURNAL;
else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
target = LOG_TARGET_JOURNAL_OR_KMSG;
}
+#endif // 0
log_target = target;
}
void log_close(void) {
- log_close_journal();
+ /// UNNEDED by elogind log_close_journal();
log_close_syslog();
log_close_kmsg();
log_close_console();
}
+/// UNNEEDED by elogind
+#if 0
void log_forget_fds(void) {
console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
}
+#endif // 0
void log_set_max_level(int level) {
assert((level & LOG_PRIMASK) == level);
return 1;
}
+/// UNNEEDED by elogind
+#if 0
static int log_do_header(
char *header,
size_t size,
return 1;
}
+#endif // 0
static int log_dispatch(
int level,
if ((e = strpbrk(buffer, NEWLINE)))
*(e++) = 0;
+/// elogind does not support logging to systemd-journald
+#if 0
if (log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
log_target == LOG_TARGET_JOURNAL) {
log_open_kmsg();
}
}
+#endif // 0
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_SYSLOG) {
(log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_SAFE ||
log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
- log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
+ /// UNNEEDED by elogind log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
log_target == LOG_TARGET_KMSG)) {
k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer);
va_list ap) {
PROTECT_ERRNO;
- char buffer[LINE_MAX];
+ char *buffer, *b;
+ size_t l;
if (error < 0)
error = -error;
if (error != 0)
errno = error;
- vsnprintf(buffer, sizeof(buffer), format, ap);
+ /* Prepend the object name before the message */
+ if (object) {
+ size_t n;
+
+ n = strlen(object);
+ l = n + 2 + LINE_MAX;
+
+ buffer = newa(char, l);
+ b = stpcpy(stpcpy(buffer, object), ": ");
+ } else {
+ l = LINE_MAX;
+ b = buffer = newa(char, l);
+ }
+
+ vsnprintf(b, l, format, ap);
return log_dispatch(level, error, file, line, func, object_field, object, buffer);
}
if ((level & LOG_FACMASK) == 0)
level = log_facility | LOG_PRI(level);
+/// elogind does not support logging to systemd-journald
+#if 0
if ((log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
log_target == LOG_TARGET_JOURNAL) &&
if (!fallback)
return -error;
}
+#endif // 0
/* Fallback if journal logging is not available or didn't work. */
[LOG_TARGET_CONSOLE] = "console",
[LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
[LOG_TARGET_KMSG] = "kmsg",
+/// elogind does not support logging to systemd-journald
+#if 0
[LOG_TARGET_JOURNAL] = "journal",
[LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
+#endif // 0
[LOG_TARGET_SYSLOG] = "syslog",
[LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
[LOG_TARGET_AUTO] = "auto",
DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
+/// UNNEEDED by elogind
+#if 0
void log_received_signal(int level, const struct signalfd_siginfo *si) {
if (si->ssi_pid > 0) {
_cleanup_free_ char *p = NULL;
void log_set_upgrade_syslog_to_journal(bool b) {
upgrade_syslog_to_journal = b;
}
+#endif // 0
+
+int log_syntax_internal(
+ const char *unit,
+ int level,
+ const char *config_file,
+ unsigned config_line,
+ int error,
+ const char *file,
+ int line,
+ const char *func,
+ const char *format, ...) {
+
+ PROTECT_ERRNO;
+ char buffer[LINE_MAX];
+ int r;
+ va_list ap;
+
+ if (error < 0)
+ error = -error;
+
+ if (_likely_(LOG_PRI(level) > log_max_level))
+ return -error;
+
+ if (log_target == LOG_TARGET_NULL)
+ return -error;
+
+ if (error != 0)
+ errno = error;
+
+ va_start(ap, format);
+ vsnprintf(buffer, sizeof(buffer), format, ap);
+ va_end(ap);
+
+ if (unit)
+ r = log_struct_internal(
+ level, error,
+ file, line, func,
+ getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s", unit,
+ LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION),
+ "CONFIG_FILE=%s", config_file,
+ "CONFIG_LINE=%u", config_line,
+ LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer),
+ NULL);
+ else
+ r = log_struct_internal(
+ level, error,
+ file, line, func,
+ LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION),
+ "CONFIG_FILE=%s", config_file,
+ "CONFIG_LINE=%u", config_line,
+ LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer),
+ NULL);
+
+ return r;
+}
#include <sys/signalfd.h>
#include <errno.h>
-#include "macro.h"
#include "sd-id128.h"
+#include "macro.h"
typedef enum LogTarget{
LOG_TARGET_CONSOLE,
LOG_TARGET_CONSOLE_PREFIXED,
LOG_TARGET_KMSG,
+/// elogind does not support logging to systemd-journald
+#if 0
LOG_TARGET_JOURNAL,
LOG_TARGET_JOURNAL_OR_KMSG,
+#endif // 0
LOG_TARGET_SYSLOG,
LOG_TARGET_SYSLOG_OR_KMSG,
LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
int log_open(void);
void log_close(void);
-void log_forget_fds(void);
+// UNNEEDED void log_forget_fds(void);
void log_close_syslog(void);
-void log_close_journal(void);
+// UNNEEDED void log_close_journal(void);
void log_close_kmsg(void);
void log_close_console(void);
# define log_trace(...) do {} while(0)
#endif
+#ifdef ENABLE_DEBUG_ELOGIND
+# define log_debug_elogind(...) log_debug(__VA_ARGS__);usleep(25*USEC_PER_MSEC)
+#else
+# define log_debug_elogind(...) do {} while(0)
+#endif // ENABLE_DEBUG_ELOGIND
+
/* Structured logging */
#define log_struct(level, ...) log_struct_internal(level, 0, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define log_struct_errno(level, error, ...) log_struct_internal(level, error, __FILE__, __LINE__, __func__, __VA_ARGS__)
/* Helpers to prepare various fields for structured logging */
#define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
#define LOG_MESSAGE_ID(x) "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(x)
-#define LOG_ERRNO(error) "ERRNO=%i", abs(error)
-void log_received_signal(int level, const struct signalfd_siginfo *si);
+// UNNEEDED void log_received_signal(int level, const struct signalfd_siginfo *si);
+
+// UNNEEDED void log_set_upgrade_syslog_to_journal(bool b);
+
+int log_syntax_internal(
+ const char *unit,
+ int level,
+ const char *config_file,
+ unsigned config_line,
+ int error,
+ const char *file,
+ int line,
+ const char *func,
+ const char *format, ...) _printf_(9, 10);
-void log_set_upgrade_syslog_to_journal(bool b);
+#define log_syntax(unit, level, config_file, config_line, error, ...) \
+ ({ \
+ int _level = (level), _e = (error); \
+ (log_get_max_level() >= LOG_PRI(_level)) \
+ ? log_syntax_internal(unit, _level, config_file, config_line, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \
+ : -abs(_e); \
+ })
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include "login-shared.h"
+#include "login-util.h"
#include "def.h"
bool session_id_valid(const char *id) {
- assert(id);
- return id[0] && id[strspn(id, LETTERS DIGITS)] == '\0';
+ if (isempty(id))
+ return false;
+
+ return id[strspn(id, LETTERS DIGITS)] == '\0';
}
#include <sys/types.h>
#include <sys/uio.h>
#include <inttypes.h>
+#include <stdbool.h>
#define _printf_(a,b) __attribute__ ((format (printf, a, b)))
#define _alloc_(...) __attribute__ ((alloc_size(__VA_ARGS__)))
REENABLE_WARNING
#endif
+#define assert_log(expr) ((_likely_(expr)) \
+ ? (true) \
+ : (log_assert_failed_return(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__), false))
+
#define assert_return(expr, r) \
do { \
- if (_unlikely_(!(expr))) { \
- log_assert_failed_return(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ if (!assert_log(expr)) \
+ return (r); \
+ } while (false)
+
+#define assert_return_errno(expr, r, err) \
+ do { \
+ if (!assert_log(expr)) { \
+ errno = err; \
return (r); \
} \
} while (false)
#define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
#define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
+#define PTR_TO_PID(p) ((pid_t) ((uintptr_t) p))
+#define PID_TO_PTR(p) ((void*) ((uintptr_t) p))
+
#define memzero(x,l) (memset((x), 0, (l)))
#define zero(x) (memzero(&(x), sizeof(x)))
#define IN_SET(x, y, ...) \
({ \
- const typeof(y) _y = (y); \
- const typeof(_y) _x = (x); \
+ static const typeof(y) _array[] = { (y), __VA_ARGS__ }; \
+ const typeof(y) _x = (x); \
unsigned _i; \
bool _found = false; \
- for (_i = 0; _i < 1 + sizeof((const typeof(_x)[]) { __VA_ARGS__ })/sizeof(const typeof(_x)); _i++) \
- if (((const typeof(_x)[]) { _y, __VA_ARGS__ })[_i] == _x) { \
+ for (_i = 0; _i < ELEMENTSOF(_array); _i++) \
+ if (_array[_i] == _x) { \
_found = true; \
break; \
} \
#define GID_INVALID ((gid_t) -1)
#define MODE_INVALID ((mode_t) -1)
+static inline bool UID_IS_INVALID(uid_t uid) {
+ /* We consider both the old 16bit -1 user and the newer 32bit
+ * -1 user invalid, since they are or used to be incompatible
+ * with syscalls such as setresuid() or chown(). */
+
+ return uid == (uid_t) ((uint32_t) -1) || uid == (uid_t) ((uint16_t) -1);
+}
+
+static inline bool GID_IS_INVALID(gid_t gid) {
+ return gid == (gid_t) ((uint32_t) -1) || gid == (gid_t) ((uint16_t) -1);
+}
+
#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
static inline void func##p(type *p) { \
if (*p) \
} \
struct __useless_struct_to_allow_trailing_semicolon__
+#define CMSG_FOREACH(cmsg, mh) \
+ for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
+
#include "log.h"
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int memfd_new_and_map(const char *name, size_t sz, void **p) {
_cleanup_close_ int fd = -1;
int r;
return r;
}
+#endif // 0
int memfd_new(const char *name);
-int memfd_new_and_map(const char *name, size_t sz, void **p);
+// UNNEEDED int memfd_new_and_map(const char *name, size_t sz, void **p);
int memfd_map(int fd, uint64_t offset, size_t size, void **p);
mp->freelist = p;
}
+/// UNNEEDED by elogind
+#if 0
#ifdef VALGRIND
void mempool_drop(struct mempool *mp) {
}
#endif
+#endif // 0
#ifdef VALGRIND
-void mempool_drop(struct mempool *mp);
+// UNNEEDED void mempool_drop(struct mempool *mp);
#endif
#include <linux/capability.h>
#include <linux/neighbour.h>
+#include "musl_missing.h"
+
#ifdef ARCH_MIPS
#include <asm/sgidefs.h>
#endif
__u64 qgroups[0];
};
+struct btrfs_ioctl_qgroup_limit_args {
+ __u64 qgroupid;
+ struct btrfs_qgroup_limit lim;
+};
+
struct btrfs_ioctl_vol_args_v2 {
__s64 fd;
__u64 transid;
__u64 src_offset, src_length;
__u64 dest_offset;
};
+
+#define BTRFS_QUOTA_CTL_ENABLE 1
+#define BTRFS_QUOTA_CTL_DISABLE 2
+#define BTRFS_QUOTA_CTL_RESCAN__NOTUSED 3
+struct btrfs_ioctl_quota_ctl_args {
+ __u64 cmd;
+ __u64 status;
+};
#endif
#ifndef BTRFS_IOC_DEFRAG
struct btrfs_ioctl_vol_args)
#endif
+#ifndef BTRFS_IOC_RESIZE
+#define BTRFS_IOC_RESIZE _IOW(BTRFS_IOCTL_MAGIC, 3, \
+ struct btrfs_ioctl_vol_args)
+#endif
+
#ifndef BTRFS_IOC_CLONE
#define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
#endif
struct btrfs_ioctl_vol_args)
#endif
+#ifndef BTRFS_IOC_QUOTA_CTL
+#define BTRFS_IOC_QUOTA_CTL _IOWR(BTRFS_IOCTL_MAGIC, 40, \
+ struct btrfs_ioctl_quota_ctl_args)
+#endif
+
+#ifndef BTRFS_IOC_QGROUP_LIMIT
+#define BTRFS_IOC_QGROUP_LIMIT _IOR(BTRFS_IOCTL_MAGIC, 43, \
+ struct btrfs_ioctl_qgroup_limit_args)
+#endif
+
#ifndef BTRFS_FIRST_FREE_OBJECTID
#define BTRFS_FIRST_FREE_OBJECTID 256
#endif
+#ifndef BTRFS_LAST_FREE_OBJECTID
+#define BTRFS_LAST_FREE_OBJECTID -256ULL
+#endif
+
#ifndef BTRFS_ROOT_TREE_OBJECTID
#define BTRFS_ROOT_TREE_OBJECTID 1
#endif
#define BTRFS_QGROUP_LIMIT_KEY 244
#endif
+#ifndef BTRFS_ROOT_BACKREF_KEY
+#define BTRFS_ROOT_BACKREF_KEY 144
+#endif
+
#ifndef BTRFS_SUPER_MAGIC
#define BTRFS_SUPER_MAGIC 0x9123683E
#endif
+#ifndef CGROUP_SUPER_MAGIC
+#define CGROUP_SUPER_MAGIC 0x27e0eb
+#endif
+
+#ifndef TMPFS_MAGIC
+#define TMPFS_MAGIC 0x01021994
+#endif
+
#ifndef MS_MOVE
#define MS_MOVE 8192
#endif
#define IFLA_VLAN_MAX (__IFLA_VLAN_MAX - 1)
#endif
-#if !HAVE_DECL_IFLA_VXLAN_LOCAL6
+#if !HAVE_DECL_IFLA_VXLAN_REMCSUM_NOPARTIAL
#define IFLA_VXLAN_UNSPEC 0
#define IFLA_VXLAN_ID 1
#define IFLA_VXLAN_GROUP 2
#define IFLA_VXLAN_PORT 15
#define IFLA_VXLAN_GROUP6 16
#define IFLA_VXLAN_LOCAL6 17
-#define __IFLA_VXLAN_MAX 18
+#define IFLA_VXLAN_UDP_CSUM 18
+#define IFLA_VXLAN_UDP_ZERO_CSUM6_TX 19
+#define IFLA_VXLAN_UDP_ZERO_CSUM6_RX 20
+#define IFLA_VXLAN_REMCSUM_TX 21
+#define IFLA_VXLAN_REMCSUM_RX 22
+#define IFLA_VXLAN_GBP 23
+#define IFLA_VXLAN_REMCSUM_NOPARTIAL 24
+#define __IFLA_VXLAN_MAX 25
#define IFLA_VXLAN_MAX (__IFLA_VXLAN_MAX - 1)
#endif
-#if !HAVE_DECL_IFLA_IPTUN_6RD_RELAY_PREFIXLEN
+#if !HAVE_DECL_IFLA_IPTUN_ENCAP_DPORT
#define IFLA_IPTUN_UNSPEC 0
#define IFLA_IPTUN_LINK 1
#define IFLA_IPTUN_LOCAL 2
#define IFLA_IPTUN_6RD_RELAY_PREFIX 12
#define IFLA_IPTUN_6RD_PREFIXLEN 13
#define IFLA_IPTUN_6RD_RELAY_PREFIXLEN 14
-#define __IFLA_IPTUN_MAX 15
+#define IFLA_IPTUN_ENCAP_TYPE 15
+#define IFLA_IPTUN_ENCAP_FLAGS 16
+#define IFLA_IPTUN_ENCAP_SPORT 17
+#define IFLA_IPTUN_ENCAP_DPORT 18
+
+#define __IFLA_IPTUN_MAX 19
#define IFLA_IPTUN_MAX (__IFLA_IPTUN_MAX - 1)
#endif
+#if !HAVE_DECL_IFLA_GRE_ENCAP_DPORT
+#define IFLA_GRE_UNSPEC 0
+#define IFLA_GRE_LINK 1
+#define IFLA_GRE_IFLAGS 2
+#define IFLA_GRE_OFLAGS 3
+#define IFLA_GRE_IKEY 4
+#define IFLA_GRE_OKEY 5
+#define IFLA_GRE_LOCAL 6
+#define IFLA_GRE_REMOTE 7
+#define IFLA_GRE_TTL 8
+#define IFLA_GRE_TOS 9
+#define IFLA_GRE_PMTUDISC 10
+#define IFLA_GRE_ENCAP_LIMIT 11
+#define IFLA_GRE_FLOWINFO 12
+#define IFLA_GRE_FLAGS 13
+#define IFLA_GRE_ENCAP_TYPE 14
+#define IFLA_GRE_ENCAP_FLAGS 15
+#define IFLA_GRE_ENCAP_SPORT 16
+#define IFLA_GRE_ENCAP_DPORT 17
+
+#define __IFLA_GRE_MAX 18
+
+#define IFLA_GRE_MAX (__IFLA_GRE_MAX - 1)
+#endif
+
#if !HAVE_DECL_IFLA_BRIDGE_VLAN_INFO
#define IFLA_BRIDGE_FLAGS 0
#define IFLA_BRIDGE_MODE 1
#define IFLA_BRIDGE_MAX (__IFLA_BRIDGE_MAX - 1)
#endif
-#if !HAVE_DECL_IFLA_BRPORT_UNICAST_FLOOD
+#if !HAVE_DECL_IFLA_BRPORT_LEARNING_SYNC
#define IFLA_BRPORT_UNSPEC 0
#define IFLA_BRPORT_STATE 1
#define IFLA_BRPORT_PRIORITY 2
#define IFLA_BRPORT_FAST_LEAVE 7
#define IFLA_BRPORT_LEARNING 8
#define IFLA_BRPORT_UNICAST_FLOOD 9
-#define __IFLA_BRPORT_MAX 10
+#define IFLA_BRPORT_PROXYARP 10
+#define IFLA_BRPORT_LEARNING_SYNC 11
+#define __IFLA_BRPORT_MAX 12
#define IFLA_BRPORT_MAX (__IFLA_BRPORT_MAX - 1)
#endif
#define LOOPBACK_IFINDEX 1
#endif
+#if !HAVE_DECL_IFA_FLAGS
+#define IFA_FLAGS 8
+#endif
+
+#ifndef IFA_F_NOPREFIXROUTE
+#define IFA_F_NOPREFIXROUTE 0x200
+#endif
+
#ifndef MAX_AUDIT_MESSAGE_LENGTH
#define MAX_AUDIT_MESSAGE_LENGTH 8970
#endif
}
static inline pid_t raw_getpid(void) {
+#if defined(__alpha__)
+ return (pid_t) syscall(__NR_getxpid);
+#else
return (pid_t) syscall(__NR_getpid);
+#endif
}
#if !HAVE_DECL_RENAMEAT2
#ifndef RENAME_NOREPLACE
#define RENAME_NOREPLACE (1 << 0)
#endif
+
+#if !HAVE_DECL_KCMP
+static inline int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
+ return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
+}
+#endif
+
+#ifndef KCMP_FILE
+#define KCMP_FILE 0
+#endif
+
+#ifndef INPUT_PROP_POINTING_STICK
+#define INPUT_PROP_POINTING_STICK 0x05
+#endif
+
+#ifndef INPUT_PROP_ACCELEROMETER
+#define INPUT_PROP_ACCELEROMETER 0x06
+#endif
return mkdir_safe_internal(path, mode, uid, gid, mkdir_label);
}
+/// UNNEEDED by elogind
+#if 0
int mkdir_parents_label(const char *path, mode_t mode) {
return mkdir_parents_internal(NULL, path, mode, mkdir_label);
}
+#endif // 0
int mkdir_p_label(const char *path, mode_t mode) {
return mkdir_p_internal(NULL, path, mode, mkdir_label);
/* mandatory access control(MAC) versions */
int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid);
-int mkdir_parents_label(const char *path, mode_t mode);
+// UNNEEDED int mkdir_parents_label(const char *path, mode_t mode);
int mkdir_p_label(const char *path, mode_t mode);
/* internally used */
--- /dev/null
+#include <string.h>
+#include "util.h"
+
+#ifndef __GLIBC__
+char *program_invocation_name = NULL;
+char *program_invocation_short_name = NULL;
+#endif // __GLIBC__
+
+#include "musl_missing.h"
+
+static void elogind_free_program_name(void) {
+ if (program_invocation_name)
+ program_invocation_name = mfree(program_invocation_name);
+ if (program_invocation_short_name)
+ program_invocation_short_name = mfree(program_invocation_short_name);
+}
+
+void elogind_set_program_name(const char* pcall) {
+ assert(pcall && pcall[0]);
+
+ if ( ( program_invocation_name
+ && strcmp(program_invocation_name, pcall))
+ || ( program_invocation_short_name
+ && strcmp(program_invocation_short_name, basename(pcall)) ) )
+ elogind_free_program_name();
+
+ if (NULL == program_invocation_name)
+ program_invocation_name = strdup(pcall);
+ if (NULL == program_invocation_short_name)
+ program_invocation_short_name = strdup(basename(pcall));
+
+#ifndef __GLIBC__
+ atexit(elogind_free_program_name);
+#endif // __GLIBC__
+}
+
--- /dev/null
+#pragma once
+#ifndef ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
+#define ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
+
+
+/****************************************************************
+ * musl_missing.h - work around glibc extensions for musl libc.
+ *
+ * Implements glibc functions missing in musl libc as macros.
+ * Is to be included where these functions are used.
+ * Also defines some glibc only constants as either 0 or
+ * as found in the corresponding glibc header file.
+ *
+ * Juergen Buchmueller <pullmoll@t-online.de> for Void Linux
+ * Public Domain; no warranties whatsoever. Thank you Mr. P.
+ *
+ ****************************************************************/
+
+
+void elogind_set_program_name(const char* pcall);
+
+#if !defined(__GLIBC__)
+#include "config.h"
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define strerror_r(e, m, k) (strerror_r(e, m, k) < 0 ? strdup("strerror_r() failed") : m);
+
+#ifndef _ERRNO_H
+extern char *program_invocation_name;
+extern char *program_invocation_short_name;
+#endif // errno.h included beforehand
+
+/*
+ * Possibly TODO according to http://man7.org/linux/man-pages/man3/getenv.3.html
+ * + test if the process's effective user ID does not match its real user ID or
+ * the process's effective group ID does not match its real group ID;
+ * typically this is the result of executing a set-user-ID or set-
+ * group-ID program. Is calling issetugid() sufficient here?
+ * + test if the effective capability bit was set on the executable file
+ * + test if the process has a nonempty permitted capability set
+ */
+#if !defined(HAVE_SECURE_GETENV) && !defined(HAVE___SECURE_GETENV)
+# define secure_getenv(name) \
+ (issetugid() ? NULL : getenv(name))
+# define HAVE_SECURE_GETENV 1
+#endif // HAVE_[__]SECURE_GETENV
+
+/* Poor man's basename */
+#define basename(path) \
+ (strrchr(path, '/') ? strrchr(path, '/')+1 : path)
+
+/* strndupa may already be defined in another compatibility header */
+#if !defined(strndupa)
+#define strndupa(src, n) \
+ (__extension__ ({const char *in = (src); \
+ size_t len = strnlen(in, (n)) + 1; \
+ char *out = (char *) alloca(len); \
+ out[len-1] = '\0'; \
+ (char *) memcpy(out, in, len-1);}) \
+ )
+#endif
+
+/* See http://man7.org/linux/man-pages/man3/canonicalize_file_name.3.html */
+#define canonicalize_file_name(path) \
+ realpath(path, NULL)
+
+/* GLOB_BRACE is another glibc extension - ignore it for musl libc */
+#define GLOB_BRACE 0
+
+/* getnameinfo(3) glibc extensions are undefined in musl libc */
+#define NI_IDN 0
+#define NI_IDN_USE_STD3_ASCII_RULES 0
+
+/* Taken from glibc's net/if_arp.h */
+#if !defined(ARPHRD_IEEE802154_PHY)
+#define ARPHRD_IEEE802154_PHY 805 /* IEEE 802.15.4 PHY header. */
+#endif
+
+/* Shorthand for type of comparison functions. */
+#ifndef __COMPAR_FN_T
+# define __COMPAR_FN_T
+typedef int (*__compar_fn_t) (const void *, const void *);
+typedef __compar_fn_t comparison_fn_t;
+#endif
+
+
+#endif // !defined(__GLIBC__)
+
+#endif // ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
+
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Emil Renner Berthing <systemd@esmil.dk>
+
+ With parts from the musl C library
+ Copyright 2005-2014 Rich Felker, et al.
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stddef.h>
+#include <string.h>
+
+#include "parse-printf-format.h"
+
+#ifndef HAVE_PRINTF_H
+
+static const char *consume_nonarg(const char *fmt)
+{
+ do {
+ if (*fmt == '\0')
+ return fmt;
+ } while (*fmt++ != '%');
+ return fmt;
+}
+
+static const char *consume_num(const char *fmt)
+{
+ for (;*fmt >= '0' && *fmt <= '9'; fmt++)
+ /* do nothing */;
+ return fmt;
+}
+
+static const char *consume_argn(const char *fmt, size_t *arg)
+{
+ const char *p = fmt;
+ size_t val = 0;
+
+ if (*p < '1' || *p > '9')
+ return fmt;
+ do {
+ val = 10*val + (*p++ - '0');
+ } while (*p >= '0' && *p <= '9');
+
+ if (*p != '$')
+ return fmt;
+ *arg = val;
+ return p+1;
+}
+
+static const char *consume_flags(const char *fmt)
+{
+ while (1) {
+ switch (*fmt) {
+ case '#':
+ case '0':
+ case '-':
+ case ' ':
+ case '+':
+ case '\'':
+ case 'I':
+ fmt++;
+ continue;
+ }
+ return fmt;
+ }
+}
+
+enum state {
+ BARE,
+ LPRE,
+ LLPRE,
+ HPRE,
+ HHPRE,
+ BIGLPRE,
+ ZTPRE,
+ JPRE,
+ STOP
+};
+
+enum type {
+ NONE,
+ PTR,
+ INT,
+ UINT,
+ ULLONG,
+ LONG,
+ ULONG,
+ SHORT,
+ USHORT,
+ CHAR,
+ UCHAR,
+ LLONG,
+ SIZET,
+ IMAX,
+ UMAX,
+ PDIFF,
+ UIPTR,
+ DBL,
+ LDBL,
+ MAXTYPE
+};
+
+static const short pa_types[MAXTYPE] = {
+ [NONE] = PA_INT,
+ [PTR] = PA_POINTER,
+ [INT] = PA_INT,
+ [UINT] = PA_INT,
+ [ULLONG] = PA_INT | PA_FLAG_LONG_LONG,
+ [LONG] = PA_INT | PA_FLAG_LONG,
+ [ULONG] = PA_INT | PA_FLAG_LONG,
+ [SHORT] = PA_INT | PA_FLAG_SHORT,
+ [USHORT] = PA_INT | PA_FLAG_SHORT,
+ [CHAR] = PA_CHAR,
+ [UCHAR] = PA_CHAR,
+ [LLONG] = PA_INT | PA_FLAG_LONG_LONG,
+ [SIZET] = PA_INT | PA_FLAG_LONG,
+ [IMAX] = PA_INT | PA_FLAG_LONG_LONG,
+ [UMAX] = PA_INT | PA_FLAG_LONG_LONG,
+ [PDIFF] = PA_INT | PA_FLAG_LONG_LONG,
+ [UIPTR] = PA_INT | PA_FLAG_LONG,
+ [DBL] = PA_DOUBLE,
+ [LDBL] = PA_DOUBLE | PA_FLAG_LONG_DOUBLE
+};
+
+#define S(x) [(x)-'A']
+#define E(x) (STOP + (x))
+
+static const unsigned char states[]['z'-'A'+1] = {
+ { /* 0: bare types */
+ S('d') = E(INT), S('i') = E(INT),
+ S('o') = E(UINT),S('u') = E(UINT),S('x') = E(UINT), S('X') = E(UINT),
+ S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL),
+ S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL),
+ S('c') = E(CHAR),S('C') = E(INT),
+ S('s') = E(PTR), S('S') = E(PTR), S('p') = E(UIPTR),S('n') = E(PTR),
+ S('m') = E(NONE),
+ S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
+ S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE
+ }, { /* 1: l-prefixed */
+ S('d') = E(LONG), S('i') = E(LONG),
+ S('o') = E(ULONG),S('u') = E(ULONG),S('x') = E(ULONG),S('X') = E(ULONG),
+ S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL),
+ S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL),
+ S('c') = E(INT), S('s') = E(PTR), S('n') = E(PTR),
+ S('l') = LLPRE
+ }, { /* 2: ll-prefixed */
+ S('d') = E(LLONG), S('i') = E(LLONG),
+ S('o') = E(ULLONG),S('u') = E(ULLONG),
+ S('x') = E(ULLONG),S('X') = E(ULLONG),
+ S('n') = E(PTR)
+ }, { /* 3: h-prefixed */
+ S('d') = E(SHORT), S('i') = E(SHORT),
+ S('o') = E(USHORT),S('u') = E(USHORT),
+ S('x') = E(USHORT),S('X') = E(USHORT),
+ S('n') = E(PTR),
+ S('h') = HHPRE
+ }, { /* 4: hh-prefixed */
+ S('d') = E(CHAR), S('i') = E(CHAR),
+ S('o') = E(UCHAR),S('u') = E(UCHAR),
+ S('x') = E(UCHAR),S('X') = E(UCHAR),
+ S('n') = E(PTR)
+ }, { /* 5: L-prefixed */
+ S('e') = E(LDBL),S('f') = E(LDBL),S('g') = E(LDBL), S('a') = E(LDBL),
+ S('E') = E(LDBL),S('F') = E(LDBL),S('G') = E(LDBL), S('A') = E(LDBL),
+ S('n') = E(PTR)
+ }, { /* 6: z- or t-prefixed (assumed to be same size) */
+ S('d') = E(PDIFF),S('i') = E(PDIFF),
+ S('o') = E(SIZET),S('u') = E(SIZET),
+ S('x') = E(SIZET),S('X') = E(SIZET),
+ S('n') = E(PTR)
+ }, { /* 7: j-prefixed */
+ S('d') = E(IMAX), S('i') = E(IMAX),
+ S('o') = E(UMAX), S('u') = E(UMAX),
+ S('x') = E(UMAX), S('X') = E(UMAX),
+ S('n') = E(PTR)
+ }
+};
+
+size_t parse_printf_format(const char *fmt, size_t n, int *types)
+{
+ size_t i = 0;
+ size_t last = 0;
+
+ memset(types, 0, n);
+
+ while (1) {
+ size_t arg;
+ unsigned int state;
+
+ fmt = consume_nonarg(fmt);
+ if (*fmt == '\0')
+ break;
+ if (*fmt == '%') {
+ fmt++;
+ continue;
+ }
+ arg = 0;
+ fmt = consume_argn(fmt, &arg);
+ /* flags */
+ fmt = consume_flags(fmt);
+ /* width */
+ if (*fmt == '*') {
+ size_t warg = 0;
+ fmt = consume_argn(fmt+1, &warg);
+ if (warg == 0)
+ warg = ++i;
+ if (warg > last)
+ last = warg;
+ if (warg <= n && types[warg-1] == NONE)
+ types[warg-1] = INT;
+ } else
+ fmt = consume_num(fmt);
+ /* precision */
+ if (*fmt == '.') {
+ fmt++;
+ if (*fmt == '*') {
+ size_t parg = 0;
+ fmt = consume_argn(fmt+1, &parg);
+ if (parg == 0)
+ parg = ++i;
+ if (parg > last)
+ last = parg;
+ if (parg <= n && types[parg-1] == NONE)
+ types[parg-1] = INT;
+ } else {
+ if (*fmt == '-')
+ fmt++;
+ fmt = consume_num(fmt);
+ }
+ }
+ /* length modifier and conversion specifier */
+ state = BARE;
+ do {
+ unsigned char c = *fmt++;
+
+ if (c < 'A' || c > 'z')
+ continue;
+ state = states[state]S(c);
+ if (state == 0)
+ continue;
+ } while (state < STOP);
+
+ if (state == E(NONE))
+ continue;
+
+ if (arg == 0)
+ arg = ++i;
+ if (arg > last)
+ last = arg;
+ if (arg <= n)
+ types[arg-1] = state - STOP;
+ }
+
+ if (last > n)
+ last = n;
+ for (i = 0; i < last; i++)
+ types[i] = pa_types[types[i]];
+
+ return last;
+}
+
+#endif // HAVE_PRINTF_H
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Emil Renner Berthing <systemd@esmil.dk>
+
+ With parts from the GNU C Library
+ Copyright 1991-2014 Free Software Foundation, Inc.
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#pragma once
+
+#include "config.h"
+
+#ifdef HAVE_PRINTF_H
+#include <printf.h>
+#else
+
+#include <stddef.h>
+
+enum { /* C type: */
+ PA_INT, /* int */
+ PA_CHAR, /* int, cast to char */
+ PA_WCHAR, /* wide char */
+ PA_STRING, /* const char *, a '\0'-terminated string */
+ PA_WSTRING, /* const wchar_t *, wide character string */
+ PA_POINTER, /* void * */
+ PA_FLOAT, /* float */
+ PA_DOUBLE, /* double */
+ PA_LAST
+};
+
+/* Flag bits that can be set in a type returned by `parse_printf_format'. */
+#define PA_FLAG_MASK 0xff00
+#define PA_FLAG_LONG_LONG (1 << 8)
+#define PA_FLAG_LONG_DOUBLE PA_FLAG_LONG_LONG
+#define PA_FLAG_LONG (1 << 9)
+#define PA_FLAG_SHORT (1 << 10)
+#define PA_FLAG_PTR (1 << 11)
+
+size_t parse_printf_format(const char *fmt, size_t n, int *types);
+
+#endif /* HAVE_PRINTF_H */
+
#include "strv.h"
#include "path-util.h"
#include "missing.h"
+#include "fileio.h"
bool path_is_absolute(const char *p) {
return p[0] == '/';
return 0;
}
+/// UNNEEDED by elogind
+#if 0
char **path_split_and_make_absolute(const char *p) {
char **l;
assert(p);
return l;
}
+#endif // 0
char *path_make_absolute(const char *p, const char *prefix) {
assert(p);
return strjoin(cwd, "/", p, NULL);
}
+/// UNNEEDED by elogind
+#if 0
int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
char *r, *p;
unsigned n_parents;
*_r = r;
return 0;
}
+#endif // 0
char **path_strv_make_absolute_cwd(char **l) {
char **s;
return path_compare(a, b) == 0;
}
+/// UNNEEDED by elogind
+#if 0
bool path_equal_or_files_same(const char *a, const char *b) {
return path_equal(a, b) || files_same(a, b) > 0;
}
rest && rest[0] == '/' ? rest+1 : rest,
NULL);
}
+#endif // 0
-int path_is_mount_point(const char *t, bool allow_symlink) {
-
- union file_handle_union h = FILE_HANDLE_INIT;
- int mount_id = -1, mount_id_parent = -1;
- struct stat a, b;
+static int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *mnt_id) {
+ char path[strlen("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
+ _cleanup_free_ char *fdinfo = NULL;
+ _cleanup_close_ int subfd = -1;
+ char *p;
int r;
- _cleanup_close_ int fd = -1;
- bool nosupp = false;
- /* We are not actually interested in the file handles, but
- * name_to_handle_at() also passes us the mount ID, hence use
- * it but throw the handle away */
-
- if (path_equal(t, "/"))
- return 1;
+ if ((flags & AT_EMPTY_PATH) && isempty(filename))
+ xsprintf(path, "/proc/self/fdinfo/%i", fd);
+ else {
+ subfd = openat(fd, filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH);
+ if (subfd < 0)
+ return -errno;
- fd = openat(AT_FDCWD, t, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|(allow_symlink ? 0 : O_PATH));
- if (fd < 0) {
- if (errno == ENOENT)
- return 0;
+ xsprintf(path, "/proc/self/fdinfo/%i", subfd);
+ }
+ r = read_full_file(path, &fdinfo, NULL);
+ if (r == -ENOENT) /* The fdinfo directory is a relatively new addition */
+ return -EOPNOTSUPP;
+ if (r < 0)
return -errno;
+
+ p = startswith(fdinfo, "mnt_id:");
+ if (!p) {
+ p = strstr(fdinfo, "\nmnt_id:");
+ if (!p) /* The mnt_id field is a relatively new addition */
+ return -EOPNOTSUPP;
+
+ p += 8;
}
- r = name_to_handle_at(fd, "", &h.handle, &mount_id, AT_EMPTY_PATH);
+ p += strspn(p, WHITESPACE);
+ p[strcspn(p, WHITESPACE)] = 0;
+
+ return safe_atoi(p, mnt_id);
+}
+
+int fd_is_mount_point(int fd, const char *filename, int flags) {
+ union file_handle_union h = FILE_HANDLE_INIT, h_parent = FILE_HANDLE_INIT;
+ int mount_id = -1, mount_id_parent = -1;
+ bool nosupp = false, check_st_dev = true;
+ struct stat a, b;
+ int r;
+
+ assert(fd >= 0);
+ assert(filename);
+
+ /* First we will try the name_to_handle_at() syscall, which
+ * tells us the mount id and an opaque file "handle". It is
+ * not supported everywhere though (kernel compile-time
+ * option, not all file systems are hooked up). If it works
+ * the mount id is usually good enough to tell us whether
+ * something is a mount point.
+ *
+ * If that didn't work we will try to read the mount id from
+ * /proc/self/fdinfo/<fd>. This is almost as good as
+ * name_to_handle_at(), however, does not return the
+ * opaque file handle. The opaque file handle is pretty useful
+ * to detect the root directory, which we should always
+ * consider a mount point. Hence we use this only as
+ * fallback. Exporting the mnt_id in fdinfo is a pretty recent
+ * kernel addition.
+ *
+ * As last fallback we do traditional fstat() based st_dev
+ * comparisons. This is how things were traditionally done,
+ * but unionfs breaks breaks this since it exposes file
+ * systems with a variety of st_dev reported. Also, btrfs
+ * subvolumes have different st_dev, even though they aren't
+ * real mounts of their own. */
+
+ r = name_to_handle_at(fd, filename, &h.handle, &mount_id, flags);
if (r < 0) {
if (errno == ENOSYS)
/* This kernel does not support name_to_handle_at()
- * fall back to the traditional stat() logic. */
- goto fallback;
+ * fall back to simpler logic. */
+ goto fallback_fdinfo;
else if (errno == EOPNOTSUPP)
/* This kernel or file system does not support
- * name_to_handle_at(), hence fallback to the
+ * name_to_handle_at(), hence let's see if the
+ * upper fs supports it (in which case it is a
+ * mount point), otherwise fallback to the
* traditional stat() logic */
nosupp = true;
- else if (errno == ENOENT)
- return 0;
else
return -errno;
}
-
- h.handle.handle_bytes = MAX_HANDLE_SZ;
- r = name_to_handle_at(fd, "..", &h.handle, &mount_id_parent, 0);
- if (r < 0)
- if (errno == EOPNOTSUPP)
+ r = name_to_handle_at(fd, "", &h_parent.handle, &mount_id_parent, AT_EMPTY_PATH);
+ if (r < 0) {
+ if (errno == EOPNOTSUPP) {
if (nosupp)
/* Neither parent nor child do name_to_handle_at()?
We have no choice but to fall back. */
- goto fallback;
+ goto fallback_fdinfo;
else
- /* The parent can't do name_to_handle_at() but
- * the directory we are interested in can?
- * Or the other way around?
+ /* The parent can't do name_to_handle_at() but the
+ * directory we are interested in can?
* If so, it must be a mount point. */
return 1;
- else
+ } else
return -errno;
- else
- return mount_id != mount_id_parent;
+ }
-fallback:
- r = fstatat(fd, "", &a, AT_EMPTY_PATH);
+ /* The parent can do name_to_handle_at() but the
+ * directory we are interested in can't? If so, it
+ * must be a mount point. */
+ if (nosupp)
+ return 1;
- if (r < 0) {
- if (errno == ENOENT)
- return 0;
+ /* If the file handle for the directory we are
+ * interested in and its parent are identical, we
+ * assume this is the root directory, which is a mount
+ * point. */
+
+ if (h.handle.handle_bytes == h_parent.handle.handle_bytes &&
+ h.handle.handle_type == h_parent.handle.handle_type &&
+ memcmp(h.handle.f_handle, h_parent.handle.f_handle, h.handle.handle_bytes) == 0)
+ return 1;
+
+ return mount_id != mount_id_parent;
+
+fallback_fdinfo:
+ r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id);
+ if (r == -EOPNOTSUPP)
+ goto fallback_fstat;
+ if (r < 0)
+ return r;
+
+ r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent);
+ if (r < 0)
+ return r;
+
+ if (mount_id != mount_id_parent)
+ return 1;
+ /* Hmm, so, the mount ids are the same. This leaves one
+ * special case though for the root file system. For that,
+ * let's see if the parent directory has the same inode as we
+ * are interested in. Hence, let's also do fstat() checks now,
+ * too, but avoid the st_dev comparisons, since they aren't
+ * that useful on unionfs mounts. */
+ check_st_dev = false;
+
+fallback_fstat:
+ /* yay for fstatat() taking a different set of flags than the other
+ * _at() above */
+ if (flags & AT_SYMLINK_FOLLOW)
+ flags &= ~AT_SYMLINK_FOLLOW;
+ else
+ flags |= AT_SYMLINK_NOFOLLOW;
+ if (fstatat(fd, filename, &a, flags) < 0)
return -errno;
- }
+ if (fstatat(fd, "", &b, AT_EMPTY_PATH) < 0)
+ return -errno;
+
+ /* A directory with same device and inode as its parent? Must
+ * be the root directory */
+ if (a.st_dev == b.st_dev &&
+ a.st_ino == b.st_ino)
+ return 1;
+
+ return check_st_dev && (a.st_dev != b.st_dev);
+}
+
+/* flags can be AT_SYMLINK_FOLLOW or 0 */
+int path_is_mount_point(const char *t, int flags) {
+ _cleanup_close_ int fd = -1;
+ _cleanup_free_ char *canonical = NULL, *parent = NULL;
+ int r;
+
+ assert(t);
+
+ if (path_equal(t, "/"))
+ return 1;
+
+ /* we need to resolve symlinks manually, we can't just rely on
+ * fd_is_mount_point() to do that for us; if we have a structure like
+ * /bin -> /usr/bin/ and /usr is a mount point, then the parent that we
+ * look at needs to be /usr, not /. */
+ if (flags & AT_SYMLINK_FOLLOW) {
+ canonical = canonicalize_file_name(t);
+ if (!canonical)
+ return -errno;
- r = fstatat(fd, "..", &b, 0);
+ t = canonical;
+ }
+
+ r = path_get_parent(t, &parent);
if (r < 0)
+ return r;
+
+ fd = openat(AT_FDCWD, parent, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_PATH);
+ if (fd < 0)
return -errno;
- return a.st_dev != b.st_dev;
+ return fd_is_mount_point(fd, basename(t), flags);
}
int path_is_read_only_fs(const char *path) {
return false;
}
+/// UNNEEDED by elogind
+#if 0
int path_is_os_tree(const char *path) {
char *p;
int r;
return 0;
}
+
+char *prefix_root(const char *root, const char *path) {
+ char *n, *p;
+ size_t l;
+
+ /* If root is passed, prefixes path with it. Otherwise returns
+ * it as is. */
+
+ assert(path);
+
+ /* First, drop duplicate prefixing slashes from the path */
+ while (path[0] == '/' && path[1] == '/')
+ path++;
+
+ if (isempty(root) || path_equal(root, "/"))
+ return strdup(path);
+
+ l = strlen(root) + 1 + strlen(path) + 1;
+
+ n = new(char, l);
+ if (!n)
+ return NULL;
+
+ p = stpcpy(n, root);
+
+ while (p > n && p[-1] == '/')
+ p--;
+
+ if (path[0] != '/')
+ *(p++) = '/';
+
+ strcpy(p, path);
+ return n;
+}
+#endif // 0
#endif
bool is_path(const char *p) _pure_;
-char** path_split_and_make_absolute(const char *p);
+// UNNEEDED char** path_split_and_make_absolute(const char *p);
int path_get_parent(const char *path, char **parent);
bool path_is_absolute(const char *p) _pure_;
char* path_make_absolute(const char *p, const char *prefix);
char* path_make_absolute_cwd(const char *p);
-int path_make_relative(const char *from_dir, const char *to_path, char **_r);
+// UNNEEDED int path_make_relative(const char *from_dir, const char *to_path, char **_r);
char* path_kill_slashes(char *path);
char* path_startswith(const char *path, const char *prefix) _pure_;
int path_compare(const char *a, const char *b) _pure_;
bool path_equal(const char *a, const char *b) _pure_;
-bool path_equal_or_files_same(const char *a, const char *b);
-char* path_join(const char *root, const char *path, const char *rest);
+// UNNEEDED bool path_equal_or_files_same(const char *a, const char *b);
+// UNNEEDED char* path_join(const char *root, const char *path, const char *rest);
char** path_strv_make_absolute_cwd(char **l);
char** path_strv_resolve(char **l, const char *prefix);
char** path_strv_resolve_uniq(char **l, const char *prefix);
-int path_is_mount_point(const char *path, bool allow_symlink);
+int fd_is_mount_point(int fd, const char *filename, int flags);
+int path_is_mount_point(const char *path, int flags);
int path_is_read_only_fs(const char *path);
-int path_is_os_tree(const char *path);
+// UNNEEDED int path_is_os_tree(const char *path);
-int find_binary(const char *name, bool local, char **filename);
+// UNNEEDED int find_binary(const char *name, bool local, char **filename);
-bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update);
+// UNNEEDED bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update);
-int fsck_exists(const char *fstype);
+// UNNEEDED int fsck_exists(const char *fstype);
/* Iterates through the path prefixes of the specified path, going up
* the tree, to root. Also returns "" (and not "/"!) for the root
/* Same as PATH_FOREACH_PREFIX but also includes the specified path itself */
#define PATH_FOREACH_PREFIX_MORE(prefix, path) \
for (char *_slash = ({ path_kill_slashes(strcpy(prefix, path)); if (streq(prefix, "/")) prefix[0] = 0; strrchr(prefix, 0); }); _slash && ((*_slash = 0), true); _slash = strrchr((prefix), '/'))
+
+// UNNEEDED char *prefix_root(const char *root, const char *path);
+
+/* Similar to prefix_root(), but returns an alloca() buffer, or
+ * possibly a const pointer into the path parameter */
+#define prefix_roota(root, path) \
+ ({ \
+ const char* _path = (path), *_root = (root), *_ret; \
+ char *_p, *_n; \
+ size_t _l; \
+ while (_path[0] == '/' && _path[1] == '/') \
+ _path ++; \
+ if (isempty(_root) || path_equal(_root, "/")) \
+ _ret = _path; \
+ else { \
+ _l = strlen(_root) + 1 + strlen(_path) + 1; \
+ _n = alloca(_l); \
+ _p = stpcpy(_n, _root); \
+ while (_p > _n && _p[-1] == '/') \
+ _p--; \
+ if (_path[0] != '/') \
+ *(_p++) = '/'; \
+ strcpy(_p, _path); \
+ _ret = _n; \
+ } \
+ _ret; \
+ })
return q;
}
-void prioq_free(Prioq *q) {
+Prioq* prioq_free(Prioq *q) {
if (!q)
- return;
+ return NULL;
free(q->items);
free(q);
+
+ return NULL;
}
int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) {
#define PRIOQ_IDX_NULL ((unsigned) -1)
Prioq *prioq_new(compare_func_t compare);
-void prioq_free(Prioq *q);
+Prioq *prioq_free(Prioq *q);
int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func);
int prioq_put(Prioq *q, void *data, unsigned *idx);
--- /dev/null
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+#include <sys/types.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <ctype.h>
+
+#include "fileio.h"
+#include "util.h"
+#include "log.h"
+#include "signal-util.h"
+#include "process-util.h"
+
+int get_process_state(pid_t pid) {
+ const char *p;
+ char state;
+ int r;
+ _cleanup_free_ char *line = NULL;
+
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "stat");
+
+ r = read_one_line_file(p, &line);
+ if (r == -ENOENT)
+ return -ESRCH;
+ if (r < 0)
+ return r;
+
+ p = strrchr(line, ')');
+ if (!p)
+ return -EIO;
+
+ p++;
+
+ if (sscanf(p, " %c", &state) != 1)
+ return -EIO;
+
+ return (unsigned char) state;
+}
+
+int get_process_comm(pid_t pid, char **name) {
+ const char *p;
+ int r;
+
+ assert(name);
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "comm");
+
+ r = read_one_line_file(p, name);
+ if (r == -ENOENT)
+ return -ESRCH;
+
+ return r;
+}
+
+int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char *r = NULL, *k;
+ const char *p;
+ int c;
+
+ assert(line);
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "cmdline");
+
+ f = fopen(p, "re");
+ if (!f) {
+ if (errno == ENOENT)
+ return -ESRCH;
+ return -errno;
+ }
+
+ if (max_length == 0) {
+ size_t len = 0, allocated = 0;
+
+ while ((c = getc(f)) != EOF) {
+
+ if (!GREEDY_REALLOC(r, allocated, len+2)) {
+ free(r);
+ return -ENOMEM;
+ }
+
+ r[len++] = isprint(c) ? c : ' ';
+ }
+
+ if (len > 0)
+ r[len-1] = 0;
+
+ } else {
+ bool space = false;
+ size_t left;
+
+ r = new(char, max_length);
+ if (!r)
+ return -ENOMEM;
+
+ k = r;
+ left = max_length;
+ while ((c = getc(f)) != EOF) {
+
+ if (isprint(c)) {
+ if (space) {
+ if (left <= 4)
+ break;
+
+ *(k++) = ' ';
+ left--;
+ space = false;
+ }
+
+ if (left <= 4)
+ break;
+
+ *(k++) = (char) c;
+ left--;
+ } else
+ space = true;
+ }
+
+ if (left <= 4) {
+ size_t n = MIN(left-1, 3U);
+ memcpy(k, "...", n);
+ k[n] = 0;
+ } else
+ *k = 0;
+ }
+
+ /* Kernel threads have no argv[] */
+ if (isempty(r)) {
+ _cleanup_free_ char *t = NULL;
+ int h;
+
+ free(r);
+
+ if (!comm_fallback)
+ return -ENOENT;
+
+ h = get_process_comm(pid, &t);
+ if (h < 0)
+ return h;
+
+ r = strjoin("[", t, "]", NULL);
+ if (!r)
+ return -ENOMEM;
+ }
+
+ *line = r;
+ return 0;
+}
+
+int is_kernel_thread(pid_t pid) {
+ const char *p;
+ size_t count;
+ char c;
+ bool eof;
+ FILE *f;
+
+ if (pid == 0 || pid == 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
+ return 0;
+
+ assert(pid > 1);
+
+ p = procfs_file_alloca(pid, "cmdline");
+ f = fopen(p, "re");
+ if (!f) {
+ if (errno == ENOENT)
+ return -ESRCH;
+ return -errno;
+ }
+
+ count = fread(&c, 1, 1, f);
+ eof = feof(f);
+ fclose(f);
+
+ /* Kernel threads have an empty cmdline */
+
+ if (count <= 0)
+ return eof ? 1 : -errno;
+
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int get_process_capeff(pid_t pid, char **capeff) {
+ const char *p;
+ int r;
+
+ assert(capeff);
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "status");
+
+ r = get_status_field(p, "\nCapEff:", capeff);
+ if (r == -ENOENT)
+ return -ESRCH;
+
+ return r;
+}
+#endif // 0
+
+static int get_process_link_contents(const char *proc_file, char **name) {
+ int r;
+
+ assert(proc_file);
+ assert(name);
+
+ r = readlink_malloc(proc_file, name);
+ if (r == -ENOENT)
+ return -ESRCH;
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+int get_process_exe(pid_t pid, char **name) {
+ const char *p;
+ char *d;
+ int r;
+
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "exe");
+ r = get_process_link_contents(p, name);
+ if (r < 0)
+ return r;
+
+ d = endswith(*name, " (deleted)");
+ if (d)
+ *d = '\0';
+
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char line[LINE_MAX];
+ const char *p;
+
+ assert(field);
+ assert(uid);
+
+ if (pid == 0)
+ return getuid();
+
+ p = procfs_file_alloca(pid, "status");
+ f = fopen(p, "re");
+ if (!f) {
+ if (errno == ENOENT)
+ return -ESRCH;
+ return -errno;
+ }
+
+ FOREACH_LINE(line, f, return -errno) {
+ char *l;
+
+ l = strstrip(line);
+
+ if (startswith(l, field)) {
+ l += strlen(field);
+ l += strspn(l, WHITESPACE);
+
+ l[strcspn(l, WHITESPACE)] = 0;
+
+ return parse_uid(l, uid);
+ }
+ }
+
+ return -EIO;
+}
+
+int get_process_uid(pid_t pid, uid_t *uid) {
+ return get_process_id(pid, "Uid:", uid);
+}
+
+int get_process_gid(pid_t pid, gid_t *gid) {
+ assert_cc(sizeof(uid_t) == sizeof(gid_t));
+ return get_process_id(pid, "Gid:", gid);
+}
+
+int get_process_cwd(pid_t pid, char **cwd) {
+ const char *p;
+
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "cwd");
+
+ return get_process_link_contents(p, cwd);
+}
+
+int get_process_root(pid_t pid, char **root) {
+ const char *p;
+
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "root");
+
+ return get_process_link_contents(p, root);
+}
+
+int get_process_environ(pid_t pid, char **env) {
+ _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_free_ char *outcome = NULL;
+ int c;
+ const char *p;
+ size_t allocated = 0, sz = 0;
+
+ assert(pid >= 0);
+ assert(env);
+
+ p = procfs_file_alloca(pid, "environ");
+
+ f = fopen(p, "re");
+ if (!f) {
+ if (errno == ENOENT)
+ return -ESRCH;
+ return -errno;
+ }
+
+ while ((c = fgetc(f)) != EOF) {
+ if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
+ return -ENOMEM;
+
+ if (c == '\0')
+ outcome[sz++] = '\n';
+ else
+ sz += cescape_char(c, outcome + sz);
+ }
+
+ if (!outcome) {
+ outcome = strdup("");
+ if (!outcome)
+ return -ENOMEM;
+ } else
+ outcome[sz] = '\0';
+
+ *env = outcome;
+ outcome = NULL;
+
+ return 0;
+}
+#endif // 0
+
+int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
+ int r;
+ _cleanup_free_ char *line = NULL;
+ long unsigned ppid;
+ const char *p;
+
+ assert(pid >= 0);
+ assert(_ppid);
+
+ if (pid == 0) {
+ *_ppid = getppid();
+ return 0;
+ }
+
+ p = procfs_file_alloca(pid, "stat");
+ r = read_one_line_file(p, &line);
+ if (r == -ENOENT)
+ return -ESRCH;
+ if (r < 0)
+ return r;
+
+ /* Let's skip the pid and comm fields. The latter is enclosed
+ * in () but does not escape any () in its value, so let's
+ * skip over it manually */
+
+ p = strrchr(line, ')');
+ if (!p)
+ return -EIO;
+
+ p++;
+
+ if (sscanf(p, " "
+ "%*c " /* state */
+ "%lu ", /* ppid */
+ &ppid) != 1)
+ return -EIO;
+
+ if ((long unsigned) (pid_t) ppid != ppid)
+ return -ERANGE;
+
+ *_ppid = (pid_t) ppid;
+
+ return 0;
+}
+
+int wait_for_terminate(pid_t pid, siginfo_t *status) {
+ siginfo_t dummy;
+
+ assert(pid >= 1);
+
+ if (!status)
+ status = &dummy;
+
+ for (;;) {
+ zero(*status);
+
+ if (waitid(P_PID, pid, status, WEXITED) < 0) {
+
+ if (errno == EINTR)
+ continue;
+
+ return -errno;
+ }
+
+ return 0;
+ }
+}
+
+/*
+ * Return values:
+ * < 0 : wait_for_terminate() failed to get the state of the
+ * process, the process was terminated by a signal, or
+ * failed for an unknown reason.
+ * >=0 : The process terminated normally, and its exit code is
+ * returned.
+ *
+ * That is, success is indicated by a return value of zero, and an
+ * error is indicated by a non-zero value.
+ *
+ * A warning is emitted if the process terminates abnormally,
+ * and also if it returns non-zero unless check_exit_code is true.
+ */
+int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
+ int r;
+ siginfo_t status;
+
+ assert(name);
+ assert(pid > 1);
+
+ r = wait_for_terminate(pid, &status);
+ if (r < 0)
+ return log_warning_errno(r, "Failed to wait for %s: %m", name);
+
+ if (status.si_code == CLD_EXITED) {
+ if (status.si_status != 0)
+ log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
+ "%s failed with error code %i.", name, status.si_status);
+ else
+ log_debug("%s succeeded.", name);
+
+ return status.si_status;
+ } else if (status.si_code == CLD_KILLED ||
+ status.si_code == CLD_DUMPED) {
+
+ log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
+ return -EPROTO;
+ }
+
+ log_warning("%s failed due to unknown reason.", name);
+ return -EPROTO;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int kill_and_sigcont(pid_t pid, int sig) {
+ int r;
+
+ r = kill(pid, sig) < 0 ? -errno : 0;
+
+ if (r >= 0)
+ kill(pid, SIGCONT);
+
+ return r;
+}
+#endif // 0
+
+int getenv_for_pid(pid_t pid, const char *field, char **_value) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char *value = NULL;
+ int r;
+ bool done = false;
+ size_t l;
+ const char *path;
+
+ assert(pid >= 0);
+ assert(field);
+ assert(_value);
+
+ path = procfs_file_alloca(pid, "environ");
+
+ f = fopen(path, "re");
+ if (!f) {
+ if (errno == ENOENT)
+ return -ESRCH;
+ return -errno;
+ }
+
+ l = strlen(field);
+ r = 0;
+
+ do {
+ char line[LINE_MAX];
+ unsigned i;
+
+ for (i = 0; i < sizeof(line)-1; i++) {
+ int c;
+
+ c = getc(f);
+ if (_unlikely_(c == EOF)) {
+ done = true;
+ break;
+ } else if (c == 0)
+ break;
+
+ line[i] = c;
+ }
+ line[i] = 0;
+
+ if (memcmp(line, field, l) == 0 && line[l] == '=') {
+ value = strdup(line + l + 1);
+ if (!value)
+ return -ENOMEM;
+
+ r = 1;
+ break;
+ }
+
+ } while (!done);
+
+ *_value = value;
+ return r;
+}
+
+bool pid_is_unwaited(pid_t pid) {
+ /* Checks whether a PID is still valid at all, including a zombie */
+
+ if (pid <= 0)
+ return false;
+
+ if (kill(pid, 0) >= 0)
+ return true;
+
+ return errno != ESRCH;
+}
+
+bool pid_is_alive(pid_t pid) {
+ int r;
+
+ /* Checks whether a PID is still valid and not a zombie */
+
+ if (pid <= 0)
+ return false;
+
+ r = get_process_state(pid);
+ if (r == -ESRCH || r == 'Z')
+ return false;
+
+ return true;
+}
--- /dev/null
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+#include <sys/types.h>
+#include <alloca.h>
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+
+#include "formats-util.h"
+
+#define procfs_file_alloca(pid, field) \
+ ({ \
+ pid_t _pid_ = (pid); \
+ const char *_r_; \
+ if (_pid_ == 0) { \
+ _r_ = ("/proc/self/" field); \
+ } else { \
+ _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
+ sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
+ } \
+ _r_; \
+ })
+
+int get_process_state(pid_t pid);
+int get_process_comm(pid_t pid, char **name);
+int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
+int get_process_exe(pid_t pid, char **name);
+// UNNEEDED int get_process_uid(pid_t pid, uid_t *uid);
+// UNNEEDED int get_process_gid(pid_t pid, gid_t *gid);
+// UNNEEDED int get_process_capeff(pid_t pid, char **capeff);
+// UNNEEDED int get_process_cwd(pid_t pid, char **cwd);
+// UNNEEDED int get_process_root(pid_t pid, char **root);
+// UNNEEDED int get_process_environ(pid_t pid, char **environ);
+
+int wait_for_terminate(pid_t pid, siginfo_t *status);
+int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code);
+
+// UNNEEDED int kill_and_sigcont(pid_t pid, int sig);
+pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
+// UNNEEDED void rename_process(const char name[8]);
+int is_kernel_thread(pid_t pid);
+int getenv_for_pid(pid_t pid, const char *field, char **_value);
+
+bool pid_is_alive(pid_t pid);
+bool pid_is_unwaited(pid_t pid);
--- /dev/null
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdint.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <time.h>
+#ifdef HAVE_SYS_AUXV_H
+#include <sys/auxv.h>
+#endif
+#include <linux/random.h>
+
+#include "random-util.h"
+#include "time-util.h"
+#include "missing.h"
+#include "util.h"
+
+int dev_urandom(void *p, size_t n) {
+ static int have_syscall = -1;
+
+ _cleanup_close_ int fd = -1;
+ int r;
+
+ /* Gathers some randomness from the kernel. This call will
+ * never block, and will always return some data from the
+ * kernel, regardless if the random pool is fully initialized
+ * or not. It thus makes no guarantee for the quality of the
+ * returned entropy, but is good enough for or usual usecases
+ * of seeding the hash functions for hashtable */
+
+ /* Use the getrandom() syscall unless we know we don't have
+ * it, or when the requested size is too large for it. */
+ if (have_syscall != 0 || (size_t) (int) n != n) {
+ r = getrandom(p, n, GRND_NONBLOCK);
+ if (r == (int) n) {
+ have_syscall = true;
+ return 0;
+ }
+
+ if (r < 0) {
+ if (errno == ENOSYS)
+ /* we lack the syscall, continue with
+ * reading from /dev/urandom */
+ have_syscall = false;
+ else if (errno == EAGAIN)
+ /* not enough entropy for now. Let's
+ * remember to use the syscall the
+ * next time, again, but also read
+ * from /dev/urandom for now, which
+ * doesn't care about the current
+ * amount of entropy. */
+ have_syscall = true;
+ else
+ return -errno;
+ } else
+ /* too short read? */
+ return -ENODATA;
+ }
+
+ fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0)
+ return errno == ENOENT ? -ENOSYS : -errno;
+
+ return loop_read_exact(fd, p, n, true);
+}
+
+void initialize_srand(void) {
+ static bool srand_called = false;
+ unsigned x;
+#ifdef HAVE_SYS_AUXV_H
+ void *auxv;
+#endif
+
+ if (srand_called)
+ return;
+
+ x = 0;
+
+#ifdef HAVE_SYS_AUXV_H
+ /* The kernel provides us with a bit of entropy in auxv, so
+ * let's try to make use of that to seed the pseudo-random
+ * generator. It's better than nothing... */
+
+ auxv = (void*) getauxval(AT_RANDOM);
+ if (auxv)
+ x ^= *(unsigned*) auxv;
+#endif
+
+ x ^= (unsigned) now(CLOCK_REALTIME);
+ x ^= (unsigned) gettid();
+
+ srand(x);
+ srand_called = true;
+}
+
+void random_bytes(void *p, size_t n) {
+ uint8_t *q;
+ int r;
+
+ r = dev_urandom(p, n);
+ if (r >= 0)
+ return;
+
+ /* If some idiot made /dev/urandom unavailable to us, he'll
+ * get a PRNG instead. */
+
+ initialize_srand();
+
+ for (q = p; q < (uint8_t*) p + n; q ++)
+ *q = rand();
+}
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
This file is part of systemd.
- Copyright 2013 Lennart Poettering
+ Copyright 2010 Lennart Poettering
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
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include "util.h"
-#include "sd-event.h"
+#include <stdint.h>
+
+int dev_urandom(void *p, size_t n);
+void random_bytes(void *p, size_t n);
+void initialize_srand(void);
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event*, sd_event_unref);
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source*, sd_event_source_unref);
+static inline uint64_t random_u64(void) {
+ uint64_t u;
+ random_bytes(&u, sizeof(u));
+ return u;
+}
-#define _cleanup_event_unref_ _cleanup_(sd_event_unrefp)
-#define _cleanup_event_source_unref_ _cleanup_(sd_event_source_unrefp)
+static inline uint32_t random_u32(void) {
+ uint32_t u;
+ random_bytes(&u, sizeof(u));
+ return u;
+}
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-/* A type-safe atomic refcounter */
+/* A type-safe atomic refcounter.
+ *
+ * DO NOT USE THIS UNLESS YOU ACTUALLY CARE ABOUT THREAD SAFETY! */
typedef struct {
volatile unsigned _value;
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2015 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include "util.h"
+#include "path-util.h"
+// #include "btrfs-util.h"
+#include "rm-rf.h"
+
+int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
+ _cleanup_closedir_ DIR *d = NULL;
+ int ret = 0, r;
+
+ assert(fd >= 0);
+
+ /* This returns the first error we run into, but nevertheless
+ * tries to go on. This closes the passed fd. */
+
+ if (!(flags & REMOVE_PHYSICAL)) {
+
+ r = fd_is_temporary_fs(fd);
+ if (r < 0) {
+ safe_close(fd);
+ return r;
+ }
+
+ if (!r) {
+ /* We refuse to clean physical file systems
+ * with this call, unless explicitly
+ * requested. This is extra paranoia just to
+ * be sure we never ever remove non-state
+ * data */
+
+ log_error("Attempted to remove disk file system, and we can't allow that.");
+ safe_close(fd);
+ return -EPERM;
+ }
+ }
+
+ d = fdopendir(fd);
+ if (!d) {
+ safe_close(fd);
+ return errno == ENOENT ? 0 : -errno;
+ }
+
+ for (;;) {
+ struct dirent *de;
+ bool is_dir;
+ struct stat st;
+
+ errno = 0;
+ de = readdir(d);
+ if (!de) {
+ if (errno != 0 && ret == 0)
+ ret = -errno;
+ return ret;
+ }
+
+ if (streq(de->d_name, ".") || streq(de->d_name, ".."))
+ continue;
+
+ if (de->d_type == DT_UNKNOWN ||
+ (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) {
+ if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
+ if (ret == 0 && errno != ENOENT)
+ ret = -errno;
+ continue;
+ }
+
+ is_dir = S_ISDIR(st.st_mode);
+ } else
+ is_dir = de->d_type == DT_DIR;
+
+ if (is_dir) {
+ int subdir_fd;
+
+ /* if root_dev is set, remove subdirectories only if device is same */
+ if (root_dev && st.st_dev != root_dev->st_dev)
+ continue;
+
+ subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
+ if (subdir_fd < 0) {
+ if (ret == 0 && errno != ENOENT)
+ ret = -errno;
+ continue;
+ }
+
+ /* Stop at mount points */
+ r = fd_is_mount_point(fd, de->d_name, 0);
+ if (r < 0) {
+ if (ret == 0 && r != -ENOENT)
+ ret = r;
+
+ safe_close(subdir_fd);
+ continue;
+ }
+ if (r) {
+ safe_close(subdir_fd);
+ continue;
+ }
+
+#if 0
+ if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
+
+ /* This could be a subvolume, try to remove it */
+ r = btrfs_subvol_remove_fd(fd, de->d_name, true);
+ if (r < 0) {
+ if (r != -ENOTTY && r != -EINVAL) {
+ if (ret == 0)
+ ret = r;
+
+ safe_close(subdir_fd);
+ continue;
+ }
+
+ /* ENOTTY, then it wasn't a
+ * btrfs subvolume, continue
+ * below. */
+ } else {
+ /* It was a subvolume, continue. */
+ safe_close(subdir_fd);
+ continue;
+ }
+ }
+#endif // 0
+
+ /* We pass REMOVE_PHYSICAL here, to avoid
+ * doing the fstatfs() to check the file
+ * system type again for each directory */
+ r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev);
+ if (r < 0 && ret == 0)
+ ret = r;
+
+ if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
+ if (ret == 0 && errno != ENOENT)
+ ret = -errno;
+ }
+
+ } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) {
+
+ if (unlinkat(fd, de->d_name, 0) < 0) {
+ if (ret == 0 && errno != ENOENT)
+ ret = -errno;
+ }
+ }
+ }
+}
+
+int rm_rf(const char *path, RemoveFlags flags) {
+ int fd, r;
+ struct statfs s;
+
+ assert(path);
+
+ /* We refuse to clean the root file system with this
+ * call. This is extra paranoia to never cause a really
+ * seriously broken system. */
+ if (path_equal(path, "/")) {
+ log_error("Attempted to remove entire root file system, and we can't allow that.");
+ return -EPERM;
+ }
+
+#if 0
+ if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
+ /* Try to remove as subvolume first */
+ r = btrfs_subvol_remove(path, true);
+ if (r >= 0)
+ return r;
+
+ if (r != -ENOTTY && r != -EINVAL && r != -ENOTDIR)
+ return r;
+ /* Not btrfs or not a subvolume */
+ }
+#endif // 0
+
+ fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
+ if (fd < 0) {
+
+ if (errno != ENOTDIR && errno != ELOOP)
+ return -errno;
+
+ if (!(flags & REMOVE_PHYSICAL)) {
+ if (statfs(path, &s) < 0)
+ return -errno;
+
+ if (!is_temporary_fs(&s)) {
+ log_error("Attempted to remove disk file system, and we can't allow that.");
+ return -EPERM;
+ }
+ }
+
+ if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES))
+ if (unlink(path) < 0 && errno != ENOENT)
+ return -errno;
+
+ return 0;
+ }
+
+ r = rm_rf_children(fd, flags, NULL);
+
+ if (flags & REMOVE_ROOT) {
+ if (rmdir(path) < 0) {
+ if (r == 0 && errno != ENOENT)
+ r = -errno;
+ }
+ }
+
+ return r;
+}
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include "sd-bus.h"
+#include <sys/stat.h>
-/* Grow the /var/lib/machines directory after each 10MiB written */
-#define GROW_INTERVAL_BYTES (UINT64_C(10) * UINT64_C(1024) * UINT64_C(1024))
+typedef enum RemoveFlags {
+ REMOVE_ONLY_DIRECTORIES = 1,
+ REMOVE_ROOT = 2,
+ REMOVE_PHYSICAL = 4, /* if not set, only removes files on tmpfs, never physical file systems */
+ REMOVE_SUBVOLUME = 8,
+} RemoveFlags;
-int setup_machine_directory(uint64_t size, sd_bus_error *error);
-int grow_machine_directory(void);
+int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev);
+int rm_rf(const char *path, RemoveFlags flags);
#endif
}
+/// UNNEEDED by elogind
+#if 0
void mac_selinux_retest(void) {
#ifdef HAVE_SELINUX
cached_use = -1;
#endif
}
+#endif // 0
int mac_selinux_init(const char *prefix) {
int r = 0;
return r;
}
+/// UNNEEDED by elogind
+#if 0
void mac_selinux_finish(void) {
#ifdef HAVE_SELINUX
label_hnd = NULL;
#endif
}
+#endif // 0
int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
return 0;
}
+/// UNNEDED by elogind
+#if 0
int mac_selinux_apply(const char *path, const char *label) {
#ifdef HAVE_SELINUX
if (!mac_selinux_use())
return -EOPNOTSUPP;
- r = getcon(&mycon);
+ r = getcon_raw(&mycon);
if (r < 0)
return -errno;
- r = getfilecon(exe, &fcon);
+ r = getfilecon_raw(exe, &fcon);
if (r < 0)
return -errno;
if (!mac_selinux_use())
return -EOPNOTSUPP;
- r = getcon(label);
+ r = getcon_raw(label);
if (r < 0)
return -errno;
#endif
if (!mac_selinux_use())
return -EOPNOTSUPP;
- r = getcon(&mycon);
+ r = getcon_raw(&mycon);
if (r < 0)
return -errno;
if (!exec_label) {
/* If there is no context set for next exec let's use context
of target executable */
- r = getfilecon(exe, &fcon);
+ r = getfilecon_raw(exe, &fcon);
if (r < 0)
return -errno;
}
freecon((security_context_t) label);
#endif
}
+#endif // 0
int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
int r = 0;
#endif
}
+/// UNNEEDED by elogind
+#if 0
int mac_selinux_create_socket_prepare(const char *label) {
#ifdef HAVE_SELINUX
#endif
return bind(fd, addr, addrlen) < 0 ? -errno : 0;
}
+#endif // 0
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <sys/types.h>
#include <sys/socket.h>
#include <stdbool.h>
bool mac_selinux_use(void);
-void mac_selinux_retest(void);
+// UNNEEDED void mac_selinux_retest(void);
int mac_selinux_init(const char *prefix);
-void mac_selinux_finish(void);
+// UNNEEDED void mac_selinux_finish(void);
int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs);
-int mac_selinux_apply(const char *path, const char *label);
+// UNNEEDED int mac_selinux_apply(const char *path, const char *label);
-int mac_selinux_get_create_label_from_exe(const char *exe, char **label);
-int mac_selinux_get_our_label(char **label);
-int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label);
-void mac_selinux_free(char *label);
+// UNNEEDED int mac_selinux_get_create_label_from_exe(const char *exe, char **label);
+// UNNEEDED int mac_selinux_get_our_label(char **label);
+// UNNEEDED int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label);
+// UNNEEDED void mac_selinux_free(char *label);
int mac_selinux_create_file_prepare(const char *path, mode_t mode);
void mac_selinux_create_file_clear(void);
-int mac_selinux_create_socket_prepare(const char *label);
-void mac_selinux_create_socket_clear(void);
+// UNNEEDED int mac_selinux_create_socket_prepare(const char *label);
+// UNNEEDED void mac_selinux_create_socket_clear(void);
-int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
+// UNNEEDED int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
#define set_new(ops) internal_set_new(ops HASHMAP_DEBUG_SRC_ARGS)
-static inline void set_free(Set *s) {
+static inline Set *set_free(Set *s) {
internal_hashmap_free(HASHMAP_BASE(s));
+ return NULL;
}
-static inline void set_free_free(Set *s) {
+static inline Set *set_free_free(Set *s) {
internal_hashmap_free_free(HASHMAP_BASE(s));
+ return NULL;
}
/* no set_free_free_free */
/* no set_remove2 */
/* no set_remove_value */
-int set_remove_and_put(Set *s, const void *old_key, const void *new_key);
+// UNNEEDED int set_remove_and_put(Set *s, const void *old_key, const void *new_key);
/* no set_remove_and_replace */
int set_merge(Set *s, Set *other);
return internal_hashmap_buckets(HASHMAP_BASE(s));
}
-void *set_iterate(Set *s, Iterator *i);
+bool set_iterate(Set *s, Iterator *i, void **value);
static inline void set_clear(Set *s) {
internal_hashmap_clear(HASHMAP_BASE(s));
int set_consume(Set *s, void *value);
int set_put_strdup(Set *s, const char *p);
-int set_put_strdupv(Set *s, char **l);
+// UNNEEDED int set_put_strdupv(Set *s, char **l);
#define SET_FOREACH(e, s, i) \
- for ((i) = ITERATOR_FIRST, (e) = set_iterate((s), &(i)); (e); (e) = set_iterate((s), &(i)))
+ for ((i) = ITERATOR_FIRST; set_iterate((s), &(i), (void**)&(e)); )
DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free_free);
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2015 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include "util.h"
+#include "signal-util.h"
+
+int reset_all_signal_handlers(void) {
+ static const struct sigaction sa = {
+ .sa_handler = SIG_DFL,
+ .sa_flags = SA_RESTART,
+ };
+ int sig, r = 0;
+
+ for (sig = 1; sig < _NSIG; sig++) {
+
+ /* These two cannot be caught... */
+ if (sig == SIGKILL || sig == SIGSTOP)
+ continue;
+
+ /* On Linux the first two RT signals are reserved by
+ * glibc, and sigaction() will return EINVAL for them. */
+ if ((sigaction(sig, &sa, NULL) < 0))
+ if (errno != EINVAL && r >= 0)
+ r = -errno;
+ }
+
+ return r;
+}
+
+int reset_signal_mask(void) {
+ sigset_t ss;
+
+ if (sigemptyset(&ss) < 0)
+ return -errno;
+
+ if (sigprocmask(SIG_SETMASK, &ss, NULL) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static int sigaction_many_ap(const struct sigaction *sa, int sig, va_list ap) {
+ int r = 0;
+
+ /* negative signal ends the list. 0 signal is skipped. */
+
+ if (sig < 0)
+ return 0;
+
+ if (sig > 0) {
+ if (sigaction(sig, sa, NULL) < 0)
+ r = -errno;
+ }
+
+ while ((sig = va_arg(ap, int)) >= 0) {
+
+ if (sig == 0)
+ continue;
+
+ if (sigaction(sig, sa, NULL) < 0) {
+ if (r >= 0)
+ r = -errno;
+ }
+ }
+
+ return r;
+}
+
+int sigaction_many(const struct sigaction *sa, ...) {
+ va_list ap;
+ int r;
+
+ va_start(ap, sa);
+ r = sigaction_many_ap(sa, 0, ap);
+ va_end(ap);
+
+ return r;
+}
+
+int ignore_signals(int sig, ...) {
+
+ static const struct sigaction sa = {
+ .sa_handler = SIG_IGN,
+ .sa_flags = SA_RESTART,
+ };
+
+ va_list ap;
+ int r;
+
+ va_start(ap, sig);
+ r = sigaction_many_ap(&sa, sig, ap);
+ va_end(ap);
+
+ return r;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int default_signals(int sig, ...) {
+
+ static const struct sigaction sa = {
+ .sa_handler = SIG_DFL,
+ .sa_flags = SA_RESTART,
+ };
+
+ va_list ap;
+ int r;
+
+ va_start(ap, sig);
+ r = sigaction_many_ap(&sa, sig, ap);
+ va_end(ap);
+
+ return r;
+}
+#endif // 0
+
+static int sigset_add_many_ap(sigset_t *ss, va_list ap) {
+ int sig, r = 0;
+
+ assert(ss);
+
+ while ((sig = va_arg(ap, int)) >= 0) {
+
+ if (sig == 0)
+ continue;
+
+ if (sigaddset(ss, sig) < 0) {
+ if (r >= 0)
+ r = -errno;
+ }
+ }
+
+ return r;
+}
+
+int sigset_add_many(sigset_t *ss, ...) {
+ va_list ap;
+ int r;
+
+ va_start(ap, ss);
+ r = sigset_add_many_ap(ss, ap);
+ va_end(ap);
+
+ return r;
+}
+
+int sigprocmask_many(int how, sigset_t *old, ...) {
+ va_list ap;
+ sigset_t ss;
+ int r;
+
+ if (sigemptyset(&ss) < 0)
+ return -errno;
+
+ va_start(ap, old);
+ r = sigset_add_many_ap(&ss, ap);
+ va_end(ap);
+
+ if (r < 0)
+ return r;
+
+ if (sigprocmask(how, &ss, old) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static const char *const __signal_table[] = {
+ [SIGHUP] = "HUP",
+ [SIGINT] = "INT",
+ [SIGQUIT] = "QUIT",
+ [SIGILL] = "ILL",
+ [SIGTRAP] = "TRAP",
+ [SIGABRT] = "ABRT",
+ [SIGBUS] = "BUS",
+ [SIGFPE] = "FPE",
+ [SIGKILL] = "KILL",
+ [SIGUSR1] = "USR1",
+ [SIGSEGV] = "SEGV",
+ [SIGUSR2] = "USR2",
+ [SIGPIPE] = "PIPE",
+ [SIGALRM] = "ALRM",
+ [SIGTERM] = "TERM",
+#ifdef SIGSTKFLT
+ [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
+#endif
+ [SIGCHLD] = "CHLD",
+ [SIGCONT] = "CONT",
+ [SIGSTOP] = "STOP",
+ [SIGTSTP] = "TSTP",
+ [SIGTTIN] = "TTIN",
+ [SIGTTOU] = "TTOU",
+ [SIGURG] = "URG",
+ [SIGXCPU] = "XCPU",
+ [SIGXFSZ] = "XFSZ",
+ [SIGVTALRM] = "VTALRM",
+ [SIGPROF] = "PROF",
+ [SIGWINCH] = "WINCH",
+ [SIGIO] = "IO",
+ [SIGPWR] = "PWR",
+ [SIGSYS] = "SYS"
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
+
+const char *signal_to_string(int signo) {
+ static thread_local char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1];
+ const char *name;
+
+ name = __signal_to_string(signo);
+ if (name)
+ return name;
+
+ if (signo >= SIGRTMIN && signo <= SIGRTMAX)
+ snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
+ else
+ snprintf(buf, sizeof(buf), "%d", signo);
+
+ return buf;
+}
+
+int signal_from_string(const char *s) {
+ int signo;
+ int offset = 0;
+ unsigned u;
+
+ signo = __signal_from_string(s);
+ if (signo > 0)
+ return signo;
+
+ if (startswith(s, "RTMIN+")) {
+ s += 6;
+ offset = SIGRTMIN;
+ }
+ if (safe_atou(s, &u) >= 0) {
+ signo = (int) u + offset;
+ if (signo > 0 && signo < _NSIG)
+ return signo;
+ }
+ return -EINVAL;
+}
+
+int signal_from_string_try_harder(const char *s) {
+ int signo;
+ assert(s);
+
+ signo = signal_from_string(s);
+ if (signo <= 0)
+ if (startswith(s, "SIG"))
+ return signal_from_string(s+3);
+
+ return signo;
+}
/***
This file is part of systemd.
- Copyright 2013 Kay Sievers
+ Copyright 2010-2015 Lennart Poettering
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
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <signal.h>
#include "macro.h"
-size_t strpcpy(char **dest, size_t size, const char *src);
-size_t strpcpyf(char **dest, size_t size, const char *src, ...) _printf_(3, 4);
-size_t strpcpyl(char **dest, size_t size, const char *src, ...) _sentinel_;
-size_t strscpy(char *dest, size_t size, const char *src);
-size_t strscpyl(char *dest, size_t size, const char *src, ...) _sentinel_;
+int reset_all_signal_handlers(void);
+int reset_signal_mask(void);
+
+int ignore_signals(int sig, ...);
+// UNNEEDED int default_signals(int sig, ...);
+int sigaction_many(const struct sigaction *sa, ...);
+
+int sigset_add_many(sigset_t *ss, ...);
+int sigprocmask_many(int how, sigset_t *old, ...);
+
+const char *signal_to_string(int i) _const_;
+int signal_from_string(const char *s) _pure_;
+
+int signal_from_string_try_harder(const char *s);
#include <sys/xattr.h>
#include "util.h"
+#include "process-util.h"
#include "path-util.h"
#include "fileio.h"
#include "smack-util.h"
#define SMACK_FLOOR_LABEL "_"
#define SMACK_STAR_LABEL "*"
-bool mac_smack_use(void) {
#ifdef HAVE_SMACK
+bool mac_smack_use(void) {
static int cached_use = -1;
if (cached_use < 0)
cached_use = access("/sys/fs/smackfs/", F_OK) >= 0;
return cached_use;
-#else
- return false;
-#endif
}
-int mac_smack_apply(const char *path, const char *label) {
- int r = 0;
+static const char* const smack_attr_table[_SMACK_ATTR_MAX] = {
+ [SMACK_ATTR_ACCESS] = "security.SMACK64",
+ [SMACK_ATTR_EXEC] = "security.SMACK64EXEC",
+ [SMACK_ATTR_MMAP] = "security.SMACK64MMAP",
+ [SMACK_ATTR_TRANSMUTE] = "security.SMACK64TRANSMUTE",
+ [SMACK_ATTR_IPIN] = "security.SMACK64IPIN",
+ [SMACK_ATTR_IPOUT] = "security.SMACK64IPOUT",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(smack_attr, SmackAttr);
+int mac_smack_read(const char *path, SmackAttr attr, char **label) {
assert(path);
+ assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
+ assert(label);
-#ifdef HAVE_SMACK
if (!mac_smack_use())
return 0;
- if (label)
- r = lsetxattr(path, "security.SMACK64", label, strlen(label), 0);
- else
- r = lremovexattr(path, "security.SMACK64");
- if (r < 0)
- return -errno;
-#endif
-
- return r;
+ return getxattr_malloc(path, smack_attr_to_string(attr), label, true);
}
-int mac_smack_apply_fd(int fd, const char *label) {
- int r = 0;
-
+int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
assert(fd >= 0);
+ assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
+ assert(label);
-#ifdef HAVE_SMACK
if (!mac_smack_use())
return 0;
- if (label)
- r = fsetxattr(fd, "security.SMACK64", label, strlen(label), 0);
- else
- r = fremovexattr(fd, "security.SMACK64");
- if (r < 0)
- return -errno;
-#endif
-
- return r;
+ return fgetxattr_malloc(fd, smack_attr_to_string(attr), label);
}
-int mac_smack_apply_ip_out_fd(int fd, const char *label) {
- int r = 0;
+int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
+ int r;
- assert(fd >= 0);
+ assert(path);
+ assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
-#ifdef HAVE_SMACK
if (!mac_smack_use())
return 0;
if (label)
- r = fsetxattr(fd, "security.SMACK64IPOUT", label, strlen(label), 0);
+ r = lsetxattr(path, smack_attr_to_string(attr), label, strlen(label), 0);
else
- r = fremovexattr(fd, "security.SMACK64IPOUT");
+ r = lremovexattr(path, smack_attr_to_string(attr));
if (r < 0)
return -errno;
-#endif
- return r;
+ return 0;
}
-int mac_smack_apply_ip_in_fd(int fd, const char *label) {
- int r = 0;
+int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
+ int r;
assert(fd >= 0);
+ assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
-#ifdef HAVE_SMACK
if (!mac_smack_use())
return 0;
if (label)
- r = fsetxattr(fd, "security.SMACK64IPIN", label, strlen(label), 0);
+ r = fsetxattr(fd, smack_attr_to_string(attr), label, strlen(label), 0);
else
- r = fremovexattr(fd, "security.SMACK64IPIN");
+ r = fremovexattr(fd, smack_attr_to_string(attr));
if (r < 0)
return -errno;
-#endif
- return r;
+ return 0;
}
int mac_smack_apply_pid(pid_t pid, const char *label) {
-
-#ifdef HAVE_SMACK
const char *p;
-#endif
int r = 0;
assert(label);
-#ifdef HAVE_SMACK
if (!mac_smack_use())
return 0;
p = procfs_file_alloca(pid, "attr/current");
- r = write_string_file(p, label);
+ r = write_string_file(p, label, 0);
if (r < 0)
return r;
-#endif
return r;
}
int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
-
-#ifdef HAVE_SMACK
struct stat st;
-#endif
int r = 0;
assert(path);
-#ifdef HAVE_SMACK
if (!mac_smack_use())
return 0;
r = log_debug_errno(errno, "Unable to fix SMACK label of %s: %m", path);
}
-#endif
return r;
}
+
+
+#else
+bool mac_smack_use(void) {
+ return false;
+}
+
+int mac_smack_read(const char *path, SmackAttr attr, char **label) {
+ return -EOPNOTSUPP;
+}
+
+int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
+ return -EOPNOTSUPP;
+}
+
+int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
+ return 0;
+}
+
+int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
+ return 0;
+}
+
+int mac_smack_apply_pid(pid_t pid, const char *label) {
+ return 0;
+}
+
+int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
+ return 0;
+}
+#endif
#include <stdbool.h>
+#include "macro.h"
+
+typedef enum SmackAttr {
+ SMACK_ATTR_ACCESS = 0,
+ SMACK_ATTR_EXEC = 1,
+ SMACK_ATTR_MMAP = 2,
+ SMACK_ATTR_TRANSMUTE = 3,
+ SMACK_ATTR_IPIN = 4,
+ SMACK_ATTR_IPOUT = 5,
+ _SMACK_ATTR_MAX,
+ _SMACK_ATTR_INVALID = -1,
+} SmackAttr;
+
bool mac_smack_use(void);
int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs);
-int mac_smack_apply(const char *path, const char *label);
-int mac_smack_apply_fd(int fd, const char *label);
+const char* smack_attr_to_string(SmackAttr i) _const_;
+SmackAttr smack_attr_from_string(const char *s) _pure_;
+int mac_smack_read(const char *path, SmackAttr attr, char **label);
+int mac_smack_read_fd(int fd, SmackAttr attr, char **label);
+int mac_smack_apply(const char *path, SmackAttr attr, const char *label);
+int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label);
+
int mac_smack_apply_pid(pid_t pid, const char *label);
-int mac_smack_apply_ip_in_fd(int fd, const char *label);
-int mac_smack_apply_ip_out_fd(int fd, const char *label);
struct sockaddr_ll ll;
};
+/// UNNEEDED by elogind
+#if 0
typedef struct SocketAddress {
union sockaddr_union sockaddr;
#define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
int socket_address_parse(SocketAddress *a, const char *s);
+int socket_address_parse_and_warn(SocketAddress *a, const char *s);
int socket_address_parse_netlink(SocketAddress *a, const char *s);
int socket_address_print(const SocketAddress *a, char **p);
int socket_address_verify(const SocketAddress *a) _pure_;
int backlog,
SocketAddressBindIPv6Only only,
const char *bind_to_device,
+ bool reuse_port,
bool free_bind,
bool transparent,
mode_t directory_mode,
#define ETHER_ADDR_TO_STRING_MAX (3*6)
char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]);
+#endif // 0
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
int r;
char **s;
return 0;
}
+#endif // 0
char **strv_split(const char *s, const char *separator) {
const char *word, *state;
return r;
}
+/// UNNEEDED by elogind
+#if 0
char **strv_split_newlines(const char *s) {
char **l;
unsigned n;
return l;
if (isempty(l[n-1])) {
- free(l[n-1]);
- l[n-1] = NULL;
+ l[n-1] = mfree(l[n-1]);
}
return l;
}
-int strv_split_quoted(char ***t, const char *s, UnquoteFlags flags) {
+int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
size_t n = 0, allocated = 0;
_cleanup_strv_free_ char **l = NULL;
int r;
for (;;) {
_cleanup_free_ char *word = NULL;
- r = unquote_first_word(&s, &word, flags);
+ r = extract_first_word(&s, &word, separators, flags);
if (r < 0)
return r;
- if (r == 0)
+ if (r == 0) {
break;
+ }
if (!GREEDY_REALLOC(l, allocated, n + 2))
return -ENOMEM;
return 0;
}
+#endif // 0
char *strv_join(char **l, const char *separator) {
char *r, *e;
return r;
}
+/// UNNEEDED by elogind
+#if 0
char *strv_join_quoted(char **l) {
char *buf = NULL;
char **s;
free(buf);
return NULL;
}
+#endif // 0
int strv_push(char ***l, char *value) {
char **c;
return r;
}
+/// UNNEEDED by elogind
+#if 0
int strv_consume_pair(char ***l, char *a, char *b) {
int r;
return r;
}
+#endif // 0
int strv_consume_prepend(char ***l, char *value) {
int r;
return l;
}
+/// UNNEEDED by elogind
+#if 0
bool strv_is_uniq(char **l) {
char **i;
return true;
}
+#endif // 0
char **strv_remove(char **l, const char *s) {
char **f, **t;
return r;
}
+/// UNNEEDED by elogind
+#if 0
bool strv_overlap(char **a, char **b) {
char **i;
return l;
}
+char **strv_shell_escape(char **l, const char *bad) {
+ char **s;
+
+ /* Escapes every character in every string in l that is in bad,
+ * edits in-place, does not roll-back on error. */
+
+ STRV_FOREACH(s, l) {
+ char *v;
+
+ v = shell_escape(*s, bad);
+ if (!v)
+ return NULL;
+
+ free(*s);
+ *s = v;
+ }
+
+ return l;
+}
+#endif // 0
+
bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
char* const* p;
unsigned strv_length(char * const *l) _pure_;
int strv_extend_strv(char ***a, char **b);
-int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
+// UNNEEDED int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
int strv_extend(char ***l, const char *value);
-int strv_extendf(char ***l, const char *format, ...) _printf_(2,0);
+// UNNEEDED int strv_extendf(char ***l, const char *format, ...) _printf_(2,0);
int strv_push(char ***l, char *value);
int strv_push_pair(char ***l, char *a, char *b);
int strv_push_prepend(char ***l, char *value);
int strv_consume(char ***l, char *value);
-int strv_consume_pair(char ***l, char *a, char *b);
+// UNNEEDED int strv_consume_pair(char ***l, char *a, char *b);
int strv_consume_prepend(char ***l, char *value);
char **strv_remove(char **l, const char *s);
char **strv_uniq(char **l);
-bool strv_is_uniq(char **l);
+// UNNEEDED bool strv_is_uniq(char **l);
-bool strv_equal(char **a, char **b);
+// UNNEEDED bool strv_equal(char **a, char **b);
#define strv_contains(l, s) (!!strv_find((l), (s)))
}
char **strv_split(const char *s, const char *separator);
-char **strv_split_newlines(const char *s);
+// UNNEEDED char **strv_split_newlines(const char *s);
-int strv_split_quoted(char ***t, const char *s, UnquoteFlags flags);
+// UNNEEDED int strv_split_quoted(char ***t, const char *s, UnquoteFlags flags);
char *strv_join(char **l, const char *separator);
-char *strv_join_quoted(char **l);
+// UNNEEDED char *strv_join_quoted(char **l);
char **strv_parse_nulstr(const char *s, size_t l);
char **strv_split_nulstr(const char *s);
-bool strv_overlap(char **a, char **b) _pure_;
+// UNNEEDED bool strv_overlap(char **a, char **b) _pure_;
#define STRV_FOREACH(s, l) \
for ((s) = (l); (s) && *(s); (s)++)
#define STRV_FOREACH_PAIR(x, y, l) \
for ((x) = (l), (y) = (x+1); (x) && *(x) && *(y); (x) += 2, (y) = (x + 1))
-char **strv_sort(char **l);
-void strv_print(char **l);
+// UNNEEDED char **strv_sort(char **l);
+// UNNEEDED void strv_print(char **l);
#define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL }))
_l[0]; \
}))
-char **strv_reverse(char **l);
+// UNNEEDED char **strv_reverse(char **l);
+// UNNEEDED char **strv_shell_escape(char **l, const char *bad);
bool strv_fnmatch(char* const* patterns, const char *s, int flags);
--- /dev/null
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <time.h>
+#include <assert.h>
+#include <poll.h>
+#include <linux/vt.h>
+#include <linux/tiocl.h>
+#include <linux/kd.h>
+
+#include "terminal-util.h"
+#include "time-util.h"
+#include "process-util.h"
+#include "util.h"
+#include "fileio.h"
+#include "path-util.h"
+
+static volatile unsigned cached_columns = 0;
+static volatile unsigned cached_lines = 0;
+
+int chvt(int vt) {
+ _cleanup_close_ int fd;
+
+ fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (fd < 0)
+ return -errno;
+
+ if (vt < 0) {
+ int tiocl[2] = {
+ TIOCL_GETKMSGREDIRECT,
+ 0
+ };
+
+ if (ioctl(fd, TIOCLINUX, tiocl) < 0)
+ return -errno;
+
+ vt = tiocl[0] <= 0 ? 1 : tiocl[0];
+ }
+
+ if (ioctl(fd, VT_ACTIVATE, vt) < 0)
+ return -errno;
+
+ return 0;
+}
+
+int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
+ struct termios old_termios, new_termios;
+ char c, line[LINE_MAX];
+
+ assert(f);
+ assert(ret);
+
+ if (tcgetattr(fileno(f), &old_termios) >= 0) {
+ new_termios = old_termios;
+
+ new_termios.c_lflag &= ~ICANON;
+ new_termios.c_cc[VMIN] = 1;
+ new_termios.c_cc[VTIME] = 0;
+
+ if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
+ size_t k;
+
+ if (t != USEC_INFINITY) {
+ if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) {
+ tcsetattr(fileno(f), TCSADRAIN, &old_termios);
+ return -ETIMEDOUT;
+ }
+ }
+
+ k = fread(&c, 1, 1, f);
+
+ tcsetattr(fileno(f), TCSADRAIN, &old_termios);
+
+ if (k <= 0)
+ return -EIO;
+
+ if (need_nl)
+ *need_nl = c != '\n';
+
+ *ret = c;
+ return 0;
+ }
+ }
+
+ if (t != USEC_INFINITY) {
+ if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0)
+ return -ETIMEDOUT;
+ }
+
+ errno = 0;
+ if (!fgets(line, sizeof(line), f))
+ return errno ? -errno : -EIO;
+
+ truncate_nl(line);
+
+ if (strlen(line) != 1)
+ return -EBADMSG;
+
+ if (need_nl)
+ *need_nl = false;
+
+ *ret = line[0];
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int ask_char(char *ret, const char *replies, const char *text, ...) {
+ int r;
+
+ assert(ret);
+ assert(replies);
+ assert(text);
+
+ for (;;) {
+ va_list ap;
+ char c;
+ bool need_nl = true;
+
+ if (on_tty())
+ fputs(ANSI_HIGHLIGHT_ON, stdout);
+
+ va_start(ap, text);
+ vprintf(text, ap);
+ va_end(ap);
+
+ if (on_tty())
+ fputs(ANSI_HIGHLIGHT_OFF, stdout);
+
+ fflush(stdout);
+
+ r = read_one_char(stdin, &c, USEC_INFINITY, &need_nl);
+ if (r < 0) {
+
+ if (r == -EBADMSG) {
+ puts("Bad input, please try again.");
+ continue;
+ }
+
+ putchar('\n');
+ return r;
+ }
+
+ if (need_nl)
+ putchar('\n');
+
+ if (strchr(replies, c)) {
+ *ret = c;
+ return 0;
+ }
+
+ puts("Read unexpected character, please try again.");
+ }
+}
+
+int ask_string(char **ret, const char *text, ...) {
+ assert(ret);
+ assert(text);
+
+ for (;;) {
+ char line[LINE_MAX];
+ va_list ap;
+
+ if (on_tty())
+ fputs(ANSI_HIGHLIGHT_ON, stdout);
+
+ va_start(ap, text);
+ vprintf(text, ap);
+ va_end(ap);
+
+ if (on_tty())
+ fputs(ANSI_HIGHLIGHT_OFF, stdout);
+
+ fflush(stdout);
+
+ errno = 0;
+ if (!fgets(line, sizeof(line), stdin))
+ return errno ? -errno : -EIO;
+
+ if (!endswith(line, "\n"))
+ putchar('\n');
+ else {
+ char *s;
+
+ if (isempty(line))
+ continue;
+
+ truncate_nl(line);
+ s = strdup(line);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+ }
+ }
+}
+#endif // 0
+
+int reset_terminal_fd(int fd, bool switch_to_text) {
+ struct termios termios;
+ int r = 0;
+
+ /* Set terminal to some sane defaults */
+
+ assert(fd >= 0);
+
+ /* We leave locked terminal attributes untouched, so that
+ * Plymouth may set whatever it wants to set, and we don't
+ * interfere with that. */
+
+ /* Disable exclusive mode, just in case */
+ (void) ioctl(fd, TIOCNXCL);
+
+ /* Switch to text mode */
+ if (switch_to_text)
+ (void) ioctl(fd, KDSETMODE, KD_TEXT);
+
+ /* Enable console unicode mode */
+ (void) ioctl(fd, KDSKBMODE, K_UNICODE);
+
+ if (tcgetattr(fd, &termios) < 0) {
+ r = -errno;
+ goto finish;
+ }
+
+ /* We only reset the stuff that matters to the software. How
+ * hardware is set up we don't touch assuming that somebody
+ * else will do that for us */
+
+ termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
+ termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
+ termios.c_oflag |= ONLCR;
+ termios.c_cflag |= CREAD;
+ termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
+
+ termios.c_cc[VINTR] = 03; /* ^C */
+ termios.c_cc[VQUIT] = 034; /* ^\ */
+ termios.c_cc[VERASE] = 0177;
+ termios.c_cc[VKILL] = 025; /* ^X */
+ termios.c_cc[VEOF] = 04; /* ^D */
+ termios.c_cc[VSTART] = 021; /* ^Q */
+ termios.c_cc[VSTOP] = 023; /* ^S */
+ termios.c_cc[VSUSP] = 032; /* ^Z */
+ termios.c_cc[VLNEXT] = 026; /* ^V */
+ termios.c_cc[VWERASE] = 027; /* ^W */
+ termios.c_cc[VREPRINT] = 022; /* ^R */
+ termios.c_cc[VEOL] = 0;
+ termios.c_cc[VEOL2] = 0;
+
+ termios.c_cc[VTIME] = 0;
+ termios.c_cc[VMIN] = 1;
+
+ if (tcsetattr(fd, TCSANOW, &termios) < 0)
+ r = -errno;
+
+finish:
+ /* Just in case, flush all crap out */
+ (void) tcflush(fd, TCIOFLUSH);
+
+ return r;
+}
+
+int reset_terminal(const char *name) {
+ _cleanup_close_ int fd = -1;
+
+ /* We open the terminal with O_NONBLOCK here, to ensure we
+ * don't block on carrier if this is a terminal with carrier
+ * configured. */
+
+ fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (fd < 0)
+ return fd;
+
+ return reset_terminal_fd(fd, true);
+}
+
+int open_terminal(const char *name, int mode) {
+ int fd, r;
+ unsigned c = 0;
+
+ /*
+ * If a TTY is in the process of being closed opening it might
+ * cause EIO. This is horribly awful, but unlikely to be
+ * changed in the kernel. Hence we work around this problem by
+ * retrying a couple of times.
+ *
+ * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
+ */
+
+ if (mode & O_CREAT)
+ return -EINVAL;
+
+ for (;;) {
+ fd = open(name, mode, 0);
+ if (fd >= 0)
+ break;
+
+ if (errno != EIO)
+ return -errno;
+
+ /* Max 1s in total */
+ if (c >= 20)
+ return -errno;
+
+ usleep(50 * USEC_PER_MSEC);
+ c++;
+ }
+
+ r = isatty(fd);
+ if (r < 0) {
+ safe_close(fd);
+ return -errno;
+ }
+
+ if (!r) {
+ safe_close(fd);
+ return -ENOTTY;
+ }
+
+ return fd;
+}
+
+int acquire_terminal(
+ const char *name,
+ bool fail,
+ bool force,
+ bool ignore_tiocstty_eperm,
+ usec_t timeout) {
+
+ int fd = -1, notify = -1, r = 0, wd = -1;
+ usec_t ts = 0;
+
+ assert(name);
+
+ /* We use inotify to be notified when the tty is closed. We
+ * create the watch before checking if we can actually acquire
+ * it, so that we don't lose any event.
+ *
+ * Note: strictly speaking this actually watches for the
+ * device being closed, it does *not* really watch whether a
+ * tty loses its controlling process. However, unless some
+ * rogue process uses TIOCNOTTY on /dev/tty *after* closing
+ * its tty otherwise this will not become a problem. As long
+ * as the administrator makes sure not configure any service
+ * on the same tty as an untrusted user this should not be a
+ * problem. (Which he probably should not do anyway.) */
+
+ if (timeout != USEC_INFINITY)
+ ts = now(CLOCK_MONOTONIC);
+
+ if (!fail && !force) {
+ notify = inotify_init1(IN_CLOEXEC | (timeout != USEC_INFINITY ? IN_NONBLOCK : 0));
+ if (notify < 0) {
+ r = -errno;
+ goto fail;
+ }
+
+ wd = inotify_add_watch(notify, name, IN_CLOSE);
+ if (wd < 0) {
+ r = -errno;
+ goto fail;
+ }
+ }
+
+ for (;;) {
+ struct sigaction sa_old, sa_new = {
+ .sa_handler = SIG_IGN,
+ .sa_flags = SA_RESTART,
+ };
+
+ if (notify >= 0) {
+ r = flush_fd(notify);
+ if (r < 0)
+ goto fail;
+ }
+
+ /* We pass here O_NOCTTY only so that we can check the return
+ * value TIOCSCTTY and have a reliable way to figure out if we
+ * successfully became the controlling process of the tty */
+ fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
+ * if we already own the tty. */
+ assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
+
+ /* First, try to get the tty */
+ if (ioctl(fd, TIOCSCTTY, force) < 0)
+ r = -errno;
+
+ assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
+
+ /* Sometimes it makes sense to ignore TIOCSCTTY
+ * returning EPERM, i.e. when very likely we already
+ * are have this controlling terminal. */
+ if (r < 0 && r == -EPERM && ignore_tiocstty_eperm)
+ r = 0;
+
+ if (r < 0 && (force || fail || r != -EPERM))
+ goto fail;
+
+ if (r >= 0)
+ break;
+
+ assert(!fail);
+ assert(!force);
+ assert(notify >= 0);
+
+ for (;;) {
+ union inotify_event_buffer buffer;
+ struct inotify_event *e;
+ ssize_t l;
+
+ if (timeout != USEC_INFINITY) {
+ usec_t n;
+
+ n = now(CLOCK_MONOTONIC);
+ if (ts + timeout < n) {
+ r = -ETIMEDOUT;
+ goto fail;
+ }
+
+ r = fd_wait_for_event(fd, POLLIN, ts + timeout - n);
+ if (r < 0)
+ goto fail;
+
+ if (r == 0) {
+ r = -ETIMEDOUT;
+ goto fail;
+ }
+ }
+
+ l = read(notify, &buffer, sizeof(buffer));
+ if (l < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+
+ r = -errno;
+ goto fail;
+ }
+
+ FOREACH_INOTIFY_EVENT(e, buffer, l) {
+ if (e->wd != wd || !(e->mask & IN_CLOSE)) {
+ r = -EIO;
+ goto fail;
+ }
+ }
+
+ break;
+ }
+
+ /* We close the tty fd here since if the old session
+ * ended our handle will be dead. It's important that
+ * we do this after sleeping, so that we don't enter
+ * an endless loop. */
+ fd = safe_close(fd);
+ }
+
+ safe_close(notify);
+
+ r = reset_terminal_fd(fd, true);
+ if (r < 0)
+ log_warning_errno(r, "Failed to reset terminal: %m");
+
+ return fd;
+
+fail:
+ safe_close(fd);
+ safe_close(notify);
+
+ return r;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int release_terminal(void) {
+ static const struct sigaction sa_new = {
+ .sa_handler = SIG_IGN,
+ .sa_flags = SA_RESTART,
+ };
+
+ _cleanup_close_ int fd = -1;
+ struct sigaction sa_old;
+ int r = 0;
+
+ fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (fd < 0)
+ return -errno;
+
+ /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
+ * by our own TIOCNOTTY */
+ assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
+
+ if (ioctl(fd, TIOCNOTTY) < 0)
+ r = -errno;
+
+ assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
+
+ return r;
+}
+#endif // 0
+
+int terminal_vhangup_fd(int fd) {
+ assert(fd >= 0);
+
+ if (ioctl(fd, TIOCVHANGUP) < 0)
+ return -errno;
+
+ return 0;
+}
+
+int terminal_vhangup(const char *name) {
+ _cleanup_close_ int fd;
+
+ fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (fd < 0)
+ return fd;
+
+ return terminal_vhangup_fd(fd);
+}
+
+int vt_disallocate(const char *name) {
+ int fd, r;
+ unsigned u;
+
+ /* Deallocate the VT if possible. If not possible
+ * (i.e. because it is the active one), at least clear it
+ * entirely (including the scrollback buffer) */
+
+ if (!startswith(name, "/dev/"))
+ return -EINVAL;
+
+ if (!tty_is_vc(name)) {
+ /* So this is not a VT. I guess we cannot deallocate
+ * it then. But let's at least clear the screen */
+
+ fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ loop_write(fd,
+ "\033[r" /* clear scrolling region */
+ "\033[H" /* move home */
+ "\033[2J", /* clear screen */
+ 10, false);
+ safe_close(fd);
+
+ return 0;
+ }
+
+ if (!startswith(name, "/dev/tty"))
+ return -EINVAL;
+
+ r = safe_atou(name+8, &u);
+ if (r < 0)
+ return r;
+
+ if (u <= 0)
+ return -EINVAL;
+
+ /* Try to deallocate */
+ fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (fd < 0)
+ return fd;
+
+ r = ioctl(fd, VT_DISALLOCATE, u);
+ safe_close(fd);
+
+ if (r >= 0)
+ return 0;
+
+ if (errno != EBUSY)
+ return -errno;
+
+ /* Couldn't deallocate, so let's clear it fully with
+ * scrollback */
+ fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ loop_write(fd,
+ "\033[r" /* clear scrolling region */
+ "\033[H" /* move home */
+ "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
+ 10, false);
+ safe_close(fd);
+
+ return 0;
+}
+
+void warn_melody(void) {
+ _cleanup_close_ int fd = -1;
+
+ fd = open("/dev/console", O_WRONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0)
+ return;
+
+ /* Yeah, this is synchronous. Kinda sucks. But well... */
+
+ (void) ioctl(fd, KIOCSOUND, (int)(1193180/440));
+ usleep(125*USEC_PER_MSEC);
+
+ (void) ioctl(fd, KIOCSOUND, (int)(1193180/220));
+ usleep(125*USEC_PER_MSEC);
+
+ (void) ioctl(fd, KIOCSOUND, (int)(1193180/220));
+ usleep(125*USEC_PER_MSEC);
+
+ (void) ioctl(fd, KIOCSOUND, 0);
+}
+
+/// UNNEEDED by elogind
+#if 0
+int make_console_stdio(void) {
+ int fd, r;
+
+ /* Make /dev/console the controlling terminal and stdin/stdout/stderr */
+
+ fd = acquire_terminal("/dev/console", false, true, true, USEC_INFINITY);
+ if (fd < 0)
+ return log_error_errno(fd, "Failed to acquire terminal: %m");
+
+ r = make_stdio(fd);
+ if (r < 0)
+ return log_error_errno(r, "Failed to duplicate terminal fd: %m");
+
+ return 0;
+}
+#endif // 0
+
+int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
+ static const char status_indent[] = " "; /* "[" STATUS "] " */
+ _cleanup_free_ char *s = NULL;
+ _cleanup_close_ int fd = -1;
+ struct iovec iovec[6] = {};
+ int n = 0;
+ static bool prev_ephemeral;
+
+ assert(format);
+
+ /* This is independent of logging, as status messages are
+ * optional and go exclusively to the console. */
+
+ if (vasprintf(&s, format, ap) < 0)
+ return log_oom();
+
+ fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ if (ellipse) {
+ char *e;
+ size_t emax, sl;
+ int c;
+
+ c = fd_columns(fd);
+ if (c <= 0)
+ c = 80;
+
+ sl = status ? sizeof(status_indent)-1 : 0;
+
+ emax = c - sl - 1;
+ if (emax < 3)
+ emax = 3;
+
+ e = ellipsize(s, emax, 50);
+ if (e) {
+ free(s);
+ s = e;
+ }
+ }
+
+ if (prev_ephemeral)
+ IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
+ prev_ephemeral = ephemeral;
+
+ if (status) {
+ if (!isempty(status)) {
+ IOVEC_SET_STRING(iovec[n++], "[");
+ IOVEC_SET_STRING(iovec[n++], status);
+ IOVEC_SET_STRING(iovec[n++], "] ");
+ } else
+ IOVEC_SET_STRING(iovec[n++], status_indent);
+ }
+
+ IOVEC_SET_STRING(iovec[n++], s);
+ if (!ephemeral)
+ IOVEC_SET_STRING(iovec[n++], "\n");
+
+ if (writev(fd, iovec, n) < 0)
+ return -errno;
+
+ return 0;
+}
+
+int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) {
+ va_list ap;
+ int r;
+
+ assert(format);
+
+ va_start(ap, format);
+ r = status_vprintf(status, ellipse, ephemeral, format, ap);
+ va_end(ap);
+
+ return r;
+}
+
+bool tty_is_vc(const char *tty) {
+ assert(tty);
+
+ return vtnr_from_tty(tty) >= 0;
+}
+
+bool tty_is_console(const char *tty) {
+ assert(tty);
+
+ if (startswith(tty, "/dev/"))
+ tty += 5;
+
+ return streq(tty, "console");
+}
+
+int vtnr_from_tty(const char *tty) {
+ int i, r;
+
+ assert(tty);
+
+ if (startswith(tty, "/dev/"))
+ tty += 5;
+
+ if (!startswith(tty, "tty") )
+ return -EINVAL;
+
+ if (tty[3] < '0' || tty[3] > '9')
+ return -EINVAL;
+
+ r = safe_atoi(tty+3, &i);
+ if (r < 0)
+ return r;
+
+ if (i < 0 || i > 63)
+ return -EINVAL;
+
+ return i;
+}
+
+char *resolve_dev_console(char **active) {
+ char *tty;
+
+ /* Resolve where /dev/console is pointing to, if /sys is actually ours
+ * (i.e. not read-only-mounted which is a sign for container setups) */
+
+ if (path_is_read_only_fs("/sys") > 0)
+ return NULL;
+
+ if (read_one_line_file("/sys/class/tty/console/active", active) < 0)
+ return NULL;
+
+ /* If multiple log outputs are configured the last one is what
+ * /dev/console points to */
+ tty = strrchr(*active, ' ');
+ if (tty)
+ tty++;
+ else
+ tty = *active;
+
+ if (streq(tty, "tty0")) {
+ char *tmp;
+
+ /* Get the active VC (e.g. tty1) */
+ if (read_one_line_file("/sys/class/tty/tty0/active", &tmp) >= 0) {
+ free(*active);
+ tty = *active = tmp;
+ }
+ }
+
+ return tty;
+}
+
+bool tty_is_vc_resolve(const char *tty) {
+ _cleanup_free_ char *active = NULL;
+
+ assert(tty);
+
+ if (startswith(tty, "/dev/"))
+ tty += 5;
+
+ if (streq(tty, "console")) {
+ tty = resolve_dev_console(&active);
+ if (!tty)
+ return false;
+ }
+
+ return tty_is_vc(tty);
+}
+
+/// UNNEEDED by elogind
+#if 0
+const char *default_term_for_tty(const char *tty) {
+ assert(tty);
+
+ return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt220";
+}
+#endif // 0
+
+int fd_columns(int fd) {
+ struct winsize ws = {};
+
+ if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
+ return -errno;
+
+ if (ws.ws_col <= 0)
+ return -EIO;
+
+ return ws.ws_col;
+}
+
+unsigned columns(void) {
+ const char *e;
+ int c;
+
+ if (_likely_(cached_columns > 0))
+ return cached_columns;
+
+ c = 0;
+ e = getenv("COLUMNS");
+ if (e)
+ (void) safe_atoi(e, &c);
+
+ if (c <= 0)
+ c = fd_columns(STDOUT_FILENO);
+
+ if (c <= 0)
+ c = 80;
+
+ cached_columns = c;
+ return cached_columns;
+}
+
+int fd_lines(int fd) {
+ struct winsize ws = {};
+
+ if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
+ return -errno;
+
+ if (ws.ws_row <= 0)
+ return -EIO;
+
+ return ws.ws_row;
+}
+
+unsigned lines(void) {
+ const char *e;
+ int l;
+
+ if (_likely_(cached_lines > 0))
+ return cached_lines;
+
+ l = 0;
+ e = getenv("LINES");
+ if (e)
+ (void) safe_atoi(e, &l);
+
+ if (l <= 0)
+ l = fd_lines(STDOUT_FILENO);
+
+ if (l <= 0)
+ l = 24;
+
+ cached_lines = l;
+ return cached_lines;
+}
+
+/* intended to be used as a SIGWINCH sighandler */
+/// UNNEEDED by elogind
+#if 0
+void columns_lines_cache_reset(int signum) {
+ cached_columns = 0;
+ cached_lines = 0;
+}
+#endif // 0
+
+bool on_tty(void) {
+ static int cached_on_tty = -1;
+
+ if (_unlikely_(cached_on_tty < 0))
+ cached_on_tty = isatty(STDOUT_FILENO) > 0;
+
+ return cached_on_tty;
+}
+
+int make_stdio(int fd) {
+ int r, s, t;
+
+ assert(fd >= 0);
+
+ r = dup2(fd, STDIN_FILENO);
+ s = dup2(fd, STDOUT_FILENO);
+ t = dup2(fd, STDERR_FILENO);
+
+ if (fd >= 3)
+ safe_close(fd);
+
+ if (r < 0 || s < 0 || t < 0)
+ return -errno;
+
+ /* Explicitly unset O_CLOEXEC, since if fd was < 3, then
+ * dup2() was a NOP and the bit hence possibly set. */
+ fd_cloexec(STDIN_FILENO, false);
+ fd_cloexec(STDOUT_FILENO, false);
+ fd_cloexec(STDERR_FILENO, false);
+
+ return 0;
+}
+
+int make_null_stdio(void) {
+ int null_fd;
+
+ null_fd = open("/dev/null", O_RDWR|O_NOCTTY);
+ if (null_fd < 0)
+ return -errno;
+
+ return make_stdio(null_fd);
+}
+
+int getttyname_malloc(int fd, char **ret) {
+ size_t l = 100;
+ int r;
+
+ assert(fd >= 0);
+ assert(ret);
+
+ for (;;) {
+ char path[l];
+
+ r = ttyname_r(fd, path, sizeof(path));
+ if (r == 0) {
+ const char *p;
+ char *c;
+
+ p = startswith(path, "/dev/");
+ c = strdup(p ?: path);
+ if (!c)
+ return -ENOMEM;
+
+ *ret = c;
+ return 0;
+ }
+
+ if (r != ERANGE)
+ return -r;
+
+ l *= 2;
+ }
+
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int getttyname_harder(int fd, char **r) {
+ int k;
+ char *s = NULL;
+
+ k = getttyname_malloc(fd, &s);
+ if (k < 0)
+ return k;
+
+ if (streq(s, "tty")) {
+ free(s);
+ return get_ctty(0, NULL, r);
+ }
+
+ *r = s;
+ return 0;
+}
+#endif // 0
+
+int get_ctty_devnr(pid_t pid, dev_t *d) {
+ int r;
+ _cleanup_free_ char *line = NULL;
+ const char *p;
+ unsigned long ttynr;
+
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "stat");
+ r = read_one_line_file(p, &line);
+ if (r < 0)
+ return r;
+
+ p = strrchr(line, ')');
+ if (!p)
+ return -EIO;
+
+ p++;
+
+ if (sscanf(p, " "
+ "%*c " /* state */
+ "%*d " /* ppid */
+ "%*d " /* pgrp */
+ "%*d " /* session */
+ "%lu ", /* ttynr */
+ &ttynr) != 1)
+ return -EIO;
+
+ if (major(ttynr) == 0 && minor(ttynr) == 0)
+ return -ENXIO;
+
+ if (d)
+ *d = (dev_t) ttynr;
+
+ return 0;
+}
+
+int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
+ char fn[sizeof("/dev/char/")-1 + 2*DECIMAL_STR_MAX(unsigned) + 1 + 1], *b = NULL;
+ _cleanup_free_ char *s = NULL;
+ const char *p;
+ dev_t devnr;
+ int k;
+
+ assert(r);
+
+ k = get_ctty_devnr(pid, &devnr);
+ if (k < 0)
+ return k;
+
+ sprintf(fn, "/dev/char/%u:%u", major(devnr), minor(devnr));
+
+ k = readlink_malloc(fn, &s);
+ if (k < 0) {
+
+ if (k != -ENOENT)
+ return k;
+
+ /* This is an ugly hack */
+ if (major(devnr) == 136) {
+ if (asprintf(&b, "pts/%u", minor(devnr)) < 0)
+ return -ENOMEM;
+ } else {
+ /* Probably something like the ptys which have no
+ * symlink in /dev/char. Let's return something
+ * vaguely useful. */
+
+ b = strdup(fn + 5);
+ if (!b)
+ return -ENOMEM;
+ }
+ } else {
+ if (startswith(s, "/dev/"))
+ p = s + 5;
+ else if (startswith(s, "../"))
+ p = s + 3;
+ else
+ p = s;
+
+ b = strdup(p);
+ if (!b)
+ return -ENOMEM;
+ }
+
+ *r = b;
+ if (_devnr)
+ *_devnr = devnr;
+
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int ptsname_namespace(int pty, char **ret) {
+ int no = -1, r;
+
+ /* Like ptsname(), but doesn't assume that the path is
+ * accessible in the local namespace. */
+
+ r = ioctl(pty, TIOCGPTN, &no);
+ if (r < 0)
+ return -errno;
+
+ if (no < 0)
+ return -EIO;
+
+ if (asprintf(ret, "/dev/pts/%i", no) < 0)
+ return -ENOMEM;
+
+ return 0;
+}
+#endif // 0
--- /dev/null
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "macro.h"
+#include "time-util.h"
+
+#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
+#define ANSI_RED_ON "\x1B[31m"
+#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
+#define ANSI_GREEN_ON "\x1B[32m"
+#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
+#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
+#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
+#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
+#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
+
+int reset_terminal_fd(int fd, bool switch_to_text);
+int reset_terminal(const char *name);
+
+int open_terminal(const char *name, int mode);
+int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
+// UNNEEDED int release_terminal(void);
+
+int terminal_vhangup_fd(int fd);
+int terminal_vhangup(const char *name);
+
+int chvt(int vt);
+
+int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
+// UNNEEDED int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
+// UNNEEDED int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
+
+int vt_disallocate(const char *name);
+
+char *resolve_dev_console(char **active);
+bool tty_is_vc(const char *tty);
+bool tty_is_vc_resolve(const char *tty);
+bool tty_is_console(const char *tty) _pure_;
+int vtnr_from_tty(const char *tty);
+// UNNEEDED const char *default_term_for_tty(const char *tty);
+
+void warn_melody(void);
+
+int make_stdio(int fd);
+int make_null_stdio(void);
+// UNNEEDED int make_console_stdio(void);
+
+int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
+int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
+
+int fd_columns(int fd);
+unsigned columns(void);
+int fd_lines(int fd);
+unsigned lines(void);
+// UNNEEDED void columns_lines_cache_reset(int _unused_ signum);
+
+bool on_tty(void);
+
+static inline const char *ansi_highlight(void) {
+ return on_tty() ? ANSI_HIGHLIGHT_ON : "";
+}
+
+static inline const char *ansi_highlight_red(void) {
+ return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
+}
+
+static inline const char *ansi_highlight_green(void) {
+ return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
+}
+
+static inline const char *ansi_highlight_yellow(void) {
+ return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
+}
+
+static inline const char *ansi_highlight_blue(void) {
+ return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
+}
+
+static inline const char *ansi_highlight_off(void) {
+ return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
+}
+
+int get_ctty_devnr(pid_t pid, dev_t *d);
+int get_ctty(pid_t, dev_t *_devnr, char **r);
+
+int getttyname_malloc(int fd, char **r);
+// UNNEEDED int getttyname_harder(int fd, char **r);
+
+// UNNEEDED int ptsname_namespace(int pty, char **ret);
#include "util.h"
#include "time-util.h"
+#include "path-util.h"
#include "strv.h"
usec_t now(clockid_t clock_id) {
return timespec_load(&ts);
}
+/// UNNEEDED by elogind
+#if 0
+nsec_t now_nsec(clockid_t clock_id) {
+ struct timespec ts;
+
+ assert_se(clock_gettime(clock_id, &ts) == 0);
+
+ return timespec_load_nsec(&ts);
+}
+#endif // 0
+
dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
assert(ts);
return ts;
}
+/// UNNEEDED by elogind
+#if 0
dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
int64_t delta;
assert(ts);
return ts;
}
+dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
+ int64_t delta;
+
+ if (u == USEC_INFINITY) {
+ ts->realtime = ts->monotonic = USEC_INFINITY;
+ return ts;
+ }
+ ts->realtime = now(CLOCK_REALTIME);
+ ts->monotonic = now(CLOCK_MONOTONIC);
+
+ delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
+
+ if ((int64_t) ts->realtime > delta)
+ ts->realtime -= delta;
+ else
+ ts->realtime = 0;
+
+ if ((int64_t) ts->monotonic > delta)
+ ts->monotonic -= delta;
+ else
+ ts->monotonic = 0;
+
+ return ts;
+}
+#endif // 0
+
usec_t timespec_load(const struct timespec *ts) {
assert(ts);
(usec_t) ts->tv_nsec / NSEC_PER_USEC;
}
+nsec_t timespec_load_nsec(const struct timespec *ts) {
+ assert(ts);
+
+ if (ts->tv_sec == (time_t) -1 &&
+ ts->tv_nsec == (long) -1)
+ return NSEC_INFINITY;
+
+ return
+ (nsec_t) ts->tv_sec * NSEC_PER_SEC +
+ (nsec_t) ts->tv_nsec;
+}
+
struct timespec *timespec_store(struct timespec *ts, usec_t u) {
assert(ts);
return format_timestamp_internal(buf, l, t, false);
}
+/// UNNEEDED by elogind
+#if 0
char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
return format_timestamp_internal(buf, l, t, true);
}
+#endif // 0
static char *format_timestamp_internal_us(char *buf, size_t l, usec_t t, bool utc) {
struct tm tm;
return format_timestamp_internal_us(buf, l, t, false);
}
+/// UNNEEDED by elogind
+#if 0
char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
return format_timestamp_internal_us(buf, l, t, true);
}
+#endif // 0
char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
const char *s;
return buf;
}
+/// UNNEEDED by elogind
+#if 0
void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
assert(f);
t->monotonic);
}
-void dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
+int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
unsigned long long a, b;
assert(value);
assert(t);
- if (sscanf(value, "%llu %llu", &a, &b) != 2)
- log_debug("Failed to parse finish timestamp value %s", value);
- else {
- t->realtime = a;
- t->monotonic = b;
+ if (sscanf(value, "%llu %llu", &a, &b) != 2) {
+ log_debug("Failed to parse finish timestamp value %s.", value);
+ return -EINVAL;
}
+
+ t->realtime = a;
+ t->monotonic = b;
+
+ return 0;
}
int parse_timestamp(const char *t, usec_t *usec) {
return 0;
}
+#endif // 0
int parse_sec(const char *t, usec_t *usec) {
static const struct {
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int parse_nsec(const char *t, nsec_t *nsec) {
static const struct {
const char *suffix;
const char *p, *t;
struct stat st;
- if (!name || *name == 0 || *name == '/')
+ if (isempty(name))
+ return false;
+
+ if (name[0] == '/')
return false;
for (p = name; *p; p++) {
return clock;
}
+
+int get_timezone(char **tz) {
+ _cleanup_free_ char *t = NULL;
+ const char *e;
+ char *z;
+ int r;
+
+ r = readlink_malloc("/etc/localtime", &t);
+ if (r < 0)
+ return r; /* returns EINVAL if not a symlink */
+
+ e = path_startswith(t, "/usr/share/zoneinfo/");
+ if (!e)
+ e = path_startswith(t, "../usr/share/zoneinfo/");
+ if (!e)
+ return -EINVAL;
+
+ if (!timezone_is_valid(e))
+ return -EINVAL;
+
+ z = strdup(e);
+ if (!z)
+ return -ENOMEM;
+
+ *tz = z;
+ return 0;
+}
+#endif // 0
#define DUAL_TIMESTAMP_NULL ((struct dual_timestamp) { 0ULL, 0ULL })
usec_t now(clockid_t clock);
+// UNNEEDED nsec_t now_nsec(clockid_t clock);
dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u);
-dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u);
+// UNNEEDED dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u);
+// UNNEEDED dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u);
static inline bool dual_timestamp_is_set(dual_timestamp *ts) {
return ((ts->realtime > 0 && ts->realtime != USEC_INFINITY) ||
usec_t timeval_load(const struct timeval *tv) _pure_;
struct timeval *timeval_store(struct timeval *tv, usec_t u);
+nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
+
char *format_timestamp(char *buf, size_t l, usec_t t);
-char *format_timestamp_utc(char *buf, size_t l, usec_t t);
+// UNNEEDED char *format_timestamp_utc(char *buf, size_t l, usec_t t);
char *format_timestamp_us(char *buf, size_t l, usec_t t);
-char *format_timestamp_us_utc(char *buf, size_t l, usec_t t);
+// UNNEEDED char *format_timestamp_us_utc(char *buf, size_t l, usec_t t);
char *format_timestamp_relative(char *buf, size_t l, usec_t t);
char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy);
-void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t);
-void dual_timestamp_deserialize(const char *value, dual_timestamp *t);
+// UNNEEDED void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t);
+// UNNEEDED int dual_timestamp_deserialize(const char *value, dual_timestamp *t);
-int parse_timestamp(const char *t, usec_t *usec);
+// UNNEEDED int parse_timestamp(const char *t, usec_t *usec);
int parse_sec(const char *t, usec_t *usec);
-int parse_nsec(const char *t, nsec_t *nsec);
+// UNNEEDED int parse_nsec(const char *t, nsec_t *nsec);
-bool ntp_synced(void);
+// UNNEEDED bool ntp_synced(void);
-int get_timezones(char ***l);
-bool timezone_is_valid(const char *name);
+// UNNEEDED int get_timezones(char ***l);
+// UNNEEDED bool timezone_is_valid(const char *name);
-clockid_t clock_boottime_or_monotonic(void);
+// UNNEEDED clockid_t clock_boottime_or_monotonic(void);
#define xstrftime(buf, fmt, tm) assert_se(strftime(buf, ELEMENTSOF(buf), fmt, tm) > 0)
+
+// UNNEEDED int get_timezone(char **timezone);
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <errno.h>
+#include <string.h>
+
+#include "path-util.h"
+#include "bus-label.h"
+#include "util.h"
+#include "unit-name.h"
+#include "def.h"
+#include "strv.h"
+
+#define VALID_CHARS \
+ DIGITS LETTERS \
+ ":-_.\\"
+
+bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
+ const char *e, *i, *at;
+
+ assert((flags & ~(UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0);
+
+ if (_unlikely_(flags == 0))
+ return false;
+
+ if (isempty(n))
+ return false;
+
+ if (strlen(n) >= UNIT_NAME_MAX)
+ return false;
+
+ e = strrchr(n, '.');
+ if (!e || e == n)
+ return false;
+
+ if (unit_type_from_string(e + 1) < 0)
+ return false;
+
+ for (i = n, at = NULL; i < e; i++) {
+
+ if (*i == '@' && !at)
+ at = i;
+
+ if (!strchr("@" VALID_CHARS, *i))
+ return false;
+ }
+
+ if (at == n)
+ return false;
+
+ if (flags & UNIT_NAME_PLAIN)
+ if (!at)
+ return true;
+
+ if (flags & UNIT_NAME_INSTANCE)
+ if (at && e > at + 1)
+ return true;
+
+ if (flags & UNIT_NAME_TEMPLATE)
+ if (at && e == at + 1)
+ return true;
+
+ return false;
+}
+
+bool unit_prefix_is_valid(const char *p) {
+
+ /* We don't allow additional @ in the prefix string */
+
+ if (isempty(p))
+ return false;
+
+ return in_charset(p, VALID_CHARS);
+}
+
+bool unit_instance_is_valid(const char *i) {
+
+ /* The max length depends on the length of the string, so we
+ * don't really check this here. */
+
+ if (isempty(i))
+ return false;
+
+ /* We allow additional @ in the instance string, we do not
+ * allow them in the prefix! */
+
+ return in_charset(i, "@" VALID_CHARS);
+}
+
+bool unit_suffix_is_valid(const char *s) {
+ if (isempty(s))
+ return false;
+
+ if (s[0] != '.')
+ return false;
+
+ if (unit_type_from_string(s + 1) < 0)
+ return false;
+
+ return true;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int unit_name_to_prefix(const char *n, char **ret) {
+ const char *p;
+ char *s;
+
+ assert(n);
+ assert(ret);
+
+ if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+ return -EINVAL;
+
+ p = strchr(n, '@');
+ if (!p)
+ p = strrchr(n, '.');
+
+ assert_se(p);
+
+ s = strndup(n, p - n);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
+int unit_name_to_instance(const char *n, char **instance) {
+ const char *p, *d;
+ char *i;
+
+ assert(n);
+ assert(instance);
+
+ if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+ return -EINVAL;
+
+ /* Everything past the first @ and before the last . is the instance */
+ p = strchr(n, '@');
+ if (!p) {
+ *instance = NULL;
+ return 0;
+ }
+
+ p++;
+
+ d = strrchr(p, '.');
+ if (!d)
+ return -EINVAL;
+
+ i = strndup(p, d-p);
+ if (!i)
+ return -ENOMEM;
+
+ *instance = i;
+ return 1;
+}
+
+int unit_name_to_prefix_and_instance(const char *n, char **ret) {
+ const char *d;
+ char *s;
+
+ assert(n);
+ assert(ret);
+
+ if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+ return -EINVAL;
+
+ d = strrchr(n, '.');
+ if (!d)
+ return -EINVAL;
+
+ s = strndup(n, d - n);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
+UnitType unit_name_to_type(const char *n) {
+ const char *e;
+
+ assert(n);
+
+ if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+ return _UNIT_TYPE_INVALID;
+
+ assert_se(e = strrchr(n, '.'));
+
+ return unit_type_from_string(e + 1);
+}
+
+int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
+ char *e, *s;
+ size_t a, b;
+
+ assert(n);
+ assert(suffix);
+ assert(ret);
+
+ if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+ return -EINVAL;
+
+ if (!unit_suffix_is_valid(suffix))
+ return -EINVAL;
+
+ assert_se(e = strrchr(n, '.'));
+
+ a = e - n;
+ b = strlen(suffix);
+
+ s = new(char, a + b + 1);
+ if (!s)
+ return -ENOMEM;
+
+ strcpy(mempcpy(s, n, a), suffix);
+ *ret = s;
+
+ return 0;
+}
+#endif // 0
+
+int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) {
+ char *s;
+
+ assert(prefix);
+ assert(suffix);
+ assert(ret);
+
+ if (!unit_prefix_is_valid(prefix))
+ return -EINVAL;
+
+ if (instance && !unit_instance_is_valid(instance))
+ return -EINVAL;
+
+ if (!unit_suffix_is_valid(suffix))
+ return -EINVAL;
+
+ if (!instance)
+ s = strappend(prefix, suffix);
+ else
+ s = strjoin(prefix, "@", instance, suffix, NULL);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+static char *do_escape_char(char c, char *t) {
+ assert(t);
+
+ *(t++) = '\\';
+ *(t++) = 'x';
+ *(t++) = hexchar(c >> 4);
+ *(t++) = hexchar(c);
+
+ return t;
+}
+
+static char *do_escape(const char *f, char *t) {
+ assert(f);
+ assert(t);
+
+ /* do not create units with a leading '.', like for "/.dotdir" mount points */
+ if (*f == '.') {
+ t = do_escape_char(*f, t);
+ f++;
+ }
+
+ for (; *f; f++) {
+ if (*f == '/')
+ *(t++) = '-';
+ else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f))
+ t = do_escape_char(*f, t);
+ else
+ *(t++) = *f;
+ }
+
+ return t;
+}
+
+char *unit_name_escape(const char *f) {
+ char *r, *t;
+
+ assert(f);
+
+ r = new(char, strlen(f)*4+1);
+ if (!r)
+ return NULL;
+
+ t = do_escape(f, r);
+ *t = 0;
+
+ return r;
+}
+
+int unit_name_unescape(const char *f, char **ret) {
+ _cleanup_free_ char *r = NULL;
+ char *t;
+
+ assert(f);
+
+ r = strdup(f);
+ if (!r)
+ return -ENOMEM;
+
+ for (t = r; *f; f++) {
+ if (*f == '-')
+ *(t++) = '/';
+ else if (*f == '\\') {
+ int a, b;
+
+ if (f[1] != 'x')
+ return -EINVAL;
+
+ a = unhexchar(f[2]);
+ if (a < 0)
+ return -EINVAL;
+
+ b = unhexchar(f[3]);
+ if (b < 0)
+ return -EINVAL;
+
+ *(t++) = (char) (((uint8_t) a << 4U) | (uint8_t) b);
+ f += 3;
+ } else
+ *(t++) = *f;
+ }
+
+ *t = 0;
+
+ *ret = r;
+ r = NULL;
+
+ return 0;
+}
+
+int unit_name_path_escape(const char *f, char **ret) {
+ char *p, *s;
+
+ assert(f);
+ assert(ret);
+
+ p = strdupa(f);
+ if (!p)
+ return -ENOMEM;
+
+ path_kill_slashes(p);
+
+ if (STR_IN_SET(p, "/", ""))
+ s = strdup("-");
+ else {
+ char *e;
+
+ if (!path_is_safe(p))
+ return -EINVAL;
+
+ /* Truncate trailing slashes */
+ e = endswith(p, "/");
+ if (e)
+ *e = 0;
+
+ /* Truncate leading slashes */
+ if (p[0] == '/')
+ p++;
+
+ s = unit_name_escape(p);
+ }
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
+int unit_name_path_unescape(const char *f, char **ret) {
+ char *s;
+ int r;
+
+ assert(f);
+
+ if (isempty(f))
+ return -EINVAL;
+
+ if (streq(f, "-")) {
+ s = strdup("/");
+ if (!s)
+ return -ENOMEM;
+ } else {
+ char *w;
+
+ r = unit_name_unescape(f, &w);
+ if (r < 0)
+ return r;
+
+ /* Don't accept trailing or leading slashes */
+ if (startswith(w, "/") || endswith(w, "/")) {
+ free(w);
+ return -EINVAL;
+ }
+
+ /* Prefix a slash again */
+ s = strappend("/", w);
+ free(w);
+ if (!s)
+ return -ENOMEM;
+
+ if (!path_is_safe(s)) {
+ free(s);
+ return -EINVAL;
+ }
+ }
+
+ if (ret)
+ *ret = s;
+ else
+ free(s);
+
+ return 0;
+}
+
+int unit_name_replace_instance(const char *f, const char *i, char **ret) {
+ const char *p, *e;
+ char *s;
+ size_t a, b;
+
+ assert(f);
+ assert(i);
+ assert(ret);
+
+ if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
+ return -EINVAL;
+ if (!unit_instance_is_valid(i))
+ return -EINVAL;
+
+ assert_se(p = strchr(f, '@'));
+ assert_se(e = strrchr(f, '.'));
+
+ a = p - f;
+ b = strlen(i);
+
+ s = new(char, a + 1 + b + strlen(e) + 1);
+ if (!s)
+ return -ENOMEM;
+
+ strcpy(mempcpy(mempcpy(s, f, a + 1), i, b), e);
+
+ *ret = s;
+ return 0;
+}
+
+int unit_name_template(const char *f, char **ret) {
+ const char *p, *e;
+ char *s;
+ size_t a;
+
+ assert(f);
+ assert(ret);
+
+ if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
+ return -EINVAL;
+
+ assert_se(p = strchr(f, '@'));
+ assert_se(e = strrchr(f, '.'));
+
+ a = p - f;
+
+ s = new(char, a + 1 + strlen(e) + 1);
+ if (!s)
+ return -ENOMEM;
+
+ strcpy(mempcpy(s, f, a + 1), e);
+
+ *ret = s;
+ return 0;
+}
+
+int unit_name_from_path(const char *path, const char *suffix, char **ret) {
+ _cleanup_free_ char *p = NULL;
+ char *s = NULL;
+ int r;
+
+ assert(path);
+ assert(suffix);
+ assert(ret);
+
+ if (!unit_suffix_is_valid(suffix))
+ return -EINVAL;
+
+ r = unit_name_path_escape(path, &p);
+ if (r < 0)
+ return r;
+
+ s = strappend(p, suffix);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
+int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret) {
+ _cleanup_free_ char *p = NULL;
+ char *s;
+ int r;
+
+ assert(prefix);
+ assert(path);
+ assert(suffix);
+ assert(ret);
+
+ if (!unit_prefix_is_valid(prefix))
+ return -EINVAL;
+
+ if (!unit_suffix_is_valid(suffix))
+ return -EINVAL;
+
+ r = unit_name_path_escape(path, &p);
+ if (r < 0)
+ return r;
+
+ s = strjoin(prefix, "@", p, suffix, NULL);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
+int unit_name_to_path(const char *name, char **ret) {
+ _cleanup_free_ char *prefix = NULL;
+ int r;
+
+ assert(name);
+
+ r = unit_name_to_prefix(name, &prefix);
+ if (r < 0)
+ return r;
+
+ return unit_name_path_unescape(prefix, ret);
+}
+
+char *unit_dbus_path_from_name(const char *name) {
+ _cleanup_free_ char *e = NULL;
+
+ assert(name);
+
+ e = bus_label_escape(name);
+ if (!e)
+ return NULL;
+
+ return strappend("/org/freedesktop/systemd1/unit/", e);
+}
+
+int unit_name_from_dbus_path(const char *path, char **name) {
+ const char *e;
+ char *n;
+
+ e = startswith(path, "/org/freedesktop/systemd1/unit/");
+ if (!e)
+ return -EINVAL;
+
+ n = bus_label_unescape(e);
+ if (!n)
+ return -ENOMEM;
+
+ *name = n;
+ return 0;
+}
+
+const char* unit_dbus_interface_from_type(UnitType t) {
+
+ static const char *const table[_UNIT_TYPE_MAX] = {
+ [UNIT_SERVICE] = "org.freedesktop.systemd1.Service",
+ [UNIT_SOCKET] = "org.freedesktop.systemd1.Socket",
+ [UNIT_BUSNAME] = "org.freedesktop.systemd1.BusName",
+ [UNIT_TARGET] = "org.freedesktop.systemd1.Target",
+ [UNIT_SNAPSHOT] = "org.freedesktop.systemd1.Snapshot",
+ [UNIT_DEVICE] = "org.freedesktop.systemd1.Device",
+ [UNIT_MOUNT] = "org.freedesktop.systemd1.Mount",
+ [UNIT_AUTOMOUNT] = "org.freedesktop.systemd1.Automount",
+ [UNIT_SWAP] = "org.freedesktop.systemd1.Swap",
+ [UNIT_TIMER] = "org.freedesktop.systemd1.Timer",
+ [UNIT_PATH] = "org.freedesktop.systemd1.Path",
+ [UNIT_SLICE] = "org.freedesktop.systemd1.Slice",
+ [UNIT_SCOPE] = "org.freedesktop.systemd1.Scope",
+ };
+
+ if (t < 0)
+ return NULL;
+ if (t >= _UNIT_TYPE_MAX)
+ return NULL;
+
+ return table[t];
+}
+
+const char *unit_dbus_interface_from_name(const char *name) {
+ UnitType t;
+
+ t = unit_name_to_type(name);
+ if (t < 0)
+ return NULL;
+
+ return unit_dbus_interface_from_type(t);
+}
+
+static char *do_escape_mangle(const char *f, UnitNameMangle allow_globs, char *t) {
+ const char *valid_chars;
+
+ assert(f);
+ assert(IN_SET(allow_globs, UNIT_NAME_GLOB, UNIT_NAME_NOGLOB));
+ assert(t);
+
+ /* We'll only escape the obvious characters here, to play
+ * safe. */
+
+ valid_chars = allow_globs == UNIT_NAME_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS;
+
+ for (; *f; f++) {
+ if (*f == '/')
+ *(t++) = '-';
+ else if (!strchr(valid_chars, *f))
+ t = do_escape_char(*f, t);
+ else
+ *(t++) = *f;
+ }
+
+ return t;
+}
+
+/**
+ * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
+ * /blah/blah is converted to blah-blah.mount, anything else is left alone,
+ * except that @suffix is appended if a valid unit suffix is not present.
+ *
+ * If @allow_globs, globs characters are preserved. Otherwise they are escaped.
+ */
+int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret) {
+ char *s, *t;
+ int r;
+
+ assert(name);
+ assert(suffix);
+ assert(ret);
+
+ if (isempty(name)) /* We cannot mangle empty unit names to become valid, sorry. */
+ return -EINVAL;
+
+ if (!unit_suffix_is_valid(suffix))
+ return -EINVAL;
+
+ if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
+ /* No mangling necessary... */
+ s = strdup(name);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+ }
+
+ if (is_device_path(name)) {
+ r = unit_name_from_path(name, ".device", ret);
+ if (r >= 0)
+ return 1;
+ if (r != -EINVAL)
+ return r;
+ }
+
+ if (path_is_absolute(name)) {
+ r = unit_name_from_path(name, ".mount", ret);
+ if (r >= 0)
+ return 1;
+ if (r != -EINVAL)
+ return r;
+ }
+
+ s = new(char, strlen(name) * 4 + strlen(suffix) + 1);
+ if (!s)
+ return -ENOMEM;
+
+ t = do_escape_mangle(name, allow_globs, s);
+ *t = 0;
+
+ if (unit_name_to_type(s) < 0)
+ strcpy(t, suffix);
+
+ *ret = s;
+ return 1;
+}
+
+int slice_build_parent_slice(const char *slice, char **ret) {
+ char *s, *dash;
+ int r;
+
+ assert(slice);
+ assert(ret);
+
+ if (!slice_name_is_valid(slice))
+ return -EINVAL;
+
+ if (streq(slice, "-.slice")) {
+ *ret = NULL;
+ return 0;
+ }
+
+ s = strdup(slice);
+ if (!s)
+ return -ENOMEM;
+
+ dash = strrchr(s, '-');
+ if (dash)
+ strcpy(dash, ".slice");
+ else {
+ r = free_and_strdup(&s, "-.slice");
+ if (r < 0) {
+ free(s);
+ return r;
+ }
+ }
+
+ *ret = s;
+ return 1;
+}
+#endif // 0
+
+int slice_build_subslice(const char *slice, const char*name, char **ret) {
+ char *subslice;
+
+ assert(slice);
+ assert(name);
+ assert(ret);
+
+ if (!slice_name_is_valid(slice))
+ return -EINVAL;
+
+ if (!unit_prefix_is_valid(name))
+ return -EINVAL;
+
+ if (streq(slice, "-.slice"))
+ subslice = strappend(name, ".slice");
+ else {
+ char *e;
+
+ assert_se(e = endswith(slice, ".slice"));
+
+ subslice = new(char, (e - slice) + 1 + strlen(name) + 6 + 1);
+ if (!subslice)
+ return -ENOMEM;
+
+ stpcpy(stpcpy(stpcpy(mempcpy(subslice, slice, e - slice), "-"), name), ".slice");
+ }
+
+ *ret = subslice;
+ return 0;
+}
+
+bool slice_name_is_valid(const char *name) {
+ const char *p, *e;
+ bool dash = false;
+
+ if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
+ return false;
+
+ if (streq(name, "-.slice"))
+ return true;
+
+ e = endswith(name, ".slice");
+ if (!e)
+ return false;
+
+ for (p = name; p < e; p++) {
+
+ if (*p == '-') {
+
+ /* Don't allow initial dash */
+ if (p == name)
+ return false;
+
+ /* Don't allow multiple dashes */
+ if (dash)
+ return false;
+
+ dash = true;
+ } else
+ dash = false;
+ }
+
+ /* Don't allow trailing hash */
+ if (dash)
+ return false;
+
+ return true;
+}
+
+static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
+ [UNIT_SERVICE] = "service",
+ [UNIT_SOCKET] = "socket",
+ [UNIT_BUSNAME] = "busname",
+ [UNIT_TARGET] = "target",
+ [UNIT_SNAPSHOT] = "snapshot",
+ [UNIT_DEVICE] = "device",
+ [UNIT_MOUNT] = "mount",
+ [UNIT_AUTOMOUNT] = "automount",
+ [UNIT_SWAP] = "swap",
+ [UNIT_TIMER] = "timer",
+ [UNIT_PATH] = "path",
+ [UNIT_SLICE] = "slice",
+ [UNIT_SCOPE] = "scope",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
+
+/// UNNEEDED by elogind
+#if 0
+static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
+ [UNIT_STUB] = "stub",
+ [UNIT_LOADED] = "loaded",
+ [UNIT_NOT_FOUND] = "not-found",
+ [UNIT_ERROR] = "error",
+ [UNIT_MERGED] = "merged",
+ [UNIT_MASKED] = "masked"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
+
+static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
+ [UNIT_REQUIRES] = "Requires",
+ [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
+ [UNIT_REQUISITE] = "Requisite",
+ [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
+ [UNIT_WANTS] = "Wants",
+ [UNIT_BINDS_TO] = "BindsTo",
+ [UNIT_PART_OF] = "PartOf",
+ [UNIT_REQUIRED_BY] = "RequiredBy",
+ [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
+ [UNIT_REQUISITE_OF] = "RequisiteOf",
+ [UNIT_REQUISITE_OF_OVERRIDABLE] = "RequisiteOfOverridable",
+ [UNIT_WANTED_BY] = "WantedBy",
+ [UNIT_BOUND_BY] = "BoundBy",
+ [UNIT_CONSISTS_OF] = "ConsistsOf",
+ [UNIT_CONFLICTS] = "Conflicts",
+ [UNIT_CONFLICTED_BY] = "ConflictedBy",
+ [UNIT_BEFORE] = "Before",
+ [UNIT_AFTER] = "After",
+ [UNIT_ON_FAILURE] = "OnFailure",
+ [UNIT_TRIGGERS] = "Triggers",
+ [UNIT_TRIGGERED_BY] = "TriggeredBy",
+ [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
+ [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom",
+ [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf",
+ [UNIT_REFERENCES] = "References",
+ [UNIT_REFERENCED_BY] = "ReferencedBy",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
+#endif // 0
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+
+#include "macro.h"
+
+#define UNIT_NAME_MAX 256
+
+typedef enum UnitType UnitType;
+// UNNEEDED typedef enum UnitLoadState UnitLoadState;
+// UNNNEEDED typedef enum UnitDependency UnitDependency;
+
+enum UnitType {
+ UNIT_SERVICE = 0,
+ UNIT_SOCKET,
+ UNIT_BUSNAME,
+ UNIT_TARGET,
+ UNIT_SNAPSHOT,
+ UNIT_DEVICE,
+ UNIT_MOUNT,
+ UNIT_AUTOMOUNT,
+ UNIT_SWAP,
+ UNIT_TIMER,
+ UNIT_PATH,
+ UNIT_SLICE,
+ UNIT_SCOPE,
+ _UNIT_TYPE_MAX,
+ _UNIT_TYPE_INVALID = -1
+};
+
+/// UNNEEDED by elogind
+#if 0
+enum UnitLoadState {
+ UNIT_STUB = 0,
+ UNIT_LOADED,
+ UNIT_NOT_FOUND,
+ UNIT_ERROR,
+ UNIT_MERGED,
+ UNIT_MASKED,
+ _UNIT_LOAD_STATE_MAX,
+ _UNIT_LOAD_STATE_INVALID = -1
+};
+
+enum UnitDependency {
+ /* Positive dependencies */
+ UNIT_REQUIRES,
+ UNIT_REQUIRES_OVERRIDABLE,
+ UNIT_REQUISITE,
+ UNIT_REQUISITE_OVERRIDABLE,
+ UNIT_WANTS,
+ UNIT_BINDS_TO,
+ UNIT_PART_OF,
+
+ /* Inverse of the above */
+ UNIT_REQUIRED_BY, /* inverse of 'requires' is 'required_by' */
+ UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'requires_overridable' is 'required_by_overridable' */
+ UNIT_REQUISITE_OF, /* inverse of 'requisite' is 'requisite_of' */
+ UNIT_REQUISITE_OF_OVERRIDABLE,/* inverse of 'requisite_overridable' is 'requisite_of_overridable' */
+ UNIT_WANTED_BY, /* inverse of 'wants' */
+ UNIT_BOUND_BY, /* inverse of 'binds_to' */
+ UNIT_CONSISTS_OF, /* inverse of 'part_of' */
+
+ /* Negative dependencies */
+ UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicted_by' */
+ UNIT_CONFLICTED_BY,
+
+ /* Order */
+ UNIT_BEFORE, /* inverse of 'before' is 'after' and vice versa */
+ UNIT_AFTER,
+
+ /* On Failure */
+ UNIT_ON_FAILURE,
+
+ /* Triggers (i.e. a socket triggers a service) */
+ UNIT_TRIGGERS,
+ UNIT_TRIGGERED_BY,
+
+ /* Propagate reloads */
+ UNIT_PROPAGATES_RELOAD_TO,
+ UNIT_RELOAD_PROPAGATED_FROM,
+
+ /* Joins namespace of */
+ UNIT_JOINS_NAMESPACE_OF,
+
+ /* Reference information for GC logic */
+ UNIT_REFERENCES, /* Inverse of 'references' is 'referenced_by' */
+ UNIT_REFERENCED_BY,
+
+ _UNIT_DEPENDENCY_MAX,
+ _UNIT_DEPENDENCY_INVALID = -1
+};
+#endif // 0
+
+typedef enum UnitNameFlags {
+ UNIT_NAME_PLAIN = 1, /* Allow foo.service */
+ UNIT_NAME_INSTANCE = 2, /* Allow foo@bar.service */
+ UNIT_NAME_TEMPLATE = 4, /* Allow foo@.service */
+ UNIT_NAME_ANY = UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE,
+} UnitNameFlags;
+
+bool unit_name_is_valid(const char *n, UnitNameFlags flags) _pure_;
+bool unit_prefix_is_valid(const char *p) _pure_;
+bool unit_instance_is_valid(const char *i) _pure_;
+bool unit_suffix_is_valid(const char *s) _pure_;
+
+/// UNNEEDED by elogind
+#if 0
+static inline int unit_prefix_and_instance_is_valid(const char *p) {
+ /* For prefix+instance and instance the same rules apply */
+ return unit_instance_is_valid(p);
+}
+#endif // 0
+
+// UNNEEDED int unit_name_to_prefix(const char *n, char **prefix);
+// UNNEEDED int unit_name_to_instance(const char *n, char **instance);
+// UNNEEDED int unit_name_to_prefix_and_instance(const char *n, char **ret);
+
+// UNNEEDED UnitType unit_name_to_type(const char *n) _pure_;
+
+// UNNEEDED int unit_name_change_suffix(const char *n, const char *suffix, char **ret);
+
+int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret);
+
+// UNNEEDED char *unit_name_escape(const char *f);
+// UNNEEDED int unit_name_unescape(const char *f, char **ret);
+// UNNEEDED int unit_name_path_escape(const char *f, char **ret);
+// UNNEEDED int unit_name_path_unescape(const char *f, char **ret);
+
+// UNNEEDED int unit_name_replace_instance(const char *f, const char *i, char **ret);
+
+// UNNEEDED int unit_name_template(const char *f, char **ret);
+
+// UNNEEDED int unit_name_from_path(const char *path, const char *suffix, char **ret);
+// UNNEEDED int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret);
+// UNNEEDED int unit_name_to_path(const char *name, char **ret);
+
+// UNNEEDED char *unit_dbus_path_from_name(const char *name);
+// UNNEEDED int unit_name_from_dbus_path(const char *path, char **name);
+
+// UNNEEDED const char* unit_dbus_interface_from_type(UnitType t);
+// UNNEEDED const char *unit_dbus_interface_from_name(const char *name);
+
+/// UNNEEDED by elogind
+#if 0
+typedef enum UnitNameMangle {
+ UNIT_NAME_NOGLOB,
+ UNIT_NAME_GLOB,
+} UnitNameMangle;
+
+int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret);
+
+static inline int unit_name_mangle(const char *name, UnitNameMangle allow_globs, char **ret) {
+ return unit_name_mangle_with_suffix(name, allow_globs, ".service", ret);
+}
+#endif // 0
+
+// UNNEEDED int slice_build_parent_slice(const char *slice, char **ret);
+int slice_build_subslice(const char *slice, const char*name, char **subslice);
+bool slice_name_is_valid(const char *name);
+
+const char *unit_type_to_string(UnitType i) _const_;
+UnitType unit_type_from_string(const char *s) _pure_;
+
+// UNNEEDED const char *unit_load_state_to_string(UnitLoadState i) _const_;
+// UNNEEDED UnitLoadState unit_load_state_from_string(const char *s) _pure_;
+
+// UNNEEDED const char *unit_dependency_to_string(UnitDependency i) _const_;
+// UNNEEDED UnitDependency unit_dependency_from_string(const char *s) _pure_;
#include "utf8.h"
#include "util.h"
-static inline bool is_unicode_valid(uint32_t ch) {
+bool unichar_is_valid(uint32_t ch) {
if (ch >= 0x110000) /* End of unicode space */
return false;
return true;
}
-static bool is_unicode_control(uint32_t ch) {
+static bool unichar_is_control(uint32_t ch) {
/*
0 to ' '-1 is the C0 range.
val = utf8_encoded_to_unichar(p);
if (val < 0 ||
- is_unicode_control(val) ||
+ unichar_is_control(val) ||
(!newline && val == '\n'))
return false;
* occupy.
*/
size_t utf8_encode_unichar(char *out_utf8, uint32_t g) {
+
if (g < (1 << 7)) {
if (out_utf8)
out_utf8[0] = g & 0x7f;
out_utf8[3] = 0x80 | (g & 0x3f);
}
return 4;
- } else {
- return 0;
}
+
+ return 0;
}
char *utf16_to_utf8(const void *s, size_t length) {
return -EINVAL;
/* check if value has valid range */
- if (!is_unicode_valid(unichar))
+ if (!unichar_is_valid(unichar))
return -EINVAL;
return len;
#define UTF8_REPLACEMENT_CHARACTER "\xef\xbf\xbd"
+bool unichar_is_valid(uint32_t c);
+
const char *utf8_is_valid(const char *s) _pure_;
char *ascii_is_valid(const char *s) _pure_;
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <signal.h>
-#include <libintl.h>
-#include <locale.h>
-#include <stdio.h>
-#include <syslog.h>
-#include <sched.h>
-#include <sys/resource.h>
-#include <linux/sched.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <dirent.h>
-#include <sys/ioctl.h>
-#include <linux/vt.h>
-#include <linux/tiocl.h>
-#include <termios.h>
-#include <stdarg.h>
-#include <poll.h>
-#include <ctype.h>
-#include <sys/prctl.h>
-#include <sys/utsname.h>
-#include <pwd.h>
-#include <netinet/ip.h>
-#include <linux/kd.h>
-#include <sys/wait.h>
-#include <sys/time.h>
-#include <glob.h>
-#include <grp.h>
-#include <sys/mman.h>
-#include <sys/vfs.h>
-#include <sys/mount.h>
-#include <linux/magic.h>
-#include <limits.h>
-#include <langinfo.h>
-#include <locale.h>
-#include <sys/personality.h>
-#include <sys/xattr.h>
-#include <sys/statvfs.h>
-#include <sys/file.h>
-#include <linux/fs.h>
+// #include <string.h>
+// #include <unistd.h>
+#include <errno.h>
+// #include <stdlib.h>
+// #include <signal.h>
+// #include <libintl.h>
+// #include <stdio.h>
+// #include <syslog.h>
+// #include <sched.h>
+// #include <sys/resource.h>
+// #include <linux/sched.h>
+// #include <sys/types.h>
+// #include <sys/stat.h>
+// #include <fcntl.h>
+// #include <dirent.h>
+// #include <sys/ioctl.h>
+// #include <stdarg.h>
+#include <poll.h>
+// #include <ctype.h>
+#include <sys/prctl.h>
+// #include <sys/utsname.h>
+#include <pwd.h>
+#include <netinet/ip.h>
+// #include <sys/wait.h>
+// #include <sys/time.h>
+// #include <glob.h>
+#include <grp.h>
+// #include <sys/mman.h>
+// #include <sys/vfs.h>
+// #include <sys/mount.h>
+#include <linux/magic.h>
+// #include <limits.h>
+#include <langinfo.h>
+// #include <locale.h>
+// #include <sys/personality.h>
+#include <sys/xattr.h>
+// #include <sys/statvfs.h>
+// #include <sys/file.h>
+#include <linux/fs.h>
/* When we include libgen.h because we need dirname() we immediately
- * undefine basename() since libgen.h defines it as a macro to the XDG
- * version which is really broken. */
-#include <libgen.h>
-#undef basename
+ * undefine basename() since libgen.h defines it as a macro to the POSIX
+ * version which is really broken. We prefer GNU basename(). */
+// #include <libgen.h>
+// #undef basename
#ifdef HAVE_SYS_AUXV_H
#include <sys/auxv.h>
#endif
-#include "config.h"
-#include "macro.h"
-#include "util.h"
-#include "ioprio.h"
-#include "missing.h"
-#include "log.h"
-#include "strv.h"
-#include "mkdir.h"
-#include "path-util.h"
-#include "exit-status.h"
-#include "hashmap.h"
-#include "env-util.h"
-#include "fileio.h"
-#include "device-nodes.h"
-#include "utf8.h"
-#include "gunicode.h"
-#include "virt.h"
-#include "def.h"
-#include "sparse-endian.h"
+#include "config.h"
+#include "macro.h"
+#include "util.h"
+// #include "ioprio.h"
+// #include "missing.h"
+// #include "log.h"
+#include "strv.h"
+#include "mkdir.h"
+#include "path-util.h"
+// #include "exit-status.h"
+#include "hashmap.h"
+#include "set.h"
+// #include "env-util.h"
+#include "fileio.h"
+// #include "device-nodes.h"
+#include "utf8.h"
+#include "gunicode.h"
+#include "virt.h"
+// #include "def.h"
+#include "sparse-endian.h"
+// #include "formats-util.h"
+#include "process-util.h"
+#include "random-util.h"
+// #include "terminal-util.h"
+#include "hostname-util.h"
+#include "signal-util.h"
/* Put this test here for a lack of better place */
assert_cc(EAGAIN == EWOULDBLOCK);
int saved_argc = 0;
char **saved_argv = NULL;
-static volatile unsigned cached_columns = 0;
-static volatile unsigned cached_lines = 0;
-
size_t page_size(void) {
static thread_local size_t pgsz = 0;
long r;
return pgsz;
}
-bool streq_ptr(const char *a, const char *b) {
-
- /* Like streq(), but tries to make sense of NULL pointers */
+int strcmp_ptr(const char *a, const char *b) {
+ /* Like strcmp(), but tries to make sense of NULL pointers */
if (a && b)
- return streq(a, b);
+ return strcmp(a, b);
- if (!a && !b)
- return true;
+ if (!a && b)
+ return -1;
- return false;
+ if (a && !b)
+ return 1;
+
+ return 0;
+}
+
+bool streq_ptr(const char *a, const char *b) {
+ return strcmp_ptr(a, b) == 0;
}
char* endswith(const char *s, const char *postfix) {
return (char*) s + sl - pl;
}
+char* endswith_no_case(const char *s, const char *postfix) {
+ size_t sl, pl;
+
+ assert(s);
+ assert(postfix);
+
+ sl = strlen(s);
+ pl = strlen(postfix);
+
+ if (pl == 0)
+ return (char*) s + sl;
+
+ if (sl < pl)
+ return NULL;
+
+ if (strcasecmp(s + sl - pl, postfix) != 0)
+ return NULL;
+
+ return (char*) s + sl - pl;
+}
+
char* first_word(const char *s, const char *word) {
size_t sl, wl;
const char *p;
return (char*) p;
}
-static size_t cescape_char(char c, char *buf) {
+size_t cescape_char(char c, char *buf) {
char * buf_old = buf;
switch (c) {
return 0;
}
+bool uid_is_valid(uid_t uid) {
+
+ /* Some libc APIs use UID_INVALID as special placeholder */
+ if (uid == (uid_t) 0xFFFFFFFF)
+ return false;
+
+ /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
+ if (uid == (uid_t) 0xFFFF)
+ return false;
+
+ return true;
+}
+
int parse_uid(const char *s, uid_t* ret_uid) {
unsigned long ul = 0;
uid_t uid;
int r;
assert(s);
- assert(ret_uid);
r = safe_atolu(s, &ul);
if (r < 0)
if ((unsigned long) uid != ul)
return -ERANGE;
- /* Some libc APIs use UID_INVALID as special placeholder */
- if (uid == (uid_t) 0xFFFFFFFF)
- return -ENXIO;
+ if (!uid_is_valid(uid))
+ return -ENXIO; /* we return ENXIO instead of EINVAL
+ * here, to make it easy to distuingish
+ * invalid numeric uids invalid
+ * strings. */
- /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
- if (uid == (uid_t) 0xFFFF)
- return -ENXIO;
+ if (ret_uid)
+ *ret_uid = uid;
- *ret_uid = uid;
return 0;
}
char quotechars[2] = {*current, '\0'};
*l = strcspn_escaped(current + 1, quotechars);
- if (current[*l + 1] == '\0' ||
+ if (current[*l + 1] == '\0' || current[*l + 1] != quotechars[0] ||
(current[*l + 2] && !strchr(separator, current[*l + 2]))) {
/* right quote missing or garbage at the end */
*state = current;
return NULL;
}
- assert(current[*l + 1] == quotechars[0]);
*state = current++ + *l + 2;
} else if (quoted) {
*l = strcspn_escaped(current, separator);
return current;
}
-int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
- int r;
- _cleanup_free_ char *line = NULL;
- long unsigned ppid;
- const char *p;
-
- assert(pid >= 0);
- assert(_ppid);
-
- if (pid == 0) {
- *_ppid = getppid();
- return 0;
- }
-
- p = procfs_file_alloca(pid, "stat");
- r = read_one_line_file(p, &line);
- if (r < 0)
- return r;
-
- /* Let's skip the pid and comm fields. The latter is enclosed
- * in () but does not escape any () in its value, so let's
- * skip over it manually */
-
- p = strrchr(line, ')');
- if (!p)
- return -EIO;
-
- p++;
-
- if (sscanf(p, " "
- "%*c " /* state */
- "%lu ", /* ppid */
- &ppid) != 1)
- return -EIO;
-
- if ((long unsigned) (pid_t) ppid != ppid)
- return -ERANGE;
-
- *_ppid = (pid_t) ppid;
-
- return 0;
-}
-
int fchmod_umask(int fd, mode_t m) {
mode_t u;
int r;
return s;
}
-int get_process_state(pid_t pid) {
- const char *p;
- char state;
- int r;
- _cleanup_free_ char *line = NULL;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "stat");
- r = read_one_line_file(p, &line);
- if (r < 0)
- return r;
-
- p = strrchr(line, ')');
- if (!p)
- return -EIO;
-
- p++;
-
- if (sscanf(p, " %c", &state) != 1)
- return -EIO;
-
- return (unsigned char) state;
-}
-
-int get_process_comm(pid_t pid, char **name) {
- const char *p;
- int r;
-
- assert(name);
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "comm");
-
- r = read_one_line_file(p, name);
- if (r == -ENOENT)
- return -ESRCH;
-
- return r;
-}
-
-int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
- _cleanup_fclose_ FILE *f = NULL;
- char *r = NULL, *k;
- const char *p;
- int c;
-
- assert(line);
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "cmdline");
-
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- if (max_length == 0) {
- size_t len = 0, allocated = 0;
-
- while ((c = getc(f)) != EOF) {
-
- if (!GREEDY_REALLOC(r, allocated, len+2)) {
- free(r);
- return -ENOMEM;
- }
-
- r[len++] = isprint(c) ? c : ' ';
- }
-
- if (len > 0)
- r[len-1] = 0;
-
- } else {
- bool space = false;
- size_t left;
-
- r = new(char, max_length);
- if (!r)
- return -ENOMEM;
-
- k = r;
- left = max_length;
- while ((c = getc(f)) != EOF) {
-
- if (isprint(c)) {
- if (space) {
- if (left <= 4)
- break;
-
- *(k++) = ' ';
- left--;
- space = false;
- }
-
- if (left <= 4)
- break;
-
- *(k++) = (char) c;
- left--;
- } else
- space = true;
- }
-
- if (left <= 4) {
- size_t n = MIN(left-1, 3U);
- memcpy(k, "...", n);
- k[n] = 0;
- } else
- *k = 0;
- }
-
- /* Kernel threads have no argv[] */
- if (isempty(r)) {
- _cleanup_free_ char *t = NULL;
- int h;
-
- free(r);
-
- if (!comm_fallback)
- return -ENOENT;
-
- h = get_process_comm(pid, &t);
- if (h < 0)
- return h;
-
- r = strjoin("[", t, "]", NULL);
- if (!r)
- return -ENOMEM;
- }
-
- *line = r;
- return 0;
-}
-
-int is_kernel_thread(pid_t pid) {
- const char *p;
- size_t count;
- char c;
- bool eof;
- FILE *f;
-
- if (pid == 0)
- return 0;
-
- assert(pid > 0);
-
- p = procfs_file_alloca(pid, "cmdline");
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- count = fread(&c, 1, 1, f);
- eof = feof(f);
- fclose(f);
-
- /* Kernel threads have an empty cmdline */
-
- if (count <= 0)
- return eof ? 1 : -errno;
-
- return 0;
-}
-
-int get_process_capeff(pid_t pid, char **capeff) {
- const char *p;
-
- assert(capeff);
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "status");
-
- return get_status_field(p, "\nCapEff:", capeff);
-}
-
-static int get_process_link_contents(const char *proc_file, char **name) {
- int r;
-
- assert(proc_file);
- assert(name);
-
- r = readlink_malloc(proc_file, name);
- if (r < 0)
- return r == -ENOENT ? -ESRCH : r;
-
- return 0;
-}
-
-int get_process_exe(pid_t pid, char **name) {
- const char *p;
- char *d;
- int r;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "exe");
- r = get_process_link_contents(p, name);
- if (r < 0)
- return r;
-
- d = endswith(*name, " (deleted)");
- if (d)
- *d = '\0';
-
- return 0;
-}
-
-static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
- _cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX];
- const char *p;
-
- assert(field);
- assert(uid);
-
- if (pid == 0)
- return getuid();
-
- p = procfs_file_alloca(pid, "status");
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- FOREACH_LINE(line, f, return -errno) {
- char *l;
-
- l = strstrip(line);
-
- if (startswith(l, field)) {
- l += strlen(field);
- l += strspn(l, WHITESPACE);
-
- l[strcspn(l, WHITESPACE)] = 0;
-
- return parse_uid(l, uid);
- }
- }
-
- return -EIO;
-}
-
-int get_process_uid(pid_t pid, uid_t *uid) {
- return get_process_id(pid, "Uid:", uid);
-}
-
-int get_process_gid(pid_t pid, gid_t *gid) {
- assert_cc(sizeof(uid_t) == sizeof(gid_t));
- return get_process_id(pid, "Gid:", gid);
-}
-
-int get_process_cwd(pid_t pid, char **cwd) {
- const char *p;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "cwd");
-
- return get_process_link_contents(p, cwd);
-}
-
-int get_process_root(pid_t pid, char **root) {
- const char *p;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "root");
-
- return get_process_link_contents(p, root);
-}
-
-int get_process_environ(pid_t pid, char **env) {
- _cleanup_fclose_ FILE *f = NULL;
- _cleanup_free_ char *outcome = NULL;
- int c;
- const char *p;
- size_t allocated = 0, sz = 0;
-
- assert(pid >= 0);
- assert(env);
-
- p = procfs_file_alloca(pid, "environ");
-
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- while ((c = fgetc(f)) != EOF) {
- if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
- return -ENOMEM;
-
- if (c == '\0')
- outcome[sz++] = '\n';
- else
- sz += cescape_char(c, outcome + sz);
- }
-
- outcome[sz] = '\0';
- *env = outcome;
- outcome = NULL;
-
- return 0;
-}
-
char *strnappend(const char *s, const char *suffix, size_t b) {
size_t a;
char *r;
return readlinkat_malloc(AT_FDCWD, p, ret);
}
+/// UNNEEDED by elogind
+#if 0
int readlink_value(const char *p, char **ret) {
_cleanup_free_ char *link = NULL;
char *value;
return 0;
}
+#endif // 0
int readlink_and_make_absolute(const char *p, char **r) {
_cleanup_free_ char *target = NULL;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int readlink_and_canonicalize(const char *p, char **r) {
char *t, *s;
int j;
return 0;
}
+#endif // 0
-int reset_all_signal_handlers(void) {
- int sig, r = 0;
+char *strstrip(char *s) {
+ char *e;
- for (sig = 1; sig < _NSIG; sig++) {
- struct sigaction sa = {
- .sa_handler = SIG_DFL,
- .sa_flags = SA_RESTART,
- };
+ /* Drops trailing whitespace. Modifies the string in
+ * place. Returns pointer to first non-space character */
- /* These two cannot be caught... */
- if (sig == SIGKILL || sig == SIGSTOP)
- continue;
+ s += strspn(s, WHITESPACE);
- /* On Linux the first two RT signals are reserved by
- * glibc, and sigaction() will return EINVAL for them. */
- if ((sigaction(sig, &sa, NULL) < 0))
- if (errno != EINVAL && r == 0)
- r = -errno;
- }
+ for (e = strchr(s, 0); e > s; e --)
+ if (!strchr(WHITESPACE, e[-1]))
+ break;
- return r;
-}
-
-int reset_signal_mask(void) {
- sigset_t ss;
-
- if (sigemptyset(&ss) < 0)
- return -errno;
-
- if (sigprocmask(SIG_SETMASK, &ss, NULL) < 0)
- return -errno;
-
- return 0;
-}
-
-char *strstrip(char *s) {
- char *e;
-
- /* Drops trailing whitespace. Modifies the string in
- * place. Returns pointer to first non-space character */
-
- s += strspn(s, WHITESPACE);
-
- for (e = strchr(s, 0); e > s; e --)
- if (!strchr(WHITESPACE, e[-1]))
- break;
-
- *e = 0;
-
- return s;
+ *e = 0;
+
+ return s;
}
+/// UNNEEDED by elogind
+#if 0
char *delete_chars(char *s, const char *bad) {
char *f, *t;
return s;
}
+#endif // 0
char *file_in_same_dir(const char *path, const char *filename) {
char *e, *ret;
return ret;
}
+/// UNNEEDED by elogind
+#if 0
int rmdir_parents(const char *path, const char *stop) {
size_t l;
int r = 0;
return 0;
}
+#endif // 0
char hexchar(int x) {
static const char table[16] = "0123456789abcdef";
return r;
}
-void *unhexmem(const char *p, size_t l) {
- uint8_t *r, *z;
+int unhexmem(const char *p, size_t l, void **mem, size_t *len) {
+ _cleanup_free_ uint8_t *r = NULL;
+ uint8_t *z;
const char *x;
+ assert(mem);
+ assert(len);
assert(p);
z = r = malloc((l + 1) / 2 + 1);
if (!r)
- return NULL;
+ return -ENOMEM;
for (x = p; x < p + l; x += 2) {
int a, b;
a = unhexchar(x[0]);
- if (x+1 < p + l)
+ if (a < 0)
+ return a;
+ else if (x+1 < p + l) {
b = unhexchar(x[1]);
- else
+ if (b < 0)
+ return b;
+ } else
b = 0;
*(z++) = (uint8_t) a << 4 | (uint8_t) b;
}
*z = 0;
- return r;
+
+ *mem = r;
+ r = NULL;
+ *len = (l + 1) / 2;
+
+ return 0;
}
-char octchar(int x) {
- return '0' + (x & 7);
+/* https://tools.ietf.org/html/rfc4648#section-6
+ * Notice that base32hex differs from base32 in the alphabet it uses.
+ * The distinction is that the base32hex representation preserves the
+ * order of the underlying data when compared as bytestrings, this is
+ * useful when representing NSEC3 hashes, as one can then verify the
+ * order of hashes directly from their representation. */
+char base32hexchar(int x) {
+ static const char table[32] = "0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUV";
+
+ return table[x & 31];
}
-int unoctchar(char c) {
+int unbase32hexchar(char c) {
+ unsigned offset;
- if (c >= '0' && c <= '7')
+ if (c >= '0' && c <= '9')
return c - '0';
+ offset = '9' - '0' + 1;
+
+ if (c >= 'A' && c <= 'V')
+ return c - 'A' + offset;
+
return -EINVAL;
}
-char decchar(int x) {
- return '0' + (x % 10);
-}
+char *base32hexmem(const void *p, size_t l, bool padding) {
+ char *r, *z;
+ const uint8_t *x;
+ size_t len;
+
+ if (padding)
+ /* five input bytes makes eight output bytes, padding is added so we must round up */
+ len = 8 * (l + 4) / 5;
+ else {
+ /* same, but round down as there is no padding */
+ len = 8 * l / 5;
+
+ switch (l % 5) {
+ case 4:
+ len += 7;
+ break;
+ case 3:
+ len += 5;
+ break;
+ case 2:
+ len += 4;
+ break;
+ case 1:
+ len += 2;
+ break;
+ }
+ }
-int undecchar(char c) {
+ z = r = malloc(len + 1);
+ if (!r)
+ return NULL;
- if (c >= '0' && c <= '9')
- return c - '0';
+ for (x = p; x < (const uint8_t*) p + (l / 5) * 5; x += 5) {
+ /* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ
+ x[3] == QQQQQQQQ; x[4] == WWWWWWWW */
+ *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
+ *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
+ *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
+ *(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */
+ *(z++) = base32hexchar((x[2] & 15) << 1 | x[3] >> 7); /* 000ZZZZQ */
+ *(z++) = base32hexchar((x[3] & 127) >> 2); /* 000QQQQQ */
+ *(z++) = base32hexchar((x[3] & 3) << 3 | x[4] >> 5); /* 000QQWWW */
+ *(z++) = base32hexchar((x[4] & 31)); /* 000WWWWW */
+ }
+
+ switch (l % 5) {
+ case 4:
+ *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
+ *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
+ *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
+ *(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */
+ *(z++) = base32hexchar((x[2] & 15) << 1 | x[3] >> 7); /* 000ZZZZQ */
+ *(z++) = base32hexchar((x[3] & 127) >> 2); /* 000QQQQQ */
+ *(z++) = base32hexchar((x[3] & 3) << 3); /* 000QQ000 */
+ if (padding)
+ *(z++) = '=';
- return -EINVAL;
-}
+ break;
-char *cescape(const char *s) {
- char *r, *t;
- const char *f;
+ case 3:
+ *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
+ *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
+ *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
+ *(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */
+ *(z++) = base32hexchar((x[2] & 15) << 1); /* 000ZZZZ0 */
+ if (padding) {
+ *(z++) = '=';
+ *(z++) = '=';
+ *(z++) = '=';
+ }
- assert(s);
+ break;
- /* Does C style string escaping. */
+ case 2:
+ *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
+ *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
+ *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
+ *(z++) = base32hexchar((x[1] & 1) << 4); /* 000Y0000 */
+ if (padding) {
+ *(z++) = '=';
+ *(z++) = '=';
+ *(z++) = '=';
+ *(z++) = '=';
+ }
- r = new(char, strlen(s)*4 + 1);
- if (!r)
- return NULL;
+ break;
- for (f = s, t = r; *f; f++)
- t += cescape_char(*f, t);
+ case 1:
+ *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
+ *(z++) = base32hexchar((x[0] & 7) << 2); /* 000XXX00 */
+ if (padding) {
+ *(z++) = '=';
+ *(z++) = '=';
+ *(z++) = '=';
+ *(z++) = '=';
+ *(z++) = '=';
+ *(z++) = '=';
+ }
- *t = 0;
+ break;
+ }
+ *z = 0;
return r;
}
-static int cunescape_one(const char *p, size_t length, char *ret) {
- int r = 1;
+int unbase32hexmem(const char *p, size_t l, bool padding, void **mem, size_t *_len) {
+ _cleanup_free_ uint8_t *r = NULL;
+ int a, b, c, d, e, f, g, h;
+ uint8_t *z;
+ const char *x;
+ size_t len;
+ unsigned pad = 0;
assert(p);
- assert(*p);
- assert(ret);
- if (length != (size_t) -1 && length < 1)
+ /* padding ensures any base32hex input has input divisible by 8 */
+ if (padding && l % 8 != 0)
return -EINVAL;
- switch (p[0]) {
+ if (padding) {
+ /* strip the padding */
+ while (l > 0 && p[l - 1] == '=' && pad < 7) {
+ pad ++;
+ l --;
+ }
+ }
- case 'a':
- *ret = '\a';
- break;
- case 'b':
- *ret = '\b';
- break;
- case 'f':
- *ret = '\f';
- break;
- case 'n':
- *ret = '\n';
- break;
- case 'r':
- *ret = '\r';
- break;
- case 't':
- *ret = '\t';
+ /* a group of eight input bytes needs five output bytes, in case of
+ padding we need to add some extra bytes */
+ len = (l / 8) * 5;
+
+ switch (l % 8) {
+ case 7:
+ len += 4;
break;
- case 'v':
- *ret = '\v';
+ case 5:
+ len += 3;
break;
- case '\\':
- *ret = '\\';
+ case 4:
+ len += 2;
break;
- case '"':
- *ret = '"';
+ case 2:
+ len += 1;
break;
- case '\'':
- *ret = '\'';
+ case 0:
break;
+ default:
+ return -EINVAL;
+ }
- case 's':
- /* This is an extension of the XDG syntax files */
- *ret = ' ';
- break;
+ z = r = malloc(len + 1);
+ if (!r)
+ return -ENOMEM;
- case 'x': {
- /* hexadecimal encoding */
- int a, b;
+ for (x = p; x < p + (l / 8) * 8; x += 8) {
+ /* a == 000XXXXX; b == 000YYYYY; c == 000ZZZZZ; d == 000WWWWW
+ e == 000SSSSS; f == 000QQQQQ; g == 000VVVVV; h == 000RRRRR */
+ a = unbase32hexchar(x[0]);
+ if (a < 0)
+ return -EINVAL;
- if (length != (size_t) -1 && length < 3)
+ b = unbase32hexchar(x[1]);
+ if (b < 0)
return -EINVAL;
- a = unhexchar(p[1]);
+ c = unbase32hexchar(x[2]);
+ if (c < 0)
+ return -EINVAL;
+
+ d = unbase32hexchar(x[3]);
+ if (d < 0)
+ return -EINVAL;
+
+ e = unbase32hexchar(x[4]);
+ if (e < 0)
+ return -EINVAL;
+
+ f = unbase32hexchar(x[5]);
+ if (f < 0)
+ return -EINVAL;
+
+ g = unbase32hexchar(x[6]);
+ if (g < 0)
+ return -EINVAL;
+
+ h = unbase32hexchar(x[7]);
+ if (h < 0)
+ return -EINVAL;
+
+ *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
+ *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
+ *(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */
+ *(z++) = (uint8_t) e << 7 | (uint8_t) f << 2 | (uint8_t) g >> 3; /* SQQQQQVV */
+ *(z++) = (uint8_t) g << 5 | (uint8_t) h; /* VVVRRRRR */
+ }
+
+ switch (l % 8) {
+ case 7:
+ a = unbase32hexchar(x[0]);
if (a < 0)
return -EINVAL;
- b = unhexchar(p[2]);
+ b = unbase32hexchar(x[1]);
if (b < 0)
return -EINVAL;
- /* don't allow NUL bytes */
- if (a == 0 && b == 0)
+ c = unbase32hexchar(x[2]);
+ if (c < 0)
return -EINVAL;
- *ret = (char) ((a << 4) | b);
- r = 3;
+ d = unbase32hexchar(x[3]);
+ if (d < 0)
+ return -EINVAL;
+
+ e = unbase32hexchar(x[4]);
+ if (e < 0)
+ return -EINVAL;
+
+ f = unbase32hexchar(x[5]);
+ if (f < 0)
+ return -EINVAL;
+
+ g = unbase32hexchar(x[6]);
+ if (g < 0)
+ return -EINVAL;
+
+ /* g == 000VV000 */
+ if (g & 7)
+ return -EINVAL;
+
+ *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
+ *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
+ *(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */
+ *(z++) = (uint8_t) e << 7 | (uint8_t) f << 2 | (uint8_t) g >> 3; /* SQQQQQVV */
+
break;
- }
+ case 5:
+ a = unbase32hexchar(x[0]);
+ if (a < 0)
+ return -EINVAL;
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7': {
- /* octal encoding */
- int a, b, c, m;
+ b = unbase32hexchar(x[1]);
+ if (b < 0)
+ return -EINVAL;
- if (length != (size_t) -1 && length < 4)
+ c = unbase32hexchar(x[2]);
+ if (c < 0)
return -EINVAL;
- a = unoctchar(p[0]);
+ d = unbase32hexchar(x[3]);
+ if (d < 0)
+ return -EINVAL;
+
+ e = unbase32hexchar(x[4]);
+ if (e < 0)
+ return -EINVAL;
+
+ /* e == 000SSSS0 */
+ if (e & 1)
+ return -EINVAL;
+
+ *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
+ *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
+ *(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */
+
+ break;
+ case 4:
+ a = unbase32hexchar(x[0]);
if (a < 0)
return -EINVAL;
- b = unoctchar(p[1]);
+ b = unbase32hexchar(x[1]);
if (b < 0)
return -EINVAL;
- c = unoctchar(p[2]);
+ c = unbase32hexchar(x[2]);
if (c < 0)
return -EINVAL;
- /* don't allow NUL bytes */
- if (a == 0 && b == 0 && c == 0)
+ d = unbase32hexchar(x[3]);
+ if (d < 0)
return -EINVAL;
- /* Don't allow bytes above 255 */
- m = (a << 6) | (b << 3) | c;
- if (m > 255)
+ /* d == 000W0000 */
+ if (d & 15)
return -EINVAL;
- *ret = (char) m;
- r = 3;
+ *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
+ *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
+
break;
- }
+ case 2:
+ a = unbase32hexchar(x[0]);
+ if (a < 0)
+ return -EINVAL;
+
+ b = unbase32hexchar(x[1]);
+ if (b < 0)
+ return -EINVAL;
+ /* b == 000YYY00 */
+ if (b & 3)
+ return -EINVAL;
+
+ *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
+
+ break;
+ case 0:
+ break;
default:
return -EINVAL;
}
- return r;
-}
-
-char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix) {
- char *r, *t;
- const char *f;
- size_t pl;
+ *z = 0;
- assert(s);
+ *mem = r;
+ r = NULL;
+ *_len = len;
- /* Undoes C style string escaping, and optionally prefixes it. */
+ return 0;
+}
- pl = prefix ? strlen(prefix) : 0;
+/* https://tools.ietf.org/html/rfc4648#section-4 */
+char base64char(int x) {
+ static const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+ return table[x & 63];
+}
- r = new(char, pl+length+1);
- if (!r)
- return NULL;
+int unbase64char(char c) {
+ unsigned offset;
- if (prefix)
- memcpy(r, prefix, pl);
+ if (c >= 'A' && c <= 'Z')
+ return c - 'A';
- for (f = s, t = r + pl; f < s + length; f++) {
- size_t remaining;
- int k;
+ offset = 'Z' - 'A' + 1;
- remaining = s + length - f;
- assert(remaining > 0);
+ if (c >= 'a' && c <= 'z')
+ return c - 'a' + offset;
- if (*f != '\\' || remaining == 1) {
- /* a literal literal, or a trailing backslash, copy verbatim */
- *(t++) = *f;
- continue;
- }
+ offset += 'z' - 'a' + 1;
- k = cunescape_one(f + 1, remaining - 1, t);
- if (k < 0) {
- /* Invalid escape code, let's take it literal then */
- *(t++) = '\\';
- continue;
- }
+ if (c >= '0' && c <= '9')
+ return c - '0' + offset;
- f += k;
- t++;
- }
+ offset += '9' - '0' + 1;
- *t = 0;
- return r;
-}
+ if (c == '+')
+ return offset;
-char *cunescape_length(const char *s, size_t length) {
- return cunescape_length_with_prefix(s, length, NULL);
-}
+ offset ++;
-char *cunescape(const char *s) {
- assert(s);
+ if (c == '/')
+ return offset;
- return cunescape_length(s, strlen(s));
+ return -EINVAL;
}
-char *xescape(const char *s, const char *bad) {
- char *r, *t;
- const char *f;
-
- /* Escapes all chars in bad, in addition to \ and all special
- * chars, in \xFF style escaping. May be reversed with
- * cunescape. */
+char *base64mem(const void *p, size_t l) {
+ char *r, *z;
+ const uint8_t *x;
- r = new(char, strlen(s) * 4 + 1);
+ /* three input bytes makes four output bytes, padding is added so we must round up */
+ z = r = malloc(4 * (l + 2) / 3 + 1);
if (!r)
return NULL;
- for (f = s, t = r; *f; f++) {
+ for (x = p; x < (const uint8_t*) p + (l / 3) * 3; x += 3) {
+ /* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ */
+ *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
+ *(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */
+ *(z++) = base64char((x[1] & 15) << 2 | x[2] >> 6); /* 00YYYYZZ */
+ *(z++) = base64char(x[2] & 63); /* 00ZZZZZZ */
+ }
- if ((*f < ' ') || (*f >= 127) ||
- (*f == '\\') || strchr(bad, *f)) {
- *(t++) = '\\';
- *(t++) = 'x';
- *(t++) = hexchar(*f >> 4);
- *(t++) = hexchar(*f);
- } else
- *(t++) = *f;
- }
-
- *t = 0;
-
- return r;
-}
-
-char *ascii_strlower(char *t) {
- char *p;
-
- assert(t);
-
- for (p = t; *p; p++)
- if (*p >= 'A' && *p <= 'Z')
- *p = *p - 'A' + 'a';
-
- return t;
-}
-
-_pure_ static bool hidden_file_allow_backup(const char *filename) {
- assert(filename);
-
- return
- filename[0] == '.' ||
- streq(filename, "lost+found") ||
- streq(filename, "aquota.user") ||
- streq(filename, "aquota.group") ||
- endswith(filename, ".rpmnew") ||
- endswith(filename, ".rpmsave") ||
- endswith(filename, ".rpmorig") ||
- endswith(filename, ".dpkg-old") ||
- endswith(filename, ".dpkg-new") ||
- endswith(filename, ".dpkg-tmp") ||
- endswith(filename, ".dpkg-dist") ||
- endswith(filename, ".dpkg-bak") ||
- endswith(filename, ".dpkg-backup") ||
- endswith(filename, ".dpkg-remove") ||
- endswith(filename, ".swp");
-}
-
-bool hidden_file(const char *filename) {
- assert(filename);
-
- if (endswith(filename, "~"))
- return true;
-
- return hidden_file_allow_backup(filename);
-}
-
-int fd_nonblock(int fd, bool nonblock) {
- int flags, nflags;
-
- assert(fd >= 0);
-
- flags = fcntl(fd, F_GETFL, 0);
- if (flags < 0)
- return -errno;
-
- if (nonblock)
- nflags = flags | O_NONBLOCK;
- else
- nflags = flags & ~O_NONBLOCK;
-
- if (nflags == flags)
- return 0;
-
- if (fcntl(fd, F_SETFL, nflags) < 0)
- return -errno;
-
- return 0;
-}
-
-int fd_cloexec(int fd, bool cloexec) {
- int flags, nflags;
-
- assert(fd >= 0);
-
- flags = fcntl(fd, F_GETFD, 0);
- if (flags < 0)
- return -errno;
-
- if (cloexec)
- nflags = flags | FD_CLOEXEC;
- else
- nflags = flags & ~FD_CLOEXEC;
-
- if (nflags == flags)
- return 0;
-
- if (fcntl(fd, F_SETFD, nflags) < 0)
- return -errno;
-
- return 0;
-}
-
-_pure_ static bool fd_in_set(int fd, const int fdset[], unsigned n_fdset) {
- unsigned i;
-
- assert(n_fdset == 0 || fdset);
-
- for (i = 0; i < n_fdset; i++)
- if (fdset[i] == fd)
- return true;
-
- return false;
-}
-
-int close_all_fds(const int except[], unsigned n_except) {
- _cleanup_closedir_ DIR *d = NULL;
- struct dirent *de;
- int r = 0;
-
- assert(n_except == 0 || except);
-
- d = opendir("/proc/self/fd");
- if (!d) {
- int fd;
- struct rlimit rl;
-
- /* When /proc isn't available (for example in chroots)
- * the fallback is brute forcing through the fd
- * table */
-
- assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0);
- for (fd = 3; fd < (int) rl.rlim_max; fd ++) {
-
- if (fd_in_set(fd, except, n_except))
- continue;
-
- if (close_nointr(fd) < 0)
- if (errno != EBADF && r == 0)
- r = -errno;
- }
-
- return r;
- }
-
- while ((de = readdir(d))) {
- int fd = -1;
-
- if (hidden_file(de->d_name))
- continue;
-
- if (safe_atoi(de->d_name, &fd) < 0)
- /* Let's better ignore this, just in case */
- continue;
-
- if (fd < 3)
- continue;
-
- if (fd == dirfd(d))
- continue;
-
- if (fd_in_set(fd, except, n_except))
- continue;
-
- if (close_nointr(fd) < 0) {
- /* Valgrind has its own FD and doesn't want to have it closed */
- if (errno != EBADF && r == 0)
- r = -errno;
- }
- }
-
- return r;
-}
-
-bool chars_intersect(const char *a, const char *b) {
- const char *p;
-
- /* Returns true if any of the chars in a are in b. */
- for (p = a; *p; p++)
- if (strchr(b, *p))
- return true;
-
- return false;
-}
-
-bool fstype_is_network(const char *fstype) {
- static const char table[] =
- "afs\0"
- "cifs\0"
- "smbfs\0"
- "sshfs\0"
- "ncpfs\0"
- "ncp\0"
- "nfs\0"
- "nfs4\0"
- "gfs\0"
- "gfs2\0"
- "glusterfs\0";
-
- const char *x;
-
- x = startswith(fstype, "fuse.");
- if (x)
- fstype = x;
-
- return nulstr_contains(table, fstype);
-}
-
-int chvt(int vt) {
- _cleanup_close_ int fd;
-
- fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- if (vt < 0) {
- int tiocl[2] = {
- TIOCL_GETKMSGREDIRECT,
- 0
- };
-
- if (ioctl(fd, TIOCLINUX, tiocl) < 0)
- return -errno;
-
- vt = tiocl[0] <= 0 ? 1 : tiocl[0];
- }
-
- if (ioctl(fd, VT_ACTIVATE, vt) < 0)
- return -errno;
-
- return 0;
-}
-
-int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
- struct termios old_termios, new_termios;
- char c, line[LINE_MAX];
-
- assert(f);
- assert(ret);
-
- if (tcgetattr(fileno(f), &old_termios) >= 0) {
- new_termios = old_termios;
-
- new_termios.c_lflag &= ~ICANON;
- new_termios.c_cc[VMIN] = 1;
- new_termios.c_cc[VTIME] = 0;
-
- if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
- size_t k;
-
- if (t != USEC_INFINITY) {
- if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) {
- tcsetattr(fileno(f), TCSADRAIN, &old_termios);
- return -ETIMEDOUT;
- }
- }
-
- k = fread(&c, 1, 1, f);
-
- tcsetattr(fileno(f), TCSADRAIN, &old_termios);
-
- if (k <= 0)
- return -EIO;
-
- if (need_nl)
- *need_nl = c != '\n';
-
- *ret = c;
- return 0;
- }
- }
-
- if (t != USEC_INFINITY) {
- if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0)
- return -ETIMEDOUT;
- }
-
- errno = 0;
- if (!fgets(line, sizeof(line), f))
- return errno ? -errno : -EIO;
-
- truncate_nl(line);
-
- if (strlen(line) != 1)
- return -EBADMSG;
-
- if (need_nl)
- *need_nl = false;
-
- *ret = line[0];
- return 0;
-}
-
-int ask_char(char *ret, const char *replies, const char *text, ...) {
- int r;
-
- assert(ret);
- assert(replies);
- assert(text);
-
- for (;;) {
- va_list ap;
- char c;
- bool need_nl = true;
-
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_ON, stdout);
-
- va_start(ap, text);
- vprintf(text, ap);
- va_end(ap);
-
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_OFF, stdout);
-
- fflush(stdout);
-
- r = read_one_char(stdin, &c, USEC_INFINITY, &need_nl);
- if (r < 0) {
-
- if (r == -EBADMSG) {
- puts("Bad input, please try again.");
- continue;
- }
-
- putchar('\n');
- return r;
- }
-
- if (need_nl)
- putchar('\n');
-
- if (strchr(replies, c)) {
- *ret = c;
- return 0;
- }
-
- puts("Read unexpected character, please try again.");
- }
-}
-
-int ask_string(char **ret, const char *text, ...) {
- assert(ret);
- assert(text);
-
- for (;;) {
- char line[LINE_MAX];
- va_list ap;
-
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_ON, stdout);
-
- va_start(ap, text);
- vprintf(text, ap);
- va_end(ap);
-
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_OFF, stdout);
-
- fflush(stdout);
-
- errno = 0;
- if (!fgets(line, sizeof(line), stdin))
- return errno ? -errno : -EIO;
-
- if (!endswith(line, "\n"))
- putchar('\n');
- else {
- char *s;
-
- if (isempty(line))
- continue;
-
- truncate_nl(line);
- s = strdup(line);
- if (!s)
- return -ENOMEM;
-
- *ret = s;
- return 0;
- }
- }
-}
-
-int reset_terminal_fd(int fd, bool switch_to_text) {
- struct termios termios;
- int r = 0;
-
- /* Set terminal to some sane defaults */
-
- assert(fd >= 0);
-
- /* We leave locked terminal attributes untouched, so that
- * Plymouth may set whatever it wants to set, and we don't
- * interfere with that. */
-
- /* Disable exclusive mode, just in case */
- ioctl(fd, TIOCNXCL);
-
- /* Switch to text mode */
- if (switch_to_text)
- ioctl(fd, KDSETMODE, KD_TEXT);
-
- /* Enable console unicode mode */
- ioctl(fd, KDSKBMODE, K_UNICODE);
-
- if (tcgetattr(fd, &termios) < 0) {
- r = -errno;
- goto finish;
- }
-
- /* We only reset the stuff that matters to the software. How
- * hardware is set up we don't touch assuming that somebody
- * else will do that for us */
-
- termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
- termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
- termios.c_oflag |= ONLCR;
- termios.c_cflag |= CREAD;
- termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
-
- termios.c_cc[VINTR] = 03; /* ^C */
- termios.c_cc[VQUIT] = 034; /* ^\ */
- termios.c_cc[VERASE] = 0177;
- termios.c_cc[VKILL] = 025; /* ^X */
- termios.c_cc[VEOF] = 04; /* ^D */
- termios.c_cc[VSTART] = 021; /* ^Q */
- termios.c_cc[VSTOP] = 023; /* ^S */
- termios.c_cc[VSUSP] = 032; /* ^Z */
- termios.c_cc[VLNEXT] = 026; /* ^V */
- termios.c_cc[VWERASE] = 027; /* ^W */
- termios.c_cc[VREPRINT] = 022; /* ^R */
- termios.c_cc[VEOL] = 0;
- termios.c_cc[VEOL2] = 0;
-
- termios.c_cc[VTIME] = 0;
- termios.c_cc[VMIN] = 1;
-
- if (tcsetattr(fd, TCSANOW, &termios) < 0)
- r = -errno;
-
-finish:
- /* Just in case, flush all crap out */
- tcflush(fd, TCIOFLUSH);
-
- return r;
-}
-
-int reset_terminal(const char *name) {
- _cleanup_close_ int fd = -1;
-
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- return reset_terminal_fd(fd, true);
-}
-
-int open_terminal(const char *name, int mode) {
- int fd, r;
- unsigned c = 0;
-
- /*
- * If a TTY is in the process of being closed opening it might
- * cause EIO. This is horribly awful, but unlikely to be
- * changed in the kernel. Hence we work around this problem by
- * retrying a couple of times.
- *
- * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
- */
-
- assert(!(mode & O_CREAT));
-
- for (;;) {
- fd = open(name, mode, 0);
- if (fd >= 0)
- break;
-
- if (errno != EIO)
- return -errno;
-
- /* Max 1s in total */
- if (c >= 20)
- return -errno;
-
- usleep(50 * USEC_PER_MSEC);
- c++;
- }
-
- r = isatty(fd);
- if (r < 0) {
- safe_close(fd);
- return -errno;
- }
-
- if (!r) {
- safe_close(fd);
- return -ENOTTY;
- }
-
- return fd;
-}
-
-int flush_fd(int fd) {
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLIN,
- };
-
- for (;;) {
- char buf[LINE_MAX];
- ssize_t l;
- int r;
-
- r = poll(&pollfd, 1, 0);
- if (r < 0) {
- if (errno == EINTR)
- continue;
-
- return -errno;
-
- } else if (r == 0)
- return 0;
-
- l = read(fd, buf, sizeof(buf));
- if (l < 0) {
-
- if (errno == EINTR)
- continue;
-
- if (errno == EAGAIN)
- return 0;
-
- return -errno;
- } else if (l == 0)
- return 0;
- }
-}
-
-int acquire_terminal(
- const char *name,
- bool fail,
- bool force,
- bool ignore_tiocstty_eperm,
- usec_t timeout) {
-
- int fd = -1, notify = -1, r = 0, wd = -1;
- usec_t ts = 0;
-
- assert(name);
-
- /* We use inotify to be notified when the tty is closed. We
- * create the watch before checking if we can actually acquire
- * it, so that we don't lose any event.
- *
- * Note: strictly speaking this actually watches for the
- * device being closed, it does *not* really watch whether a
- * tty loses its controlling process. However, unless some
- * rogue process uses TIOCNOTTY on /dev/tty *after* closing
- * its tty otherwise this will not become a problem. As long
- * as the administrator makes sure not configure any service
- * on the same tty as an untrusted user this should not be a
- * problem. (Which he probably should not do anyway.) */
-
- if (timeout != USEC_INFINITY)
- ts = now(CLOCK_MONOTONIC);
-
- if (!fail && !force) {
- notify = inotify_init1(IN_CLOEXEC | (timeout != USEC_INFINITY ? IN_NONBLOCK : 0));
- if (notify < 0) {
- r = -errno;
- goto fail;
- }
-
- wd = inotify_add_watch(notify, name, IN_CLOSE);
- if (wd < 0) {
- r = -errno;
- goto fail;
- }
- }
-
- for (;;) {
- struct sigaction sa_old, sa_new = {
- .sa_handler = SIG_IGN,
- .sa_flags = SA_RESTART,
- };
-
- if (notify >= 0) {
- r = flush_fd(notify);
- if (r < 0)
- goto fail;
- }
-
- /* We pass here O_NOCTTY only so that we can check the return
- * value TIOCSCTTY and have a reliable way to figure out if we
- * successfully became the controlling process of the tty */
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
- * if we already own the tty. */
- assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
-
- /* First, try to get the tty */
- if (ioctl(fd, TIOCSCTTY, force) < 0)
- r = -errno;
-
- assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
-
- /* Sometimes it makes sense to ignore TIOCSCTTY
- * returning EPERM, i.e. when very likely we already
- * are have this controlling terminal. */
- if (r < 0 && r == -EPERM && ignore_tiocstty_eperm)
- r = 0;
-
- if (r < 0 && (force || fail || r != -EPERM)) {
- goto fail;
- }
-
- if (r >= 0)
- break;
-
- assert(!fail);
- assert(!force);
- assert(notify >= 0);
-
- for (;;) {
- union inotify_event_buffer buffer;
- struct inotify_event *e;
- ssize_t l;
-
- if (timeout != USEC_INFINITY) {
- usec_t n;
-
- n = now(CLOCK_MONOTONIC);
- if (ts + timeout < n) {
- r = -ETIMEDOUT;
- goto fail;
- }
-
- r = fd_wait_for_event(fd, POLLIN, ts + timeout - n);
- if (r < 0)
- goto fail;
-
- if (r == 0) {
- r = -ETIMEDOUT;
- goto fail;
- }
- }
-
- l = read(notify, &buffer, sizeof(buffer));
- if (l < 0) {
- if (errno == EINTR || errno == EAGAIN)
- continue;
-
- r = -errno;
- goto fail;
- }
-
- FOREACH_INOTIFY_EVENT(e, buffer, l) {
- if (e->wd != wd || !(e->mask & IN_CLOSE)) {
- r = -EIO;
- goto fail;
- }
- }
-
- break;
- }
-
- /* We close the tty fd here since if the old session
- * ended our handle will be dead. It's important that
- * we do this after sleeping, so that we don't enter
- * an endless loop. */
- fd = safe_close(fd);
- }
-
- safe_close(notify);
-
- r = reset_terminal_fd(fd, true);
- if (r < 0)
- log_warning_errno(r, "Failed to reset terminal: %m");
-
- return fd;
-
-fail:
- safe_close(fd);
- safe_close(notify);
-
- return r;
-}
-
-int release_terminal(void) {
- static const struct sigaction sa_new = {
- .sa_handler = SIG_IGN,
- .sa_flags = SA_RESTART,
- };
-
- _cleanup_close_ int fd = -1;
- struct sigaction sa_old;
- int r = 0;
-
- fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
- * by our own TIOCNOTTY */
- assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
-
- if (ioctl(fd, TIOCNOTTY) < 0)
- r = -errno;
-
- assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
-
- return r;
-}
-
-int sigaction_many(const struct sigaction *sa, ...) {
- va_list ap;
- int r = 0, sig;
-
- va_start(ap, sa);
- while ((sig = va_arg(ap, int)) > 0)
- if (sigaction(sig, sa, NULL) < 0)
- r = -errno;
- va_end(ap);
-
- return r;
-}
-
-int ignore_signals(int sig, ...) {
- struct sigaction sa = {
- .sa_handler = SIG_IGN,
- .sa_flags = SA_RESTART,
- };
- va_list ap;
- int r = 0;
-
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
-
- va_start(ap, sig);
- while ((sig = va_arg(ap, int)) > 0)
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
- va_end(ap);
-
- return r;
-}
-
-int default_signals(int sig, ...) {
- struct sigaction sa = {
- .sa_handler = SIG_DFL,
- .sa_flags = SA_RESTART,
- };
- va_list ap;
- int r = 0;
-
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
-
- va_start(ap, sig);
- while ((sig = va_arg(ap, int)) > 0)
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
- va_end(ap);
-
- return r;
-}
-
-void safe_close_pair(int p[]) {
- assert(p);
-
- if (p[0] == p[1]) {
- /* Special case pairs which use the same fd in both
- * directions... */
- p[0] = p[1] = safe_close(p[0]);
- return;
- }
-
- p[0] = safe_close(p[0]);
- p[1] = safe_close(p[1]);
-}
-
-ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
- uint8_t *p = buf;
- ssize_t n = 0;
-
- assert(fd >= 0);
- assert(buf);
-
- while (nbytes > 0) {
- ssize_t k;
-
- k = read(fd, p, nbytes);
- if (k < 0) {
- if (errno == EINTR)
- continue;
-
- if (errno == EAGAIN && do_poll) {
-
- /* We knowingly ignore any return value here,
- * and expect that any error/EOF is reported
- * via read() */
-
- fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
- continue;
- }
-
- return n > 0 ? n : -errno;
- }
-
- if (k == 0)
- return n;
-
- p += k;
- nbytes -= k;
- n += k;
- }
-
- return n;
-}
-
-int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
- ssize_t n;
-
- n = loop_read(fd, buf, nbytes, do_poll);
- if (n < 0)
- return n;
- if ((size_t) n != nbytes)
- return -EIO;
- return 0;
-}
-
-int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
- const uint8_t *p = buf;
-
- assert(fd >= 0);
- assert(buf);
-
- errno = 0;
-
- while (nbytes > 0) {
- ssize_t k;
-
- k = write(fd, p, nbytes);
- if (k < 0) {
- if (errno == EINTR)
- continue;
-
- if (errno == EAGAIN && do_poll) {
- /* We knowingly ignore any return value here,
- * and expect that any error/EOF is reported
- * via write() */
-
- fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
- continue;
- }
-
- return -errno;
- }
-
- if (k == 0) /* Can't really happen */
- return -EIO;
-
- p += k;
- nbytes -= k;
- }
-
- return 0;
-}
+ switch (l % 3) {
+ case 2:
+ *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
+ *(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */
+ *(z++) = base64char((x[1] & 15) << 2); /* 00YYYY00 */
+ *(z++) = '=';
-int parse_size(const char *t, off_t base, off_t *size) {
-
- /* Soo, sometimes we want to parse IEC binary suffxies, and
- * sometimes SI decimal suffixes. This function can parse
- * both. Which one is the right way depends on the
- * context. Wikipedia suggests that SI is customary for
- * hardrware metrics and network speeds, while IEC is
- * customary for most data sizes used by software and volatile
- * (RAM) memory. Hence be careful which one you pick!
- *
- * In either case we use just K, M, G as suffix, and not Ki,
- * Mi, Gi or so (as IEC would suggest). That's because that's
- * frickin' ugly. But this means you really need to make sure
- * to document which base you are parsing when you use this
- * call. */
-
- struct table {
- const char *suffix;
- unsigned long long factor;
- };
-
- static const struct table iec[] = {
- { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
- { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
- { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
- { "G", 1024ULL*1024ULL*1024ULL },
- { "M", 1024ULL*1024ULL },
- { "K", 1024ULL },
- { "B", 1 },
- { "", 1 },
- };
-
- static const struct table si[] = {
- { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
- { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
- { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
- { "G", 1000ULL*1000ULL*1000ULL },
- { "M", 1000ULL*1000ULL },
- { "K", 1000ULL },
- { "B", 1 },
- { "", 1 },
- };
-
- const struct table *table;
- const char *p;
- unsigned long long r = 0;
- unsigned n_entries, start_pos = 0;
-
- assert(t);
- assert(base == 1000 || base == 1024);
- assert(size);
+ break;
+ case 1:
+ *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
+ *(z++) = base64char((x[0] & 3) << 4); /* 00XX0000 */
+ *(z++) = '=';
+ *(z++) = '=';
- if (base == 1000) {
- table = si;
- n_entries = ELEMENTSOF(si);
- } else {
- table = iec;
- n_entries = ELEMENTSOF(iec);
+ break;
}
- p = t;
- do {
- long long l;
- unsigned long long l2;
- double frac = 0;
- char *e;
- unsigned i;
+ *z = 0;
+ return r;
+}
- errno = 0;
- l = strtoll(p, &e, 10);
+int unbase64mem(const char *p, size_t l, void **mem, size_t *_len) {
+ _cleanup_free_ uint8_t *r = NULL;
+ int a, b, c, d;
+ uint8_t *z;
+ const char *x;
+ size_t len;
- if (errno > 0)
- return -errno;
+ assert(p);
- if (l < 0)
- return -ERANGE;
+ /* padding ensures any base63 input has input divisible by 4 */
+ if (l % 4 != 0)
+ return -EINVAL;
- if (e == p)
- return -EINVAL;
+ /* strip the padding */
+ if (l > 0 && p[l - 1] == '=')
+ l --;
+ if (l > 0 && p[l - 1] == '=')
+ l --;
- if (*e == '.') {
- e++;
- if (*e >= '0' && *e <= '9') {
- char *e2;
+ /* a group of four input bytes needs three output bytes, in case of
+ padding we need to add two or three extra bytes */
+ len = (l / 4) * 3 + (l % 4 ? (l % 4) - 1 : 0);
- /* strotoull itself would accept space/+/- */
- l2 = strtoull(e, &e2, 10);
+ z = r = malloc(len + 1);
+ if (!r)
+ return -ENOMEM;
- if (errno == ERANGE)
- return -errno;
+ for (x = p; x < p + (l / 4) * 4; x += 4) {
+ /* a == 00XXXXXX; b == 00YYYYYY; c == 00ZZZZZZ; d == 00WWWWWW */
+ a = unbase64char(x[0]);
+ if (a < 0)
+ return -EINVAL;
- /* Ignore failure. E.g. 10.M is valid */
- frac = l2;
- for (; e < e2; e++)
- frac /= 10;
- }
- }
+ b = unbase64char(x[1]);
+ if (b < 0)
+ return -EINVAL;
- e += strspn(e, WHITESPACE);
+ c = unbase64char(x[2]);
+ if (c < 0)
+ return -EINVAL;
- for (i = start_pos; i < n_entries; i++)
- if (startswith(e, table[i].suffix)) {
- unsigned long long tmp;
- if ((unsigned long long) l + (frac > 0) > ULLONG_MAX / table[i].factor)
- return -ERANGE;
- tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
- if (tmp > ULLONG_MAX - r)
- return -ERANGE;
+ d = unbase64char(x[3]);
+ if (d < 0)
+ return -EINVAL;
- r += tmp;
- if ((unsigned long long) (off_t) r != r)
- return -ERANGE;
+ *(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
+ *(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
+ *(z++) = (uint8_t) c << 6 | (uint8_t) d; /* ZZWWWWWW */
+ }
- p = e + strlen(table[i].suffix);
+ switch (l % 4) {
+ case 3:
+ a = unbase64char(x[0]);
+ if (a < 0)
+ return -EINVAL;
- start_pos = i + 1;
- break;
- }
+ b = unbase64char(x[1]);
+ if (b < 0)
+ return -EINVAL;
- if (i >= n_entries)
+ c = unbase64char(x[2]);
+ if (c < 0)
return -EINVAL;
- } while (*p);
+ /* c == 00ZZZZ00 */
+ if (c & 3)
+ return -EINVAL;
- *size = r;
+ *(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
+ *(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
- return 0;
-}
+ break;
+ case 2:
+ a = unbase64char(x[0]);
+ if (a < 0)
+ return -EINVAL;
-int make_stdio(int fd) {
- int r, s, t;
+ b = unbase64char(x[1]);
+ if (b < 0)
+ return -EINVAL;
- assert(fd >= 0);
+ /* b == 00YY0000 */
+ if (b & 15)
+ return -EINVAL;
- r = dup2(fd, STDIN_FILENO);
- s = dup2(fd, STDOUT_FILENO);
- t = dup2(fd, STDERR_FILENO);
+ *(z++) = (uint8_t) a << 2 | (uint8_t) (b >> 4); /* XXXXXXYY */
- if (fd >= 3)
- safe_close(fd);
+ break;
+ case 0:
- if (r < 0 || s < 0 || t < 0)
- return -errno;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ *z = 0;
- /* Explicitly unset O_CLOEXEC, since if fd was < 3, then
- * dup2() was a NOP and the bit hence possibly set. */
- fd_cloexec(STDIN_FILENO, false);
- fd_cloexec(STDOUT_FILENO, false);
- fd_cloexec(STDERR_FILENO, false);
+ *mem = r;
+ r = NULL;
+ *_len = len;
return 0;
}
-int make_null_stdio(void) {
- int null_fd;
+char octchar(int x) {
+ return '0' + (x & 7);
+}
+
+int unoctchar(char c) {
- null_fd = open("/dev/null", O_RDWR|O_NOCTTY);
- if (null_fd < 0)
- return -errno;
+ if (c >= '0' && c <= '7')
+ return c - '0';
- return make_stdio(null_fd);
+ return -EINVAL;
}
-bool is_device_path(const char *path) {
+char decchar(int x) {
+ return '0' + (x % 10);
+}
- /* Returns true on paths that refer to a device, either in
- * sysfs or in /dev */
+int undecchar(char c) {
- return
- path_startswith(path, "/dev/") ||
- path_startswith(path, "/sys/");
+ if (c >= '0' && c <= '9')
+ return c - '0';
+
+ return -EINVAL;
}
-int dir_is_empty(const char *path) {
- _cleanup_closedir_ DIR *d;
+char *cescape(const char *s) {
+ char *r, *t;
+ const char *f;
- d = opendir(path);
- if (!d)
- return -errno;
+ assert(s);
- for (;;) {
- struct dirent *de;
+ /* Does C style string escaping. May be reversed with
+ * cunescape(). */
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
+ r = new(char, strlen(s)*4 + 1);
+ if (!r)
+ return NULL;
- if (!de)
- return 1;
+ for (f = s, t = r; *f; f++)
+ t += cescape_char(*f, t);
- if (!hidden_file(de->d_name))
- return 0;
- }
+ *t = 0;
+
+ return r;
}
-char* dirname_malloc(const char *path) {
- char *d, *dir, *dir2;
+static int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode) {
+ int r = 1;
- d = strdup(path);
- if (!d)
- return NULL;
- dir = dirname(d);
- assert(dir);
+ assert(p);
+ assert(*p);
+ assert(ret);
- if (dir != d) {
- dir2 = strdup(dir);
- free(d);
- return dir2;
- }
+ /* Unescapes C style. Returns the unescaped character in ret,
+ * unless we encountered a \u sequence in which case the full
+ * unicode character is returned in ret_unicode, instead. */
- return dir;
-}
+ if (length != (size_t) -1 && length < 1)
+ return -EINVAL;
-int dev_urandom(void *p, size_t n) {
- static int have_syscall = -1;
+ switch (p[0]) {
- _cleanup_close_ int fd = -1;
- int r;
+ case 'a':
+ *ret = '\a';
+ break;
+ case 'b':
+ *ret = '\b';
+ break;
+ case 'f':
+ *ret = '\f';
+ break;
+ case 'n':
+ *ret = '\n';
+ break;
+ case 'r':
+ *ret = '\r';
+ break;
+ case 't':
+ *ret = '\t';
+ break;
+ case 'v':
+ *ret = '\v';
+ break;
+ case '\\':
+ *ret = '\\';
+ break;
+ case '"':
+ *ret = '"';
+ break;
+ case '\'':
+ *ret = '\'';
+ break;
- /* Gathers some randomness from the kernel. This call will
- * never block, and will always return some data from the
- * kernel, regardless if the random pool is fully initialized
- * or not. It thus makes no guarantee for the quality of the
- * returned entropy, but is good enough for or usual usecases
- * of seeding the hash functions for hashtable */
-
- /* Use the getrandom() syscall unless we know we don't have
- * it, or when the requested size is too large for it. */
- if (have_syscall != 0 || (size_t) (int) n != n) {
- r = getrandom(p, n, GRND_NONBLOCK);
- if (r == (int) n) {
- have_syscall = true;
- return 0;
- }
+ case 's':
+ /* This is an extension of the XDG syntax files */
+ *ret = ' ';
+ break;
- if (r < 0) {
- if (errno == ENOSYS)
- /* we lack the syscall, continue with
- * reading from /dev/urandom */
- have_syscall = false;
- else if (errno == EAGAIN)
- /* not enough entropy for now. Let's
- * remember to use the syscall the
- * next time, again, but also read
- * from /dev/urandom for now, which
- * doesn't care about the current
- * amount of entropy. */
- have_syscall = true;
- else
- return -errno;
- } else
- /* too short read? */
- return -ENODATA;
- }
+ case 'x': {
+ /* hexadecimal encoding */
+ int a, b;
- fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
- return errno == ENOENT ? -ENOSYS : -errno;
+ if (length != (size_t) -1 && length < 3)
+ return -EINVAL;
- return loop_read_exact(fd, p, n, true);
-}
+ a = unhexchar(p[1]);
+ if (a < 0)
+ return -EINVAL;
-void initialize_srand(void) {
- static bool srand_called = false;
- unsigned x;
-#ifdef HAVE_SYS_AUXV_H
- void *auxv;
-#endif
+ b = unhexchar(p[2]);
+ if (b < 0)
+ return -EINVAL;
- if (srand_called)
- return;
+ /* Don't allow NUL bytes */
+ if (a == 0 && b == 0)
+ return -EINVAL;
- x = 0;
+ *ret = (char) ((a << 4U) | b);
+ r = 3;
+ break;
+ }
-#ifdef HAVE_SYS_AUXV_H
- /* The kernel provides us with a bit of entropy in auxv, so
- * let's try to make use of that to seed the pseudo-random
- * generator. It's better than nothing... */
+ case 'u': {
+ /* C++11 style 16bit unicode */
- auxv = (void*) getauxval(AT_RANDOM);
- if (auxv)
- x ^= *(unsigned*) auxv;
-#endif
+ int a[4];
+ unsigned i;
+ uint32_t c;
- x ^= (unsigned) now(CLOCK_REALTIME);
- x ^= (unsigned) gettid();
+ if (length != (size_t) -1 && length < 5)
+ return -EINVAL;
- srand(x);
- srand_called = true;
-}
+ for (i = 0; i < 4; i++) {
+ a[i] = unhexchar(p[1 + i]);
+ if (a[i] < 0)
+ return a[i];
+ }
-void random_bytes(void *p, size_t n) {
- uint8_t *q;
- int r;
+ c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3];
- r = dev_urandom(p, n);
- if (r >= 0)
- return;
+ /* Don't allow 0 chars */
+ if (c == 0)
+ return -EINVAL;
- /* If some idiot made /dev/urandom unavailable to us, he'll
- * get a PRNG instead. */
+ if (c < 128)
+ *ret = c;
+ else {
+ if (!ret_unicode)
+ return -EINVAL;
- initialize_srand();
+ *ret = 0;
+ *ret_unicode = c;
+ }
- for (q = p; q < (uint8_t*) p + n; q ++)
- *q = rand();
-}
+ r = 5;
+ break;
+ }
-void rename_process(const char name[8]) {
- assert(name);
+ case 'U': {
+ /* C++11 style 32bit unicode */
- /* This is a like a poor man's setproctitle(). It changes the
- * comm field, argv[0], and also the glibc's internally used
- * name of the process. For the first one a limit of 16 chars
- * applies, to the second one usually one of 10 (i.e. length
- * of "/sbin/init"), to the third one one of 7 (i.e. length of
- * "systemd"). If you pass a longer string it will be
- * truncated */
+ int a[8];
+ unsigned i;
+ uint32_t c;
- prctl(PR_SET_NAME, name);
+ if (length != (size_t) -1 && length < 9)
+ return -EINVAL;
- if (program_invocation_name)
- strncpy(program_invocation_name, name, strlen(program_invocation_name));
+ for (i = 0; i < 8; i++) {
+ a[i] = unhexchar(p[1 + i]);
+ if (a[i] < 0)
+ return a[i];
+ }
- if (saved_argc > 0) {
- int i;
+ c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
+ ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
- if (saved_argv[0])
- strncpy(saved_argv[0], name, strlen(saved_argv[0]));
+ /* Don't allow 0 chars */
+ if (c == 0)
+ return -EINVAL;
+
+ /* Don't allow invalid code points */
+ if (!unichar_is_valid(c))
+ return -EINVAL;
- for (i = 1; i < saved_argc; i++) {
- if (!saved_argv[i])
- break;
+ if (c < 128)
+ *ret = c;
+ else {
+ if (!ret_unicode)
+ return -EINVAL;
- memzero(saved_argv[i], strlen(saved_argv[i]));
+ *ret = 0;
+ *ret_unicode = c;
}
+
+ r = 9;
+ break;
}
-}
-void sigset_add_many(sigset_t *ss, ...) {
- va_list ap;
- int sig;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7': {
+ /* octal encoding */
+ int a, b, c;
+ uint32_t m;
- assert(ss);
+ if (length != (size_t) -1 && length < 3)
+ return -EINVAL;
- va_start(ap, ss);
- while ((sig = va_arg(ap, int)) > 0)
- assert_se(sigaddset(ss, sig) == 0);
- va_end(ap);
-}
+ a = unoctchar(p[0]);
+ if (a < 0)
+ return -EINVAL;
-int sigprocmask_many(int how, ...) {
- va_list ap;
- sigset_t ss;
- int sig;
+ b = unoctchar(p[1]);
+ if (b < 0)
+ return -EINVAL;
+
+ c = unoctchar(p[2]);
+ if (c < 0)
+ return -EINVAL;
- assert_se(sigemptyset(&ss) == 0);
+ /* don't allow NUL bytes */
+ if (a == 0 && b == 0 && c == 0)
+ return -EINVAL;
- va_start(ap, how);
- while ((sig = va_arg(ap, int)) > 0)
- assert_se(sigaddset(&ss, sig) == 0);
- va_end(ap);
+ /* Don't allow bytes above 255 */
+ m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
+ if (m > 255)
+ return -EINVAL;
- if (sigprocmask(how, &ss, NULL) < 0)
- return -errno;
+ *ret = m;
+ r = 3;
+ break;
+ }
- return 0;
+ default:
+ return -EINVAL;
+ }
+
+ return r;
}
-char* gethostname_malloc(void) {
- struct utsname u;
+int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
+ char *r, *t;
+ const char *f;
+ size_t pl;
- assert_se(uname(&u) >= 0);
+ assert(s);
+ assert(ret);
- if (!isempty(u.nodename) && !streq(u.nodename, "(none)"))
- return strdup(u.nodename);
+ /* Undoes C style string escaping, and optionally prefixes it. */
- return strdup(u.sysname);
-}
+ pl = prefix ? strlen(prefix) : 0;
-bool hostname_is_set(void) {
- struct utsname u;
+ r = new(char, pl+length+1);
+ if (!r)
+ return -ENOMEM;
- assert_se(uname(&u) >= 0);
+ if (prefix)
+ memcpy(r, prefix, pl);
- return !isempty(u.nodename) && !streq(u.nodename, "(none)");
-}
+ for (f = s, t = r + pl; f < s + length; f++) {
+ size_t remaining;
+ uint32_t u;
+ char c;
+ int k;
-char *lookup_uid(uid_t uid) {
- long bufsize;
- char *name;
- _cleanup_free_ char *buf = NULL;
- struct passwd pwbuf, *pw = NULL;
+ remaining = s + length - f;
+ assert(remaining > 0);
- /* Shortcut things to avoid NSS lookups */
- if (uid == 0)
- return strdup("root");
+ if (*f != '\\') {
+ /* A literal literal, copy verbatim */
+ *(t++) = *f;
+ continue;
+ }
- bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
- if (bufsize <= 0)
- bufsize = 4096;
+ if (remaining == 1) {
+ if (flags & UNESCAPE_RELAX) {
+ /* A trailing backslash, copy verbatim */
+ *(t++) = *f;
+ continue;
+ }
- buf = malloc(bufsize);
- if (!buf)
- return NULL;
+ free(r);
+ return -EINVAL;
+ }
- if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
- return strdup(pw->pw_name);
+ k = cunescape_one(f + 1, remaining - 1, &c, &u);
+ if (k < 0) {
+ if (flags & UNESCAPE_RELAX) {
+ /* Invalid escape code, let's take it literal then */
+ *(t++) = '\\';
+ continue;
+ }
- if (asprintf(&name, UID_FMT, uid) < 0)
- return NULL;
+ free(r);
+ return k;
+ }
- return name;
-}
+ if (c != 0)
+ /* Non-Unicode? Let's encode this directly */
+ *(t++) = c;
+ else
+ /* Unicode? Then let's encode this in UTF-8 */
+ t += utf8_encode_unichar(t, u);
-char* getlogname_malloc(void) {
- uid_t uid;
- struct stat st;
+ f += k;
+ }
- if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
- uid = st.st_uid;
- else
- uid = getuid();
+ *t = 0;
- return lookup_uid(uid);
+ *ret = r;
+ return t - r;
}
-char *getusername_malloc(void) {
- const char *e;
-
- e = getenv("USER");
- if (e)
- return strdup(e);
+int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
+ return cunescape_length_with_prefix(s, length, NULL, flags, ret);
+}
- return lookup_uid(getuid());
+int cunescape(const char *s, UnescapeFlags flags, char **ret) {
+ return cunescape_length(s, strlen(s), flags, ret);
}
-int getttyname_malloc(int fd, char **ret) {
- size_t l = 100;
- int r;
+char *xescape(const char *s, const char *bad) {
+ char *r, *t;
+ const char *f;
- assert(fd >= 0);
- assert(ret);
+ /* Escapes all chars in bad, in addition to \ and all special
+ * chars, in \xFF style escaping. May be reversed with
+ * cunescape(). */
- for (;;) {
- char path[l];
+ r = new(char, strlen(s) * 4 + 1);
+ if (!r)
+ return NULL;
- r = ttyname_r(fd, path, sizeof(path));
- if (r == 0) {
- const char *p;
- char *c;
+ for (f = s, t = r; *f; f++) {
- p = startswith(path, "/dev/");
- c = strdup(p ?: path);
- if (!c)
- return -ENOMEM;
+ if ((*f < ' ') || (*f >= 127) ||
+ (*f == '\\') || strchr(bad, *f)) {
+ *(t++) = '\\';
+ *(t++) = 'x';
+ *(t++) = hexchar(*f >> 4);
+ *(t++) = hexchar(*f);
+ } else
+ *(t++) = *f;
+ }
- *ret = c;
- return 0;
- }
+ *t = 0;
- if (r != ERANGE)
- return -r;
+ return r;
+}
- l *= 2;
- }
+/// UNNEEDED by elogind
+#if 0
+char *ascii_strlower(char *t) {
+ char *p;
- return 0;
-}
+ assert(t);
-int getttyname_harder(int fd, char **r) {
- int k;
- char *s = NULL;
+ for (p = t; *p; p++)
+ if (*p >= 'A' && *p <= 'Z')
+ *p = *p - 'A' + 'a';
- k = getttyname_malloc(fd, &s);
- if (k < 0)
- return k;
+ return t;
+}
+#endif // 0
- if (streq(s, "tty")) {
- free(s);
- return get_ctty(0, NULL, r);
- }
+_pure_ static bool hidden_file_allow_backup(const char *filename) {
+ assert(filename);
- *r = s;
- return 0;
+ return
+ filename[0] == '.' ||
+ streq(filename, "lost+found") ||
+ streq(filename, "aquota.user") ||
+ streq(filename, "aquota.group") ||
+ endswith(filename, ".rpmnew") ||
+ endswith(filename, ".rpmsave") ||
+ endswith(filename, ".rpmorig") ||
+ endswith(filename, ".dpkg-old") ||
+ endswith(filename, ".dpkg-new") ||
+ endswith(filename, ".dpkg-tmp") ||
+ endswith(filename, ".dpkg-dist") ||
+ endswith(filename, ".dpkg-bak") ||
+ endswith(filename, ".dpkg-backup") ||
+ endswith(filename, ".dpkg-remove") ||
+ endswith(filename, ".swp");
}
-int get_ctty_devnr(pid_t pid, dev_t *d) {
- int r;
- _cleanup_free_ char *line = NULL;
- const char *p;
- unsigned long ttynr;
+bool hidden_file(const char *filename) {
+ assert(filename);
- assert(pid >= 0);
+ if (endswith(filename, "~"))
+ return true;
- p = procfs_file_alloca(pid, "stat");
- r = read_one_line_file(p, &line);
- if (r < 0)
- return r;
+ return hidden_file_allow_backup(filename);
+}
- p = strrchr(line, ')');
- if (!p)
- return -EIO;
+int fd_nonblock(int fd, bool nonblock) {
+ int flags, nflags;
- p++;
+ assert(fd >= 0);
- if (sscanf(p, " "
- "%*c " /* state */
- "%*d " /* ppid */
- "%*d " /* pgrp */
- "%*d " /* session */
- "%lu ", /* ttynr */
- &ttynr) != 1)
- return -EIO;
+ flags = fcntl(fd, F_GETFL, 0);
+ if (flags < 0)
+ return -errno;
- if (major(ttynr) == 0 && minor(ttynr) == 0)
- return -ENOENT;
+ if (nonblock)
+ nflags = flags | O_NONBLOCK;
+ else
+ nflags = flags & ~O_NONBLOCK;
+
+ if (nflags == flags)
+ return 0;
- if (d)
- *d = (dev_t) ttynr;
+ if (fcntl(fd, F_SETFL, nflags) < 0)
+ return -errno;
return 0;
}
-int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
- char fn[sizeof("/dev/char/")-1 + 2*DECIMAL_STR_MAX(unsigned) + 1 + 1], *b = NULL;
- _cleanup_free_ char *s = NULL;
- const char *p;
- dev_t devnr;
- int k;
+int fd_cloexec(int fd, bool cloexec) {
+ int flags, nflags;
- assert(r);
+ assert(fd >= 0);
- k = get_ctty_devnr(pid, &devnr);
- if (k < 0)
- return k;
+ flags = fcntl(fd, F_GETFD, 0);
+ if (flags < 0)
+ return -errno;
- sprintf(fn, "/dev/char/%u:%u", major(devnr), minor(devnr));
+ if (cloexec)
+ nflags = flags | FD_CLOEXEC;
+ else
+ nflags = flags & ~FD_CLOEXEC;
- k = readlink_malloc(fn, &s);
- if (k < 0) {
+ if (nflags == flags)
+ return 0;
- if (k != -ENOENT)
- return k;
+ if (fcntl(fd, F_SETFD, nflags) < 0)
+ return -errno;
- /* This is an ugly hack */
- if (major(devnr) == 136) {
- if (asprintf(&b, "pts/%u", minor(devnr)) < 0)
- return -ENOMEM;
- } else {
- /* Probably something like the ptys which have no
- * symlink in /dev/char. Let's return something
- * vaguely useful. */
+ return 0;
+}
- b = strdup(fn + 5);
- if (!b)
- return -ENOMEM;
- }
- } else {
- if (startswith(s, "/dev/"))
- p = s + 5;
- else if (startswith(s, "../"))
- p = s + 3;
- else
- p = s;
+_pure_ static bool fd_in_set(int fd, const int fdset[], unsigned n_fdset) {
+ unsigned i;
- b = strdup(p);
- if (!b)
- return -ENOMEM;
- }
+ assert(n_fdset == 0 || fdset);
- *r = b;
- if (_devnr)
- *_devnr = devnr;
+ for (i = 0; i < n_fdset; i++)
+ if (fdset[i] == fd)
+ return true;
- return 0;
+ return false;
}
-int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
+int close_all_fds(const int except[], unsigned n_except) {
_cleanup_closedir_ DIR *d = NULL;
- int ret = 0;
-
- assert(fd >= 0);
+ struct dirent *de;
+ int r = 0;
- /* This returns the first error we run into, but nevertheless
- * tries to go on. This closes the passed fd. */
+ assert(n_except == 0 || except);
- d = fdopendir(fd);
+ d = opendir("/proc/self/fd");
if (!d) {
- safe_close(fd);
-
- return errno == ENOENT ? 0 : -errno;
- }
-
- for (;;) {
- struct dirent *de;
- bool is_dir, keep_around;
- struct stat st;
- int r;
+ int fd;
+ struct rlimit rl;
- errno = 0;
- de = readdir(d);
- if (!de) {
- if (errno != 0 && ret == 0)
- ret = -errno;
- return ret;
- }
+ /* When /proc isn't available (for example in chroots)
+ * the fallback is brute forcing through the fd
+ * table */
- if (streq(de->d_name, ".") || streq(de->d_name, ".."))
- continue;
+ assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0);
+ for (fd = 3; fd < (int) rl.rlim_max; fd ++) {
- if (de->d_type == DT_UNKNOWN ||
- honour_sticky ||
- (de->d_type == DT_DIR && root_dev)) {
- if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
+ if (fd_in_set(fd, except, n_except))
continue;
- }
- is_dir = S_ISDIR(st.st_mode);
- keep_around =
- honour_sticky &&
- (st.st_uid == 0 || st.st_uid == getuid()) &&
- (st.st_mode & S_ISVTX);
- } else {
- is_dir = de->d_type == DT_DIR;
- keep_around = false;
+ if (close_nointr(fd) < 0)
+ if (errno != EBADF && r == 0)
+ r = -errno;
}
- if (is_dir) {
- int subdir_fd;
+ return r;
+ }
- /* if root_dev is set, remove subdirectories only, if device is same as dir */
- if (root_dev && st.st_dev != root_dev->st_dev)
- continue;
+ while ((de = readdir(d))) {
+ int fd = -1;
- subdir_fd = openat(fd, de->d_name,
- O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
- if (subdir_fd < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
- continue;
- }
+ if (hidden_file(de->d_name))
+ continue;
+
+ if (safe_atoi(de->d_name, &fd) < 0)
+ /* Let's better ignore this, just in case */
+ continue;
- r = rm_rf_children_dangerous(subdir_fd, only_dirs, honour_sticky, root_dev);
- if (r < 0 && ret == 0)
- ret = r;
+ if (fd < 3)
+ continue;
- if (!keep_around)
- if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
- }
+ if (fd == dirfd(d))
+ continue;
- } else if (!only_dirs && !keep_around) {
+ if (fd_in_set(fd, except, n_except))
+ continue;
- if (unlinkat(fd, de->d_name, 0) < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
- }
+ if (close_nointr(fd) < 0) {
+ /* Valgrind has its own FD and doesn't want to have it closed */
+ if (errno != EBADF && r == 0)
+ r = -errno;
}
}
+
+ return r;
}
-_pure_ static int is_temporary_fs(struct statfs *s) {
- assert(s);
+bool chars_intersect(const char *a, const char *b) {
+ const char *p;
- return F_TYPE_EQUAL(s->f_type, TMPFS_MAGIC) ||
- F_TYPE_EQUAL(s->f_type, RAMFS_MAGIC);
+ /* Returns true if any of the chars in a are in b. */
+ for (p = a; *p; p++)
+ if (strchr(b, *p))
+ return true;
+
+ return false;
}
-int is_fd_on_temporary_fs(int fd) {
- struct statfs s;
+/// UNNEEDED by elogind
+#if 0
+bool fstype_is_network(const char *fstype) {
+ static const char table[] =
+ "afs\0"
+ "cifs\0"
+ "smbfs\0"
+ "sshfs\0"
+ "ncpfs\0"
+ "ncp\0"
+ "nfs\0"
+ "nfs4\0"
+ "gfs\0"
+ "gfs2\0"
+ "glusterfs\0";
- if (fstatfs(fd, &s) < 0)
- return -errno;
+ const char *x;
- return is_temporary_fs(&s);
+ x = startswith(fstype, "fuse.");
+ if (x)
+ fstype = x;
+
+ return nulstr_contains(table, fstype);
}
+#endif // 0
-int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
- struct statfs s;
+int flush_fd(int fd) {
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN,
+ };
- assert(fd >= 0);
+ for (;;) {
+ char buf[LINE_MAX];
+ ssize_t l;
+ int r;
- if (fstatfs(fd, &s) < 0) {
- safe_close(fd);
- return -errno;
- }
+ r = poll(&pollfd, 1, 0);
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
- /* We refuse to clean disk file systems with this call. This
- * is extra paranoia just to be sure we never ever remove
- * non-state data */
- if (!is_temporary_fs(&s)) {
- log_error("Attempted to remove disk file system, and we can't allow that.");
- safe_close(fd);
- return -EPERM;
- }
+ return -errno;
- return rm_rf_children_dangerous(fd, only_dirs, honour_sticky, root_dev);
-}
+ } else if (r == 0)
+ return 0;
-static int file_is_priv_sticky(const char *p) {
- struct stat st;
+ l = read(fd, buf, sizeof(buf));
+ if (l < 0) {
- assert(p);
+ if (errno == EINTR)
+ continue;
- if (lstat(p, &st) < 0)
- return -errno;
+ if (errno == EAGAIN)
+ return 0;
- return
- (st.st_uid == 0 || st.st_uid == getuid()) &&
- (st.st_mode & S_ISVTX);
+ return -errno;
+ } else if (l == 0)
+ return 0;
+ }
}
-static int rm_rf_internal(const char *path, bool only_dirs, bool delete_root, bool honour_sticky, bool dangerous) {
- int fd, r;
- struct statfs s;
-
- assert(path);
+void safe_close_pair(int p[]) {
+ assert(p);
- /* We refuse to clean the root file system with this
- * call. This is extra paranoia to never cause a really
- * seriously broken system. */
- if (path_equal(path, "/")) {
- log_error("Attempted to remove entire root file system, and we can't allow that.");
- return -EPERM;
+ if (p[0] == p[1]) {
+ /* Special case pairs which use the same fd in both
+ * directions... */
+ p[0] = p[1] = safe_close(p[0]);
+ return;
}
- fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
- if (fd < 0) {
+ p[0] = safe_close(p[0]);
+ p[1] = safe_close(p[1]);
+}
- if (errno != ENOTDIR && errno != ELOOP)
- return -errno;
+ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
+ uint8_t *p = buf;
+ ssize_t n = 0;
- if (!dangerous) {
- if (statfs(path, &s) < 0)
- return -errno;
+ assert(fd >= 0);
+ assert(buf);
- if (!is_temporary_fs(&s)) {
- log_error("Attempted to remove disk file system, and we can't allow that.");
- return -EPERM;
- }
- }
+ while (nbytes > 0) {
+ ssize_t k;
- if (delete_root && !only_dirs)
- if (unlink(path) < 0 && errno != ENOENT)
- return -errno;
+ k = read(fd, p, nbytes);
+ if (k < 0) {
+ if (errno == EINTR)
+ continue;
- return 0;
- }
+ if (errno == EAGAIN && do_poll) {
- if (!dangerous) {
- if (fstatfs(fd, &s) < 0) {
- safe_close(fd);
- return -errno;
- }
+ /* We knowingly ignore any return value here,
+ * and expect that any error/EOF is reported
+ * via read() */
- if (!is_temporary_fs(&s)) {
- log_error("Attempted to remove disk file system, and we can't allow that.");
- safe_close(fd);
- return -EPERM;
- }
- }
+ fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
+ continue;
+ }
- r = rm_rf_children_dangerous(fd, only_dirs, honour_sticky, NULL);
- if (delete_root) {
+ return n > 0 ? n : -errno;
+ }
- if (honour_sticky && file_is_priv_sticky(path) > 0)
- return r;
+ if (k == 0)
+ return n;
- if (rmdir(path) < 0 && errno != ENOENT) {
- if (r == 0)
- r = -errno;
- }
+ p += k;
+ nbytes -= k;
+ n += k;
}
- return r;
+ return n;
}
-int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) {
- return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, false);
-}
+int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
+ ssize_t n;
-int rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) {
- return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, true);
+ n = loop_read(fd, buf, nbytes, do_poll);
+ if (n < 0)
+ return n;
+ if ((size_t) n != nbytes)
+ return -EIO;
+ return 0;
}
-int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
- assert(path);
+int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
+ const uint8_t *p = buf;
- /* Under the assumption that we are running privileged we
- * first change the access mode and only then hand out
- * ownership to avoid a window where access is too open. */
+ assert(fd >= 0);
+ assert(buf);
- if (mode != MODE_INVALID)
- if (chmod(path, mode) < 0)
- return -errno;
+ errno = 0;
- if (uid != UID_INVALID || gid != GID_INVALID)
- if (chown(path, uid, gid) < 0)
- return -errno;
+ do {
+ ssize_t k;
- return 0;
-}
+ k = write(fd, p, nbytes);
+ if (k < 0) {
+ if (errno == EINTR)
+ continue;
-int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) {
- assert(fd >= 0);
+ if (errno == EAGAIN && do_poll) {
+ /* We knowingly ignore any return value here,
+ * and expect that any error/EOF is reported
+ * via write() */
- /* Under the assumption that we are running privileged we
- * first change the access mode and only then hand out
- * ownership to avoid a window where access is too open. */
+ fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
+ continue;
+ }
- if (mode != MODE_INVALID)
- if (fchmod(fd, mode) < 0)
return -errno;
+ }
- if (uid != UID_INVALID || gid != GID_INVALID)
- if (fchown(fd, uid, gid) < 0)
- return -errno;
+ if (nbytes > 0 && k == 0) /* Can't really happen */
+ return -EIO;
+
+ p += k;
+ nbytes -= k;
+ } while (nbytes > 0);
return 0;
}
-cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
- cpu_set_t *r;
- unsigned n = 1024;
+int parse_size(const char *t, off_t base, off_t *size) {
+
+ /* Soo, sometimes we want to parse IEC binary suffixes, and
+ * sometimes SI decimal suffixes. This function can parse
+ * both. Which one is the right way depends on the
+ * context. Wikipedia suggests that SI is customary for
+ * hardware metrics and network speeds, while IEC is
+ * customary for most data sizes used by software and volatile
+ * (RAM) memory. Hence be careful which one you pick!
+ *
+ * In either case we use just K, M, G as suffix, and not Ki,
+ * Mi, Gi or so (as IEC would suggest). That's because that's
+ * frickin' ugly. But this means you really need to make sure
+ * to document which base you are parsing when you use this
+ * call. */
+
+ struct table {
+ const char *suffix;
+ unsigned long long factor;
+ };
- /* Allocates the cpuset in the right size */
+ static const struct table iec[] = {
+ { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
+ { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
+ { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
+ { "G", 1024ULL*1024ULL*1024ULL },
+ { "M", 1024ULL*1024ULL },
+ { "K", 1024ULL },
+ { "B", 1 },
+ { "", 1 },
+ };
- for (;;) {
- if (!(r = CPU_ALLOC(n)))
- return NULL;
+ static const struct table si[] = {
+ { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
+ { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
+ { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
+ { "G", 1000ULL*1000ULL*1000ULL },
+ { "M", 1000ULL*1000ULL },
+ { "K", 1000ULL },
+ { "B", 1 },
+ { "", 1 },
+ };
- if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
- CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
+ const struct table *table;
+ const char *p;
+ unsigned long long r = 0;
+ unsigned n_entries, start_pos = 0;
- if (ncpus)
- *ncpus = n;
+ assert(t);
+ assert(base == 1000 || base == 1024);
+ assert(size);
- return r;
- }
+ if (base == 1000) {
+ table = si;
+ n_entries = ELEMENTSOF(si);
+ } else {
+ table = iec;
+ n_entries = ELEMENTSOF(iec);
+ }
- CPU_FREE(r);
+ p = t;
+ do {
+ long long l;
+ unsigned long long l2;
+ double frac = 0;
+ char *e;
+ unsigned i;
- if (errno != EINVAL)
- return NULL;
+ errno = 0;
+ l = strtoll(p, &e, 10);
- n *= 2;
- }
-}
+ if (errno > 0)
+ return -errno;
-int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
- static const char status_indent[] = " "; /* "[" STATUS "] " */
- _cleanup_free_ char *s = NULL;
- _cleanup_close_ int fd = -1;
- struct iovec iovec[6] = {};
- int n = 0;
- static bool prev_ephemeral;
+ if (l < 0)
+ return -ERANGE;
- assert(format);
+ if (e == p)
+ return -EINVAL;
- /* This is independent of logging, as status messages are
- * optional and go exclusively to the console. */
+ if (*e == '.') {
+ e++;
+ if (*e >= '0' && *e <= '9') {
+ char *e2;
- if (vasprintf(&s, format, ap) < 0)
- return log_oom();
+ /* strotoull itself would accept space/+/- */
+ l2 = strtoull(e, &e2, 10);
- fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ if (errno == ERANGE)
+ return -errno;
- if (ellipse) {
- char *e;
- size_t emax, sl;
- int c;
+ /* Ignore failure. E.g. 10.M is valid */
+ frac = l2;
+ for (; e < e2; e++)
+ frac /= 10;
+ }
+ }
- c = fd_columns(fd);
- if (c <= 0)
- c = 80;
+ e += strspn(e, WHITESPACE);
- sl = status ? sizeof(status_indent)-1 : 0;
+ for (i = start_pos; i < n_entries; i++)
+ if (startswith(e, table[i].suffix)) {
+ unsigned long long tmp;
+ if ((unsigned long long) l + (frac > 0) > ULLONG_MAX / table[i].factor)
+ return -ERANGE;
+ tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
+ if (tmp > ULLONG_MAX - r)
+ return -ERANGE;
- emax = c - sl - 1;
- if (emax < 3)
- emax = 3;
+ r += tmp;
+ if ((unsigned long long) (off_t) r != r)
+ return -ERANGE;
- e = ellipsize(s, emax, 50);
- if (e) {
- free(s);
- s = e;
- }
- }
+ p = e + strlen(table[i].suffix);
- if (prev_ephemeral)
- IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
- prev_ephemeral = ephemeral;
+ start_pos = i + 1;
+ break;
+ }
- if (status) {
- if (!isempty(status)) {
- IOVEC_SET_STRING(iovec[n++], "[");
- IOVEC_SET_STRING(iovec[n++], status);
- IOVEC_SET_STRING(iovec[n++], "] ");
- } else
- IOVEC_SET_STRING(iovec[n++], status_indent);
- }
+ if (i >= n_entries)
+ return -EINVAL;
- IOVEC_SET_STRING(iovec[n++], s);
- if (!ephemeral)
- IOVEC_SET_STRING(iovec[n++], "\n");
+ } while (*p);
- if (writev(fd, iovec, n) < 0)
- return -errno;
+ *size = r;
return 0;
}
-int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) {
- va_list ap;
- int r;
-
- assert(format);
+bool is_device_path(const char *path) {
- va_start(ap, format);
- r = status_vprintf(status, ellipse, ephemeral, format, ap);
- va_end(ap);
+ /* Returns true on paths that refer to a device, either in
+ * sysfs or in /dev */
- return r;
+ return
+ path_startswith(path, "/dev/") ||
+ path_startswith(path, "/sys/");
}
-char *replace_env(const char *format, char **env) {
- enum {
- WORD,
- CURLY,
- VARIABLE
- } state = WORD;
+/// UNNEEDED by elogind
+#if 0
+int dir_is_empty(const char *path) {
+ _cleanup_closedir_ DIR *d;
- const char *e, *word = format;
- char *r = NULL, *k;
+ d = opendir(path);
+ if (!d)
+ return -errno;
- assert(format);
+ for (;;) {
+ struct dirent *de;
- for (e = format; *e; e ++) {
+ errno = 0;
+ de = readdir(d);
+ if (!de && errno != 0)
+ return -errno;
- switch (state) {
+ if (!de)
+ return 1;
- case WORD:
- if (*e == '$')
- state = CURLY;
- break;
+ if (!hidden_file(de->d_name))
+ return 0;
+ }
+}
+
+char* dirname_malloc(const char *path) {
+ char *d, *dir, *dir2;
- case CURLY:
- if (*e == '{') {
- k = strnappend(r, word, e-word-1);
- if (!k)
- goto fail;
+ d = strdup(path);
+ if (!d)
+ return NULL;
+ dir = dirname(d);
+ assert(dir);
- free(r);
- r = k;
+ if (dir != d) {
+ dir2 = strdup(dir);
+ free(d);
+ return dir2;
+ }
- word = e-1;
- state = VARIABLE;
+ return dir;
+}
- } else if (*e == '$') {
- k = strnappend(r, word, e-word);
- if (!k)
- goto fail;
+void rename_process(const char name[8]) {
+ assert(name);
- free(r);
- r = k;
+ /* This is a like a poor man's setproctitle(). It changes the
+ * comm field, argv[0], and also the glibc's internally used
+ * name of the process. For the first one a limit of 16 chars
+ * applies, to the second one usually one of 10 (i.e. length
+ * of "/sbin/init"), to the third one one of 7 (i.e. length of
+ * "systemd"). If you pass a longer string it will be
+ * truncated */
- word = e+1;
- state = WORD;
- } else
- state = WORD;
- break;
+ prctl(PR_SET_NAME, name);
- case VARIABLE:
- if (*e == '}') {
- const char *t;
+ if (program_invocation_name)
+ strncpy(program_invocation_name, name, strlen(program_invocation_name));
- t = strempty(strv_env_get_n(env, word+2, e-word-2));
+ if (saved_argc > 0) {
+ int i;
- k = strappend(r, t);
- if (!k)
- goto fail;
+ if (saved_argv[0])
+ strncpy(saved_argv[0], name, strlen(saved_argv[0]));
- free(r);
- r = k;
+ for (i = 1; i < saved_argc; i++) {
+ if (!saved_argv[i])
+ break;
- word = e+1;
- state = WORD;
- }
- break;
+ memzero(saved_argv[i], strlen(saved_argv[i]));
}
}
+}
+#endif // 0
- k = strnappend(r, word, e-word);
- if (!k)
- goto fail;
+char *lookup_uid(uid_t uid) {
+ long bufsize;
+ char *name;
+ _cleanup_free_ char *buf = NULL;
+ struct passwd pwbuf, *pw = NULL;
- free(r);
- return k;
+ /* Shortcut things to avoid NSS lookups */
+ if (uid == 0)
+ return strdup("root");
-fail:
- free(r);
- return NULL;
-}
+ bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (bufsize <= 0)
+ bufsize = 4096;
-char **replace_env_argv(char **argv, char **env) {
- char **ret, **i;
- unsigned k = 0, l = 0;
+ buf = malloc(bufsize);
+ if (!buf)
+ return NULL;
- l = strv_length(argv);
+ if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
+ return strdup(pw->pw_name);
- ret = new(char*, l+1);
- if (!ret)
+ if (asprintf(&name, UID_FMT, uid) < 0)
return NULL;
- STRV_FOREACH(i, argv) {
-
- /* If $FOO appears as single word, replace it by the split up variable */
- if ((*i)[0] == '$' && (*i)[1] != '{') {
- char *e;
- char **w, **m = NULL;
- unsigned q;
+ return name;
+}
- e = strv_env_get(env, *i+1);
- if (e) {
- int r;
+/// UNNEEDED by elogind
+#if 0
+char* getlogname_malloc(void) {
+ uid_t uid;
+ struct stat st;
- r = strv_split_quoted(&m, e, UNQUOTE_RELAX);
- if (r < 0) {
- ret[k] = NULL;
- strv_free(ret);
- return NULL;
- }
- } else
- m = NULL;
+ if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
+ uid = st.st_uid;
+ else
+ uid = getuid();
- q = strv_length(m);
- l = l + q - 1;
+ return lookup_uid(uid);
+}
- w = realloc(ret, sizeof(char*) * (l+1));
- if (!w) {
- ret[k] = NULL;
- strv_free(ret);
- strv_free(m);
- return NULL;
- }
+char *getusername_malloc(void) {
+ const char *e;
- ret = w;
- if (m) {
- memcpy(ret + k, m, q * sizeof(char*));
- free(m);
- }
+ e = getenv("USER");
+ if (e)
+ return strdup(e);
- k += q;
- continue;
- }
+ return lookup_uid(getuid());
+}
+#endif // 0
- /* If ${FOO} appears as part of a word, replace it by the variable as-is */
- ret[k] = replace_env(*i, env);
- if (!ret[k]) {
- strv_free(ret);
- return NULL;
- }
- k++;
- }
+bool is_temporary_fs(const struct statfs *s) {
+ assert(s);
- ret[k] = NULL;
- return ret;
+ return F_TYPE_EQUAL(s->f_type, TMPFS_MAGIC) ||
+ F_TYPE_EQUAL(s->f_type, RAMFS_MAGIC);
}
-int fd_columns(int fd) {
- struct winsize ws = {};
+int fd_is_temporary_fs(int fd) {
+ struct statfs s;
- if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
+ if (fstatfs(fd, &s) < 0)
return -errno;
- if (ws.ws_col <= 0)
- return -EIO;
-
- return ws.ws_col;
+ return is_temporary_fs(&s);
}
-unsigned columns(void) {
- const char *e;
- int c;
-
- if (_likely_(cached_columns > 0))
- return cached_columns;
+int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
+ assert(path);
- c = 0;
- e = getenv("COLUMNS");
- if (e)
- (void) safe_atoi(e, &c);
+ /* Under the assumption that we are running privileged we
+ * first change the access mode and only then hand out
+ * ownership to avoid a window where access is too open. */
- if (c <= 0)
- c = fd_columns(STDOUT_FILENO);
+ if (mode != MODE_INVALID)
+ if (chmod(path, mode) < 0)
+ return -errno;
- if (c <= 0)
- c = 80;
+ if (uid != UID_INVALID || gid != GID_INVALID)
+ if (chown(path, uid, gid) < 0)
+ return -errno;
- cached_columns = c;
- return cached_columns;
+ return 0;
}
-int fd_lines(int fd) {
- struct winsize ws = {};
+/// UNNEEDED by elogind
+#if 0
+int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) {
+ assert(fd >= 0);
- if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
- return -errno;
+ /* Under the assumption that we are running privileged we
+ * first change the access mode and only then hand out
+ * ownership to avoid a window where access is too open. */
- if (ws.ws_row <= 0)
- return -EIO;
+ if (mode != MODE_INVALID)
+ if (fchmod(fd, mode) < 0)
+ return -errno;
- return ws.ws_row;
-}
+ if (uid != UID_INVALID || gid != GID_INVALID)
+ if (fchown(fd, uid, gid) < 0)
+ return -errno;
-unsigned lines(void) {
- const char *e;
- int l;
+ return 0;
+}
- if (_likely_(cached_lines > 0))
- return cached_lines;
+cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
+ cpu_set_t *r;
+ unsigned n = 1024;
- l = 0;
- e = getenv("LINES");
- if (e)
- (void) safe_atoi(e, &l);
+ /* Allocates the cpuset in the right size */
- if (l <= 0)
- l = fd_lines(STDOUT_FILENO);
+ for (;;) {
+ if (!(r = CPU_ALLOC(n)))
+ return NULL;
- if (l <= 0)
- l = 24;
+ if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
+ CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
- cached_lines = l;
- return cached_lines;
-}
+ if (ncpus)
+ *ncpus = n;
-/* intended to be used as a SIGWINCH sighandler */
-void columns_lines_cache_reset(int signum) {
- cached_columns = 0;
- cached_lines = 0;
-}
+ return r;
+ }
-bool on_tty(void) {
- static int cached_on_tty = -1;
+ CPU_FREE(r);
- if (_unlikely_(cached_on_tty < 0))
- cached_on_tty = isatty(STDOUT_FILENO) > 0;
+ if (errno != EINVAL)
+ return NULL;
- return cached_on_tty;
+ n *= 2;
+ }
}
+#endif // 0
int files_same(const char *filea, const char *fileb) {
struct stat a, b;
return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0);
}
-char *unquote(const char *s, const char* quotes) {
+/// UNNEEDED by elogind
+#if 0
+static char *unquote(const char *s, const char* quotes) {
size_t l;
assert(s);
/* This is rather stupid, simply removes the heading and
* trailing quotes if there is one. Doesn't care about
- * escaping or anything. We should make this smarter one
- * day... */
+ * escaping or anything.
+ *
+ * DON'T USE THIS FOR NEW CODE ANYMORE!*/
l = strlen(s);
if (l < 2)
return strdup(s);
}
-
-char *normalize_env_assignment(const char *s) {
- _cleanup_free_ char *value = NULL;
- const char *eq;
- char *p, *name;
-
- eq = strchr(s, '=');
- if (!eq) {
- char *r, *t;
-
- r = strdup(s);
- if (!r)
- return NULL;
-
- t = strstrip(r);
- if (t != r)
- memmove(r, t, strlen(t) + 1);
-
- return r;
- }
-
- name = strndupa(s, eq - s);
- p = strdupa(eq + 1);
-
- value = unquote(strstrip(p), QUOTES);
- if (!value)
- return NULL;
-
- return strjoin(strstrip(name), "=", value, NULL);
-}
-
-int wait_for_terminate(pid_t pid, siginfo_t *status) {
- siginfo_t dummy;
-
- assert(pid >= 1);
-
- if (!status)
- status = &dummy;
-
- for (;;) {
- zero(*status);
-
- if (waitid(P_PID, pid, status, WEXITED) < 0) {
-
- if (errno == EINTR)
- continue;
-
- return -errno;
- }
-
- return 0;
- }
-}
-
-/*
- * Return values:
- * < 0 : wait_for_terminate() failed to get the state of the
- * process, the process was terminated by a signal, or
- * failed for an unknown reason.
- * >=0 : The process terminated normally, and its exit code is
- * returned.
- *
- * That is, success is indicated by a return value of zero, and an
- * error is indicated by a non-zero value.
- *
- * A warning is emitted if the process terminates abnormally,
- * and also if it returns non-zero unless check_exit_code is true.
- */
-int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
- int r;
- siginfo_t status;
-
- assert(name);
- assert(pid > 1);
-
- r = wait_for_terminate(pid, &status);
- if (r < 0)
- return log_warning_errno(r, "Failed to wait for %s: %m", name);
-
- if (status.si_code == CLD_EXITED) {
- if (status.si_status != 0)
- log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
- "%s failed with error code %i.", name, status.si_status);
- else
- log_debug("%s succeeded.", name);
-
- return status.si_status;
- } else if (status.si_code == CLD_KILLED ||
- status.si_code == CLD_DUMPED) {
-
- log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
- return -EPROTO;
- }
-
- log_warning("%s failed due to unknown reason.", name);
- return -EPROTO;
-}
+#endif // 0
noreturn void freeze(void) {
return null_or_empty(&st);
}
+/// UNNEEDED by elogind
+#if 0
int null_or_empty_fd(int fd) {
struct stat st;
return null_or_empty(&st);
}
+#endif // 0
DIR *xopendirat(int fd, const char *name, int flags) {
int nfd;
return d;
}
-int signal_from_string_try_harder(const char *s) {
- int signo;
- assert(s);
-
- signo = signal_from_string(s);
- if (signo <= 0)
- if (startswith(s, "SIG"))
- return signal_from_string(s+3);
-
- return signo;
-}
-
+/// UNNEEDED by elogind
+#if 0
static char *tag_to_udev_node(const char *tagvalue, const char *by) {
_cleanup_free_ char *t = NULL, *u = NULL;
size_t enc_len;
- u = unquote(tagvalue, "\"\'");
+ u = unquote(tagvalue, QUOTES);
if (!u)
return NULL;
return strdup(p);
}
-
-bool tty_is_vc(const char *tty) {
- assert(tty);
-
- return vtnr_from_tty(tty) >= 0;
-}
-
-bool tty_is_console(const char *tty) {
- assert(tty);
-
- if (startswith(tty, "/dev/"))
- tty += 5;
-
- return streq(tty, "console");
-}
-
-int vtnr_from_tty(const char *tty) {
- int i, r;
-
- assert(tty);
-
- if (startswith(tty, "/dev/"))
- tty += 5;
-
- if (!startswith(tty, "tty") )
- return -EINVAL;
-
- if (tty[3] < '0' || tty[3] > '9')
- return -EINVAL;
-
- r = safe_atoi(tty+3, &i);
- if (r < 0)
- return r;
-
- if (i < 0 || i > 63)
- return -EINVAL;
-
- return i;
-}
-
-char *resolve_dev_console(char **active) {
- char *tty;
-
- /* Resolve where /dev/console is pointing to, if /sys is actually ours
- * (i.e. not read-only-mounted which is a sign for container setups) */
-
- if (path_is_read_only_fs("/sys") > 0)
- return NULL;
-
- if (read_one_line_file("/sys/class/tty/console/active", active) < 0)
- return NULL;
-
- /* If multiple log outputs are configured the last one is what
- * /dev/console points to */
- tty = strrchr(*active, ' ');
- if (tty)
- tty++;
- else
- tty = *active;
-
- if (streq(tty, "tty0")) {
- char *tmp;
-
- /* Get the active VC (e.g. tty1) */
- if (read_one_line_file("/sys/class/tty/tty0/active", &tmp) >= 0) {
- free(*active);
- tty = *active = tmp;
- }
- }
-
- return tty;
-}
-
-bool tty_is_vc_resolve(const char *tty) {
- _cleanup_free_ char *active = NULL;
-
- assert(tty);
-
- if (startswith(tty, "/dev/"))
- tty += 5;
-
- if (streq(tty, "console")) {
- tty = resolve_dev_console(&active);
- if (!tty)
- return false;
- }
-
- return tty_is_vc(tty);
-}
-
-const char *default_term_for_tty(const char *tty) {
- assert(tty);
-
- return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt220";
-}
+#endif // 0
bool dirent_is_file(const struct dirent *de) {
assert(de);
/* We fork this all off from a child process so that we can
* somewhat cleanly make use of SIGALRM to set a time limit */
- reset_all_signal_handlers();
- reset_signal_mask();
+ (void) reset_all_signal_handlers();
+ (void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
wait_for_terminate_and_warn(name, executor_pid, true);
}
-int kill_and_sigcont(pid_t pid, int sig) {
- int r;
-
- r = kill(pid, sig) < 0 ? -errno : 0;
-
- if (r >= 0)
- kill(pid, SIGCONT);
-
- return r;
-}
-
bool nulstr_contains(const char*nulstr, const char *needle) {
const char *i;
return false;
}
+/// UNNEEDED by elogind
+#if 0
bool plymouth_running(void) {
return access("/run/plymouth/pid", F_OK) >= 0;
}
+#endif // 0
char* strshorten(char *s, size_t l) {
assert(s);
return s;
}
-static bool hostname_valid_char(char c) {
- return
- (c >= 'a' && c <= 'z') ||
- (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') ||
- c == '-' ||
- c == '_' ||
- c == '.';
-}
-
-bool hostname_is_valid(const char *s) {
- const char *p;
- bool dot;
-
- if (isempty(s))
- return false;
-
- /* Doesn't accept empty hostnames, hostnames with trailing or
- * leading dots, and hostnames with multiple dots in a
- * sequence. Also ensures that the length stays below
- * HOST_NAME_MAX. */
-
- for (p = s, dot = true; *p; p++) {
- if (*p == '.') {
- if (dot)
- return false;
-
- dot = true;
- } else {
- if (!hostname_valid_char(*p))
- return false;
-
- dot = false;
- }
- }
-
- if (dot)
- return false;
-
- if (p-s > HOST_NAME_MAX)
- return false;
-
- return true;
-}
-
-char* hostname_cleanup(char *s, bool lowercase) {
- char *p, *d;
- bool dot;
-
- for (p = s, d = s, dot = true; *p; p++) {
- if (*p == '.') {
- if (dot)
- continue;
-
- *(d++) = '.';
- dot = true;
- } else if (hostname_valid_char(*p)) {
- *(d++) = lowercase ? tolower(*p) : *p;
- dot = false;
- }
-
- }
-
- if (dot && d > s)
- d[-1] = 0;
- else
- *d = 0;
-
- strshorten(s, HOST_NAME_MAX);
-
- return s;
-}
-
-bool machine_name_is_valid(const char *s) {
-
- if (!hostname_is_valid(s))
- return false;
-
- /* Machine names should be useful hostnames, but also be
- * useful in unit names, hence we enforce a stricter length
- * limitation. */
-
- if (strlen(s) > 64)
- return false;
-
- return true;
-}
-
int pipe_eof(int fd) {
struct pollfd pollfd = {
.fd = fd,
if (r < 0)
return -errno;
- if (r == 0)
- return 0;
-
- return pollfd.revents;
-}
-
-int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
- FILE *f;
- char *t;
- int r, fd;
-
- assert(path);
- assert(_f);
- assert(_temp_path);
-
- r = tempfn_xxxxxx(path, &t);
- if (r < 0)
- return r;
-
- fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
- if (fd < 0) {
- free(t);
- return -errno;
- }
-
- f = fdopen(fd, "we");
- if (!f) {
- unlink(t);
- free(t);
- return -errno;
- }
-
- *_f = f;
- *_temp_path = t;
-
- return 0;
-}
-
-int terminal_vhangup_fd(int fd) {
- assert(fd >= 0);
-
- if (ioctl(fd, TIOCVHANGUP) < 0)
- return -errno;
-
- return 0;
-}
-
-int terminal_vhangup(const char *name) {
- _cleanup_close_ int fd;
-
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- return terminal_vhangup_fd(fd);
-}
-
-int vt_disallocate(const char *name) {
- int fd, r;
- unsigned u;
-
- /* Deallocate the VT if possible. If not possible
- * (i.e. because it is the active one), at least clear it
- * entirely (including the scrollback buffer) */
-
- if (!startswith(name, "/dev/"))
- return -EINVAL;
-
- if (!tty_is_vc(name)) {
- /* So this is not a VT. I guess we cannot deallocate
- * it then. But let's at least clear the screen */
-
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ if (r == 0)
+ return 0;
- loop_write(fd,
- "\033[r" /* clear scrolling region */
- "\033[H" /* move home */
- "\033[2J", /* clear screen */
- 10, false);
- safe_close(fd);
+ return pollfd.revents;
+}
- return 0;
- }
+int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
+ FILE *f;
+ char *t;
+ int r, fd;
- if (!startswith(name, "/dev/tty"))
- return -EINVAL;
+ assert(path);
+ assert(_f);
+ assert(_temp_path);
- r = safe_atou(name+8, &u);
+ r = tempfn_xxxxxx(path, NULL, &t);
if (r < 0)
return r;
- if (u <= 0)
- return -EINVAL;
-
- /* Try to deallocate */
- fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- r = ioctl(fd, VT_DISALLOCATE, u);
- safe_close(fd);
-
- if (r >= 0)
- return 0;
-
- if (errno != EBUSY)
+ fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
+ if (fd < 0) {
+ free(t);
return -errno;
+ }
- /* Couldn't deallocate, so let's clear it fully with
- * scrollback */
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ f = fdopen(fd, "we");
+ if (!f) {
+ unlink_noerrno(t);
+ free(t);
+ safe_close(fd);
+ return -errno;
+ }
- loop_write(fd,
- "\033[r" /* clear scrolling region */
- "\033[H" /* move home */
- "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
- 10, false);
- safe_close(fd);
+ *_f = f;
+ *_temp_path = t;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int symlink_atomic(const char *from, const char *to) {
_cleanup_free_ char *t = NULL;
int r;
assert(from);
assert(to);
- r = tempfn_random(to, &t);
+ r = tempfn_random(to, NULL, &t);
if (r < 0)
return r;
return 0;
}
+int symlink_idempotent(const char *from, const char *to) {
+ _cleanup_free_ char *p = NULL;
+ int r;
+
+ assert(from);
+ assert(to);
+
+ if (symlink(from, to) < 0) {
+ if (errno != EEXIST)
+ return -errno;
+
+ r = readlink_malloc(to, &p);
+ if (r < 0)
+ return r;
+
+ if (!streq(p, from))
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
_cleanup_free_ char *t = NULL;
int r;
assert(path);
- r = tempfn_random(path, &t);
+ r = tempfn_random(path, NULL, &t);
if (r < 0)
return r;
assert(path);
- r = tempfn_random(path, &t);
+ r = tempfn_random(path, NULL, &t);
if (r < 0)
return r;
return 0;
}
+#endif // 0
bool display_is_local(const char *display) {
assert(display);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int in_group(const char *name) {
int r;
gid_t gid;
return k;
}
+#endif // 0
int dirent_ensure_type(DIR *d, struct dirent *de) {
struct stat st;
return cached > 0;
}
+/// UNNEEDED by elogind
+#if 0
int block_get_whole_disk(dev_t d, dev_t *ret) {
char *p, *s;
int r;
};
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, INT_MAX);
+#endif // 0
static const char *const sigchld_code_table[] = {
[CLD_EXITED] = "exited",
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
-static const char *const __signal_table[] = {
- [SIGHUP] = "HUP",
- [SIGINT] = "INT",
- [SIGQUIT] = "QUIT",
- [SIGILL] = "ILL",
- [SIGTRAP] = "TRAP",
- [SIGABRT] = "ABRT",
- [SIGBUS] = "BUS",
- [SIGFPE] = "FPE",
- [SIGKILL] = "KILL",
- [SIGUSR1] = "USR1",
- [SIGSEGV] = "SEGV",
- [SIGUSR2] = "USR2",
- [SIGPIPE] = "PIPE",
- [SIGALRM] = "ALRM",
- [SIGTERM] = "TERM",
-#ifdef SIGSTKFLT
- [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
-#endif
- [SIGCHLD] = "CHLD",
- [SIGCONT] = "CONT",
- [SIGSTOP] = "STOP",
- [SIGTSTP] = "TSTP",
- [SIGTTIN] = "TTIN",
- [SIGTTOU] = "TTOU",
- [SIGURG] = "URG",
- [SIGXCPU] = "XCPU",
- [SIGXFSZ] = "XFSZ",
- [SIGVTALRM] = "VTALRM",
- [SIGPROF] = "PROF",
- [SIGWINCH] = "WINCH",
- [SIGIO] = "IO",
- [SIGPWR] = "PWR",
- [SIGSYS] = "SYS"
-};
-
-DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
-
-const char *signal_to_string(int signo) {
- static thread_local char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1];
- const char *name;
-
- name = __signal_to_string(signo);
- if (name)
- return name;
-
- if (signo >= SIGRTMIN && signo <= SIGRTMAX)
- snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
- else
- snprintf(buf, sizeof(buf), "%d", signo);
-
- return buf;
-}
-
-int signal_from_string(const char *s) {
- int signo;
- int offset = 0;
- unsigned u;
-
- signo = __signal_from_string(s);
- if (signo > 0)
- return signo;
-
- if (startswith(s, "RTMIN+")) {
- s += 6;
- offset = SIGRTMIN;
- }
- if (safe_atou(s, &u) >= 0) {
- signo = (int) u + offset;
- if (signo > 0 && signo < _NSIG)
- return signo;
- }
- return -EINVAL;
-}
-
bool kexec_loaded(void) {
bool loaded = false;
char *s;
return loaded;
}
+/// UNNEEDED by elogind
+#if 0
int prot_from_flags(int flags) {
switch (flags & O_ACCMODE) {
return buf;
}
+#endif // 0
void* memdup(const void *p, size_t l) {
void *r;
/* Make sure we actually can kill the agent, if we need to, in
* case somebody invoked us from a shell script that trapped
* SIGTERM or so... */
- reset_all_signal_handlers();
- reset_signal_mask();
+ (void) reset_all_signal_handlers();
+ (void) reset_signal_mask();
/* Check whether our parent died before we were able
* to set the death signal and unblock the signals */
_exit(EXIT_FAILURE);
}
+/// UNNEEDED by elogind
+#if 0
int setrlimit_closest(int resource, const struct rlimit *rlim) {
struct rlimit highest, fixed;
return 0;
}
-int getenv_for_pid(pid_t pid, const char *field, char **_value) {
- _cleanup_fclose_ FILE *f = NULL;
- char *value = NULL;
- int r;
- bool done = false;
- size_t l;
- const char *path;
-
- assert(pid >= 0);
- assert(field);
- assert(_value);
-
- path = procfs_file_alloca(pid, "environ");
-
- f = fopen(path, "re");
- if (!f)
- return -errno;
-
- l = strlen(field);
- r = 0;
-
- do {
- char line[LINE_MAX];
- unsigned i;
-
- for (i = 0; i < sizeof(line)-1; i++) {
- int c;
-
- c = getc(f);
- if (_unlikely_(c == EOF)) {
- done = true;
- break;
- } else if (c == 0)
- break;
-
- line[i] = c;
- }
- line[i] = 0;
-
- if (memcmp(line, field, l) == 0 && line[l] == '=') {
- value = strdup(line + l + 1);
- if (!value)
- return -ENOMEM;
-
- r = 1;
- break;
- }
-
- } while (!done);
-
- *_value = value;
- return r;
-}
-
bool http_etag_is_valid(const char *etag) {
if (isempty(etag))
return false;
return true;
}
+#endif // 0
bool http_url_is_valid(const char *url) {
const char *p;
return saved;
}
-void warn_melody(void) {
- _cleanup_close_ int fd = -1;
-
- fd = open("/dev/console", O_WRONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
- return;
-
- /* Yeah, this is synchronous. Kinda sucks. But well... */
-
- ioctl(fd, KIOCSOUND, (int)(1193180/440));
- usleep(125*USEC_PER_MSEC);
-
- ioctl(fd, KIOCSOUND, (int)(1193180/220));
- usleep(125*USEC_PER_MSEC);
-
- ioctl(fd, KIOCSOUND, (int)(1193180/220));
- usleep(125*USEC_PER_MSEC);
-
- ioctl(fd, KIOCSOUND, 0);
-}
-
-int make_console_stdio(void) {
- int fd, r;
-
- /* Make /dev/console the controlling terminal and stdin/stdout/stderr */
-
- fd = acquire_terminal("/dev/console", false, true, true, USEC_INFINITY);
- if (fd < 0)
- return log_error_errno(fd, "Failed to acquire terminal: %m");
-
- r = make_stdio(fd);
- if (r < 0)
- return log_error_errno(r, "Failed to duplicate terminal fd: %m");
-
- return 0;
-}
-
int get_home_dir(char **_h) {
struct passwd *p;
const char *e;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int get_shell(char **_s) {
struct passwd *p;
const char *e;
*_s = s;
return 0;
}
+#endif // 0
bool filename_is_valid(const char *p) {
if (*t > 0 && *t < ' ')
return false;
- if (strchr("\\\"\'\0x7f", *t))
+ if (strchr("\\\"\'\x7f", *t))
return false;
}
if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
return false;
- if (strlen(p) > PATH_MAX)
+ if (strlen(p)+1 > PATH_MAX)
return false;
/* The following two checks are not really dangerous, but hey, they still are confusing */
return true;
}
+/// UNNEEDED by elogind
+#if 0
/* hey glibc, APIs with callbacks without a user pointer are so useless */
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
int (*compar) (const void *, const void *, void *), void *arg) {
setlocale(LC_ALL, "");
textdomain(GETTEXT_PACKAGE);
}
+#endif // 0
bool is_locale_utf8(void) {
const char *set;
/* Check result, but ignore the result if C was set
* explicitly. */
cached_answer =
- streq(set, "C") &&
+ STR_IN_SET(set, "C", "POSIX") &&
!getenv("LC_ALL") &&
!getenv("LC_CTYPE") &&
!getenv("LANG");
return draw_table[!is_locale_utf8()][ch];
}
+/// UNNEEDED by elogind
+#if 0
char *strreplace(const char *text, const char *old_string, const char *new_string) {
const char *f;
char *t, *r;
return found_online || !found_offline;
}
+#endif // 0
static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
char **i;
return search_and_fopen_internal(path, mode, root, copy, _f);
}
+/// UNNEEDED by elogind
+#if 0
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
_cleanup_strv_free_ char **s = NULL;
return search_and_fopen_internal(path, mode, root, s, _f);
}
+#endif // 0
char *strextend(char **x, ...) {
va_list ap;
return true;
}
+/// UNNEEDED by elogind
+#if 0
int split_pair(const char *s, const char *sep, char **l, char **r) {
char *x, *a, *b;
return parse_boolean(value) != 0;
}
+#endif // 0
int proc_cmdline(char **ret) {
assert(ret);
_cleanup_free_ char *word = NULL;
char *value = NULL;
- r = unquote_first_word(&p, &word, UNQUOTE_RELAX);
+ r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
if (r < 0)
return r;
if (r == 0)
_cleanup_free_ char *word = NULL;
const char *e;
- r = unquote_first_word(&p, &word, UNQUOTE_RELAX);
+ r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
if (r < 0)
return r;
if (r == 0)
assert(machine);
assert(pid);
+ if (!machine_name_is_valid(machine))
+ return -EINVAL;
+
p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(p, NEWLINE, "LEADER", &s, "CLASS", &class, NULL);
if (r == -ENOENT)
return 0;
}
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd) {
- _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1;
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd) {
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1, usernsfd = -1;
int rfd = -1;
assert(pid >= 0);
return -errno;
}
+ if (userns_fd) {
+ const char *userns;
+
+ userns = procfs_file_alloca(pid, "ns/user");
+ usernsfd = open(userns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (usernsfd < 0 && errno != ENOENT)
+ return -errno;
+ }
+
if (root_fd) {
const char *root;
if (netns_fd)
*netns_fd = netnsfd;
+ if (userns_fd)
+ *userns_fd = usernsfd;
+
if (root_fd)
*root_fd = rfd;
- pidnsfd = mntnsfd = netnsfd = -1;
+ pidnsfd = mntnsfd = netnsfd = usernsfd = -1;
return 0;
}
-int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd) {
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd) {
+ if (userns_fd >= 0) {
+ /* Can't setns to your own userns, since then you could
+ * escalate from non-root to root in your own namespace, so
+ * check if namespaces equal before attempting to enter. */
+ _cleanup_free_ char *userns_fd_path = NULL;
+ int r;
+ if (asprintf(&userns_fd_path, "/proc/self/fd/%d", userns_fd) < 0)
+ return -ENOMEM;
+
+ r = files_same(userns_fd_path, "/proc/self/ns/user");
+ if (r < 0)
+ return r;
+ if (r)
+ userns_fd = -1;
+ }
if (pidns_fd >= 0)
if (setns(pidns_fd, CLONE_NEWPID) < 0)
if (setns(netns_fd, CLONE_NEWNET) < 0)
return -errno;
+ if (userns_fd >= 0)
+ if (setns(userns_fd, CLONE_NEWUSER) < 0)
+ return -errno;
+
if (root_fd >= 0) {
if (fchdir(root_fd) < 0)
return -errno;
return -errno;
}
- if (setresgid(0, 0, 0) < 0)
- return -errno;
-
- if (setgroups(0, NULL) < 0)
- return -errno;
-
- if (setresuid(0, 0, 0) < 0)
- return -errno;
-
- return 0;
-}
-
-bool pid_is_unwaited(pid_t pid) {
- /* Checks whether a PID is still valid at all, including a zombie */
-
- if (pid <= 0)
- return false;
-
- if (kill(pid, 0) >= 0)
- return true;
-
- return errno != ESRCH;
-}
-
-bool pid_is_alive(pid_t pid) {
- int r;
-
- /* Checks whether a PID is still valid and not a zombie */
-
- if (pid <= 0)
- return false;
-
- r = get_process_state(pid);
- if (r == -ENOENT || r == 'Z')
- return false;
-
- return true;
+ return reset_uid_gid();
}
int getpeercred(int fd, struct ucred *ucred) {
return fd;
}
+/// UNNEEDED by elogind
+#if 0
int open_tmpfile(const char *path, int flags) {
char *p;
int fd;
#ifdef O_TMPFILE
/* Try O_TMPFILE first, if it is supported */
- fd = open(path, flags|O_TMPFILE, S_IRUSR|S_IWUSR);
+ fd = open(path, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
if (fd >= 0)
return fd;
#endif
unlink(p);
return fd;
}
+#endif // 0
int fd_warn_permissions(const char *path, int fd) {
struct stat st;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
unsigned long personality_from_string(const char *p) {
/* Parse a personality specifier. We introduce our own
return PER_LINUX;
#endif
- /* personality(7) documents that 0xffffffffUL is used for
- * querying the current personality, hence let's use that here
- * as error indicator. */
- return 0xffffffffUL;
+ return PERSONALITY_INVALID;
}
const char* personality_to_string(unsigned long p) {
return NULL;
}
+#endif // 0
uint64_t physical_memory(void) {
long mem;
return (uint64_t) mem * (uint64_t) page_size();
}
+/// UNNEEDED by elogind
+#if 0
void hexdump(FILE *f, const void *p, size_t s) {
const uint8_t *b = p;
unsigned n = 0;
if (param) {
- r = write_string_file(REBOOT_PARAM_FILE, param);
+ r = write_string_file(REBOOT_PARAM_FILE, param, WRITE_STRING_FILE_CREATE);
if (r < 0)
log_error("Failed to write reboot param to "
REBOOT_PARAM_FILE": %s", strerror(-r));
continue;
}
- p = cunescape(path);
- if (!p)
- return -ENOMEM;
+ r = cunescape(path, UNESCAPE_RELAX, &p);
+ if (r < 0)
+ return r;
if (!path_startswith(p, prefix))
continue;
continue;
}
- p = cunescape(path);
- if (!p)
- return -ENOMEM;
+ r = cunescape(path, UNESCAPE_RELAX, &p);
+ if (r < 0)
+ return r;
/* Let's ignore autofs mounts. If they aren't
* triggered yet, we want to avoid triggering
while ((x = set_steal_first(todo))) {
r = set_consume(done, x);
- if (r == -EEXIST)
+ if (r == -EEXIST || r == 0)
continue;
if (r < 0)
return r;
}
}
}
+#endif // 0
int fflush_and_check(FILE *f) {
assert(f);
return 0;
}
-int tempfn_xxxxxx(const char *p, char **ret) {
+int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
const char *fn;
char *t;
* /foo/bar/waldo
*
* Into this:
- * /foo/bar/.#waldoXXXXXX
+ * /foo/bar/.#<extra>waldoXXXXXX
*/
fn = basename(p);
if (!filename_is_valid(fn))
return -EINVAL;
- t = new(char, strlen(p) + 2 + 6 + 1);
+ if (extra == NULL)
+ extra = "";
+
+ t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
if (!t)
return -ENOMEM;
- strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), "XXXXXX");
+ strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
*ret = path_kill_slashes(t);
return 0;
}
-int tempfn_random(const char *p, char **ret) {
+int tempfn_random(const char *p, const char *extra, char **ret) {
const char *fn;
char *t, *x;
uint64_t u;
* /foo/bar/waldo
*
* Into this:
- * /foo/bar/.#waldobaa2a261115984a9
+ * /foo/bar/.#<extra>waldobaa2a261115984a9
*/
fn = basename(p);
if (!filename_is_valid(fn))
return -EINVAL;
- t = new(char, strlen(p) + 2 + 16 + 1);
+ if (!extra)
+ extra = "";
+
+ t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
if (!t)
return -ENOMEM;
- x = stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn);
+ x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
u = random_u64();
for (i = 0; i < 16; i++) {
return 0;
}
-int tempfn_random_child(const char *p, char **ret) {
+/// UNNEEDED by elogind
+#if 0
+int tempfn_random_child(const char *p, const char *extra, char **ret) {
char *t, *x;
uint64_t u;
unsigned i;
/* Turns this:
* /foo/bar/waldo
* Into this:
- * /foo/bar/waldo/.#3c2b6219aa75d7d0
+ * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
*/
- t = new(char, strlen(p) + 3 + 16 + 1);
+ if (!extra)
+ extra = "";
+
+ t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
if (!t)
return -ENOMEM;
- x = stpcpy(stpcpy(t, p), "/.#");
+ x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
u = random_u64();
for (i = 0; i < 16; i++) {
return 0;
}
-/* make sure the hostname is not "localhost" */
-bool is_localhost(const char *hostname) {
- assert(hostname);
-
- /* This tries to identify local host and domain names
- * described in RFC6761 plus the redhatism of .localdomain */
-
- return streq(hostname, "localhost") ||
- streq(hostname, "localhost.") ||
- streq(hostname, "localdomain.") ||
- streq(hostname, "localdomain") ||
- endswith(hostname, ".localhost") ||
- endswith(hostname, ".localhost.") ||
- endswith(hostname, ".localdomain") ||
- endswith(hostname, ".localdomain.");
-}
-
int take_password_lock(const char *root) {
struct flock flock = {
return !!S_ISLNK(info.st_mode);
}
+#endif // 0
int is_dir(const char* path, bool follow) {
struct stat st;
return !!S_ISDIR(st.st_mode);
}
-int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) {
+/// UNNEEDED by elogind
+#if 0
+int is_device_node(const char *path) {
+ struct stat info;
+
+ if (lstat(path, &info) < 0)
+ return -errno;
+
+ return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
+}
+#endif // 0
+
+int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
_cleanup_free_ char *s = NULL;
size_t allocated = 0, sz = 0;
int r;
SINGLE_QUOTE_ESCAPE,
DOUBLE_QUOTE,
DOUBLE_QUOTE_ESCAPE,
- SPACE,
+ SEPARATOR,
} state = START;
assert(p);
- assert(*p);
assert(ret);
+ if (!separators)
+ separators = WHITESPACE;
+
+ /* Bail early if called after last value or with no input */
+ if (!*p)
+ goto finish_force_terminate;
+
/* Parses the first word of a string, and returns it in
* *ret. Removes all quotes in the process. When parsing fails
* (because of an uneven number of quotes or similar), leaves
switch (state) {
case START:
+ if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
+ if (!GREEDY_REALLOC(s, allocated, sz+1))
+ return -ENOMEM;
+
if (c == 0)
- goto finish;
- else if (strchr(WHITESPACE, c))
+ goto finish_force_terminate;
+ else if (strchr(separators, c)) {
+ if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
+ (*p) ++;
+ goto finish_force_next;
+ }
break;
+ }
+
+ /* We found a non-blank character, so we will always
+ * want to return a string (even if it is empty),
+ * allocate it here. */
+ if (!GREEDY_REALLOC(s, allocated, sz+1))
+ return -ENOMEM;
state = VALUE;
/* fallthrough */
case VALUE:
if (c == 0)
- goto finish;
- else if (c == '\'')
+ goto finish_force_terminate;
+ else if (c == '\'' && (flags & EXTRACT_QUOTES))
state = SINGLE_QUOTE;
else if (c == '\\')
state = VALUE_ESCAPE;
- else if (c == '\"')
+ else if (c == '\"' && (flags & EXTRACT_QUOTES))
state = DOUBLE_QUOTE;
- else if (strchr(WHITESPACE, c))
- state = SPACE;
- else {
+ else if (strchr(separators, c)) {
+ if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
+ (*p) ++;
+ goto finish_force_next;
+ }
+ state = SEPARATOR;
+ } else {
if (!GREEDY_REALLOC(s, allocated, sz+2))
return -ENOMEM;
break;
- case VALUE_ESCAPE:
- if (c == 0) {
- if (flags & UNQUOTE_RELAX)
- goto finish;
- return -EINVAL;
- }
-
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
-
- if (flags & UNQUOTE_CUNESCAPE) {
- r = cunescape_one(*p, (size_t) -1, &c);
- if (r < 0)
- return -EINVAL;
-
- (*p) += r - 1;
- }
-
- s[sz++] = c;
- state = VALUE;
-
- break;
-
case SINGLE_QUOTE:
if (c == 0) {
- if (flags & UNQUOTE_RELAX)
- goto finish;
+ if (flags & EXTRACT_RELAX)
+ goto finish_force_terminate;
return -EINVAL;
} else if (c == '\'')
state = VALUE;
break;
- case SINGLE_QUOTE_ESCAPE:
- if (c == 0) {
- if (flags & UNQUOTE_RELAX)
- goto finish;
- return -EINVAL;
- }
-
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
-
- if (flags & UNQUOTE_CUNESCAPE) {
- r = cunescape_one(*p, (size_t) -1, &c);
- if (r < 0)
- return -EINVAL;
-
- (*p) += r - 1;
- }
-
- s[sz++] = c;
- state = SINGLE_QUOTE;
- break;
-
case DOUBLE_QUOTE:
if (c == 0)
return -EINVAL;
break;
+ case SINGLE_QUOTE_ESCAPE:
case DOUBLE_QUOTE_ESCAPE:
+ case VALUE_ESCAPE:
+ if (!GREEDY_REALLOC(s, allocated, sz+7))
+ return -ENOMEM;
+
if (c == 0) {
- if (flags & UNQUOTE_RELAX)
- goto finish;
+ if ((flags & EXTRACT_CUNESCAPE_RELAX) &&
+ (state == VALUE_ESCAPE || flags & EXTRACT_RELAX)) {
+ /* If we find an unquoted trailing backslash and we're in
+ * EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
+ * output.
+ *
+ * Unbalanced quotes will only be allowed in EXTRACT_RELAX
+ * mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
+ */
+ s[sz++] = '\\';
+ goto finish_force_terminate;
+ }
+ if (flags & EXTRACT_RELAX)
+ goto finish_force_terminate;
return -EINVAL;
}
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
+ if (flags & EXTRACT_CUNESCAPE) {
+ uint32_t u;
- if (flags & UNQUOTE_CUNESCAPE) {
- r = cunescape_one(*p, (size_t) -1, &c);
- if (r < 0)
+ r = cunescape_one(*p, (size_t) -1, &c, &u);
+ if (r < 0) {
+ if (flags & EXTRACT_CUNESCAPE_RELAX) {
+ s[sz++] = '\\';
+ s[sz++] = c;
+ goto end_escape;
+ }
return -EINVAL;
+ }
(*p) += r - 1;
- }
- s[sz++] = c;
- state = DOUBLE_QUOTE;
+ if (c != 0)
+ s[sz++] = c; /* normal explicit char */
+ else
+ sz += utf8_encode_unichar(s + sz, u); /* unicode chars we'll encode as utf8 */
+ } else
+ s[sz++] = c;
+
+end_escape:
+ state = (state == SINGLE_QUOTE_ESCAPE) ? SINGLE_QUOTE :
+ (state == DOUBLE_QUOTE_ESCAPE) ? DOUBLE_QUOTE :
+ VALUE;
break;
- case SPACE:
+ case SEPARATOR:
if (c == 0)
+ goto finish_force_terminate;
+ if (!strchr(separators, c))
goto finish;
- if (!strchr(WHITESPACE, c))
- goto finish;
-
break;
}
(*p) ++;
}
+finish_force_terminate:
+ *p = NULL;
finish:
if (!s) {
+ *p = NULL;
*ret = NULL;
return 0;
}
+finish_force_next:
s[sz] = 0;
*ret = s;
s = NULL;
return 1;
}
-int unquote_many_words(const char **p, UnquoteFlags flags, ...) {
+/// UNNEEDED by elogind
+#if 0
+int extract_first_word_and_warn(
+ const char **p,
+ char **ret,
+ const char *separators,
+ ExtractFlags flags,
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *rvalue) {
+ /* Try to unquote it, if it fails, warn about it and try again but this
+ * time using EXTRACT_CUNESCAPE_RELAX to keep the backslashes verbatim
+ * in invalid escape sequences. */
+ const char *save;
+ int r;
+
+ save = *p;
+ r = extract_first_word(p, ret, separators, flags);
+ if (r < 0 && !(flags&EXTRACT_CUNESCAPE_RELAX)) {
+ /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
+ *p = save;
+ r = extract_first_word(p, ret, separators, flags|EXTRACT_CUNESCAPE_RELAX);
+ if (r < 0)
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Unbalanced quoting in command line, ignoring: \"%s\"", rvalue);
+ else
+ log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
+ "Invalid escape sequences in command line: \"%s\"", rvalue);
+ }
+ return r;
+}
+
+int extract_many_words(const char **p, const char *separators, ExtractFlags flags, ...) {
va_list ap;
char **l;
int n = 0, i, c, r;
l = newa0(char*, n);
for (c = 0; c < n; c++) {
- r = unquote_first_word(p, &l[c], flags);
+ r = extract_first_word(p, &l[c], separators, flags);
if (r < 0) {
int j;
return c;
}
+#endif // 0
int free_and_strdup(char **p, const char *s) {
char *t;
/* Replaces a string pointer with an strdup()ed new string,
* possibly freeing the old one. */
+ if (streq_ptr(*p, s))
+ return 0;
+
if (s) {
t = strdup(s);
if (!t)
free(*p);
*p = t;
- return 0;
-}
-
-int sethostname_idempotent(const char *s) {
- int r;
- char buf[HOST_NAME_MAX + 1] = {};
-
- assert(s);
-
- r = gethostname(buf, sizeof(buf));
- if (r < 0)
- return -errno;
-
- if (streq(buf, s))
- return 0;
-
- r = sethostname(s, strlen(s));
- if (r < 0)
- return -errno;
-
return 1;
}
+/// UNNEEDED by elogind
+#if 0
int ptsname_malloc(int fd, char **ret) {
size_t l = 100;
}
int openpt_in_namespace(pid_t pid, int flags) {
- _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
_cleanup_close_pair_ int pair[2] = { -1, -1 };
union {
struct cmsghdr cmsghdr;
assert(pid > 0);
- r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &rootfd);
+ r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd);
if (r < 0)
return r;
pair[0] = safe_close(pair[0]);
- r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
+ r = namespace_enter(pidnsfd, mntnsfd, -1, usernsfd, rootfd);
if (r < 0)
_exit(EXIT_FAILURE);
if (master < 0)
_exit(EXIT_FAILURE);
+ if (unlockpt(master) < 0)
+ _exit(EXIT_FAILURE);
+
cmsg = CMSG_FIRSTHDR(&mh);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
return -errno;
- for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
+ CMSG_FOREACH(cmsg, &mh)
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
int *fds;
unsigned n_fds;
return -EIO;
}
+#endif // 0
ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags) {
_cleanup_close_ int fd = -1;
return parse_crtime(le, usec);
}
+/// UNNEEDED by elogind
+#if 0
int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags) {
le64_t le;
ssize_t n;
return 0;
}
-int chattr_fd(int fd, bool b, unsigned mask) {
- unsigned old_attr, new_attr;
+int same_fd(int a, int b) {
+ struct stat sta, stb;
+ pid_t pid;
+ int r, fa, fb;
- assert(fd >= 0);
+ assert(a >= 0);
+ assert(b >= 0);
- if (mask == 0)
- return 0;
+ /* Compares two file descriptors. Note that semantics are
+ * quite different depending on whether we have kcmp() or we
+ * don't. If we have kcmp() this will only return true for
+ * dup()ed file descriptors, but not otherwise. If we don't
+ * have kcmp() this will also return true for two fds of the same
+ * file, created by separate open() calls. Since we use this
+ * call mostly for filtering out duplicates in the fd store
+ * this difference hopefully doesn't matter too much. */
- if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
- return -errno;
+ if (a == b)
+ return true;
- if (b)
- new_attr = old_attr | mask;
- else
- new_attr = old_attr & ~mask;
+ /* Try to use kcmp() if we have it. */
+ pid = getpid();
+ r = kcmp(pid, pid, KCMP_FILE, a, b);
+ if (r == 0)
+ return true;
+ if (r > 0)
+ return false;
+ if (errno != ENOSYS)
+ return -errno;
- if (new_attr == old_attr)
- return 0;
+ /* We don't have kcmp(), use fstat() instead. */
+ if (fstat(a, &sta) < 0)
+ return -errno;
- if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0)
+ if (fstat(b, &stb) < 0)
return -errno;
- return 0;
-}
+ if ((sta.st_mode & S_IFMT) != (stb.st_mode & S_IFMT))
+ return false;
-int chattr_path(const char *p, bool b, unsigned mask) {
- _cleanup_close_ int fd = -1;
+ /* We consider all device fds different, since two device fds
+ * might refer to quite different device contexts even though
+ * they share the same inode and backing dev_t. */
- assert(p);
+ if (S_ISCHR(sta.st_mode) || S_ISBLK(sta.st_mode))
+ return false;
- if (mask == 0)
- return 0;
+ if (sta.st_dev != stb.st_dev || sta.st_ino != stb.st_ino)
+ return false;
- fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
- if (fd < 0)
+ /* The fds refer to the same inode on disk, let's also check
+ * if they have the same fd flags. This is useful to
+ * distinguish the read and write side of a pipe created with
+ * pipe(). */
+ fa = fcntl(a, F_GETFL);
+ if (fa < 0)
+ return -errno;
+
+ fb = fcntl(b, F_GETFL);
+ if (fb < 0)
return -errno;
- return chattr_fd(fd, b, mask);
+ return fa == fb;
}
+#endif // 0
-int change_attr_fd(int fd, unsigned value, unsigned mask) {
+int chattr_fd(int fd, unsigned value, unsigned mask) {
unsigned old_attr, new_attr;
+ struct stat st;
assert(fd >= 0);
+ if (fstat(fd, &st) < 0)
+ return -errno;
+
+ /* Explicitly check whether this is a regular file or
+ * directory. If it is anything else (such as a device node or
+ * fifo), then the ioctl will not hit the file systems but
+ * possibly drivers, where the ioctl might have different
+ * effects. Notably, DRM is using the same ioctl() number. */
+
+ if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
+ return -ENOTTY;
+
if (mask == 0)
return 0;
if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
return -errno;
- new_attr = (old_attr & ~mask) |(value & mask);
-
+ new_attr = (old_attr & ~mask) | (value & mask);
if (new_attr == old_attr)
return 0;
if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0)
return -errno;
- return 0;
-}
-
-int read_attr_fd(int fd, unsigned *ret) {
- assert(fd >= 0);
-
- if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0)
- return -errno;
-
- return 0;
+ return 1;
}
-int read_attr_path(const char *p, unsigned *ret) {
+/// UNNEEDED by elogind
+#if 0
+int chattr_path(const char *p, unsigned value, unsigned mask) {
_cleanup_close_ int fd = -1;
assert(p);
- assert(ret);
+
+ if (mask == 0)
+ return 0;
fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
if (fd < 0)
return -errno;
- return read_attr_fd(fd, ret);
+ return chattr_fd(fd, value, mask);
}
+#endif // 0
-int make_lock_file(const char *p, int operation, LockFile *ret) {
- _cleanup_close_ int fd = -1;
- _cleanup_free_ char *t = NULL;
- int r;
-
- /*
- * We use UNPOSIX locks if they are available. They have nice
- * semantics, and are mostly compatible with NFS. However,
- * they are only available on new kernels. When we detect we
- * are running on an older kernel, then we fall back to good
- * old BSD locks. They also have nice semantics, but are
- * slightly problematic on NFS, where they are upgraded to
- * POSIX locks, even though locally they are orthogonal to
- * POSIX locks.
- */
-
- t = strdup(p);
- if (!t)
- return -ENOMEM;
-
- for (;;) {
- struct flock fl = {
- .l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
- .l_whence = SEEK_SET,
- };
- struct stat st;
-
- fd = open(p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
- if (fd < 0)
- return -errno;
-
- r = fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl);
- if (r < 0) {
-
- /* If the kernel is too old, use good old BSD locks */
- if (errno == EINVAL)
- r = flock(fd, operation);
-
- if (r < 0)
- return errno == EAGAIN ? -EBUSY : -errno;
- }
-
- /* If we acquired the lock, let's check if the file
- * still exists in the file system. If not, then the
- * previous exclusive owner removed it and then closed
- * it. In such a case our acquired lock is worthless,
- * hence try again. */
+int read_attr_fd(int fd, unsigned *ret) {
+ struct stat st;
- r = fstat(fd, &st);
- if (r < 0)
- return -errno;
- if (st.st_nlink > 0)
- break;
+ assert(fd >= 0);
- fd = safe_close(fd);
- }
+ if (fstat(fd, &st) < 0)
+ return -errno;
- ret->path = t;
- ret->fd = fd;
- ret->operation = operation;
+ if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
+ return -ENOTTY;
- fd = -1;
- t = NULL;
+ if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0)
+ return -errno;
- return r;
+ return 0;
}
-int make_lock_file_for(const char *p, int operation, LockFile *ret) {
- const char *fn;
- char *t;
+/// UNNEEDED by elogind
+#if 0
+int read_attr_path(const char *p, unsigned *ret) {
+ _cleanup_close_ int fd = -1;
assert(p);
assert(ret);
- fn = basename(p);
- if (!filename_is_valid(fn))
- return -EINVAL;
-
- t = newa(char, strlen(p) + 2 + 4 + 1);
- stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck");
-
- return make_lock_file(t, operation, ret);
-}
-
-void release_lock_file(LockFile *f) {
- int r;
-
- if (!f)
- return;
-
- if (f->path) {
-
- /* If we are the exclusive owner we can safely delete
- * the lock file itself. If we are not the exclusive
- * owner, we can try becoming it. */
-
- if (f->fd >= 0 &&
- (f->operation & ~LOCK_NB) == LOCK_SH) {
- static const struct flock fl = {
- .l_type = F_WRLCK,
- .l_whence = SEEK_SET,
- };
-
- r = fcntl(f->fd, F_OFD_SETLK, &fl);
- if (r < 0 && errno == EINVAL)
- r = flock(f->fd, LOCK_EX|LOCK_NB);
-
- if (r >= 0)
- f->operation = LOCK_EX|LOCK_NB;
- }
-
- if ((f->operation & ~LOCK_NB) == LOCK_EX)
- unlink_noerrno(f->path);
-
- free(f->path);
- f->path = NULL;
- }
+ fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
+ if (fd < 0)
+ return -errno;
- f->fd = safe_close(f->fd);
- f->operation = 0;
+ return read_attr_fd(fd, ret);
}
static size_t nul_length(const uint8_t *p, size_t sz) {
return q - (const uint8_t*) p;
}
+#endif // 0
void sigkill_wait(pid_t *pid) {
if (!pid)
(void) wait_for_terminate(*pid, NULL);
}
+/// UNNEEDED by elogind
+#if 0
int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
int a = 0, b = 0, c = 0;
int k;
*p += k;
return 1;
}
+#endif // 0
ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) {
size_t i;
return -1;
}
+/// UNNEEDED by elogind
+#if 0
void cmsg_close_all(struct msghdr *mh) {
struct cmsghdr *cmsg;
assert(mh);
- for (cmsg = CMSG_FIRSTHDR(mh); cmsg; cmsg = CMSG_NXTHDR(mh, cmsg))
+ CMSG_FOREACH(cmsg, mh)
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
close_many((int*) CMSG_DATA(cmsg), (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
}
if (ret >= 0)
return 0;
- /* Even though renameat2() exists since Linux 3.15, btrfs added
- * support for it later. If it is not implemented, fallback to another
- * method. */
- if (errno != EINVAL)
+ /* renameat2() exists since Linux 3.15, btrfs added support for it later.
+ * If it is not implemented, fallback to another method. */
+ if (!IN_SET(errno, EINVAL, ENOSYS))
return -errno;
/* The link()/unlink() fallback does not work on directories. But
return 0;
}
+
+static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
+ assert(bad);
+
+ for (; *s; s++) {
+ if (*s == '\\' || strchr(bad, *s))
+ *(t++) = '\\';
+
+ *(t++) = *s;
+ }
+
+ return t;
+}
+
+char *shell_escape(const char *s, const char *bad) {
+ char *r, *t;
+
+ r = new(char, strlen(s)*2+1);
+ if (!r)
+ return NULL;
+
+ t = strcpy_backslash_escaped(r, s, bad);
+ *t = 0;
+
+ return r;
+}
+
+char *shell_maybe_quote(const char *s) {
+ const char *p;
+ char *r, *t;
+
+ assert(s);
+
+ /* Encloses a string in double quotes if necessary to make it
+ * OK as shell string. */
+
+ for (p = s; *p; p++)
+ if (*p <= ' ' ||
+ *p >= 127 ||
+ strchr(SHELL_NEED_QUOTES, *p))
+ break;
+
+ if (!*p)
+ return strdup(s);
+
+ r = new(char, 1+strlen(s)*2+1+1);
+ if (!r)
+ return NULL;
+
+ t = r;
+ *(t++) = '"';
+ t = mempcpy(t, s, p - s);
+
+ t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE);
+
+ *(t++)= '"';
+ *t = 0;
+
+ return r;
+}
+#endif // 0
+
+int parse_mode(const char *s, mode_t *ret) {
+ char *x;
+ long l;
+
+ assert(s);
+ assert(ret);
+
+ errno = 0;
+ l = strtol(s, &x, 8);
+ if (errno != 0)
+ return -errno;
+
+ if (!x || x == s || *x)
+ return -EINVAL;
+ if (l < 0 || l > 07777)
+ return -ERANGE;
+
+ *ret = (mode_t) l;
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int mount_move_root(const char *path) {
+ assert(path);
+
+ if (chdir(path) < 0)
+ return -errno;
+
+ if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
+ return -errno;
+
+ if (chroot(".") < 0)
+ return -errno;
+
+ if (chdir("/") < 0)
+ return -errno;
+
+ return 0;
+}
+#endif // 0
+
+int reset_uid_gid(void) {
+
+ if (setgroups(0, NULL) < 0)
+ return -errno;
+
+ if (setresgid(0, 0, 0) < 0)
+ return -errno;
+
+ if (setresuid(0, 0, 0) < 0)
+ return -errno;
+
+ return 0;
+}
+
+int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink) {
+ char *v;
+ size_t l;
+ ssize_t n;
+
+ assert(path);
+ assert(name);
+ assert(value);
+
+ for (l = 100; ; l = (size_t) n + 1) {
+ v = new0(char, l);
+ if (!v)
+ return -ENOMEM;
+
+ if (allow_symlink)
+ n = lgetxattr(path, name, v, l);
+ else
+ n = getxattr(path, name, v, l);
+
+ if (n >= 0 && (size_t) n < l) {
+ *value = v;
+ return n;
+ }
+
+ free(v);
+
+ if (n < 0 && errno != ERANGE)
+ return -errno;
+
+ if (allow_symlink)
+ n = lgetxattr(path, name, NULL, 0);
+ else
+ n = getxattr(path, name, NULL, 0);
+ if (n < 0)
+ return -errno;
+ }
+}
+
+int fgetxattr_malloc(int fd, const char *name, char **value) {
+ char *v;
+ size_t l;
+ ssize_t n;
+
+ assert(fd >= 0);
+ assert(name);
+ assert(value);
+
+ for (l = 100; ; l = (size_t) n + 1) {
+ v = new0(char, l);
+ if (!v)
+ return -ENOMEM;
+
+ n = fgetxattr(fd, name, v, l);
+
+ if (n >= 0 && (size_t) n < l) {
+ *value = v;
+ return n;
+ }
+
+ free(v);
+
+ if (n < 0 && errno != ERANGE)
+ return -errno;
+
+ n = fgetxattr(fd, name, NULL, 0);
+ if (n < 0)
+ return -errno;
+ }
+}
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
-#include <signal.h>
#include <sched.h>
#include <limits.h>
#include <sys/types.h>
#include <locale.h>
#include <mntent.h>
#include <sys/inotify.h>
-
-#if SIZEOF_PID_T == 4
-# define PID_PRI PRIi32
-#elif SIZEOF_PID_T == 2
-# define PID_PRI PRIi16
-#else
-# error Unknown pid_t size
-#endif
-#define PID_FMT "%" PID_PRI
-
-#if SIZEOF_UID_T == 4
-# define UID_FMT "%" PRIu32
-#elif SIZEOF_UID_T == 2
-# define UID_FMT "%" PRIu16
-#else
-# error Unknown uid_t size
-#endif
-
-#if SIZEOF_GID_T == 4
-# define GID_FMT "%" PRIu32
-#elif SIZEOF_GID_T == 2
-# define GID_FMT "%" PRIu16
-#else
-# error Unknown gid_t size
-#endif
-
-#if SIZEOF_TIME_T == 8
-# define PRI_TIME PRIi64
-#elif SIZEOF_TIME_T == 4
-# define PRI_TIME PRIu32
-#else
-# error Unknown time_t size
-#endif
-
-#if SIZEOF_RLIM_T == 8
-# define RLIM_FMT "%" PRIu64
-#elif SIZEOF_RLIM_T == 4
-# define RLIM_FMT "%" PRIu32
-#else
-# error Unknown rlim_t size
-#endif
+#include <sys/statfs.h>
#include "macro.h"
#include "missing.h"
#include "time-util.h"
+#include "formats-util.h"
/* What is interpreted as whitespace? */
#define WHITESPACE " \t\n\r"
#define FORMAT_BYTES_MAX 8
-#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
-#define ANSI_RED_ON "\x1B[31m"
-#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
-#define ANSI_GREEN_ON "\x1B[32m"
-#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
-#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
-#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
-#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
-#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
-
size_t page_size(void) _pure_;
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
bool streq_ptr(const char *a, const char *b) _pure_;
+int strcmp_ptr(const char *a, const char *b) _pure_;
#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
-#define malloc0(n) (calloc((n), 1))
+#define malloc0(n) (calloc(1, (n)))
+
+static inline void *mfree(void *memory) {
+ free(memory);
+ return NULL;
+}
static inline const char* yes_no(bool b) {
return b ? "yes" : "no";
}
char *endswith(const char *s, const char *postfix) _pure_;
+char *endswith_no_case(const char *s, const char *postfix) _pure_;
char *first_word(const char *s, const char *word) _pure_;
int parse_boolean(const char *v) _pure_;
int parse_pid(const char *s, pid_t* ret_pid);
int parse_uid(const char *s, uid_t* ret_uid);
-#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
+#define parse_gid(s, ret_gid) parse_uid(s, ret_gid)
+
+bool uid_is_valid(uid_t uid);
+#define gid_is_valid(gid) uid_is_valid(gid)
int safe_atou(const char *s, unsigned *ret_u);
int safe_atoi(const char *s, int *ret_i);
#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
-pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
-
char *strappend(const char *s, const char *suffix);
char *strnappend(const char *s, const char *suffix, size_t length);
-char *replace_env(const char *format, char **env);
-char **replace_env_argv(char **argv, char **env);
-
int readlinkat_malloc(int fd, const char *p, char **ret);
int readlink_malloc(const char *p, char **r);
-int readlink_value(const char *p, char **ret);
+// UNNEEDED int readlink_value(const char *p, char **ret);
int readlink_and_make_absolute(const char *p, char **r);
-int readlink_and_canonicalize(const char *p, char **r);
-
-int reset_all_signal_handlers(void);
-int reset_signal_mask(void);
+// UNNEEDED int readlink_and_canonicalize(const char *p, char **r);
char *strstrip(char *s);
-char *delete_chars(char *s, const char *bad);
+// UNNEEDED char *delete_chars(char *s, const char *bad);
char *truncate_nl(char *s);
char *file_in_same_dir(const char *path, const char *filename);
-int rmdir_parents(const char *path, const char *stop);
-
-int get_process_state(pid_t pid);
-int get_process_comm(pid_t pid, char **name);
-int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
-int get_process_exe(pid_t pid, char **name);
-int get_process_uid(pid_t pid, uid_t *uid);
-int get_process_gid(pid_t pid, gid_t *gid);
-int get_process_capeff(pid_t pid, char **capeff);
-int get_process_cwd(pid_t pid, char **cwd);
-int get_process_root(pid_t pid, char **root);
-int get_process_environ(pid_t pid, char **environ);
+// UNNEEDED int rmdir_parents(const char *path, const char *stop);
char hexchar(int x) _const_;
int unhexchar(char c) _const_;
int unoctchar(char c) _const_;
char decchar(int x) _const_;
int undecchar(char c) _const_;
+char base32hexchar(int x) _const_;
+int unbase32hexchar(char c) _const_;
+char base64char(int x) _const_;
+int unbase64char(char c) _const_;
char *cescape(const char *s);
-char *cunescape(const char *s);
-char *cunescape_length(const char *s, size_t length);
-char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix);
+size_t cescape_char(char c, char *buf);
+
+typedef enum UnescapeFlags {
+ UNESCAPE_RELAX = 1,
+} UnescapeFlags;
+
+int cunescape(const char *s, UnescapeFlags flags, char **ret);
+int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret);
+int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
char *xescape(const char *s, const char *bad);
-char *ascii_strlower(char *path);
+// UNNEEDED char *ascii_strlower(char *path);
bool dirent_is_file(const struct dirent *de) _pure_;
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
bool chars_intersect(const char *a, const char *b) _pure_;
-int make_stdio(int fd);
-int make_null_stdio(void);
-int make_console_stdio(void);
-
-int dev_urandom(void *p, size_t n);
-void random_bytes(void *p, size_t n);
-void initialize_srand(void);
-
-static inline uint64_t random_u64(void) {
- uint64_t u;
- random_bytes(&u, sizeof(u));
- return u;
-}
-
-static inline uint32_t random_u32(void) {
- uint32_t u;
- random_bytes(&u, sizeof(u));
- return u;
-}
-
/* For basic lookup tables with strictly enumerated entries */
#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
scope const char *name##_to_string(type i) { \
int close_all_fds(const int except[], unsigned n_except);
-bool fstype_is_network(const char *fstype);
-
-int chvt(int vt);
-
-int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
-int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
-int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
-
-int reset_terminal_fd(int fd, bool switch_to_text);
-int reset_terminal(const char *name);
-
-int open_terminal(const char *name, int mode);
-int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
-int release_terminal(void);
+// UNNEEDED bool fstype_is_network(const char *fstype);
int flush_fd(int fd);
-int ignore_signals(int sig, ...);
-int default_signals(int sig, ...);
-int sigaction_many(const struct sigaction *sa, ...);
-
int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
bool is_device_path(const char *path);
-int dir_is_empty(const char *path);
-char* dirname_malloc(const char *path);
-
-void rename_process(const char name[8]);
-
-void sigset_add_many(sigset_t *ss, ...);
-int sigprocmask_many(int how, ...);
-
-bool hostname_is_set(void);
+// UNNEEDED int dir_is_empty(const char *path);
+// UNNEEDED char* dirname_malloc(const char *path);
char* lookup_uid(uid_t uid);
-char* gethostname_malloc(void);
-char* getlogname_malloc(void);
-char* getusername_malloc(void);
-
-int getttyname_malloc(int fd, char **r);
-int getttyname_harder(int fd, char **r);
-
-int get_ctty_devnr(pid_t pid, dev_t *d);
-int get_ctty(pid_t, dev_t *_devnr, char **r);
+// UNNEEDED char* getlogname_malloc(void);
+// UNNEEDED char* getusername_malloc(void);
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
-int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
+// UNNEEDED int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
-int is_fd_on_temporary_fs(int fd);
-
-int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
-int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
-int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
-int rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
+bool is_temporary_fs(const struct statfs *s) _pure_;
+int fd_is_temporary_fs(int fd);
int pipe_eof(int fd);
-cpu_set_t* cpu_set_malloc(unsigned *ncpus);
+DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE);
+#define _cleanup_cpu_free_ _cleanup_(CPU_FREEp)
-int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
-int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
+// UNNEEDED cpu_set_t* cpu_set_malloc(unsigned *ncpus);
#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
-int fd_columns(int fd);
-unsigned columns(void);
-int fd_lines(int fd);
-unsigned lines(void);
-void columns_lines_cache_reset(int _unused_ signum);
-
-bool on_tty(void);
-
-static inline const char *ansi_highlight(void) {
- return on_tty() ? ANSI_HIGHLIGHT_ON : "";
-}
-
-static inline const char *ansi_highlight_red(void) {
- return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
-}
-
-static inline const char *ansi_highlight_green(void) {
- return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
-}
-
-static inline const char *ansi_highlight_yellow(void) {
- return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
-}
-
-static inline const char *ansi_highlight_blue(void) {
- return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
-}
-
-static inline const char *ansi_highlight_off(void) {
- return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
-}
-
int files_same(const char *filea, const char *fileb);
int running_in_chroot(void);
int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
int touch(const char *path);
-char *unquote(const char *s, const char *quotes);
-char *normalize_env_assignment(const char *s);
-
-int wait_for_terminate(pid_t pid, siginfo_t *status);
-int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code);
-
noreturn void freeze(void);
bool null_or_empty(struct stat *st) _pure_;
int null_or_empty_path(const char *fn);
-int null_or_empty_fd(int fd);
+// UNNEEDED int null_or_empty_fd(int fd);
DIR *xopendirat(int dirfd, const char *name, int flags);
-char *fstab_node_to_udev_node(const char *p);
-
-char *resolve_dev_console(char **active);
-bool tty_is_vc(const char *tty);
-bool tty_is_vc_resolve(const char *tty);
-bool tty_is_console(const char *tty) _pure_;
-int vtnr_from_tty(const char *tty);
-const char *default_term_for_tty(const char *tty);
+// UNNEEDED char *fstab_node_to_udev_node(const char *p);
void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
-int kill_and_sigcont(pid_t pid, int sig);
-
bool nulstr_contains(const char*nulstr, const char *needle);
-bool plymouth_running(void);
-
-bool hostname_is_valid(const char *s) _pure_;
-char* hostname_cleanup(char *s, bool lowercase);
-
-bool machine_name_is_valid(const char *s) _pure_;
+// UNNEEDED bool plymouth_running(void);
char* strshorten(char *s, size_t l);
-int terminal_vhangup_fd(int fd);
-int terminal_vhangup(const char *name);
-
-int vt_disallocate(const char *name);
+// UNNEEDED int symlink_idempotent(const char *from, const char *to);
-int symlink_atomic(const char *from, const char *to);
-int mknod_atomic(const char *path, mode_t mode, dev_t dev);
-int mkfifo_atomic(const char *path, mode_t mode);
+// UNNEEDED int symlink_atomic(const char *from, const char *to);
+// UNNEEDED int mknod_atomic(const char *path, mode_t mode, dev_t dev);
+// UNNEEDED int mkfifo_atomic(const char *path, mode_t mode);
int fchmod_umask(int fd, mode_t mode);
int get_group_creds(const char **groupname, gid_t *gid);
int in_gid(gid_t gid);
-int in_group(const char *name);
+// UNNEEDED int in_group(const char *name);
char* uid_to_name(uid_t uid);
char* gid_to_name(gid_t gid);
-int glob_exists(const char *path);
-int glob_extend(char ***strv, const char *path);
+// UNNEEDED int glob_exists(const char *path);
+// UNNEEDED int glob_extend(char ***strv, const char *path);
int dirent_ensure_type(DIR *d, struct dirent *de);
return s[strspn(s, charset)] == '\0';
}
-int block_get_whole_disk(dev_t d, dev_t *ret);
+// UNNEEDED int block_get_whole_disk(dev_t d, dev_t *ret);
#define NULSTR_FOREACH(i, l) \
for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
int ip_tos_to_string_alloc(int i, char **s);
int ip_tos_from_string(const char *s);
-const char *signal_to_string(int i) _const_;
-int signal_from_string(const char *s) _pure_;
-
-int signal_from_string_try_harder(const char *s);
-
extern int saved_argc;
extern char **saved_argv;
bool kexec_loaded(void);
-int prot_from_flags(int flags) _const_;
+// UNNEEDED int prot_from_flags(int flags) _const_;
-char *format_bytes(char *buf, size_t l, off_t t);
+// UNNEEDED char *format_bytes(char *buf, size_t l, off_t t);
int fd_wait_for_event(int fd, int event, usec_t timeout);
void* memdup(const void *p, size_t l) _alloc_(2);
-int is_kernel_thread(pid_t pid);
-
int fd_inc_sndbuf(int fd, size_t n);
int fd_inc_rcvbuf(int fd, size_t n);
int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
-int setrlimit_closest(int resource, const struct rlimit *rlim);
-
-int getenv_for_pid(pid_t pid, const char *field, char **_value);
+// UNNEEDED int setrlimit_closest(int resource, const struct rlimit *rlim);
bool http_url_is_valid(const char *url) _pure_;
bool documentation_url_is_valid(const char *url) _pure_;
-bool http_etag_is_valid(const char *etag);
+// UNNEEDED bool http_etag_is_valid(const char *etag);
bool in_initrd(void);
-void warn_melody(void);
-
int get_home_dir(char **ret);
-int get_shell(char **_ret);
+// UNNEEDED int get_shell(char **_ret);
static inline void freep(void *p) {
free(*(void**) p);
return !!strpbrk(p, GLOB_CHARS);
}
-void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
- int (*compar) (const void *, const void *, void *),
- void *arg);
+// UNNEEDED void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
+// UNNEEDED int (*compar) (const void *, const void *, void *),
+// UNNEEDED void *arg);
#define _(String) gettext (String)
-void init_gettext(void);
+#define N_(String) String
+// UNNEEDED void init_gettext(void);
bool is_locale_utf8(void);
typedef enum DrawSpecialChar {
const char *draw_special_char(DrawSpecialChar ch);
-char *strreplace(const char *text, const char *old_string, const char *new_string);
+// UNNEEDED char *strreplace(const char *text, const char *old_string, const char *new_string);
-char *strip_tab_ansi(char **p, size_t *l);
+// UNNEEDED char *strip_tab_ansi(char **p, size_t *l);
-int on_ac_power(void);
+// UNNEEDED int on_ac_power(void);
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
-int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
+// UNNEEDED int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
#define FOREACH_LINE(line, f, on_error) \
for (;;) \
}
char *hexmem(const void *p, size_t l);
-void *unhexmem(const char *p, size_t l);
+int unhexmem(const char *p, size_t l, void **mem, size_t *len);
+
+char *base32hexmem(const void *p, size_t l, bool padding);
+int unbase32hexmem(const char *p, size_t l, bool padding, void **mem, size_t *len);
+
+char *base64mem(const void *p, size_t l);
+int unbase64mem(const char *p, size_t l, void **mem, size_t *len);
char *strextend(char **x, ...) _sentinel_;
char *strrep(const char *s, unsigned n);
_d_; \
})
-#define procfs_file_alloca(pid, field) \
- ({ \
- pid_t _pid_ = (pid); \
- const char *_r_; \
- if (_pid_ == 0) { \
- _r_ = ("/proc/self/" field); \
- } else { \
- _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
- sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
- } \
- _r_; \
- })
-
bool id128_is_valid(const char *s) _pure_;
-int split_pair(const char *s, const char *sep, char **l, char **r);
+// UNNEEDED int split_pair(const char *s, const char *sep, char **l, char **r);
-int shall_restore_state(void);
+// UNNEEDED int shall_restore_state(void);
/**
* Normal qsort requires base to be nonnull. Here were require
* that only if nmemb > 0.
*/
-static inline void qsort_safe(void *base, size_t nmemb, size_t size,
- int (*compar)(const void *, const void *)) {
- if (nmemb) {
- assert(base);
- qsort(base, nmemb, size, compar);
- }
+static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
+ if (nmemb <= 1)
+ return;
+
+ assert(base);
+ qsort(base, nmemb, size, compar);
+}
+
+/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
+static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
+
+ if (needlelen <= 0)
+ return (void*) haystack;
+
+ if (haystacklen < needlelen)
+ return NULL;
+
+ assert(haystack);
+ assert(needle);
+
+ return memmem(haystack, haystacklen, needle, needlelen);
}
int proc_cmdline(char **ret);
int container_get_leader(const char *machine, pid_t *pid);
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd);
-int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd);
-
-bool pid_is_alive(pid_t pid);
-bool pid_is_unwaited(pid_t pid);
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
int getpeercred(int fd, struct ucred *ucred);
int getpeersec(int fd, char **ret);
int writev_safe(int fd, const struct iovec *w, int j);
int mkostemp_safe(char *pattern, int flags);
-int open_tmpfile(const char *path, int flags);
+// UNNEEDED int open_tmpfile(const char *path, int flags);
int fd_warn_permissions(const char *path, int fd);
-unsigned long personality_from_string(const char *p);
-const char *personality_to_string(unsigned long);
+#ifndef PERSONALITY_INVALID
+/* personality(7) documents that 0xffffffffUL is used for querying the
+ * current personality, hence let's use that here as error
+ * indicator. */
+#define PERSONALITY_INVALID 0xffffffffLU
+#endif
+
+// UNNEEDED unsigned long personality_from_string(const char *p);
+// UNNEEDED const char *personality_to_string(unsigned long);
uint64_t physical_memory(void);
-void hexdump(FILE *f, const void *p, size_t s);
+// UNNEEDED void hexdump(FILE *f, const void *p, size_t s);
union file_handle_union {
struct file_handle handle;
};
#define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
-int update_reboot_param_file(const char *param);
+// UNNEEDED int update_reboot_param_file(const char *param);
-int umount_recursive(const char *target, int flags);
+// UNNEEDED int umount_recursive(const char *target, int flags);
-int bind_remount_recursive(const char *prefix, bool ro);
+// UNNEEDED int bind_remount_recursive(const char *prefix, bool ro);
int fflush_and_check(FILE *f);
-int tempfn_xxxxxx(const char *p, char **ret);
-int tempfn_random(const char *p, char **ret);
-int tempfn_random_child(const char *p, char **ret);
-
-bool is_localhost(const char *hostname);
+int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
+int tempfn_random(const char *p, const char *extra, char **ret);
+// UNNEEDED int tempfn_random_child(const char *p, const char *extra, char **ret);
-int take_password_lock(const char *root);
+// UNNEEDED int take_password_lock(const char *root);
-int is_symlink(const char *path);
+// UNNEEDED int is_symlink(const char *path);
int is_dir(const char *path, bool follow);
+// UNNEEDED int is_device_node(const char *path);
-typedef enum UnquoteFlags{
- UNQUOTE_RELAX = 1,
- UNQUOTE_CUNESCAPE = 2,
-} UnquoteFlags;
+typedef enum ExtractFlags {
+ EXTRACT_RELAX = 1,
+ EXTRACT_CUNESCAPE = 2,
+ EXTRACT_CUNESCAPE_RELAX = 4,
+ EXTRACT_QUOTES = 8,
+ EXTRACT_DONT_COALESCE_SEPARATORS = 16,
+} ExtractFlags;
-int unquote_first_word(const char **p, char **ret, UnquoteFlags flags);
-int unquote_many_words(const char **p, UnquoteFlags flags, ...) _sentinel_;
+int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags);
+// UNNEEDED int extract_first_word_and_warn(const char **p, char **ret, const char *separators, ExtractFlags flags, const char *unit, const char *filename, unsigned line, const char *rvalue);
+// UNNEEDED int extract_many_words(const char **p, const char *separators, ExtractFlags flags, ...) _sentinel_;
-int free_and_strdup(char **p, const char *s);
+static inline void free_and_replace(char **s, char *v) {
+ free(*s);
+ *s = v;
+}
-int sethostname_idempotent(const char *s);
+int free_and_strdup(char **p, const char *s);
#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
uint8_t raw[INOTIFY_EVENT_MAX];
};
-#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
+#ifdef __GLIBC__
+ #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
+#else
+ #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), 0)
+#endif
-int ptsname_malloc(int fd, char **ret);
+// UNNEEDED int ptsname_malloc(int fd, char **ret);
-int openpt_in_namespace(pid_t pid, int flags);
+// UNNEEDED int openpt_in_namespace(pid_t pid, int flags);
ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
-int fd_setcrtime(int fd, usec_t usec);
+// UNNEEDED int fd_setcrtime(int fd, usec_t usec);
int fd_getcrtime(int fd, usec_t *usec);
-int path_getcrtime(const char *p, usec_t *usec);
-int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
+// UNNEEDED int path_getcrtime(const char *p, usec_t *usec);
+// UNNEEDED int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
+
+// UNNEEDED int same_fd(int a, int b);
-int chattr_fd(int fd, bool b, unsigned mask);
-int chattr_path(const char *p, bool b, unsigned mask);
-int change_attr_fd(int fd, unsigned value, unsigned mask);
+int chattr_fd(int fd, unsigned value, unsigned mask);
+// UNNEEDED int chattr_path(const char *p, unsigned value, unsigned mask);
int read_attr_fd(int fd, unsigned *ret);
-int read_attr_path(const char *p, unsigned *ret);
+// UNNEEDED int read_attr_path(const char *p, unsigned *ret);
+
+#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
-typedef struct LockFile {
- char *path;
- int fd;
- int operation;
-} LockFile;
+// UNNEEDED ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
-int make_lock_file(const char *p, int operation, LockFile *ret);
-int make_lock_file_for(const char *p, int operation, LockFile *ret);
-void release_lock_file(LockFile *f);
+void sigkill_wait(pid_t *pid);
+#define _cleanup_sigkill_wait_ _cleanup_(sigkill_wait)
-#define _cleanup_release_lock_file_ _cleanup_(release_lock_file)
+// UNNEEDED int syslog_parse_priority(const char **p, int *priority, bool with_facility);
-#define LOCK_FILE_INIT { .fd = -1, .path = NULL }
+// UNNEEDED void cmsg_close_all(struct msghdr *mh);
-#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
+// UNNEEDED int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
-ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
+// UNNEEDED char *shell_escape(const char *s, const char *bad);
+// UNNEEDED char *shell_maybe_quote(const char *s);
-void sigkill_wait(pid_t *pid);
-#define _cleanup_sigkill_wait_ _cleanup_(sigkill_wait)
+int parse_mode(const char *s, mode_t *ret);
-int syslog_parse_priority(const char **p, int *priority, bool with_facility);
+// UNNEEDED int mount_move_root(const char *path);
-void cmsg_close_all(struct msghdr *mh);
+int reset_uid_gid(void);
-int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
+int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink);
+int fgetxattr_malloc(int fd, const char *name, char **value);
#include <unistd.h>
#include "util.h"
+#include "process-util.h"
#include "virt.h"
#include "fileio.h"
"VMW\0" "vmware\0"
"innotek GmbH\0" "oracle\0"
"Xen\0" "xen\0"
- "Bochs\0" "bochs\0";
+ "Bochs\0" "bochs\0"
+ "Parallels\0" "parallels\0";
unsigned i;
for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
_cleanup_free_ char *domcap = NULL, *cpuinfo_contents = NULL;
static thread_local int cached_found = -1;
static thread_local const char *cached_id = NULL;
- const char *_id = NULL;
+ const char *_id = NULL, *_id_cpuid = NULL;
int r;
if (_likely_(cached_found >= 0)) {
/* this will set _id to "other" and return 0 for unknown hypervisors */
r = detect_vm_cpuid(&_id);
- if (r != 0)
+
+ /* finish when found a known hypervisor other than kvm */
+ if (r < 0 || (r > 0 && !streq(_id, "kvm")))
goto finish;
+ _id_cpuid = _id;
+
r = detect_vm_dmi(&_id);
+
+ /* kvm with and without Virtualbox */
+ /* Parallels exports KVMKVMKVM leaf */
+ if (streq_ptr(_id_cpuid, "kvm")) {
+ if (r > 0 && (streq(_id, "oracle") || streq(_id, "parallels")))
+ goto finish;
+
+ _id = _id_cpuid;
+ r = 1;
+ goto finish;
+ }
+
+ /* information from dmi */
if (r != 0)
goto finish;
return r;
}
+/// UNNEEDED by elogind
+#if 0
/* Returns a short identifier for the various VM/container implementations */
int detect_virtualization(const char **id) {
int r;
return VIRTUALIZATION_NONE;
}
+#endif // 0
_VIRTUALIZATION_INVALID = -1
};
-int detect_virtualization(const char **id);
+// UNNEEDED int detect_virtualization(const char **id);
#include "bus-util.h"
int main(int argc, char *argv[]) {
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+ _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
int r;
if (argc != 2) {
return EXIT_FAILURE;
}
+ elogind_set_program_name(argv[0]);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
+#if 0
+ /* We send this event to the private D-Bus socket and then the
+ * system instance will forward this to the system bus. We do
+ * this to avoid an activation loop when we start dbus when we
+ * are called when the dbus service is shut down. */
+
+ r = bus_open_system_systemd(&bus);
+#else
/* Unlike in systemd where this has to use a private socket,
- since logind doesn't associate control groups with services
+ since elogind doesn't associate control groups with services
and doesn't manage the dbus service, we can just use the
system bus. */
r = sd_bus_open_system(&bus);
+#endif // 0
+
if (r < 0) {
+#if 0
+ /* If we couldn't connect we assume this was triggered
+ * while systemd got restarted/transitioned from
+ * initrd to the system, so let's ignore this */
+ log_debug_errno(r, "Failed to get D-Bus connection: %m");
+#else
+ /* If dbus isn't running or responding, there is nothing
+ * we can do about it. */
log_debug_errno(r, "Failed to open system bus: %m");
+#endif // 0
return EXIT_FAILURE;
}
r = sd_bus_emit_signal(bus,
- "/org/freedesktop/systemd1/agent",
- "org.freedesktop.systemd1.Agent",
+ "/org/freedesktop/elogind/agent",
+ "org.freedesktop.elogind.Agent",
"Released",
"s", argv[1]);
if (r < 0) {
+#if 0
+ log_debug_errno(r, "Failed to send signal message on private connection: %m");
+#else
log_debug_errno(r, "Failed to send signal message: %m");
+#endif // 0
return EXIT_FAILURE;
}
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2013 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <fcntl.h>
+#include <fnmatch.h>
+
+#include "process-util.h"
+#include "path-util.h"
+// #include "special.h"
+#include "cgroup-util.h"
+#include "cgroup.h"
+
+#define CGROUP_CPU_QUOTA_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
+
+// UNNEEDED by elogind
+#if 0
+void cgroup_context_init(CGroupContext *c) {
+ assert(c);
+
+ /* Initialize everything to the kernel defaults, assuming the
+ * structure is preinitialized to 0 */
+
+ c->cpu_shares = (unsigned long) -1;
+ c->startup_cpu_shares = (unsigned long) -1;
+ c->memory_limit = (uint64_t) -1;
+ c->blockio_weight = (unsigned long) -1;
+ c->startup_blockio_weight = (unsigned long) -1;
+
+ c->cpu_quota_per_sec_usec = USEC_INFINITY;
+}
+
+void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) {
+ assert(c);
+ assert(a);
+
+ LIST_REMOVE(device_allow, c->device_allow, a);
+ free(a->path);
+ free(a);
+}
+
+void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w) {
+ assert(c);
+ assert(w);
+
+ LIST_REMOVE(device_weights, c->blockio_device_weights, w);
+ free(w->path);
+ free(w);
+}
+
+void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b) {
+ assert(c);
+ assert(b);
+
+ LIST_REMOVE(device_bandwidths, c->blockio_device_bandwidths, b);
+ free(b->path);
+ free(b);
+}
+
+void cgroup_context_done(CGroupContext *c) {
+ assert(c);
+
+ while (c->blockio_device_weights)
+ cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
+
+ while (c->blockio_device_bandwidths)
+ cgroup_context_free_blockio_device_bandwidth(c, c->blockio_device_bandwidths);
+
+ while (c->device_allow)
+ cgroup_context_free_device_allow(c, c->device_allow);
+}
+
+void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
+ CGroupBlockIODeviceBandwidth *b;
+ CGroupBlockIODeviceWeight *w;
+ CGroupDeviceAllow *a;
+ char u[FORMAT_TIMESPAN_MAX];
+
+ assert(c);
+ assert(f);
+
+ prefix = strempty(prefix);
+
+ fprintf(f,
+ "%sCPUAccounting=%s\n"
+ "%sBlockIOAccounting=%s\n"
+ "%sMemoryAccounting=%s\n"
+ "%sCPUShares=%lu\n"
+ "%sStartupCPUShares=%lu\n"
+ "%sCPUQuotaPerSecSec=%s\n"
+ "%sBlockIOWeight=%lu\n"
+ "%sStartupBlockIOWeight=%lu\n"
+ "%sMemoryLimit=%" PRIu64 "\n"
+ "%sDevicePolicy=%s\n"
+ "%sDelegate=%s\n",
+ prefix, yes_no(c->cpu_accounting),
+ prefix, yes_no(c->blockio_accounting),
+ prefix, yes_no(c->memory_accounting),
+ prefix, c->cpu_shares,
+ prefix, c->startup_cpu_shares,
+ prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1),
+ prefix, c->blockio_weight,
+ prefix, c->startup_blockio_weight,
+ prefix, c->memory_limit,
+ prefix, cgroup_device_policy_to_string(c->device_policy),
+ prefix, yes_no(c->delegate));
+
+ LIST_FOREACH(device_allow, a, c->device_allow)
+ fprintf(f,
+ "%sDeviceAllow=%s %s%s%s\n",
+ prefix,
+ a->path,
+ a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : "");
+
+ LIST_FOREACH(device_weights, w, c->blockio_device_weights)
+ fprintf(f,
+ "%sBlockIODeviceWeight=%s %lu",
+ prefix,
+ w->path,
+ w->weight);
+
+ LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
+ char buf[FORMAT_BYTES_MAX];
+
+ fprintf(f,
+ "%s%s=%s %s\n",
+ prefix,
+ b->read ? "BlockIOReadBandwidth" : "BlockIOWriteBandwidth",
+ b->path,
+ format_bytes(buf, sizeof(buf), b->bandwidth));
+ }
+}
+
+static int lookup_blkio_device(const char *p, dev_t *dev) {
+ struct stat st;
+ int r;
+
+ assert(p);
+ assert(dev);
+
+ r = stat(p, &st);
+ if (r < 0)
+ return log_warning_errno(errno, "Couldn't stat device %s: %m", p);
+
+ if (S_ISBLK(st.st_mode))
+ *dev = st.st_rdev;
+ else if (major(st.st_dev) != 0) {
+ /* If this is not a device node then find the block
+ * device this file is stored on */
+ *dev = st.st_dev;
+
+ /* If this is a partition, try to get the originating
+ * block device */
+ block_get_whole_disk(*dev, dev);
+ } else {
+ log_warning("%s is not a block device and file system block device cannot be determined or is not local.", p);
+ return -ENODEV;
+ }
+
+ return 0;
+}
+
+static int whitelist_device(const char *path, const char *node, const char *acc) {
+ char buf[2+DECIMAL_STR_MAX(dev_t)*2+2+4];
+ struct stat st;
+ int r;
+
+ assert(path);
+ assert(acc);
+
+ if (stat(node, &st) < 0) {
+ log_warning("Couldn't stat device %s", node);
+ return -errno;
+ }
+
+ if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
+ log_warning("%s is not a device.", node);
+ return -ENODEV;
+ }
+
+ sprintf(buf,
+ "%c %u:%u %s",
+ S_ISCHR(st.st_mode) ? 'c' : 'b',
+ major(st.st_rdev), minor(st.st_rdev),
+ acc);
+
+ r = cg_set_attribute("devices", path, "devices.allow", buf);
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set devices.allow on %s: %m", path);
+
+ return r;
+}
+
+static int whitelist_major(const char *path, const char *name, char type, const char *acc) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char line[LINE_MAX];
+ bool good = false;
+ int r;
+
+ assert(path);
+ assert(acc);
+ assert(type == 'b' || type == 'c');
+
+ f = fopen("/proc/devices", "re");
+ if (!f)
+ return log_warning_errno(errno, "Cannot open /proc/devices to resolve %s (%c): %m", name, type);
+
+ FOREACH_LINE(line, f, goto fail) {
+ char buf[2+DECIMAL_STR_MAX(unsigned)+3+4], *p, *w;
+ unsigned maj;
+
+ truncate_nl(line);
+
+ if (type == 'c' && streq(line, "Character devices:")) {
+ good = true;
+ continue;
+ }
+
+ if (type == 'b' && streq(line, "Block devices:")) {
+ good = true;
+ continue;
+ }
+
+ if (isempty(line)) {
+ good = false;
+ continue;
+ }
+
+ if (!good)
+ continue;
+
+ p = strstrip(line);
+
+ w = strpbrk(p, WHITESPACE);
+ if (!w)
+ continue;
+ *w = 0;
+
+ r = safe_atou(p, &maj);
+ if (r < 0)
+ continue;
+ if (maj <= 0)
+ continue;
+
+ w++;
+ w += strspn(w, WHITESPACE);
+
+ if (fnmatch(name, w, 0) != 0)
+ continue;
+
+ sprintf(buf,
+ "%c %u:* %s",
+ type,
+ maj,
+ acc);
+
+ r = cg_set_attribute("devices", path, "devices.allow", buf);
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set devices.allow on %s: %m", path);
+ }
+
+ return 0;
+
+fail:
+ log_warning_errno(errno, "Failed to read /proc/devices: %m");
+ return -errno;
+}
+
+void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, ManagerState state) {
+ bool is_root;
+ int r;
+
+ assert(c);
+ assert(path);
+
+ if (mask == 0)
+ return;
+
+ /* Some cgroup attributes are not supported on the root cgroup,
+ * hence silently ignore */
+ is_root = isempty(path) || path_equal(path, "/");
+ if (is_root)
+ /* Make sure we don't try to display messages with an empty path. */
+ path = "/";
+
+ /* We generally ignore errors caused by read-only mounted
+ * cgroup trees (assuming we are running in a container then),
+ * and missing cgroups, i.e. EROFS and ENOENT. */
+
+ if ((mask & CGROUP_MASK_CPU) && !is_root) {
+ char buf[MAX(DECIMAL_STR_MAX(unsigned long), DECIMAL_STR_MAX(usec_t)) + 1];
+
+ sprintf(buf, "%lu\n",
+ IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_cpu_shares != (unsigned long) -1 ? c->startup_cpu_shares :
+ c->cpu_shares != (unsigned long) -1 ? c->cpu_shares : 1024);
+ r = cg_set_attribute("cpu", path, "cpu.shares", buf);
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set cpu.shares on %s: %m", path);
+
+ sprintf(buf, USEC_FMT "\n", CGROUP_CPU_QUOTA_PERIOD_USEC);
+ r = cg_set_attribute("cpu", path, "cpu.cfs_period_us", buf);
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set cpu.cfs_period_us on %s: %m", path);
+
+ if (c->cpu_quota_per_sec_usec != USEC_INFINITY) {
+ sprintf(buf, USEC_FMT "\n", c->cpu_quota_per_sec_usec * CGROUP_CPU_QUOTA_PERIOD_USEC / USEC_PER_SEC);
+ r = cg_set_attribute("cpu", path, "cpu.cfs_quota_us", buf);
+ } else
+ r = cg_set_attribute("cpu", path, "cpu.cfs_quota_us", "-1");
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set cpu.cfs_quota_us on %s: %m", path);
+ }
+
+ if (mask & CGROUP_MASK_BLKIO) {
+ char buf[MAX3(DECIMAL_STR_MAX(unsigned long)+1,
+ DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(unsigned long)*1,
+ DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1)];
+ CGroupBlockIODeviceWeight *w;
+ CGroupBlockIODeviceBandwidth *b;
+
+ if (!is_root) {
+ sprintf(buf, "%lu\n", IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_blockio_weight != (unsigned long) -1 ? c->startup_blockio_weight :
+ c->blockio_weight != (unsigned long) -1 ? c->blockio_weight : 1000);
+ r = cg_set_attribute("blkio", path, "blkio.weight", buf);
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set blkio.weight on %s: %m", path);
+
+ /* FIXME: no way to reset this list */
+ LIST_FOREACH(device_weights, w, c->blockio_device_weights) {
+ dev_t dev;
+
+ r = lookup_blkio_device(w->path, &dev);
+ if (r < 0)
+ continue;
+
+ sprintf(buf, "%u:%u %lu", major(dev), minor(dev), w->weight);
+ r = cg_set_attribute("blkio", path, "blkio.weight_device", buf);
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set blkio.weight_device on %s: %m", path);
+ }
+ }
+
+ /* FIXME: no way to reset this list */
+ LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
+ const char *a;
+ dev_t dev;
+
+ r = lookup_blkio_device(b->path, &dev);
+ if (r < 0)
+ continue;
+
+ a = b->read ? "blkio.throttle.read_bps_device" : "blkio.throttle.write_bps_device";
+
+ sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), b->bandwidth);
+ r = cg_set_attribute("blkio", path, a, buf);
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set %s on %s: %m", a, path);
+ }
+ }
+
+ if ((mask & CGROUP_MASK_MEMORY) && !is_root) {
+ if (c->memory_limit != (uint64_t) -1) {
+ char buf[DECIMAL_STR_MAX(uint64_t) + 1];
+
+ sprintf(buf, "%" PRIu64 "\n", c->memory_limit);
+
+ if (cg_unified() <= 0)
+ r = cg_set_attribute("memory", path, "memory.limit_in_bytes", buf);
+ else
+ r = cg_set_attribute("memory", path, "memory.max", buf);
+
+ } else {
+ if (cg_unified() <= 0)
+ r = cg_set_attribute("memory", path, "memory.limit_in_bytes", "-1");
+ else
+ r = cg_set_attribute("memory", path, "memory.max", "max");
+ }
+
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to set memory.limit_in_bytes/memory.max on %s: %m", path);
+ }
+
+ if ((mask & CGROUP_MASK_DEVICE) && !is_root) {
+ CGroupDeviceAllow *a;
+
+ /* Changing the devices list of a populated cgroup
+ * might result in EINVAL, hence ignore EINVAL
+ * here. */
+
+ if (c->device_allow || c->device_policy != CGROUP_AUTO)
+ r = cg_set_attribute("devices", path, "devices.deny", "a");
+ else
+ r = cg_set_attribute("devices", path, "devices.allow", "a");
+ if (r < 0)
+ log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL) ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to reset devices.list on %s: %m", path);
+
+ if (c->device_policy == CGROUP_CLOSED ||
+ (c->device_policy == CGROUP_AUTO && c->device_allow)) {
+ static const char auto_devices[] =
+ "/dev/null\0" "rwm\0"
+ "/dev/zero\0" "rwm\0"
+ "/dev/full\0" "rwm\0"
+ "/dev/random\0" "rwm\0"
+ "/dev/urandom\0" "rwm\0"
+ "/dev/tty\0" "rwm\0"
+ "/dev/pts/ptmx\0" "rw\0"; /* /dev/pts/ptmx may not be duplicated, but accessed */
+
+ const char *x, *y;
+
+ NULSTR_FOREACH_PAIR(x, y, auto_devices)
+ whitelist_device(path, x, y);
+
+ whitelist_major(path, "pts", 'c', "rw");
+ whitelist_major(path, "kdbus", 'c', "rw");
+ whitelist_major(path, "kdbus/*", 'c', "rw");
+ }
+
+ LIST_FOREACH(device_allow, a, c->device_allow) {
+ char acc[4];
+ unsigned k = 0;
+
+ if (a->r)
+ acc[k++] = 'r';
+ if (a->w)
+ acc[k++] = 'w';
+ if (a->m)
+ acc[k++] = 'm';
+
+ if (k == 0)
+ continue;
+
+ acc[k++] = 0;
+
+ if (startswith(a->path, "/dev/"))
+ whitelist_device(path, a->path, acc);
+ else if (startswith(a->path, "block-"))
+ whitelist_major(path, a->path + 6, 'b', acc);
+ else if (startswith(a->path, "char-"))
+ whitelist_major(path, a->path + 5, 'c', acc);
+ else
+ log_debug("Ignoring device %s while writing cgroup attribute.", a->path);
+ }
+ }
+}
+
+CGroupMask cgroup_context_get_mask(CGroupContext *c) {
+ CGroupMask mask = 0;
+
+ /* Figure out which controllers we need */
+
+ if (c->cpu_accounting ||
+ c->cpu_shares != (unsigned long) -1 ||
+ c->startup_cpu_shares != (unsigned long) -1 ||
+ c->cpu_quota_per_sec_usec != USEC_INFINITY)
+ mask |= CGROUP_MASK_CPUACCT | CGROUP_MASK_CPU;
+
+ if (c->blockio_accounting ||
+ c->blockio_weight != (unsigned long) -1 ||
+ c->startup_blockio_weight != (unsigned long) -1 ||
+ c->blockio_device_weights ||
+ c->blockio_device_bandwidths)
+ mask |= CGROUP_MASK_BLKIO;
+
+ if (c->memory_accounting ||
+ c->memory_limit != (uint64_t) -1)
+ mask |= CGROUP_MASK_MEMORY;
+
+ if (c->device_allow ||
+ c->device_policy != CGROUP_AUTO)
+ mask |= CGROUP_MASK_DEVICE;
+
+ return mask;
+}
+
+CGroupMask unit_get_own_mask(Unit *u) {
+ CGroupContext *c;
+
+ /* Returns the mask of controllers the unit needs for itself */
+
+ c = unit_get_cgroup_context(u);
+ if (!c)
+ return 0;
+
+ /* If delegation is turned on, then turn on all cgroups,
+ * unless we are on the legacy hierarchy and the process we
+ * fork into it is known to drop privileges, and hence
+ * shouldn't get access to the controllers.
+ *
+ * Note that on the unified hierarchy it is safe to delegate
+ * controllers to unprivileged services. */
+
+ if (c->delegate) {
+ ExecContext *e;
+
+ e = unit_get_exec_context(u);
+ if (!e ||
+ exec_context_maintains_privileges(e) ||
+ cg_unified() > 0)
+ return _CGROUP_MASK_ALL;
+ }
+
+ return cgroup_context_get_mask(c);
+}
+
+CGroupMask unit_get_members_mask(Unit *u) {
+ assert(u);
+
+ /* Returns the mask of controllers all of the unit's children
+ * require, merged */
+
+ if (u->cgroup_members_mask_valid)
+ return u->cgroup_members_mask;
+
+ u->cgroup_members_mask = 0;
+
+ if (u->type == UNIT_SLICE) {
+ Unit *member;
+ Iterator i;
+
+ SET_FOREACH(member, u->dependencies[UNIT_BEFORE], i) {
+
+ if (member == u)
+ continue;
+
+ if (UNIT_DEREF(member->slice) != u)
+ continue;
+
+ u->cgroup_members_mask |=
+ unit_get_own_mask(member) |
+ unit_get_members_mask(member);
+ }
+ }
+
+ u->cgroup_members_mask_valid = true;
+ return u->cgroup_members_mask;
+}
+
+CGroupMask unit_get_siblings_mask(Unit *u) {
+ assert(u);
+
+ /* Returns the mask of controllers all of the unit's siblings
+ * require, i.e. the members mask of the unit's parent slice
+ * if there is one. */
+
+ if (UNIT_ISSET(u->slice))
+ return unit_get_members_mask(UNIT_DEREF(u->slice));
+
+ return unit_get_own_mask(u) | unit_get_members_mask(u);
+}
+
+CGroupMask unit_get_subtree_mask(Unit *u) {
+
+ /* Returns the mask of this subtree, meaning of the group
+ * itself and its children. */
+
+ return unit_get_own_mask(u) | unit_get_members_mask(u);
+}
+
+CGroupMask unit_get_target_mask(Unit *u) {
+ CGroupMask mask;
+
+ /* This returns the cgroup mask of all controllers to enable
+ * for a specific cgroup, i.e. everything it needs itself,
+ * plus all that its children need, plus all that its siblings
+ * need. This is primarily useful on the legacy cgroup
+ * hierarchy, where we need to duplicate each cgroup in each
+ * hierarchy that shall be enabled for it. */
+
+ mask = unit_get_own_mask(u) | unit_get_members_mask(u) | unit_get_siblings_mask(u);
+ mask &= u->manager->cgroup_supported;
+
+ return mask;
+}
+
+CGroupMask unit_get_enable_mask(Unit *u) {
+ CGroupMask mask;
+
+ /* This returns the cgroup mask of all controllers to enable
+ * for the children of a specific cgroup. This is primarily
+ * useful for the unified cgroup hierarchy, where each cgroup
+ * controls which controllers are enabled for its children. */
+
+ mask = unit_get_members_mask(u);
+ mask &= u->manager->cgroup_supported;
+
+ return mask;
+}
+
+/* Recurse from a unit up through its containing slices, propagating
+ * mask bits upward. A unit is also member of itself. */
+void unit_update_cgroup_members_masks(Unit *u) {
+ CGroupMask m;
+ bool more;
+
+ assert(u);
+
+ /* Calculate subtree mask */
+ m = unit_get_subtree_mask(u);
+
+ /* See if anything changed from the previous invocation. If
+ * not, we're done. */
+ if (u->cgroup_subtree_mask_valid && m == u->cgroup_subtree_mask)
+ return;
+
+ more =
+ u->cgroup_subtree_mask_valid &&
+ ((m & ~u->cgroup_subtree_mask) != 0) &&
+ ((~m & u->cgroup_subtree_mask) == 0);
+
+ u->cgroup_subtree_mask = m;
+ u->cgroup_subtree_mask_valid = true;
+
+ if (UNIT_ISSET(u->slice)) {
+ Unit *s = UNIT_DEREF(u->slice);
+
+ if (more)
+ /* There's more set now than before. We
+ * propagate the new mask to the parent's mask
+ * (not caring if it actually was valid or
+ * not). */
+
+ s->cgroup_members_mask |= m;
+
+ else
+ /* There's less set now than before (or we
+ * don't know), we need to recalculate
+ * everything, so let's invalidate the
+ * parent's members mask */
+
+ s->cgroup_members_mask_valid = false;
+
+ /* And now make sure that this change also hits our
+ * grandparents */
+ unit_update_cgroup_members_masks(s);
+ }
+}
+
+static const char *migrate_callback(CGroupMask mask, void *userdata) {
+ Unit *u = userdata;
+
+ assert(mask != 0);
+ assert(u);
+
+ while (u) {
+ if (u->cgroup_path &&
+ u->cgroup_realized &&
+ (u->cgroup_realized_mask & mask) == mask)
+ return u->cgroup_path;
+
+ u = UNIT_DEREF(u->slice);
+ }
+
+ return NULL;
+}
+
+char *unit_default_cgroup_path(Unit *u) {
+ _cleanup_free_ char *escaped = NULL, *slice = NULL;
+ int r;
+
+ assert(u);
+
+ if (unit_has_name(u, SPECIAL_ROOT_SLICE))
+ return strdup(u->manager->cgroup_root);
+
+ if (UNIT_ISSET(u->slice) && !unit_has_name(UNIT_DEREF(u->slice), SPECIAL_ROOT_SLICE)) {
+ r = cg_slice_to_path(UNIT_DEREF(u->slice)->id, &slice);
+ if (r < 0)
+ return NULL;
+ }
+
+ escaped = cg_escape(u->id);
+ if (!escaped)
+ return NULL;
+
+ if (slice)
+ return strjoin(u->manager->cgroup_root, "/", slice, "/", escaped, NULL);
+ else
+ return strjoin(u->manager->cgroup_root, "/", escaped, NULL);
+}
+
+int unit_set_cgroup_path(Unit *u, const char *path) {
+ _cleanup_free_ char *p = NULL;
+ int r;
+
+ assert(u);
+
+ if (path) {
+ p = strdup(path);
+ if (!p)
+ return -ENOMEM;
+ } else
+ p = NULL;
+
+ if (streq_ptr(u->cgroup_path, p))
+ return 0;
+
+ if (p) {
+ r = hashmap_put(u->manager->cgroup_unit, p, u);
+ if (r < 0)
+ return r;
+ }
+
+ unit_release_cgroup(u);
+
+ u->cgroup_path = p;
+ p = NULL;
+
+ return 1;
+}
+
+int unit_watch_cgroup(Unit *u) {
+ _cleanup_free_ char *populated = NULL;
+ int r;
+
+ assert(u);
+
+ if (!u->cgroup_path)
+ return 0;
+
+ if (u->cgroup_inotify_wd >= 0)
+ return 0;
+
+ /* Only applies to the unified hierarchy */
+ r = cg_unified();
+ if (r < 0)
+ return log_unit_error_errno(u, r, "Failed detect wether the unified hierarchy is used: %m");
+ if (r == 0)
+ return 0;
+
+ /* Don't watch the root slice, it's pointless. */
+ if (unit_has_name(u, SPECIAL_ROOT_SLICE))
+ return 0;
+
+ r = hashmap_ensure_allocated(&u->manager->cgroup_inotify_wd_unit, &trivial_hash_ops);
+ if (r < 0)
+ return log_oom();
+
+ r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "cgroup.populated", &populated);
+ if (r < 0)
+ return log_oom();
+
+ u->cgroup_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, populated, IN_MODIFY);
+ if (u->cgroup_inotify_wd < 0) {
+
+ if (errno == ENOENT) /* If the directory is already
+ * gone we don't need to track
+ * it, so this is not an error */
+ return 0;
+
+ return log_unit_error_errno(u, errno, "Failed to add inotify watch descriptor for control group %s: %m", u->cgroup_path);
+ }
+
+ r = hashmap_put(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd), u);
+ if (r < 0)
+ return log_unit_error_errno(u, r, "Failed to add inotify watch descriptor to hash map: %m");
+
+ return 0;
+}
+
+static int unit_create_cgroup(
+ Unit *u,
+ CGroupMask target_mask,
+ CGroupMask enable_mask) {
+
+ CGroupContext *c;
+ int r;
+
+ assert(u);
+
+ c = unit_get_cgroup_context(u);
+ if (!c)
+ return 0;
+
+ if (!u->cgroup_path) {
+ _cleanup_free_ char *path = NULL;
+
+ path = unit_default_cgroup_path(u);
+ if (!path)
+ return log_oom();
+
+ r = unit_set_cgroup_path(u, path);
+ if (r == -EEXIST)
+ return log_unit_error_errno(u, r, "Control group %s exists already.", path);
+ if (r < 0)
+ return log_unit_error_errno(u, r, "Failed to set unit's control group path to %s: %m", path);
+ }
+
+ /* First, create our own group */
+ r = cg_create_everywhere(u->manager->cgroup_supported, target_mask, u->cgroup_path);
+ if (r < 0)
+ return log_unit_error_errno(u, r, "Failed to create cgroup %s: %m", u->cgroup_path);
+
+ /* Start watching it */
+ (void) unit_watch_cgroup(u);
+
+ /* Enable all controllers we need */
+ r = cg_enable_everywhere(u->manager->cgroup_supported, enable_mask, u->cgroup_path);
+ if (r < 0)
+ log_unit_warning_errno(u, r, "Failed to enable controllers on cgroup %s, ignoring: %m", u->cgroup_path);
+
+ /* Keep track that this is now realized */
+ u->cgroup_realized = true;
+ u->cgroup_realized_mask = target_mask;
+
+ if (u->type != UNIT_SLICE && !c->delegate) {
+
+ /* Then, possibly move things over, but not if
+ * subgroups may contain processes, which is the case
+ * for slice and delegation units. */
+ r = cg_migrate_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->cgroup_path, migrate_callback, u);
+ if (r < 0)
+ log_unit_warning_errno(u, r, "Failed to migrate cgroup from to %s, ignoring: %m", u->cgroup_path);
+ }
+
+ return 0;
+}
+
+int unit_attach_pids_to_cgroup(Unit *u) {
+ int r;
+ assert(u);
+
+ r = unit_realize_cgroup(u);
+ if (r < 0)
+ return r;
+
+ r = cg_attach_many_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->pids, migrate_callback, u);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+static bool unit_has_mask_realized(Unit *u, CGroupMask target_mask) {
+ assert(u);
+
+ return u->cgroup_realized && u->cgroup_realized_mask == target_mask;
+}
+
+/* Check if necessary controllers and attributes for a unit are in place.
+ *
+ * If so, do nothing.
+ * If not, create paths, move processes over, and set attributes.
+ *
+ * Returns 0 on success and < 0 on failure. */
+static int unit_realize_cgroup_now(Unit *u, ManagerState state) {
+ CGroupMask target_mask, enable_mask;
+ int r;
+
+ assert(u);
+
+ if (u->in_cgroup_queue) {
+ LIST_REMOVE(cgroup_queue, u->manager->cgroup_queue, u);
+ u->in_cgroup_queue = false;
+ }
+
+ target_mask = unit_get_target_mask(u);
+ if (unit_has_mask_realized(u, target_mask))
+ return 0;
+
+ /* First, realize parents */
+ if (UNIT_ISSET(u->slice)) {
+ r = unit_realize_cgroup_now(UNIT_DEREF(u->slice), state);
+ if (r < 0)
+ return r;
+ }
+
+ /* And then do the real work */
+ enable_mask = unit_get_enable_mask(u);
+ r = unit_create_cgroup(u, target_mask, enable_mask);
+ if (r < 0)
+ return r;
+
+ /* Finally, apply the necessary attributes. */
+ cgroup_context_apply(unit_get_cgroup_context(u), target_mask, u->cgroup_path, state);
+
+ return 0;
+}
+
+static void unit_add_to_cgroup_queue(Unit *u) {
+
+ if (u->in_cgroup_queue)
+ return;
+
+ LIST_PREPEND(cgroup_queue, u->manager->cgroup_queue, u);
+ u->in_cgroup_queue = true;
+}
+
+unsigned manager_dispatch_cgroup_queue(Manager *m) {
+ ManagerState state;
+ unsigned n = 0;
+ Unit *i;
+ int r;
+
+ state = manager_state(m);
+
+ while ((i = m->cgroup_queue)) {
+ assert(i->in_cgroup_queue);
+
+ r = unit_realize_cgroup_now(i, state);
+ if (r < 0)
+ log_warning_errno(r, "Failed to realize cgroups for queued unit %s, ignoring: %m", i->id);
+
+ n++;
+ }
+
+ return n;
+}
+
+static void unit_queue_siblings(Unit *u) {
+ Unit *slice;
+
+ /* This adds the siblings of the specified unit and the
+ * siblings of all parent units to the cgroup queue. (But
+ * neither the specified unit itself nor the parents.) */
+
+ while ((slice = UNIT_DEREF(u->slice))) {
+ Iterator i;
+ Unit *m;
+
+ SET_FOREACH(m, slice->dependencies[UNIT_BEFORE], i) {
+ if (m == u)
+ continue;
+
+ /* Skip units that have a dependency on the slice
+ * but aren't actually in it. */
+ if (UNIT_DEREF(m->slice) != slice)
+ continue;
+
+ /* No point in doing cgroup application for units
+ * without active processes. */
+ if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(m)))
+ continue;
+
+ /* If the unit doesn't need any new controllers
+ * and has current ones realized, it doesn't need
+ * any changes. */
+ if (unit_has_mask_realized(m, unit_get_target_mask(m)))
+ continue;
+
+ unit_add_to_cgroup_queue(m);
+ }
+
+ u = slice;
+ }
+}
+
+int unit_realize_cgroup(Unit *u) {
+ assert(u);
+
+ if (!UNIT_HAS_CGROUP_CONTEXT(u))
+ return 0;
+
+ /* So, here's the deal: when realizing the cgroups for this
+ * unit, we need to first create all parents, but there's more
+ * actually: for the weight-based controllers we also need to
+ * make sure that all our siblings (i.e. units that are in the
+ * same slice as we are) have cgroups, too. Otherwise, things
+ * would become very uneven as each of their processes would
+ * get as much resources as all our group together. This call
+ * will synchronously create the parent cgroups, but will
+ * defer work on the siblings to the next event loop
+ * iteration. */
+
+ /* Add all sibling slices to the cgroup queue. */
+ unit_queue_siblings(u);
+
+ /* And realize this one now (and apply the values) */
+ return unit_realize_cgroup_now(u, manager_state(u->manager));
+}
+
+void unit_release_cgroup(Unit *u) {
+ assert(u);
+
+ /* Forgets all cgroup details for this cgroup */
+
+ if (u->cgroup_path) {
+ (void) hashmap_remove(u->manager->cgroup_unit, u->cgroup_path);
+ u->cgroup_path = mfree(u->cgroup_path);
+ }
+
+ if (u->cgroup_inotify_wd >= 0) {
+ if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_inotify_wd) < 0)
+ log_unit_debug_errno(u, errno, "Failed to remove cgroup inotify watch %i for %s, ignoring", u->cgroup_inotify_wd, u->id);
+
+ (void) hashmap_remove(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd));
+ u->cgroup_inotify_wd = -1;
+ }
+}
+
+void unit_prune_cgroup(Unit *u) {
+ int r;
+ bool is_root_slice;
+
+ assert(u);
+
+ /* Removes the cgroup, if empty and possible, and stops watching it. */
+
+ if (!u->cgroup_path)
+ return;
+
+ is_root_slice = unit_has_name(u, SPECIAL_ROOT_SLICE);
+
+ r = cg_trim_everywhere(u->manager->cgroup_supported, u->cgroup_path, !is_root_slice);
+ if (r < 0) {
+ log_debug_errno(r, "Failed to destroy cgroup %s, ignoring: %m", u->cgroup_path);
+ return;
+ }
+
+ if (is_root_slice)
+ return;
+
+ unit_release_cgroup(u);
+
+ u->cgroup_realized = false;
+ u->cgroup_realized_mask = 0;
+}
+
+int unit_search_main_pid(Unit *u, pid_t *ret) {
+ _cleanup_fclose_ FILE *f = NULL;
+ pid_t pid = 0, npid, mypid;
+ int r;
+
+ assert(u);
+ assert(ret);
+
+ if (!u->cgroup_path)
+ return -ENXIO;
+
+ r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &f);
+ if (r < 0)
+ return r;
+
+ mypid = getpid();
+ while (cg_read_pid(f, &npid) > 0) {
+ pid_t ppid;
+
+ if (npid == pid)
+ continue;
+
+ /* Ignore processes that aren't our kids */
+ if (get_parent_of_pid(npid, &ppid) >= 0 && ppid != mypid)
+ continue;
+
+ if (pid != 0)
+ /* Dang, there's more than one daemonized PID
+ in this group, so we don't know what process
+ is the main process. */
+
+ return -ENODATA;
+
+ pid = npid;
+ }
+
+ *ret = pid;
+ return 0;
+}
+
+static int unit_watch_pids_in_path(Unit *u, const char *path) {
+ _cleanup_closedir_ DIR *d = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ int ret = 0, r;
+
+ assert(u);
+ assert(path);
+
+ r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, path, &f);
+ if (r < 0)
+ ret = r;
+ else {
+ pid_t pid;
+
+ while ((r = cg_read_pid(f, &pid)) > 0) {
+ r = unit_watch_pid(u, pid);
+ if (r < 0 && ret >= 0)
+ ret = r;
+ }
+
+ if (r < 0 && ret >= 0)
+ ret = r;
+ }
+
+ r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, path, &d);
+ if (r < 0) {
+ if (ret >= 0)
+ ret = r;
+ } else {
+ char *fn;
+
+ while ((r = cg_read_subgroup(d, &fn)) > 0) {
+ _cleanup_free_ char *p = NULL;
+
+ p = strjoin(path, "/", fn, NULL);
+ free(fn);
+
+ if (!p)
+ return -ENOMEM;
+
+ r = unit_watch_pids_in_path(u, p);
+ if (r < 0 && ret >= 0)
+ ret = r;
+ }
+
+ if (r < 0 && ret >= 0)
+ ret = r;
+ }
+
+ return ret;
+}
+
+int unit_watch_all_pids(Unit *u) {
+ assert(u);
+
+ /* Adds all PIDs from our cgroup to the set of PIDs we
+ * watch. This is a fallback logic for cases where we do not
+ * get reliable cgroup empty notifications: we try to use
+ * SIGCHLD as replacement. */
+
+ if (!u->cgroup_path)
+ return -ENOENT;
+
+ if (cg_unified() > 0) /* On unified we can use proper notifications */
+ return 0;
+
+ return unit_watch_pids_in_path(u, u->cgroup_path);
+}
+
+int unit_notify_cgroup_empty(Unit *u) {
+ int r;
+
+ assert(u);
+
+ if (!u->cgroup_path)
+ return 0;
+
+ r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
+ if (r <= 0)
+ return r;
+
+ unit_add_to_gc_queue(u);
+
+ if (UNIT_VTABLE(u)->notify_cgroup_empty)
+ UNIT_VTABLE(u)->notify_cgroup_empty(u);
+
+ return 0;
+}
+
+static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+ Manager *m = userdata;
+
+ assert(s);
+ assert(fd >= 0);
+ assert(m);
+
+ for (;;) {
+ union inotify_event_buffer buffer;
+ struct inotify_event *e;
+ ssize_t l;
+
+ l = read(fd, &buffer, sizeof(buffer));
+ if (l < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ return 0;
+
+ return log_error_errno(errno, "Failed to read control group inotify events: %m");
+ }
+
+ FOREACH_INOTIFY_EVENT(e, buffer, l) {
+ Unit *u;
+
+ if (e->wd < 0)
+ /* Queue overflow has no watch descriptor */
+ continue;
+
+ if (e->mask & IN_IGNORED)
+ /* The watch was just removed */
+ continue;
+
+ u = hashmap_get(m->cgroup_inotify_wd_unit, INT_TO_PTR(e->wd));
+ if (!u) /* Not that inotify might deliver
+ * events for a watch even after it
+ * was removed, because it was queued
+ * before the removal. Let's ignore
+ * this here safely. */
+ continue;
+
+ (void) unit_notify_cgroup_empty(u);
+ }
+ }
+}
+#endif // 0
+
+int manager_setup_cgroup(Manager *m) {
+ _cleanup_free_ char *path = NULL;
+ CGroupController c;
+ int r, unified;
+ char *e;
+
+ assert(m);
+
+ /* 1. Determine hierarchy */
+ m->cgroup_root = mfree(m->cgroup_root);
+ r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root);
+ if (r < 0)
+ return log_error_errno(r, "Cannot determine cgroup we are running in: %m");
+
+/// elogind does not support systemd scopes and slices
+#if 0
+ /* Chop off the init scope, if we are already located in it */
+ e = endswith(m->cgroup_root, "/" SPECIAL_INIT_SCOPE);
+
+ /* LEGACY: Also chop off the system slice if we are in
+ * it. This is to support live upgrades from older systemd
+ * versions where PID 1 was moved there. Also see
+ * cg_get_root_path(). */
+ if (!e) {
+ e = endswith(m->cgroup_root, "/" SPECIAL_SYSTEM_SLICE);
+ if (!e)
+ e = endswith(m->cgroup_root, "/system"); /* even more legacy */
+ }
+ if (e)
+ *e = 0;
+#endif // 0
+
+ /* And make sure to store away the root value without trailing
+ * slash, even for the root dir, so that we can easily prepend
+ * it everywhere. */
+ while ((e = endswith(m->cgroup_root, "/")))
+ *e = 0;
+ log_debug_elogind("Cgroup Controller \"%s\" -> root \"%s\"",
+ SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root);
+
+ /* 2. Show data */
+ r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, NULL, &path);
+ if (r < 0)
+ return log_error_errno(r, "Cannot find cgroup mount point: %m");
+
+ unified = cg_unified();
+ if (unified < 0)
+ return log_error_errno(r, "Couldn't determine if we are running in the unified hierarchy: %m");
+ if (unified > 0)
+ log_debug("Unified cgroup hierarchy is located at %s.", path);
+ else
+ log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path);
+
+ if (!m->test_run) {
+ const char *scope_path;
+
+ /* 3. Install agent */
+ if (unified) {
+
+ /* In the unified hierarchy we can can get
+ * cgroup empty notifications via inotify. */
+/// elogind does not support the unified hierarchy, yet.
+#if 0
+ m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source);
+ safe_close(m->cgroup_inotify_fd);
+
+ m->cgroup_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
+ if (m->cgroup_inotify_fd < 0)
+ return log_error_errno(errno, "Failed to create control group inotify object: %m");
+
+ r = sd_event_add_io(m->event, &m->cgroup_inotify_event_source, m->cgroup_inotify_fd, EPOLLIN, on_cgroup_inotify_event, m);
+ if (r < 0)
+ return log_error_errno(r, "Failed to watch control group inotify object: %m");
+
+ r = sd_event_source_set_priority(m->cgroup_inotify_event_source, SD_EVENT_PRIORITY_IDLE - 5);
+ if (r < 0)
+ return log_error_errno(r, "Failed to set priority of inotify event source: %m");
+
+ (void) sd_event_source_set_description(m->cgroup_inotify_event_source, "cgroup-inotify");
+
+#else
+ return log_error_errno(EOPNOTSUPP, "Unified cgroup hierarchy not supported: %m");
+#endif // 0
+ } else if (m->running_as == MANAGER_SYSTEM) {
+ /* On the legacy hierarchy we only get
+ * notifications via cgroup agents. (Which
+ * isn't really reliable, since it does not
+ * generate events when control groups with
+ * children run empty. */
+
+ r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, ELOGIND_CGROUP_AGENT_PATH);
+ if (r < 0)
+ log_warning_errno(r, "Failed to install release agent, ignoring: %m");
+ else if (r > 0)
+ log_debug("Installed release agent.");
+ else if (r == 0)
+ log_debug("Release agent already installed.");
+ }
+
+/// elogind is not meant to run in systemd init scope
+#if 0
+ /* 4. Make sure we are in the special "init.scope" unit in the root slice. */
+ scope_path = strjoina(m->cgroup_root, "/" SPECIAL_INIT_SCOPE);
+ r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0);
+#else
+ if (streq(SYSTEMD_CGROUP_CONTROLLER, "name=elogind"))
+ // we are our own cgroup controller
+ scope_path = strjoina("");
+ else if (streq(m->cgroup_root, "/elogind"))
+ // root already is our cgroup
+ scope_path = strjoina(m->cgroup_root);
+ else
+ // we have to create our own group
+ scope_path = strjoina(m->cgroup_root, "/elogind");
+ r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0);
+#endif // 0
+ if (r < 0)
+ return log_error_errno(r, "Failed to create %s control group: %m", scope_path);
+ log_debug_elogind("Created control group \"%s\"", scope_path);
+
+ /* also, move all other userspace processes remaining
+ * in the root cgroup into that scope. */
+ if (!streq(m->cgroup_root, scope_path)) {
+ r = cg_migrate(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, SYSTEMD_CGROUP_CONTROLLER, scope_path, false);
+ if (r < 0)
+ log_warning_errno(r, "Couldn't move remaining userspace processes, ignoring: %m");
+ }
+
+ /* 5. And pin it, so that it cannot be unmounted */
+ safe_close(m->pin_cgroupfs_fd);
+ m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
+ if (m->pin_cgroupfs_fd < 0)
+ return log_error_errno(errno, "Failed to open pin file: %m");
+
+ /* 6. Always enable hierarchical support if it exists... */
+ if (!unified)
+ (void) cg_set_attribute("memory", "/", "memory.use_hierarchy", "1");
+ }
+
+ /* 7. Figure out which controllers are supported */
+ r = cg_mask_supported(&m->cgroup_supported);
+ if (r < 0)
+ return log_error_errno(r, "Failed to determine supported controllers: %m");
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++)
+ log_debug("Controller '%s' supported: %s", cgroup_controller_to_string(c), yes_no(m->cgroup_supported & c));
+
+ return 0;
+}
+
+void manager_shutdown_cgroup(Manager *m, bool delete) {
+ assert(m);
+
+ /* We can't really delete the group, since we are in it. But
+ * let's trim it. */
+ if (delete && m->cgroup_root)
+ (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false);
+
+/// elogind does not support the unified hierarchy, yet.
+#if 0
+ m->cgroup_inotify_wd_unit = hashmap_free(m->cgroup_inotify_wd_unit);
+
+ m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source);
+ m->cgroup_inotify_fd = safe_close(m->cgroup_inotify_fd);
+#endif // 0
+
+ m->pin_cgroupfs_fd = safe_close(m->pin_cgroupfs_fd);
+
+ m->cgroup_root = mfree(m->cgroup_root);
+}
+
+/// UNNEEDED by elogind
+#if 0
+Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup) {
+ char *p;
+ Unit *u;
+
+ assert(m);
+ assert(cgroup);
+
+ u = hashmap_get(m->cgroup_unit, cgroup);
+ if (u)
+ return u;
+
+ p = strdupa(cgroup);
+ for (;;) {
+ char *e;
+
+ e = strrchr(p, '/');
+ if (!e || e == p)
+ return hashmap_get(m->cgroup_unit, SPECIAL_ROOT_SLICE);
+
+ *e = 0;
+
+ u = hashmap_get(m->cgroup_unit, p);
+ if (u)
+ return u;
+ }
+}
+
+Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid) {
+ _cleanup_free_ char *cgroup = NULL;
+ int r;
+
+ assert(m);
+
+ if (pid <= 0)
+ return NULL;
+
+ r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
+ if (r < 0)
+ return NULL;
+
+ return manager_get_unit_by_cgroup(m, cgroup);
+}
+
+Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) {
+ Unit *u;
+
+ assert(m);
+
+ if (pid <= 0)
+ return NULL;
+
+ if (pid == 1)
+ return hashmap_get(m->units, SPECIAL_INIT_SCOPE);
+
+ u = hashmap_get(m->watch_pids1, PID_TO_PTR(pid));
+ if (u)
+ return u;
+
+ u = hashmap_get(m->watch_pids2, PID_TO_PTR(pid));
+ if (u)
+ return u;
+
+ return manager_get_unit_by_pid_cgroup(m, pid);
+}
+
+int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
+ Unit *u;
+
+ assert(m);
+ assert(cgroup);
+
+ u = manager_get_unit_by_cgroup(m, cgroup);
+ if (!u)
+ return 0;
+
+ return unit_notify_cgroup_empty(u);
+}
+
+int unit_get_memory_current(Unit *u, uint64_t *ret) {
+ _cleanup_free_ char *v = NULL;
+ int r;
+
+ assert(u);
+ assert(ret);
+
+ if (!u->cgroup_path)
+ return -ENODATA;
+
+ if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0)
+ return -ENODATA;
+
+ if (cg_unified() <= 0)
+ r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v);
+ else
+ r = cg_get_attribute("memory", u->cgroup_path, "memory.current", &v);
+ if (r == -ENOENT)
+ return -ENODATA;
+ if (r < 0)
+ return r;
+
+ return safe_atou64(v, ret);
+}
+
+static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
+ _cleanup_free_ char *v = NULL;
+ uint64_t ns;
+ int r;
+
+ assert(u);
+ assert(ret);
+
+ if (!u->cgroup_path)
+ return -ENODATA;
+
+ if ((u->cgroup_realized_mask & CGROUP_MASK_CPUACCT) == 0)
+ return -ENODATA;
+
+ r = cg_get_attribute("cpuacct", u->cgroup_path, "cpuacct.usage", &v);
+ if (r == -ENOENT)
+ return -ENODATA;
+ if (r < 0)
+ return r;
+
+ r = safe_atou64(v, &ns);
+ if (r < 0)
+ return r;
+
+ *ret = ns;
+ return 0;
+}
+
+int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
+ nsec_t ns;
+ int r;
+
+ r = unit_get_cpu_usage_raw(u, &ns);
+ if (r < 0)
+ return r;
+
+ if (ns > u->cpuacct_usage_base)
+ ns -= u->cpuacct_usage_base;
+ else
+ ns = 0;
+
+ *ret = ns;
+ return 0;
+}
+
+int unit_reset_cpu_usage(Unit *u) {
+ nsec_t ns;
+ int r;
+
+ assert(u);
+
+ r = unit_get_cpu_usage_raw(u, &ns);
+ if (r < 0) {
+ u->cpuacct_usage_base = 0;
+ return r;
+ }
+
+ u->cpuacct_usage_base = ns;
+ return 0;
+}
+
+bool unit_cgroup_delegate(Unit *u) {
+ CGroupContext *c;
+
+ assert(u);
+
+ c = unit_get_cgroup_context(u);
+ if (!c)
+ return false;
+
+ return c->delegate;
+}
+
+static const char* const cgroup_device_policy_table[_CGROUP_DEVICE_POLICY_MAX] = {
+ [CGROUP_AUTO] = "auto",
+ [CGROUP_CLOSED] = "closed",
+ [CGROUP_STRICT] = "strict",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(cgroup_device_policy, CGroupDevicePolicy);
+#endif // 0
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2013 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+
+// #include "list.h"
+// #include "time-util.h"
+#include "logind.h"
+
+// UNNEEDED typedef struct CGroupContext CGroupContext;
+// UNNEEDED Stypedef struct CGroupDeviceAllow CGroupDeviceAllow;
+// UNNEEDED typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
+// UNNEEDED typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
+
+/// UNNEEDED by elogind
+#if 0
+typedef enum CGroupDevicePolicy {
+
+ /* When devices listed, will allow those, plus built-in ones,
+ if none are listed will allow everything. */
+ CGROUP_AUTO,
+
+ /* Everything forbidden, except built-in ones and listed ones. */
+ CGROUP_CLOSED,
+
+ /* Everythings forbidden, except for the listed devices */
+ CGROUP_STRICT,
+
+ _CGROUP_DEVICE_POLICY_MAX,
+ _CGROUP_DEVICE_POLICY_INVALID = -1
+} CGroupDevicePolicy;
+
+struct CGroupDeviceAllow {
+ LIST_FIELDS(CGroupDeviceAllow, device_allow);
+ char *path;
+ bool r:1;
+ bool w:1;
+ bool m:1;
+};
+
+struct CGroupBlockIODeviceWeight {
+ LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
+ char *path;
+ unsigned long weight;
+};
+
+struct CGroupBlockIODeviceBandwidth {
+ LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
+ char *path;
+ uint64_t bandwidth;
+ bool read;
+};
+
+struct CGroupContext {
+ bool cpu_accounting;
+ bool blockio_accounting;
+ bool memory_accounting;
+
+ unsigned long cpu_shares;
+ unsigned long startup_cpu_shares;
+ usec_t cpu_quota_per_sec_usec;
+
+ unsigned long blockio_weight;
+ unsigned long startup_blockio_weight;
+ LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
+ LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
+
+ uint64_t memory_limit;
+
+ CGroupDevicePolicy device_policy;
+ LIST_HEAD(CGroupDeviceAllow, device_allow);
+
+ bool delegate;
+};
+#endif // 0
+
+// #include "unit.h"
+// #include "cgroup-util.h"
+
+// UNNNEEDE void cgroup_context_init(CGroupContext *c);
+// UNNEEDED void cgroup_context_done(CGroupContext *c);
+// UNNEEDED void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
+// UNNEEDED void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, ManagerState state);
+
+// UNNEEDED CGroupMask cgroup_context_get_mask(CGroupContext *c);
+
+// UNNEEDED void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
+// UNNEEDED void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
+// UNNEEDED void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
+
+// UNNEEDED CGroupMask unit_get_own_mask(Unit *u);
+// UNNEEDED CGroupMask unit_get_siblings_mask(Unit *u);
+// UNNEEDED CGroupMask unit_get_members_mask(Unit *u);
+// UNNEEDED CGroupMask unit_get_subtree_mask(Unit *u);
+
+// UNNEEDED vCGroupMask unit_get_target_mask(Unit *u);
+// UNNEEDED CGroupMask unit_get_enable_mask(Unit *u);
+
+// UNNEEDED void unit_update_cgroup_members_masks(Unit *u);
+
+// UNNEEDED har *unit_default_cgroup_path(Unit *u);
+// UNNEEDED int unit_set_cgroup_path(Unit *u, const char *path);
+
+// UNNEEDED int unit_realize_cgroup(Unit *u);
+// UNNEEDED void unit_release_cgroup(Unit *u);
+// UNNEEDED void unit_prune_cgroup(Unit *u);
+// UNNEEDED int unit_watch_cgroup(Unit *u);
+
+// UNNEEDED int unit_attach_pids_to_cgroup(Unit *u);
+
+int manager_setup_cgroup(Manager *m);
+void manager_shutdown_cgroup(Manager *m, bool delete);
+
+// UNNEEDED unsigned manager_dispatch_cgroup_queue(Manager *m);
+
+// UNNEEDED Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
+// UNNEEDED Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
+// UNNEEDED Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
+
+// UNNEEDED int unit_search_main_pid(Unit *u, pid_t *ret);
+// UNNEEDED int unit_watch_all_pids(Unit *u);
+
+// UNNEEDED int unit_get_memory_current(Unit *u, uint64_t *ret);
+// UNNEEDED int unit_get_cpu_usage(Unit *u, nsec_t *ret);
+// UNNEEDED int unit_reset_cpu_usage(Unit *u);
+
+// UNNEEDED bool unit_cgroup_delegate(Unit *u);
+
+// UNNEEDED int unit_notify_cgroup_empty(Unit *u);
+// UNNEEDED int manager_notify_cgroup_empty(Manager *m, const char *group);
+
+// UNNEEDED const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
+// UNNEEDED CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/mount.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <ftw.h>
+
+#include "mount-setup.h"
+//#include "dev-setup.h"
+//#include "bus-util.h"
+//#include "log.h"
+#include "macro.h"
+//#include "util.h"
+#include "label.h"
+//#include "set.h"
+//#include "strv.h"
+#include "mkdir.h"
+#include "path-util.h"
+//#include "missing.h"
+#include "virt.h"
+//#include "efivars.h"
+//#include "smack-util.h"
+#include "cgroup-util.h"
+
+typedef enum MountMode {
+ MNT_NONE = 0,
+ MNT_FATAL = 1 << 0,
+ MNT_IN_CONTAINER = 1 << 1,
+} MountMode;
+
+typedef struct MountPoint {
+ const char *what;
+ const char *where;
+ const char *type;
+ const char *options;
+ unsigned long flags;
+ bool (*condition_fn)(void);
+ MountMode mode;
+} MountPoint;
+
+/* The first three entries we might need before SELinux is up. The
+ * fourth (securityfs) is needed by IMA to load a custom policy. The
+ * other ones we can delay until SELinux and IMA are loaded. When
+ * SMACK is enabled we need smackfs, too, so it's a fifth one. */
+#ifdef HAVE_SMACK
+#define N_EARLY_MOUNT 5
+#else
+#define N_EARLY_MOUNT 4
+#endif
+
+static const MountPoint mount_table[] = {
+/// UNNEEDED by elogind
+#if 0
+ { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ NULL, MNT_FATAL|MNT_IN_CONTAINER },
+ { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ NULL, MNT_FATAL|MNT_IN_CONTAINER },
+ { "devtmpfs", "/dev", "devtmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME,
+ NULL, MNT_FATAL|MNT_IN_CONTAINER },
+ { "securityfs", "/sys/kernel/security", "securityfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ NULL, MNT_NONE },
+#ifdef HAVE_SMACK
+ { "smackfs", "/sys/fs/smackfs", "smackfs", "smackfsdef=*", MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ mac_smack_use, MNT_FATAL },
+ { "tmpfs", "/dev/shm", "tmpfs", "mode=1777,smackfsroot=*", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
+ mac_smack_use, MNT_FATAL },
+#endif
+ { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
+ NULL, MNT_FATAL|MNT_IN_CONTAINER },
+ { "devpts", "/dev/pts", "devpts", "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC,
+ NULL, MNT_IN_CONTAINER },
+#ifdef HAVE_SMACK
+ { "tmpfs", "/run", "tmpfs", "mode=755,smackfsroot=*", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
+ mac_smack_use, MNT_FATAL },
+#endif
+ { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
+ NULL, MNT_FATAL|MNT_IN_CONTAINER },
+ { "cgroup", "/sys/fs/cgroup", "cgroup", "__DEVEL__sane_behavior", MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ cg_is_unified_wanted, MNT_FATAL|MNT_IN_CONTAINER },
+#endif // 0
+ { "tmpfs", "/sys/fs/cgroup", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
+ cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER },
+/// UNNEEDED by elogind
+#if 0
+ { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd,xattr", MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ cg_is_legacy_wanted, MNT_IN_CONTAINER },
+ { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER },
+#else
+ { "cgroup", "/sys/fs/cgroup/elogind", "cgroup", "none,name=elogind,xattr", MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ cg_is_legacy_wanted, MNT_IN_CONTAINER },
+ { "cgroup", "/sys/fs/cgroup/elogind", "cgroup", "none,name=elogind", MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER },
+#endif // 0
+/// UNNEEDED by elogind
+#if 0
+ { "pstore", "/sys/fs/pstore", "pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ NULL, MNT_NONE },
+#ifdef ENABLE_EFI
+ { "efivarfs", "/sys/firmware/efi/efivars", "efivarfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ is_efi_boot, MNT_NONE },
+#endif
+ { "kdbusfs", "/sys/fs/kdbus", "kdbusfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ is_kdbus_wanted, MNT_IN_CONTAINER },
+#endif // 0
+};
+
+/// UNNEEDED by elogind
+#if 0
+/* These are API file systems that might be mounted by other software,
+ * we just list them here so that we know that we should ignore them */
+
+static const char ignore_paths[] =
+ /* SELinux file systems */
+ "/sys/fs/selinux\0"
+ /* Container bind mounts */
+ "/proc/sys\0"
+ "/dev/console\0"
+ "/proc/kmsg\0";
+
+bool mount_point_is_api(const char *path) {
+ unsigned i;
+
+ /* Checks if this mount point is considered "API", and hence
+ * should be ignored */
+
+ for (i = 0; i < ELEMENTSOF(mount_table); i ++)
+ if (path_equal(path, mount_table[i].where))
+ return true;
+
+ return path_startswith(path, "/sys/fs/cgroup/");
+}
+
+bool mount_point_ignore(const char *path) {
+ const char *i;
+
+ NULSTR_FOREACH(i, ignore_paths)
+ if (path_equal(path, i))
+ return true;
+
+ return false;
+}
+#endif // 0
+
+static int mount_one(const MountPoint *p, bool relabel) {
+ int r;
+
+ assert(p);
+
+ if (p->condition_fn && !p->condition_fn())
+ return 0;
+
+ /* Relabel first, just in case */
+ if (relabel)
+ label_fix(p->where, true, true);
+
+ r = path_is_mount_point(p->where, AT_SYMLINK_FOLLOW);
+ if (r < 0 && r != -ENOENT)
+ return r;
+ if (r > 0)
+ return 0;
+
+ /* Skip securityfs in a container */
+ if (!(p->mode & MNT_IN_CONTAINER) && detect_container(NULL) > 0)
+ return 0;
+
+ /* The access mode here doesn't really matter too much, since
+ * the mounted file system will take precedence anyway. */
+ if (relabel)
+ mkdir_p_label(p->where, 0755);
+ else
+ mkdir_p(p->where, 0755);
+
+ log_debug("Mounting %s to %s of type %s with options %s.",
+ p->what,
+ p->where,
+ p->type,
+ strna(p->options));
+
+ if (mount(p->what,
+ p->where,
+ p->type,
+ p->flags,
+ p->options) < 0) {
+ log_full((p->mode & MNT_FATAL) ? LOG_ERR : LOG_DEBUG, "Failed to mount %s at %s: %m", p->type, p->where);
+ return (p->mode & MNT_FATAL) ? -errno : 0;
+ }
+
+ /* Relabel again, since we now mounted something fresh here */
+ if (relabel)
+ label_fix(p->where, false, false);
+
+ return 1;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int mount_setup_early(void) {
+ unsigned i;
+ int r = 0;
+
+ assert_cc(N_EARLY_MOUNT <= ELEMENTSOF(mount_table));
+
+ /* Do a minimal mount of /proc and friends to enable the most
+ * basic stuff, such as SELinux */
+ for (i = 0; i < N_EARLY_MOUNT; i ++) {
+ int j;
+
+ j = mount_one(mount_table + i, false);
+ if (r == 0)
+ r = j;
+ }
+
+ return r;
+}
+
+int mount_cgroup_controllers(char ***join_controllers) {
+ _cleanup_set_free_free_ Set *controllers = NULL;
+ int r;
+
+ if (!cg_is_legacy_wanted())
+ return 0;
+
+ /* Mount all available cgroup controllers that are built into the kernel. */
+
+ controllers = set_new(&string_hash_ops);
+ if (!controllers)
+ return log_oom();
+
+ r = cg_kernel_controllers(controllers);
+ if (r < 0)
+ return log_error_errno(r, "Failed to enumerate cgroup controllers: %m");
+
+ for (;;) {
+ _cleanup_free_ char *options = NULL, *controller = NULL, *where = NULL;
+ MountPoint p = {
+ .what = "cgroup",
+ .type = "cgroup",
+ .flags = MS_NOSUID|MS_NOEXEC|MS_NODEV,
+ .mode = MNT_IN_CONTAINER,
+ };
+ char ***k = NULL;
+
+ controller = set_steal_first(controllers);
+ if (!controller)
+ break;
+
+ if (join_controllers)
+ for (k = join_controllers; *k; k++)
+ if (strv_find(*k, controller))
+ break;
+
+ if (k && *k) {
+ char **i, **j;
+
+ for (i = *k, j = *k; *i; i++) {
+
+ if (!streq(*i, controller)) {
+ _cleanup_free_ char *t;
+
+ t = set_remove(controllers, *i);
+ if (!t) {
+ free(*i);
+ continue;
+ }
+ }
+
+ *(j++) = *i;
+ }
+
+ *j = NULL;
+
+ options = strv_join(*k, ",");
+ if (!options)
+ return log_oom();
+ } else {
+ options = controller;
+ controller = NULL;
+ }
+
+ where = strappend("/sys/fs/cgroup/", options);
+ if (!where)
+ return log_oom();
+
+ p.where = where;
+ p.options = options;
+
+ r = mount_one(&p, true);
+ if (r < 0)
+ return r;
+
+ if (r > 0 && k && *k) {
+ char **i;
+
+ for (i = *k; *i; i++) {
+ _cleanup_free_ char *t = NULL;
+
+ t = strappend("/sys/fs/cgroup/", *i);
+ if (!t)
+ return log_oom();
+
+ r = symlink(options, t);
+ if (r < 0 && errno != EEXIST)
+ return log_error_errno(errno, "Failed to create symlink %s: %m", t);
+ }
+ }
+ }
+
+ /* Now that we mounted everything, let's make the tmpfs the
+ * cgroup file systems are mounted into read-only. */
+ (void) mount("tmpfs", "/sys/fs/cgroup", "tmpfs", MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
+
+ return 0;
+}
+
+#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
+static int nftw_cb(
+ const char *fpath,
+ const struct stat *sb,
+ int tflag,
+ struct FTW *ftwbuf) {
+
+ /* No need to label /dev twice in a row... */
+ if (_unlikely_(ftwbuf->level == 0))
+ return FTW_CONTINUE;
+
+ label_fix(fpath, false, false);
+
+ /* /run/initramfs is static data and big, no need to
+ * dynamically relabel its contents at boot... */
+ if (_unlikely_(ftwbuf->level == 1 &&
+ tflag == FTW_D &&
+ streq(fpath, "/run/initramfs")))
+ return FTW_SKIP_SUBTREE;
+
+ return FTW_CONTINUE;
+};
+#endif
+#endif // 0
+
+int mount_setup(bool loaded_policy) {
+ unsigned i;
+ int r = 0;
+
+ for (i = 0; i < ELEMENTSOF(mount_table); i ++) {
+ int j;
+
+ j = mount_one(mount_table + i, loaded_policy);
+ if (r == 0)
+ r = j;
+ }
+
+ if (r < 0)
+ return r;
+
+/// elogind does not control /, /dev, /run and /run/systemd/* are setup elsewhere.
+#if 0
+#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
+ /* Nodes in devtmpfs and /run need to be manually updated for
+ * the appropriate labels, after mounting. The other virtual
+ * API file systems like /sys and /proc do not need that, they
+ * use the same label for all their files. */
+ if (loaded_policy) {
+ usec_t before_relabel, after_relabel;
+ char timespan[FORMAT_TIMESPAN_MAX];
+
+ before_relabel = now(CLOCK_MONOTONIC);
+
+ nftw("/dev", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
+ nftw("/run", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
+
+ after_relabel = now(CLOCK_MONOTONIC);
+
+ log_info("Relabelled /dev and /run in %s.",
+ format_timespan(timespan, sizeof(timespan), after_relabel - before_relabel, 0));
+ }
+#endif
+
+ /* Create a few default symlinks, which are normally created
+ * by udevd, but some scripts might need them before we start
+ * udevd. */
+ dev_setup(NULL, UID_INVALID, GID_INVALID);
+
+ /* Mark the root directory as shared in regards to mount
+ * propagation. The kernel defaults to "private", but we think
+ * it makes more sense to have a default of "shared" so that
+ * nspawn and the container tools work out of the box. If
+ * specific setups need other settings they can reset the
+ * propagation mode to private if needed. */
+ if (detect_container(NULL) <= 0)
+ if (mount(NULL, "/", NULL, MS_REC|MS_SHARED, NULL) < 0)
+ log_warning_errno(errno, "Failed to set up the root directory for shared mount propagation: %m");
+
+ /* Create a few directories we always want around, Note that
+ * sd_booted() checks for /run/systemd/system, so this mkdir
+ * really needs to stay for good, otherwise software that
+ * copied sd-daemon.c into their sources will misdetect
+ * systemd. */
+ mkdir_label("/run/systemd", 0755);
+ mkdir_label("/run/systemd/system", 0755);
+ mkdir_label("/run/systemd/inaccessible", 0000);
+#endif // 0
+
+ return 0;
+}
/***
This file is part of systemd.
- Copyright 2013 Lennart Poettering
+ Copyright 2010 Lennart Poettering
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
#include <stdbool.h>
-bool mac_apparmor_use(void);
+// UNNEEDED int mount_setup_early(void);
+int mount_setup(bool loaded_policy);
+
+// UNNEEDED int mount_cgroup_controllers(char ***join_controllers);
+
+// UNNEEDED bool mount_point_is_api(const char *path);
+// UNNEEDED bool mount_point_ignore(const char *path);
-/libelogind.sym
/libelogind.pc
-# This file is part of systemd.
+# This file is part of elogind.
#
-# systemd is free software; you can redistribute it and/or modify it
+# elogind 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.
LIBSYSTEMD_209 {
global:
/* sd-journal */
- sd_journal_print;
- sd_journal_printv;
- sd_journal_send;
- sd_journal_sendv;
- sd_journal_stream_fd;
- sd_journal_open;
- sd_journal_close;
- sd_journal_previous;
- sd_journal_next;
- sd_journal_previous_skip;
- sd_journal_next_skip;
- sd_journal_get_realtime_usec;
- sd_journal_get_monotonic_usec;
- sd_journal_get_data;
- sd_journal_enumerate_data;
- sd_journal_restart_data;
- sd_journal_add_match;
- sd_journal_flush_matches;
- sd_journal_seek_head;
- sd_journal_seek_tail;
- sd_journal_seek_monotonic_usec;
- sd_journal_seek_realtime_usec;
- sd_journal_seek_cursor;
- sd_journal_get_cursor;
- sd_journal_get_fd;
- sd_journal_process;
- sd_journal_print_with_location;
- sd_journal_printv_with_location;
- sd_journal_send_with_location;
- sd_journal_sendv_with_location;
- sd_journal_get_cutoff_realtime_usec;
- sd_journal_get_cutoff_monotonic_usec;
- sd_journal_wait;
- sd_journal_open_directory;
- sd_journal_add_disjunction;
- sd_journal_perror;
- sd_journal_perror_with_location;
- sd_journal_get_usage;
- sd_journal_test_cursor;
- sd_journal_query_unique;
- sd_journal_enumerate_unique;
- sd_journal_restart_unique;
- sd_journal_get_catalog;
- sd_journal_get_catalog_for_message_id;
- sd_journal_set_data_threshold;
- sd_journal_get_data_threshold;
- sd_journal_reliable_fd;
- sd_journal_get_events;
- sd_journal_get_timeout;
- sd_journal_add_conjunction;
- sd_journal_open_files;
- sd_journal_open_container;
+ /* sd_journal_print; */
+ /* sd_journal_printv; */
+ /* sd_journal_send; */
+ /* sd_journal_sendv; */
+ /* sd_journal_stream_fd; */
+ /* sd_journal_open; */
+ /* sd_journal_close; */
+ /* sd_journal_previous; */
+ /* sd_journal_next; */
+ /* sd_journal_previous_skip; */
+ /* sd_journal_next_skip; */
+ /* sd_journal_get_realtime_usec; */
+ /* sd_journal_get_monotonic_usec; */
+ /* sd_journal_get_data; */
+ /* sd_journal_enumerate_data; */
+ /* sd_journal_restart_data; */
+ /* sd_journal_add_match; */
+ /* sd_journal_flush_matches; */
+ /* sd_journal_seek_head; */
+ /* sd_journal_seek_tail; */
+ /* sd_journal_seek_monotonic_usec; */
+ /* sd_journal_seek_realtime_usec; */
+ /* sd_journal_seek_cursor; */
+ /* sd_journal_get_cursor; */
+ /* sd_journal_get_fd; */
+ /* sd_journal_process; */
+ /* sd_journal_print_with_location; */
+ /* sd_journal_printv_with_location; */
+ /* sd_journal_send_with_location; */
+ /* sd_journal_sendv_with_location; */
+ /* sd_journal_get_cutoff_realtime_usec; */
+ /* sd_journal_get_cutoff_monotonic_usec; */
+ /* sd_journal_wait; */
+ /* sd_journal_open_directory; */
+ /* sd_journal_add_disjunction; */
+ /* sd_journal_perror; */
+ /* sd_journal_perror_with_location; */
+ /* sd_journal_get_usage; */
+ /* sd_journal_test_cursor; */
+ /* sd_journal_query_unique; */
+ /* sd_journal_enumerate_unique; */
+ /* sd_journal_restart_unique; */
+ /* sd_journal_get_catalog; */
+ /* sd_journal_get_catalog_for_message_id; */
+ /* sd_journal_set_data_threshold; */
+ /* sd_journal_get_data_threshold; */
+ /* sd_journal_reliable_fd; */
+ /* sd_journal_get_events; */
+ /* sd_journal_get_timeout; */
+ /* sd_journal_add_conjunction; */
+ /* sd_journal_open_files; */
+ /* sd_journal_open_container; */
- /* sd-dameon */
- sd_booted;
- sd_is_fifo;
- sd_is_mq;
+ /* sd-daemon */
+ /* sd_booted; */
+ /* sd_is_fifo; */
+ /* sd_is_mq; */
sd_is_socket;
- sd_is_socket_inet;
- sd_is_socket_unix;
- sd_is_special;
- sd_listen_fds;
+ /* sd_is_socket_inet; */
+ /* sd_is_socket_unix; */
+ /* sd_is_special; */
+ /* sd_listen_fds; */
sd_notify;
- sd_notifyf;
+ /* sd_notifyf; */
sd_watchdog_enabled;
/* sd-id128 */
LIBSYSTEMD_214 {
global:
sd_pid_notify;
- sd_pid_notifyf;
+ /* sd_pid_notifyf; */
} LIBSYSTEMD_213;
LIBSYSTEMD_216 {
sd_pid_notify_with_fds;
} LIBSYSTEMD_217;
-m4_ifdef(`ENABLE_KDBUS',
-LIBSYSTEMD_FUTURE {
+LIBSYSTEMD_220 {
+global:
+ sd_pid_get_user_slice;
+ sd_peer_get_user_slice;
+} LIBSYSTEMD_219;
+
+LIBSYSTEMD_221 {
global:
/* sd-bus */
sd_bus_default;
sd_bus_new;
sd_bus_set_address;
sd_bus_set_fd;
- sd_bus_set_exec;
- sd_bus_set_bus_client;
+ /* sd_bus_set_exec; */
+ /* sd_bus_get_address; */
+ /* sd_bus_set_bus_client; */
+ /* sd_bus_is_bus_client; */
sd_bus_set_server;
- sd_bus_set_anonymous;
- sd_bus_set_trusted;
- sd_bus_set_monitor;
- sd_bus_set_description;
- sd_bus_set_allow_interactive_authorization;
- sd_bus_get_allow_interactive_authorization;
- sd_bus_negotiate_fds;
- sd_bus_negotiate_timestamp;
+ /* sd_bus_is_server; */
+ /* sd_bus_set_anonymous; */
+ /* sd_bus_is_anonymous; */
+ /* sd_bus_set_trusted; */
+ /* sd_bus_is_trusted; */
+ /* sd_bus_set_monitor; */
+ /* sd_bus_is_monitor; */
+ /* sd_bus_set_description; */
+ /* sd_bus_get_description; */
sd_bus_negotiate_creds;
+ /* sd_bus_negotiate_timestamp; */
+ /* sd_bus_negotiate_fds; */
+ sd_bus_can_send;
+ /* sd_bus_get_creds_mask; */
+ sd_bus_set_allow_interactive_authorization;
+ /* sd_bus_get_allow_interactive_authorization; */
sd_bus_start;
sd_bus_close;
sd_bus_try_close;
sd_bus_ref;
sd_bus_unref;
- sd_bus_is_open;
- sd_bus_can_send;
- sd_bus_get_bus_id;
+ /* sd_bus_is_open; */
+ /* sd_bus_get_bus_id; */
+ /* sd_bus_get_scope; */
+ /* sd_bus_get_tid; */
sd_bus_get_owner_creds;
- sd_bus_get_description;
sd_bus_send;
sd_bus_send_to;
sd_bus_call;
sd_bus_get_events;
sd_bus_get_timeout;
sd_bus_process;
- sd_bus_process_priority;
+ /* sd_bus_process_priority; */
sd_bus_wait;
sd_bus_flush;
+ /* sd_bus_get_current_slot; */
sd_bus_get_current_message;
- sd_bus_get_current_slot;
- sd_bus_get_tid;
+ sd_bus_get_current_handler;
+ sd_bus_get_current_userdata;
sd_bus_attach_event;
sd_bus_detach_event;
sd_bus_get_event;
sd_bus_add_object_vtable;
sd_bus_add_fallback_vtable;
sd_bus_add_node_enumerator;
- sd_bus_add_object_manager;
+ /* sd_bus_add_object_manager; */
sd_bus_slot_ref;
sd_bus_slot_unref;
- sd_bus_slot_get_bus;
- sd_bus_slot_get_userdata;
- sd_bus_slot_set_userdata;
- sd_bus_slot_get_description;
- sd_bus_slot_set_description;
- sd_bus_slot_get_current_message;
+ /* sd_bus_slot_get_bus; */
+ /* sd_bus_slot_get_userdata; */
+ /* sd_bus_slot_set_userdata; */
+ /* sd_bus_slot_get_description; */
+ /* sd_bus_slot_set_description; */
+ /* sd_bus_slot_get_current_message; */
+ /* sd_bus_slot_get_current_handler; */
+ /* sd_bus_slot_get_current_userdata; */
sd_bus_message_new_signal;
sd_bus_message_new_method_call;
sd_bus_message_new_method_return;
sd_bus_message_new_method_error;
sd_bus_message_new_method_errorf;
sd_bus_message_new_method_errno;
- sd_bus_message_new_method_errnof;
+ /* sd_bus_message_new_method_errnof; */
sd_bus_message_ref;
sd_bus_message_unref;
- sd_bus_message_get_bus;
- sd_bus_message_get_type;
- sd_bus_message_get_cookie;
- sd_bus_message_get_reply_cookie;
- sd_bus_message_get_expect_reply;
- sd_bus_message_get_auto_start;
- sd_bus_message_get_priority;
+ /* sd_bus_message_get_type; */
+ /* sd_bus_message_get_cookie; */
+ /* sd_bus_message_get_reply_cookie; */
+ /* sd_bus_message_get_priority; */
+ /* sd_bus_message_get_expect_reply; */
+ /* sd_bus_message_get_auto_start; */
+ sd_bus_message_get_allow_interactive_authorization;
sd_bus_message_get_signature;
sd_bus_message_get_path;
sd_bus_message_get_interface;
sd_bus_message_get_sender;
sd_bus_message_get_error;
sd_bus_message_get_errno;
- sd_bus_message_get_monotonic_usec;
- sd_bus_message_get_realtime_usec;
- sd_bus_message_get_seqnum;
+ /* sd_bus_message_get_monotonic_usec; */
+ /* sd_bus_message_get_realtime_usec; */
+ /* sd_bus_message_get_seqnum; */
+ sd_bus_message_get_bus;
sd_bus_message_get_creds;
- sd_bus_message_is_empty;
- sd_bus_message_is_signal;
+ /* sd_bus_message_is_signal; */
sd_bus_message_is_method_call;
sd_bus_message_is_method_error;
- sd_bus_message_set_expect_reply;
+ /* sd_bus_message_is_empty; */
+ /* sd_bus_message_has_signature; */
+ /* sd_bus_message_set_expect_reply; */
sd_bus_message_set_auto_start;
+ /* sd_bus_message_set_allow_interactive_authorization; */
sd_bus_message_set_destination;
- sd_bus_message_set_priority;
+ /* sd_bus_message_set_priority; */
sd_bus_message_append;
sd_bus_message_append_basic;
sd_bus_message_append_array;
sd_bus_message_append_array_space;
- sd_bus_message_append_array_iovec;
- sd_bus_message_append_array_memfd;
+ /* sd_bus_message_append_array_iovec; */
+ /* sd_bus_message_append_array_memfd; */
sd_bus_message_append_string_space;
- sd_bus_message_append_string_iovec;
- sd_bus_message_append_string_memfd;
+ /* sd_bus_message_append_string_iovec; */
+ /* sd_bus_message_append_string_memfd; */
sd_bus_message_append_strv;
sd_bus_message_open_container;
sd_bus_message_close_container;
sd_bus_message_enter_container;
sd_bus_message_exit_container;
sd_bus_message_peek_type;
- sd_bus_message_verify_type;
- sd_bus_message_at_end;
+ /* sd_bus_message_verify_type; */
+ /* sd_bus_message_at_end; */
sd_bus_message_rewind;
sd_bus_get_unique_name;
sd_bus_request_name;
sd_bus_release_name;
sd_bus_list_names;
sd_bus_get_name_creds;
- sd_bus_get_name_machine_id;
+ /* sd_bus_get_name_machine_id; */
sd_bus_call_method;
+ /* sd_bus_call_method_async; */
sd_bus_get_property;
- sd_bus_get_property_trivial;
+ /* sd_bus_get_property_trivial; */
sd_bus_get_property_string;
- sd_bus_get_property_strv;
- sd_bus_set_property;
+ /* sd_bus_get_property_strv; */
+ /* sd_bus_set_property; */
sd_bus_reply_method_return;
sd_bus_reply_method_error;
sd_bus_reply_method_errorf;
sd_bus_reply_method_errno;
- sd_bus_reply_method_errnof;
+ /* sd_bus_reply_method_errnof; */
sd_bus_emit_signal;
sd_bus_emit_properties_changed_strv;
sd_bus_emit_properties_changed;
sd_bus_emit_interfaces_removed_strv;
sd_bus_emit_interfaces_removed;
sd_bus_query_sender_creds;
- sd_bus_creds_new_from_pid;
+ sd_bus_query_sender_privilege;
+ /* sd_bus_creds_new_from_pid; */
sd_bus_creds_ref;
sd_bus_creds_unref;
- sd_bus_creds_get_mask;
- sd_bus_creds_get_uid;
- sd_bus_creds_get_gid;
+ /* sd_bus_creds_get_mask; */
+ sd_bus_creds_get_augmented_mask;
sd_bus_creds_get_pid;
+ /* sd_bus_creds_get_ppid; */
sd_bus_creds_get_tid;
- sd_bus_creds_get_comm;
- sd_bus_creds_get_tid_comm;
- sd_bus_creds_get_exe;
+ sd_bus_creds_get_uid;
+ sd_bus_creds_get_euid;
+ /* sd_bus_creds_get_suid; */
+ /* sd_bus_creds_get_fsuid; */
+ /* sd_bus_creds_get_gid; */
+ sd_bus_creds_get_egid;
+ /* sd_bus_creds_get_sgid; */
+ /* sd_bus_creds_get_fsgid; */
+ /* sd_bus_creds_get_supplementary_gids; */
+ /* sd_bus_creds_get_comm; */
+ /* sd_bus_creds_get_tid_comm; */
+ /* sd_bus_creds_get_exe; */
sd_bus_creds_get_cmdline;
- sd_bus_creds_get_cgroup;
- sd_bus_creds_get_unit;
- sd_bus_creds_get_user_unit;
- sd_bus_creds_get_slice;
+ /* sd_bus_creds_get_cgroup; */
+ /* sd_bus_creds_get_unit; */
+ /* sd_bus_creds_get_slice; */
+ /* sd_bus_creds_get_user_unit; */
+ /* sd_bus_creds_get_user_slice; */
sd_bus_creds_get_session;
sd_bus_creds_get_owner_uid;
sd_bus_creds_has_effective_cap;
- sd_bus_creds_has_permitted_cap;
- sd_bus_creds_has_inheritable_cap;
- sd_bus_creds_has_bounding_cap;
+ /* sd_bus_creds_has_permitted_cap; */
+ /* sd_bus_creds_has_inheritable_cap; */
+ /* sd_bus_creds_has_bounding_cap; */
sd_bus_creds_get_selinux_context;
- sd_bus_creds_get_audit_session_id;
+ /* sd_bus_creds_get_audit_session_id; */
sd_bus_creds_get_audit_login_uid;
- sd_bus_creds_get_unique_name;
- sd_bus_creds_get_well_known_names;
- sd_bus_creds_get_description;
+ sd_bus_creds_get_tty;
+ /* sd_bus_creds_get_unique_name; */
+ /* sd_bus_creds_get_well_known_names; */
+ /* sd_bus_creds_get_description; */
sd_bus_error_free;
sd_bus_error_set;
sd_bus_error_setf;
sd_bus_error_copy;
sd_bus_error_is_set;
sd_bus_error_has_name;
- sd_bus_path_encode;
- sd_bus_path_decode;
+ sd_bus_error_add_map;
+ /* sd_bus_path_encode; */
+ /* sd_bus_path_decode; */
sd_bus_track_new;
sd_bus_track_ref;
sd_bus_track_unref;
sd_bus_track_get_bus;
- sd_bus_track_get_userdata;
- sd_bus_track_set_userdata;
+ /* sd_bus_track_get_userdata; */
+ /* sd_bus_track_set_userdata; */
sd_bus_track_add_sender;
sd_bus_track_remove_sender;
sd_bus_track_add_name;
sd_event_add_signal;
sd_event_add_child;
sd_event_add_defer;
+ sd_event_add_post;
sd_event_add_exit;
- sd_event_wait;
sd_event_prepare;
+ sd_event_wait;
sd_event_dispatch;
sd_event_run;
- sd_event_loop;
+ /* sd_event_loop; */
sd_event_exit;
- sd_event_now;
- sd_event_get_fd;
+ /* sd_event_now; */
+ /* sd_event_get_fd; */
sd_event_get_state;
sd_event_get_tid;
sd_event_get_exit_code;
sd_event_set_watchdog;
- sd_event_get_watchdog;
- sd_event_source_ref;
+ /* sd_event_get_watchdog; */
+ /* sd_event_source_ref; */
sd_event_source_unref;
+ sd_event_source_get_event;
+ /* sd_event_source_get_userdata; */
+ /* sd_event_source_set_userdata; */
sd_event_source_set_description;
- sd_event_source_get_description;
+ /* sd_event_source_get_description; */
sd_event_source_set_prepare;
- sd_event_source_get_pending;
- sd_event_source_get_priority;
+ /* sd_event_source_get_pending; */
+ /* sd_event_source_get_priority; */
sd_event_source_set_priority;
- sd_event_source_get_enabled;
+ /* sd_event_source_get_enabled; */
sd_event_source_set_enabled;
- sd_event_source_get_userdata;
- sd_event_source_set_userdata;
- sd_event_source_get_io_fd;
+ /* sd_event_source_get_io_fd; */
sd_event_source_set_io_fd;
- sd_event_source_get_io_events;
+ /* sd_event_source_get_io_events; */
sd_event_source_set_io_events;
- sd_event_source_get_io_revents;
+ /* sd_event_source_get_io_revents; */
sd_event_source_get_time;
sd_event_source_set_time;
- sd_event_source_set_time_accuracy;
- sd_event_source_get_time_accuracy;
- sd_event_source_get_time_clock;
- sd_event_source_get_signal;
- sd_event_source_get_child_pid;
- sd_event_source_get_event;
-
- /* sd-utf8 */
- sd_utf8_is_valid;
- sd_ascii_is_valid;
+ /* sd_event_source_set_time_accuracy; */
+ /* sd_event_source_get_time_accuracy; */
+ /* sd_event_source_get_time_clock; */
+ /* sd_event_source_get_signal; */
+ /* sd_event_source_get_child_pid; */
+} LIBSYSTEMD_220;
- /* sd-resolve */
- sd_resolve_default;
- sd_resolve_new;
- sd_resolve_ref;
- sd_resolve_unref;
- sd_resolve_get_fd;
- sd_resolve_get_events;
- sd_resolve_get_timeout;
- sd_resolve_process;
- sd_resolve_wait;
- sd_resolve_get_tid;
- sd_resolve_attach_event;
- sd_resolve_detach_event;
- sd_resolve_get_event;
- sd_resolve_getaddrinfo;
- sd_resolve_getnameinfo;
- sd_resolve_res_query;
- sd_resolve_res_search;
- sd_resolve_query_ref;
- sd_resolve_query_unref;
- sd_resolve_query_is_done;
- sd_resolve_query_get_userdata;
- sd_resolve_query_set_userdata;
- sd_resolve_query_get_resolve;
+LIBSYSTEMD_222 {
+global:
+ /* sd-bus */
+ sd_bus_emit_object_added;
+ /* sd_bus_emit_object_removed; */
+ sd_bus_flush_close_unref;
+} LIBSYSTEMD_221;
- /* sd-path */
- sd_path_home;
- sd_path_search;
-} LIBSYSTEMD_217;
-)
+LIBSYSTEMD_226 {
+global:
+ sd_pid_get_cgroup;
+ sd_peer_get_cgroup;
+} LIBSYSTEMD_222;
= 12 bytes
-This header is then followed by the the fields array, whose first
-value is a 32bit array size.
+This header is then followed by the fields array, whose first value is
+a 32bit array size.
When using GVariant we keep the basic structure in place, only
slightly alter the header, and define protocol version '2'. The new
+++ /dev/null
-../../Makefile
\ No newline at end of file
p = stpcpy(stpcpy(c, a), ":");
strcpy(p, b);
+ bloom_add_data(filter, size, k, c, n);
+
for (;;) {
char *e;
e = strrchr(p, sep);
- if (!e || e == p)
+ if (!e)
+ break;
+
+ *(e + 1) = 0;
+ bloom_add_data(filter, size, k, c, e - c + 1);
+
+ if (e == p)
break;
*e = 0;
#define BUS_ERROR_NO_MACHINE_FOR_PID "org.freedesktop.machine1.NoMachineForPID"
#define BUS_ERROR_MACHINE_EXISTS "org.freedesktop.machine1.MachineExists"
#define BUS_ERROR_NO_PRIVATE_NETWORKING "org.freedesktop.machine1.NoPrivateNetworking"
+#define BUS_ERROR_NO_SUCH_USER_MAPPING "org.freedesktop.machine1.NoSuchUserMapping"
+#define BUS_ERROR_NO_SUCH_GROUP_MAPPING "org.freedesktop.machine1.NoSuchGroupMapping"
#define BUS_ERROR_NO_SUCH_SESSION "org.freedesktop.login1.NoSuchSession"
#define BUS_ERROR_NO_SESSION_FOR_PID "org.freedesktop.login1.NoSessionForPID"
#define BUS_ERROR_DEVICE_NOT_TAKEN "org.freedesktop.login1.DeviceNotTaken"
#define BUS_ERROR_OPERATION_IN_PROGRESS "org.freedesktop.login1.OperationInProgress"
#define BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED "org.freedesktop.login1.SleepVerbNotSupported"
+#define BUS_ERROR_SESSION_BUSY "org.freedesktop.login1.SessionBusy"
#define BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED "org.freedesktop.timedate1.AutomaticTimeSyncEnabled"
#include <fcntl.h>
#include "util.h"
+#include "process-util.h"
#include "bus-internal.h"
#include "bus-socket.h"
#include "bus-container.h"
int bus_container_connect_socket(sd_bus *b) {
- _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
+ _cleanup_close_pair_ int pair[2] = { -1, -1 };
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
pid_t child;
siginfo_t si;
- int r;
+ int r, error_buf = 0;
+ ssize_t n;
assert(b);
assert(b->input_fd < 0);
return r;
}
- r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &rootfd);
+ r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd);
if (r < 0)
return r;
bus_socket_setup(b);
+ if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
+ return -errno;
+
child = fork();
if (child < 0)
return -errno;
if (child == 0) {
pid_t grandchild;
- r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
+ pair[0] = safe_close(pair[0]);
+
+ r = namespace_enter(pidnsfd, mntnsfd, -1, usernsfd, rootfd);
if (r < 0)
- _exit(255);
+ _exit(EXIT_FAILURE);
/* We just changed PID namespace, however it will only
* take effect on the children we now fork. Hence,
grandchild = fork();
if (grandchild < 0)
- _exit(255);
+ _exit(EXIT_FAILURE);
if (grandchild == 0) {
r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size);
if (r < 0) {
- if (errno == EINPROGRESS)
- _exit(1);
-
- _exit(255);
+ /* Try to send error up */
+ error_buf = errno;
+ (void) write(pair[1], &error_buf, sizeof(error_buf));
+ _exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
r = wait_for_terminate(grandchild, &si);
if (r < 0)
- _exit(255);
+ _exit(EXIT_FAILURE);
if (si.si_code != CLD_EXITED)
- _exit(255);
+ _exit(EXIT_FAILURE);
_exit(si.si_status);
}
+ pair[1] = safe_close(pair[1]);
+
r = wait_for_terminate(child, &si);
if (r < 0)
return r;
+ n = read(pair[0], &error_buf, sizeof(error_buf));
+ if (n < 0)
+ return -errno;
+
+ if (n > 0) {
+ if (n != sizeof(error_buf))
+ return -EIO;
+
+ if (error_buf < 0)
+ return -EIO;
+
+ if (error_buf == EINPROGRESS)
+ return 1;
+
+ if (error_buf > 0)
+ return -error_buf;
+ }
+
if (si.si_code != CLD_EXITED)
return -EIO;
- if (si.si_status == 1)
- return 1;
-
if (si.si_status != EXIT_SUCCESS)
return -EIO;
int bus_container_connect_kernel(sd_bus *b) {
_cleanup_close_pair_ int pair[2] = { -1, -1 };
- _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
union {
struct cmsghdr cmsghdr;
uint8_t buf[CMSG_SPACE(sizeof(int))];
} control = {};
+ int error_buf = 0;
+ struct iovec iov = {
+ .iov_base = &error_buf,
+ .iov_len = sizeof(error_buf),
+ };
struct msghdr mh = {
.msg_control = &control,
.msg_controllen = sizeof(control),
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
};
struct cmsghdr *cmsg;
pid_t child;
siginfo_t si;
- int r;
- _cleanup_close_ int fd = -1;
+ int r, fd = -1;
+ ssize_t n;
assert(b);
assert(b->input_fd < 0);
return r;
}
- r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &rootfd);
+ r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd);
if (r < 0)
return r;
- if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
+ if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
return -errno;
child = fork();
pair[0] = safe_close(pair[0]);
- r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
+ r = namespace_enter(pidnsfd, mntnsfd, -1, usernsfd, rootfd);
if (r < 0)
_exit(EXIT_FAILURE);
_exit(EXIT_FAILURE);
if (grandchild == 0) {
-
fd = open(b->kernel, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
+ if (fd < 0) {
+ /* Try to send error up */
+ error_buf = errno;
+ (void) write(pair[1], &error_buf, sizeof(error_buf));
_exit(EXIT_FAILURE);
+ }
cmsg = CMSG_FIRSTHDR(&mh);
cmsg->cmsg_level = SOL_SOCKET;
if (r < 0)
return r;
- if (si.si_code != CLD_EXITED)
- return -EIO;
-
- if (si.si_status != EXIT_SUCCESS)
- return -EIO;
-
- if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
+ n = recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
+ if (n < 0)
return -errno;
- for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
+ CMSG_FOREACH(cmsg, &mh) {
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
int *fds;
unsigned n_fds;
+ assert(fd < 0);
+
fds = (int*) CMSG_DATA(cmsg);
n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
fd = fds[0];
}
+ }
+
+ /* If there's an fd passed, we are good. */
+ if (fd >= 0) {
+ b->input_fd = b->output_fd = fd;
+ return bus_kernel_take_fd(b);
+ }
- b->input_fd = b->output_fd = fd;
- fd = -1;
+ /* If there's an error passed, use it */
+ if (n == sizeof(error_buf) && error_buf > 0)
+ return -error_buf;
- return bus_kernel_take_fd(b);
+ /* Otherwise, we have no clue */
+ return -EIO;
}
assert_return(unique, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
+ if (!bus->bus_client)
+ return -EINVAL;
+
r = bus_ensure_running(bus);
if (r < 0)
return r;
_public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags) {
assert_return(bus, -EINVAL);
assert_return(name, -EINVAL);
- assert_return(bus->bus_client, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL);
assert_return(service_name_is_valid(name), -EINVAL);
assert_return(name[0] != ':', -EINVAL);
+ if (!bus->bus_client)
+ return -EINVAL;
+
/* Don't allow requesting the special driver and local names */
if (STR_IN_SET(name, "org.freedesktop.DBus", "org.freedesktop.DBus.Local"))
return -EINVAL;
_public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
assert_return(bus, -EINVAL);
assert_return(name, -EINVAL);
- assert_return(bus->bus_client, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
assert_return(service_name_is_valid(name), -EINVAL);
assert_return(name[0] != ':', -EINVAL);
+ if (!bus->bus_client)
+ return -EINVAL;
+
/* Don't allow releasing the special driver and local names */
if (STR_IN_SET(name, "org.freedesktop.DBus", "org.freedesktop.DBus.Local"))
return -EINVAL;
name_list = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
KDBUS_FOREACH(name, name_list, cmd.list_size) {
-
struct kdbus_item *item;
- const char *entry_name = NULL;
- if ((flags & KDBUS_LIST_UNIQUE) && name->id != previous_id) {
+ if ((flags & KDBUS_LIST_UNIQUE) && name->id != previous_id && !(name->flags & KDBUS_HELLO_ACTIVATOR)) {
char *n;
if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0) {
previous_id = name->id;
}
- KDBUS_ITEM_FOREACH(item, name, items)
- if (item->type == KDBUS_ITEM_OWNED_NAME)
- entry_name = item->name.name;
-
- if (entry_name && service_name_is_valid(entry_name)) {
- r = strv_extend(x, entry_name);
- if (r < 0) {
- r = -ENOMEM;
- goto fail;
+ KDBUS_ITEM_FOREACH(item, name, items) {
+ if (item->type == KDBUS_ITEM_OWNED_NAME) {
+ if (service_name_is_valid(item->name.name)) {
+ r = strv_extend(x, item->name.name);
+ if (r < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+ }
}
}
}
assert_return(acquired || activatable, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
+ if (!bus->bus_client)
+ return -EINVAL;
+
if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
c->mask |= SD_BUS_CREDS_TID;
}
+ if (mask & SD_BUS_CREDS_PPID) {
+ if (item->pids.ppid > 0) {
+ c->ppid = (pid_t) item->pids.ppid;
+ c->mask |= SD_BUS_CREDS_PPID;
+ } else if (item->pids.pid == 1) {
+ /* The structure doesn't
+ * really distinguish the case
+ * where a process has no
+ * parent and where we don't
+ * know it because it could
+ * not be translated due to
+ * namespaces. However, we
+ * know that PID 1 has no
+ * parent process, hence let's
+ * patch that in, manually. */
+ c->ppid = 0;
+ c->mask |= SD_BUS_CREDS_PPID;
+ }
+ }
+
break;
case KDBUS_ITEM_CREDS:
break;
case KDBUS_ITEM_AUDIT:
- if (mask & SD_BUS_CREDS_AUDIT_SESSION_ID && (uint32_t) item->audit.sessionid != (uint32_t) -1) {
+ if (mask & SD_BUS_CREDS_AUDIT_SESSION_ID) {
c->audit_session_id = (uint32_t) item->audit.sessionid;
c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
}
- if (mask & SD_BUS_CREDS_AUDIT_LOGIN_UID && (uid_t) item->audit.loginuid != UID_INVALID) {
+ if (mask & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
c->audit_login_uid = (uid_t) item->audit.loginuid;
c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
* the bits we want, then ask for the PID/TID so that we
* can read the rest from /proc. */
if ((mask & SD_BUS_CREDS_AUGMENT) &&
- (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+ (mask & (SD_BUS_CREDS_PPID|
+ SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
assert_return(mask == 0 || creds, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
assert_return(service_name_is_valid(name), -EINVAL);
- assert_return(bus->bus_client, -ENODATA);
+
+ if (!bus->bus_client)
+ return -EINVAL;
if (streq(name, "org.freedesktop.DBus.Local"))
return -EINVAL;
* to get the bits we want, then ask for the PID/TID so that we
* can read the rest from /proc. */
if ((mask & SD_BUS_CREDS_AUGMENT) &&
- (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+ (mask & (SD_BUS_CREDS_PPID|
+ SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
_cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
pid_t pid = 0;
int r;
+ bool do_label = bus->label && (mask & SD_BUS_CREDS_SELINUX_CONTEXT);
- if (!bus->ucred_valid && !isempty(bus->label))
+ /* Avoid allocating anything if we have no chance of returning useful data */
+ if (!bus->ucred_valid && !do_label)
return -ENODATA;
c = bus_creds_new();
}
}
- if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
+ if (do_label) {
c->label = strdup(bus->label);
if (!c->label)
return -ENOMEM;
/* If the old name is unset or empty, then
* this can match against added names */
- if (!old_owner || old_owner[0] == 0) {
+ if (isempty(old_owner)) {
item->type = KDBUS_ITEM_NAME_ADD;
r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
/* If the new name is unset or empty, then
* this can match against removed names */
- if (!new_owner || new_owner[0] == 0) {
+ if (isempty(new_owner)) {
item->type = KDBUS_ITEM_NAME_REMOVE;
r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
/* If the old name is unset or empty, then this can
* match against added ids */
- if (!old_owner || old_owner[0] == 0) {
+ if (isempty(old_owner)) {
item->type = KDBUS_ITEM_ID_ADD;
+ if (!isempty(new_owner))
+ item->id_change.id = new_owner_id;
r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
if (r < 0)
/* If thew new name is unset or empty, then this can
* match against removed ids */
- if (!new_owner || new_owner[0] == 0) {
+ if (isempty(new_owner)) {
item->type = KDBUS_ITEM_ID_REMOVE;
+ if (!isempty(old_owner))
+ item->id_change.id = old_owner_id;
r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
if (r < 0)
size_t sz;
const char *sender = NULL;
size_t sender_length = 0;
- uint64_t src_id = KDBUS_MATCH_ID_ANY;
+ uint64_t src_id = KDBUS_MATCH_ID_ANY, dst_id = KDBUS_MATCH_ID_ANY;
bool using_bloom = false;
unsigned i;
bool matches_name_change = true;
break;
case BUS_MATCH_PATH_NAMESPACE:
- if (!streq(c->value_str, "/")) {
- bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "path-slash-prefix", c->value_str);
- using_bloom = true;
- }
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "path-slash-prefix", c->value_str);
+ using_bloom = true;
break;
case BUS_MATCH_ARG...BUS_MATCH_ARG_LAST: {
break;
}
- case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST: {
- char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
+ case BUS_MATCH_ARG_HAS...BUS_MATCH_ARG_HAS_LAST: {
+ char buf[sizeof("arg")-1 + 2 + sizeof("-has")];
- xsprintf(buf, "arg%i-slash-prefix", c->type - BUS_MATCH_ARG_PATH);
+ xsprintf(buf, "arg%i-has", c->type - BUS_MATCH_ARG_HAS);
bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
using_bloom = true;
break;
}
+ case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST:
+ /*
+ * XXX: DBus spec defines arg[0..63]path= matching to be
+ * a two-way glob. That is, if either string is a prefix
+ * of the other, it matches.
+ * This is really hard to realize in bloom-filters, as
+ * we would have to create a bloom-match for each prefix
+ * of @c->value_str. This is excessive, hence we just
+ * ignore all those matches and accept everything from
+ * the kernel. People should really avoid those matches.
+ * If they're used in real-life some day, we will have
+ * to properly support multiple-matches here.
+ */
+ break;
+
case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST: {
char buf[sizeof("arg")-1 + 2 + sizeof("-dot-prefix")];
}
case BUS_MATCH_DESTINATION:
- /* The bloom filter does not include
- the destination, since it is only
- available for broadcast messages
- which do not carry a destination
- since they are undirected. */
+ /*
+ * Kernel only supports matching on destination IDs, but
+ * not on destination names. So just skip the
+ * destination name restriction and verify it in
+ * user-space on retrieval.
+ */
+ r = bus_kernel_parse_unique_name(c->value_str, &dst_id);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ sz += ALIGN8(offsetof(struct kdbus_item, id) + sizeof(uint64_t));
+
+ /* if not a broadcast, it cannot be a name-change */
+ if (r <= 0 || dst_id != KDBUS_DST_ID_BROADCAST)
+ matches_name_change = false;
+
break;
case BUS_MATCH_ROOT:
item = KDBUS_ITEM_NEXT(item);
}
+ if (dst_id != KDBUS_MATCH_ID_ANY) {
+ item->size = offsetof(struct kdbus_item, id) + sizeof(uint64_t);
+ item->type = KDBUS_ITEM_DST_ID;
+ item->id = dst_id;
+ item = KDBUS_ITEM_NEXT(item);
+ }
+
if (using_bloom) {
item->size = offsetof(struct kdbus_item, data64) + bus->bloom_size;
item->type = KDBUS_ITEM_BLOOM_MASK;
assert(bus);
+ if (!bus->bus_client)
+ return -EINVAL;
+
if (bus->is_kernel)
return bus_add_match_internal_kernel(bus, components, n_components, cookie);
else
assert(bus);
+ if (!bus->bus_client)
+ return -EINVAL;
+
if (bus->is_kernel)
return bus_remove_match_internal_kernel(bus, cookie);
else
return bus_remove_match_internal_dbus1(bus, match);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
const char *mid;
assert_return(!bus_pid_changed(bus), -ECHILD);
assert_return(service_name_is_valid(name), -EINVAL);
+ if (!bus->bus_client)
+ return -EINVAL;
+
if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
return sd_id128_from_string(mid, machine);
}
+#endif // 0
return sd_bus_send(bus, m, NULL);
}
-_public_ int sd_bus_call_method(
+/// UNNEEDED by elogind
+#if 0
+_public_ int sd_bus_call_method_async(
sd_bus *bus,
+ sd_bus_slot **slot,
const char *destination,
const char *path,
const char *interface,
const char *member,
- sd_bus_error *error,
- sd_bus_message **reply,
+ sd_bus_message_handler_t callback,
+ void *userdata,
const char *types, ...) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
return r;
}
+ return sd_bus_call_async(bus, slot, m, callback, userdata, 0);
+}
+#endif // 0
+
+_public_ int sd_bus_call_method(
+ sd_bus *bus,
+ const char *destination,
+ const char *path,
+ const char *interface,
+ const char *member,
+ sd_bus_error *error,
+ sd_bus_message **reply,
+ const char *types, ...) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ int r;
+
+ bus_assert_return(bus, -EINVAL, error);
+ bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
+
+ if (!BUS_IS_OPEN(bus->state)) {
+ r = -ENOTCONN;
+ goto fail;
+ }
+
+ r = sd_bus_message_new_method_call(bus, &m, destination, path, interface, member);
+ if (r < 0)
+ goto fail;
+
+ if (!isempty(types)) {
+ va_list ap;
+
+ va_start(ap, types);
+ r = bus_message_append_ap(m, types, ap);
+ va_end(ap);
+ if (r < 0)
+ goto fail;
+ }
+
return sd_bus_call(bus, m, 0, error, reply);
+
+fail:
+ return sd_bus_error_set_errno(error, r);
}
_public_ int sd_bus_reply_method_return(
return sd_bus_reply_method_error(call, &berror);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_reply_method_errnof(
sd_bus_message *call,
int error,
return sd_bus_reply_method_error(call, &berror);
}
+#endif // 0
_public_ int sd_bus_get_property(
sd_bus *bus,
sd_bus_message *rep = NULL;
int r;
- assert_return(bus, -EINVAL);
- assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
- assert_return(member_name_is_valid(member), -EINVAL);
- assert_return(reply, -EINVAL);
- assert_return(signature_is_single(type, false), -EINVAL);
- assert_return(!bus_pid_changed(bus), -ECHILD);
+ bus_assert_return(bus, -EINVAL, error);
+ bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
+ bus_assert_return(member_name_is_valid(member), -EINVAL, error);
+ bus_assert_return(reply, -EINVAL, error);
+ bus_assert_return(signature_is_single(type, false), -EINVAL, error);
+ bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
- if (!BUS_IS_OPEN(bus->state))
- return -ENOTCONN;
+ if (!BUS_IS_OPEN(bus->state)) {
+ r = -ENOTCONN;
+ goto fail;
+ }
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &rep, "ss", strempty(interface), member);
if (r < 0)
r = sd_bus_message_enter_container(rep, 'v', type);
if (r < 0) {
sd_bus_message_unref(rep);
- return r;
+ goto fail;
}
*reply = rep;
return 0;
+
+fail:
+ return sd_bus_error_set_errno(error, r);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_property_trivial(
sd_bus *bus,
const char *destination,
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
int r;
- assert_return(bus, -EINVAL);
- assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
- assert_return(member_name_is_valid(member), -EINVAL);
- assert_return(bus_type_is_trivial(type), -EINVAL);
- assert_return(ptr, -EINVAL);
- assert_return(!bus_pid_changed(bus), -ECHILD);
+ bus_assert_return(bus, -EINVAL, error);
+ bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
+ bus_assert_return(member_name_is_valid(member), -EINVAL, error);
+ bus_assert_return(bus_type_is_trivial(type), -EINVAL, error);
+ bus_assert_return(ptr, -EINVAL, error);
+ bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
- if (!BUS_IS_OPEN(bus->state))
- return -ENOTCONN;
+ if (!BUS_IS_OPEN(bus->state)) {
+ r = -ENOTCONN;
+ goto fail;
+ }
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
if (r < 0)
r = sd_bus_message_enter_container(reply, 'v', CHAR_TO_STR(type));
if (r < 0)
- return r;
+ goto fail;
r = sd_bus_message_read_basic(reply, type, ptr);
if (r < 0)
- return r;
+ goto fail;
return 0;
+
+fail:
+ return sd_bus_error_set_errno(error, r);
}
+#endif // 0
_public_ int sd_bus_get_property_string(
sd_bus *bus,
char *n;
int r;
- assert_return(bus, -EINVAL);
- assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
- assert_return(member_name_is_valid(member), -EINVAL);
- assert_return(ret, -EINVAL);
- assert_return(!bus_pid_changed(bus), -ECHILD);
+ bus_assert_return(bus, -EINVAL, error);
+ bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
+ bus_assert_return(member_name_is_valid(member), -EINVAL, error);
+ bus_assert_return(ret, -EINVAL, error);
+ bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
- if (!BUS_IS_OPEN(bus->state))
- return -ENOTCONN;
+ if (!BUS_IS_OPEN(bus->state)) {
+ r = -ENOTCONN;
+ goto fail;
+ }
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
if (r < 0)
r = sd_bus_message_enter_container(reply, 'v', "s");
if (r < 0)
- return r;
+ goto fail;
r = sd_bus_message_read_basic(reply, 's', &s);
if (r < 0)
- return r;
+ goto fail;
n = strdup(s);
- if (!n)
- return -ENOMEM;
+ if (!n) {
+ r = -ENOMEM;
+ goto fail;
+ }
*ret = n;
return 0;
+
+fail:
+ return sd_bus_error_set_errno(error, r);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_property_strv(
sd_bus *bus,
const char *destination,
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
int r;
- assert_return(bus, -EINVAL);
- assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
- assert_return(member_name_is_valid(member), -EINVAL);
- assert_return(ret, -EINVAL);
- assert_return(!bus_pid_changed(bus), -ECHILD);
+ bus_assert_return(bus, -EINVAL, error);
+ bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
+ bus_assert_return(member_name_is_valid(member), -EINVAL, error);
+ bus_assert_return(ret, -EINVAL, error);
+ bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
- if (!BUS_IS_OPEN(bus->state))
- return -ENOTCONN;
+ if (!BUS_IS_OPEN(bus->state)) {
+ r = -ENOTCONN;
+ goto fail;
+ }
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
if (r < 0)
r = sd_bus_message_enter_container(reply, 'v', NULL);
if (r < 0)
- return r;
+ goto fail;
r = sd_bus_message_read_strv(reply, ret);
if (r < 0)
- return r;
+ goto fail;
return 0;
+
+fail:
+ return sd_bus_error_set_errno(error, r);
}
_public_ int sd_bus_set_property(
va_list ap;
int r;
- assert_return(bus, -EINVAL);
- assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
- assert_return(member_name_is_valid(member), -EINVAL);
- assert_return(signature_is_single(type, false), -EINVAL);
- assert_return(!bus_pid_changed(bus), -ECHILD);
+ bus_assert_return(bus, -EINVAL, error);
+ bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
+ bus_assert_return(member_name_is_valid(member), -EINVAL, error);
+ bus_assert_return(signature_is_single(type, false), -EINVAL, error);
+ bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
- if (!BUS_IS_OPEN(bus->state))
- return -ENOTCONN;
+ if (!BUS_IS_OPEN(bus->state)) {
+ r = -ENOTCONN;
+ goto fail;
+ }
r = sd_bus_message_new_method_call(bus, &m, destination, path, "org.freedesktop.DBus.Properties", "Set");
if (r < 0)
- return r;
+ goto fail;
r = sd_bus_message_append(m, "ss", strempty(interface), member);
if (r < 0)
- return r;
+ goto fail;
r = sd_bus_message_open_container(m, 'v', type);
if (r < 0)
- return r;
+ goto fail;
va_start(ap, type);
r = bus_message_append_ap(m, type, ap);
va_end(ap);
if (r < 0)
- return r;
+ goto fail;
r = sd_bus_message_close_container(m);
if (r < 0)
- return r;
+ goto fail;
return sd_bus_call(bus, m, 0, error, NULL);
+
+fail:
+ return sd_bus_error_set_errno(error, r);
}
+#endif // 0
_public_ int sd_bus_query_sender_creds(sd_bus_message *call, uint64_t mask, sd_bus_creds **creds) {
sd_bus_creds *c;
/* No data passed? Or not enough data passed to retrieve the missing bits? */
if (!c || !(c->mask & SD_BUS_CREDS_PID)) {
/* We couldn't read anything from the call, let's try
- * to get it from the sender or peer */
+ * to get it from the sender or peer. */
if (call->sender)
+ /* There's a sender, but the creds are
+ * missing. This means we are talking via
+ * dbus1, or are getting a message that was
+ * sent to us via kdbus, but was converted
+ * from a dbus1 message by the bus-proxy and
+ * thus also lacks the creds. */
return sd_bus_get_name_creds(call->bus, call->sender, mask, creds);
else
+ /* There's no sender, hence we are on a dbus1
+ * direct connection. For direct connections
+ * the credentials of the AF_UNIX peer matter,
+ * which may be queried via
+ * sd_bus_get_owner_creds(). */
return sd_bus_get_owner_creds(call->bus, mask, creds);
}
return -ENOTCONN;
if (capability >= 0) {
+
r = sd_bus_query_sender_creds(call, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS, &creds);
if (r < 0)
return r;
+ /* We cannot use augmented caps for authorization,
+ * since then data is acquired raceful from
+ * /proc. This can never actually happen, but let's
+ * better be safe than sorry, and do an extra check
+ * here. */
+ assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EFFECTIVE_CAPS) == 0, -EPERM);
+
/* Note that not even on kdbus we might have the caps
* field, due to faked identities, or namespace
* translation issues. */
if (our_uid != 0 || !know_caps || capability < 0) {
uid_t sender_uid;
+ /* We cannot use augmented uid/euid for authorization,
+ * since then data is acquired raceful from
+ * /proc. This can never actually happen, but let's
+ * better be safe than sorry, and do an extra check
+ * here. */
+ assert_return((sd_bus_creds_get_augmented_mask(creds) & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID)) == 0, -EPERM);
+
/* Try to use the EUID, if we have it. */
r = sd_bus_creds_get_euid(creds, &sender_uid);
if (r < 0)
#include <linux/capability.h>
#include "util.h"
+#include "formats-util.h"
+#include "process-util.h"
+#include "terminal-util.h"
#include "capability.h"
#include "cgroup-util.h"
#include "fileio.h"
free(c->unit);
free(c->user_unit);
free(c->slice);
+ free(c->user_slice);
free(c->unescaped_description);
free(c->supplementary_gids);
+ free(c->tty);
free(c->well_known_names); /* note that this is an strv, but
* we only free the array, not the
free(c->cgroup_root);
free(c->description);
- free(c->supplementary_gids);
- c->supplementary_gids = NULL;
+ c->supplementary_gids = mfree(c->supplementary_gids);
strv_free(c->well_known_names);
c->well_known_names = NULL;
return NULL;
}
+/// UNNEEDED by elogind
+#if 0
_public_ uint64_t sd_bus_creds_get_mask(const sd_bus_creds *c) {
assert_return(c, 0);
return c->mask;
}
+#endif // 0
+
+_public_ uint64_t sd_bus_creds_get_augmented_mask(const sd_bus_creds *c) {
+ assert_return(c, 0);
+
+ return c->augmented;
+}
sd_bus_creds* bus_creds_new(void) {
sd_bus_creds *c;
return c;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_creds_new_from_pid(sd_bus_creds **ret, pid_t pid, uint64_t mask) {
sd_bus_creds *c;
int r;
*ret = c;
return 0;
}
+#endif // 0
_public_ int sd_bus_creds_get_uid(sd_bus_creds *c, uid_t *uid) {
assert_return(c, -EINVAL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_creds_get_suid(sd_bus_creds *c, uid_t *suid) {
assert_return(c, -EINVAL);
assert_return(suid, -EINVAL);
*gid = c->gid;
return 0;
}
-
+#endif // 0
_public_ int sd_bus_creds_get_egid(sd_bus_creds *c, gid_t *egid) {
assert_return(c, -EINVAL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_creds_get_sgid(sd_bus_creds *c, gid_t *sgid) {
assert_return(c, -EINVAL);
assert_return(sgid, -EINVAL);
*gids = c->supplementary_gids;
return (int) c->n_supplementary_gids;
}
+#endif // 0
_public_ int sd_bus_creds_get_pid(sd_bus_creds *c, pid_t *pid) {
assert_return(c, -EINVAL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
+_public_ int sd_bus_creds_get_ppid(sd_bus_creds *c, pid_t *ppid) {
+ assert_return(c, -EINVAL);
+ assert_return(ppid, -EINVAL);
+
+ if (!(c->mask & SD_BUS_CREDS_PPID))
+ return -ENODATA;
+
+ /* PID 1 has no parent process. Let's distinguish the case of
+ * not knowing and not having a parent process by the returned
+ * error code. */
+ if (c->ppid == 0)
+ return -ENXIO;
+
+ *ppid = c->ppid;
+ return 0;
+}
+#endif // 0
+
_public_ int sd_bus_creds_get_tid(sd_bus_creds *c, pid_t *tid) {
assert_return(c, -EINVAL);
assert_return(tid, -EINVAL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_creds_get_comm(sd_bus_creds *c, const char **ret) {
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
if (!(c->mask & SD_BUS_CREDS_EXE))
return -ENODATA;
- assert(c->exe);
+ if (!c->exe)
+ return -ENXIO;
+
*ret = c->exe;
return 0;
}
}
_public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
+ int r;
+
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert(c->cgroup);
- if (!c->unit)
- return -ESRCH;
+ if (!c->unit) {
+ const char *shifted;
+
+ r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
+ if (r < 0)
+ return r;
+
+ r = cg_path_get_unit(shifted, (char**) &c->unit);
+ if (r < 0)
+ return r;
+ }
*ret = c->unit;
return 0;
}
_public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
+ int r;
+
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert(c->cgroup);
- if (!c->user_unit)
- return -ESRCH;
+ if (!c->user_unit) {
+ const char *shifted;
+
+ r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
+ if (r < 0)
+ return r;
+
+ r = cg_path_get_user_unit(shifted, (char**) &c->user_unit);
+ if (r < 0)
+ return r;
+ }
*ret = c->user_unit;
return 0;
}
_public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
+ int r;
+
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert(c->cgroup);
- if (!c->slice)
- return -ESRCH;
+ if (!c->slice) {
+ const char *shifted;
+
+ r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
+ if (r < 0)
+ return r;
+
+ r = cg_path_get_slice(shifted, (char**) &c->slice);
+ if (r < 0)
+ return r;
+ }
*ret = c->slice;
return 0;
}
+_public_ int sd_bus_creds_get_user_slice(sd_bus_creds *c, const char **ret) {
+ int r;
+
+ assert_return(c, -EINVAL);
+ assert_return(ret, -EINVAL);
+
+ if (!(c->mask & SD_BUS_CREDS_USER_SLICE))
+ return -ENODATA;
+
+ assert(c->cgroup);
+
+ if (!c->user_slice) {
+ const char *shifted;
+
+ r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
+ if (r < 0)
+ return r;
+
+ r = cg_path_get_user_slice(shifted, (char**) &c->user_slice);
+ if (r < 0)
+ return r;
+ }
+
+ *ret = c->user_slice;
+ return 0;
+}
+#endif // 0
+
_public_ int sd_bus_creds_get_session(sd_bus_creds *c, const char **ret) {
int r;
if (!c->session) {
const char *shifted;
+ log_debug_elogind("Shifting cgroup \"%s\", root \"%s\"",
+ c->cgroup, c->cgroup_root ? c->cgroup_root : "NULL");
r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
if (r < 0)
return r;
+ log_debug_elogind("Shifted: \"%s\"", shifted);
r = cg_path_get_session(shifted, (char**) &c->session);
if (r < 0)
return r;
if (r < 0)
return r;
- return -ESRCH;
+/// elogind does not support systemd slices
+#if 0
+ return cg_path_get_owner_uid(shifted, uid);
+#else
+ *uid = c->uid;
+ return 0;
+#endif // 0
}
_public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
if (!(c->mask & SD_BUS_CREDS_CMDLINE))
return -ENODATA;
- assert_return(c->cmdline, -ESRCH);
- assert(c->cmdline);
+ if (!c->cmdline)
+ return -ENXIO;
if (!c->cmdline_array) {
c->cmdline_array = strv_parse_nulstr(c->cmdline, c->cmdline_size);
if (!(c->mask & SD_BUS_CREDS_AUDIT_SESSION_ID))
return -ENODATA;
+ if (c->audit_session_id == AUDIT_SESSION_INVALID)
+ return -ENXIO;
+
*sessionid = c->audit_session_id;
return 0;
}
if (!(c->mask & SD_BUS_CREDS_AUDIT_LOGIN_UID))
return -ENODATA;
+ if (c->audit_login_uid == UID_INVALID)
+ return -ENXIO;
+
*uid = c->audit_login_uid;
return 0;
}
+_public_ int sd_bus_creds_get_tty(sd_bus_creds *c, const char **ret) {
+ assert_return(c, -EINVAL);
+ assert_return(ret, -EINVAL);
+
+ if (!(c->mask & SD_BUS_CREDS_TTY))
+ return -ENODATA;
+
+ if (!c->tty)
+ return -ENXIO;
+
+ *ret = c->tty;
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_creds_get_unique_name(sd_bus_creds *c, const char **unique_name) {
assert_return(c, -EINVAL);
assert_return(unique_name, -EINVAL);
*ret = c->unescaped_description;
return 0;
}
+#endif // 0
static int has_cap(sd_bus_creds *c, unsigned offset, int capability) {
size_t sz;
assert(capability >= 0);
assert(c->capability);
- sz = DIV_ROUND_UP(cap_last_cap(), 32U);
- if ((unsigned)capability > cap_last_cap())
+ if ((unsigned) capability > cap_last_cap())
return 0;
+ sz = DIV_ROUND_UP(cap_last_cap(), 32U);
+
return !!(c->capability[offset * sz + CAP_TO_INDEX(capability)] & CAP_TO_MASK(capability));
}
return has_cap(c, CAP_OFFSET_EFFECTIVE, capability);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability) {
assert_return(c, -EINVAL);
assert_return(capability >= 0, -EINVAL);
return has_cap(c, CAP_OFFSET_BOUNDING, capability);
}
+#endif // 0
static int parse_caps(sd_bus_creds *c, unsigned offset, const char *p) {
size_t sz, max;
if (!(mask & SD_BUS_CREDS_AUGMENT))
return 0;
- missing = mask & ~c->mask;
- if (missing == 0)
- return 0;
-
/* Try to retrieve PID from creds if it wasn't passed to us */
- if (pid <= 0 && (c->mask & SD_BUS_CREDS_PID))
+ if (pid > 0) {
+ c->pid = pid;
+ c->mask |= SD_BUS_CREDS_PID;
+ } else if (c->mask & SD_BUS_CREDS_PID)
pid = c->pid;
+ else
+ /* Without pid we cannot do much... */
+ return 0;
+ /* Try to retrieve TID from creds if it wasn't passed to us */
if (tid <= 0 && (c->mask & SD_BUS_CREDS_TID))
- tid = c->pid;
+ tid = c->tid;
- /* Without pid we cannot do much... */
- if (pid <= 0)
+ /* Calculate what we shall and can add */
+ missing = mask & ~(c->mask|SD_BUS_CREDS_PID|SD_BUS_CREDS_TID|SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_DESCRIPTION|SD_BUS_CREDS_AUGMENT);
+ if (missing == 0)
return 0;
- if (pid > 0) {
- c->pid = pid;
- c->mask |= SD_BUS_CREDS_PID;
- }
-
if (tid > 0) {
c->tid = tid;
c->mask |= SD_BUS_CREDS_TID;
}
- if (missing & (SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_SUID | SD_BUS_CREDS_FSUID |
+ if (missing & (SD_BUS_CREDS_PPID |
+ SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_SUID | SD_BUS_CREDS_FSUID |
SD_BUS_CREDS_GID | SD_BUS_CREDS_EGID | SD_BUS_CREDS_SGID | SD_BUS_CREDS_FSGID |
SD_BUS_CREDS_SUPPLEMENTARY_GIDS |
SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_INHERITABLE_CAPS |
FOREACH_LINE(line, f, return -errno) {
truncate_nl(line);
+ if (missing & SD_BUS_CREDS_PPID) {
+ p = startswith(line, "PPid:");
+ if (p) {
+ p += strspn(p, WHITESPACE);
+
+ /* Explicitly check for PPID 0 (which is the case for PID 1) */
+ if (!streq(p, "0")) {
+ r = parse_pid(p, &c->ppid);
+ if (r < 0)
+ return r;
+
+ } else
+ c->ppid = 0;
+
+ c->mask |= SD_BUS_CREDS_PPID;
+ continue;
+ }
+ }
+
if (missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID)) {
p = startswith(line, "Uid:");
if (p) {
if (sscanf(p, "%lu %lu %lu %lu", &uid, &euid, &suid, &fsuid) != 4)
return -EIO;
- c->uid = (uid_t) uid;
- c->euid = (uid_t) euid;
- c->suid = (uid_t) suid;
- c->fsuid = (uid_t) fsuid;
+ if (missing & SD_BUS_CREDS_UID)
+ c->uid = (uid_t) uid;
+ if (missing & SD_BUS_CREDS_EUID)
+ c->euid = (uid_t) euid;
+ if (missing & SD_BUS_CREDS_SUID)
+ c->suid = (uid_t) suid;
+ if (missing & SD_BUS_CREDS_FSUID)
+ c->fsuid = (uid_t) fsuid;
+
c->mask |= missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID);
continue;
}
if (sscanf(p, "%lu %lu %lu %lu", &gid, &egid, &sgid, &fsgid) != 4)
return -EIO;
- c->gid = (gid_t) gid;
- c->egid = (gid_t) egid;
- c->sgid = (gid_t) sgid;
- c->fsgid = (gid_t) fsgid;
+ if (missing & SD_BUS_CREDS_GID)
+ c->gid = (gid_t) gid;
+ if (missing & SD_BUS_CREDS_EGID)
+ c->egid = (gid_t) egid;
+ if (missing & SD_BUS_CREDS_SGID)
+ c->sgid = (gid_t) sgid;
+ if (missing & SD_BUS_CREDS_FSGID)
+ c->fsgid = (gid_t) fsgid;
+
c->mask |= missing & (SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID);
continue;
}
if (missing & SD_BUS_CREDS_EXE) {
r = get_process_exe(pid, &c->exe);
- if (r < 0) {
+ if (r == -ESRCH) {
+ /* Unfortunately we cannot really distinguish
+ * the case here where the process does not
+ * exist, and /proc/$PID/exe being unreadable
+ * because $PID is a kernel thread. Hence,
+ * assume it is a kernel thread, and rely on
+ * that this case is caught with a later
+ * call. */
+ c->exe = NULL;
+ c->mask |= SD_BUS_CREDS_EXE;
+ } else if (r < 0) {
if (r != -EPERM && r != -EACCES)
return r;
} else
p = procfs_file_alloca(pid, "cmdline");
r = read_full_file(p, &c->cmdline, &c->cmdline_size);
+ if (r == -ENOENT)
+ return -ESRCH;
if (r < 0) {
- if (r == -ENOENT)
- return -ESRCH;
if (r != -EPERM && r != -EACCES)
return r;
} else {
if (c->cmdline_size == 0) {
- free(c->cmdline);
- c->cmdline = NULL;
- } else
- c->mask |= SD_BUS_CREDS_CMDLINE;
+ c->cmdline = mfree(c->cmdline);
+ }
+
+ c->mask |= SD_BUS_CREDS_CMDLINE;
}
}
return -ENOMEM;
r = read_one_line_file(p, &c->tid_comm);
+ if (r == -ENOENT)
+ return -ESRCH;
if (r < 0) {
- if (r == -ENOENT)
- return -ESRCH;
if (r != -EPERM && r != -EACCES)
return r;
} else
c->mask |= SD_BUS_CREDS_TID_COMM;
}
- if (missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)) {
+ if (missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)) {
- r = cg_pid_get_path(NULL, pid, &c->cgroup);
- if (r < 0) {
- if (r != -EPERM && r != -EACCES)
- return r;
- } else {
+ if (!c->cgroup) {
+ r = cg_pid_get_path(NULL, pid, &c->cgroup);
+ if (r < 0) {
+ if (r != -EPERM && r != -EACCES)
+ return r;
+ }
+ }
+
+ if (!c->cgroup_root) {
r = cg_get_root_path(&c->cgroup_root);
if (r < 0)
return r;
-
- c->mask |= missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID);
}
+
+ if (c->cgroup)
+ c->mask |= missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID);
}
if (missing & SD_BUS_CREDS_AUDIT_SESSION_ID) {
r = audit_session_from_pid(pid, &c->audit_session_id);
- if (r < 0) {
- if (r != -EOPNOTSUPP && r != -ENXIO && r != -ENOENT && r != -EPERM && r != -EACCES)
+ if (r == -ENODATA) {
+ /* ENODATA means: no audit session id assigned */
+ c->audit_session_id = AUDIT_SESSION_INVALID;
+ c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
+ } else if (r < 0) {
+ if (r != -EOPNOTSUPP && r != -ENOENT && r != -EPERM && r != -EACCES)
return r;
} else
c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
if (missing & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
r = audit_loginuid_from_pid(pid, &c->audit_login_uid);
- if (r < 0) {
- if (r != -EOPNOTSUPP && r != -ENXIO && r != -ENOENT && r != -EPERM && r != -EACCES)
+ if (r == -ENODATA) {
+ /* ENODATA means: no audit login uid assigned */
+ c->audit_login_uid = UID_INVALID;
+ c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
+ } else if (r < 0) {
+ if (r != -EOPNOTSUPP && r != -ENOENT && r != -EPERM && r != -EACCES)
return r;
} else
c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
+ if (missing & SD_BUS_CREDS_TTY) {
+ r = get_ctty(pid, NULL, &c->tty);
+ if (r == -ENXIO) {
+ /* ENXIO means: process has no controlling TTY */
+ c->tty = NULL;
+ c->mask |= SD_BUS_CREDS_TTY;
+ } else if (r < 0) {
+ if (r != -EPERM && r != -EACCES && r != -ENOENT)
+ return r;
+ } else
+ c->mask |= SD_BUS_CREDS_TTY;
+ }
+
+ /* In case only the exe path was to be read we cannot
+ * distinguish the case where the exe path was unreadable
+ * because the process was a kernel thread, or when the
+ * process didn't exist at all. Hence, let's do a final check,
+ * to be sure. */
+ if (!pid_is_alive(pid))
+ return -ESRCH;
+
+ if (tid > 0 && tid != pid && !pid_is_unwaited(tid))
+ return -ESRCH;
+
+ c->augmented = missing & c->mask;
+
return 0;
}
/* Copy the original data over */
+ if (c->mask & mask & SD_BUS_CREDS_PID) {
+ n->pid = c->pid;
+ n->mask |= SD_BUS_CREDS_PID;
+ }
+
+ if (c->mask & mask & SD_BUS_CREDS_TID) {
+ n->tid = c->tid;
+ n->mask |= SD_BUS_CREDS_TID;
+ }
+
+ if (c->mask & mask & SD_BUS_CREDS_PPID) {
+ n->ppid = c->ppid;
+ n->mask |= SD_BUS_CREDS_PPID;
+ }
+
if (c->mask & mask & SD_BUS_CREDS_UID) {
n->uid = c->uid;
n->mask |= SD_BUS_CREDS_UID;
}
if (c->mask & mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
- n->supplementary_gids = newdup(gid_t, c->supplementary_gids, c->n_supplementary_gids);
- if (!n->supplementary_gids)
- return -ENOMEM;
- n->n_supplementary_gids = c->n_supplementary_gids;
- n->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
- }
-
- if (c->mask & mask & SD_BUS_CREDS_PID) {
- n->pid = c->pid;
- n->mask |= SD_BUS_CREDS_PID;
- }
+ if (c->supplementary_gids) {
+ n->supplementary_gids = newdup(gid_t, c->supplementary_gids, c->n_supplementary_gids);
+ if (!n->supplementary_gids)
+ return -ENOMEM;
+ n->n_supplementary_gids = c->n_supplementary_gids;
+ } else {
+ n->supplementary_gids = NULL;
+ n->n_supplementary_gids = 0;
+ }
- if (c->mask & mask & SD_BUS_CREDS_TID) {
- n->tid = c->tid;
- n->mask |= SD_BUS_CREDS_TID;
+ n->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
}
if (c->mask & mask & SD_BUS_CREDS_COMM) {
+ assert(c->comm);
+
n->comm = strdup(c->comm);
if (!n->comm)
return -ENOMEM;
}
if (c->mask & mask & SD_BUS_CREDS_TID_COMM) {
+ assert(c->tid_comm);
+
n->tid_comm = strdup(c->tid_comm);
if (!n->tid_comm)
return -ENOMEM;
}
if (c->mask & mask & SD_BUS_CREDS_EXE) {
- n->exe = strdup(c->exe);
- if (!n->exe)
- return -ENOMEM;
+ if (c->exe) {
+ n->exe = strdup(c->exe);
+ if (!n->exe)
+ return -ENOMEM;
+ } else
+ n->exe = NULL;
n->mask |= SD_BUS_CREDS_EXE;
}
if (c->mask & mask & SD_BUS_CREDS_CMDLINE) {
- n->cmdline = memdup(c->cmdline, c->cmdline_size);
- if (!n->cmdline)
- return -ENOMEM;
+ if (c->cmdline) {
+ n->cmdline = memdup(c->cmdline, c->cmdline_size);
+ if (!n->cmdline)
+ return -ENOMEM;
+
+ n->cmdline_size = c->cmdline_size;
+ } else {
+ n->cmdline = NULL;
+ n->cmdline_size = 0;
+ }
- n->cmdline_size = c->cmdline_size;
n->mask |= SD_BUS_CREDS_CMDLINE;
}
- if (c->mask & mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_OWNER_UID)) {
+ if (c->mask & mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_OWNER_UID)) {
+ assert(c->cgroup);
+
n->cgroup = strdup(c->cgroup);
if (!n->cgroup)
return -ENOMEM;
if (!n->cgroup_root)
return -ENOMEM;
- n->mask |= mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_OWNER_UID);
+ n->mask |= mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_OWNER_UID);
}
if (c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)) {
+ assert(c->capability);
+
n->capability = memdup(c->capability, DIV_ROUND_UP(cap_last_cap(), 32U) * 4 * 4);
if (!n->capability)
return -ENOMEM;
}
if (c->mask & mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
+ assert(c->label);
+
n->label = strdup(c->label);
if (!n->label)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
+ if (c->mask & mask & SD_BUS_CREDS_TTY) {
+ if (c->tty) {
+ n->tty = strdup(c->tty);
+ if (!n->tty)
+ return -ENOMEM;
+ } else
+ n->tty = NULL;
+ n->mask |= SD_BUS_CREDS_TTY;
+ }
+
if (c->mask & mask & SD_BUS_CREDS_UNIQUE_NAME) {
+ assert(c->unique_name);
+
n->unique_name = strdup(c->unique_name);
if (!n->unique_name)
return -ENOMEM;
}
if (c->mask & mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
- n->well_known_names = strv_copy(c->well_known_names);
- if (!n->well_known_names)
- return -ENOMEM;
+ if (strv_isempty(c->well_known_names))
+ n->well_known_names = NULL;
+ else {
+ n->well_known_names = strv_copy(c->well_known_names);
+ if (!n->well_known_names)
+ return -ENOMEM;
+ }
+ n->well_known_names_driver = c->well_known_names_driver;
+ n->well_known_names_local = c->well_known_names_local;
n->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
}
if (c->mask & mask & SD_BUS_CREDS_DESCRIPTION) {
+ assert(c->description);
n->description = strdup(c->description);
if (!n->description)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_DESCRIPTION;
}
+ n->augmented = c->augmented & n->mask;
+
/* Get more data */
- r = bus_creds_add_more(n, mask,
- c->mask & SD_BUS_CREDS_PID ? c->pid : 0,
- c->mask & SD_BUS_CREDS_TID ? c->tid : 0);
+ r = bus_creds_add_more(n, mask, 0, 0);
if (r < 0)
return r;
struct sd_bus_creds {
bool allocated;
unsigned n_ref;
+
uint64_t mask;
+ uint64_t augmented;
uid_t uid;
uid_t euid;
gid_t *supplementary_gids;
unsigned n_supplementary_gids;
+ pid_t ppid;
pid_t pid;
pid_t tid;
char *unit;
char *user_unit;
char *slice;
+ char *user_slice;
+
+ char *tty;
uint32_t *capability;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-#include "capability.h"
-#include "strv.h"
-#include "macro.h"
-#include "cap-list.h"
-
-#include "bus-message.h"
-#include "bus-internal.h"
-#include "bus-type.h"
-#include "bus-dump.h"
-
-static char *indent(unsigned level, unsigned flags) {
- char *p;
- unsigned n, i = 0;
-
- n = 0;
-
- if (flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY && level > 0)
- level -= 1;
-
- if (flags & BUS_MESSAGE_DUMP_WITH_HEADER)
- n += 2;
-
- p = new(char, n + level*8 + 1);
- if (!p)
- return NULL;
-
- if (flags & BUS_MESSAGE_DUMP_WITH_HEADER) {
- p[i++] = ' ';
- p[i++] = ' ';
- }
-
- memset(p + i, ' ', level*8);
- p[i + level*8] = 0;
-
- return p;
-}
-
-int bus_message_dump(sd_bus_message *m, FILE *f, unsigned flags) {
- unsigned level = 1;
- int r;
-
- assert(m);
-
- if (!f)
- f = stdout;
-
- if (flags & BUS_MESSAGE_DUMP_WITH_HEADER) {
- fprintf(f,
- "%s%s%s Type=%s%s%s Endian=%c Flags=%u Version=%u Priority=%"PRIi64,
- m->header->type == SD_BUS_MESSAGE_METHOD_ERROR ? ansi_highlight_red() :
- m->header->type == SD_BUS_MESSAGE_METHOD_RETURN ? ansi_highlight_green() :
- m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "", draw_special_char(DRAW_TRIANGULAR_BULLET), ansi_highlight_off(),
- ansi_highlight(), bus_message_type_to_string(m->header->type), ansi_highlight_off(),
- m->header->endian,
- m->header->flags,
- m->header->version,
- m->priority);
-
- /* Display synthetic message serial number in a more readable
- * format than (uint32_t) -1 */
- if (BUS_MESSAGE_COOKIE(m) == 0xFFFFFFFFULL)
- fprintf(f, " Cookie=-1");
- else
- fprintf(f, " Cookie=%" PRIu64, BUS_MESSAGE_COOKIE(m));
-
- if (m->reply_cookie != 0)
- fprintf(f, " ReplyCookie=%" PRIu64, m->reply_cookie);
-
- fputs("\n", f);
-
- if (m->sender)
- fprintf(f, " Sender=%s%s%s", ansi_highlight(), m->sender, ansi_highlight_off());
- if (m->destination)
- fprintf(f, " Destination=%s%s%s", ansi_highlight(), m->destination, ansi_highlight_off());
- if (m->path)
- fprintf(f, " Path=%s%s%s", ansi_highlight(), m->path, ansi_highlight_off());
- if (m->interface)
- fprintf(f, " Interface=%s%s%s", ansi_highlight(), m->interface, ansi_highlight_off());
- if (m->member)
- fprintf(f, " Member=%s%s%s", ansi_highlight(), m->member, ansi_highlight_off());
-
- if (m->sender || m->destination || m->path || m->interface || m->member)
- fputs("\n", f);
-
- if (sd_bus_error_is_set(&m->error))
- fprintf(f,
- " ErrorName=%s%s%s"
- " ErrorMessage=%s\"%s\"%s\n",
- ansi_highlight_red(), strna(m->error.name), ansi_highlight_off(),
- ansi_highlight_red(), strna(m->error.message), ansi_highlight_off());
-
- if (m->monotonic != 0)
- fprintf(f, " Monotonic="USEC_FMT, m->monotonic);
- if (m->realtime != 0)
- fprintf(f, " Realtime="USEC_FMT, m->realtime);
- if (m->seqnum != 0)
- fprintf(f, " SequenceNumber=%"PRIu64, m->seqnum);
-
- if (m->monotonic != 0 || m->realtime != 0 || m->seqnum != 0)
- fputs("\n", f);
-
- bus_creds_dump(&m->creds, f, true);
- }
-
- r = sd_bus_message_rewind(m, !(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY));
- if (r < 0)
- return log_error_errno(r, "Failed to rewind: %m");
-
- if (!(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY)) {
- _cleanup_free_ char *prefix = NULL;
-
- prefix = indent(0, flags);
- if (!prefix)
- return log_oom();
-
- fprintf(f, "%sMESSAGE \"%s\" {\n", prefix, strempty(m->root_container.signature));
- }
-
- for (;;) {
- _cleanup_free_ char *prefix = NULL;
- const char *contents = NULL;
- char type;
- union {
- uint8_t u8;
- uint16_t u16;
- int16_t s16;
- uint32_t u32;
- int32_t s32;
- uint64_t u64;
- int64_t s64;
- double d64;
- const char *string;
- int i;
- } basic;
-
- r = sd_bus_message_peek_type(m, &type, &contents);
- if (r < 0)
- return log_error_errno(r, "Failed to peek type: %m");
-
- if (r == 0) {
- if (level <= 1)
- break;
-
- r = sd_bus_message_exit_container(m);
- if (r < 0)
- return log_error_errno(r, "Failed to exit container: %m");
-
- level--;
-
- prefix = indent(level, flags);
- if (!prefix)
- return log_oom();
-
- fprintf(f, "%s};\n", prefix);
- continue;
- }
-
- prefix = indent(level, flags);
- if (!prefix)
- return log_oom();
-
- if (bus_type_is_container(type) > 0) {
- r = sd_bus_message_enter_container(m, type, contents);
- if (r < 0)
- return log_error_errno(r, "Failed to enter container: %m");
-
- if (type == SD_BUS_TYPE_ARRAY)
- fprintf(f, "%sARRAY \"%s\" {\n", prefix, contents);
- else if (type == SD_BUS_TYPE_VARIANT)
- fprintf(f, "%sVARIANT \"%s\" {\n", prefix, contents);
- else if (type == SD_BUS_TYPE_STRUCT)
- fprintf(f, "%sSTRUCT \"%s\" {\n", prefix, contents);
- else if (type == SD_BUS_TYPE_DICT_ENTRY)
- fprintf(f, "%sDICT_ENTRY \"%s\" {\n", prefix, contents);
-
- level ++;
-
- continue;
- }
-
- r = sd_bus_message_read_basic(m, type, &basic);
- if (r < 0)
- return log_error_errno(r, "Failed to get basic: %m");
-
- assert(r > 0);
-
- switch (type) {
-
- case SD_BUS_TYPE_BYTE:
- fprintf(f, "%sBYTE %s%u%s;\n", prefix, ansi_highlight(), basic.u8, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_BOOLEAN:
- fprintf(f, "%sBOOLEAN %s%s%s;\n", prefix, ansi_highlight(), true_false(basic.i), ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_INT16:
- fprintf(f, "%sINT16 %s%i%s;\n", prefix, ansi_highlight(), basic.s16, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_UINT16:
- fprintf(f, "%sUINT16 %s%u%s;\n", prefix, ansi_highlight(), basic.u16, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_INT32:
- fprintf(f, "%sINT32 %s%i%s;\n", prefix, ansi_highlight(), basic.s32, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_UINT32:
- fprintf(f, "%sUINT32 %s%u%s;\n", prefix, ansi_highlight(), basic.u32, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_INT64:
- fprintf(f, "%sINT64 %s%"PRIi64"%s;\n", prefix, ansi_highlight(), basic.s64, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_UINT64:
- fprintf(f, "%sUINT64 %s%"PRIu64"%s;\n", prefix, ansi_highlight(), basic.u64, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_DOUBLE:
- fprintf(f, "%sDOUBLE %s%g%s;\n", prefix, ansi_highlight(), basic.d64, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_STRING:
- fprintf(f, "%sSTRING \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_OBJECT_PATH:
- fprintf(f, "%sOBJECT_PATH \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_SIGNATURE:
- fprintf(f, "%sSIGNATURE \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
- break;
-
- case SD_BUS_TYPE_UNIX_FD:
- fprintf(f, "%sUNIX_FD %s%i%s;\n", prefix, ansi_highlight(), basic.i, ansi_highlight_off());
- break;
-
- default:
- assert_not_reached("Unknown basic type.");
- }
- }
-
- if (!(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY)) {
- _cleanup_free_ char *prefix = NULL;
-
- prefix = indent(0, flags);
- if (!prefix)
- return log_oom();
-
- fprintf(f, "%s};\n\n", prefix);
- }
-
- return 0;
-}
-
-static void dump_capabilities(
- sd_bus_creds *c,
- FILE *f,
- const char *name,
- bool terse,
- int (*has)(sd_bus_creds *c, int capability)) {
-
- unsigned long i, last_cap;
- unsigned n = 0;
- int r;
-
- assert(c);
- assert(f);
- assert(name);
- assert(has);
-
- i = 0;
- r = has(c, i);
- if (r < 0)
- return;
-
- fprintf(f, "%s%s=%s", terse ? " " : "", name, terse ? "" : ansi_highlight());
- last_cap = cap_last_cap();
-
- for (;;) {
- if (r > 0) {
-
- if (n > 0)
- fputc(' ', f);
- if (n % 4 == 3)
- fprintf(f, terse ? "\n " : "\n ");
-
- fprintf(f, "%s", strna(capability_to_name(i)));
- n++;
- }
-
- i++;
-
- if (i > last_cap)
- break;
-
- r = has(c, i);
- }
-
- fputs("\n", f);
-
- if (!terse)
- fputs(ansi_highlight_off(), f);
-}
-
-int bus_creds_dump(sd_bus_creds *c, FILE *f, bool terse) {
- bool audit_sessionid_is_set = false, audit_loginuid_is_set = false;
- const char *u = NULL, *uu = NULL, *s = NULL, *sl = NULL;
- uid_t owner, audit_loginuid;
- uint32_t audit_sessionid;
- char **cmdline = NULL, **well_known = NULL;
- const char *prefix, *color, *suffix;
- int r;
-
- assert(c);
-
- if (!f)
- f = stdout;
-
- if (terse) {
- prefix = " ";
- suffix = "";
- color = "";
- } else {
- const char *off;
-
- prefix = "";
- color = ansi_highlight();
-
- off = ansi_highlight_off();
- suffix = strjoina(off, "\n");
- }
-
- if (c->mask & SD_BUS_CREDS_PID)
- fprintf(f, "%sPID=%s"PID_FMT"%s", prefix, color, c->pid, suffix);
- if (c->mask & SD_BUS_CREDS_TID)
- fprintf(f, "%sTID=%s"PID_FMT"%s", prefix, color, c->tid, suffix);
-
- if (terse && ((c->mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_TID))))
- fputs("\n", f);
-
- if (c->mask & SD_BUS_CREDS_UID)
- fprintf(f, "%sUID=%s"UID_FMT"%s", prefix, color, c->uid, suffix);
- if (c->mask & SD_BUS_CREDS_EUID)
- fprintf(f, "%sEUID=%s"UID_FMT"%s", prefix, color, c->euid, suffix);
- if (c->mask & SD_BUS_CREDS_SUID)
- fprintf(f, "%sSUID=%s"UID_FMT"%s", prefix, color, c->suid, suffix);
- if (c->mask & SD_BUS_CREDS_FSUID)
- fprintf(f, "%sFSUID=%s"UID_FMT"%s", prefix, color, c->fsuid, suffix);
- r = sd_bus_creds_get_owner_uid(c, &owner);
- if (r >= 0)
- fprintf(f, "%sOwnerUID=%s"UID_FMT"%s", prefix, color, owner, suffix);
- if (c->mask & SD_BUS_CREDS_GID)
- fprintf(f, "%sGID=%s"GID_FMT"%s", prefix, color, c->gid, suffix);
- if (c->mask & SD_BUS_CREDS_EGID)
- fprintf(f, "%sEGID=%s"GID_FMT"%s", prefix, color, c->egid, suffix);
- if (c->mask & SD_BUS_CREDS_SGID)
- fprintf(f, "%sSGID=%s"GID_FMT"%s", prefix, color, c->sgid, suffix);
- if (c->mask & SD_BUS_CREDS_FSGID)
- fprintf(f, "%sFSGID=%s"GID_FMT"%s", prefix, color, c->fsgid, suffix);
-
- if (c->mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
- unsigned i;
-
- fprintf(f, "%sSupplementaryGIDs=%s", prefix, color);
- for (i = 0; i < c->n_supplementary_gids; i++)
- fprintf(f, "%s" GID_FMT, i > 0 ? " " : "", c->supplementary_gids[i]);
- fprintf(f, "%s", suffix);
- }
-
- if (terse && ((c->mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
- SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
- SD_BUS_CREDS_SUPPLEMENTARY_GIDS)) || r >= 0))
- fputs("\n", f);
-
- if (c->mask & SD_BUS_CREDS_COMM)
- fprintf(f, "%sComm=%s%s%s", prefix, color, c->comm, suffix);
- if (c->mask & SD_BUS_CREDS_TID_COMM)
- fprintf(f, "%sTIDComm=%s%s%s", prefix, color, c->tid_comm, suffix);
- if (c->mask & SD_BUS_CREDS_EXE)
- fprintf(f, "%sExe=%s%s%s", prefix, color, c->exe, suffix);
-
- if (terse && (c->mask & (SD_BUS_CREDS_EXE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM)))
- fputs("\n", f);
-
- if (sd_bus_creds_get_cmdline(c, &cmdline) >= 0) {
- char **i;
-
- fprintf(f, "%sCommandLine=%s", prefix, color);
- STRV_FOREACH(i, cmdline) {
- if (i != cmdline)
- fputc(' ', f);
-
- fputs(*i, f);
- }
-
- fprintf(f, "%s", suffix);
- }
-
- if (c->mask & SD_BUS_CREDS_SELINUX_CONTEXT)
- fprintf(f, "%sLabel=%s%s%s", prefix, color, c->label, suffix);
- if (c->mask & SD_BUS_CREDS_DESCRIPTION)
- fprintf(f, "%sDescription=%s%s%s", prefix, color, c->description, suffix);
-
- if (terse && (c->mask & (SD_BUS_CREDS_SELINUX_CONTEXT|SD_BUS_CREDS_DESCRIPTION)))
- fputs("\n", f);
-
- if (c->mask & SD_BUS_CREDS_CGROUP)
- fprintf(f, "%sCGroup=%s%s%s", prefix, color, c->cgroup, suffix);
- (void) sd_bus_creds_get_unit(c, &u);
- if (u)
- fprintf(f, "%sUnit=%s%s%s", prefix, color, u, suffix);
- (void) sd_bus_creds_get_user_unit(c, &uu);
- if (uu)
- fprintf(f, "%sUserUnit=%s%s%s", prefix, color, uu, suffix);
- (void) sd_bus_creds_get_slice(c, &sl);
- if (sl)
- fprintf(f, "%sSlice=%s%s%s", prefix, color, sl, suffix);
- (void) sd_bus_creds_get_session(c, &s);
- if (s)
- fprintf(f, "%sSession=%s%s%s", prefix, color, s, suffix);
-
- if (terse && ((c->mask & SD_BUS_CREDS_CGROUP) || u || uu || sl || s))
- fputs("\n", f);
-
- if (sd_bus_creds_get_audit_login_uid(c, &audit_loginuid) >= 0) {
- audit_loginuid_is_set = true;
- fprintf(f, "%sAuditLoginUID=%s"UID_FMT"%s", prefix, color, audit_loginuid, suffix);
- }
- if (sd_bus_creds_get_audit_session_id(c, &audit_sessionid) >= 0) {
- audit_sessionid_is_set = true;
- fprintf(f, "%sAuditSessionID=%s%"PRIu32"%s", prefix, color, audit_sessionid, suffix);
- }
-
- if (terse && (audit_loginuid_is_set || audit_sessionid_is_set))
- fputs("\n", f);
-
- if (c->mask & SD_BUS_CREDS_UNIQUE_NAME)
- fprintf(f, "%sUniqueName=%s%s%s", prefix, color, c->unique_name, suffix);
-
- if (sd_bus_creds_get_well_known_names(c, &well_known) >= 0) {
- char **i;
-
- fprintf(f, "%sWellKnownNames=%s", prefix, color);
- STRV_FOREACH(i, well_known) {
- if (i != well_known)
- fputc(' ', f);
-
- fputs(*i, f);
- }
-
- fprintf(f, "%s", suffix);
- }
-
- if (terse && (c->mask & SD_BUS_CREDS_UNIQUE_NAME || well_known))
- fputc('\n', f);
-
- dump_capabilities(c, f, "EffectiveCapabilities", terse, sd_bus_creds_has_effective_cap);
- dump_capabilities(c, f, "PermittedCapabilities", terse, sd_bus_creds_has_permitted_cap);
- dump_capabilities(c, f, "InheritableCapabilities", terse, sd_bus_creds_has_inheritable_cap);
- dump_capabilities(c, f, "BoundingCapabilities", terse, sd_bus_creds_has_bounding_cap);
-
- return 0;
-}
-
-/*
- * For details about the file format, see:
- *
- * http://wiki.wireshark.org/Development/LibpcapFileFormat
- */
-
-typedef struct _packed_ pcap_hdr_s {
- uint32_t magic_number; /* magic number */
- uint16_t version_major; /* major version number */
- uint16_t version_minor; /* minor version number */
- int32_t thiszone; /* GMT to local correction */
- uint32_t sigfigs; /* accuracy of timestamps */
- uint32_t snaplen; /* max length of captured packets, in octets */
- uint32_t network; /* data link type */
-} pcap_hdr_t ;
-
-typedef struct _packed_ pcaprec_hdr_s {
- uint32_t ts_sec; /* timestamp seconds */
- uint32_t ts_usec; /* timestamp microseconds */
- uint32_t incl_len; /* number of octets of packet saved in file */
- uint32_t orig_len; /* actual length of packet */
-} pcaprec_hdr_t;
-
-int bus_pcap_header(size_t snaplen, FILE *f) {
-
- pcap_hdr_t hdr = {
- .magic_number = 0xa1b2c3d4U,
- .version_major = 2,
- .version_minor = 4,
- .thiszone = 0, /* UTC */
- .sigfigs = 0,
- .network = 231, /* D-Bus */
- };
-
- if (!f)
- f = stdout;
-
- assert(snaplen > 0);
- assert((size_t) (uint32_t) snaplen == snaplen);
-
- hdr.snaplen = (uint32_t) snaplen;
-
- fwrite(&hdr, 1, sizeof(hdr), f);
- fflush(f);
-
- return 0;
-}
-
-int bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f) {
- struct bus_body_part *part;
- pcaprec_hdr_t hdr = {};
- struct timeval tv;
- unsigned i;
- size_t w;
-
- if (!f)
- f = stdout;
-
- assert(m);
- assert(snaplen > 0);
- assert((size_t) (uint32_t) snaplen == snaplen);
-
- if (m->realtime != 0)
- timeval_store(&tv, m->realtime);
- else
- assert_se(gettimeofday(&tv, NULL) >= 0);
-
- hdr.ts_sec = tv.tv_sec;
- hdr.ts_usec = tv.tv_usec;
- hdr.orig_len = BUS_MESSAGE_SIZE(m);
- hdr.incl_len = MIN(hdr.orig_len, snaplen);
-
- /* write the pcap header */
- fwrite(&hdr, 1, sizeof(hdr), f);
-
- /* write the dbus header */
- w = MIN(BUS_MESSAGE_BODY_BEGIN(m), snaplen);
- fwrite(m->header, 1, w, f);
- snaplen -= w;
-
- /* write the dbus body */
- MESSAGE_FOREACH_PART(part, i, m) {
- if (snaplen <= 0)
- break;
-
- w = MIN(part->size, snaplen);
- fwrite(part->data, 1, w, f);
- snaplen -= w;
- }
-
- fflush(f);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdio.h>
-#include <stdbool.h>
-
-#include "sd-bus.h"
-
-enum {
- BUS_MESSAGE_DUMP_WITH_HEADER = 1,
- BUS_MESSAGE_DUMP_SUBTREE_ONLY = 2,
-};
-
-int bus_message_dump(sd_bus_message *m, FILE *f, unsigned flags);
-
-int bus_creds_dump(sd_bus_creds *c, FILE *f, bool terse);
-
-int bus_pcap_header(size_t snaplen, FILE *f);
-int bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f);
SD_BUS_ERROR_MAP_END
};
-/* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */
-extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
-extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
+/* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section.
+ * Hide them; for currently unknown reasons they get exported to the shared libries
+ * even without being listed in the sym file. */
+extern const sd_bus_error_map __start_BUS_ERROR_MAP[] _hidden_;
+extern const sd_bus_error_map __stop_BUS_ERROR_MAP[] _hidden_;
/* Additional maps registered with sd_bus_error_add_map() are in this
* NULL terminated array */
case SD_BUS_TYPE_STRUCT_BEGIN:
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
- char t[n-1];
-
- memcpy(t, p + 1, n - 2);
- t[n - 2] = 0;
-
- r = bus_gvariant_get_size(t);
- if (r < 0)
- return r;
+ if (n == 2) {
+ /* unary type () has fixed size of 1 */
+ r = 1;
+ } else {
+ char t[n-1];
+
+ memcpy(t, p + 1, n - 2);
+ t[n - 2] = 0;
+
+ r = bus_gvariant_get_size(t);
+ if (r < 0)
+ return r;
+ }
sum += r;
break;
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "bus-message.h"
#include "bus-internal.h"
bool object_path_is_valid(const char *p) {
return true;
}
+/// UNNEEDED by elogind
+#if 0
char* service_name_startswith(const char *a, const char *b) {
const char *p;
return NULL;
}
+#endif // 0
bool member_name_is_valid(const char *p) {
const char *q;
return true;
}
+/*
+ * Complex pattern match
+ * This checks whether @a is a 'complex-prefix' of @b, or @b is a
+ * 'complex-prefix' of @a, based on strings that consist of labels with @c as
+ * spearator. This function returns true if:
+ * - both strings are equal
+ * - either is a prefix of the other and ends with @c
+ * The second rule makes sure that either string needs to be fully included in
+ * the other, and the string which is considered the prefix needs to end with a
+ * separator.
+ */
static bool complex_pattern_check(char c, const char *a, const char *b) {
bool separator = false;
for (;;) {
if (*a != *b)
- return (separator && (*a == 0 || *b == 0)) ||
- (*a == 0 && *b == c && b[1] == 0) ||
- (*b == 0 && *a == c && a[1] == 0);
+ return (separator && (*a == 0 || *b == 0));
if (*a == 0)
return true;
return complex_pattern_check('/', pattern, value);
}
+/*
+ * Simple pattern match
+ * This checks whether @a is a 'simple-prefix' of @b, based on strings that
+ * consist of labels with @c as separator. This function returns true, if:
+ * - if @a and @b are equal
+ * - if @a is a prefix of @b, and the first following character in @b (or the
+ * last character in @a) is @c
+ * The second rule basically makes sure that if @a is a prefix of @b, then @b
+ * must follow with a new label separated by @c. It cannot extend the label.
+ */
static bool simple_pattern_check(char c, const char *a, const char *b) {
+ bool separator = false;
if (!a && !b)
return true;
for (;;) {
if (*a != *b)
- return *a == 0 && *b == c;
+ return *a == 0 && (*b == c || separator);
if (*a == 0)
return true;
+ separator = *a == c;
+
a++, b++;
}
}
*b = 0;
return r;
}
+
+int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error) {
+ assert(m);
+
+ if (r < 0) {
+ if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
+ sd_bus_reply_method_errno(m, r, error);
+
+ } else if (sd_bus_error_is_set(error)) {
+ if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
+ sd_bus_reply_method_error(m, error);
+ } else
+ return r;
+
+ log_debug("Failed to process message [type=%s sender=%s path=%s interface=%s member=%s signature=%s]: %s",
+ bus_message_type_to_string(m->header->type),
+ strna(m->sender),
+ strna(m->path),
+ strna(m->interface),
+ strna(m->member),
+ strna(m->root_container.signature),
+ bus_error_message(error, r));
+
+ return 1;
+}
void *userdata;
BusSlotType type:5;
bool floating:1;
+ bool match_added:1;
char *description;
LIST_FIELDS(sd_bus_slot, slots);
usec_t auth_timeout;
struct ucred ucred;
- char label[NAME_MAX];
+ char *label;
uint64_t creds_mask;
bool interface_name_is_valid(const char *p) _pure_;
bool service_name_is_valid(const char *p) _pure_;
-char* service_name_startswith(const char *a, const char *b);
+// UNNEEDED char* service_name_startswith(const char *a, const char *b);
bool member_name_is_valid(const char *p) _pure_;
bool object_path_is_valid(const char *p) _pure_;
char *object_path_startswith(const char *a, const char *b) _pure_;
_cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)
int bus_set_address_system(sd_bus *bus);
-int bus_set_address_user(sd_bus *bus);
+// UNNEEDED int bus_set_address_user(sd_bus *bus);
int bus_set_address_system_remote(sd_bus *b, const char *host);
int bus_set_address_system_machine(sd_bus *b, const char *machine);
-int bus_remove_match_by_string(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata);
+// UNNEEDED int bus_remove_match_by_string(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata);
int bus_get_root_path(sd_bus *bus);
+
+int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error);
+
+#define bus_assert_return(expr, r, error) \
+ do { \
+ if (!assert_log(expr)) \
+ return sd_bus_error_set_errno(error, r); \
+ } while (false)
fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i->f);
if (type == _SD_BUS_VTABLE_PROPERTY || type == _SD_BUS_VTABLE_WRITABLE_PROPERTY) {
+ if (flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
+ fputs(" <annotation name=\"org.freedesktop.login1.Explicit\" value=\"true\"/>\n", i->f);
+
if (flags & SD_BUS_VTABLE_PROPERTY_CONST)
fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i->f);
else if (flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)
if (!i->trusted &&
(type == _SD_BUS_VTABLE_METHOD || type == _SD_BUS_VTABLE_WRITABLE_PROPERTY) &&
!(flags & SD_BUS_VTABLE_UNPRIVILEGED))
- fputs(" <annotation name=\"org.freedesktop.systemd1.Privileged\" value=\"true\"/>\n", i->f);
+ fputs(" <annotation name=\"org.freedesktop.login1.Privileged\" value=\"true\"/>\n", i->f);
}
static int introspect_write_arguments(struct introspect *i, const char *signature, const char *direction) {
assert(reply);
fputs("</node>\n", i->f);
- fflush(i->f);
- if (ferror(i->f))
- return -ENOMEM;
+ r = fflush_and_check(i->f);
+ if (r < 0)
+ return r;
r = sd_bus_message_new_method_return(m, &q);
if (r < 0)
if (i->f)
fclose(i->f);
- if (i->introspection)
- free(i->introspection);
-
+ free(i->introspection);
zero(*i);
}
#include <sys/prctl.h>
/* When we include libgen.h because we need dirname() we immediately
- * undefine basename() since libgen.h defines it as a macro to the XDG
- * version which is really broken. */
+ * undefine basename() since libgen.h defines it as a macro to the POSIX
+ * version which is really broken. We prefer GNU basename(). */
#include <libgen.h>
#undef basename
#include "memfd-util.h"
#include "capability.h"
#include "fileio.h"
+#include "formats-util.h"
#include "bus-internal.h"
#include "bus-message.h"
bloom_add_prefixes(data, size, n_hash, buf, t, '/');
}
+static void add_bloom_arg_has(void *data, size_t size, unsigned n_hash, unsigned i, const char *t) {
+ char buf[sizeof("arg")-1 + 2 + sizeof("-has")];
+ char *e;
+
+ assert(data);
+ assert(size > 0);
+ assert(i < 64);
+ assert(t);
+
+ e = stpcpy(buf, "arg");
+ if (i < 10)
+ *(e++) = '0' + (char) i;
+ else {
+ *(e++) = '0' + (char) (i / 10);
+ *(e++) = '0' + (char) (i % 10);
+ }
+
+ strcpy(e, "-has");
+ bloom_add_pair(data, size, n_hash, buf, t);
+}
+
static int bus_message_setup_bloom(sd_bus_message *m, struct kdbus_bloom_filter *bloom) {
void *data;
unsigned i;
return r;
add_bloom_arg(data, m->bus->bloom_size, m->bus->bloom_n_hash, i, t);
- } if (type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g")) {
+ }
+
+ if (type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g")) {
/* As well as array of simple strings of any kinds */
r = sd_bus_message_enter_container(m, type, contents);
return r;
while ((r = sd_bus_message_read_basic(m, contents[0], &t)) > 0)
- add_bloom_arg(data, m->bus->bloom_size, m->bus->bloom_n_hash, i, t);
+ add_bloom_arg_has(data, m->bus->bloom_size, m->bus->bloom_n_hash, i, t);
if (r < 0)
return r;
footer, footer_size,
n_bytes,
fds, n_fds,
- NULL,
seclabel, 0, &m);
if (r < 0)
return r;
m->creds.mask |= SD_BUS_CREDS_TID & bus->creds_mask;
}
+ if (d->pids.ppid > 0) {
+ m->creds.ppid = (pid_t) d->pids.ppid;
+ m->creds.mask |= SD_BUS_CREDS_PPID & bus->creds_mask;
+ } else if (d->pids.pid == 1) {
+ m->creds.ppid = 0;
+ m->creds.mask |= SD_BUS_CREDS_PPID & bus->creds_mask;
+ }
+
break;
case KDBUS_ITEM_CREDS:
break;
case KDBUS_ITEM_AUDIT:
- if ((uint32_t) d->audit.sessionid != (uint32_t) -1) {
- m->creds.audit_session_id = (uint32_t) d->audit.sessionid;
- m->creds.mask |= SD_BUS_CREDS_AUDIT_SESSION_ID & bus->creds_mask;
- }
+ m->creds.audit_session_id = (uint32_t) d->audit.sessionid;
+ m->creds.mask |= SD_BUS_CREDS_AUDIT_SESSION_ID & bus->creds_mask;
- if ((uid_t) d->audit.loginuid != UID_INVALID) {
- m->creds.audit_login_uid = (uid_t) d->audit.loginuid;
- m->creds.mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID & bus->creds_mask;
- }
+ m->creds.audit_login_uid = (uid_t) d->audit.loginuid;
+ m->creds.mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID & bus->creds_mask;
break;
case KDBUS_ITEM_CAPS:
case KDBUS_ITEM_FDS:
case KDBUS_ITEM_SECLABEL:
+ case KDBUS_ITEM_BLOOM_FILTER:
break;
default:
}
r = ioctl(b->input_fd, KDBUS_CMD_HELLO, hello);
- if (r < 0)
+ if (r < 0) {
+ if (errno == ENOTTY)
+ /* If the ioctl is not supported we assume that the
+ * API version changed in a major incompatible way,
+ * let's indicate an API incompatibility in this
+ * case. */
+ return -ESOCKTNOSUPPORT;
+
return -errno;
+ }
if (!b->kdbus_buffer) {
b->kdbus_buffer = mmap(NULL, KDBUS_POOL_SIZE, PROT_READ, MAP_SHARED, b->input_fd, 0);
/* The higher 32bit of the bus_flags fields are considered
* 'incompatible flags'. Refuse them all for now. */
if (hello->bus_flags > 0xFFFFFFFFULL) {
- r = -EOPNOTSUPP;
+ r = -ESOCKTNOSUPPORT;
goto fail;
}
KDBUS_ITEM_FOREACH(d, k, items) {
if (d->type == KDBUS_ITEM_TIMESTAMP)
ts = &d->timestamp;
-
- if (d->type >= _KDBUS_ITEM_KERNEL_BASE && d->type < _KDBUS_ITEM_KERNEL_BASE + ELEMENTSOF(translate)) {
+ else if (d->type >= _KDBUS_ITEM_KERNEL_BASE && d->type < _KDBUS_ITEM_KERNEL_BASE + ELEMENTSOF(translate)) {
if (found)
return -EBADMSG;
found = d;
r = 0;
}
- } else if (k->payload_type == KDBUS_PAYLOAD_KERNEL)
+ if (r <= 0)
+ close_kdbus_msg(bus, k);
+ } else if (k->payload_type == KDBUS_PAYLOAD_KERNEL) {
r = bus_kernel_translate_message(bus, k);
- else {
+ close_kdbus_msg(bus, k);
+ } else {
log_debug("Ignoring message with unknown payload type %llu.", (unsigned long long) k->payload_type);
r = 0;
- }
-
- if (r <= 0)
close_kdbus_msg(bus, k);
+ }
return r < 0 ? r : 1;
}
SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID))
m |= KDBUS_ATTACH_CREDS;
- if (mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_TID))
+ if (mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_TID|SD_BUS_CREDS_PPID))
m |= KDBUS_ATTACH_PIDS;
if (mask & SD_BUS_CREDS_COMM)
return m;
}
+/// UNNEEDED by elogind
+#if 0
int bus_kernel_create_bus(const char *name, bool world, char **s) {
struct kdbus_cmd *make;
struct kdbus_item *n;
make = alloca0_align(offsetof(struct kdbus_cmd, items) +
ALIGN8(offsetof(struct kdbus_item, bloom_parameter) + sizeof(struct kdbus_bloom_parameter)) +
ALIGN8(offsetof(struct kdbus_item, data64) + sizeof(uint64_t)) +
- ALIGN8(offsetof(struct kdbus_item, data64) + sizeof(uint64_t)) +
ALIGN8(offsetof(struct kdbus_item, str) + DECIMAL_STR_MAX(uid_t) + 1 + l + 1),
8);
make->size += ALIGN8(n->size);
- /* The busses we create make no restrictions on what metadata
- * peers can read from incoming messages. */
- n = KDBUS_ITEM_NEXT(n);
- n->type = KDBUS_ITEM_ATTACH_FLAGS_RECV;
- n->size = offsetof(struct kdbus_item, data64) + sizeof(uint64_t);
- n->data64[0] = _KDBUS_ATTACH_ANY;
- make->size += ALIGN8(n->size);
-
/* Provide all metadata via bus-owner queries */
n = KDBUS_ITEM_NEXT(n);
n->type = KDBUS_ITEM_ATTACH_FLAGS_SEND;
if (ioctl(fd, KDBUS_CMD_BUS_MAKE, make) < 0) {
safe_close(fd);
+
+ /* Major API change? then the ioctls got shuffled around. */
+ if (errno == ENOTTY)
+ return -ESOCKTNOSUPPORT;
+
return -errno;
}
return fd;
}
+#endif // 0
int bus_kernel_open_bus_fd(const char *bus, char **path) {
char *p;
return fd;
}
+/// UNNEEDED by elogind
+#if 0
int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char **ep_path) {
_cleanup_free_ char *path = NULL;
struct kdbus_cmd *make;
return fd;
}
+#endif // 0
int bus_kernel_try_close(sd_bus *bus) {
struct kdbus_cmd byebye = { .size = sizeof(byebye) };
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int bus_kernel_drop_one(int fd) {
struct kdbus_cmd_recv recv = {
.size = sizeof(recv),
return 0;
}
+#endif // 0
int bus_kernel_realize_attach_flags(sd_bus *bus) {
struct kdbus_cmd *update;
return 0;
}
-int bus_kernel_fix_attach_mask(void) {
- _cleanup_free_ char *mask = NULL;
- uint64_t m = (uint64_t) -1;
- char buf[2+16+2];
- int r;
-
- /* By default we don't want any kdbus metadata fields to be
- * suppressed, hence we reset the kernel mask for it to
- * (uint64_t) -1. If the module argument was overwritten by
- * the kernel cmdline, we leave it as is. */
-
- r = get_proc_cmdline_key("kdbus.attach_flags_mask=", &mask);
- if (r < 0)
- return log_warning_errno(r, "Failed to read kernel command line: %m");
-
- if (r == 0) {
- sprintf(buf, "0x%" PRIx64 "\n", m);
- r = write_string_file("/sys/module/kdbus/parameters/attach_flags_mask", buf);
- if (r < 0)
- return log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
- "Failed to write kdbus attach mask: %m");
- }
-
- return 0;
-}
-
int bus_kernel_get_bus_name(sd_bus *bus, char **name) {
struct kdbus_cmd_info cmd = {
.size = sizeof(struct kdbus_cmd_info),
int bus_kernel_open_bus_fd(const char *bus, char **path);
-int bus_kernel_create_bus(const char *name, bool world, char **s);
-int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char **path);
+// UNNEEDED int bus_kernel_create_bus(const char *name, bool world, char **s);
+// UNNEEDED int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char **path);
int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *mapped, size_t *allocated);
void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t mapped, size_t allocated);
int bus_kernel_try_close(sd_bus *bus);
-int bus_kernel_drop_one(int fd);
+// UNNEEDED int bus_kernel_drop_one(int fd);
int bus_kernel_realize_attach_flags(sd_bus *bus);
-int bus_kernel_fix_attach_mask(void);
-
int bus_kernel_get_bus_name(sd_bus *bus, char **name);
int bus_kernel_cmd_free(sd_bus *bus, uint64_t offset);
*/
static inline bool BUS_MATCH_IS_COMPARE(enum bus_match_node_type t) {
- return t >= BUS_MATCH_SENDER && t <= BUS_MATCH_ARG_NAMESPACE_LAST;
+ return t >= BUS_MATCH_SENDER && t <= BUS_MATCH_ARG_HAS_LAST;
}
static inline bool BUS_MATCH_CAN_HASH(enum bus_match_node_type t) {
return (t >= BUS_MATCH_MESSAGE_TYPE && t <= BUS_MATCH_PATH) ||
- (t >= BUS_MATCH_ARG && t <= BUS_MATCH_ARG_LAST);
+ (t >= BUS_MATCH_ARG && t <= BUS_MATCH_ARG_LAST) ||
+ (t >= BUS_MATCH_ARG_HAS && t <= BUS_MATCH_ARG_HAS_LAST);
}
static void bus_match_node_free(struct bus_match_node *node) {
case BUS_MATCH_INTERFACE:
case BUS_MATCH_MEMBER:
case BUS_MATCH_PATH:
- case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST: {
- char **i;
+ case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST:
if (value_str)
return streq_ptr(node->value.str, value_str);
+ return false;
+
+ case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST: {
+ char **i;
+
STRV_FOREACH(i, value_strv)
if (streq_ptr(node->value.str, *i))
return true;
return false;
}
- case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST: {
- char **i;
-
+ case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST:
if (value_str)
return namespace_simple_pattern(node->value.str, value_str);
- STRV_FOREACH(i, value_strv)
- if (namespace_simple_pattern(node->value.str, *i))
- return true;
return false;
- }
case BUS_MATCH_PATH_NAMESPACE:
return path_simple_pattern(node->value.str, value_str);
- case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST: {
- char **i;
-
+ case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST:
if (value_str)
return path_complex_pattern(node->value.str, value_str);
- STRV_FOREACH(i, value_strv)
- if (path_complex_pattern(node->value.str, *i))
- return true;
-
return false;
- }
default:
assert_not_reached("Invalid node type");
case BUS_MATCH_MEMBER:
case BUS_MATCH_PATH:
case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST:
+ case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST:
case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST:
case BUS_MATCH_PATH_NAMESPACE:
case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST:
bus->current_handler = node->leaf.callback->callback;
bus->current_userdata = slot->userdata;
}
- r = node->leaf.callback->callback(bus, m, slot->userdata, &error_buffer);
+ r = node->leaf.callback->callback(m, slot->userdata, &error_buffer);
if (bus) {
bus->current_userdata = NULL;
bus->current_handler = NULL;
break;
case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST:
- (void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG, &test_str, &test_strv);
+ (void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG, &test_str);
break;
case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST:
- (void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG_PATH, &test_str, &test_strv);
+ (void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG_PATH, &test_str);
break;
case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST:
- (void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG_NAMESPACE, &test_str, &test_strv);
+ (void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG_NAMESPACE, &test_str);
+ break;
+
+ case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST:
+ (void) bus_message_get_arg_strv(m, node->type - BUS_MATCH_ARG_HAS, &test_strv);
break;
default:
return t;
}
+ if (n == 7 && startswith(k, "arg") && startswith(k + 4, "has")) {
+ int j;
+
+ j = undecchar(k[3]);
+ if (j < 0)
+ return -EINVAL;
+
+ return BUS_MATCH_ARG_HAS + j;
+ }
+
+ if (n == 8 && startswith(k, "arg") && startswith(k + 5, "has")) {
+ enum bus_match_node_type t;
+ int a, b;
+
+ a = undecchar(k[3]);
+ b = undecchar(k[4]);
+ if (a <= 0 || b < 0)
+ return -EINVAL;
+
+ t = BUS_MATCH_ARG_HAS + a * 10 + b;
+ if (t > BUS_MATCH_ARG_HAS_LAST)
+ return -EINVAL;
+
+ return t;
+ }
+
return -EINVAL;
}
if (r < 0)
goto fail;
- free(value);
- value = NULL;
+ value = mfree(value);
} else
u = 0;
return r;
}
+/// UNNEEDED by elogind
+#if 0
char *bus_match_to_string(struct bus_match_component *components, unsigned n_components) {
- _cleanup_free_ FILE *f = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
char *buffer = NULL;
size_t size = 0;
unsigned i;
+ int r;
if (n_components <= 0)
return strdup("");
fputc('\'', f);
}
- fflush(f);
- if (ferror(f))
+ r = fflush_and_check(f);
+ if (r < 0)
return NULL;
return buffer;
}
+#endif // 0
int bus_match_add(
struct bus_match_node *root,
snprintf(buf, l, "arg%inamespace", t - BUS_MATCH_ARG_NAMESPACE);
return buf;
+ case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST:
+ snprintf(buf, l, "arg%ihas", t - BUS_MATCH_ARG_HAS);
+ return buf;
+
default:
return NULL;
}
for (c = node->child; c; c = c->next)
bus_match_dump(c, level + 1);
}
+
+enum bus_match_scope bus_match_get_scope(const struct bus_match_component *components, unsigned n_components) {
+ bool found_driver = false;
+ unsigned i;
+
+ if (n_components <= 0)
+ return BUS_MATCH_GENERIC;
+
+ assert(components);
+
+ /* Checks whether the specified match can only match the
+ * pseudo-service for local messages, which we detect by
+ * sender, interface or path. If a match is not restricted to
+ * local messages, then we check if it only matches on the
+ * driver. */
+
+ for (i = 0; i < n_components; i++) {
+ const struct bus_match_component *c = components + i;
+
+ if (c->type == BUS_MATCH_SENDER) {
+ if (streq_ptr(c->value_str, "org.freedesktop.DBus.Local"))
+ return BUS_MATCH_LOCAL;
+
+ if (streq_ptr(c->value_str, "org.freedesktop.DBus"))
+ found_driver = true;
+ }
+
+ if (c->type == BUS_MATCH_INTERFACE && streq_ptr(c->value_str, "org.freedesktop.DBus.Local"))
+ return BUS_MATCH_LOCAL;
+
+ if (c->type == BUS_MATCH_PATH && streq_ptr(c->value_str, "/org/freedesktop/DBus/Local"))
+ return BUS_MATCH_LOCAL;
+ }
+
+ return found_driver ? BUS_MATCH_DRIVER : BUS_MATCH_GENERIC;
+
+}
BUS_MATCH_ARG_PATH_LAST = BUS_MATCH_ARG_PATH + 63,
BUS_MATCH_ARG_NAMESPACE,
BUS_MATCH_ARG_NAMESPACE_LAST = BUS_MATCH_ARG_NAMESPACE + 63,
+ BUS_MATCH_ARG_HAS,
+ BUS_MATCH_ARG_HAS_LAST = BUS_MATCH_ARG_HAS + 63,
_BUS_MATCH_NODE_TYPE_MAX,
_BUS_MATCH_NODE_TYPE_INVALID = -1
};
char *value_str;
};
+enum bus_match_scope {
+ BUS_MATCH_GENERIC,
+ BUS_MATCH_LOCAL,
+ BUS_MATCH_DRIVER,
+};
+
int bus_match_run(sd_bus *bus, struct bus_match_node *root, sd_bus_message *m);
int bus_match_add(struct bus_match_node *root, struct bus_match_component *components, unsigned n_components, struct match_callback *callback);
int bus_match_parse(const char *match, struct bus_match_component **_components, unsigned *_n_components);
void bus_match_parse_free(struct bus_match_component *components, unsigned n_components);
-char *bus_match_to_string(struct bus_match_component *components, unsigned n_components);
+// UNNEEDED char *bus_match_to_string(struct bus_match_component *components, unsigned n_components);
+
+enum bus_match_scope bus_match_get_scope(const struct bus_match_component *components, unsigned n_components);
free(m->containers[i].offsets);
}
- free(m->containers);
- m->containers = NULL;
+ m->containers = mfree(m->containers);
m->n_containers = m->containers_allocated = 0;
m->root_container.index = 0;
if (m->iovec != m->iovec_fixed)
free(m->iovec);
- if (m->destination_ptr) {
- free(m->destination_ptr);
- m->destination_ptr = NULL;
- }
-
+ m->destination_ptr = mfree(m->destination_ptr);
message_reset_containers(m);
free(m->root_container.signature);
free(m->root_container.offsets);
size_t message_size,
int *fds,
unsigned n_fds,
- const struct ucred *ucred,
const char *label,
size_t extra,
sd_bus_message **ret) {
m->fds = fds;
m->n_fds = n_fds;
- if (ucred) {
- m->creds.pid = ucred->pid;
- m->creds.euid = ucred->uid;
- m->creds.egid = ucred->gid;
-
- /* Due to namespace translations some data might be
- * missing from this ucred record. */
- if (m->creds.pid > 0)
- m->creds.mask |= SD_BUS_CREDS_PID;
-
- if (m->creds.euid != UID_INVALID)
- m->creds.mask |= SD_BUS_CREDS_EUID;
-
- if (m->creds.egid != GID_INVALID)
- m->creds.mask |= SD_BUS_CREDS_EGID;
- }
-
if (label) {
m->creds.label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
memcpy(m->creds.label, label, label_sz + 1);
size_t length,
int *fds,
unsigned n_fds,
- const struct ucred *ucred,
const char *label,
sd_bus_message **ret) {
buffer, length,
length,
fds, n_fds,
- ucred, label,
+ label,
0, &m);
if (r < 0)
return r;
m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
m->header->endian = BUS_NATIVE_ENDIAN;
m->header->type = type;
- m->header->version = bus ? bus->message_version : 1;
- m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
+ m->header->version = bus->message_version;
+ m->allow_fds = bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
m->root_container.need_offsets = BUS_MESSAGE_IS_GVARIANT(m);
m->bus = sd_bus_ref(bus);
const char *format,
...) {
- _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
va_list ap;
assert_return(name, -EINVAL);
int error,
const sd_bus_error *p) {
- _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
+ _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
if (sd_bus_error_is_set(p))
return sd_bus_message_new_method_error(call, m, p);
return sd_bus_message_new_method_error(call, m, &berror);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_new_method_errnof(
sd_bus_message *call,
sd_bus_message **m,
const char *format,
...) {
- _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
+ _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
va_list ap;
va_start(ap, format);
return sd_bus_message_new_method_error(call, m, &berror);
}
+#endif // 0
void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m) {
assert(bus);
return NULL;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
assert_return(m, -EINVAL);
assert_return(type, -EINVAL);
return !(m->header->flags & BUS_MESSAGE_NO_AUTO_START);
}
+#endif // 0
_public_ int sd_bus_message_get_allow_interactive_authorization(sd_bus_message *m) {
assert_return(m, -EINVAL);
_public_ const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
assert_return(m, NULL);
- assert_return(sd_bus_error_is_set(&m->error), NULL);
+
+ if (!sd_bus_error_is_set(&m->error))
+ return NULL;
return &m->error;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_get_monotonic_usec(sd_bus_message *m, uint64_t *usec) {
assert_return(m, -EINVAL);
assert_return(usec, -EINVAL);
*seqnum = m->seqnum;
return 0;
}
+#endif // 0
_public_ sd_bus_creds *sd_bus_message_get_creds(sd_bus_message *m) {
assert_return(m, NULL);
return &m->creds;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_is_signal(
sd_bus_message *m,
const char *interface,
return 1;
}
+#endif // 0
_public_ int sd_bus_message_is_method_call(
sd_bus_message *m,
return 1;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_set_expect_reply(sd_bus_message *m, int b) {
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
return 0;
}
+#endif // 0
_public_ int sd_bus_message_set_auto_start(sd_bus_message *m, int b) {
assert_return(m, -EINVAL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_set_allow_interactive_authorization(sd_bus_message *m, int b) {
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
return 0;
}
+#endif // 0
static struct bus_container *message_get_container(sd_bus_message *m) {
assert(m);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_append_string_iovec(
sd_bus_message *m,
const struct iovec *iov,
return 0;
}
+#endif // 0
static int bus_message_open_array(
sd_bus_message *m,
}
static int bus_message_close_struct(sd_bus_message *m, struct bus_container *c, bool add_offset) {
+ bool fixed_size = true;
size_t n_variable = 0;
unsigned i = 0;
const char *p;
/* We need to add an offset for each item that has a
* variable size and that is not the last one in the
* list */
+ if (r == 0)
+ fixed_size = false;
if (r == 0 && p[n] != 0)
n_variable++;
assert(!c->need_offsets || i == c->n_offsets);
assert(c->need_offsets || n_variable == 0);
- if (n_variable <= 0) {
- a = message_extend_body(m, 1, 0, add_offset, false);
+ if (isempty(c->signature)) {
+ /* The unary type is encoded as fixed 1 byte padding */
+ a = message_extend_body(m, 1, 1, add_offset, false);
+ if (!a)
+ return -ENOMEM;
+
+ *a = 0;
+ } else if (n_variable <= 0) {
+ int alignment = 1;
+
+ /* Structures with fixed-size members only have to be
+ * fixed-size themselves. But gvariant requires all fixed-size
+ * elements to be sized a multiple of their alignment. Hence,
+ * we must *always* add final padding after the last member so
+ * the overall size of the structure is properly aligned. */
+ if (fixed_size)
+ alignment = bus_gvariant_get_alignment(strempty(c->signature));
+
+ assert(alignment > 0);
+
+ a = message_extend_body(m, alignment, 0, add_offset, false);
if (!a)
return -ENOMEM;
} else {
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_append_array_iovec(
sd_bus_message *m,
char type,
int r;
assert_return(m, -EINVAL);
- assert_return(memfd >= 0, -EINVAL);
+ assert_return(memfd >= 0, -EBADF);
assert_return(bus_type_is_trivial(type), -EINVAL);
assert_return(size > 0, -EINVAL);
assert_return(!m->sealed, -EPERM);
int r;
assert_return(m, -EINVAL);
- assert_return(memfd >= 0, -EINVAL);
+ assert_return(memfd >= 0, -EBADF);
assert_return(size > 0, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(!m->poisoned, -ESTALE);
return 0;
}
+#endif // 0
_public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) {
char **i;
signature = strempty(m->root_container.signature);
l = strlen(signature);
- sz = bus_gvariant_determine_word_size(sizeof(struct bus_header) + ALIGN8(m->fields_size) + m->body_size + 1 + l, 1);
- d = message_extend_body(m, 1, 1 + l + sz, false, true);
+ sz = bus_gvariant_determine_word_size(sizeof(struct bus_header) + ALIGN8(m->fields_size) + m->body_size + 1 + l + 2, 1);
+ d = message_extend_body(m, 1, 1 + l + 2 + sz, false, true);
if (!d)
return -ENOMEM;
*(uint8_t*) d = 0;
- memcpy((uint8_t*) d + 1, signature, l);
+ *((uint8_t*) d + 1) = SD_BUS_TYPE_STRUCT_BEGIN;
+ memcpy((uint8_t*) d + 2, signature, l);
+ *((uint8_t*) d + 1 + l + 1) = SD_BUS_TYPE_STRUCT_END;
- bus_gvariant_write_word_le((uint8_t*) d + 1 + l, sz, sizeof(struct bus_header) + m->fields_size);
+ bus_gvariant_write_word_le((uint8_t*) d + 1 + l + 2, sz, sizeof(struct bus_header) + m->fields_size);
m->footer = d;
- m->footer_accessible = 1 + l + sz;
+ m->footer_accessible = 1 + l + 2 + sz;
} else {
m->header->dbus1.fields_size = m->fields_size;
m->header->dbus1.body_size = m->body_size;
}
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) {
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
return false;
}
+#endif // 0
static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) {
struct bus_body_part *part;
assert(n_offsets);
if (isempty(signature)) {
+ /* Unary type is encoded as *fixed* 1 byte padding */
+ r = message_peek_body(m, &m->rindex, 1, 1, &q);
+ if (r < 0)
+ return r;
+
+ if (*(uint8_t *) q != 0)
+ return -EBADMSG;
+
*item_size = 0;
*offsets = NULL;
*n_offsets = 0;
if (r < 0)
return r;
- } else if (c->item_size <= 0) {
-
- /* gvariant empty struct */
- *item_size = 0;
- *offsets = NULL;
- *n_offsets = 0;
} else
/* gvariant with contents */
return build_struct_offsets(m, contents, c->item_size, item_size, offsets, n_offsets);
w->before = before;
w->begin = m->rindex;
- w->end = m->rindex + c->item_size;
+
+ /* Unary type has fixed size of 1, but virtual size of 0 */
+ if (BUS_MESSAGE_IS_GVARIANT(m) &&
+ type == SD_BUS_TYPE_STRUCT &&
+ isempty(signature))
+ w->end = m->rindex + 0;
+ else
+ w->end = m->rindex + c->item_size;
w->array_size = array_size;
w->item_size = item_size;
r = sd_bus_message_skip(m, s);
if (r < 0)
return r;
- assert(r != 0);
r = sd_bus_message_exit_container(m);
if (r < 0)
return -EBADMSG;
if (*p == 0) {
+ size_t l;
char *c;
- /* We found the beginning of the signature string, yay! */
+ /* We found the beginning of the signature
+ * string, yay! We require the body to be a
+ * structure, so verify it and then strip the
+ * opening/closing brackets. */
- c = strndup(p + 1, ((char*) m->footer + m->footer_accessible) - p - (1 + sz));
+ l = ((char*) m->footer + m->footer_accessible) - p - (1 + sz);
+ if (l < 2 ||
+ p[1] != SD_BUS_TYPE_STRUCT_BEGIN ||
+ p[1 + l - 1] != SD_BUS_TYPE_STRUCT_END)
+ return -EBADMSG;
+
+ c = strndup(p + 1 + 1, l - 2);
if (!c)
return -ENOMEM;
return message_append_field_string(m, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
}
+/// UNNEEDED by elogind
+#if 0
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
size_t total;
void *p, *e;
return 0;
}
+#endif // 0
int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
+ const char *s;
int r;
assert(m);
if (r <= 0)
return r;
- for (;;) {
- const char *s;
-
- r = sd_bus_message_read_basic(m, 's', &s);
- if (r < 0)
- return r;
- if (r == 0)
- break;
-
+ while ((r = sd_bus_message_read_basic(m, 's', &s)) > 0) {
r = strv_extend(l, s);
if (r < 0)
return r;
}
+ if (r < 0)
+ return r;
r = sd_bus_message_exit_container(m);
if (r < 0)
return 1;
}
-int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str, char ***strv) {
- const char *contents;
+static int bus_message_get_arg_skip(
+ sd_bus_message *m,
+ unsigned i,
+ char *_type,
+ const char **_contents) {
+
unsigned j;
- char type;
int r;
- assert(m);
- assert(str);
- assert(strv);
-
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
for (j = 0;; j++) {
+ const char *contents;
+ char type;
+
r = sd_bus_message_peek_type(m, &type, &contents);
if (r < 0)
return r;
!(type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g")))
return -ENXIO;
- if (j >= i)
- break;
+ if (j >= i) {
+ if (_contents)
+ *_contents = contents;
+ if (_type)
+ *_type = type;
+ return 0;
+ }
r = sd_bus_message_skip(m, NULL);
if (r < 0)
return r;
}
- if (type == SD_BUS_TYPE_ARRAY) {
+}
+
+int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str) {
+ char type;
+ int r;
+
+ assert(m);
+ assert(str);
- r = sd_bus_message_read_strv(m, strv);
+ r = bus_message_get_arg_skip(m, i, &type, NULL);
if (r < 0)
return r;
- *str = NULL;
+ if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE))
+ return -ENXIO;
+
+ return sd_bus_message_read_basic(m, type, str);
+}
- } else {
- r = sd_bus_message_read_basic(m, type, str);
+int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv) {
+ const char *contents;
+ char type;
+ int r;
+
+ assert(m);
+ assert(strv);
+
+ r = bus_message_get_arg_skip(m, i, &type, &contents);
if (r < 0)
return r;
- *strv = NULL;
- }
+ if (type != SD_BUS_TYPE_ARRAY)
+ return -ENXIO;
+ if (!STR_IN_SET(contents, "s", "o", "g"))
+ return -ENXIO;
- return 0;
+ return sd_bus_message_read_strv(m, strv);
}
_public_ int sd_bus_message_get_errno(sd_bus_message *m) {
return strempty(c->signature);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_is_empty(sd_bus_message *m) {
assert_return(m, -EINVAL);
return streq(strempty(m->root_container.signature), strempty(signature));
}
+#endif // 0
_public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
bool done_something = false;
return done_something;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) {
const char *c;
char t;
return 1;
}
+#endif // 0
_public_ sd_bus *sd_bus_message_get_bus(sd_bus_message *m) {
assert_return(m, NULL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int bus_message_append_sender(sd_bus_message *m, const char *sender) {
assert(m);
assert(sender);
m->priority = priority;
return 0;
}
+#endif // 0
}
int bus_message_seal(sd_bus_message *m, uint64_t serial, usec_t timeout);
-int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
+// UNNEEDED int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
int bus_message_from_header(
size_t message_size,
int *fds,
unsigned n_fds,
- const struct ucred *ucred,
const char *label,
size_t extra,
sd_bus_message **ret);
size_t length,
int *fds,
unsigned n_fds,
- const struct ucred *ucred,
const char *label,
sd_bus_message **ret);
-int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str, char ***strv);
+int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str);
+int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv);
int bus_message_append_ap(sd_bus_message *m, const char *types, va_list ap);
int bus_message_remarshal(sd_bus *bus, sd_bus_message **m);
-int bus_message_append_sender(sd_bus_message *m, const char *sender);
+// UNNEEDED int bus_message_append_sender(sd_bus_message *m, const char *sender);
void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m);
void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);
return 1;
}
+static void *vtable_method_convert_userdata(const sd_bus_vtable *p, void *u) {
+ assert(p);
+
+ return (uint8_t*) u + p->x.method.offset;
+}
+
static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
assert(p);
return 0;
}
+enum {
+ /* if set, add_subtree() works recursively */
+ CHILDREN_RECURSIVE = (1U << 1),
+ /* if set, add_subtree() scans object-manager hierarchies recursively */
+ CHILDREN_SUBHIERARCHIES = (1U << 0),
+};
+
static int add_subtree_to_set(
sd_bus *bus,
const char *prefix,
struct node *n,
+ unsigned int flags,
Set *s,
sd_bus_error *error) {
if (r < 0 && r != -EEXIST)
return r;
- r = add_subtree_to_set(bus, prefix, i, s, error);
- if (r < 0)
- return r;
- if (bus->nodes_modified)
- return 0;
+ if ((flags & CHILDREN_RECURSIVE) &&
+ ((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
+ r = add_subtree_to_set(bus, prefix, i, flags, s, error);
+ if (r < 0)
+ return r;
+ if (bus->nodes_modified)
+ return 0;
+ }
}
return 0;
sd_bus *bus,
const char *prefix,
struct node *n,
+ unsigned int flags,
Set **_s,
sd_bus_error *error) {
if (!s)
return -ENOMEM;
- r = add_subtree_to_set(bus, prefix, n, s, error);
+ r = add_subtree_to_set(bus, prefix, n, flags, s, error);
if (r < 0) {
set_free_free(s);
return r;
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_handler = c->callback;
bus->current_userdata = slot->userdata;
- r = c->callback(bus, m, slot->userdata, &error_buffer);
+ r = c->callback(m, slot->userdata, &error_buffer);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
if (bus->nodes_modified)
return 0;
+ u = vtable_method_convert_userdata(c->vtable, u);
+
*found_object = true;
if (c->last_iteration == bus->iteration_counter)
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_handler = c->vtable->x.method.handler;
bus->current_userdata = u;
- r = c->vtable->x.method.handler(bus, m, u, &error);
+ r = c->vtable->x.method.handler(m, u, &error);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
if (v->flags & SD_BUS_VTABLE_HIDDEN)
continue;
+ if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
+ continue;
+
r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
if (r < 0)
return r;
assert(n);
assert(found_object);
- r = get_child_nodes(bus, m->path, n, &s, &error);
+ r = get_child_nodes(bus, m->path, n, 0, &s, &error);
if (r < 0)
return bus_maybe_reply_error(m, r, &error);
if (bus->nodes_modified)
if (require_fallback || !n->object_managers)
return 0;
- r = get_child_nodes(bus, m->path, n, &s, &error);
+ r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
if (r < 0)
return r;
if (bus->nodes_modified)
e = strrchr(path, '/');
assert(e);
- p = strndupa(path, MAX(1, path - e));
+ p = strndupa(path, MAX(1, e - path));
parent = bus_node_allocate(bus, p);
if (!parent)
free(n);
}
+static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const char *path) {
+ struct node *n;
+
+ assert(bus);
+ assert(path);
+
+ n = hashmap_get(bus->nodes, path);
+ if (!n) {
+ char *prefix;
+
+ prefix = alloca(strlen(path) + 1);
+ OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
+ n = hashmap_get(bus->nodes, prefix);
+ if (n)
+ break;
+ }
+ }
+
+ while (n && !n->object_managers)
+ n = n->parent;
+
+ if (out)
+ *out = n;
+ return !!n;
+}
+
static int bus_add_object(
sd_bus *bus,
sd_bus_slot **slot,
if (!member_name_is_valid(v->x.property.member) ||
!signature_is_single(v->x.property.signature, false) ||
!(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
- v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY ||
+ (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
(!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
+ ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
(v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
r = -EINVAL;
goto fail;
return 0;
}
-int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
+_public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
BUS_DONT_DESTROY(bus);
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ struct node *object_manager;
int r;
/*
if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
+ r = bus_find_parent_object_manager(bus, &object_manager, path);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ESRCH;
+
do {
bus->nodes_modified = false;
m = sd_bus_message_unref(m);
- r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
+ r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
if (r < 0)
return r;
return sd_bus_send(bus, m, NULL);
}
+/// UNNEEDED by elogind
+#if 0
static int object_removed_append_all_prefix(
sd_bus *bus,
sd_bus_message *m,
return 0;
}
-int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
+_public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
BUS_DONT_DESTROY(bus);
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ struct node *object_manager;
int r;
/*
if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
+ r = bus_find_parent_object_manager(bus, &object_manager, path);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ESRCH;
+
do {
bus->nodes_modified = false;
m = sd_bus_message_unref(m);
- r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
+ r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
if (r < 0)
return r;
return sd_bus_send(bus, m, NULL);
}
+#endif // 0
static int interfaces_added_append_one_prefix(
sd_bus *bus,
BUS_DONT_DESTROY(bus);
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ struct node *object_manager;
char **i;
int r;
if (strv_isempty(interfaces))
return 0;
+ r = bus_find_parent_object_manager(bus, &object_manager, path);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ESRCH;
+
do {
bus->nodes_modified = false;
m = sd_bus_message_unref(m);
- r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
+ r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
if (r < 0)
return r;
_public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ struct node *object_manager;
int r;
assert_return(bus, -EINVAL);
if (strv_isempty(interfaces))
return 0;
- r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
+ r = bus_find_parent_object_manager(bus, &object_manager, path);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ESRCH;
+
+ r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
if (r < 0)
return r;
return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
sd_bus_slot *s;
struct node *n;
return r;
}
+#endif // 0
case BUS_MATCH_CALLBACK:
- if (slot->bus->bus_client)
+ if (slot->match_added)
bus_remove_match_internal(slot->bus, slot->match_callback.match_string, slot->match_callback.cookie);
slot->bus->match_callbacks_modified = true;
return NULL;
}
+/// UNNEEDED by elogind
+#if 0
_public_ sd_bus* sd_bus_slot_get_bus(sd_bus_slot *slot) {
assert_return(slot, NULL);
return free_and_strdup(&slot->description, description);
}
-_public_ int sd_bus_slot_get_description(sd_bus_slot *slot, char **description) {
+_public_ int sd_bus_slot_get_description(sd_bus_slot *slot, const char **description) {
assert_return(slot, -EINVAL);
assert_return(description, -EINVAL);
assert_return(slot->description, -ENXIO);
*description = slot->description;
return 0;
}
+#endif // 0
#include <unistd.h>
#include <poll.h>
+#include "sd-daemon.h"
#include "util.h"
#include "macro.h"
#include "missing.h"
#include "utf8.h"
-#include "sd-daemon.h"
+#include "formats-util.h"
+#include "signal-util.h"
#include "sd-bus.h"
#include "bus-socket.h"
/* We expect two response lines: "OK" and possibly
* "AGREE_UNIX_FD" */
- e = memmem(b->rbuffer, b->rbuffer_size, "\r\n", 2);
+ e = memmem_safe(b->rbuffer, b->rbuffer_size, "\r\n", 2);
if (!e)
return 0;
static int verify_anonymous_token(sd_bus *b, const char *p, size_t l) {
_cleanup_free_ char *token = NULL;
+ size_t len;
+ int r;
if (!b->anonymous_auth)
return 0;
if (l % 2 != 0)
return 0;
- token = unhexmem(p, l);
- if (!token)
- return -ENOMEM;
- if (memchr(token, 0, l/2))
+ r = unhexmem(p, l, (void **) &token, &len);
+ if (r < 0)
+ return 0;
+
+ if (memchr(token, 0, len))
return 0;
return !!utf8_is_valid(token);
static int verify_external_token(sd_bus *b, const char *p, size_t l) {
_cleanup_free_ char *token = NULL;
+ size_t len;
uid_t u;
int r;
if (l % 2 != 0)
return 0;
- token = unhexmem(p, l);
- if (!token)
- return -ENOMEM;
+ r = unhexmem(p, l, (void**) &token, &len);
+ if (r < 0)
+ return 0;
- if (memchr(token, 0, l/2))
+ if (memchr(token, 0, len))
return 0;
r = parse_uid(token, &u);
static int bus_socket_read_auth(sd_bus *b) {
struct msghdr mh;
- struct iovec iov;
+ struct iovec iov = {};
size_t n;
ssize_t k;
int r;
void *p;
union {
struct cmsghdr cmsghdr;
- uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) +
- CMSG_SPACE(sizeof(struct ucred)) +
- CMSG_SPACE(NAME_MAX)]; /*selinux label */
+ uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
} control;
- struct cmsghdr *cmsg;
bool handle_cmsg = false;
assert(b);
b->rbuffer = p;
- zero(iov);
iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size;
iov.iov_len = n - b->rbuffer_size;
b->rbuffer_size += k;
if (handle_cmsg) {
- for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
+ struct cmsghdr *cmsg;
+
+ CMSG_FOREACH(cmsg, &mh)
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS) {
int j;
j = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
close_many((int*) CMSG_DATA(cmsg), j);
return -EIO;
-
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_CREDENTIALS &&
- cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
-
- /* Ignore bogus data, which we might
- * get on socketpair() sockets */
- if (((struct ucred*) CMSG_DATA(cmsg))->pid != 0) {
- memcpy(&b->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
- b->ucred_valid = true;
- }
-
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_SECURITY) {
-
- size_t l;
-
- l = cmsg->cmsg_len - CMSG_LEN(0);
- if (l > 0) {
- memcpy(&b->label, CMSG_DATA(cmsg), l);
- b->label[l] = 0;
- }
- }
- }
+ } else
+ log_debug("Got unexpected auxiliary data with level=%d and type=%d",
+ cmsg->cmsg_level, cmsg->cmsg_type);
}
r = bus_socket_auth_verify(b);
}
void bus_socket_setup(sd_bus *b) {
- int enable;
-
assert(b);
- /* Enable SO_PASSCRED + SO_PASSEC. We try this on any
- * socket, just in case. */
- enable = !b->bus_client;
- (void) setsockopt(b->input_fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable));
-
- enable = !b->bus_client && (b->attach_flags & KDBUS_ATTACH_SECLABEL);
- (void) setsockopt(b->input_fd, SOL_SOCKET, SO_PASSSEC, &enable, sizeof(enable));
-
/* Increase the buffers to 8 MB */
fd_inc_rcvbuf(b->input_fd, SNDBUF_SIZE);
fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE);
}
static void bus_get_peercred(sd_bus *b) {
+ int r;
+
assert(b);
/* Get the peer for socketpair() sockets */
b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
+
+ /* Get the SELinux context of the peer */
+ r = getpeersec(b->input_fd, &b->label);
+ if (r < 0 && r != -EOPNOTSUPP)
+ log_debug_errno(r, "Failed to determine peer security context: %m");
}
static int bus_socket_start_auth_client(sd_bus *b) {
if (pid == 0) {
/* Child */
- reset_all_signal_handlers();
+ (void) reset_all_signal_handlers();
+ (void) reset_signal_mask();
close_all_fds(s+1, 1);
if (bus->prefer_writev)
k = writev(bus->output_fd, iov, m->n_iovec);
else {
- struct msghdr mh;
- zero(mh);
+ struct msghdr mh = {
+ .msg_iov = iov,
+ .msg_iovlen = m->n_iovec,
+ };
if (m->n_fds > 0) {
struct cmsghdr *control;
- control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds));
- mh.msg_control = control;
+ mh.msg_control = control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds));
+ mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds);
control->cmsg_level = SOL_SOCKET;
control->cmsg_type = SCM_RIGHTS;
- mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds);
memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds);
}
- mh.msg_iov = iov;
- mh.msg_iovlen = m->n_iovec;
-
k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
if (k < 0 && errno == ENOTSOCK) {
bus->prefer_writev = true;
r = bus_message_from_malloc(bus,
bus->rbuffer, size,
bus->fds, bus->n_fds,
- !bus->bus_client && bus->ucred_valid ? &bus->ucred : NULL,
- !bus->bus_client && bus->label[0] ? bus->label : NULL,
+ NULL,
&t);
if (r < 0) {
free(b);
int bus_socket_read_message(sd_bus *bus) {
struct msghdr mh;
- struct iovec iov;
+ struct iovec iov = {};
ssize_t k;
size_t need;
int r;
void *b;
union {
struct cmsghdr cmsghdr;
- uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) +
- CMSG_SPACE(sizeof(struct ucred)) +
- CMSG_SPACE(NAME_MAX)]; /*selinux label */
+ uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
} control;
- struct cmsghdr *cmsg;
bool handle_cmsg = false;
assert(bus);
bus->rbuffer = b;
- zero(iov);
iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size;
iov.iov_len = need - bus->rbuffer_size;
bus->rbuffer_size += k;
if (handle_cmsg) {
- for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
+ struct cmsghdr *cmsg;
+
+ CMSG_FOREACH(cmsg, &mh)
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS) {
int n, *f;
memcpy(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int));
bus->fds = f;
bus->n_fds += n;
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_CREDENTIALS &&
- cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
-
- /* Ignore bogus data, which we might
- * get on socketpair() sockets */
- if (((struct ucred*) CMSG_DATA(cmsg))->pid != 0) {
- memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
- bus->ucred_valid = true;
- }
-
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_SECURITY) {
-
- size_t l;
- l = cmsg->cmsg_len - CMSG_LEN(0);
- if (l > 0) {
- memcpy(&bus->label, CMSG_DATA(cmsg), l);
- bus->label[l] = 0;
- }
- }
- }
+ } else
+ log_debug("Got unexpected auxiliary data with level=%d and type=%d",
+ cmsg->cmsg_level, cmsg->cmsg_type);
}
r = bus_socket_read_message_need(bus, &need);
assert_return(bus, -EINVAL);
assert_return(track, -EINVAL);
+ if (!bus->bus_client)
+ return -EINVAL;
+
t = new0(sd_bus_track, 1);
if (!t)
return -ENOMEM;
return NULL;
}
-static int on_name_owner_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
sd_bus_track *track = userdata;
const char *name, *old, *new;
int r;
- assert(bus);
assert(message);
assert(track);
track->modified = false;
track->iterator = ITERATOR_FIRST;
- hashmap_iterate(track->names, &track->iterator, (const void**) &n);
+ hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
return n;
}
if (track->modified)
return NULL;
- hashmap_iterate(track->names, &track->iterator, (const void**) &n);
+ hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
return n;
}
sd_bus_track_unref(track);
}
+/// UNNEEDED by elogind
+#if 0
_public_ void *sd_bus_track_get_userdata(sd_bus_track *track) {
assert_return(track, NULL);
return ret;
}
+#endif // 0
return !!memchr(valid, c, sizeof(valid));
}
+/// UNNEEDED by elogind
+#if 0
bool bus_type_is_valid_in_signature(char c) {
static const char valid[] = {
SD_BUS_TYPE_BYTE,
return !!memchr(valid, c, sizeof(valid));
}
+#endif // 0
bool bus_type_is_basic(char c) {
static const char valid[] = {
#include "sd-bus.h"
bool bus_type_is_valid(char c) _const_;
-bool bus_type_is_valid_in_signature(char c) _const_;
+// UNNEEDED bool bus_type_is_valid_in_signature(char c) _const_;
bool bus_type_is_basic(char c) _const_;
/* "trivial" is systemd's term for what the D-Bus Specification calls
* a "fixed type": that is, a basic type of fixed length */
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-#include "xml.h"
-#include "sd-bus-vtable.h"
-
-#include "busctl-introspect.h"
-
-#define NODE_DEPTH_MAX 16
-
-typedef struct Context {
- const XMLIntrospectOps *ops;
- void *userdata;
-
- char *interface_name;
- uint64_t interface_flags;
-
- char *member_name;
- char *member_signature;
- char *member_result;
- uint64_t member_flags;
- bool member_writable;
-
- const char *current;
- void *xml_state;
-} Context;
-
-static void context_reset_member(Context *c) {
- free(c->member_name);
- free(c->member_signature);
- free(c->member_result);
-
- c->member_name = c->member_signature = c->member_result = NULL;
- c->member_flags = 0;
- c->member_writable = false;
-}
-
-static void context_reset_interface(Context *c) {
- free(c->interface_name);
- c->interface_name = NULL;
- c->interface_flags = 0;
-
- context_reset_member(c);
-}
-
-static int parse_xml_annotation(Context *context, uint64_t *flags) {
-
- enum {
- STATE_ANNOTATION,
- STATE_NAME,
- STATE_VALUE
- } state = STATE_ANNOTATION;
-
- _cleanup_free_ char *field = NULL, *value = NULL;
-
- assert(context);
-
- for (;;) {
- _cleanup_free_ char *name = NULL;
-
- int t;
-
- t = xml_tokenize(&context->current, &name, &context->xml_state, NULL);
- if (t < 0) {
- log_error("XML parse error.");
- return t;
- }
-
- if (t == XML_END) {
- log_error("Premature end of XML data.");
- return -EBADMSG;
- }
-
- switch (state) {
-
- case STATE_ANNOTATION:
-
- if (t == XML_ATTRIBUTE_NAME) {
-
- if (streq_ptr(name, "name"))
- state = STATE_NAME;
-
- else if (streq_ptr(name, "value"))
- state = STATE_VALUE;
-
- else {
- log_error("Unexpected <annotation> attribute %s.", name);
- return -EBADMSG;
- }
-
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "annotation"))) {
-
- if (flags) {
- if (streq_ptr(field, "org.freedesktop.DBus.Deprecated")) {
-
- if (streq_ptr(value, "true"))
- *flags |= SD_BUS_VTABLE_DEPRECATED;
-
- } else if (streq_ptr(field, "org.freedesktop.DBus.Method.NoReply")) {
-
- if (streq_ptr(value, "true"))
- *flags |= SD_BUS_VTABLE_METHOD_NO_REPLY;
-
- } else if (streq_ptr(field, "org.freedesktop.DBus.Property.EmitsChangedSignal")) {
-
- if (streq_ptr(value, "const"))
- *flags = (*flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE)) | SD_BUS_VTABLE_PROPERTY_CONST;
- else if (streq_ptr(value, "invalidates"))
- *flags = (*flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_CONST)) | SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION;
- else if (streq_ptr(value, "false"))
- *flags = *flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION);
- }
- }
-
- return 0;
-
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in <annotation>. (1)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE) {
- free(field);
- field = name;
- name = NULL;
-
- state = STATE_ANNOTATION;
- } else {
- log_error("Unexpected token in <annotation>. (2)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_VALUE:
-
- if (t == XML_ATTRIBUTE_VALUE) {
- free(value);
- value = name;
- name = NULL;
-
- state = STATE_ANNOTATION;
- } else {
- log_error("Unexpected token in <annotation>. (3)");
- return -EINVAL;
- }
-
- break;
-
- default:
- assert_not_reached("Bad state");
- }
- }
-}
-
-static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth) {
-
- enum {
- STATE_NODE,
- STATE_NODE_NAME,
- STATE_INTERFACE,
- STATE_INTERFACE_NAME,
- STATE_METHOD,
- STATE_METHOD_NAME,
- STATE_METHOD_ARG,
- STATE_METHOD_ARG_NAME,
- STATE_METHOD_ARG_TYPE,
- STATE_METHOD_ARG_DIRECTION,
- STATE_SIGNAL,
- STATE_SIGNAL_NAME,
- STATE_SIGNAL_ARG,
- STATE_SIGNAL_ARG_NAME,
- STATE_SIGNAL_ARG_TYPE,
- STATE_PROPERTY,
- STATE_PROPERTY_NAME,
- STATE_PROPERTY_TYPE,
- STATE_PROPERTY_ACCESS,
- } state = STATE_NODE;
-
- _cleanup_free_ char *node_path = NULL, *argument_type = NULL, *argument_direction = NULL;
- const char *np = prefix;
- int r;
-
- assert(context);
- assert(prefix);
-
- if (n_depth > NODE_DEPTH_MAX) {
- log_error("<node> depth too high.");
- return -EINVAL;
- }
-
- for (;;) {
- _cleanup_free_ char *name = NULL;
- int t;
-
- t = xml_tokenize(&context->current, &name, &context->xml_state, NULL);
- if (t < 0) {
- log_error("XML parse error.");
- return t;
- }
-
- if (t == XML_END) {
- log_error("Premature end of XML data.");
- return -EBADMSG;
- }
-
- switch (state) {
-
- case STATE_NODE:
- if (t == XML_ATTRIBUTE_NAME) {
-
- if (streq_ptr(name, "name"))
- state = STATE_NODE_NAME;
- else {
- log_error("Unexpected <node> attribute %s.", name);
- return -EBADMSG;
- }
-
- } else if (t == XML_TAG_OPEN) {
-
- if (streq_ptr(name, "interface"))
- state = STATE_INTERFACE;
- else if (streq_ptr(name, "node")) {
-
- r = parse_xml_node(context, np, n_depth+1);
- if (r < 0)
- return r;
- } else {
- log_error("Unexpected <node> tag %s.", name);
- return -EBADMSG;
- }
-
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "node"))) {
-
- if (context->ops->on_path) {
- r = context->ops->on_path(node_path ? node_path : np, context->userdata);
- if (r < 0)
- return r;
- }
-
- return 0;
-
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in <node>. (1)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_NODE_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE) {
-
- free(node_path);
-
- if (name[0] == '/') {
- node_path = name;
- name = NULL;
- } else {
-
- if (endswith(prefix, "/"))
- node_path = strappend(prefix, name);
- else
- node_path = strjoin(prefix, "/", name, NULL);
- if (!node_path)
- return log_oom();
- }
-
- np = node_path;
- state = STATE_NODE;
- } else {
- log_error("Unexpected token in <node>. (2)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_INTERFACE:
-
- if (t == XML_ATTRIBUTE_NAME) {
- if (streq_ptr(name, "name"))
- state = STATE_INTERFACE_NAME;
- else {
- log_error("Unexpected <interface> attribute %s.", name);
- return -EBADMSG;
- }
-
- } else if (t == XML_TAG_OPEN) {
- if (streq_ptr(name, "method"))
- state = STATE_METHOD;
- else if (streq_ptr(name, "signal"))
- state = STATE_SIGNAL;
- else if (streq_ptr(name, "property")) {
- context->member_flags |= SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE;
- state = STATE_PROPERTY;
- } else if (streq_ptr(name, "annotation")) {
- r = parse_xml_annotation(context, &context->interface_flags);
- if (r < 0)
- return r;
- } else {
- log_error("Unexpected <interface> tag %s.", name);
- return -EINVAL;
- }
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "interface"))) {
-
- if (n_depth == 0) {
- if (context->ops->on_interface) {
- r = context->ops->on_interface(context->interface_name, context->interface_flags, context->userdata);
- if (r < 0)
- return r;
- }
-
- context_reset_interface(context);
- }
-
- state = STATE_NODE;
-
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in <interface>. (1)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_INTERFACE_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE) {
- if (n_depth == 0) {
- free(context->interface_name);
- context->interface_name = name;
- name = NULL;
- }
-
- state = STATE_INTERFACE;
- } else {
- log_error("Unexpected token in <interface>. (2)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_METHOD:
-
- if (t == XML_ATTRIBUTE_NAME) {
- if (streq_ptr(name, "name"))
- state = STATE_METHOD_NAME;
- else {
- log_error("Unexpected <method> attribute %s", name);
- return -EBADMSG;
- }
- } else if (t == XML_TAG_OPEN) {
- if (streq_ptr(name, "arg"))
- state = STATE_METHOD_ARG;
- else if (streq_ptr(name, "annotation")) {
- r = parse_xml_annotation(context, &context->member_flags);
- if (r < 0)
- return r;
- } else {
- log_error("Unexpected <method> tag %s.", name);
- return -EINVAL;
- }
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "method"))) {
-
- if (n_depth == 0) {
- if (context->ops->on_method) {
- r = context->ops->on_method(context->interface_name, context->member_name, context->member_signature, context->member_result, context->member_flags, context->userdata);
- if (r < 0)
- return r;
- }
-
- context_reset_member(context);
- }
-
- state = STATE_INTERFACE;
-
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in <method> (1).");
- return -EINVAL;
- }
-
- break;
-
- case STATE_METHOD_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE) {
-
- if (n_depth == 0) {
- free(context->member_name);
- context->member_name = name;
- name = NULL;
- }
-
- state = STATE_METHOD;
- } else {
- log_error("Unexpected token in <method> (2).");
- return -EINVAL;
- }
-
- break;
-
- case STATE_METHOD_ARG:
-
- if (t == XML_ATTRIBUTE_NAME) {
- if (streq_ptr(name, "name"))
- state = STATE_METHOD_ARG_NAME;
- else if (streq_ptr(name, "type"))
- state = STATE_METHOD_ARG_TYPE;
- else if (streq_ptr(name, "direction"))
- state = STATE_METHOD_ARG_DIRECTION;
- else {
- log_error("Unexpected method <arg> attribute %s.", name);
- return -EBADMSG;
- }
- } else if (t == XML_TAG_OPEN) {
- if (streq_ptr(name, "annotation")) {
- r = parse_xml_annotation(context, NULL);
- if (r < 0)
- return r;
- } else {
- log_error("Unexpected method <arg> tag %s.", name);
- return -EINVAL;
- }
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) {
-
- if (n_depth == 0) {
-
- if (argument_type) {
- if (!argument_direction || streq(argument_direction, "in")) {
- if (!strextend(&context->member_signature, argument_type, NULL))
- return log_oom();
- } else if (streq(argument_direction, "out")) {
- if (!strextend(&context->member_result, argument_type, NULL))
- return log_oom();
- }
- }
-
- free(argument_type);
- free(argument_direction);
- argument_type = argument_direction = NULL;
- }
-
- state = STATE_METHOD;
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in method <arg>. (1)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_METHOD_ARG_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE)
- state = STATE_METHOD_ARG;
- else {
- log_error("Unexpected token in method <arg>. (2)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_METHOD_ARG_TYPE:
-
- if (t == XML_ATTRIBUTE_VALUE) {
- free(argument_type);
- argument_type = name;
- name = NULL;
-
- state = STATE_METHOD_ARG;
- } else {
- log_error("Unexpected token in method <arg>. (3)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_METHOD_ARG_DIRECTION:
-
- if (t == XML_ATTRIBUTE_VALUE) {
- free(argument_direction);
- argument_direction = name;
- name = NULL;
-
- state = STATE_METHOD_ARG;
- } else {
- log_error("Unexpected token in method <arg>. (4)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_SIGNAL:
-
- if (t == XML_ATTRIBUTE_NAME) {
- if (streq_ptr(name, "name"))
- state = STATE_SIGNAL_NAME;
- else {
- log_error("Unexpected <signal> attribute %s.", name);
- return -EBADMSG;
- }
- } else if (t == XML_TAG_OPEN) {
- if (streq_ptr(name, "arg"))
- state = STATE_SIGNAL_ARG;
- else if (streq_ptr(name, "annotation")) {
- r = parse_xml_annotation(context, &context->member_flags);
- if (r < 0)
- return r;
- } else {
- log_error("Unexpected <signal> tag %s.", name);
- return -EINVAL;
- }
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "signal"))) {
-
- if (n_depth == 0) {
- if (context->ops->on_signal) {
- r = context->ops->on_signal(context->interface_name, context->member_name, context->member_signature, context->member_flags, context->userdata);
- if (r < 0)
- return r;
- }
-
- context_reset_member(context);
- }
-
- state = STATE_INTERFACE;
-
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in <signal>. (1)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_SIGNAL_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE) {
-
- if (n_depth == 0) {
- free(context->member_name);
- context->member_name = name;
- name = NULL;
- }
-
- state = STATE_SIGNAL;
- } else {
- log_error("Unexpected token in <signal>. (2)");
- return -EINVAL;
- }
-
- break;
-
-
- case STATE_SIGNAL_ARG:
-
- if (t == XML_ATTRIBUTE_NAME) {
- if (streq_ptr(name, "name"))
- state = STATE_SIGNAL_ARG_NAME;
- else if (streq_ptr(name, "type"))
- state = STATE_SIGNAL_ARG_TYPE;
- else {
- log_error("Unexpected signal <arg> attribute %s.", name);
- return -EBADMSG;
- }
- } else if (t == XML_TAG_OPEN) {
- if (streq_ptr(name, "annotation")) {
- r = parse_xml_annotation(context, NULL);
- if (r < 0)
- return r;
- } else {
- log_error("Unexpected signal <arg> tag %s.", name);
- return -EINVAL;
- }
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) {
-
- if (argument_type) {
- if (!strextend(&context->member_signature, argument_type, NULL))
- return log_oom();
-
- free(argument_type);
- argument_type = NULL;
- }
-
- state = STATE_SIGNAL;
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in signal <arg> (1).");
- return -EINVAL;
- }
-
- break;
-
- case STATE_SIGNAL_ARG_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE)
- state = STATE_SIGNAL_ARG;
- else {
- log_error("Unexpected token in signal <arg> (2).");
- return -EINVAL;
- }
-
- break;
-
- case STATE_SIGNAL_ARG_TYPE:
-
- if (t == XML_ATTRIBUTE_VALUE) {
- free(argument_type);
- argument_type = name;
- name = NULL;
-
- state = STATE_SIGNAL_ARG;
- } else {
- log_error("Unexpected token in signal <arg> (3).");
- return -EINVAL;
- }
-
- break;
-
- case STATE_PROPERTY:
-
- if (t == XML_ATTRIBUTE_NAME) {
- if (streq_ptr(name, "name"))
- state = STATE_PROPERTY_NAME;
- else if (streq_ptr(name, "type"))
- state = STATE_PROPERTY_TYPE;
- else if (streq_ptr(name, "access"))
- state = STATE_PROPERTY_ACCESS;
- else {
- log_error("Unexpected <property> attribute %s.", name);
- return -EBADMSG;
- }
- } else if (t == XML_TAG_OPEN) {
-
- if (streq_ptr(name, "annotation")) {
- r = parse_xml_annotation(context, &context->member_flags);
- if (r < 0)
- return r;
- } else {
- log_error("Unexpected <property> tag %s.", name);
- return -EINVAL;
- }
-
- } else if (t == XML_TAG_CLOSE_EMPTY ||
- (t == XML_TAG_CLOSE && streq_ptr(name, "property"))) {
-
- if (n_depth == 0) {
- if (context->ops->on_property) {
- r = context->ops->on_property(context->interface_name, context->member_name, context->member_signature, context->member_writable, context->member_flags, context->userdata);
- if (r < 0)
- return r;
- }
-
- context_reset_member(context);
- }
-
- state = STATE_INTERFACE;
-
- } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token in <property>. (1)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_PROPERTY_NAME:
-
- if (t == XML_ATTRIBUTE_VALUE) {
-
- if (n_depth == 0) {
- free(context->member_name);
- context->member_name = name;
- name = NULL;
- }
- state = STATE_PROPERTY;
- } else {
- log_error("Unexpected token in <property>. (2)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_PROPERTY_TYPE:
-
- if (t == XML_ATTRIBUTE_VALUE) {
-
- if (n_depth == 0) {
- free(context->member_signature);
- context->member_signature = name;
- name = NULL;
- }
-
- state = STATE_PROPERTY;
- } else {
- log_error("Unexpected token in <property>. (3)");
- return -EINVAL;
- }
-
- break;
-
- case STATE_PROPERTY_ACCESS:
-
- if (t == XML_ATTRIBUTE_VALUE) {
-
- if (streq(name, "readwrite") || streq(name, "write"))
- context->member_writable = true;
-
- state = STATE_PROPERTY;
- } else {
- log_error("Unexpected token in <property>. (4)");
- return -EINVAL;
- }
-
- break;
- }
- }
-}
-
-int parse_xml_introspect(const char *prefix, const char *xml, const XMLIntrospectOps *ops, void *userdata) {
- Context context = {
- .ops = ops,
- .userdata = userdata,
- .current = xml,
- };
-
- int r;
-
- assert(prefix);
- assert(xml);
- assert(ops);
-
- for (;;) {
- _cleanup_free_ char *name = NULL;
-
- r = xml_tokenize(&context.current, &name, &context.xml_state, NULL);
- if (r < 0) {
- log_error("XML parse error");
- goto finish;
- }
-
- if (r == XML_END) {
- r = 0;
- break;
- }
-
- if (r == XML_TAG_OPEN) {
-
- if (streq(name, "node")) {
- r = parse_xml_node(&context, prefix, 0);
- if (r < 0)
- goto finish;
- } else {
- log_error("Unexpected tag '%s' in introspection data.", name);
- r = -EBADMSG;
- goto finish;
- }
- } else if (r != XML_TEXT || !in_charset(name, WHITESPACE)) {
- log_error("Unexpected token.");
- r = -EBADMSG;
- goto finish;
- }
- }
-
-finish:
- context_reset_interface(&context);
-
- return r;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-typedef struct XMLIntrospectOps {
- int (*on_path)(const char *path, void *userdata);
- int (*on_interface)(const char *name, uint64_t flags, void *userdata);
- int (*on_method)(const char *interface, const char *name, const char *signature, const char *result, uint64_t flags, void *userdata);
- int (*on_signal)(const char *interface, const char *name, const char *signature, uint64_t flags, void *userdata);
- int (*on_property)(const char *interface, const char *name, const char *signature, bool writable, uint64_t flags, void *userdata);
-} XMLIntrospectOps;
-
-int parse_xml_introspect(const char *prefix, const char *xml, const XMLIntrospectOps *ops, void *userdata);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <getopt.h>
-
-#include "strv.h"
-#include "util.h"
-#include "log.h"
-#include "build.h"
-#include "pager.h"
-#include "path-util.h"
-#include "set.h"
-
-#include "sd-bus.h"
-#include "bus-internal.h"
-#include "bus-util.h"
-#include "bus-dump.h"
-#include "bus-signature.h"
-#include "bus-type.h"
-#include "busctl-introspect.h"
-
-static bool arg_no_pager = false;
-static bool arg_legend = true;
-static char *arg_address = NULL;
-static bool arg_unique = false;
-static bool arg_acquired = false;
-static bool arg_activatable = false;
-static bool arg_show_machine = false;
-static char **arg_matches = NULL;
-static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
-static char *arg_host = NULL;
-static bool arg_user = false;
-static size_t arg_snaplen = 4096;
-static bool arg_list = false;
-static bool arg_quiet = false;
-static bool arg_verbose = false;
-static bool arg_expect_reply = true;
-static bool arg_auto_start = true;
-static bool arg_allow_interactive_authorization = true;
-static bool arg_augment_creds = true;
-static usec_t arg_timeout = 0;
-
-static void pager_open_if_enabled(void) {
-
- /* Cache result before we open the pager */
- if (arg_no_pager)
- return;
-
- pager_open(false);
-}
-
-#define NAME_IS_ACQUIRED INT_TO_PTR(1)
-#define NAME_IS_ACTIVATABLE INT_TO_PTR(2)
-
-static int list_bus_names(sd_bus *bus, char **argv) {
- _cleanup_strv_free_ char **acquired = NULL, **activatable = NULL;
- _cleanup_free_ char **merged = NULL;
- _cleanup_hashmap_free_ Hashmap *names = NULL;
- char **i;
- int r;
- size_t max_i = 0;
- unsigned n = 0;
- void *v;
- char *k;
- Iterator iterator;
-
- assert(bus);
-
- if (!arg_unique && !arg_acquired && !arg_activatable)
- arg_unique = arg_acquired = arg_activatable = true;
-
- r = sd_bus_list_names(bus, (arg_acquired || arg_unique) ? &acquired : NULL, arg_activatable ? &activatable : NULL);
- if (r < 0)
- return log_error_errno(r, "Failed to list names: %m");
-
- pager_open_if_enabled();
-
- names = hashmap_new(&string_hash_ops);
- if (!names)
- return log_oom();
-
- STRV_FOREACH(i, acquired) {
- max_i = MAX(max_i, strlen(*i));
-
- r = hashmap_put(names, *i, NAME_IS_ACQUIRED);
- if (r < 0)
- return log_error_errno(r, "Failed to add to hashmap: %m");
- }
-
- STRV_FOREACH(i, activatable) {
- max_i = MAX(max_i, strlen(*i));
-
- r = hashmap_put(names, *i, NAME_IS_ACTIVATABLE);
- if (r < 0 && r != -EEXIST)
- return log_error_errno(r, "Failed to add to hashmap: %m");
- }
-
- merged = new(char*, hashmap_size(names) + 1);
- HASHMAP_FOREACH_KEY(v, k, names, iterator)
- merged[n++] = k;
-
- merged[n] = NULL;
- strv_sort(merged);
-
- if (arg_legend) {
- printf("%-*s %*s %-*s %-*s %-*s %-*s %-*s %-*s",
- (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER", 13, "CONNECTION", 25, "UNIT", 10, "SESSION", 19, "DESCRIPTION");
-
- if (arg_show_machine)
- puts(" MACHINE");
- else
- putchar('\n');
- }
-
- STRV_FOREACH(i, merged) {
- _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
- sd_id128_t mid;
-
- if (hashmap_get(names, *i) == NAME_IS_ACTIVATABLE) {
- /* Activatable */
-
- printf("%-*s", (int) max_i, *i);
- printf(" - - - (activatable) - - ");
- if (arg_show_machine)
- puts(" -");
- else
- putchar('\n');
- continue;
-
- }
-
- if (!arg_unique && (*i)[0] == ':')
- continue;
-
- if (!arg_acquired && (*i)[0] != ':')
- continue;
-
- printf("%-*s", (int) max_i, *i);
-
- r = sd_bus_get_name_creds(
- bus, *i,
- (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) |
- SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|
- SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_SESSION|
- SD_BUS_CREDS_DESCRIPTION, &creds);
- if (r >= 0) {
- const char *unique, *session, *unit, *cn;
- pid_t pid;
- uid_t uid;
-
- r = sd_bus_creds_get_pid(creds, &pid);
- if (r >= 0) {
- const char *comm = NULL;
-
- sd_bus_creds_get_comm(creds, &comm);
-
- printf(" %10lu %-15s", (unsigned long) pid, strna(comm));
- } else
- fputs(" - - ", stdout);
-
- r = sd_bus_creds_get_euid(creds, &uid);
- if (r >= 0) {
- _cleanup_free_ char *u = NULL;
-
- u = uid_to_name(uid);
- if (!u)
- return log_oom();
-
- if (strlen(u) > 16)
- u[16] = 0;
-
- printf(" %-16s", u);
- } else
- fputs(" - ", stdout);
-
- r = sd_bus_creds_get_unique_name(creds, &unique);
- if (r >= 0)
- printf(" %-13s", unique);
- else
- fputs(" - ", stdout);
-
- r = sd_bus_creds_get_unit(creds, &unit);
- if (r >= 0) {
- _cleanup_free_ char *e;
-
- e = ellipsize(unit, 25, 100);
- if (!e)
- return log_oom();
-
- printf(" %-25s", e);
- } else
- fputs(" - ", stdout);
-
- r = sd_bus_creds_get_session(creds, &session);
- if (r >= 0)
- printf(" %-10s", session);
- else
- fputs(" - ", stdout);
-
- r = sd_bus_creds_get_description(creds, &cn);
- if (r >= 0)
- printf(" %-19s", cn);
- else
- fputs(" - ", stdout);
-
- } else
- printf(" - - - - - - - ");
-
- if (arg_show_machine) {
- r = sd_bus_get_name_machine_id(bus, *i, &mid);
- if (r >= 0) {
- char m[SD_ID128_STRING_MAX];
- printf(" %s\n", sd_id128_to_string(mid, m));
- } else
- puts(" -");
- } else
- putchar('\n');
- }
-
- return 0;
-}
-
-static void print_subtree(const char *prefix, const char *path, char **l) {
- const char *vertical, *space;
- char **n;
-
- /* We assume the list is sorted. Let's first skip over the
- * entry we are looking at. */
- for (;;) {
- if (!*l)
- return;
-
- if (!streq(*l, path))
- break;
-
- l++;
- }
-
- vertical = strjoina(prefix, draw_special_char(DRAW_TREE_VERTICAL));
- space = strjoina(prefix, draw_special_char(DRAW_TREE_SPACE));
-
- for (;;) {
- bool has_more = false;
-
- if (!*l || !path_startswith(*l, path))
- break;
-
- n = l + 1;
- for (;;) {
- if (!*n || !path_startswith(*n, path))
- break;
-
- if (!path_startswith(*n, *l)) {
- has_more = true;
- break;
- }
-
- n++;
- }
-
- printf("%s%s%s\n", prefix, draw_special_char(has_more ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT), *l);
-
- print_subtree(has_more ? vertical : space, *l, l);
- l = n;
- }
-}
-
-static void print_tree(const char *prefix, char **l) {
-
- pager_open_if_enabled();
-
- prefix = strempty(prefix);
-
- if (arg_list) {
- char **i;
-
- STRV_FOREACH(i, l)
- printf("%s%s\n", prefix, *i);
- return;
- }
-
- if (strv_isempty(l)) {
- printf("No objects discovered.\n");
- return;
- }
-
- if (streq(l[0], "/") && !l[1]) {
- printf("Only root object discovered.\n");
- return;
- }
-
- print_subtree(prefix, "/", l);
-}
-
-static int on_path(const char *path, void *userdata) {
- Set *paths = userdata;
- int r;
-
- assert(paths);
-
- r = set_put_strdup(paths, path);
- if (r < 0)
- return log_oom();
-
- return 0;
-}
-
-static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *paths, bool many) {
- static const XMLIntrospectOps ops = {
- .on_path = on_path,
- };
-
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- const char *xml;
- int r;
-
- r = sd_bus_call_method(bus, service, path, "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
- if (r < 0) {
- if (many)
- printf("Failed to introspect object %s of service %s: %s\n", path, service, bus_error_message(&error, r));
- else
- log_error("Failed to introspect object %s of service %s: %s", path, service, bus_error_message(&error, r));
- return r;
- }
-
- r = sd_bus_message_read(reply, "s", &xml);
- if (r < 0)
- return bus_log_parse_error(r);
-
- return parse_xml_introspect(path, xml, &ops, paths);
-}
-
-static int tree_one(sd_bus *bus, const char *service, const char *prefix, bool many) {
- _cleanup_set_free_free_ Set *paths = NULL, *done = NULL, *failed = NULL;
- _cleanup_free_ char **l = NULL;
- char *m;
- int r;
-
- paths = set_new(&string_hash_ops);
- if (!paths)
- return log_oom();
-
- done = set_new(&string_hash_ops);
- if (!done)
- return log_oom();
-
- failed = set_new(&string_hash_ops);
- if (!failed)
- return log_oom();
-
- m = strdup("/");
- if (!m)
- return log_oom();
-
- r = set_put(paths, m);
- if (r < 0) {
- free(m);
- return log_oom();
- }
-
- for (;;) {
- _cleanup_free_ char *p = NULL;
- int q;
-
- p = set_steal_first(paths);
- if (!p)
- break;
-
- if (set_contains(done, p) ||
- set_contains(failed, p))
- continue;
-
- q = find_nodes(bus, service, p, paths, many);
- if (q < 0) {
- if (r >= 0)
- r = q;
-
- q = set_put(failed, p);
- } else
- q = set_put(done, p);
-
- if (q < 0)
- return log_oom();
-
- assert(q != 0);
- p = NULL;
- }
-
- pager_open_if_enabled();
-
- l = set_get_strv(done);
- if (!l)
- return log_oom();
-
- strv_sort(l);
- print_tree(prefix, l);
-
- fflush(stdout);
-
- return r;
-}
-
-static int tree(sd_bus *bus, char **argv) {
- char **i;
- int r = 0;
-
- if (!arg_unique && !arg_acquired)
- arg_acquired = true;
-
- if (strv_length(argv) <= 1) {
- _cleanup_strv_free_ char **names = NULL;
- bool not_first = false;
-
- r = sd_bus_list_names(bus, &names, NULL);
- if (r < 0)
- return log_error_errno(r, "Failed to get name list: %m");
-
- pager_open_if_enabled();
-
- STRV_FOREACH(i, names) {
- int q;
-
- if (!arg_unique && (*i)[0] == ':')
- continue;
-
- if (!arg_acquired && (*i)[0] == ':')
- continue;
-
- if (not_first)
- printf("\n");
-
- printf("Service %s%s%s:\n", ansi_highlight(), *i, ansi_highlight_off());
-
- q = tree_one(bus, *i, NULL, true);
- if (q < 0 && r >= 0)
- r = q;
-
- not_first = true;
- }
- } else {
- STRV_FOREACH(i, argv+1) {
- int q;
-
- if (i > argv+1)
- printf("\n");
-
- if (argv[2]) {
- pager_open_if_enabled();
- printf("Service %s%s%s:\n", ansi_highlight(), *i, ansi_highlight_off());
- }
-
- q = tree_one(bus, *i, NULL, !!argv[2]);
- if (q < 0 && r >= 0)
- r = q;
- }
- }
-
- return r;
-}
-
-static int format_cmdline(sd_bus_message *m, FILE *f, bool needs_space) {
- int r;
-
- for (;;) {
- const char *contents = NULL;
- char type;
- union {
- uint8_t u8;
- uint16_t u16;
- int16_t s16;
- uint32_t u32;
- int32_t s32;
- uint64_t u64;
- int64_t s64;
- double d64;
- const char *string;
- int i;
- } basic;
-
- r = sd_bus_message_peek_type(m, &type, &contents);
- if (r <= 0)
- return r;
-
- if (bus_type_is_container(type) > 0) {
-
- r = sd_bus_message_enter_container(m, type, contents);
- if (r < 0)
- return r;
-
- if (type == SD_BUS_TYPE_ARRAY) {
- unsigned n = 0;
-
- /* count array entries */
- for (;;) {
-
- r = sd_bus_message_skip(m, contents);
- if (r < 0)
- return r;
- if (r == 0)
- break;
-
- n++;
- }
-
- r = sd_bus_message_rewind(m, false);
- if (r < 0)
- return r;
-
- if (needs_space)
- fputc(' ', f);
-
- fprintf(f, "%u", n);
- } else if (type == SD_BUS_TYPE_VARIANT) {
-
- if (needs_space)
- fputc(' ', f);
-
- fprintf(f, "%s", contents);
- }
-
- r = format_cmdline(m, f, needs_space || IN_SET(type, SD_BUS_TYPE_ARRAY, SD_BUS_TYPE_VARIANT));
- if (r < 0)
- return r;
-
- r = sd_bus_message_exit_container(m);
- if (r < 0)
- return r;
-
- continue;
- }
-
- r = sd_bus_message_read_basic(m, type, &basic);
- if (r < 0)
- return r;
-
- if (needs_space)
- fputc(' ', f);
-
- switch (type) {
- case SD_BUS_TYPE_BYTE:
- fprintf(f, "%u", basic.u8);
- break;
-
- case SD_BUS_TYPE_BOOLEAN:
- fputs(true_false(basic.i), f);
- break;
-
- case SD_BUS_TYPE_INT16:
- fprintf(f, "%i", basic.s16);
- break;
-
- case SD_BUS_TYPE_UINT16:
- fprintf(f, "%u", basic.u16);
- break;
-
- case SD_BUS_TYPE_INT32:
- fprintf(f, "%i", basic.s32);
- break;
-
- case SD_BUS_TYPE_UINT32:
- fprintf(f, "%u", basic.u32);
- break;
-
- case SD_BUS_TYPE_INT64:
- fprintf(f, "%" PRIi64, basic.s64);
- break;
-
- case SD_BUS_TYPE_UINT64:
- fprintf(f, "%" PRIu64, basic.u64);
- break;
-
- case SD_BUS_TYPE_DOUBLE:
- fprintf(f, "%g", basic.d64);
- break;
-
- case SD_BUS_TYPE_STRING:
- case SD_BUS_TYPE_OBJECT_PATH:
- case SD_BUS_TYPE_SIGNATURE: {
- _cleanup_free_ char *b = NULL;
-
- b = cescape(basic.string);
- if (!b)
- return -ENOMEM;
-
- fprintf(f, "\"%s\"", b);
- break;
- }
-
- case SD_BUS_TYPE_UNIX_FD:
- fprintf(f, "%i", basic.i);
- break;
-
- default:
- assert_not_reached("Unknown basic type.");
- }
-
- needs_space = true;
- }
-}
-
-typedef struct Member {
- const char *type;
- char *interface;
- char *name;
- char *signature;
- char *result;
- char *value;
- bool writable;
- uint64_t flags;
-} Member;
-
-static unsigned long member_hash_func(const void *p, const uint8_t hash_key[]) {
- const Member *m = p;
- unsigned long ul;
-
- assert(m);
- assert(m->type);
-
- ul = string_hash_func(m->type, hash_key);
-
- if (m->name)
- ul ^= string_hash_func(m->name, hash_key);
-
- if (m->interface)
- ul ^= string_hash_func(m->interface, hash_key);
-
- return ul;
-}
-
-static int member_compare_func(const void *a, const void *b) {
- const Member *x = a, *y = b;
- int d;
-
- assert(x);
- assert(y);
- assert(x->type);
- assert(y->type);
-
- if (!x->interface && y->interface)
- return -1;
- if (x->interface && !y->interface)
- return 1;
- if (x->interface && y->interface) {
- d = strcmp(x->interface, y->interface);
- if (d != 0)
- return d;
- }
-
- d = strcmp(x->type, y->type);
- if (d != 0)
- return d;
-
- if (!x->name && y->name)
- return -1;
- if (x->name && !y->name)
- return 1;
- if (x->name && y->name)
- return strcmp(x->name, y->name);
-
- return 0;
-}
-
-static int member_compare_funcp(const void *a, const void *b) {
- const Member *const * x = (const Member *const *) a, * const *y = (const Member *const *) b;
-
- return member_compare_func(*x, *y);
-}
-
-static void member_free(Member *m) {
- if (!m)
- return;
-
- free(m->interface);
- free(m->name);
- free(m->signature);
- free(m->result);
- free(m->value);
- free(m);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(Member*, member_free);
-
-static void member_set_free(Set *s) {
- Member *m;
-
- while ((m = set_steal_first(s)))
- member_free(m);
-
- set_free(s);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, member_set_free);
-
-static int on_interface(const char *interface, uint64_t flags, void *userdata) {
- _cleanup_(member_freep) Member *m;
- Set *members = userdata;
- int r;
-
- assert(interface);
- assert(members);
-
- m = new0(Member, 1);
- if (!m)
- return log_oom();
-
- m->type = "interface";
- m->flags = flags;
-
- r = free_and_strdup(&m->interface, interface);
- if (r < 0)
- return log_oom();
-
- r = set_put(members, m);
- if (r <= 0) {
- log_error("Duplicate interface");
- return -EINVAL;
- }
-
- m = NULL;
- return 0;
-}
-
-static int on_method(const char *interface, const char *name, const char *signature, const char *result, uint64_t flags, void *userdata) {
- _cleanup_(member_freep) Member *m;
- Set *members = userdata;
- int r;
-
- assert(interface);
- assert(name);
-
- m = new0(Member, 1);
- if (!m)
- return log_oom();
-
- m->type = "method";
- m->flags = flags;
-
- r = free_and_strdup(&m->interface, interface);
- if (r < 0)
- return log_oom();
-
- r = free_and_strdup(&m->name, name);
- if (r < 0)
- return log_oom();
-
- r = free_and_strdup(&m->signature, signature);
- if (r < 0)
- return log_oom();
-
- r = free_and_strdup(&m->result, result);
- if (r < 0)
- return log_oom();
-
- r = set_put(members, m);
- if (r <= 0) {
- log_error("Duplicate method");
- return -EINVAL;
- }
-
- m = NULL;
- return 0;
-}
-
-static int on_signal(const char *interface, const char *name, const char *signature, uint64_t flags, void *userdata) {
- _cleanup_(member_freep) Member *m;
- Set *members = userdata;
- int r;
-
- assert(interface);
- assert(name);
-
- m = new0(Member, 1);
- if (!m)
- return log_oom();
-
- m->type = "signal";
- m->flags = flags;
-
- r = free_and_strdup(&m->interface, interface);
- if (r < 0)
- return log_oom();
-
- r = free_and_strdup(&m->name, name);
- if (r < 0)
- return log_oom();
-
- r = free_and_strdup(&m->signature, signature);
- if (r < 0)
- return log_oom();
-
- r = set_put(members, m);
- if (r <= 0) {
- log_error("Duplicate signal");
- return -EINVAL;
- }
-
- m = NULL;
- return 0;
-}
-
-static int on_property(const char *interface, const char *name, const char *signature, bool writable, uint64_t flags, void *userdata) {
- _cleanup_(member_freep) Member *m;
- Set *members = userdata;
- int r;
-
- assert(interface);
- assert(name);
-
- m = new0(Member, 1);
- if (!m)
- return log_oom();
-
- m->type = "property";
- m->flags = flags;
- m->writable = writable;
-
- r = free_and_strdup(&m->interface, interface);
- if (r < 0)
- return log_oom();
-
- r = free_and_strdup(&m->name, name);
- if (r < 0)
- return log_oom();
-
- r = free_and_strdup(&m->signature, signature);
- if (r < 0)
- return log_oom();
-
- r = set_put(members, m);
- if (r <= 0) {
- log_error("Duplicate property");
- return -EINVAL;
- }
-
- m = NULL;
- return 0;
-}
-
-static const char *strdash(const char *x) {
- return isempty(x) ? "-" : x;
-}
-
-static int introspect(sd_bus *bus, char **argv) {
- static const struct hash_ops member_hash_ops = {
- .hash = member_hash_func,
- .compare = member_compare_func,
- };
-
- static const XMLIntrospectOps ops = {
- .on_interface = on_interface,
- .on_method = on_method,
- .on_signal = on_signal,
- .on_property = on_property,
- };
-
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_(member_set_freep) Set *members = NULL;
- Iterator i;
- Member *m;
- const char *xml;
- int r;
- unsigned name_width, type_width, signature_width, result_width;
- Member **sorted = NULL;
- unsigned k = 0, j, n_args;
-
- n_args = strv_length(argv);
- if (n_args < 3) {
- log_error("Requires service and object path argument.");
- return -EINVAL;
- }
-
- if (n_args > 4) {
- log_error("Too many arguments.");
- return -EINVAL;
- }
-
- members = set_new(&member_hash_ops);
- if (!members)
- return log_oom();
-
- r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
- if (r < 0) {
- log_error("Failed to introspect object %s of service %s: %s", argv[2], argv[1], bus_error_message(&error, r));
- return r;
- }
-
- r = sd_bus_message_read(reply, "s", &xml);
- if (r < 0)
- return bus_log_parse_error(r);
-
- /* First, get list of all properties */
- r = parse_xml_introspect(argv[2], xml, &ops, members);
- if (r < 0)
- return r;
-
- /* Second, find the current values for them */
- SET_FOREACH(m, members, i) {
-
- if (!streq(m->type, "property"))
- continue;
-
- if (m->value)
- continue;
-
- if (argv[3] && !streq(argv[3], m->interface))
- continue;
-
- r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", m->interface);
- if (r < 0) {
- log_error("%s", bus_error_message(&error, r));
- return r;
- }
-
- r = sd_bus_message_enter_container(reply, 'a', "{sv}");
- if (r < 0)
- return bus_log_parse_error(r);
-
- for (;;) {
- Member *z;
- _cleanup_free_ char *buf = NULL;
- _cleanup_fclose_ FILE *mf = NULL;
- size_t sz = 0;
- const char *name;
-
- r = sd_bus_message_enter_container(reply, 'e', "sv");
- if (r < 0)
- return bus_log_parse_error(r);
-
- if (r == 0)
- break;
-
- r = sd_bus_message_read(reply, "s", &name);
- if (r < 0)
- return bus_log_parse_error(r);
-
- r = sd_bus_message_enter_container(reply, 'v', NULL);
- if (r < 0)
- return bus_log_parse_error(r);
-
- mf = open_memstream(&buf, &sz);
- if (!mf)
- return log_oom();
-
- r = format_cmdline(reply, mf, false);
- if (r < 0)
- return bus_log_parse_error(r);
-
- fclose(mf);
- mf = NULL;
-
- z = set_get(members, &((Member) {
- .type = "property",
- .interface = m->interface,
- .name = (char*) name }));
- if (z) {
- free(z->value);
- z->value = buf;
- buf = NULL;
- }
-
- r = sd_bus_message_exit_container(reply);
- if (r < 0)
- return bus_log_parse_error(r);
-
- r = sd_bus_message_exit_container(reply);
- if (r < 0)
- return bus_log_parse_error(r);
- }
-
- r = sd_bus_message_exit_container(reply);
- if (r < 0)
- return bus_log_parse_error(r);
- }
-
- pager_open_if_enabled();
-
- name_width = strlen("NAME");
- type_width = strlen("TYPE");
- signature_width = strlen("SIGNATURE");
- result_width = strlen("RESULT/VALUE");
-
- sorted = newa(Member*, set_size(members));
-
- SET_FOREACH(m, members, i) {
-
- if (argv[3] && !streq(argv[3], m->interface))
- continue;
-
- if (m->interface)
- name_width = MAX(name_width, strlen(m->interface));
- if (m->name)
- name_width = MAX(name_width, strlen(m->name) + 1);
- if (m->type)
- type_width = MAX(type_width, strlen(m->type));
- if (m->signature)
- signature_width = MAX(signature_width, strlen(m->signature));
- if (m->result)
- result_width = MAX(result_width, strlen(m->result));
- if (m->value)
- result_width = MAX(result_width, strlen(m->value));
-
- sorted[k++] = m;
- }
-
- if (result_width > 40)
- result_width = 40;
-
- qsort(sorted, k, sizeof(Member*), member_compare_funcp);
-
- if (arg_legend) {
- printf("%-*s %-*s %-*s %-*s %s\n",
- (int) name_width, "NAME",
- (int) type_width, "TYPE",
- (int) signature_width, "SIGNATURE",
- (int) result_width, "RESULT/VALUE",
- "FLAGS");
- }
-
- for (j = 0; j < k; j++) {
- _cleanup_free_ char *ellipsized = NULL;
- const char *rv;
- bool is_interface;
-
- m = sorted[j];
-
- if (argv[3] && !streq(argv[3], m->interface))
- continue;
-
- is_interface = streq(m->type, "interface");
-
- if (argv[3] && is_interface)
- continue;
-
- if (m->value) {
- ellipsized = ellipsize(m->value, result_width, 100);
- if (!ellipsized)
- return log_oom();
-
- rv = ellipsized;
- } else
- rv = strdash(m->result);
-
- printf("%s%s%-*s%s %-*s %-*s %-*s%s%s%s%s%s%s\n",
- is_interface ? ansi_highlight() : "",
- is_interface ? "" : ".",
- - !is_interface + (int) name_width, strdash(streq_ptr(m->type, "interface") ? m->interface : m->name),
- is_interface ? ansi_highlight_off() : "",
- (int) type_width, strdash(m->type),
- (int) signature_width, strdash(m->signature),
- (int) result_width, rv,
- (m->flags & SD_BUS_VTABLE_DEPRECATED) ? " deprecated" : (m->flags || m->writable ? "" : " -"),
- (m->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ? " no-reply" : "",
- (m->flags & SD_BUS_VTABLE_PROPERTY_CONST) ? " const" : "",
- (m->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) ? " emits-change" : "",
- (m->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) ? " emits-invalidation" : "",
- m->writable ? " writable" : "");
- }
-
- return 0;
-}
-
-static int message_dump(sd_bus_message *m, FILE *f) {
- return bus_message_dump(m, f, BUS_MESSAGE_DUMP_WITH_HEADER);
-}
-
-static int message_pcap(sd_bus_message *m, FILE *f) {
- return bus_message_pcap_frame(m, arg_snaplen, f);
-}
-
-static int monitor(sd_bus *bus, char *argv[], int (*dump)(sd_bus_message *m, FILE *f)) {
- bool added_something = false;
- char **i;
- int r;
-
- STRV_FOREACH(i, argv+1) {
- _cleanup_free_ char *m = NULL;
-
- if (!service_name_is_valid(*i)) {
- log_error("Invalid service name '%s'", *i);
- return -EINVAL;
- }
-
- m = strjoin("sender='", *i, "'", NULL);
- if (!m)
- return log_oom();
-
- r = sd_bus_add_match(bus, NULL, m, NULL, NULL);
- if (r < 0)
- return log_error_errno(r, "Failed to add match: %m");
-
- added_something = true;
- }
-
- STRV_FOREACH(i, arg_matches) {
- r = sd_bus_add_match(bus, NULL, *i, NULL, NULL);
- if (r < 0)
- return log_error_errno(r, "Failed to add match: %m");
-
- added_something = true;
- }
-
- if (!added_something) {
- r = sd_bus_add_match(bus, NULL, "", NULL, NULL);
- if (r < 0)
- return log_error_errno(r, "Failed to add match: %m");
- }
-
- log_info("Monitoring bus message stream.");
-
- for (;;) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-
- r = sd_bus_process(bus, &m);
- if (r < 0)
- return log_error_errno(r, "Failed to process bus: %m");
-
- if (m) {
- dump(m, stdout);
-
- if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected") > 0) {
- log_info("Connection terminated, exiting.");
- return 0;
- }
-
- continue;
- }
-
- if (r > 0)
- continue;
-
- r = sd_bus_wait(bus, (uint64_t) -1);
- if (r < 0)
- return log_error_errno(r, "Failed to wait for bus: %m");
- }
-}
-
-static int capture(sd_bus *bus, char *argv[]) {
- int r;
-
- if (isatty(fileno(stdout)) > 0) {
- log_error("Refusing to write message data to console, please redirect output to a file.");
- return -EINVAL;
- }
-
- bus_pcap_header(arg_snaplen, stdout);
-
- r = monitor(bus, argv, message_pcap);
- if (r < 0)
- return r;
-
- if (ferror(stdout)) {
- log_error("Couldn't write capture file.");
- return -EIO;
- }
-
- return r;
-}
-
-static int status(sd_bus *bus, char *argv[]) {
- _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
- pid_t pid;
- int r;
-
- assert(bus);
-
- if (strv_length(argv) > 2) {
- log_error("Expects no or one argument.");
- return -EINVAL;
- }
-
- if (argv[1]) {
- r = parse_pid(argv[1], &pid);
- if (r < 0)
- r = sd_bus_get_name_creds(
- bus,
- argv[1],
- (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) | _SD_BUS_CREDS_ALL,
- &creds);
- else
- r = sd_bus_creds_new_from_pid(
- &creds,
- pid,
- _SD_BUS_CREDS_ALL);
- } else {
- const char *scope, *address;
- sd_id128_t bus_id;
-
- r = sd_bus_get_address(bus, &address);
- if (r >= 0)
- printf("BusAddress=%s%s%s\n", ansi_highlight(), address, ansi_highlight_off());
-
- r = sd_bus_get_scope(bus, &scope);
- if (r >= 0)
- printf("BusScope=%s%s%s\n", ansi_highlight(), scope, ansi_highlight_off());
-
- r = sd_bus_get_bus_id(bus, &bus_id);
- if (r >= 0)
- printf("BusID=%s" SD_ID128_FORMAT_STR "%s\n", ansi_highlight(), SD_ID128_FORMAT_VAL(bus_id), ansi_highlight_off());
-
- r = sd_bus_get_owner_creds(
- bus,
- (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) | _SD_BUS_CREDS_ALL,
- &creds);
- }
-
- if (r < 0)
- return log_error_errno(r, "Failed to get credentials: %m");
-
- bus_creds_dump(creds, NULL, false);
- return 0;
-}
-
-static int message_append_cmdline(sd_bus_message *m, const char *signature, char ***x) {
- char **p;
- int r;
-
- assert(m);
- assert(signature);
- assert(x);
-
- p = *x;
-
- for (;;) {
- const char *v;
- char t;
-
- t = *signature;
- v = *p;
-
- if (t == 0)
- break;
- if (!v) {
- log_error("Too few parameters for signature.");
- return -EINVAL;
- }
-
- signature++;
- p++;
-
- switch (t) {
-
- case SD_BUS_TYPE_BOOLEAN:
-
- r = parse_boolean(v);
- if (r < 0) {
- log_error("Failed to parse as boolean: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &r);
- break;
-
- case SD_BUS_TYPE_BYTE: {
- uint8_t z;
-
- r = safe_atou8(v, &z);
- if (r < 0) {
- log_error("Failed to parse as byte (unsigned 8bit integer): %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
- case SD_BUS_TYPE_INT16: {
- int16_t z;
-
- r = safe_atoi16(v, &z);
- if (r < 0) {
- log_error("Failed to parse as signed 16bit integer: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
- case SD_BUS_TYPE_UINT16: {
- uint16_t z;
-
- r = safe_atou16(v, &z);
- if (r < 0) {
- log_error("Failed to parse as unsigned 16bit integer: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
- case SD_BUS_TYPE_INT32: {
- int32_t z;
-
- r = safe_atoi32(v, &z);
- if (r < 0) {
- log_error("Failed to parse as signed 32bit integer: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
- case SD_BUS_TYPE_UINT32: {
- uint32_t z;
-
- r = safe_atou32(v, &z);
- if (r < 0) {
- log_error("Failed to parse as unsigned 32bit integer: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
- case SD_BUS_TYPE_INT64: {
- int64_t z;
-
- r = safe_atoi64(v, &z);
- if (r < 0) {
- log_error("Failed to parse as signed 64bit integer: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
- case SD_BUS_TYPE_UINT64: {
- uint64_t z;
-
- r = safe_atou64(v, &z);
- if (r < 0) {
- log_error("Failed to parse as unsigned 64bit integer: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
-
- case SD_BUS_TYPE_DOUBLE: {
- double z;
-
- r = safe_atod(v, &z);
- if (r < 0) {
- log_error("Failed to parse as double precision floating point: %s", v);
- return r;
- }
-
- r = sd_bus_message_append_basic(m, t, &z);
- break;
- }
-
- case SD_BUS_TYPE_STRING:
- case SD_BUS_TYPE_OBJECT_PATH:
- case SD_BUS_TYPE_SIGNATURE:
-
- r = sd_bus_message_append_basic(m, t, v);
- break;
-
- case SD_BUS_TYPE_ARRAY: {
- uint32_t n;
- size_t k;
-
- r = safe_atou32(v, &n);
- if (r < 0) {
- log_error("Failed to parse number of array entries: %s", v);
- return r;
- }
-
- r = signature_element_length(signature, &k);
- if (r < 0) {
- log_error("Invalid array signature.");
- return r;
- }
-
- {
- unsigned i;
- char s[k + 1];
- memcpy(s, signature, k);
- s[k] = 0;
-
- r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
- if (r < 0)
- return bus_log_create_error(r);
-
- for (i = 0; i < n; i++) {
- r = message_append_cmdline(m, s, &p);
- if (r < 0)
- return r;
- }
- }
-
- signature += k;
-
- r = sd_bus_message_close_container(m);
- break;
- }
-
- case SD_BUS_TYPE_VARIANT:
- r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, v);
- if (r < 0)
- return bus_log_create_error(r);
-
- r = message_append_cmdline(m, v, &p);
- if (r < 0)
- return r;
-
- r = sd_bus_message_close_container(m);
- break;
-
- case SD_BUS_TYPE_STRUCT_BEGIN:
- case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
- size_t k;
-
- signature--;
- p--;
-
- r = signature_element_length(signature, &k);
- if (r < 0) {
- log_error("Invalid struct/dict entry signature.");
- return r;
- }
-
- {
- char s[k-1];
- memcpy(s, signature + 1, k - 2);
- s[k - 2] = 0;
-
- r = sd_bus_message_open_container(m, t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
- if (r < 0)
- return bus_log_create_error(r);
-
- r = message_append_cmdline(m, s, &p);
- if (r < 0)
- return r;
- }
-
- signature += k;
-
- r = sd_bus_message_close_container(m);
- break;
- }
-
- case SD_BUS_TYPE_UNIX_FD:
- log_error("UNIX file descriptor not supported as type.");
- return -EINVAL;
-
- default:
- log_error("Unknown signature type %c.", t);
- return -EINVAL;
- }
-
- if (r < 0)
- return bus_log_create_error(r);
- }
-
- *x = p;
- return 0;
-}
-
-static int call(sd_bus *bus, char *argv[]) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
- int r;
-
- assert(bus);
-
- if (strv_length(argv) < 5) {
- log_error("Expects at least four arguments.");
- return -EINVAL;
- }
-
- r = sd_bus_message_new_method_call(bus, &m, argv[1], argv[2], argv[3], argv[4]);
- if (r < 0)
- return bus_log_create_error(r);
-
- r = sd_bus_message_set_expect_reply(m, arg_expect_reply);
- if (r < 0)
- return bus_log_create_error(r);
-
- r = sd_bus_message_set_auto_start(m, arg_auto_start);
- if (r < 0)
- return bus_log_create_error(r);
-
- r = sd_bus_message_set_allow_interactive_authorization(m, arg_allow_interactive_authorization);
- if (r < 0)
- return bus_log_create_error(r);
-
- if (!isempty(argv[5])) {
- char **p;
-
- p = argv+6;
-
- r = message_append_cmdline(m, argv[5], &p);
- if (r < 0)
- return r;
-
- if (*p) {
- log_error("Too many parameters for signature.");
- return -EINVAL;
- }
- }
-
- if (!arg_expect_reply) {
- r = sd_bus_send(bus, m, NULL);
- if (r < 0) {
- log_error("Failed to send message.");
- return r;
- }
-
- return 0;
- }
-
- r = sd_bus_call(bus, m, arg_timeout, &error, &reply);
- if (r < 0) {
- log_error("%s", bus_error_message(&error, r));
- return r;
- }
-
- r = sd_bus_message_is_empty(reply);
- if (r < 0)
- return bus_log_parse_error(r);
-
- if (r == 0 && !arg_quiet) {
-
- if (arg_verbose) {
- pager_open_if_enabled();
-
- r = bus_message_dump(reply, stdout, 0);
- if (r < 0)
- return r;
- } else {
-
- fputs(sd_bus_message_get_signature(reply, true), stdout);
- fputc(' ', stdout);
-
- r = format_cmdline(reply, stdout, false);
- if (r < 0)
- return bus_log_parse_error(r);
-
- fputc('\n', stdout);
- }
- }
-
- return 0;
-}
-
-static int get_property(sd_bus *bus, char *argv[]) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- unsigned n;
- char **i;
- int r;
-
- assert(bus);
-
- n = strv_length(argv);
- if (n < 5) {
- log_error("Expects at least four arguments.");
- return -EINVAL;
- }
-
- STRV_FOREACH(i, argv + 4) {
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- const char *contents = NULL;
- char type;
-
- r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "Get", &error, &reply, "ss", argv[3], *i);
- if (r < 0) {
- log_error("%s", bus_error_message(&error, r));
- return r;
- }
-
- r = sd_bus_message_peek_type(reply, &type, &contents);
- if (r < 0)
- return bus_log_parse_error(r);
-
- r = sd_bus_message_enter_container(reply, 'v', contents);
- if (r < 0)
- return bus_log_parse_error(r);
-
- if (arg_verbose) {
- pager_open_if_enabled();
-
- r = bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_SUBTREE_ONLY);
- if (r < 0)
- return r;
- } else {
- fputs(contents, stdout);
- fputc(' ', stdout);
-
- r = format_cmdline(reply, stdout, false);
- if (r < 0)
- return bus_log_parse_error(r);
-
- fputc('\n', stdout);
- }
-
- r = sd_bus_message_exit_container(reply);
- if (r < 0)
- return bus_log_parse_error(r);
- }
-
- return 0;
-}
-
-static int set_property(sd_bus *bus, char *argv[]) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- unsigned n;
- char **p;
- int r;
-
- assert(bus);
-
- n = strv_length(argv);
- if (n < 6) {
- log_error("Expects at least five arguments.");
- return -EINVAL;
- }
-
- r = sd_bus_message_new_method_call(bus, &m, argv[1], argv[2], "org.freedesktop.DBus.Properties", "Set");
- if (r < 0)
- return bus_log_create_error(r);
-
- r = sd_bus_message_append(m, "ss", argv[3], argv[4]);
- if (r < 0)
- return bus_log_create_error(r);
-
- r = sd_bus_message_open_container(m, 'v', argv[5]);
- if (r < 0)
- return bus_log_create_error(r);
-
- p = argv+6;
- r = message_append_cmdline(m, argv[5], &p);
- if (r < 0)
- return r;
-
- r = sd_bus_message_close_container(m);
- if (r < 0)
- return bus_log_create_error(r);
-
- if (*p) {
- log_error("Too many parameters for signature.");
- return -EINVAL;
- }
-
- r = sd_bus_call(bus, m, arg_timeout, &error, NULL);
- if (r < 0) {
- log_error("%s", bus_error_message(&error, r));
- return r;
- }
-
- return 0;
-}
-
-static int help(void) {
- printf("%s [OPTIONS...] {COMMAND} ...\n\n"
- "Introspect the bus.\n\n"
- " -h --help Show this help\n"
- " --version Show package version\n"
- " --no-pager Do not pipe output into a pager\n"
- " --no-legend Do not show the headers and footers\n"
- " --system Connect to system bus\n"
- " --user Connect to user bus\n"
- " -H --host=[USER@]HOST Operate on remote host\n"
- " -M --machine=CONTAINER Operate on local container\n"
- " --address=ADDRESS Connect to bus specified by address\n"
- " --show-machine Show machine ID column in list\n"
- " --unique Only show unique names\n"
- " --acquired Only show acquired names\n"
- " --activatable Only show activatable names\n"
- " --match=MATCH Only show matching messages\n"
- " --list Don't show tree, but simple object path list\n"
- " --quiet Don't show method call reply\n"
- " --verbose Show result values in long format\n"
- " --expect-reply=BOOL Expect a method call reply\n"
- " --auto-start=BOOL Auto-start destination service\n"
- " --allow-interactive-authorization=BOOL\n"
- " Allow interactive authorization for operation\n"
- " --timeout=SECS Maximum time to wait for method call completion\n"
- " --augment-creds=BOOL Extend credential data with data read from /proc/$PID\n\n"
- "Commands:\n"
- " list List bus names\n"
- " status [SERVICE] Show bus service, process or bus owner credentials\n"
- " monitor [SERVICE...] Show bus traffic\n"
- " capture [SERVICE...] Capture bus traffic as pcap\n"
- " tree [SERVICE...] Show object tree of service\n"
- " introspect SERVICE OBJECT [INTERFACE]\n"
- " call SERVICE OBJECT INTERFACE METHOD [SIGNATURE [ARGUMENT...]]\n"
- " Call a method\n"
- " get-property SERVICE OBJECT INTERFACE PROPERTY...\n"
- " Get property value\n"
- " set-property SERVICE OBJECT INTERFACE PROPERTY SIGNATURE ARGUMENT...\n"
- " Set property value\n"
- " help Show this help\n"
- , program_invocation_short_name);
-
- return 0;
-}
-
-static int parse_argv(int argc, char *argv[]) {
-
- enum {
- ARG_VERSION = 0x100,
- ARG_NO_PAGER,
- ARG_NO_LEGEND,
- ARG_SYSTEM,
- ARG_USER,
- ARG_ADDRESS,
- ARG_MATCH,
- ARG_SHOW_MACHINE,
- ARG_UNIQUE,
- ARG_ACQUIRED,
- ARG_ACTIVATABLE,
- ARG_SIZE,
- ARG_LIST,
- ARG_VERBOSE,
- ARG_EXPECT_REPLY,
- ARG_AUTO_START,
- ARG_ALLOW_INTERACTIVE_AUTHORIZATION,
- ARG_TIMEOUT,
- ARG_AUGMENT_CREDS,
- };
-
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, ARG_VERSION },
- { "no-pager", no_argument, NULL, ARG_NO_PAGER },
- { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
- { "system", no_argument, NULL, ARG_SYSTEM },
- { "user", no_argument, NULL, ARG_USER },
- { "address", required_argument, NULL, ARG_ADDRESS },
- { "show-machine", no_argument, NULL, ARG_SHOW_MACHINE },
- { "unique", no_argument, NULL, ARG_UNIQUE },
- { "acquired", no_argument, NULL, ARG_ACQUIRED },
- { "activatable", no_argument, NULL, ARG_ACTIVATABLE },
- { "match", required_argument, NULL, ARG_MATCH },
- { "host", required_argument, NULL, 'H' },
- { "machine", required_argument, NULL, 'M' },
- { "size", required_argument, NULL, ARG_SIZE },
- { "list", no_argument, NULL, ARG_LIST },
- { "quiet", no_argument, NULL, 'q' },
- { "verbose", no_argument, NULL, ARG_VERBOSE },
- { "expect-reply", required_argument, NULL, ARG_EXPECT_REPLY },
- { "auto-start", required_argument, NULL, ARG_AUTO_START },
- { "allow-interactive-authorization", required_argument, NULL, ARG_ALLOW_INTERACTIVE_AUTHORIZATION },
- { "timeout", required_argument, NULL, ARG_TIMEOUT },
- { "augment-creds",required_argument, NULL, ARG_AUGMENT_CREDS},
- {},
- };
-
- int c, r;
-
- assert(argc >= 0);
- assert(argv);
-
- while ((c = getopt_long(argc, argv, "hH:M:q", options, NULL)) >= 0)
-
- switch (c) {
-
- case 'h':
- return help();
-
- case ARG_VERSION:
- puts(PACKAGE_STRING);
- puts(SYSTEMD_FEATURES);
- return 0;
-
- case ARG_NO_PAGER:
- arg_no_pager = true;
- break;
-
- case ARG_NO_LEGEND:
- arg_legend = false;
- break;
-
- case ARG_USER:
- arg_user = true;
- break;
-
- case ARG_SYSTEM:
- arg_user = false;
- break;
-
- case ARG_ADDRESS:
- arg_address = optarg;
- break;
-
- case ARG_SHOW_MACHINE:
- arg_show_machine = true;
- break;
-
- case ARG_UNIQUE:
- arg_unique = true;
- break;
-
- case ARG_ACQUIRED:
- arg_acquired = true;
- break;
-
- case ARG_ACTIVATABLE:
- arg_activatable = true;
- break;
-
- case ARG_MATCH:
- if (strv_extend(&arg_matches, optarg) < 0)
- return log_oom();
- break;
-
- case ARG_SIZE: {
- off_t o;
-
- r = parse_size(optarg, 0, &o);
- if (r < 0) {
- log_error("Failed to parse size: %s", optarg);
- return r;
- }
-
- if ((off_t) (size_t) o != o) {
- log_error("Size out of range.");
- return -E2BIG;
- }
-
- arg_snaplen = (size_t) o;
- break;
- }
-
- case ARG_LIST:
- arg_list = true;
- break;
-
- case 'H':
- arg_transport = BUS_TRANSPORT_REMOTE;
- arg_host = optarg;
- break;
-
- case 'M':
- arg_transport = BUS_TRANSPORT_MACHINE;
- arg_host = optarg;
- break;
-
- case 'q':
- arg_quiet = true;
- break;
-
- case ARG_VERBOSE:
- arg_verbose = true;
- break;
-
- case ARG_EXPECT_REPLY:
- r = parse_boolean(optarg);
- if (r < 0) {
- log_error("Failed to parse --expect-reply= parameter.");
- return r;
- }
-
- arg_expect_reply = !!r;
- break;
-
-
- case ARG_AUTO_START:
- r = parse_boolean(optarg);
- if (r < 0) {
- log_error("Failed to parse --auto-start= parameter.");
- return r;
- }
-
- arg_auto_start = !!r;
- break;
-
-
- case ARG_ALLOW_INTERACTIVE_AUTHORIZATION:
- r = parse_boolean(optarg);
- if (r < 0) {
- log_error("Failed to parse --allow-interactive-authorization= parameter.");
- return r;
- }
-
- arg_allow_interactive_authorization = !!r;
- break;
-
- case ARG_TIMEOUT:
- r = parse_sec(optarg, &arg_timeout);
- if (r < 0) {
- log_error("Failed to parse --timeout= parameter.");
- return r;
- }
-
- break;
-
- case ARG_AUGMENT_CREDS:
- r = parse_boolean(optarg);
- if (r < 0) {
- log_error("Failed to parse --augment-creds= parameter.");
- return r;
- }
-
- arg_augment_creds = !!r;
- break;
-
- case '?':
- return -EINVAL;
-
- default:
- assert_not_reached("Unhandled option");
- }
-
- return 1;
-}
-
-static int busctl_main(sd_bus *bus, int argc, char *argv[]) {
- assert(bus);
-
- if (optind >= argc ||
- streq(argv[optind], "list"))
- return list_bus_names(bus, argv + optind);
-
- if (streq(argv[optind], "monitor"))
- return monitor(bus, argv + optind, message_dump);
-
- if (streq(argv[optind], "capture"))
- return capture(bus, argv + optind);
-
- if (streq(argv[optind], "status"))
- return status(bus, argv + optind);
-
- if (streq(argv[optind], "tree"))
- return tree(bus, argv + optind);
-
- if (streq(argv[optind], "introspect"))
- return introspect(bus, argv + optind);
-
- if (streq(argv[optind], "call"))
- return call(bus, argv + optind);
-
- if (streq(argv[optind], "get-property"))
- return get_property(bus, argv + optind);
-
- if (streq(argv[optind], "set-property"))
- return set_property(bus, argv + optind);
-
- if (streq(argv[optind], "help"))
- return help();
-
- log_error("Unknown command '%s'", argv[optind]);
- return -EINVAL;
-}
-
-int main(int argc, char *argv[]) {
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
- int r;
-
- log_parse_environment();
- log_open();
-
- r = parse_argv(argc, argv);
- if (r <= 0)
- goto finish;
-
- r = sd_bus_new(&bus);
- if (r < 0) {
- log_error_errno(r, "Failed to allocate bus: %m");
- goto finish;
- }
-
- if (streq_ptr(argv[optind], "monitor") ||
- streq_ptr(argv[optind], "capture")) {
-
- r = sd_bus_set_monitor(bus, true);
- if (r < 0) {
- log_error_errno(r, "Failed to set monitor mode: %m");
- goto finish;
- }
-
- r = sd_bus_negotiate_creds(bus, true, _SD_BUS_CREDS_ALL);
- if (r < 0) {
- log_error_errno(r, "Failed to enable credentials: %m");
- goto finish;
- }
-
- r = sd_bus_negotiate_timestamp(bus, true);
- if (r < 0) {
- log_error_errno(r, "Failed to enable timestamps: %m");
- goto finish;
- }
-
- r = sd_bus_negotiate_fds(bus, true);
- if (r < 0) {
- log_error_errno(r, "Failed to enable fds: %m");
- goto finish;
- }
- }
-
- if (arg_address)
- r = sd_bus_set_address(bus, arg_address);
- else {
- r = sd_bus_set_bus_client(bus, true);
- if (r < 0) {
- log_error_errno(r, "Failed to set bus client: %m");
- goto finish;
- }
-
- switch (arg_transport) {
-
- case BUS_TRANSPORT_LOCAL:
- if (arg_user) {
- bus->is_user = true;
- r = bus_set_address_user(bus);
- } else {
- bus->is_system = true;
- r = bus_set_address_system(bus);
- }
- break;
-
- case BUS_TRANSPORT_REMOTE:
- r = bus_set_address_system_remote(bus, arg_host);
- break;
-
- case BUS_TRANSPORT_MACHINE:
- r = bus_set_address_system_machine(bus, arg_host);
- break;
-
- default:
- assert_not_reached("Hmm, unknown transport type.");
- }
- }
- if (r < 0) {
- log_error_errno(r, "Failed to set address: %m");
- goto finish;
- }
-
- r = sd_bus_start(bus);
- if (r < 0) {
- log_error_errno(r, "Failed to connect to bus: %m");
- goto finish;
- }
-
- r = busctl_main(bus, argc, argv);
-
-finish:
- pager_close();
-
- strv_free(arg_matches);
-
- return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
-}
* your option) any later version.
*/
-#ifndef _KDBUS_UAPI_H_
-#define _KDBUS_UAPI_H_
+#ifndef _UAPI_KDBUS_H_
+#define _UAPI_KDBUS_H_
#include <linux/ioctl.h>
#include <linux/types.h>
KDBUS_ITEM_ATTACH_FLAGS_RECV,
KDBUS_ITEM_ID,
KDBUS_ITEM_NAME,
+ KDBUS_ITEM_DST_ID,
/* keep these item types in sync with KDBUS_ATTACH_* flags */
_KDBUS_ITEM_ATTACH_BASE = 0x1000,
* reply to this message. The
* KDBUS_CMD_SEND ioctl() will block
* until the reply is received, and
- * offset_reply in struct kdbus_msg will
+ * reply in struct kdbus_cmd_send will
* yield the offset in the sender's pool
* where the reply can be found.
* This flag is only valid if
struct kdbus_cmd_match),
};
-#endif /* _KDBUS_UAPI_H_ */
+#endif /* _UAPI_KDBUS_H_ */
#include "missing.h"
#include "def.h"
#include "cgroup-util.h"
+#include "hostname-util.h"
#include "bus-label.h"
#include "sd-bus.h"
#include "bus-track.h"
#include "bus-slot.h"
+#define log_debug_bus_message(m) \
+ do { \
+ sd_bus_message *_mm = (m); \
+ log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s", \
+ bus_message_type_to_string(_mm->header->type), \
+ strna(sd_bus_message_get_sender(_mm)), \
+ strna(sd_bus_message_get_destination(_mm)), \
+ strna(sd_bus_message_get_path(_mm)), \
+ strna(sd_bus_message_get_interface(_mm)), \
+ strna(sd_bus_message_get_member(_mm)), \
+ BUS_MESSAGE_COOKIE(_mm), \
+ _mm->reply_cookie, \
+ strna(_mm->error.message)); \
+ } while (false)
+
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
static int attach_io_events(sd_bus *b);
static void detach_io_events(sd_bus *b);
detach_io_events(b);
- if (b->input_fd >= 0)
- safe_close(b->input_fd);
-
- if (b->output_fd >= 0 && b->output_fd != b->input_fd)
+ if (b->input_fd != b->output_fd)
safe_close(b->output_fd);
-
- b->input_fd = b->output_fd = -1;
+ b->output_fd = b->input_fd = safe_close(b->input_fd);
}
static void bus_reset_queues(sd_bus *b) {
while (b->rqueue_size > 0)
sd_bus_message_unref(b->rqueue[--b->rqueue_size]);
- free(b->rqueue);
- b->rqueue = NULL;
+ b->rqueue = mfree(b->rqueue);
b->rqueue_allocated = 0;
while (b->wqueue_size > 0)
sd_bus_message_unref(b->wqueue[--b->wqueue_size]);
- free(b->wqueue);
- b->wqueue = NULL;
+ b->wqueue = mfree(b->wqueue);
b->wqueue_allocated = 0;
}
if (b->kdbus_buffer)
munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
+ free(b->label);
free(b->rbuffer);
free(b->unique_name);
free(b->auth_buffer);
_public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
assert_return(bus, -EINVAL);
assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(input_fd >= 0, -EINVAL);
- assert_return(output_fd >= 0, -EINVAL);
+ assert_return(input_fd >= 0, -EBADF);
+ assert_return(output_fd >= 0, -EBADF);
assert_return(!bus_pid_changed(bus), -ECHILD);
bus->input_fd = input_fd;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
char *p, **a;
return 0;
}
+#endif // 0
_public_ int sd_bus_negotiate_creds(sd_bus *bus, int b, uint64_t mask) {
uint64_t new_flags;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
assert_return(bus, -EINVAL);
assert_return(bus->state == BUS_UNSET, -EPERM);
return free_and_strdup(&bus->description, description);
}
+#endif // 0
_public_ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) {
assert_return(bus, -EINVAL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_allow_interactive_authorization(sd_bus *bus) {
assert_return(bus, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
return bus->allow_interactive_authorization;
}
+#endif // 0
-static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
+static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
const char *s;
+ sd_bus *bus;
int r;
+ assert(reply);
+ bus = reply->bus;
assert(bus);
assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
- assert(reply);
r = sd_bus_message_get_errno(reply);
if (r > 0)
b->machine = machine;
machine = NULL;
} else {
- free(b->machine);
- b->machine = NULL;
+ b->machine = mfree(b->machine);
}
if (pid) {
b->machine = machine;
machine = NULL;
} else {
- free(b->machine);
- b->machine = NULL;
+ b->machine = mfree(b->machine);
}
if (pid) {
} else
b->nspid = 0;
- free(b->kernel);
- b->kernel = strdup("/sys/fs/kdbus/0-system/bus");
- if (!b->kernel)
- return -ENOMEM;
+ r = free_and_strdup(&b->kernel, "/sys/fs/kdbus/0-system/bus");
+ if (r < 0)
+ return r;
return 0;
}
zero(b->sockaddr);
b->sockaddr_size = 0;
- strv_free(b->exec_argv);
- free(b->exec_path);
- b->exec_path = NULL;
- b->exec_argv = NULL;
+ b->exec_argv = strv_free(b->exec_argv);
+ b->exec_path = mfree(b->exec_path);
b->server_id = SD_ID128_NULL;
- free(b->kernel);
- b->kernel = NULL;
- free(b->machine);
- b->machine = NULL;
+ b->kernel = mfree(b->kernel);
+ b->machine = mfree(b->machine);
b->nspid = 0;
}
}
static int bus_start_address(sd_bus *b) {
+ bool container_kdbus_available = false;
+ bool kdbus_available = false;
int r;
assert(b);
bus_close_fds(b);
+ /*
+ * Usually, if you provide multiple different bus-addresses, we
+ * try all of them in order. We use the first one that
+ * succeeds. However, if you mix kernel and unix addresses, we
+ * never try unix-addresses if a previous kernel address was
+ * tried and kdbus was available. This is required to prevent
+ * clients to fallback to the bus-proxy if kdbus is available
+ * but failed (eg., too many connections).
+ */
+
if (b->exec_path)
r = bus_socket_exec(b);
- else if ((b->nspid > 0 || b->machine) && b->kernel)
+
+ else if ((b->nspid > 0 || b->machine) && b->kernel) {
r = bus_container_connect_kernel(b);
- else if ((b->nspid > 0 || b->machine) && b->sockaddr.sa.sa_family != AF_UNSPEC)
- r = bus_container_connect_socket(b);
- else if (b->kernel)
+ if (r < 0 && !IN_SET(r, -ENOENT, -ESOCKTNOSUPPORT))
+ container_kdbus_available = true;
+
+ } else if ((b->nspid > 0 || b->machine) && b->sockaddr.sa.sa_family != AF_UNSPEC) {
+ if (!container_kdbus_available)
+ r = bus_container_connect_socket(b);
+ else
+ skipped = true;
+
+ } else if (b->kernel) {
r = bus_kernel_connect(b);
- else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
- r = bus_socket_connect(b);
- else
+ if (r < 0 && !IN_SET(r, -ENOENT, -ESOCKTNOSUPPORT))
+ kdbus_available = true;
+
+ } else if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
+ if (!kdbus_available)
+ r = bus_socket_connect(b);
+ else
+ skipped = true;
+
+ } else
skipped = true;
if (!skipped) {
if (e) {
if (streq(e, "system"))
return sd_bus_open_system(ret);
+/// elogind does not support systemd units
+#if 0
else if (STR_IN_SET(e, "session", "user"))
return sd_bus_open_user(ret);
+#endif // 0
}
e = secure_getenv("DBUS_STARTER_ADDRESS");
if (!e) {
+/// elogind does not support systemd units
+#if 0
+ if (cg_pid_get_owner_uid(0, NULL) >= 0)
+ return sd_bus_open_user(ret);
+ else
+#endif // 0
return sd_bus_open_system(ret);
}
return r;
}
+/// elogind can not open/use a user bus
+#if 0
int bus_set_address_user(sd_bus *b) {
const char *e;
+ uid_t uid;
+ int r;
assert(b);
if (e)
return sd_bus_set_address(b, e);
+ r = cg_pid_get_owner_uid(0, &uid);
+ if (r < 0)
+ uid = getuid();
+
e = secure_getenv("XDG_RUNTIME_DIR");
if (e) {
_cleanup_free_ char *ee = NULL;
if (!ee)
return -ENOMEM;
-#ifdef ENABLE_KDBUS
- (void) asprintf(&b->address, KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT, getuid(), ee);
-#else
- (void) asprintf(&b->address, UNIX_USER_BUS_ADDRESS_FMT, ee);
-#endif
- } else {
-#ifdef ENABLE_KDBUS
- (void) asprintf(&b->address, KERNEL_USER_BUS_ADDRESS_FMT, getuid());
-#else
- return -ECONNREFUSED;
-#endif
- }
+ (void) asprintf(&b->address, KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT, uid, ee);
+ } else
+ (void) asprintf(&b->address, KERNEL_USER_BUS_ADDRESS_FMT, uid);
if (!b->address)
return -ENOMEM;
return 0;
}
+#endif // 0
_public_ int sd_bus_open_user(sd_bus **ret) {
+/// elogind does not support user buses
+#if 0
sd_bus *b;
int r;
fail:
bus_free(b);
return r;
+#else
+ return sd_bus_open_system(ret);
+#endif // 0
}
int bus_set_address_system_remote(sd_bus *b, const char *host) {
if (!e)
return -ENOMEM;
-#ifdef ENABLE_KDBUS
b->address = strjoin("x-machine-kernel:machine=", e, ";x-machine-unix:machine=", e, NULL);
-#else
- b->address = strjoin("x-machine-unix:machine=", e, NULL);
-#endif
if (!b->address)
return -ENOMEM;
* ioctl on the fd when they are freed. */
}
+_public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) {
+
+ if (!bus)
+ return NULL;
+
+ sd_bus_flush(bus);
+ sd_bus_close(bus);
+
+ return sd_bus_unref(bus);
+}
+
static void bus_enter_closing(sd_bus *bus) {
assert(bus);
return NULL;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_is_open(sd_bus *bus) {
assert_return(bus, -EINVAL);
return BUS_IS_OPEN(bus->state);
}
+#endif // 0
_public_ int sd_bus_can_send(sd_bus *bus, char type) {
int r;
return bus_type_is_valid(type);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
int r;
*id = bus->server_id;
return 0;
}
+#endif // 0
static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
assert(b);
_cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
int r;
- assert_return(bus, -EINVAL);
assert_return(m, -EINVAL);
+
+ if (!bus)
+ bus = m->bus;
+
assert_return(!bus_pid_changed(bus), -ECHILD);
assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS);
_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
int r;
- assert_return(bus, -EINVAL);
assert_return(m, -EINVAL);
+
+ if (!bus)
+ bus = m->bus;
+
assert_return(!bus_pid_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state))
_cleanup_bus_slot_unref_ sd_bus_slot *s = NULL;
int r;
- assert_return(bus, -EINVAL);
assert_return(m, -EINVAL);
assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
assert_return(callback, -EINVAL);
+
+ if (!bus)
+ bus = m->bus;
+
assert_return(!bus_pid_changed(bus), -ECHILD);
assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS);
unsigned i;
int r;
- assert_return(bus, -EINVAL);
- assert_return(m, -EINVAL);
- assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
- assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
- assert_return(!bus_error_is_dirty(error), -EINVAL);
- assert_return(!bus_pid_changed(bus), -ECHILD);
- assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS);
+ bus_assert_return(m, -EINVAL, error);
+ bus_assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL, error);
+ bus_assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL, error);
+ bus_assert_return(!bus_error_is_dirty(error), -EINVAL, error);
- if (!BUS_IS_OPEN(bus->state))
- return -ENOTCONN;
+ if (!bus)
+ bus = m->bus;
+
+ bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
+ bus_assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS, error);
+
+ if (!BUS_IS_OPEN(bus->state)) {
+ r = -ENOTCONN;
+ goto fail;
+ }
r = bus_ensure_running(bus);
if (r < 0)
- return r;
+ goto fail;
i = bus->rqueue_size;
r = bus_seal_message(bus, m, usec);
if (r < 0)
- return r;
+ goto fail;
r = bus_remarshal_message(bus, &m);
if (r < 0)
- return r;
+ goto fail;
r = bus_send_internal(bus, m, &cookie, true);
if (r < 0)
- return r;
+ goto fail;
timeout = calc_elapse(m->timeout);
memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
bus->rqueue_size--;
+ log_debug_bus_message(incoming);
if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
}
r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
+ sd_bus_message_unref(incoming);
+ return r;
- } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
+ } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
r = sd_bus_error_copy(error, &incoming->error);
- else
- r = -EIO;
-
sd_bus_message_unref(incoming);
return r;
+ } else {
+ r = -EIO;
+ goto fail;
+ }
} else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
bus->unique_name &&
* immediately. */
sd_bus_message_unref(incoming);
- return -ELOOP;
+ r = -ELOOP;
+ goto fail;
}
/* Try to read more, right-away */
if (r < 0) {
if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
bus_enter_closing(bus);
- return -ECONNRESET;
+ r = -ECONNRESET;
}
- return r;
+ goto fail;
}
if (r > 0)
continue;
usec_t n;
n = now(CLOCK_MONOTONIC);
- if (n >= timeout)
- return -ETIMEDOUT;
+ if (n >= timeout) {
+ r = -ETIMEDOUT;
+ goto fail;
+ }
left = timeout - n;
} else
r = bus_poll(bus, true, left);
if (r < 0)
- return r;
- if (r == 0)
- return -ETIMEDOUT;
+ goto fail;
+ if (r == 0) {
+ r = -ETIMEDOUT;
+ goto fail;
+ }
r = dispatch_wqueue(bus);
if (r < 0) {
if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
bus_enter_closing(bus);
- return -ECONNRESET;
+ r = -ECONNRESET;
}
- return r;
+ goto fail;
}
}
+
+fail:
+ return sd_bus_error_set_errno(error, r);
}
_public_ int sd_bus_get_fd(sd_bus *bus) {
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_handler = c->callback;
bus->current_userdata = slot->userdata;
- r = c->callback(bus, m, slot->userdata, &error_buffer);
+ r = c->callback(m, slot->userdata, &error_buffer);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = NULL;
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_handler = c->callback;
bus->current_userdata = slot->userdata;
- r = c->callback(bus, m, slot->userdata, &error_buffer);
+ r = c->callback(m, slot->userdata, &error_buffer);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = NULL;
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_handler = l->callback;
bus->current_userdata = slot->userdata;
- r = l->callback(bus, m, slot->userdata, &error_buffer);
+ r = l->callback(m, slot->userdata, &error_buffer);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
bus->current_message = m;
bus->iteration_counter++;
- log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
- bus_message_type_to_string(m->header->type),
- strna(sd_bus_message_get_sender(m)),
- strna(sd_bus_message_get_destination(m)),
- strna(sd_bus_message_get_path(m)),
- strna(sd_bus_message_get_interface(m)),
- strna(sd_bus_message_get_member(m)),
- BUS_MESSAGE_COOKIE(m),
- m->reply_cookie,
- strna(m->error.message));
+ log_debug_bus_message(m);
r = process_hello(bus, m);
if (r != 0)
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_handler = c->callback;
bus->current_userdata = slot->userdata;
- r = c->callback(bus, m, slot->userdata, &error_buffer);
+ r = c->callback(m, slot->userdata, &error_buffer);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = NULL;
return bus_process_internal(bus, false, 0, ret);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
return bus_process_internal(bus, true, priority, ret);
}
+#endif // 0
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
struct pollfd p[2] = {};
s->match_callback.cookie = ++bus->match_cookie;
if (bus->bus_client) {
+ enum bus_match_scope scope;
- if (!bus->is_kernel) {
- /* When this is not a kernel transport, we
- * store the original match string, so that we
- * can use it to remove the match again */
+ scope = bus_match_get_scope(components, n_components);
- s->match_callback.match_string = strdup(match);
- if (!s->match_callback.match_string) {
- r = -ENOMEM;
- goto finish;
+ /* Do not install server-side matches for matches
+ * against the local service, interface or bus
+ * path. */
+ if (scope != BUS_MATCH_LOCAL) {
+
+ if (!bus->is_kernel) {
+ /* When this is not a kernel transport, we
+ * store the original match string, so that we
+ * can use it to remove the match again */
+
+ s->match_callback.match_string = strdup(match);
+ if (!s->match_callback.match_string) {
+ r = -ENOMEM;
+ goto finish;
+ }
}
- }
- r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components, s->match_callback.cookie);
- if (r < 0)
- goto finish;
+ r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components, s->match_callback.cookie);
+ if (r < 0)
+ goto finish;
+
+ s->match_added = true;
+ }
}
bus->match_callbacks_modified = true;
return r;
}
+/// UNNEEDED by elogind
+#if 0
int bus_remove_match_by_string(
sd_bus *bus,
const char *match,
return r;
}
+#endif // 0
bool bus_pid_changed(sd_bus *bus) {
assert(bus);
return bus->current_message;
}
+/// UNNEEDED by elogind
+#if 0
_public_ sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus) {
assert_return(bus, NULL);
return bus->current_slot;
}
+#endif // 0
_public_ sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus) {
assert_return(bus, NULL);
}
_public_ int sd_bus_default_user(sd_bus **ret) {
+/// elogind does not support user buses
+#if 0
static thread_local sd_bus *default_user_bus = NULL;
return bus_default(sd_bus_open_user, &default_user_bus, ret);
+#else
+ return sd_bus_default_system(ret);
+#endif // 0
}
_public_ int sd_bus_default(sd_bus **ret) {
if (e) {
if (streq(e, "system"))
return sd_bus_default_system(ret);
+/// elogind does not support systemd units
+#if 0
else if (STR_IN_SET(e, "user", "session"))
return sd_bus_default_user(ret);
+#endif // 0
}
/* No type is specified, so we have not other option than to
/* Finally, if nothing is set use the cached connection for
* the right scope */
-
+/// elogind does not support systemd units
+#if 0
+ if (cg_pid_get_owner_uid(0, NULL) >= 0)
+ return sd_bus_default_user(ret);
+ else
+#endif // 0
return sd_bus_default_system(ret);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
assert_return(b, -EINVAL);
assert_return(tid, -EINVAL);
*external_id = ret;
return 1;
}
+#endif // 0
_public_ int sd_bus_try_close(sd_bus *bus) {
int r;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_description(sd_bus *bus, const char **description) {
assert_return(bus, -EINVAL);
assert_return(description, -EINVAL);
*description = bus->description;
return 0;
}
+#endif // 0
int bus_get_root_path(sd_bus *bus) {
int r;
return r;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
int r;
return -ENODATA;
}
-int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
+_public_ int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
assert_return(bus, -EINVAL);
assert_return(mask, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
return 0;
}
-int sd_bus_is_bus_client(sd_bus *bus) {
+_public_ int sd_bus_is_bus_client(sd_bus *bus) {
assert_return(bus, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
return bus->bus_client;
}
-int sd_bus_is_server(sd_bus *bus) {
+_public_ int sd_bus_is_server(sd_bus *bus) {
assert_return(bus, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
return bus->is_server;
}
-int sd_bus_is_anonymous(sd_bus *bus) {
+_public_ int sd_bus_is_anonymous(sd_bus *bus) {
assert_return(bus, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
return bus->anonymous_auth;
}
-int sd_bus_is_trusted(sd_bus *bus) {
+_public_ int sd_bus_is_trusted(sd_bus *bus) {
assert_return(bus, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
return bus->trusted;
}
-int sd_bus_is_monitor(sd_bus *bus) {
+_public_ int sd_bus_is_monitor(sd_bus *bus) {
assert_return(bus, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
return !!(bus->hello_flags & KDBUS_HELLO_MONITOR);
}
+#endif // 0
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <pthread.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include "log.h"
-#include "util.h"
-#include "macro.h"
-
-#include "sd-bus.h"
-#include "bus-error.h"
-#include "bus-match.h"
-#include "bus-internal.h"
-#include "bus-util.h"
-
-static int match_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
- log_info("Match triggered! interface=%s member=%s", strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member(m)));
- return 0;
-}
-
-static int object_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
- int r;
-
- assert_se(bus);
-
- if (sd_bus_message_is_method_error(m, NULL))
- return 0;
-
- if (sd_bus_message_is_method_call(m, "org.object.test", "Foobar")) {
- log_info("Invoked Foobar() on %s", sd_bus_message_get_path(m));
-
- r = sd_bus_reply_method_return(m, NULL);
- if (r < 0)
- return log_error_errno(r, "Failed to send reply: %m");
-
- return 1;
- }
-
- return 0;
-}
-
-static int server_init(sd_bus **_bus) {
- sd_bus *bus = NULL;
- sd_id128_t id;
- int r;
- const char *unique;
-
- assert_se(_bus);
-
- r = sd_bus_open_user(&bus);
- if (r < 0) {
- log_error_errno(r, "Failed to connect to user bus: %m");
- goto fail;
- }
-
- r = sd_bus_get_bus_id(bus, &id);
- if (r < 0) {
- log_error_errno(r, "Failed to get server ID: %m");
- goto fail;
- }
-
- r = sd_bus_get_unique_name(bus, &unique);
- if (r < 0) {
- log_error_errno(r, "Failed to get unique name: %m");
- goto fail;
- }
-
- log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id));
- log_info("Unique ID: %s", unique);
- log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h'));
-
- r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0);
- if (r < 0) {
- log_error_errno(r, "Failed to acquire name: %m");
- goto fail;
- }
-
- r = sd_bus_add_fallback(bus, NULL, "/foo/bar", object_callback, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to add object: %m");
- goto fail;
- }
-
- r = sd_bus_add_match(bus, NULL, "type='signal',interface='foo.bar',member='Notify'", match_callback, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to add match: %m");
- goto fail;
- }
-
- r = sd_bus_add_match(bus, NULL, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to add match: %m");
- goto fail;
- }
-
- bus_match_dump(&bus->match_callbacks, 0);
-
- *_bus = bus;
- return 0;
-
-fail:
- if (bus)
- sd_bus_unref(bus);
-
- return r;
-}
-
-static int server(sd_bus *bus) {
- int r;
- bool client1_gone = false, client2_gone = false;
-
- while (!client1_gone || !client2_gone) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- pid_t pid = 0;
- const char *label = NULL;
-
- r = sd_bus_process(bus, &m);
- if (r < 0) {
- log_error_errno(r, "Failed to process requests: %m");
- goto fail;
- }
-
- if (r == 0) {
- r = sd_bus_wait(bus, (uint64_t) -1);
- if (r < 0) {
- log_error_errno(r, "Failed to wait: %m");
- goto fail;
- }
-
- continue;
- }
-
- if (!m)
- continue;
-
- sd_bus_creds_get_pid(sd_bus_message_get_creds(m), &pid);
- sd_bus_creds_get_selinux_context(sd_bus_message_get_creds(m), &label);
- log_info("Got message! member=%s pid="PID_FMT" label=%s",
- strna(sd_bus_message_get_member(m)),
- pid,
- strna(label));
- /* bus_message_dump(m); */
- /* sd_bus_message_rewind(m, true); */
-
- if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "LowerCase")) {
- const char *hello;
- _cleanup_free_ char *lowercase = NULL;
-
- r = sd_bus_message_read(m, "s", &hello);
- if (r < 0) {
- log_error_errno(r, "Failed to get parameter: %m");
- goto fail;
- }
-
- lowercase = strdup(hello);
- if (!lowercase) {
- r = log_oom();
- goto fail;
- }
-
- ascii_strlower(lowercase);
-
- r = sd_bus_reply_method_return(m, "s", lowercase);
- if (r < 0) {
- log_error_errno(r, "Failed to send reply: %m");
- goto fail;
- }
- } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient1")) {
-
- r = sd_bus_reply_method_return(m, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to send reply: %m");
- goto fail;
- }
-
- client1_gone = true;
- } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient2")) {
-
- r = sd_bus_reply_method_return(m, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to send reply: %m");
- goto fail;
- }
-
- client2_gone = true;
- } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Slow")) {
-
- sleep(1);
-
- r = sd_bus_reply_method_return(m, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to send reply: %m");
- goto fail;
- }
-
- } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "FileDescriptor")) {
- int fd;
- static const char x = 'X';
-
- r = sd_bus_message_read(m, "h", &fd);
- if (r < 0) {
- log_error_errno(r, "Failed to get parameter: %m");
- goto fail;
- }
-
- log_info("Received fd=%d", fd);
-
- if (write(fd, &x, 1) < 0) {
- log_error_errno(errno, "Failed to write to fd: %m");
- safe_close(fd);
- goto fail;
- }
-
- r = sd_bus_reply_method_return(m, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to send reply: %m");
- goto fail;
- }
-
- } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
-
- r = sd_bus_reply_method_error(
- m,
- &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
- if (r < 0) {
- log_error_errno(r, "Failed to send reply: %m");
- goto fail;
- }
- }
- }
-
- r = 0;
-
-fail:
- if (bus) {
- sd_bus_flush(bus);
- sd_bus_unref(bus);
- }
-
- return r;
-}
-
-static void* client1(void*p) {
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- sd_bus *bus = NULL;
- sd_bus_error error = SD_BUS_ERROR_NULL;
- const char *hello;
- int r;
- int pp[2] = { -1, -1 };
- char x;
-
- r = sd_bus_open_user(&bus);
- if (r < 0) {
- log_error_errno(r, "Failed to connect to user bus: %m");
- goto finish;
- }
-
- r = sd_bus_call_method(
- bus,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.systemd.test",
- "LowerCase",
- &error,
- &reply,
- "s",
- "HELLO");
- if (r < 0) {
- log_error_errno(r, "Failed to issue method call: %m");
- goto finish;
- }
-
- r = sd_bus_message_read(reply, "s", &hello);
- if (r < 0) {
- log_error_errno(r, "Failed to get string: %m");
- goto finish;
- }
-
- assert_se(streq(hello, "hello"));
-
- if (pipe2(pp, O_CLOEXEC|O_NONBLOCK) < 0) {
- log_error_errno(errno, "Failed to allocate pipe: %m");
- r = -errno;
- goto finish;
- }
-
- log_info("Sending fd=%d", pp[1]);
-
- r = sd_bus_call_method(
- bus,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.systemd.test",
- "FileDescriptor",
- &error,
- NULL,
- "h",
- pp[1]);
- if (r < 0) {
- log_error_errno(r, "Failed to issue method call: %m");
- goto finish;
- }
-
- errno = 0;
- if (read(pp[0], &x, 1) <= 0) {
- log_error("Failed to read from pipe: %s", errno ? strerror(errno) : "early read");
- goto finish;
- }
-
- r = 0;
-
-finish:
- if (bus) {
- _cleanup_bus_message_unref_ sd_bus_message *q;
-
- r = sd_bus_message_new_method_call(
- bus,
- &q,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.systemd.test",
- "ExitClient1");
- if (r < 0)
- log_error_errno(r, "Failed to allocate method call: %m");
- else
- sd_bus_send(bus, q, NULL);
-
- sd_bus_flush(bus);
- sd_bus_unref(bus);
- }
-
- sd_bus_error_free(&error);
-
- safe_close_pair(pp);
-
- return INT_TO_PTR(r);
-}
-
-static int quit_callback(sd_bus *b, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
- bool *x = userdata;
-
- log_error("Quit callback: %s", strerror(sd_bus_message_get_errno(m)));
-
- *x = 1;
- return 1;
-}
-
-static void* client2(void*p) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
- sd_bus *bus = NULL;
- sd_bus_error error = SD_BUS_ERROR_NULL;
- bool quit = false;
- const char *mid;
- int r;
-
- r = sd_bus_open_user(&bus);
- if (r < 0) {
- log_error_errno(r, "Failed to connect to user bus: %m");
- goto finish;
- }
-
- r = sd_bus_message_new_method_call(
- bus,
- &m,
- "org.freedesktop.systemd.test",
- "/foo/bar/waldo/piep",
- "org.object.test",
- "Foobar");
- if (r < 0) {
- log_error_errno(r, "Failed to allocate method call: %m");
- goto finish;
- }
-
- r = sd_bus_send(bus, m, NULL);
- if (r < 0) {
- log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
- goto finish;
- }
-
- sd_bus_message_unref(m);
- m = NULL;
-
- r = sd_bus_message_new_signal(
- bus,
- &m,
- "/foobar",
- "foo.bar",
- "Notify");
- if (r < 0) {
- log_error_errno(r, "Failed to allocate signal: %m");
- goto finish;
- }
-
- r = sd_bus_send(bus, m, NULL);
- if (r < 0) {
- log_error("Failed to issue signal: %s", bus_error_message(&error, -r));
- goto finish;
- }
-
- sd_bus_message_unref(m);
- m = NULL;
-
- r = sd_bus_message_new_method_call(
- bus,
- &m,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.DBus.Peer",
- "GetMachineId");
- if (r < 0) {
- log_error_errno(r, "Failed to allocate method call: %m");
- goto finish;
- }
-
- r = sd_bus_call(bus, m, 0, &error, &reply);
- if (r < 0) {
- log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
- goto finish;
- }
-
- r = sd_bus_message_read(reply, "s", &mid);
- if (r < 0) {
- log_error_errno(r, "Failed to parse machine ID: %m");
- goto finish;
- }
-
- log_info("Machine ID is %s.", mid);
-
- sd_bus_message_unref(m);
- m = NULL;
-
- r = sd_bus_message_new_method_call(
- bus,
- &m,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.systemd.test",
- "Slow");
- if (r < 0) {
- log_error_errno(r, "Failed to allocate method call: %m");
- goto finish;
- }
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call(bus, m, 200 * USEC_PER_MSEC, &error, &reply);
- if (r < 0)
- log_info("Failed to issue method call: %s", bus_error_message(&error, -r));
- else
- log_info("Slow call succeed.");
-
- sd_bus_message_unref(m);
- m = NULL;
-
- r = sd_bus_message_new_method_call(
- bus,
- &m,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.systemd.test",
- "Slow");
- if (r < 0) {
- log_error_errno(r, "Failed to allocate method call: %m");
- goto finish;
- }
-
- r = sd_bus_call_async(bus, NULL, m, quit_callback, &quit, 200 * USEC_PER_MSEC);
- if (r < 0) {
- log_info("Failed to issue method call: %s", bus_error_message(&error, -r));
- goto finish;
- }
-
- while (!quit) {
- r = sd_bus_process(bus, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to process requests: %m");
- goto finish;
- }
- if (r == 0) {
- r = sd_bus_wait(bus, (uint64_t) -1);
- if (r < 0) {
- log_error_errno(r, "Failed to wait: %m");
- goto finish;
- }
- }
- }
-
- r = 0;
-
-finish:
- if (bus) {
- _cleanup_bus_message_unref_ sd_bus_message *q;
-
- r = sd_bus_message_new_method_call(
- bus,
- &q,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.systemd.test",
- "ExitClient2");
- if (r < 0) {
- log_error_errno(r, "Failed to allocate method call: %m");
- goto finish;
- }
-
- sd_bus_send(bus, q, NULL);
- sd_bus_flush(bus);
- sd_bus_unref(bus);
- }
-
- sd_bus_error_free(&error);
- return INT_TO_PTR(r);
-}
-
-int main(int argc, char *argv[]) {
- pthread_t c1, c2;
- sd_bus *bus;
- void *p;
- int q, r;
-
- r = server_init(&bus);
- if (r < 0) {
- log_info("Failed to connect to bus, skipping tests.");
- return EXIT_TEST_SKIP;
- }
-
- log_info("Initialized...");
-
- r = pthread_create(&c1, NULL, client1, bus);
- if (r != 0)
- return EXIT_FAILURE;
-
- r = pthread_create(&c2, NULL, client2, bus);
- if (r != 0)
- return EXIT_FAILURE;
-
- r = server(bus);
-
- q = pthread_join(c1, &p);
- if (q != 0)
- return EXIT_FAILURE;
- if (PTR_TO_INT(p) < 0)
- return EXIT_FAILURE;
-
- q = pthread_join(c2, &p);
- if (q != 0)
- return EXIT_FAILURE;
- if (PTR_TO_INT(p) < 0)
- return EXIT_FAILURE;
-
- if (r < 0)
- return EXIT_FAILURE;
-
- return EXIT_SUCCESS;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Zbigniew Jędrzejewski-Szmek
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdio.h>
-
-#include "sd-bus.h"
-#include "bus-util.h"
-#include "bus-internal.h"
-#include "bus-message.h"
-#include "refcnt.h"
-
-static void test_bus_new(void) {
- _cleanup_bus_unref_ sd_bus *bus = NULL;
-
- assert_se(sd_bus_new(&bus) == 0);
- printf("after new: refcount %u\n", REFCNT_GET(bus->n_ref));
-}
-
-static int test_bus_open(void) {
- _cleanup_bus_unref_ sd_bus *bus = NULL;
- int r;
-
- r = sd_bus_open_system(&bus);
- if (r == -ECONNREFUSED || r == -ENOENT)
- return r;
-
- assert_se(r >= 0);
- printf("after open: refcount %u\n", REFCNT_GET(bus->n_ref));
-
- return 0;
-}
-
-static void test_bus_new_method_call(void) {
- sd_bus *bus = NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-
- assert_se(sd_bus_open_system(&bus) >= 0);
-
- assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0);
-
- printf("after message_new_method_call: refcount %u\n", REFCNT_GET(bus->n_ref));
-
- sd_bus_unref(bus);
- printf("after bus_unref: refcount %u\n", m->n_ref);
-}
-
-static void test_bus_new_signal(void) {
- sd_bus *bus = NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-
- assert_se(sd_bus_open_system(&bus) >= 0);
-
- assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0);
-
- printf("after message_new_signal: refcount %u\n", REFCNT_GET(bus->n_ref));
-
- sd_bus_unref(bus);
- printf("after bus_unref: refcount %u\n", m->n_ref);
-}
-
-int main(int argc, char **argv) {
- int r;
-
- log_parse_environment();
- log_open();
-
- test_bus_new();
- r = test_bus_open();
- if (r < 0) {
- log_info("Failed to connect to bus, skipping tests.");
- return EXIT_TEST_SKIP;
- }
-
- test_bus_new_method_call();
- test_bus_new_signal();
-
- return EXIT_SUCCESS;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "sd-bus.h"
-#include "bus-dump.h"
-#include "bus-util.h"
-
-int main(int argc, char *argv[]) {
- _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
- int r;
-
- r = sd_bus_creds_new_from_pid(&creds, 0, _SD_BUS_CREDS_ALL);
- assert_se(r >= 0);
-
- bus_creds_dump(creds, NULL, true);
-
- creds = sd_bus_creds_unref(creds);
-
- r = sd_bus_creds_new_from_pid(&creds, 1, _SD_BUS_CREDS_ALL);
- if (r != -EACCES) {
- assert_se(r >= 0);
- putchar('\n');
- bus_creds_dump(creds, NULL, true);
- }
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "sd-bus.h"
-#include "bus-error.h"
-#include "bus-util.h"
-#include "errno-list.h"
-#include "bus-common-errors.h"
-
-static void test_error(void) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL, second = SD_BUS_ERROR_NULL;
- const sd_bus_error const_error = SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "const error");
- const sd_bus_error temporarily_const_error = {
- .name = SD_BUS_ERROR_ACCESS_DENIED,
- .message = "oh! no",
- ._need_free = -1
- };
-
- assert_se(!sd_bus_error_is_set(&error));
- assert_se(sd_bus_error_set(&error, SD_BUS_ERROR_NOT_SUPPORTED, "xxx") == -EOPNOTSUPP);
- assert_se(streq(error.name, SD_BUS_ERROR_NOT_SUPPORTED));
- assert_se(streq(error.message, "xxx"));
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_NOT_SUPPORTED));
- assert_se(sd_bus_error_get_errno(&error) == EOPNOTSUPP);
- assert_se(sd_bus_error_is_set(&error));
- sd_bus_error_free(&error);
-
- assert_se(!sd_bus_error_is_set(&error));
- assert_se(sd_bus_error_setf(&error, SD_BUS_ERROR_FILE_NOT_FOUND, "yyy %i", -1) == -ENOENT);
- assert_se(streq(error.name, SD_BUS_ERROR_FILE_NOT_FOUND));
- assert_se(streq(error.message, "yyy -1"));
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
- assert_se(sd_bus_error_get_errno(&error) == ENOENT);
- assert_se(sd_bus_error_is_set(&error));
-
- assert_se(!sd_bus_error_is_set(&second));
- assert_se(second._need_free == 0);
- assert_se(error._need_free > 0);
- assert_se(sd_bus_error_copy(&second, &error) == -ENOENT);
- assert_se(second._need_free > 0);
- assert_se(streq(error.name, second.name));
- assert_se(streq(error.message, second.message));
- assert_se(sd_bus_error_get_errno(&second) == ENOENT);
- assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_NOT_FOUND));
- assert_se(sd_bus_error_is_set(&second));
-
- sd_bus_error_free(&error);
- sd_bus_error_free(&second);
-
- assert_se(!sd_bus_error_is_set(&second));
- assert_se(const_error._need_free == 0);
- assert_se(sd_bus_error_copy(&second, &const_error) == -EEXIST);
- assert_se(second._need_free == 0);
- assert_se(streq(const_error.name, second.name));
- assert_se(streq(const_error.message, second.message));
- assert_se(sd_bus_error_get_errno(&second) == EEXIST);
- assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_EXISTS));
- assert_se(sd_bus_error_is_set(&second));
- sd_bus_error_free(&second);
-
- assert_se(!sd_bus_error_is_set(&second));
- assert_se(temporarily_const_error._need_free < 0);
- assert_se(sd_bus_error_copy(&second, &temporarily_const_error) == -EACCES);
- assert_se(second._need_free > 0);
- assert_se(streq(temporarily_const_error.name, second.name));
- assert_se(streq(temporarily_const_error.message, second.message));
- assert_se(sd_bus_error_get_errno(&second) == EACCES);
- assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_ACCESS_DENIED));
- assert_se(sd_bus_error_is_set(&second));
-
- assert_se(!sd_bus_error_is_set(&error));
- assert_se(sd_bus_error_set_const(&error, "System.Error.EUCLEAN", "Hallo") == -EUCLEAN);
- assert_se(streq(error.name, "System.Error.EUCLEAN"));
- assert_se(streq(error.message, "Hallo"));
- assert_se(sd_bus_error_has_name(&error, "System.Error.EUCLEAN"));
- assert_se(sd_bus_error_get_errno(&error) == EUCLEAN);
- assert_se(sd_bus_error_is_set(&error));
- sd_bus_error_free(&error);
-
- assert_se(!sd_bus_error_is_set(&error));
- assert_se(sd_bus_error_set_errno(&error, EBUSY) == -EBUSY);
- assert_se(streq(error.name, "System.Error.EBUSY"));
- assert_se(streq(error.message, strerror(EBUSY)));
- assert_se(sd_bus_error_has_name(&error, "System.Error.EBUSY"));
- assert_se(sd_bus_error_get_errno(&error) == EBUSY);
- assert_se(sd_bus_error_is_set(&error));
- sd_bus_error_free(&error);
-
- assert_se(!sd_bus_error_is_set(&error));
- assert_se(sd_bus_error_set_errnof(&error, EIO, "Waldi %c", 'X') == -EIO);
- assert_se(streq(error.name, SD_BUS_ERROR_IO_ERROR));
- assert_se(streq(error.message, "Waldi X"));
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR));
- assert_se(sd_bus_error_get_errno(&error) == EIO);
- assert_se(sd_bus_error_is_set(&error));
-}
-
-extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
-extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
-
-static void dump_mapping_table(void) {
- const sd_bus_error_map *m;
-
- printf("----- errno mappings ------\n");
- m = __start_BUS_ERROR_MAP;
- while (m < __stop_BUS_ERROR_MAP) {
-
- if (m->code == BUS_ERROR_MAP_END_MARKER) {
- m = ALIGN8_PTR(m+1);
- continue;
- }
-
- printf("%s -> %i/%s\n", strna(m->name), m->code, strna(errno_to_name(m->code)));
- m ++;
- }
- printf("---------------------------\n");
-}
-
-static void test_errno_mapping_standard(void) {
- assert_se(sd_bus_error_set(NULL, "System.Error.EUCLEAN", NULL) == -EUCLEAN);
- assert_se(sd_bus_error_set(NULL, "System.Error.EBUSY", NULL) == -EBUSY);
- assert_se(sd_bus_error_set(NULL, "System.Error.EINVAL", NULL) == -EINVAL);
- assert_se(sd_bus_error_set(NULL, "System.Error.WHATSIT", NULL) == -EIO);
-}
-
-BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors[] = {
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error", 5),
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-2", 52),
- SD_BUS_ERROR_MAP_END
-};
-
-BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors2[] = {
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-3", 33),
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-4", 44),
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-33", 333),
- SD_BUS_ERROR_MAP_END
-};
-
-static const sd_bus_error_map test_errors3[] = {
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-88", 888),
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-99", 999),
- SD_BUS_ERROR_MAP_END
-};
-
-static const sd_bus_error_map test_errors4[] = {
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-77", 777),
- SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-78", 778),
- SD_BUS_ERROR_MAP_END
-};
-
-static void test_errno_mapping_custom(void) {
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error", NULL) == -5);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-x", NULL) == -EIO);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-33", NULL) == -333);
-
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -EIO);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -EIO);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -EIO);
-
- assert_se(sd_bus_error_add_map(test_errors3) > 0);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -888);
- assert_se(sd_bus_error_add_map(test_errors4) > 0);
- assert_se(sd_bus_error_add_map(test_errors4) == 0);
- assert_se(sd_bus_error_add_map(test_errors3) == 0);
-
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -999);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -777);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-78", NULL) == -778);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52);
- assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-y", NULL) == -EIO);
-
- assert_se(sd_bus_error_set(NULL, BUS_ERROR_NO_SUCH_UNIT, NULL) == -ENOENT);
-}
-
-int main(int argc, char *argv[]) {
- dump_mapping_table();
-
- test_error();
- test_errno_mapping_standard();
- test_errno_mapping_custom();
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#ifdef HAVE_GLIB
-#include <glib.h>
-#endif
-
-#include "util.h"
-#include "macro.h"
-#include "sd-bus.h"
-#include "bus-gvariant.h"
-#include "bus-util.h"
-#include "bus-internal.h"
-#include "bus-message.h"
-#include "bus-dump.h"
-
-static void test_bus_gvariant_is_fixed_size(void) {
- assert_se(bus_gvariant_is_fixed_size("") > 0);
- assert_se(bus_gvariant_is_fixed_size("()") > 0);
- assert_se(bus_gvariant_is_fixed_size("y") > 0);
- assert_se(bus_gvariant_is_fixed_size("u") > 0);
- assert_se(bus_gvariant_is_fixed_size("b") > 0);
- assert_se(bus_gvariant_is_fixed_size("n") > 0);
- assert_se(bus_gvariant_is_fixed_size("q") > 0);
- assert_se(bus_gvariant_is_fixed_size("i") > 0);
- assert_se(bus_gvariant_is_fixed_size("t") > 0);
- assert_se(bus_gvariant_is_fixed_size("d") > 0);
- assert_se(bus_gvariant_is_fixed_size("s") == 0);
- assert_se(bus_gvariant_is_fixed_size("o") == 0);
- assert_se(bus_gvariant_is_fixed_size("g") == 0);
- assert_se(bus_gvariant_is_fixed_size("h") > 0);
- assert_se(bus_gvariant_is_fixed_size("ay") == 0);
- assert_se(bus_gvariant_is_fixed_size("v") == 0);
- assert_se(bus_gvariant_is_fixed_size("(u)") > 0);
- assert_se(bus_gvariant_is_fixed_size("(uuuuy)") > 0);
- assert_se(bus_gvariant_is_fixed_size("(uusuuy)") == 0);
- assert_se(bus_gvariant_is_fixed_size("a{ss}") == 0);
- assert_se(bus_gvariant_is_fixed_size("((u)yyy(b(iiii)))") > 0);
- assert_se(bus_gvariant_is_fixed_size("((u)yyy(b(iiivi)))") == 0);
-}
-
-static void test_bus_gvariant_get_size(void) {
- assert_se(bus_gvariant_get_size("") == 0);
- assert_se(bus_gvariant_get_size("()") == 0);
- assert_se(bus_gvariant_get_size("y") == 1);
- assert_se(bus_gvariant_get_size("u") == 4);
- assert_se(bus_gvariant_get_size("b") == 1);
- assert_se(bus_gvariant_get_size("n") == 2);
- assert_se(bus_gvariant_get_size("q") == 2);
- assert_se(bus_gvariant_get_size("i") == 4);
- assert_se(bus_gvariant_get_size("t") == 8);
- assert_se(bus_gvariant_get_size("d") == 8);
- assert_se(bus_gvariant_get_size("s") < 0);
- assert_se(bus_gvariant_get_size("o") < 0);
- assert_se(bus_gvariant_get_size("g") < 0);
- assert_se(bus_gvariant_get_size("h") == 4);
- assert_se(bus_gvariant_get_size("ay") < 0);
- assert_se(bus_gvariant_get_size("v") < 0);
- assert_se(bus_gvariant_get_size("(u)") == 4);
- assert_se(bus_gvariant_get_size("(uuuuy)") == 20);
- assert_se(bus_gvariant_get_size("(uusuuy)") < 0);
- assert_se(bus_gvariant_get_size("a{ss}") < 0);
- assert_se(bus_gvariant_get_size("((u)yyy(b(iiii)))") == 28);
- assert_se(bus_gvariant_get_size("((u)yyy(b(iiivi)))") < 0);
- assert_se(bus_gvariant_get_size("((b)(t))") == 16);
- assert_se(bus_gvariant_get_size("((b)(b)(t))") == 16);
- assert_se(bus_gvariant_get_size("(bt)") == 16);
- assert_se(bus_gvariant_get_size("((t)(b))") == 16);
- assert_se(bus_gvariant_get_size("(tb)") == 16);
- assert_se(bus_gvariant_get_size("((b)(b))") == 2);
- assert_se(bus_gvariant_get_size("((t)(t))") == 16);
-}
-
-static void test_bus_gvariant_get_alignment(void) {
- assert_se(bus_gvariant_get_alignment("") == 1);
- assert_se(bus_gvariant_get_alignment("()") == 1);
- assert_se(bus_gvariant_get_alignment("y") == 1);
- assert_se(bus_gvariant_get_alignment("b") == 1);
- assert_se(bus_gvariant_get_alignment("u") == 4);
- assert_se(bus_gvariant_get_alignment("s") == 1);
- assert_se(bus_gvariant_get_alignment("o") == 1);
- assert_se(bus_gvariant_get_alignment("g") == 1);
- assert_se(bus_gvariant_get_alignment("v") == 8);
- assert_se(bus_gvariant_get_alignment("h") == 4);
- assert_se(bus_gvariant_get_alignment("i") == 4);
- assert_se(bus_gvariant_get_alignment("t") == 8);
- assert_se(bus_gvariant_get_alignment("x") == 8);
- assert_se(bus_gvariant_get_alignment("q") == 2);
- assert_se(bus_gvariant_get_alignment("n") == 2);
- assert_se(bus_gvariant_get_alignment("d") == 8);
- assert_se(bus_gvariant_get_alignment("ay") == 1);
- assert_se(bus_gvariant_get_alignment("as") == 1);
- assert_se(bus_gvariant_get_alignment("au") == 4);
- assert_se(bus_gvariant_get_alignment("an") == 2);
- assert_se(bus_gvariant_get_alignment("ans") == 2);
- assert_se(bus_gvariant_get_alignment("ant") == 8);
- assert_se(bus_gvariant_get_alignment("(ss)") == 1);
- assert_se(bus_gvariant_get_alignment("(ssu)") == 4);
- assert_se(bus_gvariant_get_alignment("a(ssu)") == 4);
- assert_se(bus_gvariant_get_alignment("(u)") == 4);
- assert_se(bus_gvariant_get_alignment("(uuuuy)") == 4);
- assert_se(bus_gvariant_get_alignment("(uusuuy)") == 4);
- assert_se(bus_gvariant_get_alignment("a{ss}") == 1);
- assert_se(bus_gvariant_get_alignment("((u)yyy(b(iiii)))") == 4);
- assert_se(bus_gvariant_get_alignment("((u)yyy(b(iiivi)))") == 8);
- assert_se(bus_gvariant_get_alignment("((b)(t))") == 8);
- assert_se(bus_gvariant_get_alignment("((b)(b)(t))") == 8);
- assert_se(bus_gvariant_get_alignment("(bt)") == 8);
- assert_se(bus_gvariant_get_alignment("((t)(b))") == 8);
- assert_se(bus_gvariant_get_alignment("(tb)") == 8);
- assert_se(bus_gvariant_get_alignment("((b)(b))") == 1);
- assert_se(bus_gvariant_get_alignment("((t)(t))") == 8);
-}
-
-static void test_marshal(void) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *n = NULL;
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
- _cleanup_free_ void *blob;
- size_t sz;
- int r;
-
- r = sd_bus_open_system(&bus);
- if (r < 0)
- exit(EXIT_TEST_SKIP);
-
- bus->message_version = 2; /* dirty hack to enable gvariant */
-
- assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path/which/is/really/really/long/so/that/we/hit/the/eight/bit/boundary/by/quite/some/margin/to/test/this/stuff/that/it/really/works", "an.interface.name", "AMethodName") >= 0);
-
- assert_cc(sizeof(struct bus_header) == 16);
-
- assert_se(sd_bus_message_append(m,
- "a(usv)", 3,
- 4711, "first-string-parameter", "(st)", "X", (uint64_t) 1111,
- 4712, "second-string-parameter", "(a(si))", 2, "Y", 5, "Z", 6,
- 4713, "third-string-parameter", "(uu)", 1, 2) >= 0);
-
- assert_se(bus_message_seal(m, 4711, 0) >= 0);
-
-#ifdef HAVE_GLIB
- {
- GVariant *v;
- char *t;
-
-#if !defined(GLIB_VERSION_2_36)
- g_type_init();
-#endif
-
- v = g_variant_new_from_data(G_VARIANT_TYPE("(yyyyuta{tv})"), m->header, sizeof(struct bus_header) + m->fields_size, false, NULL, NULL);
- assert_se(g_variant_is_normal_form(v));
- t = g_variant_print(v, TRUE);
- printf("%s\n", t);
- g_free(t);
- g_variant_unref(v);
-
- v = g_variant_new_from_data(G_VARIANT_TYPE("(a(usv))"), m->body.data, m->user_body_size, false, NULL, NULL);
- assert_se(g_variant_is_normal_form(v));
- t = g_variant_print(v, TRUE);
- printf("%s\n", t);
- g_free(t);
- g_variant_unref(v);
- }
-#endif
-
- assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
-
- assert_se(bus_message_get_blob(m, &blob, &sz) >= 0);
-
-#ifdef HAVE_GLIB
- {
- GVariant *v;
- char *t;
-
- v = g_variant_new_from_data(G_VARIANT_TYPE("(yyyyuta{tv}v)"), blob, sz, false, NULL, NULL);
- assert_se(g_variant_is_normal_form(v));
- t = g_variant_print(v, TRUE);
- printf("%s\n", t);
- g_free(t);
- g_variant_unref(v);
- }
-#endif
-
- assert_se(bus_message_from_malloc(bus, blob, sz, NULL, 0, NULL, NULL, &n) >= 0);
- blob = NULL;
-
- assert_se(bus_message_dump(n, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
-
- m = sd_bus_message_unref(m);
-
- assert_se(sd_bus_message_new_method_call(bus, &m, "a.x", "/a/x", "a.x", "Ax") >= 0);
-
- assert_se(sd_bus_message_append(m, "as", 0) >= 0);
-
- assert_se(bus_message_seal(m, 4712, 0) >= 0);
- assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
-}
-
-int main(int argc, char *argv[]) {
-
- test_bus_gvariant_is_fixed_size();
- test_bus_gvariant_get_size();
- test_bus_gvariant_get_alignment();
- test_marshal();
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "log.h"
-#include "bus-introspect.h"
-
-static int prop_get(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
- return -EINVAL;
-}
-
-static int prop_set(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
- return -EINVAL;
-}
-
-static const sd_bus_vtable vtable[] = {
- SD_BUS_VTABLE_START(0),
- SD_BUS_METHOD("Hello", "ssas", "a(uu)", NULL, 0),
- SD_BUS_METHOD("DeprecatedHello", "", "", NULL, SD_BUS_VTABLE_DEPRECATED),
- SD_BUS_METHOD("DeprecatedHelloNoReply", "", "", NULL, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_METHOD_NO_REPLY),
- SD_BUS_SIGNAL("Wowza", "sss", 0),
- SD_BUS_SIGNAL("DeprecatedWowza", "ut", SD_BUS_VTABLE_DEPRECATED),
- SD_BUS_WRITABLE_PROPERTY("AProperty", "s", prop_get, prop_set, 0, 0),
- SD_BUS_PROPERTY("AReadOnlyDeprecatedProperty", "(ut)", prop_get, 0, SD_BUS_VTABLE_DEPRECATED),
- SD_BUS_PROPERTY("ChangingProperty", "t", prop_get, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
- SD_BUS_PROPERTY("Invalidating", "t", prop_get, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
- SD_BUS_PROPERTY("Constant", "t", prop_get, 0, SD_BUS_VTABLE_PROPERTY_CONST),
- SD_BUS_VTABLE_END
-};
-
-int main(int argc, char *argv[]) {
- struct introspect intro;
-
- log_set_max_level(LOG_DEBUG);
-
- assert_se(introspect_begin(&intro, false) >= 0);
-
- fprintf(intro.f, " <interface name=\"org.foo\">\n");
- assert_se(introspect_write_interface(&intro, vtable) >= 0);
- fputs(" </interface>\n", intro.f);
-
- fflush(intro.f);
- fputs(intro.introspection, stdout);
-
- introspect_free(&intro);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/wait.h>
-
-#include "util.h"
-#include "time-util.h"
-
-#include "sd-bus.h"
-#include "bus-kernel.h"
-#include "bus-internal.h"
-#include "bus-util.h"
-
-#define MAX_SIZE (2*1024*1024)
-
-static usec_t arg_loop_usec = 100 * USEC_PER_MSEC;
-
-static void server(sd_bus *b, size_t *result) {
- int r;
-
- for (;;) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-
- r = sd_bus_process(b, &m);
- assert_se(r >= 0);
-
- if (r == 0)
- assert_se(sd_bus_wait(b, USEC_INFINITY) >= 0);
- if (!m)
- continue;
-
- if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping"))
- assert_se(sd_bus_reply_method_return(m, NULL) >= 0);
- else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) {
- const void *p;
- size_t sz;
-
- /* Make sure the mmap is mapped */
- assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
-
- r = sd_bus_reply_method_return(m, NULL);
- assert_se(r >= 0);
- } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
- uint64_t res;
- assert_se(sd_bus_message_read(m, "t", &res) > 0);
-
- *result = res;
- return;
-
- } else
- assert_not_reached("Unknown method");
- }
-}
-
-static void transaction(sd_bus *b, size_t sz, const char *server_name) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
- uint8_t *p;
-
- assert_se(sd_bus_message_new_method_call(b, &m, server_name, "/", "benchmark.server", "Work") >= 0);
- assert_se(sd_bus_message_append_array_space(m, 'y', sz, (void**) &p) >= 0);
-
- memset(p, 0x80, sz);
-
- assert_se(sd_bus_call(b, m, 0, NULL, &reply) >= 0);
-}
-
-static void client_bisect(const char *address, const char *server_name) {
- _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
- size_t lsize, rsize, csize;
- sd_bus *b;
- int r;
-
- r = sd_bus_new(&b);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(b, address);
- assert_se(r >= 0);
-
- r = sd_bus_start(b);
- assert_se(r >= 0);
-
- r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL);
- assert_se(r >= 0);
-
- lsize = 1;
- rsize = MAX_SIZE;
-
- printf("SIZE\tCOPY\tMEMFD\n");
-
- for (;;) {
- usec_t t;
- unsigned n_copying, n_memfd;
-
- csize = (lsize + rsize) / 2;
-
- if (csize <= lsize)
- break;
-
- if (csize <= 0)
- break;
-
- printf("%zu\t", csize);
-
- b->use_memfd = 0;
-
- t = now(CLOCK_MONOTONIC);
- for (n_copying = 0;; n_copying++) {
- transaction(b, csize, server_name);
- if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
- break;
- }
- printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
-
- b->use_memfd = -1;
-
- t = now(CLOCK_MONOTONIC);
- for (n_memfd = 0;; n_memfd++) {
- transaction(b, csize, server_name);
- if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
- break;
- }
- printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
-
- if (n_copying == n_memfd)
- break;
-
- if (n_copying > n_memfd)
- lsize = csize;
- else
- rsize = csize;
- }
-
- b->use_memfd = 1;
- assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0);
- assert_se(sd_bus_message_append(x, "t", csize) >= 0);
- assert_se(sd_bus_send(b, x, NULL) >= 0);
-
- sd_bus_unref(b);
-}
-
-static void client_chart(const char *address, const char *server_name) {
- _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
- size_t csize;
- sd_bus *b;
- int r;
-
- r = sd_bus_new(&b);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(b, address);
- assert_se(r >= 0);
-
- r = sd_bus_start(b);
- assert_se(r >= 0);
-
- assert_se(sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL) >= 0);
-
- printf("SIZE\tCOPY\tMEMFD\n");
-
- for (csize = 1; csize <= MAX_SIZE; csize *= 2) {
- usec_t t;
- unsigned n_copying, n_memfd;
-
- printf("%zu\t", csize);
-
- b->use_memfd = 0;
-
- t = now(CLOCK_MONOTONIC);
- for (n_copying = 0;; n_copying++) {
- transaction(b, csize, server_name);
- if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
- break;
- }
-
- printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
-
- b->use_memfd = -1;
-
- t = now(CLOCK_MONOTONIC);
- for (n_memfd = 0;; n_memfd++) {
- transaction(b, csize, server_name);
- if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
- break;
- }
-
- printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
- }
-
- b->use_memfd = 1;
- assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0);
- assert_se(sd_bus_message_append(x, "t", csize) >= 0);
- assert_se(sd_bus_send(b, x, NULL) >= 0);
-
- sd_bus_unref(b);
-}
-
-int main(int argc, char *argv[]) {
- enum {
- MODE_BISECT,
- MODE_CHART,
- } mode = MODE_BISECT;
- int i;
- _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *server_name = NULL;
- _cleanup_close_ int bus_ref = -1;
- const char *unique;
- cpu_set_t cpuset;
- size_t result;
- sd_bus *b;
- pid_t pid;
- int r;
-
- for (i = 1; i < argc; i++) {
- if (streq(argv[i], "chart")) {
- mode = MODE_CHART;
- continue;
- }
-
- assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0);
- }
-
- assert_se(arg_loop_usec > 0);
-
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
-
- bus_ref = bus_kernel_create_bus(name, false, &bus_name);
- if (bus_ref == -ENOENT)
- exit(EXIT_TEST_SKIP);
-
- assert_se(bus_ref >= 0);
-
- address = strappend("kernel:path=", bus_name);
- assert_se(address);
-
- r = sd_bus_new(&b);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(b, address);
- assert_se(r >= 0);
-
- r = sd_bus_start(b);
- assert_se(r >= 0);
-
- r = sd_bus_get_unique_name(b, &unique);
- assert_se(r >= 0);
-
- server_name = strdup(unique);
- assert_se(server_name);
-
- sync();
- setpriority(PRIO_PROCESS, 0, -19);
-
- pid = fork();
- assert_se(pid >= 0);
-
- if (pid == 0) {
- CPU_ZERO(&cpuset);
- CPU_SET(0, &cpuset);
- pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
-
- safe_close(bus_ref);
- sd_bus_unref(b);
-
- switch (mode) {
- case MODE_BISECT:
- client_bisect(address, server_name);
- break;
-
- case MODE_CHART:
- client_chart(address, server_name);
- break;
- }
-
- _exit(0);
- }
-
- CPU_ZERO(&cpuset);
- CPU_SET(1, &cpuset);
- pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
-
- server(b, &result);
-
- if (mode == MODE_BISECT)
- printf("Copying/memfd are equally fast at %zu bytes\n", result);
-
- assert_se(waitpid(pid, NULL, 0) == pid);
-
- sd_bus_unref(b);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-#include "log.h"
-
-#include "sd-bus.h"
-#include "bus-kernel.h"
-#include "bus-util.h"
-
-static void test_one(
- const char *path,
- const char *interface,
- const char *member,
- bool as_list,
- const char *arg0,
- const char *match,
- bool good) {
-
- _cleanup_close_ int bus_ref = -1;
- _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- sd_bus *a, *b;
- int r;
-
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
-
- bus_ref = bus_kernel_create_bus(name, false, &bus_name);
- if (bus_ref == -ENOENT)
- exit(EXIT_TEST_SKIP);
-
- assert_se(bus_ref >= 0);
-
- address = strappend("kernel:path=", bus_name);
- assert_se(address);
-
- r = sd_bus_new(&a);
- assert_se(r >= 0);
-
- r = sd_bus_new(&b);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(a, address);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(b, address);
- assert_se(r >= 0);
-
- r = sd_bus_start(a);
- assert_se(r >= 0);
-
- r = sd_bus_start(b);
- assert_se(r >= 0);
-
- log_debug("match");
- r = sd_bus_add_match(b, NULL, match, NULL, NULL);
- assert_se(r >= 0);
-
- log_debug("signal");
-
- if (as_list)
- r = sd_bus_emit_signal(a, path, interface, member, "as", 1, arg0);
- else
- r = sd_bus_emit_signal(a, path, interface, member, "s", arg0);
- assert_se(r >= 0);
-
- r = sd_bus_process(b, &m);
- assert_se(r >= 0 && (good == !!m));
-
- sd_bus_unref(a);
- sd_bus_unref(b);
-}
-
-int main(int argc, char *argv[]) {
- log_set_max_level(LOG_DEBUG);
-
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo/tuut'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "interface='waldo.com'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "member='Piep'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "member='Pi_ep'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "arg0='foobar'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "arg0='foo_bar'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", true, "foobar", "arg0='foobar'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", true, "foobar", "arg0='foo_bar'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo',interface='waldo.com',member='Piep',arg0='foobar'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo',interface='waldo.com',member='Piep',arg0='foobar2'", false);
-
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo/quux'", false);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo/bar/waldo'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo/bar'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/'", true);
- test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/quux'", false);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <fcntl.h>
-
-#include "util.h"
-#include "log.h"
-
-#include "sd-bus.h"
-#include "bus-kernel.h"
-#include "bus-util.h"
-#include "bus-dump.h"
-
-int main(int argc, char *argv[]) {
- _cleanup_close_ int bus_ref = -1;
- _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *bname = NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- const char *ua = NULL, *ub = NULL, *the_string = NULL;
- sd_bus *a, *b;
- int r, pipe_fds[2];
- const char *nn;
-
- log_set_max_level(LOG_DEBUG);
-
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
-
- bus_kernel_fix_attach_mask();
-
- bus_ref = bus_kernel_create_bus(name, false, &bus_name);
- if (bus_ref == -ENOENT)
- return EXIT_TEST_SKIP;
-
- assert_se(bus_ref >= 0);
-
- address = strappend("kernel:path=", bus_name);
- assert_se(address);
-
- r = sd_bus_new(&a);
- assert_se(r >= 0);
-
- r = sd_bus_new(&b);
- assert_se(r >= 0);
-
- r = sd_bus_set_description(a, "a");
- assert_se(r >= 0);
-
- r = sd_bus_set_address(a, address);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(b, address);
- assert_se(r >= 0);
-
- assert_se(sd_bus_negotiate_timestamp(a, 1) >= 0);
- assert_se(sd_bus_negotiate_creds(a, true, _SD_BUS_CREDS_ALL) >= 0);
-
- assert_se(sd_bus_negotiate_timestamp(b, 0) >= 0);
- assert_se(sd_bus_negotiate_creds(b, true, 0) >= 0);
-
- r = sd_bus_start(a);
- assert_se(r >= 0);
-
- r = sd_bus_start(b);
- assert_se(r >= 0);
-
- assert_se(sd_bus_negotiate_timestamp(b, 1) >= 0);
- assert_se(sd_bus_negotiate_creds(b, true, _SD_BUS_CREDS_ALL) >= 0);
-
- r = sd_bus_get_unique_name(a, &ua);
- assert_se(r >= 0);
- printf("unique a: %s\n", ua);
-
- r = sd_bus_get_description(a, &nn);
- assert_se(r >= 0);
- printf("name of a: %s\n", nn);
-
- r = sd_bus_get_unique_name(b, &ub);
- assert_se(r >= 0);
- printf("unique b: %s\n", ub);
-
- r = sd_bus_get_description(b, &nn);
- assert_se(r >= 0);
- printf("name of b: %s\n", nn);
-
- assert_se(bus_kernel_get_bus_name(b, &bname) >= 0);
- assert_se(endswith(bname, name));
-
- r = sd_bus_call_method(a, "this.doesnt.exist", "/foo", "meh.mah", "muh", &error, NULL, "s", "yayayay");
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN));
- assert_se(r == -EHOSTUNREACH);
-
- r = sd_bus_add_match(b, NULL, "interface='waldo.com',member='Piep'", NULL, NULL);
- assert_se(r >= 0);
-
- r = sd_bus_emit_signal(a, "/foo/bar/waldo", "waldo.com", "Piep", "sss", "I am a string", "/this/is/a/path", "and.this.a.domain.name");
- assert_se(r >= 0);
-
- r = sd_bus_try_close(b);
- assert_se(r == -EBUSY);
-
- r = sd_bus_process_priority(b, -10, &m);
- assert_se(r == 0);
-
- r = sd_bus_process(b, &m);
- assert_se(r > 0);
- assert_se(m);
-
- bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
- assert_se(sd_bus_message_rewind(m, true) >= 0);
-
- r = sd_bus_message_read(m, "s", &the_string);
- assert_se(r >= 0);
- assert_se(streq(the_string, "I am a string"));
-
- sd_bus_message_unref(m);
- m = NULL;
-
- r = sd_bus_request_name(a, "net.x0pointer.foobar", 0);
- assert_se(r >= 0);
-
- r = sd_bus_message_new_method_call(b, &m, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod");
- assert_se(r >= 0);
-
- assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0);
-
- assert_se(write(pipe_fds[1], "x", 1) == 1);
-
- pipe_fds[1] = safe_close(pipe_fds[1]);
-
- r = sd_bus_message_append(m, "h", pipe_fds[0]);
- assert_se(r >= 0);
-
- pipe_fds[0] = safe_close(pipe_fds[0]);
-
- r = sd_bus_send(b, m, NULL);
- assert_se(r >= 0);
-
- for (;;) {
- sd_bus_message_unref(m);
- m = NULL;
- r = sd_bus_process(a, &m);
- assert_se(r > 0);
- assert_se(m);
-
- bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
- assert_se(sd_bus_message_rewind(m, true) >= 0);
-
- if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) {
- int fd;
- char x;
-
- r = sd_bus_message_read(m, "h", &fd);
- assert_se(r >= 0);
-
- assert_se(read(fd, &x, 1) == 1);
- assert_se(x == 'x');
- break;
- }
- }
-
- r = sd_bus_release_name(a, "net.x0pointer.foobar");
- assert_se(r >= 0);
-
- r = sd_bus_release_name(a, "net.x0pointer.foobar");
- assert_se(r == -ESRCH);
-
- r = sd_bus_try_close(a);
- assert_se(r >= 0);
-
- sd_bus_unref(a);
- sd_bus_unref(b);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <math.h>
-
-#ifdef HAVE_GLIB
-#include <gio/gio.h>
-#endif
-
-#ifdef HAVE_DBUS
-#include <dbus/dbus.h>
-#endif
-
-#include "log.h"
-#include "util.h"
-
-#include "sd-bus.h"
-#include "bus-message.h"
-#include "bus-util.h"
-#include "bus-dump.h"
-#include "bus-label.h"
-
-static void test_bus_path_encode(void) {
- _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
-
- assert_se(sd_bus_path_encode("/foo/bar", "waldo", &a) >= 0 && streq(a, "/foo/bar/waldo"));
- assert_se(sd_bus_path_decode(a, "/waldo", &b) == 0 && b == NULL);
- assert_se(sd_bus_path_decode(a, "/foo/bar", &b) > 0 && streq(b, "waldo"));
-
- assert_se(sd_bus_path_encode("xxxx", "waldo", &c) < 0);
- assert_se(sd_bus_path_encode("/foo/", "waldo", &c) < 0);
-
- assert_se(sd_bus_path_encode("/foo/bar", "", &c) >= 0 && streq(c, "/foo/bar/_"));
- assert_se(sd_bus_path_decode(c, "/foo/bar", &d) > 0 && streq(d, ""));
-
- assert_se(sd_bus_path_encode("/foo/bar", "foo.bar", &e) >= 0 && streq(e, "/foo/bar/foo_2ebar"));
- assert_se(sd_bus_path_decode(e, "/foo/bar", &f) > 0 && streq(f, "foo.bar"));
-}
-
-static void test_bus_label_escape_one(const char *a, const char *b) {
- _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL;
-
- assert_se(t = bus_label_escape(a));
- assert_se(streq(t, b));
-
- assert_se(x = bus_label_unescape(t));
- assert_se(streq(a, x));
-
- assert_se(y = bus_label_unescape(b));
- assert_se(streq(a, y));
-}
-
-static void test_bus_label_escape(void) {
- test_bus_label_escape_one("foo123bar", "foo123bar");
- test_bus_label_escape_one("foo.bar", "foo_2ebar");
- test_bus_label_escape_one("foo_2ebar", "foo_5f2ebar");
- test_bus_label_escape_one("", "_");
- test_bus_label_escape_one("_", "_5f");
- test_bus_label_escape_one("1", "_31");
- test_bus_label_escape_one(":1", "_3a1");
-}
-
-int main(int argc, char *argv[]) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *copy = NULL;
- int r, boolean;
- const char *x, *x2, *y, *z, *a, *b, *c, *d, *a_signature;
- uint8_t u, v;
- void *buffer = NULL;
- size_t sz;
- char *h;
- const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array;
- char *s;
- _cleanup_free_ char *first = NULL, *second = NULL, *third = NULL;
- _cleanup_fclose_ FILE *ms = NULL;
- size_t first_size = 0, second_size = 0, third_size = 0;
- _cleanup_bus_unref_ sd_bus *bus = NULL;
- double dbl;
- uint64_t u64;
-
- r = sd_bus_default_system(&bus);
- if (r < 0)
- return EXIT_TEST_SKIP;
-
- r = sd_bus_message_new_method_call(bus, &m, "foobar.waldo", "/", "foobar.waldo", "Piep");
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "");
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "s", "a string");
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "s", NULL);
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "asg", 2, "string #1", "string #2", "sba(tt)ss");
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
- assert_se(r >= 0);
-
- r = sd_bus_message_open_container(m, 'a', "s");
- assert_se(r >= 0);
-
- r = sd_bus_message_append_basic(m, 's', "foobar");
- assert_se(r >= 0);
-
- r = sd_bus_message_append_basic(m, 's', "waldo");
- assert_se(r >= 0);
-
- r = sd_bus_message_close_container(m);
- assert_se(r >= 0);
-
- r = sd_bus_message_append_string_space(m, 5, &s);
- assert_se(r >= 0);
- strcpy(s, "hallo");
-
- r = sd_bus_message_append_array(m, 'i', integer_array, sizeof(integer_array));
- assert_se(r >= 0);
-
- r = sd_bus_message_append_array(m, 'u', NULL, 0);
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "a(stdo)", 1, "foo", 815ULL, 47.0, "/");
- assert_se(r >= 0);
-
- r = bus_message_seal(m, 4711, 0);
- assert_se(r >= 0);
-
- bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- ms = open_memstream(&first, &first_size);
- bus_message_dump(m, ms, 0);
- fflush(ms);
- assert_se(!ferror(ms));
-
- r = bus_message_get_blob(m, &buffer, &sz);
- assert_se(r >= 0);
-
- h = hexmem(buffer, sz);
- assert_se(h);
-
- log_info("message size = %zu, contents =\n%s", sz, h);
- free(h);
-
-#ifdef HAVE_GLIB
- {
- GDBusMessage *g;
- char *p;
-
-#if !defined(GLIB_VERSION_2_36)
- g_type_init();
-#endif
-
- g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
- p = g_dbus_message_print(g, 0);
- log_info("%s", p);
- g_free(p);
- g_object_unref(g);
- }
-#endif
-
-#ifdef HAVE_DBUS
- {
- DBusMessage *w;
- DBusError error;
-
- dbus_error_init(&error);
-
- w = dbus_message_demarshal(buffer, sz, &error);
- if (!w)
- log_error("%s", error.message);
- else
- dbus_message_unref(w);
- }
-#endif
-
- m = sd_bus_message_unref(m);
-
- r = bus_message_from_malloc(bus, buffer, sz, NULL, 0, NULL, NULL, &m);
- assert_se(r >= 0);
-
- bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- fclose(ms);
- ms = open_memstream(&second, &second_size);
- bus_message_dump(m, ms, 0);
- fflush(ms);
- assert_se(!ferror(ms));
- assert_se(first_size == second_size);
- assert_se(memcmp(first, second, first_size) == 0);
-
- assert_se(sd_bus_message_rewind(m, true) >= 0);
-
- r = sd_bus_message_read(m, "ssasg", &x, &x2, 2, &y, &z, &a_signature);
- assert_se(r > 0);
- assert_se(streq(x, "a string"));
- assert_se(streq(x2, ""));
- assert_se(streq(y, "string #1"));
- assert_se(streq(z, "string #2"));
- assert_se(streq(a_signature, "sba(tt)ss"));
-
- r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
- assert_se(r > 0);
- assert_se(streq(x, "foobar"));
- assert_se(streq(y, "foo"));
- assert_se(streq(z, "bar"));
- assert_se(streq(a, "waldo"));
- assert_se(streq(b, "piep"));
- assert_se(streq(c, "pap"));
- assert_se(streq(d, "after"));
-
- r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
- assert_se(r > 0);
- assert_se(u == 3);
- assert_se(streq(x, "foo"));
- assert_se(v == 5);
- assert_se(streq(y, "waldo"));
-
- r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
- assert_se(r > 0);
- assert_se(boolean);
- assert_se(streq(x, "aaa"));
- assert_se(streq(y, "1"));
- assert_se(streq(a, "bbb"));
- assert_se(streq(b, "2"));
- assert_se(streq(c, "ccc"));
- assert_se(streq(d, "3"));
-
- assert_se(sd_bus_message_verify_type(m, 'a', "s") > 0);
-
- r = sd_bus_message_read(m, "as", 2, &x, &y);
- assert_se(r > 0);
- assert_se(streq(x, "foobar"));
- assert_se(streq(y, "waldo"));
-
- r = sd_bus_message_read_basic(m, 's', &s);
- assert_se(r > 0);
- assert_se(streq(s, "hallo"));
-
- r = sd_bus_message_read_array(m, 'i', (const void**) &return_array, &sz);
- assert_se(r > 0);
- assert_se(sz == sizeof(integer_array));
- assert_se(memcmp(integer_array, return_array, sz) == 0);
-
- r = sd_bus_message_read_array(m, 'u', (const void**) &return_array, &sz);
- assert_se(r > 0);
- assert_se(sz == 0);
-
- r = sd_bus_message_read(m, "a(stdo)", 1, &x, &u64, &dbl, &y);
- assert_se(r > 0);
- assert_se(streq(x, "foo"));
- assert_se(u64 == 815ULL);
- assert_se(fabs(dbl - 47.0) < 0.1);
- assert_se(streq(y, "/"));
-
- r = sd_bus_message_peek_type(m, NULL, NULL);
- assert_se(r == 0);
-
- r = sd_bus_message_new_method_call(bus, ©, "foobar.waldo", "/", "foobar.waldo", "Piep");
- assert_se(r >= 0);
-
- r = sd_bus_message_rewind(m, true);
- assert_se(r >= 0);
-
- r = sd_bus_message_copy(copy, m, true);
- assert_se(r >= 0);
-
- r = bus_message_seal(copy, 4712, 0);
- assert_se(r >= 0);
-
- fclose(ms);
- ms = open_memstream(&third, &third_size);
- bus_message_dump(copy, ms, 0);
- fflush(ms);
- assert_se(!ferror(ms));
-
- printf("<%.*s>\n", (int) first_size, first);
- printf("<%.*s>\n", (int) third_size, third);
-
- assert_se(first_size == third_size);
- assert_se(memcmp(first, third, third_size) == 0);
-
- r = sd_bus_message_rewind(m, true);
- assert_se(r >= 0);
-
- assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
-
- r = sd_bus_message_skip(m, "ssasg");
- assert_se(r > 0);
-
- assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
-
- r = sd_bus_message_skip(m, "sass");
- assert_se(r >= 0);
-
- assert_se(sd_bus_message_verify_type(m, 'a', "{yv}") > 0);
-
- r = sd_bus_message_skip(m, "a{yv}");
- assert_se(r >= 0);
-
- assert_se(sd_bus_message_verify_type(m, 'b', NULL) > 0);
-
- r = sd_bus_message_read(m, "b", &boolean);
- assert_se(r > 0);
- assert_se(boolean);
-
- r = sd_bus_message_enter_container(m, 0, NULL);
- assert_se(r > 0);
-
- r = sd_bus_message_read(m, "(ss)", &x, &y);
- assert_se(r > 0);
-
- r = sd_bus_message_read(m, "(ss)", &a, &b);
- assert_se(r > 0);
-
- r = sd_bus_message_read(m, "(ss)", &c, &d);
- assert_se(r > 0);
-
- r = sd_bus_message_read(m, "(ss)", &x, &y);
- assert_se(r == 0);
-
- r = sd_bus_message_exit_container(m);
- assert_se(r >= 0);
-
- assert_se(streq(x, "aaa"));
- assert_se(streq(y, "1"));
- assert_se(streq(a, "bbb"));
- assert_se(streq(b, "2"));
- assert_se(streq(c, "ccc"));
- assert_se(streq(d, "3"));
-
- test_bus_label_escape();
- test_bus_path_encode();
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "log.h"
-#include "macro.h"
-
-#include "bus-match.h"
-#include "bus-message.h"
-#include "bus-util.h"
-#include "bus-slot.h"
-
-static bool mask[32];
-
-static int filter(sd_bus *b, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
- log_info("Ran %u", PTR_TO_UINT(userdata));
- assert_se(PTR_TO_UINT(userdata) < ELEMENTSOF(mask));
- mask[PTR_TO_UINT(userdata)] = true;
- return 0;
-}
-
-static bool mask_contains(unsigned a[], unsigned n) {
- unsigned i, j;
-
- for (i = 0; i < ELEMENTSOF(mask); i++) {
- bool found = false;
-
- for (j = 0; j < n; j++)
- if (a[j] == i) {
- found = true;
- break;
- }
-
- if (found != mask[i])
- return false;
- }
-
- return true;
-}
-
-static int match_add(sd_bus_slot *slots, struct bus_match_node *root, const char *match, int value) {
- struct bus_match_component *components = NULL;
- unsigned n_components = 0;
- sd_bus_slot *s;
- int r;
-
- s = slots + value;
- zero(*s);
-
- r = bus_match_parse(match, &components, &n_components);
- if (r < 0)
- return r;
-
- s->userdata = INT_TO_PTR(value);
- s->match_callback.callback = filter;
-
- r = bus_match_add(root, components, n_components, &s->match_callback);
- bus_match_parse_free(components, n_components);
-
- return r;
-}
-
-int main(int argc, char *argv[]) {
- struct bus_match_node root = {
- .type = BUS_MATCH_ROOT,
- };
-
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
- enum bus_match_node_type i;
- sd_bus_slot slots[19];
- int r;
-
- r = sd_bus_open_system(&bus);
- if (r < 0)
- return EXIT_TEST_SKIP;
-
- assert_se(match_add(slots, &root, "arg2='wal\\'do',sender='foo',type='signal',interface='bar.x',", 1) >= 0);
- assert_se(match_add(slots, &root, "arg2='wal\\'do2',sender='foo',type='signal',interface='bar.x',", 2) >= 0);
- assert_se(match_add(slots, &root, "arg3='test',sender='foo',type='signal',interface='bar.x',", 3) >= 0);
- assert_se(match_add(slots, &root, "arg3='test',sender='foo',type='method_call',interface='bar.x',", 4) >= 0);
- assert_se(match_add(slots, &root, "", 5) >= 0);
- assert_se(match_add(slots, &root, "interface='quux.x'", 6) >= 0);
- assert_se(match_add(slots, &root, "interface='bar.x'", 7) >= 0);
- assert_se(match_add(slots, &root, "member='waldo',path='/foo/bar'", 8) >= 0);
- assert_se(match_add(slots, &root, "path='/foo/bar'", 9) >= 0);
- assert_se(match_add(slots, &root, "path_namespace='/foo'", 10) >= 0);
- assert_se(match_add(slots, &root, "path_namespace='/foo/quux'", 11) >= 0);
- assert_se(match_add(slots, &root, "arg1='two'", 12) >= 0);
- assert_se(match_add(slots, &root, "member='waldo',arg2path='/prefix/'", 13) >= 0);
- assert_se(match_add(slots, &root, "member=waldo,path='/foo/bar',arg3namespace='prefix'", 14) >= 0);
- assert_se(match_add(slots, &root, "arg4='pi'", 15) >= 0);
- assert_se(match_add(slots, &root, "arg4='pa'", 16) >= 0);
- assert_se(match_add(slots, &root, "arg4='po'", 17) >= 0);
- assert_se(match_add(slots, &root, "arg4='pu'", 18) >= 0);
-
- bus_match_dump(&root, 0);
-
- assert_se(sd_bus_message_new_signal(bus, &m, "/foo/bar", "bar.x", "waldo") >= 0);
- assert_se(sd_bus_message_append(m, "ssssas", "one", "two", "/prefix/three", "prefix.four", 3, "pi", "pa", "po") >= 0);
- assert_se(bus_message_seal(m, 1, 0) >= 0);
-
- zero(mask);
- assert_se(bus_match_run(NULL, &root, m) == 0);
- assert_se(mask_contains((unsigned[]) { 9, 8, 7, 5, 10, 12, 13, 14, 15, 16, 17 }, 11));
-
- assert_se(bus_match_remove(&root, &slots[8].match_callback) >= 0);
- assert_se(bus_match_remove(&root, &slots[13].match_callback) >= 0);
-
- bus_match_dump(&root, 0);
-
- zero(mask);
- assert_se(bus_match_run(NULL, &root, m) == 0);
- assert_se(mask_contains((unsigned[]) { 9, 5, 10, 12, 14, 7, 15, 16, 17 }, 9));
-
- for (i = 0; i < _BUS_MATCH_NODE_TYPE_MAX; i++) {
- char buf[32];
- const char *x;
-
- assert_se(x = bus_match_node_type_to_string(i, buf, sizeof(buf)));
-
- if (i >= BUS_MATCH_MESSAGE_TYPE)
- assert_se(bus_match_node_type_from_string(x, strlen(x)) == i);
- }
-
- bus_match_free(&root);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <pthread.h>
-
-#include "log.h"
-#include "util.h"
-#include "macro.h"
-#include "strv.h"
-
-#include "sd-bus.h"
-#include "bus-internal.h"
-#include "bus-message.h"
-#include "bus-util.h"
-#include "bus-dump.h"
-
-struct context {
- int fds[2];
- bool quit;
- char *something;
- char *automatic_string_property;
- uint32_t automatic_integer_property;
-};
-
-static int something_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- struct context *c = userdata;
- const char *s;
- char *n = NULL;
- int r;
-
- r = sd_bus_message_read(m, "s", &s);
- assert_se(r > 0);
-
- n = strjoin("<<<", s, ">>>", NULL);
- assert_se(n);
-
- free(c->something);
- c->something = n;
-
- log_info("AlterSomething() called, got %s, returning %s", s, n);
-
- /* This should fail, since the return type doesn't match */
- assert_se(sd_bus_reply_method_return(m, "u", 4711) == -ENOMSG);
-
- r = sd_bus_reply_method_return(m, "s", n);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int exit_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- struct context *c = userdata;
- int r;
-
- c->quit = true;
-
- log_info("Exit called");
-
- r = sd_bus_reply_method_return(m, "");
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int get_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
- struct context *c = userdata;
- int r;
-
- log_info("property get for %s called, returning \"%s\".", property, c->something);
-
- r = sd_bus_message_append(reply, "s", c->something);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int set_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error) {
- struct context *c = userdata;
- const char *s;
- char *n;
- int r;
-
- log_info("property set for %s called", property);
-
- r = sd_bus_message_read(value, "s", &s);
- assert_se(r >= 0);
-
- n = strdup(s);
- assert_se(n);
-
- free(c->something);
- c->something = n;
-
- return 1;
-}
-
-static int value_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
- _cleanup_free_ char *s = NULL;
- const char *x;
- int r;
-
- assert_se(asprintf(&s, "object %p, path %s", userdata, path) >= 0);
- r = sd_bus_message_append(reply, "s", s);
- assert_se(r >= 0);
-
- assert_se(x = startswith(path, "/value/"));
-
- assert_se(PTR_TO_UINT(userdata) == 30);
-
- return 1;
-}
-
-static int notify_test(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- int r;
-
- assert_se(sd_bus_emit_properties_changed(bus, m->path, "org.freedesktop.systemd.ValueTest", "Value", NULL) >= 0);
-
- r = sd_bus_reply_method_return(m, NULL);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int notify_test2(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- int r;
-
- assert_se(sd_bus_emit_properties_changed_strv(bus, m->path, "org.freedesktop.systemd.ValueTest", NULL) >= 0);
-
- r = sd_bus_reply_method_return(m, NULL);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int emit_interfaces_added(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- int r;
-
- assert_se(sd_bus_emit_interfaces_added(bus, m->path, "org.freedesktop.systemd.test", NULL) >= 0);
-
- r = sd_bus_reply_method_return(m, NULL);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int emit_interfaces_removed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- int r;
-
- assert_se(sd_bus_emit_interfaces_removed(bus, m->path, "org.freedesktop.systemd.test", NULL) >= 0);
-
- r = sd_bus_reply_method_return(m, NULL);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int emit_object_added(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- int r;
-
- assert_se(sd_bus_emit_object_added(bus, m->path) >= 0);
-
- r = sd_bus_reply_method_return(m, NULL);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static int emit_object_removed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- int r;
-
- assert_se(sd_bus_emit_object_removed(bus, m->path) >= 0);
-
- r = sd_bus_reply_method_return(m, NULL);
- assert_se(r >= 0);
-
- return 1;
-}
-
-static const sd_bus_vtable vtable[] = {
- SD_BUS_VTABLE_START(0),
- SD_BUS_METHOD("AlterSomething", "s", "s", something_handler, 0),
- SD_BUS_METHOD("Exit", "", "", exit_handler, 0),
- SD_BUS_WRITABLE_PROPERTY("Something", "s", get_handler, set_handler, 0, 0),
- SD_BUS_WRITABLE_PROPERTY("AutomaticStringProperty", "s", NULL, NULL, offsetof(struct context, automatic_string_property), 0),
- SD_BUS_WRITABLE_PROPERTY("AutomaticIntegerProperty", "u", NULL, NULL, offsetof(struct context, automatic_integer_property), 0),
- SD_BUS_METHOD("NoOperation", NULL, NULL, NULL, 0),
- SD_BUS_METHOD("EmitInterfacesAdded", NULL, NULL, emit_interfaces_added, 0),
- SD_BUS_METHOD("EmitInterfacesRemoved", NULL, NULL, emit_interfaces_removed, 0),
- SD_BUS_METHOD("EmitObjectAdded", NULL, NULL, emit_object_added, 0),
- SD_BUS_METHOD("EmitObjectRemoved", NULL, NULL, emit_object_removed, 0),
- SD_BUS_VTABLE_END
-};
-
-static const sd_bus_vtable vtable2[] = {
- SD_BUS_VTABLE_START(0),
- SD_BUS_METHOD("NotifyTest", "", "", notify_test, 0),
- SD_BUS_METHOD("NotifyTest2", "", "", notify_test2, 0),
- SD_BUS_PROPERTY("Value", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
- SD_BUS_PROPERTY("Value2", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
- SD_BUS_PROPERTY("Value3", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_CONST),
- SD_BUS_PROPERTY("Value4", "s", value_handler, 10, 0),
- SD_BUS_VTABLE_END
-};
-
-static int enumerator_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
-
- if (object_path_startswith("/value", path))
- assert_se(*nodes = strv_new("/value/a", "/value/b", "/value/c", NULL));
-
- return 1;
-}
-
-static void *server(void *p) {
- struct context *c = p;
- sd_bus *bus = NULL;
- sd_id128_t id;
- int r;
-
- c->quit = false;
-
- assert_se(sd_id128_randomize(&id) >= 0);
-
- assert_se(sd_bus_new(&bus) >= 0);
- assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
- assert_se(sd_bus_set_server(bus, 1, id) >= 0);
-
- assert_se(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.test", vtable, c) >= 0);
- assert_se(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.test2", vtable, c) >= 0);
- assert_se(sd_bus_add_fallback_vtable(bus, NULL, "/value", "org.freedesktop.systemd.ValueTest", vtable2, NULL, UINT_TO_PTR(20)) >= 0);
- assert_se(sd_bus_add_node_enumerator(bus, NULL, "/value", enumerator_callback, NULL) >= 0);
- assert_se(sd_bus_add_object_manager(bus, NULL, "/value") >= 0);
-
- assert_se(sd_bus_start(bus) >= 0);
-
- log_error("Entering event loop on server");
-
- while (!c->quit) {
- log_error("Loop!");
-
- r = sd_bus_process(bus, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to process requests: %m");
- goto fail;
- }
-
- if (r == 0) {
- r = sd_bus_wait(bus, (uint64_t) -1);
- if (r < 0) {
- log_error_errno(r, "Failed to wait: %m");
- goto fail;
- }
-
- continue;
- }
- }
-
- r = 0;
-
-fail:
- if (bus) {
- sd_bus_flush(bus);
- sd_bus_unref(bus);
- }
-
- return INT_TO_PTR(r);
-}
-
-static int client(struct context *c) {
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- _cleanup_bus_unref_ sd_bus *bus = NULL;
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- const char *s;
- int r;
-
- assert_se(sd_bus_new(&bus) >= 0);
- assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
- assert_se(sd_bus_start(bus) >= 0);
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "NoOperation", &error, NULL, NULL);
- assert_se(r >= 0);
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "s", "hallo");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- assert_se(streq(s, "<<<hallo>>>"));
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Doesntexist", &error, &reply, "");
- assert_se(r < 0);
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD));
-
- sd_bus_error_free(&error);
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "as", 1, "hallo");
- assert_se(r < 0);
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS));
-
- sd_bus_error_free(&error);
-
- r = sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, &reply, "s");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- assert_se(streq(s, "<<<hallo>>>"));
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, "s", "test");
- assert_se(r >= 0);
-
- r = sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, &reply, "s");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- assert_se(streq(s, "test"));
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticIntegerProperty", &error, "u", 815);
- assert_se(r >= 0);
-
- assert_se(c->automatic_integer_property == 815);
-
- r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticStringProperty", &error, "s", "Du Dödel, Du!");
- assert_se(r >= 0);
-
- assert_se(streq(c->automatic_string_property, "Du Dödel, Du!"));
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- fputs(s, stdout);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/value/xuzz", "org.freedesktop.systemd.ValueTest", "Value", &error, &reply, "s");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- log_info("read %s", s);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- fputs(s, stdout);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- fputs(s, stdout);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
- assert_se(r >= 0);
-
- r = sd_bus_message_read(reply, "s", &s);
- assert_se(r >= 0);
- fputs(s, stdout);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", "");
- assert_se(r >= 0);
-
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", "org.freedesktop.systemd.ValueTest2");
- assert_se(r < 0);
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_INTERFACE));
- sd_bus_error_free(&error);
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, "");
- assert_se(r < 0);
- assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD));
- sd_bus_error_free(&error);
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, "");
- assert_se(r >= 0);
-
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.systemd.ValueTest", "NotifyTest", &error, NULL, "");
- assert_se(r >= 0);
-
- r = sd_bus_process(bus, &reply);
- assert_se(r > 0);
-
- assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.Properties", "PropertiesChanged"));
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.systemd.ValueTest", "NotifyTest2", &error, NULL, "");
- assert_se(r >= 0);
-
- r = sd_bus_process(bus, &reply);
- assert_se(r > 0);
-
- assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.Properties", "PropertiesChanged"));
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitInterfacesAdded", &error, NULL, "");
- assert_se(r >= 0);
-
- r = sd_bus_process(bus, &reply);
- assert_se(r > 0);
-
- assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded"));
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitInterfacesRemoved", &error, NULL, "");
- assert_se(r >= 0);
-
- r = sd_bus_process(bus, &reply);
- assert_se(r > 0);
-
- assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved"));
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitObjectAdded", &error, NULL, "");
- assert_se(r >= 0);
-
- r = sd_bus_process(bus, &reply);
- assert_se(r > 0);
-
- assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded"));
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitObjectRemoved", &error, NULL, "");
- assert_se(r >= 0);
-
- r = sd_bus_process(bus, &reply);
- assert_se(r > 0);
-
- assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved"));
- bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- sd_bus_message_unref(reply);
- reply = NULL;
-
- r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Exit", &error, NULL, "");
- assert_se(r >= 0);
-
- sd_bus_flush(bus);
-
- return 0;
-}
-
-int main(int argc, char *argv[]) {
- struct context c = {};
- pthread_t s;
- void *p;
- int r, q;
-
- zero(c);
-
- c.automatic_integer_property = 4711;
- assert_se(c.automatic_string_property = strdup("dudeldu"));
-
- assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
-
- r = pthread_create(&s, NULL, server, &c);
- if (r != 0)
- return -r;
-
- r = client(&c);
-
- q = pthread_join(s, &p);
- if (q != 0)
- return -q;
-
- if (r < 0)
- return r;
-
- if (PTR_TO_INT(p) < 0)
- return PTR_TO_INT(p);
-
- free(c.something);
- free(c.automatic_string_property);
-
- return EXIT_SUCCESS;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <pthread.h>
-
-#include "log.h"
-#include "util.h"
-#include "macro.h"
-
-#include "sd-bus.h"
-#include "bus-internal.h"
-#include "bus-util.h"
-
-struct context {
- int fds[2];
-
- bool client_negotiate_unix_fds;
- bool server_negotiate_unix_fds;
-
- bool client_anonymous_auth;
- bool server_anonymous_auth;
-};
-
-static void *server(void *p) {
- struct context *c = p;
- sd_bus *bus = NULL;
- sd_id128_t id;
- bool quit = false;
- int r;
-
- assert_se(sd_id128_randomize(&id) >= 0);
-
- assert_se(sd_bus_new(&bus) >= 0);
- assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
- assert_se(sd_bus_set_server(bus, 1, id) >= 0);
- assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
- assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
- assert_se(sd_bus_start(bus) >= 0);
-
- while (!quit) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
-
- r = sd_bus_process(bus, &m);
- if (r < 0) {
- log_error_errno(r, "Failed to process requests: %m");
- goto fail;
- }
-
- if (r == 0) {
- r = sd_bus_wait(bus, (uint64_t) -1);
- if (r < 0) {
- log_error_errno(r, "Failed to wait: %m");
- goto fail;
- }
-
- continue;
- }
-
- if (!m)
- continue;
-
- log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
-
- if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
-
- assert_se((sd_bus_can_send(bus, 'h') >= 1) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
-
- r = sd_bus_message_new_method_return(m, &reply);
- if (r < 0) {
- log_error_errno(r, "Failed to allocate return: %m");
- goto fail;
- }
-
- quit = true;
-
- } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
- r = sd_bus_message_new_method_error(
- m,
- &reply,
- &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
- if (r < 0) {
- log_error_errno(r, "Failed to allocate return: %m");
- goto fail;
- }
- }
-
- if (reply) {
- r = sd_bus_send(bus, reply, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed to send reply: %m");
- goto fail;
- }
- }
- }
-
- r = 0;
-
-fail:
- if (bus) {
- sd_bus_flush(bus);
- sd_bus_unref(bus);
- }
-
- return INT_TO_PTR(r);
-}
-
-static int client(struct context *c) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
- _cleanup_bus_unref_ sd_bus *bus = NULL;
- sd_bus_error error = SD_BUS_ERROR_NULL;
- int r;
-
- assert_se(sd_bus_new(&bus) >= 0);
- assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
- assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
- assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
- assert_se(sd_bus_start(bus) >= 0);
-
- r = sd_bus_message_new_method_call(
- bus,
- &m,
- "org.freedesktop.systemd.test",
- "/",
- "org.freedesktop.systemd.test",
- "Exit");
- if (r < 0)
- return log_error_errno(r, "Failed to allocate method call: %m");
-
- r = sd_bus_call(bus, m, 0, &error, &reply);
- if (r < 0) {
- log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
- return r;
- }
-
- return 0;
-}
-
-static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
- bool client_anonymous_auth, bool server_anonymous_auth) {
-
- struct context c;
- pthread_t s;
- void *p;
- int r, q;
-
- zero(c);
-
- assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
-
- c.client_negotiate_unix_fds = client_negotiate_unix_fds;
- c.server_negotiate_unix_fds = server_negotiate_unix_fds;
- c.client_anonymous_auth = client_anonymous_auth;
- c.server_anonymous_auth = server_anonymous_auth;
-
- r = pthread_create(&s, NULL, server, &c);
- if (r != 0)
- return -r;
-
- r = client(&c);
-
- q = pthread_join(s, &p);
- if (q != 0)
- return -q;
-
- if (r < 0)
- return r;
-
- if (PTR_TO_INT(p) < 0)
- return PTR_TO_INT(p);
-
- return 0;
-}
-
-int main(int argc, char *argv[]) {
- int r;
-
- r = test_one(true, true, false, false);
- assert_se(r >= 0);
-
- r = test_one(true, false, false, false);
- assert_se(r >= 0);
-
- r = test_one(false, true, false, false);
- assert_se(r >= 0);
-
- r = test_one(false, false, false, false);
- assert_se(r >= 0);
-
- r = test_one(true, true, true, true);
- assert_se(r >= 0);
-
- r = test_one(true, true, false, true);
- assert_se(r >= 0);
-
- r = test_one(true, true, true, false);
- assert_se(r == -EPERM);
-
- return EXIT_SUCCESS;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-#include "log.h"
-#include "bus-signature.h"
-#include "bus-internal.h"
-
-int main(int argc, char *argv[]) {
- char prefix[256];
- int r;
-
- assert_se(signature_is_single("y", false));
- assert_se(signature_is_single("u", false));
- assert_se(signature_is_single("v", false));
- assert_se(signature_is_single("as", false));
- assert_se(signature_is_single("(ss)", false));
- assert_se(signature_is_single("()", false));
- assert_se(signature_is_single("(()()()()())", false));
- assert_se(signature_is_single("(((())))", false));
- assert_se(signature_is_single("((((s))))", false));
- assert_se(signature_is_single("{ss}", true));
- assert_se(signature_is_single("a{ss}", false));
- assert_se(!signature_is_single("uu", false));
- assert_se(!signature_is_single("", false));
- assert_se(!signature_is_single("(", false));
- assert_se(!signature_is_single(")", false));
- assert_se(!signature_is_single("())", false));
- assert_se(!signature_is_single("((())", false));
- assert_se(!signature_is_single("{)", false));
- assert_se(!signature_is_single("{}", true));
- assert_se(!signature_is_single("{sss}", true));
- assert_se(!signature_is_single("{s}", true));
- assert_se(!signature_is_single("{ss}", false));
- assert_se(!signature_is_single("{ass}", true));
- assert_se(!signature_is_single("a}", true));
-
- assert_se(signature_is_pair("yy"));
- assert_se(signature_is_pair("ss"));
- assert_se(signature_is_pair("sas"));
- assert_se(signature_is_pair("sv"));
- assert_se(signature_is_pair("sa(vs)"));
- assert_se(!signature_is_pair(""));
- assert_se(!signature_is_pair("va"));
- assert_se(!signature_is_pair("sss"));
- assert_se(!signature_is_pair("{s}ss"));
-
- assert_se(signature_is_valid("ssa{ss}sssub", true));
- assert_se(signature_is_valid("ssa{ss}sssub", false));
- assert_se(signature_is_valid("{ss}", true));
- assert_se(!signature_is_valid("{ss}", false));
- assert_se(signature_is_valid("", true));
- assert_se(signature_is_valid("", false));
-
- assert_se(signature_is_valid("sssusa(uuubbba(uu)uuuu)a{u(uuuvas)}", false));
-
- assert_se(!signature_is_valid("a", false));
- assert_se(signature_is_valid("as", false));
- assert_se(signature_is_valid("aas", false));
- assert_se(signature_is_valid("aaas", false));
- assert_se(signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad", false));
- assert_se(signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaas", false));
- assert_se(!signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau", false));
-
- assert_se(signature_is_valid("(((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))", false));
- assert_se(!signature_is_valid("((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))", false));
-
- assert_se(namespace_complex_pattern("", ""));
- assert_se(namespace_complex_pattern("foobar", "foobar"));
- assert_se(namespace_complex_pattern("foobar.waldo", "foobar.waldo"));
- assert_se(namespace_complex_pattern("foobar.", "foobar.waldo"));
- assert_se(namespace_complex_pattern("foobar.waldo", "foobar."));
- assert_se(!namespace_complex_pattern("foobar.waldo", "foobar"));
- assert_se(!namespace_complex_pattern("foobar", "foobar.waldo"));
- assert_se(!namespace_complex_pattern("", "foo"));
- assert_se(!namespace_complex_pattern("foo", ""));
- assert_se(!namespace_complex_pattern("foo.", ""));
-
- assert_se(path_complex_pattern("", ""));
- assert_se(path_complex_pattern("", "/"));
- assert_se(path_complex_pattern("/", ""));
- assert_se(path_complex_pattern("/", "/"));
- assert_se(path_complex_pattern("/foobar/", "/"));
- assert_se(path_complex_pattern("/foobar/", "/foobar"));
- assert_se(path_complex_pattern("/foobar", "/foobar"));
- assert_se(path_complex_pattern("/foobar", "/foobar/"));
- assert_se(!path_complex_pattern("/foobar", "/foobar/waldo"));
- assert_se(path_complex_pattern("/foobar/", "/foobar/waldo"));
-
- assert_se(namespace_simple_pattern("", ""));
- assert_se(namespace_simple_pattern("foobar", "foobar"));
- assert_se(namespace_simple_pattern("foobar.waldo", "foobar.waldo"));
- assert_se(namespace_simple_pattern("foobar", "foobar.waldo"));
- assert_se(!namespace_simple_pattern("foobar.waldo", "foobar"));
- assert_se(!namespace_simple_pattern("", "foo"));
- assert_se(!namespace_simple_pattern("foo", ""));
-
- assert_se(streq(object_path_startswith("/foo/bar", "/foo"), "bar"));
- assert_se(streq(object_path_startswith("/foo", "/foo"), ""));
- assert_se(streq(object_path_startswith("/foo", "/"), "foo"));
- assert_se(streq(object_path_startswith("/", "/"), ""));
- assert_se(!object_path_startswith("/foo", "/bar"));
- assert_se(!object_path_startswith("/", "/bar"));
- assert_se(!object_path_startswith("/foo", ""));
-
- assert_se(object_path_is_valid("/foo/bar"));
- assert_se(object_path_is_valid("/foo"));
- assert_se(object_path_is_valid("/"));
- assert_se(object_path_is_valid("/foo5"));
- assert_se(object_path_is_valid("/foo_5"));
- assert_se(!object_path_is_valid(""));
- assert_se(!object_path_is_valid("/foo/"));
- assert_se(!object_path_is_valid("//"));
- assert_se(!object_path_is_valid("//foo"));
- assert_se(!object_path_is_valid("/foo//bar"));
- assert_se(!object_path_is_valid("/foo/aaaäöä"));
-
- OBJECT_PATH_FOREACH_PREFIX(prefix, "/") {
- log_info("<%s>", prefix);
- assert_not_reached("???");
- }
-
- r = 0;
- OBJECT_PATH_FOREACH_PREFIX(prefix, "/xxx") {
- log_info("<%s>", prefix);
- assert_se(streq(prefix, "/"));
- assert_se(r == 0);
- r++;
- }
- assert_se(r == 1);
-
- r = 0;
- OBJECT_PATH_FOREACH_PREFIX(prefix, "/xxx/yyy/zzz") {
- log_info("<%s>", prefix);
- assert_se(r != 0 || streq(prefix, "/xxx/yyy"));
- assert_se(r != 1 || streq(prefix, "/xxx"));
- assert_se(r != 2 || streq(prefix, "/"));
- r++;
- }
- assert_se(r == 3);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/mman.h>
-
-#include "util.h"
-#include "log.h"
-#include "memfd-util.h"
-
-#include "sd-bus.h"
-#include "bus-message.h"
-#include "bus-kernel.h"
-#include "bus-dump.h"
-
-#define FIRST_ARRAY 17
-#define SECOND_ARRAY 33
-
-#define STRING_SIZE 123
-
-int main(int argc, char *argv[]) {
- _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
- const char *unique;
- uint8_t *p;
- sd_bus *a, *b;
- int r, bus_ref;
- sd_bus_message *m;
- int f;
- uint64_t sz;
- uint32_t u32;
- size_t i, l;
- char *s;
- _cleanup_close_ int sfd = -1;
-
- log_set_max_level(LOG_DEBUG);
-
- assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
-
- bus_ref = bus_kernel_create_bus(name, false, &bus_name);
- if (bus_ref == -ENOENT)
- return EXIT_TEST_SKIP;
-
- assert_se(bus_ref >= 0);
-
- address = strappend("kernel:path=", bus_name);
- assert_se(address);
-
- r = sd_bus_new(&a);
- assert_se(r >= 0);
-
- r = sd_bus_new(&b);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(a, address);
- assert_se(r >= 0);
-
- r = sd_bus_set_address(b, address);
- assert_se(r >= 0);
-
- r = sd_bus_start(a);
- assert_se(r >= 0);
-
- r = sd_bus_start(b);
- assert_se(r >= 0);
-
- r = sd_bus_get_unique_name(a, &unique);
- assert_se(r >= 0);
-
- r = sd_bus_message_new_method_call(b, &m, unique, "/a/path", "an.inter.face", "AMethod");
- assert_se(r >= 0);
-
- r = sd_bus_message_open_container(m, 'r', "aysay");
- assert_se(r >= 0);
-
- r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
- assert_se(r >= 0);
-
- p[0] = '<';
- memset(p+1, 'L', FIRST_ARRAY-2);
- p[FIRST_ARRAY-1] = '>';
-
- f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
- assert_se(f >= 0);
-
- s[0] = '<';
- for (i = 1; i < STRING_SIZE-2; i++)
- s[i] = '0' + (i % 10);
- s[STRING_SIZE-2] = '>';
- s[STRING_SIZE-1] = 0;
- munmap(s, STRING_SIZE);
-
- r = memfd_get_size(f, &sz);
- assert_se(r >= 0);
- assert_se(sz == STRING_SIZE);
-
- r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
- assert_se(r >= 0);
-
- close(f);
-
- f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
- assert_se(f >= 0);
-
- p[0] = '<';
- memset(p+1, 'P', SECOND_ARRAY-2);
- p[SECOND_ARRAY-1] = '>';
- munmap(p, SECOND_ARRAY);
-
- r = memfd_get_size(f, &sz);
- assert_se(r >= 0);
- assert_se(sz == SECOND_ARRAY);
-
- r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
- assert_se(r >= 0);
-
- close(f);
-
- r = sd_bus_message_close_container(m);
- assert_se(r >= 0);
-
- r = sd_bus_message_append(m, "u", 4711);
- assert_se(r >= 0);
-
- assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
- memcpy(p, "abcd\0", 6);
- munmap(p, 6);
- assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);
-
- r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
- assert_se(r >= 0);
-
- bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
-
- r = sd_bus_send(b, m, NULL);
- assert_se(r >= 0);
-
- sd_bus_message_unref(m);
-
- r = sd_bus_process(a, &m);
- assert_se(r > 0);
-
- bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
- sd_bus_message_rewind(m, true);
-
- r = sd_bus_message_enter_container(m, 'r', "aysay");
- assert_se(r > 0);
-
- r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
- assert_se(r > 0);
- assert_se(l == FIRST_ARRAY);
-
- assert_se(p[0] == '<');
- for (i = 1; i < l-1; i++)
- assert_se(p[i] == 'L');
- assert_se(p[l-1] == '>');
-
- r = sd_bus_message_read(m, "s", &s);
- assert_se(r > 0);
-
- assert_se(s[0] == '<');
- for (i = 1; i < STRING_SIZE-2; i++)
- assert_se(s[i] == (char) ('0' + (i % 10)));
- assert_se(s[STRING_SIZE-2] == '>');
- assert_se(s[STRING_SIZE-1] == 0);
-
- r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
- assert_se(r > 0);
- assert_se(l == SECOND_ARRAY);
-
- assert_se(p[0] == '<');
- for (i = 1; i < l-1; i++)
- assert_se(p[i] == 'P');
- assert_se(p[l-1] == '>');
-
- r = sd_bus_message_exit_container(m);
- assert_se(r > 0);
-
- r = sd_bus_message_read(m, "u", &u32);
- assert_se(r > 0);
- assert_se(u32 == 4711);
-
- r = sd_bus_message_read(m, "s", &s);
- assert_se(r > 0);
- assert_se(streq_ptr(s, "bcd"));
-
- sd_bus_message_unref(m);
-
- sd_bus_unref(a);
- sd_bus_unref(b);
-
- return 0;
-}
-../Makefile
\ No newline at end of file
+../../Makefile
\ No newline at end of file
#include <stdio.h>
#include <stddef.h>
#include <limits.h>
-#include <mqueue.h>
+//#include <mqueue.h>
#include "util.h"
#include "path-util.h"
#include "socket-util.h"
#include "sd-daemon.h"
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_listen_fds(int unset_environment) {
const char *e;
unsigned n;
_public_ int sd_is_fifo(int fd, const char *path) {
struct stat st_fd;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
if (fstat(fd, &st_fd) < 0)
return -errno;
_public_ int sd_is_special(int fd, const char *path) {
struct stat st_fd;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
if (fstat(fd, &st_fd) < 0)
return -errno;
return 1;
}
+#endif // 0
static int sd_is_socket_internal(int fd, int type, int listening) {
struct stat st_fd;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(type >= 0, -EINVAL);
if (fstat(fd, &st_fd) < 0)
_public_ int sd_is_socket(int fd, int family, int type, int listening) {
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(family >= 0, -EINVAL);
r = sd_is_socket_internal(fd, type, listening);
return 1;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
union sockaddr_union sockaddr = {};
socklen_t l = sizeof(sockaddr);
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
r = sd_is_socket_internal(fd, type, listening);
socklen_t l = sizeof(sockaddr);
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
r = sd_is_socket_internal(fd, type, listening);
if (r <= 0)
return 1;
}
+_public_ int sd_is_mq(int fd, const char *path) {
+ struct mq_attr attr;
+
+ assert_return(fd >= 0, -EBADF);
+
+ if (mq_getattr(fd, &attr) < 0)
+ return -errno;
+
+ if (path) {
+ char fpath[PATH_MAX];
+ struct stat a, b;
+
+ assert_return(path_is_absolute(path), -EINVAL);
+
+ if (fstat(fd, &a) < 0)
+ return -errno;
+
+ strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
+ fpath[sizeof(fpath)-1] = 0;
+
+ if (stat(fpath, &b) < 0)
+ return -errno;
+
+ if (a.st_dev != b.st_dev ||
+ a.st_ino != b.st_ino)
+ return 0;
+ }
+
+ return 1;
+}
+#endif // 0
+
_public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) {
union sockaddr_union sockaddr = {
.sa.sa_family = AF_UNIX,
return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
_cleanup_free_ char *p = NULL;
int r;
return !!S_ISDIR(st.st_mode);
}
+#endif // 0
_public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
const char *s, *p = ""; /* p is set to dummy value to do unsetting */
+++ /dev/null
-/***
- This file is part of systemd.
-
- Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#pragma once
-
-#include "hashmap.h"
-#include "set.h"
-
-struct sd_device {
- uint64_t n_ref;
-
- sd_device *parent;
- bool parent_set; /* no need to try to reload parent */
-
- OrderedHashmap *properties;
- Iterator properties_iterator;
- uint64_t properties_generation; /* changes whenever the properties are changed */
- uint64_t properties_iterator_generation; /* generation when iteration was started */
-
- /* the subset of the properties that should be written to the db*/
- OrderedHashmap *properties_db;
-
- Hashmap *sysattr_values; /* cached sysattr values */
-
- Set *sysattrs; /* names of sysattrs */
- Iterator sysattrs_iterator;
- bool sysattrs_read; /* don't try to re-read sysattrs once read */
-
- Set *tags;
- Iterator tags_iterator;
- uint64_t tags_generation; /* changes whenever the tags are changed */
- uint64_t tags_iterator_generation; /* generation when iteration was started */
- bool property_tags_outdated; /* need to update TAGS= property */
-
- Set *devlinks;
- Iterator devlinks_iterator;
- uint64_t devlinks_generation; /* changes whenever the devlinks are changed */
- uint64_t devlinks_iterator_generation; /* generation when iteration was started */
- bool property_devlinks_outdated; /* need to update DEVLINKS= property */
- int devlink_priority;
-
- char **properties_strv; /* the properties hashmap as a strv */
- uint8_t *properties_nulstr; /* the same as a nulstr */
- size_t properties_nulstr_len;
- bool properties_buf_outdated; /* need to reread hashmap */
-
- int watch_handle;
-
- char *syspath;
- const char *devpath;
- const char *sysnum;
- char *sysname;
- bool sysname_set; /* don't reread sysname */
-
- char *devtype;
- int ifindex;
- char *devname;
- dev_t devnum;
-
- char *subsystem;
- bool subsystem_set; /* don't reread subsystem */
- char *driver;
- bool driver_set; /* don't reread driver */
-
- char *id_filename;
-
- bool is_initialized;
- uint64_t usec_initialized;
-
- mode_t devmode;
- uid_t devuid;
- gid_t devgid;
-
- bool uevent_loaded; /* don't reread uevent */
- bool db_loaded; /* don't reread db */
-
- bool sealed; /* don't read more information from uevent/db */
- bool db_persist; /* don't clean up the db when switching from initrd to real root */
-};
-
-typedef enum DeviceAction {
- DEVICE_ACTION_ADD,
- DEVICE_ACTION_REMOVE,
- DEVICE_ACTION_CHANGE,
- DEVICE_ACTION_MOVE,
- DEVICE_ACTION_ONLINE,
- DEVICE_ACTION_OFFLINE,
- _DEVICE_ACTION_MAX,
- _DEVICE_ACTION_INVALID = -1,
-} DeviceAction;
-
-int device_new_aux(sd_device **ret);
-int device_add_property_aux(sd_device *device, const char *key, const char *value, bool db);
-int device_add_property_internal(sd_device *device, const char *key, const char *value);
-int device_read_uevent_file(sd_device *device);
-
-int device_set_syspath(sd_device *device, const char *_syspath, bool verify);
-int device_set_ifindex(sd_device *device, const char *ifindex);
-int device_set_devmode(sd_device *device, const char *devmode);
-int device_set_devname(sd_device *device, const char *_devname);
-int device_set_devtype(sd_device *device, const char *_devtype);
-int device_set_devnum(sd_device *device, const char *major, const char *minor);
-int device_set_subsystem(sd_device *device, const char *_subsystem);
-int device_set_driver(sd_device *device, const char *_driver);
-int device_set_usec_initialized(sd_device *device, const char *initialized);
-
-DeviceAction device_action_from_string(const char *s) _pure_;
-const char *device_action_to_string(DeviceAction a) _const_;
+++ /dev/null
-/***
- This file is part of systemd.
-
- Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <ctype.h>
-#include <sys/types.h>
-#include <net/if.h>
-
-#include "util.h"
-#include "macro.h"
-#include "refcnt.h"
-#include "path-util.h"
-#include "strxcpyx.h"
-#include "fileio.h"
-#include "hashmap.h"
-#include "set.h"
-#include "strv.h"
-#include "mkdir.h"
-
-#include "sd-device.h"
-
-#include "device-util.h"
-#include "device-internal.h"
-#include "device-private.h"
-
-int device_add_property(sd_device *device, const char *key, const char *value) {
- int r;
-
- assert(device);
- assert(key);
-
- r = device_add_property_aux(device, key, value, false);
- if (r < 0)
- return r;
-
- if (key[0] != '.') {
- r = device_add_property_aux(device, key, value, true);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-static int device_add_property_internal_from_string(sd_device *device, const char *str) {
- _cleanup_free_ char *key = NULL;
- char *value;
-
- assert(device);
- assert(str);
-
- key = strdup(str);
- if (!key)
- return -ENOMEM;
-
- value = strchr(key, '=');
- if (!value)
- return -EINVAL;
-
- *value = '\0';
-
- if (isempty(++value))
- value = NULL;
-
- return device_add_property_internal(device, key, value);
-}
-
-static int handle_db_line(sd_device *device, char key, const char *value) {
- char *path;
- int r;
-
- assert(device);
- assert(value);
-
- switch (key) {
- case 'S':
- path = strjoina("/dev/", value);
- r = device_add_devlink(device, path);
- if (r < 0)
- return r;
-
- break;
- case 'L':
- r = safe_atoi(value, &device->devlink_priority);
- if (r < 0)
- return r;
-
- break;
- case 'E':
- r = device_add_property_internal_from_string(device, value);
- if (r < 0)
- return r;
-
- break;
- case 'G':
- r = device_add_tag(device, value);
- if (r < 0)
- return r;
-
- break;
- case 'W':
- r = safe_atoi(value, &device->watch_handle);
- if (r < 0)
- return r;
-
- break;
- case 'I':
- r = device_set_usec_initialized(device, value);
- if (r < 0)
- return r;
-
- break;
- default:
- log_debug("device db: unknown key '%c'", key);
- }
-
- return 0;
-}
-
-void device_set_devlink_priority(sd_device *device, int priority) {
- assert(device);
-
- device->devlink_priority = priority;
-}
-
-void device_set_is_initialized(sd_device *device) {
- assert(device);
-
- device->is_initialized = true;
-}
-
-int device_ensure_usec_initialized(sd_device *device, sd_device *device_old) {
- char num[DECIMAL_STR_MAX(usec_t)];
- usec_t usec_initialized;
- int r;
-
- assert(device);
-
- if (device_old && device_old->usec_initialized > 0)
- usec_initialized = device_old->usec_initialized;
- else
- usec_initialized = now(CLOCK_MONOTONIC);
-
- r = snprintf(num, sizeof(num), USEC_FMT, usec_initialized);
- if (r < 0)
- return -errno;
-
- r = device_set_usec_initialized(device, num);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-static int device_read_db(sd_device *device) {
- _cleanup_free_ char *db = NULL;
- char *path;
- const char *id, *value;
- char key;
- size_t db_len;
- unsigned i;
- int r;
-
- enum {
- PRE_KEY,
- KEY,
- PRE_VALUE,
- VALUE,
- INVALID_LINE,
- } state = PRE_KEY;
-
- assert(device);
-
- if (device->db_loaded || device->sealed)
- return 0;
-
- r = device_get_id_filename(device, &id);
- if (r < 0)
- return r;
-
- path = strjoina("/run/udev/data/", id);
-
- r = read_full_file(path, &db, &db_len);
- if (r < 0) {
- if (r == -ENOENT)
- return 0;
- else {
- log_debug("sd-device: failed to read db '%s': %s", path, strerror(-r));
- return r;
- }
- }
-
- /* devices with a database entry are initialized */
- device_set_is_initialized(device);
-
- for (i = 0; i < db_len; i++) {
- switch (state) {
- case PRE_KEY:
- if (!strchr(NEWLINE, db[i])) {
- key = db[i];
-
- state = KEY;
- }
-
- break;
- case KEY:
- if (db[i] != ':') {
- log_debug("sd-device: ignoring invalid db entry with key '%c'", key);
-
- state = INVALID_LINE;
- } else {
- db[i] = '\0';
-
- state = PRE_VALUE;
- }
-
- break;
- case PRE_VALUE:
- value = &db[i];
-
- state = VALUE;
-
- break;
- case INVALID_LINE:
- if (strchr(NEWLINE, db[i]))
- state = PRE_KEY;
-
- break;
- case VALUE:
- if (strchr(NEWLINE, db[i])) {
- db[i] = '\0';
- r = handle_db_line(device, key, value);
- if (r < 0)
- log_debug("sd-device: failed to handle db entry '%c:%s': %s", key, value, strerror(-r));
-
- state = PRE_KEY;
- }
-
- break;
- default:
- assert_not_reached("invalid state when parsing db");
- }
- }
-
- device->db_loaded = true;
-
- return 0;
-}
-
-uint64_t device_get_properties_generation(sd_device *device) {
- assert(device);
-
- return device->properties_generation;
-}
-
-uint64_t device_get_tags_generation(sd_device *device) {
- assert(device);
-
- return device->tags_generation;
-}
-
-uint64_t device_get_devlinks_generation(sd_device *device) {
- assert(device);
-
- return device->devlinks_generation;
-}
-
-int device_get_devnode_mode(sd_device *device, mode_t *mode) {
- int r;
-
- assert(device);
- assert(mode);
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- *mode = device->devmode;
-
- return 0;
-}
-
-int device_get_devnode_uid(sd_device *device, uid_t *uid) {
- int r;
-
- assert(device);
- assert(uid);
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- *uid = device->devuid;
-
- return 0;
-}
-
-static int device_set_devuid(sd_device *device, const char *uid) {
- unsigned u;
- int r;
-
- assert(device);
- assert(uid);
-
- r = safe_atou(uid, &u);
- if (r < 0)
- return r;
-
- r = device_add_property_internal(device, "DEVUID", uid);
- if (r < 0)
- return r;
-
- device->devuid = u;
-
- return 0;
-}
-
-int device_get_devnode_gid(sd_device *device, gid_t *gid) {
- int r;
-
- assert(device);
- assert(gid);
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- *gid = device->devgid;
-
- return 0;
-}
-
-static int device_set_devgid(sd_device *device, const char *gid) {
- unsigned g;
- int r;
-
- assert(device);
- assert(gid);
-
- r = safe_atou(gid, &g);
- if (r < 0)
- return r;
-
- r = device_add_property_internal(device, "DEVGID", gid);
- if (r < 0)
- return r;
-
- device->devgid = g;
-
- return 0;
-}
-
-static int device_ammend(sd_device *device, const char *key, const char *value) {
- int r;
-
- assert(device);
- assert(key);
- assert(value);
-
- if (streq(key, "DEVPATH")) {
- char *path;
-
- path = strjoina("/sys", value);
-
- /* the caller must verify or trust this data (e.g., if it comes from the kernel) */
- r = device_set_syspath(device, path, false);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set syspath to '%s': %m", path);
- } else if (streq(key, "SUBSYSTEM")) {
- r = device_set_subsystem(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set subsystem to '%s': %m", value);
- } else if (streq(key, "DEVTYPE")) {
- r = device_set_devtype(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set devtype to '%s': %m", value);
- } else if (streq(key, "DEVNAME")) {
- r = device_set_devname(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set devname to '%s': %m", value);
- } else if (streq(key, "USEC_INITIALIZED")) {
- r = device_set_usec_initialized(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set usec-initialized to '%s': %m", value);
- } else if (streq(key, "DRIVER")) {
- r = device_set_driver(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set driver to '%s': %m", value);
- } else if (streq(key, "IFINDEX")) {
- r = device_set_ifindex(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set ifindex to '%s': %m", value);
- } else if (streq(key, "DEVMODE")) {
- r = device_set_devmode(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set devmode to '%s': %m", value);
- } else if (streq(key, "DEVUID")) {
- r = device_set_devuid(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set devuid to '%s': %m", value);
- } else if (streq(key, "DEVGID")) {
- r = device_set_devgid(device, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set devgid to '%s': %m", value);
- } else if (streq(key, "DEVLINKS")) {
- char *devlinks, *next;
-
- devlinks = strdupa(value);
-
- while ((next = strchr(devlinks, ' '))) {
- next[0] = '\0';
-
- r = device_add_devlink(device, devlinks);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not add devlink '%s': %m", devlinks);
-
- devlinks = next + 1;
- }
- } else if (streq(key, "TAGS")) {
- char *tags, *next;
-
- tags = strdupa(value);
-
- while ((next = strchr(tags, ':'))) {
- next[0] = '\0';
-
- r = device_add_tag(device, tags);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not add tag '%s': %m", tags);
-
- tags = next + 1;
- }
- } else {
- r = device_add_property_internal(device, key, value);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not add property '%s=%s': %m", key, value);
- }
-
- return 0;
-}
-
-static const char* const device_action_table[_DEVICE_ACTION_MAX] = {
- [DEVICE_ACTION_ADD] = "add",
- [DEVICE_ACTION_REMOVE] = "remove",
- [DEVICE_ACTION_CHANGE] = "change",
- [DEVICE_ACTION_MOVE] = "move",
- [DEVICE_ACTION_ONLINE] = "online",
- [DEVICE_ACTION_OFFLINE] = "offline",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(device_action, DeviceAction);
-
-static int device_append(sd_device *device, char *key, const char **_major, const char **_minor, uint64_t *_seqnum,
- DeviceAction *_action) {
- DeviceAction action = _DEVICE_ACTION_INVALID;
- uint64_t seqnum = 0;
- const char *major = NULL, *minor = NULL;
- char *value;
- int r;
-
- assert(device);
- assert(key);
- assert(_major);
- assert(_minor);
- assert(_seqnum);
- assert(_action);
-
- value = strchr(key, '=');
- if (!value) {
- log_debug("sd-device: not a key-value pair: '%s'", key);
- return -EINVAL;
- }
-
- *value = '\0';
-
- value++;
-
- if (streq(key, "MAJOR"))
- major = value;
- else if (streq(key, "MINOR"))
- minor = value;
- else {
- if (streq(key, "ACTION")) {
- action = device_action_from_string(value);
- if (action == _DEVICE_ACTION_INVALID)
- return -EINVAL;
- } else if (streq(key, "SEQNUM")) {
- r = safe_atou64(value, &seqnum);
- if (r < 0)
- return r;
- else if (seqnum == 0)
- /* kernel only sends seqnum > 0 */
- return -EINVAL;
- }
-
- r = device_ammend(device, key, value);
- if (r < 0)
- return r;
- }
-
- if (major != 0)
- *_major = major;
-
- if (minor != 0)
- *_minor = minor;
-
- if (action != _DEVICE_ACTION_INVALID)
- *_action = action;
-
- if (seqnum > 0)
- *_seqnum = seqnum;
-
- return 0;
-}
-
-void device_seal(sd_device *device) {
- assert(device);
-
- device->sealed = true;
-}
-
-static int device_verify(sd_device *device, DeviceAction action, uint64_t seqnum) {
- assert(device);
-
- if (!device->devpath || !device->subsystem || action == _DEVICE_ACTION_INVALID || seqnum == 0) {
- log_debug("sd-device: device created from strv lacks devpath, subsystem, action or seqnum");
- return -EINVAL;
- }
-
- device->sealed = true;
-
- return 0;
-}
-
-int device_new_from_strv(sd_device **ret, char **strv) {
- _cleanup_device_unref_ sd_device *device = NULL;
- char **key;
- const char *major = NULL, *minor = NULL;
- DeviceAction action = _DEVICE_ACTION_INVALID;
- uint64_t seqnum;
- int r;
-
- assert(ret);
- assert(strv);
-
- r = device_new_aux(&device);
- if (r < 0)
- return r;
-
- STRV_FOREACH(key, strv) {
- r = device_append(device, *key, &major, &minor, &seqnum, &action);
- if (r < 0)
- return r;
- }
-
- if (major) {
- r = device_set_devnum(device, major, minor);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set devnum %s:%s: %m", major, minor);
- }
-
- r = device_verify(device, action, seqnum);
- if (r < 0)
- return r;
-
- *ret = device;
- device = NULL;
-
- return 0;
-}
-
-int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) {
- _cleanup_device_unref_ sd_device *device = NULL;
- const char *major = NULL, *minor = NULL;
- DeviceAction action = _DEVICE_ACTION_INVALID;
- uint64_t seqnum;
- unsigned i = 0;
- int r;
-
- assert(ret);
- assert(nulstr);
- assert(len);
-
- r = device_new_aux(&device);
- if (r < 0)
- return r;
-
- while (i < len) {
- char *key;
- const char *end;
-
- key = (char*)&nulstr[i];
- end = memchr(key, '\0', len - i);
- if (!end) {
- log_debug("sd-device: failed to parse nulstr");
- return -EINVAL;
- }
- i += end - key + 1;
-
- r = device_append(device, key, &major, &minor, &seqnum, &action);
- if (r < 0)
- return r;
- }
-
- if (major) {
- r = device_set_devnum(device, major, minor);
- if (r < 0)
- return log_debug_errno(r, "sd-device: could not set devnum %s:%s: %m", major, minor);
- }
-
- r = device_verify(device, action, seqnum);
- if (r < 0)
- return r;
-
- *ret = device;
- device = NULL;
-
- return 0;
-}
-
-static int device_update_properties_bufs(sd_device *device) {
- const char *val, *prop;
- char **buf_strv = NULL;
- uint8_t *buf_nulstr = NULL;
- size_t allocated_nulstr = 0, allocated_strv = 0;
- size_t nulstr_len = 0, strv_size = 0;
-
- assert(device);
-
- FOREACH_DEVICE_PROPERTY(device, prop, val) {
- size_t len = 0;
-
- len = strlen(prop) + 1 + strlen(val);
-
- buf_nulstr = GREEDY_REALLOC0(buf_nulstr, allocated_nulstr, nulstr_len + len + 2);
- if (!buf_nulstr)
- return -ENOMEM;
-
- buf_strv = GREEDY_REALLOC0(buf_strv, allocated_strv, strv_size + 2);
- if (!buf_strv)
- return -ENOMEM;
-
- buf_strv[++ strv_size] = (char *)&buf_nulstr[nulstr_len];
- strscpyl((char *)buf_nulstr + nulstr_len, len + 1, prop, "=", val, NULL);
- nulstr_len += len + 1;
- }
-
- free(device->properties_nulstr);
- free(device->properties_strv);
- device->properties_nulstr = buf_nulstr;
- device->properties_nulstr_len = nulstr_len;
- device->properties_strv = buf_strv;
-
- device->properties_buf_outdated = false;
-
- return 0;
-}
-
-int device_get_properties_nulstr(sd_device *device, const uint8_t **nulstr, size_t *len) {
- int r;
-
- assert(device);
- assert(nulstr);
- assert(len);
-
- if (device->properties_buf_outdated) {
- r = device_update_properties_bufs(device);
- if (r < 0)
- return r;
- }
-
- *nulstr = device->properties_nulstr;
- *len = device->properties_nulstr_len;
-
- return 0;
-}
-
-int device_get_properties_strv(sd_device *device, char ***strv) {
- int r;
-
- assert(device);
- assert(strv);
-
- r = device_update_properties_bufs(device);
- if (r < 0)
- return r;
-
- *strv = device->properties_strv;
-
- return 0;
-}
-
-int device_get_devlink_priority(sd_device *device, int *priority) {
- int r;
-
- assert(device);
- assert(priority);
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- *priority = device->devlink_priority;
-
- return 0;
-}
-
-int device_get_watch_handle(sd_device *device, int *handle) {
- int r;
-
- assert(device);
- assert(handle);
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- *handle = device->watch_handle;
-
- return 0;
-}
-
-void device_set_watch_handle(sd_device *device, int handle) {
- assert(device);
-
- device->watch_handle = handle;
-}
-
-int device_rename(sd_device *device, const char *name) {
- _cleanup_free_ char *dirname = NULL;
- char *new_syspath;
- const char *interface;
- int r;
-
- assert(device);
- assert(name);
-
- dirname = dirname_malloc(device->syspath);
- if (!dirname)
- return -ENOMEM;
-
- new_syspath = strjoina(dirname, "/", name);
-
- /* the user must trust that the new name is correct */
- r = device_set_syspath(device, new_syspath, false);
- if (r < 0)
- return r;
-
- r = sd_device_get_property_value(device, "INTERFACE", &interface);
- if (r >= 0) {
- r = device_add_property_internal(device, "INTERFACE", name);
- if (r < 0)
- return r;
-
- /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
- r = device_add_property_internal(device, "INTERFACE_OLD", interface);
- if (r < 0)
- return r;
- } else if (r != -ENOENT)
- return r;
-
- return 0;
-}
-
-int device_shallow_clone(sd_device *old_device, sd_device **new_device) {
- _cleanup_device_unref_ sd_device *ret = NULL;
- int r;
-
- assert(old_device);
- assert(new_device);
-
- r = device_new_aux(&ret);
- if (r < 0)
- return r;
-
- r = device_set_syspath(ret, old_device->syspath, false);
- if (r < 0)
- return r;
-
- r = device_set_subsystem(ret, old_device->subsystem);
- if (r < 0)
- return r;
-
- ret->devnum = old_device->devnum;
-
- *new_device = ret;
- ret = NULL;
-
- return 0;
-}
-
-int device_clone_with_db(sd_device *old_device, sd_device **new_device) {
- _cleanup_device_unref_ sd_device *ret = NULL;
- int r;
-
- assert(old_device);
- assert(new_device);
-
- r = device_shallow_clone(old_device, &ret);
- if (r < 0)
- return r;
-
- r = device_read_db(ret);
- if (r < 0)
- return r;
-
- ret->sealed = true;
-
- *new_device = ret;
- ret = NULL;
-
- return 0;
-}
-
-int device_new_from_synthetic_event(sd_device **new_device, const char *syspath, const char *action) {
- _cleanup_device_unref_ sd_device *ret = NULL;
- int r;
-
- assert(new_device);
- assert(syspath);
- assert(action);
-
- r = sd_device_new_from_syspath(&ret, syspath);
- if (r < 0)
- return r;
-
- r = device_read_uevent_file(ret);
- if (r < 0)
- return r;
-
- r = device_add_property_internal(ret, "ACTION", action);
- if (r < 0)
- return r;
-
- *new_device = ret;
- ret = NULL;
-
- return 0;
-}
-
-int device_copy_properties(sd_device *device_dst, sd_device *device_src) {
- const char *property, *value;
- int r;
-
- assert(device_dst);
- assert(device_src);
-
- FOREACH_DEVICE_PROPERTY(device_src, property, value) {
- r = device_add_property(device_dst, property, value);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-void device_cleanup_tags(sd_device *device) {
- assert(device);
-
- set_free_free(device->tags);
- device->tags = NULL;
- device->property_tags_outdated = true;
- device->tags_generation ++;
-}
-
-void device_cleanup_devlinks(sd_device *device) {
- assert(device);
-
- set_free_free(device->devlinks);
- device->devlinks = NULL;
- device->property_devlinks_outdated = true;
- device->devlinks_generation ++;
-}
-
-void device_remove_tag(sd_device *device, const char *tag) {
- assert(device);
- assert(tag);
-
- free(set_remove(device->tags, tag));
- device->property_tags_outdated = true;
- device->tags_generation ++;
-}
-
-static int device_tag(sd_device *device, const char *tag, bool add) {
- const char *id;
- char *path;
- int r;
-
- assert(device);
- assert(tag);
-
- r = device_get_id_filename(device, &id);
- if (r < 0)
- return r;
-
- path = strjoina("/run/udev/tags/", tag, "/", id);
-
- if (add) {
- r = touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0444);
- if (r < 0)
- return r;
- } else {
- r = unlink(path);
- if (r < 0 && errno != ENOENT)
- return -errno;
- }
-
- return 0;
-}
-
-int device_tag_index(sd_device *device, sd_device *device_old, bool add) {
- const char *tag;
- int r = 0, k;
-
- if (add && device_old) {
- /* delete possible left-over tags */
- FOREACH_DEVICE_TAG(device_old, tag) {
- if (!sd_device_has_tag(device, tag)) {
- k = device_tag(device_old, tag, false);
- if (r >= 0 && k < 0)
- r = k;
- }
- }
- }
-
- FOREACH_DEVICE_TAG(device, tag) {
- k = device_tag(device, tag, add);
- if (r >= 0 && k < 0)
- r = k;
- }
-
- return r;
-}
-
-static bool device_has_info(sd_device *device) {
- assert(device);
-
- if (!set_isempty(device->devlinks))
- return true;
-
- if (device->devlink_priority != 0)
- return true;
-
- if (!ordered_hashmap_isempty(device->properties_db))
- return true;
-
- if (!set_isempty(device->tags))
- return true;
-
- if (device->watch_handle >= 0)
- return true;
-
- return false;
-}
-
-void device_set_db_persist(sd_device *device) {
- assert(device);
-
- device->db_persist = true;
-}
-
-int device_update_db(sd_device *device) {
- const char *id;
- char *path;
- _cleanup_fclose_ FILE *f = NULL;
- _cleanup_free_ char *path_tmp = NULL;
- bool has_info;
- int r;
-
- assert(device);
-
- has_info = device_has_info(device);
-
- r = device_get_id_filename(device, &id);
- if (r < 0)
- return r;
-
- path = strjoina("/run/udev/data/", id);
-
- /* do not store anything for otherwise empty devices */
- if (!has_info && major(device->devnum) == 0 && device->ifindex == 0) {
- r = unlink(path);
- if (r < 0 && errno != ENOENT)
- return -errno;
-
- return 0;
- }
-
- /* write a database file */
- r = mkdir_parents(path, 0755);
- if (r < 0)
- return r;
-
- r = fopen_temporary(path, &f, &path_tmp);
- if (r < 0)
- return r;
-
- /*
- * set 'sticky' bit to indicate that we should not clean the
- * database when we transition from initramfs to the real root
- */
- if (device->db_persist) {
- r = fchmod(fileno(f), 01644);
- if (r < 0) {
- r = -errno;
- goto fail;
- }
- } else {
- r = fchmod(fileno(f), 0644);
- if (r < 0) {
- r = -errno;
- goto fail;
- }
- }
-
- if (has_info) {
- const char *property, *value, *tag;
- Iterator i;
-
- if (major(device->devnum) > 0) {
- const char *devlink;
-
- FOREACH_DEVICE_DEVLINK(device, devlink)
- fprintf(f, "S:%s\n", devlink + strlen("/dev/"));
-
- if (device->devlink_priority != 0)
- fprintf(f, "L:%i\n", device->devlink_priority);
-
- if (device->watch_handle >= 0)
- fprintf(f, "W:%i\n", device->watch_handle);
- }
-
- if (device->usec_initialized > 0)
- fprintf(f, "I:"USEC_FMT"\n", device->usec_initialized);
-
- ORDERED_HASHMAP_FOREACH_KEY(value, property, device->properties_db, i)
- fprintf(f, "E:%s=%s\n", property, value);
-
- FOREACH_DEVICE_TAG(device, tag)
- fprintf(f, "G:%s\n", tag);
- }
-
- r = fflush_and_check(f);
- if (r < 0)
- goto fail;
-
- r = rename(path_tmp, path);
- if (r < 0) {
- r = -errno;
- goto fail;
- }
-
- log_debug("created %s file '%s' for '%s'", has_info ? "db" : "empty",
- path, device->devpath);
-
- return 0;
-
-fail:
- log_error_errno(r, "failed to create %s file '%s' for '%s'", has_info ? "db" : "empty",
- path, device->devpath);
- unlink(path);
- unlink(path_tmp);
-
- return r;
-}
-
-int device_delete_db(sd_device *device) {
- const char *id;
- char *path;
- int r;
-
- assert(device);
-
- r = device_get_id_filename(device, &id);
- if (r < 0)
- return r;
-
- path = strjoina("/run/udev/data/", id);
-
- r = unlink(path);
- if (r < 0 && errno != ENOENT)
- return -errno;
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len);
-int device_new_from_strv(sd_device **ret, char **strv);
-
-int device_get_id_filename(sd_device *device, const char **ret);
-
-int device_get_devlink_priority(sd_device *device, int *priority);
-int device_get_watch_handle(sd_device *device, int *handle);
-int device_get_devnode_mode(sd_device *device, mode_t *mode);
-int device_get_devnode_uid(sd_device *device, uid_t *uid);
-int device_get_devnode_gid(sd_device *device, gid_t *gid);
-
-void device_seal(sd_device *device);
-void device_set_is_initialized(sd_device *device);
-void device_set_watch_handle(sd_device *device, int fd);
-void device_set_db_persist(sd_device *device);
-void device_set_devlink_priority(sd_device *device, int priority);
-int device_ensure_usec_initialized(sd_device *devcie, sd_device *device_old);
-int device_add_devlink(sd_device *device, const char *devlink);
-int device_add_property(sd_device *device, const char *property, const char *value);
-int device_add_tag(sd_device *device, const char *tag);
-void device_remove_tag(sd_device *device, const char *tag);
-void device_cleanup_tags(sd_device *device);
-void device_cleanup_devlinks(sd_device *device);
-
-uint64_t device_get_properties_generation(sd_device *device);
-uint64_t device_get_tags_generation(sd_device *device);
-uint64_t device_get_devlinks_generation(sd_device *device);
-
-int device_get_properties_nulstr(sd_device *device, const uint8_t **nulstr, size_t *len);
-int device_get_properties_strv(sd_device *device, char ***strv);
-
-int device_rename(sd_device *device, const char *name);
-int device_shallow_clone(sd_device *old_device, sd_device **new_device);
-int device_clone_with_db(sd_device *old_device, sd_device **new_device);
-int device_copy_properties(sd_device *device_dst, sd_device *device_src);
-int device_new_from_synthetic_event(sd_device **new_device, const char *syspath, const char *action);
-
-int device_tag_index(sd_device *dev, sd_device *dev_old, bool add);
-int device_update_db(sd_device *device);
-int device_delete_db(sd_device *device);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_device*, sd_device_unref);
-
-#define _cleanup_device_unref_ _cleanup_(sd_device_unrefp)
-
-#define FOREACH_DEVICE_PROPERTY(device, key, value) \
- for (key = sd_device_get_property_first(device, &(value)); \
- key; \
- key = sd_device_get_property_next(device, &(value)))
-
-#define FOREACH_DEVICE_TAG(device, tag) \
- for (tag = sd_device_get_tag_first(device); \
- tag; \
- tag = sd_device_get_tag_next(device))
-
-#define FOREACH_DEVICE_SYSATTR(device, attr) \
- for (attr = sd_device_get_sysattr_first(device); \
- attr; \
- attr = sd_device_get_sysattr_next(device))
-
-#define FOREACH_DEVICE_DEVLINK(device, devlink) \
- for (devlink = sd_device_get_devlink_first(device); \
- devlink; \
- devlink = sd_device_get_devlink_next(device))
+++ /dev/null
-/***
- This file is part of systemd.
-
- Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <ctype.h>
-#include <sys/types.h>
-#include <net/if.h>
-
-#include "util.h"
-#include "macro.h"
-#include "path-util.h"
-#include "strxcpyx.h"
-#include "fileio.h"
-#include "hashmap.h"
-#include "set.h"
-#include "strv.h"
-
-#include "sd-device.h"
-
-#include "device-util.h"
-#include "device-private.h"
-#include "device-internal.h"
-
-int device_new_aux(sd_device **ret) {
- _cleanup_device_unref_ sd_device *device = NULL;
-
- assert(ret);
-
- device = new0(sd_device, 1);
- if (!device)
- return -ENOMEM;
-
- device->n_ref = 1;
- device->watch_handle = -1;
-
- *ret = device;
- device = NULL;
-
- return 0;
-}
-
-_public_ sd_device *sd_device_ref(sd_device *device) {
- if (device)
- assert_se(++ device->n_ref >= 2);
-
- return device;
-}
-
-_public_ sd_device *sd_device_unref(sd_device *device) {
- if (device && -- device->n_ref == 0) {
- sd_device_unref(device->parent);
- free(device->syspath);
- free(device->sysname);
- free(device->devtype);
- free(device->devname);
- free(device->subsystem);
- free(device->driver);
- free(device->id_filename);
- free(device->properties_strv);
- free(device->properties_nulstr);
-
- ordered_hashmap_free_free_free(device->properties);
- ordered_hashmap_free_free_free(device->properties_db);
- hashmap_free_free_free(device->sysattr_values);
- set_free_free(device->sysattrs);
- set_free_free(device->tags);
- set_free_free(device->devlinks);
-
- free(device);
- }
-
- return NULL;
-}
-
-int device_add_property_aux(sd_device *device, const char *_key, const char *_value, bool db) {
- OrderedHashmap **properties;
-
- assert(device);
- assert(_key);
-
- if (db)
- properties = &device->properties_db;
- else
- properties = &device->properties;
-
- if (_value) {
- _cleanup_free_ char *key = NULL, *value = NULL, *old_key = NULL, *old_value = NULL;
- int r;
-
- r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops);
- if (r < 0)
- return r;
-
- key = strdup(_key);
- if (!key)
- return -ENOMEM;
-
- value = strdup(_value);
- if (!value)
- return -ENOMEM;
-
- old_value = ordered_hashmap_get2(*properties, key, (void**) &old_key);
-
- r = ordered_hashmap_replace(*properties, key, value);
- if (r < 0)
- return r;
-
- key = NULL;
- value = NULL;
- } else {
- _cleanup_free_ char *key = NULL;
- _cleanup_free_ char *value = NULL;
-
- value = ordered_hashmap_remove2(*properties, _key, (void**) &key);
- }
-
- if (!db) {
- device->properties_generation ++;
- device->properties_buf_outdated = true;
- }
-
- return 0;
-}
-
-int device_add_property_internal(sd_device *device, const char *key, const char *value) {
- return device_add_property_aux(device, key, value, false);
-}
-
-int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
- _cleanup_free_ char *syspath = NULL;
- const char *devpath;
- int r;
-
- assert(device);
- assert(_syspath);
-
- /* must be a subdirectory of /sys */
- if (!path_startswith(_syspath, "/sys/")) {
- log_debug("sd-device: syspath '%s' is not a subdirectory of /sys", _syspath);
- return -EINVAL;
- }
-
- if (verify) {
- r = readlink_and_canonicalize(_syspath, &syspath);
- if (r == -EINVAL) {
- /* not a symlink */
- syspath = canonicalize_file_name(_syspath);
- if (!syspath) {
- log_debug("sd-device: could not canonicalize '%s': %m", _syspath);
- return -errno;
- }
- /* ignore errors due to the link not being a symlink */
- } else if (r < 0 && r != -EINVAL) {
- log_debug("sd-device: could not get target of '%s': %s", _syspath, strerror(-r));
- return r;
- }
-
- if (path_startswith(syspath, "/sys/devices/")) {
- char *path;
-
- /* all 'devices' require an 'uevent' file */
- path = strjoina(syspath, "/uevent");
- r = access(path, F_OK);
- if (r < 0) {
- log_debug("sd-device: %s does not have an uevent file: %m", syspath);
- return -errno;
- }
- } else {
- /* everything else just just needs to be a directory */
- if (!is_dir(syspath, false)) {
- log_debug("sd-device: %s is not a directory", syspath);
- return -EINVAL;
- }
- }
- } else {
- syspath = strdup(_syspath);
- if (!syspath)
- return -ENOMEM;
- }
-
- devpath = syspath + strlen("/sys");
-
- r = device_add_property_internal(device, "DEVPATH", devpath);
- if (r < 0)
- return r;
-
- free(device->syspath);
- device->syspath = syspath;
- syspath = NULL;
-
- device->devpath = devpath;
-
- return 0;
-}
-
-_public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) {
- _cleanup_device_unref_ sd_device *device = NULL;
- int r;
-
- assert_return(ret, -EINVAL);
- assert_return(syspath, -EINVAL);
-
- r = device_new_aux(&device);
- if (r < 0)
- return r;
-
- r = device_set_syspath(device, syspath, true);
- if (r < 0)
- return r;
-
- *ret = device;
- device = NULL;
-
- return 0;
-}
-
-_public_ int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum) {
- char *syspath;
- char id[DECIMAL_STR_MAX(unsigned) * 2 + 1];
-
- assert_return(ret, -EINVAL);
- assert_return(type == 'b' || type == 'c', -EINVAL);
-
- /* use /sys/dev/{block,char}/<maj>:<min> link */
- snprintf(id, sizeof(id), "%u:%u", major(devnum), minor(devnum));
-
- syspath = strjoina("/sys/dev/", (type == 'b' ? "block" : "char"), "/", id);
-
- return sd_device_new_from_syspath(ret, syspath);
-}
-
-_public_ int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *subsystem, const char *sysname) {
- char *syspath;
-
- assert_return(ret, -EINVAL);
- assert_return(subsystem, -EINVAL);
- assert_return(sysname, -EINVAL);
-
- if (streq(subsystem, "subsystem")) {
- syspath = strjoina("/sys/subsystem/", sysname);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
-
- syspath = strjoina("/sys/bus/", sysname);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
-
- syspath = strjoina("/sys/class/", sysname);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
- } else if (streq(subsystem, "module")) {
- syspath = strjoina("/sys/module/", sysname);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
- } else if (streq(subsystem, "drivers")) {
- char subsys[PATH_MAX];
- char *driver;
-
- strscpy(subsys, sizeof(subsys), sysname);
- driver = strchr(subsys, ':');
- if (driver) {
- driver[0] = '\0';
- driver++;
-
- syspath = strjoina("/sys/subsystem/", subsys, "/drivers/", driver);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
-
- syspath = strjoina("/sys/bus/", subsys, "/drivers/", driver);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
- } else
- return -EINVAL;
- } else {
- syspath = strjoina("/sys/subsystem/", subsystem, "/devices/", sysname);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
-
- syspath = strjoina("/sys/bus/", subsystem, "/devices/", sysname);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
-
- syspath = strjoina("/sys/class/", subsystem, "/", sysname);
- if (access(syspath, F_OK) >= 0)
- return sd_device_new_from_syspath(ret, syspath);
- }
-
- return -ENOENT;
-}
-
-int device_set_devtype(sd_device *device, const char *_devtype) {
- _cleanup_free_ char *devtype = NULL;
- int r;
-
- assert(device);
- assert(_devtype);
-
- devtype = strdup(_devtype);
- if (!devtype)
- return -ENOMEM;
-
- r = device_add_property_internal(device, "DEVTYPE", devtype);
- if (r < 0)
- return r;
-
- free(device->devtype);
- device->devtype = devtype;
- devtype = NULL;
-
- return 0;
-}
-
-int device_set_ifindex(sd_device *device, const char *_ifindex) {
- int ifindex, r;
-
- assert(device);
- assert(_ifindex);
-
- r = safe_atoi(_ifindex, &ifindex);
- if (r < 0)
- return r;
-
- if (ifindex <= 0)
- return -EINVAL;
-
- r = device_add_property_internal(device, "IFINDEX", _ifindex);
- if (r < 0)
- return r;
-
- device->ifindex = ifindex;
-
- return 0;
-}
-
-int device_set_devname(sd_device *device, const char *_devname) {
- _cleanup_free_ char *devname = NULL;
- int r;
-
- assert(device);
- assert(_devname);
-
- if (_devname[0] != '/') {
- r = asprintf(&devname, "/dev/%s", _devname);
- if (r < 0)
- return -ENOMEM;
- } else {
- devname = strdup(_devname);
- if (!devname)
- return -ENOMEM;
- }
-
- r = device_add_property_internal(device, "DEVNAME", devname);
- if (r < 0)
- return r;
-
- free(device->devname);
- device->devname = devname;
- devname = NULL;
-
- return 0;
-}
-
-int device_set_devmode(sd_device *device, const char *_devmode) {
- unsigned devmode;
- int r;
-
- assert(device);
- assert(_devmode);
-
- r = safe_atou(_devmode, &devmode);
- if (r < 0)
- return r;
-
- if (devmode > 07777)
- return -EINVAL;
-
- r = device_add_property_internal(device, "DEVMODE", _devmode);
- if (r < 0)
- return r;
-
- device->devmode = devmode;
-
- return 0;
-}
-
-int device_set_devnum(sd_device *device, const char *major, const char *minor) {
- unsigned maj = 0, min = 0;
- int r;
-
- assert(device);
- assert(major);
-
- r = safe_atou(major, &maj);
- if (r < 0)
- return r;
- if (!maj)
- return 0;
-
- if (minor) {
- r = safe_atou(minor, &min);
- if (r < 0)
- return r;
- }
-
- r = device_add_property_internal(device, "MAJOR", major);
- if (r < 0)
- return r;
-
- if (minor) {
- r = device_add_property_internal(device, "MINOR", minor);
- if (r < 0)
- return r;
- }
-
- device->devnum = makedev(maj, min);
-
- return 0;
-}
-
-static int handle_uevent_line(sd_device *device, const char *key, const char *value, const char **major, const char **minor) {
- int r;
-
- assert(device);
- assert(key);
- assert(value);
- assert(major);
- assert(minor);
-
- if (streq(key, "DEVTYPE")) {
- r = device_set_devtype(device, value);
- if (r < 0)
- return r;
- } else if (streq(key, "IFINDEX")) {
- r = device_set_ifindex(device, value);
- if (r < 0)
- return r;
- } else if (streq(key, "DEVNAME")) {
- r = device_set_devname(device, value);
- if (r < 0)
- return r;
- } else if (streq(key, "DEVMODE")) {
- r = device_set_devmode(device, value);
- if (r < 0)
- return r;
- } else if (streq(key, "MAJOR"))
- *major = value;
- else if (streq(key, "MINOR"))
- *minor = value;
- else {
- r = device_add_property_internal(device, key, value);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-int device_read_uevent_file(sd_device *device) {
- _cleanup_free_ char *uevent = NULL;
- const char *syspath, *key, *value, *major = NULL, *minor = NULL;
- char *path;
- size_t uevent_len;
- unsigned i;
- int r;
-
- enum {
- PRE_KEY,
- KEY,
- PRE_VALUE,
- VALUE,
- INVALID_LINE,
- } state = PRE_KEY;
-
- assert(device);
-
- if (device->uevent_loaded || device->sealed)
- return 0;
-
- r = sd_device_get_syspath(device, &syspath);
- if (r < 0)
- return r;
-
- path = strjoina(syspath, "/uevent");
-
- r = read_full_file(path, &uevent, &uevent_len);
- if (r < 0) {
- log_debug("sd-device: failed to read uevent file '%s': %s", path, strerror(-r));
- return r;
- }
-
- for (i = 0; i < uevent_len; i++) {
- switch (state) {
- case PRE_KEY:
- if (!strchr(NEWLINE, uevent[i])) {
- key = &uevent[i];
-
- state = KEY;
- }
-
- break;
- case KEY:
- if (uevent[i] == '=') {
- uevent[i] = '\0';
-
- state = PRE_VALUE;
- } else if (strchr(NEWLINE, uevent[i])) {
- uevent[i] = '\0';
- log_debug("sd-device: ignoring invalid uevent line '%s'", key);
-
- state = PRE_KEY;
- }
-
- break;
- case PRE_VALUE:
- value = &uevent[i];
-
- state = VALUE;
-
- break;
- case VALUE:
- if (strchr(NEWLINE, uevent[i])) {
- uevent[i] = '\0';
-
- r = handle_uevent_line(device, key, value, &major, &minor);
- if (r < 0)
- log_debug("sd-device: failed to handle uevent entry '%s=%s': %s", key, value, strerror(-r));
-
- state = PRE_KEY;
- }
-
- break;
- default:
- assert_not_reached("invalid state when parsing uevent file");
- }
- }
-
- if (major) {
- r = device_set_devnum(device, major, minor);
- if (r < 0)
- log_debug("sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %s", major, minor, path, strerror(-r));
- }
-
- device->uevent_loaded = true;
-
- return 0;
-}
-
-_public_ int sd_device_get_ifindex(sd_device *device, int *ifindex) {
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(ifindex, -EINVAL);
-
- r = device_read_uevent_file(device);
- if (r < 0)
- return r;
-
- *ifindex = device->ifindex;
-
- return 0;
-}
-
-_public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
- int r;
-
- assert_return(ret, -EINVAL);
- assert_return(id, -EINVAL);
-
- switch (id[0]) {
- case 'b':
- case 'c':
- {
- char type;
- int maj, min;
-
- r = sscanf(id, "%c%i:%i", &type, &maj, &min);
- if (r != 3)
- return -EINVAL;
-
- return sd_device_new_from_devnum(ret, type, makedev(maj, min));
- }
- case 'n':
- {
- _cleanup_device_unref_ sd_device *device = NULL;
- _cleanup_close_ int sk = -1;
- struct ifreq ifr = {};
- int ifindex;
-
- r = safe_atoi(&id[1], &ifr.ifr_ifindex);
- if (r < 0)
- return r;
- else if (ifr.ifr_ifindex <= 0)
- return -EINVAL;
-
- sk = socket(PF_INET, SOCK_DGRAM, 0);
- if (sk < 0)
- return -errno;
-
- r = ioctl(sk, SIOCGIFNAME, &ifr);
- if (r < 0)
- return -errno;
-
- r = sd_device_new_from_subsystem_sysname(&device, "net", ifr.ifr_name);
- if (r < 0)
- return r;
-
- r = sd_device_get_ifindex(device, &ifindex);
- if (r < 0)
- return r;
-
- /* this si racey, so we might end up with the wrong device */
- if (ifr.ifr_ifindex != ifindex)
- return -ENODEV;
-
- *ret = device;
- device = NULL;
-
- return 0;
- }
- case '+':
- {
- char subsys[PATH_MAX];
- char *sysname;
-
- (void)strscpy(subsys, sizeof(subsys), id + 1);
- sysname = strchr(subsys, ':');
- if (!sysname)
- return -EINVAL;
-
- sysname[0] = '\0';
- sysname ++;
-
- return sd_device_new_from_subsystem_sysname(ret, subsys, sysname);
- }
- default:
- return -EINVAL;
- }
-}
-
-_public_ int sd_device_get_syspath(sd_device *device, const char **ret) {
- assert_return(device, -EINVAL);
- assert_return(ret, -EINVAL);
-
- assert(path_startswith(device->syspath, "/sys/"));
-
- *ret = device->syspath;
-
- return 0;
-}
-
-static int device_new_from_child(sd_device **ret, sd_device *child) {
- _cleanup_free_ char *path = NULL;
- const char *subdir, *syspath;
- int r;
-
- assert(ret);
- assert(child);
-
- r = sd_device_get_syspath(child, &syspath);
- if (r < 0)
- return r;
-
- path = strdup(syspath);
- if (!path)
- return -ENOMEM;
- subdir = path + strlen("/sys");
-
- for (;;) {
- char *pos;
-
- pos = strrchr(subdir, '/');
- if (!pos || pos < subdir + 2)
- break;
-
- *pos = '\0';
-
- r = sd_device_new_from_syspath(ret, path);
- if (r < 0)
- continue;
-
- return 0;
- }
-
- return -ENOENT;
-}
-
-_public_ int sd_device_get_parent(sd_device *child, sd_device **ret) {
-
- assert_return(ret, -EINVAL);
- assert_return(child, -EINVAL);
-
- if (!child->parent_set) {
- child->parent_set = true;
-
- (void)device_new_from_child(&child->parent, child);
- }
-
- if (!child->parent)
- return -ENOENT;
-
- *ret = child->parent;
-
- return 0;
-}
-
-int device_set_subsystem(sd_device *device, const char *_subsystem) {
- _cleanup_free_ char *subsystem = NULL;
- int r;
-
- assert(device);
- assert(_subsystem);
-
- subsystem = strdup(_subsystem);
- if (!subsystem)
- return -ENOMEM;
-
- r = device_add_property_internal(device, "SUBSYSTEM", subsystem);
- if (r < 0)
- return r;
-
- free(device->subsystem);
- device->subsystem = subsystem;
- subsystem = NULL;
-
- device->subsystem_set = true;
-
- return 0;
-}
-
-_public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
- assert_return(ret, -EINVAL);
- assert_return(device, -EINVAL);
-
- if (!device->subsystem_set) {
- _cleanup_free_ char *subsystem = NULL;
- const char *syspath;
- char *path;
- int r;
-
- /* read 'subsystem' link */
- r = sd_device_get_syspath(device, &syspath);
- if (r < 0)
- return r;
-
- path = strjoina(syspath, "/subsystem");
- r = readlink_value(path, &subsystem);
- if (r >= 0)
- r = device_set_subsystem(device, subsystem);
- /* use implicit names */
- else if (path_startswith(device->devpath, "/module/"))
- r = device_set_subsystem(device, "module");
- else if (strstr(device->devpath, "/drivers/"))
- r = device_set_subsystem(device, "drivers");
- else if (path_startswith(device->devpath, "/subsystem/") ||
- path_startswith(device->devpath, "/class/") ||
- path_startswith(device->devpath, "/buss/"))
- r = device_set_subsystem(device, "subsystem");
- if (r < 0)
- return r;
-
- device->subsystem_set = true;
- }
-
- *ret = device->subsystem;
-
- return 0;
-}
-
-_public_ int sd_device_get_devtype(sd_device *device, const char **devtype) {
- int r;
-
- assert(devtype);
- assert(device);
-
- r = device_read_uevent_file(device);
- if (r < 0)
- return r;
-
- *devtype = device->devtype;
-
- return 0;
-}
-
-_public_ int sd_device_get_parent_with_subsystem_devtype(sd_device *child, const char *subsystem, const char *devtype, sd_device **ret) {
- sd_device *parent = NULL;
- int r;
-
- assert_return(child, -EINVAL);
- assert_return(subsystem, -EINVAL);
-
- r = sd_device_get_parent(child, &parent);
- while (r >= 0) {
- const char *parent_subsystem = NULL;
- const char *parent_devtype = NULL;
-
- (void)sd_device_get_subsystem(parent, &parent_subsystem);
- if (streq_ptr(parent_subsystem, subsystem)) {
- if (!devtype)
- break;
-
- (void)sd_device_get_devtype(parent, &parent_devtype);
- if (streq_ptr(parent_devtype, devtype))
- break;
- }
- r = sd_device_get_parent(parent, &parent);
- }
-
- if (r < 0)
- return r;
-
- *ret = parent;
-
- return 0;
-}
-
-_public_ int sd_device_get_devnum(sd_device *device, dev_t *devnum) {
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(devnum, -EINVAL);
-
- r = device_read_uevent_file(device);
- if (r < 0)
- return r;
-
- *devnum = device->devnum;
-
- return 0;
-}
-
-int device_set_driver(sd_device *device, const char *_driver) {
- _cleanup_free_ char *driver = NULL;
- int r;
-
- assert(device);
- assert(_driver);
-
- driver = strdup(_driver);
- if (!driver)
- return -ENOMEM;
-
- r = device_add_property_internal(device, "DRIVER", driver);
- if (r < 0)
- return r;
-
- free(device->driver);
- device->driver = driver;
- driver = NULL;
-
- device->driver_set = true;
-
- return 0;
-}
-
-_public_ int sd_device_get_driver(sd_device *device, const char **ret) {
- assert_return(device, -EINVAL);
- assert_return(ret, -EINVAL);
-
- if (!device->driver_set) {
- _cleanup_free_ char *driver = NULL;
- const char *syspath;
- char *path;
- int r;
-
- r = sd_device_get_syspath(device, &syspath);
- if (r < 0)
- return r;
-
- path = strjoina(syspath, "/driver");
- r = readlink_value(path, &driver);
- if (r >= 0) {
- r = device_set_driver(device, driver);
- if (r < 0)
- return r;
- }
- }
-
- *ret = device->driver;
-
- return 0;
-}
-
-_public_ int sd_device_get_devpath(sd_device *device, const char **devpath) {
- assert_return(device, -EINVAL);
- assert_return(devpath, -EINVAL);
-
- assert(device->devpath);
- assert(device->devpath[0] == '/');
-
- *devpath = device->devpath;
-
- return 0;
-}
-
-_public_ int sd_device_get_devname(sd_device *device, const char **devname) {
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(devname, -EINVAL);
-
- r = device_read_uevent_file(device);
- if (r < 0)
- return r;
-
- if (!device->devname)
- return -ENOENT;
-
- assert(path_startswith(device->devname, "/dev/"));
-
- *devname = device->devname;
-
- return 0;
-}
-
-static int device_set_sysname(sd_device *device) {
- _cleanup_free_ char *sysname = NULL;
- const char *sysnum = NULL;
- const char *pos;
- size_t len = 0;
-
- pos = strrchr(device->devpath, '/');
- if (!pos)
- return -EINVAL;
- pos ++;
-
- /* devpath is not a root directory */
- if (*pos == '\0' || pos <= device->devpath)
- return -EINVAL;
-
- sysname = strdup(pos);
- if (!sysname)
- return -ENOMEM;
-
- /* some devices have '!' in their name, change that to '/' */
- while (sysname[len] != '\0') {
- if (sysname[len] == '!')
- sysname[len] = '/';
-
- len ++;
- }
-
- /* trailing number */
- while (len > 0 && isdigit(sysname[--len]))
- sysnum = &sysname[len];
-
- if (len == 0)
- sysnum = NULL;
-
- free(device->sysname);
- device->sysname = sysname;
- sysname = NULL;
-
- device->sysnum = sysnum;
-
- device->sysname_set = true;
-
- return 0;
-}
-
-_public_ int sd_device_get_sysname(sd_device *device, const char **ret) {
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(ret, -EINVAL);
-
- if (!device->sysname_set) {
- r = device_set_sysname(device);
- if (r < 0)
- return r;
- }
-
- *ret = device->sysname;
-
- return 0;
-}
-
-_public_ int sd_device_get_sysnum(sd_device *device, const char **ret) {
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(ret, -EINVAL);
-
- if (!device->sysname_set) {
- r = device_set_sysname(device);
- if (r < 0)
- return r;
- }
-
- *ret = device->sysnum;
-
- return 0;
-}
-
-static bool is_valid_tag(const char *tag) {
- assert(tag);
-
- return !strchr(tag, ':') && !strchr(tag, ' ');
-}
-
-int device_add_tag(sd_device *device, const char *tag) {
- int r;
-
- assert(device);
- assert(tag);
-
- if (!is_valid_tag(tag))
- return -EINVAL;
-
- r = set_ensure_allocated(&device->tags, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = set_put_strdup(device->tags, tag);
- if (r < 0)
- return r;
-
- device->tags_generation ++;
- device->property_tags_outdated = true;
-
- return 0;
-}
-
-int device_add_devlink(sd_device *device, const char *devlink) {
- int r;
-
- assert(device);
- assert(devlink);
-
- r = set_ensure_allocated(&device->devlinks, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = set_put_strdup(device->devlinks, devlink);
- if (r < 0)
- return r;
-
- device->devlinks_generation ++;
- device->property_devlinks_outdated = true;
-
- return 0;
-}
-
-static int device_add_property_internal_from_string(sd_device *device, const char *str) {
- _cleanup_free_ char *key = NULL;
- char *value;
-
- assert(device);
- assert(str);
-
- key = strdup(str);
- if (!key)
- return -ENOMEM;
-
- value = strchr(key, '=');
- if (!value)
- return -EINVAL;
-
- *value = '\0';
-
- if (isempty(++value))
- value = NULL;
-
- return device_add_property_internal(device, key, value);
-}
-
-int device_set_usec_initialized(sd_device *device, const char *initialized) {
- uint64_t usec_initialized;
- int r;
-
- assert(device);
- assert(initialized);
-
- r = safe_atou64(initialized, &usec_initialized);
- if (r < 0)
- return r;
-
- r = device_add_property_internal(device, "USEC_INITIALIZED", initialized);
- if (r < 0)
- return r;
-
- device->usec_initialized = usec_initialized;
-
- return 0;
-}
-
-static int handle_db_line(sd_device *device, char key, const char *value) {
- char *path;
- int r;
-
- assert(device);
- assert(value);
-
- switch (key) {
- case 'G':
- r = device_add_tag(device, value);
- if (r < 0)
- return r;
-
- break;
- case 'S':
- path = strjoina("/dev/", value);
- r = device_add_devlink(device, path);
- if (r < 0)
- return r;
-
- break;
- case 'E':
- r = device_add_property_internal_from_string(device, value);
- if (r < 0)
- return r;
-
- break;
- case 'I':
- r = device_set_usec_initialized(device, value);
- if (r < 0)
- return r;
-
- break;
- case 'L':
- r = safe_atoi(value, &device->devlink_priority);
- if (r < 0)
- return r;
-
- break;
- case 'W':
- r = safe_atoi(value, &device->watch_handle);
- if (r < 0)
- return r;
-
- break;
- default:
- log_debug("device db: unknown key '%c'", key);
- }
-
- return 0;
-}
-
-int device_get_id_filename(sd_device *device, const char **ret) {
- assert(device);
- assert(ret);
-
- if (!device->id_filename) {
- _cleanup_free_ char *id = NULL;
- const char *subsystem;
- dev_t devnum;
- int ifindex, r;
-
- r = sd_device_get_subsystem(device, &subsystem);
- if (r < 0)
- return r;
-
- r = sd_device_get_devnum(device, &devnum);
- if (r < 0)
- return r;
-
- r = sd_device_get_ifindex(device, &ifindex);
- if (r < 0)
- return r;
-
- if (major(devnum) > 0) {
- /* use dev_t -- b259:131072, c254:0 */
- r = asprintf(&id, "%c%u:%u",
- streq(subsystem, "block") ? 'b' : 'c',
- major(devnum), minor(devnum));
- if (r < 0)
- return -errno;
- } else if (ifindex > 0) {
- /* use netdev ifindex -- n3 */
- r = asprintf(&id, "n%u", ifindex);
- if (r < 0)
- return -errno;
- } else {
- /* use $subsys:$sysname -- pci:0000:00:1f.2
- * sysname() has '!' translated, get it from devpath
- */
- const char *sysname;
-
- sysname = basename(device->devpath);
- if (!sysname)
- return -EINVAL;
-
- r = asprintf(&id, "+%s:%s", subsystem, sysname);
- if (r < 0)
- return -errno;
- }
-
- device->id_filename = id;
- id = NULL;
- }
-
- *ret = device->id_filename;
-
- return 0;
-}
-
-static int device_read_db(sd_device *device) {
- _cleanup_free_ char *db = NULL;
- char *path;
- const char *id, *value;
- char key;
- size_t db_len;
- unsigned i;
- int r;
-
- enum {
- PRE_KEY,
- KEY,
- PRE_VALUE,
- VALUE,
- INVALID_LINE,
- } state = PRE_KEY;
-
- if (device->db_loaded || device->sealed)
- return 0;
-
- r = device_get_id_filename(device, &id);
- if (r < 0)
- return r;
-
- path = strjoina("/run/udev/data/", id);
-
- r = read_full_file(path, &db, &db_len);
- if (r < 0) {
- if (r == -ENOENT)
- return 0;
- else {
- log_debug("sd-device: failed to read db '%s': %s", path, strerror(-r));
- return r;
- }
- }
-
- /* devices with a database entry are initialized */
- device->is_initialized = true;;
-
- for (i = 0; i < db_len; i++) {
- switch (state) {
- case PRE_KEY:
- if (!strchr(NEWLINE, db[i])) {
- key = db[i];
-
- state = KEY;
- }
-
- break;
- case KEY:
- if (db[i] != ':') {
- log_debug("sd-device: ignoring invalid db entry with key '%c'", key);
-
- state = INVALID_LINE;
- } else {
- db[i] = '\0';
-
- state = PRE_VALUE;
- }
-
- break;
- case PRE_VALUE:
- value = &db[i];
-
- state = VALUE;
-
- break;
- case INVALID_LINE:
- if (strchr(NEWLINE, db[i]))
- state = PRE_KEY;
-
- break;
- case VALUE:
- if (strchr(NEWLINE, db[i])) {
- db[i] = '\0';
- r = handle_db_line(device, key, value);
- if (r < 0)
- log_debug("sd-device: failed to handle db entry '%c:%s': %s", key, value, strerror(-r));
-
- state = PRE_KEY;
- }
-
- break;
- default:
- assert_not_reached("invalid state when parsing db");
- }
- }
-
- device->db_loaded = true;
-
- return 0;
-}
-
-_public_ int sd_device_get_is_initialized(sd_device *device, int *initialized) {
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(initialized, -EINVAL);
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- *initialized = device->is_initialized;
-
- return 0;
-}
-
-_public_ int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *usec) {
- usec_t now_ts;
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(usec, -EINVAL);
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- if (!device->is_initialized)
- return -EBUSY;
-
- if (!device->usec_initialized)
- return -ENODATA;
-
- now_ts = now(clock_boottime_or_monotonic());
-
- if (now_ts < device->usec_initialized)
- return -EIO;
-
- *usec = now_ts - device->usec_initialized;
-
- return 0;
-}
-
-_public_ const char *sd_device_get_tag_first(sd_device *device) {
- assert_return(device, NULL);
-
- (void) device_read_db(device);
-
- device->tags_iterator_generation = device->tags_generation;
- device->tags_iterator = ITERATOR_FIRST;
-
- return set_iterate(device->tags, &device->tags_iterator);
-}
-
-_public_ const char *sd_device_get_tag_next(sd_device *device) {
- assert_return(device, NULL);
-
- (void) device_read_db(device);
-
- if (device->tags_iterator_generation != device->tags_generation)
- return NULL;
-
- return set_iterate(device->tags, &device->tags_iterator);
-}
-
-_public_ const char *sd_device_get_devlink_first(sd_device *device) {
- assert_return(device, NULL);
-
- (void) device_read_db(device);
-
- device->devlinks_iterator_generation = device->devlinks_generation;
- device->devlinks_iterator = ITERATOR_FIRST;
-
- return set_iterate(device->devlinks, &device->devlinks_iterator);
-}
-
-_public_ const char *sd_device_get_devlink_next(sd_device *device) {
- assert_return(device, NULL);
-
- (void) device_read_db(device);
-
- if (device->devlinks_iterator_generation != device->devlinks_generation)
- return NULL;
-
- return set_iterate(device->devlinks, &device->devlinks_iterator);
-}
-
-static int device_properties_prepare(sd_device *device) {
- int r;
-
- assert(device);
-
- r = device_read_uevent_file(device);
- if (r < 0)
- return r;
-
- r = device_read_db(device);
- if (r < 0)
- return r;
-
- if (device->property_devlinks_outdated) {
- char *devlinks = NULL;
- const char *devlink;
-
- devlink = sd_device_get_devlink_first(device);
- if (devlink)
- devlinks = strdupa(devlink);
-
- while ((devlink = sd_device_get_devlink_next(device)))
- devlinks = strjoina(devlinks, " ", devlink);
-
- r = device_add_property_internal(device, "DEVLINKS", devlinks);
- if (r < 0)
- return r;
-
- device->property_devlinks_outdated = false;
- }
-
- if (device->property_tags_outdated) {
- char *tags = NULL;
- const char *tag;
-
- tag = sd_device_get_tag_first(device);
- if (tag)
- tags = strjoina(":", tag);
-
- while ((tag = sd_device_get_tag_next(device)))
- tags = strjoina(tags, ":", tag);
-
- tags = strjoina(tags, ":");
-
- r = device_add_property_internal(device, "TAGS", tags);
- if (r < 0)
- return r;
-
- device->property_tags_outdated = false;
- }
-
- return 0;
-}
-
-_public_ const char *sd_device_get_property_first(sd_device *device, const char **_value) {
- const char *key;
- const char *value;
- int r;
-
- assert_return(device, NULL);
-
- r = device_properties_prepare(device);
- if (r < 0)
- return NULL;
-
- device->properties_iterator_generation = device->properties_generation;
- device->properties_iterator = ITERATOR_FIRST;
-
- value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key);
-
- if (_value)
- *_value = value;
-
- return key;
-}
-
-_public_ const char *sd_device_get_property_next(sd_device *device, const char **_value) {
- const char *key;
- const char *value;
- int r;
-
- assert_return(device, NULL);
-
- r = device_properties_prepare(device);
- if (r < 0)
- return NULL;
-
- if (device->properties_iterator_generation != device->properties_generation)
- return NULL;
-
- value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key);
-
- if (_value)
- *_value = value;
-
- return key;
-}
-
-static int device_sysattrs_read_all(sd_device *device) {
- _cleanup_closedir_ DIR *dir = NULL;
- const char *syspath;
- struct dirent *dent;
- int r;
-
- assert(device);
-
- if (device->sysattrs_read)
- return 0;
-
- r = sd_device_get_syspath(device, &syspath);
- if (r < 0)
- return r;
-
- dir = opendir(syspath);
- if (!dir)
- return -errno;
-
- r = set_ensure_allocated(&device->sysattrs, &string_hash_ops);
- if (r < 0)
- return r;
-
- for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
- char *path;
- struct stat statbuf;
-
- /* only handle symlinks and regular files */
- if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
- continue;
-
- path = strjoina(syspath, "/", dent->d_name);
-
- if (lstat(path, &statbuf) != 0)
- continue;
-
- if (!(statbuf.st_mode & S_IRUSR))
- continue;
-
- r = set_put_strdup(device->sysattrs, dent->d_name);
- if (r < 0)
- return r;
- }
-
- device->sysattrs_read = true;
-
- return 0;
-}
-
-_public_ const char *sd_device_get_sysattr_first(sd_device *device) {
- int r;
-
- assert_return(device, NULL);
-
- if (!device->sysattrs_read) {
- r = device_sysattrs_read_all(device);
- if (r < 0) {
- errno = -r;
- return NULL;
- }
- }
-
- device->sysattrs_iterator = ITERATOR_FIRST;
-
- return set_iterate(device->sysattrs, &device->sysattrs_iterator);
-}
-
-_public_ const char *sd_device_get_sysattr_next(sd_device *device) {
- assert_return(device, NULL);
-
- if (!device->sysattrs_read)
- return NULL;
-
- return set_iterate(device->sysattrs, &device->sysattrs_iterator);
-}
-
-_public_ int sd_device_has_tag(sd_device *device, const char *tag) {
- assert_return(device, -EINVAL);
- assert_return(tag, -EINVAL);
-
- (void) device_read_db(device);
-
- return !!set_contains(device->tags, tag);
-}
-
-_public_ int sd_device_get_property_value(sd_device *device, const char *key, const char **_value) {
- char *value;
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(key, -EINVAL);
- assert_return(_value, -EINVAL);
-
- r = device_properties_prepare(device);
- if (r < 0)
- return r;
-
- value = ordered_hashmap_get(device->properties, key);
- if (!value)
- return -ENOENT;
-
- *_value = value;
-
- return 0;
-}
-
-/* replaces the value if it already exists */
-static int device_add_sysattr_value(sd_device *device, const char *_key, char *value) {
- _cleanup_free_ char *key = NULL;
- _cleanup_free_ char *value_old = NULL;
- int r;
-
- assert(device);
- assert(_key);
-
- r = hashmap_ensure_allocated(&device->sysattr_values, &string_hash_ops);
- if (r < 0)
- return r;
-
- value_old = hashmap_remove2(device->sysattr_values, _key, (void **)&key);
- if (!key) {
- key = strdup(_key);
- if (!key)
- return -ENOMEM;
- }
-
- r = hashmap_put(device->sysattr_values, key, value);
- if (r < 0)
- return r;
-
- key = NULL;
-
- return 0;
-}
-
-static int device_get_sysattr_value(sd_device *device, const char *_key, const char **_value) {
- const char *key = NULL, *value;
-
- assert(device);
- assert(_key);
-
- value = hashmap_get2(device->sysattr_values, _key, (void **) &key);
- if (!key)
- return -ENOENT;
-
- if (_value)
- *_value = value;
-
- return 0;
-}
-
-/* We cache all sysattr lookups. If an attribute does not exist, it is stored
- * with a NULL value in the cache, otherwise the returned string is stored */
-_public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **_value) {
- _cleanup_free_ char *value = NULL;
- const char *syspath, *cached_value = NULL;
- char *path;
- struct stat statbuf;
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(sysattr, -EINVAL);
-
- /* look for possibly already cached result */
- r = device_get_sysattr_value(device, sysattr, &cached_value);
- if (r != -ENOENT) {
- if (r < 0)
- return r;
-
- if (!cached_value)
- /* we looked up the sysattr before and it did not exist */
- return -ENOENT;
-
- if (_value)
- *_value = cached_value;
-
- return 0;
- }
-
- r = sd_device_get_syspath(device, &syspath);
- if (r < 0)
- return r;
-
- path = strjoina(syspath, "/", sysattr);
- r = lstat(path, &statbuf);
- if (r < 0) {
- /* remember that we could not access the sysattr */
- r = device_add_sysattr_value(device, sysattr, NULL);
- if (r < 0)
- return r;
-
- return -ENOENT;
- } else if (S_ISLNK(statbuf.st_mode)) {
- /* Some core links return only the last element of the target path,
- * these are just values, the paths should not be exposed. */
- if (STR_IN_SET(sysattr, "driver", "subsystem", "module")) {
- r = readlink_value(path, &value);
- if (r < 0)
- return r;
- } else
- return -EINVAL;
- } else if (S_ISDIR(statbuf.st_mode)) {
- /* skip directories */
- return -EINVAL;
- } else if (!(statbuf.st_mode & S_IRUSR)) {
- /* skip non-readable files */
- return -EPERM;
- } else {
- size_t size;
-
- /* read attribute value */
- r = read_full_file(path, &value, &size);
- if (r < 0)
- return r;
-
- /* drop trailing newlines */
- while (size > 0 && value[--size] == '\n')
- value[size] = '\0';
- }
-
- r = device_add_sysattr_value(device, sysattr, value);
- if (r < 0)
- return r;
-
- *_value = value;
- value = NULL;
-
- return 0;
-}
-
-static void device_remove_sysattr_value(sd_device *device, const char *_key) {
- _cleanup_free_ char *key = NULL;
- _cleanup_free_ char *value = NULL;
-
- assert(device);
- assert(_key);
-
- value = hashmap_remove2(device->sysattr_values, _key, (void **) &key);
-
- return;
-}
-
-/* set the attribute and save it in the cache. If a NULL value is passed the
- * attribute is cleared from the cache */
-_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, char *_value) {
- _cleanup_close_ int fd = -1;
- _cleanup_free_ char *value = NULL;
- const char *syspath;
- char *path;
- struct stat statbuf;
- size_t value_len = 0;
- ssize_t size;
- int r;
-
- assert_return(device, -EINVAL);
- assert_return(sysattr, -EINVAL);
-
- if (!_value) {
- device_remove_sysattr_value(device, sysattr);
-
- return 0;
- }
-
- r = sd_device_get_syspath(device, &syspath);
- if (r < 0)
- return r;
-
- path = strjoina(syspath, "/", sysattr);
- r = lstat(path, &statbuf);
- if (r < 0) {
- value = strdup("");
- if (!value)
- return -ENOMEM;
-
- r = device_add_sysattr_value(device, sysattr, value);
- if (r < 0)
- return r;
-
- return -ENXIO;
- }
-
- if (S_ISLNK(statbuf.st_mode))
- return -EINVAL;
-
- /* skip directories */
- if (S_ISDIR(statbuf.st_mode))
- return -EISDIR;
-
- /* skip non-readable files */
- if ((statbuf.st_mode & S_IRUSR) == 0)
- return -EACCES;
-
- value_len = strlen(_value);
-
- /* drop trailing newlines */
- while (value_len > 0 && _value[--value_len] == '\n')
- _value[value_len] = '\0';
-
- /* value length is limited to 4k */
- if (value_len > 4096)
- return -EINVAL;
-
- fd = open(path, O_WRONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- value = strdup(_value);
- if (!value)
- return -ENOMEM;
-
- size = write(fd, value, value_len);
- if (size < 0)
- return -errno;
-
- if ((size_t)size != value_len)
- return -EIO;
-
- r = device_add_sysattr_value(device, sysattr, value);
- if (r < 0)
- return r;
-
- value = NULL;
-
- return 0;
-}
+++ /dev/null
-../../Makefile
\ No newline at end of file
#include "missing.h"
#include "set.h"
#include "list.h"
+#include "signal-util.h"
#include "sd-event.h"
_SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
} EventSourceType;
+/* All objects we use in epoll events start with this value, so that
+ * we know how to dispatch it */
+typedef enum WakeupType {
+ WAKEUP_NONE,
+ WAKEUP_EVENT_SOURCE,
+ WAKEUP_CLOCK_DATA,
+ WAKEUP_SIGNAL_DATA,
+ _WAKEUP_TYPE_MAX,
+ _WAKEUP_TYPE_INVALID = -1,
+} WakeupType;
+
#define EVENT_SOURCE_IS_TIME(t) IN_SET((t), SOURCE_TIME_REALTIME, SOURCE_TIME_BOOTTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
struct sd_event_source {
+ WakeupType wakeup;
+
unsigned n_ref;
sd_event *event;
};
struct clock_data {
+ WakeupType wakeup;
int fd;
/* For all clocks we maintain two priority queues each, one
bool needs_rearm:1;
};
+struct signal_data {
+ WakeupType wakeup;
+
+ /* For each priority we maintain one signal fd, so that we
+ * only have to dequeue a single event per priority at a
+ * time. */
+
+ int fd;
+ int64_t priority;
+ sigset_t sigset;
+ sd_event_source *current;
+};
+
struct sd_event {
unsigned n_ref;
int epoll_fd;
- int signal_fd;
int watchdog_fd;
Prioq *pending;
usec_t perturb;
- sigset_t sigset;
- sd_event_source **signal_sources;
+ sd_event_source **signal_sources; /* indexed by signal number */
+ Hashmap *signal_data; /* indexed by priority */
Hashmap *child_sources;
unsigned n_enabled_child_sources;
static void free_clock_data(struct clock_data *d) {
assert(d);
+ assert(d->wakeup == WAKEUP_CLOCK_DATA);
safe_close(d->fd);
prioq_free(d->earliest);
*(e->default_event_ptr) = NULL;
safe_close(e->epoll_fd);
- safe_close(e->signal_fd);
safe_close(e->watchdog_fd);
free_clock_data(&e->realtime);
prioq_free(e->exit);
free(e->signal_sources);
+ hashmap_free(e->signal_data);
hashmap_free(e->child_sources);
set_free(e->post_sources);
return -ENOMEM;
e->n_ref = 1;
- e->signal_fd = e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
+ e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
+ e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
e->original_pid = getpid();
e->perturb = USEC_INFINITY;
- assert_se(sigemptyset(&e->sigset) == 0);
-
e->pending = prioq_new(pending_prioq_compare);
if (!e->pending) {
r = -ENOMEM;
return e->original_pid != getpid();
}
-static int source_io_unregister(sd_event_source *s) {
+static void source_io_unregister(sd_event_source *s) {
int r;
assert(s);
assert(s->type == SOURCE_IO);
+ if (event_pid_changed(s->event))
+ return;
+
if (!s->io.registered)
- return 0;
+ return;
r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
if (r < 0)
- return -errno;
+ log_debug_errno(errno, "Failed to remove source %s from epoll: %m", strna(s->description));
s->io.registered = false;
- return 0;
}
static int source_io_register(
r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
else
r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
-
if (r < 0)
return -errno;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
static clockid_t event_source_type_to_clock(EventSourceType t) {
switch (t) {
return (clockid_t) -1;
}
}
+#endif // 0
static EventSourceType clock_to_event_source_type(clockid_t clock) {
}
}
-static bool need_signal(sd_event *e, int signal) {
- return (e->signal_sources && e->signal_sources[signal] &&
- e->signal_sources[signal]->enabled != SD_EVENT_OFF)
- ||
- (signal == SIGCHLD &&
- e->n_enabled_child_sources > 0);
-}
+static int event_make_signal_data(
+ sd_event *e,
+ int sig,
+ struct signal_data **ret) {
-static int event_update_signal_fd(sd_event *e) {
struct epoll_event ev = {};
- bool add_to_epoll;
+ struct signal_data *d;
+ bool added = false;
+ sigset_t ss_copy;
+ int64_t priority;
int r;
assert(e);
- add_to_epoll = e->signal_fd < 0;
+ if (event_pid_changed(e))
+ return -ECHILD;
+
+ if (e->signal_sources && e->signal_sources[sig])
+ priority = e->signal_sources[sig]->priority;
+ else
+ priority = 0;
+
+ d = hashmap_get(e->signal_data, &priority);
+ if (d) {
+ if (sigismember(&d->sigset, sig) > 0) {
+ if (ret)
+ *ret = d;
+ return 0;
+ }
+ } else {
+ r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
+ if (r < 0)
+ return r;
+
+ d = new0(struct signal_data, 1);
+ if (!d)
+ return -ENOMEM;
- r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
+ d->wakeup = WAKEUP_SIGNAL_DATA;
+ d->fd = -1;
+ d->priority = priority;
+
+ r = hashmap_put(e->signal_data, &d->priority, d);
if (r < 0)
- return -errno;
+ return r;
- e->signal_fd = r;
+ added = true;
+ }
+
+ ss_copy = d->sigset;
+ assert_se(sigaddset(&ss_copy, sig) >= 0);
- if (!add_to_epoll)
+ r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
+ if (r < 0) {
+ r = -errno;
+ goto fail;
+ }
+
+ d->sigset = ss_copy;
+
+ if (d->fd >= 0) {
+ if (ret)
+ *ret = d;
return 0;
+ }
+
+ d->fd = r;
ev.events = EPOLLIN;
- ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
+ ev.data.ptr = d;
- r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
+ r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
if (r < 0) {
- e->signal_fd = safe_close(e->signal_fd);
- return -errno;
+ r = -errno;
+ goto fail;
}
+ if (ret)
+ *ret = d;
+
return 0;
+
+fail:
+ if (added) {
+ d->fd = safe_close(d->fd);
+ hashmap_remove(e->signal_data, &d->priority);
+ free(d);
+ }
+
+ return r;
+}
+
+static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
+ assert(e);
+ assert(d);
+
+ /* Turns off the specified signal in the signal data
+ * object. If the signal mask of the object becomes empty that
+ * way removes it. */
+
+ if (sigismember(&d->sigset, sig) == 0)
+ return;
+
+ assert_se(sigdelset(&d->sigset, sig) >= 0);
+
+ if (sigisemptyset(&d->sigset)) {
+
+ /* If all the mask is all-zero we can get rid of the structure */
+ hashmap_remove(e->signal_data, &d->priority);
+ assert(!d->current);
+ safe_close(d->fd);
+ free(d);
+ return;
+ }
+
+ assert(d->fd >= 0);
+
+ if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
+ log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
+}
+
+static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
+ struct signal_data *d;
+ static const int64_t zero_priority = 0;
+
+ assert(e);
+
+ /* Rechecks if the specified signal is still something we are
+ * interested in. If not, we'll unmask it, and possibly drop
+ * the signalfd for it. */
+
+ if (sig == SIGCHLD &&
+ e->n_enabled_child_sources > 0)
+ return;
+
+ if (e->signal_sources &&
+ e->signal_sources[sig] &&
+ e->signal_sources[sig]->enabled != SD_EVENT_OFF)
+ return;
+
+ /*
+ * The specified signal might be enabled in three different queues:
+ *
+ * 1) the one that belongs to the priority passed (if it is non-NULL)
+ * 2) the one that belongs to the priority of the event source of the signal (if there is one)
+ * 3) the 0 priority (to cover the SIGCHLD case)
+ *
+ * Hence, let's remove it from all three here.
+ */
+
+ if (priority) {
+ d = hashmap_get(e->signal_data, priority);
+ if (d)
+ event_unmask_signal_data(e, d, sig);
+ }
+
+ if (e->signal_sources && e->signal_sources[sig]) {
+ d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
+ if (d)
+ event_unmask_signal_data(e, d, sig);
+ }
+
+ d = hashmap_get(e->signal_data, &zero_priority);
+ if (d)
+ event_unmask_signal_data(e, d, sig);
}
static void source_disconnect(sd_event_source *s) {
case SOURCE_SIGNAL:
if (s->signal.sig > 0) {
+
if (s->event->signal_sources)
s->event->signal_sources[s->signal.sig] = NULL;
- /* If the signal was on and now it is off... */
- if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
- assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
-
- (void) event_update_signal_fd(s->event);
- /* If disabling failed, we might get a spurious event,
- * but otherwise nothing bad should happen. */
- }
+ event_gc_signal_data(s->event, &s->priority, s->signal.sig);
}
break;
if (s->enabled != SD_EVENT_OFF) {
assert(s->event->n_enabled_child_sources > 0);
s->event->n_enabled_child_sources--;
-
- /* We know the signal was on, if it is off now... */
- if (!need_signal(s->event, SIGCHLD)) {
- assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
-
- (void) event_update_signal_fd(s->event);
- /* If disabling failed, we might get a spurious event,
- * but otherwise nothing bad should happen. */
- }
}
- hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
+ (void) hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
+ event_gc_signal_data(s->event, &s->priority, SIGCHLD);
}
break;
d->needs_rearm = true;
}
+ if (s->type == SOURCE_SIGNAL && !b) {
+ struct signal_data *d;
+
+ d = hashmap_get(s->event->signal_data, &s->priority);
+ if (d && d->current == s)
+ d->current = NULL;
+ }
+
return 0;
}
int r;
assert_return(e, -EINVAL);
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
assert_return(callback, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
if (!s)
return -ENOMEM;
+ s->wakeup = WAKEUP_EVENT_SOURCE;
s->io.fd = fd;
s->io.events = events;
s->io.callback = callback;
return -errno;
ev.events = EPOLLIN;
- ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
+ ev.data.ptr = d;
r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
if (r < 0) {
void *userdata) {
sd_event_source *s;
+ struct signal_data *d;
sigset_t ss;
int r;
- bool previous;
assert_return(e, -EINVAL);
assert_return(sig > 0, -EINVAL);
} else if (e->signal_sources[sig])
return -EBUSY;
- previous = need_signal(e, sig);
-
s = source_new(e, !ret, SOURCE_SIGNAL);
if (!s)
return -ENOMEM;
e->signal_sources[sig] = s;
- if (!previous) {
- assert_se(sigaddset(&e->sigset, sig) == 0);
-
- r = event_update_signal_fd(e);
+ r = event_make_signal_data(e, sig, &d);
if (r < 0) {
source_free(s);
return r;
}
- }
/* Use the signal name as description for the event source by default */
(void) sd_event_source_set_description(s, signal_to_string(sig));
sd_event_source *s;
int r;
- bool previous;
assert_return(e, -EINVAL);
assert_return(pid > 1, -EINVAL);
if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
return -EBUSY;
- previous = need_signal(e, SIGCHLD);
-
s = source_new(e, !ret, SOURCE_CHILD);
if (!s)
return -ENOMEM;
e->n_enabled_child_sources ++;
- if (!previous) {
- assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
-
- r = event_update_signal_fd(e);
+ r = event_make_signal_data(e, SIGCHLD, NULL);
if (r < 0) {
+ e->n_enabled_child_sources--;
source_free(s);
return r;
}
- }
e->need_process_child = true;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
assert_return(s, NULL);
return s;
}
+#endif // 0
_public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
return free_and_strdup(&s->description, description);
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
assert_return(s, -EINVAL);
assert_return(description, -EINVAL);
*description = s->description;
return 0;
}
+#endif // 0
_public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
assert_return(s, NULL);
return s->event;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_source_get_pending(sd_event_source *s) {
assert_return(s, -EINVAL);
assert_return(s->type != SOURCE_EXIT, -EDOM);
return s->io.fd;
}
+#endif // 0
_public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
int r;
assert_return(s, -EINVAL);
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
assert_return(s, -EINVAL);
assert_return(events, -EINVAL);
*events = s->io.events;
return 0;
}
+#endif // 0
_public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
int r;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
assert_return(s, -EINVAL);
assert_return(revents, -EINVAL);
return s->priority;
}
+#endif // 0
_public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
+ int r;
+
assert_return(s, -EINVAL);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD);
if (s->priority == priority)
return 0;
+ if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
+ struct signal_data *old, *d;
+
+ /* Move us from the signalfd belonging to the old
+ * priority to the signalfd of the new priority */
+
+ assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
+
+ s->priority = priority;
+
+ r = event_make_signal_data(s->event, s->signal.sig, &d);
+ if (r < 0) {
+ s->priority = old->priority;
+ return r;
+ }
+
+ event_unmask_signal_data(s->event, old, s->signal.sig);
+ } else
s->priority = priority;
if (s->pending)
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
assert_return(s, -EINVAL);
assert_return(m, -EINVAL);
*m = s->enabled;
return 0;
}
+#endif // 0
_public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
int r;
switch (s->type) {
case SOURCE_IO:
- r = source_io_unregister(s);
- if (r < 0)
- return r;
-
+ source_io_unregister(s);
s->enabled = m;
break;
}
case SOURCE_SIGNAL:
- assert(need_signal(s->event, s->signal.sig));
-
s->enabled = m;
- if (!need_signal(s->event, s->signal.sig)) {
- assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
-
- (void) event_update_signal_fd(s->event);
- /* If disabling failed, we might get a spurious event,
- * but otherwise nothing bad should happen. */
- }
-
+ event_gc_signal_data(s->event, &s->priority, s->signal.sig);
break;
case SOURCE_CHILD:
- assert(need_signal(s->event, SIGCHLD));
-
s->enabled = m;
assert(s->event->n_enabled_child_sources > 0);
s->event->n_enabled_child_sources--;
- if (!need_signal(s->event, SIGCHLD)) {
- assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
-
- (void) event_update_signal_fd(s->event);
- }
-
+ event_gc_signal_data(s->event, &s->priority, SIGCHLD);
break;
case SOURCE_EXIT:
}
case SOURCE_SIGNAL:
- /* Check status before enabling. */
- if (!need_signal(s->event, s->signal.sig)) {
- assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
- r = event_update_signal_fd(s->event);
+ s->enabled = m;
+
+ r = event_make_signal_data(s->event, s->signal.sig, NULL);
if (r < 0) {
s->enabled = SD_EVENT_OFF;
+ event_gc_signal_data(s->event, &s->priority, s->signal.sig);
return r;
}
- }
- s->enabled = m;
break;
case SOURCE_CHILD:
- /* Check status before enabling. */
- if (s->enabled == SD_EVENT_OFF) {
- if (!need_signal(s->event, SIGCHLD)) {
- assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
- r = event_update_signal_fd(s->event);
+ if (s->enabled == SD_EVENT_OFF)
+ s->event->n_enabled_child_sources++;
+
+ s->enabled = m;
+
+ r = event_make_signal_data(s->event, SIGCHLD, NULL);
if (r < 0) {
s->enabled = SD_EVENT_OFF;
+ s->event->n_enabled_child_sources--;
+ event_gc_signal_data(s->event, &s->priority, SIGCHLD);
return r;
}
- }
- s->event->n_enabled_child_sources++;
- }
-
- s->enabled = m;
break;
case SOURCE_EXIT:
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
assert_return(s, -EINVAL);
assert_return(usec, -EINVAL);
*pid = s->child.pid;
return 0;
}
+#endif // 0
_public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
int r;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ void* sd_event_source_get_userdata(sd_event_source *s) {
assert_return(s, NULL);
return ret;
}
+#endif // 0
static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
usec_t c;
return 0;
}
-static int process_signal(sd_event *e, uint32_t events) {
+static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
bool read_one = false;
int r;
assert(e);
-
assert_return(events == EPOLLIN, -EIO);
+ /* If there's a signal queued on this priority and SIGCHLD is
+ on this priority too, then make sure to recheck the
+ children we watch. This is because we only ever dequeue
+ the first signal per priority, and if we dequeue one, and
+ SIGCHLD might be enqueued later we wouldn't know, but we
+ might have higher priority children we care about hence we
+ need to check that explicitly. */
+
+ if (sigismember(&d->sigset, SIGCHLD))
+ e->need_process_child = true;
+
+ /* If there's already an event source pending for this
+ * priority we don't read another */
+ if (d->current)
+ return 0;
+
for (;;) {
struct signalfd_siginfo si;
ssize_t n;
sd_event_source *s = NULL;
- n = read(e->signal_fd, &si, sizeof(si));
+ n = read(d->fd, &si, sizeof(si));
if (n < 0) {
if (errno == EAGAIN || errno == EINTR)
return read_one;
read_one = true;
- if (si.ssi_signo == SIGCHLD) {
- r = process_child(e);
- if (r < 0)
- return r;
- if (r > 0)
- continue;
- }
-
if (e->signal_sources)
s = e->signal_sources[si.ssi_signo];
-
if (!s)
continue;
+ if (s->pending)
+ continue;
s->signal.siginfo = si;
+ d->current = s;
+
r = source_set_pending(s, true);
if (r < 0)
return r;
+
+ return 1;
}
}
}
r = -errno;
-
goto finish;
}
for (i = 0; i < m; i++) {
- if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
- r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
- else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
- r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
- else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
- r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
- else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
- r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
- else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
- r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
- else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
- r = process_signal(e, ev_queue[i].events);
- else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
+ if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
- else
+ else {
+ WakeupType *t = ev_queue[i].data.ptr;
+
+ switch (*t) {
+
+ case WAKEUP_EVENT_SOURCE:
r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
+ break;
+
+ case WAKEUP_CLOCK_DATA: {
+ struct clock_data *d = ev_queue[i].data.ptr;
+ r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
+ break;
+ }
+
+ case WAKEUP_SIGNAL_DATA:
+ r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
+ break;
+ default:
+ assert_not_reached("Invalid wake-up pointer");
+ }
+ }
if (r < 0)
goto finish;
}
assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
r = sd_event_prepare(e);
- if (r > 0) {
- r = sd_event_dispatch(e);
- if (r < 0)
- return r;
- else
- return 1;
- } else if (r < 0)
- return r;
+ if (r == 0)
+ /* There was nothing? Then wait... */
+ r = sd_event_wait(e, timeout);
- r = sd_event_wait(e, timeout);
if (r > 0) {
+ /* There's something now, then let's dispatch it */
r = sd_event_dispatch(e);
if (r < 0)
return r;
- else
- return 1;
- } else
- return r;
+
+ return 1;
+ }
+
+ return r;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_loop(sd_event *e) {
int r;
return e->epoll_fd;
}
+#endif // 0
_public_ int sd_event_get_state(sd_event *e) {
assert_return(e, -EINVAL);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
assert_return(e, -EINVAL);
assert_return(usec, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD);
- /* If we haven't run yet, just get the actual time */
- if (!dual_timestamp_is_set(&e->timestamp))
- return -ENODATA;
+ if (!dual_timestamp_is_set(&e->timestamp)) {
+ /* Implicitly fall back to now() if we never ran
+ * before and thus have no cached time. */
+ *usec = now(clock);
+ return 1;
+ }
switch (clock) {
return 0;
}
+#endif // 0
_public_ int sd_event_default(sd_event **ret) {
return r;
}
+/// UNNEEDED by elogind
+#if 0
_public_ int sd_event_get_watchdog(sd_event *e) {
assert_return(e, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD);
return e->watchdog;
}
+#endif // 0
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "sd-event.h"
-#include "log.h"
-#include "util.h"
-#include "macro.h"
-
-static int prepare_handler(sd_event_source *s, void *userdata) {
- log_info("preparing %c", PTR_TO_INT(userdata));
- return 1;
-}
-
-static bool got_a, got_b, got_c, got_unref;
-static unsigned got_d;
-
-static int unref_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- sd_event_source_unref(s);
- got_unref = true;
- return 0;
-}
-
-static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
-
- log_info("got IO on %c", PTR_TO_INT(userdata));
-
- if (userdata == INT_TO_PTR('a')) {
- assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
- assert_se(!got_a);
- got_a = true;
- } else if (userdata == INT_TO_PTR('b')) {
- assert_se(!got_b);
- got_b = true;
- } else if (userdata == INT_TO_PTR('d')) {
- got_d++;
- if (got_d < 2)
- assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0);
- else
- assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
- } else
- assert_not_reached("Yuck!");
-
- return 1;
-}
-
-static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
-
- assert_se(s);
- assert_se(si);
-
- log_info("got child on %c", PTR_TO_INT(userdata));
-
- assert_se(userdata == INT_TO_PTR('f'));
-
- assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
- sd_event_source_unref(s);
-
- return 1;
-}
-
-static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
- sd_event_source *p = NULL;
- sigset_t ss;
- pid_t pid;
-
- assert_se(s);
- assert_se(si);
-
- log_info("got signal on %c", PTR_TO_INT(userdata));
-
- assert_se(userdata == INT_TO_PTR('e'));
-
- assert_se(sigemptyset(&ss) >= 0);
- assert_se(sigaddset(&ss, SIGCHLD) >= 0);
- assert_se(sigprocmask(SIG_BLOCK, &ss, NULL) >= 0);
-
- pid = fork();
- assert_se(pid >= 0);
-
- if (pid == 0)
- _exit(0);
-
- assert_se(sd_event_add_child(sd_event_source_get_event(s), &p, pid, WEXITED, child_handler, INT_TO_PTR('f')) >= 0);
- assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
-
- sd_event_source_unref(s);
-
- return 1;
-}
-
-static int defer_handler(sd_event_source *s, void *userdata) {
- sd_event_source *p = NULL;
- sigset_t ss;
-
- assert_se(s);
-
- log_info("got defer on %c", PTR_TO_INT(userdata));
-
- assert_se(userdata == INT_TO_PTR('d'));
-
- assert_se(sigemptyset(&ss) >= 0);
- assert_se(sigaddset(&ss, SIGUSR1) >= 0);
- assert_se(sigprocmask(SIG_BLOCK, &ss, NULL) >= 0);
- assert_se(sd_event_add_signal(sd_event_source_get_event(s), &p, SIGUSR1, signal_handler, INT_TO_PTR('e')) >= 0);
- assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
- raise(SIGUSR1);
-
- sd_event_source_unref(s);
-
- return 1;
-}
-
-static bool do_quit = false;
-
-static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
- log_info("got timer on %c", PTR_TO_INT(userdata));
-
- if (userdata == INT_TO_PTR('c')) {
-
- if (do_quit) {
- sd_event_source *p;
-
- assert_se(sd_event_add_defer(sd_event_source_get_event(s), &p, defer_handler, INT_TO_PTR('d')) >= 0);
- assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
- } else {
- assert_se(!got_c);
- got_c = true;
- }
- } else
- assert_not_reached("Huh?");
-
- return 2;
-}
-
-static bool got_exit = false;
-
-static int exit_handler(sd_event_source *s, void *userdata) {
- log_info("got quit handler on %c", PTR_TO_INT(userdata));
-
- got_exit = true;
-
- return 3;
-}
-
-int main(int argc, char *argv[]) {
- sd_event *e = NULL;
- sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL;
- static const char ch = 'x';
- int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1}, k[2] = { -1, -1 };
-
- assert_se(pipe(a) >= 0);
- assert_se(pipe(b) >= 0);
- assert_se(pipe(d) >= 0);
- assert_se(pipe(k) >= 0);
-
- assert_se(sd_event_default(&e) >= 0);
-
- assert_se(sd_event_set_watchdog(e, true) >= 0);
-
- /* Test whether we cleanly can destroy an io event source from its own handler */
- got_unref = false;
- assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0);
- assert_se(write(k[1], &ch, 1) == 1);
- assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
- assert_se(got_unref);
-
- got_a = false, got_b = false, got_c = false, got_d = 0;
-
- /* Add a oneshot handler, trigger it, re-enable it, and trigger
- * it again. */
- assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0);
- assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
- assert_se(write(d[1], &ch, 1) >= 0);
- assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
- assert_se(got_d == 1);
- assert_se(write(d[1], &ch, 1) >= 0);
- assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
- assert_se(got_d == 2);
-
- assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0);
- assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0);
- assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0);
- assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0);
-
- assert_se(sd_event_source_set_priority(x, 99) >= 0);
- assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
- assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
- assert_se(sd_event_source_set_priority(z, 50) >= 0);
- assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
- assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
-
- /* Test for floating event sources */
- assert_se(sigprocmask_many(SIG_BLOCK, SIGRTMIN+1, -1) == 0);
- assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0);
-
- assert_se(write(a[1], &ch, 1) >= 0);
- assert_se(write(b[1], &ch, 1) >= 0);
-
- assert_se(!got_a && !got_b && !got_c);
-
- assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
-
- assert_se(!got_a && got_b && !got_c);
-
- assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
-
- assert_se(!got_a && got_b && got_c);
-
- assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
-
- assert_se(got_a && got_b && got_c);
-
- sd_event_source_unref(x);
- sd_event_source_unref(y);
-
- do_quit = true;
- assert_se(sd_event_source_set_time(z, now(CLOCK_MONOTONIC) + 200 * USEC_PER_MSEC) >= 0);
- assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
-
- assert_se(sd_event_loop(e) >= 0);
-
- sd_event_source_unref(z);
- sd_event_source_unref(q);
-
- sd_event_source_unref(w);
-
- sd_event_unref(e);
-
- safe_close_pair(a);
- safe_close_pair(b);
- safe_close_pair(d);
- safe_close_pair(k);
-
- return 0;
-}
+++ /dev/null
-../../Makefile
\ No newline at end of file
+++ /dev/null
-/***
- This file is part of systemd.
-
- Copyright 2012 Kay Sievers <kay@vrfy.org>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-#pragma once
-
-#include "sparse-endian.h"
-
-#define HWDB_SIG { 'K', 'S', 'L', 'P', 'H', 'H', 'R', 'H' }
-
-/* on-disk trie objects */
-struct trie_header_f {
- uint8_t signature[8];
-
- /* version of tool which created the file */
- le64_t tool_version;
- le64_t file_size;
-
- /* size of structures to allow them to grow */
- le64_t header_size;
- le64_t node_size;
- le64_t child_entry_size;
- le64_t value_entry_size;
-
- /* offset of the root trie node */
- le64_t nodes_root_off;
-
- /* size of the nodes and string section */
- le64_t nodes_len;
- le64_t strings_len;
-} _packed_;
-
-struct trie_node_f {
- /* prefix of lookup string, shared by all children */
- le64_t prefix_off;
- /* size of children entry array appended to the node */
- uint8_t children_count;
- uint8_t padding[7];
- /* size of value entry array appended to the node */
- le64_t values_count;
-} _packed_;
-
-/* array of child entries, follows directly the node record */
-struct trie_child_entry_f {
- /* index of the child node */
- uint8_t c;
- uint8_t padding[7];
- /* offset of the child node */
- le64_t child_off;
-} _packed_;
-
-/* array of value entries, follows directly the node record/child array */
-struct trie_value_entry_f {
- le64_t key_off;
- le64_t value_off;
-} _packed_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-
-#include "sd-hwdb.h"
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_hwdb*, sd_hwdb_unref);
-#define _cleanup_hwdb_unref_ _cleanup_(sd_hwdb_unrefp)
-
-bool hwdb_validate(sd_hwdb *hwdb);
+++ /dev/null
-/***
- This file is part of systemd.
-
- Copyright 2012 Kay Sievers <kay@vrfy.org>
- Copyright 2008 Alan Jenkins <alan.christopher.jenkins@googlemail.com>
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <inttypes.h>
-#include <stdlib.h>
-#include <fnmatch.h>
-#include <sys/mman.h>
-
-#include "sd-hwdb.h"
-
-#include "hashmap.h"
-#include "refcnt.h"
-
-#include "hwdb-util.h"
-#include "hwdb-internal.h"
-
-struct sd_hwdb {
- RefCount n_ref;
- int refcount;
-
- FILE *f;
- struct stat st;
- union {
- struct trie_header_f *head;
- const char *map;
- };
-
- char *modalias;
-
- OrderedHashmap *properties;
- Iterator properties_iterator;
- bool properties_modified;
-};
-
-struct linebuf {
- char bytes[LINE_MAX];
- size_t size;
- size_t len;
-};
-
-static void linebuf_init(struct linebuf *buf) {
- buf->size = 0;
- buf->len = 0;
-}
-
-static const char *linebuf_get(struct linebuf *buf) {
- if (buf->len + 1 >= sizeof(buf->bytes))
- return NULL;
- buf->bytes[buf->len] = '\0';
- return buf->bytes;
-}
-
-static bool linebuf_add(struct linebuf *buf, const char *s, size_t len) {
- if (buf->len + len >= sizeof(buf->bytes))
- return false;
- memcpy(buf->bytes + buf->len, s, len);
- buf->len += len;
- return true;
-}
-
-static bool linebuf_add_char(struct linebuf *buf, char c)
-{
- if (buf->len + 1 >= sizeof(buf->bytes))
- return false;
- buf->bytes[buf->len++] = c;
- return true;
-}
-
-static void linebuf_rem(struct linebuf *buf, size_t count) {
- assert(buf->len >= count);
- buf->len -= count;
-}
-
-static void linebuf_rem_char(struct linebuf *buf) {
- linebuf_rem(buf, 1);
-}
-
-static const struct trie_child_entry_f *trie_node_children(sd_hwdb *hwdb, const struct trie_node_f *node) {
- return (const struct trie_child_entry_f *)((const char *)node + le64toh(hwdb->head->node_size));
-}
-
-static const struct trie_value_entry_f *trie_node_values(sd_hwdb *hwdb, const struct trie_node_f *node) {
- const char *base = (const char *)node;
-
- base += le64toh(hwdb->head->node_size);
- base += node->children_count * le64toh(hwdb->head->child_entry_size);
- return (const struct trie_value_entry_f *)base;
-}
-
-static const struct trie_node_f *trie_node_from_off(sd_hwdb *hwdb, le64_t off) {
- return (const struct trie_node_f *)(hwdb->map + le64toh(off));
-}
-
-static const char *trie_string(sd_hwdb *hwdb, le64_t off) {
- return hwdb->map + le64toh(off);
-}
-
-static int trie_children_cmp_f(const void *v1, const void *v2) {
- const struct trie_child_entry_f *n1 = v1;
- const struct trie_child_entry_f *n2 = v2;
-
- return n1->c - n2->c;
-}
-
-static const struct trie_node_f *node_lookup_f(sd_hwdb *hwdb, const struct trie_node_f *node, uint8_t c) {
- struct trie_child_entry_f *child;
- struct trie_child_entry_f search;
-
- search.c = c;
- child = bsearch(&search, trie_node_children(hwdb, node), node->children_count,
- le64toh(hwdb->head->child_entry_size), trie_children_cmp_f);
- if (child)
- return trie_node_from_off(hwdb, child->child_off);
- return NULL;
-}
-
-static int hwdb_add_property(sd_hwdb *hwdb, const char *key, const char *value) {
- int r;
-
- assert(hwdb);
- assert(key);
- assert(value);
-
- /*
- * Silently ignore all properties which do not start with a
- * space; future extensions might use additional prefixes.
- */
- if (key[0] != ' ')
- return 0;
-
- key++;
-
- r = ordered_hashmap_ensure_allocated(&hwdb->properties, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = ordered_hashmap_replace(hwdb->properties, key, (char*)value);
- if (r < 0)
- return r;
-
- hwdb->properties_modified = true;
-
- return 0;
-}
-
-static int trie_fnmatch_f(sd_hwdb *hwdb, const struct trie_node_f *node, size_t p,
- struct linebuf *buf, const char *search) {
- size_t len;
- size_t i;
- const char *prefix;
- int err;
-
- prefix = trie_string(hwdb, node->prefix_off);
- len = strlen(prefix + p);
- linebuf_add(buf, prefix + p, len);
-
- for (i = 0; i < node->children_count; i++) {
- const struct trie_child_entry_f *child = &trie_node_children(hwdb, node)[i];
-
- linebuf_add_char(buf, child->c);
- err = trie_fnmatch_f(hwdb, trie_node_from_off(hwdb, child->child_off), 0, buf, search);
- if (err < 0)
- return err;
- linebuf_rem_char(buf);
- }
-
- if (le64toh(node->values_count) && fnmatch(linebuf_get(buf), search, 0) == 0)
- for (i = 0; i < le64toh(node->values_count); i++) {
- err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[i].key_off),
- trie_string(hwdb, trie_node_values(hwdb, node)[i].value_off));
- if (err < 0)
- return err;
- }
-
- linebuf_rem(buf, len);
- return 0;
-}
-
-static int trie_search_f(sd_hwdb *hwdb, const char *search) {
- struct linebuf buf;
- const struct trie_node_f *node;
- size_t i = 0;
- int err;
-
- linebuf_init(&buf);
-
- node = trie_node_from_off(hwdb, hwdb->head->nodes_root_off);
- while (node) {
- const struct trie_node_f *child;
- size_t p = 0;
-
- if (node->prefix_off) {
- uint8_t c;
-
- for (; (c = trie_string(hwdb, node->prefix_off)[p]); p++) {
- if (c == '*' || c == '?' || c == '[')
- return trie_fnmatch_f(hwdb, node, p, &buf, search + i + p);
- if (c != search[i + p])
- return 0;
- }
- i += p;
- }
-
- child = node_lookup_f(hwdb, node, '*');
- if (child) {
- linebuf_add_char(&buf, '*');
- err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
- if (err < 0)
- return err;
- linebuf_rem_char(&buf);
- }
-
- child = node_lookup_f(hwdb, node, '?');
- if (child) {
- linebuf_add_char(&buf, '?');
- err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
- if (err < 0)
- return err;
- linebuf_rem_char(&buf);
- }
-
- child = node_lookup_f(hwdb, node, '[');
- if (child) {
- linebuf_add_char(&buf, '[');
- err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
- if (err < 0)
- return err;
- linebuf_rem_char(&buf);
- }
-
- if (search[i] == '\0') {
- size_t n;
-
- for (n = 0; n < le64toh(node->values_count); n++) {
- err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[n].key_off),
- trie_string(hwdb, trie_node_values(hwdb, node)[n].value_off));
- if (err < 0)
- return err;
- }
- return 0;
- }
-
- child = node_lookup_f(hwdb, node, search[i]);
- node = child;
- i++;
- }
- return 0;
-}
-
-static const char hwdb_bin_paths[] =
- "/etc/systemd/hwdb/hwdb.bin\0"
- "/etc/udev/hwdb.bin\0"
- "/usr/lib/systemd/hwdb/hwdb.bin\0"
-#ifdef HAVE_SPLIT_USR
- "/lib/systemd/hwdb/hwdb.bin\0"
-#endif
- UDEVLIBEXECDIR "/hwdb.bin\0";
-
-_public_ int sd_hwdb_new(sd_hwdb **ret) {
- _cleanup_hwdb_unref_ sd_hwdb *hwdb = NULL;
- const char *hwdb_bin_path;
- const char sig[] = HWDB_SIG;
-
- assert_return(ret, -EINVAL);
-
- hwdb = new0(sd_hwdb, 1);
- if (!hwdb)
- return -ENOMEM;
-
- hwdb->n_ref = REFCNT_INIT;
-
- /* find hwdb.bin in hwdb_bin_paths */
- NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
- hwdb->f = fopen(hwdb_bin_path, "re");
- if (hwdb->f)
- break;
- else if (errno == ENOENT)
- continue;
- else
- return log_debug_errno(errno, "error reading %s: %m", hwdb_bin_path);
- }
-
- if (!hwdb->f) {
- log_debug("hwdb.bin does not exist, please run udevadm hwdb --update");
- return -ENOENT;
- }
-
- if (fstat(fileno(hwdb->f), &hwdb->st) < 0 ||
- (size_t)hwdb->st.st_size < offsetof(struct trie_header_f, strings_len) + 8)
- return log_debug_errno(errno, "error reading %s: %m", hwdb_bin_path);
-
- hwdb->map = mmap(0, hwdb->st.st_size, PROT_READ, MAP_SHARED, fileno(hwdb->f), 0);
- if (hwdb->map == MAP_FAILED)
- return log_debug_errno(errno, "error mapping %s: %m", hwdb_bin_path);
-
- if (memcmp(hwdb->map, sig, sizeof(hwdb->head->signature)) != 0 ||
- (size_t)hwdb->st.st_size != le64toh(hwdb->head->file_size)) {
- log_debug("error recognizing the format of %s", hwdb_bin_path);
- return -EINVAL;;
- }
-
- log_debug("=== trie on-disk ===");
- log_debug("tool version: %"PRIu64, le64toh(hwdb->head->tool_version));
- log_debug("file size: %8"PRIi64" bytes", hwdb->st.st_size);
- log_debug("header size %8"PRIu64" bytes", le64toh(hwdb->head->header_size));
- log_debug("strings %8"PRIu64" bytes", le64toh(hwdb->head->strings_len));
- log_debug("nodes %8"PRIu64" bytes", le64toh(hwdb->head->nodes_len));
-
- *ret = hwdb;
- hwdb = NULL;
-
- return 0;
-}
-
-_public_ sd_hwdb *sd_hwdb_ref(sd_hwdb *hwdb) {
- assert_return(hwdb, NULL);
-
- assert_se(REFCNT_INC(hwdb->n_ref) >= 2);
-
- return hwdb;
-}
-
-_public_ sd_hwdb *sd_hwdb_unref(sd_hwdb *hwdb) {
- if (hwdb && REFCNT_DEC(hwdb->n_ref) == 0) {
- if (hwdb->map)
- munmap((void *)hwdb->map, hwdb->st.st_size);
- if (hwdb->f)
- fclose(hwdb->f);
- free(hwdb->modalias);
- ordered_hashmap_free(hwdb->properties);
- free(hwdb);
- }
-
- return NULL;
-}
-
-bool hwdb_validate(sd_hwdb *hwdb) {
- bool found = false;
- const char* p;
- struct stat st;
-
- if (!hwdb)
- return false;
- if (!hwdb->f)
- return false;
-
- /* if hwdb.bin doesn't exist anywhere, we need to update */
- NULSTR_FOREACH(p, hwdb_bin_paths) {
- if (stat(p, &st) >= 0) {
- found = true;
- break;
- }
- }
- if (!found)
- return true;
-
- if (timespec_load(&hwdb->st.st_mtim) != timespec_load(&st.st_mtim))
- return true;
- return false;
-}
-
-static int properties_prepare(sd_hwdb *hwdb, const char *modalias) {
- _cleanup_free_ char *mod = NULL;
- int r;
-
- assert(hwdb);
- assert(modalias);
-
- if (streq_ptr(modalias, hwdb->modalias))
- return 0;
-
- mod = strdup(modalias);
- if (!mod)
- return -ENOMEM;
-
- ordered_hashmap_clear(hwdb->properties);
-
- hwdb->properties_modified = true;
-
- r = trie_search_f(hwdb, modalias);
- if (r < 0)
- return r;
-
- free(hwdb->modalias);
- hwdb->modalias = mod;
- mod = NULL;
-
- return 0;
-}
-
-_public_ int sd_hwdb_get(sd_hwdb *hwdb, const char *modalias, const char *key, const char **_value) {
- const char *value;
- int r;
-
- assert_return(hwdb, -EINVAL);
- assert_return(hwdb->f, -EINVAL);
- assert_return(modalias, -EINVAL);
- assert_return(_value, -EINVAL);
-
- r = properties_prepare(hwdb, modalias);
- if (r < 0)
- return r;
-
- value = ordered_hashmap_get(hwdb->properties, key);
- if (!value)
- return -ENOENT;
-
- *_value = value;
-
- return 0;
-}
-
-_public_ int sd_hwdb_seek(sd_hwdb *hwdb, const char *modalias) {
- int r;
-
- assert_return(hwdb, -EINVAL);
- assert_return(hwdb->f, -EINVAL);
- assert_return(modalias, -EINVAL);
-
- r = properties_prepare(hwdb, modalias);
- if (r < 0)
- return r;
-
- hwdb->properties_modified = false;
- hwdb->properties_iterator = ITERATOR_FIRST;
-
- return 0;
-}
-
-_public_ int sd_hwdb_enumerate(sd_hwdb *hwdb, const char **key, const char **value) {
- const void *k, *v;
-
- assert_return(hwdb, -EINVAL);
- assert_return(key, -EINVAL);
- assert_return(value, -EINVAL);
-
- if (hwdb->properties_modified)
- return -EAGAIN;
-
- v = ordered_hashmap_iterate(hwdb->properties, &hwdb->properties_iterator, &k);
- if (!k)
- return 0;
-
- *key = k;
- *value = v;
-
- return 1;
-}
+++ /dev/null
-# This file is part of systemd.
-#
-# 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.
-
-prefix=@prefix@
-exec_prefix=@exec_prefix@
-libdir=@libdir@
-includedir=@includedir@
-
-Name: elogind
-Description: elogind 128 Bit ID Utility Library
-URL: @PACKAGE_URL@
-Version: @PACKAGE_VERSION@
-Libs: -L${libdir} -lelogind-id128
-Cflags: -I${includedir}
#include "util.h"
#include "macro.h"
#include "sd-id128.h"
+#include "random-util.h"
_public_ char *sd_id128_to_string(sd_id128_t id, char s[33]) {
unsigned n;
-../Makefile
\ No newline at end of file
+../../Makefile
\ No newline at end of file
#include "macro.h"
#include "strv.h"
#include "fileio.h"
-#include "login-shared.h"
+#include "login-util.h"
+#include "formats-util.h"
+#include "hostname-util.h"
#include "sd-login.h"
+/* Error codes:
+ *
+ * invalid input parameters → -EINVAL
+ * invalid fd → -EBADF
+ * process does not exist → -ESRCH
+ * cgroup does not exist → -ENOENT
+ * machine, session does not exist → -ENXIO
+ * requested metadata on object is missing → -ENODATA
+ */
+
_public_ int sd_pid_get_session(pid_t pid, char **session) {
assert_return(pid >= 0, -EINVAL);
assert_return(pid >= 0, -EINVAL);
assert_return(unit, -EINVAL);
+/// elogind does not support systemd units
+#if 0
+ return cg_pid_get_unit(pid, unit);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
assert_return(pid >= 0, -EINVAL);
assert_return(unit, -EINVAL);
+/// elogind does not support systemd units
+#if 0
+ return cg_pid_get_user_unit(pid, unit);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
assert_return(pid >= 0, -EINVAL);
assert_return(name, -EINVAL);
+/// elogind does not support systemd units
+#if 0
+ return cg_pid_get_machine_name(pid, name);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_pid_get_slice(pid_t pid, char **slice) {
assert_return(pid >= 0, -EINVAL);
assert_return(slice, -EINVAL);
+/// elogind does not support systemd slices
+#if 0
+ return cg_pid_get_slice(pid, slice);
+#else
+ return -ESRCH;
+#endif // 0
+}
+
+_public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
+
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(slice, -EINVAL);
+
+/// elogind does not support systemd slices
+#if 0
+ return cg_pid_get_user_slice(pid, slice);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
assert_return(pid >= 0, -EINVAL);
assert_return(uid, -EINVAL);
+/// elogind does not support systemd slices
+#if 0
+ return cg_pid_get_owner_uid(pid, uid);
+#else
return -ESRCH;
+#endif // 0
+}
+
+_public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
+ char *c;
+ int r;
+
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(cgroup, -EINVAL);
+
+ r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
+ if (r < 0)
+ return r;
+
+ /* The internal APIs return the empty string for the root
+ * cgroup, let's return the "/" in the public APIs instead, as
+ * that's easier and less ambigious for people to grok. */
+ if (isempty(c)) {
+ free(c);
+ c = strdup("/");
+ if (!c)
+ return -ENOMEM;
+
+ }
+
+ *cgroup = c;
+ return 0;
}
_public_ int sd_peer_get_session(int fd, char **session) {
struct ucred ucred = {};
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(session, -EINVAL);
r = getpeercred(fd, &ucred);
if (r < 0)
return r;
+/// elogind does not support systemd scopes
+#if 0
return cg_pid_get_session(ucred.pid, session);
+#else
+ return -ESRCH;
+#endif // 0
}
_public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
struct ucred ucred;
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(uid, -EINVAL);
r = getpeercred(fd, &ucred);
if (r < 0)
return r;
+/// elogind does not support systemd units
+#if 0
+ return cg_pid_get_owner_uid(ucred.pid, uid);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_peer_get_unit(int fd, char **unit) {
struct ucred ucred;
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(unit, -EINVAL);
r = getpeercred(fd, &ucred);
if (r < 0)
return r;
+/// elogind does not support systemd units
+#if 0
+ return cg_pid_get_unit(ucred.pid, unit);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_peer_get_user_unit(int fd, char **unit) {
struct ucred ucred;
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(unit, -EINVAL);
r = getpeercred(fd, &ucred);
if (r < 0)
return r;
+/// elogind does not support systemd units
+#if 0
+ return cg_pid_get_user_unit(ucred.pid, unit);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_peer_get_machine_name(int fd, char **machine) {
struct ucred ucred;
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(machine, -EINVAL);
r = getpeercred(fd, &ucred);
if (r < 0)
return r;
+/// elogind does not support systemd units
+#if 0
+ return cg_pid_get_machine_name(ucred.pid, machine);
+#else
return -ESRCH;
+#endif // 0
}
_public_ int sd_peer_get_slice(int fd, char **slice) {
struct ucred ucred;
int r;
- assert_return(fd >= 0, -EINVAL);
+ assert_return(fd >= 0, -EBADF);
assert_return(slice, -EINVAL);
r = getpeercred(fd, &ucred);
if (r < 0)
return r;
+/// elogind does not support systemd slices
+#if 0
+ return cg_pid_get_slice(ucred.pid, slice);
+#else
return -ESRCH;
+#endif // 0
+}
+
+_public_ int sd_peer_get_user_slice(int fd, char **slice) {
+ struct ucred ucred;
+ int r;
+
+ assert_return(fd >= 0, -EBADF);
+ assert_return(slice, -EINVAL);
+
+ r = getpeercred(fd, &ucred);
+ if (r < 0)
+ return r;
+
+/// elogind does not support systemd slices
+#if 0
+ return cg_pid_get_user_slice(ucred.pid, slice);
+#else
+ return -ESRCH;
+#endif // 0
+}
+
+_public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
+ struct ucred ucred;
+ int r;
+
+ assert_return(fd >= 0, -EBADF);
+ assert_return(cgroup, -EINVAL);
+
+ r = getpeercred(fd, &ucred);
+ if (r < 0)
+ return r;
+
+ return sd_pid_get_cgroup(ucred.pid, cgroup);
}
static int file_of_uid(uid_t uid, char **p) {
+
+ assert_return(uid_is_valid(uid), -EINVAL);
assert(p);
if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
if (!s)
return -ENOMEM;
- } else if (r < 0) {
+ }
+ if (r < 0) {
free(s);
return r;
- } else if (!s)
+ }
+ if (isempty(s)) {
+ free(s);
return -EIO;
+ }
*state = s;
return 0;
return r;
r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
+ if (r == -ENOENT)
+ return -ENODATA;
if (r < 0)
return r;
-
if (isempty(s))
- return -ENOENT;
+ return -ENODATA;
*session = s;
s = NULL;
return 0;
}
+static int file_of_seat(const char *seat, char **_p) {
+ char *p;
+ int r;
+
+ assert(_p);
+
+ if (seat) {
+ if (!filename_is_valid(seat))
+ return -EINVAL;
+
+ p = strappend("/run/systemd/seats/", seat);
+ } else {
+ _cleanup_free_ char *buf = NULL;
+
+ r = sd_session_get_seat(NULL, &buf);
+ if (r < 0)
+ return r;
+
+ p = strappend("/run/systemd/seats/", buf);
+ }
+
+ if (!p)
+ return -ENOMEM;
+
+ *_p = p;
+ p = NULL;
+ return 0;
+}
+
_public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
_cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
size_t l;
int r;
const char *word, *variable, *state;
- assert_return(seat, -EINVAL);
+ assert_return(uid_is_valid(uid), -EINVAL);
- variable = require_active ? "ACTIVE_UID" : "UIDS";
+ r = file_of_seat(seat, &p);
+ if (r < 0)
+ return r;
- p = strappend("/run/systemd/seats/", seat);
- if (!p)
- return -ENOMEM;
+ variable = require_active ? "ACTIVE_UID" : "UIDS";
r = parse_env_file(p, NEWLINE, variable, &s, NULL);
-
+ if (r == -ENOENT)
+ return 0;
if (r < 0)
return r;
-
- if (!s)
- return -EIO;
+ if (isempty(s))
+ return 0;
if (asprintf(&t, UID_FMT, uid) < 0)
return -ENOMEM;
- FOREACH_WORD(word, l, s, state) {
+ FOREACH_WORD(word, l, s, state)
if (strneq(t, word, l))
return 1;
- }
return 0;
}
char **a;
int r;
+ assert(variable);
+
r = file_of_uid(uid, &p);
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
- variable, &s,
- NULL);
- if (r < 0) {
- if (r == -ENOENT) {
- if (array)
- *array = NULL;
- return 0;
- }
-
- return r;
- }
-
- if (!s) {
+ r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ if (r == -ENOENT || (r >= 0 && isempty(s))) {
if (array)
*array = NULL;
return 0;
}
+ if (r < 0)
+ return r;
a = strv_split(s, " ");
-
if (!a)
return -ENOMEM;
}
_public_ int sd_session_is_active(const char *session) {
- int r;
_cleanup_free_ char *p = NULL, *s = NULL;
+ int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
- if (!s)
+ if (isempty(s))
return -EIO;
return parse_boolean(s);
}
_public_ int sd_session_is_remote(const char *session) {
- int r;
_cleanup_free_ char *p = NULL, *s = NULL;
+ int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
- if (!s)
- return -EIO;
+ if (isempty(s))
+ return -ENODATA;
return parse_boolean(s);
}
return r;
r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
- else if (!s)
+ if (isempty(s))
return -EIO;
*state = s;
return r;
r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
- if (!s)
+ if (isempty(s))
return -EIO;
return parse_uid(s, uid);
int r;
assert_return(value, -EINVAL);
+ assert(field);
r = file_of_session(session, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE, field, &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
if (isempty(s))
- return -ENOENT;
+ return -ENODATA;
*value = s;
s = NULL;
unsigned u;
int r;
+ assert_return(vtnr, -EINVAL);
+
r = session_get_string(session, "VTNR", &vtnr_string);
if (r < 0)
return r;
if (r < 0)
return r;
- t = cunescape(escaped);
- if (!t)
- return -ENOMEM;
+ r = cunescape(escaped, 0, &t);
+ if (r < 0)
+ return r;
*desktop = t;
return 0;
return session_get_string(session, "REMOTE_HOST", remote_host);
}
-static int file_of_seat(const char *seat, char **_p) {
- char *p;
- int r;
-
- assert(_p);
-
- if (seat)
- p = strappend("/run/systemd/seats/", seat);
- else {
- _cleanup_free_ char *buf = NULL;
-
- r = sd_session_get_seat(NULL, &buf);
- if (r < 0)
- return r;
-
- p = strappend("/run/systemd/seats/", buf);
- }
-
- if (!p)
- return -ENOMEM;
-
- *_p = p;
- p = NULL;
- return 0;
-}
-
_public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
_cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
int r;
"ACTIVE", &s,
"ACTIVE_UID", &t,
NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
if (session && !s)
- return -ENOENT;
+ return -ENODATA;
if (uid && !t)
- return -ENOENT;
+ return -ENODATA;
if (uid && t) {
r = parse_uid(t, uid);
"SESSIONS", &s,
"ACTIVE_SESSIONS", &t,
NULL);
-
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
return -ENOMEM;
r = parse_uid(k, b + i);
-
if (r < 0)
continue;
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
- assert_return(variable, -EINVAL);
+ assert(variable);
r = file_of_seat(seat, &p);
if (r < 0)
r = parse_env_file(p, NEWLINE,
variable, &s,
NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
- if (!s)
- return 0;
+ if (isempty(s))
+ return -ENODATA;
return parse_boolean(s);
}
/* Filter out the unit: symlinks */
for (a = l, b = l; *a; a++) {
- if (startswith(*a, "unit:"))
+ if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
free(*a);
else {
*b = *a;
p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
if (!c)
p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
if (!netif) {
#include "util.h"
#include "strv.h"
+#include "formats-util.h"
static void test_login(void) {
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_free_ char *pp = NULL, *qq = NULL;
int r, k;
uid_t u, u2;
- char *seat, *type, *class, *display, *remote_user, *remote_host;
+ char *seat, *type, *class, *display, *remote_user, *remote_host, *display_session, *cgroup;
char *session;
char *state;
char *session2;
assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
printf("user = "UID_FMT"\n", u2);
+ assert_se(sd_pid_get_cgroup(0, &cgroup) == 0);
+ printf("cgroup = %s\n", cgroup);
+ free(cgroup);
+
+ display_session = NULL;
+ r = sd_uid_get_display(u2, &display_session);
+ assert_se(r >= 0 || r == -ENODATA);
+ printf("user's display session = %s\n", strna(display_session));
+ free(display_session);
+
assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
sd_peer_get_session(pair[0], &pp);
sd_peer_get_session(pair[1], &qq);
printf("class = %s\n", class);
free(class);
- assert_se(sd_session_get_display(session, &display) >= 0);
- printf("display = %s\n", display);
+ display = NULL;
+ r = sd_session_get_display(session, &display);
+ assert_se(r >= 0 || r == -ENODATA);
+ printf("display = %s\n", strna(display));
free(display);
- assert_se(sd_session_get_remote_user(session, &remote_user) >= 0);
- printf("remote_user = %s\n", remote_user);
+ remote_user = NULL;
+ r = sd_session_get_remote_user(session, &remote_user);
+ assert_se(r >= 0 || r == -ENODATA);
+ printf("remote_user = %s\n", strna(remote_user));
free(remote_user);
- assert_se(sd_session_get_remote_host(session, &remote_host) >= 0);
- printf("remote_host = %s\n", remote_host);
+ remote_host = NULL;
+ r = sd_session_get_remote_host(session, &remote_host);
+ assert_se(r >= 0 || r == -ENODATA);
+ printf("remote_host = %s\n", strna(remote_host));
free(remote_host);
assert_se(sd_session_get_seat(session, &seat) >= 0);
}
int main(int argc, char* argv[]) {
+ elogind_set_program_name(argv[0]);
log_parse_environment();
log_open();
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "strv.h"
-#include "network-util.h"
-
-bool network_is_online(void) {
- _cleanup_free_ char *state = NULL;
- int r;
-
- r = sd_network_get_operational_state(&state);
- if (r < 0) /* if we don't know anything, we consider the system online */
- return true;
-
- if (STR_IN_SET(state, "routable", "degraded"))
- return true;
-
- return false;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Thomas Hindø Paabøl Andersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "sd-network.h"
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_network_monitor*, sd_network_monitor_unref);
-#define _cleanup_network_monitor_unref_ _cleanup_(sd_network_monitor_unrefp)
-
-bool network_is_online(void);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
- Copyright 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-#include <errno.h>
-#include <sys/inotify.h>
-#include <poll.h>
-
-#include "util.h"
-#include "macro.h"
-#include "strv.h"
-#include "fileio.h"
-#include "sd-network.h"
-
-_public_ int sd_network_get_operational_state(char **state) {
- _cleanup_free_ char *s = NULL;
- int r;
-
- assert_return(state, -EINVAL);
-
- r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s))
- return -ENODATA;
-
- *state = s;
- s = NULL;
-
- return 0;
-}
-
-static int network_get_strv(const char *key, char ***ret) {
- _cleanup_strv_free_ char **a = NULL;
- _cleanup_free_ char *s = NULL;
- int r;
-
- assert_return(ret, -EINVAL);
-
- r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s)) {
- *ret = NULL;
- return 0;
- }
-
- a = strv_split(s, " ");
- if (!a)
- return -ENOMEM;
-
- strv_uniq(a);
- r = strv_length(a);
-
- *ret = a;
- a = NULL;
-
- return r;
-}
-
-_public_ int sd_network_get_dns(char ***ret) {
- return network_get_strv("DNS", ret);
-}
-
-_public_ int sd_network_get_ntp(char ***ret) {
- return network_get_strv("NTP", ret);
-}
-
-_public_ int sd_network_get_domains(char ***ret) {
- return network_get_strv("DOMAINS", ret);
-}
-
-_public_ int sd_network_link_get_setup_state(int ifindex, char **state) {
- _cleanup_free_ char *s = NULL, *p = NULL;
- int r;
-
- assert_return(ifindex > 0, -EINVAL);
- assert_return(state, -EINVAL);
-
- if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
- return -ENOMEM;
-
- r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s))
- return -ENODATA;
-
- *state = s;
- s = NULL;
-
- return 0;
-}
-
-_public_ int sd_network_link_get_network_file(int ifindex, char **filename) {
- _cleanup_free_ char *s = NULL, *p = NULL;
- int r;
-
- assert_return(ifindex > 0, -EINVAL);
- assert_return(filename, -EINVAL);
-
- if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
- return -ENOMEM;
-
- r = parse_env_file(p, NEWLINE, "NETWORK_FILE", &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s))
- return -ENODATA;
-
- *filename = s;
- s = NULL;
-
- return 0;
-}
-
-_public_ int sd_network_link_get_operational_state(int ifindex, char **state) {
- _cleanup_free_ char *s = NULL, *p = NULL;
- int r;
-
- assert_return(ifindex > 0, -EINVAL);
- assert_return(state, -EINVAL);
-
- if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
- return -ENOMEM;
-
- r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s))
- return -ENODATA;
-
- *state = s;
- s = NULL;
-
- return 0;
-}
-
-_public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
- _cleanup_free_ char *s = NULL, *p = NULL;
- int r;
-
- assert_return(ifindex > 0, -EINVAL);
- assert_return(llmnr, -EINVAL);
-
- if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
- return -ENOMEM;
-
- r = parse_env_file(p, NEWLINE, "LLMNR", &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s))
- return -ENODATA;
-
- *llmnr = s;
- s = NULL;
-
- return 0;
-}
-
-_public_ int sd_network_link_get_lldp(int ifindex, char **lldp) {
- _cleanup_free_ char *s = NULL, *p = NULL;
- size_t size;
- int r;
-
- assert_return(ifindex > 0, -EINVAL);
- assert_return(lldp, -EINVAL);
-
- if (asprintf(&p, "/run/systemd/netif/lldp/%d", ifindex) < 0)
- return -ENOMEM;
-
- r = read_full_file(p, &s, &size);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (size <= 0)
- return -ENODATA;
-
- *lldp = s;
- s = NULL;
-
- return 0;
-}
-
-
-static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
- _cleanup_free_ char *p = NULL, *s = NULL;
- _cleanup_strv_free_ char **a = NULL;
- int r;
-
- assert_return(ifindex > 0, -EINVAL);
- assert_return(ret, -EINVAL);
-
- if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
- return -ENOMEM;
-
- r = parse_env_file(p, NEWLINE, key, &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s)) {
- *ret = NULL;
- return 0;
- }
-
- a = strv_split(s, " ");
- if (!a)
- return -ENOMEM;
-
- strv_uniq(a);
- r = strv_length(a);
-
- *ret = a;
- a = NULL;
-
- return r;
-}
-
-_public_ int sd_network_link_get_dns(int ifindex, char ***ret) {
- return network_get_link_strv("DNS", ifindex, ret);
-}
-
-_public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
- return network_get_link_strv("NTP", ifindex, ret);
-}
-
-_public_ int sd_network_link_get_domains(int ifindex, char ***ret) {
- return network_get_link_strv("DOMAINS", ifindex, ret);
-}
-
-_public_ int sd_network_link_get_carrier_bound_to(int ifindex, char ***ret) {
- return network_get_link_strv("CARRIER_BOUND_TO", ifindex, ret);
-}
-
-_public_ int sd_network_link_get_carrier_bound_by(int ifindex, char ***ret) {
- return network_get_link_strv("CARRIER_BOUND_BY", ifindex, ret);
-}
-
-_public_ int sd_network_link_get_wildcard_domain(int ifindex) {
- int r;
- _cleanup_free_ char *p = NULL, *s = NULL;
-
- assert_return(ifindex > 0, -EINVAL);
-
- if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
- return -ENOMEM;
-
- r = parse_env_file(p, NEWLINE, "WILDCARD_DOMAIN", &s, NULL);
- if (r == -ENOENT)
- return -ENODATA;
- if (r < 0)
- return r;
- if (isempty(s))
- return -ENODATA;
-
- return parse_boolean(s);
-}
-
-static inline int MONITOR_TO_FD(sd_network_monitor *m) {
- return (int) (unsigned long) m - 1;
-}
-
-static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
- return (sd_network_monitor*) (unsigned long) (fd + 1);
-}
-
-_public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
- int fd, k;
- bool good = false;
-
- assert_return(m, -EINVAL);
-
- fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- if (!category || streq(category, "links")) {
- k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
- if (k < 0) {
- safe_close(fd);
- return -errno;
- }
-
- good = true;
- }
-
- if (!good) {
- close_nointr(fd);
- return -EINVAL;
- }
-
- *m = FD_TO_MONITOR(fd);
- return 0;
-}
-
-_public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
- int fd;
-
- assert_return(m, NULL);
-
- fd = MONITOR_TO_FD(m);
- close_nointr(fd);
-
- return NULL;
-}
-
-_public_ int sd_network_monitor_flush(sd_network_monitor *m) {
-
- assert_return(m, -EINVAL);
-
- return flush_fd(MONITOR_TO_FD(m));
-}
-
-_public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
-
- assert_return(m, -EINVAL);
-
- return MONITOR_TO_FD(m);
-}
-
-_public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
-
- assert_return(m, -EINVAL);
-
- /* For now we will only return POLLIN here, since we don't
- * need anything else ever for inotify. However, let's have
- * this API to keep our options open should we later on need
- * it. */
- return POLLIN;
-}
-
-_public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
-
- assert_return(m, -EINVAL);
- assert_return(timeout_usec, -EINVAL);
-
- /* For now we will only return (uint64_t) -1, since we don't
- * need any timeout. However, let's have this API to keep our
- * options open should we later on need it. */
- *timeout_usec = (uint64_t) -1;
- return 0;
-}
+++ /dev/null
-../Makefile
\ No newline at end of file
+++ /dev/null
-../../Makefile
\ No newline at end of file
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Daniel Buch
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-#include "sd-resolve.h"
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_resolve*, sd_resolve_unref);
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_resolve_query*, sd_resolve_query_unref);
-
-#define _cleanup_resolve_unref_ _cleanup_(sd_resolve_unrefp)
-#define _cleanup_resolve_query_unref_ _cleanup_(sd_resolve_query_unrefp)
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2005-2008 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <signal.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <resolv.h>
-#include <stdint.h>
-#include <pthread.h>
-#include <sys/prctl.h>
-#include <poll.h>
-
-#include "util.h"
-#include "list.h"
-#include "socket-util.h"
-#include "missing.h"
-#include "resolve-util.h"
-#include "sd-resolve.h"
-
-#define WORKERS_MIN 1U
-#define WORKERS_MAX 16U
-#define QUERIES_MAX 256U
-#define BUFSIZE 10240U
-
-typedef enum {
- REQUEST_ADDRINFO,
- RESPONSE_ADDRINFO,
- REQUEST_NAMEINFO,
- RESPONSE_NAMEINFO,
- REQUEST_RES_QUERY,
- REQUEST_RES_SEARCH,
- RESPONSE_RES,
- REQUEST_TERMINATE,
- RESPONSE_DIED
-} QueryType;
-
-enum {
- REQUEST_RECV_FD,
- REQUEST_SEND_FD,
- RESPONSE_RECV_FD,
- RESPONSE_SEND_FD,
- _FD_MAX
-};
-
-struct sd_resolve {
- unsigned n_ref;
-
- bool dead:1;
- pid_t original_pid;
-
- int fds[_FD_MAX];
-
- pthread_t workers[WORKERS_MAX];
- unsigned n_valid_workers;
-
- unsigned current_id;
- sd_resolve_query* query_array[QUERIES_MAX];
- unsigned n_queries, n_done, n_outstanding;
-
- sd_event_source *event_source;
- sd_event *event;
-
- sd_resolve_query *current;
-
- sd_resolve **default_resolve_ptr;
- pid_t tid;
-
- LIST_HEAD(sd_resolve_query, queries);
-};
-
-struct sd_resolve_query {
- unsigned n_ref;
-
- sd_resolve *resolve;
-
- QueryType type:4;
- bool done:1;
- bool floating:1;
- unsigned id;
-
- int ret;
- int _errno;
- int _h_errno;
- struct addrinfo *addrinfo;
- char *serv, *host;
- unsigned char *answer;
-
- union {
- sd_resolve_getaddrinfo_handler_t getaddrinfo_handler;
- sd_resolve_getnameinfo_handler_t getnameinfo_handler;
- sd_resolve_res_handler_t res_handler;
- };
-
- void *userdata;
-
- LIST_FIELDS(sd_resolve_query, queries);
-};
-
-typedef struct RHeader {
- QueryType type;
- unsigned id;
- size_t length;
-} RHeader;
-
-typedef struct AddrInfoRequest {
- struct RHeader header;
- bool hints_valid;
- int ai_flags;
- int ai_family;
- int ai_socktype;
- int ai_protocol;
- size_t node_len, service_len;
-} AddrInfoRequest;
-
-typedef struct AddrInfoResponse {
- struct RHeader header;
- int ret;
- int _errno;
- int _h_errno;
- /* followed by addrinfo_serialization[] */
-} AddrInfoResponse;
-
-typedef struct AddrInfoSerialization {
- int ai_flags;
- int ai_family;
- int ai_socktype;
- int ai_protocol;
- size_t ai_addrlen;
- size_t canonname_len;
- /* Followed by ai_addr amd ai_canonname with variable lengths */
-} AddrInfoSerialization;
-
-typedef struct NameInfoRequest {
- struct RHeader header;
- int flags;
- socklen_t sockaddr_len;
- bool gethost:1, getserv:1;
-} NameInfoRequest;
-
-typedef struct NameInfoResponse {
- struct RHeader header;
- size_t hostlen, servlen;
- int ret;
- int _errno;
- int _h_errno;
-} NameInfoResponse;
-
-typedef struct ResRequest {
- struct RHeader header;
- int class;
- int type;
- size_t dname_len;
-} ResRequest;
-
-typedef struct ResResponse {
- struct RHeader header;
- int ret;
- int _errno;
- int _h_errno;
-} ResResponse;
-
-typedef union Packet {
- RHeader rheader;
- AddrInfoRequest addrinfo_request;
- AddrInfoResponse addrinfo_response;
- NameInfoRequest nameinfo_request;
- NameInfoResponse nameinfo_response;
- ResRequest res_request;
- ResResponse res_response;
-} Packet;
-
-static int getaddrinfo_done(sd_resolve_query* q);
-static int getnameinfo_done(sd_resolve_query *q);
-static int res_query_done(sd_resolve_query* q);
-
-static void resolve_query_disconnect(sd_resolve_query *q);
-
-#define RESOLVE_DONT_DESTROY(resolve) \
- _cleanup_resolve_unref_ _unused_ sd_resolve *_dont_destroy_##resolve = sd_resolve_ref(resolve)
-
-static int send_died(int out_fd) {
-
- RHeader rh = {
- .type = RESPONSE_DIED,
- .length = sizeof(RHeader),
- };
-
- assert(out_fd >= 0);
-
- if (send(out_fd, &rh, rh.length, MSG_NOSIGNAL) < 0)
- return -errno;
-
- return 0;
-}
-
-static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) {
- AddrInfoSerialization s;
- size_t cnl, l;
-
- assert(p);
- assert(ai);
- assert(length);
- assert(*length <= maxlength);
-
- cnl = ai->ai_canonname ? strlen(ai->ai_canonname)+1 : 0;
- l = sizeof(AddrInfoSerialization) + ai->ai_addrlen + cnl;
-
- if (*length + l > maxlength)
- return NULL;
-
- s.ai_flags = ai->ai_flags;
- s.ai_family = ai->ai_family;
- s.ai_socktype = ai->ai_socktype;
- s.ai_protocol = ai->ai_protocol;
- s.ai_addrlen = ai->ai_addrlen;
- s.canonname_len = cnl;
-
- memcpy((uint8_t*) p, &s, sizeof(AddrInfoSerialization));
- memcpy((uint8_t*) p + sizeof(AddrInfoSerialization), ai->ai_addr, ai->ai_addrlen);
-
- if (ai->ai_canonname)
- memcpy((char*) p + sizeof(AddrInfoSerialization) + ai->ai_addrlen, ai->ai_canonname, cnl);
-
- *length += l;
- return (uint8_t*) p + l;
-}
-
-static int send_addrinfo_reply(
- int out_fd,
- unsigned id,
- int ret,
- struct addrinfo *ai,
- int _errno,
- int _h_errno) {
-
- AddrInfoResponse resp = {
- .header.type = RESPONSE_ADDRINFO,
- .header.id = id,
- .header.length = sizeof(AddrInfoResponse),
- .ret = ret,
- ._errno = _errno,
- ._h_errno = _h_errno,
- };
-
- struct msghdr mh = {};
- struct iovec iov[2];
- union {
- AddrInfoSerialization ais;
- uint8_t space[BUFSIZE];
- } buffer;
-
- assert(out_fd >= 0);
-
- if (ret == 0 && ai) {
- void *p = &buffer;
- struct addrinfo *k;
-
- for (k = ai; k; k = k->ai_next) {
- p = serialize_addrinfo(p, k, &resp.header.length, (uint8_t*) &buffer + BUFSIZE - (uint8_t*) p);
- if (!p) {
- freeaddrinfo(ai);
- return -ENOBUFS;
- }
- }
- }
-
- if (ai)
- freeaddrinfo(ai);
-
- iov[0] = (struct iovec) { .iov_base = &resp, .iov_len = sizeof(AddrInfoResponse) };
- iov[1] = (struct iovec) { .iov_base = &buffer, .iov_len = resp.header.length - sizeof(AddrInfoResponse) };
-
- mh.msg_iov = iov;
- mh.msg_iovlen = ELEMENTSOF(iov);
-
- if (sendmsg(out_fd, &mh, MSG_NOSIGNAL) < 0)
- return -errno;
-
- return 0;
-}
-
-static int send_nameinfo_reply(
- int out_fd,
- unsigned id,
- int ret,
- const char *host,
- const char *serv,
- int _errno,
- int _h_errno) {
-
- NameInfoResponse resp = {
- .header.type = RESPONSE_NAMEINFO,
- .header.id = id,
- .ret = ret,
- ._errno = _errno,
- ._h_errno = _h_errno,
- };
-
- struct msghdr mh = {};
- struct iovec iov[3];
- size_t hl, sl;
-
- assert(out_fd >= 0);
-
- sl = serv ? strlen(serv)+1 : 0;
- hl = host ? strlen(host)+1 : 0;
-
- resp.header.length = sizeof(NameInfoResponse) + hl + sl;
- resp.hostlen = hl;
- resp.servlen = sl;
-
- iov[0] = (struct iovec) { .iov_base = &resp, .iov_len = sizeof(NameInfoResponse) };
- iov[1] = (struct iovec) { .iov_base = (void*) host, .iov_len = hl };
- iov[2] = (struct iovec) { .iov_base = (void*) serv, .iov_len = sl };
-
- mh.msg_iov = iov;
- mh.msg_iovlen = ELEMENTSOF(iov);
-
- if (sendmsg(out_fd, &mh, MSG_NOSIGNAL) < 0)
- return -errno;
-
- return 0;
-}
-
-static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) {
-
- ResResponse resp = {
- .header.type = RESPONSE_RES,
- .header.id = id,
- .ret = ret,
- ._errno = _errno,
- ._h_errno = _h_errno,
- };
-
- struct msghdr mh = {};
- struct iovec iov[2];
- size_t l;
-
- assert(out_fd >= 0);
-
- l = ret > 0 ? (size_t) ret : 0;
-
- resp.header.length = sizeof(ResResponse) + l;
-
- iov[0] = (struct iovec) { .iov_base = &resp, .iov_len = sizeof(ResResponse) };
- iov[1] = (struct iovec) { .iov_base = (void*) answer, .iov_len = l };
-
- mh.msg_iov = iov;
- mh.msg_iovlen = ELEMENTSOF(iov);
-
- if (sendmsg(out_fd, &mh, MSG_NOSIGNAL) < 0)
- return -errno;
-
- return 0;
-}
-
-static int handle_request(int out_fd, const Packet *packet, size_t length) {
- const RHeader *req;
-
- assert(out_fd >= 0);
- assert(packet);
-
- req = &packet->rheader;
-
- assert(length >= sizeof(RHeader));
- assert(length == req->length);
-
- switch (req->type) {
-
- case REQUEST_ADDRINFO: {
- const AddrInfoRequest *ai_req = &packet->addrinfo_request;
- struct addrinfo hints = {}, *result = NULL;
- const char *node, *service;
- int ret;
-
- assert(length >= sizeof(AddrInfoRequest));
- assert(length == sizeof(AddrInfoRequest) + ai_req->node_len + ai_req->service_len);
-
- hints.ai_flags = ai_req->ai_flags;
- hints.ai_family = ai_req->ai_family;
- hints.ai_socktype = ai_req->ai_socktype;
- hints.ai_protocol = ai_req->ai_protocol;
-
- node = ai_req->node_len ? (const char*) ai_req + sizeof(AddrInfoRequest) : NULL;
- service = ai_req->service_len ? (const char*) ai_req + sizeof(AddrInfoRequest) + ai_req->node_len : NULL;
-
- ret = getaddrinfo(
- node, service,
- ai_req->hints_valid ? &hints : NULL,
- &result);
-
- /* send_addrinfo_reply() frees result */
- return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno);
- }
-
- case REQUEST_NAMEINFO: {
- const NameInfoRequest *ni_req = &packet->nameinfo_request;
- char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
- union sockaddr_union sa;
- int ret;
-
- assert(length >= sizeof(NameInfoRequest));
- assert(length == sizeof(NameInfoRequest) + ni_req->sockaddr_len);
- assert(sizeof(sa) >= ni_req->sockaddr_len);
-
- memcpy(&sa, (const uint8_t *) ni_req + sizeof(NameInfoRequest), ni_req->sockaddr_len);
-
- ret = getnameinfo(&sa.sa, ni_req->sockaddr_len,
- ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0,
- ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0,
- ni_req->flags);
-
- return send_nameinfo_reply(out_fd, req->id, ret,
- ret == 0 && ni_req->gethost ? hostbuf : NULL,
- ret == 0 && ni_req->getserv ? servbuf : NULL,
- errno, h_errno);
- }
-
- case REQUEST_RES_QUERY:
- case REQUEST_RES_SEARCH: {
- const ResRequest *res_req = &packet->res_request;
- union {
- HEADER header;
- uint8_t space[BUFSIZE];
- } answer;
- const char *dname;
- int ret;
-
- assert(length >= sizeof(ResRequest));
- assert(length == sizeof(ResRequest) + res_req->dname_len);
-
- dname = (const char *) res_req + sizeof(ResRequest);
-
- if (req->type == REQUEST_RES_QUERY)
- ret = res_query(dname, res_req->class, res_req->type, (unsigned char *) &answer, BUFSIZE);
- else
- ret = res_search(dname, res_req->class, res_req->type, (unsigned char *) &answer, BUFSIZE);
-
- return send_res_reply(out_fd, req->id, (unsigned char *) &answer, ret, errno, h_errno);
- }
-
- case REQUEST_TERMINATE:
- /* Quit */
- return -ECONNRESET;
-
- default:
- assert_not_reached("Unknown request");
- }
-
- return 0;
-}
-
-static void* thread_worker(void *p) {
- sd_resolve *resolve = p;
- sigset_t fullset;
-
- /* No signals in this thread please */
- assert_se(sigfillset(&fullset) == 0);
- assert_se(pthread_sigmask(SIG_BLOCK, &fullset, NULL) == 0);
-
- /* Assign a pretty name to this thread */
- prctl(PR_SET_NAME, (unsigned long) "sd-resolve");
-
- while (!resolve->dead) {
- union {
- Packet packet;
- uint8_t space[BUFSIZE];
- } buf;
- ssize_t length;
-
- length = recv(resolve->fds[REQUEST_RECV_FD], &buf, sizeof(buf), 0);
- if (length < 0) {
- if (errno == EINTR)
- continue;
-
- break;
- }
- if (length == 0)
- break;
-
- if (resolve->dead)
- break;
-
- if (handle_request(resolve->fds[RESPONSE_SEND_FD], &buf.packet, (size_t) length) < 0)
- break;
- }
-
- send_died(resolve->fds[RESPONSE_SEND_FD]);
-
- return NULL;
-}
-
-static int start_threads(sd_resolve *resolve, unsigned extra) {
- unsigned n;
- int r;
-
- n = resolve->n_outstanding + extra;
- n = CLAMP(n, WORKERS_MIN, WORKERS_MAX);
-
- while (resolve->n_valid_workers < n) {
-
- r = pthread_create(&resolve->workers[resolve->n_valid_workers], NULL, thread_worker, resolve);
- if (r != 0)
- return -r;
-
- resolve->n_valid_workers ++;
- }
-
- return 0;
-}
-
-static bool resolve_pid_changed(sd_resolve *r) {
- assert(r);
-
- /* We don't support people creating a resolver and keeping it
- * around after fork(). Let's complain. */
-
- return r->original_pid != getpid();
-}
-
-_public_ int sd_resolve_new(sd_resolve **ret) {
- sd_resolve *resolve = NULL;
- int i, r;
-
- assert_return(ret, -EINVAL);
-
- resolve = new0(sd_resolve, 1);
- if (!resolve)
- return -ENOMEM;
-
- resolve->n_ref = 1;
- resolve->original_pid = getpid();
-
- for (i = 0; i < _FD_MAX; i++)
- resolve->fds[i] = -1;
-
- r = socketpair(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, resolve->fds + REQUEST_RECV_FD);
- if (r < 0) {
- r = -errno;
- goto fail;
- }
-
- r = socketpair(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, resolve->fds + RESPONSE_RECV_FD);
- if (r < 0) {
- r = -errno;
- goto fail;
- }
-
- fd_inc_sndbuf(resolve->fds[REQUEST_SEND_FD], QUERIES_MAX * BUFSIZE);
- fd_inc_rcvbuf(resolve->fds[REQUEST_RECV_FD], QUERIES_MAX * BUFSIZE);
- fd_inc_sndbuf(resolve->fds[RESPONSE_SEND_FD], QUERIES_MAX * BUFSIZE);
- fd_inc_rcvbuf(resolve->fds[RESPONSE_RECV_FD], QUERIES_MAX * BUFSIZE);
-
- fd_nonblock(resolve->fds[RESPONSE_RECV_FD], true);
-
- *ret = resolve;
- return 0;
-
-fail:
- sd_resolve_unref(resolve);
- return r;
-}
-
-_public_ int sd_resolve_default(sd_resolve **ret) {
-
- static thread_local sd_resolve *default_resolve = NULL;
- sd_resolve *e = NULL;
- int r;
-
- if (!ret)
- return !!default_resolve;
-
- if (default_resolve) {
- *ret = sd_resolve_ref(default_resolve);
- return 0;
- }
-
- r = sd_resolve_new(&e);
- if (r < 0)
- return r;
-
- e->default_resolve_ptr = &default_resolve;
- e->tid = gettid();
- default_resolve = e;
-
- *ret = e;
- return 1;
-}
-
-_public_ int sd_resolve_get_tid(sd_resolve *resolve, pid_t *tid) {
- assert_return(resolve, -EINVAL);
- assert_return(tid, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- if (resolve->tid != 0) {
- *tid = resolve->tid;
- return 0;
- }
-
- if (resolve->event)
- return sd_event_get_tid(resolve->event, tid);
-
- return -ENXIO;
-}
-
-static void resolve_free(sd_resolve *resolve) {
- PROTECT_ERRNO;
- sd_resolve_query *q;
- unsigned i;
-
- assert(resolve);
-
- while ((q = resolve->queries)) {
- assert(q->floating);
- resolve_query_disconnect(q);
- sd_resolve_query_unref(q);
- }
-
- if (resolve->default_resolve_ptr)
- *(resolve->default_resolve_ptr) = NULL;
-
- resolve->dead = true;
-
- sd_resolve_detach_event(resolve);
-
- if (resolve->fds[REQUEST_SEND_FD] >= 0) {
-
- RHeader req = {
- .type = REQUEST_TERMINATE,
- .length = sizeof(req)
- };
-
- /* Send one termination packet for each worker */
- for (i = 0; i < resolve->n_valid_workers; i++)
- (void) send(resolve->fds[REQUEST_SEND_FD], &req, req.length, MSG_NOSIGNAL);
- }
-
- /* Now terminate them and wait until they are gone. */
- for (i = 0; i < resolve->n_valid_workers; i++) {
- for (;;) {
- if (pthread_join(resolve->workers[i], NULL) != EINTR)
- break;
- }
- }
-
- /* Close all communication channels */
- for (i = 0; i < _FD_MAX; i++)
- safe_close(resolve->fds[i]);
-
- free(resolve);
-}
-
-_public_ sd_resolve* sd_resolve_ref(sd_resolve *resolve) {
- assert_return(resolve, NULL);
-
- assert(resolve->n_ref >= 1);
- resolve->n_ref++;
-
- return resolve;
-}
-
-_public_ sd_resolve* sd_resolve_unref(sd_resolve *resolve) {
-
- if (!resolve)
- return NULL;
-
- assert(resolve->n_ref >= 1);
- resolve->n_ref--;
-
- if (resolve->n_ref <= 0)
- resolve_free(resolve);
-
- return NULL;
-}
-
-_public_ int sd_resolve_get_fd(sd_resolve *resolve) {
- assert_return(resolve, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- return resolve->fds[RESPONSE_RECV_FD];
-}
-
-_public_ int sd_resolve_get_events(sd_resolve *resolve) {
- assert_return(resolve, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- return resolve->n_queries > resolve->n_done ? POLLIN : 0;
-}
-
-_public_ int sd_resolve_get_timeout(sd_resolve *resolve, uint64_t *usec) {
- assert_return(resolve, -EINVAL);
- assert_return(usec, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- *usec = (uint64_t) -1;
- return 0;
-}
-
-static sd_resolve_query *lookup_query(sd_resolve *resolve, unsigned id) {
- sd_resolve_query *q;
-
- assert(resolve);
-
- q = resolve->query_array[id % QUERIES_MAX];
- if (q)
- if (q->id == id)
- return q;
-
- return NULL;
-}
-
-static int complete_query(sd_resolve *resolve, sd_resolve_query *q) {
- int r;
-
- assert(q);
- assert(!q->done);
- assert(q->resolve == resolve);
-
- q->done = true;
- resolve->n_done ++;
-
- resolve->current = sd_resolve_query_ref(q);
-
- switch (q->type) {
-
- case REQUEST_ADDRINFO:
- r = getaddrinfo_done(q);
- break;
-
- case REQUEST_NAMEINFO:
- r = getnameinfo_done(q);
- break;
-
- case REQUEST_RES_QUERY:
- case REQUEST_RES_SEARCH:
- r = res_query_done(q);
- break;
-
- default:
- assert_not_reached("Cannot complete unknown query type");
- }
-
- resolve->current = NULL;
-
- if (q->floating) {
- resolve_query_disconnect(q);
- sd_resolve_query_unref(q);
- }
-
- sd_resolve_query_unref(q);
-
- return r;
-}
-
-static int unserialize_addrinfo(const void **p, size_t *length, struct addrinfo **ret_ai) {
- AddrInfoSerialization s;
- size_t l;
- struct addrinfo *ai;
-
- assert(p);
- assert(*p);
- assert(ret_ai);
- assert(length);
-
- if (*length < sizeof(AddrInfoSerialization))
- return -EBADMSG;
-
- memcpy(&s, *p, sizeof(s));
-
- l = sizeof(AddrInfoSerialization) + s.ai_addrlen + s.canonname_len;
- if (*length < l)
- return -EBADMSG;
-
- ai = new0(struct addrinfo, 1);
- if (!ai)
- return -ENOMEM;
-
- ai->ai_flags = s.ai_flags;
- ai->ai_family = s.ai_family;
- ai->ai_socktype = s.ai_socktype;
- ai->ai_protocol = s.ai_protocol;
- ai->ai_addrlen = s.ai_addrlen;
-
- if (s.ai_addrlen > 0) {
- ai->ai_addr = memdup((const uint8_t*) *p + sizeof(AddrInfoSerialization), s.ai_addrlen);
- if (!ai->ai_addr) {
- free(ai);
- return -ENOMEM;
- }
- }
-
- if (s.canonname_len > 0) {
- ai->ai_canonname = memdup((const uint8_t*) *p + sizeof(AddrInfoSerialization) + s.ai_addrlen, s.canonname_len);
- if (!ai->ai_canonname) {
- free(ai->ai_addr);
- free(ai);
- return -ENOMEM;
- }
- }
-
- *length -= l;
- *ret_ai = ai;
- *p = ((const uint8_t*) *p) + l;
-
- return 0;
-}
-
-static int handle_response(sd_resolve *resolve, const Packet *packet, size_t length) {
- const RHeader *resp;
- sd_resolve_query *q;
- int r;
-
- assert(resolve);
-
- resp = &packet->rheader;
- assert(resp);
- assert(length >= sizeof(RHeader));
- assert(length == resp->length);
-
- if (resp->type == RESPONSE_DIED) {
- resolve->dead = true;
- return 0;
- }
-
- assert(resolve->n_outstanding > 0);
- resolve->n_outstanding--;
-
- q = lookup_query(resolve, resp->id);
- if (!q)
- return 0;
-
- switch (resp->type) {
-
- case RESPONSE_ADDRINFO: {
- const AddrInfoResponse *ai_resp = &packet->addrinfo_response;
- const void *p;
- size_t l;
- struct addrinfo *prev = NULL;
-
- assert(length >= sizeof(AddrInfoResponse));
- assert(q->type == REQUEST_ADDRINFO);
-
- q->ret = ai_resp->ret;
- q->_errno = ai_resp->_errno;
- q->_h_errno = ai_resp->_h_errno;
-
- l = length - sizeof(AddrInfoResponse);
- p = (const uint8_t*) resp + sizeof(AddrInfoResponse);
-
- while (l > 0 && p) {
- struct addrinfo *ai = NULL;
-
- r = unserialize_addrinfo(&p, &l, &ai);
- if (r < 0) {
- q->ret = EAI_SYSTEM;
- q->_errno = -r;
- q->_h_errno = 0;
- freeaddrinfo(q->addrinfo);
- q->addrinfo = NULL;
- break;
- }
-
- if (prev)
- prev->ai_next = ai;
- else
- q->addrinfo = ai;
-
- prev = ai;
- }
-
- return complete_query(resolve, q);
- }
-
- case RESPONSE_NAMEINFO: {
- const NameInfoResponse *ni_resp = &packet->nameinfo_response;
-
- assert(length >= sizeof(NameInfoResponse));
- assert(q->type == REQUEST_NAMEINFO);
-
- q->ret = ni_resp->ret;
- q->_errno = ni_resp->_errno;
- q->_h_errno = ni_resp->_h_errno;
-
- if (ni_resp->hostlen > 0) {
- q->host = strndup((const char*) ni_resp + sizeof(NameInfoResponse), ni_resp->hostlen-1);
- if (!q->host) {
- q->ret = EAI_MEMORY;
- q->_errno = ENOMEM;
- q->_h_errno = 0;
- }
- }
-
- if (ni_resp->servlen > 0) {
- q->serv = strndup((const char*) ni_resp + sizeof(NameInfoResponse) + ni_resp->hostlen, ni_resp->servlen-1);
- if (!q->serv) {
- q->ret = EAI_MEMORY;
- q->_errno = ENOMEM;
- q->_h_errno = 0;
- }
- }
-
- return complete_query(resolve, q);
- }
-
- case RESPONSE_RES: {
- const ResResponse *res_resp = &packet->res_response;
-
- assert(length >= sizeof(ResResponse));
- assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
-
- q->ret = res_resp->ret;
- q->_errno = res_resp->_errno;
- q->_h_errno = res_resp->_h_errno;
-
- if (res_resp->ret >= 0) {
- q->answer = memdup((const char *)resp + sizeof(ResResponse), res_resp->ret);
- if (!q->answer) {
- q->ret = -1;
- q->_errno = ENOMEM;
- q->_h_errno = 0;
- }
- }
-
- return complete_query(resolve, q);
- }
-
- default:
- return 0;
- }
-}
-
-_public_ int sd_resolve_process(sd_resolve *resolve) {
- RESOLVE_DONT_DESTROY(resolve);
-
- union {
- Packet packet;
- uint8_t space[BUFSIZE];
- } buf;
- ssize_t l;
- int r;
-
- assert_return(resolve, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- /* We don't allow recursively invoking sd_resolve_process(). */
- assert_return(!resolve->current, -EBUSY);
-
- l = recv(resolve->fds[RESPONSE_RECV_FD], &buf, sizeof(buf), 0);
- if (l < 0) {
- if (errno == EAGAIN)
- return 0;
-
- return -errno;
- }
- if (l == 0)
- return -ECONNREFUSED;
-
- r = handle_response(resolve, &buf.packet, (size_t) l);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-_public_ int sd_resolve_wait(sd_resolve *resolve, uint64_t timeout_usec) {
- int r;
-
- assert_return(resolve, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- if (resolve->n_done >= resolve->n_queries)
- return 0;
-
- do {
- r = fd_wait_for_event(resolve->fds[RESPONSE_RECV_FD], POLLIN, timeout_usec);
- } while (r == -EINTR);
-
- if (r < 0)
- return r;
-
- return sd_resolve_process(resolve);
-}
-
-static int alloc_query(sd_resolve *resolve, bool floating, sd_resolve_query **_q) {
- sd_resolve_query *q;
- int r;
-
- assert(resolve);
- assert(_q);
-
- if (resolve->n_queries >= QUERIES_MAX)
- return -ENOBUFS;
-
- r = start_threads(resolve, 1);
- if (r < 0)
- return r;
-
- while (resolve->query_array[resolve->current_id % QUERIES_MAX])
- resolve->current_id++;
-
- q = resolve->query_array[resolve->current_id % QUERIES_MAX] = new0(sd_resolve_query, 1);
- if (!q)
- return -ENOMEM;
-
- q->n_ref = 1;
- q->resolve = resolve;
- q->floating = floating;
- q->id = resolve->current_id++;
-
- if (!floating)
- sd_resolve_ref(resolve);
-
- LIST_PREPEND(queries, resolve->queries, q);
- resolve->n_queries++;
-
- *_q = q;
- return 0;
-}
-
-_public_ int sd_resolve_getaddrinfo(
- sd_resolve *resolve,
- sd_resolve_query **_q,
- const char *node, const char *service,
- const struct addrinfo *hints,
- sd_resolve_getaddrinfo_handler_t callback, void *userdata) {
-
- AddrInfoRequest req = {};
- struct msghdr mh = {};
- struct iovec iov[3];
- sd_resolve_query *q;
- int r;
-
- assert_return(resolve, -EINVAL);
- assert_return(node || service, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- r = alloc_query(resolve, !_q, &q);
- if (r < 0)
- return r;
-
- q->type = REQUEST_ADDRINFO;
- q->getaddrinfo_handler = callback;
- q->userdata = userdata;
-
- req.node_len = node ? strlen(node)+1 : 0;
- req.service_len = service ? strlen(service)+1 : 0;
-
- req.header.id = q->id;
- req.header.type = REQUEST_ADDRINFO;
- req.header.length = sizeof(AddrInfoRequest) + req.node_len + req.service_len;
-
- if (hints) {
- req.hints_valid = true;
- req.ai_flags = hints->ai_flags;
- req.ai_family = hints->ai_family;
- req.ai_socktype = hints->ai_socktype;
- req.ai_protocol = hints->ai_protocol;
- }
-
- iov[mh.msg_iovlen++] = (struct iovec) { .iov_base = &req, .iov_len = sizeof(AddrInfoRequest) };
- if (node)
- iov[mh.msg_iovlen++] = (struct iovec) { .iov_base = (void*) node, .iov_len = req.node_len };
- if (service)
- iov[mh.msg_iovlen++] = (struct iovec) { .iov_base = (void*) service, .iov_len = req.service_len };
- mh.msg_iov = iov;
-
- if (sendmsg(resolve->fds[REQUEST_SEND_FD], &mh, MSG_NOSIGNAL) < 0) {
- sd_resolve_query_unref(q);
- return -errno;
- }
-
- resolve->n_outstanding++;
-
- if (_q)
- *_q = q;
-
- return 0;
-}
-
-static int getaddrinfo_done(sd_resolve_query* q) {
- assert(q);
- assert(q->done);
- assert(q->getaddrinfo_handler);
-
- errno = q->_errno;
- h_errno = q->_h_errno;
-
- return q->getaddrinfo_handler(q, q->ret, q->addrinfo, q->userdata);
-}
-
-_public_ int sd_resolve_getnameinfo(
- sd_resolve *resolve,
- sd_resolve_query**_q,
- const struct sockaddr *sa, socklen_t salen,
- int flags,
- uint64_t get,
- sd_resolve_getnameinfo_handler_t callback,
- void *userdata) {
-
- NameInfoRequest req = {};
- struct msghdr mh = {};
- struct iovec iov[2];
- sd_resolve_query *q;
- int r;
-
- assert_return(resolve, -EINVAL);
- assert_return(sa, -EINVAL);
- assert_return(salen >= sizeof(struct sockaddr), -EINVAL);
- assert_return(salen <= sizeof(union sockaddr_union), -EINVAL);
- assert_return((get & ~SD_RESOLVE_GET_BOTH) == 0, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- r = alloc_query(resolve, !_q, &q);
- if (r < 0)
- return r;
-
- q->type = REQUEST_NAMEINFO;
- q->getnameinfo_handler = callback;
- q->userdata = userdata;
-
- req.header.id = q->id;
- req.header.type = REQUEST_NAMEINFO;
- req.header.length = sizeof(NameInfoRequest) + salen;
-
- req.flags = flags;
- req.sockaddr_len = salen;
- req.gethost = !!(get & SD_RESOLVE_GET_HOST);
- req.getserv = !!(get & SD_RESOLVE_GET_SERVICE);
-
- iov[0] = (struct iovec) { .iov_base = &req, .iov_len = sizeof(NameInfoRequest) };
- iov[1] = (struct iovec) { .iov_base = (void*) sa, .iov_len = salen };
-
- mh.msg_iov = iov;
- mh.msg_iovlen = 2;
-
- if (sendmsg(resolve->fds[REQUEST_SEND_FD], &mh, MSG_NOSIGNAL) < 0) {
- sd_resolve_query_unref(q);
- return -errno;
- }
-
- resolve->n_outstanding++;
-
- if (_q)
- *_q = q;
-
- return 0;
-}
-
-static int getnameinfo_done(sd_resolve_query *q) {
-
- assert(q);
- assert(q->done);
- assert(q->getnameinfo_handler);
-
- errno = q->_errno;
- h_errno= q->_h_errno;
-
- return q->getnameinfo_handler(q, q->ret, q->host, q->serv, q->userdata);
-}
-
-static int resolve_res(
- sd_resolve *resolve,
- sd_resolve_query **_q,
- QueryType qtype,
- const char *dname,
- int class, int type,
- sd_resolve_res_handler_t callback, void *userdata) {
-
- struct msghdr mh = {};
- struct iovec iov[2];
- ResRequest req = {};
- sd_resolve_query *q;
- int r;
-
- assert_return(resolve, -EINVAL);
- assert_return(dname, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!resolve_pid_changed(resolve), -ECHILD);
-
- r = alloc_query(resolve, !_q, &q);
- if (r < 0)
- return r;
-
- q->type = qtype;
- q->res_handler = callback;
- q->userdata = userdata;
-
- req.dname_len = strlen(dname) + 1;
- req.class = class;
- req.type = type;
-
- req.header.id = q->id;
- req.header.type = qtype;
- req.header.length = sizeof(ResRequest) + req.dname_len;
-
- iov[0] = (struct iovec) { .iov_base = &req, .iov_len = sizeof(ResRequest) };
- iov[1] = (struct iovec) { .iov_base = (void*) dname, .iov_len = req.dname_len };
-
- mh.msg_iov = iov;
- mh.msg_iovlen = 2;
-
- if (sendmsg(resolve->fds[REQUEST_SEND_FD], &mh, MSG_NOSIGNAL) < 0) {
- sd_resolve_query_unref(q);
- return -errno;
- }
-
- resolve->n_outstanding++;
-
- if (_q)
- *_q = q;
-
- return 0;
-}
-
-_public_ int sd_resolve_res_query(sd_resolve *resolve, sd_resolve_query** q, const char *dname, int class, int type, sd_resolve_res_handler_t callback, void *userdata) {
- return resolve_res(resolve, q, REQUEST_RES_QUERY, dname, class, type, callback, userdata);
-}
-
-_public_ int sd_resolve_res_search(sd_resolve *resolve, sd_resolve_query** q, const char *dname, int class, int type, sd_resolve_res_handler_t callback, void *userdata) {
- return resolve_res(resolve, q, REQUEST_RES_SEARCH, dname, class, type, callback, userdata);
-}
-
-static int res_query_done(sd_resolve_query* q) {
- assert(q);
- assert(q->done);
- assert(q->res_handler);
-
- errno = q->_errno;
- h_errno = q->_h_errno;
-
- return q->res_handler(q, q->ret, q->answer, q->userdata);
-}
-
-_public_ sd_resolve_query* sd_resolve_query_ref(sd_resolve_query *q) {
- assert_return(q, NULL);
-
- assert(q->n_ref >= 1);
- q->n_ref++;
-
- return q;
-}
-
-static void resolve_freeaddrinfo(struct addrinfo *ai) {
- while (ai) {
- struct addrinfo *next = ai->ai_next;
-
- free(ai->ai_addr);
- free(ai->ai_canonname);
- free(ai);
- ai = next;
- }
-}
-
-static void resolve_query_disconnect(sd_resolve_query *q) {
- sd_resolve *resolve;
- unsigned i;
-
- assert(q);
-
- if (!q->resolve)
- return;
-
- resolve = q->resolve;
- assert(resolve->n_queries > 0);
-
- if (q->done) {
- assert(resolve->n_done > 0);
- resolve->n_done--;
- }
-
- i = q->id % QUERIES_MAX;
- assert(resolve->query_array[i] == q);
- resolve->query_array[i] = NULL;
- LIST_REMOVE(queries, resolve->queries, q);
- resolve->n_queries--;
-
- q->resolve = NULL;
- if (!q->floating)
- sd_resolve_unref(resolve);
-}
-
-static void resolve_query_free(sd_resolve_query *q) {
- assert(q);
-
- resolve_query_disconnect(q);
-
- resolve_freeaddrinfo(q->addrinfo);
- free(q->host);
- free(q->serv);
- free(q->answer);
- free(q);
-}
-
-_public_ sd_resolve_query* sd_resolve_query_unref(sd_resolve_query* q) {
- if (!q)
- return NULL;
-
- assert(q->n_ref >= 1);
- q->n_ref--;
-
- if (q->n_ref <= 0)
- resolve_query_free(q);
-
- return NULL;
-}
-
-_public_ int sd_resolve_query_is_done(sd_resolve_query *q) {
- assert_return(q, -EINVAL);
- assert_return(!resolve_pid_changed(q->resolve), -ECHILD);
-
- return q->done;
-}
-
-_public_ void* sd_resolve_query_set_userdata(sd_resolve_query *q, void *userdata) {
- void *ret;
-
- assert_return(q, NULL);
- assert_return(!resolve_pid_changed(q->resolve), NULL);
-
- ret = q->userdata;
- q->userdata = userdata;
-
- return ret;
-}
-
-_public_ void* sd_resolve_query_get_userdata(sd_resolve_query *q) {
- assert_return(q, NULL);
- assert_return(!resolve_pid_changed(q->resolve), NULL);
-
- return q->userdata;
-}
-
-_public_ sd_resolve *sd_resolve_query_get_resolve(sd_resolve_query *q) {
- assert_return(q, NULL);
- assert_return(!resolve_pid_changed(q->resolve), NULL);
-
- return q->resolve;
-}
-
-static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- sd_resolve *resolve = userdata;
- int r;
-
- assert(resolve);
-
- r = sd_resolve_process(resolve);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-_public_ int sd_resolve_attach_event(sd_resolve *resolve, sd_event *event, int priority) {
- int r;
-
- assert_return(resolve, -EINVAL);
- assert_return(!resolve->event, -EBUSY);
-
- assert(!resolve->event_source);
-
- if (event)
- resolve->event = sd_event_ref(event);
- else {
- r = sd_event_default(&resolve->event);
- if (r < 0)
- return r;
- }
-
- r = sd_event_add_io(resolve->event, &resolve->event_source, resolve->fds[RESPONSE_RECV_FD], POLLIN, io_callback, resolve);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_priority(resolve->event_source, priority);
- if (r < 0)
- goto fail;
-
- return 0;
-
-fail:
- sd_resolve_detach_event(resolve);
- return r;
-}
-
-_public_ int sd_resolve_detach_event(sd_resolve *resolve) {
- assert_return(resolve, -EINVAL);
-
- if (!resolve->event)
- return 0;
-
- if (resolve->event_source) {
- sd_event_source_set_enabled(resolve->event_source, SD_EVENT_OFF);
- resolve->event_source = sd_event_source_unref(resolve->event_source);
- }
-
- resolve->event = sd_event_unref(resolve->event);
- return 1;
-}
-
-_public_ sd_event *sd_resolve_get_event(sd_resolve *resolve) {
- assert_return(resolve, NULL);
-
- return resolve->event;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2005-2008 Lennart Poettering
- Copyright 2014 Daniel Buch
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <stdio.h>
-#include <netinet/in.h>
-#include <resolv.h>
-#include <errno.h>
-
-#include "socket-util.h"
-#include "sd-resolve.h"
-#include "resolve-util.h"
-#include "macro.h"
-
-static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
- const struct addrinfo *i;
-
- assert_se(q);
-
- if (ret != 0) {
- log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret);
- return 0;
- }
-
- for (i = ai; i; i = i->ai_next) {
- _cleanup_free_ char *addr = NULL;
-
- assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
- puts(addr);
- }
-
- printf("canonical name: %s\n", strna(ai->ai_canonname));
-
- return 0;
-}
-
-static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
- assert_se(q);
-
- if (ret != 0) {
- log_error("getnameinfo error: %s %i", gai_strerror(ret), ret);
- return 0;
- }
-
- printf("Host: %s -- Serv: %s\n", strna(host), strna(serv));
- return 0;
-}
-
-static int res_handler(sd_resolve_query *q, int ret, unsigned char *answer, void *userdata) {
- int qdcount, ancount, len;
- const unsigned char *pos = answer + sizeof(HEADER);
- unsigned char *end = answer + ret;
- HEADER *head = (HEADER *) answer;
- char name[256];
- assert_se(q);
-
- if (ret < 0) {
- log_error("res_query() error: %s %i", strerror(errno), errno);
- return 0;
- }
-
- if (ret == 0) {
- log_error("No reply for SRV lookup");
- return 0;
- }
-
- qdcount = ntohs(head->qdcount);
- ancount = ntohs(head->ancount);
-
- printf("%d answers for srv lookup:\n", ancount);
-
- /* Ignore the questions */
- while (qdcount-- > 0 && (len = dn_expand(answer, end, pos, name, 255)) >= 0) {
- assert_se(len >= 0);
- pos += len + QFIXEDSZ;
- }
-
- /* Parse the answers */
- while (ancount-- > 0 && (len = dn_expand(answer, end, pos, name, 255)) >= 0) {
- /* Ignore the initial string */
- uint16_t pref, weight, port;
- assert_se(len >= 0);
- pos += len;
- /* Ignore type, ttl, class and dlen */
- pos += 10;
-
- GETSHORT(pref, pos);
- GETSHORT(weight, pos);
- GETSHORT(port, pos);
- len = dn_expand(answer, end, pos, name, 255);
- printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
- pref, weight, port, name);
-
- pos += len;
- }
-
- return 0;
-}
-
-int main(int argc, char *argv[]) {
- _cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL;
- _cleanup_resolve_unref_ sd_resolve *resolve = NULL;
- int r = 0;
-
- struct addrinfo hints = {
- .ai_family = PF_UNSPEC,
- .ai_socktype = SOCK_STREAM,
- .ai_flags = AI_CANONNAME
- };
-
- struct sockaddr_in sa = {
- .sin_family = AF_INET,
- .sin_port = htons(80)
- };
-
- assert_se(sd_resolve_default(&resolve) >= 0);
-
- /* Test a floating resolver query */
- sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
-
- /* Make a name -> address query */
- r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
- if (r < 0)
- log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
-
- /* Make an address -> name query */
- sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
- r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
- if (r < 0)
- log_error_errno(r, "sd_resolve_getnameinfo(): %m");
-
- /* Make a res_query() call */
- r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV, res_handler, NULL);
- if (r < 0)
- log_error_errno(r, "sd_resolve_res_query(): %m");
-
- /* Wait until the three queries are completed */
- while (sd_resolve_query_is_done(q1) == 0 ||
- sd_resolve_query_is_done(q2) == 0 ||
- sd_resolve_query_is_done(q3) == 0) {
-
- r = sd_resolve_wait(resolve, (uint64_t) -1);
- if (r < 0) {
- log_error_errno(r, "sd_resolve_wait(): %m");
- assert_not_reached("sd_resolve_wait() failed");
- }
- }
-
- return 0;
-}
+++ /dev/null
-../../Makefile
\ No newline at end of file
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2008-2011 Lennart Poettering
- Copyright 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "sd-rtnl.h"
-#include "rtnl-util.h"
-#include "macro.h"
-#include "local-addresses.h"
-
-static int address_compare(const void *_a, const void *_b) {
- const struct local_address *a = _a, *b = _b;
-
- /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
-
- if (a->family == AF_INET && b->family == AF_INET6)
- return -1;
- if (a->family == AF_INET6 && b->family == AF_INET)
- return 1;
-
- if (a->scope < b->scope)
- return -1;
- if (a->scope > b->scope)
- return 1;
-
- if (a->metric < b->metric)
- return -1;
- if (a->metric > b->metric)
- return 1;
-
- if (a->ifindex < b->ifindex)
- return -1;
- if (a->ifindex > b->ifindex)
- return 1;
-
- return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family));
-}
-
-int local_addresses(sd_rtnl *context, int ifindex, int af, struct local_address **ret) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_free_ struct local_address *list = NULL;
- size_t n_list = 0, n_allocated = 0;
- sd_rtnl_message *m;
- int r;
-
- assert(ret);
-
- if (context)
- rtnl = sd_rtnl_ref(context);
- else {
- r = sd_rtnl_open(&rtnl, 0);
- if (r < 0)
- return r;
- }
-
- r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, af);
- if (r < 0)
- return r;
-
- r = sd_rtnl_call(rtnl, req, 0, &reply);
- if (r < 0)
- return r;
-
- for (m = reply; m; m = sd_rtnl_message_next(m)) {
- struct local_address *a;
- unsigned char flags;
- uint16_t type;
- int ifi, family;
-
- r = sd_rtnl_message_get_errno(m);
- if (r < 0)
- return r;
-
- r = sd_rtnl_message_get_type(m, &type);
- if (r < 0)
- return r;
- if (type != RTM_NEWADDR)
- continue;
-
- r = sd_rtnl_message_addr_get_ifindex(m, &ifi);
- if (r < 0)
- return r;
- if (ifindex > 0 && ifi != ifindex)
- continue;
-
- r = sd_rtnl_message_addr_get_family(m, &family);
- if (r < 0)
- return r;
- if (af != AF_UNSPEC && af != family)
- continue;
-
- r = sd_rtnl_message_addr_get_flags(m, &flags);
- if (r < 0)
- return r;
- if (flags & IFA_F_DEPRECATED)
- continue;
-
- if (!GREEDY_REALLOC0(list, n_allocated, n_list+1))
- return -ENOMEM;
-
- a = list + n_list;
-
- r = sd_rtnl_message_addr_get_scope(m, &a->scope);
- if (r < 0)
- return r;
-
- if (ifindex == 0 && (a->scope == RT_SCOPE_HOST || a->scope == RT_SCOPE_NOWHERE))
- continue;
-
- switch (family) {
-
- case AF_INET:
- r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &a->address.in);
- if (r < 0) {
- r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &a->address.in);
- if (r < 0)
- continue;
- }
- break;
-
- case AF_INET6:
- r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6);
- if (r < 0) {
- r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6);
- if (r < 0)
- continue;
- }
- break;
-
- default:
- continue;
- }
-
- a->ifindex = ifi;
- a->family = family;
-
- n_list++;
- };
-
- if (n_list > 0)
- qsort(list, n_list, sizeof(struct local_address), address_compare);
-
- *ret = list;
- list = NULL;
-
- return (int) n_list;
-}
-
-int local_gateways(sd_rtnl *context, int ifindex, int af, struct local_address **ret) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_free_ struct local_address *list = NULL;
- sd_rtnl_message *m = NULL;
- size_t n_list = 0, n_allocated = 0;
- int r;
-
- assert(ret);
-
- if (context)
- rtnl = sd_rtnl_ref(context);
- else {
- r = sd_rtnl_open(&rtnl, 0);
- if (r < 0)
- return r;
- }
-
- r = sd_rtnl_message_new_route(rtnl, &req, RTM_GETROUTE, af, RTPROT_UNSPEC);
- if (r < 0)
- return r;
-
- r = sd_rtnl_message_request_dump(req, true);
- if (r < 0)
- return r;
-
- r = sd_rtnl_call(rtnl, req, 0, &reply);
- if (r < 0)
- return r;
-
- for (m = reply; m; m = sd_rtnl_message_next(m)) {
- struct local_address *a;
- uint16_t type;
- unsigned char dst_len, src_len;
- uint32_t ifi;
- int family;
-
- r = sd_rtnl_message_get_errno(m);
- if (r < 0)
- return r;
-
- r = sd_rtnl_message_get_type(m, &type);
- if (r < 0)
- return r;
- if (type != RTM_NEWROUTE)
- continue;
-
- /* We only care for default routes */
- r = sd_rtnl_message_route_get_dst_prefixlen(m, &dst_len);
- if (r < 0)
- return r;
- if (dst_len != 0)
- continue;
-
- r = sd_rtnl_message_route_get_src_prefixlen(m, &src_len);
- if (r < 0)
- return r;
- if (src_len != 0)
- continue;
-
- r = sd_rtnl_message_read_u32(m, RTA_OIF, &ifi);
- if (r < 0)
- return r;
- if (ifindex > 0 && (int) ifi != ifindex)
- continue;
-
- r = sd_rtnl_message_route_get_family(m, &family);
- if (r < 0)
- return r;
- if (af != AF_UNSPEC && af != family)
- continue;
-
- if (!GREEDY_REALLOC0(list, n_allocated, n_list + 1))
- return -ENOMEM;
-
- a = list + n_list;
-
- switch (family) {
- case AF_INET:
- r = sd_rtnl_message_read_in_addr(m, RTA_GATEWAY, &a->address.in);
- if (r < 0)
- continue;
-
- break;
- case AF_INET6:
- r = sd_rtnl_message_read_in6_addr(m, RTA_GATEWAY, &a->address.in6);
- if (r < 0)
- continue;
-
- break;
- default:
- continue;
- }
-
- sd_rtnl_message_read_u32(m, RTA_PRIORITY, &a->metric);
-
- a->ifindex = ifi;
- a->family = family;
-
- n_list++;
- }
-
- if (n_list > 0)
- qsort(list, n_list, sizeof(struct local_address), address_compare);
-
- *ret = list;
- list = NULL;
-
- return (int) n_list;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2008-2011 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-#include "sd-rtnl.h"
-#include "in-addr-util.h"
-
-struct local_address {
- int family, ifindex;
- unsigned char scope;
- uint32_t metric;
- union in_addr_union address;
-};
-
-int local_addresses(sd_rtnl *rtnl, int ifindex, int af, struct local_address **ret);
-
-int local_gateways(sd_rtnl *rtnl, int ifindex, int af, struct local_address **ret);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <linux/netlink.h>
-
-#include "refcnt.h"
-#include "prioq.h"
-#include "list.h"
-
-#include "sd-rtnl.h"
-
-#include "rtnl-types.h"
-
-#define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
-
-#define RTNL_WQUEUE_MAX 1024
-#define RTNL_RQUEUE_MAX 64*1024
-
-#define RTNL_CONTAINER_DEPTH 32
-
-struct reply_callback {
- sd_rtnl_message_handler_t callback;
- void *userdata;
- usec_t timeout;
- uint64_t serial;
- unsigned prioq_idx;
-};
-
-struct match_callback {
- sd_rtnl_message_handler_t callback;
- uint16_t type;
- void *userdata;
-
- LIST_FIELDS(struct match_callback, match_callbacks);
-};
-
-struct sd_rtnl {
- RefCount n_ref;
-
- int fd;
-
- union {
- struct sockaddr sa;
- struct sockaddr_nl nl;
- } sockaddr;
-
- sd_rtnl_message **rqueue;
- unsigned rqueue_size;
- size_t rqueue_allocated;
-
- sd_rtnl_message **rqueue_partial;
- unsigned rqueue_partial_size;
- size_t rqueue_partial_allocated;
-
- sd_rtnl_message **wqueue;
- unsigned wqueue_size;
- size_t wqueue_allocated;
-
- struct nlmsghdr *rbuffer;
- size_t rbuffer_allocated;
-
- bool processing:1;
-
- uint32_t serial;
-
- struct Prioq *reply_callbacks_prioq;
- Hashmap *reply_callbacks;
-
- LIST_HEAD(struct match_callback, match_callbacks);
-
- pid_t original_pid;
-
- sd_event_source *io_event_source;
- sd_event_source *time_event_source;
- sd_event_source *exit_event_source;
- sd_event *event;
-};
-
-struct sd_rtnl_message {
- RefCount n_ref;
-
- sd_rtnl *rtnl;
-
- struct nlmsghdr *hdr;
- const struct NLTypeSystem *(container_type_system[RTNL_CONTAINER_DEPTH]); /* the type of the container and all its parents */
- size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */
- unsigned n_containers; /* number of containers */
- size_t next_rta_offset; /* offset from hdr to next rta */
- size_t *rta_offset_tb[RTNL_CONTAINER_DEPTH];
- unsigned short rta_tb_size[RTNL_CONTAINER_DEPTH];
- bool sealed:1;
- bool broadcast:1;
-
- sd_rtnl_message *next; /* next in a chain of multi-part messages */
-};
-
-int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type);
-
-int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m);
-int socket_read_message(sd_rtnl *nl);
-
-int rtnl_rqueue_make_room(sd_rtnl *rtnl);
-int rtnl_rqueue_partial_make_room(sd_rtnl *rtnl);
-
-int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data);
-int rtnl_message_parse(sd_rtnl_message *m,
- size_t **rta_offset_tb,
- unsigned short *rta_tb_size,
- int max,
- struct rtattr *rta,
- unsigned int rt_len);
-
-/* Make sure callbacks don't destroy the rtnl connection */
-#define RTNL_DONT_DESTROY(rtnl) \
- _cleanup_rtnl_unref_ _unused_ sd_rtnl *_dont_destroy_##rtnl = sd_rtnl_ref(rtnl)
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <netinet/in.h>
-#include <stdbool.h>
-#include <unistd.h>
-
-#include "util.h"
-#include "refcnt.h"
-#include "missing.h"
-
-#include "sd-rtnl.h"
-#include "rtnl-util.h"
-#include "rtnl-internal.h"
-#include "rtnl-types.h"
-
-#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
-#define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
-
-#define RTA_TYPE(rta) ((rta)->rta_type & NLA_TYPE_MASK)
-
-static int message_new_empty(sd_rtnl *rtnl, sd_rtnl_message **ret) {
- sd_rtnl_message *m;
-
- assert_return(ret, -EINVAL);
-
- /* Note that 'rtnl' is currently unused, if we start using it internally
- we must take care to avoid problems due to mutual references between
- busses and their queued messages. See sd-bus.
- */
-
- m = new0(sd_rtnl_message, 1);
- if (!m)
- return -ENOMEM;
-
- m->n_ref = REFCNT_INIT;
-
- m->sealed = false;
-
- *ret = m;
-
- return 0;
-}
-
-int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- const NLType *nl_type;
- size_t size;
- int r;
-
- r = type_system_get_type(NULL, &nl_type, type);
- if (r < 0)
- return r;
-
- r = message_new_empty(rtnl, &m);
- if (r < 0)
- return r;
-
- size = NLMSG_SPACE(nl_type->size);
-
- assert(size >= sizeof(struct nlmsghdr));
- m->hdr = malloc0(size);
- if (!m->hdr)
- return -ENOMEM;
-
- m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
-
- m->container_type_system[0] = nl_type->type_system;
- m->hdr->nlmsg_len = size;
- m->hdr->nlmsg_type = type;
-
- *ret = m;
- m = NULL;
-
- return 0;
-}
-
-int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
- struct rtmsg *rtm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
-
- rtm = NLMSG_DATA(m->hdr);
-
- if ((rtm->rtm_family == AF_INET && prefixlen > 32) ||
- (rtm->rtm_family == AF_INET6 && prefixlen > 128))
- return -ERANGE;
-
- rtm->rtm_dst_len = prefixlen;
-
- return 0;
-}
-
-int sd_rtnl_message_route_set_src_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
- struct rtmsg *rtm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
-
- rtm = NLMSG_DATA(m->hdr);
-
- if ((rtm->rtm_family == AF_INET && prefixlen > 32) ||
- (rtm->rtm_family == AF_INET6 && prefixlen > 128))
- return -ERANGE;
-
- rtm->rtm_src_len = prefixlen;
-
- return 0;
-}
-
-int sd_rtnl_message_route_set_scope(sd_rtnl_message *m, unsigned char scope) {
- struct rtmsg *rtm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
-
- rtm = NLMSG_DATA(m->hdr);
-
- rtm->rtm_scope = scope;
-
- return 0;
-}
-
-int sd_rtnl_message_route_get_family(sd_rtnl_message *m, int *family) {
- struct rtmsg *rtm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
- assert_return(family, -EINVAL);
-
- rtm = NLMSG_DATA(m->hdr);
-
- *family = rtm->rtm_family;
-
- return 0;
-}
-
-int sd_rtnl_message_route_get_dst_prefixlen(sd_rtnl_message *m, unsigned char *dst_len) {
- struct rtmsg *rtm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
- assert_return(dst_len, -EINVAL);
-
- rtm = NLMSG_DATA(m->hdr);
-
- *dst_len = rtm->rtm_dst_len;
-
- return 0;
-}
-
-int sd_rtnl_message_route_get_src_prefixlen(sd_rtnl_message *m, unsigned char *src_len) {
- struct rtmsg *rtm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
- assert_return(src_len, -EINVAL);
-
- rtm = NLMSG_DATA(m->hdr);
-
- *src_len = rtm->rtm_src_len;
-
- return 0;
-}
-
-int sd_rtnl_message_new_route(sd_rtnl *rtnl, sd_rtnl_message **ret,
- uint16_t nlmsg_type, int rtm_family,
- unsigned char rtm_protocol) {
- struct rtmsg *rtm;
- int r;
-
- assert_return(rtnl_message_type_is_route(nlmsg_type), -EINVAL);
- assert_return((nlmsg_type == RTM_GETROUTE && rtm_family == AF_UNSPEC) ||
- rtm_family == AF_INET || rtm_family == AF_INET6, -EINVAL);
- assert_return(ret, -EINVAL);
-
- r = message_new(rtnl, ret, nlmsg_type);
- if (r < 0)
- return r;
-
- if (nlmsg_type == RTM_NEWROUTE)
- (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_APPEND;
-
- rtm = NLMSG_DATA((*ret)->hdr);
-
- rtm->rtm_family = rtm_family;
- rtm->rtm_scope = RT_SCOPE_UNIVERSE;
- rtm->rtm_type = RTN_UNICAST;
- rtm->rtm_table = RT_TABLE_MAIN;
- rtm->rtm_protocol = rtm_protocol;
-
- return 0;
-}
-
-int sd_rtnl_message_neigh_set_flags(sd_rtnl_message *m, uint8_t flags) {
- struct ndmsg *ndm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
-
- ndm = NLMSG_DATA(m->hdr);
- ndm->ndm_flags |= flags;
-
- return 0;
-}
-
-int sd_rtnl_message_neigh_set_state(sd_rtnl_message *m, uint16_t state) {
- struct ndmsg *ndm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
-
- ndm = NLMSG_DATA(m->hdr);
- ndm->ndm_state |= state;
-
- return 0;
-}
-
-int sd_rtnl_message_neigh_get_flags(sd_rtnl_message *m, uint8_t *flags) {
- struct ndmsg *ndm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
-
- ndm = NLMSG_DATA(m->hdr);
- *flags = ndm->ndm_flags;
-
- return 0;
-}
-
-int sd_rtnl_message_neigh_get_state(sd_rtnl_message *m, uint16_t *state) {
- struct ndmsg *ndm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
-
- ndm = NLMSG_DATA(m->hdr);
- *state = ndm->ndm_state;
-
- return 0;
-}
-
-int sd_rtnl_message_neigh_get_family(sd_rtnl_message *m, int *family) {
- struct ndmsg *ndm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
- assert_return(family, -EINVAL);
-
- ndm = NLMSG_DATA(m->hdr);
-
- *family = ndm->ndm_family;
-
- return 0;
-}
-
-int sd_rtnl_message_neigh_get_ifindex(sd_rtnl_message *m, int *index) {
- struct ndmsg *ndm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
- assert_return(index, -EINVAL);
-
- ndm = NLMSG_DATA(m->hdr);
-
- *index = ndm->ndm_ifindex;
-
- return 0;
-}
-
-int sd_rtnl_message_new_neigh(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t nlmsg_type, int index, int ndm_family) {
- struct ndmsg *ndm;
- int r;
-
- assert_return(rtnl_message_type_is_neigh(nlmsg_type), -EINVAL);
- assert_return(ndm_family == AF_INET ||
- ndm_family == AF_INET6 ||
- ndm_family == PF_BRIDGE, -EINVAL);
- assert_return(ret, -EINVAL);
-
- r = message_new(rtnl, ret, nlmsg_type);
- if (r < 0)
- return r;
-
- if (nlmsg_type == RTM_NEWNEIGH)
- (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_APPEND;
-
- ndm = NLMSG_DATA((*ret)->hdr);
-
- ndm->ndm_family = ndm_family;
- ndm->ndm_ifindex = index;
-
- return 0;
-}
-
-int sd_rtnl_message_link_set_flags(sd_rtnl_message *m, unsigned flags, unsigned change) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
- assert_return(change, -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- ifi->ifi_flags = flags;
- ifi->ifi_change = change;
-
- return 0;
-}
-
-int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- ifi->ifi_type = type;
-
- return 0;
-}
-
-int sd_rtnl_message_link_set_family(sd_rtnl_message *m, unsigned family) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- ifi->ifi_family = family;
-
- return 0;
-}
-
-int sd_rtnl_message_new_link(sd_rtnl *rtnl, sd_rtnl_message **ret,
- uint16_t nlmsg_type, int index) {
- struct ifinfomsg *ifi;
- int r;
-
- assert_return(rtnl_message_type_is_link(nlmsg_type), -EINVAL);
- assert_return(nlmsg_type != RTM_DELLINK || index > 0, -EINVAL);
- assert_return(ret, -EINVAL);
-
- r = message_new(rtnl, ret, nlmsg_type);
- if (r < 0)
- return r;
-
- if (nlmsg_type == RTM_NEWLINK)
- (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
-
- ifi = NLMSG_DATA((*ret)->hdr);
-
- ifi->ifi_family = AF_UNSPEC;
- ifi->ifi_index = index;
-
- return 0;
-}
-
-int sd_rtnl_message_request_dump(sd_rtnl_message *m, int dump) {
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(m->hdr->nlmsg_type == RTM_GETLINK ||
- m->hdr->nlmsg_type == RTM_GETADDR ||
- m->hdr->nlmsg_type == RTM_GETROUTE ||
- m->hdr->nlmsg_type == RTM_GETNEIGH,
- -EINVAL);
-
- if (dump)
- m->hdr->nlmsg_flags |= NLM_F_DUMP;
- else
- m->hdr->nlmsg_flags &= ~NLM_F_DUMP;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_set_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- if ((ifa->ifa_family == AF_INET && prefixlen > 32) ||
- (ifa->ifa_family == AF_INET6 && prefixlen > 128))
- return -ERANGE;
-
- ifa->ifa_prefixlen = prefixlen;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_set_flags(sd_rtnl_message *m, unsigned char flags) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- ifa->ifa_flags = flags;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_set_scope(sd_rtnl_message *m, unsigned char scope) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- ifa->ifa_scope = scope;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_get_family(sd_rtnl_message *m, int *family) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
- assert_return(family, -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- *family = ifa->ifa_family;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_get_prefixlen(sd_rtnl_message *m, unsigned char *prefixlen) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
- assert_return(prefixlen, -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- *prefixlen = ifa->ifa_prefixlen;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_get_scope(sd_rtnl_message *m, unsigned char *scope) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
- assert_return(scope, -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- *scope = ifa->ifa_scope;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_get_flags(sd_rtnl_message *m, unsigned char *flags) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
- assert_return(flags, -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- *flags = ifa->ifa_flags;
-
- return 0;
-}
-
-int sd_rtnl_message_addr_get_ifindex(sd_rtnl_message *m, int *ifindex) {
- struct ifaddrmsg *ifa;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
- assert_return(ifindex, -EINVAL);
-
- ifa = NLMSG_DATA(m->hdr);
-
- *ifindex = ifa->ifa_index;
-
- return 0;
-}
-
-int sd_rtnl_message_new_addr(sd_rtnl *rtnl, sd_rtnl_message **ret,
- uint16_t nlmsg_type, int index,
- int family) {
- struct ifaddrmsg *ifa;
- int r;
-
- assert_return(rtnl_message_type_is_addr(nlmsg_type), -EINVAL);
- assert_return((nlmsg_type == RTM_GETADDR && index == 0) ||
- index > 0, -EINVAL);
- assert_return((nlmsg_type == RTM_GETADDR && family == AF_UNSPEC) ||
- family == AF_INET || family == AF_INET6, -EINVAL);
- assert_return(ret, -EINVAL);
-
- r = message_new(rtnl, ret, nlmsg_type);
- if (r < 0)
- return r;
-
- if (nlmsg_type == RTM_GETADDR)
- (*ret)->hdr->nlmsg_flags |= NLM_F_DUMP;
-
- ifa = NLMSG_DATA((*ret)->hdr);
-
- ifa->ifa_index = index;
- ifa->ifa_family = family;
- if (family == AF_INET)
- ifa->ifa_prefixlen = 32;
- else if (family == AF_INET6)
- ifa->ifa_prefixlen = 128;
-
- return 0;
-}
-
-int sd_rtnl_message_new_addr_update(sd_rtnl *rtnl, sd_rtnl_message **ret,
- int index, int family) {
- int r;
-
- r = sd_rtnl_message_new_addr(rtnl, ret, RTM_NEWADDR, index, family);
- if (r < 0)
- return r;
-
- (*ret)->hdr->nlmsg_flags |= NLM_F_REPLACE;
-
- return 0;
-}
-
-sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m) {
- if (m)
- assert_se(REFCNT_INC(m->n_ref) >= 2);
-
- return m;
-}
-
-sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
- if (m && REFCNT_DEC(m->n_ref) == 0) {
- unsigned i;
-
- free(m->hdr);
-
- for (i = 0; i <= m->n_containers; i++)
- free(m->rta_offset_tb[i]);
-
- sd_rtnl_message_unref(m->next);
-
- free(m);
- }
-
- return NULL;
-}
-
-int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type) {
- assert_return(m, -EINVAL);
- assert_return(type, -EINVAL);
-
- *type = m->hdr->nlmsg_type;
-
- return 0;
-}
-
-int sd_rtnl_message_get_family(sd_rtnl_message *m, int *family) {
- assert_return(m, -EINVAL);
- assert_return(family, -EINVAL);
-
- assert(m->hdr);
-
- if (rtnl_message_type_is_link(m->hdr->nlmsg_type)) {
- struct ifinfomsg *ifi;
-
- ifi = NLMSG_DATA(m->hdr);
-
- *family = ifi->ifi_family;
-
- return 0;
- } else if (rtnl_message_type_is_route(m->hdr->nlmsg_type)) {
- struct rtmsg *rtm;
-
- rtm = NLMSG_DATA(m->hdr);
-
- *family = rtm->rtm_family;
-
- return 0;
- } else if (rtnl_message_type_is_neigh(m->hdr->nlmsg_type)) {
- struct ndmsg *ndm;
-
- ndm = NLMSG_DATA(m->hdr);
-
- *family = ndm->ndm_family;
-
- return 0;
- } else if (rtnl_message_type_is_addr(m->hdr->nlmsg_type)) {
- struct ifaddrmsg *ifa;
-
- ifa = NLMSG_DATA(m->hdr);
-
- *family = ifa->ifa_family;
-
- return 0;
- }
-
- return -EOPNOTSUPP;
-}
-
-int sd_rtnl_message_is_broadcast(sd_rtnl_message *m) {
- assert_return(m, -EINVAL);
-
- return m->broadcast;
-}
-
-int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
- assert_return(ifindex, -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- *ifindex = ifi->ifi_index;
-
- return 0;
-}
-
-int sd_rtnl_message_link_get_flags(sd_rtnl_message *m, unsigned *flags) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
- assert_return(flags, -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- *flags = ifi->ifi_flags;
-
- return 0;
-}
-
-int sd_rtnl_message_link_get_type(sd_rtnl_message *m, unsigned *type) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
- assert_return(type, -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- *type = ifi->ifi_type;
-
- return 0;
-}
-
-/* If successful the updated message will be correctly aligned, if
- unsuccessful the old message is untouched. */
-static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data, size_t data_length) {
- uint32_t rta_length;
- size_t message_length, padding_length;
- struct nlmsghdr *new_hdr;
- struct rtattr *rta;
- char *padding;
- unsigned i;
- int offset;
-
- assert(m);
- assert(m->hdr);
- assert(!m->sealed);
- assert(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len);
- assert(!data || data_length);
-
- /* get offset of the new attribute */
- offset = m->hdr->nlmsg_len;
-
- /* get the size of the new rta attribute (with padding at the end) */
- rta_length = RTA_LENGTH(data_length);
-
- /* get the new message size (with padding at the end) */
- message_length = offset + RTA_ALIGN(rta_length);
-
- /* realloc to fit the new attribute */
- new_hdr = realloc(m->hdr, message_length);
- if (!new_hdr)
- return -ENOMEM;
- m->hdr = new_hdr;
-
- /* get pointer to the attribute we are about to add */
- rta = (struct rtattr *) ((uint8_t *) m->hdr + offset);
-
- /* if we are inside containers, extend them */
- for (i = 0; i < m->n_containers; i++)
- GET_CONTAINER(m, i)->rta_len += message_length - offset;
-
- /* fill in the attribute */
- rta->rta_type = type;
- rta->rta_len = rta_length;
- if (data)
- /* we don't deal with the case where the user lies about the type
- * and gives us too little data (so don't do that)
- */
- padding = mempcpy(RTA_DATA(rta), data, data_length);
- else {
- /* if no data was passed, make sure we still initialize the padding
- note that we can have data_length > 0 (used by some containers) */
- padding = RTA_DATA(rta);
- }
-
- /* make sure also the padding at the end of the message is initialized */
- padding_length = (uint8_t*)m->hdr + message_length - (uint8_t*)padding;
- memzero(padding, padding_length);
-
- /* update message size */
- m->hdr->nlmsg_len = message_length;
-
- return offset;
-}
-
-static int message_attribute_has_type(sd_rtnl_message *m, uint16_t attribute_type, uint16_t data_type) {
- const NLType *type;
- int r;
-
- r = type_system_get_type(m->container_type_system[m->n_containers], &type, attribute_type);
- if (r < 0)
- return r;
-
- if (type->type != data_type)
- return -EINVAL;
-
- return type->size;
-}
-
-int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data) {
- size_t length, size;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
- assert_return(data, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_STRING);
- if (r < 0)
- return r;
- else
- size = (size_t)r;
-
- if (size) {
- length = strnlen(data, size+1);
- if (length > size)
- return -EINVAL;
- } else
- length = strlen(data);
-
- r = add_rtattr(m, type, data, length + 1);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t data) {
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
-
- r = message_attribute_has_type(m, type, NLA_U8);
- if (r < 0)
- return r;
-
- r = add_rtattr(m, type, &data, sizeof(uint8_t));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-
-int sd_rtnl_message_append_u16(sd_rtnl_message *m, unsigned short type, uint16_t data) {
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
-
- r = message_attribute_has_type(m, type, NLA_U16);
- if (r < 0)
- return r;
-
- r = add_rtattr(m, type, &data, sizeof(uint16_t));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t data) {
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
-
- r = message_attribute_has_type(m, type, NLA_U32);
- if (r < 0)
- return r;
-
- r = add_rtattr(m, type, &data, sizeof(uint32_t));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_in_addr(sd_rtnl_message *m, unsigned short type, const struct in_addr *data) {
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
- assert_return(data, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
- if (r < 0)
- return r;
-
- r = add_rtattr(m, type, data, sizeof(struct in_addr));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_in6_addr(sd_rtnl_message *m, unsigned short type, const struct in6_addr *data) {
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
- assert_return(data, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
- if (r < 0)
- return r;
-
- r = add_rtattr(m, type, data, sizeof(struct in6_addr));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_ether_addr(sd_rtnl_message *m, unsigned short type, const struct ether_addr *data) {
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
- assert_return(data, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_ETHER_ADDR);
- if (r < 0)
- return r;
-
- r = add_rtattr(m, type, data, ETH_ALEN);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_cache_info(sd_rtnl_message *m, unsigned short type, const struct ifa_cacheinfo *info) {
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
- assert_return(info, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_CACHE_INFO);
- if (r < 0)
- return r;
-
- r = add_rtattr(m, type, info, sizeof(struct ifa_cacheinfo));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
- size_t size;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
- assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -ERANGE);
-
- r = message_attribute_has_type(m, type, NLA_NESTED);
- if (r < 0) {
- const NLTypeSystemUnion *type_system_union;
- int family;
-
- r = message_attribute_has_type(m, type, NLA_UNION);
- if (r < 0)
- return r;
- size = (size_t) r;
-
- r = sd_rtnl_message_get_family(m, &family);
- if (r < 0)
- return r;
-
- r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
- if (r < 0)
- return r;
-
- r = type_system_union_protocol_get_type_system(type_system_union,
- &m->container_type_system[m->n_containers + 1],
- family);
- if (r < 0)
- return r;
- } else {
- size = (size_t)r;
-
- r = type_system_get_type_system(m->container_type_system[m->n_containers],
- &m->container_type_system[m->n_containers + 1],
- type);
- if (r < 0)
- return r;
- }
-
- r = add_rtattr(m, type | NLA_F_NESTED, NULL, size);
- if (r < 0)
- return r;
-
- m->container_offsets[m->n_containers ++] = r;
-
- return 0;
-}
-
-int sd_rtnl_message_open_container_union(sd_rtnl_message *m, unsigned short type, const char *key) {
- const NLTypeSystemUnion *type_system_union;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
-
- r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
- if (r < 0)
- return r;
-
- r = type_system_union_get_type_system(type_system_union,
- &m->container_type_system[m->n_containers + 1],
- key);
- if (r < 0)
- return r;
-
- r = sd_rtnl_message_append_string(m, type_system_union->match, key);
- if (r < 0)
- return r;
-
- /* do we evere need non-null size */
- r = add_rtattr(m, type, NULL, 0);
- if (r < 0)
- return r;
-
- m->container_offsets[m->n_containers ++] = r;
-
- return 0;
-}
-
-
-int sd_rtnl_message_close_container(sd_rtnl_message *m) {
- assert_return(m, -EINVAL);
- assert_return(!m->sealed, -EPERM);
- assert_return(m->n_containers > 0, -EINVAL);
-
- m->container_type_system[m->n_containers] = NULL;
- m->n_containers --;
-
- return 0;
-}
-
-int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data) {
- struct rtattr *rta;
-
- assert_return(m, -EINVAL);
- assert_return(m->sealed, -EPERM);
- assert_return(data, -EINVAL);
- assert(m->n_containers <= RTNL_CONTAINER_DEPTH);
- assert(m->rta_offset_tb[m->n_containers]);
- assert(type < m->rta_tb_size[m->n_containers]);
-
- if(!m->rta_offset_tb[m->n_containers][type])
- return -ENODATA;
-
- rta = (struct rtattr*)((uint8_t *) m->hdr + m->rta_offset_tb[m->n_containers][type]);
-
- *data = RTA_DATA(rta);
-
- return RTA_PAYLOAD(rta);
-}
-
-int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, const char **data) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_STRING);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if (strnlen(attr_data, r) >= (size_t) r)
- return -EIO;
-
- if (data)
- *data = (const char *) attr_data;
-
- return 0;
-}
-
-int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_U8);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if ((size_t) r < sizeof(uint8_t))
- return -EIO;
-
- if (data)
- *data = *(uint8_t *) attr_data;
-
- return 0;
-}
-
-int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_U16);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if ((size_t) r < sizeof(uint16_t))
- return -EIO;
-
- if (data)
- *data = *(uint16_t *) attr_data;
-
- return 0;
-}
-
-int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_U32);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if ((size_t)r < sizeof(uint32_t))
- return -EIO;
-
- if (data)
- *data = *(uint32_t *) attr_data;
-
- return 0;
-}
-
-int sd_rtnl_message_read_ether_addr(sd_rtnl_message *m, unsigned short type, struct ether_addr *data) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_ETHER_ADDR);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if ((size_t)r < sizeof(struct ether_addr))
- return -EIO;
-
- if (data)
- memcpy(data, attr_data, sizeof(struct ether_addr));
-
- return 0;
-}
-
-int sd_rtnl_message_read_cache_info(sd_rtnl_message *m, unsigned short type, struct ifa_cacheinfo *info) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_CACHE_INFO);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if ((size_t)r < sizeof(struct ifa_cacheinfo))
- return -EIO;
-
- if (info)
- memcpy(info, attr_data, sizeof(struct ifa_cacheinfo));
-
- return 0;
-}
-
-int sd_rtnl_message_read_in_addr(sd_rtnl_message *m, unsigned short type, struct in_addr *data) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if ((size_t)r < sizeof(struct in_addr))
- return -EIO;
-
- if (data)
- memcpy(data, attr_data, sizeof(struct in_addr));
-
- return 0;
-}
-
-int sd_rtnl_message_read_in6_addr(sd_rtnl_message *m, unsigned short type, struct in6_addr *data) {
- int r;
- void *attr_data;
-
- assert_return(m, -EINVAL);
-
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
- if (r < 0)
- return r;
-
- r = rtnl_message_read_internal(m, type, &attr_data);
- if (r < 0)
- return r;
- else if ((size_t)r < sizeof(struct in6_addr))
- return -EIO;
-
- if (data)
- memcpy(data, attr_data, sizeof(struct in6_addr));
-
- return 0;
-}
-
-int sd_rtnl_message_enter_container(sd_rtnl_message *m, unsigned short type) {
- const NLType *nl_type;
- const NLTypeSystem *type_system;
- void *container;
- size_t size;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -EINVAL);
-
- r = type_system_get_type(m->container_type_system[m->n_containers],
- &nl_type,
- type);
- if (r < 0)
- return r;
-
- if (nl_type->type == NLA_NESTED) {
- r = type_system_get_type_system(m->container_type_system[m->n_containers],
- &type_system,
- type);
- if (r < 0)
- return r;
- } else if (nl_type->type == NLA_UNION) {
- const NLTypeSystemUnion *type_system_union;
-
- r = type_system_get_type_system_union(m->container_type_system[m->n_containers],
- &type_system_union,
- type);
- if (r < 0)
- return r;
-
- switch (type_system_union->match_type) {
- case NL_MATCH_SIBLING:
- {
- const char *key;
-
- r = sd_rtnl_message_read_string(m, type_system_union->match, &key);
- if (r < 0)
- return r;
-
- r = type_system_union_get_type_system(type_system_union,
- &type_system,
- key);
- if (r < 0)
- return r;
-
- break;
- }
- case NL_MATCH_PROTOCOL:
- {
- int family;
-
- r = sd_rtnl_message_get_family(m, &family);
- if (r < 0)
- return r;
-
- r = type_system_union_protocol_get_type_system(type_system_union,
- &type_system,
- family);
- if (r < 0)
- return r;
-
- break;
- }
- default:
- assert_not_reached("sd-rtnl: invalid type system union type");
- }
- } else
- return -EINVAL;
-
- r = rtnl_message_read_internal(m, type, &container);
- if (r < 0)
- return r;
- else
- size = (size_t)r;
-
- m->n_containers ++;
-
- r = rtnl_message_parse(m,
- &m->rta_offset_tb[m->n_containers],
- &m->rta_tb_size[m->n_containers],
- type_system->max,
- container,
- size);
- if (r < 0) {
- m->n_containers --;
- return r;
- }
-
- m->container_type_system[m->n_containers] = type_system;
-
- return 0;
-}
-
-int sd_rtnl_message_exit_container(sd_rtnl_message *m) {
- assert_return(m, -EINVAL);
- assert_return(m->sealed, -EINVAL);
- assert_return(m->n_containers > 0, -EINVAL);
-
- free(m->rta_offset_tb[m->n_containers]);
- m->rta_offset_tb[m->n_containers] = NULL;
- m->container_type_system[m->n_containers] = NULL;
-
- m->n_containers --;
-
- return 0;
-}
-
-uint32_t rtnl_message_get_serial(sd_rtnl_message *m) {
- assert(m);
- assert(m->hdr);
-
- return m->hdr->nlmsg_seq;
-}
-
-int sd_rtnl_message_is_error(sd_rtnl_message *m) {
- assert_return(m, 0);
- assert_return(m->hdr, 0);
-
- return m->hdr->nlmsg_type == NLMSG_ERROR;
-}
-
-int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
- struct nlmsgerr *err;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
-
- if (!sd_rtnl_message_is_error(m))
- return 0;
-
- err = NLMSG_DATA(m->hdr);
-
- return err->error;
-}
-
-int rtnl_message_parse(sd_rtnl_message *m,
- size_t **rta_offset_tb,
- unsigned short *rta_tb_size,
- int max,
- struct rtattr *rta,
- unsigned int rt_len) {
- unsigned short type;
- size_t *tb;
-
- tb = new0(size_t, max + 1);
- if(!tb)
- return -ENOMEM;
-
- *rta_tb_size = max + 1;
-
- for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
- type = RTA_TYPE(rta);
-
- /* if the kernel is newer than the headers we used
- when building, we ignore out-of-range attributes
- */
- if (type > max)
- continue;
-
- if (tb[type])
- log_debug("rtnl: message parse - overwriting repeated attribute");
-
- tb[type] = (uint8_t *) rta - (uint8_t *) m->hdr;
- }
-
- *rta_offset_tb = tb;
-
- return 0;
-}
-
-/* returns the number of bytes sent, or a negative error code */
-int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
- union {
- struct sockaddr sa;
- struct sockaddr_nl nl;
- } addr = {
- .nl.nl_family = AF_NETLINK,
- };
- ssize_t k;
-
- assert(nl);
- assert(m);
- assert(m->hdr);
-
- k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len,
- 0, &addr.sa, sizeof(addr));
- if (k < 0)
- return (errno == EAGAIN) ? 0 : -errno;
-
- return k;
-}
-
-static int socket_recv_message(int fd, struct iovec *iov, uint32_t *_group, bool peek) {
- uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred)) +
- CMSG_SPACE(sizeof(struct nl_pktinfo))];
- struct msghdr msg = {
- .msg_iov = iov,
- .msg_iovlen = 1,
- .msg_control = cred_buffer,
- .msg_controllen = sizeof(cred_buffer),
- };
- struct cmsghdr *cmsg;
- uint32_t group = 0;
- bool auth = false;
- int r;
-
- assert(fd >= 0);
- assert(iov);
-
- r = recvmsg(fd, &msg, MSG_TRUNC | (peek ? MSG_PEEK : 0));
- if (r < 0) {
- /* no data */
- if (errno == ENOBUFS)
- log_debug("rtnl: kernel receive buffer overrun");
- else if (errno == EAGAIN)
- log_debug("rtnl: no data in socket");
-
- return (errno == EAGAIN || errno == EINTR) ? 0 : -errno;
- }
-
- for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
- if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_CREDENTIALS &&
- cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
- struct ucred *ucred = (void *)CMSG_DATA(cmsg);
-
- /* from the kernel */
- if (ucred->pid == 0)
- auth = true;
- else
- log_debug("rtnl: ignoring message from PID "PID_FMT, ucred->pid);
- } else if (cmsg->cmsg_level == SOL_NETLINK &&
- cmsg->cmsg_type == NETLINK_PKTINFO &&
- cmsg->cmsg_len == CMSG_LEN(sizeof(struct nl_pktinfo))) {
- struct nl_pktinfo *pktinfo = (void *)CMSG_DATA(cmsg);
-
- /* multi-cast group */
- group = pktinfo->group;
- }
- }
-
- if (!auth) {
- /* not from the kernel, ignore */
- if (peek) {
- /* drop the message */
- r = recvmsg(fd, &msg, 0);
- if (r < 0)
- return (errno == EAGAIN || errno == EINTR) ? 0 : -errno;
- }
-
- return 0;
- }
-
- if (_group)
- *_group = group;
-
- return r;
-}
-
-/* On success, the number of bytes received is returned and *ret points to the received message
- * which has a valid header and the correct size.
- * If nothing useful was received 0 is returned.
- * On failure, a negative error code is returned.
- */
-int socket_read_message(sd_rtnl *rtnl) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *first = NULL;
- struct iovec iov = {};
- uint32_t group = 0;
- bool multi_part = false, done = false;
- struct nlmsghdr *new_msg;
- size_t len;
- int r;
- unsigned i = 0;
-
- assert(rtnl);
- assert(rtnl->rbuffer);
- assert(rtnl->rbuffer_allocated >= sizeof(struct nlmsghdr));
-
- /* read nothing, just get the pending message size */
- r = socket_recv_message(rtnl->fd, &iov, NULL, true);
- if (r <= 0)
- return r;
- else
- len = (size_t)r;
-
- /* make room for the pending message */
- if (!greedy_realloc((void **)&rtnl->rbuffer,
- &rtnl->rbuffer_allocated,
- len, sizeof(uint8_t)))
- return -ENOMEM;
-
- iov.iov_base = rtnl->rbuffer;
- iov.iov_len = rtnl->rbuffer_allocated;
-
- /* read the pending message */
- r = socket_recv_message(rtnl->fd, &iov, &group, false);
- if (r <= 0)
- return r;
- else
- len = (size_t)r;
-
- if (len > rtnl->rbuffer_allocated)
- /* message did not fit in read buffer */
- return -EIO;
-
- if (NLMSG_OK(rtnl->rbuffer, len) && rtnl->rbuffer->nlmsg_flags & NLM_F_MULTI) {
- multi_part = true;
-
- for (i = 0; i < rtnl->rqueue_partial_size; i++) {
- if (rtnl_message_get_serial(rtnl->rqueue_partial[i]) ==
- rtnl->rbuffer->nlmsg_seq) {
- first = rtnl->rqueue_partial[i];
- break;
- }
- }
- }
-
- for (new_msg = rtnl->rbuffer; NLMSG_OK(new_msg, len) && !done; new_msg = NLMSG_NEXT(new_msg, len)) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- const NLType *nl_type;
-
- if (!group && new_msg->nlmsg_pid != rtnl->sockaddr.nl.nl_pid)
- /* not broadcast and not for us */
- continue;
-
- if (new_msg->nlmsg_type == NLMSG_NOOP)
- /* silently drop noop messages */
- continue;
-
- if (new_msg->nlmsg_type == NLMSG_DONE) {
- /* finished reading multi-part message */
- done = true;
-
- /* if first is not defined, put NLMSG_DONE into the receive queue. */
- if (first)
- continue;
- }
-
- /* check that we support this message type */
- r = type_system_get_type(NULL, &nl_type, new_msg->nlmsg_type);
- if (r < 0) {
- if (r == -EOPNOTSUPP)
- log_debug("sd-rtnl: ignored message with unknown type: %i",
- new_msg->nlmsg_type);
-
- continue;
- }
-
- /* check that the size matches the message type */
- if (new_msg->nlmsg_len < NLMSG_LENGTH(nl_type->size)) {
- log_debug("sd-rtnl: message larger than expected, dropping");
- continue;
- }
-
- r = message_new_empty(rtnl, &m);
- if (r < 0)
- return r;
-
- m->broadcast = !!group;
-
- m->hdr = memdup(new_msg, new_msg->nlmsg_len);
- if (!m->hdr)
- return -ENOMEM;
-
- /* seal and parse the top-level message */
- r = sd_rtnl_message_rewind(m);
- if (r < 0)
- return r;
-
- /* push the message onto the multi-part message stack */
- if (first)
- m->next = first;
- first = m;
- m = NULL;
- }
-
- if (len)
- log_debug("sd-rtnl: discarding %zu bytes of incoming message", len);
-
- if (!first)
- return 0;
-
- if (!multi_part || done) {
- /* we got a complete message, push it on the read queue */
- r = rtnl_rqueue_make_room(rtnl);
- if (r < 0)
- return r;
-
- rtnl->rqueue[rtnl->rqueue_size ++] = first;
- first = NULL;
-
- if (multi_part && (i < rtnl->rqueue_partial_size)) {
- /* remove the message form the partial read queue */
- memmove(rtnl->rqueue_partial + i,rtnl->rqueue_partial + i + 1,
- sizeof(sd_rtnl_message*) * (rtnl->rqueue_partial_size - i - 1));
- rtnl->rqueue_partial_size --;
- }
-
- return 1;
- } else {
- /* we only got a partial multi-part message, push it on the
- partial read queue */
- if (i < rtnl->rqueue_partial_size) {
- rtnl->rqueue_partial[i] = first;
- } else {
- r = rtnl_rqueue_partial_make_room(rtnl);
- if (r < 0)
- return r;
-
- rtnl->rqueue_partial[rtnl->rqueue_partial_size ++] = first;
- }
- first = NULL;
-
- return 0;
- }
-}
-
-int sd_rtnl_message_rewind(sd_rtnl_message *m) {
- const NLType *type;
- unsigned i;
- int r;
-
- assert_return(m, -EINVAL);
-
- /* don't allow appending to message once parsed */
- if (!m->sealed)
- rtnl_message_seal(m);
-
- for (i = 1; i <= m->n_containers; i++) {
- free(m->rta_offset_tb[i]);
- m->rta_offset_tb[i] = NULL;
- m->rta_tb_size[i] = 0;
- m->container_type_system[i] = NULL;
- }
-
- m->n_containers = 0;
-
- if (m->rta_offset_tb[0]) {
- /* top-level attributes have already been parsed */
- return 0;
- }
-
- assert(m->hdr);
-
- r = type_system_get_type(NULL, &type, m->hdr->nlmsg_type);
- if (r < 0)
- return r;
-
- if (type->type == NLA_NESTED) {
- const NLTypeSystem *type_system = type->type_system;
-
- assert(type_system);
-
- m->container_type_system[0] = type_system;
-
- r = rtnl_message_parse(m,
- &m->rta_offset_tb[m->n_containers],
- &m->rta_tb_size[m->n_containers],
- type_system->max,
- (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) +
- NLMSG_ALIGN(type->size)),
- NLMSG_PAYLOAD(m->hdr, type->size));
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-void rtnl_message_seal(sd_rtnl_message *m) {
- assert(m);
- assert(!m->sealed);
-
- m->sealed = true;
-}
-
-sd_rtnl_message *sd_rtnl_message_next(sd_rtnl_message *m) {
- assert_return(m, NULL);
-
- return m->next;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdint.h>
-#include <sys/socket.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
-#include <linux/in6.h>
-#include <linux/veth.h>
-#include <linux/if_bridge.h>
-#include <linux/if_addr.h>
-#include <linux/if.h>
-
-#include <linux/ip.h>
-#include <linux/if_link.h>
-#include <linux/if_tunnel.h>
-
-#include "macro.h"
-#include "util.h"
-
-#include "rtnl-types.h"
-#include "missing.h"
-
-static const NLTypeSystem rtnl_link_type_system;
-
-static const NLType rtnl_link_info_data_veth_types[VETH_INFO_MAX + 1] = {
- [VETH_INFO_PEER] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
-};
-
-static const NLType rtnl_link_info_data_ipvlan_types[IFLA_IPVLAN_MAX + 1] = {
- [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
-};
-
-static const NLType rtnl_link_info_data_macvlan_types[IFLA_MACVLAN_MAX + 1] = {
- [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
- [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
-};
-
-static const NLType rtnl_link_info_data_bridge_types[IFLA_BRIDGE_MAX + 1] = {
- [IFLA_BRIDGE_FLAGS] = { .type = NLA_U16 },
- [IFLA_BRIDGE_MODE] = { .type = NLA_U16 },
-/*
- [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY,
- .len = sizeof(struct bridge_vlan_info), },
-*/
-};
-
-static const NLType rtnl_link_info_data_vlan_types[IFLA_VLAN_MAX + 1] = {
- [IFLA_VLAN_ID] = { .type = NLA_U16 },
-/*
- [IFLA_VLAN_FLAGS] = { .len = sizeof(struct ifla_vlan_flags) },
- [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED },
- [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
-*/
- [IFLA_VLAN_PROTOCOL] = { .type = NLA_U16 },
-};
-
-static const NLType rtnl_link_info_data_vxlan_types[IFLA_VXLAN_MAX+1] = {
- [IFLA_VXLAN_ID] = { .type = NLA_U32 },
- [IFLA_VXLAN_GROUP] = {.type = NLA_IN_ADDR },
- [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
- [IFLA_VXLAN_LOCAL] = { .type = NLA_U32},
- [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
- [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
- [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
- [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
- [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
- [IFLA_VXLAN_PORT_RANGE] = { .type = NLA_U32},
- [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
- [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
- [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
- [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
-};
-
-static const NLType rtnl_link_info_data_bond_types[IFLA_BOND_MAX + 1] = {
- [IFLA_BOND_MODE] = { .type = NLA_U8 },
- [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
- [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
- [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
- [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
- [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
- [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
- [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
- [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
- [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
- [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
- [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
- [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
- [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
- [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
- [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
- [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
- [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
- [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
- [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 },
- [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 },
- [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 },
- [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
-};
-
-static const NLType rtnl_link_info_data_iptun_types[IFLA_IPTUN_MAX + 1] = {
- [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
- [IFLA_IPTUN_LOCAL] = { .type = NLA_IN_ADDR },
- [IFLA_IPTUN_REMOTE] = { .type = NLA_IN_ADDR },
- [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
- [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
- [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
- [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
- [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
- [IFLA_IPTUN_6RD_PREFIX] = { .type = NLA_IN_ADDR },
- [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
- [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
- [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
-};
-
-static const NLType rtnl_link_info_data_ipgre_types[IFLA_GRE_MAX + 1] = {
- [IFLA_GRE_LINK] = { .type = NLA_U32 },
- [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
- [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
- [IFLA_GRE_IKEY] = { .type = NLA_U32 },
- [IFLA_GRE_OKEY] = { .type = NLA_U32 },
- [IFLA_GRE_LOCAL] = { .type = NLA_IN_ADDR },
- [IFLA_GRE_REMOTE] = { .type = NLA_IN_ADDR },
- [IFLA_GRE_TTL] = { .type = NLA_U8 },
- [IFLA_GRE_TOS] = { .type = NLA_U8 },
- [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
-};
-
-static const NLType rtnl_link_info_data_ipvti_types[IFLA_VTI_MAX + 1] = {
- [IFLA_VTI_LINK] = { .type = NLA_U32 },
- [IFLA_VTI_IKEY] = { .type = NLA_U32 },
- [IFLA_VTI_OKEY] = { .type = NLA_U32 },
- [IFLA_VTI_LOCAL] = { .type = NLA_IN_ADDR },
- [IFLA_VTI_REMOTE] = { .type = NLA_IN_ADDR },
-};
-
-static const NLType rtnl_link_info_data_ip6tnl_types[IFLA_IPTUN_MAX + 1] = {
- [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
- [IFLA_IPTUN_LOCAL] = { .type = NLA_IN_ADDR },
- [IFLA_IPTUN_REMOTE] = { .type = NLA_IN_ADDR },
- [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
- [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
- [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
- [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
- [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32},
-};
-
-/* these strings must match the .kind entries in the kernel */
-static const char* const nl_union_link_info_data_table[_NL_UNION_LINK_INFO_DATA_MAX] = {
- [NL_UNION_LINK_INFO_DATA_BOND] = "bond",
- [NL_UNION_LINK_INFO_DATA_BRIDGE] = "bridge",
- [NL_UNION_LINK_INFO_DATA_VLAN] = "vlan",
- [NL_UNION_LINK_INFO_DATA_VETH] = "veth",
- [NL_UNION_LINK_INFO_DATA_DUMMY] = "dummy",
- [NL_UNION_LINK_INFO_DATA_MACVLAN] = "macvlan",
- [NL_UNION_LINK_INFO_DATA_IPVLAN] = "ipvlan",
- [NL_UNION_LINK_INFO_DATA_VXLAN] = "vxlan",
- [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = "ipip",
- [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = "gre",
- [NL_UNION_LINK_INFO_DATA_IPGRETAP_TUNNEL] = "gretap",
- [NL_UNION_LINK_INFO_DATA_IP6GRE_TUNNEL] = "ip6gre",
- [NL_UNION_LINK_INFO_DATA_IP6GRETAP_TUNNEL] = "ip6gretap",
- [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = "sit",
- [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = "vti",
- [NL_UNION_LINK_INFO_DATA_IP6TNL_TUNNEL] = "ip6tnl",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(nl_union_link_info_data, NLUnionLinkInfoData);
-
-static const NLTypeSystem rtnl_link_info_data_type_systems[_NL_UNION_LINK_INFO_DATA_MAX] = {
- [NL_UNION_LINK_INFO_DATA_BOND] = { .max = ELEMENTSOF(rtnl_link_info_data_bond_types) - 1,
- .types = rtnl_link_info_data_bond_types },
- [NL_UNION_LINK_INFO_DATA_BRIDGE] = { .max = ELEMENTSOF(rtnl_link_info_data_bridge_types) - 1,
- .types = rtnl_link_info_data_bridge_types },
- [NL_UNION_LINK_INFO_DATA_VLAN] = { .max = ELEMENTSOF(rtnl_link_info_data_vlan_types) - 1,
- .types = rtnl_link_info_data_vlan_types },
- [NL_UNION_LINK_INFO_DATA_VETH] = { .max = ELEMENTSOF(rtnl_link_info_data_veth_types) - 1,
- .types = rtnl_link_info_data_veth_types },
- [NL_UNION_LINK_INFO_DATA_MACVLAN] = { .max = ELEMENTSOF(rtnl_link_info_data_macvlan_types) - 1,
- .types = rtnl_link_info_data_macvlan_types },
- [NL_UNION_LINK_INFO_DATA_IPVLAN] = { .max = ELEMENTSOF(rtnl_link_info_data_ipvlan_types) - 1,
- .types = rtnl_link_info_data_ipvlan_types },
- [NL_UNION_LINK_INFO_DATA_VXLAN] = { .max = ELEMENTSOF(rtnl_link_info_data_vxlan_types) - 1,
- .types = rtnl_link_info_data_vxlan_types },
- [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
- .types = rtnl_link_info_data_iptun_types },
- [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
- .types = rtnl_link_info_data_ipgre_types },
- [NL_UNION_LINK_INFO_DATA_IPGRETAP_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
- .types = rtnl_link_info_data_ipgre_types },
- [NL_UNION_LINK_INFO_DATA_IP6GRE_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
- .types = rtnl_link_info_data_ipgre_types },
- [NL_UNION_LINK_INFO_DATA_IP6GRETAP_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
- .types = rtnl_link_info_data_ipgre_types },
- [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
- .types = rtnl_link_info_data_iptun_types },
- [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ipvti_types) - 1,
- .types = rtnl_link_info_data_ipvti_types },
- [NL_UNION_LINK_INFO_DATA_IP6TNL_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ip6tnl_types) - 1,
- .types = rtnl_link_info_data_ip6tnl_types },
-
-};
-
-static const NLTypeSystemUnion rtnl_link_info_data_type_system_union = {
- .num = _NL_UNION_LINK_INFO_DATA_MAX,
- .lookup = nl_union_link_info_data_from_string,
- .type_systems = rtnl_link_info_data_type_systems,
- .match_type = NL_MATCH_SIBLING,
- .match = IFLA_INFO_KIND,
-};
-
-static const NLType rtnl_link_info_types[IFLA_INFO_MAX + 1] = {
- [IFLA_INFO_KIND] = { .type = NLA_STRING },
- [IFLA_INFO_DATA] = { .type = NLA_UNION, .type_system_union = &rtnl_link_info_data_type_system_union},
-/*
- [IFLA_INFO_XSTATS],
- [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
- [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
-*/
-};
-
-static const NLTypeSystem rtnl_link_info_type_system = {
- .max = ELEMENTSOF(rtnl_link_info_types) - 1,
- .types = rtnl_link_info_types,
-};
-
-static const struct NLType rtnl_prot_info_bridge_port_types[IFLA_BRPORT_MAX + 1] = {
- [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
- [IFLA_BRPORT_COST] = { .type = NLA_U32 },
- [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
- [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
- [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
- [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
- [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
- [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
-};
-
-static const NLTypeSystem rtnl_prot_info_type_systems[AF_MAX] = {
- [AF_BRIDGE] = { .max = ELEMENTSOF(rtnl_prot_info_bridge_port_types) - 1,
- .types = rtnl_prot_info_bridge_port_types },
-};
-
-static const NLTypeSystemUnion rtnl_prot_info_type_system_union = {
- .num = AF_MAX,
- .type_systems = rtnl_prot_info_type_systems,
- .match_type = NL_MATCH_PROTOCOL,
-};
-
-static const struct NLType rtnl_af_spec_inet6_types[IFLA_INET6_MAX + 1] = {
- [IFLA_INET6_FLAGS] = { .type = NLA_U32 },
-/*
- IFLA_INET6_CONF,
- IFLA_INET6_STATS,
- IFLA_INET6_MCAST,
- IFLA_INET6_CACHEINFO,
- IFLA_INET6_ICMP6STATS,
-*/
- [IFLA_INET6_TOKEN] = { .type = NLA_IN_ADDR },
- [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
-};
-
-static const NLTypeSystem rtnl_af_spec_inet6_type_system = {
- .max = ELEMENTSOF(rtnl_af_spec_inet6_types) - 1,
- .types = rtnl_af_spec_inet6_types,
-};
-
-static const NLType rtnl_af_spec_types[AF_MAX + 1] = {
- [AF_INET6] = { .type = NLA_NESTED, .type_system = &rtnl_af_spec_inet6_type_system },
-};
-
-static const NLTypeSystem rtnl_af_spec_type_system = {
- .max = ELEMENTSOF(rtnl_af_spec_types) - 1,
- .types = rtnl_af_spec_types,
-};
-
-static const NLType rtnl_link_types[IFLA_MAX + 1 ] = {
- [IFLA_ADDRESS] = { .type = NLA_ETHER_ADDR, },
- [IFLA_BROADCAST] = { .type = NLA_ETHER_ADDR, },
- [IFLA_IFNAME] = { .type = NLA_STRING, .size = IFNAMSIZ - 1, },
- [IFLA_MTU] = { .type = NLA_U32 },
- [IFLA_LINK] = { .type = NLA_U32 },
-/*
- [IFLA_QDISC],
- [IFLA_STATS],
- [IFLA_COST],
- [IFLA_PRIORITY],
-*/
- [IFLA_MASTER] = { .type = NLA_U32 },
-/*
- [IFLA_WIRELESS],
-*/
- [IFLA_PROTINFO] = { .type = NLA_UNION, .type_system_union = &rtnl_prot_info_type_system_union },
- [IFLA_TXQLEN] = { .type = NLA_U32 },
-/*
- [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
-*/
- [IFLA_WEIGHT] = { .type = NLA_U32 },
- [IFLA_OPERSTATE] = { .type = NLA_U8 },
- [IFLA_LINKMODE] = { .type = NLA_U8 },
- [IFLA_LINKINFO] = { .type = NLA_NESTED, .type_system = &rtnl_link_info_type_system },
- [IFLA_NET_NS_PID] = { .type = NLA_U32 },
- [IFLA_IFALIAS] = { .type = NLA_STRING, .size = IFALIASZ - 1 },
-/*
- [IFLA_NUM_VF],
- [IFLA_VFINFO_LIST] = {. type = NLA_NESTED, },
- [IFLA_STATS64],
- [IFLA_VF_PORTS] = { .type = NLA_NESTED },
- [IFLA_PORT_SELF] = { .type = NLA_NESTED },
-*/
- [IFLA_AF_SPEC] = { .type = NLA_NESTED, .type_system = &rtnl_af_spec_type_system },
-/*
- [IFLA_VF_PORTS],
- [IFLA_PORT_SELF],
- [IFLA_AF_SPEC],
-*/
- [IFLA_GROUP] = { .type = NLA_U32 },
- [IFLA_NET_NS_FD] = { .type = NLA_U32 },
- [IFLA_EXT_MASK] = { .type = NLA_U32 },
- [IFLA_PROMISCUITY] = { .type = NLA_U32 },
- [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
- [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
- [IFLA_CARRIER] = { .type = NLA_U8 },
-/*
- [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
-*/
-};
-
-static const NLTypeSystem rtnl_link_type_system = {
- .max = ELEMENTSOF(rtnl_link_types) - 1,
- .types = rtnl_link_types,
-};
-
-static const NLType rtnl_address_types[IFA_MAX + 1] = {
- [IFA_ADDRESS] = { .type = NLA_IN_ADDR },
- [IFA_LOCAL] = { .type = NLA_IN_ADDR },
- [IFA_LABEL] = { .type = NLA_STRING, .size = IFNAMSIZ - 1 },
- [IFA_BROADCAST] = { .type = NLA_IN_ADDR }, /* 6? */
- [IFA_CACHEINFO] = { .type = NLA_CACHE_INFO, .size = sizeof(struct ifa_cacheinfo) },
-/*
- [IFA_ANYCAST],
- [IFA_MULTICAST],
- [IFA_FLAGS] = { .type = NLA_U32 },
-*/
-};
-
-static const NLTypeSystem rtnl_address_type_system = {
- .max = ELEMENTSOF(rtnl_address_types) - 1,
- .types = rtnl_address_types,
-};
-
-static const NLType rtnl_route_types[RTA_MAX + 1] = {
- [RTA_DST] = { .type = NLA_IN_ADDR }, /* 6? */
- [RTA_SRC] = { .type = NLA_IN_ADDR }, /* 6? */
- [RTA_IIF] = { .type = NLA_U32 },
- [RTA_OIF] = { .type = NLA_U32 },
- [RTA_GATEWAY] = { .type = NLA_IN_ADDR },
- [RTA_PRIORITY] = { .type = NLA_U32 },
- [RTA_PREFSRC] = { .type = NLA_IN_ADDR }, /* 6? */
-/*
- [RTA_METRICS] = { .type = NLA_NESTED },
- [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
-*/
- [RTA_FLOW] = { .type = NLA_U32 }, /* 6? */
-/*
- RTA_CACHEINFO,
- RTA_TABLE,
- RTA_MARK,
- RTA_MFC_STATS,
-*/
-};
-
-static const NLTypeSystem rtnl_route_type_system = {
- .max = ELEMENTSOF(rtnl_route_types) - 1,
- .types = rtnl_route_types,
-};
-
-static const NLType rtnl_neigh_types[NDA_MAX + 1] = {
- [NDA_DST] = { .type = NLA_IN_ADDR },
- [NDA_LLADDR] = { .type = NLA_ETHER_ADDR },
- [NDA_CACHEINFO] = { .type = NLA_CACHE_INFO, .size = sizeof(struct nda_cacheinfo) },
- [NDA_PROBES] = { .type = NLA_U32 },
- [NDA_VLAN] = { .type = NLA_U16 },
- [NDA_PORT] = { .type = NLA_U16 },
- [NDA_VNI] = { .type = NLA_U32 },
- [NDA_IFINDEX] = { .type = NLA_U32 },
-};
-
-static const NLTypeSystem rtnl_neigh_type_system = {
- .max = ELEMENTSOF(rtnl_neigh_types) - 1,
- .types = rtnl_neigh_types,
-};
-
-static const NLType rtnl_types[RTM_MAX + 1] = {
- [NLMSG_DONE] = { .type = NLA_META, .size = 0 },
- [NLMSG_ERROR] = { .type = NLA_META, .size = sizeof(struct nlmsgerr) },
- [RTM_NEWLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
- [RTM_DELLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
- [RTM_GETLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
- [RTM_SETLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
- [RTM_NEWADDR] = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
- [RTM_DELADDR] = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
- [RTM_GETADDR] = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
- [RTM_NEWROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
- [RTM_DELROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
- [RTM_GETROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
- [RTM_NEWNEIGH] = { .type = NLA_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
- [RTM_DELNEIGH] = { .type = NLA_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
- [RTM_GETNEIGH] = { .type = NLA_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
-};
-
-const NLTypeSystem rtnl_type_system = {
- .max = ELEMENTSOF(rtnl_types) - 1,
- .types = rtnl_types,
-};
-
-int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
- const NLType *nl_type;
-
- assert(ret);
-
- if (!type_system)
- type_system = &rtnl_type_system;
-
- assert(type_system->types);
-
- if (type > type_system->max)
- return -EOPNOTSUPP;
-
- nl_type = &type_system->types[type];
-
- if (nl_type->type == NLA_UNSPEC)
- return -EOPNOTSUPP;
-
- *ret = nl_type;
-
- return 0;
-}
-
-int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type) {
- const NLType *nl_type;
- int r;
-
- assert(ret);
-
- r = type_system_get_type(type_system, &nl_type, type);
- if (r < 0)
- return r;
-
- assert(nl_type->type == NLA_NESTED);
- assert(nl_type->type_system);
-
- *ret = nl_type->type_system;
-
- return 0;
-}
-
-int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type) {
- const NLType *nl_type;
- int r;
-
- assert(ret);
-
- r = type_system_get_type(type_system, &nl_type, type);
- if (r < 0)
- return r;
-
- assert(nl_type->type == NLA_UNION);
- assert(nl_type->type_system_union);
-
- *ret = nl_type->type_system_union;
-
- return 0;
-}
-
-int type_system_union_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
- int type;
-
- assert(type_system_union);
- assert(type_system_union->match_type == NL_MATCH_SIBLING);
- assert(type_system_union->lookup);
- assert(type_system_union->type_systems);
- assert(ret);
- assert(key);
-
- type = type_system_union->lookup(key);
- if (type < 0)
- return -EOPNOTSUPP;
-
- assert(type < type_system_union->num);
-
- *ret = &type_system_union->type_systems[type];
-
- return 0;
-}
-
-int type_system_union_protocol_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol) {
- const NLTypeSystem *type_system;
-
- assert(type_system_union);
- assert(type_system_union->type_systems);
- assert(type_system_union->match_type == NL_MATCH_PROTOCOL);
- assert(ret);
-
- if (protocol >= type_system_union->num)
- return -EOPNOTSUPP;
-
- type_system = &type_system_union->type_systems[protocol];
- if (type_system->max == 0)
- return -EOPNOTSUPP;
-
- *ret = type_system;
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-enum {
- NLA_UNSPEC,
- NLA_META,
- NLA_U8,
- NLA_U16,
- NLA_U32,
- NLA_U64,
- NLA_STRING,
- NLA_IN_ADDR,
- NLA_ETHER_ADDR,
- NLA_CACHE_INFO,
- NLA_NESTED,
- NLA_UNION,
-};
-
-typedef enum NLMatchType {
- NL_MATCH_SIBLING,
- NL_MATCH_PROTOCOL,
-} NLMatchType;
-
-typedef struct NLTypeSystemUnion NLTypeSystemUnion;
-typedef struct NLTypeSystem NLTypeSystem;
-typedef struct NLType NLType;
-
-struct NLTypeSystemUnion {
- int num;
- NLMatchType match_type;
- uint16_t match;
- int (*lookup)(const char *);
- const NLTypeSystem *type_systems;
-};
-
-struct NLTypeSystem {
- uint16_t max;
- const NLType *types;
-};
-
-struct NLType {
- uint16_t type;
- size_t size;
- const NLTypeSystem *type_system;
- const NLTypeSystemUnion *type_system_union;
-};
-
-int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type);
-int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type);
-int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type);
-int type_system_union_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key);
-int type_system_union_protocol_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol);
-
-typedef enum NLUnionLinkInfoData {
- NL_UNION_LINK_INFO_DATA_BOND,
- NL_UNION_LINK_INFO_DATA_BRIDGE,
- NL_UNION_LINK_INFO_DATA_VLAN,
- NL_UNION_LINK_INFO_DATA_VETH,
- NL_UNION_LINK_INFO_DATA_DUMMY,
- NL_UNION_LINK_INFO_DATA_MACVLAN,
- NL_UNION_LINK_INFO_DATA_IPVLAN,
- NL_UNION_LINK_INFO_DATA_VXLAN,
- NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL,
- NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL,
- NL_UNION_LINK_INFO_DATA_IPGRETAP_TUNNEL,
- NL_UNION_LINK_INFO_DATA_IP6GRE_TUNNEL,
- NL_UNION_LINK_INFO_DATA_IP6GRETAP_TUNNEL,
- NL_UNION_LINK_INFO_DATA_SIT_TUNNEL,
- NL_UNION_LINK_INFO_DATA_VTI_TUNNEL,
- NL_UNION_LINK_INFO_DATA_IP6TNL_TUNNEL,
- _NL_UNION_LINK_INFO_DATA_MAX,
- _NL_UNION_LINK_INFO_DATA_INVALID = -1
-} NLUnionLinkInfoData;
-
-const char *nl_union_link_info_data_to_string(NLUnionLinkInfoData p) _const_;
-NLUnionLinkInfoData nl_union_link_info_data_from_string(const char *p) _pure_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-#include "sd-rtnl.h"
-
-#include "rtnl-util.h"
-#include "rtnl-internal.h"
-
-int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
- int r;
-
- assert(rtnl);
- assert(ifindex > 0);
- assert(name);
-
- if (!*rtnl) {
- r = sd_rtnl_open(rtnl, 0);
- if (r < 0)
- return r;
- }
-
- r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
- if (r < 0)
- return r;
-
- r = sd_rtnl_message_append_string(message, IFLA_IFNAME, name);
- if (r < 0)
- return r;
-
- r = sd_rtnl_call(*rtnl, message, 0, NULL);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int rtnl_set_link_properties(sd_rtnl **rtnl, int ifindex, const char *alias,
- const struct ether_addr *mac, unsigned mtu) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
- int r;
-
- assert(rtnl);
- assert(ifindex > 0);
-
- if (!alias && !mac && mtu == 0)
- return 0;
-
- if (!*rtnl) {
- r = sd_rtnl_open(rtnl, 0);
- if (r < 0)
- return r;
- }
-
- r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
- if (r < 0)
- return r;
-
- if (alias) {
- r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
- if (r < 0)
- return r;
- }
-
- if (mac) {
- r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
- if (r < 0)
- return r;
- }
-
- if (mtu > 0) {
- r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
- if (r < 0)
- return r;
- }
-
- r = sd_rtnl_call(*rtnl, message, 0, NULL);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret) {
- struct nlmsgerr *err;
- int r;
-
- assert(error <= 0);
-
- r = message_new(NULL, ret, NLMSG_ERROR);
- if (r < 0)
- return r;
-
- (*ret)->hdr->nlmsg_seq = serial;
-
- err = NLMSG_DATA((*ret)->hdr);
-
- err->error = error;
-
- return 0;
-}
-
-bool rtnl_message_type_is_neigh(uint16_t type) {
- switch (type) {
- case RTM_NEWNEIGH:
- case RTM_GETNEIGH:
- case RTM_DELNEIGH:
- return true;
- default:
- return false;
- }
-}
-
-bool rtnl_message_type_is_route(uint16_t type) {
- switch (type) {
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- return true;
- default:
- return false;
- }
-}
-
-bool rtnl_message_type_is_link(uint16_t type) {
- switch (type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- return true;
- default:
- return false;
- }
-}
-
-bool rtnl_message_type_is_addr(uint16_t type) {
- switch (type) {
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- return true;
- default:
- return false;
- }
-}
-
-int rtnl_log_parse_error(int r) {
- return log_error_errno(r, "Failed to parse netlink message: %m");
-}
-
-int rtnl_log_create_error(int r) {
- return log_error_errno(r, "Failed to create netlink message: %m");
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-#include "util.h"
-#include "sd-rtnl.h"
-
-int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret);
-uint32_t rtnl_message_get_serial(sd_rtnl_message *m);
-void rtnl_message_seal(sd_rtnl_message *m);
-
-bool rtnl_message_type_is_link(uint16_t type);
-bool rtnl_message_type_is_addr(uint16_t type);
-bool rtnl_message_type_is_route(uint16_t type);
-bool rtnl_message_type_is_neigh(uint16_t type);
-
-int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name);
-int rtnl_set_link_properties(sd_rtnl **rtnl, int ifindex, const char *alias, const struct ether_addr *mac, unsigned mtu);
-
-int rtnl_log_parse_error(int r);
-int rtnl_log_create_error(int r);
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_rtnl*, sd_rtnl_unref);
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_rtnl_message*, sd_rtnl_message_unref);
-
-#define _cleanup_rtnl_unref_ _cleanup_(sd_rtnl_unrefp)
-#define _cleanup_rtnl_message_unref_ _cleanup_(sd_rtnl_message_unrefp)
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/socket.h>
-#include <poll.h>
-
-#include "missing.h"
-#include "macro.h"
-#include "util.h"
-#include "hashmap.h"
-
-#include "sd-rtnl.h"
-#include "rtnl-internal.h"
-#include "rtnl-util.h"
-
-static int sd_rtnl_new(sd_rtnl **ret) {
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
-
- assert_return(ret, -EINVAL);
-
- rtnl = new0(sd_rtnl, 1);
- if (!rtnl)
- return -ENOMEM;
-
- rtnl->n_ref = REFCNT_INIT;
-
- rtnl->fd = -1;
-
- rtnl->sockaddr.nl.nl_family = AF_NETLINK;
-
- rtnl->original_pid = getpid();
-
- LIST_HEAD_INIT(rtnl->match_callbacks);
-
- /* We guarantee that wqueue always has space for at least
- * one entry */
- if (!GREEDY_REALLOC(rtnl->wqueue, rtnl->wqueue_allocated, 1))
- return -ENOMEM;
-
- /* We guarantee that the read buffer has at least space for
- * a message header */
- if (!greedy_realloc((void**)&rtnl->rbuffer, &rtnl->rbuffer_allocated,
- sizeof(struct nlmsghdr), sizeof(uint8_t)))
- return -ENOMEM;
-
- /* Change notification responses have sequence 0, so we must
- * start our request sequence numbers at 1, or we may confuse our
- * responses with notifications from the kernel */
- rtnl->serial = 1;
-
- *ret = rtnl;
- rtnl = NULL;
-
- return 0;
-}
-
-int sd_rtnl_new_from_netlink(sd_rtnl **ret, int fd) {
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- socklen_t addrlen;
- int r;
-
- assert_return(ret, -EINVAL);
-
- r = sd_rtnl_new(&rtnl);
- if (r < 0)
- return r;
-
- addrlen = sizeof(rtnl->sockaddr);
-
- r = getsockname(fd, &rtnl->sockaddr.sa, &addrlen);
- if (r < 0)
- return -errno;
-
- rtnl->fd = fd;
-
- *ret = rtnl;
- rtnl = NULL;
-
- return 0;
-}
-
-static bool rtnl_pid_changed(sd_rtnl *rtnl) {
- assert(rtnl);
-
- /* We don't support people creating an rtnl connection and
- * keeping it around over a fork(). Let's complain. */
-
- return rtnl->original_pid != getpid();
-}
-
-static int rtnl_compute_groups_ap(uint32_t *_groups, unsigned n_groups, va_list ap) {
- uint32_t groups = 0;
- unsigned i;
-
- for (i = 0; i < n_groups; i++) {
- unsigned group;
-
- group = va_arg(ap, unsigned);
- assert_return(group < 32, -EINVAL);
-
- groups |= group ? (1 << (group - 1)) : 0;
- }
-
- *_groups = groups;
-
- return 0;
-}
-
-static int rtnl_open_fd_ap(sd_rtnl **ret, int fd, unsigned n_groups, va_list ap) {
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- socklen_t addrlen;
- int r, one = 1;
-
- assert_return(ret, -EINVAL);
- assert_return(fd >= 0, -EINVAL);
-
- r = sd_rtnl_new(&rtnl);
- if (r < 0)
- return r;
-
- r = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
- if (r < 0)
- return -errno;
-
- r = setsockopt(fd, SOL_NETLINK, NETLINK_PKTINFO, &one, sizeof(one));
- if (r < 0)
- return -errno;
-
- r = rtnl_compute_groups_ap(&rtnl->sockaddr.nl.nl_groups, n_groups, ap);
- if (r < 0)
- return r;
-
- addrlen = sizeof(rtnl->sockaddr);
-
- r = bind(fd, &rtnl->sockaddr.sa, addrlen);
- /* ignore EINVAL to allow opening an already bound socket */
- if (r < 0 && errno != EINVAL)
- return -errno;
-
- r = getsockname(fd, &rtnl->sockaddr.sa, &addrlen);
- if (r < 0)
- return -errno;
-
- rtnl->fd = fd;
-
- *ret = rtnl;
- rtnl = NULL;
-
- return 0;
-}
-
-int sd_rtnl_open_fd(sd_rtnl **ret, int fd, unsigned n_groups, ...) {
- va_list ap;
- int r;
-
- va_start(ap, n_groups);
- r = rtnl_open_fd_ap(ret, fd, n_groups, ap);
- va_end(ap);
-
- return r;
-}
-
-int sd_rtnl_open(sd_rtnl **ret, unsigned n_groups, ...) {
- va_list ap;
- int fd, r;
-
- fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_ROUTE);
- if (fd < 0)
- return -errno;
-
- va_start(ap, n_groups);
- r = rtnl_open_fd_ap(ret, fd, n_groups, ap);
- va_end(ap);
-
- if (r < 0) {
- safe_close(fd);
- return r;
- }
-
- return 0;
-}
-
-int sd_rtnl_inc_rcvbuf(const sd_rtnl *const rtnl, const int size) {
- return fd_inc_rcvbuf(rtnl->fd, size);
-}
-
-sd_rtnl *sd_rtnl_ref(sd_rtnl *rtnl) {
- assert_return(rtnl, NULL);
- assert_return(!rtnl_pid_changed(rtnl), NULL);
-
- if (rtnl)
- assert_se(REFCNT_INC(rtnl->n_ref) >= 2);
-
- return rtnl;
-}
-
-sd_rtnl *sd_rtnl_unref(sd_rtnl *rtnl) {
- if (!rtnl)
- return NULL;
-
- assert_return(!rtnl_pid_changed(rtnl), NULL);
-
- if (REFCNT_DEC(rtnl->n_ref) == 0) {
- struct match_callback *f;
- unsigned i;
-
- for (i = 0; i < rtnl->rqueue_size; i++)
- sd_rtnl_message_unref(rtnl->rqueue[i]);
- free(rtnl->rqueue);
-
- for (i = 0; i < rtnl->rqueue_partial_size; i++)
- sd_rtnl_message_unref(rtnl->rqueue_partial[i]);
- free(rtnl->rqueue_partial);
-
- for (i = 0; i < rtnl->wqueue_size; i++)
- sd_rtnl_message_unref(rtnl->wqueue[i]);
- free(rtnl->wqueue);
-
- free(rtnl->rbuffer);
-
- hashmap_free_free(rtnl->reply_callbacks);
- prioq_free(rtnl->reply_callbacks_prioq);
-
- sd_event_source_unref(rtnl->io_event_source);
- sd_event_source_unref(rtnl->time_event_source);
- sd_event_source_unref(rtnl->exit_event_source);
- sd_event_unref(rtnl->event);
-
- while ((f = rtnl->match_callbacks)) {
- LIST_REMOVE(match_callbacks, rtnl->match_callbacks, f);
- free(f);
- }
-
- safe_close(rtnl->fd);
- free(rtnl);
- }
-
- return NULL;
-}
-
-static void rtnl_seal_message(sd_rtnl *rtnl, sd_rtnl_message *m) {
- assert(rtnl);
- assert(!rtnl_pid_changed(rtnl));
- assert(m);
- assert(m->hdr);
-
- /* don't use seq == 0, as that is used for broadcasts, so we
- would get confused by replies to such messages */
- m->hdr->nlmsg_seq = rtnl->serial++ ? : rtnl->serial++;
-
- rtnl_message_seal(m);
-
- return;
-}
-
-int sd_rtnl_send(sd_rtnl *nl,
- sd_rtnl_message *message,
- uint32_t *serial) {
- int r;
-
- assert_return(nl, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
- assert_return(message, -EINVAL);
- assert_return(!message->sealed, -EPERM);
-
- rtnl_seal_message(nl, message);
-
- if (nl->wqueue_size <= 0) {
- /* send directly */
- r = socket_write_message(nl, message);
- if (r < 0)
- return r;
- else if (r == 0) {
- /* nothing was sent, so let's put it on
- * the queue */
- nl->wqueue[0] = sd_rtnl_message_ref(message);
- nl->wqueue_size = 1;
- }
- } else {
- /* append to queue */
- if (nl->wqueue_size >= RTNL_WQUEUE_MAX) {
- log_debug("rtnl: exhausted the write queue size (%d)", RTNL_WQUEUE_MAX);
- return -ENOBUFS;
- }
-
- if (!GREEDY_REALLOC(nl->wqueue, nl->wqueue_allocated, nl->wqueue_size + 1))
- return -ENOMEM;
-
- nl->wqueue[nl->wqueue_size ++] = sd_rtnl_message_ref(message);
- }
-
- if (serial)
- *serial = rtnl_message_get_serial(message);
-
- return 1;
-}
-
-int rtnl_rqueue_make_room(sd_rtnl *rtnl) {
- assert(rtnl);
-
- if (rtnl->rqueue_size >= RTNL_RQUEUE_MAX) {
- log_debug("rtnl: exhausted the read queue size (%d)", RTNL_RQUEUE_MAX);
- return -ENOBUFS;
- }
-
- if (!GREEDY_REALLOC(rtnl->rqueue, rtnl->rqueue_allocated, rtnl->rqueue_size + 1))
- return -ENOMEM;
-
- return 0;
-}
-
-int rtnl_rqueue_partial_make_room(sd_rtnl *rtnl) {
- assert(rtnl);
-
- if (rtnl->rqueue_partial_size >= RTNL_RQUEUE_MAX) {
- log_debug("rtnl: exhausted the partial read queue size (%d)", RTNL_RQUEUE_MAX);
- return -ENOBUFS;
- }
-
- if (!GREEDY_REALLOC(rtnl->rqueue_partial, rtnl->rqueue_partial_allocated,
- rtnl->rqueue_partial_size + 1))
- return -ENOMEM;
-
- return 0;
-}
-
-static int dispatch_rqueue(sd_rtnl *rtnl, sd_rtnl_message **message) {
- int r;
-
- assert(rtnl);
- assert(message);
-
- if (rtnl->rqueue_size <= 0) {
- /* Try to read a new message */
- r = socket_read_message(rtnl);
- if (r <= 0)
- return r;
- }
-
- /* Dispatch a queued message */
- *message = rtnl->rqueue[0];
- rtnl->rqueue_size --;
- memmove(rtnl->rqueue, rtnl->rqueue + 1, sizeof(sd_rtnl_message*) * rtnl->rqueue_size);
-
- return 1;
-}
-
-static int dispatch_wqueue(sd_rtnl *rtnl) {
- int r, ret = 0;
-
- assert(rtnl);
-
- while (rtnl->wqueue_size > 0) {
- r = socket_write_message(rtnl, rtnl->wqueue[0]);
- if (r < 0)
- return r;
- else if (r == 0)
- /* Didn't do anything this time */
- return ret;
- else {
- /* see equivalent in sd-bus.c */
- sd_rtnl_message_unref(rtnl->wqueue[0]);
- rtnl->wqueue_size --;
- memmove(rtnl->wqueue, rtnl->wqueue + 1, sizeof(sd_rtnl_message*) * rtnl->wqueue_size);
-
- ret = 1;
- }
- }
-
- return ret;
-}
-
-static int process_timeout(sd_rtnl *rtnl) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- struct reply_callback *c;
- usec_t n;
- int r;
-
- assert(rtnl);
-
- c = prioq_peek(rtnl->reply_callbacks_prioq);
- if (!c)
- return 0;
-
- n = now(CLOCK_MONOTONIC);
- if (c->timeout > n)
- return 0;
-
- r = rtnl_message_new_synthetic_error(-ETIMEDOUT, c->serial, &m);
- if (r < 0)
- return r;
-
- assert_se(prioq_pop(rtnl->reply_callbacks_prioq) == c);
- hashmap_remove(rtnl->reply_callbacks, &c->serial);
-
- r = c->callback(rtnl, m, c->userdata);
- if (r < 0)
- log_debug_errno(r, "sd-rtnl: timedout callback failed: %m");
-
- free(c);
-
- return 1;
-}
-
-static int process_reply(sd_rtnl *rtnl, sd_rtnl_message *m) {
- _cleanup_free_ struct reply_callback *c = NULL;
- uint64_t serial;
- uint16_t type;
- int r;
-
- assert(rtnl);
- assert(m);
-
- serial = rtnl_message_get_serial(m);
- c = hashmap_remove(rtnl->reply_callbacks, &serial);
- if (!c)
- return 0;
-
- if (c->timeout != 0)
- prioq_remove(rtnl->reply_callbacks_prioq, c, &c->prioq_idx);
-
- r = sd_rtnl_message_get_type(m, &type);
- if (r < 0)
- return 0;
-
- if (type == NLMSG_DONE)
- m = NULL;
-
- r = c->callback(rtnl, m, c->userdata);
- if (r < 0)
- log_debug_errno(r, "sd-rtnl: callback failed: %m");
-
- return 1;
-}
-
-static int process_match(sd_rtnl *rtnl, sd_rtnl_message *m) {
- struct match_callback *c;
- uint16_t type;
- int r;
-
- assert(rtnl);
- assert(m);
-
- r = sd_rtnl_message_get_type(m, &type);
- if (r < 0)
- return r;
-
- LIST_FOREACH(match_callbacks, c, rtnl->match_callbacks) {
- if (type == c->type) {
- r = c->callback(rtnl, m, c->userdata);
- if (r != 0) {
- if (r < 0)
- log_debug_errno(r, "sd-rtnl: match callback failed: %m");
-
- break;
- }
- }
- }
-
- return 1;
-}
-
-static int process_running(sd_rtnl *rtnl, sd_rtnl_message **ret) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- int r;
-
- assert(rtnl);
-
- r = process_timeout(rtnl);
- if (r != 0)
- goto null_message;
-
- r = dispatch_wqueue(rtnl);
- if (r != 0)
- goto null_message;
-
- r = dispatch_rqueue(rtnl, &m);
- if (r < 0)
- return r;
- if (!m)
- goto null_message;
-
- if (sd_rtnl_message_is_broadcast(m)) {
- r = process_match(rtnl, m);
- if (r != 0)
- goto null_message;
- } else {
- r = process_reply(rtnl, m);
- if (r != 0)
- goto null_message;
- }
-
- if (ret) {
- *ret = m;
- m = NULL;
-
- return 1;
- }
-
- return 1;
-
-null_message:
- if (r >= 0 && ret)
- *ret = NULL;
-
- return r;
-}
-
-int sd_rtnl_process(sd_rtnl *rtnl, sd_rtnl_message **ret) {
- RTNL_DONT_DESTROY(rtnl);
- int r;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
- assert_return(!rtnl->processing, -EBUSY);
-
- rtnl->processing = true;
- r = process_running(rtnl, ret);
- rtnl->processing = false;
-
- return r;
-}
-
-static usec_t calc_elapse(uint64_t usec) {
- if (usec == (uint64_t) -1)
- return 0;
-
- if (usec == 0)
- usec = RTNL_DEFAULT_TIMEOUT;
-
- return now(CLOCK_MONOTONIC) + usec;
-}
-
-static int rtnl_poll(sd_rtnl *rtnl, bool need_more, uint64_t timeout_usec) {
- struct pollfd p[1] = {};
- struct timespec ts;
- usec_t m = USEC_INFINITY;
- int r, e;
-
- assert(rtnl);
-
- e = sd_rtnl_get_events(rtnl);
- if (e < 0)
- return e;
-
- if (need_more)
- /* Caller wants more data, and doesn't care about
- * what's been read or any other timeouts. */
- e |= POLLIN;
- else {
- usec_t until;
- /* Caller wants to process if there is something to
- * process, but doesn't care otherwise */
-
- r = sd_rtnl_get_timeout(rtnl, &until);
- if (r < 0)
- return r;
- if (r > 0) {
- usec_t nw;
- nw = now(CLOCK_MONOTONIC);
- m = until > nw ? until - nw : 0;
- }
- }
-
- if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
- m = timeout_usec;
-
- p[0].fd = rtnl->fd;
- p[0].events = e;
-
- r = ppoll(p, 1, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
- if (r < 0)
- return -errno;
-
- return r > 0 ? 1 : 0;
-}
-
-int sd_rtnl_wait(sd_rtnl *nl, uint64_t timeout_usec) {
- assert_return(nl, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
-
- if (nl->rqueue_size > 0)
- return 0;
-
- return rtnl_poll(nl, false, timeout_usec);
-}
-
-static int timeout_compare(const void *a, const void *b) {
- const struct reply_callback *x = a, *y = b;
-
- if (x->timeout != 0 && y->timeout == 0)
- return -1;
-
- if (x->timeout == 0 && y->timeout != 0)
- return 1;
-
- if (x->timeout < y->timeout)
- return -1;
-
- if (x->timeout > y->timeout)
- return 1;
-
- return 0;
-}
-
-int sd_rtnl_call_async(sd_rtnl *nl,
- sd_rtnl_message *m,
- sd_rtnl_message_handler_t callback,
- void *userdata,
- uint64_t usec,
- uint32_t *serial) {
- struct reply_callback *c;
- uint32_t s;
- int r, k;
-
- assert_return(nl, -EINVAL);
- assert_return(m, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
-
- r = hashmap_ensure_allocated(&nl->reply_callbacks, &uint64_hash_ops);
- if (r < 0)
- return r;
-
- if (usec != (uint64_t) -1) {
- r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare);
- if (r < 0)
- return r;
- }
-
- c = new0(struct reply_callback, 1);
- if (!c)
- return -ENOMEM;
-
- c->callback = callback;
- c->userdata = userdata;
- c->timeout = calc_elapse(usec);
-
- k = sd_rtnl_send(nl, m, &s);
- if (k < 0) {
- free(c);
- return k;
- }
-
- c->serial = s;
-
- r = hashmap_put(nl->reply_callbacks, &c->serial, c);
- if (r < 0) {
- free(c);
- return r;
- }
-
- if (c->timeout != 0) {
- r = prioq_put(nl->reply_callbacks_prioq, c, &c->prioq_idx);
- if (r > 0) {
- c->timeout = 0;
- sd_rtnl_call_async_cancel(nl, c->serial);
- return r;
- }
- }
-
- if (serial)
- *serial = s;
-
- return k;
-}
-
-int sd_rtnl_call_async_cancel(sd_rtnl *nl, uint32_t serial) {
- struct reply_callback *c;
- uint64_t s = serial;
-
- assert_return(nl, -EINVAL);
- assert_return(serial != 0, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
-
- c = hashmap_remove(nl->reply_callbacks, &s);
- if (!c)
- return 0;
-
- if (c->timeout != 0)
- prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx);
-
- free(c);
- return 1;
-}
-
-int sd_rtnl_call(sd_rtnl *rtnl,
- sd_rtnl_message *message,
- uint64_t usec,
- sd_rtnl_message **ret) {
- usec_t timeout;
- uint32_t serial;
- int r;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
- assert_return(message, -EINVAL);
-
- r = sd_rtnl_send(rtnl, message, &serial);
- if (r < 0)
- return r;
-
- timeout = calc_elapse(usec);
-
- for (;;) {
- usec_t left;
- unsigned i;
-
- for (i = 0; i < rtnl->rqueue_size; i++) {
- uint32_t received_serial;
-
- received_serial = rtnl_message_get_serial(rtnl->rqueue[i]);
-
- if (received_serial == serial) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *incoming = NULL;
- uint16_t type;
-
- incoming = rtnl->rqueue[i];
-
- /* found a match, remove from rqueue and return it */
- memmove(rtnl->rqueue + i,rtnl->rqueue + i + 1,
- sizeof(sd_rtnl_message*) * (rtnl->rqueue_size - i - 1));
- rtnl->rqueue_size--;
-
- r = sd_rtnl_message_get_errno(incoming);
- if (r < 0)
- return r;
-
- r = sd_rtnl_message_get_type(incoming, &type);
- if (r < 0)
- return r;
-
- if (type == NLMSG_DONE) {
- *ret = NULL;
- return 0;
- }
-
- if (ret) {
- *ret = incoming;
- incoming = NULL;
- }
-
- return 1;
- }
- }
-
- r = socket_read_message(rtnl);
- if (r < 0)
- return r;
- if (r > 0)
- /* received message, so try to process straight away */
- continue;
-
- if (timeout > 0) {
- usec_t n;
-
- n = now(CLOCK_MONOTONIC);
- if (n >= timeout)
- return -ETIMEDOUT;
-
- left = timeout - n;
- } else
- left = (uint64_t) -1;
-
- r = rtnl_poll(rtnl, true, left);
- if (r < 0)
- return r;
- else if (r == 0)
- return -ETIMEDOUT;
-
- r = dispatch_wqueue(rtnl);
- if (r < 0)
- return r;
- }
-}
-
-int sd_rtnl_flush(sd_rtnl *rtnl) {
- int r;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- if (rtnl->wqueue_size <= 0)
- return 0;
-
- for (;;) {
- r = dispatch_wqueue(rtnl);
- if (r < 0)
- return r;
-
- if (rtnl->wqueue_size <= 0)
- return 0;
-
- r = rtnl_poll(rtnl, false, (uint64_t) -1);
- if (r < 0)
- return r;
- }
-}
-
-int sd_rtnl_get_events(sd_rtnl *rtnl) {
- int flags = 0;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- if (rtnl->rqueue_size <= 0)
- flags |= POLLIN;
- if (rtnl->wqueue_size > 0)
- flags |= POLLOUT;
-
- return flags;
-}
-
-int sd_rtnl_get_timeout(sd_rtnl *rtnl, uint64_t *timeout_usec) {
- struct reply_callback *c;
-
- assert_return(rtnl, -EINVAL);
- assert_return(timeout_usec, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- if (rtnl->rqueue_size > 0) {
- *timeout_usec = 0;
- return 1;
- }
-
- c = prioq_peek(rtnl->reply_callbacks_prioq);
- if (!c) {
- *timeout_usec = (uint64_t) -1;
- return 0;
- }
-
- *timeout_usec = c->timeout;
-
- return 1;
-}
-
-static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- sd_rtnl *rtnl = userdata;
- int r;
-
- assert(rtnl);
-
- r = sd_rtnl_process(rtnl, NULL);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
- sd_rtnl *rtnl = userdata;
- int r;
-
- assert(rtnl);
-
- r = sd_rtnl_process(rtnl, NULL);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-static int prepare_callback(sd_event_source *s, void *userdata) {
- sd_rtnl *rtnl = userdata;
- int r, e;
- usec_t until;
-
- assert(s);
- assert(rtnl);
-
- e = sd_rtnl_get_events(rtnl);
- if (e < 0)
- return e;
-
- r = sd_event_source_set_io_events(rtnl->io_event_source, e);
- if (r < 0)
- return r;
-
- r = sd_rtnl_get_timeout(rtnl, &until);
- if (r < 0)
- return r;
- if (r > 0) {
- int j;
-
- j = sd_event_source_set_time(rtnl->time_event_source, until);
- if (j < 0)
- return j;
- }
-
- r = sd_event_source_set_enabled(rtnl->time_event_source, r > 0);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-static int exit_callback(sd_event_source *event, void *userdata) {
- sd_rtnl *rtnl = userdata;
-
- assert(event);
-
- sd_rtnl_flush(rtnl);
-
- return 1;
-}
-
-int sd_rtnl_attach_event(sd_rtnl *rtnl, sd_event *event, int priority) {
- int r;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl->event, -EBUSY);
-
- assert(!rtnl->io_event_source);
- assert(!rtnl->time_event_source);
-
- if (event)
- rtnl->event = sd_event_ref(event);
- else {
- r = sd_event_default(&rtnl->event);
- if (r < 0)
- return r;
- }
-
- r = sd_event_add_io(rtnl->event, &rtnl->io_event_source, rtnl->fd, 0, io_callback, rtnl);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_priority(rtnl->io_event_source, priority);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_description(rtnl->io_event_source, "rtnl-receive-message");
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_prepare(rtnl->io_event_source, prepare_callback);
- if (r < 0)
- goto fail;
-
- r = sd_event_add_time(rtnl->event, &rtnl->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, rtnl);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_priority(rtnl->time_event_source, priority);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_description(rtnl->time_event_source, "rtnl-timer");
- if (r < 0)
- goto fail;
-
- r = sd_event_add_exit(rtnl->event, &rtnl->exit_event_source, exit_callback, rtnl);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_description(rtnl->exit_event_source, "rtnl-exit");
- if (r < 0)
- goto fail;
-
- return 0;
-
-fail:
- sd_rtnl_detach_event(rtnl);
- return r;
-}
-
-int sd_rtnl_detach_event(sd_rtnl *rtnl) {
- assert_return(rtnl, -EINVAL);
- assert_return(rtnl->event, -ENXIO);
-
- if (rtnl->io_event_source)
- rtnl->io_event_source = sd_event_source_unref(rtnl->io_event_source);
-
- if (rtnl->time_event_source)
- rtnl->time_event_source = sd_event_source_unref(rtnl->time_event_source);
-
- if (rtnl->exit_event_source)
- rtnl->exit_event_source = sd_event_source_unref(rtnl->exit_event_source);
-
- if (rtnl->event)
- rtnl->event = sd_event_unref(rtnl->event);
-
- return 0;
-}
-
-int sd_rtnl_add_match(sd_rtnl *rtnl,
- uint16_t type,
- sd_rtnl_message_handler_t callback,
- void *userdata) {
- struct match_callback *c;
-
- assert_return(rtnl, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
- assert_return(rtnl_message_type_is_link(type) ||
- rtnl_message_type_is_addr(type) ||
- rtnl_message_type_is_route(type), -EOPNOTSUPP);
-
- c = new0(struct match_callback, 1);
- if (!c)
- return -ENOMEM;
-
- c->callback = callback;
- c->type = type;
- c->userdata = userdata;
-
- LIST_PREPEND(match_callbacks, rtnl->match_callbacks, c);
-
- return 0;
-}
-
-int sd_rtnl_remove_match(sd_rtnl *rtnl,
- uint16_t type,
- sd_rtnl_message_handler_t callback,
- void *userdata) {
- struct match_callback *c;
-
- assert_return(rtnl, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- LIST_FOREACH(match_callbacks, c, rtnl->match_callbacks)
- if (c->callback == callback && c->type == type && c->userdata == userdata) {
- LIST_REMOVE(match_callbacks, rtnl->match_callbacks, c);
- free(c);
-
- return 1;
- }
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "in-addr-util.h"
-#include "local-addresses.h"
-#include "af-list.h"
-
-static void print_local_addresses(struct local_address *a, unsigned n) {
- unsigned i;
-
- for (i = 0; i < n; i++) {
- _cleanup_free_ char *b = NULL;
-
- assert_se(in_addr_to_string(a[i].family, &a[i].address, &b) >= 0);
- printf("%s if%i scope=%i metric=%u address=%s\n", af_to_name(a[i].family), a[i].ifindex, a[i].scope, a[i].metric, b);
- }
-}
-
-int main(int argc, char *argv[]) {
- struct local_address *a;
- int n;
-
- a = NULL;
- n = local_addresses(NULL, 0, AF_UNSPEC, &a);
- assert_se(n >= 0);
-
- printf("Local Addresses:\n");
- print_local_addresses(a, (unsigned) n);
- free(a);
-
- a = NULL;
- n = local_gateways(NULL, 0, AF_UNSPEC, &a);
- assert_se(n >= 0);
-
- printf("Local Gateways:\n");
- print_local_addresses(a, (unsigned) n);
- free(a);
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <netinet/ether.h>
-#include <net/if.h>
-
-#include "util.h"
-#include "macro.h"
-#include "sd-rtnl.h"
-#include "socket-util.h"
-#include "rtnl-util.h"
-#include "event-util.h"
-#include "missing.h"
-
-static void test_message_link_bridge(sd_rtnl *rtnl) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
- uint32_t cost;
-
- assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_NEWLINK, 1) >= 0);
- assert_se(sd_rtnl_message_link_set_family(message, PF_BRIDGE) >= 0);
- assert_se(sd_rtnl_message_open_container(message, IFLA_PROTINFO) >= 0);
- assert_se(sd_rtnl_message_append_u32(message, IFLA_BRPORT_COST, 10) >= 0);
- assert_se(sd_rtnl_message_close_container(message) >= 0);
-
- assert_se(sd_rtnl_message_rewind(message) >= 0);
-
- assert_se(sd_rtnl_message_enter_container(message, IFLA_PROTINFO) >= 0);
- assert_se(sd_rtnl_message_read_u32(message, IFLA_BRPORT_COST, &cost) >= 0);
- assert_se(cost == 10);
- assert_se(sd_rtnl_message_exit_container(message) >= 0);
-}
-
-static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
- const char *mac = "98:fe:94:3f:c6:18", *name = "test";
- char buffer[ETHER_ADDR_TO_STRING_MAX];
- unsigned int mtu = 1450, mtu_out;
- const char *name_out;
- struct ether_addr mac_out;
-
- /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
- assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
- assert_se(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
- assert_se(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
- assert_se(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);
-
- assert_se(sd_rtnl_call(rtnl, message, 0, NULL) == 1);
- assert_se(sd_rtnl_message_rewind(message) >= 0);
-
- assert_se(sd_rtnl_message_read_string(message, IFLA_IFNAME, &name_out) >= 0);
- assert_se(streq(name, name_out));
-
- assert_se(sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &mac_out) >= 0);
- assert_se(streq(mac, ether_addr_to_string(&mac_out, buffer)));
-
- assert_se(sd_rtnl_message_read_u32(message, IFLA_MTU, &mtu_out) >= 0);
- assert_se(mtu == mtu_out);
-}
-
-static void test_link_get(sd_rtnl *rtnl, int ifindex) {
- sd_rtnl_message *m;
- sd_rtnl_message *r;
- unsigned int mtu = 1500;
- const char *str_data;
- uint8_t u8_data;
- uint32_t u32_data;
- struct ether_addr eth_data;
-
- assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
- assert_se(m);
-
- /* u8 test cases */
- assert_se(sd_rtnl_message_append_u8(m, IFLA_CARRIER, 0) >= 0);
- assert_se(sd_rtnl_message_append_u8(m, IFLA_OPERSTATE, 0) >= 0);
- assert_se(sd_rtnl_message_append_u8(m, IFLA_LINKMODE, 0) >= 0);
-
- /* u32 test cases */
- assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, mtu) >= 0);
- assert_se(sd_rtnl_message_append_u32(m, IFLA_GROUP, 0) >= 0);
- assert_se(sd_rtnl_message_append_u32(m, IFLA_TXQLEN, 0) >= 0);
- assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_TX_QUEUES, 0) >= 0);
- assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_RX_QUEUES, 0) >= 0);
-
- assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);
-
- assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0);
-
- assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
- assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
- assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);
-
- assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
- assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
- assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
- assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
- assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
-
- assert_se(sd_rtnl_message_read_ether_addr(r, IFLA_ADDRESS, ð_data) == 0);
-
- assert_se(sd_rtnl_flush(rtnl) >= 0);
- assert_se((m = sd_rtnl_message_unref(m)) == NULL);
- assert_se((r = sd_rtnl_message_unref(r)) == NULL);
-}
-
-
-static void test_address_get(sd_rtnl *rtnl, int ifindex) {
- sd_rtnl_message *m;
- sd_rtnl_message *r;
- struct in_addr in_data;
- struct ifa_cacheinfo cache;
- const char *label;
-
- assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
- assert_se(m);
-
- assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);
-
- assert_se(sd_rtnl_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
- assert_se(sd_rtnl_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0);
- assert_se(sd_rtnl_message_read_string(r, IFA_LABEL, &label) == 0);
- assert_se(sd_rtnl_message_read_cache_info(r, IFA_CACHEINFO, &cache) == 0);
-
- assert_se(sd_rtnl_flush(rtnl) >= 0);
- assert_se((m = sd_rtnl_message_unref(m)) == NULL);
- assert_se((r = sd_rtnl_message_unref(r)) == NULL);
-
-}
-
-static void test_route(void) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *req;
- struct in_addr addr, addr_data;
- uint32_t index = 2, u32_data;
- int r;
-
- r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET, RTPROT_STATIC);
- if (r < 0) {
- log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
- return;
- }
-
- addr.s_addr = htonl(INADDR_LOOPBACK);
-
- r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &addr);
- if (r < 0) {
- log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
- return;
- }
-
- r = sd_rtnl_message_append_u32(req, RTA_OIF, index);
- if (r < 0) {
- log_error_errno(r, "Could not append RTA_OIF attribute: %m");
- return;
- }
-
- assert_se(sd_rtnl_message_rewind(req) >= 0);
-
- assert_se(sd_rtnl_message_read_in_addr(req, RTA_GATEWAY, &addr_data) >= 0);
- assert_se(addr_data.s_addr == addr.s_addr);
-
- assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) >= 0);
- assert_se(u32_data == index);
-
- assert_se((req = sd_rtnl_message_unref(req)) == NULL);
-}
-
-static void test_multiple(void) {
- sd_rtnl *rtnl1, *rtnl2;
-
- assert_se(sd_rtnl_open(&rtnl1, 0) >= 0);
- assert_se(sd_rtnl_open(&rtnl2, 0) >= 0);
-
- rtnl1 = sd_rtnl_unref(rtnl1);
- rtnl2 = sd_rtnl_unref(rtnl2);
-}
-
-static int link_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
- char *ifname = userdata;
- const char *data;
-
- assert_se(rtnl);
- assert_se(m);
-
- log_info("got link info about %s", ifname);
- free(ifname);
-
- assert_se(sd_rtnl_message_read_string(m, IFLA_IFNAME, &data) >= 0);
- assert_se(streq(data, "lo"));
-
- return 1;
-}
-
-static void test_event_loop(int ifindex) {
- _cleanup_event_unref_ sd_event *event = NULL;
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- char *ifname;
-
- ifname = strdup("lo2");
- assert_se(ifname);
-
- assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
- assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
-
- assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);
-
- assert_se(sd_event_default(&event) >= 0);
-
- assert_se(sd_rtnl_attach_event(rtnl, event, 0) >= 0);
-
- assert_se(sd_event_run(event, 0) >= 0);
-
- assert_se(sd_rtnl_detach_event(rtnl) >= 0);
-
- assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
-}
-
-static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
- int *counter = userdata;
- int r;
-
- (*counter) --;
-
- r = sd_rtnl_message_get_errno(m);
-
- log_info_errno(r, "%d left in pipe. got reply: %m", *counter);
-
- assert_se(r >= 0);
-
- return 1;
-}
-
-static void test_async(int ifindex) {
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL, *r = NULL;
- uint32_t serial;
- char *ifname;
-
- ifname = strdup("lo");
- assert_se(ifname);
-
- assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
-
- assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
-
- assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);
-
- assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
- assert_se(sd_rtnl_process(rtnl, &r) >= 0);
-
- assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
-}
-
-static void test_pipe(int ifindex) {
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m1 = NULL, *m2 = NULL;
- int counter = 0;
-
- assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
-
- assert_se(sd_rtnl_message_new_link(rtnl, &m1, RTM_GETLINK, ifindex) >= 0);
- assert_se(sd_rtnl_message_new_link(rtnl, &m2, RTM_GETLINK, ifindex) >= 0);
-
- counter ++;
- assert_se(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);
-
- counter ++;
- assert_se(sd_rtnl_call_async(rtnl, m2, &pipe_handler, &counter, 0, NULL) >= 0);
-
- while (counter > 0) {
- assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
- assert_se(sd_rtnl_process(rtnl, NULL) >= 0);
- }
-
- assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
-}
-
-static void test_container(void) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- uint16_t u16_data;
- uint32_t u32_data;
- const char *string_data;
-
- assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);
-
- assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
- assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "vlan") >= 0);
- assert_se(sd_rtnl_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
- assert_se(sd_rtnl_message_close_container(m) >= 0);
- assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "vlan") >= 0);
- assert_se(sd_rtnl_message_close_container(m) >= 0);
- assert_se(sd_rtnl_message_close_container(m) == -EINVAL);
-
- assert_se(sd_rtnl_message_rewind(m) >= 0);
-
- assert_se(sd_rtnl_message_enter_container(m, IFLA_LINKINFO) >= 0);
- assert_se(sd_rtnl_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
- assert_se(streq("vlan", string_data));
-
- assert_se(sd_rtnl_message_enter_container(m, IFLA_INFO_DATA) >= 0);
- assert_se(sd_rtnl_message_read_u16(m, IFLA_VLAN_ID, &u16_data) >= 0);
- assert_se(sd_rtnl_message_exit_container(m) >= 0);
-
- assert_se(sd_rtnl_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
- assert_se(streq("vlan", string_data));
- assert_se(sd_rtnl_message_exit_container(m) >= 0);
-
- assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) < 0);
-
- assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
-}
-
-static void test_match(void) {
- _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
-
- assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
-
- assert_se(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
- assert_se(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
-
- assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
- assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
- assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 0);
-
- assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
-}
-
-static void test_get_addresses(sd_rtnl *rtnl) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
- sd_rtnl_message *m;
-
- assert_se(sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC) >= 0);
-
- assert_se(sd_rtnl_call(rtnl, req, 0, &reply) >= 0);
-
- for (m = reply; m; m = sd_rtnl_message_next(m)) {
- uint16_t type;
- unsigned char scope, flags;
- int family, ifindex;
-
- assert_se(sd_rtnl_message_get_type(m, &type) >= 0);
- assert_se(type == RTM_NEWADDR);
-
- assert_se(sd_rtnl_message_addr_get_ifindex(m, &ifindex) >= 0);
- assert_se(sd_rtnl_message_addr_get_family(m, &family) >= 0);
- assert_se(sd_rtnl_message_addr_get_scope(m, &scope) >= 0);
- assert_se(sd_rtnl_message_addr_get_flags(m, &flags) >= 0);
-
- assert_se(ifindex > 0);
- assert_se(family == AF_INET || family == AF_INET6);
-
- log_info("got IPv%u address on ifindex %i", family == AF_INET ? 4: 6, ifindex);
- }
-}
-
-static void test_message(void) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
-
- assert_se(rtnl_message_new_synthetic_error(-ETIMEDOUT, 1, &m) >= 0);
- assert_se(sd_rtnl_message_get_errno(m) == -ETIMEDOUT);
-}
-
-int main(void) {
- sd_rtnl *rtnl;
- sd_rtnl_message *m;
- sd_rtnl_message *r;
- const char *string_data;
- int if_loopback;
- uint16_t type;
-
- test_message();
-
- test_match();
-
- test_multiple();
-
- test_route();
-
- test_container();
-
- assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
- assert_se(rtnl);
-
- if_loopback = (int) if_nametoindex("lo");
- assert_se(if_loopback > 0);
-
- test_async(if_loopback);
-
- test_pipe(if_loopback);
-
- test_event_loop(if_loopback);
-
- test_link_configure(rtnl, if_loopback);
-
- test_get_addresses(rtnl);
-
- test_message_link_bridge(rtnl);
-
- assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, if_loopback) >= 0);
- assert_se(m);
-
- assert_se(sd_rtnl_message_get_type(m, &type) >= 0);
- assert_se(type == RTM_GETLINK);
-
- assert_se(sd_rtnl_message_read_string(m, IFLA_IFNAME, &string_data) == -EPERM);
-
- assert_se(sd_rtnl_call(rtnl, m, 0, &r) == 1);
- assert_se(sd_rtnl_message_get_type(r, &type) >= 0);
- assert_se(type == RTM_NEWLINK);
-
- assert_se((r = sd_rtnl_message_unref(r)) == NULL);
-
- assert_se(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM);
- assert_se((m = sd_rtnl_message_unref(m)) == NULL);
- assert_se((r = sd_rtnl_message_unref(r)) == NULL);
-
- test_link_get(rtnl, if_loopback);
- test_address_get(rtnl, if_loopback);
-
- assert_se(sd_rtnl_flush(rtnl) >= 0);
- assert_se((m = sd_rtnl_message_unref(m)) == NULL);
- assert_se((r = sd_rtnl_message_unref(r)) == NULL);
- assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
-
- return EXIT_SUCCESS;
-}
+++ /dev/null
-../../Makefile
\ No newline at end of file
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-#include "utf8.h"
-#include "sd-utf8.h"
-
-_public_ const char *sd_utf8_is_valid(const char *s) {
- assert_return(s, NULL);
-
- return utf8_is_valid(s);
-}
-
-_public_ const char *sd_ascii_is_valid(const char *s) {
- assert_return(s, NULL);
-
- return ascii_is_valid(s);
-}
SUBSYSTEM=="input", KERNEL=="event*", KERNELS=="thinkpad_acpi", TAG+="power-switch"
SUBSYSTEM=="input", KERNEL=="event*", ATTRS{name}=="twl4030_pwrbutton", TAG+="power-switch"
SUBSYSTEM=="input", KERNEL=="event*", ATTRS{name}=="tps65217_pwr_but", TAG+="power-switch"
+SUBSYSTEM=="input", KERNEL=="event*", ATTRS{name}=="* WMI hotkeys", TAG+="power-switch"
+SUBSYSTEM=="input", KERNEL=="event*", \
+ SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", ATTRS{keys}=="116", TAG+="power-switch"
LABEL="power_switch_end"
SUBSYSTEM=="sound", KERNEL=="card*", TAG+="seat"
SUBSYSTEM=="input", KERNEL=="input*", TAG+="seat"
SUBSYSTEM=="graphics", KERNEL=="fb[0-9]*", TAG+="seat", TAG+="master-of-seat"
+SUBSYSTEM=="drm", KERNEL=="card[0-9]*", TAG+="seat", TAG+="master-of-seat"
SUBSYSTEM=="usb", ATTR{bDeviceClass}=="09", TAG+="seat"
# 'Plugable' USB hub, sound, network, graphics adapter
SUBSYSTEM=="usb", ATTR{idVendor}=="2230", ATTR{idProduct}=="000[13]", ENV{ID_AUTOSEAT}="1"
+# qemu (version 2.4+) has a PCI-PCI bridge (-device pci-bridge-seat) to group
+# devices belonging to one seat. See:
+# http://git.qemu.org/?p=qemu.git;a=blob;f=docs/multiseat.txt
+SUBSYSTEM=="pci", ATTR{vendor}=="0x1b36", ATTR{device}=="0x000a", TAG+="seat", ENV{ID_AUTOSEAT}="1"
+
# Mimo 720, with integrated USB hub, displaylink graphics, and e2i
# touchscreen. This device carries no proper VID/PID in the USB hub,
# but it does carry good ID data in the graphics component, hence we
SUBSYSTEM=="usb", ATTR{idVendor}=="17e9", ATTR{idProduct}=="401a", ATTR{product}=="mimo inc", \
ATTR{../idVendor}=="058f", ATTR{../idProduct}=="6254", \
ENV{ID_AVOID_LOOP}=="", \
- RUN+="@udevbindir@/udevadm trigger --parent-match=%p/.."
+ RUN+="@rootbindir@/udevadm trigger --parent-match=%p/.."
TAG=="seat", ENV{ID_PATH}=="", IMPORT{builtin}="path_id"
TAG=="seat", ENV{ID_FOR_SEAT}=="", ENV{ID_PATH_TAG}!="", ENV{ID_FOR_SEAT}="$env{SUBSYSTEM}-$env{ID_PATH_TAG}"
-SUBSYSTEM=="input", ATTR{name}=="Wiebetech LLC Wiebetech", RUN+="@bindir@/loginctl lock-sessions"
+SUBSYSTEM=="input", ATTR{name}=="Wiebetech LLC Wiebetech", RUN+="@rootbindir@/loginctl lock-sessions"
LABEL="seat_end"
#include "util.h"
#include "build.h"
#include "strv.h"
+#include "formats-util.h"
+#include "process-util.h"
+#include "signal-util.h"
static const char* arg_what = "idle:sleep:shutdown";
static const char* arg_who = NULL;
int main(int argc, char *argv[]) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+ _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
int r;
+ elogind_set_program_name(argv[0]);
log_parse_environment();
log_open();
if (pid == 0) {
/* Child */
+ (void) reset_all_signal_handlers();
+ (void) reset_signal_mask();
+
close_all_fds(NULL, 0);
execvp(argv[optind], argv + optind);
#include "strv.h"
#include "unit-name.h"
#include "sysfs-show.h"
-#include "logs-show.h"
+// #include "logs-show.h"
#include "cgroup-show.h"
#include "cgroup-util.h"
#include "spawn-polkit-agent.h"
#include "verbs.h"
+#include "process-util.h"
+#include "terminal-util.h"
+#include "signal-util.h"
+#include "logind-action.h"
static char **arg_property = NULL;
static bool arg_all = false;
static char *arg_host = NULL;
static bool arg_ask_password = true;
static bool arg_ignore_inhibitors = false;
+#if 0
static unsigned arg_lines = 10;
static OutputMode arg_output = OUTPUT_SHORT;
+#endif // 0
+static enum action {
+ _ACTION_INVALID,
+ ACTION_POWEROFF,
+ ACTION_REBOOT,
+ ACTION_SUSPEND,
+ ACTION_HIBERNATE,
+ ACTION_HYBRID_SLEEP,
+ ACTION_CANCEL_SHUTDOWN,
+ _ACTION_MAX
+} arg_action;
+
static void pager_open_if_enabled(void) {
polkit_agent_open();
}
+/// UNNEEDED by elogind
+#if 0
static OutputFlags get_output_flags(void) {
return
(!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
on_tty() * OUTPUT_COLOR;
}
+#endif // 0
static int list_sessions(int argc, char *argv[], void *userdata) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
if (isempty(cgroup))
return 0;
- if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup, false) != 0 && leader <= 0)
+ if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
return 0;
c = columns();
show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, get_output_flags());
return 0;
}
+#endif // 0
typedef struct SessionStatusInfo {
- const char *id;
+ char *id;
uid_t uid;
- const char *name;
+ char *name;
struct dual_timestamp timestamp;
unsigned int vtnr;
- const char *seat;
- const char *tty;
- const char *display;
+ char *seat;
+ char *tty;
+ char *display;
bool remote;
- const char *remote_host;
- const char *remote_user;
- const char *service;
+ char *remote_host;
+ char *remote_user;
+ char *service;
pid_t leader;
- const char *type;
- const char *class;
- const char *state;
- const char *scope;
- const char *desktop;
+ char *type;
+ char *class;
+ char *state;
+ char *scope;
+ char *desktop;
} SessionStatusInfo;
typedef struct UserStatusInfo {
uid_t uid;
- const char *name;
+ char *name;
struct dual_timestamp timestamp;
- const char *state;
+ char *state;
char **sessions;
- const char *display;
- const char *slice;
+ char *display;
+ char *slice;
} UserStatusInfo;
typedef struct SeatStatusInfo {
- const char *id;
- const char *active_session;
+ char *id;
+ char *active_session;
char **sessions;
} SeatStatusInfo;
+static void session_status_info_clear(SessionStatusInfo *info) {
+ if (info) {
+ free(info->id);
+ free(info->name);
+ free(info->seat);
+ free(info->tty);
+ free(info->display);
+ free(info->remote_host);
+ free(info->remote_user);
+ free(info->service);
+ free(info->type);
+ free(info->class);
+ free(info->state);
+ free(info->scope);
+ free(info->desktop);
+ zero(*info);
+ }
+}
+
+static void user_status_info_clear(UserStatusInfo *info) {
+ if (info) {
+ free(info->name);
+ free(info->state);
+ strv_free(info->sessions);
+ free(info->display);
+ free(info->slice);
+ zero(*info);
+ }
+}
+
+static void seat_status_info_clear(SeatStatusInfo *info) {
+ if (info) {
+ free(info->id);
+ free(info->active_session);
+ strv_free(info->sessions);
+ zero(*info);
+ }
+}
+
static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
const char *contents;
int r;
if (r < 0)
return r;
- free(*p);
- *p = strdup(s);
-
- if (!*p)
- return -ENOMEM;
+ r = free_and_strdup(p, s);
+ if (r < 0)
+ return r;
} else {
r = sd_bus_message_read_basic(m, contents[0], userdata);
if (r < 0)
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
- SessionStatusInfo i = {};
+ _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
int r;
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
if (i.scope) {
printf("\t Unit: %s\n", i.scope);
- show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
#if 0
+ show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
if (arg_transport == BUS_TRANSPORT_LOCAL) {
show_journal_by_unit(
true,
NULL);
}
-#endif
+
+#endif // 0
}
return 0;
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
- UserStatusInfo i = {};
+ _cleanup_(user_status_info_clear) UserStatusInfo i = {};
int r;
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
- if (r < 0) {
- log_error_errno(r, "Could not get properties: %m");
- goto finish;
- }
+ if (r < 0)
+ return log_error_errno(r, "Could not get properties: %m");
if (*new_line)
printf("\n");
if (i.slice) {
printf("\t Unit: %s\n", i.slice);
- show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
#if 0
+ show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
show_journal_by_unit(
stdout,
i.slice,
SD_JOURNAL_LOCAL_ONLY,
true,
NULL);
-#endif
- }
-finish:
- strv_free(i.sessions);
+#endif // 0
+ }
- return r;
+ return 0;
}
static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
{}
};
- SeatStatusInfo i = {};
+ _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
int r;
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
- if (r < 0) {
- log_error_errno(r, "Could not get properties: %m");
- goto finish;
- }
+ if (r < 0)
+ return log_error_errno(r, "Could not get properties: %m");
if (*new_line)
printf("\n");
show_sysfs(i.id, "\t\t ", c);
}
-finish:
- strv_free(i.sessions);
-
- return r;
+ return 0;
}
static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
return 0;
}
-static int check_inhibitors(sd_bus *bus, const char *verb, const char *inhibit_what) {
+/* Ask elogind, which might grant access to unprivileged users
+ * through PolicyKit */
+static int reboot_with_logind(sd_bus *bus, enum action a) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ const char *method;
+ int r;
+ static const char *table[_ACTION_MAX] = {
+ [ACTION_REBOOT] = "The system is going down for reboot NOW!",
+ [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
+ [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
+ };
+
+ if (!bus)
+ return -EIO;
+
+ polkit_agent_open_if_enabled();
+
+ switch (a) {
+
+ case ACTION_POWEROFF:
+ method = "PowerOff";
+ break;
+
+ case ACTION_REBOOT:
+ method = "Reboot";
+ break;
+
+ case ACTION_SUSPEND:
+ method = "Suspend";
+ break;
+
+ case ACTION_HIBERNATE:
+ method = "Hibernate";
+ break;
+
+ case ACTION_HYBRID_SLEEP:
+ method = "HybridSleep";
+ break;
+
+ case ACTION_CANCEL_SHUTDOWN:
+ method = "CancelScheduledShutdown";
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ if (table[a]) {
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ "SetWallMessage",
+ &error,
+ NULL,
+ "sb",
+ table[a],
+ true);
+
+ if (r < 0) {
+ log_warning_errno(r, "Failed to set wall message, ignoring: %s",
+ bus_error_message(&error, r));
+ sd_bus_error_free(&error);
+ }
+ }
+
+
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ method,
+ &error,
+ NULL,
+ "b", arg_ask_password);
+ if (r < 0)
+ log_error("Failed to execute operation: %s", bus_error_message(&error, r));
+
+ return r;
+}
+
+static const struct {
+ HandleAction action;
+ const char* verb;
+} action_table[_ACTION_MAX] = {
+ [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
+ [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
+ [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
+ [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
+ [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
+};
+
+static enum action verb_to_action(const char *verb) {
+ enum action i;
+
+ for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
+ if (streq_ptr(action_table[i].verb, verb))
+ return i;
+
+ return _ACTION_INVALID;
+}
+
+static int check_inhibitors(sd_bus *bus, enum action a) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
_cleanup_strv_free_ char **sessions = NULL;
const char *what, *who, *why, *mode;
char **s;
int r;
- assert(bus);
+ if (!bus)
+ return 0;
if (arg_ignore_inhibitors)
return 0;
if (!sv)
return log_oom();
- if (!strv_contains(sv, inhibit_what))
+ if ((pid_t) pid < 0)
+ return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
+
+ if (!strv_contains(sv,
+ a == ACTION_POWEROFF ||
+ a == ACTION_REBOOT ? "shutdown" : "sleep"))
continue;
get_process_comm(pid, &comm);
user = uid_to_name(uid);
log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
- who, pid, strna(comm), strna(user), why);
+ who, (pid_t) pid, strna(comm), strna(user), why);
c++;
}
if (c <= 0)
return 0;
- log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'loginctl %s -i'.", verb);
+ log_error("Please retry operation after closing inhibitors and logging out other users.\n"
+ "Alternatively, ignore inhibitors and users with 'loginctl %s -i'.",
+ action_table[a].verb);
return -EPERM;
}
-static int poweroff(int argc, char *argv[], void *userdata) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- sd_bus *bus = userdata;
- int r;
-
- assert(bus);
-
- r = check_inhibitors(bus, "poweroff", "shutdown");
- if (r < 0)
- return r;
-
- polkit_agent_open_if_enabled();
-
- r = sd_bus_call_method(
- bus,
- "org.freedesktop.login1",
- "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- "PowerOff",
- &error,
- NULL,
- "b", arg_ask_password);
- if (r < 0)
- log_error("Failed to power off: %s", bus_error_message(&error, r));
-
- return r;
-}
-
-static int reboot(int argc, char *argv[], void *userdata) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- sd_bus *bus = userdata;
- int r;
-
- assert(bus);
-
- r = check_inhibitors(bus, "reboot", "shutdown");
- if (r < 0)
- return r;
-
- polkit_agent_open_if_enabled();
-
- r = sd_bus_call_method(
- bus,
- "org.freedesktop.login1",
- "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- "Reboot",
- &error,
- NULL,
- "b", arg_ask_password);
- if (r < 0)
- log_error("Failed to reboot: %s", bus_error_message(&error, r));
-
- return r;
-}
-
-static int suspend(int argc, char *argv[], void *userdata) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+static int start_special(int argc, char *argv[], void *userdata) {
sd_bus *bus = userdata;
+ enum action a;
int r;
- assert(bus);
-
- r = check_inhibitors(bus, "suspend", "sleep");
- if (r < 0)
- return r;
-
- polkit_agent_open_if_enabled();
-
- r = sd_bus_call_method(
- bus,
- "org.freedesktop.login1",
- "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- "Suspend",
- &error,
- NULL,
- "b", arg_ask_password);
- if (r < 0)
- log_error("Failed to suspend: %s", bus_error_message(&error, r));
-
- return r;
-}
-
-static int hibernate(int argc, char *argv[], void *userdata) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- sd_bus *bus = userdata;
- int r;
-
- assert(bus);
-
- r = check_inhibitors(bus, "hibernate", "sleep");
- if (r < 0)
- return r;
-
- polkit_agent_open_if_enabled();
-
- r = sd_bus_call_method(
- bus,
- "org.freedesktop.login1",
- "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- "Hibernate",
- &error,
- NULL,
- "b", arg_ask_password);
- if (r < 0)
- log_error("Failed to hibernate: %s", bus_error_message(&error, r));
-
- return r;
-}
-
-static int hybrid_sleep(int argc, char *argv[], void *userdata) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- sd_bus *bus = userdata;
- int r;
+ assert(argv);
- assert(bus);
+ a = verb_to_action(argv[0]);
- r = check_inhibitors(bus, "hybrid-sleep", "sleep");
+ r = check_inhibitors(bus, a);
if (r < 0)
return r;
- polkit_agent_open_if_enabled();
-
- r = sd_bus_call_method(
- bus,
- "org.freedesktop.login1",
- "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- "HybridSleep",
- &error,
- NULL,
- "b", arg_ask_password);
- if (r < 0)
- log_error("Failed to hybrid sleep: %s", bus_error_message(&error, r));
+ /* Now power off actions in chroot environments */
+ if ((a == ACTION_POWEROFF ||
+ a == ACTION_REBOOT) &&
+ (running_in_chroot() > 0) ) {
+ log_info("Running in chroot, ignoring request.");
+ return 0;
+ }
- return r;
+ /* Switch to cancel shutdown, if a shutdown action was requested,
+ and the option to cancel it was set: */
+ if ((a == ACTION_POWEROFF ||
+ a == ACTION_REBOOT) &&
+ (arg_action == ACTION_CANCEL_SHUTDOWN))
+ return reboot_with_logind(bus, arg_action);
+
+ /* Otherwise perform requested action */
+ if (a == ACTION_POWEROFF ||
+ a == ACTION_REBOOT ||
+ a == ACTION_SUSPEND ||
+ a == ACTION_HIBERNATE ||
+ a == ACTION_HYBRID_SLEEP)
+ return reboot_with_logind(bus, a);
+
+ return -EOPNOTSUPP;
}
static int help(int argc, char *argv[], void *userdata) {
" --no-pager Do not pipe output into a pager\n"
" --no-legend Do not show the headers and footers\n"
" --no-ask-password Don't prompt for password\n"
- " -i --ignore-inhibitors Ignore inhibitors when suspending or shutting down\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
" -p --property=NAME Show only properties by this name\n"
" -l --full Do not ellipsize output\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n"
+#if 0
" -n --lines=INTEGER Number of journal entries to show\n"
" -o --output=STRING Change journal output mode (short, short-monotonic,\n"
" verbose, export, json, json-pretty, json-sse, cat)\n\n"
+#endif // 0
+ " -c Cancel a pending shutdown\n"
+ " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n"
"Session Commands:\n"
" list-sessions List sessions\n"
" session-status [ID...] Show session status\n"
" show-seat [NAME...] Show properties of seats or the manager\n"
" attach NAME DEVICE... Attach one or more devices to a seat\n"
" flush-devices Flush all device associations\n"
- " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
+ " terminate-seat NAME... Terminate all sessions on one or more seats\n"
"System Commands:\n"
" poweroff Turn off the machine\n"
" reboot Reboot the machine\n"
{ "host", required_argument, NULL, 'H' },
{ "machine", required_argument, NULL, 'M' },
{ "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
- { "ignore-inhibitors", no_argument, NULL, 'i' },
+#if 0
{ "lines", required_argument, NULL, 'n' },
{ "output", required_argument, NULL, 'o' },
+#endif // 0
+ { "ignore-inhibitors", no_argument, NULL, 'i' },
{}
};
assert(argc >= 0);
assert(argv);
- while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:i", options, NULL)) >= 0)
+ while ((c = getopt_long(argc, argv, "hp:als:H:M:ci", options, NULL)) >= 0)
switch (c) {
case 'l':
arg_full = true;
break;
-
+#if 0
case 'n':
if (safe_atou(optarg, &arg_lines) < 0) {
log_error("Failed to parse lines '%s'", optarg);
break;
case 'o':
-#if 0
arg_output = output_mode_from_string(optarg);
-#else
- arg_output = -1;
-#endif
if (arg_output < 0) {
log_error("Unknown output '%s'.", optarg);
return -EINVAL;
}
break;
-
- case 'i':
- arg_ignore_inhibitors = true;
- break;
-
+#endif // 0
case ARG_NO_PAGER:
arg_no_pager = true;
break;
arg_host = optarg;
break;
+ case 'c':
+ arg_action = ACTION_CANCEL_SHUTDOWN;
+ break;
+
+ case 'i':
+ arg_ignore_inhibitors = true;
+ break;
+
case '?':
return -EINVAL;
{ "attach", 3, VERB_ANY, 0, attach },
{ "flush-devices", VERB_ANY, 1, 0, flush_devices },
{ "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
- { "poweroff", VERB_ANY, 1, 0, poweroff },
- { "reboot", VERB_ANY, 1, 0, reboot },
- { "suspend", VERB_ANY, 1, 0, suspend },
- { "hibernate", VERB_ANY, 1, 0, hibernate },
- { "hybrid-sleep", VERB_ANY, 1, 0, hybrid_sleep },
+ { "poweroff", VERB_ANY, 1, 0, start_special },
+ { "reboot", VERB_ANY, 1, 0, start_special },
+ { "suspend", VERB_ANY, 1, 0, start_special },
+ { "hibernate", VERB_ANY, 1, 0, start_special },
+ { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
+ { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
{}
};
}
int main(int argc, char *argv[]) {
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+ _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
+ elogind_set_program_name(argv[0]);
log_parse_environment();
log_open();
#include <string.h>
#include "util.h"
+#include "formats-util.h"
#include "acl-util.h"
#include "set.h"
#include "logind-acl.h"
FOREACH_DIRENT(dent, dir, return -errno) {
_cleanup_free_ char *unescaped_devname = NULL;
- unescaped_devname = cunescape(dent->d_name);
- if (!unescaped_devname)
+ if (cunescape(dent->d_name, UNESCAPE_RELAX, &unescaped_devname) < 0)
return -ENOMEM;
n = strappend("/dev/", unescaped_devname);
***/
#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
#include "sd-messages.h"
-#include "log.h"
#include "util.h"
#include "strv.h"
#include "fileio.h"
-#include "build.h"
-#include "def.h"
#include "conf-parser.h"
+// #include "special.h"
#include "sleep-config.h"
#include "bus-error.h"
#include "bus-util.h"
#include "logind-action.h"
+// #include "formats-util.h"
+#include "process-util.h"
+#include "terminal-util.h"
int manager_handle_action(
Manager *m,
[HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
};
+/// elogind does this itself. No target table required
+#if 0
+ static const char * const target_table[_HANDLE_ACTION_MAX] = {
+ [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
+ [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
+ [HANDLE_HALT] = SPECIAL_HALT_TARGET,
+ [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
+ [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
+ [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
+ [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
+ };
+#endif // 0
+
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
InhibitWhat inhibit_operation;
Inhibitor *offending = NULL;
log_info("%s", message_table[handle]);
+/// elogind uses its own variant, which can use the handle directly.
+#if 0
+ r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
+#else
r = bus_manager_shutdown_or_sleep_now_or_later(m, handle, inhibit_operation, &error);
+#endif // 0
if (r < 0) {
log_error("Failed to execute operation: %s", bus_error_message(&error, r));
return r;
STRV_FOREACH(mode, modes) {
int k;
- k = write_string_file("/sys/power/disk", *mode);
+ k = write_string_file("/sys/power/disk", *mode, 0);
if (k == 0)
return 0;
STRV_FOREACH(state, states) {
int k;
- k = write_string_stream(*f, *state);
+ k = write_string_stream(*f, *state, true);
if (k == 0)
return 0;
log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
return r;
}
-static int do_sleep(const char *arg_verb, const char **modes, const char **states) {
+static int do_sleep(const char *arg_verb, char **modes, char **states) {
char *arguments[] = {
NULL,
(char*) "pre",
(char*) arg_verb,
NULL
};
- static const char* const dirs[] = { SYSTEM_SLEEP_PATH, NULL};
+ static const char* const dirs[] = {SYSTEM_SLEEP_PATH, NULL};
+
int r;
_cleanup_fclose_ FILE *f = NULL;
}
int shutdown_or_sleep(Manager *m, HandleAction action) {
+
+ assert(m);
+
switch (action) {
case HANDLE_POWEROFF:
return run_helper(HALT);
bool ignore_inhibited,
bool is_edge);
+int shutdown_or_sleep(Manager *m, HandleAction action);
+
const char* handle_action_to_string(HandleAction h) _const_;
HandleAction handle_action_from_string(const char *s) _pure_;
/* If the device has been unplugged close() returns
* ENODEV, let's ignore this, hence we don't use
* safe_close() */
- close(b->fd);
+ (void) close(b->fd);
}
free(b->name);
assert(manager);
/* If we are docked, handle the lid switch differently */
- if (manager_is_docked_or_multiple_displays(manager))
+ if (manager_is_docked_or_external_displays(manager))
handle_action = manager->handle_lid_switch_docked;
else
handle_action = manager->handle_lid_switch;
#include "bus-error.h"
#include "udev-util.h"
#include "logind.h"
+#include "terminal-util.h"
int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
Device *d;
return 0;
}
-int manager_watch_busname(Manager *m, const char *name) {
- char *n;
- int r;
-
- assert(m);
- assert(name);
-
- if (set_get(m->busnames, (char*) name))
- return 0;
-
- n = strdup(name);
- if (!n)
- return -ENOMEM;
-
- r = set_put(m->busnames, n);
- if (r < 0) {
- free(n);
- return r;
- }
-
- return 0;
-}
-
-void manager_drop_busname(Manager *m, const char *name) {
- Session *session;
- Iterator i;
-
- assert(m);
- assert(name);
-
- /* keep it if the name still owns a controller */
- HASHMAP_FOREACH(session, m->sessions, i)
- if (session_is_controller(session, name))
- return;
-
- free(set_remove(m->busnames, (char*) name));
-}
-
int manager_process_seat_device(Manager *m, struct udev_device *d) {
Device *device;
int r;
}
int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
+/// elogind does not support systemd units, but its own session system
+#if 0
+ _cleanup_free_ char *unit = NULL;
+#else
_cleanup_free_ char *session_name = NULL;
+#endif // 0
Session *s;
int r;
assert(m);
- assert(session);
if (pid < 1)
return -EINVAL;
+/// elogind does not support systemd units, but its own session system
+#if 0
+ r = cg_pid_get_unit(pid, &unit);
+ if (r < 0)
+ return 0;
+
+ s = hashmap_get(m->session_units, unit);
+#else
+ log_debug_elogind("Searching session for PID %u", pid);
r = cg_pid_get_session(pid, &session_name);
if (r < 0)
return 0;
s = hashmap_get(m->sessions, session_name);
+ log_debug_elogind("Session Name \"%s\" -> Session \"%s\"",
+ session_name, s && s->id ? s->id : "NULL");
+#endif // 0
if (!s)
return 0;
- *session = s;
+ if (session)
+ *session = s;
return 1;
}
int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
+/// elogind does not support systemd units, but its own session system
+#if 0
+ _cleanup_free_ char *unit = NULL;
+ User *u;
+#else
Session *s;
+#endif // 0
int r;
assert(m);
assert(user);
+ if (pid < 1)
+ return -EINVAL;
+
+/// elogind does not support systemd units, but its own session system
+#if 0
+ r = cg_pid_get_slice(pid, &unit);
+ if (r < 0)
+ return 0;
+
+ u = hashmap_get(m->user_units, unit);
+ if (!u)
+ return 0;
+
+ *user = u;
+#else
r = manager_get_session_by_pid (m, pid, &s);
if (r <= 0)
return r;
*user = s->user;
+#endif // 0
+
return 1;
}
int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
Session *s;
bool idle_hint;
- dual_timestamp ts = { 0, 0 };
+ dual_timestamp ts = DUAL_TIMESTAMP_NULL;
Iterator i;
assert(m);
return strv_contains(m->kill_only_users, user);
}
+/// UNNEEDED by elogind
+#if 0
static int vt_is_busy(unsigned int vtnr) {
struct vt_stat vt_stat;
int r = 0;
return r;
}
-bool manager_is_docked(Manager *m) {
+int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
+ int r;
+
+ assert(m);
+ assert(vtnr >= 1);
+
+ if (vtnr > m->n_autovts &&
+ vtnr != m->reserve_vt)
+ return 0;
+
+ if (vtnr != m->reserve_vt) {
+ /* If this is the reserved TTY, we'll start the getty
+ * on it in any case, but otherwise only if it is not
+ * busy. */
+
+ r = vt_is_busy(vtnr);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ return -EBUSY;
+ }
+
+ snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
+ r = sd_bus_call_method(
+ m->bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartUnit",
+ &error,
+ NULL,
+ "ss", name, "fail");
+ if (r < 0)
+ log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
+
+ return r;
+}
+#endif // 0
+
+static bool manager_is_docked(Manager *m) {
Iterator i;
Button *b;
return false;
}
-int manager_count_displays(Manager *m) {
+static int manager_count_external_displays(Manager *m) {
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
int r;
udev_list_entry_foreach(item, first) {
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
struct udev_device *p;
- const char *status;
+ const char *status, *enabled, *dash, *nn, *i;
+ bool external = false;
d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
if (!d)
if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
continue;
+ nn = udev_device_get_sysname(d);
+ if (!nn)
+ continue;
+
+ /* Ignore internal displays: the type is encoded in
+ * the sysfs name, as the second dash seperated item
+ * (the first is the card name, the last the connector
+ * number). We implement a whitelist of external
+ * displays here, rather than a whitelist, to ensure
+ * we don't block suspends too eagerly. */
+ dash = strchr(nn, '-');
+ if (!dash)
+ continue;
+
+ dash++;
+ FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
+ "Composite-", "SVIDEO-", "Component-",
+ "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
+
+ if (startswith(dash, i)) {
+ external = true;
+ break;
+ }
+ }
+ if (!external)
+ continue;
+
+ /* Ignore ports that are not enabled */
+ enabled = udev_device_get_sysattr_value(d, "enabled");
+ if (!enabled)
+ continue;
+ if (!streq_ptr(enabled, "enabled"))
+ continue;
+
/* We count any connector which is not explicitly
* "disconnected" as connected. */
status = udev_device_get_sysattr_value(d, "status");
return n;
}
-bool manager_is_docked_or_multiple_displays(Manager *m) {
+bool manager_is_docked_or_external_displays(Manager *m) {
int n;
/* If we are docked don't react to lid closing */
/* If we have more than one display connected,
* assume that we are docked. */
- n = manager_count_displays(m);
+ n = manager_count_external_displays(m);
if (n < 0)
log_warning_errno(n, "Display counting failed: %m");
- else if (n > 1) {
- log_debug("Multiple (%i) displays connected.", n);
+ else if (n >= 1) {
+ log_debug("External (%i) displays connected.", n);
return true;
}
#include "strv.h"
#include "mkdir.h"
#include "path-util.h"
-#include "special.h"
+// #include "special.h"
#include "sleep-config.h"
#include "fileio-label.h"
#include "unit-name.h"
#include "bus-common-errors.h"
#include "udev-util.h"
#include "selinux-util.h"
+#include "efivars.h"
#include "logind.h"
+#include "formats-util.h"
+#include "process-util.h"
+#include "terminal-util.h"
+#include "utmp-wtmp.h"
int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
sd_bus_error *error) {
Manager *m = userdata;
- dual_timestamp t;
+ dual_timestamp t = DUAL_TIMESTAMP_NULL;
assert(bus);
assert(reply);
return sd_bus_message_append(reply, "b", b);
}
+static int property_get_scheduled_shutdown(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ Manager *m = userdata;
+ int r;
+
+ assert(bus);
+ assert(reply);
+ assert(m);
+
+ r = sd_bus_message_open_container(reply, 'r', "st");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
+ if (r < 0)
+ return r;
+
+ return sd_bus_message_close_container(reply);
+}
+
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
-static int method_get_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int property_get_docked(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ Manager *m = userdata;
+
+ assert(bus);
+ assert(reply);
+ assert(m);
+
+ return sd_bus_message_append(reply, "b", manager_is_docked_or_external_displays(m));
+}
+
+static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
Manager *m = userdata;
const char *name;
Session *session;
int r;
- assert(bus);
assert(message);
assert(m);
return sd_bus_reply_method_return(message, "o", p);
}
-static int method_get_session_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
Session *session = NULL;
Manager *m = userdata;
pid_t pid;
int r;
- assert(bus);
assert(message);
assert(m);
return sd_bus_reply_method_return(message, "o", p);
}
-static int method_get_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
Manager *m = userdata;
uint32_t uid;
User *user;
int r;
- assert(bus);
assert(message);
assert(m);
return sd_bus_reply_method_return(message, "o", p);
}
-static int method_get_user_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
Manager *m = userdata;
User *user = NULL;
pid_t pid;
int r;
- assert(bus);
assert(message);
assert(m);
return sd_bus_reply_method_return(message, "o", p);
}
-static int method_get_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
Manager *m = userdata;
const char *name;
Seat *seat;
int r;
- assert(bus);
assert(message);
assert(m);
return sd_bus_reply_method_return(message, "o", p);
}
-static int method_list_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
Manager *m = userdata;
Session *session;
Iterator i;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return sd_bus_send(bus, reply, NULL);
+ return sd_bus_send(NULL, reply, NULL);
}
-static int method_list_users(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
Manager *m = userdata;
User *user;
Iterator i;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return sd_bus_send(bus, reply, NULL);
+ return sd_bus_send(NULL, reply, NULL);
}
-static int method_list_seats(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
Manager *m = userdata;
Seat *seat;
Iterator i;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return sd_bus_send(bus, reply, NULL);
+ return sd_bus_send(NULL, reply, NULL);
}
-static int method_list_inhibitors(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
Manager *m = userdata;
Inhibitor *inhibitor;
Iterator i;
int r;
+ assert(message);
+ assert(m);
+
r = sd_bus_message_new_method_return(message, &reply);
if (r < 0)
return r;
if (r < 0)
return r;
- return sd_bus_send(bus, reply, NULL);
+ return sd_bus_send(NULL, reply, NULL);
}
-static int method_create_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
uint32_t uid, leader, audit_id = 0;
_cleanup_free_ char *id = NULL;
SessionClass c;
int r;
- assert(bus);
assert(message);
assert(m);
return r;
}
- manager_get_session_by_pid(m, leader, &session);
- if (session) {
- _cleanup_free_ char *path = NULL;
- _cleanup_close_ int fifo_fd = -1;
-
- /* Session already exists, client is probably
- * something like "su" which changes uid but is still
- * the same session */
-
- fifo_fd = session_create_fifo(session);
- if (fifo_fd < 0)
- return fifo_fd;
-
- path = session_bus_path(session);
- if (!path)
- return -ENOMEM;
-
- log_debug("Sending reply about an existing session: "
- "id=%s object_path=%s uid=%u runtime_path=%s "
- "session_fd=%d seat=%s vtnr=%u",
- session->id,
- path,
- (uint32_t) session->user->uid,
- session->user->runtime_path,
- fifo_fd,
- session->seat ? session->seat->id : "",
- (uint32_t) session->vtnr);
-
- return sd_bus_reply_method_return(
- message, "soshusub",
- session->id,
- path,
- session->user->runtime_path,
- fifo_fd,
- (uint32_t) session->user->uid,
- session->seat ? session->seat->id : "",
- (uint32_t) session->vtnr,
- true);
- }
+ r = manager_get_session_by_pid(m, leader, NULL);
+ if (r > 0)
+ return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session");
+
+ /*
+ * Old gdm and lightdm start the user-session on the same VT as
+ * the greeter session. But they destroy the greeter session
+ * after the user-session and want the user-session to take
+ * over the VT. We need to support this for
+ * backwards-compatibility, so make sure we allow new sessions
+ * on a VT that a greeter is running on. Furthermore, to allow
+ * re-logins, we have to allow a greeter to take over a used VT for
+ * the exact same reasons.
+ */
+ if (c != SESSION_GREETER &&
+ vtnr > 0 &&
+ vtnr < m->seat0->position_count &&
+ m->seat0->positions[vtnr] &&
+ m->seat0->positions[vtnr]->class != SESSION_GREETER)
+ return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
audit_session_from_pid(leader, &audit_id);
if (audit_id > 0) {
log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
audit_id = 0;
- free(id);
- id = NULL;
+ id = mfree(id);
}
}
if (!id) {
do {
- free(id);
- id = NULL;
+ id = mfree(id);
if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
return -ENOMEM;
return r;
}
-static int method_release_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
Session *session;
const char *name;
int r;
- assert(bus);
assert(message);
assert(m);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
Session *session;
const char *name;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return bus_session_method_activate(bus, message, session, error);
+ return bus_session_method_activate(message, session, error);
}
-static int method_activate_session_on_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *session_name, *seat_name;
Manager *m = userdata;
Session *session;
Seat *seat;
int r;
- assert(bus);
assert(message);
assert(m);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_lock_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
Session *session;
const char *name;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return bus_session_method_lock(bus, message, session, error);
+ return bus_session_method_lock(message, session, error);
}
-static int method_lock_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
int r;
- assert(bus);
assert(message);
assert(m);
message,
CAP_SYS_ADMIN,
"org.freedesktop.login1.lock-sessions",
+ NULL,
false,
UID_INVALID,
&m->polkit_registry,
return sd_bus_reply_method_return(message, NULL);
}
-static int method_kill_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
Manager *m = userdata;
Session *session;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return bus_session_method_kill(bus, message, session, error);
+ return bus_session_method_kill(message, session, error);
}
-static int method_kill_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
uint32_t uid;
User *user;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return bus_user_method_kill(bus, message, user, error);
+ return bus_user_method_kill(message, user, error);
}
-static int method_terminate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
const char *name;
Session *session;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return bus_session_method_terminate(bus, message, session, error);
+ return bus_session_method_terminate(message, session, error);
}
-static int method_terminate_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
uint32_t uid;
User *user;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return bus_user_method_terminate(bus, message, user, error);
+ return bus_user_method_terminate(message, user, error);
}
-static int method_terminate_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
const char *name;
Seat *seat;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return r;
- return bus_seat_method_terminate(bus, message, seat, error);
+ return bus_seat_method_terminate(message, seat, error);
}
-static int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *cc = NULL;
Manager *m = userdata;
int b, r;
uint32_t uid;
int interactive;
- assert(bus);
assert(message);
assert(m);
message,
CAP_SYS_ADMIN,
"org.freedesktop.login1.set-user-linger",
+ NULL,
interactive,
UID_INVALID,
&m->polkit_registry,
if (!t)
return -ENOMEM;
- write_string_file(t, "change");
+ write_string_file(t, "change", WRITE_STRING_FILE_CREATE);
}
return 0;
return trigger_device(m, NULL);
}
-static int method_attach_device(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *sysfs, *seat;
Manager *m = userdata;
int interactive, r;
- assert(bus);
assert(message);
assert(m);
message,
CAP_SYS_ADMIN,
"org.freedesktop.login1.attach-device",
+ NULL,
interactive,
UID_INVALID,
&m->polkit_registry,
return sd_bus_reply_method_return(message, NULL);
}
-static int method_flush_devices(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
int interactive, r;
- assert(bus);
assert(message);
assert(m);
message,
CAP_SYS_ADMIN,
"org.freedesktop.login1.flush-devices",
+ NULL,
interactive,
UID_INVALID,
&m->polkit_registry,
default:
p = "MESSAGE=System is shutting down.";
q = NULL;
- break;
}
+ if (!isempty(m->wall_message))
+ p = strjoina(p, " (", m->wall_message, ")");
+
return log_struct(LOG_NOTICE,
LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
p,
InhibitWhat w,
HandleAction action,
sd_bus_error *error) {
+
+/// elogind does not need these, we do it ourselves
+#if 0
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ const char *p;
+ char *c;
+#endif // 0
int r;
assert(m);
bus_manager_log_shutdown(m, w, action);
+/// elogind does it directly without depending on systemd running the system
+#if 0
+ r = sd_bus_call_method(
+ m->bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartUnit",
+ error,
+ &reply,
+ "ss", NULL, "replace-irreversibly");
+#else
r = shutdown_or_sleep(m, action);
+#endif // 0
+ if (r < 0)
+ return r;
+
+/// elogind neither needs a dbus reply, nor supports systemd action jobs
+#if 0
+ r = sd_bus_message_read(reply, "o", &p);
if (r < 0)
return r;
+ c = strdup(p);
+ if (!c)
+ return -ENOMEM;
+
+ m->action_unit = unit_name;
+ free(m->action_job);
+ m->action_job = c;
+ m->action_what = w;
+#endif // 0
+
if (w == INHIBIT_SLEEP)
/* And we're back. */
send_prepare_for(m, w, false);
m->action_what = 0;
- /* Make sure the lid switch is ignored for a while (?) */
+ /* Make sure the lid switch is ignored for a while */
manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
return 0;
}
+int manager_dispatch_delayed(Manager *manager, bool timeout) {
+
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ Inhibitor *offending = NULL;
+ int r;
+
+ assert(manager);
+
+ if (manager->action_what == 0)
+ return 0;
+
+ if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
+ _cleanup_free_ char *comm = NULL, *u = NULL;
+
+ if (!timeout)
+ return 0;
+
+ (void) get_process_comm(offending->pid, &comm);
+ u = uid_to_name(offending->uid);
+
+ log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
+ offending->uid, strna(u),
+ offending->pid, strna(comm));
+ }
+
+ /* Actually do the operation */
+ r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
+ if (r < 0) {
+ log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
+
+ manager->pending_action = HANDLE_IGNORE;
+ manager->action_what = 0;
+ return r;
+ }
+
+ return 1;
+}
+
+static int manager_inhibit_timeout_handler(
+ sd_event_source *s,
+ uint64_t usec,
+ void *userdata) {
+
+ Manager *manager = userdata;
+ int r;
+
+ assert(manager);
+ assert(manager->inhibit_timeout_source == s);
+
+ r = manager_dispatch_delayed(manager, true);
+ return (r < 0) ? r : 0;
+}
+
static int delay_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
HandleAction action) {
+ int r;
+ usec_t timeout_val;
+
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
- m->action_timestamp = now(CLOCK_MONOTONIC);
+ timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
+
+ if (m->inhibit_timeout_source) {
+ r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_time() failed: %m");
+
+ r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
+ } else {
+ r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
+ timeout_val, 0, manager_inhibit_timeout_handler, m);
+ if (r < 0)
+ return r;
+ }
+
m->pending_action = action;
m->action_what = w;
return r;
}
-static int method_do_shutdown_or_sleep(
+static int verify_shutdown_creds(
Manager *m,
sd_bus_message *message,
- HandleAction sleep_action,
InhibitWhat w,
+ bool interactive,
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
- const char *sleep_verb,
- sd_bus_message_handler_t method,
sd_bus_error *error) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
bool multiple_sessions, blocked;
- int interactive, r;
uid_t uid;
+ int r;
assert(m);
assert(message);
assert(w >= 0);
assert(w <= _INHIBIT_WHAT_MAX);
- assert(action);
- assert(action_multiple_sessions);
- assert(action_ignore_inhibit);
- assert(method);
-
- r = sd_bus_message_read(message, "b", &interactive);
- if (r < 0)
- return r;
-
- /* Don't allow multiple jobs being executed at the same time */
- if (m->action_what)
- return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
-
- if (sleep_verb) {
- r = can_sleep(sleep_verb);
- if (r < 0)
- return r;
-
- if (r == 0)
- return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
- }
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
if (r < 0)
multiple_sessions = r > 0;
blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
- if (multiple_sessions) {
- r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, interactive, UID_INVALID, &m->polkit_registry, error);
+ if (multiple_sessions && action_multiple_sessions) {
+ r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
}
- if (blocked) {
- r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, interactive, UID_INVALID, &m->polkit_registry, error);
+ if (blocked && action_ignore_inhibit) {
+ r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
}
- if (!multiple_sessions && !blocked) {
- r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, interactive, UID_INVALID, &m->polkit_registry, error);
+ if (!multiple_sessions && !blocked && action) {
+ r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
}
+ return 0;
+}
+
+static int method_do_shutdown_or_sleep(
+ Manager *m,
+ sd_bus_message *message,
+ HandleAction sleep_action,
+ InhibitWhat w,
+ const char *action,
+ const char *action_multiple_sessions,
+ const char *action_ignore_inhibit,
+ const char *sleep_verb,
+ sd_bus_error *error) {
+
+ int interactive, r;
+
+ assert(m);
+ assert(message);
+ assert(w >= 0);
+ assert(w <= _INHIBIT_WHAT_MAX);
+
+ r = sd_bus_message_read(message, "b", &interactive);
+ if (r < 0)
+ return r;
+
+ /* Don't allow multiple jobs being executed at the same time */
+ if (m->action_what)
+ return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
+
+ if (sleep_verb) {
+ r = can_sleep(sleep_verb);
+ if (r < 0)
+ return r;
+
+ if (r == 0)
+ return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
+ }
+
+ r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
+ action_ignore_inhibit, error);
+ if (r != 0)
+ return r;
+
r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
-static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
"org.freedesktop.login1.power-off-multiple-sessions",
"org.freedesktop.login1.power-off-ignore-inhibit",
NULL,
- method_poweroff,
error);
}
-static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
"org.freedesktop.login1.reboot-multiple-sessions",
"org.freedesktop.login1.reboot-ignore-inhibit",
NULL,
- method_reboot,
error);
}
-static int method_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
"org.freedesktop.login1.suspend-multiple-sessions",
"org.freedesktop.login1.suspend-ignore-inhibit",
"suspend",
- method_suspend,
error);
}
-static int method_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int nologin_timeout_handler(
+ sd_event_source *s,
+ uint64_t usec,
+ void *userdata) {
+
Manager *m = userdata;
+ int r;
- return method_do_shutdown_or_sleep(
- m, message,
- HANDLE_HIBERNATE,
- INHIBIT_SLEEP,
- "org.freedesktop.login1.hibernate",
- "org.freedesktop.login1.hibernate-multiple-sessions",
- "org.freedesktop.login1.hibernate-ignore-inhibit",
- "hibernate",
- method_hibernate,
- error);
-}
+ log_info("Creating /run/nologin, blocking further logins...");
-static int method_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
- Manager *m = userdata;
+ r = write_string_file("/run/nologin", "System is going down.", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
+ if (r < 0)
+ log_error_errno(r, "Failed to create /run/nologin: %m");
+ else
+ m->unlink_nologin = true;
- return method_do_shutdown_or_sleep(
- m, message,
- HANDLE_HYBRID_SLEEP,
- INHIBIT_SLEEP,
- "org.freedesktop.login1.hibernate",
- "org.freedesktop.login1.hibernate-multiple-sessions",
- "org.freedesktop.login1.hibernate-ignore-inhibit",
- "hybrid-sleep",
- method_hybrid_sleep,
- error);
+ return 0;
}
-static int method_can_shutdown_or_sleep(
- Manager *m,
- sd_bus_message *message,
- InhibitWhat w,
- const char *action,
- const char *action_multiple_sessions,
- const char *action_ignore_inhibit,
- const char *sleep_verb,
- sd_bus_error *error) {
-
- _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
- bool multiple_sessions, challenge, blocked;
- const char *result = NULL;
- uid_t uid;
+static int update_schedule_file(Manager *m) {
+ _cleanup_free_ char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
int r;
assert(m);
- assert(message);
- assert(w >= 0);
- assert(w <= _INHIBIT_WHAT_MAX);
- assert(action);
- assert(action_multiple_sessions);
- assert(action_ignore_inhibit);
- if (sleep_verb) {
- r = can_sleep(sleep_verb);
- if (r < 0)
- return r;
- if (r == 0)
- return sd_bus_reply_method_return(message, "s", "na");
- }
+ r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0);
+ if (r < 0)
+ return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
+
+ r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
+ if (r < 0)
+ return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
+
+ (void) fchmod(fileno(f), 0644);
+
+ fprintf(f,
+ "USEC="USEC_FMT"\n"
+ "WARN_WALL=%i\n"
+ "MODE=%s\n",
+ m->scheduled_shutdown_timeout,
+ m->enable_wall_messages,
+ m->scheduled_shutdown_type);
+
+ if (!isempty(m->wall_message)) {
+ _cleanup_free_ char *t;
+
+ t = cescape(m->wall_message);
+ if (!t) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ fprintf(f, "WALL_MESSAGE=%s\n", t);
+ }
+
+ r = fflush_and_check(f);
+ if (r < 0)
+ goto fail;
+
+ if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
+ r = -errno;
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ (void) unlink(temp_path);
+ (void) unlink("/run/systemd/shutdown/scheduled");
+
+ return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
+}
+
+static int manager_scheduled_shutdown_handler(
+ sd_event_source *s,
+ uint64_t usec,
+ void *userdata) {
+
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ Manager *m = userdata;
+ HandleAction action;
+ int r;
+
+ assert(m);
+
+ if (isempty(m->scheduled_shutdown_type))
+ return 0;
+
+ if (streq(m->scheduled_shutdown_type, "halt"))
+ action = HANDLE_HALT;
+ else if (streq(m->scheduled_shutdown_type, "poweroff"))
+ action = HANDLE_POWEROFF;
+ else
+ action = HANDLE_REBOOT;
+
+ r = execute_shutdown_or_sleep(m, 0, action, &error);
+ if (r < 0)
+ return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
+
+ return 0;
+}
+
+static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+ const char *action_multiple_sessions = NULL;
+ const char *action_ignore_inhibit = NULL;
+ const char *action = NULL;
+ uint64_t elapse;
+ char *type;
+ int r;
+
+ assert(m);
+ assert(message);
+
+ r = sd_bus_message_read(message, "st", &type, &elapse);
+ if (r < 0)
+ return r;
+
+ if (streq(type, "reboot")) {
+ action = "org.freedesktop.login1.reboot";
+ action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
+ action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
+ } else if (streq(type, "halt")) {
+ action = "org.freedesktop.login1.halt";
+ action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
+ action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
+ } else if (streq(type, "poweroff")) {
+ action = "org.freedesktop.login1.poweroff";
+ action_multiple_sessions = "org.freedesktop.login1.poweroff-multiple-sessions";
+ action_ignore_inhibit = "org.freedesktop.login1.poweroff-ignore-inhibit";
+ } else
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
+
+ r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
+ action, action_multiple_sessions, action_ignore_inhibit, error);
+ if (r != 0)
+ return r;
+
+ if (m->scheduled_shutdown_timeout_source) {
+ r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_time() failed: %m");
+
+ r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
+ } else {
+ r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
+ CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_add_time() failed: %m");
+ }
+
+ r = free_and_strdup(&m->scheduled_shutdown_type, type);
+ if (r < 0) {
+ m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
+ return log_oom();
+ }
+
+ if (m->nologin_timeout_source) {
+ r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_time() failed: %m");
+
+ r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
+ } else {
+ r = sd_event_add_time(m->event, &m->nologin_timeout_source,
+ CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_add_time() failed: %m");
+ }
+
+ m->scheduled_shutdown_timeout = elapse;
+
+ r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
+ if (r >= 0) {
+ const char *tty;
+
+ (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
+ (void) sd_bus_creds_get_tty(creds, &tty);
+
+ r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
+ if (r < 0) {
+ m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
+ return log_oom();
+ }
+ }
+
+ r = manager_setup_wall_message_timer(m);
+ if (r < 0)
+ return r;
+
+ if (!isempty(type)) {
+ r = update_schedule_file(m);
+ if (r < 0)
+ return r;
+ } else
+ (void) unlink("/run/systemd/shutdown/scheduled");
+
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ bool cancelled;
+
+ assert(m);
+ assert(message);
+
+ cancelled = m->scheduled_shutdown_type != NULL;
+
+ m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
+ m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
+ m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
+ m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
+ m->scheduled_shutdown_timeout = 0;
+
+ if (m->unlink_nologin) {
+ (void) unlink("/run/nologin");
+ m->unlink_nologin = false;
+ }
+
+ if (cancelled) {
+ _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+ const char *tty = NULL;
+ uid_t uid = 0;
+ int r;
+
+ r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
+ if (r >= 0) {
+ (void) sd_bus_creds_get_uid(creds, &uid);
+ (void) sd_bus_creds_get_tty(creds, &tty);
+ }
+
+ utmp_wall("The system shutdown has been cancelled",
+ lookup_uid(uid), tty, logind_wall_tty_filter, m);
+ }
+
+ return sd_bus_reply_method_return(message, "b", cancelled);
+}
+
+static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+
+ return method_do_shutdown_or_sleep(
+ m, message,
+ HANDLE_HIBERNATE,
+ INHIBIT_SLEEP,
+ "org.freedesktop.login1.hibernate",
+ "org.freedesktop.login1.hibernate-multiple-sessions",
+ "org.freedesktop.login1.hibernate-ignore-inhibit",
+ "hibernate",
+ error);
+}
+
+static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+
+ return method_do_shutdown_or_sleep(
+ m, message,
+ HANDLE_HYBRID_SLEEP,
+ INHIBIT_SLEEP,
+ "org.freedesktop.login1.hibernate",
+ "org.freedesktop.login1.hibernate-multiple-sessions",
+ "org.freedesktop.login1.hibernate-ignore-inhibit",
+ "hybrid-sleep",
+ error);
+}
+
+static int method_can_shutdown_or_sleep(
+ Manager *m,
+ sd_bus_message *message,
+ InhibitWhat w,
+ const char *action,
+ const char *action_multiple_sessions,
+ const char *action_ignore_inhibit,
+ const char *sleep_verb,
+ sd_bus_error *error) {
+
+ _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+ bool multiple_sessions, challenge, blocked;
+ const char *result = NULL;
+ uid_t uid;
+ int r;
+
+ assert(m);
+ assert(message);
+ assert(w >= 0);
+ assert(w <= _INHIBIT_WHAT_MAX);
+ assert(action);
+ assert(action_multiple_sessions);
+ assert(action_ignore_inhibit);
+
+ if (sleep_verb) {
+ r = can_sleep(sleep_verb);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return sd_bus_reply_method_return(message, "s", "na");
+ }
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
if (r < 0)
blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
if (multiple_sessions) {
- r = bus_verify_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, false, UID_INVALID, &challenge, error);
+ r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
if (r < 0)
return r;
}
if (blocked) {
- r = bus_verify_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, false, UID_INVALID, &challenge, error);
+ r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
if (r < 0)
return r;
/* If neither inhibit nor multiple sessions
* apply then just check the normal policy */
- r = bus_verify_polkit(message, CAP_SYS_BOOT, action, false, UID_INVALID, &challenge, error);
+ r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, "s", result);
}
-static int method_can_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
error);
}
-static int method_can_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
error);
}
-static int method_can_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
error);
}
-static int method_can_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
error);
}
-static int method_can_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
error);
}
-static int method_inhibit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int property_get_reboot_to_firmware_setup(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+ int r;
+
+ assert(bus);
+ assert(reply);
+ assert(userdata);
+
+ r = efi_get_reboot_to_firmware();
+ if (r < 0 && r != -EOPNOTSUPP)
+ return r;
+
+ return sd_bus_message_append(reply, "b", r > 0);
+}
+
+static int method_set_reboot_to_firmware_setup(
+ sd_bus_message *message,
+ void *userdata,
+ sd_bus_error *error) {
+
+ int b, r;
+ Manager *m = userdata;
+
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read(message, "b", &b);
+ if (r < 0)
+ return r;
+
+ r = bus_verify_polkit_async(message,
+ CAP_SYS_ADMIN,
+ "org.freedesktop.login1.set-reboot-to-firmware-setup",
+ NULL,
+ false,
+ UID_INVALID,
+ &m->polkit_registry,
+ error);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
+
+ r = efi_set_reboot_to_firmware(b);
+ if (r < 0)
+ return r;
+
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_can_reboot_to_firmware_setup(
+ sd_bus_message *message,
+ void *userdata,
+ sd_bus_error *error) {
+
+ int r;
+ bool challenge;
+ const char *result;
+ Manager *m = userdata;
+
+ assert(message);
+ assert(m);
+
+ r = efi_reboot_to_firmware_supported();
+ if (r == -EOPNOTSUPP)
+ return sd_bus_reply_method_return(message, "s", "na");
+ else if (r < 0)
+ return r;
+
+ r = bus_test_polkit(message,
+ CAP_SYS_ADMIN,
+ "org.freedesktop.login1.set-reboot-to-firmware-setup",
+ NULL,
+ UID_INVALID,
+ &challenge,
+ error);
+ if (r < 0)
+ return r;
+
+ if (r > 0)
+ result = "yes";
+ else if (challenge)
+ result = "challenge";
+ else
+ result = "no";
+
+ return sd_bus_reply_method_return(message, "s", result);
+}
+
+static int method_set_wall_message(
+ sd_bus_message *message,
+ void *userdata,
+ sd_bus_error *error) {
+
+ int r;
+ Manager *m = userdata;
+ char *wall_message;
+ int enable_wall_messages;
+
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
+ if (r < 0)
+ return r;
+
+ r = bus_verify_polkit_async(message,
+ CAP_SYS_ADMIN,
+ "org.freedesktop.login1.set-wall-message",
+ NULL,
+ false,
+ UID_INVALID,
+ &m->polkit_registry,
+ error);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return 1; /* Will call us back */
+
+ if (isempty(wall_message))
+ m->wall_message = mfree(m->wall_message);
+ else {
+ r = free_and_strdup(&m->wall_message, wall_message);
+ if (r < 0)
+ return log_oom();
+ }
+
+ m->enable_wall_messages = enable_wall_messages;
+
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
const char *who, *why, *what, *mode;
_cleanup_free_ char *id = NULL;
uid_t uid;
int r;
- assert(bus);
assert(message);
assert(m);
w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
"org.freedesktop.login1.inhibit-handle-lid-switch",
+ NULL,
false,
UID_INVALID,
&m->polkit_registry,
return r;
do {
- free(id);
- id = NULL;
+ id = mfree(id);
if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
return -ENOMEM;
const sd_bus_vtable manager_vtable[] = {
SD_BUS_VTABLE_START(0),
+ SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
+ SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
+
+// SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
+ SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
+ SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_SIGNAL("SessionNew", "so", 0),
SD_BUS_SIGNAL("SessionRemoved", "so", 0),
SD_BUS_VTABLE_END
};
-int manager_send_changed(Manager *manager, const char *property, ...) {
- char **l;
+/// UNNEEDED by elogind
+#if 0
+static int session_jobs_reply(Session *s, const char *unit, const char *result) {
+ int r = 0;
- assert(manager);
+ assert(s);
+ assert(unit);
- l = strv_from_stdarg_alloca(property);
+ if (!s->started)
+ return r;
- return sd_bus_emit_properties_changed_strv(
- manager->bus,
+ if (streq(result, "done"))
+ r = session_send_create_reply(s, NULL);
+ else {
+ _cleanup_bus_error_free_ sd_bus_error e = SD_BUS_ERROR_NULL;
+
+ sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
+ r = session_send_create_reply(s, &e);
+ }
+
+ return r;
+}
+
+int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ const char *path, *result, *unit;
+ Manager *m = userdata;
+ Session *session;
+ uint32_t id;
+ User *user;
+ int r;
+
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
+ if (r < 0) {
+ bus_log_parse_error(r);
+ return 0;
+ }
+
+ if (m->action_job && streq(m->action_job, path)) {
+ log_info("Operation finished.");
+
+ /* Tell people that they now may take a lock again */
+ send_prepare_for(m, m->action_what, false);
+
+ m->action_job = mfree(m->action_job);
+ m->action_unit = NULL;
+ m->action_what = 0;
+ return 0;
+ }
+
+ session = hashmap_get(m->session_units, unit);
+ if (session) {
+
+ if (streq_ptr(path, session->scope_job))
+ session->scope_job = mfree(session->scope_job);
+
+ session_jobs_reply(session, unit, result);
+
+ session_save(session);
+ user_save(session->user);
+ session_add_to_gc_queue(session);
+ }
+
+ user = hashmap_get(m->user_units, unit);
+ if (user) {
+
+ if (streq_ptr(path, user->service_job))
+ user->service_job = mfree(user->service_job);
+
+ if (streq_ptr(path, user->slice_job))
+ user->slice_job = mfree(user->slice_job);
+
+ LIST_FOREACH(sessions_by_user, session, user->sessions)
+ session_jobs_reply(session, unit, result);
+
+ user_save(user);
+ user_add_to_gc_queue(user);
+ }
+
+ return 0;
+}
+
+int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ const char *path, *unit;
+ Manager *m = userdata;
+ Session *session;
+ User *user;
+ int r;
+
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read(message, "so", &unit, &path);
+ if (r < 0) {
+ bus_log_parse_error(r);
+ return 0;
+ }
+
+ session = hashmap_get(m->session_units, unit);
+ if (session)
+ session_add_to_gc_queue(session);
+
+ user = hashmap_get(m->user_units, unit);
+ if (user)
+ user_add_to_gc_queue(user);
+
+ return 0;
+}
+
+int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *unit = NULL;
+ Manager *m = userdata;
+ const char *path;
+ Session *session;
+ User *user;
+ int r;
+
+ assert(message);
+ assert(m);
+
+ path = sd_bus_message_get_path(message);
+ if (!path)
+ return 0;
+
+ r = unit_name_from_dbus_path(path, &unit);
+ if (r == -EINVAL) /* not a unit */
+ return 0;
+ if (r < 0) {
+ log_oom();
+ return 0;
+ }
+
+ session = hashmap_get(m->session_units, unit);
+ if (session)
+ session_add_to_gc_queue(session);
+
+ user = hashmap_get(m->user_units, unit);
+ if (user)
+ user_add_to_gc_queue(user);
+
+ return 0;
+}
+
+int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ Session *session;
+ Iterator i;
+ int b, r;
+
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read(message, "b", &b);
+ if (r < 0) {
+ bus_log_parse_error(r);
+ return 0;
+ }
+
+ if (b)
+ return 0;
+
+ /* systemd finished reloading, let's recheck all our sessions */
+ log_debug("System manager has been reloaded, rechecking sessions...");
+
+ HASHMAP_FOREACH(session, m->sessions, i)
+ session_add_to_gc_queue(session);
+
+ return 0;
+}
+#endif // 0
+
+int manager_send_changed(Manager *manager, const char *property, ...) {
+ char **l;
+
+ assert(manager);
+
+ l = strv_from_stdarg_alloca(property);
+
+ return sd_bus_emit_properties_changed_strv(
+ manager->bus,
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
l);
}
-int manager_dispatch_delayed(Manager *manager) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- Inhibitor *offending = NULL;
+/// UNNEEDED by elogind
+#if 0
+int manager_start_scope(
+ Manager *manager,
+ const char *scope,
+ pid_t pid,
+ const char *slice,
+ const char *description,
+ const char *after, const char *after2,
+ sd_bus_error *error,
+ char **job) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
int r;
assert(manager);
+ assert(scope);
+ assert(pid > 1);
- if (manager->action_what == 0)
- return 0;
+ r = sd_bus_message_new_method_call(
+ manager->bus,
+ &m,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartTransientUnit");
+ if (r < 0)
+ return r;
- /* Continue delay? */
- if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
- _cleanup_free_ char *comm = NULL, *u = NULL;
+ r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
+ if (r < 0)
+ return r;
- get_process_comm(offending->pid, &comm);
- u = uid_to_name(offending->uid);
+ r = sd_bus_message_open_container(m, 'a', "(sv)");
+ if (r < 0)
+ return r;
+
+ if (!isempty(slice)) {
+ r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
+ if (r < 0)
+ return r;
+ }
- if (manager->action_timestamp + manager->inhibit_delay_max > now(CLOCK_MONOTONIC))
+ if (!isempty(description)) {
+ r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
+ if (r < 0)
+ return r;
+ }
+
+ if (!isempty(after)) {
+ r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
+ if (r < 0)
+ return r;
+ }
+
+ if (!isempty(after2)) {
+ r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
+ if (r < 0)
+ return r;
+ }
+
+ /* cgroup empty notification is not available in containers
+ * currently. To make this less problematic, let's shorten the
+ * stop timeout for sessions, so that we don't wait
+ * forever. */
+
+ /* Make sure that the session shells are terminated with
+ * SIGHUP since bash and friends tend to ignore SIGTERM */
+ r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_close_container(m);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "a(sa(sv))", 0);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_call(manager->bus, m, 0, error, &reply);
+ if (r < 0)
+ return r;
+
+ if (job) {
+ const char *j;
+ char *copy;
+
+ r = sd_bus_message_read(reply, "o", &j);
+ if (r < 0)
+ return r;
+
+ copy = strdup(j);
+ if (!copy)
+ return -ENOMEM;
+
+ *job = copy;
+ }
+
+ return 1;
+}
+
+int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ int r;
+
+ assert(manager);
+ assert(unit);
+
+ r = sd_bus_call_method(
+ manager->bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartUnit",
+ error,
+ &reply,
+ "ss", unit, "fail");
+ if (r < 0)
+ return r;
+
+ if (job) {
+ const char *j;
+ char *copy;
+
+ r = sd_bus_message_read(reply, "o", &j);
+ if (r < 0)
+ return r;
+
+ copy = strdup(j);
+ if (!copy)
+ return -ENOMEM;
+
+ *job = copy;
+ }
+
+ return 1;
+}
+
+int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ int r;
+
+ assert(manager);
+ assert(unit);
+
+ r = sd_bus_call_method(
+ manager->bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StopUnit",
+ error,
+ &reply,
+ "ss", unit, "fail");
+ if (r < 0) {
+ if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
+ sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
+
+ if (job)
+ *job = NULL;
+
+ sd_bus_error_free(error);
return 0;
+ }
+
+ return r;
+ }
+
+ if (job) {
+ const char *j;
+ char *copy;
- log_info("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
- offending->uid, strna(u),
- offending->pid, strna(comm));
+ r = sd_bus_message_read(reply, "o", &j);
+ if (r < 0)
+ return r;
+
+ copy = strdup(j);
+ if (!copy)
+ return -ENOMEM;
+
+ *job = copy;
}
- /* Actually do the operation */
- r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
+ return 1;
+}
+
+int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
+ _cleanup_free_ char *path = NULL;
+ int r;
+
+ assert(manager);
+ assert(scope);
+
+ path = unit_dbus_path_from_name(scope);
+ if (!path)
+ return -ENOMEM;
+
+ r = sd_bus_call_method(
+ manager->bus,
+ "org.freedesktop.systemd1",
+ path,
+ "org.freedesktop.systemd1.Scope",
+ "Abandon",
+ error,
+ NULL,
+ NULL);
if (r < 0) {
- log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
+ if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
+ sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
+ sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
+ sd_bus_error_free(error);
+ return 0;
+ }
- manager->pending_action = HANDLE_IGNORE;
- manager->action_what = 0;
return r;
}
return 1;
}
+
+int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
+ assert(manager);
+ assert(unit);
+
+ return sd_bus_call_method(
+ manager->bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "KillUnit",
+ error,
+ NULL,
+ "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
+}
+
+int manager_unit_is_active(Manager *manager, const char *unit) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ _cleanup_free_ char *path = NULL;
+ const char *state;
+ int r;
+
+ assert(manager);
+ assert(unit);
+
+ path = unit_dbus_path_from_name(unit);
+ if (!path)
+ return -ENOMEM;
+
+ r = sd_bus_get_property(
+ manager->bus,
+ "org.freedesktop.systemd1",
+ path,
+ "org.freedesktop.systemd1.Unit",
+ "ActiveState",
+ &error,
+ &reply,
+ "s");
+ if (r < 0) {
+ /* systemd might have droppped off momentarily, let's
+ * not make this an error */
+ if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
+ sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
+ return true;
+
+ /* If the unit is already unloaded then it's not
+ * active */
+ if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
+ sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
+ return false;
+
+ return r;
+ }
+
+ r = sd_bus_message_read(reply, "s", &state);
+ if (r < 0)
+ return -EINVAL;
+
+ return !streq(state, "inactive") && !streq(state, "failed");
+}
+
+int manager_job_is_active(Manager *manager, const char *path) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ int r;
+
+ assert(manager);
+ assert(path);
+
+ r = sd_bus_get_property(
+ manager->bus,
+ "org.freedesktop.systemd1",
+ path,
+ "org.freedesktop.systemd1.Job",
+ "State",
+ &error,
+ &reply,
+ "s");
+ if (r < 0) {
+ if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
+ sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
+ return true;
+
+ if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
+ return false;
+
+ return r;
+ }
+
+ /* We don't actually care about the state really. The fact
+ * that we could read the job state is enough for us */
+
+ return true;
+}
+#endif //
#include "mkdir.h"
#include "logind-inhibit.h"
#include "fileio.h"
+#include "formats-util.h"
Inhibitor* inhibitor_new(Manager *m, const char* id) {
Inhibitor *i;
r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
if (r < 0)
- goto finish;
+ goto fail;
r = fopen_temporary(i->state_file, &f, &temp_path);
if (r < 0)
- goto finish;
+ goto fail;
fchmod(fileno(f), 0644);
_cleanup_free_ char *cc = NULL;
cc = cescape(i->who);
- if (!cc)
+ if (!cc) {
r = -ENOMEM;
- else
- fprintf(f, "WHO=%s\n", cc);
+ goto fail;
+ }
+
+ fprintf(f, "WHO=%s\n", cc);
}
if (i->why) {
_cleanup_free_ char *cc = NULL;
cc = cescape(i->why);
- if (!cc)
+ if (!cc) {
r = -ENOMEM;
- else
- fprintf(f, "WHY=%s\n", cc);
+ goto fail;
+ }
+
+ fprintf(f, "WHY=%s\n", cc);
}
if (i->fifo_path)
fprintf(f, "FIFO=%s\n", i->fifo_path);
- fflush(f);
+ r = fflush_and_check(f);
+ if (r < 0)
+ goto fail;
- if (ferror(f) || rename(temp_path, i->state_file) < 0) {
+ if (rename(temp_path, i->state_file) < 0) {
r = -errno;
- unlink(i->state_file);
- unlink(temp_path);
+ goto fail;
}
-finish:
- if (r < 0)
- log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
+ return 0;
- return r;
+fail:
+ (void) unlink(i->state_file);
+
+ if (temp_path)
+ (void) unlink(temp_path);
+
+ return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
}
int inhibitor_start(Inhibitor *i) {
}
if (who) {
- cc = cunescape(who);
- if (!cc)
- return -ENOMEM;
+ r = cunescape(who, 0, &cc);
+ if (r < 0)
+ return r;
free(i->who);
i->who = cc;
}
if (why) {
- cc = cunescape(why);
- if (!cc)
- return -ENOMEM;
+ r = cunescape(why, 0, &cc);
+ if (r < 0)
+ return r;
free(i->why);
i->why = cc;
if (i->fifo_path) {
unlink(i->fifo_path);
- free(i->fifo_path);
- i->fifo_path = NULL;
+ i->fifo_path = mfree(i->fifo_path);
}
}
Inhibitor *i;
Iterator j;
- struct dual_timestamp ts = { 0, 0 };
+ struct dual_timestamp ts = DUAL_TIMESTAMP_NULL;
bool inhibited = false;
assert(m);
return sd_bus_message_append(reply, "t", u);
}
-int bus_seat_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_seat_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Seat *s = userdata;
int r;
- assert(bus);
assert(message);
assert(s);
message,
CAP_KILL,
"org.freedesktop.login1.manage",
+ NULL,
false,
UID_INVALID,
&s->manager->polkit_registry,
return sd_bus_reply_method_return(message, NULL);
}
-static int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Seat *s = userdata;
const char *name;
Session *session;
int r;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_switch_to(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_switch_to(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Seat *s = userdata;
unsigned int to;
int r;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_switch_to_next(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_switch_to_next(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Seat *s = userdata;
int r;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_switch_to_previous(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_switch_to_previous(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Seat *s = userdata;
int r;
- assert(bus);
assert(message);
assert(s);
#include "logind-acl.h"
#include "util.h"
#include "mkdir.h"
+#include "formats-util.h"
+#include "terminal-util.h"
Seat *seat_new(Manager *m, const char *id) {
Seat *s;
r = mkdir_safe_label("/run/systemd/seats", 0755, 0, 0);
if (r < 0)
- goto finish;
+ goto fail;
r = fopen_temporary(s->state_file, &f, &temp_path);
if (r < 0)
- goto finish;
+ goto fail;
fchmod(fileno(f), 0644);
i->sessions_by_seat_next ? ' ' : '\n');
}
- fflush(f);
+ r = fflush_and_check(f);
+ if (r < 0)
+ goto fail;
- if (ferror(f) || rename(temp_path, s->state_file) < 0) {
+ if (rename(temp_path, s->state_file) < 0) {
r = -errno;
- unlink(s->state_file);
- unlink(temp_path);
+ goto fail;
}
-finish:
- if (r < 0)
- log_error_errno(r, "Failed to save seat data %s: %m", s->state_file);
+ return 0;
- return r;
+fail:
+ (void) unlink(s->state_file);
+
+ if (temp_path)
+ (void) unlink(temp_path);
+
+ return log_error_errno(r, "Failed to save seat data %s: %m", s->state_file);
}
int seat_load(Seat *s) {
return 0;
}
+/// UNNEEDED by elogind
+#if 0
static int vt_allocate(unsigned int vtnr) {
char p[sizeof("/dev/tty") + DECIMAL_STR_MAX(unsigned int)];
_cleanup_close_ int fd = -1;
return 0;
}
+int seat_preallocate_vts(Seat *s) {
+ int r = 0;
+ unsigned i;
+
+ assert(s);
+ assert(s->manager);
+
+ log_debug("Preallocating VTs...");
+
+ if (s->manager->n_autovts <= 0)
+ return 0;
+
+ if (!seat_has_vts(s))
+ return 0;
+
+ for (i = 1; i <= s->manager->n_autovts; i++) {
+ int q;
+
+ q = vt_allocate(i);
+ if (q < 0) {
+ log_error_errno(q, "Failed to preallocate VT %u: %m", i);
+ r = q;
+ }
+ }
+
+ return r;
+}
+#endif // 0
+
int seat_apply_acls(Seat *s, Session *old_active) {
int r;
int seat_switch_to(Seat *s, unsigned int num) {
/* Public session positions skip 0 (there is only F1-F12). Maybe it
* will get reassigned in the future, so return error for now. */
- if (!num)
+ if (num == 0)
return -EINVAL;
if (num >= s->position_count || !s->positions[num]) {
int seat_switch_to_next(Seat *s) {
unsigned int start, i;
- if (!s->position_count)
+ if (s->position_count == 0)
return -EINVAL;
start = 1;
- if (s->active && s->active->pos > 0)
- start = s->active->pos;
+ if (s->active && s->active->position > 0)
+ start = s->active->position;
for (i = start + 1; i < s->position_count; ++i)
if (s->positions[i])
int seat_switch_to_previous(Seat *s) {
unsigned int start, i;
- if (!s->position_count)
+ if (s->position_count == 0)
return -EINVAL;
start = 1;
- if (s->active && s->active->pos > 0)
- start = s->active->pos;
+ if (s->active && s->active->position > 0)
+ start = s->active->position;
for (i = start - 1; i > 0; --i)
if (s->positions[i])
r = seat_set_active(s, new_active);
+/// elogind does not spawn autovt
+#if 0
+ manager_spawn_autovt(s->manager, vtnr);
+#endif // 0
+
return r;
}
LOG_MESSAGE("New seat %s.", s->id),
NULL);
+ /* Initialize VT magic stuff */
+/// elogind does not support autospawning vts
+#if 0
+ seat_preallocate_vts(s);
+#endif // 0
+
/* Read current VT */
seat_read_active_vt(s);
void seat_evict_position(Seat *s, Session *session) {
Session *iter;
- unsigned int pos = session->pos;
+ unsigned int pos = session->position;
- session->pos = 0;
+ session->position = 0;
- if (!pos)
+ if (pos == 0)
return;
if (pos < s->position_count && s->positions[pos] == session) {
s->positions[pos] = NULL;
/* There might be another session claiming the same
- * position (eg., during gdm->session transition), so lets look
+ * position (eg., during gdm->session transition), so let's look
* for it and set it on the free slot. */
LIST_FOREACH(sessions_by_seat, iter, s->sessions) {
- if (iter->pos == pos) {
+ if (iter->position == pos && session_get_state(iter) != SESSION_CLOSING) {
s->positions[pos] = iter;
break;
}
seat_evict_position(s, session);
- session->pos = pos;
- if (pos > 0 && !s->positions[pos])
+ session->position = pos;
+ if (pos > 0)
s->positions[pos] = session;
}
static void seat_assign_position(Seat *s, Session *session) {
unsigned int pos;
- if (session->pos > 0)
+ if (session->position > 0)
return;
for (pos = 1; pos < s->position_count; ++pos)
int seat_get_idle_hint(Seat *s, dual_timestamp *t) {
Session *session;
bool idle_hint = true;
- dual_timestamp ts = { 0, 0 };
+ dual_timestamp ts = DUAL_TIMESTAMP_NULL;
assert(s);
int seat_switch_to_previous(Seat *s);
int seat_active_vt_changed(Seat *s, unsigned int vtnr);
int seat_read_active_vt(Seat *s);
-int seat_preallocate_vts(Seat *s);
+// UNNEEDED int seat_preallocate_vts(Seat *s);
int seat_attach_session(Seat *s, Session *session);
void seat_complete_switch(Seat *s);
int seat_send_signal(Seat *s, bool new_seat);
int seat_send_changed(Seat *s, const char *properties, ...) _sentinel_;
-int bus_seat_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_seat_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error);
sd_bus_error *error) {
Session *s = userdata;
- dual_timestamp t;
+ dual_timestamp t = DUAL_TIMESTAMP_NULL;
uint64_t u;
int r;
return sd_bus_message_append(reply, "t", u);
}
-int bus_session_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_session_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
int r;
- assert(bus);
assert(message);
assert(s);
message,
CAP_KILL,
"org.freedesktop.login1.manage",
+ NULL,
false,
s->user->uid,
&s->manager->polkit_registry,
return sd_bus_reply_method_return(message, NULL);
}
-int bus_session_method_activate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
int r;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-int bus_session_method_lock(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_session_method_lock(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
int r;
- assert(bus);
assert(message);
assert(s);
message,
CAP_SYS_ADMIN,
"org.freedesktop.login1.lock-sessions",
+ NULL,
false,
s->user->uid,
&s->manager->polkit_registry,
return sd_bus_reply_method_return(message, NULL);
}
-static int method_set_idle_hint(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_set_idle_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
Session *s = userdata;
uid_t uid;
int r, b;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-int bus_session_method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
const char *swho;
int32_t signo;
KillWho who;
int r;
- assert(bus);
assert(message);
assert(s);
message,
CAP_KILL,
"org.freedesktop.login1.manage",
+ NULL,
false,
s->user->uid,
&s->manager->polkit_registry,
return sd_bus_reply_method_return(message, NULL);
}
-static int method_take_control(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_take_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
Session *s = userdata;
int r, force;
uid_t uid;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_release_control(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_release_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_take_device(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
uint32_t major, minor;
SessionDevice *sd;
dev_t dev;
int r;
- assert(bus);
assert(message);
assert(s);
return r;
}
-static int method_release_device(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
uint32_t major, minor;
SessionDevice *sd;
dev_t dev;
int r;
- assert(bus);
assert(message);
assert(s);
return sd_bus_reply_method_return(message, NULL);
}
-static int method_pause_device_complete(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+static int method_pause_device_complete(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = userdata;
uint32_t major, minor;
SessionDevice *sd;
dev_t dev;
int r;
- assert(bus);
assert(message);
assert(s);
if (!s->create_message)
return 0;
+/// elogind does not support scope and service jobs
+#if 0
+ if (!sd_bus_error_is_set(error) && (s->scope_job || s->user->service_job))
+ return 0;
+#endif // 0
+
c = s->create_message;
s->create_message = NULL;
#include "audit.h"
#include "bus-util.h"
#include "bus-error.h"
-#include "cgroup-util.h"
-#include "def.h"
#include "logind-session.h"
+#include "formats-util.h"
+#include "terminal-util.h"
+
+// #define RELEASE_USEC (20*USEC_PER_SEC)
static void session_remove_fifo(Session *s);
LIST_REMOVE(sessions_by_seat, s->seat->sessions, s);
}
+ if (s->scope) {
+ hashmap_remove(s->manager->session_units, s->scope);
+ free(s->scope);
+ }
+
+/// elogind does not support systemd scope_jobs
+#if 0
+ free(s->scope_job);
+#endif // 0
+
sd_bus_message_unref(s->create_message);
free(s->tty);
r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0);
if (r < 0)
- goto finish;
+ goto fail;
r = fopen_temporary(s->state_file, &f, &temp_path);
if (r < 0)
- goto finish;
+ goto fail;
assert(s->user);
if (s->class >= 0)
fprintf(f, "CLASS=%s\n", session_class_to_string(s->class));
+ if (s->scope)
+ fprintf(f, "SCOPE=%s\n", s->scope);
+/// elogind does not support systemd scope_jobs
+#if 0
+ if (s->scope_job)
+ fprintf(f, "SCOPE_JOB=%s\n", s->scope_job);
+#endif // 0
+
if (s->fifo_path)
fprintf(f, "FIFO=%s\n", s->fifo_path);
escaped = cescape(s->remote_host);
if (!escaped) {
r = -ENOMEM;
- goto finish;
+ goto fail;
}
fprintf(f, "REMOTE_HOST=%s\n", escaped);
escaped = cescape(s->remote_user);
if (!escaped) {
r = -ENOMEM;
- goto finish;
+ goto fail;
}
fprintf(f, "REMOTE_USER=%s\n", escaped);
escaped = cescape(s->service);
if (!escaped) {
r = -ENOMEM;
- goto finish;
+ goto fail;
}
fprintf(f, "SERVICE=%s\n", escaped);
escaped = cescape(s->desktop);
if (!escaped) {
r = -ENOMEM;
- goto finish;
+ goto fail;
}
fprintf(f, "DESKTOP=%s\n", escaped);
fprintf(f, "VTNR=%u\n", s->vtnr);
if (!s->vtnr)
- fprintf(f, "POS=%u\n", s->pos);
+ fprintf(f, "POSITION=%u\n", s->position);
if (s->leader > 0)
fprintf(f, "LEADER="PID_FMT"\n", s->leader);
if (s->controller)
fprintf(f, "CONTROLLER=%s\n", s->controller);
- fflush(f);
+ r = fflush_and_check(f);
+ if (r < 0)
+ goto fail;
- if (ferror(f) || rename(temp_path, s->state_file) < 0) {
+ if (rename(temp_path, s->state_file) < 0) {
r = -errno;
- unlink(s->state_file);
- unlink(temp_path);
+ goto fail;
}
-finish:
- if (r < 0)
- log_error_errno(r, "Failed to save session data %s: %m", s->state_file);
+ return 0;
- return r;
+fail:
+ (void) unlink(s->state_file);
+
+ if (temp_path)
+ (void) unlink(temp_path);
+
+ return log_error_errno(r, "Failed to save session data %s: %m", s->state_file);
}
+
int session_load(Session *s) {
_cleanup_free_ char *remote = NULL,
*seat = NULL,
*vtnr = NULL,
*state = NULL,
- *pos = NULL,
+ *position = NULL,
*leader = NULL,
*type = NULL,
*class = NULL,
r = parse_env_file(s->state_file, NEWLINE,
"REMOTE", &remote,
+ "SCOPE", &s->scope,
+/// elogind does not support systemd scope_jobs
+#if 0
+ "SCOPE_JOB", &s->scope_job,
+#endif // 0
"FIFO", &s->fifo_path,
"SEAT", &seat,
"TTY", &s->tty,
"DESKTOP", &s->desktop,
"VTNR", &vtnr,
"STATE", &state,
- "POS", &pos,
+ "POSITION", &position,
"LEADER", &leader,
"TYPE", &type,
"CLASS", &class,
if (!s->seat || !seat_has_vts(s->seat))
s->vtnr = 0;
- if (pos && s->seat) {
+ if (position && s->seat) {
unsigned int npos;
- safe_atou(pos, &npos);
+ safe_atou(position, &npos);
seat_claim_position(s->seat, s, npos);
}
return 0;
}
+/// UNNEEDED by elogind
+#if 0
+static int session_start_scope(Session *s) {
+ int r = 0;
+
+ assert(s);
+ assert(s->user);
+ assert(s->user->slice);
+
+ if (!s->scope) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_free_ char *description = NULL;
+ char *scope = NULL; //, *job = NULL;
+
+ description = strjoin("Session ", s->id, " of user ", s->user->name, NULL);
+ if (!description)
+ return log_oom();
+
+ scope = strjoin("session-", s->id, ".scope", NULL);
+ if (!scope)
+ return log_oom();
+
+ r = manager_start_scope(s->manager, scope, s->leader, s->user->slice, description, "logind.service", "systemd-user-sessions.service", &error, &job);
+ if (r < 0) {
+ log_error("Failed to start session scope %s: %s %s",
+ scope, bus_error_message(&error, r), error.name);
+ free(scope);
+ return r;
+ } else {
+ s->scope = scope;
+ free(s->scope_job);
+ s->scope_job = job;
+ }
+ }
+
+ if (s->scope)
+ hashmap_put(s->manager->session_units, s->scope, s);
+
+ return 0;
+}
+#endif // 0
+
static int session_start_cgroup(Session *s) {
int r;
return 0;
}
+
int session_start(Session *s) {
int r;
if (r < 0)
return r;
+ /* Create cgroup */
+/// elogind does its own session management without systemd units,
+/// slices and scopes
+#if 0
+ r = session_start_scope(s);
+#else
r = session_start_cgroup(s);
+#endif // 0
if (r < 0)
return r;
return 0;
}
+/// UNNEEDED by elogind
+#if 0
+static int session_stop_scope(Session *s, bool force) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ char *job = NULL;
+ int r;
+
+ assert(s);
+
+ if (!s->scope)
+ return 0;
+
+ if (force || manager_shall_kill(s->manager, s->user->name)) {
+ r = manager_stop_unit(s->manager, s->scope, &error, &job);
+ if (r < 0) {
+ log_error("Failed to stop session scope: %s", bus_error_message(&error, r));
+ return r;
+ }
+
+ free(s->scope_job);
+ s->scope_job = job;
+ } else {
+ r = manager_abandon_scope(s->manager, s->scope, &error);
+ if (r < 0) {
+ log_error("Failed to abandon session scope: %s", bus_error_message(&error, r));
+ return r;
+ }
+ }
+
+ return 0;
+}
+#endif // 0
+
static int session_stop_cgroup(Session *s, bool force) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
s->timer_event_source = sd_event_source_unref(s->timer_event_source);
+ if (s->seat)
+ seat_evict_position(s->seat, s);
+
/* We are going down, don't care about FIFOs anymore */
session_remove_fifo(s);
/* Kill cgroup */
+/// elogind does not start scopes, but sessions
+#if 0
+ r = session_stop_scope(s, force);
+#else
r = session_stop_cgroup(s, force);
+#endif // 0
s->stopping = true;
}
int session_finalize(Session *s) {
- int r = 0;
SessionDevice *sd;
assert(s);
s->timer_event_source = sd_event_source_unref(s->timer_event_source);
+ if (s->seat)
+ seat_evict_position(s->seat, s);
+
/* Kill session devices */
while ((sd = hashmap_first(s->devices)))
session_device_free(sd);
- unlink(s->state_file);
+ (void) unlink(s->state_file);
session_add_to_gc_queue(s);
user_add_to_gc_queue(s->user);
user_save(s->user);
user_send_changed(s->user, "Sessions", "Display", NULL);
- return r;
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+static int release_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
+ Session *s = userdata;
+
+ assert(es);
+ assert(s);
+
+ session_stop(s, false);
+ return 0;
}
+#endif // 0
int session_release(Session *s) {
assert(s);
/* In systemd, session release is triggered by user jobs
dying. In elogind we don't have that so go ahead and stop
now. */
+#if 0
+ return sd_event_add_time(s->manager->event,
+ &s->timer_event_source,
+ CLOCK_MONOTONIC,
+ now(CLOCK_MONOTONIC) + RELEASE_USEC, 0,
+ release_timeout_callback, s);
+
+#else
return session_stop(s, false);
+#endif // 0
}
bool session_is_active(Session *s) {
if (s->fifo_path) {
unlink(s->fifo_path);
- free(s->fifo_path);
- s->fifo_path = NULL;
+ s->fifo_path = mfree(s->fifo_path);
}
}
return true;
}
- if (cg_is_empty_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, false) > 0)
+/// elogind supports neither scopes nor jobs
+#if 0
+ if (s->scope_job && manager_job_is_active(s->manager, s->scope_job))
+ return true;
+
+ if (s->scope && manager_unit_is_active(s->manager, s->scope))
+ return true;
+#endif // 0
+
+ if ( s->user->manager
+ && (cg_is_empty_recursive (SYSTEMD_CGROUP_CONTROLLER, s->user->manager->cgroup_root) > 0) )
return true;
return false;
if (s->stopping || s->timer_event_source)
return SESSION_CLOSING;
+/// elogind does not support systemd scope_jobs
+#if 0
+ if (s->scope_job || s->fifo_fd < 0)
+#else
if (s->fifo_fd < 0)
+#endif // 0
return SESSION_OPENING;
if (session_is_active(s))
int session_kill(Session *s, KillWho who, int signo) {
assert(s);
+/// Without direct cgroup support, elogind can not kill sessions
+#if 0
+ if (!s->scope)
+ return -ESRCH;
+
+ return manager_kill_unit(s->manager, s->scope, who, signo, NULL);
+#else
if (who == KILL_LEADER) {
if (s->leader <= 0)
return -ESRCH;
return cg_kill_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, signo,
sigcont, ignore_self, rem, NULL);
}
+#endif // 0
}
static int session_open_vt(Session *s) {
return s->vtfd;
sprintf(path, "/dev/tty%u", s->vtnr);
- s->vtfd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
+ s->vtfd = open_terminal(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
if (s->vtfd < 0)
return log_error_errno(errno, "cannot open VT %s of session %s: %m", path, s->id);
int vt, kb = K_XLATE;
struct vt_mode mode = { 0 };
+ /* We need to get a fresh handle to the virtual terminal,
+ * since the old file-descriptor is potentially in a hung-up
+ * state after the controlling process exited; we do a
+ * little dance to avoid having the terminal be available
+ * for reuse before we've cleaned it up.
+ */
+ int old_fd = s->vtfd;
+ s->vtfd = -1;
+
vt = session_open_vt(s);
+ safe_close(old_fd);
+
if (vt < 0)
return;
session_device_free(sd);
s->controller = NULL;
- manager_drop_busname(s->manager, name);
+ s->track = sd_bus_track_unref(s->track);
+}
+
+static int on_bus_track(sd_bus_track *track, void *userdata) {
+ Session *s = userdata;
+
+ assert(track);
+ assert(s);
+
+ session_drop_controller(s);
+
+ return 0;
}
int session_set_controller(Session *s, const char *sender, bool force) {
if (!name)
return -ENOMEM;
- r = manager_watch_busname(s->manager, name);
- if (r)
+ s->track = sd_bus_track_unref(s->track);
+ r = sd_bus_track_new(s->manager->bus, &s->track, on_bus_track, s);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_track_add_name(s->track, name);
+ if (r < 0)
return r;
/* When setting a session controller, we forcibly mute the VT and set
* or reset the VT in case it crashed/exited, too. */
r = session_prepare_vt(s);
if (r < 0) {
- manager_drop_busname(s->manager, name);
+ s->track = sd_bus_track_unref(s->track);
return r;
}
if (!s->controller)
return;
+ s->track = sd_bus_track_unref(s->track);
session_release_controller(s, false);
session_save(s);
session_restore_vt(s);
#include "list.h"
#include "logind-user.h"
-#include "login-shared.h"
+#include "login-util.h"
typedef enum SessionState {
SESSION_OPENING, /* Session scope is being created */
Manager *manager;
const char *id;
- unsigned int pos;
+ unsigned int position;
SessionType type;
SessionClass class;
char *service;
char *desktop;
- /* Always NULL. */
+ /* always NULL */
char *scope;
+/// UNNEEDED (and unsupported) by elogind
+#if 0
+ char *scope_job;
+#endif // 0
Seat *seat;
unsigned int vtnr;
char *controller;
Hashmap *devices;
+ sd_bus_track *track;
LIST_FIELDS(Session, sessions_by_user);
LIST_FIELDS(Session, sessions_by_seat);
int session_set_controller(Session *s, const char *sender, bool force);
void session_drop_controller(Session *s);
-int bus_session_method_activate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
-int bus_session_method_lock(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
-int bus_session_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
-int bus_session_method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_session_method_lock(sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_session_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error);
#include "bus-util.h"
#include "logind.h"
#include "logind-user.h"
+#include "formats-util.h"
static int property_get_display(
sd_bus *bus,
}
- r = sd_bus_message_close_container(reply);
- if (r < 0)
- return r;
-
- return 1;
+ return sd_bus_message_close_container(reply);
}
static int property_get_idle_hint(
sd_bus_error *error) {
User *u = userdata;
- dual_timestamp t;
+ dual_timestamp t = DUAL_TIMESTAMP_NULL;
uint64_t k;
assert(bus);
return sd_bus_message_append(reply, "b", r > 0);
}
-int bus_user_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
User *u = userdata;
int r;
- assert(bus);
assert(message);
assert(u);
message,
CAP_KILL,
"org.freedesktop.login1.manage",
+ NULL,
false,
u->uid,
&u->manager->polkit_registry,
return sd_bus_reply_method_return(message, NULL);
}
-int bus_user_method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
User *u = userdata;
int32_t signo;
int r;
- assert(bus);
assert(message);
assert(u);
message,
CAP_KILL,
"org.freedesktop.login1.manage",
+ NULL,
false,
u->uid,
&u->manager->polkit_registry,
#include "util.h"
#include "mkdir.h"
+#include "rm-rf.h"
#include "hashmap.h"
#include "fileio.h"
#include "path-util.h"
-#include "special.h"
+// #include "special.h"
#include "unit-name.h"
#include "bus-util.h"
#include "bus-error.h"
#include "conf-parser.h"
#include "clean-ipc.h"
-#include "logind-user.h"
#include "smack-util.h"
+#include "formats-util.h"
+#include "label.h"
+#include "logind-user.h"
User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
User *u;
while (u->sessions)
session_free(u->sessions);
+ if (u->slice) {
+ hashmap_remove(u->manager->user_units, u->slice);
+ free(u->slice);
+ }
+
+ if (u->service) {
+ hashmap_remove(u->manager->user_units, u->service);
+ free(u->service);
+ }
+
+/// elogind does not support slice and service jobs
+#if 0
+ free(u->slice_job);
+ free(u->service_job);
+#endif // 0
+
free(u->runtime_path);
hashmap_remove(u->manager->users, UID_TO_PTR(u->uid));
free(u);
}
-int user_save(User *u) {
+static int user_save_internal(User *u) {
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(u);
assert(u->state_file);
- if (!u->started)
- return 0;
-
r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0);
if (r < 0)
- goto finish;
+ goto fail;
r = fopen_temporary(u->state_file, &f, &temp_path);
if (r < 0)
- goto finish;
+ goto fail;
fchmod(fileno(f), 0644);
if (u->runtime_path)
fprintf(f, "RUNTIME=%s\n", u->runtime_path);
+ if (u->service)
+ fprintf(f, "SERVICE=%s\n", u->service);
+/// elogind does not support service jobs
+#if 0
+ if (u->service_job)
+ fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
+#endif // 0
+
+ if (u->slice)
+ fprintf(f, "SLICE=%s\n", u->slice);
+/// elogind does not support slice jobs
+#if 0
+ if (u->slice_job)
+ fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
+#endif // 0
+
if (u->display)
fprintf(f, "DISPLAY=%s\n", u->display->id);
fputc('\n', f);
}
- fflush(f);
+ r = fflush_and_check(f);
+ if (r < 0)
+ goto fail;
- if (ferror(f) || rename(temp_path, u->state_file) < 0) {
+ if (rename(temp_path, u->state_file) < 0) {
r = -errno;
- unlink(u->state_file);
- unlink(temp_path);
+ goto fail;
}
-finish:
- if (r < 0)
- log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
+ return 0;
- return r;
+fail:
+ (void) unlink(u->state_file);
+
+ if (temp_path)
+ (void) unlink(temp_path);
+
+ return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
+}
+
+int user_save(User *u) {
+ assert(u);
+
+ if (!u->started)
+ return 0;
+
+ return user_save_internal (u);
}
int user_load(User *u) {
r = parse_env_file(u->state_file, NEWLINE,
"RUNTIME", &u->runtime_path,
+ "SERVICE", &u->service,
+/// elogind does not support service jobs
+#if 0
+ "SERVICE_JOB", &u->service_job,
+#endif // 0
+ "SLICE", &u->slice,
+/// elogind does not support slice jobs
+#if 0
+ "SLICE_JOB", &u->slice_job,
+#endif // 0
"DISPLAY", &display,
"REALTIME", &realtime,
"MONOTONIC", &monotonic,
} else
p = u->runtime_path;
- if (path_is_mount_point(p, false) <= 0) {
+ if (path_is_mount_point(p, 0) <= 0) {
_cleanup_free_ char *t = NULL;
- (void) mkdir(p, 0700);
+ (void) mkdir_label(p, 0700);
if (mac_smack_use())
r = asprintf(&t, "mode=0700,smackfsroot=*,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu", u->uid, u->gid, u->manager->runtime_dir_size);
goto fail;
}
}
+
+ r = label_fix(p, false, false);
+ if (r < 0)
+ log_warning_errno(r, "Failed to fix label of '%s', ignoring: %m", p);
}
u->runtime_path = p;
return r;
}
+static int user_start_slice(User *u) {
+ // char *job;
+ int r;
+
+ assert(u);
+
+ if (!u->slice) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ char lu[DECIMAL_STR_MAX(uid_t) + 1], *slice;
+ sprintf(lu, UID_FMT, u->uid);
+
+ r = slice_build_subslice("user.slice", lu, &slice);
+ if (r < 0)
+ return r;
+
+/// elogind : Do not try to use dbus to ask systemd
+#if 0
+ r = manager_start_unit(u->manager, slice, &error, &job);
+ if (r < 0) {
+ log_error("Failed to start user slice: %s", bus_error_message(&error, r));
+ free(slice);
+ } else {
+#endif // 0
+ u->slice = slice;
+
+/// elogind does not support slice jobs
+#if 0
+ free(u->slice_job);
+ u->slice_job = job;
+ }
+#endif // 0
+ }
+
+ if (u->slice)
+ hashmap_put(u->manager->user_units, u->slice, u);
+
+ return 0;
+}
+
+static int user_start_service(User *u) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ // char *job;
+ int r;
+
+ assert(u);
+
+ if (!u->service) {
+ char lu[DECIMAL_STR_MAX(uid_t) + 1], *service;
+ sprintf(lu, UID_FMT, u->uid);
+
+ r = unit_name_build("user", lu, ".service", &service);
+ if (r < 0)
+ return log_error_errno(r, "Failed to build service name: %m");
+
+/// elogind : Do not try to use dbus to ask systemd
+#if 0
+ r = manager_start_unit(u->manager, service, &error, &job);
+#endif // 0
+ if (r < 0) {
+ log_error("Failed to start user service: %s", bus_error_message(&error, r));
+ free(service);
+ } else {
+ u->service = service;
+
+/// elogind does not support service jobs
+#if 0
+ free(u->service_job);
+ u->service_job = job;
+#endif // 0
+ }
+ }
+
+ if (u->service)
+ hashmap_put(u->manager->user_units, u->service, u);
+
+ return 0;
+}
+
int user_start(User *u) {
int r;
if (r < 0)
return r;
+ /* Create cgroup */
+ r = user_start_slice(u);
+ if (r < 0)
+ return r;
+
+ /* Save the user data so far, because pam_systemd will read the
+ * XDG_RUNTIME_DIR out of it while starting up systemd --user.
+ * We need to do user_save_internal() because we have not
+ * "officially" started yet. */
+ user_save_internal(u);
+
+ /* Spawn user systemd */
+ r = user_start_service(u);
+ if (r < 0)
+ return r;
+
if (!dual_timestamp_is_set(&u->timestamp))
dual_timestamp_get(&u->timestamp);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
+static int user_stop_slice(User *u) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ // char *job;
+ int r = 0;
+
+ assert(u);
+
+ if (!u->slice)
+ return 0;
+
+ r = manager_stop_unit(u->manager, u->slice, &error, &job);
+ if (r < 0) {
+ log_error("Failed to stop user slice: %s", bus_error_message(&error, r));
+ return r;
+ }
+
+ free(u->slice_job);
+ u->slice_job = job;
+
+ return r;
+}
+
+static int user_stop_service(User *u) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ // char *job;
+ int r = 0;
+
+ assert(u);
+
+ if (!u->service)
+ return 0;
+
+ r = manager_stop_unit(u->manager, u->service, &error, &job);
+ if (r < 0) {
+ log_error("Failed to stop user service: %s", bus_error_message(&error, r));
+ return r;
+ }
+
+ free(u->service_job);
+ u->service_job = job;
+
+ return r;
+}
+#endif // 0
+
static int user_remove_runtime_path(User *u) {
int r;
if (!u->runtime_path)
return 0;
- r = rm_rf(u->runtime_path, false, false, false);
+ r = rm_rf(u->runtime_path, 0);
if (r < 0)
log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
if (r < 0 && errno != EINVAL && errno != ENOENT)
log_error_errno(errno, "Failed to unmount user runtime directory %s: %m", u->runtime_path);
- r = rm_rf(u->runtime_path, false, true, false);
+ r = rm_rf(u->runtime_path, REMOVE_ROOT);
if (r < 0)
log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
- free(u->runtime_path);
- u->runtime_path = NULL;
+ u->runtime_path = mfree(u->runtime_path);
return r;
}
r = k;
}
+ /* Kill systemd */
+/// elogind does not support service or slice jobs
+#if 0
+ k = user_stop_service(u);
+ if (k < 0)
+ r = k;
+
+ /* Kill cgroup */
+ k = user_stop_slice(u);
+ if (k < 0)
+ r = k;
+#endif // 0
+
u->stopping = true;
user_save(u);
int user_get_idle_hint(User *u, dual_timestamp *t) {
Session *s;
bool idle_hint = true;
- dual_timestamp ts = { 0, 0 };
+ dual_timestamp ts = DUAL_TIMESTAMP_NULL;
assert(u);
if (user_check_linger_file(u) > 0)
return true;
+/// elogind does not support systemd services and slices
+#if 0
+ if (u->slice_job && manager_job_is_active(u->manager, u->slice_job))
+ return true;
+
+ if (u->service_job && manager_job_is_active(u->manager, u->service_job))
+ return true;
+#endif // 0
+
return false;
}
if (u->stopping)
return USER_CLOSING;
+/// elogind does not support slice and service jobs
+#if 0
+ if (!u->started || u->slice_job || u->service_job)
+#else
+ if (!u->started)
+#endif // 0
+ return USER_OPENING;
+
if (u->sessions) {
bool all_closing = true;
}
int user_kill(User *u, int signo) {
+/// Without systemd unit support, elogind has to rely on its session system
+#if 0
+ assert(u);
+
+ if (!u->slice)
+ return -ESRCH;
+
+ return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
+#else
Session *s;
int res = 0;
}
return res;
+#endif // 0
+}
+
+static bool elect_display_filter(Session *s) {
+ /* Return true if the session is a candidate for the user’s ‘primary
+ * session’ or ‘display’. */
+ assert(s);
+
+ return (s->class == SESSION_USER && !s->stopping);
+}
+
+static int elect_display_compare(Session *s1, Session *s2) {
+ /* Indexed by SessionType. Lower numbers mean more preferred. */
+ const int type_ranks[_SESSION_TYPE_MAX] = {
+ [SESSION_UNSPECIFIED] = 0,
+ [SESSION_TTY] = -2,
+ [SESSION_X11] = -3,
+ [SESSION_WAYLAND] = -3,
+ [SESSION_MIR] = -3,
+ [SESSION_WEB] = -1,
+ };
+
+ /* Calculate the partial order relationship between s1 and s2,
+ * returning < 0 if s1 is preferred as the user’s ‘primary session’,
+ * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
+ * is preferred.
+ *
+ * s1 or s2 may be NULL. */
+ if (!s1 && !s2)
+ return 0;
+
+ if ((s1 == NULL) != (s2 == NULL))
+ return (s1 == NULL) - (s2 == NULL);
+
+ if (s1->stopping != s2->stopping)
+ return s1->stopping - s2->stopping;
+
+ if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
+ return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
+
+ if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
+ return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
+
+ if (s1->type != s2->type)
+ return type_ranks[s1->type] - type_ranks[s2->type];
+
+ return 0;
}
void user_elect_display(User *u) {
- Session *graphical = NULL, *text = NULL, *other = NULL, *s;
+ Session *s;
assert(u);
/* This elects a primary session for each user, which we call
* the "display". We try to keep the assignment stable, but we
* "upgrade" to better choices. */
+ log_debug("Electing new display for user %s", u->name);
LIST_FOREACH(sessions_by_user, s, u->sessions) {
-
- if (s->class != SESSION_USER)
+ if (!elect_display_filter(s)) {
+ log_debug("Ignoring session %s", s->id);
continue;
+ }
- if (s->stopping)
- continue;
-
- if (SESSION_TYPE_IS_GRAPHICAL(s->type))
- graphical = s;
- else if (s->type == SESSION_TTY)
- text = s;
- else
- other = s;
- }
-
- if (graphical &&
- (!u->display ||
- u->display->class != SESSION_USER ||
- u->display->stopping ||
- !SESSION_TYPE_IS_GRAPHICAL(u->display->type))) {
- u->display = graphical;
- return;
- }
-
- if (text &&
- (!u->display ||
- u->display->class != SESSION_USER ||
- u->display->stopping ||
- u->display->type != SESSION_TTY)) {
- u->display = text;
- return;
+ if (elect_display_compare(s, u->display) < 0) {
+ log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
+ u->display = s;
+ }
}
-
- if (other &&
- (!u->display ||
- u->display->class != SESSION_USER ||
- u->display->stopping))
- u->display = other;
}
static const char* const user_state_table[_USER_STATE_MAX] = {
char *service;
char *slice;
+/// UNNEEDED (and unsupported) by elogind
+#if 0
+ char *service_job;
+ char *slice_job;
+#endif // 0
+
Session *display;
dual_timestamp timestamp;
const char* user_state_to_string(UserState s) _const_;
UserState user_state_from_string(const char *s) _pure_;
-int bus_user_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
-int bus_user_method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error);
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2015 Daniel Mack
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <pwd.h>
+
+#include "sd-messages.h"
+#include "strv.h"
+// #include "special.h"
+#include "unit-name.h"
+#include "audit.h"
+#include "bus-util.h"
+#include "bus-error.h"
+#include "bus-common-errors.h"
+#include "logind.h"
+#include "formats-util.h"
+#include "utmp-wtmp.h"
+
+_const_ static usec_t when_wall(usec_t n, usec_t elapse) {
+
+ usec_t left;
+ unsigned int i;
+ static const int wall_timers[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
+ 25, 40, 55, 70, 100, 130, 150, 180,
+ };
+
+ /* If the time is already passed, then don't announce */
+ if (n >= elapse)
+ return 0;
+
+ left = elapse - n;
+
+ for (i = 1; i < ELEMENTSOF(wall_timers); i++)
+ if (wall_timers[i] * USEC_PER_MINUTE >= left)
+ return left - wall_timers[i-1] * USEC_PER_MINUTE;
+
+ return left % USEC_PER_HOUR;
+}
+
+bool logind_wall_tty_filter(const char *tty, void *userdata) {
+
+ Manager *m = userdata;
+
+ assert(m);
+
+ if (!startswith(tty, "/dev/"))
+ return true;
+
+ return !streq(tty + 5, m->scheduled_shutdown_tty);
+}
+
+static int warn_wall(Manager *m, usec_t n) {
+ char date[FORMAT_TIMESTAMP_MAX] = {};
+ _cleanup_free_ char *l = NULL;
+ usec_t left;
+ int r;
+
+ assert(m);
+
+ if (!m->enable_wall_messages)
+ return 0;
+
+ left = m->scheduled_shutdown_timeout > n;
+
+ r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
+ strempty(m->wall_message),
+ isempty(m->wall_message) ? "" : "\n",
+ m->scheduled_shutdown_type,
+ left ? "at " : "NOW",
+ left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
+ if (r < 0) {
+ log_oom();
+ return 0;
+ }
+
+ utmp_wall(l, lookup_uid(m->scheduled_shutdown_uid),
+ m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
+
+ return 1;
+}
+
+static int wall_message_timeout_handler(
+ sd_event_source *s,
+ uint64_t usec,
+ void *userdata) {
+
+ Manager *m = userdata;
+ usec_t n, next;
+ int r;
+
+ assert(m);
+ assert(s == m->wall_message_timeout_source);
+
+ n = now(CLOCK_REALTIME);
+
+ r = warn_wall(m, n);
+ if (r == 0)
+ return 0;
+
+ next = when_wall(n, m->scheduled_shutdown_timeout);
+ if (next > 0) {
+ r = sd_event_source_set_time(s, n + next);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_time() failed. %m");
+
+ r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
+ }
+
+ return 0;
+}
+
+int manager_setup_wall_message_timer(Manager *m) {
+
+ usec_t n, elapse;
+ int r;
+
+ assert(m);
+
+ n = now(CLOCK_REALTIME);
+ elapse = m->scheduled_shutdown_timeout;
+
+ /* wall message handling */
+
+ if (isempty(m->scheduled_shutdown_type)) {
+ warn_wall(m, n);
+ return 0;
+ }
+
+ if (elapse < n)
+ return 0;
+
+ /* Warn immediately if less than 15 minutes are left */
+ if (elapse - n < 15 * USEC_PER_MINUTE) {
+ r = warn_wall(m, n);
+ if (r == 0)
+ return 0;
+ }
+
+ elapse = when_wall(n, elapse);
+ if (elapse == 0)
+ return 0;
+
+ if (m->wall_message_timeout_source) {
+ r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_time() failed. %m");
+
+ r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
+ } else {
+ r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
+ CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
+ if (r < 0)
+ return log_error_errno(r, "sd_event_add_time() failed. %m");
+ }
+
+ return 0;
+}
#include <string.h>
#include <unistd.h>
-#include "label.h"
#include "sd-daemon.h"
#include "strv.h"
-#include "cgroup-util.h"
#include "conf-parser.h"
#include "bus-util.h"
#include "bus-error.h"
-#include "logind.h"
#include "udev-util.h"
+#include "formats-util.h"
+#include "signal-util.h"
+#include "label.h"
+#include "logind.h"
+#include "cgroup.h"
+#include "mount-setup.h"
+#include "virt.h"
-Manager *manager_new(void) {
+static void manager_free(Manager *m);
+
+static Manager *manager_new(void) {
Manager *m;
int r;
if (!m)
return NULL;
+ m->pin_cgroupfs_fd = -1;
+
m->console_active_fd = -1;
+/// elogind does not support autospawning of vts
+#if 0
+ m->reserve_vt_fd = -1;
+
+ m->n_autovts = 6;
+ m->reserve_vt = 6;
+#endif // 0
m->remove_ipc = true;
m->inhibit_delay_max = 5 * USEC_PER_SEC;
m->handle_power_key = HANDLE_POWEROFF;
m->inhibitors = hashmap_new(&string_hash_ops);
m->buttons = hashmap_new(&string_hash_ops);
- m->busnames = set_new(&string_hash_ops);
+ m->user_units = hashmap_new(&string_hash_ops);
+ m->session_units = hashmap_new(&string_hash_ops);
- if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->busnames)
+ m->running_as = MANAGER_SYSTEM;
+ m->test_run = false;
+
+ if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
goto fail;
m->kill_exclude_users = strv_new("root", NULL);
if (!m->kill_exclude_users)
goto fail;
+ /* If elogind should be its own controller, mount its cgroup */
+ if (streq(SYSTEMD_CGROUP_CONTROLLER, "name=elogind")) {
+ r = mount_setup(true);
+ if (r < 0)
+ goto fail;
+ }
+
+ /* Make cgroups */
+ r = manager_setup_cgroup(m);
+ if (r < 0)
+ goto fail;
+
m->suspend_mode = NULL;
m->suspend_state = strv_new("mem", "standby", "freeze", NULL);
if (!m->suspend_state)
sd_event_set_watchdog(m->event, true);
-
return m;
fail:
return NULL;
}
-void manager_free(Manager *m) {
+static void manager_free(Manager *m) {
Session *session;
User *u;
Device *d;
hashmap_free(m->inhibitors);
hashmap_free(m->buttons);
- set_free_free(m->busnames);
+ hashmap_free(m->user_units);
+ hashmap_free(m->session_units);
sd_event_source_unref(m->idle_action_event_source);
+ sd_event_source_unref(m->inhibit_timeout_source);
+ sd_event_source_unref(m->scheduled_shutdown_timeout_source);
+ sd_event_source_unref(m->nologin_timeout_source);
+ sd_event_source_unref(m->wall_message_timeout_source);
sd_event_source_unref(m->console_active_event_source);
sd_event_source_unref(m->udev_seat_event_source);
safe_close(m->console_active_fd);
- if (m->udev_seat_monitor)
udev_monitor_unref(m->udev_seat_monitor);
- if (m->udev_device_monitor)
udev_monitor_unref(m->udev_device_monitor);
- if (m->udev_vcsa_monitor)
udev_monitor_unref(m->udev_vcsa_monitor);
- if (m->udev_button_monitor)
udev_monitor_unref(m->udev_button_monitor);
- if (m->udev)
udev_unref(m->udev);
+ if (m->unlink_nologin)
+ (void) unlink("/run/nologin");
+
bus_verify_polkit_async_registry_free(m->polkit_registry);
sd_bus_unref(m->bus);
sd_event_unref(m->event);
+/// elogind does not support autospawning of vts
+#if 0
+ safe_close(m->reserve_vt_fd);
+#endif // 0
+
+ /* Avoid the creation of new processes forked by the
+ * kernel; at this point, we will not listen to the
+ * signals anyway */
+ if (detect_container(NULL) <= 0)
+ (void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
+
+ manager_shutdown_cgroup(m, true);
+
strv_free(m->kill_only_users);
strv_free(m->kill_exclude_users);
+ free(m->scheduled_shutdown_type);
+ free(m->scheduled_shutdown_tty);
+ free(m->wall_message);
+
strv_free(m->suspend_mode);
strv_free(m->suspend_state);
strv_free(m->hibernate_mode);
return 0;
}
+/// UNNEEDED by elogind
+#if 0
+static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+ _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+ Manager *m = userdata;
+ const char *name;
+
+ assert(m);
+
+ d = udev_monitor_receive_device(m->udev_vcsa_monitor);
+ if (!d)
+ return -ENOMEM;
+
+ name = udev_device_get_sysname(d);
+
+ /* Whenever a VCSA device is removed try to reallocate our
+ * VTs, to make sure our auto VTs never go away. */
+
+ if (name && startswith(name, "vcsa") && streq_ptr(udev_device_get_action(d), "remove"))
+ seat_preallocate_vts(m->seat0);
+
+ return 0;
+}
+#endif // 0
+
static int manager_dispatch_button_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
Manager *m = userdata;
return 0;
}
-static int signal_agent_released(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+/// UNNEEDED by elogind
+#if 0
+static int manager_reserve_vt(Manager *m) {
+ _cleanup_free_ char *p = NULL;
+
+ assert(m);
+
+ if (m->reserve_vt <= 0)
+ return 0;
+
+ if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0)
+ return log_oom();
+
+ m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (m->reserve_vt_fd < 0) {
+
+ /* Don't complain on VT-less systems */
+ if (errno != ENOENT)
+ log_warning_errno(errno, "Failed to pin reserved VT: %m");
+ return -errno;
+ }
+
+ return 0;
+}
+#endif // 0
+
+static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
Session *s;
const char *cgroup;
int r;
- assert(bus);
assert(message);
assert(m);
if (r < 0)
return log_error_errno(r, "Failed to add manager object vtable: %m");
+ /* elogind relies on signals from its release agent */
r = sd_bus_add_match(m->bus, NULL,
"type='signal',"
- "interface='org.freedesktop.systemd1.Agent',"
+ "interface='org.freedesktop.elogind.Agent',"
"member='Released',"
- "path='/org/freedesktop/systemd1/agent'",
+ "path='/org/freedesktop/elogind/agent'",
signal_agent_released, m);
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add user enumerator: %m");
+/// elogind does not support systemd as PID 1
+#if 0
+ r = sd_bus_add_match(m->bus,
+ NULL,
+ "type='signal',"
+ "sender='org.freedesktop.systemd1',"
+ "interface='org.freedesktop.systemd1.Manager',"
+ "member='JobRemoved',"
+ "path='/org/freedesktop/systemd1'",
+ match_job_removed, m);
+ if (r < 0)
+ log_warning_errno(r, "Failed to add match for JobRemoved: %m");
+
+ r = sd_bus_add_match(m->bus,
+ NULL,
+ "type='signal',"
+ "sender='org.freedesktop.systemd1',"
+ "interface='org.freedesktop.systemd1.Manager',"
+ "member='UnitRemoved',"
+ "path='/org/freedesktop/systemd1'",
+ match_unit_removed, m);
+ if (r < 0)
+ log_warning_errno(r, "Failed to add match for UnitRemoved: %m");
+
+ r = sd_bus_add_match(m->bus,
+ NULL,
+ "type='signal',"
+ "sender='org.freedesktop.systemd1',"
+ "interface='org.freedesktop.DBus.Properties',"
+ "member='PropertiesChanged'",
+ match_properties_changed, m);
+ if (r < 0)
+ log_warning_errno(r, "Failed to add match for PropertiesChanged: %m");
+
+ r = sd_bus_add_match(m->bus,
+ NULL,
+ "type='signal',"
+ "sender='org.freedesktop.systemd1',"
+ "interface='org.freedesktop.systemd1.Manager',"
+ "member='Reloading',"
+ "path='/org/freedesktop/systemd1'",
+ match_reloading, m);
+ if (r < 0)
+ log_warning_errno(r, "Failed to add match for Reloading: %m");
+
+ r = sd_bus_call_method(
+ m->bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "Subscribe",
+ &error,
+ NULL, NULL);
+ if (r < 0)
+ log_notice("Failed to enable subscription: %s", bus_error_message(&error, r));
+#endif // 0
+
r = sd_bus_request_name(m->bus, "org.freedesktop.login1", 0);
if (r < 0)
return log_error_errno(r, "Failed to register name: %m");
return 0;
}
-static int manager_setup_cgroup(Manager *m) {
- _cleanup_free_ char *path = NULL;
- int r;
-
- assert(m);
-
- /* 1. Determine hierarchy */
- free(m->cgroup_root);
- m->cgroup_root = NULL;
-
- r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root);
- if (r < 0)
- return log_error_errno(r, "Cannot determine cgroup we are running in: %m");
-
- /* Make sure to store away the root value without trailing
- * slash, even for the root dir, so that we can easily prepend
- * it everywhere. */
- if (streq(m->cgroup_root, "/"))
- m->cgroup_root[0] = 0;
-
- /* 2. Show data */
- r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, NULL, &path);
- if (r < 0)
- return log_error_errno(r, "Cannot find cgroup mount point: %m");
-
- log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path);
-
- /* 3. Install agent */
- r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, SYSTEMD_CGROUP_AGENT_PATH);
- if (r < 0)
- log_warning_errno(r, "Failed to install release agent, ignoring: %m");
- else if (r > 0)
- log_debug("Installed release agent.");
- else
- log_debug("Release agent already installed.");
-
- /* 4. Make sure we are in the root cgroup */
- r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, 0);
- if (r < 0)
- return log_error_errno(r, "Failed to create root cgroup hierarchy: %m");
-
- /* 5. And pin it, so that it cannot be unmounted */
- safe_close(m->pin_cgroupfs_fd);
-
- m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
- if (m->pin_cgroupfs_fd < 0)
- return log_error_errno(errno, "Failed to open pin file: %m");
-
- return 0;
-}
-
static int manager_connect_console(Manager *m) {
int r;
return -EINVAL;
}
- r = ignore_signals(SIGRTMIN + 1, -1);
- if (r < 0)
- return log_error_errno(r, "Cannot ignore SIGRTMIN + 1: %m");
-
- r = sigprocmask_many(SIG_BLOCK, SIGRTMIN, -1);
- if (r < 0)
- return log_error_errno(r, "Cannot block SIGRTMIN: %m");
+ assert_se(ignore_signals(SIGRTMIN + 1, -1) >= 0);
+ assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN, -1) >= 0);
r = sd_event_add_signal(m->event, NULL, SIGRTMIN, manager_vt_switch, m);
if (r < 0)
return r;
}
+ /* Don't bother watching VCSA devices, if nobody cares */
+/// elogind does not support autospawning of vts
+#if 0
+ if (m->n_autovts > 0 && m->console_active_fd >= 0) {
+
+ m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
+ if (!m->udev_vcsa_monitor)
+ return -ENOMEM;
+
+ r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_vcsa_monitor, "vc", NULL);
+ if (r < 0)
+ return r;
+
+ r = udev_monitor_enable_receiving(m->udev_vcsa_monitor);
+ if (r < 0)
+ return r;
+
+ r = sd_event_add_io(m->event, &m->udev_vcsa_event_source, udev_monitor_get_fd(m->udev_vcsa_monitor), EPOLLIN, manager_dispatch_vcsa_udev, m);
+ if (r < 0)
+ return r;
+ }
+#endif // 0
+
return 0;
}
-void manager_gc(Manager *m, bool drop_not_started) {
+static void manager_gc(Manager *m, bool drop_not_started) {
Seat *seat;
Session *session;
User *user;
session_get_state(session) != SESSION_CLOSING)
session_stop(session, false);
- /* Normally, this should make the session busy again,
- * if it doesn't then let's get rid of it
+ /* Normally, this should make the session referenced
+ * again, if it doesn't then let's get rid of it
* immediately */
if (!session_check_gc(session, drop_not_started)) {
session_finalize(session);
return 0;
}
-int manager_startup(Manager *m) {
+static int manager_startup(Manager *m) {
int r;
Seat *seat;
Session *session;
assert(m);
- /* Make cgroups */
- r = manager_setup_cgroup(m);
- if (r < 0)
- return r;
-
/* Connect to console */
r = manager_connect_console(m);
if (r < 0)
/* Remove stale objects before we start them */
manager_gc(m, false);
+ /* Reserve the special reserved VT */
+/// elogind does not support autospawning of vts
+#if 0
+ manager_reserve_vt(m);
+#endif // 0
+
/* And start everything */
HASHMAP_FOREACH(seat, m->seats, i)
seat_start(seat);
return 0;
}
-int manager_run(Manager *m) {
+static int manager_run(Manager *m) {
int r;
assert(m);
for (;;) {
- usec_t us = (uint64_t) -1;
-
r = sd_event_get_state(m->event);
if (r < 0)
return r;
manager_gc(m, true);
- if (manager_dispatch_delayed(m) > 0)
+ r = manager_dispatch_delayed(m, false);
+ if (r < 0)
+ return r;
+ if (r > 0)
continue;
- if (m->action_what != 0) {
- usec_t x, y;
-
- x = now(CLOCK_MONOTONIC);
- y = m->action_timestamp + m->inhibit_delay_max;
-
- us = x >= y ? 0 : y - x;
- }
-
- r = sd_event_run(m->event, us);
+ r = sd_event_run(m->event, (uint64_t) -1);
if (r < 0)
return r;
}
}
static int manager_parse_config_file(Manager *m) {
- const char *unit, *logind_conf, *sections;
- FILE *file;
- bool relaxed, allow_include, warn;
+ const char *unit = NULL, *logind_conf, *sections;
+ FILE *file = NULL;
+ bool relaxed = false, allow_include = false, warn = true;
assert(m);
- unit = NULL;
+/// elogind parses its own config file
+#if 0
+ return config_parse_many("/etc/systemd/logind.conf",
+ CONF_DIRS_NULSTR("systemd/logind.conf"),
+ "Login\0",
+ config_item_perf_lookup, logind_gperf_lookup,
+ false, m);
+#endif // 0
+
logind_conf = getenv("ELOGIND_CONF_FILE");
if (!logind_conf)
logind_conf = PKGSYSCONFDIR "/logind.conf";
sections = "Login\0Sleep\0";
- file = NULL;
- relaxed = false;
- allow_include = false;
- warn = true;
return config_parse(unit, logind_conf, file, sections,
config_item_perf_lookup, logind_gperf_lookup,
Manager *m = NULL;
int r;
+ elogind_set_program_name(argv[0]);
log_set_target(LOG_TARGET_AUTO);
log_set_facility(LOG_AUTH);
log_parse_environment();
log_open();
+#ifdef ENABLE_DEBUG_ELOGIND
+ log_set_max_level(LOG_DEBUG);
+#endif // ENABLE_DEBUG_ELOGIND
+
umask(0022);
if (argc != 1) {
* existence of /run/systemd/seats/ to determine whether
* logind is available, so please always make sure this check
* stays in. */
- mkdir_label("/run/systemd/seats", 0755);
- mkdir_label("/run/systemd/users", 0755);
- mkdir_label("/run/systemd/sessions", 0755);
- mkdir_label("/run/systemd/machines", 0755);
+ r = mkdir_label("/run/systemd", 0755);
+ if ( (r < 0) && (-EEXIST != r) )
+ return log_error_errno(r, "Failed to create /run/systemd : %m");
+ r = mkdir_label("/run/systemd/seats", 0755);
+ if ( r < 0 && (-EEXIST != r) )
+ return log_error_errno(r, "Failed to create /run/systemd/seats : %m");
+ r = mkdir_label("/run/systemd/users", 0755);
+ if ( r < 0 && (-EEXIST != r) )
+ return log_error_errno(r, "Failed to create /run/systemd/users : %m");
+ r = mkdir_label("/run/systemd/sessions", 0755);
+ if ( r < 0 && (-EEXIST != r) )
+ return log_error_errno(r, "Failed to create /run/systemd/sessions : %m");
+ r = mkdir_label("/run/systemd/machines", 0755);
+ if ( r < 0 && (-EEXIST != r) )
+ return log_error_errno(r, "Failed to create /run/systemd/machines : %m");
m = manager_new();
if (!m) {
"STOPPING=1\n"
"STATUS=Shutting down...");
- if (m)
manager_free(m);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
-# See logind.conf(5) for details.
+# See elogind.conf(5) for details.
[Login]
#KillUserProcesses=no
#include <stdbool.h>
#include <libudev.h>
+#include "config.h"
#include "sd-event.h"
#include "sd-bus.h"
+#include "cgroup-util.h"
+#include "path-lookup.h"
#include "list.h"
#include "hashmap.h"
#include "set.h"
Hashmap *inhibitors;
Hashmap *buttons;
- Set *busnames;
-
LIST_HEAD(Seat, seat_gc_queue);
LIST_HEAD(Session, session_gc_queue);
LIST_HEAD(User, user_gc_queue);
* file system */
int pin_cgroupfs_fd;
+ /* Flags */
+ ManagerRunningAs running_as;
+ bool test_run:1;
+
/* Data specific to the cgroup subsystem */
+ CGroupMask cgroup_supported;
char *cgroup_root;
int console_active_fd;
+/// elogind does not support autospawning of vts
+#if 0
+ unsigned n_autovts;
+
+ unsigned reserve_vt;
+ int reserve_vt_fd;
+#endif // 0
+
Seat *seat0;
char **kill_only_users, **kill_exclude_users;
unsigned long session_counter;
unsigned long inhibit_counter;
+ Hashmap *session_units;
+ Hashmap *user_units;
+
usec_t inhibit_delay_max;
/* If an action is currently being executed or is delayed,
* this is != 0 and encodes what is being done */
InhibitWhat action_what;
+/// elogind does all relevant actions on its own. No systemd jobs and units.
+#if 0
+ /* If a shutdown/suspend was delayed due to a inhibitor this
+ contains the unit name we are supposed to start after the
+ delay is over */
+ const char *action_unit;
+
+ /* If a shutdown/suspend is currently executed, then this is
+ * the job of it */
+ char *action_job;
+#else
/* If a shutdown/suspend was delayed due to a inhibitor this
contains the action we are supposed to perform after the
delay is over */
HandleAction pending_action;
- usec_t action_timestamp;
+#endif // 0
+
+ sd_event_source *inhibit_timeout_source;
+
+ char *scheduled_shutdown_type;
+ usec_t scheduled_shutdown_timeout;
+ sd_event_source *scheduled_shutdown_timeout_source;
+ uid_t scheduled_shutdown_uid;
+ char *scheduled_shutdown_tty;
+ sd_event_source *nologin_timeout_source;
+ bool unlink_nologin;
+
+ char *wall_message;
+ unsigned enable_wall_messages;
+ sd_event_source *wall_message_timeout_source;
sd_event_source *idle_action_event_source;
usec_t idle_action_usec;
bool remove_ipc;
- char **suspend_state, **suspend_mode;
- char **hibernate_state, **hibernate_mode;
+ char **suspend_state, **suspend_mode;
+ char **hibernate_state, **hibernate_mode;
char **hybrid_sleep_state, **hybrid_sleep_mode;
Hashmap *polkit_registry;
size_t runtime_dir_size;
};
-Manager *manager_new(void);
-void manager_free(Manager *m);
-
int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device);
int manager_add_button(Manager *m, const char *name, Button **_button);
int manager_add_seat(Manager *m, const char *id, Seat **_seat);
int manager_process_seat_device(Manager *m, struct udev_device *d);
int manager_process_button_device(Manager *m, struct udev_device *d);
-int manager_startup(Manager *m);
-int manager_run(Manager *m);
-
-void manager_gc(Manager *m, bool drop_not_started);
+// UNNEEDED int manager_spawn_autovt(Manager *m, unsigned int vtnr);
bool manager_shall_kill(Manager *m, const char *user);
int manager_get_user_by_pid(Manager *m, pid_t pid, User **user);
int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session);
-bool manager_is_docked(Manager *m);
-int manager_count_displays(Manager *m);
-bool manager_is_docked_or_multiple_displays(Manager *m);
+bool manager_is_docked_or_external_displays(Manager *m);
extern const sd_bus_vtable manager_vtable[];
+// UNNEEDED int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error);
+// UNNEEDED int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error);
+// UNNEEDED int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error);
+// UNNEEDED int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error);
+// UNNEEDED int match_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error);
+
+/// eloginds own version does the action itself
+#if 0
+int bus_manager_shutdown_or_sleep_now_or_later(Manager *m, const char *unit_name, InhibitWhat w, sd_bus_error *error);
+#else
int bus_manager_shutdown_or_sleep_now_or_later(Manager *m, HandleAction action, InhibitWhat w, sd_bus_error *error);
-int shutdown_or_sleep(Manager *m, HandleAction action);
+#endif // 0
int manager_send_changed(Manager *manager, const char *property, ...) _sentinel_;
-int manager_dispatch_delayed(Manager *manager);
+// UNNEEDED int manager_start_scope(Manager *manager, const char *scope, pid_t pid, const char *slice, const char *description, const char *after, const char *after2, sd_bus_error *error, char **job);
+// UNNEEDED int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job);
+// UNNEEDED int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job);
+// UNNEEDED int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error);
+// UNNEEDED int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error);
+// UNNEEDED int manager_unit_is_active(Manager *manager, const char *unit);
+// UNNEEDED int manager_job_is_active(Manager *manager, const char *path);
/* gperf lookup function */
-const struct ConfigPerfItem* logind_gperf_lookup(const char *key, unsigned length);
-
-int manager_watch_busname(Manager *manager, const char *name);
-void manager_drop_busname(Manager *manager, const char *name);
+const struct ConfigPerfItem* logind_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
int manager_set_lid_switch_ignore(Manager *m, usec_t until);
int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret);
int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret);
int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret);
+
+int manager_setup_wall_message_timer(Manager *m);
+bool logind_wall_tty_filter(const char *tty, void *userdata);
+
+int manager_dispatch_delayed(Manager *manager, bool timeout);
send_interface="org.freedesktop.login1.Manager"
send_member="ActivateSessionOnSeat"/>
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="LockSession"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="UnlockSession"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="LockSessions"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="UnlockSessions"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="KillSession"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="KillUser"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="TerminateSession"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="TerminateUser"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="TerminateSeat"/>
+
<allow send_destination="org.freedesktop.login1"
send_interface="org.freedesktop.login1.Manager"
send_member="PowerOff"/>
send_interface="org.freedesktop.login1.Manager"
send_member="CanHybridSleep"/>
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="ScheduleShutdown"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="CancelScheduledShutdown"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="CanRebootToFirmwareSetup"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="SetRebootToFirmwareSetup"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Manager"
+ send_member="SetWallMessage"/>
+
<allow send_destination="org.freedesktop.login1"
send_interface="org.freedesktop.login1.Manager"
send_member="AttachDevice"/>
send_interface="org.freedesktop.login1.Manager"
send_member="FlushDevices"/>
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Seat"
+ send_member="Terminate"/>
+
<allow send_destination="org.freedesktop.login1"
send_interface="org.freedesktop.login1.Seat"
send_member="ActivateSession"/>
send_interface="org.freedesktop.login1.Seat"
send_member="SwitchToNext"/>
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Session"
+ send_member="Terminate"/>
+
<allow send_destination="org.freedesktop.login1"
send_interface="org.freedesktop.login1.Session"
send_member="Activate"/>
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Session"
+ send_member="Lock"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Session"
+ send_member="Unlock"/>
+
<allow send_destination="org.freedesktop.login1"
send_interface="org.freedesktop.login1.Session"
send_member="SetIdleHint"/>
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.Session"
+ send_member="Kill"/>
+
<allow send_destination="org.freedesktop.login1"
send_interface="org.freedesktop.login1.Session"
send_member="TakeControl"/>
send_interface="org.freedesktop.login1.Session"
send_member="PauseDeviceComplete"/>
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.User"
+ send_member="Terminate"/>
+
+ <allow send_destination="org.freedesktop.login1"
+ send_interface="org.freedesktop.login1.User"
+ send_member="Kill"/>
+
<allow receive_sender="org.freedesktop.login1"/>
</policy>
<allow_inactive>auth_admin_keep</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
+ <annotate key="org.freedesktop.policykit.imply">org.freedesktop.login1.set-wall-message</annotate>
</action>
<action id="org.freedesktop.login1.power-off-multiple-sessions">
<allow_inactive>auth_admin_keep</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
+ <annotate key="org.freedesktop.policykit.imply">org.freedesktop.login1.set-wall-message</annotate>
</action>
<action id="org.freedesktop.login1.reboot-multiple-sessions">
</defaults>
</action>
+ <action id="org.freedesktop.login1.set-reboot-to-firmware-setup">
+ <_description>Allow indication to the firmware to boot to setup interface</_description>
+ <_message>Authentication is required to indicate to the firmware to boot to setup interface.</_message>
+ <defaults>
+ <allow_any>auth_admin_keep</allow_any>
+ <allow_inactive>auth_admin_keep</allow_inactive>
+ <allow_active>auth_admin_keep</allow_active>
+ </defaults>
+ </action>
+
+ <action id="org.freedesktop.login1.set-wall-message">
+ <_description>Set a wall message</_description>
+ <_message>Authentication is required to set a wall message</_message>
+ <defaults>
+ <allow_any>auth_admin_keep</allow_any>
+ <allow_inactive>auth_admin_keep</allow_inactive>
+ <allow_active>auth_admin_keep</allow_active>
+ </defaults>
+ </action>
+
</policyconfig>
#include <security/pam_ext.h>
#include <security/pam_misc.h>
+#include "bus-common-errors.h"
#include "util.h"
#include "audit.h"
#include "macro.h"
#include "socket-util.h"
#include "fileio.h"
#include "bus-error.h"
+#include "formats-util.h"
+#include "terminal-util.h"
+#include "hostname-util.h"
static int parse_argv(
pam_handle_t *handle,
uid_t uid,
const char *runtime) {
-#ifdef ENABLE_KDBUS
_cleanup_free_ char *s = NULL;
- int r;
-
- /* skip export if kdbus is not active */
- if (access("/sys/fs/kdbus", F_OK) < 0)
- return PAM_SUCCESS;
-
- if (asprintf(&s, KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT, uid, runtime) < 0) {
- pam_syslog(handle, LOG_ERR, "Failed to set bus variable.");
- return PAM_BUF_ERR;
+ int r = PAM_BUF_ERR;
+
+ if (is_kdbus_available()) {
+ if (asprintf(&s, KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT, uid, runtime) < 0)
+ goto error;
+ } else {
+ /* FIXME: We *really* should move the access() check into the
+ * daemons that spawn dbus-daemon, instead of forcing
+ * DBUS_SESSION_BUS_ADDRESS= here. */
+
+ s = strjoin(runtime, "/bus", NULL);
+ if (!s)
+ goto error;
+
+ if (access(s, F_OK) < 0)
+ return PAM_SUCCESS;
+
+ s = mfree(s);
+ if (asprintf(&s, UNIX_USER_BUS_ADDRESS_FMT, runtime) < 0)
+ goto error;
}
r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", s, 0);
- if (r != PAM_SUCCESS) {
- pam_syslog(handle, LOG_ERR, "Failed to set bus variable.");
- return r;
- }
-#endif
+ if (r != PAM_SUCCESS)
+ goto error;
+
return PAM_SUCCESS;
+
+error:
+ pam_syslog(handle, LOG_ERR, "Failed to set bus variable.");
+ return r;
}
_public_ PAM_EXTERN int pam_sm_open_session(
*seat = NULL,
*type = NULL, *class = NULL,
*class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL;
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+ _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
int session_fd = -1, existing, r;
bool debug = false, remote;
struct passwd *pw;
/* If this fails vtnr will be 0, that's intended */
if (!isempty(cvtnr))
- safe_atou32(cvtnr, &vtnr);
+ (void) safe_atou32(cvtnr, &vtnr);
if (!isempty(display) && !vtnr) {
if (isempty(seat))
remote_host,
0);
if (r < 0) {
- pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r));
- return PAM_SYSTEM_ERR;
+ if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) {
+ pam_syslog(handle, LOG_DEBUG, "Cannot create session: %s", bus_error_message(&error, r));
+ return PAM_SUCCESS;
+ } else {
+ pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r));
+ return PAM_SYSTEM_ERR;
+ }
}
r = sd_bus_message_read(reply,
int argc, const char **argv) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+ _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
const void *existing = NULL;
const char *id;
int r;
#include "sysfs-show.h"
#include "path-util.h"
#include "udev-util.h"
+#include "terminal-util.h"
static int show_sysfs_one(
struct udev *udev,
***/
#include "macro.h"
-#include "login-shared.h"
+#include "login-util.h"
+#include "musl_missing.h"
static void test_session_id_valid(void) {
assert_se(session_id_valid("c1"));
}
int main(int argc, char* argv[]) {
+ elogind_set_program_name(argv[0]);
log_parse_environment();
log_open();
+++ /dev/null
-../Makefile
\ No newline at end of file
+++ /dev/null
-//-----------------------------------------------------------------------------
-// MurmurHash2 was written by Austin Appleby, and is placed in the public
-// domain. The author hereby disclaims copyright to this source code.
-
-// Note - This code makes a few assumptions about how your machine behaves -
-
-// 1. We can read a 4-byte value from any address without crashing
-// 2. sizeof(int) == 4
-
-// And it has a few limitations -
-
-// 1. It will not work incrementally.
-// 2. It will not produce the same results on little-endian and big-endian
-// machines.
-
-#include "MurmurHash2.h"
-
-//-----------------------------------------------------------------------------
-// Platform-specific functions and macros
-
-// Microsoft Visual Studio
-
-#if defined(_MSC_VER)
-
-#define BIG_CONSTANT(x) (x)
-
-// Other compilers
-
-#else // defined(_MSC_VER)
-
-#define BIG_CONSTANT(x) (x##LLU)
-
-#endif // !defined(_MSC_VER)
-
-//-----------------------------------------------------------------------------
-
-uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed )
-{
- // 'm' and 'r' are mixing constants generated offline.
- // They're not really 'magic', they just happen to work well.
-
- const uint32_t m = 0x5bd1e995;
- const int r = 24;
-
- // Initialize the hash to a 'random' value
-
- uint32_t h = seed ^ len;
-
- // Mix 4 bytes at a time into the hash
-
- const unsigned char * data = (const unsigned char *)key;
-
- while(len >= 4)
- {
- uint32_t k = *(uint32_t*)data;
-
- k *= m;
- k ^= k >> r;
- k *= m;
-
- h *= m;
- h ^= k;
-
- data += 4;
- len -= 4;
- }
-
- // Handle the last few bytes of the input array
-
- switch(len)
- {
- case 3: h ^= data[2] << 16;
- case 2: h ^= data[1] << 8;
- case 1: h ^= data[0];
- h *= m;
- };
-
- // Do a few final mixes of the hash to ensure the last few
- // bytes are well-incorporated.
-
- h ^= h >> 13;
- h *= m;
- h ^= h >> 15;
-
- return h;
-}
+++ /dev/null
-//-----------------------------------------------------------------------------
-// MurmurHash2 was written by Austin Appleby, and is placed in the public
-// domain. The author hereby disclaims copyright to this source code.
-
-#ifndef _MURMURHASH2_H_
-#define _MURMURHASH2_H_
-
-//-----------------------------------------------------------------------------
-// Platform-specific functions and macros
-
-// Microsoft Visual Studio
-
-#if defined(_MSC_VER)
-
-typedef unsigned char uint8_t;
-typedef unsigned long uint32_t;
-typedef unsigned __int64 uint64_t;
-
-// Other compilers
-
-#else // defined(_MSC_VER)
-
-#include <stdint.h>
-
-#endif // !defined(_MSC_VER)
-
-//-----------------------------------------------------------------------------
-
-uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed );
-
-//-----------------------------------------------------------------------------
-
-#endif // _MURMURHASH2_H_
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int calc_acl_mask_if_needed(acl_t *acl_p) {
acl_entry_t i;
int r;
if (tag == ACL_MASK)
return 0;
- if (IN_SET(tag, ACL_USER, ACL_GROUP))
- goto calc;
+
+ if (IN_SET(tag, ACL_USER, ACL_GROUP)) {
+ if (acl_calc_mask(acl_p) < 0)
+ return -errno;
+
+ return 1;
+ }
}
if (r < 0)
return -errno;
- return 0;
-calc:
- if (acl_calc_mask(acl_p) < 0)
- return -errno;
- return 1;
+ return 0;
}
int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
return 0;
}
-int search_acl_groups(char*** dst, const char* path, bool* belong) {
- acl_t acl;
+int acl_search_groups(const char *path, char ***ret_groups) {
+ _cleanup_strv_free_ char **g = NULL;
+ _cleanup_(acl_free) acl_t acl = NULL;
+ bool ret = false;
+ acl_entry_t entry;
+ int r;
assert(path);
- assert(belong);
acl = acl_get_file(path, ACL_TYPE_DEFAULT);
- if (acl) {
- acl_entry_t entry;
- int r;
-
- r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
- while (r > 0) {
- acl_tag_t tag;
- gid_t *gid;
- char *name;
+ if (!acl)
+ return -errno;
- r = acl_get_tag_type(entry, &tag);
- if (r < 0)
- break;
+ r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
+ for (;;) {
+ _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
+ acl_tag_t tag;
+
+ if (r < 0)
+ return -errno;
+ if (r == 0)
+ break;
+
+ if (acl_get_tag_type(entry, &tag) < 0)
+ return -errno;
- if (tag != ACL_GROUP)
- goto next;
+ if (tag != ACL_GROUP)
+ goto next;
- gid = acl_get_qualifier(entry);
- if (!gid)
- break;
+ gid = acl_get_qualifier(entry);
+ if (!gid)
+ return -errno;
- if (in_gid(*gid) > 0) {
- *belong = true;
- break;
- }
+ if (in_gid(*gid) > 0) {
+ if (!ret_groups)
+ return true;
+
+ ret = true;
+ }
+
+ if (ret_groups) {
+ char *name;
name = gid_to_name(*gid);
- if (!name) {
- acl_free(acl);
- return log_oom();
- }
-
- r = strv_consume(dst, name);
- if (r < 0) {
- acl_free(acl);
- return log_oom();
- }
-
- next:
- r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
+ if (!name)
+ return -ENOMEM;
+
+ r = strv_consume(&g, name);
+ if (r < 0)
+ return r;
}
- acl_free(acl);
+ next:
+ r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
}
- return 0;
+ if (ret_groups) {
+ *ret_groups = g;
+ g = NULL;
+ }
+
+ return ret;
}
-int parse_acl(char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
+int parse_acl(const char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
_cleanup_free_ char **a = NULL, **d = NULL; /* strings are not be freed */
_cleanup_strv_free_ char **split;
char **entry;
split = strv_split(text, ",");
if (!split)
- return log_oom();
+ return -ENOMEM;
STRV_FOREACH(entry, split) {
char *p;
r = strv_push(&d, p);
else
r = strv_push(&a, *entry);
+ if (r < 0)
+ return r;
}
- if (r < 0)
- return r;
if (!strv_isempty(a)) {
_cleanup_free_ char *join;
a_acl = acl_from_text(join);
if (!a_acl)
- return -EINVAL;
+ return -errno;
if (want_mask) {
r = calc_acl_mask_if_needed(&a_acl);
d_acl = acl_from_text(join);
if (!d_acl)
- return -EINVAL;
+ return -errno;
if (want_mask) {
r = calc_acl_mask_if_needed(&d_acl);
*acl_access = a_acl;
*acl_default = d_acl;
a_acl = d_acl = NULL;
+
return 0;
}
old = NULL;
return 0;
}
+#endif // 0
#include "macro.h"
int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry);
-int calc_acl_mask_if_needed(acl_t *acl_p);
-int add_base_acls_if_needed(acl_t *acl_p, const char *path);
-int search_acl_groups(char*** dst, const char* path, bool* belong);
-int parse_acl(char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask);
-int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl);
+// UNNEEDED int calc_acl_mask_if_needed(acl_t *acl_p);
+// UNNEEDED int add_base_acls_if_needed(acl_t *acl_p, const char *path);
+// UNNEEDED int acl_search_groups(const char* path, char ***ret_groups);
+// UNNEEDED int parse_acl(const char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask);
+// UNNEEDED int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl);
/* acl_free takes multiple argument types.
* Multiple cleanup functions are necessary. */
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Kay Sievers
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <util.h>
-#include <fileio.h>
-#include <time-util.h>
-#include <acpi-fpdt.h>
-
-struct acpi_table_header {
- char signature[4];
- uint32_t length;
- uint8_t revision;
- uint8_t checksum;
- char oem_id[6];
- char oem_table_id[8];
- uint32_t oem_revision;
- char asl_compiler_id[4];
- uint32_t asl_compiler_revision;
-};
-
-enum {
- ACPI_FPDT_TYPE_BOOT = 0,
- ACPI_FPDT_TYPE_S3PERF = 1,
-};
-
-struct acpi_fpdt_header {
- uint16_t type;
- uint8_t length;
- uint8_t revision;
- uint8_t reserved[4];
- uint64_t ptr;
-};
-
-struct acpi_fpdt_boot_header {
- char signature[4];
- uint32_t length;
-};
-
-enum {
- ACPI_FPDT_S3PERF_RESUME_REC = 0,
- ACPI_FPDT_S3PERF_SUSPEND_REC = 1,
- ACPI_FPDT_BOOT_REC = 2,
-};
-
-struct acpi_fpdt_boot {
- uint16_t type;
- uint8_t length;
- uint8_t revision;
- uint8_t reserved[4];
- uint64_t reset_end;
- uint64_t load_start;
- uint64_t startup_start;
- uint64_t exit_services_entry;
- uint64_t exit_services_exit;
-};
-
-int acpi_get_boot_usec(usec_t *loader_start, usec_t *loader_exit) {
- _cleanup_free_ char *buf = NULL;
- struct acpi_table_header *tbl;
- size_t l = 0;
- struct acpi_fpdt_header *rec;
- int r;
- uint64_t ptr = 0;
- _cleanup_close_ int fd = -1;
- struct acpi_fpdt_boot_header hbrec;
- struct acpi_fpdt_boot brec;
-
- r = read_full_file("/sys/firmware/acpi/tables/FPDT", &buf, &l);
- if (r < 0)
- return r;
-
- if (l < sizeof(struct acpi_table_header) + sizeof(struct acpi_fpdt_header))
- return -EINVAL;
-
- tbl = (struct acpi_table_header *)buf;
- if (l != tbl->length)
- return -EINVAL;
-
- if (memcmp(tbl->signature, "FPDT", 4) != 0)
- return -EINVAL;
-
- /* find Firmware Basic Boot Performance Pointer Record */
- for (rec = (struct acpi_fpdt_header *)(buf + sizeof(struct acpi_table_header));
- (char *)rec < buf + l;
- rec = (struct acpi_fpdt_header *)((char *)rec + rec->length)) {
- if (rec->length <= 0)
- break;
- if (rec->type != ACPI_FPDT_TYPE_BOOT)
- continue;
- if (rec->length != sizeof(struct acpi_fpdt_header))
- continue;
-
- ptr = rec->ptr;
- break;
- }
-
- if (ptr == 0)
- return -EINVAL;
-
- /* read Firmware Basic Boot Performance Data Record */
- fd = open("/dev/mem", O_CLOEXEC|O_RDONLY);
- if (fd < 0)
- return -errno;
-
- l = pread(fd, &hbrec, sizeof(struct acpi_fpdt_boot_header), ptr);
- if (l != sizeof(struct acpi_fpdt_boot_header))
- return -EINVAL;
-
- if (memcmp(hbrec.signature, "FBPT", 4) != 0)
- return -EINVAL;
-
- if (hbrec.length < sizeof(struct acpi_fpdt_boot_header) + sizeof(struct acpi_fpdt_boot))
- return -EINVAL;
-
- l = pread(fd, &brec, sizeof(struct acpi_fpdt_boot), ptr + sizeof(struct acpi_fpdt_boot_header));
- if (l != sizeof(struct acpi_fpdt_boot))
- return -EINVAL;
-
- if (brec.length != sizeof(struct acpi_fpdt_boot))
- return -EINVAL;
-
- if (brec.type != ACPI_FPDT_BOOT_REC)
- return -EINVAL;
-
- if (brec.startup_start == 0 || brec.exit_services_exit < brec.startup_start)
- return -EINVAL;
- if (brec.exit_services_exit > NSEC_PER_HOUR)
- return -EINVAL;
-
- if (loader_start)
- *loader_start = brec.startup_start / 1000;
- if (loader_exit)
- *loader_exit = brec.exit_services_exit / 1000;
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Kay Sievers
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <time-util.h>
-
-int acpi_get_boot_usec(usec_t *loader_start, usec_t *loader_exit);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/socket.h>
-#include <string.h>
-
-#include "util.h"
-#include "af-list.h"
-
-static const struct af_name* lookup_af(register const char *str, register unsigned int len);
-
-#include "af-to-name.h"
-#include "af-from-name.h"
-
-const char *af_to_name(int id) {
-
- if (id <= 0)
- return NULL;
-
- if (id >= (int) ELEMENTSOF(af_names))
- return NULL;
-
- return af_names[id];
-}
-
-int af_from_name(const char *name) {
- const struct af_name *sc;
-
- assert(name);
-
- sc = lookup_af(name, strlen(name));
- if (!sc)
- return AF_UNSPEC;
-
- return sc->id;
-}
-
-int af_max(void) {
- return ELEMENTSOF(af_names);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-const char *af_to_name(int id);
-int af_from_name(const char *name);
-
-int af_max(void);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-#include "util.h"
-#include "fileio.h"
-#include "apparmor-util.h"
-
-bool mac_apparmor_use(void) {
- static int cached_use = -1;
-
- if (cached_use < 0) {
- _cleanup_free_ char *p = NULL;
-
- cached_use =
- read_one_line_file("/sys/module/apparmor/parameters/enabled", &p) >= 0 &&
- parse_boolean(p) > 0;
- }
-
- return cached_use;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/utsname.h>
-
-#include "architecture.h"
-
-int uname_architecture(void) {
-
- /* Return a sanitized enum identifying the architecture we are
- * running on. This is based on uname(), and the user may
- * hence control what this returns by using
- * personality(). This puts the user in control on systems
- * that can run binaries of multiple architectures.
- *
- * We do not translate the string returned by uname()
- * 1:1. Instead we try to clean it up and break down the
- * confusion on x86 and arm in particular.
- *
- * We do not try to distuingish CPUs not CPU features, but
- * actual architectures, i.e. that have genuinely different
- * code. */
-
- static const struct {
- const char *machine;
- int arch;
- } arch_map[] = {
-#if defined(__x86_64__) || defined(__i386__)
- { "x86_64", ARCHITECTURE_X86_64 },
- { "i686", ARCHITECTURE_X86 },
- { "i586", ARCHITECTURE_X86 },
- { "i486", ARCHITECTURE_X86 },
- { "i386", ARCHITECTURE_X86 },
-#elif defined(__powerpc__) || defined(__powerpc64__)
- { "ppc64", ARCHITECTURE_PPC64 },
- { "ppc64le", ARCHITECTURE_PPC64_LE },
- { "ppc", ARCHITECTURE_PPC },
- { "ppcle", ARCHITECTURE_PPC_LE },
-#elif defined(__ia64__)
- { "ia64", ARCHITECTURE_IA64 },
-#elif defined(__hppa__) || defined(__hppa64__)
- { "parisc64", ARCHITECTURE_PARISC64 },
- { "parisc", ARCHITECTURE_PARISC },
-#elif defined(__s390__) || defined(__s390x__)
- { "s390x", ARCHITECTURE_S390X },
- { "s390", ARCHITECTURE_S390 },
-#elif defined(__sparc__) || defined(__sparc64__)
- { "sparc64", ARCHITECTURE_SPARC64 },
- { "sparc", ARCHITECTURE_SPARC },
-#elif defined(__mips__) || defined(__mips64__)
- { "mips64", ARCHITECTURE_MIPS64 },
- { "mips", ARCHITECTURE_MIPS },
-#elif defined(__alpha__)
- { "alpha" , ARCHITECTURE_ALPHA },
-#elif defined(__arm__) || defined(__aarch64__)
- { "aarch64", ARCHITECTURE_ARM64 },
- { "aarch64_be", ARCHITECTURE_ARM64_BE },
- { "armv4l", ARCHITECTURE_ARM },
- { "armv4b", ARCHITECTURE_ARM_BE },
- { "armv4tl", ARCHITECTURE_ARM },
- { "armv4tb", ARCHITECTURE_ARM_BE },
- { "armv5tl", ARCHITECTURE_ARM },
- { "armv5tb", ARCHITECTURE_ARM_BE },
- { "armv5tel", ARCHITECTURE_ARM },
- { "armv5teb" , ARCHITECTURE_ARM_BE },
- { "armv5tejl", ARCHITECTURE_ARM },
- { "armv5tejb", ARCHITECTURE_ARM_BE },
- { "armv6l", ARCHITECTURE_ARM },
- { "armv6b", ARCHITECTURE_ARM_BE },
- { "armv7l", ARCHITECTURE_ARM },
- { "armv7b", ARCHITECTURE_ARM_BE },
- { "armv7ml", ARCHITECTURE_ARM },
- { "armv7mb", ARCHITECTURE_ARM_BE },
- { "armv4l", ARCHITECTURE_ARM },
- { "armv4b", ARCHITECTURE_ARM_BE },
- { "armv4tl", ARCHITECTURE_ARM },
- { "armv4tb", ARCHITECTURE_ARM_BE },
- { "armv5tl", ARCHITECTURE_ARM },
- { "armv5tb", ARCHITECTURE_ARM_BE },
- { "armv5tel", ARCHITECTURE_ARM },
- { "armv5teb", ARCHITECTURE_ARM_BE },
- { "armv5tejl", ARCHITECTURE_ARM },
- { "armv5tejb", ARCHITECTURE_ARM_BE },
- { "armv6l", ARCHITECTURE_ARM },
- { "armv6b", ARCHITECTURE_ARM_BE },
- { "armv7l", ARCHITECTURE_ARM },
- { "armv7b", ARCHITECTURE_ARM_BE },
- { "armv7ml", ARCHITECTURE_ARM },
- { "armv7mb", ARCHITECTURE_ARM_BE },
- { "armv8l", ARCHITECTURE_ARM },
- { "armv8b", ARCHITECTURE_ARM_BE },
-#elif defined(__sh__) || defined(__sh64__)
- { "sh5", ARCHITECTURE_SH64 },
- { "sh2", ARCHITECTURE_SH },
- { "sh2a", ARCHITECTURE_SH },
- { "sh3", ARCHITECTURE_SH },
- { "sh4", ARCHITECTURE_SH },
- { "sh4a", ARCHITECTURE_SH },
-#elif defined(__m68k__)
- { "m68k", ARCHITECTURE_M68K },
-#elif defined(__tilegx__)
- { "tilegx", ARCHITECTURE_TILEGX },
-#elif defined(__cris__)
- { "crisv32", ARCHITECTURE_CRIS },
-#else
-#error "Please register your architecture here!"
-#endif
- };
-
- static int cached = _ARCHITECTURE_INVALID;
- struct utsname u;
- unsigned i;
-
- if (cached != _ARCHITECTURE_INVALID)
- return cached;
-
- assert_se(uname(&u) >= 0);
-
- for (i = 0; i < ELEMENTSOF(arch_map); i++)
- if (streq(arch_map[i].machine, u.machine))
- return cached = arch_map[i].arch;
-
- assert_not_reached("Couldn't identify architecture. You need to patch systemd.");
- return _ARCHITECTURE_INVALID;
-}
-
-static const char *const architecture_table[_ARCHITECTURE_MAX] = {
- [ARCHITECTURE_X86] = "x86",
- [ARCHITECTURE_X86_64] = "x86-64",
- [ARCHITECTURE_PPC] = "ppc",
- [ARCHITECTURE_PPC_LE] = "ppc-le",
- [ARCHITECTURE_PPC64] = "ppc64",
- [ARCHITECTURE_PPC64_LE] = "ppc64-le",
- [ARCHITECTURE_IA64] = "ia64",
- [ARCHITECTURE_PARISC] = "parisc",
- [ARCHITECTURE_PARISC64] = "parisc64",
- [ARCHITECTURE_S390] = "s390",
- [ARCHITECTURE_S390X] = "s390x",
- [ARCHITECTURE_SPARC] = "sparc",
- [ARCHITECTURE_SPARC64] = "sparc64",
- [ARCHITECTURE_MIPS] = "mips",
- [ARCHITECTURE_MIPS_LE] = "mips-le",
- [ARCHITECTURE_MIPS64] = "mips64",
- [ARCHITECTURE_MIPS64_LE] = "mips64-le",
- [ARCHITECTURE_ALPHA] = "alpha",
- [ARCHITECTURE_ARM] = "arm",
- [ARCHITECTURE_ARM_BE] = "arm-be",
- [ARCHITECTURE_ARM64] = "arm64",
- [ARCHITECTURE_ARM64_BE] = "arm64-be",
- [ARCHITECTURE_SH] = "sh",
- [ARCHITECTURE_SH64] = "sh64",
- [ARCHITECTURE_M68K] = "m68k",
- [ARCHITECTURE_TILEGX] = "tilegx",
- [ARCHITECTURE_CRIS] = "cris",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(architecture, int);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <endian.h>
-
-#include "util.h"
-
-/* A cleaned up architecture definition. We don't want to get lost in
- * processor features, models, generations or even ABIs. Hence we
- * focus on general family, and distuignish word width and
- * endianness. */
-
-enum {
- ARCHITECTURE_X86 = 0,
- ARCHITECTURE_X86_64,
- ARCHITECTURE_PPC,
- ARCHITECTURE_PPC_LE,
- ARCHITECTURE_PPC64,
- ARCHITECTURE_PPC64_LE,
- ARCHITECTURE_IA64,
- ARCHITECTURE_PARISC,
- ARCHITECTURE_PARISC64,
- ARCHITECTURE_S390,
- ARCHITECTURE_S390X,
- ARCHITECTURE_SPARC,
- ARCHITECTURE_SPARC64,
- ARCHITECTURE_MIPS,
- ARCHITECTURE_MIPS_LE,
- ARCHITECTURE_MIPS64,
- ARCHITECTURE_MIPS64_LE,
- ARCHITECTURE_ALPHA,
- ARCHITECTURE_ARM,
- ARCHITECTURE_ARM_BE,
- ARCHITECTURE_ARM64,
- ARCHITECTURE_ARM64_BE,
- ARCHITECTURE_SH,
- ARCHITECTURE_SH64,
- ARCHITECTURE_M68K,
- ARCHITECTURE_TILEGX,
- ARCHITECTURE_CRIS,
- _ARCHITECTURE_MAX,
- _ARCHITECTURE_INVALID = -1
-};
-
-int uname_architecture(void);
-
-/*
- * LIB_ARCH_TUPLE should resolve to the local library path
- * architecture tuple systemd is built for, according to the Debian
- * tuple list:
- *
- * https://wiki.debian.org/Multiarch/Tuples
- *
- * This is used in library search paths that should understand
- * Debian's paths on all distributions.
- */
-
-#if defined(__x86_64__)
-# define native_architecture() ARCHITECTURE_X86_64
-# define LIB_ARCH_TUPLE "x86_64-linux-gnu"
-#elif defined(__i386__)
-# define native_architecture() ARCHITECTURE_X86
-# define LIB_ARCH_TUPLE "i386-linux-gnu"
-#elif defined(__powerpc64__)
-# if __BYTE_ORDER == __BIG_ENDIAN
-# define native_architecture() ARCHITECTURE_PPC64
-# define LIB_ARCH_TUPLE "ppc64-linux-gnu"
-# else
-# define native_architecture() ARCHITECTURE_PPC64_LE
-# define LIB_ARCH_TUPLE "powerpc64le-linux-gnu"
-# endif
-#elif defined(__powerpc__)
-# if __BYTE_ORDER == __BIG_ENDIAN
-# define native_architecture() ARCHITECTURE_PPC
-# define LIB_ARCH_TUPLE "powerpc-linux-gnu"
-# else
-# define native_architecture() ARCHITECTURE_PPC_LE
-# error "Missing LIB_ARCH_TUPLE for PPCLE"
-# endif
-#elif defined(__ia64__)
-# define native_architecture() ARCHITECTURE_IA64
-# define LIB_ARCH_TUPLE "ia64-linux-gnu"
-#elif defined(__hppa64__)
-# define native_architecture() ARCHITECTURE_PARISC64
-# error "Missing LIB_ARCH_TUPLE for HPPA64"
-#elif defined(__hppa__)
-# define native_architecture() ARCHITECTURE_PARISC
-# define LIB_ARCH_TUPLE "hppa‑linux‑gnu"
-#elif defined(__s390x__)
-# define native_architecture() ARCHITECTURE_S390X
-# define LIB_ARCH_TUPLE "s390x-linux-gnu"
-#elif defined(__s390__)
-# define native_architecture() ARCHITECTURE_S390
-# define LIB_ARCH_TUPLE "s390-linux-gnu"
-#elif defined(__sparc64__)
-# define native_architecture() ARCHITECTURE_SPARC64
-# define LIB_ARCH_TUPLE "sparc64-linux-gnu"
-#elif defined(__sparc__)
-# define native_architecture() ARCHITECTURE_SPARC
-# define LIB_ARCH_TUPLE "sparc-linux-gnu"
-#elif defined(__mips64__)
-# if __BYTE_ORDER == __BIG_ENDIAN
-# define native_architecture() ARCHITECTURE_MIPS64
-# error "Missing LIB_ARCH_TUPLE for MIPS64"
-# else
-# define native_architecture() ARCHITECTURE_MIPS64_LE
-# error "Missing LIB_ARCH_TUPLE for MIPS64_LE"
-# endif
-#elif defined(__mips__)
-# if __BYTE_ORDER == __BIG_ENDIAN
-# define native_architecture() ARCHITECTURE_MIPS
-# define LIB_ARCH_TUPLE "mips-linux-gnu"
-# else
-# define native_architecture() ARCHITECTURE_MIPS_LE
-# define LIB_ARCH_TUPLE "mipsel-linux-gnu"
-# endif
-#elif defined(__alpha__)
-# define native_architecture() ARCHITECTURE_ALPHA
-# define LIB_ARCH_TUPLE "alpha-linux-gnu"
-#elif defined(__aarch64__)
-# if __BYTE_ORDER == __BIG_ENDIAN
-# define native_architecture() ARCHITECTURE_ARM64_BE
-# define LIB_ARCH_TUPLE "aarch64_be-linux-gnu"
-# else
-# define native_architecture() ARCHITECTURE_ARM64
-# define LIB_ARCH_TUPLE "aarch64-linux-gnu"
-# endif
-#elif defined(__arm__)
-# if __BYTE_ORDER == __BIG_ENDIAN
-# define native_architecture() ARCHITECTURE_ARM_BE
-# if defined(__ARM_EABI__)
-# if defined(__ARM_PCS_VFP)
-# define LIB_ARCH_TUPLE "armeb-linux-gnueabihf"
-# else
-# define LIB_ARCH_TUPLE "armeb-linux-gnueabi"
-# endif
-# else
-# define LIB_ARCH_TUPLE "armeb-linux-gnu"
-# endif
-# else
-# define native_architecture() ARCHITECTURE_ARM
-# if defined(__ARM_EABI__)
-# if defined(__ARM_PCS_VFP)
-# define LIB_ARCH_TUPLE "arm-linux-gnueabihf"
-# else
-# define LIB_ARCH_TUPLE "arm-linux-gnueabi"
-# endif
-# else
-# define LIB_ARCH_TUPLE "arm-linux-gnu"
-# endif
-# endif
-#elif defined(__sh64__)
-# define native_architecture() ARCHITECTURE_SH64
-# error "Missing LIB_ARCH_TUPLE for SH64"
-#elif defined(__sh__)
-# define native_architecture() ARCHITECTURE_SH
-# define LIB_ARCH_TUPLE "sh4-linux-gnu"
-#elif defined(__m68k__)
-# define native_architecture() ARCHITECTURE_M68K
-# define LIB_ARCH_TUPLE "m68k-linux-gnu"
-#elif defined(__tilegx__)
-# define native_architecture() ARCHITECTURE_TILEGX
-# error "Missing LIB_ARCH_TUPLE for TILEGX"
-#elif defined(__cris__)
-# define native_architecture() ARCHITECTURE_CRIS
-# error "Missing LIB_ARCH_TUPLE for CRIS"
-#else
-# error "Please register your architecture here!"
-#endif
-
-const char *architecture_to_string(int a) _const_;
-int architecture_from_string(const char *s) _pure_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <net/if_arp.h>
-#include <string.h>
-
-#include "util.h"
-#include "arphrd-list.h"
-
-static const struct arphrd_name* lookup_arphrd(register const char *str, register unsigned int len);
-
-#include "arphrd-to-name.h"
-#include "arphrd-from-name.h"
-
-const char *arphrd_to_name(int id) {
-
- if (id <= 0)
- return NULL;
-
- if (id >= (int) ELEMENTSOF(arphrd_names))
- return NULL;
-
- return arphrd_names[id];
-}
-
-int arphrd_from_name(const char *name) {
- const struct arphrd_name *sc;
-
- assert(name);
-
- sc = lookup_arphrd(name, strlen(name));
- if (!sc)
- return 0;
-
- return sc->id;
-}
-
-int arphrd_max(void) {
- return ELEMENTSOF(arphrd_names);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-const char *arphrd_to_name(int id);
-int arphrd_from_name(const char *name);
-
-int arphrd_max(void);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-#include <stdbool.h>
-#include <termios.h>
-#include <unistd.h>
-#include <poll.h>
-#include <sys/inotify.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/socket.h>
-#include <string.h>
-#include <sys/un.h>
-#include <stddef.h>
-#include <sys/signalfd.h>
-
-#include "util.h"
-#include "mkdir.h"
-#include "strv.h"
-
-#include "ask-password-api.h"
-
-static void backspace_chars(int ttyfd, size_t p) {
-
- if (ttyfd < 0)
- return;
-
- while (p > 0) {
- p--;
-
- loop_write(ttyfd, "\b \b", 3, false);
- }
-}
-
-int ask_password_tty(
- const char *message,
- usec_t until,
- bool echo,
- const char *flag_file,
- char **_passphrase) {
-
- struct termios old_termios, new_termios;
- char passphrase[LINE_MAX], *x;
- size_t p = 0;
- int r;
- _cleanup_close_ int ttyfd = -1, notify = -1;
- struct pollfd pollfd[2];
- bool reset_tty = false;
- bool silent_mode = false;
- bool dirty = false;
- enum {
- POLL_TTY,
- POLL_INOTIFY
- };
-
- assert(message);
- assert(_passphrase);
-
- if (flag_file) {
- notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
- if (notify < 0) {
- r = -errno;
- goto finish;
- }
-
- if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
- r = -errno;
- goto finish;
- }
- }
-
- ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (ttyfd >= 0) {
-
- if (tcgetattr(ttyfd, &old_termios) < 0) {
- r = -errno;
- goto finish;
- }
-
- loop_write(ttyfd, ANSI_HIGHLIGHT_ON, sizeof(ANSI_HIGHLIGHT_ON)-1, false);
- loop_write(ttyfd, message, strlen(message), false);
- loop_write(ttyfd, " ", 1, false);
- loop_write(ttyfd, ANSI_HIGHLIGHT_OFF, sizeof(ANSI_HIGHLIGHT_OFF)-1, false);
-
- new_termios = old_termios;
- new_termios.c_lflag &= ~(ICANON|ECHO);
- new_termios.c_cc[VMIN] = 1;
- new_termios.c_cc[VTIME] = 0;
-
- if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) {
- r = -errno;
- goto finish;
- }
-
- reset_tty = true;
- }
-
- zero(pollfd);
- pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
- pollfd[POLL_TTY].events = POLLIN;
- pollfd[POLL_INOTIFY].fd = notify;
- pollfd[POLL_INOTIFY].events = POLLIN;
-
- for (;;) {
- char c;
- int sleep_for = -1, k;
- ssize_t n;
-
- if (until > 0) {
- usec_t y;
-
- y = now(CLOCK_MONOTONIC);
-
- if (y > until) {
- r = -ETIME;
- goto finish;
- }
-
- sleep_for = (int) ((until - y) / USEC_PER_MSEC);
- }
-
- if (flag_file)
- if (access(flag_file, F_OK) < 0) {
- r = -errno;
- goto finish;
- }
-
- k = poll(pollfd, notify > 0 ? 2 : 1, sleep_for);
- if (k < 0) {
- if (errno == EINTR)
- continue;
-
- r = -errno;
- goto finish;
- } else if (k == 0) {
- r = -ETIME;
- goto finish;
- }
-
- if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
- flush_fd(notify);
-
- if (pollfd[POLL_TTY].revents == 0)
- continue;
-
- n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1);
- if (n < 0) {
- if (errno == EINTR || errno == EAGAIN)
- continue;
-
- r = -errno;
- goto finish;
-
- } else if (n == 0)
- break;
-
- if (c == '\n')
- break;
- else if (c == 21) { /* C-u */
-
- if (!silent_mode)
- backspace_chars(ttyfd, p);
- p = 0;
-
- } else if (c == '\b' || c == 127) {
-
- if (p > 0) {
-
- if (!silent_mode)
- backspace_chars(ttyfd, 1);
-
- p--;
- } else if (!dirty && !silent_mode) {
-
- silent_mode = true;
-
- /* There are two ways to enter silent
- * mode. Either by pressing backspace
- * as first key (and only as first key),
- * or ... */
- if (ttyfd >= 0)
- loop_write(ttyfd, "(no echo) ", 10, false);
-
- } else if (ttyfd >= 0)
- loop_write(ttyfd, "\a", 1, false);
-
- } else if (c == '\t' && !silent_mode) {
-
- backspace_chars(ttyfd, p);
- silent_mode = true;
-
- /* ... or by pressing TAB at any time. */
-
- if (ttyfd >= 0)
- loop_write(ttyfd, "(no echo) ", 10, false);
- } else {
- if (p >= sizeof(passphrase)-1) {
- loop_write(ttyfd, "\a", 1, false);
- continue;
- }
-
- passphrase[p++] = c;
-
- if (!silent_mode && ttyfd >= 0)
- loop_write(ttyfd, echo ? &c : "*", 1, false);
-
- dirty = true;
- }
- }
-
- x = strndup(passphrase, p);
- if (!x) {
- r = -ENOMEM;
- goto finish;
- }
-
- *_passphrase = x;
- r = 0;
-
-finish:
- if (ttyfd >= 0 && reset_tty) {
- loop_write(ttyfd, "\n", 1, false);
- tcsetattr(ttyfd, TCSADRAIN, &old_termios);
- }
-
- return r;
-}
-
-static int create_socket(char **name) {
- int fd;
- union {
- struct sockaddr sa;
- struct sockaddr_un un;
- } sa = {
- .un.sun_family = AF_UNIX,
- };
- int one = 1;
- int r = 0;
- char *c;
-
- assert(name);
-
- fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
- if (fd < 0)
- return log_error_errno(errno, "socket() failed: %m");
-
- snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%" PRIx64, random_u64());
-
- RUN_WITH_UMASK(0177) {
- r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
- }
-
- if (r < 0) {
- r = -errno;
- log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
- goto fail;
- }
-
- if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
- r = -errno;
- log_error_errno(errno, "SO_PASSCRED failed: %m");
- goto fail;
- }
-
- c = strdup(sa.un.sun_path);
- if (!c) {
- r = log_oom();
- goto fail;
- }
-
- *name = c;
- return fd;
-
-fail:
- safe_close(fd);
-
- return r;
-}
-
-int ask_password_agent(
- const char *message,
- const char *icon,
- const char *id,
- usec_t until,
- bool echo,
- bool accept_cached,
- char ***_passphrases) {
-
- enum {
- FD_SOCKET,
- FD_SIGNAL,
- _FD_MAX
- };
-
- char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
- char final[sizeof(temp)] = "";
- _cleanup_fclose_ FILE *f = NULL;
- _cleanup_free_ char *socket_name = NULL;
- _cleanup_close_ int socket_fd = -1, signal_fd = -1, fd = -1;
- sigset_t mask, oldmask;
- struct pollfd pollfd[_FD_MAX];
- int r;
-
- assert(_passphrases);
-
- assert_se(sigemptyset(&mask) == 0);
- sigset_add_many(&mask, SIGINT, SIGTERM, -1);
- assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
-
- mkdir_p_label("/run/systemd/ask-password", 0755);
-
- fd = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
- if (fd < 0) {
- log_error_errno(errno, "Failed to create password file: %m");
- r = -errno;
- goto finish;
- }
-
- fchmod(fd, 0644);
-
- f = fdopen(fd, "w");
- if (!f) {
- log_error_errno(errno, "Failed to allocate FILE: %m");
- r = -errno;
- goto finish;
- }
-
- fd = -1;
-
- signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
- if (signal_fd < 0) {
- log_error_errno(errno, "signalfd(): %m");
- r = -errno;
- goto finish;
- }
-
- socket_fd = create_socket(&socket_name);
- if (socket_fd < 0) {
- r = socket_fd;
- goto finish;
- }
-
- fprintf(f,
- "[Ask]\n"
- "PID="PID_FMT"\n"
- "Socket=%s\n"
- "AcceptCached=%i\n"
- "Echo=%i\n"
- "NotAfter="USEC_FMT"\n",
- getpid(),
- socket_name,
- accept_cached ? 1 : 0,
- echo ? 1 : 0,
- until);
-
- if (message)
- fprintf(f, "Message=%s\n", message);
-
- if (icon)
- fprintf(f, "Icon=%s\n", icon);
-
- if (id)
- fprintf(f, "Id=%s\n", id);
-
- fflush(f);
-
- if (ferror(f)) {
- log_error_errno(errno, "Failed to write query file: %m");
- r = -errno;
- goto finish;
- }
-
- memcpy(final, temp, sizeof(temp));
-
- final[sizeof(final)-11] = 'a';
- final[sizeof(final)-10] = 's';
- final[sizeof(final)-9] = 'k';
-
- if (rename(temp, final) < 0) {
- log_error_errno(errno, "Failed to rename query file: %m");
- r = -errno;
- goto finish;
- }
-
- zero(pollfd);
- pollfd[FD_SOCKET].fd = socket_fd;
- pollfd[FD_SOCKET].events = POLLIN;
- pollfd[FD_SIGNAL].fd = signal_fd;
- pollfd[FD_SIGNAL].events = POLLIN;
-
- for (;;) {
- char passphrase[LINE_MAX+1];
- struct msghdr msghdr;
- struct iovec iovec;
- struct ucred *ucred;
- union {
- struct cmsghdr cmsghdr;
- uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
- } control;
- ssize_t n;
- int k;
- usec_t t;
-
- t = now(CLOCK_MONOTONIC);
-
- if (until > 0 && until <= t) {
- log_notice("Timed out");
- r = -ETIME;
- goto finish;
- }
-
- k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1);
- if (k < 0) {
- if (errno == EINTR)
- continue;
-
- log_error_errno(errno, "poll() failed: %m");
- r = -errno;
- goto finish;
- }
-
- if (k <= 0) {
- log_notice("Timed out");
- r = -ETIME;
- goto finish;
- }
-
- if (pollfd[FD_SIGNAL].revents & POLLIN) {
- r = -EINTR;
- goto finish;
- }
-
- if (pollfd[FD_SOCKET].revents != POLLIN) {
- log_error("Unexpected poll() event.");
- r = -EIO;
- goto finish;
- }
-
- zero(iovec);
- iovec.iov_base = passphrase;
- iovec.iov_len = sizeof(passphrase);
-
- zero(control);
- zero(msghdr);
- msghdr.msg_iov = &iovec;
- msghdr.msg_iovlen = 1;
- msghdr.msg_control = &control;
- msghdr.msg_controllen = sizeof(control);
-
- n = recvmsg(socket_fd, &msghdr, 0);
- if (n < 0) {
- if (errno == EAGAIN ||
- errno == EINTR)
- continue;
-
- log_error_errno(errno, "recvmsg() failed: %m");
- r = -errno;
- goto finish;
- }
-
- cmsg_close_all(&msghdr);
-
- if (n <= 0) {
- log_error("Message too short");
- continue;
- }
-
- if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
- control.cmsghdr.cmsg_level != SOL_SOCKET ||
- control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
- control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
- log_warning("Received message without credentials. Ignoring.");
- continue;
- }
-
- ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
- if (ucred->uid != 0) {
- log_warning("Got request from unprivileged user. Ignoring.");
- continue;
- }
-
- if (passphrase[0] == '+') {
- char **l;
-
- if (n == 1)
- l = strv_new("", NULL);
- else
- l = strv_parse_nulstr(passphrase+1, n-1);
- /* An empty message refers to the empty password */
-
- if (!l) {
- r = -ENOMEM;
- goto finish;
- }
-
- if (strv_length(l) <= 0) {
- strv_free(l);
- log_error("Invalid packet");
- continue;
- }
-
- *_passphrases = l;
-
- } else if (passphrase[0] == '-') {
- r = -ECANCELED;
- goto finish;
- } else {
- log_error("Invalid packet");
- continue;
- }
-
- break;
- }
-
- r = 0;
-
-finish:
- if (socket_name)
- unlink(socket_name);
-
- unlink(temp);
-
- if (final[0])
- unlink(final);
-
- assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
-
- return r;
-}
-
-int ask_password_auto(const char *message, const char *icon, const char *id,
- usec_t until, bool accept_cached, char ***_passphrases) {
- assert(message);
- assert(_passphrases);
-
- if (isatty(STDIN_FILENO)) {
- int r;
- char *s = NULL, **l = NULL;
-
- r = ask_password_tty(message, until, false, NULL, &s);
- if (r < 0)
- return r;
-
- r = strv_consume(&l, s);
- if (r < 0)
- return r;
-
- *_passphrases = l;
- return r;
- } else
- return ask_password_agent(message, icon, id, until, false, accept_cached, _passphrases);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-int ask_password_tty(const char *message, usec_t until, bool echo, const char *flag_file, char **_passphrase);
-
-int ask_password_agent(const char *message, const char *icon, const char *id,
- usec_t until, bool echo, bool accept_cached, char ***_passphrases);
-
-int ask_password_auto(const char *message, const char *icon, const char *id,
- usec_t until, bool accept_cached, char ***_passphrases);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <pthread.h>
-#include <unistd.h>
-
-#include "async.h"
-#include "log.h"
-#include "util.h"
-
-int asynchronous_job(void* (*func)(void *p), void *arg) {
- pthread_attr_t a;
- pthread_t t;
- int r;
-
- /* It kinda sucks that we have to resort to threads to
- * implement an asynchronous sync(), but well, such is
- * life.
- *
- * Note that issuing this command right before exiting a
- * process will cause the process to wait for the sync() to
- * complete. This function hence is nicely asynchronous really
- * only in long running processes. */
-
- r = pthread_attr_init(&a);
- if (r > 0)
- return -r;
-
- r = pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
- if (r > 0)
- goto finish;
-
- r = pthread_create(&t, &a, func, arg);
-
-finish:
- pthread_attr_destroy(&a);
- return -r;
-}
-
-static void *sync_thread(void *p) {
- sync();
- return NULL;
-}
-
-int asynchronous_sync(void) {
- log_debug("Spawning new thread for sync");
-
- return asynchronous_job(sync_thread, NULL);
-}
-
-static void *close_thread(void *p) {
- assert_se(close_nointr(PTR_TO_INT(p)) != -EBADF);
- return NULL;
-}
-
-int asynchronous_close(int fd) {
- int r;
-
- /* This is supposed to behave similar to safe_close(), but
- * actually invoke close() asynchronously, so that it will
- * never block. Ideally the kernel would have an API for this,
- * but it doesn't, so we work around it, and hide this as a
- * far away as we can. */
-
- if (fd >= 0) {
- PROTECT_ERRNO;
-
- r = asynchronous_job(close_thread, INT_TO_PTR(fd));
- if (r < 0)
- assert_se(close_nointr(fd) != -EBADF);
- }
-
- return -1;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-int asynchronous_job(void* (*func)(void *p), void *arg);
-
-int asynchronous_sync(void);
-int asynchronous_close(int fd);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <fcntl.h>
-#include <poll.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <sys/eventfd.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include "barrier.h"
-#include "macro.h"
-#include "util.h"
-
-/**
- * Barriers
- * This barrier implementation provides a simple synchronization method based
- * on file-descriptors that can safely be used between threads and processes. A
- * barrier object contains 2 shared counters based on eventfd. Both processes
- * can now place barriers and wait for the other end to reach a random or
- * specific barrier.
- * Barriers are numbered, so you can either wait for the other end to reach any
- * barrier or the last barrier that you placed. This way, you can use barriers
- * for one-way *and* full synchronization. Note that even-though barriers are
- * numbered, these numbers are internal and recycled once both sides reached the
- * same barrier (implemented as a simple signed counter). It is thus not
- * possible to address barriers by their ID.
- *
- * Barrier-API: Both ends can place as many barriers via barrier_place() as
- * they want and each pair of barriers on both sides will be implicitly linked.
- * Each side can use the barrier_wait/sync_*() family of calls to wait for the
- * other side to place a specific barrier. barrier_wait_next() waits until the
- * other side calls barrier_place(). No links between the barriers are
- * considered and this simply serves as most basic asynchronous barrier.
- * barrier_sync_next() is like barrier_wait_next() and waits for the other side
- * to place their next barrier via barrier_place(). However, it only waits for
- * barriers that are linked to a barrier we already placed. If the other side
- * already placed more barriers than we did, barrier_sync_next() returns
- * immediately.
- * barrier_sync() extends barrier_sync_next() and waits until the other end
- * placed as many barriers via barrier_place() as we did. If they already placed
- * as many as we did (or more), it returns immediately.
- *
- * Additionally to basic barriers, an abortion event is available.
- * barrier_abort() places an abortion event that cannot be undone. An abortion
- * immediately cancels all placed barriers and replaces them. Any running and
- * following wait/sync call besides barrier_wait_abortion() will immediately
- * return false on both sides (otherwise, they always return true).
- * barrier_abort() can be called multiple times on both ends and will be a
- * no-op if already called on this side.
- * barrier_wait_abortion() can be used to wait for the other side to call
- * barrier_abort() and is the only wait/sync call that does not return
- * immediately if we aborted outself. It only returns once the other side
- * called barrier_abort().
- *
- * Barriers can be used for in-process and inter-process synchronization.
- * However, for in-process synchronization you could just use mutexes.
- * Therefore, main target is IPC and we require both sides to *not* share the FD
- * table. If that's given, barriers provide target tracking: If the remote side
- * exit()s, an abortion event is implicitly queued on the other side. This way,
- * a sync/wait call will be woken up if the remote side crashed or exited
- * unexpectedly. However, note that these abortion events are only queued if the
- * barrier-queue has been drained. Therefore, it is safe to place a barrier and
- * exit. The other side can safely wait on the barrier even though the exit
- * queued an abortion event. Usually, the abortion event would overwrite the
- * barrier, however, that's not true for exit-abortion events. Those are only
- * queued if the barrier-queue is drained (thus, the receiving side has placed
- * more barriers than the remote side).
- */
-
-/**
- * barrier_create() - Initialize a barrier object
- * @obj: barrier to initialize
- *
- * This initializes a barrier object. The caller is responsible of allocating
- * the memory and keeping it valid. The memory does not have to be zeroed
- * beforehand.
- * Two eventfd objects are allocated for each barrier. If allocation fails, an
- * error is returned.
- *
- * If this function fails, the barrier is reset to an invalid state so it is
- * safe to call barrier_destroy() on the object regardless whether the
- * initialization succeeded or not.
- *
- * The caller is responsible to destroy the object via barrier_destroy() before
- * releasing the underlying memory.
- *
- * Returns: 0 on success, negative error code on failure.
- */
-int barrier_create(Barrier *b) {
- _cleanup_(barrier_destroyp) Barrier *staging = b;
- int r;
-
- assert(b);
-
- b->me = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
- if (b->me < 0)
- return -errno;
-
- b->them = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
- if (b->them < 0)
- return -errno;
-
- r = pipe2(b->pipe, O_CLOEXEC | O_NONBLOCK);
- if (r < 0)
- return -errno;
-
- staging = NULL;
- return 0;
-}
-
-/**
- * barrier_destroy() - Destroy a barrier object
- * @b: barrier to destroy or NULL
- *
- * This destroys a barrier object that has previously been passed to
- * barrier_create(). The object is released and reset to invalid
- * state. Therefore, it is safe to call barrier_destroy() multiple
- * times or even if barrier_create() failed. However, barrier must be
- * always initalized with BARRIER_NULL.
- *
- * If @b is NULL, this is a no-op.
- */
-void barrier_destroy(Barrier *b) {
- if (!b)
- return;
-
- b->me = safe_close(b->me);
- b->them = safe_close(b->them);
- safe_close_pair(b->pipe);
- b->barriers = 0;
-}
-
-/**
- * barrier_set_role() - Set the local role of the barrier
- * @b: barrier to operate on
- * @role: role to set on the barrier
- *
- * This sets the roles on a barrier object. This is needed to know
- * which side of the barrier you're on. Usually, the parent creates
- * the barrier via barrier_create() and then calls fork() or clone().
- * Therefore, the FDs are duplicated and the child retains the same
- * barrier object.
- *
- * Both sides need to call barrier_set_role() after fork() or clone()
- * are done. If this is not done, barriers will not work correctly.
- *
- * Note that barriers could be supported without fork() or clone(). However,
- * this is currently not needed so it hasn't been implemented.
- */
-void barrier_set_role(Barrier *b, unsigned int role) {
- int fd;
-
- assert(b);
- assert(role == BARRIER_PARENT || role == BARRIER_CHILD);
- /* make sure this is only called once */
- assert(b->pipe[0] >= 0 && b->pipe[1] >= 0);
-
- if (role == BARRIER_PARENT)
- b->pipe[1] = safe_close(b->pipe[1]);
- else {
- b->pipe[0] = safe_close(b->pipe[0]);
-
- /* swap me/them for children */
- fd = b->me;
- b->me = b->them;
- b->them = fd;
- }
-}
-
-/* places barrier; returns false if we aborted, otherwise true */
-static bool barrier_write(Barrier *b, uint64_t buf) {
- ssize_t len;
-
- /* prevent new sync-points if we already aborted */
- if (barrier_i_aborted(b))
- return false;
-
- do {
- len = write(b->me, &buf, sizeof(buf));
- } while (len < 0 && IN_SET(errno, EAGAIN, EINTR));
-
- if (len != sizeof(buf))
- goto error;
-
- /* lock if we aborted */
- if (buf >= (uint64_t)BARRIER_ABORTION) {
- if (barrier_they_aborted(b))
- b->barriers = BARRIER_WE_ABORTED;
- else
- b->barriers = BARRIER_I_ABORTED;
- } else if (!barrier_is_aborted(b))
- b->barriers += buf;
-
- return !barrier_i_aborted(b);
-
-error:
- /* If there is an unexpected error, we have to make this fatal. There
- * is no way we can recover from sync-errors. Therefore, we close the
- * pipe-ends and treat this as abortion. The other end will notice the
- * pipe-close and treat it as abortion, too. */
-
- safe_close_pair(b->pipe);
- b->barriers = BARRIER_WE_ABORTED;
- return false;
-}
-
-/* waits for barriers; returns false if they aborted, otherwise true */
-static bool barrier_read(Barrier *b, int64_t comp) {
- if (barrier_they_aborted(b))
- return false;
-
- while (b->barriers > comp) {
- struct pollfd pfd[2] = {
- { .fd = b->pipe[0] >= 0 ? b->pipe[0] : b->pipe[1],
- .events = POLLHUP },
- { .fd = b->them,
- .events = POLLIN }};
- uint64_t buf;
- int r;
-
- r = poll(pfd, 2, -1);
- if (r < 0 && IN_SET(errno, EAGAIN, EINTR))
- continue;
- else if (r < 0)
- goto error;
-
- if (pfd[1].revents) {
- ssize_t len;
-
- /* events on @them signal new data for us */
- len = read(b->them, &buf, sizeof(buf));
- if (len < 0 && IN_SET(errno, EAGAIN, EINTR))
- continue;
-
- if (len != sizeof(buf))
- goto error;
- } else if (pfd[0].revents & (POLLHUP | POLLERR | POLLNVAL))
- /* POLLHUP on the pipe tells us the other side exited.
- * We treat this as implicit abortion. But we only
- * handle it if there's no event on the eventfd. This
- * guarantees that exit-abortions do not overwrite real
- * barriers. */
- buf = BARRIER_ABORTION;
- else
- continue;
-
- /* lock if they aborted */
- if (buf >= (uint64_t)BARRIER_ABORTION) {
- if (barrier_i_aborted(b))
- b->barriers = BARRIER_WE_ABORTED;
- else
- b->barriers = BARRIER_THEY_ABORTED;
- } else if (!barrier_is_aborted(b))
- b->barriers -= buf;
- }
-
- return !barrier_they_aborted(b);
-
-error:
- /* If there is an unexpected error, we have to make this fatal. There
- * is no way we can recover from sync-errors. Therefore, we close the
- * pipe-ends and treat this as abortion. The other end will notice the
- * pipe-close and treat it as abortion, too. */
-
- safe_close_pair(b->pipe);
- b->barriers = BARRIER_WE_ABORTED;
- return false;
-}
-
-/**
- * barrier_place() - Place a new barrier
- * @b: barrier object
- *
- * This places a new barrier on the barrier object. If either side already
- * aborted, this is a no-op and returns "false". Otherwise, the barrier is
- * placed and this returns "true".
- *
- * Returns: true if barrier was placed, false if either side aborted.
- */
-bool barrier_place(Barrier *b) {
- assert(b);
-
- if (barrier_is_aborted(b))
- return false;
-
- barrier_write(b, BARRIER_SINGLE);
- return true;
-}
-
-/**
- * barrier_abort() - Abort the synchronization
- * @b: barrier object to abort
- *
- * This aborts the barrier-synchronization. If barrier_abort() was already
- * called on this side, this is a no-op. Otherwise, the barrier is put into the
- * ABORT-state and will stay there. The other side is notified about the
- * abortion. Any following attempt to place normal barriers or to wait on normal
- * barriers will return immediately as "false".
- *
- * You can wait for the other side to call barrier_abort(), too. Use
- * barrier_wait_abortion() for that.
- *
- * Returns: false if the other side already aborted, true otherwise.
- */
-bool barrier_abort(Barrier *b) {
- assert(b);
-
- barrier_write(b, BARRIER_ABORTION);
- return !barrier_they_aborted(b);
-}
-
-/**
- * barrier_wait_next() - Wait for the next barrier of the other side
- * @b: barrier to operate on
- *
- * This waits until the other side places its next barrier. This is independent
- * of any barrier-links and just waits for any next barrier of the other side.
- *
- * If either side aborted, this returns false.
- *
- * Returns: false if either side aborted, true otherwise.
- */
-bool barrier_wait_next(Barrier *b) {
- assert(b);
-
- if (barrier_is_aborted(b))
- return false;
-
- barrier_read(b, b->barriers - 1);
- return !barrier_is_aborted(b);
-}
-
-/**
- * barrier_wait_abortion() - Wait for the other side to abort
- * @b: barrier to operate on
- *
- * This waits until the other side called barrier_abort(). This can be called
- * regardless whether the local side already called barrier_abort() or not.
- *
- * If the other side has already aborted, this returns immediately.
- *
- * Returns: false if the local side aborted, true otherwise.
- */
-bool barrier_wait_abortion(Barrier *b) {
- assert(b);
-
- barrier_read(b, BARRIER_THEY_ABORTED);
- return !barrier_i_aborted(b);
-}
-
-/**
- * barrier_sync_next() - Wait for the other side to place a next linked barrier
- * @b: barrier to operate on
- *
- * This is like barrier_wait_next() and waits for the other side to call
- * barrier_place(). However, this only waits for linked barriers. That means, if
- * the other side already placed more barriers than (or as much as) we did, this
- * returns immediately instead of waiting.
- *
- * If either side aborted, this returns false.
- *
- * Returns: false if either side aborted, true otherwise.
- */
-bool barrier_sync_next(Barrier *b) {
- assert(b);
-
- if (barrier_is_aborted(b))
- return false;
-
- barrier_read(b, MAX((int64_t)0, b->barriers - 1));
- return !barrier_is_aborted(b);
-}
-
-/**
- * barrier_sync() - Wait for the other side to place as many barriers as we did
- * @b: barrier to operate on
- *
- * This is like barrier_sync_next() but waits for the other side to call
- * barrier_place() as often as we did (in total). If they already placed as much
- * as we did (or more), this returns immediately instead of waiting.
- *
- * If either side aborted, this returns false.
- *
- * Returns: false if either side aborted, true otherwise.
- */
-bool barrier_sync(Barrier *b) {
- assert(b);
-
- if (barrier_is_aborted(b))
- return false;
-
- barrier_read(b, 0);
- return !barrier_is_aborted(b);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-
-#include "macro.h"
-
-/* See source file for an API description. */
-
-typedef struct Barrier Barrier;
-
-enum {
- BARRIER_SINGLE = 1LL,
- BARRIER_ABORTION = INT64_MAX,
-
- /* bias values to store state; keep @WE < @THEY < @I */
- BARRIER_BIAS = INT64_MIN,
- BARRIER_WE_ABORTED = BARRIER_BIAS + 1LL,
- BARRIER_THEY_ABORTED = BARRIER_BIAS + 2LL,
- BARRIER_I_ABORTED = BARRIER_BIAS + 3LL,
-};
-
-enum {
- BARRIER_PARENT,
- BARRIER_CHILD,
-};
-
-struct Barrier {
- int me;
- int them;
- int pipe[2];
- int64_t barriers;
-};
-
-#define BARRIER_NULL {-1, -1, {-1, -1}, 0}
-
-int barrier_create(Barrier *obj);
-void barrier_destroy(Barrier *b);
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy);
-
-void barrier_set_role(Barrier *b, unsigned int role);
-
-bool barrier_place(Barrier *b);
-bool barrier_abort(Barrier *b);
-
-bool barrier_wait_next(Barrier *b);
-bool barrier_wait_abortion(Barrier *b);
-bool barrier_sync_next(Barrier *b);
-bool barrier_sync(Barrier *b);
-
-static inline bool barrier_i_aborted(Barrier *b) {
- return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_WE_ABORTED;
-}
-
-static inline bool barrier_they_aborted(Barrier *b) {
- return b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
-}
-
-static inline bool barrier_we_aborted(Barrier *b) {
- return b->barriers == BARRIER_WE_ABORTED;
-}
-
-static inline bool barrier_is_aborted(Barrier *b) {
- return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
-}
-
-static inline bool barrier_place_and_sync(Barrier *b) {
- (void) barrier_place(b);
- return barrier_sync(b);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Kay Sievers
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <sys/stat.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include "base-filesystem.h"
-#include "log.h"
-#include "macro.h"
-#include "util.h"
-
-typedef struct BaseFilesystem {
- const char *dir;
- mode_t mode;
- const char *target;
- const char *exists;
-} BaseFilesystem;
-
-static const BaseFilesystem table[] = {
- { "bin", 0, "usr/bin\0", NULL },
- { "lib", 0, "usr/lib\0", NULL },
- { "root", 0755, NULL, NULL },
- { "sbin", 0, "usr/sbin\0", NULL },
-#if defined(__i386__) || defined(__x86_64__)
- { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
- "usr/lib64\0", "ld-linux-x86-64.so.2" },
-#endif
-};
-
-int base_filesystem_create(const char *root) {
- _cleanup_close_ int fd = -1;
- unsigned i;
- int r = 0;
-
- fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
- if (fd < 0)
- return log_error_errno(errno, "Failed to open root file system: %m");
-
- for (i = 0; i < ELEMENTSOF(table); i ++) {
- if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
- continue;
-
- if (table[i].target) {
- const char *target = NULL, *s;
-
- /* check if one of the targets exists */
- NULSTR_FOREACH(s, table[i].target) {
- if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
- continue;
-
- /* check if a specific file exists at the target path */
- if (table[i].exists) {
- _cleanup_free_ char *p = NULL;
-
- p = strjoin(s, "/", table[i].exists, NULL);
- if (!p)
- return log_oom();
-
- if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
- continue;
- }
-
- target = s;
- break;
- }
-
- if (!target)
- continue;
-
- r = symlinkat(target, fd, table[i].dir);
- if (r < 0 && errno != EEXIST)
- return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
- continue;
- }
-
- RUN_WITH_UMASK(0000)
- r = mkdirat(fd, table[i].dir, table[i].mode);
- if (r < 0 && errno != EEXIST)
- return log_error_errno(errno, "Failed to create directory at %s/%s: %m", root, table[i].dir);
- }
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Kay Sievers
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-int base_filesystem_create(const char *root);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#ifdef HAVE_BLKID
-#include <blkid/blkid.h>
-#endif
-
-#include "util.h"
-
-#ifdef HAVE_BLKID
-DEFINE_TRIVIAL_CLEANUP_FUNC(blkid_probe, blkid_free_probe);
-#define _cleanup_blkid_free_probe_ _cleanup_(blkid_free_probep)
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-#include "macro.h"
-#include "sparse-endian.h"
-
-/* Stolen from btrfs' ctree.h */
-
-struct btrfs_timespec {
- le64_t sec;
- le32_t nsec;
-} _packed_;
-
-struct btrfs_disk_key {
- le64_t objectid;
- uint8_t type;
- le64_t offset;
-} _packed_;
-
-struct btrfs_inode_item {
- le64_t generation;
- le64_t transid;
- le64_t size;
- le64_t nbytes;
- le64_t block_group;
- le32_t nlink;
- le32_t uid;
- le32_t gid;
- le32_t mode;
- le64_t rdev;
- le64_t flags;
- le64_t sequence;
- le64_t reserved[4];
- struct btrfs_timespec atime;
- struct btrfs_timespec ctime;
- struct btrfs_timespec mtime;
- struct btrfs_timespec otime;
-} _packed_;
-
-struct btrfs_root_item {
- struct btrfs_inode_item inode;
- le64_t generation;
- le64_t root_dirid;
- le64_t bytenr;
- le64_t byte_limit;
- le64_t bytes_used;
- le64_t last_snapshot;
- le64_t flags;
- le32_t refs;
- struct btrfs_disk_key drop_progress;
- uint8_t drop_level;
- uint8_t level;
- le64_t generation_v2;
- uint8_t uuid[BTRFS_UUID_SIZE];
- uint8_t parent_uuid[BTRFS_UUID_SIZE];
- uint8_t received_uuid[BTRFS_UUID_SIZE];
- le64_t ctransid;
- le64_t otransid;
- le64_t stransid;
- le64_t rtransid;
- struct btrfs_timespec ctime;
- struct btrfs_timespec otime;
- struct btrfs_timespec stime;
- struct btrfs_timespec rtime;
- le64_t reserved[8];
-} _packed_;
-
-#define BTRFS_ROOT_SUBVOL_RDONLY (1ULL << 0)
-
-struct btrfs_qgroup_info_item {
- le64_t generation;
- le64_t rfer;
- le64_t rfer_cmpr;
- le64_t excl;
- le64_t excl_cmpr;
-} _packed_;
-
-#define BTRFS_QGROUP_LIMIT_MAX_RFER (1ULL << 0)
-#define BTRFS_QGROUP_LIMIT_MAX_EXCL (1ULL << 1)
-#define BTRFS_QGROUP_LIMIT_RSV_RFER (1ULL << 2)
-#define BTRFS_QGROUP_LIMIT_RSV_EXCL (1ULL << 3)
-#define BTRFS_QGROUP_LIMIT_RFER_CMPR (1ULL << 4)
-#define BTRFS_QGROUP_LIMIT_EXCL_CMPR (1ULL << 5)
-
-struct btrfs_qgroup_limit_item {
- le64_t flags;
- le64_t max_rfer;
- le64_t max_excl;
- le64_t rsv_rfer;
- le64_t rsv_excl;
-} _packed_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <sys/vfs.h>
-#include <sys/stat.h>
-
-#ifdef HAVE_LINUX_BTRFS_H
-#include <linux/btrfs.h>
-#endif
-
-#include "missing.h"
-#include "util.h"
-#include "path-util.h"
-#include "macro.h"
-#include "copy.h"
-#include "selinux-util.h"
-#include "smack-util.h"
-#include "fileio.h"
-#include "btrfs-ctree.h"
-#include "btrfs-util.h"
-
-static int validate_subvolume_name(const char *name) {
-
- if (!filename_is_valid(name))
- return -EINVAL;
-
- if (strlen(name) > BTRFS_SUBVOL_NAME_MAX)
- return -E2BIG;
-
- return 0;
-}
-
-static int open_parent(const char *path, int flags) {
- _cleanup_free_ char *parent = NULL;
- int r, fd;
-
- assert(path);
-
- r = path_get_parent(path, &parent);
- if (r < 0)
- return r;
-
- fd = open(parent, flags);
- if (fd < 0)
- return -errno;
-
- return fd;
-}
-
-static int extract_subvolume_name(const char *path, const char **subvolume) {
- const char *fn;
- int r;
-
- assert(path);
- assert(subvolume);
-
- fn = basename(path);
-
- r = validate_subvolume_name(fn);
- if (r < 0)
- return r;
-
- *subvolume = fn;
- return 0;
-}
-
-int btrfs_is_snapshot(int fd) {
- struct stat st;
- struct statfs sfs;
-
- /* On btrfs subvolumes always have the inode 256 */
-
- if (fstat(fd, &st) < 0)
- return -errno;
-
- if (!S_ISDIR(st.st_mode) || st.st_ino != 256)
- return 0;
-
- if (fstatfs(fd, &sfs) < 0)
- return -errno;
-
- return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
-}
-
-int btrfs_subvol_snapshot_fd(int old_fd, const char *new_path, bool read_only, bool fallback_copy) {
- struct btrfs_ioctl_vol_args_v2 args = {
- .flags = read_only ? BTRFS_SUBVOL_RDONLY : 0,
- };
- _cleanup_close_ int new_fd = -1;
- const char *subvolume;
- int r;
-
- assert(new_path);
-
- r = btrfs_is_snapshot(old_fd);
- if (r < 0)
- return r;
- if (r == 0) {
- if (!fallback_copy)
- return -EISDIR;
-
- r = btrfs_subvol_make(new_path);
- if (r < 0)
- return r;
-
- r = copy_directory_fd(old_fd, new_path, true);
- if (r < 0) {
- btrfs_subvol_remove(new_path);
- return r;
- }
-
- if (read_only) {
- r = btrfs_subvol_set_read_only(new_path, true);
- if (r < 0) {
- btrfs_subvol_remove(new_path);
- return r;
- }
- }
-
- return 0;
- }
-
- r = extract_subvolume_name(new_path, &subvolume);
- if (r < 0)
- return r;
-
- new_fd = open_parent(new_path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
- if (new_fd < 0)
- return new_fd;
-
- strncpy(args.name, subvolume, sizeof(args.name)-1);
- args.fd = old_fd;
-
- if (ioctl(new_fd, BTRFS_IOC_SNAP_CREATE_V2, &args) < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_subvol_snapshot(const char *old_path, const char *new_path, bool read_only, bool fallback_copy) {
- _cleanup_close_ int old_fd = -1;
-
- assert(old_path);
- assert(new_path);
-
- old_fd = open(old_path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
- if (old_fd < 0)
- return -errno;
-
- return btrfs_subvol_snapshot_fd(old_fd, new_path, read_only, fallback_copy);
-}
-
-int btrfs_subvol_make(const char *path) {
- struct btrfs_ioctl_vol_args args = {};
- _cleanup_close_ int fd = -1;
- const char *subvolume;
- int r;
-
- assert(path);
-
- r = extract_subvolume_name(path, &subvolume);
- if (r < 0)
- return r;
-
- fd = open_parent(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
- if (fd < 0)
- return fd;
-
- strncpy(args.name, subvolume, sizeof(args.name)-1);
-
- if (ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args) < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_subvol_make_label(const char *path) {
- int r;
-
- assert(path);
-
- r = mac_selinux_create_file_prepare(path, S_IFDIR);
- if (r < 0)
- return r;
-
- r = btrfs_subvol_make(path);
- mac_selinux_create_file_clear();
-
- if (r < 0)
- return r;
-
- return mac_smack_fix(path, false, false);
-}
-
-int btrfs_subvol_remove(const char *path) {
- struct btrfs_ioctl_vol_args args = {};
- _cleanup_close_ int fd = -1;
- const char *subvolume;
- int r;
-
- assert(path);
-
- r = extract_subvolume_name(path, &subvolume);
- if (r < 0)
- return r;
-
- fd = open_parent(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
- if (fd < 0)
- return fd;
-
- strncpy(args.name, subvolume, sizeof(args.name)-1);
-
- if (ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args) < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_subvol_set_read_only_fd(int fd, bool b) {
- uint64_t flags, nflags;
- struct stat st;
-
- assert(fd >= 0);
-
- if (fstat(fd, &st) < 0)
- return -errno;
-
- if (!S_ISDIR(st.st_mode) || st.st_ino != 256)
- return -EINVAL;
-
- if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
- return -errno;
-
- if (b)
- nflags = flags | BTRFS_SUBVOL_RDONLY;
- else
- nflags = flags & ~BTRFS_SUBVOL_RDONLY;
-
- if (flags == nflags)
- return 0;
-
- if (ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &nflags) < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_subvol_set_read_only(const char *path, bool b) {
- _cleanup_close_ int fd = -1;
-
- fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
- if (fd < 0)
- return -errno;
-
- return btrfs_subvol_set_read_only_fd(fd, b);
-}
-
-int btrfs_subvol_get_read_only_fd(int fd) {
- uint64_t flags;
-
- if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
- return -errno;
-
- return !!(flags & BTRFS_SUBVOL_RDONLY);
-}
-
-int btrfs_reflink(int infd, int outfd) {
- int r;
-
- assert(infd >= 0);
- assert(outfd >= 0);
-
- r = ioctl(outfd, BTRFS_IOC_CLONE, infd);
- if (r < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_clone_range(int infd, uint64_t in_offset, int outfd, uint64_t out_offset, uint64_t sz) {
- struct btrfs_ioctl_clone_range_args args = {
- .src_fd = infd,
- .src_offset = in_offset,
- .src_length = sz,
- .dest_offset = out_offset,
- };
- int r;
-
- assert(infd >= 0);
- assert(outfd >= 0);
- assert(sz > 0);
-
- r = ioctl(outfd, BTRFS_IOC_CLONE_RANGE, &args);
- if (r < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_get_block_device_fd(int fd, dev_t *dev) {
- struct btrfs_ioctl_fs_info_args fsi = {};
- uint64_t id;
-
- assert(fd >= 0);
- assert(dev);
-
- if (ioctl(fd, BTRFS_IOC_FS_INFO, &fsi) < 0)
- return -errno;
-
- /* We won't do this for btrfs RAID */
- if (fsi.num_devices != 1)
- return 0;
-
- for (id = 1; id <= fsi.max_id; id++) {
- struct btrfs_ioctl_dev_info_args di = {
- .devid = id,
- };
- struct stat st;
-
- if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di) < 0) {
- if (errno == ENODEV)
- continue;
-
- return -errno;
- }
-
- if (stat((char*) di.path, &st) < 0)
- return -errno;
-
- if (!S_ISBLK(st.st_mode))
- return -ENODEV;
-
- if (major(st.st_rdev) == 0)
- return -ENODEV;
-
- *dev = st.st_rdev;
- return 1;
- }
-
- return -ENODEV;
-}
-
-int btrfs_get_block_device(const char *path, dev_t *dev) {
- _cleanup_close_ int fd = -1;
-
- assert(path);
- assert(dev);
-
- fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- return btrfs_get_block_device_fd(fd, dev);
-}
-
-int btrfs_subvol_get_id_fd(int fd, uint64_t *ret) {
- struct btrfs_ioctl_ino_lookup_args args = {
- .objectid = BTRFS_FIRST_FREE_OBJECTID
- };
-
- assert(fd >= 0);
- assert(ret);
-
- if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
- return -errno;
-
- *ret = args.treeid;
- return 0;
-}
-
-static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args *args) {
- assert(args);
-
- /* the objectid, type, offset together make up the btrfs key,
- * which is considered a single 136byte integer when
- * comparing. This call increases the counter by one, dealing
- * with the overflow between the overflows */
-
- if (args->key.min_offset < (uint64_t) -1) {
- args->key.min_offset++;
- return true;
- }
-
- if (args->key.min_type < (uint8_t) -1) {
- args->key.min_type++;
- args->key.min_offset = 0;
- return true;
- }
-
- if (args->key.min_objectid < (uint64_t) -1) {
- args->key.min_objectid++;
- args->key.min_offset = 0;
- args->key.min_type = 0;
- return true;
- }
-
- return 0;
-}
-
-static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args *args, const struct btrfs_ioctl_search_header *h) {
- assert(args);
- assert(h);
-
- args->key.min_objectid = h->objectid;
- args->key.min_type = h->type;
- args->key.min_offset = h->offset;
-}
-
-static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args *args) {
- assert(args);
-
- /* Compare min and max */
-
- if (args->key.min_objectid < args->key.max_objectid)
- return -1;
- if (args->key.min_objectid > args->key.max_objectid)
- return 1;
-
- if (args->key.min_type < args->key.max_type)
- return -1;
- if (args->key.min_type > args->key.max_type)
- return 1;
-
- if (args->key.min_offset < args->key.max_offset)
- return -1;
- if (args->key.min_offset > args->key.max_offset)
- return 1;
-
- return 0;
-}
-
-#define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
- for ((i) = 0, \
- (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
- (i) < (args).key.nr_items; \
- (i)++, \
- (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
-
-#define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
- ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
-
-int btrfs_subvol_get_info_fd(int fd, BtrfsSubvolInfo *ret) {
- struct btrfs_ioctl_search_args args = {
- /* Tree of tree roots */
- .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
-
- /* Look precisely for the subvolume items */
- .key.min_type = BTRFS_ROOT_ITEM_KEY,
- .key.max_type = BTRFS_ROOT_ITEM_KEY,
-
- .key.min_offset = 0,
- .key.max_offset = (uint64_t) -1,
-
- /* No restrictions on the other components */
- .key.min_transid = 0,
- .key.max_transid = (uint64_t) -1,
- };
-
- uint64_t subvol_id;
- bool found = false;
- int r;
-
- assert(fd >= 0);
- assert(ret);
-
- r = btrfs_subvol_get_id_fd(fd, &subvol_id);
- if (r < 0)
- return r;
-
- args.key.min_objectid = args.key.max_objectid = subvol_id;
-
- while (btrfs_ioctl_search_args_compare(&args) <= 0) {
- const struct btrfs_ioctl_search_header *sh;
- unsigned i;
-
- args.key.nr_items = 256;
- if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
- return -errno;
-
- if (args.key.nr_items <= 0)
- break;
-
- FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
-
- const struct btrfs_root_item *ri;
-
- /* Make sure we start the next search at least from this entry */
- btrfs_ioctl_search_args_set(&args, sh);
-
- if (sh->objectid != subvol_id)
- continue;
- if (sh->type != BTRFS_ROOT_ITEM_KEY)
- continue;
-
- /* Older versions of the struct lacked the otime setting */
- if (sh->len < offsetof(struct btrfs_root_item, otime) + sizeof(struct btrfs_timespec))
- continue;
-
- ri = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
-
- ret->otime = (usec_t) le64toh(ri->otime.sec) * USEC_PER_SEC +
- (usec_t) le32toh(ri->otime.nsec) / NSEC_PER_USEC;
-
- ret->subvol_id = subvol_id;
- ret->read_only = !!(le64toh(ri->flags) & BTRFS_ROOT_SUBVOL_RDONLY);
-
- assert_cc(sizeof(ri->uuid) == sizeof(ret->uuid));
- memcpy(&ret->uuid, ri->uuid, sizeof(ret->uuid));
- memcpy(&ret->parent_uuid, ri->parent_uuid, sizeof(ret->parent_uuid));
-
- found = true;
- goto finish;
- }
-
- /* Increase search key by one, to read the next item, if we can. */
- if (!btrfs_ioctl_search_args_inc(&args))
- break;
- }
-
-finish:
- if (!found)
- return -ENODATA;
-
- return 0;
-}
-
-int btrfs_subvol_get_quota_fd(int fd, BtrfsQuotaInfo *ret) {
-
- struct btrfs_ioctl_search_args args = {
- /* Tree of quota items */
- .key.tree_id = BTRFS_QUOTA_TREE_OBJECTID,
-
- /* The object ID is always 0 */
- .key.min_objectid = 0,
- .key.max_objectid = 0,
-
- /* Look precisely for the quota items */
- .key.min_type = BTRFS_QGROUP_STATUS_KEY,
- .key.max_type = BTRFS_QGROUP_LIMIT_KEY,
-
- /* No restrictions on the other components */
- .key.min_transid = 0,
- .key.max_transid = (uint64_t) -1,
- };
-
- uint64_t subvol_id;
- bool found_info = false, found_limit = false;
- int r;
-
- assert(fd >= 0);
- assert(ret);
-
- r = btrfs_subvol_get_id_fd(fd, &subvol_id);
- if (r < 0)
- return r;
-
- args.key.min_offset = args.key.max_offset = subvol_id;
-
- while (btrfs_ioctl_search_args_compare(&args) <= 0) {
- const struct btrfs_ioctl_search_header *sh;
- unsigned i;
-
- args.key.nr_items = 256;
- if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
- return -errno;
-
- if (args.key.nr_items <= 0)
- break;
-
- FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
-
- /* Make sure we start the next search at least from this entry */
- btrfs_ioctl_search_args_set(&args, sh);
-
- if (sh->objectid != 0)
- continue;
- if (sh->offset != subvol_id)
- continue;
-
- if (sh->type == BTRFS_QGROUP_INFO_KEY) {
- const struct btrfs_qgroup_info_item *qii = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
-
- ret->referenced = le64toh(qii->rfer);
- ret->exclusive = le64toh(qii->excl);
-
- found_info = true;
-
- } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
- const struct btrfs_qgroup_limit_item *qli = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
-
- ret->referenced_max = le64toh(qli->max_rfer);
- ret->exclusive_max = le64toh(qli->max_excl);
-
- if (ret->referenced_max == 0)
- ret->referenced_max = (uint64_t) -1;
- if (ret->exclusive_max == 0)
- ret->exclusive_max = (uint64_t) -1;
-
- found_limit = true;
- }
-
- if (found_info && found_limit)
- goto finish;
- }
-
- /* Increase search key by one, to read the next item, if we can. */
- if (!btrfs_ioctl_search_args_inc(&args))
- break;
- }
-
-finish:
- if (!found_limit && !found_info)
- return -ENODATA;
-
- if (!found_info) {
- ret->referenced = (uint64_t) -1;
- ret->exclusive = (uint64_t) -1;
- }
-
- if (!found_limit) {
- ret->referenced_max = (uint64_t) -1;
- ret->exclusive_max = (uint64_t) -1;
- }
-
- return 0;
-}
-
-int btrfs_defrag_fd(int fd) {
- assert(fd >= 0);
-
- if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_defrag(const char *p) {
- _cleanup_close_ int fd = -1;
-
- fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
- if (fd < 0)
- return -errno;
-
- return btrfs_defrag_fd(fd);
-}
-
-int btrfs_quota_enable_fd(int fd, bool b) {
- struct btrfs_ioctl_quota_ctl_args args = {
- .cmd = b ? BTRFS_QUOTA_CTL_ENABLE : BTRFS_QUOTA_CTL_DISABLE,
- };
-
- assert(fd >= 0);
-
- if (ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args) < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_quota_enable(const char *path, bool b) {
- _cleanup_close_ int fd = -1;
-
- fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
- if (fd < 0)
- return -errno;
-
- return btrfs_quota_enable_fd(fd, b);
-}
-
-int btrfs_quota_limit_fd(int fd, uint64_t referenced_max) {
- struct btrfs_ioctl_qgroup_limit_args args = {
- .lim.max_rfer =
- referenced_max == (uint64_t) -1 ? 0 :
- referenced_max == 0 ? 1 : referenced_max,
- .lim.flags = BTRFS_QGROUP_LIMIT_MAX_RFER,
- };
-
- assert(fd >= 0);
-
- if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args) < 0)
- return -errno;
-
- return 0;
-}
-
-int btrfs_quota_limit(const char *path, uint64_t referenced_max) {
- _cleanup_close_ int fd = -1;
-
- fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
- if (fd < 0)
- return -errno;
-
- return btrfs_quota_limit_fd(fd, referenced_max);
-}
-
-int btrfs_resize_loopback_fd(int fd, uint64_t new_size, bool grow_only) {
- struct btrfs_ioctl_vol_args args = {};
- _cleanup_free_ char *p = NULL, *loop = NULL, *backing = NULL;
- _cleanup_close_ int loop_fd = -1, backing_fd = -1;
- struct stat st;
- dev_t dev = 0;
- int r;
-
- /* btrfs cannot handle file systems < 16M, hence use this as minimum */
- if (new_size < 16*1024*1024)
- new_size = 16*1024*1024;
-
- r = btrfs_get_block_device_fd(fd, &dev);
- if (r < 0)
- return r;
- if (r == 0)
- return -ENODEV;
-
- if (asprintf(&p, "/sys/dev/block/%u:%u/loop/backing_file", major(dev), minor(dev)) < 0)
- return -ENOMEM;
- r = read_one_line_file(p, &backing);
- if (r == -ENOENT)
- return -ENODEV;
- if (r < 0)
- return r;
- if (isempty(backing) || !path_is_absolute(backing))
- return -ENODEV;
-
- backing_fd = open(backing, O_RDWR|O_CLOEXEC|O_NOCTTY);
- if (backing_fd < 0)
- return -errno;
-
- if (fstat(backing_fd, &st) < 0)
- return -errno;
- if (!S_ISREG(st.st_mode))
- return -ENODEV;
-
- if (new_size == (uint64_t) st.st_size)
- return 0;
-
- if (grow_only && new_size < (uint64_t) st.st_size)
- return -EINVAL;
-
- if (asprintf(&loop, "/dev/block/%u:%u", major(dev), minor(dev)) < 0)
- return -ENOMEM;
- loop_fd = open(loop, O_RDWR|O_CLOEXEC|O_NOCTTY);
- if (loop_fd < 0)
- return -errno;
-
- if (snprintf(args.name, sizeof(args.name), "%" PRIu64, new_size) >= (int) sizeof(args.name))
- return -EINVAL;
-
- if (new_size < (uint64_t) st.st_size) {
- /* Decrease size: first decrease btrfs size, then shorten loopback */
- if (ioctl(fd, BTRFS_IOC_RESIZE, &args) < 0)
- return -errno;
- }
-
- if (ftruncate(backing_fd, new_size) < 0)
- return -errno;
-
- if (ioctl(loop_fd, LOOP_SET_CAPACITY, 0) < 0)
- return -errno;
-
- if (new_size > (uint64_t) st.st_size) {
- /* Increase size: first enlarge loopback, then increase btrfs size */
- if (ioctl(fd, BTRFS_IOC_RESIZE, &args) < 0)
- return -errno;
- }
-
- /* Make sure the free disk space is correctly updated for both file systems */
- (void) fsync(fd);
- (void) fsync(backing_fd);
-
- return 1;
-}
-
-int btrfs_resize_loopback(const char *p, uint64_t new_size, bool grow_only) {
- _cleanup_close_ int fd = -1;
-
- fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- return btrfs_resize_loopback_fd(fd, new_size, grow_only);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#pragma once
-
-#include <stdbool.h>
-#include <sys/types.h>
-
-#include "time-util.h"
-
-typedef struct BtrfsSubvolInfo {
- uint64_t subvol_id;
- usec_t otime;
-
- sd_id128_t uuid;
- sd_id128_t parent_uuid;
-
- bool read_only;
-} BtrfsSubvolInfo;
-
-typedef struct BtrfsQuotaInfo {
- uint64_t referenced;
- uint64_t exclusive;
- uint64_t referenced_max;
- uint64_t exclusive_max;
-} BtrfsQuotaInfo;
-
-int btrfs_is_snapshot(int fd);
-
-int btrfs_subvol_make(const char *path);
-int btrfs_subvol_make_label(const char *path);
-int btrfs_subvol_remove(const char *path);
-
-int btrfs_subvol_snapshot_fd(int old_fd, const char *new_path, bool read_only, bool fallback_copy);
-int btrfs_subvol_snapshot(const char *old_path, const char *new_path, bool read_only, bool fallback_copy);
-
-int btrfs_subvol_set_read_only_fd(int fd, bool b);
-int btrfs_subvol_set_read_only(const char *path, bool b);
-int btrfs_subvol_get_read_only_fd(int fd);
-int btrfs_subvol_get_id_fd(int fd, uint64_t *ret);
-int btrfs_subvol_get_info_fd(int fd, BtrfsSubvolInfo *info);
-int btrfs_subvol_get_quota_fd(int fd, BtrfsQuotaInfo *quota);
-
-int btrfs_reflink(int infd, int outfd);
-int btrfs_clone_range(int infd, uint64_t in_offset, int ofd, uint64_t out_offset, uint64_t sz);
-
-int btrfs_get_block_device_fd(int fd, dev_t *dev);
-int btrfs_get_block_device(const char *path, dev_t *dev);
-
-int btrfs_defrag_fd(int fd);
-int btrfs_defrag(const char *p);
-
-int btrfs_quota_enable_fd(int fd, bool b);
-int btrfs_quota_enable(const char *path, bool b);
-
-int btrfs_quota_limit_fd(int fd, uint64_t referenced_max);
-int btrfs_quota_limit(const char *path, uint64_t referenced_max);
-
-int btrfs_resize_loopback_fd(int fd, uint64_t size, bool grow_only);
-int btrfs_resize_loopback(const char *path, uint64_t size, bool grow_only);
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <sys/socket.h>
+// #include <sys/socket.h>
-#include "sd-daemon.h"
-#include "sd-event.h"
-#include "util.h"
+// #include "sd-daemon.h"
+// #include "sd-event.h"
+// #include "util.h"
#include "strv.h"
-#include "macro.h"
-#include "def.h"
-#include "path-util.h"
-#include "missing.h"
-#include "set.h"
-
-#include "sd-bus.h"
-#include "bus-error.h"
+// #include "macro.h"
+// #include "def.h"
+// #include "path-util.h"
+// #include "missing.h"
+// #include "set.h"
+// #include "signal-util.h"
+#include "unit-name.h"
+
+// #include "sd-bus.h"
+// #include "bus-error.h"
+// #include "bus-label.h"
#include "bus-message.h"
#include "bus-util.h"
#include "bus-internal.h"
-static int name_owner_change_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
+/// UNNEEDED by elogind
+#if 0
+static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
sd_event *e = userdata;
- assert(bus);
assert(m);
assert(e);
- sd_bus_close(bus);
+ sd_bus_close(sd_bus_message_get_bus(m));
sd_event_exit(e, 0);
return 1;
return code;
}
+#endif // 0
int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
_cleanup_bus_message_unref_ sd_bus_message *rep = NULL;
if (r < 0)
return r;
+ /* Don't trust augmented credentials for authorization */
+ assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
+
r = sd_bus_creds_get_euid(creds, &sender_uid);
if (r < 0)
return r;
return sender_uid == good_user;
}
-int bus_verify_polkit(
+int bus_test_polkit(
sd_bus_message *call,
int capability,
const char *action,
- bool interactive,
+ const char **details,
uid_t good_user,
bool *_challenge,
sd_bus_error *e) {
assert(call);
assert(action);
+ /* Tests non-interactively! */
+
r = check_good_user(call, good_user);
if (r != 0)
return r;
return 1;
#ifdef ENABLE_POLKIT
else {
+ _cleanup_bus_message_unref_ sd_bus_message *request = NULL;
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- int authorized = false, challenge = false, c;
- const char *sender;
+ int authorized = false, challenge = false;
+ const char *sender, **k, **v;
sender = sd_bus_message_get_sender(call);
if (!sender)
return -EBADMSG;
- c = sd_bus_message_get_allow_interactive_authorization(call);
- if (c < 0)
- return c;
- if (c > 0)
- interactive = true;
-
- r = sd_bus_call_method(
+ r = sd_bus_message_new_method_call(
call->bus,
+ &request,
"org.freedesktop.PolicyKit1",
"/org/freedesktop/PolicyKit1/Authority",
"org.freedesktop.PolicyKit1.Authority",
- "CheckAuthorization",
- e,
- &reply,
- "(sa{sv})sa{ss}us",
+ "CheckAuthorization");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(
+ request,
+ "(sa{sv})s",
"system-bus-name", 1, "name", "s", sender,
- action,
- 0,
- !!interactive,
- "");
+ action);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_open_container(request, 'a', "{ss}");
+ if (r < 0)
+ return r;
+
+ STRV_FOREACH_PAIR(k, v, details) {
+ r = sd_bus_message_append(request, "{ss}", *k, *v);
+ if (r < 0)
+ return r;
+ }
+
+ r = sd_bus_message_close_container(request);
+ if (r < 0)
+ return r;
+ r = sd_bus_message_append(request, "us", 0, NULL);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_call(call->bus, request, 0, e, &reply);
if (r < 0) {
/* Treat no PK available as access denied */
if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
free(q);
}
-static int async_polkit_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
+static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
_cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
AsyncPolkitQuery *q = userdata;
int r;
- assert(bus);
assert(reply);
assert(q);
goto finish;
}
- r = q->callback(bus, q->request, q->userdata, &error_buffer);
+ r = q->callback(q->request, q->userdata, &error_buffer);
r = bus_maybe_reply_error(q->request, r, &error_buffer);
finish:
sd_bus_message *call,
int capability,
const char *action,
+ const char **details,
bool interactive,
uid_t good_user,
Hashmap **registry,
#ifdef ENABLE_POLKIT
_cleanup_bus_message_unref_ sd_bus_message *pk = NULL;
AsyncPolkitQuery *q;
- const char *sender;
+ const char *sender, **k, **v;
sd_bus_message_handler_t callback;
void *userdata;
int c;
r = sd_bus_message_append(
pk,
- "(sa{sv})sa{ss}us",
+ "(sa{sv})s",
"system-bus-name", 1, "name", "s", sender,
- action,
- 0,
- !!interactive,
- NULL);
+ action);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_open_container(pk, 'a', "{ss}");
+ if (r < 0)
+ return r;
+
+ STRV_FOREACH_PAIR(k, v, details) {
+ r = sd_bus_message_append(pk, "{ss}", *k, *v);
+ if (r < 0)
+ return r;
+ }
+
+ r = sd_bus_message_close_container(pk);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(pk, "us", !!interactive, NULL);
if (r < 0)
return r;
#endif
}
+/// UNNEEDED by elogind
+#if 0
int bus_check_peercred(sd_bus *c) {
struct ucred ucred;
socklen_t l;
* directly to the system instance, instead of going via the
* bus */
-#ifdef ENABLE_KDBUS
r = sd_bus_new(&bus);
if (r < 0)
return r;
}
bus = sd_bus_unref(bus);
-#endif
r = sd_bus_new(&bus);
if (r < 0)
assert(_bus);
-#ifdef ENABLE_KDBUS
r = sd_bus_new(&bus);
if (r < 0)
return r;
}
bus = sd_bus_unref(bus);
-#endif
e = secure_getenv("XDG_RUNTIME_DIR");
if (!e)
return 0;
}
+#endif // 0
int bus_print_property(const char *name, sd_bus_message *property, bool all) {
char type;
return 1;
}
+ case SD_BUS_TYPE_INT64: {
+ int64_t i;
+
+ r = sd_bus_message_read_basic(property, type, &i);
+ if (r < 0)
+ return r;
+
+ printf("%s=%lld\n", name, (long long) i);
+
+ return 1;
+ }
+
case SD_BUS_TYPE_UINT32: {
uint32_t u;
switch (type) {
case SD_BUS_TYPE_STRING: {
const char *s;
- char *str;
char **p = userdata;
r = sd_bus_message_read_basic(m, type, &s);
if (isempty(s))
break;
- str = strdup(s);
- if (!str) {
- r = -ENOMEM;
- break;
- }
- free(*p);
- *p = str;
-
+ r = free_and_strdup(p, s);
break;
}
return r;
}
-int bus_message_map_all_properties(sd_bus *bus,
- sd_bus_message *m,
- const struct bus_properties_map *map,
- void *userdata) {
+int bus_message_map_all_properties(
+ sd_bus_message *m,
+ const struct bus_properties_map *map,
+ void *userdata) {
+
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
- assert(bus);
assert(m);
assert(map);
v = (uint8_t *)userdata + prop->offset;
if (map[i].set)
- r = prop->set(bus, member, m, &error, v);
+ r = prop->set(sd_bus_message_get_bus(m), member, m, &error, v);
else
- r = map_basic(bus, member, m, &error, v);
+ r = map_basic(sd_bus_message_get_bus(m), member, m, &error, v);
if (r < 0)
return r;
if (r < 0)
return r;
}
+ if (r < 0)
+ return r;
return sd_bus_message_exit_container(m);
}
-int bus_message_map_properties_changed(sd_bus *bus,
- sd_bus_message *m,
- const struct bus_properties_map *map,
- void *userdata) {
+/// UNNEEDED by elogind
+#if 0
+int bus_message_map_properties_changed(
+ sd_bus_message *m,
+ const struct bus_properties_map *map,
+ void *userdata) {
+
const char *member;
int r, invalidated, i;
- assert(bus);
assert(m);
assert(map);
- r = bus_message_map_all_properties(bus, m, map, userdata);
+ r = bus_message_map_all_properties(m, map, userdata);
if (r < 0)
return r;
++invalidated;
break;
}
+ if (r < 0)
+ return r;
r = sd_bus_message_exit_container(m);
if (r < 0)
return invalidated;
}
+#endif // 0
+
+int bus_map_all_properties(
+ sd_bus *bus,
+ const char *destination,
+ const char *path,
+ const struct bus_properties_map *map,
+ void *userdata) {
-int bus_map_all_properties(sd_bus *bus,
- const char *destination,
- const char *path,
- const struct bus_properties_map *map,
- void *userdata) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
if (r < 0)
return r;
- return bus_message_map_all_properties(bus, m, map, userdata);
+ return bus_message_map_all_properties(m, map, userdata);
}
int bus_open_transport(BusTransport transport, const char *host, bool user, sd_bus **bus) {
switch (transport) {
case BUS_TRANSPORT_LOCAL:
+/// elogind does not support a user bus
+#if 0
if (user)
r = sd_bus_default_user(bus);
else
+#endif // 0
r = sd_bus_default_system(bus);
break;
return r;
}
+/// UNNEEDED by elogind
+#if 0
int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
int r;
return r;
}
+#endif // 0
int bus_property_get_bool(
sd_bus *bus,
return log_error_errno(r, "Failed to parse bus message: %m");
}
+/// UNNEEDED by elogind
+#if 0
int bus_log_create_error(int r) {
return log_error_errno(r, "Failed to create bus message: %m");
}
&u->job_path);
}
-int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error) {
- assert(m);
-
- if (r < 0) {
- if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
- sd_bus_reply_method_errno(m, r, error);
-
- } else if (sd_bus_error_is_set(error)) {
- if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
- sd_bus_reply_method_error(m, error);
- } else
- return r;
-
- log_debug("Failed to process message [type=%s sender=%s path=%s interface=%s member=%s signature=%s]: %s",
- bus_message_type_to_string(m->header->type),
- strna(m->sender),
- strna(m->path),
- strna(m->interface),
- strna(m->member),
- strna(m->root_container.signature),
- bus_error_message(error, r));
-
- return 1;
-}
-
int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignment) {
const char *eq, *field;
int r;
if (STR_IN_SET(field,
"CPUAccounting", "MemoryAccounting", "BlockIOAccounting",
- "SendSIGHUP", "SendSIGKILL", "WakeSystem", "DefaultDependencies")) {
+ "SendSIGHUP", "SendSIGKILL", "WakeSystem", "DefaultDependencies",
+ "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "RemainAfterExit")) {
r = parse_boolean(eq);
if (r < 0) {
r = sd_bus_message_append(m, "v", "t", u);
- } else if (STR_IN_SET(field, "User", "Group", "DevicePolicy", "KillMode"))
+ } else if (STR_IN_SET(field,
+ "User", "Group", "DevicePolicy", "KillMode",
+ "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
+ "StandardInput", "StandardOutput", "StandardError",
+ "Description", "Slice", "Type"))
r = sd_bus_message_append(m, "v", "s", eq);
else if (streq(field, "DeviceAllow")) {
return 0;
}
+#endif // 0
typedef struct BusWaitForJobs {
sd_bus *bus;
sd_bus_slot *slot_disconnected;
} BusWaitForJobs;
-static int match_disconnected(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
- assert(bus);
+/// UNNEEDED by elogind
+#if 0
+static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *error) {
assert(m);
log_error("Warning! D-Bus connection terminated.");
- sd_bus_close(bus);
+ sd_bus_close(sd_bus_message_get_bus(m));
return 0;
}
-static int match_job_removed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
const char *path, *unit, *result;
BusWaitForJobs *d = userdata;
uint32_t id;
char *found;
int r;
- assert(bus);
assert(m);
assert(d);
}
}
+static int bus_job_get_service_result(BusWaitForJobs *d, char **result) {
+ _cleanup_free_ char *dbus_path = NULL;
+
+ assert(d);
+ assert(d->name);
+ assert(result);
+
+ dbus_path = unit_dbus_path_from_name(d->name);
+ if (!dbus_path)
+ return -ENOMEM;
+
+ return sd_bus_get_property_string(d->bus,
+ "org.freedesktop.systemd1",
+ dbus_path,
+ "org.freedesktop.systemd1.Service",
+ "Result",
+ NULL,
+ result);
+}
+
+static const struct {
+ const char *result, *explanation;
+} explanations [] = {
+ { "resources", "a configured resource limit was exceeded" },
+ { "timeout", "a timeout was exceeded" },
+ { "exit-code", "the control process exited with error code" },
+ { "signal", "a fatal signal was delivered to the control process" },
+ { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
+ { "watchdog", "the service failed to send watchdog ping" },
+ { "start-limit", "start of the service was attempted too often" }
+};
+
+static void log_job_error_with_service_result(const char* service, const char *result) {
+ _cleanup_free_ char *service_shell_quoted = NULL;
+
+ assert(service);
+
+ service_shell_quoted = shell_maybe_quote(service);
+
+ if (!isempty(result)) {
+ unsigned i;
+
+ for (i = 0; i < ELEMENTSOF(explanations); ++i)
+ if (streq(result, explanations[i].result))
+ break;
+
+ if (i < ELEMENTSOF(explanations)) {
+ log_error("Job for %s failed because %s. See \"systemctl status %s\" and \"journalctl -xe\" for details.\n",
+ service,
+ explanations[i].explanation,
+ strna(service_shell_quoted));
+
+ goto finish;
+ }
+ }
+
+ log_error("Job for %s failed. See \"systemctl status %s\" and \"journalctl -xe\" for details.\n",
+ service,
+ strna(service_shell_quoted));
+
+finish:
+ /* For some results maybe additional explanation is required */
+ if (streq_ptr(result, "start-limit"))
+ log_info("To force a start use \"systemctl reset-failed %1$s\" followed by \"systemctl start %1$s\" again.",
+ strna(service_shell_quoted));
+}
+
static int check_wait_response(BusWaitForJobs *d, bool quiet) {
int r = 0;
log_error("Operation on or unit type of %s not supported on this system.", strna(d->name));
else if (!streq(d->result, "done") && !streq(d->result, "skipped")) {
if (d->name) {
- bool quotes;
+ int q;
+ _cleanup_free_ char *result = NULL;
- quotes = chars_intersect(d->name, SHELL_NEED_QUOTES);
+ q = bus_job_get_service_result(d, &result);
+ if (q < 0)
+ log_debug_errno(q, "Failed to get Result property of service %s: %m", d->name);
- log_error("Job for %s failed. See \"systemctl status %s%s%s\" and \"journalctl -xe\" for details.",
- d->name,
- quotes ? "'" : "", d->name, quotes ? "'" : "");
+ log_job_error_with_service_result(d->name, result);
} else
log_error("Job failed. See \"journalctl -xe\" for details.");
}
log_debug_errno(q, "Got result %s/%m for job %s", strna(d->result), strna(d->name));
}
- free(d->name);
- d->name = NULL;
+ d->name = mfree(d->name);
- free(d->result);
- d->result = NULL;
+ d->result = mfree(d->result);
}
return r;
return set_put_strdup(d->jobs, path);
}
-int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet) {
+int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
+ int r;
+
+ r = bus_wait_for_jobs_add(d, path);
+ if (r < 0)
+ return log_oom();
+
+ return bus_wait_for_jobs(d, quiet);
+}
+
+int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, unsigned *n_changes) {
const char *type, *path, *source;
int r;
else
log_info("Removed symlink %s.", path);
}
+
+ r = unit_file_changes_add(changes, n_changes, streq(type, "symlink") ? UNIT_FILE_SYMLINK : UNIT_FILE_UNLINK, path, source);
+ if (r < 0)
+ return r;
}
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
+
+/**
+ * bus_path_encode_unique() - encode unique object path
+ * @b: bus connection or NULL
+ * @prefix: object path prefix
+ * @sender_id: unique-name of client, or NULL
+ * @external_id: external ID to be chosen by client, or NULL
+ * @ret_path: storage for encoded object path pointer
+ *
+ * Whenever we provide a bus API that allows clients to create and manage
+ * server-side objects, we need to provide a unique name for these objects. If
+ * we let the server choose the name, we suffer from a race condition: If a
+ * client creates an object asynchronously, it cannot destroy that object until
+ * it received the method reply. It cannot know the name of the new object,
+ * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
+ *
+ * Therefore, many APIs allow the client to choose the unique name for newly
+ * created objects. There're two problems to solve, though:
+ * 1) Object names are usually defined via dbus object paths, which are
+ * usually globally namespaced. Therefore, multiple clients must be able
+ * to choose unique object names without interference.
+ * 2) If multiple libraries share the same bus connection, they must be
+ * able to choose unique object names without interference.
+ * The first problem is solved easily by prefixing a name with the
+ * unique-bus-name of a connection. The server side must enforce this and
+ * reject any other name. The second problem is solved by providing unique
+ * suffixes from within sd-bus.
+ *
+ * This helper allows clients to create unique object-paths. It uses the
+ * template '/prefix/sender_id/external_id' and returns the new path in
+ * @ret_path (must be freed by the caller).
+ * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
+ * NULL, this function allocates a unique suffix via @b (by requesting a new
+ * cookie). If both @sender_id and @external_id are given, @b can be passed as
+ * NULL.
+ *
+ * Returns: 0 on success, negative error code on failure.
+ */
+int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
+ _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
+ char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
+ int r;
+
+ assert_return(b || (sender_id && external_id), -EINVAL);
+ assert_return(object_path_is_valid(prefix), -EINVAL);
+ assert_return(ret_path, -EINVAL);
+
+ if (!sender_id) {
+ r = sd_bus_get_unique_name(b, &sender_id);
+ if (r < 0)
+ return r;
+ }
+
+ if (!external_id) {
+ xsprintf(external_buf, "%"PRIu64, ++b->cookie);
+ external_id = external_buf;
+ }
+
+ sender_label = bus_label_escape(sender_id);
+ if (!sender_label)
+ return -ENOMEM;
+
+ external_label = bus_label_escape(external_id);
+ if (!external_label)
+ return -ENOMEM;
+
+ p = strjoin(prefix, "/", sender_label, "/", external_label, NULL);
+ if (!p)
+ return -ENOMEM;
+
+ *ret_path = p;
+ return 0;
+}
+
+/**
+ * bus_path_decode_unique() - decode unique object path
+ * @path: object path to decode
+ * @prefix: object path prefix
+ * @ret_sender: output parameter for sender-id label
+ * @ret_external: output parameter for external-id label
+ *
+ * This does the reverse of bus_path_encode_unique() (see its description for
+ * details). Both trailing labels, sender-id and external-id, are unescaped and
+ * returned in the given output parameters (the caller must free them).
+ *
+ * Note that this function returns 0 if the path does not match the template
+ * (see bus_path_encode_unique()), 1 if it matched.
+ *
+ * Returns: Negative error code on failure, 0 if the given object path does not
+ * match the template (return parameters are set to NULL), 1 if it was
+ * parsed successfully (return parameters contain allocated labels).
+ */
+int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
+ const char *p, *q;
+ char *sender, *external;
+
+ assert(object_path_is_valid(path));
+ assert(object_path_is_valid(prefix));
+ assert(ret_sender);
+ assert(ret_external);
+
+ p = object_path_startswith(path, prefix);
+ if (!p) {
+ *ret_sender = NULL;
+ *ret_external = NULL;
+ return 0;
+ }
+
+ q = strchr(p, '/');
+ if (!q) {
+ *ret_sender = NULL;
+ *ret_external = NULL;
+ return 0;
+ }
+
+ sender = bus_label_unescape_n(p, q - p);
+ external = bus_label_unescape(q + 1);
+ if (!sender || !external) {
+ free(sender);
+ free(external);
+ return -ENOMEM;
+ }
+
+ *ret_sender = sender;
+ *ret_external = external;
+ return 1;
+}
+#endif // 0
+
+bool is_kdbus_wanted(void) {
+ _cleanup_free_ char *value = NULL;
+#ifdef ENABLE_KDBUS
+ const bool configured = true;
+#else
+ const bool configured = false;
+#endif
+
+ int r;
+
+ if (get_proc_cmdline_key("kdbus", NULL) > 0)
+ return true;
+
+ r = get_proc_cmdline_key("kdbus=", &value);
+ if (r <= 0)
+ return configured;
+
+ return parse_boolean(value) == 1;
+}
+
+bool is_kdbus_available(void) {
+ _cleanup_close_ int fd = -1;
+ struct kdbus_cmd cmd = { .size = sizeof(cmd), .flags = KDBUS_FLAG_NEGOTIATE };
+
+ if (!is_kdbus_wanted())
+ return false;
+
+ fd = open("/sys/fs/kdbus/control", O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
+ if (fd < 0)
+ return false;
+
+ return ioctl(fd, KDBUS_CMD_BUS_MAKE, &cmd) >= 0;
+}
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include "sd-event.h"
+#include <sys/types.h>
+#include <inttypes.h>
+#include <stdbool.h>
+
+// #include "sd-event.h"
#include "sd-bus.h"
#include "hashmap.h"
-#include "time-util.h"
+// #include "install.h"
+// #include "time-util.h"
typedef enum BusTransport {
BUS_TRANSPORT_LOCAL,
int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata);
-int bus_message_map_all_properties(sd_bus *bus,
- sd_bus_message *m,
- const struct bus_properties_map *map,
- void *userdata);
-int bus_message_map_properties_changed(sd_bus *bus,
- sd_bus_message *m,
- const struct bus_properties_map *map,
- void *userdata);
-int bus_map_all_properties(sd_bus *bus,
- const char *destination,
- const char *path,
- const struct bus_properties_map *map,
- void *userdata);
-
-int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name);
+int bus_message_map_all_properties(sd_bus_message *m, const struct bus_properties_map *map, void *userdata);
+// UNNEEDED int bus_message_map_properties_changed(sd_bus_message *m, const struct bus_properties_map *map, void *userdata);
+int bus_map_all_properties(sd_bus *bus, const char *destination, const char *path, const struct bus_properties_map *map, void *userdata);
+
+// UNNEEDED int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name);
typedef bool (*check_idle_t)(void *userdata);
-int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout, check_idle_t check_idle, void *userdata);
+// UNNEEDED int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout, check_idle_t check_idle, void *userdata);
int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error);
-int bus_check_peercred(sd_bus *c);
+// UNNEEDED int bus_check_peercred(sd_bus *c);
-int bus_verify_polkit(sd_bus_message *call, int capability, const char *action, bool interactive, uid_t good_user, bool *_challenge, sd_bus_error *e);
+int bus_test_polkit(sd_bus_message *call, int capability, const char *action, const char **details, uid_t good_user, bool *_challenge, sd_bus_error *e);
-int bus_verify_polkit_async(sd_bus_message *call, int capability, const char *action, bool interactive, uid_t good_user, Hashmap **registry, sd_bus_error *error);
+int bus_verify_polkit_async(sd_bus_message *call, int capability, const char *action, const char **details, bool interactive, uid_t good_user, Hashmap **registry, sd_bus_error *error);
void bus_verify_polkit_async_registry_free(Hashmap *registry);
-int bus_open_system_systemd(sd_bus **_bus);
-int bus_open_user_systemd(sd_bus **_bus);
+// UNNEEDED int bus_open_system_systemd(sd_bus **_bus);
+// UNNEEDED int bus_open_user_systemd(sd_bus **_bus);
int bus_open_transport(BusTransport transport, const char *host, bool user, sd_bus **bus);
-int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus);
+// UNNEEDED int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus);
int bus_print_property(const char *name, sd_bus_message *property, bool all);
int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool all);
#define bus_property_get_mode ((sd_bus_property_get_t) NULL)
int bus_log_parse_error(int r);
-int bus_log_create_error(int r);
+// UNNEEDED int bus_log_create_error(int r);
+/// UNNEEDED by elogind
+#if 0
typedef struct UnitInfo {
const char *machine;
const char *id;
const char *job_type;
const char *job_path;
} UnitInfo;
+#endif // 0
-int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u);
-
-static inline void sd_bus_close_unrefp(sd_bus **bus) {
- if (*bus) {
- sd_bus_flush(*bus);
- sd_bus_close(*bus);
- sd_bus_unref(*bus);
- }
-}
+// UNNEEDED int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_unref);
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_flush_close_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_slot*, sd_bus_slot_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message*, sd_bus_message_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_creds*, sd_bus_creds_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_track*, sd_bus_track_unref);
#define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp)
-#define _cleanup_bus_close_unref_ _cleanup_(sd_bus_close_unrefp)
+#define _cleanup_bus_flush_close_unref_ _cleanup_(sd_bus_flush_close_unrefp)
#define _cleanup_bus_slot_unref_ _cleanup_(sd_bus_slot_unrefp)
#define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp)
#define _cleanup_bus_creds_unref_ _cleanup_(sd_bus_creds_unrefp)
SD_BUS_PROPERTY(name, "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, realtime), (flags)), \
SD_BUS_PROPERTY(name "Monotonic", "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, monotonic), (flags))
-int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error);
+// UNNEEDED int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignment);
+
+// UNNEEDED typedef struct BusWaitForJobs BusWaitForJobs;
-int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignment);
+// UNNEEDED int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret);
+// UNNEEDED void bus_wait_for_jobs_free(BusWaitForJobs *d);
+// UNNEEDED int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path);
+// UNNEEDED int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet);
+// UNNEEDED int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet);
-typedef struct BusWaitForJobs BusWaitForJobs;
+// UNNEEDED DEFINE_TRIVIAL_CLEANUP_FUNC(BusWaitForJobs*, bus_wait_for_jobs_free);
-int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret);
-void bus_wait_for_jobs_free(BusWaitForJobs *d);
-int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path);
-int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet);
+// UNNEEDED int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, unsigned *n_changes);
-DEFINE_TRIVIAL_CLEANUP_FUNC(BusWaitForJobs*, bus_wait_for_jobs_free);
+// UNNEEDED int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path);
+// UNNEEDED int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external);
-int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet);
+bool is_kdbus_wanted(void);
+bool is_kdbus_available(void);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <string.h>
-
-#include "calendarspec.h"
-
-#define BITS_WEEKDAYS 127
-
-static void free_chain(CalendarComponent *c) {
- CalendarComponent *n;
-
- while (c) {
- n = c->next;
- free(c);
- c = n;
- }
-}
-
-void calendar_spec_free(CalendarSpec *c) {
-
- if (!c)
- return;
-
- free_chain(c->year);
- free_chain(c->month);
- free_chain(c->day);
- free_chain(c->hour);
- free_chain(c->minute);
- free_chain(c->second);
-
- free(c);
-}
-
-static int component_compare(const void *_a, const void *_b) {
- CalendarComponent * const *a = _a, * const *b = _b;
-
- if ((*a)->value < (*b)->value)
- return -1;
- if ((*a)->value > (*b)->value)
- return 1;
-
- if ((*a)->repeat < (*b)->repeat)
- return -1;
- if ((*a)->repeat > (*b)->repeat)
- return 1;
-
- return 0;
-}
-
-static void sort_chain(CalendarComponent **c) {
- unsigned n = 0, k;
- CalendarComponent **b, *i, **j, *next;
-
- assert(c);
-
- for (i = *c; i; i = i->next)
- n++;
-
- if (n <= 1)
- return;
-
- j = b = alloca(sizeof(CalendarComponent*) * n);
- for (i = *c; i; i = i->next)
- *(j++) = i;
-
- qsort(b, n, sizeof(CalendarComponent*), component_compare);
-
- b[n-1]->next = NULL;
- next = b[n-1];
-
- /* Drop non-unique entries */
- for (k = n-1; k > 0; k--) {
- if (b[k-1]->value == next->value &&
- b[k-1]->repeat == next->repeat) {
- free(b[k-1]);
- continue;
- }
-
- b[k-1]->next = next;
- next = b[k-1];
- }
-
- *c = next;
-}
-
-static void fix_year(CalendarComponent *c) {
- /* Turns 12 → 2012, 89 → 1989 */
-
- while(c) {
- CalendarComponent *n = c->next;
-
- if (c->value >= 0 && c->value < 70)
- c->value += 2000;
-
- if (c->value >= 70 && c->value < 100)
- c->value += 1900;
-
- c = n;
- }
-}
-
-int calendar_spec_normalize(CalendarSpec *c) {
- assert(c);
-
- if (c->weekdays_bits <= 0 || c->weekdays_bits >= BITS_WEEKDAYS)
- c->weekdays_bits = -1;
-
- fix_year(c->year);
-
- sort_chain(&c->year);
- sort_chain(&c->month);
- sort_chain(&c->day);
- sort_chain(&c->hour);
- sort_chain(&c->minute);
- sort_chain(&c->second);
-
- return 0;
-}
-
-_pure_ static bool chain_valid(CalendarComponent *c, int from, int to) {
- if (!c)
- return true;
-
- if (c->value < from || c->value > to)
- return false;
-
- if (c->value + c->repeat > to)
- return false;
-
- if (c->next)
- return chain_valid(c->next, from, to);
-
- return true;
-}
-
-_pure_ bool calendar_spec_valid(CalendarSpec *c) {
- assert(c);
-
- if (c->weekdays_bits > BITS_WEEKDAYS)
- return false;
-
- if (!chain_valid(c->year, 1970, 2199))
- return false;
-
- if (!chain_valid(c->month, 1, 12))
- return false;
-
- if (!chain_valid(c->day, 1, 31))
- return false;
-
- if (!chain_valid(c->hour, 0, 23))
- return false;
-
- if (!chain_valid(c->minute, 0, 59))
- return false;
-
- if (!chain_valid(c->second, 0, 59))
- return false;
-
- return true;
-}
-
-static void format_weekdays(FILE *f, const CalendarSpec *c) {
- static const char *const days[] = {
- "Mon",
- "Tue",
- "Wed",
- "Thu",
- "Fri",
- "Sat",
- "Sun"
- };
-
- int l, x;
- bool need_colon = false;
-
- assert(f);
- assert(c);
- assert(c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS);
-
- for (x = 0, l = -1; x < (int) ELEMENTSOF(days); x++) {
-
- if (c->weekdays_bits & (1 << x)) {
-
- if (l < 0) {
- if (need_colon)
- fputc(',', f);
- else
- need_colon = true;
-
- fputs(days[x], f);
- l = x;
- }
-
- } else if (l >= 0) {
-
- if (x > l + 1) {
- fputc(x > l + 2 ? '-' : ',', f);
- fputs(days[x-1], f);
- }
-
- l = -1;
- }
- }
-
- if (l >= 0 && x > l + 1) {
- fputc(x > l + 2 ? '-' : ',', f);
- fputs(days[x-1], f);
- }
-}
-
-static void format_chain(FILE *f, int space, const CalendarComponent *c) {
- assert(f);
-
- if (!c) {
- fputc('*', f);
- return;
- }
-
- assert(c->value >= 0);
- fprintf(f, "%0*i", space, c->value);
-
- if (c->repeat > 0)
- fprintf(f, "/%i", c->repeat);
-
- if (c->next) {
- fputc(',', f);
- format_chain(f, space, c->next);
- }
-}
-
-int calendar_spec_to_string(const CalendarSpec *c, char **p) {
- char *buf = NULL;
- size_t sz = 0;
- FILE *f;
-
- assert(c);
- assert(p);
-
- f = open_memstream(&buf, &sz);
- if (!f)
- return -ENOMEM;
-
- if (c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS) {
- format_weekdays(f, c);
- fputc(' ', f);
- }
-
- format_chain(f, 4, c->year);
- fputc('-', f);
- format_chain(f, 2, c->month);
- fputc('-', f);
- format_chain(f, 2, c->day);
- fputc(' ', f);
- format_chain(f, 2, c->hour);
- fputc(':', f);
- format_chain(f, 2, c->minute);
- fputc(':', f);
- format_chain(f, 2, c->second);
-
- fflush(f);
-
- if (ferror(f)) {
- free(buf);
- fclose(f);
- return -ENOMEM;
- }
-
- fclose(f);
-
- *p = buf;
- return 0;
-}
-
-static int parse_weekdays(const char **p, CalendarSpec *c) {
- static const struct {
- const char *name;
- const int nr;
- } day_nr[] = {
- { "Monday", 0 },
- { "Mon", 0 },
- { "Tuesday", 1 },
- { "Tue", 1 },
- { "Wednesday", 2 },
- { "Wed", 2 },
- { "Thursday", 3 },
- { "Thu", 3 },
- { "Friday", 4 },
- { "Fri", 4 },
- { "Saturday", 5 },
- { "Sat", 5 },
- { "Sunday", 6 },
- { "Sun", 6 }
- };
-
- int l = -1;
- bool first = true;
-
- assert(p);
- assert(*p);
- assert(c);
-
- for (;;) {
- unsigned i;
-
- if (!first && **p == ' ')
- return 0;
-
- for (i = 0; i < ELEMENTSOF(day_nr); i++) {
- size_t skip;
-
- if (!startswith_no_case(*p, day_nr[i].name))
- continue;
-
- skip = strlen(day_nr[i].name);
-
- if ((*p)[skip] != '-' &&
- (*p)[skip] != ',' &&
- (*p)[skip] != ' ' &&
- (*p)[skip] != 0)
- return -EINVAL;
-
- c->weekdays_bits |= 1 << day_nr[i].nr;
-
- if (l >= 0) {
- int j;
-
- if (l > day_nr[i].nr)
- return -EINVAL;
-
- for (j = l + 1; j < day_nr[i].nr; j++)
- c->weekdays_bits |= 1 << j;
- }
-
- *p += skip;
- break;
- }
-
- /* Couldn't find this prefix, so let's assume the
- weekday was not specified and let's continue with
- the date */
- if (i >= ELEMENTSOF(day_nr))
- return first ? 0 : -EINVAL;
-
- /* We reached the end of the string */
- if (**p == 0)
- return 0;
-
- /* We reached the end of the weekday spec part */
- if (**p == ' ') {
- *p += strspn(*p, " ");
- return 0;
- }
-
- if (**p == '-') {
- if (l >= 0)
- return -EINVAL;
-
- l = day_nr[i].nr;
- } else
- l = -1;
-
- *p += 1;
- first = false;
- }
-}
-
-static int prepend_component(const char **p, CalendarComponent **c) {
- unsigned long value, repeat = 0;
- char *e = NULL, *ee = NULL;
- CalendarComponent *cc;
-
- assert(p);
- assert(c);
-
- errno = 0;
- value = strtoul(*p, &e, 10);
- if (errno > 0)
- return -errno;
- if (e == *p)
- return -EINVAL;
- if ((unsigned long) (int) value != value)
- return -ERANGE;
-
- if (*e == '/') {
- repeat = strtoul(e+1, &ee, 10);
- if (errno > 0)
- return -errno;
- if (ee == e+1)
- return -EINVAL;
- if ((unsigned long) (int) repeat != repeat)
- return -ERANGE;
- if (repeat <= 0)
- return -ERANGE;
-
- e = ee;
- }
-
- if (*e != 0 && *e != ' ' && *e != ',' && *e != '-' && *e != ':')
- return -EINVAL;
-
- cc = new0(CalendarComponent, 1);
- if (!cc)
- return -ENOMEM;
-
- cc->value = value;
- cc->repeat = repeat;
- cc->next = *c;
-
- *p = e;
- *c = cc;
-
- if (*e ==',') {
- *p += 1;
- return prepend_component(p, c);
- }
-
- return 0;
-}
-
-static int parse_chain(const char **p, CalendarComponent **c) {
- const char *t;
- CalendarComponent *cc = NULL;
- int r;
-
- assert(p);
- assert(c);
-
- t = *p;
-
- if (t[0] == '*') {
- *p = t + 1;
- *c = NULL;
- return 0;
- }
-
- r = prepend_component(&t, &cc);
- if (r < 0) {
- free_chain(cc);
- return r;
- }
-
- *p = t;
- *c = cc;
- return 0;
-}
-
-static int const_chain(int value, CalendarComponent **c) {
- CalendarComponent *cc = NULL;
-
- assert(c);
-
- cc = new0(CalendarComponent, 1);
- if (!cc)
- return -ENOMEM;
-
- cc->value = value;
- cc->repeat = 0;
- cc->next = *c;
-
- *c = cc;
-
- return 0;
-}
-
-static int parse_date(const char **p, CalendarSpec *c) {
- const char *t;
- int r;
- CalendarComponent *first, *second, *third;
-
- assert(p);
- assert(*p);
- assert(c);
-
- t = *p;
-
- if (*t == 0)
- return 0;
-
- r = parse_chain(&t, &first);
- if (r < 0)
- return r;
-
- /* Already the end? A ':' as separator? In that case this was a time, not a date */
- if (*t == 0 || *t == ':') {
- free_chain(first);
- return 0;
- }
-
- if (*t != '-') {
- free_chain(first);
- return -EINVAL;
- }
-
- t++;
- r = parse_chain(&t, &second);
- if (r < 0) {
- free_chain(first);
- return r;
- }
-
- /* Got two parts, hence it's month and day */
- if (*t == ' ' || *t == 0) {
- *p = t + strspn(t, " ");
- c->month = first;
- c->day = second;
- return 0;
- }
-
- if (*t != '-') {
- free_chain(first);
- free_chain(second);
- return -EINVAL;
- }
-
- t++;
- r = parse_chain(&t, &third);
- if (r < 0) {
- free_chain(first);
- free_chain(second);
- return r;
- }
-
- /* Got tree parts, hence it is year, month and day */
- if (*t == ' ' || *t == 0) {
- *p = t + strspn(t, " ");
- c->year = first;
- c->month = second;
- c->day = third;
- return 0;
- }
-
- free_chain(first);
- free_chain(second);
- free_chain(third);
- return -EINVAL;
-}
-
-static int parse_time(const char **p, CalendarSpec *c) {
- CalendarComponent *h = NULL, *m = NULL, *s = NULL;
- const char *t;
- int r;
-
- assert(p);
- assert(*p);
- assert(c);
-
- t = *p;
-
- if (*t == 0) {
- /* If no time is specified at all, but a date of some
- * kind, then this means 00:00:00 */
- if (c->day || c->weekdays_bits > 0)
- goto null_hour;
-
- goto finish;
- }
-
- r = parse_chain(&t, &h);
- if (r < 0)
- goto fail;
-
- if (*t != ':') {
- r = -EINVAL;
- goto fail;
- }
-
- t++;
- r = parse_chain(&t, &m);
- if (r < 0)
- goto fail;
-
- /* Already at the end? Then it's hours and minutes, and seconds are 0 */
- if (*t == 0) {
- if (m != NULL)
- goto null_second;
-
- goto finish;
- }
-
- if (*t != ':') {
- r = -EINVAL;
- goto fail;
- }
-
- t++;
- r = parse_chain(&t, &s);
- if (r < 0)
- goto fail;
-
- /* At the end? Then it's hours, minutes and seconds */
- if (*t == 0)
- goto finish;
-
- r = -EINVAL;
- goto fail;
-
-null_hour:
- r = const_chain(0, &h);
- if (r < 0)
- goto fail;
-
- r = const_chain(0, &m);
- if (r < 0)
- goto fail;
-
-null_second:
- r = const_chain(0, &s);
- if (r < 0)
- goto fail;
-
-finish:
- *p = t;
- c->hour = h;
- c->minute = m;
- c->second = s;
- return 0;
-
-fail:
- free_chain(h);
- free_chain(m);
- free_chain(s);
- return r;
-}
-
-int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
- CalendarSpec *c;
- int r;
-
- assert(p);
- assert(spec);
-
- if (isempty(p))
- return -EINVAL;
-
- c = new0(CalendarSpec, 1);
- if (!c)
- return -ENOMEM;
-
- if (strcaseeq(p, "minutely")) {
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else if (strcaseeq(p, "hourly")) {
- r = const_chain(0, &c->minute);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else if (strcaseeq(p, "daily")) {
- r = const_chain(0, &c->hour);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->minute);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else if (strcaseeq(p, "monthly")) {
- r = const_chain(1, &c->day);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->hour);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->minute);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else if (strcaseeq(p, "annually") ||
- strcaseeq(p, "yearly") ||
- strcaseeq(p, "anually") /* backwards compatibility */ ) {
-
- r = const_chain(1, &c->month);
- if (r < 0)
- goto fail;
- r = const_chain(1, &c->day);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->hour);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->minute);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else if (strcaseeq(p, "weekly")) {
-
- c->weekdays_bits = 1;
-
- r = const_chain(0, &c->hour);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->minute);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else if (strcaseeq(p, "quarterly")) {
-
- r = const_chain(1, &c->month);
- if (r < 0)
- goto fail;
- r = const_chain(4, &c->month);
- if (r < 0)
- goto fail;
- r = const_chain(7, &c->month);
- if (r < 0)
- goto fail;
- r = const_chain(10, &c->month);
- if (r < 0)
- goto fail;
- r = const_chain(1, &c->day);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->hour);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->minute);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else if (strcaseeq(p, "biannually") ||
- strcaseeq(p, "bi-annually") ||
- strcaseeq(p, "semiannually") ||
- strcaseeq(p, "semi-annually")) {
-
- r = const_chain(1, &c->month);
- if (r < 0)
- goto fail;
- r = const_chain(7, &c->month);
- if (r < 0)
- goto fail;
- r = const_chain(1, &c->day);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->hour);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->minute);
- if (r < 0)
- goto fail;
- r = const_chain(0, &c->second);
- if (r < 0)
- goto fail;
-
- } else {
- r = parse_weekdays(&p, c);
- if (r < 0)
- goto fail;
-
- r = parse_date(&p, c);
- if (r < 0)
- goto fail;
-
- r = parse_time(&p, c);
- if (r < 0)
- goto fail;
-
- if (*p != 0) {
- r = -EINVAL;
- goto fail;
- }
- }
-
- r = calendar_spec_normalize(c);
- if (r < 0)
- goto fail;
-
- if (!calendar_spec_valid(c)) {
- r = -EINVAL;
- goto fail;
- }
-
- *spec = c;
- return 0;
-
-fail:
- calendar_spec_free(c);
- return r;
-}
-
-static int find_matching_component(const CalendarComponent *c, int *val) {
- const CalendarComponent *n;
- int d = -1;
- bool d_set = false;
- int r;
-
- assert(val);
-
- if (!c)
- return 0;
-
- while (c) {
- n = c->next;
-
- if (c->value >= *val) {
-
- if (!d_set || c->value < d) {
- d = c->value;
- d_set = true;
- }
-
- } else if (c->repeat > 0) {
- int k;
-
- k = c->value + c->repeat * ((*val - c->value + c->repeat -1) / c->repeat);
-
- if (!d_set || k < d) {
- d = k;
- d_set = true;
- }
- }
-
- c = n;
- }
-
- if (!d_set)
- return -ENOENT;
-
- r = *val != d;
- *val = d;
- return r;
-}
-
-static bool tm_out_of_bounds(const struct tm *tm) {
- struct tm t;
- assert(tm);
-
- t = *tm;
-
- if (mktime(&t) == (time_t) -1)
- return true;
-
- /* Did any normalization take place? If so, it was out of bounds before */
- return
- t.tm_year != tm->tm_year ||
- t.tm_mon != tm->tm_mon ||
- t.tm_mday != tm->tm_mday ||
- t.tm_hour != tm->tm_hour ||
- t.tm_min != tm->tm_min ||
- t.tm_sec != tm->tm_sec;
-}
-
-static bool matches_weekday(int weekdays_bits, const struct tm *tm) {
- struct tm t;
- int k;
-
- if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
- return true;
-
- t = *tm;
- if (mktime(&t) == (time_t) -1)
- return false;
-
- k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
- return (weekdays_bits & (1 << k));
-}
-
-static int find_next(const CalendarSpec *spec, struct tm *tm) {
- struct tm c;
- int r;
-
- assert(spec);
- assert(tm);
-
- c = *tm;
-
- for (;;) {
- /* Normalize the current date */
- mktime(&c);
- c.tm_isdst = -1;
-
- c.tm_year += 1900;
- r = find_matching_component(spec->year, &c.tm_year);
- c.tm_year -= 1900;
-
- if (r > 0) {
- c.tm_mon = 0;
- c.tm_mday = 1;
- c.tm_hour = c.tm_min = c.tm_sec = 0;
- }
- if (r < 0 || tm_out_of_bounds(&c))
- return r;
-
- c.tm_mon += 1;
- r = find_matching_component(spec->month, &c.tm_mon);
- c.tm_mon -= 1;
-
- if (r > 0) {
- c.tm_mday = 1;
- c.tm_hour = c.tm_min = c.tm_sec = 0;
- }
- if (r < 0 || tm_out_of_bounds(&c)) {
- c.tm_year ++;
- c.tm_mon = 0;
- c.tm_mday = 1;
- c.tm_hour = c.tm_min = c.tm_sec = 0;
- continue;
- }
-
- r = find_matching_component(spec->day, &c.tm_mday);
- if (r > 0)
- c.tm_hour = c.tm_min = c.tm_sec = 0;
- if (r < 0 || tm_out_of_bounds(&c)) {
- c.tm_mon ++;
- c.tm_mday = 1;
- c.tm_hour = c.tm_min = c.tm_sec = 0;
- continue;
- }
-
- if (!matches_weekday(spec->weekdays_bits, &c)) {
- c.tm_mday++;
- c.tm_hour = c.tm_min = c.tm_sec = 0;
- continue;
- }
-
- r = find_matching_component(spec->hour, &c.tm_hour);
- if (r > 0)
- c.tm_min = c.tm_sec = 0;
- if (r < 0 || tm_out_of_bounds(&c)) {
- c.tm_mday ++;
- c.tm_hour = c.tm_min = c.tm_sec = 0;
- continue;
- }
-
- r = find_matching_component(spec->minute, &c.tm_min);
- if (r > 0)
- c.tm_sec = 0;
- if (r < 0 || tm_out_of_bounds(&c)) {
- c.tm_hour ++;
- c.tm_min = c.tm_sec = 0;
- continue;
- }
-
- r = find_matching_component(spec->second, &c.tm_sec);
- if (r < 0 || tm_out_of_bounds(&c)) {
- c.tm_min ++;
- c.tm_sec = 0;
- continue;
- }
-
-
- *tm = c;
- return 0;
- }
-}
-
-int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
- struct tm tm;
- time_t t;
- int r;
-
- assert(spec);
- assert(next);
-
- t = (time_t) (usec / USEC_PER_SEC) + 1;
- assert_se(localtime_r(&t, &tm));
-
- r = find_next(spec, &tm);
- if (r < 0)
- return r;
-
- t = mktime(&tm);
- if (t == (time_t) -1)
- return -EINVAL;
-
- *next = (usec_t) t * USEC_PER_SEC;
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-/* A structure for specifying (possibly repetitive) points in calendar
- * time, a la cron */
-
-#include <stdbool.h>
-#include "util.h"
-
-typedef struct CalendarComponent {
- int value;
- int repeat;
-
- struct CalendarComponent *next;
-} CalendarComponent;
-
-typedef struct CalendarSpec {
- int weekdays_bits;
-
- CalendarComponent *year;
- CalendarComponent *month;
- CalendarComponent *day;
-
- CalendarComponent *hour;
- CalendarComponent *minute;
- CalendarComponent *second;
-} CalendarSpec;
-
-void calendar_spec_free(CalendarSpec *c);
-
-int calendar_spec_normalize(CalendarSpec *spec);
-bool calendar_spec_valid(CalendarSpec *spec);
-
-int calendar_spec_to_string(const CalendarSpec *spec, char **p);
-int calendar_spec_from_string(const char *p, CalendarSpec **spec);
-
-int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-
-#include "util.h"
-#include "cap-list.h"
-#include "missing.h"
-
-static const struct capability_name* lookup_capability(register const char *str, register unsigned int len);
-
-#include "cap-to-name.h"
-#include "cap-from-name.h"
-
-const char *capability_to_name(int id) {
-
- if (id < 0)
- return NULL;
-
- if (id >= (int) ELEMENTSOF(capability_names))
- return NULL;
-
- return capability_names[id];
-}
-
-int capability_from_name(const char *name) {
- const struct capability_name *sc;
- int r, i;
-
- assert(name);
-
- /* Try to parse numeric capability */
- r = safe_atoi(name, &i);
- if (r >= 0 && i >= 0)
- return i;
-
- /* Try to parse string capability */
- sc = lookup_capability(name, strlen(name));
- if (!sc)
- return -EINVAL;
-
- return sc->id;
-}
-
-int capability_list_length(void) {
- return (int) ELEMENTSOF(capability_names);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-const char *capability_to_name(int id);
-int capability_from_name(const char *name);
-int capability_list_length(void);
#include <errno.h>
#include "util.h"
+#include "formats-util.h"
+#include "process-util.h"
#include "macro.h"
#include "path-util.h"
#include "cgroup-util.h"
#include "cgroup-show.h"
+#include "terminal-util.h"
static int compare(const void *a, const void *b) {
const pid_t *p = a, *q = b;
if (!k)
return -ENOMEM;
- if (!(flags & OUTPUT_SHOW_ALL) && cg_is_empty_recursive(NULL, k, false) > 0)
+ if (!(flags & OUTPUT_SHOW_ALL) && cg_is_empty_recursive(NULL, k) > 0)
continue;
if (!shown_pids) {
}
if (last) {
- printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_BRANCH),
- basename(last));
+ printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_BRANCH), cg_unescape(basename(last)));
if (!p1) {
p1 = strappend(prefix, draw_special_char(DRAW_TREE_VERTICAL));
show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads, flags);
if (last) {
- printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_RIGHT),
- basename(last));
+ printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_RIGHT), cg_unescape(basename(last)));
if (!p2) {
p2 = strappend(prefix, " ");
return show_extra_pids(controller, path, prefix, n_columns, extra_pids, n_extra_pids, flags);
}
+/// UNNEEDED by elogind
+#if 0
int show_cgroup_and_extra_by_spec(const char *spec, const char *prefix, unsigned n_columns, bool kernel_threads, const pid_t extra_pids[], unsigned n_extra_pids, OutputFlags flags) {
_cleanup_free_ char *controller = NULL, *path = NULL;
int r;
return show_cgroup_and_extra(controller, path, prefix, n_columns, kernel_threads, extra_pids, n_extra_pids, flags);
}
+#endif // 0
#include <stdbool.h>
#include <sys/types.h>
-#include "logs-show.h"
+// #include "logs-show.h"
+#include "output-mode.h"
int show_cgroup_by_path(const char *path, const char *prefix, unsigned columns, bool kernel_threads, OutputFlags flags);
int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned columns, bool kernel_threads, OutputFlags flags);
-int show_cgroup_and_extra_by_spec(const char *spec, const char *prefix, unsigned n_columns, bool kernel_threads, const pid_t extra_pids[], unsigned n_extra_pids, OutputFlags flags);
+// UNNEEDED int show_cgroup_and_extra_by_spec(const char *spec, const char *prefix, unsigned n_columns, bool kernel_threads, const pid_t extra_pids[], unsigned n_extra_pids, OutputFlags flags);
int show_cgroup_and_extra(const char *controller, const char *path, const char *prefix, unsigned n_columns, bool kernel_threads, const pid_t extra_pids[], unsigned n_extra_pids, OutputFlags flags);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <unistd.h>
-#include <signal.h>
-#include <string.h>
-#include <stdlib.h>
-#include <dirent.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <ftw.h>
-
-#include "cgroup-util.h"
-#include "set.h"
-#include "macro.h"
-#include "util.h"
-#include "path-util.h"
-#include "fileio.h"
-#include "special.h"
-#include "mkdir.h"
-
-int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
- _cleanup_free_ char *fs = NULL;
- FILE *f;
- int r;
-
- assert(_f);
-
- r = cg_get_path(controller, path, "cgroup.procs", &fs);
- if (r < 0)
- return r;
-
- f = fopen(fs, "re");
- if (!f)
- return -errno;
-
- *_f = f;
- return 0;
-}
-
-int cg_read_pid(FILE *f, pid_t *_pid) {
- unsigned long ul;
-
- /* Note that the cgroup.procs might contain duplicates! See
- * cgroups.txt for details. */
-
- assert(f);
- assert(_pid);
-
- errno = 0;
- if (fscanf(f, "%lu", &ul) != 1) {
-
- if (feof(f))
- return 0;
-
- return errno ? -errno : -EIO;
- }
-
- if (ul <= 0)
- return -EIO;
-
- *_pid = (pid_t) ul;
- return 1;
-}
-
-int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
- _cleanup_free_ char *fs = NULL;
- int r;
- DIR *d;
-
- assert(_d);
-
- /* This is not recursive! */
-
- r = cg_get_path(controller, path, NULL, &fs);
- if (r < 0)
- return r;
-
- d = opendir(fs);
- if (!d)
- return -errno;
-
- *_d = d;
- return 0;
-}
-
-int cg_read_subgroup(DIR *d, char **fn) {
- struct dirent *de;
-
- assert(d);
- assert(fn);
-
- FOREACH_DIRENT(de, d, return -errno) {
- char *b;
-
- if (de->d_type != DT_DIR)
- continue;
-
- if (streq(de->d_name, ".") ||
- streq(de->d_name, ".."))
- continue;
-
- b = strdup(de->d_name);
- if (!b)
- return -ENOMEM;
-
- *fn = b;
- return 1;
- }
-
- return 0;
-}
-
-int cg_rmdir(const char *controller, const char *path) {
- _cleanup_free_ char *p = NULL;
- int r;
-
- r = cg_get_path(controller, path, NULL, &p);
- if (r < 0)
- return r;
-
- r = rmdir(p);
- if (r < 0 && errno != ENOENT)
- return -errno;
-
- return 0;
-}
-
-int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
- _cleanup_set_free_ Set *allocated_set = NULL;
- bool done = false;
- int r, ret = 0;
- pid_t my_pid;
-
- assert(sig >= 0);
-
- /* This goes through the tasks list and kills them all. This
- * is repeated until no further processes are added to the
- * tasks list, to properly handle forking processes */
-
- if (!s) {
- s = allocated_set = set_new(NULL);
- if (!s)
- return -ENOMEM;
- }
-
- my_pid = getpid();
-
- do {
- _cleanup_fclose_ FILE *f = NULL;
- pid_t pid = 0;
- done = true;
-
- r = cg_enumerate_processes(controller, path, &f);
- if (r < 0) {
- if (ret >= 0 && r != -ENOENT)
- return r;
-
- return ret;
- }
-
- while ((r = cg_read_pid(f, &pid)) > 0) {
-
- if (ignore_self && pid == my_pid)
- continue;
-
- if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
- continue;
-
- /* If we haven't killed this process yet, kill
- * it */
- if (kill(pid, sig) < 0) {
- if (ret >= 0 && errno != ESRCH)
- ret = -errno;
- } else {
- if (sigcont && sig != SIGKILL)
- kill(pid, SIGCONT);
-
- if (ret == 0)
- ret = 1;
- }
-
- done = false;
-
- r = set_put(s, LONG_TO_PTR(pid));
- if (r < 0) {
- if (ret >= 0)
- return r;
-
- return ret;
- }
- }
-
- if (r < 0) {
- if (ret >= 0)
- return r;
-
- return ret;
- }
-
- /* To avoid racing against processes which fork
- * quicker than we can kill them we repeat this until
- * no new pids need to be killed. */
-
- } while (!done);
-
- return ret;
-}
-
-int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
- _cleanup_set_free_ Set *allocated_set = NULL;
- _cleanup_closedir_ DIR *d = NULL;
- int r, ret = 0;
- char *fn;
-
- assert(path);
- assert(sig >= 0);
-
- if (!s) {
- s = allocated_set = set_new(NULL);
- if (!s)
- return -ENOMEM;
- }
-
- ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
-
- r = cg_enumerate_subgroups(controller, path, &d);
- if (r < 0) {
- if (ret >= 0 && r != -ENOENT)
- return r;
-
- return ret;
- }
-
- while ((r = cg_read_subgroup(d, &fn)) > 0) {
- _cleanup_free_ char *p = NULL;
-
- p = strjoin(path, "/", fn, NULL);
- free(fn);
- if (!p)
- return -ENOMEM;
-
- r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
- if (ret >= 0 && r != 0)
- ret = r;
- }
-
- if (ret >= 0 && r < 0)
- ret = r;
-
- if (rem) {
- r = cg_rmdir(controller, path);
- if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
- return r;
- }
-
- return ret;
-}
-
-int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
- bool done = false;
- _cleanup_set_free_ Set *s = NULL;
- int r, ret = 0;
- pid_t my_pid;
-
- assert(cfrom);
- assert(pfrom);
- assert(cto);
- assert(pto);
-
- s = set_new(NULL);
- if (!s)
- return -ENOMEM;
-
- my_pid = getpid();
-
- do {
- _cleanup_fclose_ FILE *f = NULL;
- pid_t pid = 0;
- done = true;
-
- r = cg_enumerate_processes(cfrom, pfrom, &f);
- if (r < 0) {
- if (ret >= 0 && r != -ENOENT)
- return r;
-
- return ret;
- }
-
- while ((r = cg_read_pid(f, &pid)) > 0) {
-
- /* This might do weird stuff if we aren't a
- * single-threaded program. However, we
- * luckily know we are not */
- if (ignore_self && pid == my_pid)
- continue;
-
- if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
- continue;
-
- r = cg_attach(cto, pto, pid);
- if (r < 0) {
- if (ret >= 0 && r != -ESRCH)
- ret = r;
- } else if (ret == 0)
- ret = 1;
-
- done = false;
-
- r = set_put(s, LONG_TO_PTR(pid));
- if (r < 0) {
- if (ret >= 0)
- return r;
-
- return ret;
- }
- }
-
- if (r < 0) {
- if (ret >= 0)
- return r;
-
- return ret;
- }
- } while (!done);
-
- return ret;
-}
-
-int cg_migrate_recursive(
- const char *cfrom,
- const char *pfrom,
- const char *cto,
- const char *pto,
- bool ignore_self,
- bool rem) {
-
- _cleanup_closedir_ DIR *d = NULL;
- int r, ret = 0;
- char *fn;
-
- assert(cfrom);
- assert(pfrom);
- assert(cto);
- assert(pto);
-
- ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
-
- r = cg_enumerate_subgroups(cfrom, pfrom, &d);
- if (r < 0) {
- if (ret >= 0 && r != -ENOENT)
- return r;
-
- return ret;
- }
-
- while ((r = cg_read_subgroup(d, &fn)) > 0) {
- _cleanup_free_ char *p = NULL;
-
- p = strjoin(pfrom, "/", fn, NULL);
- free(fn);
- if (!p) {
- if (ret >= 0)
- return -ENOMEM;
-
- return ret;
- }
-
- r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
- if (r != 0 && ret >= 0)
- ret = r;
- }
-
- if (r < 0 && ret >= 0)
- ret = r;
-
- if (rem) {
- r = cg_rmdir(cfrom, pfrom);
- if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
- return r;
- }
-
- return ret;
-}
-
-int cg_migrate_recursive_fallback(
- const char *cfrom,
- const char *pfrom,
- const char *cto,
- const char *pto,
- bool ignore_self,
- bool rem) {
-
- int r;
-
- assert(cfrom);
- assert(pfrom);
- assert(cto);
- assert(pto);
-
- r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
- if (r < 0) {
- char prefix[strlen(pto) + 1];
-
- /* This didn't work? Then let's try all prefixes of the destination */
-
- PATH_FOREACH_PREFIX(prefix, pto) {
- r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
- if (r >= 0)
- break;
- }
- }
-
- return 0;
-}
-
-static const char *normalize_controller(const char *controller) {
-
- assert(controller);
-
- if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
- return "elogind";
- else if (startswith(controller, "name="))
- return controller + 5;
- else
- return controller;
-}
-
-static int join_path(const char *controller, const char *path, const char *suffix, char **fs) {
- char *t = NULL;
-
- if (!isempty(controller)) {
- if (!isempty(path) && !isempty(suffix))
- t = strjoin("/sys/fs/cgroup/", controller, "/", path, "/", suffix, NULL);
- else if (!isempty(path))
- t = strjoin("/sys/fs/cgroup/", controller, "/", path, NULL);
- else if (!isempty(suffix))
- t = strjoin("/sys/fs/cgroup/", controller, "/", suffix, NULL);
- else
- t = strappend("/sys/fs/cgroup/", controller);
- } else {
- if (!isempty(path) && !isempty(suffix))
- t = strjoin(path, "/", suffix, NULL);
- else if (!isempty(path))
- t = strdup(path);
- else
- return -EINVAL;
- }
-
- if (!t)
- return -ENOMEM;
-
- *fs = path_kill_slashes(t);
- return 0;
-}
-
-int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
- const char *p;
- static thread_local bool good = false;
-
- assert(fs);
-
- if (controller && !cg_controller_is_valid(controller, true))
- return -EINVAL;
-
- if (_unlikely_(!good)) {
- int r;
-
- r = path_is_mount_point("/sys/fs/cgroup", false);
- if (r <= 0)
- return r < 0 ? r : -ENOENT;
-
- /* Cache this to save a few stat()s */
- good = true;
- }
-
- p = controller ? normalize_controller(controller) : NULL;
-
- return join_path(p, path, suffix, fs);
-}
-
-static int check_hierarchy(const char *p) {
- const char *cc;
-
- assert(p);
-
- if (!filename_is_valid(p))
- return 0;
-
- /* Check if this controller actually really exists */
- cc = strjoina("/sys/fs/cgroup/", p);
- if (laccess(cc, F_OK) < 0)
- return -errno;
-
- return 0;
-}
-
-int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
- const char *p;
- int r;
-
- assert(fs);
-
- if (!cg_controller_is_valid(controller, true))
- return -EINVAL;
-
- /* Normalize the controller syntax */
- p = normalize_controller(controller);
-
- /* Check if this controller actually really exists */
- r = check_hierarchy(p);
- if (r < 0)
- return r;
-
- return join_path(p, path, suffix, fs);
-}
-
-static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
- assert(path);
- assert(sb);
- assert(ftwbuf);
-
- if (typeflag != FTW_DP)
- return 0;
-
- if (ftwbuf->level < 1)
- return 0;
-
- rmdir(path);
- return 0;
-}
-
-int cg_trim(const char *controller, const char *path, bool delete_root) {
- _cleanup_free_ char *fs = NULL;
- int r = 0;
-
- assert(path);
-
- r = cg_get_path(controller, path, NULL, &fs);
- if (r < 0)
- return r;
-
- errno = 0;
- if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
- r = errno ? -errno : -EIO;
-
- if (delete_root) {
- if (rmdir(fs) < 0 && errno != ENOENT)
- return -errno;
- }
-
- return r;
-}
-
-int cg_delete(const char *controller, const char *path) {
- _cleanup_free_ char *parent = NULL;
- int r;
-
- assert(path);
-
- r = path_get_parent(path, &parent);
- if (r < 0)
- return r;
-
- r = cg_migrate_recursive(controller, path, controller, parent, false, true);
- return r == -ENOENT ? 0 : r;
-}
-
-int cg_create(const char *controller, const char *path) {
- _cleanup_free_ char *fs = NULL;
- int r;
-
- r = cg_get_path_and_check(controller, path, NULL, &fs);
- if (r < 0)
- return r;
-
- r = mkdir_parents(fs, 0755);
- if (r < 0)
- return r;
-
- if (mkdir(fs, 0755) < 0) {
-
- if (errno == EEXIST)
- return 0;
-
- return -errno;
- }
-
- return 1;
-}
-
-int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
- int r, q;
-
- assert(pid >= 0);
-
- r = cg_create(controller, path);
- if (r < 0)
- return r;
-
- q = cg_attach(controller, path, pid);
- if (q < 0)
- return q;
-
- /* This does not remove the cgroup on failure */
- return r;
-}
-
-int cg_attach(const char *controller, const char *path, pid_t pid) {
- _cleanup_free_ char *fs = NULL;
- char c[DECIMAL_STR_MAX(pid_t) + 2];
- int r;
-
- assert(path);
- assert(pid >= 0);
-
- r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
- if (r < 0)
- return r;
-
- if (pid == 0)
- pid = getpid();
-
- snprintf(c, sizeof(c), PID_FMT"\n", pid);
-
- return write_string_file_no_create(fs, c);
-}
-
-int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
- int r;
-
- assert(controller);
- assert(path);
- assert(pid >= 0);
-
- r = cg_attach(controller, path, pid);
- if (r < 0) {
- char prefix[strlen(path) + 1];
-
- /* This didn't work? Then let's try all prefixes of
- * the destination */
-
- PATH_FOREACH_PREFIX(prefix, path) {
- r = cg_attach(controller, prefix, pid);
- if (r >= 0)
- break;
- }
- }
-
- return 0;
-}
-
-int cg_set_group_access(
- const char *controller,
- const char *path,
- mode_t mode,
- uid_t uid,
- gid_t gid) {
-
- _cleanup_free_ char *fs = NULL;
- int r;
-
- assert(path);
-
- if (mode != MODE_INVALID)
- mode &= 0777;
-
- r = cg_get_path(controller, path, NULL, &fs);
- if (r < 0)
- return r;
-
- return chmod_and_chown(fs, mode, uid, gid);
-}
-
-int cg_set_task_access(
- const char *controller,
- const char *path,
- mode_t mode,
- uid_t uid,
- gid_t gid) {
-
- _cleanup_free_ char *fs = NULL, *procs = NULL;
- int r;
-
- assert(path);
-
- if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
- return 0;
-
- if (mode != MODE_INVALID)
- mode &= 0666;
-
- r = cg_get_path(controller, path, "cgroup.procs", &fs);
- if (r < 0)
- return r;
-
- r = chmod_and_chown(fs, mode, uid, gid);
- if (r < 0)
- return r;
-
- /* Compatibility, Always keep values for "tasks" in sync with
- * "cgroup.procs" */
- r = cg_get_path(controller, path, "tasks", &procs);
- if (r < 0)
- return r;
-
- return chmod_and_chown(procs, mode, uid, gid);
-}
-
-int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
- _cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX];
- const char *fs;
- size_t cs;
-
- assert(path);
- assert(pid >= 0);
-
- if (controller) {
- if (!cg_controller_is_valid(controller, true))
- return -EINVAL;
-
- controller = normalize_controller(controller);
- } else
- controller = SYSTEMD_CGROUP_CONTROLLER;
-
- fs = procfs_file_alloca(pid, "cgroup");
-
- f = fopen(fs, "re");
- if (!f)
- return errno == ENOENT ? -ESRCH : -errno;
-
- cs = strlen(controller);
-
- FOREACH_LINE(line, f, return -errno) {
- char *l, *p, *e;
- size_t k;
- const char *word, *state;
- bool found = false;
-
- truncate_nl(line);
-
- l = strchr(line, ':');
- if (!l)
- continue;
-
- l++;
- e = strchr(l, ':');
- if (!e)
- continue;
-
- *e = 0;
-
- FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
-
- if (k == cs && memcmp(word, controller, cs) == 0) {
- found = true;
- break;
- }
-
- if (k == 5 + cs &&
- memcmp(word, "name=", 5) == 0 &&
- memcmp(word+5, controller, cs) == 0) {
- found = true;
- break;
- }
- }
-
- if (!found)
- continue;
-
- p = strdup(e + 1);
- if (!p)
- return -ENOMEM;
-
- *path = p;
- return 0;
- }
-
- return -ENOENT;
-}
-
-int cg_install_release_agent(const char *controller, const char *agent) {
- _cleanup_free_ char *fs = NULL, *contents = NULL;
- char *sc;
- int r;
-
- assert(agent);
-
- r = cg_get_path(controller, NULL, "release_agent", &fs);
- if (r < 0)
- return r;
-
- r = read_one_line_file(fs, &contents);
- if (r < 0)
- return r;
-
- sc = strstrip(contents);
- if (sc[0] == 0) {
- r = write_string_file_no_create(fs, agent);
- if (r < 0)
- return r;
- } else if (!streq(sc, agent))
- return -EEXIST;
-
- free(fs);
- fs = NULL;
- r = cg_get_path(controller, NULL, "notify_on_release", &fs);
- if (r < 0)
- return r;
-
- free(contents);
- contents = NULL;
- r = read_one_line_file(fs, &contents);
- if (r < 0)
- return r;
-
- sc = strstrip(contents);
- if (streq(sc, "0")) {
- r = write_string_file_no_create(fs, "1");
- if (r < 0)
- return r;
-
- return 1;
- }
-
- if (!streq(sc, "1"))
- return -EIO;
-
- return 0;
-}
-
-int cg_uninstall_release_agent(const char *controller) {
- _cleanup_free_ char *fs = NULL;
- int r;
-
- r = cg_get_path(controller, NULL, "notify_on_release", &fs);
- if (r < 0)
- return r;
-
- r = write_string_file_no_create(fs, "0");
- if (r < 0)
- return r;
-
- free(fs);
- fs = NULL;
-
- r = cg_get_path(controller, NULL, "release_agent", &fs);
- if (r < 0)
- return r;
-
- r = write_string_file_no_create(fs, "");
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
- _cleanup_fclose_ FILE *f = NULL;
- pid_t pid = 0, self_pid;
- bool found = false;
- int r;
-
- assert(path);
-
- r = cg_enumerate_processes(controller, path, &f);
- if (r < 0)
- return r == -ENOENT ? 1 : r;
-
- self_pid = getpid();
-
- while ((r = cg_read_pid(f, &pid)) > 0) {
-
- if (ignore_self && pid == self_pid)
- continue;
-
- found = true;
- break;
- }
-
- if (r < 0)
- return r;
-
- return !found;
-}
-
-int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
- _cleanup_closedir_ DIR *d = NULL;
- char *fn;
- int r;
-
- assert(path);
-
- r = cg_is_empty(controller, path, ignore_self);
- if (r <= 0)
- return r;
-
- r = cg_enumerate_subgroups(controller, path, &d);
- if (r < 0)
- return r == -ENOENT ? 1 : r;
-
- while ((r = cg_read_subgroup(d, &fn)) > 0) {
- _cleanup_free_ char *p = NULL;
-
- p = strjoin(path, "/", fn, NULL);
- free(fn);
- if (!p)
- return -ENOMEM;
-
- r = cg_is_empty_recursive(controller, p, ignore_self);
- if (r <= 0)
- return r;
- }
-
- if (r < 0)
- return r;
-
- return 1;
-}
-
-int cg_split_spec(const char *spec, char **controller, char **path) {
- const char *e;
- char *t = NULL, *u = NULL;
- _cleanup_free_ char *v = NULL;
-
- assert(spec);
-
- if (*spec == '/') {
- if (!path_is_safe(spec))
- return -EINVAL;
-
- if (path) {
- t = strdup(spec);
- if (!t)
- return -ENOMEM;
-
- *path = path_kill_slashes(t);
- }
-
- if (controller)
- *controller = NULL;
-
- return 0;
- }
-
- e = strchr(spec, ':');
- if (!e) {
- if (!cg_controller_is_valid(spec, true))
- return -EINVAL;
-
- if (controller) {
- t = strdup(normalize_controller(spec));
- if (!t)
- return -ENOMEM;
-
- *controller = t;
- }
-
- if (path)
- *path = NULL;
-
- return 0;
- }
-
- v = strndup(spec, e-spec);
- if (!v)
- return -ENOMEM;
- t = strdup(normalize_controller(v));
- if (!t)
- return -ENOMEM;
- if (!cg_controller_is_valid(t, true)) {
- free(t);
- return -EINVAL;
- }
-
- if (streq(e+1, "")) {
- u = strdup("/");
- if (!u) {
- free(t);
- return -ENOMEM;
- }
- } else {
- u = strdup(e+1);
- if (!u) {
- free(t);
- return -ENOMEM;
- }
-
- if (!path_is_safe(u) ||
- !path_is_absolute(u)) {
- free(t);
- free(u);
- return -EINVAL;
- }
-
- path_kill_slashes(u);
- }
-
- if (controller)
- *controller = t;
- else
- free(t);
-
- if (path)
- *path = u;
- else
- free(u);
-
- return 0;
-}
-
-int cg_mangle_path(const char *path, char **result) {
- _cleanup_free_ char *c = NULL, *p = NULL;
- char *t;
- int r;
-
- assert(path);
- assert(result);
-
- /* First, check if it already is a filesystem path */
- if (path_startswith(path, "/sys/fs/cgroup")) {
-
- t = strdup(path);
- if (!t)
- return -ENOMEM;
-
- *result = path_kill_slashes(t);
- return 0;
- }
-
- /* Otherwise, treat it as cg spec */
- r = cg_split_spec(path, &c, &p);
- if (r < 0)
- return r;
-
- return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
-}
-
-int cg_get_root_path(char **path) {
- assert(path);
-
- return cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, path);
-}
-
-int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
- _cleanup_free_ char *rt = NULL;
- char *p;
- int r;
-
- assert(cgroup);
- assert(shifted);
-
- if (!root) {
- /* If the root was specified let's use that, otherwise
- * let's determine it from PID 1 */
-
- r = cg_get_root_path(&rt);
- if (r < 0)
- return r;
-
- root = rt;
- }
-
- p = path_startswith(cgroup, root);
- if (p)
- *shifted = p - 1;
- else
- *shifted = cgroup;
-
- return 0;
-}
-
-int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
- _cleanup_free_ char *raw = NULL;
- const char *c;
- int r;
-
- assert(pid >= 0);
- assert(cgroup);
-
- r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
- if (r < 0)
- return r;
-
- r = cg_shift_path(raw, root, &c);
- if (r < 0)
- return r;
-
- if (c == raw) {
- *cgroup = raw;
- raw = NULL;
- } else {
- char *n;
-
- n = strdup(c);
- if (!n)
- return -ENOMEM;
-
- *cgroup = n;
- }
-
- return 0;
-}
-
-int cg_path_get_session(const char *path, char **session) {
- const char *e, *n, *s;
-
- /* Elogind uses a flat hierarchy, just "/SESSION". The only
- wrinkle is that SESSION might be escaped. */
-
- assert(path);
- assert(path[0] == '/');
-
- e = path + 1;
- n = strchrnul(e, '/');
- if (e == n)
- return -ENOENT;
-
- s = strndupa(e, n - e);
- s = cg_unescape(s);
-
- if (!s[0])
- return -ENOENT;
-
- if (session) {
- char *r;
-
- r = strdup(s);
- if (!r)
- return -ENOMEM;
-
- *session = r;
- }
-
- return 0;
-}
-
-int cg_pid_get_session(pid_t pid, char **session) {
- _cleanup_free_ char *cgroup = NULL;
- int r;
-
- r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
- if (r < 0)
- return r;
-
- return cg_path_get_session(cgroup, session);
-}
-
-char *cg_escape(const char *p) {
- bool need_prefix = false;
-
- /* This implements very minimal escaping for names to be used
- * as file names in the cgroup tree: any name which might
- * conflict with a kernel name or is prefixed with '_' is
- * prefixed with a '_'. That way, when reading cgroup names it
- * is sufficient to remove a single prefixing underscore if
- * there is one. */
-
- /* The return value of this function (unlike cg_unescape())
- * needs free()! */
-
- if (p[0] == 0 ||
- p[0] == '_' ||
- p[0] == '.' ||
- streq(p, "notify_on_release") ||
- streq(p, "release_agent") ||
- streq(p, "tasks"))
- need_prefix = true;
- else {
- const char *dot;
-
- dot = strrchr(p, '.');
- if (dot) {
-
- if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
- need_prefix = true;
- else {
- char *n;
-
- n = strndupa(p, dot - p);
-
- if (check_hierarchy(n) >= 0)
- need_prefix = true;
- }
- }
- }
-
- if (need_prefix)
- return strappend("_", p);
- else
- return strdup(p);
-}
-
-char *cg_unescape(const char *p) {
- assert(p);
-
- /* The return value of this function (unlike cg_escape())
- * doesn't need free()! */
-
- if (p[0] == '_')
- return (char*) p+1;
-
- return (char*) p;
-}
-
-#define CONTROLLER_VALID \
- DIGITS LETTERS \
- "_"
-
-bool cg_controller_is_valid(const char *p, bool allow_named) {
- const char *t, *s;
-
- if (!p)
- return false;
-
- if (allow_named) {
- s = startswith(p, "name=");
- if (s)
- p = s;
- }
-
- if (*p == 0 || *p == '_')
- return false;
-
- for (t = p; *t; t++)
- if (!strchr(CONTROLLER_VALID, *t))
- return false;
-
- if (t - p > FILENAME_MAX)
- return false;
-
- return true;
-}
-
-int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
- _cleanup_free_ char *p = NULL;
- int r;
-
- r = cg_get_path(controller, path, attribute, &p);
- if (r < 0)
- return r;
-
- return write_string_file_no_create(p, value);
-}
-
-int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
- _cleanup_free_ char *p = NULL;
- int r;
-
- r = cg_get_path(controller, path, attribute, &p);
- if (r < 0)
- return r;
-
- return read_one_line_file(p, ret);
-}
-
-static const char mask_names[] =
- "cpu\0"
- "cpuacct\0"
- "blkio\0"
- "memory\0"
- "devices\0";
-
-int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
- CGroupControllerMask bit = 1;
- const char *n;
- int r;
-
- /* This one will create a cgroup in our private tree, but also
- * duplicate it in the trees specified in mask, and remove it
- * in all others */
-
- /* First create the cgroup in our own hierarchy. */
- r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
- if (r < 0)
- return r;
-
- /* Then, do the same in the other hierarchies */
- NULSTR_FOREACH(n, mask_names) {
- if (mask & bit)
- cg_create(n, path);
- else if (supported & bit)
- cg_trim(n, path, true);
-
- bit <<= 1;
- }
-
- return 0;
-}
-
-int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
- CGroupControllerMask bit = 1;
- const char *n;
- int r;
-
- r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
- if (r < 0)
- return r;
-
- NULSTR_FOREACH(n, mask_names) {
-
- if (supported & bit) {
- const char *p = NULL;
-
- if (path_callback)
- p = path_callback(bit, userdata);
-
- if (!p)
- p = path;
-
- cg_attach_fallback(n, path, pid);
- }
-
- bit <<= 1;
- }
-
- return 0;
-}
-
-int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
- Iterator i;
- void *pidp;
- int r = 0;
-
- SET_FOREACH(pidp, pids, i) {
- pid_t pid = PTR_TO_LONG(pidp);
- int q;
-
- q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
- if (q < 0)
- r = q;
- }
-
- return r;
-}
-
-int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
- CGroupControllerMask bit = 1;
- const char *n;
- int r;
-
- if (!path_equal(from, to)) {
- r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
- if (r < 0)
- return r;
- }
-
- NULSTR_FOREACH(n, mask_names) {
- if (supported & bit) {
- const char *p = NULL;
-
- if (to_callback)
- p = to_callback(bit, userdata);
-
- if (!p)
- p = to;
-
- cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, p, false, false);
- }
-
- bit <<= 1;
- }
-
- return 0;
-}
-
-int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root) {
- CGroupControllerMask bit = 1;
- const char *n;
- int r;
-
- r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
- if (r < 0)
- return r;
-
- NULSTR_FOREACH(n, mask_names) {
- if (supported & bit)
- cg_trim(n, path, delete_root);
-
- bit <<= 1;
- }
-
- return 0;
-}
-
-CGroupControllerMask cg_mask_supported(void) {
- CGroupControllerMask bit = 1, mask = 0;
- const char *n;
-
- NULSTR_FOREACH(n, mask_names) {
- if (check_hierarchy(n) >= 0)
- mask |= bit;
-
- bit <<= 1;
- }
-
- return mask;
-}
-
-int cg_kernel_controllers(Set *controllers) {
- _cleanup_fclose_ FILE *f = NULL;
- char buf[LINE_MAX];
- int r;
-
- assert(controllers);
-
- f = fopen("/proc/cgroups", "re");
- if (!f) {
- if (errno == ENOENT)
- return 0;
- return -errno;
- }
-
- /* Ignore the header line */
- (void) fgets(buf, sizeof(buf), f);
-
- for (;;) {
- char *controller;
- int enabled = 0;
-
- errno = 0;
- if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
-
- if (feof(f))
- break;
-
- if (ferror(f) && errno)
- return -errno;
-
- return -EBADMSG;
- }
-
- if (!enabled) {
- free(controller);
- continue;
- }
-
- if (!filename_is_valid(controller)) {
- free(controller);
- return -EBADMSG;
- }
-
- r = set_consume(controllers, controller);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
-#include <mqueue.h>
+//#include <mqueue.h>
#include "util.h"
+#include "formats-util.h"
#include "strv.h"
#include "clean-ipc.h"
return clean_posix_shm_internal(dir, uid);
}
+/// UNNEEDED by elogind
+#if 0
static int clean_posix_mq(uid_t uid) {
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *de;
log_warning_errno(errno, "Failed to read /dev/mqueue: %m");
return -errno;
}
+#endif // 0
int clean_ipc(uid_t uid) {
int ret = 0, r;
if (r < 0)
ret = r;
+/// elogind does not use mq_open anywhere
+#if 0
r = clean_posix_mq(uid);
if (r < 0)
ret = r;
+#endif // 0
return ret;
}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010-2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/time.h>
-#include <linux/rtc.h>
-
-#include "macro.h"
-#include "util.h"
-#include "clock-util.h"
-
-int clock_get_hwclock(struct tm *tm) {
- _cleanup_close_ int fd = -1;
-
- assert(tm);
-
- fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- /* This leaves the timezone fields of struct tm
- * uninitialized! */
- if (ioctl(fd, RTC_RD_TIME, tm) < 0)
- return -errno;
-
- /* We don't know daylight saving, so we reset this in order not
- * to confuse mktime(). */
- tm->tm_isdst = -1;
-
- return 0;
-}
-
-int clock_set_hwclock(const struct tm *tm) {
- _cleanup_close_ int fd = -1;
-
- assert(tm);
-
- fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- if (ioctl(fd, RTC_SET_TIME, tm) < 0)
- return -errno;
-
- return 0;
-}
-
-int clock_is_localtime(void) {
- _cleanup_fclose_ FILE *f;
-
- /*
- * The third line of adjtime is "UTC" or "LOCAL" or nothing.
- * # /etc/adjtime
- * 0.0 0 0
- * 0
- * UTC
- */
- f = fopen("/etc/adjtime", "re");
- if (f) {
- char line[LINE_MAX];
- bool b;
-
- b = fgets(line, sizeof(line), f) &&
- fgets(line, sizeof(line), f) &&
- fgets(line, sizeof(line), f);
- if (!b)
- return -EIO;
-
- truncate_nl(line);
- return streq(line, "LOCAL");
-
- } else if (errno != ENOENT)
- return -errno;
-
- return 0;
-}
-
-int clock_set_timezone(int *min) {
- const struct timeval *tv_null = NULL;
- struct timespec ts;
- struct tm *tm;
- int minutesdelta;
- struct timezone tz;
-
- assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
- assert_se(tm = localtime(&ts.tv_sec));
- minutesdelta = tm->tm_gmtoff / 60;
-
- tz.tz_minuteswest = -minutesdelta;
- tz.tz_dsttime = 0; /* DST_NONE */
-
- /*
- * If the RTC does not run in UTC but in local time, the very first
- * call to settimeofday() will set the kernel's timezone and will warp the
- * system clock, so that it runs in UTC instead of the local time we
- * have read from the RTC.
- */
- if (settimeofday(tv_null, &tz) < 0)
- return -errno;
- if (min)
- *min = minutesdelta;
- return 0;
-}
-
-int clock_reset_timewarp(void) {
- const struct timeval *tv_null = NULL;
- struct timezone tz;
-
- tz.tz_minuteswest = 0;
- tz.tz_dsttime = 0; /* DST_NONE */
-
- /*
- * The very first call to settimeofday() does time warp magic. Do a
- * dummy call here, so the time warping is sealed and all later calls
- * behave as expected.
- */
- if (settimeofday(tv_null, &tz) < 0)
- return -errno;
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010-2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-int clock_is_localtime(void);
-int clock_set_timezone(int *min);
-int clock_reset_timewarp(void);
-int clock_get_hwclock(struct tm *tm);
-int clock_set_hwclock(const struct tm *tm);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <fnmatch.h>
-
-#include "sd-id128.h"
-#include "util.h"
-#include "virt.h"
-#include "path-util.h"
-#include "architecture.h"
-#include "smack-util.h"
-#include "apparmor-util.h"
-#include "ima-util.h"
-#include "selinux-util.h"
-#include "audit.h"
-#include "condition.h"
-#include "cap-list.h"
-
-Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
- Condition *c;
- int r;
-
- assert(type >= 0);
- assert(type < _CONDITION_TYPE_MAX);
- assert((!parameter) == (type == CONDITION_NULL));
-
- c = new0(Condition, 1);
- if (!c)
- return NULL;
-
- c->type = type;
- c->trigger = trigger;
- c->negate = negate;
-
- r = free_and_strdup(&c->parameter, parameter);
- if (r < 0) {
- free(c);
- return NULL;
- }
-
- return c;
-}
-
-void condition_free(Condition *c) {
- assert(c);
-
- free(c->parameter);
- free(c);
-}
-
-Condition* condition_free_list(Condition *first) {
- Condition *c, *n;
-
- LIST_FOREACH_SAFE(conditions, c, n, first)
- condition_free(c);
-
- return NULL;
-}
-
-static int condition_test_kernel_command_line(Condition *c) {
- _cleanup_free_ char *line = NULL;
- const char *p;
- bool equal;
- int r;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_KERNEL_COMMAND_LINE);
-
- r = proc_cmdline(&line);
- if (r < 0)
- return r;
-
- equal = !!strchr(c->parameter, '=');
- p = line;
-
- for (;;) {
- _cleanup_free_ char *word = NULL;
- bool found;
-
- r = unquote_first_word(&p, &word, UNQUOTE_RELAX);
- if (r < 0)
- return r;
- if (r == 0)
- break;
-
- if (equal)
- found = streq(word, c->parameter);
- else {
- const char *f;
-
- f = startswith(word, c->parameter);
- found = f && (*f == '=' || *f == 0);
- }
-
- if (found)
- return true;
- }
-
- return false;
-}
-
-static int condition_test_virtualization(Condition *c) {
- int b, v;
- const char *id;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_VIRTUALIZATION);
-
- v = detect_virtualization(&id);
- if (v < 0)
- return v;
-
- /* First, compare with yes/no */
- b = parse_boolean(c->parameter);
-
- if (v > 0 && b > 0)
- return true;
-
- if (v == 0 && b == 0)
- return true;
-
- /* Then, compare categorization */
- if (v == VIRTUALIZATION_VM && streq(c->parameter, "vm"))
- return true;
-
- if (v == VIRTUALIZATION_CONTAINER && streq(c->parameter, "container"))
- return true;
-
- /* Finally compare id */
- return v > 0 && streq(c->parameter, id);
-}
-
-static int condition_test_architecture(Condition *c) {
- int a, b;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_ARCHITECTURE);
-
- a = uname_architecture();
- if (a < 0)
- return a;
-
- if (streq(c->parameter, "native"))
- b = native_architecture();
- else
- b = architecture_from_string(c->parameter);
- if (b < 0)
- return b;
-
- return a == b;
-}
-
-static int condition_test_host(Condition *c) {
- _cleanup_free_ char *h = NULL;
- sd_id128_t x, y;
- int r;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_HOST);
-
- if (sd_id128_from_string(c->parameter, &x) >= 0) {
-
- r = sd_id128_get_machine(&y);
- if (r < 0)
- return r;
-
- return sd_id128_equal(x, y);
- }
-
- h = gethostname_malloc();
- if (!h)
- return -ENOMEM;
-
- return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0;
-}
-
-static int condition_test_ac_power(Condition *c) {
- int r;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_AC_POWER);
-
- r = parse_boolean(c->parameter);
- if (r < 0)
- return r;
-
- return (on_ac_power() != 0) == !!r;
-}
-
-static int condition_test_security(Condition *c) {
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_SECURITY);
-
- if (streq(c->parameter, "selinux"))
- return mac_selinux_use();
- if (streq(c->parameter, "smack"))
- return mac_smack_use();
- if (streq(c->parameter, "apparmor"))
- return mac_apparmor_use();
- if (streq(c->parameter, "audit"))
- return use_audit();
- if (streq(c->parameter, "ima"))
- return use_ima();
-
- return false;
-}
-
-static int condition_test_capability(Condition *c) {
- _cleanup_fclose_ FILE *f = NULL;
- int value;
- char line[LINE_MAX];
- unsigned long long capabilities = -1;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_CAPABILITY);
-
- /* If it's an invalid capability, we don't have it */
- value = capability_from_name(c->parameter);
- if (value < 0)
- return -EINVAL;
-
- /* If it's a valid capability we default to assume
- * that we have it */
-
- f = fopen("/proc/self/status", "re");
- if (!f)
- return -errno;
-
- while (fgets(line, sizeof(line), f)) {
- truncate_nl(line);
-
- if (startswith(line, "CapBnd:")) {
- (void) sscanf(line+7, "%llx", &capabilities);
- break;
- }
- }
-
- return !!(capabilities & (1ULL << value));
-}
-
-static int condition_test_needs_update(Condition *c) {
- const char *p;
- struct stat usr, other;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_NEEDS_UPDATE);
-
- /* If the file system is read-only we shouldn't suggest an update */
- if (path_is_read_only_fs(c->parameter) > 0)
- return false;
-
- /* Any other failure means we should allow the condition to be true,
- * so that we rather invoke too many update tools then too
- * few. */
-
- if (!path_is_absolute(c->parameter))
- return true;
-
- p = strjoina(c->parameter, "/.updated");
- if (lstat(p, &other) < 0)
- return true;
-
- if (lstat("/usr/", &usr) < 0)
- return true;
-
- return usr.st_mtim.tv_sec > other.st_mtim.tv_sec ||
- (usr.st_mtim.tv_sec == other.st_mtim.tv_sec && usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec);
-}
-
-static int condition_test_first_boot(Condition *c) {
- int r;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_FIRST_BOOT);
-
- r = parse_boolean(c->parameter);
- if (r < 0)
- return r;
-
- return (access("/run/systemd/first-boot", F_OK) >= 0) == !!r;
-}
-
-static int condition_test_path_exists(Condition *c) {
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_PATH_EXISTS);
-
- return access(c->parameter, F_OK) >= 0;
-}
-
-static int condition_test_path_exists_glob(Condition *c) {
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_PATH_EXISTS_GLOB);
-
- return glob_exists(c->parameter) > 0;
-}
-
-static int condition_test_path_is_directory(Condition *c) {
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_PATH_IS_DIRECTORY);
-
- return is_dir(c->parameter, true) > 0;
-}
-
-static int condition_test_path_is_symbolic_link(Condition *c) {
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK);
-
- return is_symlink(c->parameter) > 0;
-}
-
-static int condition_test_path_is_mount_point(Condition *c) {
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
-
- return path_is_mount_point(c->parameter, true) > 0;
-}
-
-static int condition_test_path_is_read_write(Condition *c) {
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_PATH_IS_READ_WRITE);
-
- return path_is_read_only_fs(c->parameter) <= 0;
-}
-
-static int condition_test_directory_not_empty(Condition *c) {
- int r;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY);
-
- r = dir_is_empty(c->parameter);
- return r <= 0 && r != -ENOENT;
-}
-
-static int condition_test_file_not_empty(Condition *c) {
- struct stat st;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_FILE_NOT_EMPTY);
-
- return (stat(c->parameter, &st) >= 0 &&
- S_ISREG(st.st_mode) &&
- st.st_size > 0);
-}
-
-static int condition_test_file_is_executable(Condition *c) {
- struct stat st;
-
- assert(c);
- assert(c->parameter);
- assert(c->type == CONDITION_FILE_IS_EXECUTABLE);
-
- return (stat(c->parameter, &st) >= 0 &&
- S_ISREG(st.st_mode) &&
- (st.st_mode & 0111));
-}
-
-static int condition_test_null(Condition *c) {
- assert(c);
- assert(c->type == CONDITION_NULL);
-
- /* Note that during parsing we already evaluate the string and
- * store it in c->negate */
- return true;
-}
-
-int condition_test(Condition *c) {
-
- static int (*const condition_tests[_CONDITION_TYPE_MAX])(Condition *c) = {
- [CONDITION_PATH_EXISTS] = condition_test_path_exists,
- [CONDITION_PATH_EXISTS_GLOB] = condition_test_path_exists_glob,
- [CONDITION_PATH_IS_DIRECTORY] = condition_test_path_is_directory,
- [CONDITION_PATH_IS_SYMBOLIC_LINK] = condition_test_path_is_symbolic_link,
- [CONDITION_PATH_IS_MOUNT_POINT] = condition_test_path_is_mount_point,
- [CONDITION_PATH_IS_READ_WRITE] = condition_test_path_is_read_write,
- [CONDITION_DIRECTORY_NOT_EMPTY] = condition_test_directory_not_empty,
- [CONDITION_FILE_NOT_EMPTY] = condition_test_file_not_empty,
- [CONDITION_FILE_IS_EXECUTABLE] = condition_test_file_is_executable,
- [CONDITION_KERNEL_COMMAND_LINE] = condition_test_kernel_command_line,
- [CONDITION_VIRTUALIZATION] = condition_test_virtualization,
- [CONDITION_SECURITY] = condition_test_security,
- [CONDITION_CAPABILITY] = condition_test_capability,
- [CONDITION_HOST] = condition_test_host,
- [CONDITION_AC_POWER] = condition_test_ac_power,
- [CONDITION_ARCHITECTURE] = condition_test_architecture,
- [CONDITION_NEEDS_UPDATE] = condition_test_needs_update,
- [CONDITION_FIRST_BOOT] = condition_test_first_boot,
- [CONDITION_NULL] = condition_test_null,
- };
-
- int r, b;
-
- assert(c);
- assert(c->type >= 0);
- assert(c->type < _CONDITION_TYPE_MAX);
-
- r = condition_tests[c->type](c);
- if (r < 0) {
- c->result = CONDITION_ERROR;
- return r;
- }
-
- b = (r > 0) == !c->negate;
- c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED;
- return b;
-}
-
-void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
- assert(c);
- assert(f);
-
- if (!prefix)
- prefix = "";
-
- fprintf(f,
- "%s\t%s: %s%s%s %s\n",
- prefix,
- to_string(c->type),
- c->trigger ? "|" : "",
- c->negate ? "!" : "",
- c->parameter,
- condition_result_to_string(c->result));
-}
-
-void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
- Condition *c;
-
- LIST_FOREACH(conditions, c, first)
- condition_dump(c, f, prefix, to_string);
-}
-
-static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
- [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
- [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
- [CONDITION_HOST] = "ConditionHost",
- [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
- [CONDITION_SECURITY] = "ConditionSecurity",
- [CONDITION_CAPABILITY] = "ConditionCapability",
- [CONDITION_AC_POWER] = "ConditionACPower",
- [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
- [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
- [CONDITION_PATH_EXISTS] = "ConditionPathExists",
- [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
- [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
- [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
- [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
- [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
- [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
- [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
- [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
- [CONDITION_NULL] = "ConditionNull"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
-
-static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
- [CONDITION_ARCHITECTURE] = "AssertArchitecture",
- [CONDITION_VIRTUALIZATION] = "AssertVirtualization",
- [CONDITION_HOST] = "AssertHost",
- [CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine",
- [CONDITION_SECURITY] = "AssertSecurity",
- [CONDITION_CAPABILITY] = "AssertCapability",
- [CONDITION_AC_POWER] = "AssertACPower",
- [CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate",
- [CONDITION_FIRST_BOOT] = "AssertFirstBoot",
- [CONDITION_PATH_EXISTS] = "AssertPathExists",
- [CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob",
- [CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory",
- [CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink",
- [CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint",
- [CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite",
- [CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty",
- [CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty",
- [CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable",
- [CONDITION_NULL] = "AssertNull"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
-
-static const char* const condition_result_table[_CONDITION_RESULT_MAX] = {
- [CONDITION_UNTESTED] = "untested",
- [CONDITION_SUCCEEDED] = "succeeded",
- [CONDITION_FAILED] = "failed",
- [CONDITION_ERROR] = "error",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <stdio.h>
-
-#include "list.h"
-#include "macro.h"
-
-typedef enum ConditionType {
- CONDITION_ARCHITECTURE,
- CONDITION_VIRTUALIZATION,
- CONDITION_HOST,
- CONDITION_KERNEL_COMMAND_LINE,
- CONDITION_SECURITY,
- CONDITION_CAPABILITY,
- CONDITION_AC_POWER,
-
- CONDITION_NEEDS_UPDATE,
- CONDITION_FIRST_BOOT,
-
- CONDITION_PATH_EXISTS,
- CONDITION_PATH_EXISTS_GLOB,
- CONDITION_PATH_IS_DIRECTORY,
- CONDITION_PATH_IS_SYMBOLIC_LINK,
- CONDITION_PATH_IS_MOUNT_POINT,
- CONDITION_PATH_IS_READ_WRITE,
- CONDITION_DIRECTORY_NOT_EMPTY,
- CONDITION_FILE_NOT_EMPTY,
- CONDITION_FILE_IS_EXECUTABLE,
-
- CONDITION_NULL,
-
- _CONDITION_TYPE_MAX,
- _CONDITION_TYPE_INVALID = -1
-} ConditionType;
-
-typedef enum ConditionResult {
- CONDITION_UNTESTED,
- CONDITION_SUCCEEDED,
- CONDITION_FAILED,
- CONDITION_ERROR,
- _CONDITION_RESULT_MAX,
- _CONDITION_RESULT_INVALID = -1
-} ConditionResult;
-
-typedef struct Condition {
- ConditionType type:8;
-
- bool trigger:1;
- bool negate:1;
-
- ConditionResult result:6;
-
- char *parameter;
-
- LIST_FIELDS(struct Condition, conditions);
-} Condition;
-
-Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate);
-void condition_free(Condition *c);
-Condition* condition_free_list(Condition *c);
-
-int condition_test(Condition *c);
-
-void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t));
-void condition_dump_list(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t));
-
-const char* condition_type_to_string(ConditionType t) _const_;
-ConditionType condition_type_from_string(const char *s) _pure_;
-
-const char* assert_type_to_string(ConditionType t) _const_;
-ConditionType assert_type_from_string(const char *s) _pure_;
-
-const char* condition_result_to_string(ConditionResult r) _const_;
-ConditionResult condition_result_from_string(const char *s) _pure_;
#include <errno.h>
#include <stdlib.h>
-#include "conf-parser.h"
+#include "sd-messages.h"
#include "conf-files.h"
#include "util.h"
#include "macro.h"
#include "log.h"
#include "utf8.h"
#include "path-util.h"
-#include "sd-messages.h"
-
-int log_syntax_internal(
- const char *unit,
- int level,
- const char *file,
- int line,
- const char *func,
- const char *config_file,
- unsigned config_line,
- int error,
- const char *format, ...) {
-
- _cleanup_free_ char *msg = NULL;
- int r;
- va_list ap;
-
- va_start(ap, format);
- r = vasprintf(&msg, format, ap);
- va_end(ap);
- if (r < 0)
- return log_oom();
-
- if (unit)
- r = log_struct_internal(level,
- error,
- file, line, func,
- getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s", unit,
- LOG_MESSAGE_ID(SD_MESSAGE_CONFIG_ERROR),
- "CONFIG_FILE=%s", config_file,
- "CONFIG_LINE=%u", config_line,
- LOG_MESSAGE("[%s:%u] %s", config_file, config_line, msg),
- NULL);
- else
- r = log_struct_internal(level,
- error,
- file, line, func,
- LOG_MESSAGE_ID(SD_MESSAGE_CONFIG_ERROR),
- "CONFIG_FILE=%s", config_file,
- "CONFIG_LINE=%u", config_line,
- LOG_MESSAGE("[%s:%u] %s", config_file, config_line, msg),
- NULL);
-
- return r;
-}
+#include "signal-util.h"
+#include "conf-parser.h"
int config_item_table_lookup(
const void *table,
return -ENOMEM;
}
- free(continuation);
- continuation = NULL;
+ continuation = mfree(continuation);
p = c;
} else
p = l;
if (r < 0) \
log_syntax(unit, LOG_ERR, filename, line, -r, \
"Failed to parse %s value, ignoring: %s", \
- #vartype, rvalue); \
+ #type, rvalue); \
\
return 0; \
}
DEFINE_PARSER(int, int, safe_atoi)
DEFINE_PARSER(long, long, safe_atoli)
+DEFINE_PARSER(uint32, uint32_t, safe_atou32)
DEFINE_PARSER(uint64, uint64_t, safe_atou64)
DEFINE_PARSER(unsigned, unsigned, safe_atou)
DEFINE_PARSER(double, double, safe_atod)
-DEFINE_PARSER(nsec, nsec_t, parse_nsec)
+// UNNEEDED DEFINE_PARSER(nsec, nsec_t, parse_nsec)
DEFINE_PARSER(sec, usec_t, parse_sec)
int config_parse_iec_size(const char* unit,
return 0;
}
+/// UNNEEDED by elogind
+#if 0
int config_parse_si_size(const char* unit,
const char *filename,
unsigned line,
return 0;
}
+#endif // 0
int config_parse_bool(const char* unit,
const char *filename,
return 0;
}
+/// UNNEEDED by elogind
+#if 0
+int config_parse_tristate(
+ const char* unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ int k, *t = data;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ /* A tristate is pretty much a boolean, except that it can
+ * also take the special value -1, indicating "uninitialized",
+ * much like NULL is for a pointer type. */
+
+ k = parse_boolean(rvalue);
+ if (k < 0) {
+ log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
+ return 0;
+ }
+
+ *t = !!k;
+ return 0;
+}
+#endif // 0
+
int config_parse_string(
const char *unit,
const char *filename,
return 0;
}
-int config_parse_mode(const char *unit,
- const char *filename,
- unsigned line,
- const char *section,
+int config_parse_mode(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
unsigned section_line,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
mode_t *m = data;
- long l;
- char *x = NULL;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
- errno = 0;
- l = strtol(rvalue, &x, 8);
- if (!x || x == rvalue || *x || errno) {
- log_syntax(unit, LOG_ERR, filename, line, errno,
- "Failed to parse mode value, ignoring: %s", rvalue);
+ if (parse_mode(rvalue, m) < 0) {
+ log_syntax(unit, LOG_ERR, filename, line, errno, "Failed to parse mode value, ignoring: %s", rvalue);
return 0;
}
- if (l < 0000 || l > 07777) {
- log_syntax(unit, LOG_ERR, filename, line, ERANGE,
- "Mode value out of range, ignoring: %s", rvalue);
- return 0;
- }
-
- *m = (mode_t) l;
return 0;
}
*o = (*o & LOG_FACMASK) | x;
return 0;
}
+
+int config_parse_signal(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ int *sig = data, r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(sig);
+
+ r = signal_from_string_try_harder(rvalue);
+ if (r <= 0) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Failed to parse signal name, ignoring: %s", rvalue);
+ return 0;
+ }
+
+ *sig = r;
+ return 0;
+}
+
+/// UNNEEDED by elogind
+#if 0
+int config_parse_personality(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ unsigned long *personality = data, p;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(personality);
+
+ p = personality_from_string(rvalue);
+ if (p == PERSONALITY_INVALID) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Failed to parse personality, ignoring: %s", rvalue);
+ return 0;
+ }
+
+ *personality = p;
+ return 0;
+}
+#endif // 0
int config_parse_int(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_unsigned(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_long(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_uint32(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_uint64(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_double(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_iec_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
-int config_parse_si_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
-int config_parse_iec_off(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+// UNNEEDED int config_parse_si_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+// UNNEEDED int config_parse_iec_off(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_bool(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+// UNNEEDED int config_parse_tristate(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_string(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_path(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_strv(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_sec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
-int config_parse_nsec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+// UNNEEDED int config_parse_nsec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_mode(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_log_facility(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_log_level(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
-
-int log_syntax_internal(
- const char *unit,
- int level,
- const char *file,
- int line,
- const char *func,
- const char *config_file,
- unsigned config_line,
- int error,
- const char *format, ...) _printf_(9, 10);
-
-#define log_syntax(unit, level, config_file, config_line, error, ...) \
- log_syntax_internal(unit, level, \
- __FILE__, __LINE__, __func__, \
- config_file, config_line, \
- error, __VA_ARGS__)
+int config_parse_signal(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+// UNNEEDED int config_parse_personality(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
#define log_invalid_utf8(unit, level, config_file, config_line, error, rvalue) \
do { \
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010-2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include "dev-setup.h"
-#include "util.h"
-#include "label.h"
-
-int dev_setup(const char *prefix) {
- const char *j, *k;
-
- static const char symlinks[] =
- "-/proc/kcore\0" "/dev/core\0"
- "/proc/self/fd\0" "/dev/fd\0"
- "/proc/self/fd/0\0" "/dev/stdin\0"
- "/proc/self/fd/1\0" "/dev/stdout\0"
- "/proc/self/fd/2\0" "/dev/stderr\0";
-
- NULSTR_FOREACH_PAIR(j, k, symlinks) {
- if (j[0] == '-') {
- j++;
-
- if (access(j, F_OK) < 0)
- continue;
- }
-
- if (prefix) {
- _cleanup_free_ char *link_name = NULL;
-
- link_name = strjoin(prefix, "/", k, NULL);
- if (!link_name)
- return -ENOMEM;
-
- symlink_label(j, link_name);
- } else
- symlink_label(j, k);
- }
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010-2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-int dev_setup(const char *pathprefix);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2008-2011 Kay Sievers
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdio.h>
-
-#include "device-nodes.h"
-#include "utf8.h"
-
-int whitelisted_char_for_devnode(char c, const char *white) {
- if ((c >= '0' && c <= '9') ||
- (c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z') ||
- strchr("#+-.:=@_", c) != NULL ||
- (white != NULL && strchr(white, c) != NULL))
- return 1;
- return 0;
-}
-
-int encode_devnode_name(const char *str, char *str_enc, size_t len) {
- size_t i, j;
-
- if (str == NULL || str_enc == NULL)
- return -EINVAL;
-
- for (i = 0, j = 0; str[i] != '\0'; i++) {
- int seqlen;
-
- seqlen = utf8_encoded_valid_unichar(&str[i]);
- if (seqlen > 1) {
- if (len-j < (size_t)seqlen)
- goto err;
- memcpy(&str_enc[j], &str[i], seqlen);
- j += seqlen;
- i += (seqlen-1);
- } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i], NULL)) {
- if (len-j < 4)
- goto err;
- sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
- j += 4;
- } else {
- if (len-j < 1)
- goto err;
- str_enc[j] = str[i];
- j++;
- }
- }
- if (len-j < 1)
- goto err;
- str_enc[j] = '\0';
- return 0;
-err:
- return -EINVAL;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-int encode_devnode_name(const char *str, char *str_enc, size_t len);
-int whitelisted_char_for_devnode(char c, const char *additional);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Zbigniew Jędrzejewski-Szmek
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "dropin.h"
-#include "util.h"
-#include "strv.h"
-#include "mkdir.h"
-#include "fileio-label.h"
-#include "conf-files.h"
-
-int drop_in_file(const char *dir, const char *unit, unsigned level,
- const char *name, char **_p, char **_q) {
-
- _cleanup_free_ char *b = NULL;
- char *p, *q;
-
- char prefix[DECIMAL_STR_MAX(unsigned)];
-
- assert(unit);
- assert(name);
- assert(_p);
- assert(_q);
-
- sprintf(prefix, "%u", level);
-
- b = xescape(name, "/.");
- if (!b)
- return -ENOMEM;
-
- if (!filename_is_valid(b))
- return -EINVAL;
-
- p = strjoin(dir, "/", unit, ".d", NULL);
- if (!p)
- return -ENOMEM;
-
- q = strjoin(p, "/", prefix, "-", b, ".conf", NULL);
- if (!q) {
- free(p);
- return -ENOMEM;
- }
-
- *_p = p;
- *_q = q;
- return 0;
-}
-
-int write_drop_in(const char *dir, const char *unit, unsigned level,
- const char *name, const char *data) {
-
- _cleanup_free_ char *p = NULL, *q = NULL;
- int r;
-
- assert(dir);
- assert(unit);
- assert(name);
- assert(data);
-
- r = drop_in_file(dir, unit, level, name, &p, &q);
- if (r < 0)
- return r;
-
- mkdir_p(p, 0755);
- return write_string_file_atomic_label(q, data);
-}
-
-int write_drop_in_format(const char *dir, const char *unit, unsigned level,
- const char *name, const char *format, ...) {
- _cleanup_free_ char *p = NULL;
- va_list ap;
- int r;
-
- assert(dir);
- assert(unit);
- assert(name);
- assert(format);
-
- va_start(ap, format);
- r = vasprintf(&p, format, ap);
- va_end(ap);
-
- if (r < 0)
- return -ENOMEM;
-
- return write_drop_in(dir, unit, level, name, p);
-}
-
-static int iterate_dir(
- const char *path,
- UnitDependency dependency,
- dependency_consumer_t consumer,
- void *arg,
- char ***strv) {
-
- _cleanup_closedir_ DIR *d = NULL;
- int r;
-
- assert(path);
-
- /* The config directories are special, since the order of the
- * drop-ins matters */
- if (dependency < 0) {
- r = strv_extend(strv, path);
- if (r < 0)
- return log_oom();
-
- return 0;
- }
-
- assert(consumer);
-
- d = opendir(path);
- if (!d) {
- if (errno == ENOENT)
- return 0;
-
- log_error_errno(errno, "Failed to open directory %s: %m", path);
- return -errno;
- }
-
- for (;;) {
- struct dirent *de;
- _cleanup_free_ char *f = NULL;
-
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return log_error_errno(errno, "Failed to read directory %s: %m", path);
-
- if (!de)
- break;
-
- if (hidden_file(de->d_name))
- continue;
-
- f = strjoin(path, "/", de->d_name, NULL);
- if (!f)
- return log_oom();
-
- r = consumer(dependency, de->d_name, f, arg);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-int unit_file_process_dir(
- Set * unit_path_cache,
- const char *unit_path,
- const char *name,
- const char *suffix,
- UnitDependency dependency,
- dependency_consumer_t consumer,
- void *arg,
- char ***strv) {
-
- _cleanup_free_ char *path = NULL;
-
- assert(unit_path);
- assert(name);
- assert(suffix);
-
- path = strjoin(unit_path, "/", name, suffix, NULL);
- if (!path)
- return log_oom();
-
- if (!unit_path_cache || set_get(unit_path_cache, path))
- iterate_dir(path, dependency, consumer, arg, strv);
-
- if (unit_name_is_instance(name)) {
- _cleanup_free_ char *template = NULL, *p = NULL;
- /* Also try the template dir */
-
- template = unit_name_template(name);
- if (!template)
- return log_oom();
-
- p = strjoin(unit_path, "/", template, suffix, NULL);
- if (!p)
- return log_oom();
-
- if (!unit_path_cache || set_get(unit_path_cache, p))
- iterate_dir(p, dependency, consumer, arg, strv);
- }
-
- return 0;
-}
-
-int unit_file_find_dropin_paths(
- char **lookup_path,
- Set *unit_path_cache,
- Set *names,
- char ***paths) {
-
- _cleanup_strv_free_ char **strv = NULL, **ans = NULL;
- Iterator i;
- char *t;
- int r;
-
- assert(paths);
-
- SET_FOREACH(t, names, i) {
- char **p;
-
- STRV_FOREACH(p, lookup_path)
- unit_file_process_dir(unit_path_cache, *p, t, ".d", _UNIT_DEPENDENCY_INVALID, NULL, NULL, &strv);
- }
-
- if (strv_isempty(strv))
- return 0;
-
- r = conf_files_list_strv(&ans, ".conf", NULL, (const char**) strv);
- if (r < 0)
- return log_warning_errno(r, "Failed to get list of configuration files: %m");
-
- *paths = ans;
- ans = NULL;
- return 1;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Zbigniew Jędrzejewski-Szmek
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "macro.h"
-#include "set.h"
-#include "unit-name.h"
-
-int drop_in_file(const char *dir, const char *unit, unsigned level,
- const char *name, char **_p, char **_q);
-
-int write_drop_in(const char *dir, const char *unit, unsigned level,
- const char *name, const char *data);
-
-int write_drop_in_format(const char *dir, const char *unit, unsigned level,
- const char *name, const char *format, ...) _printf_(5, 6);
-
-/**
- * This callback will be called for each directory entry @entry,
- * with @filepath being the full path to the entry.
- *
- * If return value is negative, loop will be aborted.
- */
-typedef int (*dependency_consumer_t)(UnitDependency dependency,
- const char *entry,
- const char* filepath,
- void *arg);
-
-int unit_file_process_dir(
- Set * unit_path_cache,
- const char *unit_path,
- const char *name,
- const char *suffix,
- UnitDependency dependency,
- dependency_consumer_t consumer,
- void *arg,
- char ***strv);
-
-int unit_file_find_dropin_paths(
- char **lookup_path,
- Set *unit_path_cache,
- Set *names,
- char ***paths);
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2013 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+
+#include "sd-id128.h"
+#include "time-util.h"
+
+#define EFI_VENDOR_LOADER SD_ID128_MAKE(4a,67,b0,82,0a,4c,41,cf,b6,c7,44,0b,29,bb,8c,4f)
+#define EFI_VENDOR_GLOBAL SD_ID128_MAKE(8b,e4,df,61,93,ca,11,d2,aa,0d,00,e0,98,03,2b,8c)
+#define EFI_VARIABLE_NON_VOLATILE 0x0000000000000001
+#define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
+#define EFI_VARIABLE_RUNTIME_ACCESS 0x0000000000000004
+
+#ifdef ENABLE_EFI
+
+bool is_efi_boot(void);
+bool is_efi_secure_boot(void);
+bool is_efi_secure_boot_setup_mode(void);
+int efi_reboot_to_firmware_supported(void);
+int efi_get_reboot_to_firmware(void);
+int efi_set_reboot_to_firmware(bool value);
+
+int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size);
+int efi_set_variable(sd_id128_t vendor, const char *name, const void *value, size_t size);
+int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p);
+
+int efi_get_boot_option(uint16_t nr, char **title, sd_id128_t *part_uuid, char **path, bool *active);
+int efi_add_boot_option(uint16_t id, const char *title, uint32_t part, uint64_t pstart, uint64_t psize, sd_id128_t part_uuid, const char *path);
+int efi_remove_boot_option(uint16_t id);
+int efi_get_boot_order(uint16_t **order);
+int efi_set_boot_order(uint16_t *order, size_t n);
+int efi_get_boot_options(uint16_t **options);
+
+int efi_loader_get_device_part_uuid(sd_id128_t *u);
+int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader);
+
+#else
+
+static inline bool is_efi_boot(void) {
+ return false;
+}
+
+static inline bool is_efi_secure_boot(void) {
+ return false;
+}
+
+static inline bool is_efi_secure_boot_setup_mode(void) {
+ return false;
+}
+
+static inline int efi_reboot_to_firmware_supported(void) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_get_reboot_to_firmware(void) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_set_reboot_to_firmware(bool value) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_set_variable(sd_id128_t vendor, const char *name, const void *value, size_t size) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_get_boot_option(uint16_t nr, char **title, sd_id128_t *part_uuid, char **path, bool *active) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_add_boot_option(uint16_t id, const char *title, uint32_t part, uint64_t pstart, uint64_t psize, sd_id128_t part_uuid, const char *path) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_remove_boot_option(uint16_t id) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_get_boot_order(uint16_t **order) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_set_boot_order(uint16_t *order, size_t n) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_get_boot_options(uint16_t **options) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_loader_get_device_part_uuid(sd_id128_t *u) {
+ return -EOPNOTSUPP;
+}
+
+static inline int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) {
+ return -EOPNOTSUPP;
+}
+
+#endif
+
+char *efi_tilt_backslashes(char *s);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <limits.h>
-#include <unistd.h>
-
-#include "strv.h"
-#include "utf8.h"
-#include "util.h"
-#include "env-util.h"
-#include "def.h"
-
-#define VALID_CHARS_ENV_NAME \
- DIGITS LETTERS \
- "_"
-
-#ifndef ARG_MAX
-#define ARG_MAX ((size_t) sysconf(_SC_ARG_MAX))
-#endif
-
-static bool env_name_is_valid_n(const char *e, size_t n) {
- const char *p;
-
- if (!e)
- return false;
-
- if (n <= 0)
- return false;
-
- if (e[0] >= '0' && e[0] <= '9')
- return false;
-
- /* POSIX says the overall size of the environment block cannot
- * be > ARG_MAX, an individual assignment hence cannot be
- * either. Discounting the equal sign and trailing NUL this
- * hence leaves ARG_MAX-2 as longest possible variable
- * name. */
- if (n > ARG_MAX - 2)
- return false;
-
- for (p = e; p < e + n; p++)
- if (!strchr(VALID_CHARS_ENV_NAME, *p))
- return false;
-
- return true;
-}
-
-bool env_name_is_valid(const char *e) {
- if (!e)
- return false;
-
- return env_name_is_valid_n(e, strlen(e));
-}
-
-bool env_value_is_valid(const char *e) {
- if (!e)
- return false;
-
- if (!utf8_is_valid(e))
- return false;
-
- /* bash allows tabs in environment variables, and so should
- * we */
- if (string_has_cc(e, "\t"))
- return false;
-
- /* POSIX says the overall size of the environment block cannot
- * be > ARG_MAX, an individual assignment hence cannot be
- * either. Discounting the shortest possible variable name of
- * length 1, the equal sign and trailing NUL this hence leaves
- * ARG_MAX-3 as longest possible variable value. */
- if (strlen(e) > ARG_MAX - 3)
- return false;
-
- return true;
-}
-
-bool env_assignment_is_valid(const char *e) {
- const char *eq;
-
- eq = strchr(e, '=');
- if (!eq)
- return false;
-
- if (!env_name_is_valid_n(e, eq - e))
- return false;
-
- if (!env_value_is_valid(eq + 1))
- return false;
-
- /* POSIX says the overall size of the environment block cannot
- * be > ARG_MAX, hence the individual variable assignments
- * cannot be either, but let's leave room for one trailing NUL
- * byte. */
- if (strlen(e) > ARG_MAX - 1)
- return false;
-
- return true;
-}
-
-bool strv_env_is_valid(char **e) {
- char **p, **q;
-
- STRV_FOREACH(p, e) {
- size_t k;
-
- if (!env_assignment_is_valid(*p))
- return false;
-
- /* Check if there are duplicate assginments */
- k = strcspn(*p, "=");
- STRV_FOREACH(q, p + 1)
- if (strneq(*p, *q, k) && (*q)[k] == '=')
- return false;
- }
-
- return true;
-}
-
-bool strv_env_name_or_assignment_is_valid(char **l) {
- char **p, **q;
-
- STRV_FOREACH(p, l) {
- if (!env_assignment_is_valid(*p) && !env_name_is_valid(*p))
- return false;
-
- STRV_FOREACH(q, p + 1)
- if (streq(*p, *q))
- return false;
- }
-
- return true;
-}
-
-static int env_append(char **r, char ***k, char **a) {
- assert(r);
- assert(k);
-
- if (!a)
- return 0;
-
- /* Add the entries of a to *k unless they already exist in *r
- * in which case they are overridden instead. This assumes
- * there is enough space in the r array. */
-
- for (; *a; a++) {
- char **j;
- size_t n;
-
- n = strcspn(*a, "=");
-
- if ((*a)[n] == '=')
- n++;
-
- for (j = r; j < *k; j++)
- if (strneq(*j, *a, n))
- break;
-
- if (j >= *k)
- (*k)++;
- else
- free(*j);
-
- *j = strdup(*a);
- if (!*j)
- return -ENOMEM;
- }
-
- return 0;
-}
-
-char **strv_env_merge(unsigned n_lists, ...) {
- size_t n = 0;
- char **l, **k, **r;
- va_list ap;
- unsigned i;
-
- /* Merges an arbitrary number of environment sets */
-
- va_start(ap, n_lists);
- for (i = 0; i < n_lists; i++) {
- l = va_arg(ap, char**);
- n += strv_length(l);
- }
- va_end(ap);
-
- r = new(char*, n+1);
- if (!r)
- return NULL;
-
- k = r;
-
- va_start(ap, n_lists);
- for (i = 0; i < n_lists; i++) {
- l = va_arg(ap, char**);
- if (env_append(r, &k, l) < 0)
- goto fail;
- }
- va_end(ap);
-
- *k = NULL;
-
- return r;
-
-fail:
- va_end(ap);
- strv_free(r);
-
- return NULL;
-}
-
-_pure_ static bool env_match(const char *t, const char *pattern) {
- assert(t);
- assert(pattern);
-
- /* pattern a matches string a
- * a matches a=
- * a matches a=b
- * a= matches a=
- * a=b matches a=b
- * a= does not match a
- * a=b does not match a=
- * a=b does not match a
- * a=b does not match a=c */
-
- if (streq(t, pattern))
- return true;
-
- if (!strchr(pattern, '=')) {
- size_t l = strlen(pattern);
-
- return strneq(t, pattern, l) && t[l] == '=';
- }
-
- return false;
-}
-
-char **strv_env_delete(char **x, unsigned n_lists, ...) {
- size_t n, i = 0;
- char **k, **r;
- va_list ap;
-
- /* Deletes every entry from x that is mentioned in the other
- * string lists */
-
- n = strv_length(x);
-
- r = new(char*, n+1);
- if (!r)
- return NULL;
-
- STRV_FOREACH(k, x) {
- unsigned v;
-
- va_start(ap, n_lists);
- for (v = 0; v < n_lists; v++) {
- char **l, **j;
-
- l = va_arg(ap, char**);
- STRV_FOREACH(j, l)
- if (env_match(*k, *j))
- goto skip;
- }
- va_end(ap);
-
- r[i] = strdup(*k);
- if (!r[i]) {
- strv_free(r);
- return NULL;
- }
-
- i++;
- continue;
-
- skip:
- va_end(ap);
- }
-
- r[i] = NULL;
-
- assert(i <= n);
-
- return r;
-}
-
-char **strv_env_unset(char **l, const char *p) {
-
- char **f, **t;
-
- if (!l)
- return NULL;
-
- assert(p);
-
- /* Drops every occurrence of the env var setting p in the
- * string list. Edits in-place. */
-
- for (f = t = l; *f; f++) {
-
- if (env_match(*f, p)) {
- free(*f);
- continue;
- }
-
- *(t++) = *f;
- }
-
- *t = NULL;
- return l;
-}
-
-char **strv_env_unset_many(char **l, ...) {
-
- char **f, **t;
-
- if (!l)
- return NULL;
-
- /* Like strv_env_unset() but applies many at once. Edits in-place. */
-
- for (f = t = l; *f; f++) {
- bool found = false;
- const char *p;
- va_list ap;
-
- va_start(ap, l);
-
- while ((p = va_arg(ap, const char*))) {
- if (env_match(*f, p)) {
- found = true;
- break;
- }
- }
-
- va_end(ap);
-
- if (found) {
- free(*f);
- continue;
- }
-
- *(t++) = *f;
- }
-
- *t = NULL;
- return l;
-}
-
-char **strv_env_set(char **x, const char *p) {
-
- char **k, **r;
- char* m[2] = { (char*) p, NULL };
-
- /* Overrides the env var setting of p, returns a new copy */
-
- r = new(char*, strv_length(x)+2);
- if (!r)
- return NULL;
-
- k = r;
- if (env_append(r, &k, x) < 0)
- goto fail;
-
- if (env_append(r, &k, m) < 0)
- goto fail;
-
- *k = NULL;
-
- return r;
-
-fail:
- strv_free(r);
- return NULL;
-}
-
-char *strv_env_get_n(char **l, const char *name, size_t k) {
- char **i;
-
- assert(name);
-
- if (k <= 0)
- return NULL;
-
- STRV_FOREACH(i, l)
- if (strneq(*i, name, k) &&
- (*i)[k] == '=')
- return *i + k + 1;
-
- return NULL;
-}
-
-char *strv_env_get(char **l, const char *name) {
- assert(name);
-
- return strv_env_get_n(l, name, strlen(name));
-}
-
-char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) {
- char **p, **q;
- int k = 0;
-
- STRV_FOREACH(p, e) {
- size_t n;
- bool duplicate = false;
-
- if (!env_assignment_is_valid(*p)) {
- if (invalid_callback)
- invalid_callback(*p, userdata);
- free(*p);
- continue;
- }
-
- n = strcspn(*p, "=");
- STRV_FOREACH(q, p + 1)
- if (strneq(*p, *q, n) && (*q)[n] == '=') {
- duplicate = true;
- break;
- }
-
- if (duplicate) {
- free(*p);
- continue;
- }
-
- e[k++] = *p;
- }
-
- if (e)
- e[k] = NULL;
-
- return e;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-#include "macro.h"
-
-bool env_name_is_valid(const char *e);
-bool env_value_is_valid(const char *e);
-bool env_assignment_is_valid(const char *e);
-
-bool strv_env_is_valid(char **e);
-#define strv_env_clean(l) strv_env_clean_with_callback(l, NULL, NULL)
-char **strv_env_clean_with_callback(char **l, void (*invalid_callback)(const char *p, void *userdata), void *userdata);
-
-bool strv_env_name_or_assignment_is_valid(char **l);
-
-char **strv_env_merge(unsigned n_lists, ...);
-char **strv_env_delete(char **x, unsigned n_lists, ...); /* New copy */
-
-char **strv_env_set(char **x, const char *p); /* New copy ... */
-char **strv_env_unset(char **l, const char *p); /* In place ... */
-char **strv_env_unset_many(char **l, ...) _sentinel_;
-
-char *strv_env_get_n(char **l, const char *name, size_t k) _pure_;
-char *strv_env_get(char **x, const char *n) _pure_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <net/ethernet.h>
-
-#define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X"
-#define ETHER_ADDR_FORMAT_VAL(x) (x).ether_addr_octet[0], (x).ether_addr_octet[1], (x).ether_addr_octet[2], (x).ether_addr_octet[3], (x).ether_addr_octet[4], (x).ether_addr_octet[5]
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-
-#include "exit-status.h"
-#include "set.h"
-#include "macro.h"
-
-const char* exit_status_to_string(ExitStatus status, ExitStatusLevel level) {
-
- /* We cast to int here, so that -Wenum doesn't complain that
- * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
-
- switch ((int) status) {
-
- case EXIT_SUCCESS:
- return "SUCCESS";
-
- case EXIT_FAILURE:
- return "FAILURE";
- }
-
-
- if (level == EXIT_STATUS_SYSTEMD || level == EXIT_STATUS_LSB) {
- switch ((int) status) {
-
- case EXIT_CHDIR:
- return "CHDIR";
-
- case EXIT_NICE:
- return "NICE";
-
- case EXIT_FDS:
- return "FDS";
-
- case EXIT_EXEC:
- return "EXEC";
-
- case EXIT_MEMORY:
- return "MEMORY";
-
- case EXIT_LIMITS:
- return "LIMITS";
-
- case EXIT_OOM_ADJUST:
- return "OOM_ADJUST";
-
- case EXIT_SIGNAL_MASK:
- return "SIGNAL_MASK";
-
- case EXIT_STDIN:
- return "STDIN";
-
- case EXIT_STDOUT:
- return "STDOUT";
-
- case EXIT_CHROOT:
- return "CHROOT";
-
- case EXIT_IOPRIO:
- return "IOPRIO";
-
- case EXIT_TIMERSLACK:
- return "TIMERSLACK";
-
- case EXIT_SECUREBITS:
- return "SECUREBITS";
-
- case EXIT_SETSCHEDULER:
- return "SETSCHEDULER";
-
- case EXIT_CPUAFFINITY:
- return "CPUAFFINITY";
-
- case EXIT_GROUP:
- return "GROUP";
-
- case EXIT_USER:
- return "USER";
-
- case EXIT_CAPABILITIES:
- return "CAPABILITIES";
-
- case EXIT_CGROUP:
- return "CGROUP";
-
- case EXIT_SETSID:
- return "SETSID";
-
- case EXIT_CONFIRM:
- return "CONFIRM";
-
- case EXIT_STDERR:
- return "STDERR";
-
- case EXIT_PAM:
- return "PAM";
-
- case EXIT_NETWORK:
- return "NETWORK";
-
- case EXIT_NAMESPACE:
- return "NAMESPACE";
-
- case EXIT_NO_NEW_PRIVILEGES:
- return "NO_NEW_PRIVILEGES";
-
- case EXIT_SECCOMP:
- return "SECCOMP";
-
- case EXIT_SELINUX_CONTEXT:
- return "SELINUX_CONTEXT";
-
- case EXIT_PERSONALITY:
- return "PERSONALITY";
-
- case EXIT_APPARMOR_PROFILE:
- return "APPARMOR";
-
- case EXIT_ADDRESS_FAMILIES:
- return "ADDRESS_FAMILIES";
-
- case EXIT_RUNTIME_DIRECTORY:
- return "RUNTIME_DIRECTORY";
-
- case EXIT_CHOWN:
- return "CHOWN";
-
- case EXIT_MAKE_STARTER:
- return "MAKE_STARTER";
-
- case EXIT_BUS_ENDPOINT:
- return "BUS_ENDPOINT";
- }
- }
-
- if (level == EXIT_STATUS_LSB) {
- switch ((int) status) {
-
- case EXIT_INVALIDARGUMENT:
- return "INVALIDARGUMENT";
-
- case EXIT_NOTIMPLEMENTED:
- return "NOTIMPLEMENTED";
-
- case EXIT_NOPERMISSION:
- return "NOPERMISSION";
-
- case EXIT_NOTINSTALLED:
- return "NOTINSSTALLED";
-
- case EXIT_NOTCONFIGURED:
- return "NOTCONFIGURED";
-
- case EXIT_NOTRUNNING:
- return "NOTRUNNING";
- }
- }
-
- return NULL;
-}
-
-
-bool is_clean_exit(int code, int status, ExitStatusSet *success_status) {
-
- if (code == CLD_EXITED)
- return status == 0 ||
- (success_status &&
- set_contains(success_status->status, INT_TO_PTR(status)));
-
- /* If a daemon does not implement handlers for some of the
- * signals that's not considered an unclean shutdown */
- if (code == CLD_KILLED)
- return
- status == SIGHUP ||
- status == SIGINT ||
- status == SIGTERM ||
- status == SIGPIPE ||
- (success_status &&
- set_contains(success_status->signal, INT_TO_PTR(status)));
-
- return false;
-}
-
-bool is_clean_exit_lsb(int code, int status, ExitStatusSet *success_status) {
-
- if (is_clean_exit(code, status, success_status))
- return true;
-
- return
- code == CLD_EXITED &&
- (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED);
-}
-
-void exit_status_set_free(ExitStatusSet *x) {
- assert(x);
-
- set_free(x->status);
- set_free(x->signal);
- x->status = x->signal = NULL;
-}
-
-bool exit_status_set_is_empty(ExitStatusSet *x) {
- if (!x)
- return true;
-
- return set_isempty(x->status) && set_isempty(x->signal);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-#include "set.h"
-
-typedef enum ExitStatus {
- /* EXIT_SUCCESS defined by libc */
- /* EXIT_FAILURE defined by libc */
- EXIT_INVALIDARGUMENT = 2,
- EXIT_NOTIMPLEMENTED = 3,
- EXIT_NOPERMISSION = 4,
- EXIT_NOTINSTALLED = 5,
- EXIT_NOTCONFIGURED = 6,
- EXIT_NOTRUNNING = 7,
-
- /* The LSB suggests that error codes >= 200 are "reserved". We
- * use them here under the assumption that they hence are
- * unused by init scripts.
- *
- * http://refspecs.linuxfoundation.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html */
-
- EXIT_CHDIR = 200,
- EXIT_NICE,
- EXIT_FDS,
- EXIT_EXEC,
- EXIT_MEMORY,
- EXIT_LIMITS,
- EXIT_OOM_ADJUST,
- EXIT_SIGNAL_MASK,
- EXIT_STDIN,
- EXIT_STDOUT,
- EXIT_CHROOT, /* 210 */
- EXIT_IOPRIO,
- EXIT_TIMERSLACK,
- EXIT_SECUREBITS,
- EXIT_SETSCHEDULER,
- EXIT_CPUAFFINITY,
- EXIT_GROUP,
- EXIT_USER,
- EXIT_CAPABILITIES,
- EXIT_CGROUP,
- EXIT_SETSID, /* 220 */
- EXIT_CONFIRM,
- EXIT_STDERR,
- _EXIT_RESERVED, /* used to be tcpwrap, don't reuse! */
- EXIT_PAM,
- EXIT_NETWORK,
- EXIT_NAMESPACE,
- EXIT_NO_NEW_PRIVILEGES,
- EXIT_SECCOMP,
- EXIT_SELINUX_CONTEXT,
- EXIT_PERSONALITY, /* 230 */
- EXIT_APPARMOR_PROFILE,
- EXIT_ADDRESS_FAMILIES,
- EXIT_RUNTIME_DIRECTORY,
- EXIT_MAKE_STARTER,
- EXIT_CHOWN,
- EXIT_BUS_ENDPOINT,
- EXIT_SMACK_PROCESS_LABEL,
-} ExitStatus;
-
-typedef enum ExitStatusLevel {
- EXIT_STATUS_MINIMAL,
- EXIT_STATUS_SYSTEMD,
- EXIT_STATUS_LSB,
- EXIT_STATUS_FULL = EXIT_STATUS_LSB
-} ExitStatusLevel;
-
-typedef struct ExitStatusSet {
- Set *status;
- Set *signal;
-} ExitStatusSet;
-
-const char* exit_status_to_string(ExitStatus status, ExitStatusLevel level) _const_;
-
-bool is_clean_exit(int code, int status, ExitStatusSet *success_status);
-bool is_clean_exit_lsb(int code, int status, ExitStatusSet *success_status);
-
-void exit_status_set_free(ExitStatusSet *x);
-bool exit_status_set_is_empty(ExitStatusSet *x);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <dirent.h>
-#include <fcntl.h>
-
-#include "set.h"
-#include "util.h"
-#include "macro.h"
-#include "fdset.h"
-#include "sd-daemon.h"
-
-#define MAKE_SET(s) ((Set*) s)
-#define MAKE_FDSET(s) ((FDSet*) s)
-
-/* Make sure we can distuingish fd 0 and NULL */
-#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
-#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
-
-FDSet *fdset_new(void) {
- return MAKE_FDSET(set_new(NULL));
-}
-
-int fdset_new_array(FDSet **ret, int *fds, unsigned n_fds) {
- unsigned i;
- FDSet *s;
- int r;
-
- assert(ret);
-
- s = fdset_new();
- if (!s)
- return -ENOMEM;
-
- for (i = 0; i < n_fds; i++) {
-
- r = fdset_put(s, fds[i]);
- if (r < 0) {
- set_free(MAKE_SET(s));
- return r;
- }
- }
-
- *ret = s;
- return 0;
-}
-
-FDSet* fdset_free(FDSet *s) {
- void *p;
-
- while ((p = set_steal_first(MAKE_SET(s)))) {
- /* Valgrind's fd might have ended up in this set here,
- * due to fdset_new_fill(). We'll ignore all failures
- * here, so that the EBADFD that valgrind will return
- * us on close() doesn't influence us */
-
- /* When reloading duplicates of the private bus
- * connection fds and suchlike are closed here, which
- * has no effect at all, since they are only
- * duplicates. So don't be surprised about these log
- * messages. */
-
- log_debug("Closing left-over fd %i", PTR_TO_FD(p));
- close_nointr(PTR_TO_FD(p));
- }
-
- set_free(MAKE_SET(s));
- return NULL;
-}
-
-int fdset_put(FDSet *s, int fd) {
- assert(s);
- assert(fd >= 0);
-
- return set_put(MAKE_SET(s), FD_TO_PTR(fd));
-}
-
-int fdset_consume(FDSet *s, int fd) {
- int r;
-
- assert(s);
- assert(fd >= 0);
-
- r = fdset_put(s, fd);
- if (r <= 0)
- safe_close(fd);
-
- return r;
-}
-
-int fdset_put_dup(FDSet *s, int fd) {
- int copy, r;
-
- assert(s);
- assert(fd >= 0);
-
- copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
- if (copy < 0)
- return -errno;
-
- r = fdset_put(s, copy);
- if (r < 0) {
- safe_close(copy);
- return r;
- }
-
- return copy;
-}
-
-bool fdset_contains(FDSet *s, int fd) {
- assert(s);
- assert(fd >= 0);
-
- return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
-}
-
-int fdset_remove(FDSet *s, int fd) {
- assert(s);
- assert(fd >= 0);
-
- return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
-}
-
-int fdset_new_fill(FDSet **_s) {
- _cleanup_closedir_ DIR *d = NULL;
- struct dirent *de;
- int r = 0;
- FDSet *s;
-
- assert(_s);
-
- /* Creates an fdset and fills in all currently open file
- * descriptors. */
-
- d = opendir("/proc/self/fd");
- if (!d)
- return -errno;
-
- s = fdset_new();
- if (!s) {
- r = -ENOMEM;
- goto finish;
- }
-
- while ((de = readdir(d))) {
- int fd = -1;
-
- if (hidden_file(de->d_name))
- continue;
-
- r = safe_atoi(de->d_name, &fd);
- if (r < 0)
- goto finish;
-
- if (fd < 3)
- continue;
-
- if (fd == dirfd(d))
- continue;
-
- r = fdset_put(s, fd);
- if (r < 0)
- goto finish;
- }
-
- r = 0;
- *_s = s;
- s = NULL;
-
-finish:
- /* We won't close the fds here! */
- if (s)
- set_free(MAKE_SET(s));
-
- return r;
-}
-
-int fdset_cloexec(FDSet *fds, bool b) {
- Iterator i;
- void *p;
- int r;
-
- assert(fds);
-
- SET_FOREACH(p, MAKE_SET(fds), i)
- if ((r = fd_cloexec(PTR_TO_FD(p), b)) < 0)
- return r;
-
- return 0;
-}
-
-int fdset_new_listen_fds(FDSet **_s, bool unset) {
- int n, fd, r;
- FDSet *s;
-
- assert(_s);
-
- /* Creates an fdset and fills in all passed file descriptors */
-
- s = fdset_new();
- if (!s) {
- r = -ENOMEM;
- goto fail;
- }
-
- n = sd_listen_fds(unset);
- for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
- r = fdset_put(s, fd);
- if (r < 0)
- goto fail;
- }
-
- *_s = s;
- return 0;
-
-
-fail:
- if (s)
- set_free(MAKE_SET(s));
-
- return r;
-}
-
-int fdset_close_others(FDSet *fds) {
- void *e;
- Iterator i;
- int *a;
- unsigned j, m;
-
- j = 0, m = fdset_size(fds);
- a = alloca(sizeof(int) * m);
- SET_FOREACH(e, MAKE_SET(fds), i)
- a[j++] = PTR_TO_FD(e);
-
- assert(j == m);
-
- return close_all_fds(a, j);
-}
-
-unsigned fdset_size(FDSet *fds) {
- return set_size(MAKE_SET(fds));
-}
-
-bool fdset_isempty(FDSet *fds) {
- return set_isempty(MAKE_SET(fds));
-}
-
-int fdset_iterate(FDSet *s, Iterator *i) {
- void *p;
-
- p = set_iterate(MAKE_SET(s), i);
- if (!p)
- return -ENOENT;
-
- return PTR_TO_FD(p);
-}
-
-int fdset_steal_first(FDSet *fds) {
- void *p;
-
- p = set_steal_first(MAKE_SET(fds));
- if (!p)
- return -ENOENT;
-
- return PTR_TO_FD(p);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "set.h"
-
-typedef struct FDSet FDSet;
-
-FDSet* fdset_new(void);
-FDSet* fdset_free(FDSet *s);
-
-int fdset_put(FDSet *s, int fd);
-int fdset_put_dup(FDSet *s, int fd);
-int fdset_consume(FDSet *s, int fd);
-
-bool fdset_contains(FDSet *s, int fd);
-int fdset_remove(FDSet *s, int fd);
-
-int fdset_new_array(FDSet **ret, int *fds, unsigned n_fds);
-int fdset_new_fill(FDSet **ret);
-int fdset_new_listen_fds(FDSet **ret, bool unset);
-
-int fdset_cloexec(FDSet *fds, bool b);
-
-int fdset_close_others(FDSet *fds);
-
-unsigned fdset_size(FDSet *fds);
-bool fdset_isempty(FDSet *fds);
-
-int fdset_iterate(FDSet *s, Iterator *i);
-
-int fdset_steal_first(FDSet *fds);
-
-#define FDSET_FOREACH(fd, fds, i) \
- for ((i) = ITERATOR_FIRST, (fd) = fdset_iterate((fds), &(i)); (fd) >= 0; (fd) = fdset_iterate((fds), &(i)))
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(FDSet*, fdset_free);
-#define _cleanup_fdset_free_ _cleanup_(fdset_freep)
--- /dev/null
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2015 Ronny Chevalier
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <inttypes.h>
+
+#if SIZEOF_PID_T == 4
+# define PID_PRI PRIi32
+#elif SIZEOF_PID_T == 2
+# define PID_PRI PRIi16
+#else
+# error Unknown pid_t size
+#endif
+#define PID_FMT "%" PID_PRI
+
+#if SIZEOF_UID_T == 4
+# define UID_FMT "%" PRIu32
+#elif SIZEOF_UID_T == 2
+# define UID_FMT "%" PRIu16
+#else
+# error Unknown uid_t size
+#endif
+
+#if SIZEOF_GID_T == 4
+# define GID_FMT "%" PRIu32
+#elif SIZEOF_GID_T == 2
+# define GID_FMT "%" PRIu16
+#else
+# error Unknown gid_t size
+#endif
+
+#if SIZEOF_TIME_T == 8
+# define PRI_TIME PRIi64
+#elif SIZEOF_TIME_T == 4
+# define PRI_TIME PRIu32
+#else
+# error Unknown time_t size
+#endif
+
+#if SIZEOF_RLIM_T == 8
+# define RLIM_FMT "%" PRIu64
+#elif SIZEOF_RLIM_T == 4
+# define RLIM_FMT "%" PRIu32
+#else
+# error Unknown rlim_t size
+#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2015 Zbigniew Jędrzejewski-Szmek
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "fstab-util.h"
-#include "strv.h"
-#include "util.h"
-
-int fstab_filter_options(const char *opts, const char *names,
- const char **namefound, char **value, char **filtered) {
- const char *name, *n = NULL, *x;
- _cleanup_strv_free_ char **stor = NULL;
- _cleanup_free_ char *v = NULL, **strv = NULL;
-
- assert(names && *names);
-
- if (!opts)
- goto answer;
-
- /* If !value and !filtered, this function is not allowed to fail. */
-
- if (!filtered) {
- const char *word, *state;
- size_t l;
-
- FOREACH_WORD_SEPARATOR(word, l, opts, ",", state)
- NULSTR_FOREACH(name, names) {
- if (l < strlen(name))
- continue;
- if (!strneq(word, name, strlen(name)))
- continue;
-
- /* we know that the string is NUL
- * terminated, so *x is valid */
- x = word + strlen(name);
- if (IN_SET(*x, '\0', '=', ',')) {
- n = name;
- if (value) {
- free(v);
- if (IN_SET(*x, '\0', ','))
- v = NULL;
- else {
- assert(*x == '=');
- x++;
- v = strndup(x, l - strlen(name) - 1);
- if (!v)
- return -ENOMEM;
- }
- }
- }
- }
- } else {
- char **t, **s;
-
- stor = strv_split(opts, ",");
- if (!stor)
- return -ENOMEM;
- strv = memdup(stor, sizeof(char*) * (strv_length(stor) + 1));
- if (!strv)
- return -ENOMEM;
-
- for (s = t = strv; *s; s++) {
- NULSTR_FOREACH(name, names) {
- x = startswith(*s, name);
- if (x && IN_SET(*x, '\0', '='))
- goto found;
- }
-
- *t = *s;
- t++;
- continue;
- found:
- /* Keep the last occurence found */
- n = name;
- if (value) {
- free(v);
- if (*x == '\0')
- v = NULL;
- else {
- assert(*x == '=');
- x++;
- v = strdup(x);
- if (!v)
- return -ENOMEM;
- }
- }
- }
- *t = NULL;
- }
-
-answer:
- if (namefound)
- *namefound = n;
- if (filtered) {
- char *f;
-
- f = strv_join(strv, ",");
- if (!f)
- return -ENOMEM;
-
- *filtered = f;
- }
- if (value) {
- *value = v;
- v = NULL;
- }
-
- return !!n;
-}
-
-int fstab_find_pri(const char *options, int *ret) {
- _cleanup_free_ char *opt = NULL;
- int r;
- unsigned pri;
-
- assert(ret);
-
- r = fstab_filter_options(options, "pri\0", NULL, &opt, NULL);
- if (r < 0)
- return r;
- if (r == 0 || !opt)
- return 0;
-
- r = safe_atou(opt, &pri);
- if (r < 0)
- return r;
-
- if ((int) pri < 0)
- return -ERANGE;
-
- *ret = (int) pri;
- return 1;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2015 Zbigniew Jędrzejewski-Szmek
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <stddef.h>
-#include "macro.h"
-
-int fstab_filter_options(const char *opts, const char *names,
- const char **namefound, char **value, char **filtered);
-
-static inline bool fstab_test_option(const char *opts, const char *names) {
- return !!fstab_filter_options(opts, names, NULL, NULL, NULL);
-}
-
-int fstab_find_pri(const char *options, int *ret);
-
-static inline bool fstab_test_yes_no_option(const char *opts, const char *yes_no) {
- int r;
- const char *opt;
-
- /* If first name given is last, return 1.
- * If second name given is last or neither is found, return 0. */
-
- r = fstab_filter_options(opts, yes_no, &opt, NULL, NULL);
- assert(r >= 0);
-
- return opt == yes_no;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2015 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-#include <arpa/inet.h>
-#include <net/if.h>
-#include <linux/netfilter_ipv4/ip_tables.h>
-#include <linux/netfilter/nf_nat.h>
-#include <linux/netfilter/xt_addrtype.h>
-#include <libiptc/libiptc.h>
-
-#include "util.h"
-#include "fw-util.h"
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct xtc_handle*, iptc_free);
-
-static int entry_fill_basics(
- struct ipt_entry *entry,
- int protocol,
- const char *in_interface,
- const union in_addr_union *source,
- unsigned source_prefixlen,
- const char *out_interface,
- const union in_addr_union *destination,
- unsigned destination_prefixlen) {
-
- assert(entry);
-
- if (out_interface && strlen(out_interface) >= IFNAMSIZ)
- return -EINVAL;
-
- if (in_interface && strlen(in_interface) >= IFNAMSIZ)
- return -EINVAL;
-
- entry->ip.proto = protocol;
-
- if (in_interface) {
- strcpy(entry->ip.iniface, in_interface);
- memset(entry->ip.iniface_mask, 0xFF, strlen(in_interface)+1);
- }
- if (source) {
- entry->ip.src = source->in;
- in_addr_prefixlen_to_netmask(&entry->ip.smsk, source_prefixlen);
- }
-
- if (out_interface) {
- strcpy(entry->ip.outiface, out_interface);
- memset(entry->ip.outiface_mask, 0xFF, strlen(out_interface)+1);
- }
- if (destination) {
- entry->ip.dst = destination->in;
- in_addr_prefixlen_to_netmask(&entry->ip.dmsk, destination_prefixlen);
- }
-
- return 0;
-}
-
-int fw_add_masquerade(
- bool add,
- int af,
- int protocol,
- const union in_addr_union *source,
- unsigned source_prefixlen,
- const char *out_interface,
- const union in_addr_union *destination,
- unsigned destination_prefixlen) {
-
- _cleanup_(iptc_freep) struct xtc_handle *h = NULL;
- struct ipt_entry *entry, *mask;
- struct ipt_entry_target *t;
- size_t sz;
- struct nf_nat_ipv4_multi_range_compat *mr;
- int r;
-
- if (af != AF_INET)
- return -EOPNOTSUPP;
-
- if (protocol != 0 && protocol != IPPROTO_TCP && protocol != IPPROTO_UDP)
- return -EOPNOTSUPP;
-
- h = iptc_init("nat");
- if (!h)
- return -errno;
-
- sz = XT_ALIGN(sizeof(struct ipt_entry)) +
- XT_ALIGN(sizeof(struct ipt_entry_target)) +
- XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat));
-
- /* Put together the entry we want to add or remove */
- entry = alloca0(sz);
- entry->next_offset = sz;
- entry->target_offset = XT_ALIGN(sizeof(struct ipt_entry));
- r = entry_fill_basics(entry, protocol, NULL, source, source_prefixlen, out_interface, destination, destination_prefixlen);
- if (r < 0)
- return r;
-
- /* Fill in target part */
- t = ipt_get_target(entry);
- t->u.target_size =
- XT_ALIGN(sizeof(struct ipt_entry_target)) +
- XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat));
- strncpy(t->u.user.name, "MASQUERADE", sizeof(t->u.user.name));
- mr = (struct nf_nat_ipv4_multi_range_compat*) t->data;
- mr->rangesize = 1;
-
- /* Create a search mask entry */
- mask = alloca(sz);
- memset(mask, 0xFF, sz);
-
- if (add) {
- if (iptc_check_entry("POSTROUTING", entry, (unsigned char*) mask, h))
- return 0;
- if (errno != ENOENT) /* if other error than not existing yet, fail */
- return -errno;
-
- if (!iptc_insert_entry("POSTROUTING", entry, 0, h))
- return -errno;
- } else {
- if (!iptc_delete_entry("POSTROUTING", entry, (unsigned char*) mask, h)) {
- if (errno == ENOENT) /* if it's already gone, all is good! */
- return 0;
-
- return -errno;
- }
- }
-
- if (!iptc_commit(h))
- return -errno;
-
- return 0;
-}
-
-int fw_add_local_dnat(
- bool add,
- int af,
- int protocol,
- const char *in_interface,
- const union in_addr_union *source,
- unsigned source_prefixlen,
- const union in_addr_union *destination,
- unsigned destination_prefixlen,
- uint16_t local_port,
- const union in_addr_union *remote,
- uint16_t remote_port,
- const union in_addr_union *previous_remote) {
-
-
- _cleanup_(iptc_freep) struct xtc_handle *h = NULL;
- struct ipt_entry *entry, *mask;
- struct ipt_entry_target *t;
- struct ipt_entry_match *m;
- struct xt_addrtype_info_v1 *at;
- struct nf_nat_ipv4_multi_range_compat *mr;
- size_t sz, msz;
- int r;
-
- assert(add || !previous_remote);
-
- if (af != AF_INET)
- return -EOPNOTSUPP;
-
- if (protocol != IPPROTO_TCP && protocol != IPPROTO_UDP)
- return -EOPNOTSUPP;
-
- if (local_port <= 0)
- return -EINVAL;
-
- if (remote_port <= 0)
- return -EINVAL;
-
- h = iptc_init("nat");
- if (!h)
- return -errno;
-
- sz = XT_ALIGN(sizeof(struct ipt_entry)) +
- XT_ALIGN(sizeof(struct ipt_entry_match)) +
- XT_ALIGN(sizeof(struct xt_addrtype_info_v1)) +
- XT_ALIGN(sizeof(struct ipt_entry_target)) +
- XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat));
-
- if (protocol == IPPROTO_TCP)
- msz = XT_ALIGN(sizeof(struct ipt_entry_match)) +
- XT_ALIGN(sizeof(struct xt_tcp));
- else
- msz = XT_ALIGN(sizeof(struct ipt_entry_match)) +
- XT_ALIGN(sizeof(struct xt_udp));
-
- sz += msz;
-
- /* Fill in basic part */
- entry = alloca0(sz);
- entry->next_offset = sz;
- entry->target_offset =
- XT_ALIGN(sizeof(struct ipt_entry)) +
- XT_ALIGN(sizeof(struct ipt_entry_match)) +
- XT_ALIGN(sizeof(struct xt_addrtype_info_v1)) +
- msz;
- r = entry_fill_basics(entry, protocol, in_interface, source, source_prefixlen, NULL, destination, destination_prefixlen);
- if (r < 0)
- return r;
-
- /* Fill in first match */
- m = (struct ipt_entry_match*) ((uint8_t*) entry + XT_ALIGN(sizeof(struct ipt_entry)));
- m->u.match_size = msz;
- if (protocol == IPPROTO_TCP) {
- struct xt_tcp *tcp;
-
- strncpy(m->u.user.name, "tcp", sizeof(m->u.user.name));
- tcp = (struct xt_tcp*) m->data;
- tcp->dpts[0] = tcp->dpts[1] = local_port;
- tcp->spts[0] = 0;
- tcp->spts[1] = 0xFFFF;
-
- } else {
- struct xt_udp *udp;
-
- strncpy(m->u.user.name, "udp", sizeof(m->u.user.name));
- udp = (struct xt_udp*) m->data;
- udp->dpts[0] = udp->dpts[1] = local_port;
- udp->spts[0] = 0;
- udp->spts[1] = 0xFFFF;
- }
-
- /* Fill in second match */
- m = (struct ipt_entry_match*) ((uint8_t*) entry + XT_ALIGN(sizeof(struct ipt_entry)) + msz);
- m->u.match_size =
- XT_ALIGN(sizeof(struct ipt_entry_match)) +
- XT_ALIGN(sizeof(struct xt_addrtype_info_v1));
- strncpy(m->u.user.name, "addrtype", sizeof(m->u.user.name));
- m->u.user.revision = 1;
- at = (struct xt_addrtype_info_v1*) m->data;
- at->dest = XT_ADDRTYPE_LOCAL;
-
- /* Fill in target part */
- t = ipt_get_target(entry);
- t->u.target_size =
- XT_ALIGN(sizeof(struct ipt_entry_target)) +
- XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat));
- strncpy(t->u.user.name, "DNAT", sizeof(t->u.user.name));
- mr = (struct nf_nat_ipv4_multi_range_compat*) t->data;
- mr->rangesize = 1;
- mr->range[0].flags = NF_NAT_RANGE_PROTO_SPECIFIED|NF_NAT_RANGE_MAP_IPS;
- mr->range[0].min_ip = mr->range[0].max_ip = remote->in.s_addr;
- if (protocol == IPPROTO_TCP)
- mr->range[0].min.tcp.port = mr->range[0].max.tcp.port = htons(remote_port);
- else
- mr->range[0].min.udp.port = mr->range[0].max.udp.port = htons(remote_port);
-
- mask = alloca0(sz);
- memset(mask, 0xFF, sz);
-
- if (add) {
- /* Add the PREROUTING rule, if it is missing so far */
- if (!iptc_check_entry("PREROUTING", entry, (unsigned char*) mask, h)) {
- if (errno != ENOENT)
- return -EINVAL;
-
- if (!iptc_insert_entry("PREROUTING", entry, 0, h))
- return -errno;
- }
-
- /* If a previous remote is set, remove its entry */
- if (previous_remote && previous_remote->in.s_addr != remote->in.s_addr) {
- mr->range[0].min_ip = mr->range[0].max_ip = previous_remote->in.s_addr;
-
- if (!iptc_delete_entry("PREROUTING", entry, (unsigned char*) mask, h)) {
- if (errno != ENOENT)
- return -errno;
- }
-
- mr->range[0].min_ip = mr->range[0].max_ip = remote->in.s_addr;
- }
-
- /* Add the OUTPUT rule, if it is missing so far */
- if (!in_interface) {
-
- /* Don't apply onto loopback addresses */
- if (!destination) {
- entry->ip.dst.s_addr = htobe32(0x7F000000);
- entry->ip.dmsk.s_addr = htobe32(0xFF000000);
- entry->ip.invflags = IPT_INV_DSTIP;
- }
-
- if (!iptc_check_entry("OUTPUT", entry, (unsigned char*) mask, h)) {
- if (errno != ENOENT)
- return -errno;
-
- if (!iptc_insert_entry("OUTPUT", entry, 0, h))
- return -errno;
- }
-
- /* If a previous remote is set, remove its entry */
- if (previous_remote && previous_remote->in.s_addr != remote->in.s_addr) {
- mr->range[0].min_ip = mr->range[0].max_ip = previous_remote->in.s_addr;
-
- if (!iptc_delete_entry("OUTPUT", entry, (unsigned char*) mask, h)) {
- if (errno != ENOENT)
- return -errno;
- }
- }
- }
- } else {
- if (!iptc_delete_entry("PREROUTING", entry, (unsigned char*) mask, h)) {
- if (errno != ENOENT)
- return -errno;
- }
-
- if (!in_interface) {
- if (!destination) {
- entry->ip.dst.s_addr = htobe32(0x7F000000);
- entry->ip.dmsk.s_addr = htobe32(0xFF000000);
- entry->ip.invflags = IPT_INV_DSTIP;
- }
-
- if (!iptc_delete_entry("OUTPUT", entry, (unsigned char*) mask, h)) {
- if (errno != ENOENT)
- return -errno;
- }
- }
- }
-
- if (!iptc_commit(h))
- return -errno;
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <unistd.h>
-
-#include "util.h"
-#include "special.h"
-#include "mkdir.h"
-#include "unit-name.h"
-#include "generator.h"
-#include "path-util.h"
-#include "fstab-util.h"
-#include "dropin.h"
-
-int generator_write_fsck_deps(
- FILE *f,
- const char *dest,
- const char *what,
- const char *where,
- const char *fstype) {
-
- assert(f);
- assert(dest);
- assert(what);
- assert(where);
-
- if (!is_device_path(what)) {
- log_warning("Checking was requested for \"%s\", but it is not a device.", what);
- return 0;
- }
-
- if (!isempty(fstype) && !streq(fstype, "auto")) {
- int r;
- r = fsck_exists(fstype);
- if (r == -ENOENT) {
- /* treat missing check as essentially OK */
- log_debug_errno(r, "Checking was requested for %s, but fsck.%s does not exist: %m", what, fstype);
- return 0;
- } else if (r < 0)
- return log_warning_errno(r, "Checking was requested for %s, but fsck.%s cannot be used: %m", what, fstype);
- }
-
- if (streq(where, "/")) {
- char *lnk;
-
- lnk = strjoina(dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
-
- mkdir_parents(lnk, 0755);
- if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0)
- return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
-
- } else {
- _cleanup_free_ char *fsck = NULL;
-
- fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
- if (!fsck)
- return log_oom();
-
- fprintf(f,
- "RequiresOverridable=%s\n"
- "After=%s\n",
- fsck,
- fsck);
- }
-
- return 0;
-}
-
-int generator_write_timeouts(const char *dir, const char *what, const char *where,
- const char *opts, char **filtered) {
-
- /* Allow configuration how long we wait for a device that
- * backs a mount point to show up. This is useful to support
- * endless device timeouts for devices that show up only after
- * user input, like crypto devices. */
-
- _cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL;
- usec_t u;
- int r;
-
- r = fstab_filter_options(opts, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0",
- NULL, &timeout, filtered);
- if (r <= 0)
- return r;
-
- r = parse_sec(timeout, &u);
- if (r < 0) {
- log_warning("Failed to parse timeout for %s, ignoring: %s",
- where, timeout);
- return 0;
- }
-
- node = fstab_node_to_udev_node(what);
- if (!node)
- return log_oom();
-
- unit = unit_name_from_path(node, ".device");
- if (!unit)
- return log_oom();
-
- return write_drop_in_format(dir, unit, 50, "device-timeout",
- "# Automatically generated by %s\n\n"
- "[Unit]\nJobTimeoutSec=" USEC_FMT,
- program_invocation_short_name,
- u / USEC_PER_SEC);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdio.h>
-
-int generator_write_fsck_deps(FILE *f, const char *dest, const char *what, const char *where, const char *type);
-
-int generator_write_timeouts(const char *dir, const char *what, const char *where,
- const char *opts, char **filtered);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#pragma once
-
-#include <endian.h>
-
-#include "sd-id128.h"
-
-/* We only support root disk discovery for x86, x86-64 and ARM for
- * now, since EFI for anything else doesn't really exist, and we only
- * care for root partitions on the same disk as the EFI ESP. */
-
-#define GPT_ROOT_X86 SD_ID128_MAKE(44,47,95,40,f2,97,41,b2,9a,f7,d1,31,d5,f0,45,8a)
-#define GPT_ROOT_X86_64 SD_ID128_MAKE(4f,68,bc,e3,e8,cd,4d,b1,96,e7,fb,ca,f9,84,b7,09)
-#define GPT_ROOT_ARM SD_ID128_MAKE(69,da,d7,10,2c,e4,4e,3c,b1,6c,21,a1,d4,9a,be,d3)
-#define GPT_ROOT_ARM_64 SD_ID128_MAKE(b9,21,b0,45,1d,f0,41,c3,af,44,4c,6f,28,0d,3f,ae)
-
-#define GPT_ESP SD_ID128_MAKE(c1,2a,73,28,f8,1f,11,d2,ba,4b,00,a0,c9,3e,c9,3b)
-#define GPT_SWAP SD_ID128_MAKE(06,57,fd,6d,a4,ab,43,c4,84,e5,09,33,c8,4b,4f,4f)
-#define GPT_HOME SD_ID128_MAKE(93,3a,c7,e1,2e,b4,4f,13,b8,44,0e,14,e2,ae,f9,15)
-#define GPT_SRV SD_ID128_MAKE(3b,8f,84,25,20,e0,4f,3b,90,7f,1a,25,a7,6f,98,e8)
-
-#if defined(__x86_64__)
-# define GPT_ROOT_NATIVE GPT_ROOT_X86_64
-# define GPT_ROOT_SECONDARY GPT_ROOT_X86
-#elif defined(__i386__)
-# define GPT_ROOT_NATIVE GPT_ROOT_X86
-#endif
-
-#if defined(__aarch64__) && (__BYTE_ORDER != __BIG_ENDIAN)
-# define GPT_ROOT_NATIVE GPT_ROOT_ARM_64
-# define GPT_ROOT_SECONDARY GPT_ROOT_ARM
-#elif defined(__arm__) && (__BYTE_ORDER != __BIG_ENDIAN)
-# define GPT_ROOT_NATIVE GPT_ROOT_ARM
-#endif
-
-/* Flags we recognize on the root, swap, home and srv partitions when
- * doing auto-discovery. These happen to be identical to what
- * Microsoft defines for its own Basic Data Partitions, but that's
- * just because we saw no point in defining any other values here. */
-#define GPT_FLAG_READ_ONLY (1ULL << 60)
-#define GPT_FLAG_NO_AUTO (1ULL << 63)
-
-#define GPT_LINUX_GENERIC SD_ID128_MAKE(0f,c6,3d,af,84,83,47,72,8e,79,3d,69,d8,47,7d,e4)
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <unistd.h>
-
-#include "ima-util.h"
-
-static int use_ima_cached = -1;
-
-bool use_ima(void) {
-
- if (use_ima_cached < 0)
- use_ima_cached = access("/sys/kernel/security/ima/", F_OK) >= 0;
-
- return use_ima_cached;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-bool use_ima(void);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2015 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-#include "import-util.h"
-
-int import_url_last_component(const char *url, char **ret) {
- const char *e, *p;
- char *s;
-
- e = strchrnul(url, '?');
-
- while (e > url && e[-1] == '/')
- e--;
-
- p = e;
- while (p > url && p[-1] != '/')
- p--;
-
- if (e <= p)
- return -EINVAL;
-
- s = strndup(p, e - p);
- if (!s)
- return -ENOMEM;
-
- *ret = s;
- return 0;
-}
-
-
-int import_url_change_last_component(const char *url, const char *suffix, char **ret) {
- const char *e;
- char *s;
-
- assert(url);
- assert(ret);
-
- e = strchrnul(url, '?');
-
- while (e > url && e[-1] == '/')
- e--;
-
- while (e > url && e[-1] != '/')
- e--;
-
- if (e <= url)
- return -EINVAL;
-
- s = new(char, (e - url) + strlen(suffix) + 1);
- if (!s)
- return -ENOMEM;
-
- strcpy(mempcpy(s, url, e - url), suffix);
- *ret = s;
- return 0;
-}
-
-static const char* const import_verify_table[_IMPORT_VERIFY_MAX] = {
- [IMPORT_VERIFY_NO] = "no",
- [IMPORT_VERIFY_CHECKSUM] = "checksum",
- [IMPORT_VERIFY_SIGNATURE] = "signature",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(import_verify, ImportVerify);
-
-int tar_strip_suffixes(const char *name, char **ret) {
- const char *e;
- char *s;
-
- e = endswith(name, ".tar");
- if (!e)
- e = endswith(name, ".tar.xz");
- if (!e)
- e = endswith(name, ".tar.gz");
- if (!e)
- e = endswith(name, ".tar.bz2");
- if (!e)
- e = endswith(name, ".tgz");
- if (!e)
- e = strchr(name, 0);
-
- if (e <= name)
- return -EINVAL;
-
- s = strndup(name, e - name);
- if (!s)
- return -ENOMEM;
-
- *ret = s;
- return 0;
-}
-
-int raw_strip_suffixes(const char *p, char **ret) {
-
- static const char suffixes[] =
- ".xz\0"
- ".gz\0"
- ".bz2\0"
- ".raw\0"
- ".qcow2\0"
- ".img\0"
- ".bin\0";
-
- _cleanup_free_ char *q = NULL;
-
- q = strdup(p);
- if (!q)
- return -ENOMEM;
-
- for (;;) {
- const char *sfx;
- bool changed = false;
-
- NULSTR_FOREACH(sfx, suffixes) {
- char *e;
-
- e = endswith(q, sfx);
- if (e) {
- *e = 0;
- changed = true;
- }
- }
-
- if (!changed)
- break;
- }
-
- *ret = q;
- q = NULL;
-
- return 0;
-}
-
-bool dkr_name_is_valid(const char *name) {
- const char *slash, *p;
-
- if (isempty(name))
- return false;
-
- slash = strchr(name, '/');
- if (!slash)
- return false;
-
- if (!filename_is_valid(slash + 1))
- return false;
-
- p = strndupa(name, slash - name);
- if (!filename_is_valid(p))
- return false;
-
- return true;
-}
-
-bool dkr_id_is_valid(const char *id) {
-
- if (!filename_is_valid(id))
- return false;
-
- if (!in_charset(id, "0123456789abcdef"))
- return false;
-
- return true;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2015 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-#include "macro.h"
-
-typedef enum ImportVerify {
- IMPORT_VERIFY_NO,
- IMPORT_VERIFY_CHECKSUM,
- IMPORT_VERIFY_SIGNATURE,
- _IMPORT_VERIFY_MAX,
- _IMPORT_VERIFY_INVALID = -1,
-} ImportVerify;
-
-int import_url_last_component(const char *url, char **ret);
-int import_url_change_last_component(const char *url, const char *suffix, char **ret);
-
-const char* import_verify_to_string(ImportVerify v) _const_;
-ImportVerify import_verify_from_string(const char *s) _pure_;
-
-int tar_strip_suffixes(const char *name, char **ret);
-int raw_strip_suffixes(const char *name, char **ret);
-
-bool dkr_name_is_valid(const char *name);
-bool dkr_id_is_valid(const char *id);
-#define dkr_tag_is_valid(tag) filename_is_valid(tag)
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <arpa/inet.h>
-
-#include "in-addr-util.h"
-
-int in_addr_is_null(int family, const union in_addr_union *u) {
- assert(u);
-
- if (family == AF_INET)
- return u->in.s_addr == 0;
-
- if (family == AF_INET6)
- return
- u->in6.s6_addr32[0] == 0 &&
- u->in6.s6_addr32[1] == 0 &&
- u->in6.s6_addr32[2] == 0 &&
- u->in6.s6_addr32[3] == 0;
-
- return -EAFNOSUPPORT;
-}
-
-int in_addr_is_link_local(int family, const union in_addr_union *u) {
- assert(u);
-
- if (family == AF_INET)
- return (be32toh(u->in.s_addr) & 0xFFFF0000) == (169U << 24 | 254U << 16);
-
- if (family == AF_INET6)
- return IN6_IS_ADDR_LINKLOCAL(&u->in6);
-
- return -EAFNOSUPPORT;
-}
-
-int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
- assert(a);
- assert(b);
-
- if (family == AF_INET)
- return a->in.s_addr == b->in.s_addr;
-
- if (family == AF_INET6)
- return
- a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
- a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
- a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
- a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
-
- return -EAFNOSUPPORT;
-}
-
-int in_addr_prefix_intersect(
- int family,
- const union in_addr_union *a,
- unsigned aprefixlen,
- const union in_addr_union *b,
- unsigned bprefixlen) {
-
- unsigned m;
-
- assert(a);
- assert(b);
-
- /* Checks whether there are any addresses that are in both
- * networks */
-
- m = MIN(aprefixlen, bprefixlen);
-
- if (family == AF_INET) {
- uint32_t x, nm;
-
- x = be32toh(a->in.s_addr ^ b->in.s_addr);
- nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
-
- return (x & nm) == 0;
- }
-
- if (family == AF_INET6) {
- unsigned i;
-
- if (m > 128)
- m = 128;
-
- for (i = 0; i < 16; i++) {
- uint8_t x, nm;
-
- x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
-
- if (m < 8)
- nm = 0xFF << (8 - m);
- else
- nm = 0xFF;
-
- if ((x & nm) != 0)
- return 0;
-
- if (m > 8)
- m -= 8;
- else
- m = 0;
- }
-
- return 1;
- }
-
- return -EAFNOSUPPORT;
-}
-
-int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
- assert(u);
-
- /* Increases the network part of an address by one. Returns
- * positive it that succeeds, or 0 if this overflows. */
-
- if (prefixlen <= 0)
- return 0;
-
- if (family == AF_INET) {
- uint32_t c, n;
-
- if (prefixlen > 32)
- prefixlen = 32;
-
- c = be32toh(u->in.s_addr);
- n = c + (1UL << (32 - prefixlen));
- if (n < c)
- return 0;
- n &= 0xFFFFFFFFUL << (32 - prefixlen);
-
- u->in.s_addr = htobe32(n);
- return 1;
- }
-
- if (family == AF_INET6) {
- struct in6_addr add = {}, result;
- uint8_t overflow = 0;
- unsigned i;
-
- if (prefixlen > 128)
- prefixlen = 128;
-
- /* First calculate what we have to add */
- add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
-
- for (i = 16; i > 0; i--) {
- unsigned j = i - 1;
-
- result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
- overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
- }
-
- if (overflow)
- return 0;
-
- u->in6 = result;
- return 1;
- }
-
- return -EAFNOSUPPORT;
-}
-
-int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
- char *x;
- size_t l;
-
- assert(u);
- assert(ret);
-
- if (family == AF_INET)
- l = INET_ADDRSTRLEN;
- else if (family == AF_INET6)
- l = INET6_ADDRSTRLEN;
- else
- return -EAFNOSUPPORT;
-
- x = new(char, l);
- if (!x)
- return -ENOMEM;
-
- errno = 0;
- if (!inet_ntop(family, u, x, l)) {
- free(x);
- return errno ? -errno : -EINVAL;
- }
-
- *ret = x;
- return 0;
-}
-
-int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
-
- assert(s);
- assert(ret);
-
- if (!IN_SET(family, AF_INET, AF_INET6))
- return -EAFNOSUPPORT;
-
- errno = 0;
- if (inet_pton(family, s, ret) <= 0)
- return errno ? -errno : -EINVAL;
-
- return 0;
-}
-
-int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret) {
- int r;
-
- assert(s);
- assert(family);
- assert(ret);
-
- r = in_addr_from_string(AF_INET, s, ret);
- if (r >= 0) {
- *family = AF_INET;
- return 0;
- }
-
- r = in_addr_from_string(AF_INET6, s, ret);
- if (r >= 0) {
- *family = AF_INET6;
- return 0;
- }
-
- return -EINVAL;
-}
-
-unsigned char in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
- assert(addr);
-
- return 32 - u32ctz(be32toh(addr->s_addr));
-}
-
-struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
- assert(addr);
- assert(prefixlen <= 32);
-
- /* Shifting beyond 32 is not defined, handle this specially. */
- if (prefixlen == 0)
- addr->s_addr = 0;
- else
- addr->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
-
- return addr;
-}
-
-int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
- uint8_t msb_octet = *(uint8_t*) addr;
-
- /* addr may not be aligned, so make sure we only access it byte-wise */
-
- assert(addr);
- assert(prefixlen);
-
- if (msb_octet < 128)
- /* class A, leading bits: 0 */
- *prefixlen = 8;
- else if (msb_octet < 192)
- /* class B, leading bits 10 */
- *prefixlen = 16;
- else if (msb_octet < 224)
- /* class C, leading bits 110 */
- *prefixlen = 24;
- else
- /* class D or E, no default prefixlen */
- return -ERANGE;
-
- return 0;
-}
-
-int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
- unsigned char prefixlen;
- int r;
-
- assert(addr);
- assert(mask);
-
- r = in_addr_default_prefixlen(addr, &prefixlen);
- if (r < 0)
- return r;
-
- in_addr_prefixlen_to_netmask(mask, prefixlen);
- return 0;
-}
-
-int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) {
- assert(addr);
-
- if (family == AF_INET) {
- struct in_addr mask;
-
- if (!in_addr_prefixlen_to_netmask(&mask, prefixlen))
- return -EINVAL;
-
- addr->in.s_addr &= mask.s_addr;
- return 0;
- }
-
- if (family == AF_INET6) {
- unsigned i;
-
- for (i = 0; i < 16; i++) {
- uint8_t mask;
-
- if (prefixlen >= 8) {
- mask = 0xFF;
- prefixlen -= 8;
- } else {
- mask = 0xFF << (8 - prefixlen);
- prefixlen = 0;
- }
-
- addr->in6.s6_addr[i] &= mask;
- }
-
- return 0;
- }
-
- return -EAFNOSUPPORT;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <netinet/in.h>
-
-#include "macro.h"
-#include "util.h"
-
-union in_addr_union {
- struct in_addr in;
- struct in6_addr in6;
-};
-
-int in_addr_is_null(int family, const union in_addr_union *u);
-int in_addr_is_link_local(int family, const union in_addr_union *u);
-int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b);
-int in_addr_prefix_intersect(int family, const union in_addr_union *a, unsigned aprefixlen, const union in_addr_union *b, unsigned bprefixlen);
-int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen);
-int in_addr_to_string(int family, const union in_addr_union *u, char **ret);
-int in_addr_from_string(int family, const char *s, union in_addr_union *ret);
-int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret);
-unsigned char in_addr_netmask_to_prefixlen(const struct in_addr *addr);
-struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen);
-int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen);
-int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask);
-int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen);
-
-static inline size_t FAMILY_ADDRESS_SIZE(int family) {
- assert(family == AF_INET || family == AF_INET6);
- return family == AF_INET6 ? 16 : 4;
-}
-
-#define IN_ADDR_NULL ((union in_addr_union) {})
+++ /dev/null
-/*
- * initreq.h Interface to talk to init through /dev/initctl.
- *
- * Copyright (C) 1995-2004 Miquel van Smoorenburg
- *
- * This library 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 of the License, or (at your option) any later version.
- *
- * Version: @(#)initreq.h 1.28 31-Mar-2004 MvS
- *
- */
-#ifndef _INITREQ_H
-#define _INITREQ_H
-
-#include <sys/param.h>
-
-#if defined(__FreeBSD_kernel__)
-# define INIT_FIFO "/etc/.initctl"
-#else
-# define INIT_FIFO "/dev/initctl"
-#endif
-
-#define INIT_MAGIC 0x03091969
-#define INIT_CMD_START 0
-#define INIT_CMD_RUNLVL 1
-#define INIT_CMD_POWERFAIL 2
-#define INIT_CMD_POWERFAILNOW 3
-#define INIT_CMD_POWEROK 4
-#define INIT_CMD_BSD 5
-#define INIT_CMD_SETENV 6
-#define INIT_CMD_UNSETENV 7
-
-#define INIT_CMD_CHANGECONS 12345
-
-#ifdef MAXHOSTNAMELEN
-# define INITRQ_HLEN MAXHOSTNAMELEN
-#else
-# define INITRQ_HLEN 64
-#endif
-
-/*
- * This is what BSD 4.4 uses when talking to init.
- * Linux doesn't use this right now.
- */
-struct init_request_bsd {
- char gen_id[8]; /* Beats me.. telnetd uses "fe" */
- char tty_id[16]; /* Tty name minus /dev/tty */
- char host[INITRQ_HLEN]; /* Hostname */
- char term_type[16]; /* Terminal type */
- int signal; /* Signal to send */
- int pid; /* Process to send to */
- char exec_name[128]; /* Program to execute */
- char reserved[128]; /* For future expansion. */
-};
-
-
-/*
- * Because of legacy interfaces, "runlevel" and "sleeptime"
- * aren't in a separate struct in the union.
- *
- * The weird sizes are because init expects the whole
- * struct to be 384 bytes.
- */
-struct init_request {
- int magic; /* Magic number */
- int cmd; /* What kind of request */
- int runlevel; /* Runlevel to change to */
- int sleeptime; /* Time between TERM and KILL */
- union {
- struct init_request_bsd bsd;
- char data[368];
- } i;
-};
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Zbigniew Jędrzejewski-Szmek
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-
-#include "specifier.h"
-#include "unit-name.h"
-#include "util.h"
-#include "install-printf.h"
-
-static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
- InstallInfo *i = userdata;
- char *n;
-
- assert(i);
-
- n = unit_name_to_prefix_and_instance(i->name);
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
-}
-
-static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
- InstallInfo *i = userdata;
- char *n;
-
- assert(i);
-
- n = unit_name_to_prefix(i->name);
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
-}
-
-static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
- InstallInfo *i = userdata;
- char *instance;
- int r;
-
- assert(i);
-
- r = unit_name_to_instance(i->name, &instance);
- if (r < 0)
- return r;
-
- if (!instance) {
- instance = strdup("");
- if (!instance)
- return -ENOMEM;
- }
-
- *ret = instance;
- return 0;
-}
-
-static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
- InstallInfo *i = userdata;
- const char *username;
- _cleanup_free_ char *tmp = NULL;
- char *printed = NULL;
-
- assert(i);
-
- if (i->user)
- username = i->user;
- else
- /* get USER env from env or our own uid */
- username = tmp = getusername_malloc();
-
- switch (specifier) {
- case 'u':
- printed = strdup(username);
- break;
- case 'U': {
- /* fish username from passwd */
- uid_t uid;
- int r;
-
- r = get_user_creds(&username, &uid, NULL, NULL, NULL);
- if (r < 0)
- return r;
-
- if (asprintf(&printed, UID_FMT, uid) < 0)
- return -ENOMEM;
- break;
- }}
-
-
- *ret = printed;
- return 0;
-}
-
-
-int install_full_printf(InstallInfo *i, const char *format, char **ret) {
-
- /* This is similar to unit_full_printf() but does not support
- * anything path-related.
- *
- * %n: the full id of the unit (foo@bar.waldo)
- * %N: the id of the unit without the suffix (foo@bar)
- * %p: the prefix (foo)
- * %i: the instance (bar)
-
- * %U the UID of the configured user or running user
- * %u the username of the configured user or running user
- * %m the machine ID of the running system
- * %H the host name of the running system
- * %b the boot ID of the running system
- * %v `uname -r` of the running system
- */
-
- const Specifier table[] = {
- { 'n', specifier_string, i->name },
- { 'N', specifier_prefix_and_instance, NULL },
- { 'p', specifier_prefix, NULL },
- { 'i', specifier_instance, NULL },
-
- { 'U', specifier_user_name, NULL },
- { 'u', specifier_user_name, NULL },
-
- { 'm', specifier_machine_id, NULL },
- { 'H', specifier_host_name, NULL },
- { 'b', specifier_boot_id, NULL },
- { 'v', specifier_kernel_release, NULL },
- {}
- };
-
- assert(i);
- assert(format);
- assert(ret);
-
- return specifier_printf(format, table, i, ret);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Zbigniew Jędrzejewski-Szmek
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#pragma once
-
-#include "install.h"
-int install_full_printf(InstallInfo *i, const char *format, char **ret);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <string.h>
-#include <fnmatch.h>
-
-#include "util.h"
-#include "mkdir.h"
-#include "hashmap.h"
-#include "set.h"
-#include "path-util.h"
-#include "path-lookup.h"
-#include "strv.h"
-#include "unit-name.h"
-#include "install.h"
-#include "conf-parser.h"
-#include "conf-files.h"
-#include "install-printf.h"
-#include "special.h"
-
-typedef struct {
- OrderedHashmap *will_install;
- OrderedHashmap *have_installed;
-} InstallContext;
-
-static int in_search_path(const char *path, char **search) {
- _cleanup_free_ char *parent = NULL;
- int r;
-
- assert(path);
-
- r = path_get_parent(path, &parent);
- if (r < 0)
- return r;
-
- return strv_contains(search, parent);
-}
-
-static int get_config_path(UnitFileScope scope, bool runtime, const char *root_dir, char **ret) {
- char *p = NULL;
- int r;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(ret);
-
- switch (scope) {
-
- case UNIT_FILE_SYSTEM:
-
- if (runtime)
- p = path_join(root_dir, "/run/systemd/system", NULL);
- else
- p = path_join(root_dir, SYSTEM_CONFIG_UNIT_PATH, NULL);
- break;
-
- case UNIT_FILE_GLOBAL:
-
- if (root_dir)
- return -EINVAL;
-
- if (runtime)
- p = strdup("/run/systemd/user");
- else
- p = strdup(USER_CONFIG_UNIT_PATH);
- break;
-
- case UNIT_FILE_USER:
-
- if (root_dir)
- return -EINVAL;
-
- if (runtime)
- r = user_runtime_dir(&p);
- else
- r = user_config_home(&p);
-
- if (r <= 0)
- return r < 0 ? r : -ENOENT;
-
- break;
-
- default:
- assert_not_reached("Bad scope");
- }
-
- if (!p)
- return -ENOMEM;
-
- *ret = p;
- return 0;
-}
-
-static int add_file_change(
- UnitFileChange **changes,
- unsigned *n_changes,
- UnitFileChangeType type,
- const char *path,
- const char *source) {
-
- UnitFileChange *c;
- unsigned i;
-
- assert(path);
- assert(!changes == !n_changes);
-
- if (!changes)
- return 0;
-
- c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange));
- if (!c)
- return -ENOMEM;
-
- *changes = c;
- i = *n_changes;
-
- c[i].type = type;
- c[i].path = strdup(path);
- if (!c[i].path)
- return -ENOMEM;
-
- path_kill_slashes(c[i].path);
-
- if (source) {
- c[i].source = strdup(source);
- if (!c[i].source) {
- free(c[i].path);
- return -ENOMEM;
- }
-
- path_kill_slashes(c[i].path);
- } else
- c[i].source = NULL;
-
- *n_changes = i+1;
- return 0;
-}
-
-static int mark_symlink_for_removal(
- Set **remove_symlinks_to,
- const char *p) {
-
- char *n;
- int r;
-
- assert(p);
-
- r = set_ensure_allocated(remove_symlinks_to, &string_hash_ops);
- if (r < 0)
- return r;
-
- n = strdup(p);
- if (!n)
- return -ENOMEM;
-
- path_kill_slashes(n);
-
- r = set_consume(*remove_symlinks_to, n);
- if (r < 0)
- return r == -EEXIST ? 0 : r;
-
- return 0;
-}
-
-static int remove_marked_symlinks_fd(
- Set *remove_symlinks_to,
- int fd,
- const char *path,
- const char *config_path,
- bool *deleted,
- UnitFileChange **changes,
- unsigned *n_changes,
- char** instance_whitelist) {
-
- _cleanup_closedir_ DIR *d = NULL;
- int r = 0;
-
- assert(remove_symlinks_to);
- assert(fd >= 0);
- assert(path);
- assert(config_path);
- assert(deleted);
-
- d = fdopendir(fd);
- if (!d) {
- safe_close(fd);
- return -errno;
- }
-
- rewinddir(d);
-
- for (;;) {
- struct dirent *de;
-
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0) {
- r = -errno;
- break;
- }
-
- if (!de)
- break;
-
- if (hidden_file(de->d_name))
- continue;
-
- dirent_ensure_type(d, de);
-
- if (de->d_type == DT_DIR) {
- int nfd, q;
- _cleanup_free_ char *p = NULL;
-
- nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
- if (nfd < 0) {
- if (errno == ENOENT)
- continue;
-
- if (r == 0)
- r = -errno;
- continue;
- }
-
- p = path_make_absolute(de->d_name, path);
- if (!p) {
- safe_close(nfd);
- return -ENOMEM;
- }
-
- /* This will close nfd, regardless whether it succeeds or not */
- q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, deleted, changes, n_changes, instance_whitelist);
- if (q < 0 && r == 0)
- r = q;
-
- } else if (de->d_type == DT_LNK) {
- _cleanup_free_ char *p = NULL, *dest = NULL;
- int q;
- bool found;
-
- if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
- continue;
-
- if (unit_name_is_instance(de->d_name) &&
- instance_whitelist &&
- !strv_contains(instance_whitelist, de->d_name)) {
-
- _cleanup_free_ char *w;
-
- /* OK, the file is not listed directly
- * in the whitelist, so let's check if
- * the template of it might be
- * listed. */
-
- w = unit_name_template(de->d_name);
- if (!w)
- return -ENOMEM;
-
- if (!strv_contains(instance_whitelist, w))
- continue;
- }
-
- p = path_make_absolute(de->d_name, path);
- if (!p)
- return -ENOMEM;
-
- q = readlink_and_canonicalize(p, &dest);
- if (q < 0) {
- if (q == -ENOENT)
- continue;
-
- if (r == 0)
- r = q;
- continue;
- }
-
- found =
- set_get(remove_symlinks_to, dest) ||
- set_get(remove_symlinks_to, basename(dest));
-
- if (!found)
- continue;
-
- if (unlink(p) < 0 && errno != ENOENT) {
- if (r == 0)
- r = -errno;
- continue;
- }
-
- path_kill_slashes(p);
- rmdir_parents(p, config_path);
- add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
-
- if (!set_get(remove_symlinks_to, p)) {
-
- q = mark_symlink_for_removal(&remove_symlinks_to, p);
- if (q < 0) {
- if (r == 0)
- r = q;
- } else
- *deleted = true;
- }
- }
- }
-
- return r;
-}
-
-static int remove_marked_symlinks(
- Set *remove_symlinks_to,
- const char *config_path,
- UnitFileChange **changes,
- unsigned *n_changes,
- char** instance_whitelist) {
-
- _cleanup_close_ int fd = -1;
- int r = 0;
- bool deleted;
-
- assert(config_path);
-
- if (set_size(remove_symlinks_to) <= 0)
- return 0;
-
- fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
- if (fd < 0)
- return -errno;
-
- do {
- int q, cfd;
- deleted = false;
-
- cfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
- if (cfd < 0) {
- r = -errno;
- break;
- }
-
- /* This takes possession of cfd and closes it */
- q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, &deleted, changes, n_changes, instance_whitelist);
- if (r == 0)
- r = q;
- } while (deleted);
-
- return r;
-}
-
-static int find_symlinks_fd(
- const char *name,
- int fd,
- const char *path,
- const char *config_path,
- bool *same_name_link) {
-
- int r = 0;
- _cleanup_closedir_ DIR *d = NULL;
-
- assert(name);
- assert(fd >= 0);
- assert(path);
- assert(config_path);
- assert(same_name_link);
-
- d = fdopendir(fd);
- if (!d) {
- safe_close(fd);
- return -errno;
- }
-
- for (;;) {
- struct dirent *de;
-
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
-
- if (!de)
- return r;
-
- if (hidden_file(de->d_name))
- continue;
-
- dirent_ensure_type(d, de);
-
- if (de->d_type == DT_DIR) {
- int nfd, q;
- _cleanup_free_ char *p = NULL;
-
- nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
- if (nfd < 0) {
- if (errno == ENOENT)
- continue;
-
- if (r == 0)
- r = -errno;
- continue;
- }
-
- p = path_make_absolute(de->d_name, path);
- if (!p) {
- safe_close(nfd);
- return -ENOMEM;
- }
-
- /* This will close nfd, regardless whether it succeeds or not */
- q = find_symlinks_fd(name, nfd, p, config_path, same_name_link);
- if (q > 0)
- return 1;
- if (r == 0)
- r = q;
-
- } else if (de->d_type == DT_LNK) {
- _cleanup_free_ char *p = NULL, *dest = NULL;
- bool found_path, found_dest, b = false;
- int q;
-
- /* Acquire symlink name */
- p = path_make_absolute(de->d_name, path);
- if (!p)
- return -ENOMEM;
-
- /* Acquire symlink destination */
- q = readlink_and_canonicalize(p, &dest);
- if (q < 0) {
- if (q == -ENOENT)
- continue;
-
- if (r == 0)
- r = q;
- continue;
- }
-
- /* Check if the symlink itself matches what we
- * are looking for */
- if (path_is_absolute(name))
- found_path = path_equal(p, name);
- else
- found_path = streq(de->d_name, name);
-
- /* Check if what the symlink points to
- * matches what we are looking for */
- if (path_is_absolute(name))
- found_dest = path_equal(dest, name);
- else
- found_dest = streq(basename(dest), name);
-
- if (found_path && found_dest) {
- _cleanup_free_ char *t = NULL;
-
- /* Filter out same name links in the main
- * config path */
- t = path_make_absolute(name, config_path);
- if (!t)
- return -ENOMEM;
-
- b = path_equal(t, p);
- }
-
- if (b)
- *same_name_link = true;
- else if (found_path || found_dest)
- return 1;
- }
- }
-}
-
-static int find_symlinks(
- const char *name,
- const char *config_path,
- bool *same_name_link) {
-
- int fd;
-
- assert(name);
- assert(config_path);
- assert(same_name_link);
-
- fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
- if (fd < 0) {
- if (errno == ENOENT)
- return 0;
- return -errno;
- }
-
- /* This takes possession of fd and closes it */
- return find_symlinks_fd(name, fd, config_path, config_path, same_name_link);
-}
-
-static int find_symlinks_in_scope(
- UnitFileScope scope,
- const char *root_dir,
- const char *name,
- UnitFileState *state) {
-
- int r;
- _cleanup_free_ char *path = NULL;
- bool same_name_link_runtime = false, same_name_link = false;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(name);
-
- /* First look in runtime config path */
- r = get_config_path(scope, true, root_dir, &path);
- if (r < 0)
- return r;
-
- r = find_symlinks(name, path, &same_name_link_runtime);
- if (r < 0)
- return r;
- else if (r > 0) {
- *state = UNIT_FILE_ENABLED_RUNTIME;
- return r;
- }
-
- /* Then look in the normal config path */
- r = get_config_path(scope, false, root_dir, &path);
- if (r < 0)
- return r;
-
- r = find_symlinks(name, path, &same_name_link);
- if (r < 0)
- return r;
- else if (r > 0) {
- *state = UNIT_FILE_ENABLED;
- return r;
- }
-
- /* Hmm, we didn't find it, but maybe we found the same name
- * link? */
- if (same_name_link_runtime) {
- *state = UNIT_FILE_LINKED_RUNTIME;
- return 1;
- } else if (same_name_link) {
- *state = UNIT_FILE_LINKED;
- return 1;
- }
-
- return 0;
-}
-
-int unit_file_mask(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- char **i;
- _cleanup_free_ char *prefix = NULL;
- int r;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
-
- r = get_config_path(scope, runtime, root_dir, &prefix);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, files) {
- _cleanup_free_ char *path = NULL;
-
- if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
- if (r == 0)
- r = -EINVAL;
- continue;
- }
-
- path = path_make_absolute(*i, prefix);
- if (!path) {
- r = -ENOMEM;
- break;
- }
-
- if (symlink("/dev/null", path) >= 0) {
- add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
- continue;
- }
-
- if (errno == EEXIST) {
-
- if (null_or_empty_path(path) > 0)
- continue;
-
- if (force) {
- if (symlink_atomic("/dev/null", path) >= 0) {
- add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
- add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
- continue;
- }
- }
-
- if (r == 0)
- r = -EEXIST;
- } else {
- if (r == 0)
- r = -errno;
- }
- }
-
- return r;
-}
-
-int unit_file_unmask(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- char **i, *config_path = NULL;
- int r, q;
- Set *remove_symlinks_to = NULL;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
-
- r = get_config_path(scope, runtime, root_dir, &config_path);
- if (r < 0)
- goto finish;
-
- STRV_FOREACH(i, files) {
- _cleanup_free_ char *path = NULL;
-
- if (!unit_name_is_valid(*i, TEMPLATE_VALID)) {
- if (r == 0)
- r = -EINVAL;
- continue;
- }
-
- path = path_make_absolute(*i, config_path);
- if (!path) {
- r = -ENOMEM;
- break;
- }
-
- q = null_or_empty_path(path);
- if (q > 0) {
- if (unlink(path) < 0)
- q = -errno;
- else {
- q = mark_symlink_for_removal(&remove_symlinks_to, path);
- add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
- }
- }
-
- if (q != -ENOENT && r == 0)
- r = q;
- }
-
-
-finish:
- q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
- if (r == 0)
- r = q;
-
- set_free_free(remove_symlinks_to);
- free(config_path);
-
- return r;
-}
-
-int unit_file_link(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- char **i;
- _cleanup_free_ char *config_path = NULL;
- int r, q;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- r = get_config_path(scope, runtime, root_dir, &config_path);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, files) {
- _cleanup_free_ char *path = NULL;
- char *fn;
- struct stat st;
-
- fn = basename(*i);
-
- if (!path_is_absolute(*i) ||
- !unit_name_is_valid(fn, TEMPLATE_VALID)) {
- if (r == 0)
- r = -EINVAL;
- continue;
- }
-
- if (lstat(*i, &st) < 0) {
- if (r == 0)
- r = -errno;
- continue;
- }
-
- if (!S_ISREG(st.st_mode)) {
- r = -ENOENT;
- continue;
- }
-
- q = in_search_path(*i, paths.unit_path);
- if (q < 0)
- return q;
-
- if (q > 0)
- continue;
-
- path = path_make_absolute(fn, config_path);
- if (!path)
- return -ENOMEM;
-
- if (symlink(*i, path) >= 0) {
- add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
- continue;
- }
-
- if (errno == EEXIST) {
- _cleanup_free_ char *dest = NULL;
-
- q = readlink_and_make_absolute(path, &dest);
- if (q < 0 && errno != ENOENT) {
- if (r == 0)
- r = q;
- continue;
- }
-
- if (q >= 0 && path_equal(dest, *i))
- continue;
-
- if (force) {
- if (symlink_atomic(*i, path) >= 0) {
- add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
- add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
- continue;
- }
- }
-
- if (r == 0)
- r = -EEXIST;
- } else {
- if (r == 0)
- r = -errno;
- }
- }
-
- return r;
-}
-
-void unit_file_list_free(Hashmap *h) {
- UnitFileList *i;
-
- while ((i = hashmap_steal_first(h))) {
- free(i->path);
- free(i);
- }
-
- hashmap_free(h);
-}
-
-void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes) {
- unsigned i;
-
- assert(changes || n_changes == 0);
-
- if (!changes)
- return;
-
- for (i = 0; i < n_changes; i++) {
- free(changes[i].path);
- free(changes[i].source);
- }
-
- free(changes);
-}
-
-static void install_info_free(InstallInfo *i) {
- assert(i);
-
- free(i->name);
- free(i->path);
- strv_free(i->aliases);
- strv_free(i->wanted_by);
- strv_free(i->required_by);
- strv_free(i->also);
- free(i->default_instance);
- free(i);
-}
-
-static void install_info_hashmap_free(OrderedHashmap *m) {
- InstallInfo *i;
-
- if (!m)
- return;
-
- while ((i = ordered_hashmap_steal_first(m)))
- install_info_free(i);
-
- ordered_hashmap_free(m);
-}
-
-static void install_context_done(InstallContext *c) {
- assert(c);
-
- install_info_hashmap_free(c->will_install);
- install_info_hashmap_free(c->have_installed);
-
- c->will_install = c->have_installed = NULL;
-}
-
-static int install_info_add(
- InstallContext *c,
- const char *name,
- const char *path) {
- InstallInfo *i = NULL;
- int r;
-
- assert(c);
- assert(name || path);
-
- if (!name)
- name = basename(path);
-
- if (!unit_name_is_valid(name, TEMPLATE_VALID))
- return -EINVAL;
-
- if (ordered_hashmap_get(c->have_installed, name) ||
- ordered_hashmap_get(c->will_install, name))
- return 0;
-
- r = ordered_hashmap_ensure_allocated(&c->will_install, &string_hash_ops);
- if (r < 0)
- return r;
-
- i = new0(InstallInfo, 1);
- if (!i)
- return -ENOMEM;
-
- i->name = strdup(name);
- if (!i->name) {
- r = -ENOMEM;
- goto fail;
- }
-
- if (path) {
- i->path = strdup(path);
- if (!i->path) {
- r = -ENOMEM;
- goto fail;
- }
- }
-
- r = ordered_hashmap_put(c->will_install, i->name, i);
- if (r < 0)
- goto fail;
-
- return 0;
-
-fail:
- if (i)
- install_info_free(i);
-
- return r;
-}
-
-static int install_info_add_auto(
- InstallContext *c,
- const char *name_or_path) {
-
- assert(c);
- assert(name_or_path);
-
- if (path_is_absolute(name_or_path))
- return install_info_add(c, NULL, name_or_path);
- else
- return install_info_add(c, name_or_path, NULL);
-}
-
-static int config_parse_also(
- const char *unit,
- const char *filename,
- unsigned line,
- const char *section,
- unsigned section_line,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- size_t l;
- const char *word, *state;
- InstallContext *c = data;
- InstallInfo *i = userdata;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
-
- FOREACH_WORD_QUOTED(word, l, rvalue, state) {
- _cleanup_free_ char *n;
- int r;
-
- n = strndup(word, l);
- if (!n)
- return -ENOMEM;
-
- r = install_info_add(c, n, NULL);
- if (r < 0)
- return r;
-
- r = strv_extend(&i->also, n);
- if (r < 0)
- return r;
- }
- if (!isempty(state))
- log_syntax(unit, LOG_ERR, filename, line, EINVAL,
- "Trailing garbage, ignoring.");
-
- return 0;
-}
-
-static int config_parse_user(
- const char *unit,
- const char *filename,
- unsigned line,
- const char *section,
- unsigned section_line,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- InstallInfo *i = data;
- char *printed;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
-
- r = install_full_printf(i, rvalue, &printed);
- if (r < 0)
- return r;
-
- free(i->user);
- i->user = printed;
-
- return 0;
-}
-
-static int config_parse_default_instance(
- const char *unit,
- const char *filename,
- unsigned line,
- const char *section,
- unsigned section_line,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- InstallInfo *i = data;
- char *printed;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
-
- r = install_full_printf(i, rvalue, &printed);
- if (r < 0)
- return r;
-
- if (!unit_instance_is_valid(printed)) {
- free(printed);
- return -EINVAL;
- }
-
- free(i->default_instance);
- i->default_instance = printed;
-
- return 0;
-}
-
-static int unit_file_load(
- InstallContext *c,
- InstallInfo *info,
- const char *path,
- const char *root_dir,
- bool allow_symlink,
- bool load,
- bool *also) {
-
- const ConfigTableItem items[] = {
- { "Install", "Alias", config_parse_strv, 0, &info->aliases },
- { "Install", "WantedBy", config_parse_strv, 0, &info->wanted_by },
- { "Install", "RequiredBy", config_parse_strv, 0, &info->required_by },
- { "Install", "DefaultInstance", config_parse_default_instance, 0, info },
- { "Install", "Also", config_parse_also, 0, c },
- { "Exec", "User", config_parse_user, 0, info },
- {}
- };
-
- _cleanup_fclose_ FILE *f = NULL;
- int fd, r;
-
- assert(c);
- assert(info);
- assert(path);
-
- if (!isempty(root_dir))
- path = strjoina(root_dir, "/", path);
-
- if (!load) {
- r = access(path, F_OK) ? -errno : 0;
- return r;
- }
-
- fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
- if (fd < 0)
- return -errno;
-
- f = fdopen(fd, "re");
- if (!f) {
- safe_close(fd);
- return -ENOMEM;
- }
-
- r = config_parse(NULL, path, f,
- NULL,
- config_item_table_lookup, items,
- true, true, false, info);
- if (r < 0)
- return r;
-
- if (also)
- *also = !strv_isempty(info->also);
-
- return
- (int) strv_length(info->aliases) +
- (int) strv_length(info->wanted_by) +
- (int) strv_length(info->required_by);
-}
-
-static int unit_file_search(
- InstallContext *c,
- InstallInfo *info,
- const LookupPaths *paths,
- const char *root_dir,
- bool allow_symlink,
- bool load,
- bool *also) {
-
- char **p;
- int r;
-
- assert(c);
- assert(info);
- assert(paths);
-
- if (info->path)
- return unit_file_load(c, info, info->path, root_dir, allow_symlink, load, also);
-
- assert(info->name);
-
- STRV_FOREACH(p, paths->unit_path) {
- _cleanup_free_ char *path = NULL;
-
- path = strjoin(*p, "/", info->name, NULL);
- if (!path)
- return -ENOMEM;
-
- r = unit_file_load(c, info, path, root_dir, allow_symlink, load, also);
- if (r >= 0) {
- info->path = path;
- path = NULL;
- return r;
- }
- if (r != -ENOENT && r != -ELOOP)
- return r;
- }
-
- if (unit_name_is_instance(info->name)) {
-
- /* Unit file doesn't exist, however instance
- * enablement was requested. We will check if it is
- * possible to load template unit file. */
-
- _cleanup_free_ char *template = NULL;
-
- template = unit_name_template(info->name);
- if (!template)
- return -ENOMEM;
-
- STRV_FOREACH(p, paths->unit_path) {
- _cleanup_free_ char *path = NULL;
-
- path = strjoin(*p, "/", template, NULL);
- if (!path)
- return -ENOMEM;
-
- r = unit_file_load(c, info, path, root_dir, allow_symlink, load, also);
- if (r >= 0) {
- info->path = path;
- path = NULL;
- return r;
- }
- if (r != -ENOENT && r != -ELOOP)
- return r;
- }
- }
-
- return -ENOENT;
-}
-
-static int unit_file_can_install(
- const LookupPaths *paths,
- const char *root_dir,
- const char *name,
- bool allow_symlink,
- bool *also) {
-
- _cleanup_(install_context_done) InstallContext c = {};
- InstallInfo *i;
- int r;
-
- assert(paths);
- assert(name);
-
- r = install_info_add_auto(&c, name);
- if (r < 0)
- return r;
-
- assert_se(i = ordered_hashmap_first(c.will_install));
-
- r = unit_file_search(&c, i, paths, root_dir, allow_symlink, true, also);
-
- if (r >= 0)
- r =
- (int) strv_length(i->aliases) +
- (int) strv_length(i->wanted_by) +
- (int) strv_length(i->required_by);
-
- return r;
-}
-
-static int create_symlink(
- const char *old_path,
- const char *new_path,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_free_ char *dest = NULL;
- int r;
-
- assert(old_path);
- assert(new_path);
-
- mkdir_parents_label(new_path, 0755);
-
- if (symlink(old_path, new_path) >= 0) {
- add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
- return 0;
- }
-
- if (errno != EEXIST)
- return -errno;
-
- r = readlink_and_make_absolute(new_path, &dest);
- if (r < 0)
- return r;
-
- if (path_equal(dest, old_path))
- return 0;
-
- if (!force)
- return -EEXIST;
-
- r = symlink_atomic(old_path, new_path);
- if (r < 0)
- return r;
-
- add_file_change(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
- add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
-
- return 0;
-}
-
-static int install_info_symlink_alias(
- InstallInfo *i,
- const char *config_path,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- char **s;
- int r = 0, q;
-
- assert(i);
- assert(config_path);
-
- STRV_FOREACH(s, i->aliases) {
- _cleanup_free_ char *alias_path = NULL, *dst = NULL;
-
- q = install_full_printf(i, *s, &dst);
- if (q < 0)
- return q;
-
- alias_path = path_make_absolute(dst, config_path);
- if (!alias_path)
- return -ENOMEM;
-
- q = create_symlink(i->path, alias_path, force, changes, n_changes);
- if (r == 0)
- r = q;
- }
-
- return r;
-}
-
-static int install_info_symlink_wants(
- InstallInfo *i,
- const char *config_path,
- char **list,
- const char *suffix,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_free_ char *buf = NULL;
- const char *n;
- char **s;
- int r = 0, q;
-
- assert(i);
- assert(config_path);
-
- if (unit_name_is_template(i->name)) {
-
- /* Don't install any symlink if there's no default
- * instance configured */
-
- if (!i->default_instance)
- return 0;
-
- buf = unit_name_replace_instance(i->name, i->default_instance);
- if (!buf)
- return -ENOMEM;
-
- n = buf;
- } else
- n = i->name;
-
- STRV_FOREACH(s, list) {
- _cleanup_free_ char *path = NULL, *dst = NULL;
-
- q = install_full_printf(i, *s, &dst);
- if (q < 0)
- return q;
-
- if (!unit_name_is_valid(dst, TEMPLATE_VALID)) {
- r = -EINVAL;
- continue;
- }
-
- path = strjoin(config_path, "/", dst, suffix, n, NULL);
- if (!path)
- return -ENOMEM;
-
- q = create_symlink(i->path, path, force, changes, n_changes);
- if (r == 0)
- r = q;
- }
-
- return r;
-}
-
-static int install_info_symlink_link(
- InstallInfo *i,
- const LookupPaths *paths,
- const char *config_path,
- const char *root_dir,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_free_ char *path = NULL;
- int r;
-
- assert(i);
- assert(paths);
- assert(config_path);
- assert(i->path);
-
- r = in_search_path(i->path, paths->unit_path);
- if (r != 0)
- return r;
-
- path = strjoin(config_path, "/", i->name, NULL);
- if (!path)
- return -ENOMEM;
-
- return create_symlink(i->path, path, force, changes, n_changes);
-}
-
-static int install_info_apply(
- InstallInfo *i,
- const LookupPaths *paths,
- const char *config_path,
- const char *root_dir,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- int r, q;
-
- assert(i);
- assert(paths);
- assert(config_path);
-
- r = install_info_symlink_alias(i, config_path, force, changes, n_changes);
-
- q = install_info_symlink_wants(i, config_path, i->wanted_by, ".wants/", force, changes, n_changes);
- if (r == 0)
- r = q;
-
- q = install_info_symlink_wants(i, config_path, i->required_by, ".requires/", force, changes, n_changes);
- if (r == 0)
- r = q;
-
- q = install_info_symlink_link(i, paths, config_path, root_dir, force, changes, n_changes);
- if (r == 0)
- r = q;
-
- return r;
-}
-
-static int install_context_apply(
- InstallContext *c,
- const LookupPaths *paths,
- const char *config_path,
- const char *root_dir,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- InstallInfo *i;
- int r, q;
-
- assert(c);
- assert(paths);
- assert(config_path);
-
- if (!ordered_hashmap_isempty(c->will_install)) {
- r = ordered_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = ordered_hashmap_reserve(c->have_installed, ordered_hashmap_size(c->will_install));
- if (r < 0)
- return r;
- }
-
- r = 0;
- while ((i = ordered_hashmap_first(c->will_install))) {
- assert_se(ordered_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
-
- q = unit_file_search(c, i, paths, root_dir, false, true, NULL);
- if (q < 0) {
- if (r >= 0)
- r = q;
-
- return r;
- } else if (r >= 0)
- r += q;
-
- q = install_info_apply(i, paths, config_path, root_dir, force, changes, n_changes);
- if (r >= 0 && q < 0)
- r = q;
- }
-
- return r;
-}
-
-static int install_context_mark_for_removal(
- InstallContext *c,
- const LookupPaths *paths,
- Set **remove_symlinks_to,
- const char *config_path,
- const char *root_dir) {
-
- InstallInfo *i;
- int r, q;
-
- assert(c);
- assert(paths);
- assert(config_path);
-
- /* Marks all items for removal */
-
- if (!ordered_hashmap_isempty(c->will_install)) {
- r = ordered_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = ordered_hashmap_reserve(c->have_installed, ordered_hashmap_size(c->will_install));
- if (r < 0)
- return r;
- }
-
- r = 0;
- while ((i = ordered_hashmap_first(c->will_install))) {
- assert_se(ordered_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
-
- q = unit_file_search(c, i, paths, root_dir, false, true, NULL);
- if (q == -ENOENT) {
- /* do nothing */
- } else if (q < 0) {
- if (r >= 0)
- r = q;
-
- return r;
- } else if (r >= 0)
- r += q;
-
- if (unit_name_is_instance(i->name)) {
- char *unit_file;
-
- if (i->path) {
- unit_file = basename(i->path);
-
- if (unit_name_is_instance(unit_file))
- /* unit file named as instance exists, thus all symlinks
- * pointing to it will be removed */
- q = mark_symlink_for_removal(remove_symlinks_to, i->name);
- else
- /* does not exist, thus we will mark for removal symlinks
- * to template unit file */
- q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
- } else {
- /* If i->path is not set, it means that we didn't actually find
- * the unit file. But we can still remove symlinks to the
- * nonexistent template. */
- unit_file = unit_name_template(i->name);
- if (!unit_file)
- return log_oom();
-
- q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
- free(unit_file);
- }
- } else
- q = mark_symlink_for_removal(remove_symlinks_to, i->name);
-
- if (r >= 0 && q < 0)
- r = q;
- }
-
- return r;
-}
-
-int unit_file_add_dependency(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- char *target,
- UnitDependency dep,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_(install_context_done) InstallContext c = {};
- _cleanup_free_ char *config_path = NULL;
- char **i;
- int r;
- InstallInfo *info;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- r = get_config_path(scope, runtime, root_dir, &config_path);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, files) {
- UnitFileState state;
-
- state = unit_file_get_state(scope, root_dir, *i);
- if (state < 0)
- return log_error_errno(state, "Failed to get unit file state for %s: %m", *i);
-
- if (state == UNIT_FILE_MASKED || state == UNIT_FILE_MASKED_RUNTIME) {
- log_error("Failed to enable unit: Unit %s is masked", *i);
- return -EOPNOTSUPP;
- }
-
- r = install_info_add_auto(&c, *i);
- if (r < 0)
- return r;
- }
-
- if (!ordered_hashmap_isempty(c.will_install)) {
- r = ordered_hashmap_ensure_allocated(&c.have_installed, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = ordered_hashmap_reserve(c.have_installed, ordered_hashmap_size(c.will_install));
- if (r < 0)
- return r;
- }
-
- while ((info = ordered_hashmap_first(c.will_install))) {
- assert_se(ordered_hashmap_move_one(c.have_installed, c.will_install, info->name) == 0);
-
- r = unit_file_search(&c, info, &paths, root_dir, false, false, NULL);
- if (r < 0)
- return r;
-
- if (dep == UNIT_WANTS)
- r = strv_extend(&info->wanted_by, target);
- else if (dep == UNIT_REQUIRES)
- r = strv_extend(&info->required_by, target);
- else
- r = -EINVAL;
-
- if (r < 0)
- return r;
-
- r = install_info_apply(info, &paths, config_path, root_dir, force, changes, n_changes);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-int unit_file_enable(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_(install_context_done) InstallContext c = {};
- char **i;
- _cleanup_free_ char *config_path = NULL;
- int r;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- r = get_config_path(scope, runtime, root_dir, &config_path);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, files) {
- UnitFileState state;
-
- /* We only want to know if this unit is masked, so we ignore
- * errors from unit_file_get_state, deferring other checks.
- * This allows templated units to be enabled on the fly. */
- state = unit_file_get_state(scope, root_dir, *i);
- if (state == UNIT_FILE_MASKED || state == UNIT_FILE_MASKED_RUNTIME) {
- log_error("Failed to enable unit: Unit %s is masked", *i);
- return -EOPNOTSUPP;
- }
-
- r = install_info_add_auto(&c, *i);
- if (r < 0)
- return r;
- }
-
- /* This will return the number of symlink rules that were
- supposed to be created, not the ones actually created. This is
- useful to determine whether the passed files had any
- installation data at all. */
-
- return install_context_apply(&c, &paths, config_path, root_dir, force, changes, n_changes);
-}
-
-int unit_file_disable(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_(install_context_done) InstallContext c = {};
- char **i;
- _cleanup_free_ char *config_path = NULL;
- _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
- int r, q;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- r = get_config_path(scope, runtime, root_dir, &config_path);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, files) {
- r = install_info_add_auto(&c, *i);
- if (r < 0)
- return r;
- }
-
- r = install_context_mark_for_removal(&c, &paths, &remove_symlinks_to, config_path, root_dir);
-
- q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
- if (r >= 0)
- r = q;
-
- return r;
-}
-
-int unit_file_reenable(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
- int r;
-
- r = unit_file_disable(scope, runtime, root_dir, files,
- changes, n_changes);
- if (r < 0)
- return r;
-
- return unit_file_enable(scope, runtime, root_dir, files, force,
- changes, n_changes);
-}
-
-int unit_file_set_default(
- UnitFileScope scope,
- const char *root_dir,
- const char *file,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_(install_context_done) InstallContext c = {};
- _cleanup_free_ char *config_path = NULL;
- char *path;
- int r;
- InstallInfo *i = NULL;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(file);
-
- if (unit_name_to_type(file) != UNIT_TARGET)
- return -EINVAL;
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- r = get_config_path(scope, false, root_dir, &config_path);
- if (r < 0)
- return r;
-
- r = install_info_add_auto(&c, file);
- if (r < 0)
- return r;
-
- assert_se(i = ordered_hashmap_first(c.will_install));
-
- r = unit_file_search(&c, i, &paths, root_dir, false, true, NULL);
- if (r < 0)
- return r;
-
- path = strjoina(config_path, "/" SPECIAL_DEFAULT_TARGET);
-
- r = create_symlink(i->path, path, force, changes, n_changes);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int unit_file_get_default(
- UnitFileScope scope,
- const char *root_dir,
- char **name) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- char **p;
- int r;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(name);
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- STRV_FOREACH(p, paths.unit_path) {
- _cleanup_free_ char *path = NULL, *tmp = NULL;
- char *n;
-
- path = path_join(root_dir, *p, SPECIAL_DEFAULT_TARGET);
- if (!path)
- return -ENOMEM;
-
- r = readlink_malloc(path, &tmp);
- if (r == -ENOENT)
- continue;
- else if (r == -EINVAL)
- /* not a symlink */
- n = strdup(SPECIAL_DEFAULT_TARGET);
- else if (r < 0)
- return r;
- else
- n = strdup(basename(tmp));
-
- if (!n)
- return -ENOMEM;
-
- *name = n;
- return 0;
- }
-
- return -ENOENT;
-}
-
-UnitFileState unit_file_lookup_state(
- UnitFileScope scope,
- const char *root_dir,
- const LookupPaths *paths,
- const char *name) {
-
- UnitFileState state = _UNIT_FILE_STATE_INVALID;
- char **i;
- _cleanup_free_ char *path = NULL;
- int r = 0;
-
- assert(paths);
-
- if (!unit_name_is_valid(name, TEMPLATE_VALID))
- return -EINVAL;
-
- STRV_FOREACH(i, paths->unit_path) {
- struct stat st;
- char *partial;
- bool also = false;
-
- free(path);
- path = path_join(root_dir, *i, name);
- if (!path)
- return -ENOMEM;
-
- if (root_dir)
- partial = path + strlen(root_dir);
- else
- partial = path;
-
- /*
- * Search for a unit file in our default paths, to
- * be sure, that there are no broken symlinks.
- */
- if (lstat(path, &st) < 0) {
- r = -errno;
- if (errno != ENOENT)
- return r;
-
- if (!unit_name_is_instance(name))
- continue;
- } else {
- if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
- return -ENOENT;
-
- r = null_or_empty_path(path);
- if (r < 0 && r != -ENOENT)
- return r;
- else if (r > 0) {
- state = path_startswith(*i, "/run") ?
- UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
- return state;
- }
- }
-
- r = find_symlinks_in_scope(scope, root_dir, name, &state);
- if (r < 0)
- return r;
- else if (r > 0)
- return state;
-
- r = unit_file_can_install(paths, root_dir, partial, true, &also);
- if (r < 0 && errno != ENOENT)
- return r;
- else if (r > 0)
- return UNIT_FILE_DISABLED;
- else if (r == 0) {
- if (also)
- return UNIT_FILE_INDIRECT;
- return UNIT_FILE_STATIC;
- }
- }
-
- return r < 0 ? r : state;
-}
-
-UnitFileState unit_file_get_state(
- UnitFileScope scope,
- const char *root_dir,
- const char *name) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- int r;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(name);
-
- if (root_dir && scope != UNIT_FILE_SYSTEM)
- return -EINVAL;
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- return unit_file_lookup_state(scope, root_dir, &paths, name);
-}
-
-int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
- _cleanup_strv_free_ char **files = NULL;
- char **p;
- int r;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(name);
-
- if (scope == UNIT_FILE_SYSTEM)
- r = conf_files_list(&files, ".preset", root_dir,
- "/etc/systemd/system-preset",
- "/usr/local/lib/systemd/system-preset",
- "/usr/lib/systemd/system-preset",
-#ifdef HAVE_SPLIT_USR
- "/lib/systemd/system-preset",
-#endif
- NULL);
- else if (scope == UNIT_FILE_GLOBAL)
- r = conf_files_list(&files, ".preset", root_dir,
- "/etc/systemd/user-preset",
- "/usr/local/lib/systemd/user-preset",
- "/usr/lib/systemd/user-preset",
- NULL);
- else
- return 1;
-
- if (r < 0)
- return r;
-
- STRV_FOREACH(p, files) {
- _cleanup_fclose_ FILE *f;
-
- f = fopen(*p, "re");
- if (!f) {
- if (errno == ENOENT)
- continue;
-
- return -errno;
- }
-
- for (;;) {
- char line[LINE_MAX], *l;
-
- if (!fgets(line, sizeof(line), f))
- break;
-
- l = strstrip(line);
- if (!*l)
- continue;
-
- if (strchr(COMMENTS "\n", *l))
- continue;
-
- if (first_word(l, "enable")) {
- l += 6;
- l += strspn(l, WHITESPACE);
-
- if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
- log_debug("Preset file says enable %s.", name);
- return 1;
- }
-
- } else if (first_word(l, "disable")) {
- l += 7;
- l += strspn(l, WHITESPACE);
-
- if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
- log_debug("Preset file says disable %s.", name);
- return 0;
- }
-
- } else
- log_debug("Couldn't parse line '%s'", l);
- }
- }
-
- /* Default is "enable" */
- log_debug("Preset file doesn't say anything about %s, enabling.", name);
- return 1;
-}
-
-int unit_file_preset(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- char **files,
- UnitFilePresetMode mode,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_free_ char *config_path = NULL;
- char **i;
- int r, q;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(mode < _UNIT_FILE_PRESET_MAX);
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- r = get_config_path(scope, runtime, root_dir, &config_path);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, files) {
-
- if (!unit_name_is_valid(*i, TEMPLATE_VALID))
- return -EINVAL;
-
- r = unit_file_query_preset(scope, root_dir, *i);
- if (r < 0)
- return r;
-
- if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
- r = install_info_add_auto(&plus, *i);
- else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
- r = install_info_add_auto(&minus, *i);
- else
- r = 0;
- if (r < 0)
- return r;
- }
-
- r = 0;
-
- if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
- _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
-
- r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
-
- q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
- if (r == 0)
- r = q;
- }
-
- if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
- /* Returns number of symlinks that where supposed to be installed. */
- q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
- if (r == 0)
- r = q;
- }
-
- return r;
-}
-
-int unit_file_preset_all(
- UnitFileScope scope,
- bool runtime,
- const char *root_dir,
- UnitFilePresetMode mode,
- bool force,
- UnitFileChange **changes,
- unsigned *n_changes) {
-
- _cleanup_(install_context_done) InstallContext plus = {}, minus = {};
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- _cleanup_free_ char *config_path = NULL;
- char **i;
- int r, q;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(mode < _UNIT_FILE_PRESET_MAX);
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- r = get_config_path(scope, runtime, root_dir, &config_path);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, paths.unit_path) {
- _cleanup_closedir_ DIR *d = NULL;
- _cleanup_free_ char *units_dir;
-
- units_dir = path_join(root_dir, *i, NULL);
- if (!units_dir)
- return -ENOMEM;
-
- d = opendir(units_dir);
- if (!d) {
- if (errno == ENOENT)
- continue;
-
- return -errno;
- }
-
- for (;;) {
- struct dirent *de;
-
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
-
- if (!de)
- break;
-
- if (hidden_file(de->d_name))
- continue;
-
- if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
- continue;
-
- dirent_ensure_type(d, de);
-
- if (de->d_type != DT_REG)
- continue;
-
- r = unit_file_query_preset(scope, root_dir, de->d_name);
- if (r < 0)
- return r;
-
- if (r && mode != UNIT_FILE_PRESET_DISABLE_ONLY)
- r = install_info_add_auto(&plus, de->d_name);
- else if (!r && mode != UNIT_FILE_PRESET_ENABLE_ONLY)
- r = install_info_add_auto(&minus, de->d_name);
- else
- r = 0;
- if (r < 0)
- return r;
- }
- }
-
- r = 0;
-
- if (mode != UNIT_FILE_PRESET_ENABLE_ONLY) {
- _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
-
- r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
-
- q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, NULL);
- if (r == 0)
- r = q;
- }
-
- if (mode != UNIT_FILE_PRESET_DISABLE_ONLY) {
- q = install_context_apply(&plus, &paths, config_path, root_dir, force, changes, n_changes);
- if (r == 0)
- r = q;
- }
-
- return r;
-}
-
-static void unit_file_list_free_one(UnitFileList *f) {
- if (!f)
- return;
-
- free(f->path);
- free(f);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(UnitFileList*, unit_file_list_free_one);
-
-int unit_file_get_list(
- UnitFileScope scope,
- const char *root_dir,
- Hashmap *h) {
-
- _cleanup_lookup_paths_free_ LookupPaths paths = {};
- char **i;
- int r;
-
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
- assert(h);
-
- if (root_dir && scope != UNIT_FILE_SYSTEM)
- return -EINVAL;
-
- if (root_dir) {
- r = access(root_dir, F_OK);
- if (r < 0)
- return -errno;
- }
-
- r = lookup_paths_init_from_scope(&paths, scope, root_dir);
- if (r < 0)
- return r;
-
- STRV_FOREACH(i, paths.unit_path) {
- _cleanup_closedir_ DIR *d = NULL;
- _cleanup_free_ char *units_dir;
-
- units_dir = path_join(root_dir, *i, NULL);
- if (!units_dir)
- return -ENOMEM;
-
- d = opendir(units_dir);
- if (!d) {
- if (errno == ENOENT)
- continue;
-
- return -errno;
- }
-
- for (;;) {
- _cleanup_(unit_file_list_free_onep) UnitFileList *f = NULL;
- struct dirent *de;
- _cleanup_free_ char *path = NULL;
-
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
-
- if (!de)
- break;
-
- if (hidden_file(de->d_name))
- continue;
-
- if (!unit_name_is_valid(de->d_name, TEMPLATE_VALID))
- continue;
-
- if (hashmap_get(h, de->d_name))
- continue;
-
- dirent_ensure_type(d, de);
-
- if (!IN_SET(de->d_type, DT_LNK, DT_REG))
- continue;
-
- f = new0(UnitFileList, 1);
- if (!f)
- return -ENOMEM;
-
- f->path = path_make_absolute(de->d_name, units_dir);
- if (!f->path)
- return -ENOMEM;
-
- r = null_or_empty_path(f->path);
- if (r < 0 && r != -ENOENT)
- return r;
- else if (r > 0) {
- f->state =
- path_startswith(*i, "/run") ?
- UNIT_FILE_MASKED_RUNTIME : UNIT_FILE_MASKED;
- goto found;
- }
-
- r = find_symlinks_in_scope(scope, root_dir, de->d_name, &f->state);
- if (r < 0)
- return r;
- else if (r > 0) {
- f->state = UNIT_FILE_ENABLED;
- goto found;
- }
-
- path = path_make_absolute(de->d_name, *i);
- if (!path)
- return -ENOMEM;
-
- r = unit_file_can_install(&paths, root_dir, path, true, NULL);
- if (r == -EINVAL || /* Invalid setting? */
- r == -EBADMSG || /* Invalid format? */
- r == -ENOENT /* Included file not found? */)
- f->state = UNIT_FILE_INVALID;
- else if (r < 0)
- return r;
- else if (r > 0)
- f->state = UNIT_FILE_DISABLED;
- else
- f->state = UNIT_FILE_STATIC;
-
- found:
- r = hashmap_put(h, basename(f->path), f);
- if (r < 0)
- return r;
- f = NULL; /* prevent cleanup */
- }
- }
-
- return r;
-}
-
-static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
- [UNIT_FILE_ENABLED] = "enabled",
- [UNIT_FILE_ENABLED_RUNTIME] = "enabled-runtime",
- [UNIT_FILE_LINKED] = "linked",
- [UNIT_FILE_LINKED_RUNTIME] = "linked-runtime",
- [UNIT_FILE_MASKED] = "masked",
- [UNIT_FILE_MASKED_RUNTIME] = "masked-runtime",
- [UNIT_FILE_STATIC] = "static",
- [UNIT_FILE_DISABLED] = "disabled",
- [UNIT_FILE_INDIRECT] = "indirect",
- [UNIT_FILE_INVALID] = "invalid",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(unit_file_state, UnitFileState);
-
-static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX] = {
- [UNIT_FILE_SYMLINK] = "symlink",
- [UNIT_FILE_UNLINK] = "unlink",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
-
-static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MAX] = {
- [UNIT_FILE_PRESET_FULL] = "full",
- [UNIT_FILE_PRESET_ENABLE_ONLY] = "enable-only",
- [UNIT_FILE_PRESET_DISABLE_ONLY] = "disable-only",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(unit_file_preset_mode, UnitFilePresetMode);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "hashmap.h"
-#include "unit-name.h"
-#include "path-lookup.h"
-
-typedef enum UnitFileScope {
- UNIT_FILE_SYSTEM,
- UNIT_FILE_GLOBAL,
- UNIT_FILE_USER,
- _UNIT_FILE_SCOPE_MAX,
- _UNIT_FILE_SCOPE_INVALID = -1
-} UnitFileScope;
-
-typedef enum UnitFileState {
- UNIT_FILE_ENABLED,
- UNIT_FILE_ENABLED_RUNTIME,
- UNIT_FILE_LINKED,
- UNIT_FILE_LINKED_RUNTIME,
- UNIT_FILE_MASKED,
- UNIT_FILE_MASKED_RUNTIME,
- UNIT_FILE_STATIC,
- UNIT_FILE_DISABLED,
- UNIT_FILE_INDIRECT,
- UNIT_FILE_INVALID,
- _UNIT_FILE_STATE_MAX,
- _UNIT_FILE_STATE_INVALID = -1
-} UnitFileState;
-
-typedef enum UnitFilePresetMode {
- UNIT_FILE_PRESET_FULL,
- UNIT_FILE_PRESET_ENABLE_ONLY,
- UNIT_FILE_PRESET_DISABLE_ONLY,
- _UNIT_FILE_PRESET_MAX,
- _UNIT_FILE_PRESET_INVALID = -1
-} UnitFilePresetMode;
-
-typedef enum UnitFileChangeType {
- UNIT_FILE_SYMLINK,
- UNIT_FILE_UNLINK,
- _UNIT_FILE_CHANGE_TYPE_MAX,
- _UNIT_FILE_CHANGE_TYPE_INVALID = -1
-} UnitFileChangeType;
-
-typedef struct UnitFileChange {
- UnitFileChangeType type;
- char *path;
- char *source;
-} UnitFileChange;
-
-typedef struct UnitFileList {
- char *path;
- UnitFileState state;
-} UnitFileList;
-
-typedef struct {
- char *name;
- char *path;
- char *user;
-
- char **aliases;
- char **wanted_by;
- char **required_by;
- char **also;
-
- char *default_instance;
-} InstallInfo;
-
-int unit_file_enable(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_disable(UnitFileScope scope, bool runtime, const char *root_dir, char **files, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_reenable(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_link(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_preset(UnitFileScope scope, bool runtime, const char *root_dir, char **files, UnitFilePresetMode mode, bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_preset_all(UnitFileScope scope, bool runtime, const char *root_dir, UnitFilePresetMode mode, bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_mask(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_unmask(UnitFileScope scope, bool runtime, const char *root_dir, char **files, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_set_default(UnitFileScope scope, const char *root_dir, const char *file, bool force, UnitFileChange **changes, unsigned *n_changes);
-int unit_file_get_default(UnitFileScope scope, const char *root_dir, char **name);
-int unit_file_add_dependency(UnitFileScope scope, bool runtime, const char *root_dir, char **files, char *target, UnitDependency dep, bool force, UnitFileChange **changes, unsigned *n_changes);
-
-UnitFileState unit_file_lookup_state(
- UnitFileScope scope,
- const char *root_dir,
- const LookupPaths *paths,
- const char *name);
-UnitFileState unit_file_get_state(
- UnitFileScope scope,
- const char *root_dir,
- const char *filename);
-
-int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h);
-
-void unit_file_list_free(Hashmap *h);
-void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes);
-
-int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name);
-
-const char *unit_file_state_to_string(UnitFileState s) _const_;
-UnitFileState unit_file_state_from_string(const char *s) _pure_;
-
-const char *unit_file_change_type_to_string(UnitFileChangeType s) _const_;
-UnitFileChangeType unit_file_change_type_from_string(const char *s) _pure_;
-
-const char *unit_file_preset_mode_to_string(UnitFilePresetMode m) _const_;
-UnitFilePresetMode unit_file_preset_mode_from_string(const char *s) _pure_;
+++ /dev/null
-#ifndef IOPRIO_H
-#define IOPRIO_H
-
-/* This is minimal version of Linux' linux/ioprio.h header file, which
- * is licensed GPL2 */
-
-#include <unistd.h>
-#include <sys/syscall.h>
-
-/*
- * Gives us 8 prio classes with 13-bits of data for each class
- */
-#define IOPRIO_BITS (16)
-#define IOPRIO_CLASS_SHIFT (13)
-#define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
-
-#define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
-#define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
-#define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
-
-#define ioprio_valid(mask) (IOPRIO_PRIO_CLASS((mask)) != IOPRIO_CLASS_NONE)
-
-/*
- * These are the io priority groups as implemented by CFQ. RT is the realtime
- * class, it always gets premium service. BE is the best-effort scheduling
- * class, the default for any process. IDLE is the idle scheduling class, it
- * is only served when no one else is using the disk.
- */
-enum {
- IOPRIO_CLASS_NONE,
- IOPRIO_CLASS_RT,
- IOPRIO_CLASS_BE,
- IOPRIO_CLASS_IDLE,
-};
-
-/*
- * 8 best effort priority levels are supported
- */
-#define IOPRIO_BE_NR (8)
-
-enum {
- IOPRIO_WHO_PROCESS = 1,
- IOPRIO_WHO_PGRP,
- IOPRIO_WHO_USER,
-};
-
-static inline int ioprio_set(int which, int who, int ioprio) {
- return syscall(__NR_ioprio_set, which, who, ioprio);
-}
-
-static inline int ioprio_get(int which, int who) {
- return syscall(__NR_ioprio_get, which, who);
-}
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-#include <math.h>
-
-#include "macro.h"
-#include "util.h"
-#include "utf8.h"
-#include "json.h"
-
-enum {
- STATE_NULL,
- STATE_VALUE,
- STATE_VALUE_POST,
-};
-
-static void inc_lines(unsigned *line, const char *s, size_t n) {
- const char *p = s;
-
- if (!line)
- return;
-
- for (;;) {
- const char *f;
-
- f = memchr(p, '\n', n);
- if (!f)
- return;
-
- n -= (f - p) + 1;
- p = f + 1;
- (*line)++;
- }
-}
-
-static int unhex_ucs2(const char *c, uint16_t *ret) {
- int aa, bb, cc, dd;
- uint16_t x;
-
- assert(c);
- assert(ret);
-
- aa = unhexchar(c[0]);
- if (aa < 0)
- return -EINVAL;
-
- bb = unhexchar(c[1]);
- if (bb < 0)
- return -EINVAL;
-
- cc = unhexchar(c[2]);
- if (cc < 0)
- return -EINVAL;
-
- dd = unhexchar(c[3]);
- if (dd < 0)
- return -EINVAL;
-
- x = ((uint16_t) aa << 12) |
- ((uint16_t) bb << 8) |
- ((uint16_t) cc << 4) |
- ((uint16_t) dd);
-
- if (x <= 0)
- return -EINVAL;
-
- *ret = x;
-
- return 0;
-}
-
-static int json_parse_string(const char **p, char **ret) {
- _cleanup_free_ char *s = NULL;
- size_t n = 0, allocated = 0;
- const char *c;
-
- assert(p);
- assert(*p);
- assert(ret);
-
- c = *p;
-
- if (*c != '"')
- return -EINVAL;
-
- c++;
-
- for (;;) {
- int len;
-
- /* Check for EOF */
- if (*c == 0)
- return -EINVAL;
-
- /* Check for control characters 0x00..0x1f */
- if (*c > 0 && *c < ' ')
- return -EINVAL;
-
- /* Check for control character 0x7f */
- if (*c == 0x7f)
- return -EINVAL;
-
- if (*c == '"') {
- if (!s) {
- s = strdup("");
- if (!s)
- return -ENOMEM;
- } else
- s[n] = 0;
-
- *p = c + 1;
-
- *ret = s;
- s = NULL;
- return JSON_STRING;
- }
-
- if (*c == '\\') {
- char ch = 0;
- c++;
-
- if (*c == 0)
- return -EINVAL;
-
- if (IN_SET(*c, '"', '\\', '/'))
- ch = *c;
- else if (*c == 'b')
- ch = '\b';
- else if (*c == 'f')
- ch = '\f';
- else if (*c == 'n')
- ch = '\n';
- else if (*c == 'r')
- ch = '\r';
- else if (*c == 't')
- ch = '\t';
- else if (*c == 'u') {
- uint16_t x;
- int r;
-
- r = unhex_ucs2(c + 1, &x);
- if (r < 0)
- return r;
-
- c += 5;
-
- if (!GREEDY_REALLOC(s, allocated, n + 4))
- return -ENOMEM;
-
- if (!utf16_is_surrogate(x))
- n += utf8_encode_unichar(s + n, x);
- else if (utf16_is_trailing_surrogate(x))
- return -EINVAL;
- else {
- uint16_t y;
-
- if (c[0] != '\\' || c[1] != 'u')
- return -EINVAL;
-
- r = unhex_ucs2(c + 2, &y);
- if (r < 0)
- return r;
-
- c += 6;
-
- if (!utf16_is_trailing_surrogate(y))
- return -EINVAL;
-
- n += utf8_encode_unichar(s + n, utf16_surrogate_pair_to_unichar(x, y));
- }
-
- continue;
- } else
- return -EINVAL;
-
- if (!GREEDY_REALLOC(s, allocated, n + 2))
- return -ENOMEM;
-
- s[n++] = ch;
- c ++;
- continue;
- }
-
- len = utf8_encoded_valid_unichar(c);
- if (len < 0)
- return len;
-
- if (!GREEDY_REALLOC(s, allocated, n + len + 1))
- return -ENOMEM;
-
- memcpy(s + n, c, len);
- n += len;
- c += len;
- }
-}
-
-static int json_parse_number(const char **p, union json_value *ret) {
- bool negative = false, exponent_negative = false, is_double = false;
- double x = 0.0, y = 0.0, exponent = 0.0, shift = 1.0;
- intmax_t i = 0;
- const char *c;
-
- assert(p);
- assert(*p);
- assert(ret);
-
- c = *p;
-
- if (*c == '-') {
- negative = true;
- c++;
- }
-
- if (*c == '0')
- c++;
- else {
- if (!strchr("123456789", *c) || *c == 0)
- return -EINVAL;
-
- do {
- if (!is_double) {
- int64_t t;
-
- t = 10 * i + (*c - '0');
- if (t < i) /* overflow */
- is_double = false;
- else
- i = t;
- }
-
- x = 10.0 * x + (*c - '0');
- c++;
- } while (strchr("0123456789", *c) && *c != 0);
- }
-
- if (*c == '.') {
- is_double = true;
- c++;
-
- if (!strchr("0123456789", *c) || *c == 0)
- return -EINVAL;
-
- do {
- y = 10.0 * y + (*c - '0');
- shift = 10.0 * shift;
- c++;
- } while (strchr("0123456789", *c) && *c != 0);
- }
-
- if (*c == 'e' || *c == 'E') {
- is_double = true;
- c++;
-
- if (*c == '-') {
- exponent_negative = true;
- c++;
- } else if (*c == '+')
- c++;
-
- if (!strchr("0123456789", *c) || *c == 0)
- return -EINVAL;
-
- do {
- exponent = 10.0 * exponent + (*c - '0');
- c++;
- } while (strchr("0123456789", *c) && *c != 0);
- }
-
- if (*c != 0)
- return -EINVAL;
-
- *p = c;
-
- if (is_double) {
- ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * exp10((exponent_negative ? -1.0 : 1.0) * exponent);
- return JSON_REAL;
- } else {
- ret->integer = negative ? -i : i;
- return JSON_INTEGER;
- }
-}
-
-int json_tokenize(
- const char **p,
- char **ret_string,
- union json_value *ret_value,
- void **state,
- unsigned *line) {
-
- const char *c;
- int t;
- int r;
-
- assert(p);
- assert(*p);
- assert(ret_string);
- assert(ret_value);
- assert(state);
-
- t = PTR_TO_INT(*state);
- c = *p;
-
- if (t == STATE_NULL) {
- if (line)
- *line = 1;
- t = STATE_VALUE;
- }
-
- for (;;) {
- const char *b;
-
- b = c + strspn(c, WHITESPACE);
- if (*b == 0)
- return JSON_END;
-
- inc_lines(line, c, b - c);
- c = b;
-
- switch (t) {
-
- case STATE_VALUE:
-
- if (*c == '{') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE);
- return JSON_OBJECT_OPEN;
-
- } else if (*c == '}') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return JSON_OBJECT_CLOSE;
-
- } else if (*c == '[') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE);
- return JSON_ARRAY_OPEN;
-
- } else if (*c == ']') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return JSON_ARRAY_CLOSE;
-
- } else if (*c == '"') {
- r = json_parse_string(&c, ret_string);
- if (r < 0)
- return r;
-
- *ret_value = JSON_VALUE_NULL;
- *p = c;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return r;
-
- } else if (strchr("-0123456789", *c)) {
- r = json_parse_number(&c, ret_value);
- if (r < 0)
- return r;
-
- *ret_string = NULL;
- *p = c;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return r;
-
- } else if (startswith(c, "true")) {
- *ret_string = NULL;
- ret_value->boolean = true;
- *p = c + 4;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return JSON_BOOLEAN;
-
- } else if (startswith(c, "false")) {
- *ret_string = NULL;
- ret_value->boolean = false;
- *p = c + 5;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return JSON_BOOLEAN;
-
- } else if (startswith(c, "null")) {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 4;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return JSON_NULL;
-
- } else
- return -EINVAL;
-
- case STATE_VALUE_POST:
-
- if (*c == ':') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE);
- return JSON_COLON;
- } else if (*c == ',') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE);
- return JSON_COMMA;
- } else if (*c == '}') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return JSON_OBJECT_CLOSE;
- } else if (*c == ']') {
- *ret_string = NULL;
- *ret_value = JSON_VALUE_NULL;
- *p = c + 1;
- *state = INT_TO_PTR(STATE_VALUE_POST);
- return JSON_ARRAY_CLOSE;
- } else
- return -EINVAL;
- }
-
- }
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-enum {
- JSON_END,
- JSON_COLON,
- JSON_COMMA,
- JSON_OBJECT_OPEN,
- JSON_OBJECT_CLOSE,
- JSON_ARRAY_OPEN,
- JSON_ARRAY_CLOSE,
- JSON_STRING,
- JSON_REAL,
- JSON_INTEGER,
- JSON_BOOLEAN,
- JSON_NULL,
-};
-
-union json_value {
- bool boolean;
- double real;
- intmax_t integer;
-};
-
-#define JSON_VALUE_NULL ((union json_value) {})
-
-int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);
+++ /dev/null
-../Makefile
\ No newline at end of file
+++ /dev/null
-/*
- * Copyright 2008 Red Hat, Inc. All rights reserved.
- * Copyright 2008 Ian Kent <raven@themaw.net>
- *
- * This file is part of the Linux kernel and is made available under
- * the terms of the GNU General Public License, version 2, or at your
- * option, any later version, incorporated herein by reference.
- */
-
-#ifndef _LINUX_AUTO_DEV_IOCTL_H
-#define _LINUX_AUTO_DEV_IOCTL_H
-
-#include <linux/auto_fs.h>
-
-#ifdef __KERNEL__
-#include <linux/string.h>
-#else
-#include <string.h>
-#endif /* __KERNEL__ */
-
-#define AUTOFS_DEVICE_NAME "autofs"
-
-#define AUTOFS_DEV_IOCTL_VERSION_MAJOR 1
-#define AUTOFS_DEV_IOCTL_VERSION_MINOR 0
-
-#define AUTOFS_DEVID_LEN 16
-
-#define AUTOFS_DEV_IOCTL_SIZE sizeof(struct autofs_dev_ioctl)
-
-/*
- * An ioctl interface for autofs mount point control.
- */
-
-struct args_protover {
- __u32 version;
-};
-
-struct args_protosubver {
- __u32 sub_version;
-};
-
-struct args_openmount {
- __u32 devid;
-};
-
-struct args_ready {
- __u32 token;
-};
-
-struct args_fail {
- __u32 token;
- __s32 status;
-};
-
-struct args_setpipefd {
- __s32 pipefd;
-};
-
-struct args_timeout {
- __u64 timeout;
-};
-
-struct args_requester {
- __u32 uid;
- __u32 gid;
-};
-
-struct args_expire {
- __u32 how;
-};
-
-struct args_askumount {
- __u32 may_umount;
-};
-
-struct args_ismountpoint {
- union {
- struct args_in {
- __u32 type;
- } in;
- struct args_out {
- __u32 devid;
- __u32 magic;
- } out;
- };
-};
-
-/*
- * All the ioctls use this structure.
- * When sending a path size must account for the total length
- * of the chunk of memory otherwise is is the size of the
- * structure.
- */
-
-struct autofs_dev_ioctl {
- __u32 ver_major;
- __u32 ver_minor;
- __u32 size; /* total size of data passed in
- * including this struct */
- __s32 ioctlfd; /* automount command fd */
-
- /* Command parameters */
-
- union {
- struct args_protover protover;
- struct args_protosubver protosubver;
- struct args_openmount openmount;
- struct args_ready ready;
- struct args_fail fail;
- struct args_setpipefd setpipefd;
- struct args_timeout timeout;
- struct args_requester requester;
- struct args_expire expire;
- struct args_askumount askumount;
- struct args_ismountpoint ismountpoint;
- };
-
- char path[0];
-};
-
-static inline void init_autofs_dev_ioctl(struct autofs_dev_ioctl *in) {
- memset(in, 0, sizeof(struct autofs_dev_ioctl));
- in->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
- in->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
- in->size = sizeof(struct autofs_dev_ioctl);
- in->ioctlfd = -1;
- return;
-}
-
-/*
- * If you change this make sure you make the corresponding change
- * to autofs-dev-ioctl.c:lookup_ioctl()
- */
-enum {
- /* Get various version info */
- AUTOFS_DEV_IOCTL_VERSION_CMD = 0x71,
- AUTOFS_DEV_IOCTL_PROTOVER_CMD,
- AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD,
-
- /* Open mount ioctl fd */
- AUTOFS_DEV_IOCTL_OPENMOUNT_CMD,
-
- /* Close mount ioctl fd */
- AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD,
-
- /* Mount/expire status returns */
- AUTOFS_DEV_IOCTL_READY_CMD,
- AUTOFS_DEV_IOCTL_FAIL_CMD,
-
- /* Activate/deactivate autofs mount */
- AUTOFS_DEV_IOCTL_SETPIPEFD_CMD,
- AUTOFS_DEV_IOCTL_CATATONIC_CMD,
-
- /* Expiry timeout */
- AUTOFS_DEV_IOCTL_TIMEOUT_CMD,
-
- /* Get mount last requesting uid and gid */
- AUTOFS_DEV_IOCTL_REQUESTER_CMD,
-
- /* Check for eligible expire candidates */
- AUTOFS_DEV_IOCTL_EXPIRE_CMD,
-
- /* Request busy status */
- AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD,
-
- /* Check if path is a mountpoint */
- AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD,
-};
-
-#define AUTOFS_IOCTL 0x93
-
-#define AUTOFS_DEV_IOCTL_VERSION \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_VERSION_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_PROTOVER \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_PROTOVER_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_PROTOSUBVER \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_OPENMOUNT \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_OPENMOUNT_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_CLOSEMOUNT \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_READY \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_READY_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_FAIL \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_FAIL_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_SETPIPEFD \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_SETPIPEFD_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_CATATONIC \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_CATATONIC_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_TIMEOUT \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_TIMEOUT_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_REQUESTER \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_REQUESTER_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_EXPIRE \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_EXPIRE_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_ASKUMOUNT \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD, struct autofs_dev_ioctl)
-
-#define AUTOFS_DEV_IOCTL_ISMOUNTPOINT \
- _IOWR(AUTOFS_IOCTL, \
- AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD, struct autofs_dev_ioctl)
-
-#endif /* _LINUX_AUTO_DEV_IOCTL_H */
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/mman.h>
-
-#include "set.h"
-#include "util.h"
-#include "utf8.h"
-#include "strv.h"
-
-#include "locale-util.h"
-
-static int add_locales_from_archive(Set *locales) {
- /* Stolen from glibc... */
-
- struct locarhead {
- uint32_t magic;
- /* Serial number. */
- uint32_t serial;
- /* Name hash table. */
- uint32_t namehash_offset;
- uint32_t namehash_used;
- uint32_t namehash_size;
- /* String table. */
- uint32_t string_offset;
- uint32_t string_used;
- uint32_t string_size;
- /* Table with locale records. */
- uint32_t locrectab_offset;
- uint32_t locrectab_used;
- uint32_t locrectab_size;
- /* MD5 sum hash table. */
- uint32_t sumhash_offset;
- uint32_t sumhash_used;
- uint32_t sumhash_size;
- };
-
- struct namehashent {
- /* Hash value of the name. */
- uint32_t hashval;
- /* Offset of the name in the string table. */
- uint32_t name_offset;
- /* Offset of the locale record. */
- uint32_t locrec_offset;
- };
-
- const struct locarhead *h;
- const struct namehashent *e;
- const void *p = MAP_FAILED;
- _cleanup_close_ int fd = -1;
- size_t sz = 0;
- struct stat st;
- unsigned i;
- int r;
-
- fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return errno == ENOENT ? 0 : -errno;
-
- if (fstat(fd, &st) < 0)
- return -errno;
-
- if (!S_ISREG(st.st_mode))
- return -EBADMSG;
-
- if (st.st_size < (off_t) sizeof(struct locarhead))
- return -EBADMSG;
-
- p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (p == MAP_FAILED)
- return -errno;
-
- h = (const struct locarhead *) p;
- if (h->magic != 0xde020109 ||
- h->namehash_offset + h->namehash_size > st.st_size ||
- h->string_offset + h->string_size > st.st_size ||
- h->locrectab_offset + h->locrectab_size > st.st_size ||
- h->sumhash_offset + h->sumhash_size > st.st_size) {
- r = -EBADMSG;
- goto finish;
- }
-
- e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
- for (i = 0; i < h->namehash_size; i++) {
- char *z;
-
- if (e[i].locrec_offset == 0)
- continue;
-
- if (!utf8_is_valid((char*) p + e[i].name_offset))
- continue;
-
- z = strdup((char*) p + e[i].name_offset);
- if (!z) {
- r = -ENOMEM;
- goto finish;
- }
-
- r = set_consume(locales, z);
- if (r < 0)
- goto finish;
- }
-
- r = 0;
-
- finish:
- if (p != MAP_FAILED)
- munmap((void*) p, sz);
-
- return r;
-}
-
-static int add_locales_from_libdir (Set *locales) {
- _cleanup_closedir_ DIR *dir = NULL;
- struct dirent *entry;
- int r;
-
- dir = opendir("/usr/lib/locale");
- if (!dir)
- return errno == ENOENT ? 0 : -errno;
-
- FOREACH_DIRENT(entry, dir, return -errno) {
- char *z;
-
- if (entry->d_type != DT_DIR)
- continue;
-
- z = strdup(entry->d_name);
- if (!z)
- return -ENOMEM;
-
- r = set_consume(locales, z);
- if (r < 0 && r != -EEXIST)
- return r;
- }
-
- return 0;
-}
-
-int get_locales(char ***ret) {
- _cleanup_set_free_ Set *locales = NULL;
- _cleanup_strv_free_ char **l = NULL;
- int r;
-
- locales = set_new(&string_hash_ops);
- if (!locales)
- return -ENOMEM;
-
- r = add_locales_from_archive(locales);
- if (r < 0 && r != -ENOENT)
- return r;
-
- r = add_locales_from_libdir(locales);
- if (r < 0)
- return r;
-
- l = set_get_strv(locales);
- if (!l)
- return -ENOMEM;
-
- strv_sort(l);
-
- *ret = l;
- l = NULL;
-
- return 0;
-}
-
-bool locale_is_valid(const char *name) {
-
- if (isempty(name))
- return false;
-
- if (strlen(name) >= 128)
- return false;
-
- if (!utf8_is_valid(name))
- return false;
-
- if (!filename_is_valid(name))
- return false;
-
- if (!string_is_safe(name))
- return false;
-
- return true;
-}
-
-static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
- [VARIABLE_LANG] = "LANG",
- [VARIABLE_LANGUAGE] = "LANGUAGE",
- [VARIABLE_LC_CTYPE] = "LC_CTYPE",
- [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
- [VARIABLE_LC_TIME] = "LC_TIME",
- [VARIABLE_LC_COLLATE] = "LC_COLLATE",
- [VARIABLE_LC_MONETARY] = "LC_MONETARY",
- [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
- [VARIABLE_LC_PAPER] = "LC_PAPER",
- [VARIABLE_LC_NAME] = "LC_NAME",
- [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
- [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
- [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
- [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-#include "macro.h"
-
-typedef enum LocaleVariable {
- /* We don't list LC_ALL here on purpose. People should be
- * using LANG instead. */
-
- VARIABLE_LANG,
- VARIABLE_LANGUAGE,
- VARIABLE_LC_CTYPE,
- VARIABLE_LC_NUMERIC,
- VARIABLE_LC_TIME,
- VARIABLE_LC_COLLATE,
- VARIABLE_LC_MONETARY,
- VARIABLE_LC_MESSAGES,
- VARIABLE_LC_PAPER,
- VARIABLE_LC_NAME,
- VARIABLE_LC_ADDRESS,
- VARIABLE_LC_TELEPHONE,
- VARIABLE_LC_MEASUREMENT,
- VARIABLE_LC_IDENTIFICATION,
- _VARIABLE_LC_MAX,
- _VARIABLE_LC_INVALID = -1
-} LocaleVariable;
-
-int get_locales(char ***l);
-bool locale_is_valid(const char *name);
-
-const char* locale_variable_to_string(LocaleVariable i) _const_;
-LocaleVariable locale_variable_from_string(const char *s) _pure_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <time.h>
-#include <errno.h>
-#include <sys/socket.h>
-#include <string.h>
-#include <fcntl.h>
-
-#include "logs-show.h"
-#include "log.h"
-#include "util.h"
-#include "utf8.h"
-#include "hashmap.h"
-#include "journal-internal.h"
-
-/* up to three lines (each up to 100 characters),
- or 300 characters, whichever is less */
-#define PRINT_LINE_THRESHOLD 3
-#define PRINT_CHAR_THRESHOLD 300
-
-#define JSON_THRESHOLD 4096
-
-static int print_catalog(FILE *f, sd_journal *j) {
- int r;
- _cleanup_free_ char *t = NULL, *z = NULL;
-
-
- r = sd_journal_get_catalog(j, &t);
- if (r < 0)
- return r;
-
- z = strreplace(strstrip(t), "\n", "\n-- ");
- if (!z)
- return log_oom();
-
- fputs("-- ", f);
- fputs(z, f);
- fputc('\n', f);
-
- return 0;
-}
-
-static int parse_field(const void *data, size_t length, const char *field, char **target, size_t *target_size) {
- size_t fl, nl;
- void *buf;
-
- assert(data);
- assert(field);
- assert(target);
- assert(target_size);
-
- fl = strlen(field);
- if (length < fl)
- return 0;
-
- if (memcmp(data, field, fl))
- return 0;
-
- nl = length - fl;
- buf = malloc(nl+1);
- if (!buf)
- return log_oom();
-
- memcpy(buf, (const char*) data + fl, nl);
- ((char*)buf)[nl] = 0;
-
- free(*target);
- *target = buf;
- *target_size = nl;
-
- return 1;
-}
-
-static bool shall_print(const char *p, size_t l, OutputFlags flags) {
- assert(p);
-
- if (flags & OUTPUT_SHOW_ALL)
- return true;
-
- if (l >= PRINT_CHAR_THRESHOLD)
- return false;
-
- if (!utf8_is_printable(p, l))
- return false;
-
- return true;
-}
-
-static bool print_multiline(FILE *f, unsigned prefix, unsigned n_columns, OutputFlags flags, int priority, const char* message, size_t message_len) {
- const char *color_on = "", *color_off = "";
- const char *pos, *end;
- bool ellipsized = false;
- int line = 0;
-
- if (flags & OUTPUT_COLOR) {
- if (priority <= LOG_ERR) {
- color_on = ANSI_HIGHLIGHT_RED_ON;
- color_off = ANSI_HIGHLIGHT_OFF;
- } else if (priority <= LOG_NOTICE) {
- color_on = ANSI_HIGHLIGHT_ON;
- color_off = ANSI_HIGHLIGHT_OFF;
- }
- }
-
- /* A special case: make sure that we print a newline when
- the message is empty. */
- if (message_len == 0)
- fputs("\n", f);
-
- for (pos = message;
- pos < message + message_len;
- pos = end + 1, line++) {
- bool continuation = line > 0;
- bool tail_line;
- int len;
- for (end = pos; end < message + message_len && *end != '\n'; end++)
- ;
- len = end - pos;
- assert(len >= 0);
-
- /* We need to figure out when we are showing not-last line, *and*
- * will skip subsequent lines. In that case, we will put the dots
- * at the end of the line, instead of putting dots in the middle
- * or not at all.
- */
- tail_line =
- line + 1 == PRINT_LINE_THRESHOLD ||
- end + 1 >= message + PRINT_CHAR_THRESHOLD;
-
- if (flags & (OUTPUT_FULL_WIDTH | OUTPUT_SHOW_ALL) ||
- (prefix + len + 1 < n_columns && !tail_line)) {
- fprintf(f, "%*s%s%.*s%s\n",
- continuation * prefix, "",
- color_on, len, pos, color_off);
- continue;
- }
-
- /* Beyond this point, ellipsization will happen. */
- ellipsized = true;
-
- if (prefix < n_columns && n_columns - prefix >= 3) {
- if (n_columns - prefix > (unsigned) len + 3)
- fprintf(f, "%*s%s%.*s...%s\n",
- continuation * prefix, "",
- color_on, len, pos, color_off);
- else {
- _cleanup_free_ char *e;
-
- e = ellipsize_mem(pos, len, n_columns - prefix,
- tail_line ? 100 : 90);
- if (!e)
- fprintf(f, "%*s%s%.*s%s\n",
- continuation * prefix, "",
- color_on, len, pos, color_off);
- else
- fprintf(f, "%*s%s%s%s\n",
- continuation * prefix, "",
- color_on, e, color_off);
- }
- } else
- fputs("...\n", f);
-
- if (tail_line)
- break;
- }
-
- return ellipsized;
-}
-
-static int output_short(
- FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags) {
-
- int r;
- const void *data;
- size_t length;
- size_t n = 0;
- _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL;
- size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, realtime_len = 0, monotonic_len = 0, priority_len = 0;
- int p = LOG_INFO;
- bool ellipsized = false;
-
- assert(f);
- assert(j);
-
- /* Set the threshold to one bigger than the actual print
- * threshold, so that if the line is actually longer than what
- * we're willing to print, ellipsization will occur. This way
- * we won't output a misleading line without any indication of
- * truncation.
- */
- sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1);
-
- JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
-
- r = parse_field(data, length, "PRIORITY=", &priority, &priority_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "SYSLOG_IDENTIFIER=", &identifier, &identifier_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "_COMM=", &comm, &comm_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "_PID=", &pid, &pid_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "SYSLOG_PID=", &fake_pid, &fake_pid_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len);
- if (r < 0)
- return r;
- else if (r > 0)
- continue;
-
- r = parse_field(data, length, "MESSAGE=", &message, &message_len);
- if (r < 0)
- return r;
- }
-
- if (r < 0)
- return r;
-
- if (!message)
- return 0;
-
- if (!(flags & OUTPUT_SHOW_ALL))
- strip_tab_ansi(&message, &message_len);
-
- if (priority_len == 1 && *priority >= '0' && *priority <= '7')
- p = *priority - '0';
-
- if (mode == OUTPUT_SHORT_MONOTONIC) {
- uint64_t t;
- sd_id128_t boot_id;
-
- r = -ENOENT;
-
- if (monotonic)
- r = safe_atou64(monotonic, &t);
-
- if (r < 0)
- r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
-
- if (r < 0)
- return log_error_errno(r, "Failed to get monotonic timestamp: %m");
-
- fprintf(f, "[%5llu.%06llu]",
- (unsigned long long) (t / USEC_PER_SEC),
- (unsigned long long) (t % USEC_PER_SEC));
-
- n += 1 + 5 + 1 + 6 + 1;
-
- } else {
- char buf[64];
- uint64_t x;
- time_t t;
- struct tm tm;
- struct tm *(*gettime_r)(const time_t *, struct tm *);
-
- r = -ENOENT;
- gettime_r = (flags & OUTPUT_UTC) ? gmtime_r : localtime_r;
-
- if (realtime)
- r = safe_atou64(realtime, &x);
-
- if (r < 0)
- r = sd_journal_get_realtime_usec(j, &x);
-
- if (r < 0)
- return log_error_errno(r, "Failed to get realtime timestamp: %m");
-
- t = (time_t) (x / USEC_PER_SEC);
-
- switch(mode) {
- case OUTPUT_SHORT_ISO:
- r = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t, &tm));
- break;
- case OUTPUT_SHORT_PRECISE:
- r = strftime(buf, sizeof(buf), "%b %d %H:%M:%S", gettime_r(&t, &tm));
- if (r > 0) {
- snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
- ".%06llu", (unsigned long long) (x % USEC_PER_SEC));
- }
- break;
- default:
- r = strftime(buf, sizeof(buf), "%b %d %H:%M:%S", gettime_r(&t, &tm));
- }
-
- if (r <= 0) {
- log_error("Failed to format time.");
- return -EINVAL;
- }
-
- fputs(buf, f);
- n += strlen(buf);
- }
-
- if (hostname && shall_print(hostname, hostname_len, flags)) {
- fprintf(f, " %.*s", (int) hostname_len, hostname);
- n += hostname_len + 1;
- }
-
- if (identifier && shall_print(identifier, identifier_len, flags)) {
- fprintf(f, " %.*s", (int) identifier_len, identifier);
- n += identifier_len + 1;
- } else if (comm && shall_print(comm, comm_len, flags)) {
- fprintf(f, " %.*s", (int) comm_len, comm);
- n += comm_len + 1;
- } else
- fputs(" unknown", f);
-
- if (pid && shall_print(pid, pid_len, flags)) {
- fprintf(f, "[%.*s]", (int) pid_len, pid);
- n += pid_len + 2;
- } else if (fake_pid && shall_print(fake_pid, fake_pid_len, flags)) {
- fprintf(f, "[%.*s]", (int) fake_pid_len, fake_pid);
- n += fake_pid_len + 2;
- }
-
- if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) {
- char bytes[FORMAT_BYTES_MAX];
- fprintf(f, ": [%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
- } else {
- fputs(": ", f);
- ellipsized |=
- print_multiline(f, n + 2, n_columns, flags, p, message, message_len);
- }
-
- if (flags & OUTPUT_CATALOG)
- print_catalog(f, j);
-
- return ellipsized;
-}
-
-static int output_verbose(
- FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags) {
-
- const void *data;
- size_t length;
- _cleanup_free_ char *cursor = NULL;
- uint64_t realtime;
- char ts[FORMAT_TIMESTAMP_MAX + 7];
- int r;
-
- assert(f);
- assert(j);
-
- sd_journal_set_data_threshold(j, 0);
-
- r = sd_journal_get_data(j, "_SOURCE_REALTIME_TIMESTAMP", &data, &length);
- if (r == -ENOENT)
- log_debug("Source realtime timestamp not found");
- else if (r < 0) {
- log_full(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR,
- "Failed to get source realtime timestamp: %s", strerror(-r));
- return r;
- } else {
- _cleanup_free_ char *value = NULL;
- size_t size;
-
- r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=", &value, &size);
- if (r < 0)
- log_debug_errno(r, "_SOURCE_REALTIME_TIMESTAMP invalid: %m");
- else {
- r = safe_atou64(value, &realtime);
- if (r < 0)
- log_debug_errno(r, "Failed to parse realtime timestamp: %m");
- }
- }
-
- if (r < 0) {
- r = sd_journal_get_realtime_usec(j, &realtime);
- if (r < 0) {
- log_full(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR,
- "Failed to get realtime timestamp: %s", strerror(-r));
- return r;
- }
- }
-
- r = sd_journal_get_cursor(j, &cursor);
- if (r < 0)
- return log_error_errno(r, "Failed to get cursor: %m");
-
- fprintf(f, "%s [%s]\n",
- flags & OUTPUT_UTC ?
- format_timestamp_us_utc(ts, sizeof(ts), realtime) :
- format_timestamp_us(ts, sizeof(ts), realtime),
- cursor);
-
- JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
- const char *c;
- int fieldlen;
- const char *on = "", *off = "";
-
- c = memchr(data, '=', length);
- if (!c) {
- log_error("Invalid field.");
- return -EINVAL;
- }
- fieldlen = c - (const char*) data;
-
- if (flags & OUTPUT_COLOR && startswith(data, "MESSAGE=")) {
- on = ANSI_HIGHLIGHT_ON;
- off = ANSI_HIGHLIGHT_OFF;
- }
-
- if (flags & OUTPUT_SHOW_ALL ||
- (((length < PRINT_CHAR_THRESHOLD) || flags & OUTPUT_FULL_WIDTH)
- && utf8_is_printable(data, length))) {
- fprintf(f, " %s%.*s=", on, fieldlen, (const char*)data);
- print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, c + 1, length - fieldlen - 1);
- fputs(off, f);
- } else {
- char bytes[FORMAT_BYTES_MAX];
-
- fprintf(f, " %s%.*s=[%s blob data]%s\n",
- on,
- (int) (c - (const char*) data),
- (const char*) data,
- format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1),
- off);
- }
- }
-
- if (r < 0)
- return r;
-
- if (flags & OUTPUT_CATALOG)
- print_catalog(f, j);
-
- return 0;
-}
-
-static int output_export(
- FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags) {
-
- sd_id128_t boot_id;
- char sid[33];
- int r;
- usec_t realtime, monotonic;
- _cleanup_free_ char *cursor = NULL;
- const void *data;
- size_t length;
-
- assert(j);
-
- sd_journal_set_data_threshold(j, 0);
-
- r = sd_journal_get_realtime_usec(j, &realtime);
- if (r < 0)
- return log_error_errno(r, "Failed to get realtime timestamp: %m");
-
- r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
- if (r < 0)
- return log_error_errno(r, "Failed to get monotonic timestamp: %m");
-
- r = sd_journal_get_cursor(j, &cursor);
- if (r < 0)
- return log_error_errno(r, "Failed to get cursor: %m");
-
- fprintf(f,
- "__CURSOR=%s\n"
- "__REALTIME_TIMESTAMP="USEC_FMT"\n"
- "__MONOTONIC_TIMESTAMP="USEC_FMT"\n"
- "_BOOT_ID=%s\n",
- cursor,
- realtime,
- monotonic,
- sd_id128_to_string(boot_id, sid));
-
- JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
-
- /* We already printed the boot id, from the data in
- * the header, hence let's suppress it here */
- if (length >= 9 &&
- startswith(data, "_BOOT_ID="))
- continue;
-
- if (utf8_is_printable_newline(data, length, false))
- fwrite(data, length, 1, f);
- else {
- const char *c;
- uint64_t le64;
-
- c = memchr(data, '=', length);
- if (!c) {
- log_error("Invalid field.");
- return -EINVAL;
- }
-
- fwrite(data, c - (const char*) data, 1, f);
- fputc('\n', f);
- le64 = htole64(length - (c - (const char*) data) - 1);
- fwrite(&le64, sizeof(le64), 1, f);
- fwrite(c + 1, length - (c - (const char*) data) - 1, 1, f);
- }
-
- fputc('\n', f);
- }
-
- if (r < 0)
- return r;
-
- fputc('\n', f);
-
- return 0;
-}
-
-void json_escape(
- FILE *f,
- const char* p,
- size_t l,
- OutputFlags flags) {
-
- assert(f);
- assert(p);
-
- if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD)
-
- fputs("null", f);
-
- else if (!utf8_is_printable(p, l)) {
- bool not_first = false;
-
- fputs("[ ", f);
-
- while (l > 0) {
- if (not_first)
- fprintf(f, ", %u", (uint8_t) *p);
- else {
- not_first = true;
- fprintf(f, "%u", (uint8_t) *p);
- }
-
- p++;
- l--;
- }
-
- fputs(" ]", f);
- } else {
- fputc('\"', f);
-
- while (l > 0) {
- if (*p == '"' || *p == '\\') {
- fputc('\\', f);
- fputc(*p, f);
- } else if (*p == '\n')
- fputs("\\n", f);
- else if (*p < ' ')
- fprintf(f, "\\u%04x", *p);
- else
- fputc(*p, f);
-
- p++;
- l--;
- }
-
- fputc('\"', f);
- }
-}
-
-static int output_json(
- FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags) {
-
- uint64_t realtime, monotonic;
- _cleanup_free_ char *cursor = NULL;
- const void *data;
- size_t length;
- sd_id128_t boot_id;
- char sid[33], *k;
- int r;
- Hashmap *h = NULL;
- bool done, separator;
-
- assert(j);
-
- sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD);
-
- r = sd_journal_get_realtime_usec(j, &realtime);
- if (r < 0)
- return log_error_errno(r, "Failed to get realtime timestamp: %m");
-
- r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
- if (r < 0)
- return log_error_errno(r, "Failed to get monotonic timestamp: %m");
-
- r = sd_journal_get_cursor(j, &cursor);
- if (r < 0)
- return log_error_errno(r, "Failed to get cursor: %m");
-
- if (mode == OUTPUT_JSON_PRETTY)
- fprintf(f,
- "{\n"
- "\t\"__CURSOR\" : \"%s\",\n"
- "\t\"__REALTIME_TIMESTAMP\" : \""USEC_FMT"\",\n"
- "\t\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT"\",\n"
- "\t\"_BOOT_ID\" : \"%s\"",
- cursor,
- realtime,
- monotonic,
- sd_id128_to_string(boot_id, sid));
- else {
- if (mode == OUTPUT_JSON_SSE)
- fputs("data: ", f);
-
- fprintf(f,
- "{ \"__CURSOR\" : \"%s\", "
- "\"__REALTIME_TIMESTAMP\" : \""USEC_FMT"\", "
- "\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT"\", "
- "\"_BOOT_ID\" : \"%s\"",
- cursor,
- realtime,
- monotonic,
- sd_id128_to_string(boot_id, sid));
- }
-
- h = hashmap_new(&string_hash_ops);
- if (!h)
- return -ENOMEM;
-
- /* First round, iterate through the entry and count how often each field appears */
- JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
- const char *eq;
- char *n;
- unsigned u;
-
- if (length >= 9 &&
- memcmp(data, "_BOOT_ID=", 9) == 0)
- continue;
-
- eq = memchr(data, '=', length);
- if (!eq)
- continue;
-
- n = strndup(data, eq - (const char*) data);
- if (!n) {
- r = -ENOMEM;
- goto finish;
- }
-
- u = PTR_TO_UINT(hashmap_get(h, n));
- if (u == 0) {
- r = hashmap_put(h, n, UINT_TO_PTR(1));
- if (r < 0) {
- free(n);
- goto finish;
- }
- } else {
- r = hashmap_update(h, n, UINT_TO_PTR(u + 1));
- free(n);
- if (r < 0)
- goto finish;
- }
- }
-
- if (r < 0)
- return r;
-
- separator = true;
- do {
- done = true;
-
- SD_JOURNAL_FOREACH_DATA(j, data, length) {
- const char *eq;
- char *kk, *n;
- size_t m;
- unsigned u;
-
- /* We already printed the boot id, from the data in
- * the header, hence let's suppress it here */
- if (length >= 9 &&
- memcmp(data, "_BOOT_ID=", 9) == 0)
- continue;
-
- eq = memchr(data, '=', length);
- if (!eq)
- continue;
-
- if (separator) {
- if (mode == OUTPUT_JSON_PRETTY)
- fputs(",\n\t", f);
- else
- fputs(", ", f);
- }
-
- m = eq - (const char*) data;
-
- n = strndup(data, m);
- if (!n) {
- r = -ENOMEM;
- goto finish;
- }
-
- u = PTR_TO_UINT(hashmap_get2(h, n, (void**) &kk));
- if (u == 0) {
- /* We already printed this, let's jump to the next */
- free(n);
- separator = false;
-
- continue;
- } else if (u == 1) {
- /* Field only appears once, output it directly */
-
- json_escape(f, data, m, flags);
- fputs(" : ", f);
-
- json_escape(f, eq + 1, length - m - 1, flags);
-
- hashmap_remove(h, n);
- free(kk);
- free(n);
-
- separator = true;
-
- continue;
-
- } else {
- /* Field appears multiple times, output it as array */
- json_escape(f, data, m, flags);
- fputs(" : [ ", f);
- json_escape(f, eq + 1, length - m - 1, flags);
-
- /* Iterate through the end of the list */
-
- while (sd_journal_enumerate_data(j, &data, &length) > 0) {
- if (length < m + 1)
- continue;
-
- if (memcmp(data, n, m) != 0)
- continue;
-
- if (((const char*) data)[m] != '=')
- continue;
-
- fputs(", ", f);
- json_escape(f, (const char*) data + m + 1, length - m - 1, flags);
- }
-
- fputs(" ]", f);
-
- hashmap_remove(h, n);
- free(kk);
- free(n);
-
- /* Iterate data fields form the beginning */
- done = false;
- separator = true;
-
- break;
- }
- }
-
- } while (!done);
-
- if (mode == OUTPUT_JSON_PRETTY)
- fputs("\n}\n", f);
- else if (mode == OUTPUT_JSON_SSE)
- fputs("}\n\n", f);
- else
- fputs(" }\n", f);
-
- r = 0;
-
-finish:
- while ((k = hashmap_steal_first_key(h)))
- free(k);
-
- hashmap_free(h);
-
- return r;
-}
-
-static int output_cat(
- FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags) {
-
- const void *data;
- size_t l;
- int r;
-
- assert(j);
- assert(f);
-
- sd_journal_set_data_threshold(j, 0);
-
- r = sd_journal_get_data(j, "MESSAGE", &data, &l);
- if (r < 0) {
- /* An entry without MESSAGE=? */
- if (r == -ENOENT)
- return 0;
-
- return log_error_errno(r, "Failed to get data: %m");
- }
-
- assert(l >= 8);
-
- fwrite((const char*) data + 8, 1, l - 8, f);
- fputc('\n', f);
-
- return 0;
-}
-
-static int (*output_funcs[_OUTPUT_MODE_MAX])(
- FILE *f,
- sd_journal*j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags) = {
-
- [OUTPUT_SHORT] = output_short,
- [OUTPUT_SHORT_ISO] = output_short,
- [OUTPUT_SHORT_PRECISE] = output_short,
- [OUTPUT_SHORT_MONOTONIC] = output_short,
- [OUTPUT_VERBOSE] = output_verbose,
- [OUTPUT_EXPORT] = output_export,
- [OUTPUT_JSON] = output_json,
- [OUTPUT_JSON_PRETTY] = output_json,
- [OUTPUT_JSON_SSE] = output_json,
- [OUTPUT_CAT] = output_cat
-};
-
-int output_journal(
- FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags,
- bool *ellipsized) {
-
- int ret;
- assert(mode >= 0);
- assert(mode < _OUTPUT_MODE_MAX);
-
- if (n_columns <= 0)
- n_columns = columns();
-
- ret = output_funcs[mode](f, j, mode, n_columns, flags);
- fflush(stdout);
-
- if (ellipsized && ret > 0)
- *ellipsized = true;
-
- return ret;
-}
-
-static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) {
- assert(f);
- assert(flags);
-
- if (!(*flags & OUTPUT_BEGIN_NEWLINE))
- return 0;
-
- /* Print a beginning new line if that's request, but only once
- * on the first line we print. */
-
- fputc('\n', f);
- *flags &= ~OUTPUT_BEGIN_NEWLINE;
- return 0;
-}
-
-static int show_journal(FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- usec_t not_before,
- unsigned how_many,
- OutputFlags flags,
- bool *ellipsized) {
-
- int r;
- unsigned line = 0;
- bool need_seek = false;
- int warn_cutoff = flags & OUTPUT_WARN_CUTOFF;
-
- assert(j);
- assert(mode >= 0);
- assert(mode < _OUTPUT_MODE_MAX);
-
- /* Seek to end */
- r = sd_journal_seek_tail(j);
- if (r < 0)
- goto finish;
-
- r = sd_journal_previous_skip(j, how_many);
- if (r < 0)
- goto finish;
-
- for (;;) {
- for (;;) {
- usec_t usec;
-
- if (need_seek) {
- r = sd_journal_next(j);
- if (r < 0)
- goto finish;
- }
-
- if (r == 0)
- break;
-
- need_seek = true;
-
- if (not_before > 0) {
- r = sd_journal_get_monotonic_usec(j, &usec, NULL);
-
- /* -ESTALE is returned if the
- timestamp is not from this boot */
- if (r == -ESTALE)
- continue;
- else if (r < 0)
- goto finish;
-
- if (usec < not_before)
- continue;
- }
-
- line ++;
- maybe_print_begin_newline(f, &flags);
-
- r = output_journal(f, j, mode, n_columns, flags, ellipsized);
- if (r < 0)
- goto finish;
- }
-
- if (warn_cutoff && line < how_many && not_before > 0) {
- sd_id128_t boot_id;
- usec_t cutoff = 0;
-
- /* Check whether the cutoff line is too early */
-
- r = sd_id128_get_boot(&boot_id);
- if (r < 0)
- goto finish;
-
- r = sd_journal_get_cutoff_monotonic_usec(j, boot_id, &cutoff, NULL);
- if (r < 0)
- goto finish;
-
- if (r > 0 && not_before < cutoff) {
- maybe_print_begin_newline(f, &flags);
- fprintf(f, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
- }
-
- warn_cutoff = false;
- }
-
- if (!(flags & OUTPUT_FOLLOW))
- break;
-
- r = sd_journal_wait(j, USEC_INFINITY);
- if (r < 0)
- goto finish;
-
- }
-
-finish:
- return r;
-}
-
-int add_matches_for_unit(sd_journal *j, const char *unit) {
- int r;
- char *m1, *m2, *m3, *m4;
-
- assert(j);
- assert(unit);
-
- m1 = strjoina("_SYSTEMD_UNIT=", unit);
- m2 = strjoina("COREDUMP_UNIT=", unit);
- m3 = strjoina("UNIT=", unit);
- m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit);
-
- (void)(
- /* Look for messages from the service itself */
- (r = sd_journal_add_match(j, m1, 0)) ||
-
- /* Look for coredumps of the service */
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
- (r = sd_journal_add_match(j, "_UID=0", 0)) ||
- (r = sd_journal_add_match(j, m2, 0)) ||
-
- /* Look for messages from PID 1 about this service */
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, "_PID=1", 0)) ||
- (r = sd_journal_add_match(j, m3, 0)) ||
-
- /* Look for messages from authorized daemons about this service */
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, "_UID=0", 0)) ||
- (r = sd_journal_add_match(j, m4, 0))
- );
-
- if (r == 0 && endswith(unit, ".slice")) {
- char *m5 = strappend("_SYSTEMD_SLICE=", unit);
-
- /* Show all messages belonging to a slice */
- (void)(
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, m5, 0))
- );
- }
-
- return r;
-}
-
-int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
- int r;
- char *m1, *m2, *m3, *m4;
- char muid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)];
-
- assert(j);
- assert(unit);
-
- m1 = strjoina("_SYSTEMD_USER_UNIT=", unit);
- m2 = strjoina("USER_UNIT=", unit);
- m3 = strjoina("COREDUMP_USER_UNIT=", unit);
- m4 = strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit);
- sprintf(muid, "_UID="UID_FMT, uid);
-
- (void) (
- /* Look for messages from the user service itself */
- (r = sd_journal_add_match(j, m1, 0)) ||
- (r = sd_journal_add_match(j, muid, 0)) ||
-
- /* Look for messages from systemd about this service */
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, m2, 0)) ||
- (r = sd_journal_add_match(j, muid, 0)) ||
-
- /* Look for coredumps of the service */
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, m3, 0)) ||
- (r = sd_journal_add_match(j, muid, 0)) ||
- (r = sd_journal_add_match(j, "_UID=0", 0)) ||
-
- /* Look for messages from authorized daemons about this service */
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, m4, 0)) ||
- (r = sd_journal_add_match(j, muid, 0)) ||
- (r = sd_journal_add_match(j, "_UID=0", 0))
- );
-
- if (r == 0 && endswith(unit, ".slice")) {
- char *m5 = strappend("_SYSTEMD_SLICE=", unit);
-
- /* Show all messages belonging to a slice */
- (void)(
- (r = sd_journal_add_disjunction(j)) ||
- (r = sd_journal_add_match(j, m5, 0)) ||
- (r = sd_journal_add_match(j, muid, 0))
- );
- }
-
- return r;
-}
-
-static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
- _cleanup_close_pair_ int pair[2] = { -1, -1 };
- _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
- pid_t pid, child;
- siginfo_t si;
- char buf[37];
- ssize_t k;
- int r;
-
- assert(machine);
- assert(boot_id);
-
- if (!machine_name_is_valid(machine))
- return -EINVAL;
-
- r = container_get_leader(machine, &pid);
- if (r < 0)
- return r;
-
- r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &rootfd);
- if (r < 0)
- return r;
-
- if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
- return -errno;
-
- child = fork();
- if (child < 0)
- return -errno;
-
- if (child == 0) {
- int fd;
-
- pair[0] = safe_close(pair[0]);
-
- r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
- if (r < 0)
- _exit(EXIT_FAILURE);
-
- fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
- _exit(EXIT_FAILURE);
-
- r = loop_read_exact(fd, buf, 36, false);
- safe_close(fd);
- if (r < 0)
- _exit(EXIT_FAILURE);
-
- k = send(pair[1], buf, 36, MSG_NOSIGNAL);
- if (k != 36)
- _exit(EXIT_FAILURE);
-
- _exit(EXIT_SUCCESS);
- }
-
- pair[1] = safe_close(pair[1]);
-
- r = wait_for_terminate(child, &si);
- if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
- return r < 0 ? r : -EIO;
-
- k = recv(pair[0], buf, 36, 0);
- if (k != 36)
- return -EIO;
-
- buf[36] = 0;
- r = sd_id128_from_string(buf, boot_id);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int add_match_this_boot(sd_journal *j, const char *machine) {
- char match[9+32+1] = "_BOOT_ID=";
- sd_id128_t boot_id;
- int r;
-
- assert(j);
-
- if (machine) {
- r = get_boot_id_for_machine(machine, &boot_id);
- if (r < 0)
- return log_error_errno(r, "Failed to get boot id of container %s: %m", machine);
- } else {
- r = sd_id128_get_boot(&boot_id);
- if (r < 0)
- return log_error_errno(r, "Failed to get boot id: %m");
- }
-
- sd_id128_to_string(boot_id, match + 9);
- r = sd_journal_add_match(j, match, strlen(match));
- if (r < 0)
- return log_error_errno(r, "Failed to add match: %m");
-
- r = sd_journal_add_conjunction(j);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int show_journal_by_unit(
- FILE *f,
- const char *unit,
- OutputMode mode,
- unsigned n_columns,
- usec_t not_before,
- unsigned how_many,
- uid_t uid,
- OutputFlags flags,
- int journal_open_flags,
- bool system_unit,
- bool *ellipsized) {
-
- _cleanup_journal_close_ sd_journal*j = NULL;
- int r;
-
- assert(mode >= 0);
- assert(mode < _OUTPUT_MODE_MAX);
- assert(unit);
-
- if (how_many <= 0)
- return 0;
-
- r = sd_journal_open(&j, journal_open_flags);
- if (r < 0)
- return r;
-
- r = add_match_this_boot(j, NULL);
- if (r < 0)
- return r;
-
- if (system_unit)
- r = add_matches_for_unit(j, unit);
- else
- r = add_matches_for_user_unit(j, unit, uid);
- if (r < 0)
- return r;
-
- if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) {
- _cleanup_free_ char *filter;
-
- filter = journal_make_match_string(j);
- log_debug("Journal filter: %s", filter);
- }
-
- return show_journal(f, j, mode, n_columns, not_before, how_many, flags, ellipsized);
-}
-
-static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
- [OUTPUT_SHORT] = "short",
- [OUTPUT_SHORT_ISO] = "short-iso",
- [OUTPUT_SHORT_PRECISE] = "short-precise",
- [OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
- [OUTPUT_VERBOSE] = "verbose",
- [OUTPUT_EXPORT] = "export",
- [OUTPUT_JSON] = "json",
- [OUTPUT_JSON_PRETTY] = "json-pretty",
- [OUTPUT_JSON_SSE] = "json-sse",
- [OUTPUT_CAT] = "cat"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <sys/types.h>
-
-#include "sd-journal.h"
-
-#include "util.h"
-#include "output-mode.h"
-
-int output_journal(
- FILE *f,
- sd_journal *j,
- OutputMode mode,
- unsigned n_columns,
- OutputFlags flags,
- bool *ellipsized);
-
-int add_match_this_boot(sd_journal *j, const char *machine);
-
-int add_matches_for_unit(
- sd_journal *j,
- const char *unit);
-
-int add_matches_for_user_unit(
- sd_journal *j,
- const char *unit,
- uid_t uid);
-
-int show_journal_by_unit(
- FILE *f,
- const char *unit,
- OutputMode mode,
- unsigned n_columns,
- usec_t not_before,
- unsigned how_many,
- uid_t uid,
- OutputFlags flags,
- int journal_open_flags,
- bool system_unit,
- bool *ellipsized);
-
-void json_escape(
- FILE *f,
- const char* p,
- size_t l,
- OutputFlags flags);
-
-const char* output_mode_to_string(OutputMode m) _const_;
-OutputMode output_mode_from_string(const char *s) _pure_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/statfs.h>
-#include <linux/fs.h>
-#include <fcntl.h>
-
-#include "utf8.h"
-#include "btrfs-util.h"
-#include "path-util.h"
-#include "copy.h"
-#include "mkdir.h"
-#include "machine-image.h"
-
-static const char image_search_path[] =
- "/var/lib/machines\0"
- "/var/lib/container\0"
- "/usr/local/lib/machines\0"
- "/usr/lib/machines\0";
-
-Image *image_unref(Image *i) {
- if (!i)
- return NULL;
-
- free(i->name);
- free(i->path);
- free(i);
- return NULL;
-}
-
-static int image_new(
- ImageType t,
- const char *pretty,
- const char *path,
- const char *filename,
- bool read_only,
- usec_t crtime,
- usec_t mtime,
- Image **ret) {
-
- _cleanup_(image_unrefp) Image *i = NULL;
-
- assert(t >= 0);
- assert(t < _IMAGE_TYPE_MAX);
- assert(pretty);
- assert(filename);
- assert(ret);
-
- i = new0(Image, 1);
- if (!i)
- return -ENOMEM;
-
- i->type = t;
- i->read_only = read_only;
- i->crtime = crtime;
- i->mtime = mtime;
- i->usage = i->usage_exclusive = (uint64_t) -1;
- i->limit = i->limit_exclusive = (uint64_t) -1;
-
- i->name = strdup(pretty);
- if (!i->name)
- return -ENOMEM;
-
- if (path)
- i->path = strjoin(path, "/", filename, NULL);
- else
- i->path = strdup(filename);
-
- if (!i->path)
- return -ENOMEM;
-
- path_kill_slashes(i->path);
-
- *ret = i;
- i = NULL;
-
- return 0;
-}
-
-static int image_make(
- const char *pretty,
- int dfd,
- const char *path,
- const char *filename,
- Image **ret) {
-
- struct stat st;
- bool read_only;
- int r;
-
- assert(filename);
-
- /* We explicitly *do* follow symlinks here, since we want to
- * allow symlinking trees into /var/lib/machines/, and treat
- * them normally. */
-
- if (fstatat(dfd, filename, &st, 0) < 0)
- return -errno;
-
- read_only =
- (path && path_startswith(path, "/usr")) ||
- (faccessat(dfd, filename, W_OK, AT_EACCESS) < 0 && errno == EROFS);
-
- if (S_ISDIR(st.st_mode)) {
- _cleanup_close_ int fd = -1;
- unsigned file_attr = 0;
-
- if (!ret)
- return 1;
-
- if (!pretty)
- pretty = filename;
-
- fd = openat(dfd, filename, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
- if (fd < 0)
- return -errno;
-
- /* btrfs subvolumes have inode 256 */
- if (st.st_ino == 256) {
- struct statfs sfs;
-
- if (fstatfs(fd, &sfs) < 0)
- return -errno;
-
- if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
- BtrfsSubvolInfo info;
- BtrfsQuotaInfo quota;
-
- /* It's a btrfs subvolume */
-
- r = btrfs_subvol_get_info_fd(fd, &info);
- if (r < 0)
- return r;
-
- r = image_new(IMAGE_SUBVOLUME,
- pretty,
- path,
- filename,
- info.read_only || read_only,
- info.otime,
- 0,
- ret);
- if (r < 0)
- return r;
-
- r = btrfs_subvol_get_quota_fd(fd, "a);
- if (r >= 0) {
- (*ret)->usage = quota.referenced;
- (*ret)->usage_exclusive = quota.exclusive;
-
- (*ret)->limit = quota.referenced_max;
- (*ret)->limit_exclusive = quota.exclusive_max;
- }
-
- return 1;
- }
- }
-
- /* If the IMMUTABLE bit is set, we consider the
- * directory read-only. Since the ioctl is not
- * supported everywhere we ignore failures. */
- (void) read_attr_fd(fd, &file_attr);
-
- /* It's just a normal directory. */
- r = image_new(IMAGE_DIRECTORY,
- pretty,
- path,
- filename,
- read_only || (file_attr & FS_IMMUTABLE_FL),
- 0,
- 0,
- ret);
- if (r < 0)
- return r;
-
- return 1;
-
- } else if (S_ISREG(st.st_mode) && endswith(filename, ".raw")) {
- usec_t crtime = 0;
-
- /* It's a RAW disk image */
-
- if (!ret)
- return 1;
-
- fd_getcrtime_at(dfd, filename, &crtime, 0);
-
- if (!pretty)
- pretty = strndupa(filename, strlen(filename) - 4);
-
- r = image_new(IMAGE_RAW,
- pretty,
- path,
- filename,
- !(st.st_mode & 0222) || read_only,
- crtime,
- timespec_load(&st.st_mtim),
- ret);
- if (r < 0)
- return r;
-
- (*ret)->usage = (*ret)->usage_exclusive = st.st_blocks * 512;
- (*ret)->limit = (*ret)->limit_exclusive = st.st_size;
-
- return 1;
- }
-
- return 0;
-}
-
-int image_find(const char *name, Image **ret) {
- const char *path;
- int r;
-
- assert(name);
-
- /* There are no images with invalid names */
- if (!image_name_is_valid(name))
- return 0;
-
- NULSTR_FOREACH(path, image_search_path) {
- _cleanup_closedir_ DIR *d = NULL;
-
- d = opendir(path);
- if (!d) {
- if (errno == ENOENT)
- continue;
-
- return -errno;
- }
-
- r = image_make(NULL, dirfd(d), path, name, ret);
- if (r == 0 || r == -ENOENT) {
- _cleanup_free_ char *raw = NULL;
-
- raw = strappend(name, ".raw");
- if (!raw)
- return -ENOMEM;
-
- r = image_make(NULL, dirfd(d), path, raw, ret);
- if (r == 0 || r == -ENOENT)
- continue;
- }
- if (r < 0)
- return r;
-
- return 1;
- }
-
- if (streq(name, ".host"))
- return image_make(".host", AT_FDCWD, NULL, "/", ret);
-
- return 0;
-};
-
-int image_discover(Hashmap *h) {
- const char *path;
- int r;
-
- assert(h);
-
- NULSTR_FOREACH(path, image_search_path) {
- _cleanup_closedir_ DIR *d = NULL;
- struct dirent *de;
-
- d = opendir(path);
- if (!d) {
- if (errno == ENOENT)
- continue;
-
- return -errno;
- }
-
- FOREACH_DIRENT_ALL(de, d, return -errno) {
- _cleanup_(image_unrefp) Image *image = NULL;
-
- if (!image_name_is_valid(de->d_name))
- continue;
-
- if (hashmap_contains(h, de->d_name))
- continue;
-
- r = image_make(NULL, dirfd(d), path, de->d_name, &image);
- if (r == 0 || r == -ENOENT)
- continue;
- if (r < 0)
- return r;
-
- r = hashmap_put(h, image->name, image);
- if (r < 0)
- return r;
-
- image = NULL;
- }
- }
-
- if (!hashmap_contains(h, ".host")) {
- _cleanup_(image_unrefp) Image *image = NULL;
-
- r = image_make(".host", AT_FDCWD, NULL, "/", &image);
- if (r < 0)
- return r;
-
- r = hashmap_put(h, image->name, image);
- if (r < 0)
- return r;
-
- image = NULL;
-
- }
-
- return 0;
-}
-
-void image_hashmap_free(Hashmap *map) {
- Image *i;
-
- while ((i = hashmap_steal_first(map)))
- image_unref(i);
-
- hashmap_free(map);
-}
-
-int image_remove(Image *i) {
- _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
- int r;
-
- assert(i);
-
- if (path_equal(i->path, "/") ||
- path_startswith(i->path, "/usr"))
- return -EROFS;
-
- /* Make sure we don't interfere with a running nspawn */
- r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
- if (r < 0)
- return r;
-
- switch (i->type) {
-
- case IMAGE_SUBVOLUME:
- return btrfs_subvol_remove(i->path);
-
- case IMAGE_DIRECTORY:
- /* Allow deletion of read-only directories */
- (void) chattr_path(i->path, false, FS_IMMUTABLE_FL);
-
- /* fall through */
-
- case IMAGE_RAW:
- return rm_rf_dangerous(i->path, false, true, false);
-
- default:
- return -EOPNOTSUPP;
- }
-}
-
-int image_rename(Image *i, const char *new_name) {
- _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT;
- _cleanup_free_ char *new_path = NULL, *nn = NULL;
- unsigned file_attr = 0;
- int r;
-
- assert(i);
-
- if (!image_name_is_valid(new_name))
- return -EINVAL;
-
- if (path_equal(i->path, "/") ||
- path_startswith(i->path, "/usr"))
- return -EROFS;
-
- /* Make sure we don't interfere with a running nspawn */
- r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
- if (r < 0)
- return r;
-
- /* Make sure nobody takes the new name, between the time we
- * checked it is currently unused in all search paths, and the
- * time we take possesion of it */
- r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
- if (r < 0)
- return r;
-
- r = image_find(new_name, NULL);
- if (r < 0)
- return r;
- if (r > 0)
- return -EEXIST;
-
- switch (i->type) {
-
- case IMAGE_DIRECTORY:
- /* Turn of the immutable bit while we rename the image, so that we can rename it */
- (void) read_attr_path(i->path, &file_attr);
-
- if (file_attr & FS_IMMUTABLE_FL)
- (void) chattr_path(i->path, false, FS_IMMUTABLE_FL);
-
- /* fall through */
-
- case IMAGE_SUBVOLUME:
- new_path = file_in_same_dir(i->path, new_name);
- break;
-
- case IMAGE_RAW: {
- const char *fn;
-
- fn = strjoina(new_name, ".raw");
- new_path = file_in_same_dir(i->path, fn);
- break;
- }
-
- default:
- return -EOPNOTSUPP;
- }
-
- if (!new_path)
- return -ENOMEM;
-
- nn = strdup(new_name);
- if (!nn)
- return -ENOMEM;
-
- r = rename_noreplace(AT_FDCWD, i->path, AT_FDCWD, new_path);
- if (r < 0)
- return r;
-
- /* Restore the immutable bit, if it was set before */
- if (file_attr & FS_IMMUTABLE_FL)
- (void) chattr_path(new_path, true, FS_IMMUTABLE_FL);
-
- free(i->path);
- i->path = new_path;
- new_path = NULL;
-
- free(i->name);
- i->name = nn;
- nn = NULL;
-
- return 0;
-}
-
-int image_clone(Image *i, const char *new_name, bool read_only) {
- _cleanup_release_lock_file_ LockFile name_lock = LOCK_FILE_INIT;
- const char *new_path;
- int r;
-
- assert(i);
-
- if (!image_name_is_valid(new_name))
- return -EINVAL;
-
- /* Make sure nobody takes the new name, between the time we
- * checked it is currently unused in all search paths, and the
- * time we take possesion of it */
- r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
- if (r < 0)
- return r;
-
- r = image_find(new_name, NULL);
- if (r < 0)
- return r;
- if (r > 0)
- return -EEXIST;
-
- switch (i->type) {
-
- case IMAGE_SUBVOLUME:
- case IMAGE_DIRECTORY:
- new_path = strjoina("/var/lib/machines/", new_name);
-
- r = btrfs_subvol_snapshot(i->path, new_path, read_only, true);
- break;
-
- case IMAGE_RAW:
- new_path = strjoina("/var/lib/machines/", new_name, ".raw");
-
- r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL);
- break;
-
- default:
- return -EOPNOTSUPP;
- }
-
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int image_read_only(Image *i, bool b) {
- _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
- int r;
- assert(i);
-
- if (path_equal(i->path, "/") ||
- path_startswith(i->path, "/usr"))
- return -EROFS;
-
- /* Make sure we don't interfere with a running nspawn */
- r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
- if (r < 0)
- return r;
-
- switch (i->type) {
-
- case IMAGE_SUBVOLUME:
- r = btrfs_subvol_set_read_only(i->path, b);
- if (r < 0)
- return r;
-
- break;
-
- case IMAGE_DIRECTORY:
- /* For simple directory trees we cannot use the access
- mode of the top-level directory, since it has an
- effect on the container itself. However, we can
- use the "immutable" flag, to at least make the
- top-level directory read-only. It's not as good as
- a read-only subvolume, but at least something, and
- we can read the value back.*/
-
- r = chattr_path(i->path, b, FS_IMMUTABLE_FL);
- if (r < 0)
- return r;
-
- break;
-
- case IMAGE_RAW: {
- struct stat st;
-
- if (stat(i->path, &st) < 0)
- return -errno;
-
- if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0)
- return -errno;
-
- /* If the images is now read-only, it's a good time to
- * defrag it, given that no write patterns will
- * fragment it again. */
- if (b)
- (void) btrfs_defrag(i->path);
- break;
- }
-
- default:
- return -EOPNOTSUPP;
- }
-
- return 0;
-}
-
-int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local) {
- _cleanup_free_ char *p = NULL;
- LockFile t = LOCK_FILE_INIT;
- struct stat st;
- int r;
-
- assert(path);
- assert(global);
- assert(local);
-
- /* Locks an image path. This actually creates two locks: one
- * "local" one, next to the image path itself, which might be
- * shared via NFS. And another "global" one, in /run, that
- * uses the device/inode number. This has the benefit that we
- * can even lock a tree that is a mount point, correctly. */
-
- if (path_equal(path, "/"))
- return -EBUSY;
-
- if (!path_is_absolute(path))
- return -EINVAL;
-
- if (stat(path, &st) >= 0) {
- if (asprintf(&p, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
- return -ENOMEM;
- }
-
- r = make_lock_file_for(path, operation, &t);
- if (r < 0)
- return r;
-
- if (p) {
- mkdir_p("/run/systemd/nspawn/locks", 0600);
-
- r = make_lock_file(p, operation, global);
- if (r < 0) {
- release_lock_file(&t);
- return r;
- }
- }
-
- *local = t;
- return 0;
-}
-
-int image_set_limit(Image *i, uint64_t referenced_max) {
- assert(i);
-
- if (path_equal(i->path, "/") ||
- path_startswith(i->path, "/usr"))
- return -EROFS;
-
- if (i->type != IMAGE_SUBVOLUME)
- return -EOPNOTSUPP;
-
- return btrfs_quota_limit(i->path, referenced_max);
-}
-
-int image_name_lock(const char *name, int operation, LockFile *ret) {
- const char *p;
-
- assert(name);
- assert(ret);
-
- /* Locks an image name, regardless of the precise path used. */
-
- if (!image_name_is_valid(name))
- return -EINVAL;
-
- if (streq(name, ".host"))
- return -EBUSY;
-
- mkdir_p("/run/systemd/nspawn/locks", 0600);
- p = strjoina("/run/systemd/nspawn/locks/name-", name);
-
- return make_lock_file(p, operation, ret);
-}
-
-bool image_name_is_valid(const char *s) {
- if (!filename_is_valid(s))
- return false;
-
- if (string_has_cc(s, NULL))
- return false;
-
- if (!utf8_is_valid(s))
- return false;
-
- /* Temporary files for atomically creating new files */
- if (startswith(s, ".#"))
- return false;
-
- return true;
-}
-
-static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
- [IMAGE_DIRECTORY] = "directory",
- [IMAGE_SUBVOLUME] = "subvolume",
- [IMAGE_RAW] = "raw",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "time-util.h"
-#include "hashmap.h"
-
-typedef enum ImageType {
- IMAGE_DIRECTORY,
- IMAGE_SUBVOLUME,
- IMAGE_RAW,
- _IMAGE_TYPE_MAX,
- _IMAGE_TYPE_INVALID = -1
-} ImageType;
-
-typedef struct Image {
- ImageType type;
- char *name;
- char *path;
- bool read_only;
-
- usec_t crtime;
- usec_t mtime;
-
- uint64_t usage;
- uint64_t usage_exclusive;
- uint64_t limit;
- uint64_t limit_exclusive;
-
- void *userdata;
-} Image;
-
-Image *image_unref(Image *i);
-void image_hashmap_free(Hashmap *map);
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
-DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free);
-
-int image_find(const char *name, Image **ret);
-int image_discover(Hashmap *map);
-
-int image_remove(Image *i);
-int image_rename(Image *i, const char *new_name);
-int image_clone(Image *i, const char *new_name, bool read_only);
-int image_read_only(Image *i, bool b);
-
-const char* image_type_to_string(ImageType t) _const_;
-ImageType image_type_from_string(const char *s) _pure_;
-
-bool image_name_is_valid(const char *s) _pure_;
-
-int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local);
-int image_name_lock(const char *name, int operation, LockFile *ret);
-
-int image_set_limit(Image *i, uint64_t referenced_max);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2015 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/prctl.h>
-#include <sys/vfs.h>
-#include <sys/statvfs.h>
-#include <sys/mount.h>
-
-#include "util.h"
-#include "mkdir.h"
-#include "btrfs-util.h"
-#include "path-util.h"
-#include "machine-pool.h"
-
-#define VAR_LIB_MACHINES_SIZE_START (1024UL*1024UL*500UL)
-#define VAR_LIB_MACHINES_FREE_MIN (1024UL*1024UL*750UL)
-
-static int check_btrfs(void) {
- struct statfs sfs;
-
- if (statfs("/var/lib/machines", &sfs) < 0) {
- if (errno != ENOENT)
- return -errno;
-
- if (statfs("/var/lib", &sfs) < 0)
- return -errno;
- }
-
- return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
-}
-
-static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
- _cleanup_free_ char *tmp = NULL;
- _cleanup_close_ int fd = -1;
- struct statvfs ss;
- pid_t pid = 0;
- siginfo_t si;
- int r;
-
- /* We want to be able to make use of btrfs-specific file
- * system features, in particular subvolumes, reflinks and
- * quota. Hence, if we detect that /var/lib/machines.raw is
- * not located on btrfs, let's create a loopback file, place a
- * btrfs file system into it, and mount it to
- * /var/lib/machines. */
-
- fd = open("/var/lib/machines.raw", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
- if (fd >= 0) {
- r = fd;
- fd = -1;
- return r;
- }
-
- if (errno != ENOENT)
- return sd_bus_error_set_errnof(error, errno, "Failed to open /var/lib/machines.raw: %m");
-
- r = tempfn_xxxxxx("/var/lib/machines.raw", &tmp);
- if (r < 0)
- return r;
-
- (void) mkdir_p_label("/var/lib", 0755);
- fd = open(tmp, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0600);
- if (fd < 0)
- return sd_bus_error_set_errnof(error, errno, "Failed to create /var/lib/machines.raw: %m");
-
- if (fstatvfs(fd, &ss) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to determine free space on /var/lib/machines.raw: %m");
- goto fail;
- }
-
- if (ss.f_bsize * ss.f_bavail < VAR_LIB_MACHINES_FREE_MIN) {
- r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Not enough free disk space to set up /var/lib/machines.");
- goto fail;
- }
-
- if (ftruncate(fd, size) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to enlarge /var/lib/machines.raw: %m");
- goto fail;
- }
-
- pid = fork();
- if (pid < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to fork mkfs.btrfs: %m");
- goto fail;
- }
-
- if (pid == 0) {
-
- /* Child */
-
- reset_all_signal_handlers();
- reset_signal_mask();
- assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
-
- fd = safe_close(fd);
-
- execlp("mkfs.btrfs", "-Lvar-lib-machines", tmp, NULL);
- if (errno == ENOENT)
- return 99;
-
- _exit(EXIT_FAILURE);
- }
-
- r = wait_for_terminate(pid, &si);
- if (r < 0) {
- sd_bus_error_set_errnof(error, r, "Failed to wait for mkfs.btrfs: %m");
- goto fail;
- }
-
- pid = 0;
-
- if (si.si_code != CLD_EXITED) {
- r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs died abnormally.");
- goto fail;
- }
- if (si.si_status == 99) {
- r = sd_bus_error_set_errnof(error, ENOENT, "Cannot set up /var/lib/machines, mkfs.btrfs is missing");
- goto fail;
- }
- if (si.si_status != 0) {
- r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs failed with error code %i", si.si_status);
- goto fail;
- }
-
- r = rename_noreplace(AT_FDCWD, tmp, AT_FDCWD, "/var/lib/machines.raw");
- if (r < 0) {
- sd_bus_error_set_errnof(error, r, "Failed to move /var/lib/machines.raw into place: %m");
- goto fail;
- }
-
- r = fd;
- fd = -1;
-
- return r;
-
-fail:
- unlink_noerrno(tmp);
-
- if (pid > 1)
- kill_and_sigcont(pid, SIGKILL);
-
- return r;
-}
-
-int setup_machine_directory(uint64_t size, sd_bus_error *error) {
- _cleanup_release_lock_file_ LockFile lock_file = LOCK_FILE_INIT;
- struct loop_info64 info = {
- .lo_flags = LO_FLAGS_AUTOCLEAR,
- };
- _cleanup_close_ int fd = -1, control = -1, loop = -1;
- _cleanup_free_ char* loopdev = NULL;
- char tmpdir[] = "/tmp/import-mount.XXXXXX", *mntdir = NULL;
- bool tmpdir_made = false, mntdir_made = false, mntdir_mounted = false;
- char buf[FORMAT_BYTES_MAX];
- int r, nr = -1;
-
- /* btrfs cannot handle file systems < 16M, hence use this as minimum */
- if (size == (uint64_t) -1)
- size = VAR_LIB_MACHINES_SIZE_START;
- else if (size < 16*1024*1024)
- size = 16*1024*1024;
-
- /* Make sure we only set the directory up once at a time */
- r = make_lock_file("/run/systemd/machines.lock", LOCK_EX, &lock_file);
- if (r < 0)
- return r;
-
- r = check_btrfs();
- if (r < 0)
- return sd_bus_error_set_errnof(error, r, "Failed to determine whether /var/lib/machines is located on btrfs: %m");
- if (r > 0) {
- (void) btrfs_subvol_make_label("/var/lib/machines");
-
- r = btrfs_quota_enable("/var/lib/machines", true);
- if (r < 0)
- log_warning_errno(r, "Failed to enable quota, ignoring: %m");
-
- return 0;
- }
-
- if (path_is_mount_point("/var/lib/machines", true) > 0 ||
- dir_is_empty("/var/lib/machines") == 0)
- return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "/var/lib/machines is not a btrfs file system. Operation is not supported on legacy file systems.");
-
- fd = setup_machine_raw(size, error);
- if (fd < 0)
- return fd;
-
- control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
- if (control < 0)
- return sd_bus_error_set_errnof(error, errno, "Failed to open /dev/loop-control: %m");
-
- nr = ioctl(control, LOOP_CTL_GET_FREE);
- if (nr < 0)
- return sd_bus_error_set_errnof(error, errno, "Failed to allocate loop device: %m");
-
- if (asprintf(&loopdev, "/dev/loop%i", nr) < 0) {
- r = -ENOMEM;
- goto fail;
- }
-
- loop = open(loopdev, O_CLOEXEC|O_RDWR|O_NOCTTY|O_NONBLOCK);
- if (loop < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to open loopback device: %m");
- goto fail;
- }
-
- if (ioctl(loop, LOOP_SET_FD, fd) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to bind loopback device: %m");
- goto fail;
- }
-
- if (ioctl(loop, LOOP_SET_STATUS64, &info) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to enable auto-clear for loopback device: %m");
- goto fail;
- }
-
- /* We need to make sure the new /var/lib/machines directory
- * has an access mode of 0700 at the time it is first made
- * available. mkfs will create it with 0755 however. Hence,
- * let's mount the directory into an inaccessible directory
- * below /tmp first, fix the access mode, and move it to the
- * public place then. */
-
- if (!mkdtemp(tmpdir)) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to create temporary mount parent directory: %m");
- goto fail;
- }
- tmpdir_made = true;
-
- mntdir = strjoina(tmpdir, "/mnt");
- if (mkdir(mntdir, 0700) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to create temporary mount directory: %m");
- goto fail;
- }
- mntdir_made = true;
-
- if (mount(loopdev, mntdir, "btrfs", 0, NULL) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to mount loopback device: %m");
- goto fail;
- }
- mntdir_mounted = true;
-
- r = btrfs_quota_enable(mntdir, true);
- if (r < 0)
- log_warning_errno(r, "Failed to enable quota, ignoring: %m");
-
- if (chmod(mntdir, 0700) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to fix owner: %m");
- goto fail;
- }
-
- (void) mkdir_p_label("/var/lib/machines", 0700);
-
- if (mount(mntdir, "/var/lib/machines", NULL, MS_BIND, NULL) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to mount directory into right place: %m");
- goto fail;
- }
-
- (void) syncfs(fd);
-
- log_info("Set up /var/lib/machines as btrfs loopback file system of size %s mounted on /var/lib/machines.raw.", format_bytes(buf, sizeof(buf), size));
-
- (void) umount2(mntdir, MNT_DETACH);
- (void) rmdir(mntdir);
- (void) rmdir(tmpdir);
-
- return 0;
-
-fail:
- if (mntdir_mounted)
- (void) umount2(mntdir, MNT_DETACH);
-
- if (mntdir_made)
- (void) rmdir(mntdir);
- if (tmpdir_made)
- (void) rmdir(tmpdir);
-
- if (loop >= 0) {
- (void) ioctl(loop, LOOP_CLR_FD);
- loop = safe_close(loop);
- }
-
- if (control >= 0 && nr >= 0)
- (void) ioctl(control, LOOP_CTL_REMOVE, nr);
-
- return r;
-}
-
-static int sync_path(const char *p) {
- _cleanup_close_ int fd = -1;
-
- fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
- return -errno;
-
- if (syncfs(fd) < 0)
- return -errno;
-
- return 0;
-}
-
-int grow_machine_directory(void) {
- char buf[FORMAT_BYTES_MAX];
- struct statvfs a, b;
- uint64_t old_size, new_size, max_add;
- int r;
-
- /* Ensure the disk space data is accurate */
- sync_path("/var/lib/machines");
- sync_path("/var/lib/machines.raw");
-
- if (statvfs("/var/lib/machines.raw", &a) < 0)
- return -errno;
-
- if (statvfs("/var/lib/machines", &b) < 0)
- return -errno;
-
- /* Don't grow if not enough disk space is available on the host */
- if (((uint64_t) a.f_bavail * (uint64_t) a.f_bsize) <= VAR_LIB_MACHINES_FREE_MIN)
- return 0;
-
- /* Don't grow if at least 1/3th of the fs is still free */
- if (b.f_bavail > b.f_blocks / 3)
- return 0;
-
- /* Calculate how much we are willing to add at maximum */
- max_add = ((uint64_t) a.f_bavail * (uint64_t) a.f_bsize) - VAR_LIB_MACHINES_FREE_MIN;
-
- /* Calculate the old size */
- old_size = (uint64_t) b.f_blocks * (uint64_t) b.f_bsize;
-
- /* Calculate the new size as three times the size of what is used right now */
- new_size = ((uint64_t) b.f_blocks - (uint64_t) b.f_bavail) * (uint64_t) b.f_bsize * 3;
-
- /* Always, grow at least to the start size */
- if (new_size < VAR_LIB_MACHINES_SIZE_START)
- new_size = VAR_LIB_MACHINES_SIZE_START;
-
- /* If the new size is smaller than the old size, don't grow */
- if (new_size < old_size)
- return 0;
-
- /* Ensure we never add more than the maximum */
- if (new_size > old_size + max_add)
- new_size = old_size + max_add;
-
- r = btrfs_resize_loopback("/var/lib/machines", new_size, true);
- if (r <= 0)
- return r;
-
- r = btrfs_quota_limit("/var/lib/machines", new_size);
- if (r < 0)
- return r;
-
- log_info("Grew /var/lib/machines btrfs loopback file system to %s.", format_bytes(buf, sizeof(buf), new_size));
- return 1;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <nss.h>
-#include <netdb.h>
-#include <resolv.h>
-
-#define NSS_GETHOSTBYNAME_PROTOTYPES(module) \
-enum nss_status _nss_##module##_gethostbyname4_r( \
- const char *name, \
- struct gaih_addrtuple **pat, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop, \
- int32_t *ttlp) _public_; \
-enum nss_status _nss_##module##_gethostbyname3_r( \
- const char *name, \
- int af, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop, \
- int32_t *ttlp, \
- char **canonp) _public_; \
-enum nss_status _nss_##module##_gethostbyname2_r( \
- const char *name, \
- int af, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop) _public_; \
-enum nss_status _nss_##module##_gethostbyname_r( \
- const char *name, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop) _public_
-
-#define NSS_GETHOSTBYADDR_PROTOTYPES(module) \
-enum nss_status _nss_##module##_gethostbyaddr2_r( \
- const void* addr, socklen_t len, \
- int af, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop, \
- int32_t *ttlp) _public_; \
-enum nss_status _nss_##module##_gethostbyaddr_r( \
- const void* addr, socklen_t len, \
- int af, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop) _public_
-
-#define NSS_GETHOSTBYNAME_FALLBACKS(module) \
-enum nss_status _nss_##module##_gethostbyname2_r( \
- const char *name, \
- int af, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop) { \
- return _nss_##module##_gethostbyname3_r( \
- name, \
- af, \
- host, \
- buffer, buflen, \
- errnop, h_errnop, \
- NULL, \
- NULL); \
-} \
-enum nss_status _nss_##module##_gethostbyname_r( \
- const char *name, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop) { \
- enum nss_status ret = NSS_STATUS_NOTFOUND; \
- \
- if (_res.options & RES_USE_INET6) \
- ret = _nss_##module##_gethostbyname3_r( \
- name, \
- AF_INET6, \
- host, \
- buffer, buflen, \
- errnop, h_errnop, \
- NULL, \
- NULL); \
- if (ret == NSS_STATUS_NOTFOUND) \
- ret = _nss_##module##_gethostbyname3_r( \
- name, \
- AF_INET, \
- host, \
- buffer, buflen, \
- errnop, h_errnop, \
- NULL, \
- NULL); \
- return ret; \
-}
-
-#define NSS_GETHOSTBYADDR_FALLBACKS(module) \
-enum nss_status _nss_##module##_gethostbyaddr_r( \
- const void* addr, socklen_t len, \
- int af, \
- struct hostent *host, \
- char *buffer, size_t buflen, \
- int *errnop, int *h_errnop) { \
- return _nss_##module##_gethostbyaddr2_r( \
- addr, len, \
- af, \
- host, \
- buffer, buflen, \
- errnop, h_errnop, \
- NULL); \
-}
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+/// UNNEEDED by elogind
+#if 0
typedef enum OutputMode {
OUTPUT_SHORT,
OUTPUT_SHORT_ISO,
_OUTPUT_MODE_MAX,
_OUTPUT_MODE_INVALID = -1
} OutputMode;
+#endif // 0
typedef enum OutputFlags {
OUTPUT_SHOW_ALL = 1 << 0,
#include "pager.h"
#include "util.h"
+#include "process-util.h"
#include "macro.h"
+#include "terminal-util.h"
+#include "signal-util.h"
+#include "copy.h"
static pid_t pager_pid = 0;
noreturn static void pager_fallback(void) {
- ssize_t n;
-
- do {
- n = splice(STDIN_FILENO, NULL, STDOUT_FILENO, NULL, 64*1024, 0);
- } while (n > 0);
+ int r;
- if (n < 0) {
- log_error_errno(errno, "Internal pager failed: %m");
+ r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (off_t) -1, false);
+ if (r < 0) {
+ log_error_errno(r, "Internal pager failed: %m");
_exit(EXIT_FAILURE);
}
/* In the child start the pager */
if (pager_pid == 0) {
- const char* less_opts;
+ const char* less_opts, *less_charset;
+
+ (void) reset_all_signal_handlers();
+ (void) reset_signal_mask();
dup2(fd[0], STDIN_FILENO);
safe_close_pair(fd);
+ /* Initialize a good set of less options */
less_opts = getenv("SYSTEMD_LESS");
if (!less_opts)
less_opts = "FRSXMK";
less_opts = strjoina(less_opts, " +G");
setenv("LESS", less_opts, 1);
+ /* Initialize a good charset for less. This is
+ * particularly important if we output UTF-8
+ * characters. */
+ less_charset = getenv("SYSTEMD_LESSCHARSET");
+ if (!less_charset && is_locale_utf8())
+ less_charset = "utf-8";
+ if (less_charset)
+ setenv("LESSCHARSET", less_charset, 1);
+
/* Make sure the pager goes away when the parent dies */
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
_exit(EXIT_FAILURE);
/* Return in the parent */
if (dup2(fd[1], STDOUT_FILENO) < 0)
return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
+ if (dup2(fd[1], STDERR_FILENO) < 0)
+ return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
safe_close_pair(fd);
return 1;
/* Inform pager that we are done */
fclose(stdout);
+#if defined(__GLIBC__)
+ stdout = NULL;
+#endif // in musl-libc this is a const
+
+ fclose(stderr);
+#if defined(__GLIBC__)
+ stderr = NULL;
+#endif // in musl-libc this is a const
+
kill(pager_pid, SIGCONT);
(void) wait_for_terminate(pager_pid, NULL);
pager_pid = 0;
return pager_pid > 0;
}
+/// UNNEEDED by elogind
+#if 0
int show_man_page(const char *desc, bool null_stdio) {
const char *args[4] = { "man", NULL, NULL, NULL };
char *e = NULL;
if (pid == 0) {
/* Child */
+
+ (void) reset_all_signal_handlers();
+ (void) reset_signal_mask();
+
if (null_stdio) {
r = make_null_stdio();
if (r < 0) {
log_debug("Exit code %i status %i", status.si_code, status.si_status);
return status.si_status;
}
+#endif // 0
void pager_close(void);
bool pager_have(void) _pure_;
-int show_man_page(const char *page, bool null_stdio);
+// UNNEEDED int show_man_page(const char *page, bool null_stdio);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-
-#include "util.h"
-#include "strv.h"
-#include "path-util.h"
-#include "path-lookup.h"
-#include "install.h"
-
-int user_config_home(char **config_home) {
- const char *e;
- char *r;
-
- e = getenv("XDG_CONFIG_HOME");
- if (e) {
- r = strappend(e, "/systemd/user");
- if (!r)
- return -ENOMEM;
-
- *config_home = r;
- return 1;
- } else {
- const char *home;
-
- home = getenv("HOME");
- if (home) {
- r = strappend(home, "/.config/systemd/user");
- if (!r)
- return -ENOMEM;
-
- *config_home = r;
- return 1;
- }
- }
-
- return 0;
-}
-
-int user_runtime_dir(char **runtime_dir) {
- const char *e;
- char *r;
-
- e = getenv("XDG_RUNTIME_DIR");
- if (e) {
- r = strappend(e, "/systemd/user");
- if (!r)
- return -ENOMEM;
-
- *runtime_dir = r;
- return 1;
- }
-
- return 0;
-}
-
-static int user_data_home_dir(char **dir, const char *suffix) {
- const char *e;
- char *res;
-
- /* We don't treat /etc/xdg/systemd here as the spec
- * suggests because we assume that that is a link to
- * /etc/systemd/ anyway. */
-
- e = getenv("XDG_DATA_HOME");
- if (e)
- res = strappend(e, suffix);
- else {
- const char *home;
-
- home = getenv("HOME");
- if (home)
- res = strjoin(home, "/.local/share", suffix, NULL);
- else
- return 0;
- }
- if (!res)
- return -ENOMEM;
-
- *dir = res;
- return 0;
-}
-
-static char** user_dirs(
- const char *generator,
- const char *generator_early,
- const char *generator_late) {
-
- const char * const config_unit_paths[] = {
- USER_CONFIG_UNIT_PATH,
- "/etc/systemd/user",
- NULL
- };
-
- const char * const runtime_unit_path = "/run/systemd/user";
-
- const char * const data_unit_paths[] = {
- "/usr/local/lib/systemd/user",
- "/usr/local/share/systemd/user",
- USER_DATA_UNIT_PATH,
- "/usr/lib/systemd/user",
- "/usr/share/systemd/user",
- NULL
- };
-
- const char *e;
- _cleanup_free_ char *config_home = NULL, *runtime_dir = NULL, *data_home = NULL;
- _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
- _cleanup_free_ char **res = NULL;
- char **tmp;
- int r;
-
- /* Implement the mechanisms defined in
- *
- * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
- *
- * We look in both the config and the data dirs because we
- * want to encourage that distributors ship their unit files
- * as data, and allow overriding as configuration.
- */
-
- if (user_config_home(&config_home) < 0)
- return NULL;
-
- if (user_runtime_dir(&runtime_dir) < 0)
- return NULL;
-
- e = getenv("XDG_CONFIG_DIRS");
- if (e) {
- config_dirs = strv_split(e, ":");
- if (!config_dirs)
- return NULL;
- }
-
- r = user_data_home_dir(&data_home, "/systemd/user");
- if (r < 0)
- return NULL;
-
- e = getenv("XDG_DATA_DIRS");
- if (e)
- data_dirs = strv_split(e, ":");
- else
- data_dirs = strv_new("/usr/local/share",
- "/usr/share",
- NULL);
- if (!data_dirs)
- return NULL;
-
- /* Now merge everything we found. */
- if (generator_early)
- if (strv_extend(&res, generator_early) < 0)
- return NULL;
-
- if (config_home)
- if (strv_extend(&res, config_home) < 0)
- return NULL;
-
- if (!strv_isempty(config_dirs))
- if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
- return NULL;
-
- if (strv_extend_strv(&res, (char**) config_unit_paths) < 0)
- return NULL;
-
- if (runtime_dir)
- if (strv_extend(&res, runtime_dir) < 0)
- return NULL;
-
- if (strv_extend(&res, runtime_unit_path) < 0)
- return NULL;
-
- if (generator)
- if (strv_extend(&res, generator) < 0)
- return NULL;
-
- if (data_home)
- if (strv_extend(&res, data_home) < 0)
- return NULL;
-
- if (!strv_isempty(data_dirs))
- if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
- return NULL;
-
- if (strv_extend_strv(&res, (char**) data_unit_paths) < 0)
- return NULL;
-
- if (generator_late)
- if (strv_extend(&res, generator_late) < 0)
- return NULL;
-
- if (!path_strv_make_absolute_cwd(res))
- return NULL;
-
- tmp = res;
- res = NULL;
- return tmp;
-}
-
-int lookup_paths_init(
- LookupPaths *p,
- SystemdRunningAs running_as,
- bool personal,
- const char *root_dir,
- const char *generator,
- const char *generator_early,
- const char *generator_late) {
-
- const char *e;
- bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */
-
- assert(p);
-
- /* First priority is whatever has been passed to us via env
- * vars */
- e = getenv("SYSTEMD_UNIT_PATH");
- if (e) {
- if (endswith(e, ":")) {
- e = strndupa(e, strlen(e) - 1);
- append = true;
- }
-
- /* FIXME: empty components in other places should be
- * rejected. */
-
- p->unit_path = path_split_and_make_absolute(e);
- if (!p->unit_path)
- return -ENOMEM;
- } else
- p->unit_path = NULL;
-
- if (!p->unit_path || append) {
- /* Let's figure something out. */
-
- _cleanup_strv_free_ char **unit_path;
- int r;
-
- /* For the user units we include share/ in the search
- * path in order to comply with the XDG basedir spec.
- * For the system stuff we avoid such nonsense. OTOH
- * we include /lib in the search path for the system
- * stuff but avoid it for user stuff. */
-
- if (running_as == SYSTEMD_USER) {
- if (personal)
- unit_path = user_dirs(generator, generator_early, generator_late);
- else
- unit_path = strv_new(
- /* If you modify this you also want to modify
- * systemduserunitpath= in systemd.pc.in, and
- * the arrays in user_dirs() above! */
- STRV_IFNOTNULL(generator_early),
- USER_CONFIG_UNIT_PATH,
- "/etc/systemd/user",
- "/run/systemd/user",
- STRV_IFNOTNULL(generator),
- "/usr/local/lib/systemd/user",
- "/usr/local/share/systemd/user",
- USER_DATA_UNIT_PATH,
- "/usr/lib/systemd/user",
- "/usr/share/systemd/user",
- STRV_IFNOTNULL(generator_late),
- NULL);
- } else
- unit_path = strv_new(
- /* If you modify this you also want to modify
- * systemdsystemunitpath= in systemd.pc.in! */
- STRV_IFNOTNULL(generator_early),
- SYSTEM_CONFIG_UNIT_PATH,
- "/etc/systemd/system",
- "/run/systemd/system",
- STRV_IFNOTNULL(generator),
- "/usr/local/lib/systemd/system",
- SYSTEM_DATA_UNIT_PATH,
- "/usr/lib/systemd/system",
-#ifdef HAVE_SPLIT_USR
- "/lib/systemd/system",
-#endif
- STRV_IFNOTNULL(generator_late),
- NULL);
-
- if (!unit_path)
- return -ENOMEM;
-
- r = strv_extend_strv(&p->unit_path, unit_path);
- if (r < 0)
- return r;
- }
-
- if (!path_strv_resolve_uniq(p->unit_path, root_dir))
- return -ENOMEM;
-
- if (!strv_isempty(p->unit_path)) {
- _cleanup_free_ char *t = strv_join(p->unit_path, "\n\t");
- if (!t)
- return -ENOMEM;
- log_debug("Looking for unit files in (higher priority first):\n\t%s", t);
- } else {
- log_debug("Ignoring unit files.");
- strv_free(p->unit_path);
- p->unit_path = NULL;
- }
-
- if (running_as == SYSTEMD_SYSTEM) {
- log_debug("SysV init scripts and rcN.d links support disabled");
- }
-
- return 0;
-}
-
-void lookup_paths_free(LookupPaths *p) {
- assert(p);
-
- strv_free(p->unit_path);
- p->unit_path = NULL;
-}
-
-int lookup_paths_init_from_scope(LookupPaths *paths,
- UnitFileScope scope,
- const char *root_dir) {
- assert(paths);
- assert(scope >= 0);
- assert(scope < _UNIT_FILE_SCOPE_MAX);
-
- zero(*paths);
-
- return lookup_paths_init(paths,
- scope == UNIT_FILE_SYSTEM ? SYSTEMD_SYSTEM : SYSTEMD_USER,
- scope == UNIT_FILE_USER,
- root_dir,
- NULL, NULL, NULL);
-}
#include "macro.h"
-typedef enum UnitFileScope UnitFileScope;
-
typedef struct LookupPaths {
char **unit_path;
} LookupPaths;
-typedef enum SystemdRunningAs {
- SYSTEMD_SYSTEM,
- SYSTEMD_USER,
- _SYSTEMD_RUNNING_AS_MAX,
- _SYSTEMD_RUNNING_AS_INVALID = -1
-} SystemdRunningAs;
+typedef enum ManagerRunningAs {
+ MANAGER_SYSTEM,
+ MANAGER_USER,
+ _MANAGER_RUNNING_AS_MAX,
+ _MANAGER_RUNNING_AS_INVALID = -1
+} ManagerRunningAs;
+/// UNNEEDED by elogind
+#if 0
int user_config_home(char **config_home);
int user_runtime_dir(char **runtime_dir);
+char **generator_paths(ManagerRunningAs running_as);
+
int lookup_paths_init(LookupPaths *p,
- SystemdRunningAs running_as,
+ ManagerRunningAs running_as,
bool personal,
const char *root_dir,
const char *generator,
const char *generator_early,
const char *generator_late);
-void lookup_paths_free(LookupPaths *p);
+
+#include "install.h"
+
int lookup_paths_init_from_scope(LookupPaths *paths,
UnitFileScope scope,
const char *root_dir);
+void lookup_paths_free(LookupPaths *p);
#define _cleanup_lookup_paths_free_ _cleanup_(lookup_paths_free)
+#endif // 0
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-/*
- * PTY
- * A PTY object represents a single PTY connection between a master and a
- * child. The child process is fork()ed so the caller controls what program
- * will be run.
- *
- * Programs like /bin/login tend to perform a vhangup() on their TTY
- * before running the login procedure. This also causes the pty master
- * to get a EPOLLHUP event as long as no client has the TTY opened.
- * This means, we cannot use the TTY connection as reliable way to track
- * the client. Instead, we _must_ rely on the PID of the client to track
- * them.
- * However, this has the side effect that if the client forks and the
- * parent exits, we loose them and restart the client. But this seems to
- * be the expected behavior so we implement it here.
- *
- * Unfortunately, epoll always polls for EPOLLHUP so as long as the
- * vhangup() is ongoing, we will _always_ get EPOLLHUP and cannot sleep.
- * This gets worse if the client closes the TTY but doesn't exit.
- * Therefore, the fd must be edge-triggered in the epoll-set so we
- * only get the events once they change.
- */
-
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <sys/epoll.h>
-#include <sys/ioctl.h>
-#include <sys/uio.h>
-#include <sys/wait.h>
-#include <termios.h>
-#include <unistd.h>
-
-#include "barrier.h"
-#include "macro.h"
-#include "pty.h"
-#include "ring.h"
-#include "util.h"
-
-#define PTY_BUFSIZE 4096
-
-enum {
- PTY_ROLE_UNKNOWN,
- PTY_ROLE_PARENT,
- PTY_ROLE_CHILD,
-};
-
-struct Pty {
- unsigned long ref;
- Barrier barrier;
- int fd;
- pid_t child;
- sd_event_source *fd_source;
- sd_event_source *child_source;
-
- char in_buf[PTY_BUFSIZE];
- Ring out_buf;
-
- pty_event_t event_fn;
- void *event_fn_userdata;
-
- bool needs_requeue : 1;
- unsigned int role : 2;
-};
-
-int pty_new(Pty **out) {
- _pty_unref_ Pty *pty = NULL;
- int r;
-
- assert_return(out, -EINVAL);
-
- pty = new0(Pty, 1);
- if (!pty)
- return -ENOMEM;
-
- pty->ref = 1;
- pty->fd = -1;
- pty->barrier = (Barrier) BARRIER_NULL;
-
- pty->fd = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC | O_NONBLOCK);
- if (pty->fd < 0)
- return -errno;
-
- /*
- * The slave-node is initialized to uid/gid of the caller of
- * posix_openpt(). Only if devpts is mounted with fixed uid/gid this is
- * skipped. In that case, grantpt() can overwrite these, but then you
- * have to be root to use chown() (or a pt_chown helper has to be
- * present). In those cases grantpt() really does something,
- * otherwise it's a no-op. We call grantpt() here to try supporting
- * those cases, even though no-one uses that, I guess. If you need other
- * access-rights, set them yourself after this call returns (no, this is
- * not racy, it looks racy, but races regarding your own UID are never
- * important as an attacker could ptrace you; and the slave-pty is also
- * still locked).
- */
- r = grantpt(pty->fd);
- if (r < 0)
- return -errno;
-
- r = barrier_create(&pty->barrier);
- if (r < 0)
- return r;
-
- *out = pty;
- pty = NULL;
- return 0;
-}
-
-Pty *pty_ref(Pty *pty) {
- if (!pty || pty->ref < 1)
- return NULL;
-
- ++pty->ref;
- return pty;
-}
-
-Pty *pty_unref(Pty *pty) {
- if (!pty || pty->ref < 1 || --pty->ref > 0)
- return NULL;
-
- pty_close(pty);
- pty->child_source = sd_event_source_unref(pty->child_source);
- barrier_destroy(&pty->barrier);
- ring_clear(&pty->out_buf);
- free(pty);
-
- return NULL;
-}
-
-Barrier *pty_get_barrier(Pty *pty) {
- assert(pty);
- return &pty->barrier;
-}
-
-bool pty_is_unknown(Pty *pty) {
- return pty && pty->role == PTY_ROLE_UNKNOWN;
-}
-
-bool pty_is_parent(Pty *pty) {
- return pty && pty->role == PTY_ROLE_PARENT;
-}
-
-bool pty_is_child(Pty *pty) {
- return pty && pty->role == PTY_ROLE_CHILD;
-}
-
-bool pty_has_child(Pty *pty) {
- return pty_is_parent(pty) && pty->child > 0;
-}
-
-pid_t pty_get_child(Pty *pty) {
- return pty_has_child(pty) ? pty->child : -ECHILD;
-}
-
-bool pty_is_open(Pty *pty) {
- return pty && pty->fd >= 0;
-}
-
-int pty_get_fd(Pty *pty) {
- assert_return(pty, -EINVAL);
-
- return pty_is_open(pty) ? pty->fd : -EPIPE;
-}
-
-int pty_make_child(Pty *pty) {
- _cleanup_free_ char *slave_name = NULL;
- int r, fd;
-
- assert_return(pty, -EINVAL);
- assert_return(pty_is_unknown(pty), -EALREADY);
-
- r = ptsname_malloc(pty->fd, &slave_name);
- if (r < 0)
- return -errno;
-
- fd = open(slave_name, O_RDWR | O_CLOEXEC | O_NOCTTY);
- if (fd < 0)
- return -errno;
-
- safe_close(pty->fd);
- pty->fd = fd;
- pty->child = getpid();
- pty->role = PTY_ROLE_CHILD;
- barrier_set_role(&pty->barrier, BARRIER_CHILD);
-
- return 0;
-}
-
-int pty_make_parent(Pty *pty, pid_t child) {
- assert_return(pty, -EINVAL);
- assert_return(pty_is_unknown(pty), -EALREADY);
-
- pty->child = child;
- pty->role = PTY_ROLE_PARENT;
-
- return 0;
-}
-
-int pty_unlock(Pty *pty) {
- assert_return(pty, -EINVAL);
- assert_return(pty_is_unknown(pty) || pty_is_parent(pty), -EINVAL);
- assert_return(pty_is_open(pty), -ENODEV);
-
- return unlockpt(pty->fd) < 0 ? -errno : 0;
-}
-
-int pty_setup_child(Pty *pty) {
- struct termios attr;
- pid_t pid;
- int r;
-
- assert_return(pty, -EINVAL);
- assert_return(pty_is_child(pty), -EINVAL);
- assert_return(pty_is_open(pty), -EALREADY);
-
- r = sigprocmask_many(SIG_SETMASK, -1);
- if (r < 0)
- return r;
-
- r = reset_all_signal_handlers();
- if (r < 0)
- return r;
-
- pid = setsid();
- if (pid < 0 && errno != EPERM)
- return -errno;
-
- r = ioctl(pty->fd, TIOCSCTTY, 0);
- if (r < 0)
- return -errno;
-
- r = tcgetattr(pty->fd, &attr);
- if (r < 0)
- return -errno;
-
- /* erase character should be normal backspace, PLEASEEE! */
- attr.c_cc[VERASE] = 010;
- /* always set UTF8 flag */
- attr.c_iflag |= IUTF8;
-
- r = tcsetattr(pty->fd, TCSANOW, &attr);
- if (r < 0)
- return -errno;
-
- if (dup2(pty->fd, STDIN_FILENO) != STDIN_FILENO ||
- dup2(pty->fd, STDOUT_FILENO) != STDOUT_FILENO ||
- dup2(pty->fd, STDERR_FILENO) != STDERR_FILENO)
- return -errno;
-
- /* only close FD if it's not a std-fd */
- pty->fd = (pty->fd > 2) ? safe_close(pty->fd) : -1;
-
- return 0;
-}
-
-void pty_close(Pty *pty) {
- if (!pty_is_open(pty))
- return;
-
- pty->fd_source = sd_event_source_unref(pty->fd_source);
- pty->fd = safe_close(pty->fd);
-}
-
-/*
- * Drain input-queue and dispatch data via the event-handler. Returns <0 on
- * error, 0 if queue is empty and 1 if we couldn't empty the input queue fast
- * enough and there's still data left.
- */
-static int pty_dispatch_read(Pty *pty) {
- unsigned int i;
- ssize_t len;
- int r;
-
- /*
- * We're edge-triggered, means we need to read the whole queue. This,
- * however, might cause us to stall if the writer is faster than we
- * are. Therefore, try reading as much as 8 times (32KiB) and only
- * bail out then.
- */
-
- for (i = 0; i < 8; ++i) {
- len = read(pty->fd, pty->in_buf, sizeof(pty->in_buf) - 1);
- if (len < 0) {
- if (errno == EINTR)
- continue;
-
- return (errno == EAGAIN) ? 0 : -errno;
- } else if (len == 0) {
- continue;
- }
-
- /* set terminating zero for debugging safety */
- pty->in_buf[len] = 0;
- r = pty->event_fn(pty, pty->event_fn_userdata, PTY_DATA, pty->in_buf, len);
- if (r < 0)
- return r;
- }
-
- /* still data left, make sure we're queued again */
- pty->needs_requeue = true;
-
- return 1;
-}
-
-/*
- * Drain output-queue by writing data to the pty. Returns <0 on error, 0 if the
- * output queue is empty now and 1 if we couldn't empty the output queue fast
- * enough and there's still data left.
- */
-static int pty_dispatch_write(Pty *pty) {
- struct iovec vec[2];
- unsigned int i;
- ssize_t len;
- size_t num;
-
- /*
- * Same as pty_dispatch_read(), we're edge-triggered so we need to call
- * write() until either all data is written or it returns EAGAIN. We
- * call it twice and if it still writes successfully, we reschedule.
- */
-
- for (i = 0; i < 2; ++i) {
- num = ring_peek(&pty->out_buf, vec);
- if (num < 1)
- return 0;
-
- len = writev(pty->fd, vec, (int)num);
- if (len < 0) {
- if (errno == EINTR)
- continue;
-
- return (errno == EAGAIN) ? 1 : -errno;
- } else if (len == 0) {
- continue;
- }
-
- ring_pull(&pty->out_buf, (size_t)len);
- }
-
- /* still data left, make sure we're queued again */
- if (ring_get_size(&pty->out_buf) > 0) {
- pty->needs_requeue = true;
- return 1;
- }
-
- return 0;
-}
-
-static int pty_fd_fn(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
- Pty *pty = userdata;
- int r_hup = 0, r_write = 0, r_read = 0, r;
-
- /*
- * Whenever we encounter I/O errors, we have to make sure to drain the
- * input queue first, before we handle any HUP. A child might send us
- * a message and immediately close the queue. We must not handle the
- * HUP first or we loose data.
- * Therefore, if we read a message successfully, we always return
- * success and wait for the next event-loop iteration. Furthermore,
- * whenever there is a write-error, we must try reading from the input
- * queue even if EPOLLIN is not set. The input might have arrived in
- * between epoll_wait() and write(). Therefore, write-errors are only
- * ever handled if the input-queue is empty. In all other cases they
- * are ignored until either reading fails or the input queue is empty.
- */
-
- if (revents & (EPOLLHUP | EPOLLERR))
- r_hup = -EPIPE;
-
- if (revents & EPOLLOUT)
- r_write = pty_dispatch_write(pty);
-
- /* Awesome! Kernel signals HUP without IN but queues are not empty.. */
- if ((revents & EPOLLIN) || r_hup < 0 || r_write < 0) {
- r_read = pty_dispatch_read(pty);
- if (r_read > 0)
- return 0; /* still data left to fetch next round */
- }
-
- if (r_hup < 0 || r_write < 0 || r_read < 0) {
- /* PTY closed and input-queue drained */
- pty_close(pty);
- r = pty->event_fn(pty, pty->event_fn_userdata, PTY_HUP, NULL, 0);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-static int pty_fd_prepare_fn(sd_event_source *source, void *userdata) {
- Pty *pty = userdata;
- int r;
-
- if (pty->needs_requeue) {
- /*
- * We're edge-triggered. In case we couldn't handle all events
- * or in case new write-data is queued, we set needs_requeue.
- * Before going asleep, we set the io-events *again*. sd-event
- * notices that we're edge-triggered and forwards the call to
- * the kernel even if the events didn't change. The kernel will
- * check the events and re-queue us on the ready queue in case
- * an event is pending.
- */
- r = sd_event_source_set_io_events(source, EPOLLHUP | EPOLLERR | EPOLLIN | EPOLLOUT | EPOLLET);
- if (r >= 0)
- pty->needs_requeue = false;
- }
-
- return 0;
-}
-
-static int pty_child_fn(sd_event_source *source, const siginfo_t *si, void *userdata) {
- Pty *pty = userdata;
- int r;
-
- pty->child = 0;
-
- r = pty->event_fn(pty, pty->event_fn_userdata, PTY_CHILD, si, sizeof(*si));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int pty_attach_event(Pty *pty, sd_event *event, pty_event_t event_fn, void *event_fn_userdata) {
- int r;
-
- assert_return(pty, -EINVAL);
- assert_return(event, -EINVAL);
- assert_return(event_fn, -EINVAL);
- assert_return(pty_is_parent(pty), -EINVAL);
-
- pty_detach_event(pty);
-
- if (pty_is_open(pty)) {
- r = sd_event_add_io(event,
- &pty->fd_source,
- pty->fd,
- EPOLLHUP | EPOLLERR | EPOLLIN | EPOLLOUT | EPOLLET,
- pty_fd_fn,
- pty);
- if (r < 0)
- goto error;
-
- r = sd_event_source_set_prepare(pty->fd_source, pty_fd_prepare_fn);
- if (r < 0)
- goto error;
- }
-
- if (pty_has_child(pty)) {
- r = sd_event_add_child(event,
- &pty->child_source,
- pty->child,
- WEXITED,
- pty_child_fn,
- pty);
- if (r < 0)
- goto error;
- }
-
- pty->event_fn = event_fn;
- pty->event_fn_userdata = event_fn_userdata;
-
- return 0;
-
-error:
- pty_detach_event(pty);
- return r;
-}
-
-void pty_detach_event(Pty *pty) {
- if (!pty)
- return;
-
- pty->child_source = sd_event_source_unref(pty->child_source);
- pty->fd_source = sd_event_source_unref(pty->fd_source);
- pty->event_fn = NULL;
- pty->event_fn_userdata = NULL;
-}
-
-int pty_write(Pty *pty, const void *buf, size_t size) {
- bool was_empty;
- int r;
-
- assert_return(pty, -EINVAL);
- assert_return(pty_is_open(pty), -ENODEV);
- assert_return(pty_is_parent(pty), -ENODEV);
-
- if (size < 1)
- return 0;
-
- /*
- * Push @buf[0..@size] into the output ring-buffer. In case the
- * ring-buffer wasn't empty beforehand, we're already waiting for
- * EPOLLOUT and we're done. If it was empty, we have to re-queue the
- * FD for EPOLLOUT as we're edge-triggered and wouldn't get any new
- * EPOLLOUT event.
- */
-
- was_empty = ring_get_size(&pty->out_buf) < 1;
-
- r = ring_push(&pty->out_buf, buf, size);
- if (r < 0)
- return r;
-
- if (was_empty)
- pty->needs_requeue = true;
-
- return 0;
-}
-
-int pty_signal(Pty *pty, int sig) {
- assert_return(pty, -EINVAL);
- assert_return(pty_is_open(pty), -ENODEV);
- assert_return(pty_is_parent(pty), -ENODEV);
-
- return ioctl(pty->fd, TIOCSIG, sig) < 0 ? -errno : 0;
-}
-
-int pty_resize(Pty *pty, unsigned short term_width, unsigned short term_height) {
- struct winsize ws = {
- .ws_col = term_width,
- .ws_row = term_height,
- };
-
- assert_return(pty, -EINVAL);
- assert_return(pty_is_open(pty), -ENODEV);
- assert_return(pty_is_parent(pty), -ENODEV);
-
- /*
- * This will send SIGWINCH to the pty slave foreground process group.
- * We will also get one, but we don't need it.
- */
- return ioctl(pty->fd, TIOCSWINSZ, &ws) < 0 ? -errno : 0;
-}
-
-pid_t pty_fork(Pty **out, sd_event *event, pty_event_t event_fn, void *event_fn_userdata, unsigned short initial_term_width, unsigned short initial_term_height) {
- _pty_unref_ Pty *pty = NULL;
- int r;
- pid_t pid;
-
- assert_return(out, -EINVAL);
- assert_return((event && event_fn) || (!event && !event_fn), -EINVAL);
-
- r = pty_new(&pty);
- if (r < 0)
- return r;
-
- r = pty_unlock(pty);
- if (r < 0)
- return r;
-
- pid = fork();
- if (pid < 0)
- return -errno;
-
- if (pid == 0) {
- /* child */
-
- r = pty_make_child(pty);
- if (r < 0)
- _exit(-r);
-
- r = pty_setup_child(pty);
- if (r < 0)
- _exit(-r);
-
- /* sync with parent */
- if (!barrier_place_and_sync(&pty->barrier))
- _exit(1);
-
- /* fallthrough and return the child's PTY object */
- } else {
- /* parent */
-
- r = pty_make_parent(pty, pid);
- if (r < 0)
- goto parent_error;
-
- r = pty_resize(pty, initial_term_width, initial_term_height);
- if (r < 0)
- goto parent_error;
-
- if (event) {
- r = pty_attach_event(pty, event, event_fn, event_fn_userdata);
- if (r < 0)
- goto parent_error;
- }
-
- /* sync with child */
- if (!barrier_place_and_sync(&pty->barrier)) {
- r = -ECHILD;
- goto parent_error;
- }
-
- /* fallthrough and return the parent's PTY object */
- }
-
- *out = pty;
- pty = NULL;
- return pid;
-
-parent_error:
- barrier_abort(&pty->barrier);
- waitpid(pty->child, NULL, 0);
- pty->child = 0;
- return r;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <unistd.h>
-
-#include "barrier.h"
-#include "macro.h"
-#include "sd-event.h"
-
-typedef struct Pty Pty;
-
-enum {
- PTY_CHILD,
- PTY_HUP,
- PTY_DATA,
-};
-
-typedef int (*pty_event_t) (Pty *pty, void *userdata, unsigned int event, const void *ptr, size_t size);
-
-int pty_new(Pty **out);
-Pty *pty_ref(Pty *pty);
-Pty *pty_unref(Pty *pty);
-
-#define _pty_unref_ _cleanup_(pty_unrefp)
-DEFINE_TRIVIAL_CLEANUP_FUNC(Pty*, pty_unref);
-
-Barrier *pty_get_barrier(Pty *pty);
-
-bool pty_is_unknown(Pty *pty);
-bool pty_is_parent(Pty *pty);
-bool pty_is_child(Pty *pty);
-bool pty_has_child(Pty *pty);
-pid_t pty_get_child(Pty *pty);
-
-bool pty_is_open(Pty *pty);
-int pty_get_fd(Pty *pty);
-
-int pty_make_child(Pty *pty);
-int pty_make_parent(Pty *pty, pid_t child);
-int pty_unlock(Pty *pty);
-int pty_setup_child(Pty *pty);
-void pty_close(Pty *pty);
-
-int pty_attach_event(Pty *pty, sd_event *event, pty_event_t event_fn, void *event_fn_userdata);
-void pty_detach_event(Pty *pty);
-
-int pty_write(Pty *pty, const void *buf, size_t size);
-int pty_signal(Pty *pty, int sig);
-int pty_resize(Pty *pty, unsigned short term_width, unsigned short term_height);
-
-pid_t pty_fork(Pty **out, sd_event *event, pty_event_t event_fn, void *event_fn_userdata, unsigned short initial_term_width, unsigned short initial_term_height);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010-2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/epoll.h>
-#include <sys/ioctl.h>
-#include <limits.h>
-#include <termios.h>
-
-#include "util.h"
-#include "ptyfwd.h"
-
-struct PTYForward {
- sd_event *event;
-
- int master;
-
- sd_event_source *stdin_event_source;
- sd_event_source *stdout_event_source;
- sd_event_source *master_event_source;
-
- sd_event_source *sigwinch_event_source;
-
- struct termios saved_stdin_attr;
- struct termios saved_stdout_attr;
-
- bool read_only:1;
-
- bool saved_stdin:1;
- bool saved_stdout:1;
-
- bool stdin_readable:1;
- bool stdin_hangup:1;
- bool stdout_writable:1;
- bool stdout_hangup:1;
- bool master_readable:1;
- bool master_writable:1;
- bool master_hangup:1;
-
- /* Continue reading after hangup? */
- bool ignore_vhangup:1;
-
- bool last_char_set:1;
- char last_char;
-
- char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
- size_t in_buffer_full, out_buffer_full;
-
- usec_t escape_timestamp;
- unsigned escape_counter;
-};
-
-#define ESCAPE_USEC (1*USEC_PER_SEC)
-
-static bool look_for_escape(PTYForward *f, const char *buffer, size_t n) {
- const char *p;
-
- assert(f);
- assert(buffer);
- assert(n > 0);
-
- for (p = buffer; p < buffer + n; p++) {
-
- /* Check for ^] */
- if (*p == 0x1D) {
- usec_t nw = now(CLOCK_MONOTONIC);
-
- if (f->escape_counter == 0 || nw > f->escape_timestamp + ESCAPE_USEC) {
- f->escape_timestamp = nw;
- f->escape_counter = 1;
- } else {
- (f->escape_counter)++;
-
- if (f->escape_counter >= 3)
- return true;
- }
- } else {
- f->escape_timestamp = 0;
- f->escape_counter = 0;
- }
- }
-
- return false;
-}
-
-static int shovel(PTYForward *f) {
- ssize_t k;
-
- assert(f);
-
- while ((f->stdin_readable && f->in_buffer_full <= 0) ||
- (f->master_writable && f->in_buffer_full > 0) ||
- (f->master_readable && f->out_buffer_full <= 0) ||
- (f->stdout_writable && f->out_buffer_full > 0)) {
-
- if (f->stdin_readable && f->in_buffer_full < LINE_MAX) {
-
- k = read(STDIN_FILENO, f->in_buffer + f->in_buffer_full, LINE_MAX - f->in_buffer_full);
- if (k < 0) {
-
- if (errno == EAGAIN)
- f->stdin_readable = false;
- else if (errno == EIO || errno == EPIPE || errno == ECONNRESET) {
- f->stdin_readable = false;
- f->stdin_hangup = true;
-
- f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
- } else {
- log_error_errno(errno, "read(): %m");
- return sd_event_exit(f->event, EXIT_FAILURE);
- }
- } else if (k == 0) {
- /* EOF on stdin */
- f->stdin_readable = false;
- f->stdin_hangup = true;
-
- f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
- } else {
- /* Check if ^] has been
- * pressed three times within
- * one second. If we get this
- * we quite immediately. */
- if (look_for_escape(f, f->in_buffer + f->in_buffer_full, k))
- return sd_event_exit(f->event, EXIT_FAILURE);
-
- f->in_buffer_full += (size_t) k;
- }
- }
-
- if (f->master_writable && f->in_buffer_full > 0) {
-
- k = write(f->master, f->in_buffer, f->in_buffer_full);
- if (k < 0) {
-
- if (errno == EAGAIN || errno == EIO)
- f->master_writable = false;
- else if (errno == EPIPE || errno == ECONNRESET) {
- f->master_writable = f->master_readable = false;
- f->master_hangup = true;
-
- f->master_event_source = sd_event_source_unref(f->master_event_source);
- } else {
- log_error_errno(errno, "write(): %m");
- return sd_event_exit(f->event, EXIT_FAILURE);
- }
- } else {
- assert(f->in_buffer_full >= (size_t) k);
- memmove(f->in_buffer, f->in_buffer + k, f->in_buffer_full - k);
- f->in_buffer_full -= k;
- }
- }
-
- if (f->master_readable && f->out_buffer_full < LINE_MAX) {
-
- k = read(f->master, f->out_buffer + f->out_buffer_full, LINE_MAX - f->out_buffer_full);
- if (k < 0) {
-
- /* Note that EIO on the master device
- * might be caused by vhangup() or
- * temporary closing of everything on
- * the other side, we treat it like
- * EAGAIN here and try again, unless
- * ignore_vhangup is off. */
-
- if (errno == EAGAIN || (errno == EIO && f->ignore_vhangup))
- f->master_readable = false;
- else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
- f->master_readable = f->master_writable = false;
- f->master_hangup = true;
-
- f->master_event_source = sd_event_source_unref(f->master_event_source);
- } else {
- log_error_errno(errno, "read(): %m");
- return sd_event_exit(f->event, EXIT_FAILURE);
- }
- } else
- f->out_buffer_full += (size_t) k;
- }
-
- if (f->stdout_writable && f->out_buffer_full > 0) {
-
- k = write(STDOUT_FILENO, f->out_buffer, f->out_buffer_full);
- if (k < 0) {
-
- if (errno == EAGAIN)
- f->stdout_writable = false;
- else if (errno == EIO || errno == EPIPE || errno == ECONNRESET) {
- f->stdout_writable = false;
- f->stdout_hangup = true;
- f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
- } else {
- log_error_errno(errno, "write(): %m");
- return sd_event_exit(f->event, EXIT_FAILURE);
- }
-
- } else {
-
- if (k > 0) {
- f->last_char = f->out_buffer[k-1];
- f->last_char_set = true;
- }
-
- assert(f->out_buffer_full >= (size_t) k);
- memmove(f->out_buffer, f->out_buffer + k, f->out_buffer_full - k);
- f->out_buffer_full -= k;
- }
- }
- }
-
- if (f->stdin_hangup || f->stdout_hangup || f->master_hangup) {
- /* Exit the loop if any side hung up and if there's
- * nothing more to write or nothing we could write. */
-
- if ((f->out_buffer_full <= 0 || f->stdout_hangup) &&
- (f->in_buffer_full <= 0 || f->master_hangup))
- return sd_event_exit(f->event, EXIT_SUCCESS);
- }
-
- return 0;
-}
-
-static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
- PTYForward *f = userdata;
-
- assert(f);
- assert(e);
- assert(e == f->master_event_source);
- assert(fd >= 0);
- assert(fd == f->master);
-
- if (revents & (EPOLLIN|EPOLLHUP))
- f->master_readable = true;
-
- if (revents & (EPOLLOUT|EPOLLHUP))
- f->master_writable = true;
-
- return shovel(f);
-}
-
-static int on_stdin_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
- PTYForward *f = userdata;
-
- assert(f);
- assert(e);
- assert(e == f->stdin_event_source);
- assert(fd >= 0);
- assert(fd == STDIN_FILENO);
-
- if (revents & (EPOLLIN|EPOLLHUP))
- f->stdin_readable = true;
-
- return shovel(f);
-}
-
-static int on_stdout_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
- PTYForward *f = userdata;
-
- assert(f);
- assert(e);
- assert(e == f->stdout_event_source);
- assert(fd >= 0);
- assert(fd == STDOUT_FILENO);
-
- if (revents & (EPOLLOUT|EPOLLHUP))
- f->stdout_writable = true;
-
- return shovel(f);
-}
-
-static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *si, void *userdata) {
- PTYForward *f = userdata;
- struct winsize ws;
-
- assert(f);
- assert(e);
- assert(e == f->sigwinch_event_source);
-
- /* The window size changed, let's forward that. */
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
- (void) ioctl(f->master, TIOCSWINSZ, &ws);
-
- return 0;
-}
-
-int pty_forward_new(
- sd_event *event,
- int master,
- bool ignore_vhangup,
- bool read_only,
- PTYForward **ret) {
-
- _cleanup_(pty_forward_freep) PTYForward *f = NULL;
- struct winsize ws;
- int r;
-
- f = new0(PTYForward, 1);
- if (!f)
- return -ENOMEM;
-
- f->read_only = read_only;
- f->ignore_vhangup = ignore_vhangup;
-
- if (event)
- f->event = sd_event_ref(event);
- else {
- r = sd_event_default(&f->event);
- if (r < 0)
- return r;
- }
-
- if (!read_only) {
- r = fd_nonblock(STDIN_FILENO, true);
- if (r < 0)
- return r;
-
- r = fd_nonblock(STDOUT_FILENO, true);
- if (r < 0)
- return r;
- }
-
- r = fd_nonblock(master, true);
- if (r < 0)
- return r;
-
- f->master = master;
-
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
- (void) ioctl(master, TIOCSWINSZ, &ws);
-
- if (!read_only) {
- if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
- struct termios raw_stdin_attr;
-
- f->saved_stdin = true;
-
- raw_stdin_attr = f->saved_stdin_attr;
- cfmakeraw(&raw_stdin_attr);
- raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
- tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
- }
-
- if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
- struct termios raw_stdout_attr;
-
- f->saved_stdout = true;
-
- raw_stdout_attr = f->saved_stdout_attr;
- cfmakeraw(&raw_stdout_attr);
- raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
- raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
- tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
- }
-
- r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
- if (r < 0 && r != -EPERM)
- return r;
- }
-
- r = sd_event_add_io(f->event, &f->stdout_event_source, STDOUT_FILENO, EPOLLOUT|EPOLLET, on_stdout_event, f);
- if (r == -EPERM)
- /* stdout without epoll support. Likely redirected to regular file. */
- f->stdout_writable = true;
- else if (r < 0)
- return r;
-
- r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
- if (r < 0)
- return r;
-
- r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
- if (r < 0)
- return r;
-
- *ret = f;
- f = NULL;
-
- return 0;
-}
-
-PTYForward *pty_forward_free(PTYForward *f) {
-
- if (f) {
- sd_event_source_unref(f->stdin_event_source);
- sd_event_source_unref(f->stdout_event_source);
- sd_event_source_unref(f->master_event_source);
- sd_event_unref(f->event);
-
- if (f->saved_stdout)
- tcsetattr(STDOUT_FILENO, TCSANOW, &f->saved_stdout_attr);
- if (f->saved_stdin)
- tcsetattr(STDIN_FILENO, TCSANOW, &f->saved_stdin_attr);
-
- free(f);
- }
-
- /* STDIN/STDOUT should not be nonblocking normally, so let's
- * unconditionally reset it */
- fd_nonblock(STDIN_FILENO, false);
- fd_nonblock(STDOUT_FILENO, false);
-
- return NULL;
-}
-
-int pty_forward_get_last_char(PTYForward *f, char *ch) {
- assert(f);
- assert(ch);
-
- if (!f->last_char_set)
- return -ENXIO;
-
- *ch = f->last_char;
- return 0;
-}
-
-int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup) {
- int r;
-
- assert(f);
-
- if (f->ignore_vhangup == ignore_vhangup)
- return 0;
-
- f->ignore_vhangup = ignore_vhangup;
- if (!f->ignore_vhangup) {
-
- /* We shall now react to vhangup()s? Let's check
- * immediately if we might be in one */
-
- f->master_readable = true;
- r = shovel(f);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
-
-int pty_forward_get_ignore_vhangup(PTYForward *f) {
- assert(f);
-
- return f->ignore_vhangup;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-#include "ratelimit.h"
-
-/* Modelled after Linux' lib/ratelimit.c by Dave Young
- * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
-
-bool ratelimit_test(RateLimit *r) {
- usec_t ts;
-
- assert(r);
-
- if (r->interval <= 0 || r->burst <= 0)
- return true;
-
- ts = now(CLOCK_MONOTONIC);
-
- if (r->begin <= 0 ||
- r->begin + r->interval < ts) {
- r->begin = ts;
-
- /* Reset counter */
- r->num = 0;
- goto good;
- }
-
- if (r->num < r->burst)
- goto good;
-
- return false;
-
-good:
- r->num++;
- return true;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-
-typedef struct RateLimit {
- usec_t interval;
- usec_t begin;
- unsigned burst;
- unsigned num;
-} RateLimit;
-
-#define RATELIMIT_DEFINE(_name, _interval, _burst) \
- RateLimit _name = { \
- .interval = (_interval), \
- .burst = (_burst), \
- .num = 0, \
- .begin = 0 \
- }
-
-#define RATELIMIT_INIT(v, _interval, _burst) \
- do { \
- RateLimit *_r = &(v); \
- _r->interval = (_interval); \
- _r->burst = (_burst); \
- _r->num = 0; \
- _r->begin = 0; \
- } while (false)
-
-#define RATELIMIT_RESET(v) \
- do { \
- RateLimit *_r = &(v); \
- _r->num = 0; \
- _r->begin = 0; \
- } while (false)
-
-bool ratelimit_test(RateLimit *r);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-
-#include "macro.h"
-#include "util.h"
-#include "replace-var.h"
-#include "def.h"
-
-/*
- * Generic infrastructure for replacing @FOO@ style variables in
- * strings. Will call a callback for each replacement.
- */
-
-static int get_variable(const char *b, char **r) {
- size_t k;
- char *t;
-
- assert(b);
- assert(r);
-
- if (*b != '@')
- return 0;
-
- k = strspn(b + 1, UPPERCASE_LETTERS "_");
- if (k <= 0 || b[k+1] != '@')
- return 0;
-
- t = strndup(b + 1, k);
- if (!t)
- return -ENOMEM;
-
- *r = t;
- return 1;
-}
-
-char *replace_var(const char *text, char *(*lookup)(const char *variable, void*userdata), void *userdata) {
- char *r, *t;
- const char *f;
- size_t l;
-
- assert(text);
- assert(lookup);
-
- l = strlen(text);
- r = new(char, l+1);
- if (!r)
- return NULL;
-
- f = text;
- t = r;
- while (*f) {
- _cleanup_free_ char *v = NULL, *n = NULL;
- char *a;
- int k;
- size_t skip, d, nl;
-
- k = get_variable(f, &v);
- if (k < 0)
- goto oom;
- if (k == 0) {
- *(t++) = *(f++);
- continue;
- }
-
- n = lookup(v, userdata);
- if (!n)
- goto oom;
-
- skip = strlen(v) + 2;
-
- d = t - r;
- nl = l - skip + strlen(n);
- a = realloc(r, nl + 1);
- if (!a)
- goto oom;
-
- l = nl;
- r = a;
- t = r + d;
-
- t = stpcpy(t, n);
- f += skip;
- }
-
- *t = 0;
- return r;
-
-oom:
- free(r);
- return NULL;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-char *replace_var(const char *text, char *(*lookup)(const char *variable, void*userdata), void *userdata);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/uio.h>
-#include "macro.h"
-#include "ring.h"
-
-#define RING_MASK(_r, _v) ((_v) & ((_r)->size - 1))
-
-void ring_flush(Ring *r) {
- assert(r);
-
- r->start = 0;
- r->used = 0;
-}
-
-void ring_clear(Ring *r) {
- assert(r);
-
- free(r->buf);
- zero(*r);
-}
-
-/*
- * Get data pointers for current ring-buffer data. @vec must be an array of 2
- * iovec objects. They are filled according to the data available in the
- * ring-buffer. 0, 1 or 2 is returned according to the number of iovec objects
- * that were filled (0 meaning buffer is empty).
- *
- * Hint: "struct iovec" is defined in <sys/uio.h> and looks like this:
- * struct iovec {
- * void *iov_base;
- * size_t iov_len;
- * };
- */
-size_t ring_peek(Ring *r, struct iovec *vec) {
- assert(r);
-
- if (r->used == 0) {
- return 0;
- } else if (r->start + r->used <= r->size) {
- if (vec) {
- vec[0].iov_base = &r->buf[r->start];
- vec[0].iov_len = r->used;
- }
- return 1;
- } else {
- if (vec) {
- vec[0].iov_base = &r->buf[r->start];
- vec[0].iov_len = r->size - r->start;
- vec[1].iov_base = r->buf;
- vec[1].iov_len = r->used - (r->size - r->start);
- }
- return 2;
- }
-}
-
-/*
- * Copy data from the ring buffer into the linear external buffer @buf. Copy
- * at most @size bytes. If the ring buffer size is smaller, copy less bytes and
- * return the number of bytes copied.
- */
-size_t ring_copy(Ring *r, void *buf, size_t size) {
- size_t l;
-
- assert(r);
- assert(buf);
-
- if (size > r->used)
- size = r->used;
-
- if (size > 0) {
- l = r->size - r->start;
- if (size <= l) {
- memcpy(buf, &r->buf[r->start], size);
- } else {
- memcpy(buf, &r->buf[r->start], l);
- memcpy((uint8_t*)buf + l, r->buf, size - l);
- }
- }
-
- return size;
-}
-
-/*
- * Resize ring-buffer to size @nsize. @nsize must be a power-of-2, otherwise
- * ring operations will behave incorrectly.
- */
-static int ring_resize(Ring *r, size_t nsize) {
- uint8_t *buf;
- size_t l;
-
- assert(r);
- assert(nsize > 0);
-
- buf = malloc(nsize);
- if (!buf)
- return -ENOMEM;
-
- if (r->used > 0) {
- l = r->size - r->start;
- if (r->used <= l) {
- memcpy(buf, &r->buf[r->start], r->used);
- } else {
- memcpy(buf, &r->buf[r->start], l);
- memcpy(&buf[l], r->buf, r->used - l);
- }
- }
-
- free(r->buf);
- r->buf = buf;
- r->size = nsize;
- r->start = 0;
-
- return 0;
-}
-
-/*
- * Resize ring-buffer to provide enough room for @add bytes of new data. This
- * resizes the buffer if it is too small. It returns -ENOMEM on OOM and 0 on
- * success.
- */
-static int ring_grow(Ring *r, size_t add) {
- size_t need;
-
- assert(r);
-
- if (r->size - r->used >= add)
- return 0;
-
- need = r->used + add;
- if (need <= r->used)
- return -ENOMEM;
- else if (need < 4096)
- need = 4096;
-
- need = ALIGN_POWER2(need);
- if (need == 0)
- return -ENOMEM;
-
- return ring_resize(r, need);
-}
-
-/*
- * Push @len bytes from @u8 into the ring buffer. The buffer is resized if it
- * is too small. -ENOMEM is returned on OOM, 0 on success.
- */
-int ring_push(Ring *r, const void *u8, size_t size) {
- int err;
- size_t pos, l;
-
- assert(r);
- assert(u8);
-
- if (size == 0)
- return 0;
-
- err = ring_grow(r, size);
- if (err < 0)
- return err;
-
- pos = RING_MASK(r, r->start + r->used);
- l = r->size - pos;
- if (l >= size) {
- memcpy(&r->buf[pos], u8, size);
- } else {
- memcpy(&r->buf[pos], u8, l);
- memcpy(r->buf, (const uint8_t*)u8 + l, size - l);
- }
-
- r->used += size;
-
- return 0;
-}
-
-/*
- * Remove @len bytes from the start of the ring-buffer. Note that we protect
- * against overflows so removing more bytes than available is safe.
- */
-void ring_pull(Ring *r, size_t size) {
- assert(r);
-
- if (size > r->used)
- size = r->used;
-
- r->start = RING_MASK(r, r->start + size);
- r->used -= size;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-
-typedef struct Ring Ring;
-
-struct Ring {
- uint8_t *buf; /* buffer or NULL */
- size_t size; /* actual size of @buf */
- size_t start; /* start position of ring */
- size_t used; /* number of actually used bytes */
-};
-
-/* flush buffer so it is empty again */
-void ring_flush(Ring *r);
-
-/* flush buffer, free allocated data and reset to initial state */
-void ring_clear(Ring *r);
-
-/* get pointers to buffer data and their length */
-size_t ring_peek(Ring *r, struct iovec *vec);
-
-/* copy data into external linear buffer */
-size_t ring_copy(Ring *r, void *buf, size_t size);
-
-/* push data to the end of the buffer */
-int ring_push(Ring *r, const void *u8, size_t size);
-
-/* pull data from the front of the buffer */
-void ring_pull(Ring *r, size_t size);
-
-/* return size of occupied buffer in bytes */
-static inline size_t ring_get_size(Ring *r)
-{
- return r->used;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <seccomp.h>
-
-#include "util.h"
-#include "seccomp-util.h"
-
-const char* seccomp_arch_to_string(uint32_t c) {
-
- if (c == SCMP_ARCH_NATIVE)
- return "native";
- if (c == SCMP_ARCH_X86)
- return "x86";
- if (c == SCMP_ARCH_X86_64)
- return "x86-64";
- if (c == SCMP_ARCH_X32)
- return "x32";
- if (c == SCMP_ARCH_ARM)
- return "arm";
-
- return NULL;
-}
-
-int seccomp_arch_from_string(const char *n, uint32_t *ret) {
- if (!n)
- return -EINVAL;
-
- assert(ret);
-
- if (streq(n, "native"))
- *ret = SCMP_ARCH_NATIVE;
- else if (streq(n, "x86"))
- *ret = SCMP_ARCH_X86;
- else if (streq(n, "x86-64"))
- *ret = SCMP_ARCH_X86_64;
- else if (streq(n, "x32"))
- *ret = SCMP_ARCH_X32;
- else if (streq(n, "arm"))
- *ret = SCMP_ARCH_ARM;
- else
- return -EINVAL;
-
- return 0;
-}
-
-int seccomp_add_secondary_archs(scmp_filter_ctx *c) {
-
-#if defined(__i386__) || defined(__x86_64__)
- int r;
-
- /* Add in all possible secondary archs we are aware of that
- * this kernel might support. */
-
- r = seccomp_arch_add(c, SCMP_ARCH_X86);
- if (r < 0 && r != -EEXIST)
- return r;
-
- r = seccomp_arch_add(c, SCMP_ARCH_X86_64);
- if (r < 0 && r != -EEXIST)
- return r;
-
- r = seccomp_arch_add(c, SCMP_ARCH_X32);
- if (r < 0 && r != -EEXIST)
- return r;
-
-#endif
-
- return 0;
-
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <seccomp.h>
-
-const char* seccomp_arch_to_string(uint32_t c);
-int seccomp_arch_from_string(const char *n, uint32_t *ret);
-
-int seccomp_add_secondary_archs(scmp_filter_ctx *c);
+++ /dev/null
-#ifndef _LINUX_SECUREBITS_H
-#define _LINUX_SECUREBITS_H 1
-
-/* This is minimal version of Linux' linux/securebits.h header file,
- * which is licensed GPL2 */
-
-#define SECUREBITS_DEFAULT 0x00000000
-
-/* When set UID 0 has no special privileges. When unset, we support
- inheritance of root-permissions and suid-root executable under
- compatibility mode. We raise the effective and inheritable bitmasks
- *of the executable file* if the effective uid of the new process is
- 0. If the real uid is 0, we raise the effective (legacy) bit of the
- executable file. */
-#define SECURE_NOROOT 0
-#define SECURE_NOROOT_LOCKED 1 /* make bit-0 immutable */
-
-/* When set, setuid to/from uid 0 does not trigger capability-"fixup".
- When unset, to provide compatibility with old programs relying on
- set*uid to gain/lose privilege, transitions to/from uid 0 cause
- capabilities to be gained/lost. */
-#define SECURE_NO_SETUID_FIXUP 2
-#define SECURE_NO_SETUID_FIXUP_LOCKED 3 /* make bit-2 immutable */
-
-/* When set, a process can retain its capabilities even after
- transitioning to a non-root user (the set-uid fixup suppressed by
- bit 2). Bit-4 is cleared when a process calls exec(); setting both
- bit 4 and 5 will create a barrier through exec that no exec()'d
- child can use this feature again. */
-#define SECURE_KEEP_CAPS 4
-#define SECURE_KEEP_CAPS_LOCKED 5 /* make bit-4 immutable */
-
-/* Each securesetting is implemented using two bits. One bit specifies
- whether the setting is on or off. The other bit specify whether the
- setting is locked or not. A setting which is locked cannot be
- changed from user-level. */
-#define issecure_mask(X) (1 << (X))
-#define issecure(X) (issecure_mask(X) & current_cred_xxx(securebits))
-
-#define SECURE_ALL_BITS (issecure_mask(SECURE_NOROOT) | \
- issecure_mask(SECURE_NO_SETUID_FIXUP) | \
- issecure_mask(SECURE_KEEP_CAPS))
-#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
-
-#endif /* !_LINUX_SECUREBITS_H */
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <signal.h>
-#include <sys/mman.h>
-
-#include "macro.h"
-#include "util.h"
-#include "sigbus.h"
-
-#define SIGBUS_QUEUE_MAX 64
-
-static struct sigaction old_sigaction;
-static unsigned n_installed = 0;
-
-/* We maintain a fixed size list of page addresses that triggered a
- SIGBUS. We access with list with atomic operations, so that we
- don't have to deal with locks between signal handler and main
- programs in possibly multiple threads. */
-
-static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX];
-static volatile sig_atomic_t n_sigbus_queue = 0;
-
-static void sigbus_push(void *addr) {
- unsigned u;
-
- assert(addr);
-
- /* Find a free place, increase the number of entries and leave, if we can */
- for (u = 0; u < SIGBUS_QUEUE_MAX; u++)
- if (__sync_bool_compare_and_swap(&sigbus_queue[u], NULL, addr)) {
- __sync_fetch_and_add(&n_sigbus_queue, 1);
- return;
- }
-
- /* If we can't, make sure the queue size is out of bounds, to
- * mark it as overflow */
- for (;;) {
- unsigned c;
-
- __sync_synchronize();
- c = n_sigbus_queue;
-
- if (c > SIGBUS_QUEUE_MAX) /* already overflow */
- return;
-
- if (__sync_bool_compare_and_swap(&n_sigbus_queue, c, c + SIGBUS_QUEUE_MAX))
- return;
- }
-}
-
-int sigbus_pop(void **ret) {
- assert(ret);
-
- for (;;) {
- unsigned u, c;
-
- __sync_synchronize();
- c = n_sigbus_queue;
-
- if (_likely_(c == 0))
- return 0;
-
- if (_unlikely_(c >= SIGBUS_QUEUE_MAX))
- return -EOVERFLOW;
-
- for (u = 0; u < SIGBUS_QUEUE_MAX; u++) {
- void *addr;
-
- addr = sigbus_queue[u];
- if (!addr)
- continue;
-
- if (__sync_bool_compare_and_swap(&sigbus_queue[u], addr, NULL)) {
- __sync_fetch_and_sub(&n_sigbus_queue, 1);
- *ret = addr;
- return 1;
- }
- }
- }
-}
-
-static void sigbus_handler(int sn, siginfo_t *si, void *data) {
- unsigned long ul;
- void *aligned;
-
- assert(sn == SIGBUS);
- assert(si);
-
- if (si->si_code != BUS_ADRERR || !si->si_addr) {
- assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
- raise(SIGBUS);
- return;
- }
-
- ul = (unsigned long) si->si_addr;
- ul = ul / page_size();
- ul = ul * page_size();
- aligned = (void*) ul;
-
- /* Let's remember which address failed */
- sigbus_push(aligned);
-
- /* Replace mapping with an anonymous page, so that the
- * execution can continue, however with a zeroed out page */
- assert_se(mmap(aligned, page_size(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == aligned);
-}
-
-void sigbus_install(void) {
- struct sigaction sa = {
- .sa_sigaction = sigbus_handler,
- .sa_flags = SA_SIGINFO,
- };
-
- n_installed++;
-
- if (n_installed == 1)
- assert_se(sigaction(SIGBUS, &sa, &old_sigaction) == 0);
-
- return;
-}
-
-void sigbus_reset(void) {
-
- if (n_installed <= 0)
- return;
-
- n_installed--;
-
- if (n_installed == 0)
- assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
-
- return;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#pragma once
-
-void sigbus_install(void);
-void sigbus_reset(void);
-
-int sigbus_pop(void **ret);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <stddef.h>
-
-#include "macro.h"
-#include "util.h"
-#include "mkdir.h"
-#include "missing.h"
-#include "selinux-util.h"
-#include "socket-util.h"
-
-int socket_address_listen(
- const SocketAddress *a,
- int flags,
- int backlog,
- SocketAddressBindIPv6Only only,
- const char *bind_to_device,
- bool free_bind,
- bool transparent,
- mode_t directory_mode,
- mode_t socket_mode,
- const char *label) {
-
- _cleanup_close_ int fd = -1;
- int r, one;
-
- assert(a);
-
- r = socket_address_verify(a);
- if (r < 0)
- return r;
-
- if (socket_address_family(a) == AF_INET6 && !socket_ipv6_is_supported())
- return -EAFNOSUPPORT;
-
- if (label) {
- r = mac_selinux_create_socket_prepare(label);
- if (r < 0)
- return r;
- }
-
- fd = socket(socket_address_family(a), a->type | flags, a->protocol);
- r = fd < 0 ? -errno : 0;
-
- if (label)
- mac_selinux_create_socket_clear();
-
- if (r < 0)
- return r;
-
- if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
- int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
-
- if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
- return -errno;
- }
-
- if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
- if (bind_to_device)
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
- return -errno;
-
- if (free_bind) {
- one = 1;
- if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
- log_warning_errno(errno, "IP_FREEBIND failed: %m");
- }
-
- if (transparent) {
- one = 1;
- if (setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one)) < 0)
- log_warning_errno(errno, "IP_TRANSPARENT failed: %m");
- }
- }
-
- one = 1;
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
- return -errno;
-
- if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
- mode_t old_mask;
-
- /* Create parents */
- mkdir_parents_label(a->sockaddr.un.sun_path, directory_mode);
-
- /* Enforce the right access mode for the socket */
- old_mask = umask(~ socket_mode);
-
- /* Include the original umask in our mask */
- umask(~socket_mode | old_mask);
-
- r = mac_selinux_bind(fd, &a->sockaddr.sa, a->size);
-
- if (r < 0 && errno == EADDRINUSE) {
- /* Unlink and try again */
- unlink(a->sockaddr.un.sun_path);
- r = bind(fd, &a->sockaddr.sa, a->size);
- }
-
- umask(old_mask);
- } else
- r = bind(fd, &a->sockaddr.sa, a->size);
-
- if (r < 0)
- return -errno;
-
- if (socket_address_can_accept(a))
- if (listen(fd, backlog) < 0)
- return -errno;
-
- r = fd;
- fd = -1;
-
- return r;
-}
-
-int make_socket_fd(int log_level, const char* address, int flags) {
- SocketAddress a;
- int fd, r;
-
- r = socket_address_parse(&a, address);
- if (r < 0) {
- log_error("Failed to parse socket address \"%s\": %s",
- address, strerror(-r));
- return r;
- }
-
- fd = socket_address_listen(&a, flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT,
- NULL, false, false, 0755, 0644, NULL);
- if (fd < 0 || log_get_max_level() >= log_level) {
- _cleanup_free_ char *p = NULL;
-
- r = socket_address_print(&a, &p);
- if (r < 0)
- return log_error_errno(r, "socket_address_print(): %m");
-
- if (fd < 0)
- log_error_errno(fd, "Failed to listen on %s: %m", p);
- else
- log_full(log_level, "Listening on %s", p);
- }
-
- return fd;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <arpa/inet.h>
-#include <stdio.h>
-#include <net/if.h>
-#include <sys/types.h>
-#include <stddef.h>
-#include <netdb.h>
-
-#include "macro.h"
-#include "path-util.h"
-#include "util.h"
-#include "socket-util.h"
-#include "missing.h"
-#include "fileio.h"
-
-int socket_address_parse(SocketAddress *a, const char *s) {
- char *e, *n;
- unsigned u;
- int r;
-
- assert(a);
- assert(s);
-
- zero(*a);
- a->type = SOCK_STREAM;
-
- if (*s == '[') {
- /* IPv6 in [x:.....:z]:p notation */
-
- if (!socket_ipv6_is_supported()) {
- log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
- return -EAFNOSUPPORT;
- }
-
- e = strchr(s+1, ']');
- if (!e)
- return -EINVAL;
-
- n = strndupa(s+1, e-s-1);
-
- errno = 0;
- if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0)
- return errno > 0 ? -errno : -EINVAL;
-
- e++;
- if (*e != ':')
- return -EINVAL;
-
- e++;
- r = safe_atou(e, &u);
- if (r < 0)
- return r;
-
- if (u <= 0 || u > 0xFFFF)
- return -EINVAL;
-
- a->sockaddr.in6.sin6_family = AF_INET6;
- a->sockaddr.in6.sin6_port = htons((uint16_t) u);
- a->size = sizeof(struct sockaddr_in6);
-
- } else if (*s == '/') {
- /* AF_UNIX socket */
-
- size_t l;
-
- l = strlen(s);
- if (l >= sizeof(a->sockaddr.un.sun_path))
- return -EINVAL;
-
- a->sockaddr.un.sun_family = AF_UNIX;
- memcpy(a->sockaddr.un.sun_path, s, l);
- a->size = offsetof(struct sockaddr_un, sun_path) + l + 1;
-
- } else if (*s == '@') {
- /* Abstract AF_UNIX socket */
- size_t l;
-
- l = strlen(s+1);
- if (l >= sizeof(a->sockaddr.un.sun_path) - 1)
- return -EINVAL;
-
- a->sockaddr.un.sun_family = AF_UNIX;
- memcpy(a->sockaddr.un.sun_path+1, s+1, l);
- a->size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
-
- } else {
- e = strchr(s, ':');
- if (e) {
- r = safe_atou(e+1, &u);
- if (r < 0)
- return r;
-
- if (u <= 0 || u > 0xFFFF)
- return -EINVAL;
-
- n = strndupa(s, e-s);
-
- /* IPv4 in w.x.y.z:p notation? */
- r = inet_pton(AF_INET, n, &a->sockaddr.in.sin_addr);
- if (r < 0)
- return -errno;
-
- if (r > 0) {
- /* Gotcha, it's a traditional IPv4 address */
- a->sockaddr.in.sin_family = AF_INET;
- a->sockaddr.in.sin_port = htons((uint16_t) u);
- a->size = sizeof(struct sockaddr_in);
- } else {
- unsigned idx;
-
- if (strlen(n) > IF_NAMESIZE-1)
- return -EINVAL;
-
- /* Uh, our last resort, an interface name */
- idx = if_nametoindex(n);
- if (idx == 0)
- return -EINVAL;
-
- if (!socket_ipv6_is_supported()) {
- log_warning("Binding to interface is not available since kernel does not support IPv6.");
- return -EAFNOSUPPORT;
- }
-
- a->sockaddr.in6.sin6_family = AF_INET6;
- a->sockaddr.in6.sin6_port = htons((uint16_t) u);
- a->sockaddr.in6.sin6_scope_id = idx;
- a->sockaddr.in6.sin6_addr = in6addr_any;
- a->size = sizeof(struct sockaddr_in6);
- }
- } else {
-
- /* Just a port */
- r = safe_atou(s, &u);
- if (r < 0)
- return r;
-
- if (u <= 0 || u > 0xFFFF)
- return -EINVAL;
-
- if (socket_ipv6_is_supported()) {
- a->sockaddr.in6.sin6_family = AF_INET6;
- a->sockaddr.in6.sin6_port = htons((uint16_t) u);
- a->sockaddr.in6.sin6_addr = in6addr_any;
- a->size = sizeof(struct sockaddr_in6);
- } else {
- a->sockaddr.in.sin_family = AF_INET;
- a->sockaddr.in.sin_port = htons((uint16_t) u);
- a->sockaddr.in.sin_addr.s_addr = INADDR_ANY;
- a->size = sizeof(struct sockaddr_in);
- }
- }
- }
-
- return 0;
-}
-
-int socket_address_parse_netlink(SocketAddress *a, const char *s) {
- int family;
- unsigned group = 0;
- _cleanup_free_ char *sfamily = NULL;
- assert(a);
- assert(s);
-
- zero(*a);
- a->type = SOCK_RAW;
-
- errno = 0;
- if (sscanf(s, "%ms %u", &sfamily, &group) < 1)
- return errno > 0 ? -errno : -EINVAL;
-
- family = netlink_family_from_string(sfamily);
- if (family < 0)
- return -EINVAL;
-
- a->sockaddr.nl.nl_family = AF_NETLINK;
- a->sockaddr.nl.nl_groups = group;
-
- a->type = SOCK_RAW;
- a->size = sizeof(struct sockaddr_nl);
- a->protocol = family;
-
- return 0;
-}
-
-int socket_address_verify(const SocketAddress *a) {
- assert(a);
-
- switch (socket_address_family(a)) {
-
- case AF_INET:
- if (a->size != sizeof(struct sockaddr_in))
- return -EINVAL;
-
- if (a->sockaddr.in.sin_port == 0)
- return -EINVAL;
-
- if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
- return -EINVAL;
-
- return 0;
-
- case AF_INET6:
- if (a->size != sizeof(struct sockaddr_in6))
- return -EINVAL;
-
- if (a->sockaddr.in6.sin6_port == 0)
- return -EINVAL;
-
- if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
- return -EINVAL;
-
- return 0;
-
- case AF_UNIX:
- if (a->size < offsetof(struct sockaddr_un, sun_path))
- return -EINVAL;
-
- if (a->size > offsetof(struct sockaddr_un, sun_path)) {
-
- if (a->sockaddr.un.sun_path[0] != 0) {
- char *e;
-
- /* path */
- e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
- if (!e)
- return -EINVAL;
-
- if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
- return -EINVAL;
- }
- }
-
- if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM && a->type != SOCK_SEQPACKET)
- return -EINVAL;
-
- return 0;
-
- case AF_NETLINK:
-
- if (a->size != sizeof(struct sockaddr_nl))
- return -EINVAL;
-
- if (a->type != SOCK_RAW && a->type != SOCK_DGRAM)
- return -EINVAL;
-
- return 0;
-
- default:
- return -EAFNOSUPPORT;
- }
-}
-
-int socket_address_print(const SocketAddress *a, char **ret) {
- int r;
-
- assert(a);
- assert(ret);
-
- r = socket_address_verify(a);
- if (r < 0)
- return r;
-
- if (socket_address_family(a) == AF_NETLINK) {
- _cleanup_free_ char *sfamily = NULL;
-
- r = netlink_family_to_string_alloc(a->protocol, &sfamily);
- if (r < 0)
- return r;
-
- r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
- if (r < 0)
- return -ENOMEM;
-
- return 0;
- }
-
- return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
-}
-
-bool socket_address_can_accept(const SocketAddress *a) {
- assert(a);
-
- return
- a->type == SOCK_STREAM ||
- a->type == SOCK_SEQPACKET;
-}
-
-bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
- assert(a);
- assert(b);
-
- /* Invalid addresses are unequal to all */
- if (socket_address_verify(a) < 0 ||
- socket_address_verify(b) < 0)
- return false;
-
- if (a->type != b->type)
- return false;
-
- if (socket_address_family(a) != socket_address_family(b))
- return false;
-
- switch (socket_address_family(a)) {
-
- case AF_INET:
- if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
- return false;
-
- if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
- return false;
-
- break;
-
- case AF_INET6:
- if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
- return false;
-
- if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
- return false;
-
- break;
-
- case AF_UNIX:
- if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
- b->size <= offsetof(struct sockaddr_un, sun_path))
- return false;
-
- if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
- return false;
-
- if (a->sockaddr.un.sun_path[0]) {
- if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path))
- return false;
- } else {
- if (a->size != b->size)
- return false;
-
- if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
- return false;
- }
-
- break;
-
- case AF_NETLINK:
- if (a->protocol != b->protocol)
- return false;
-
- if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
- return false;
-
- break;
-
- default:
- /* Cannot compare, so we assume the addresses are different */
- return false;
- }
-
- return true;
-}
-
-bool socket_address_is(const SocketAddress *a, const char *s, int type) {
- struct SocketAddress b;
-
- assert(a);
- assert(s);
-
- if (socket_address_parse(&b, s) < 0)
- return false;
-
- b.type = type;
-
- return socket_address_equal(a, &b);
-}
-
-bool socket_address_is_netlink(const SocketAddress *a, const char *s) {
- struct SocketAddress b;
-
- assert(a);
- assert(s);
-
- if (socket_address_parse_netlink(&b, s) < 0)
- return false;
-
- return socket_address_equal(a, &b);
-}
-
-const char* socket_address_get_path(const SocketAddress *a) {
- assert(a);
-
- if (socket_address_family(a) != AF_UNIX)
- return NULL;
-
- if (a->sockaddr.un.sun_path[0] == 0)
- return NULL;
-
- return a->sockaddr.un.sun_path;
-}
-
-bool socket_ipv6_is_supported(void) {
- _cleanup_free_ char *l = NULL;
-
- if (access("/sys/module/ipv6", F_OK) != 0)
- return false;
-
- /* If we can't check "disable" parameter, assume enabled */
- if (read_one_line_file("/sys/module/ipv6/parameters/disable", &l) < 0)
- return true;
-
- /* If module was loaded with disable=1 no IPv6 available */
- return l[0] == '0';
-}
-
-bool socket_address_matches_fd(const SocketAddress *a, int fd) {
- SocketAddress b;
- socklen_t solen;
-
- assert(a);
- assert(fd >= 0);
-
- b.size = sizeof(b.sockaddr);
- if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
- return false;
-
- if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
- return false;
-
- solen = sizeof(b.type);
- if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
- return false;
-
- if (b.type != a->type)
- return false;
-
- if (a->protocol != 0) {
- solen = sizeof(b.protocol);
- if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
- return false;
-
- if (b.protocol != a->protocol)
- return false;
- }
-
- return socket_address_equal(a, &b);
-}
-
-int sockaddr_port(const struct sockaddr *_sa) {
- union sockaddr_union *sa = (union sockaddr_union*) _sa;
-
- assert(sa);
-
- if (!IN_SET(sa->sa.sa_family, AF_INET, AF_INET6))
- return -EAFNOSUPPORT;
-
- return ntohs(sa->sa.sa_family == AF_INET6 ?
- sa->in6.sin6_port :
- sa->in.sin_port);
-}
-
-int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret) {
- union sockaddr_union *sa = (union sockaddr_union*) _sa;
- char *p;
- int r;
-
- assert(sa);
- assert(salen >= sizeof(sa->sa.sa_family));
-
- switch (sa->sa.sa_family) {
-
- case AF_INET: {
- uint32_t a;
-
- a = ntohl(sa->in.sin_addr.s_addr);
-
- if (include_port)
- r = asprintf(&p,
- "%u.%u.%u.%u:%u",
- a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
- ntohs(sa->in.sin_port));
- else
- r = asprintf(&p,
- "%u.%u.%u.%u",
- a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
- if (r < 0)
- return -ENOMEM;
- break;
- }
-
- case AF_INET6: {
- static const unsigned char ipv4_prefix[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
- };
-
- if (translate_ipv6 &&
- memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
- const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
- if (include_port)
- r = asprintf(&p,
- "%u.%u.%u.%u:%u",
- a[0], a[1], a[2], a[3],
- ntohs(sa->in6.sin6_port));
- else
- r = asprintf(&p,
- "%u.%u.%u.%u",
- a[0], a[1], a[2], a[3]);
- if (r < 0)
- return -ENOMEM;
- } else {
- char a[INET6_ADDRSTRLEN];
-
- inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a));
-
- if (include_port) {
- r = asprintf(&p,
- "[%s]:%u",
- a,
- ntohs(sa->in6.sin6_port));
- if (r < 0)
- return -ENOMEM;
- } else {
- p = strdup(a);
- if (!p)
- return -ENOMEM;
- }
- }
-
- break;
- }
-
- case AF_UNIX:
- if (salen <= offsetof(struct sockaddr_un, sun_path)) {
- p = strdup("<unnamed>");
- if (!p)
- return -ENOMEM;
-
- } else if (sa->un.sun_path[0] == 0) {
- /* abstract */
-
- /* FIXME: We assume we can print the
- * socket path here and that it hasn't
- * more than one NUL byte. That is
- * actually an invalid assumption */
-
- p = new(char, sizeof(sa->un.sun_path)+1);
- if (!p)
- return -ENOMEM;
-
- p[0] = '@';
- memcpy(p+1, sa->un.sun_path+1, sizeof(sa->un.sun_path)-1);
- p[sizeof(sa->un.sun_path)] = 0;
-
- } else {
- p = strndup(sa->un.sun_path, sizeof(sa->un.sun_path));
- if (!ret)
- return -ENOMEM;
- }
-
- break;
-
- default:
- return -EOPNOTSUPP;
- }
-
-
- *ret = p;
- return 0;
-}
-
-int getpeername_pretty(int fd, char **ret) {
- union sockaddr_union sa;
- socklen_t salen = sizeof(sa);
- int r;
-
- assert(fd >= 0);
- assert(ret);
-
- if (getpeername(fd, &sa.sa, &salen) < 0)
- return -errno;
-
- if (sa.sa.sa_family == AF_UNIX) {
- struct ucred ucred = {};
-
- /* UNIX connection sockets are anonymous, so let's use
- * PID/UID as pretty credentials instead */
-
- r = getpeercred(fd, &ucred);
- if (r < 0)
- return r;
-
- if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
- return -ENOMEM;
-
- return 0;
- }
-
- /* For remote sockets we translate IPv6 addresses back to IPv4
- * if applicable, since that's nicer. */
-
- return sockaddr_pretty(&sa.sa, salen, true, true, ret);
-}
-
-int getsockname_pretty(int fd, char **ret) {
- union sockaddr_union sa;
- socklen_t salen = sizeof(sa);
-
- assert(fd >= 0);
- assert(ret);
-
- if (getsockname(fd, &sa.sa, &salen) < 0)
- return -errno;
-
- /* For local sockets we do not translate IPv6 addresses back
- * to IPv6 if applicable, since this is usually used for
- * listening sockets where the difference between IPv4 and
- * IPv6 matters. */
-
- return sockaddr_pretty(&sa.sa, salen, false, true, ret);
-}
-
-int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) {
- int r;
- char host[NI_MAXHOST], *ret;
-
- assert(_ret);
-
- r = getnameinfo(&sa->sa, salen, host, sizeof(host), NULL, 0,
- NI_IDN|NI_IDN_USE_STD3_ASCII_RULES);
- if (r != 0) {
- int saved_errno = errno;
-
- r = sockaddr_pretty(&sa->sa, salen, true, true, &ret);
- if (r < 0)
- return log_error_errno(r, "sockadd_pretty() failed: %m");
-
- log_debug_errno(saved_errno, "getnameinfo(%s) failed: %m", ret);
- } else {
- ret = strdup(host);
- if (!ret)
- return log_oom();
- }
-
- *_ret = ret;
- return 0;
-}
-
-int getnameinfo_pretty(int fd, char **ret) {
- union sockaddr_union sa;
- socklen_t salen = sizeof(sa);
-
- assert(fd >= 0);
- assert(ret);
-
- if (getsockname(fd, &sa.sa, &salen) < 0)
- return log_error_errno(errno, "getsockname(%d) failed: %m", fd);
-
- return socknameinfo_pretty(&sa, salen, ret);
-}
-
-int socket_address_unlink(SocketAddress *a) {
- assert(a);
-
- if (socket_address_family(a) != AF_UNIX)
- return 0;
-
- if (a->sockaddr.un.sun_path[0] == 0)
- return 0;
-
- if (unlink(a->sockaddr.un.sun_path) < 0)
- return -errno;
-
- return 1;
-}
-
-static const char* const netlink_family_table[] = {
- [NETLINK_ROUTE] = "route",
- [NETLINK_FIREWALL] = "firewall",
- [NETLINK_INET_DIAG] = "inet-diag",
- [NETLINK_NFLOG] = "nflog",
- [NETLINK_XFRM] = "xfrm",
- [NETLINK_SELINUX] = "selinux",
- [NETLINK_ISCSI] = "iscsi",
- [NETLINK_AUDIT] = "audit",
- [NETLINK_FIB_LOOKUP] = "fib-lookup",
- [NETLINK_CONNECTOR] = "connector",
- [NETLINK_NETFILTER] = "netfilter",
- [NETLINK_IP6_FW] = "ip6-fw",
- [NETLINK_DNRTMSG] = "dnrtmsg",
- [NETLINK_KOBJECT_UEVENT] = "kobject-uevent",
- [NETLINK_GENERIC] = "generic",
- [NETLINK_SCSITRANSPORT] = "scsitransport",
- [NETLINK_ECRYPTFS] = "ecryptfs"
-};
-
-DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
-
-static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
- [SOCKET_ADDRESS_DEFAULT] = "default",
- [SOCKET_ADDRESS_BOTH] = "both",
- [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only);
-
-bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
- assert(a);
- assert(b);
-
- if (a->sa.sa_family != b->sa.sa_family)
- return false;
-
- if (a->sa.sa_family == AF_INET)
- return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
-
- if (a->sa.sa_family == AF_INET6)
- return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
-
- return false;
-}
-
-char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
- assert(addr);
- assert(buffer);
-
- /* Like ether_ntoa() but uses %02x instead of %x to print
- * ethernet addresses, which makes them look less funny. Also,
- * doesn't use a static buffer. */
-
- sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
- addr->ether_addr_octet[0],
- addr->ether_addr_octet[1],
- addr->ether_addr_octet[2],
- addr->ether_addr_octet[3],
- addr->ether_addr_octet[4],
- addr->ether_addr_octet[5]);
-
- return buffer;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <signal.h>
-
-#include "log.h"
-#include "util.h"
-#include "spawn-ask-password-agent.h"
-
-static pid_t agent_pid = 0;
-
-int ask_password_agent_open(void) {
- int r;
-
- if (agent_pid > 0)
- return 0;
-
- /* We check STDIN here, not STDOUT, since this is about input,
- * not output */
- if (!isatty(STDIN_FILENO))
- return 0;
-
- r = fork_agent(&agent_pid,
- NULL, 0,
- SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
- SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
- if (r < 0)
- log_error_errno(r, "Failed to fork TTY ask password agent: %m");
-
- return r;
-}
-
-void ask_password_agent_close(void) {
-
- if (agent_pid <= 0)
- return;
-
- /* Inform agent that we are done */
- kill(agent_pid, SIGTERM);
- kill(agent_pid, SIGCONT);
- (void) wait_for_terminate(agent_pid, NULL);
- agent_pid = 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-int ask_password_agent_open(void);
-void ask_password_agent_close(void);
#include "log.h"
#include "util.h"
+#include "process-util.h"
#include "spawn-polkit-agent.h"
#ifdef ENABLE_POLKIT
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#define SPECIAL_DEFAULT_TARGET "default.target"
-
-/* Shutdown targets */
-#define SPECIAL_UMOUNT_TARGET "umount.target"
-/* This is not really intended to be started by directly. This is
- * mostly so that other targets (reboot/halt/poweroff) can depend on
- * it to bring all services down that want to be brought down on
- * system shutdown. */
-#define SPECIAL_SHUTDOWN_TARGET "shutdown.target"
-#define SPECIAL_HALT_TARGET "halt.target"
-#define SPECIAL_POWEROFF_TARGET "poweroff.target"
-#define SPECIAL_REBOOT_TARGET "reboot.target"
-#define SPECIAL_KEXEC_TARGET "kexec.target"
-#define SPECIAL_EXIT_TARGET "exit.target"
-#define SPECIAL_SUSPEND_TARGET "suspend.target"
-#define SPECIAL_HIBERNATE_TARGET "hibernate.target"
-#define SPECIAL_HYBRID_SLEEP_TARGET "hybrid-sleep.target"
-
-/* Special boot targets */
-#define SPECIAL_RESCUE_TARGET "rescue.target"
-#define SPECIAL_EMERGENCY_TARGET "emergency.target"
-#define SPECIAL_MULTI_USER_TARGET "multi-user.target"
-#define SPECIAL_GRAPHICAL_TARGET "graphical.target"
-
-/* Early boot targets */
-#define SPECIAL_SYSINIT_TARGET "sysinit.target"
-#define SPECIAL_SOCKETS_TARGET "sockets.target"
-#define SPECIAL_BUSNAMES_TARGET "busnames.target"
-#define SPECIAL_TIMERS_TARGET "timers.target"
-#define SPECIAL_PATHS_TARGET "paths.target"
-#define SPECIAL_LOCAL_FS_TARGET "local-fs.target"
-#define SPECIAL_LOCAL_FS_PRE_TARGET "local-fs-pre.target"
-#define SPECIAL_INITRD_FS_TARGET "initrd-fs.target"
-#define SPECIAL_INITRD_ROOT_FS_TARGET "initrd-root-fs.target"
-#define SPECIAL_REMOTE_FS_TARGET "remote-fs.target" /* LSB's $remote_fs */
-#define SPECIAL_REMOTE_FS_PRE_TARGET "remote-fs-pre.target"
-#define SPECIAL_SWAP_TARGET "swap.target"
-#define SPECIAL_NETWORK_ONLINE_TARGET "network-online.target"
-#define SPECIAL_TIME_SYNC_TARGET "time-sync.target" /* LSB's $time */
-#define SPECIAL_BASIC_TARGET "basic.target"
-
-/* LSB compatibility */
-#define SPECIAL_NETWORK_TARGET "network.target" /* LSB's $network */
-#define SPECIAL_NSS_LOOKUP_TARGET "nss-lookup.target" /* LSB's $named */
-#define SPECIAL_RPCBIND_TARGET "rpcbind.target" /* LSB's $portmap */
-
-/*
- * Rules regarding adding further high level targets like the above:
- *
- * - Be conservative, only add more of these when we really need
- * them. We need strong usecases for further additions.
- *
- * - When there can be multiple implementations running side-by-side,
- * it needs to be a .target unit which can pull in all
- * implementations.
- *
- * - If something can be implemented with socket activation, and
- * without, it needs to be a .target unit, so that it can pull in
- * the appropriate unit.
- *
- * - Otherwise, it should be a .service unit.
- *
- * - In some cases it is OK to have both a .service and a .target
- * unit, i.e. if there can be multiple parallel implementations, but
- * only one is the "system" one. Example: syslog.
- *
- * Or to put this in other words: .service symlinks can be used to
- * arbitrate between multiple implementations if there can be only one
- * of a kind. .target units can be used to support multiple
- * implementations that can run side-by-side.
- */
-
-/* Magic early boot services */
-#define SPECIAL_FSCK_SERVICE "systemd-fsck@.service"
-#define SPECIAL_REMOUNT_FS_SERVICE "systemd-remount-fs.service"
-
-/* Services systemd relies on */
-#define SPECIAL_DBUS_SERVICE "dbus.service"
-#define SPECIAL_DBUS_SOCKET "dbus.socket"
-#define SPECIAL_JOURNALD_SOCKET "systemd-journald.socket"
-#define SPECIAL_JOURNALD_SERVICE "systemd-journald.service"
-
-/* Magic init signals */
-#define SPECIAL_KBREQUEST_TARGET "kbrequest.target"
-#define SPECIAL_SIGPWR_TARGET "sigpwr.target"
-#define SPECIAL_CTRL_ALT_DEL_TARGET "ctrl-alt-del.target"
-
-/* Where we add all our system units, users and machines by default */
-#define SPECIAL_SYSTEM_SLICE "system.slice"
-#define SPECIAL_USER_SLICE "user.slice"
-#define SPECIAL_MACHINE_SLICE "machine.slice"
-#define SPECIAL_ROOT_SLICE "-.slice"
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-#include <sys/utsname.h>
-
-#include "macro.h"
-#include "util.h"
-#include "specifier.h"
-
-/*
- * Generic infrastructure for replacing %x style specifiers in
- * strings. Will call a callback for each replacement.
- *
- */
-
-int specifier_printf(const char *text, const Specifier table[], void *userdata, char **_ret) {
- char *ret, *t;
- const char *f;
- bool percent = false;
- size_t l;
- int r;
-
- assert(text);
- assert(table);
-
- l = strlen(text);
- ret = new(char, l+1);
- if (!ret)
- return -ENOMEM;
-
- t = ret;
-
- for (f = text; *f; f++, l--) {
-
- if (percent) {
- if (*f == '%')
- *(t++) = '%';
- else {
- const Specifier *i;
-
- for (i = table; i->specifier; i++)
- if (i->specifier == *f)
- break;
-
- if (i->lookup) {
- _cleanup_free_ char *w = NULL;
- char *n;
- size_t k, j;
-
- r = i->lookup(i->specifier, i->data, userdata, &w);
- if (r < 0) {
- free(ret);
- return r;
- }
-
- j = t - ret;
- k = strlen(w);
-
- n = new(char, j + k + l + 1);
- if (!n) {
- free(ret);
- return -ENOMEM;
- }
-
- memcpy(n, ret, j);
- memcpy(n + j, w, k);
-
- free(ret);
-
- ret = n;
- t = n + j + k;
- } else {
- *(t++) = '%';
- *(t++) = *f;
- }
- }
-
- percent = false;
- } else if (*f == '%')
- percent = true;
- else
- *(t++) = *f;
- }
-
- *t = 0;
- *_ret = ret;
- return 0;
-}
-
-/* Generic handler for simple string replacements */
-
-int specifier_string(char specifier, void *data, void *userdata, char **ret) {
- char *n;
-
- n = strdup(strempty(data));
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
-}
-
-int specifier_machine_id(char specifier, void *data, void *userdata, char **ret) {
- sd_id128_t id;
- char *n;
- int r;
-
- r = sd_id128_get_machine(&id);
- if (r < 0)
- return r;
-
- n = new(char, 33);
- if (!n)
- return -ENOMEM;
-
- *ret = sd_id128_to_string(id, n);
- return 0;
-}
-
-int specifier_boot_id(char specifier, void *data, void *userdata, char **ret) {
- sd_id128_t id;
- char *n;
- int r;
-
- r = sd_id128_get_boot(&id);
- if (r < 0)
- return r;
-
- n = new(char, 33);
- if (!n)
- return -ENOMEM;
-
- *ret = sd_id128_to_string(id, n);
- return 0;
-}
-
-int specifier_host_name(char specifier, void *data, void *userdata, char **ret) {
- char *n;
-
- n = gethostname_malloc();
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
-}
-
-int specifier_kernel_release(char specifier, void *data, void *userdata, char **ret) {
- struct utsname uts;
- char *n;
- int r;
-
- r = uname(&uts);
- if (r < 0)
- return -errno;
-
- n = strdup(uts.release);
- if (!n)
- return -ENOMEM;
-
- *ret = n;
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-typedef int (*SpecifierCallback)(char specifier, void *data, void *userdata, char **ret);
-
-typedef struct Specifier {
- const char specifier;
- const SpecifierCallback lookup;
- void *data;
-} Specifier;
-
-int specifier_printf(const char *text, const Specifier table[], void *userdata, char **ret);
-
-int specifier_string(char specifier, void *data, void *userdata, char **ret);
-
-int specifier_machine_id(char specifier, void *data, void *userdata, char **ret);
-int specifier_boot_id(char specifier, void *data, void *userdata, char **ret);
-int specifier_host_name(char specifier, void *data, void *userdata, char **ret);
-int specifier_kernel_release(char specifier, void *data, void *userdata, char **ret);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Kay Sievers <kay@vrfy.org>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <string.h>
-
-#include "util.h"
-#include "strbuf.h"
-
-/*
- * Strbuf stores given strings in a single continuous allocated memory
- * area. Identical strings are de-duplicated and return the same offset
- * as the first string stored. If the tail of a string already exists
- * in the buffer, the tail is returned.
- *
- * A trie (http://en.wikipedia.org/wiki/Trie) is used to maintain the
- * information about the stored strings.
- *
- * Example of udev rules:
- * $ ./udevadm test .
- * ...
- * read rules file: /usr/lib/udev/rules.d/99-systemd.rules
- * rules contain 196608 bytes tokens (16384 * 12 bytes), 39742 bytes strings
- * 23939 strings (207859 bytes), 20404 de-duplicated (171653 bytes), 3536 trie nodes used
- * ...
- */
-
-struct strbuf *strbuf_new(void) {
- struct strbuf *str;
-
- str = new0(struct strbuf, 1);
- if (!str)
- return NULL;
-
- str->buf = new0(char, 1);
- if (!str->buf)
- goto err;
- str->len = 1;
-
- str->root = new0(struct strbuf_node, 1);
- if (!str->root)
- goto err;
- str->nodes_count = 1;
- return str;
-err:
- free(str->buf);
- free(str->root);
- free(str);
- return NULL;
-}
-
-static void strbuf_node_cleanup(struct strbuf_node *node) {
- size_t i;
-
- for (i = 0; i < node->children_count; i++)
- strbuf_node_cleanup(node->children[i].child);
- free(node->children);
- free(node);
-}
-
-/* clean up trie data, leave only the string buffer */
-void strbuf_complete(struct strbuf *str) {
- if (!str)
- return;
- if (str->root)
- strbuf_node_cleanup(str->root);
- str->root = NULL;
-}
-
-/* clean up everything */
-void strbuf_cleanup(struct strbuf *str) {
- if (!str)
- return;
- if (str->root)
- strbuf_node_cleanup(str->root);
- free(str->buf);
- free(str);
-}
-
-static int strbuf_children_cmp(const struct strbuf_child_entry *n1,
- const struct strbuf_child_entry *n2) {
- return n1->c - n2->c;
-}
-
-static void bubbleinsert(struct strbuf_node *node,
- uint8_t c,
- struct strbuf_node *node_child) {
-
- struct strbuf_child_entry new = {
- .c = c,
- .child = node_child,
- };
- int left = 0, right = node->children_count;
-
- while (right > left) {
- int middle = (right + left) / 2 ;
- if (strbuf_children_cmp(&node->children[middle], &new) <= 0)
- left = middle + 1;
- else
- right = middle;
- }
-
- memmove(node->children + left + 1, node->children + left,
- sizeof(struct strbuf_child_entry) * (node->children_count - left));
- node->children[left] = new;
-
- node->children_count ++;
-}
-
-/* add string, return the index/offset into the buffer */
-ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
- uint8_t c;
- struct strbuf_node *node;
- size_t depth;
- char *buf_new;
- struct strbuf_child_entry *child;
- struct strbuf_node *node_child;
- ssize_t off;
-
- if (!str->root)
- return -EINVAL;
-
- /* search string; start from last character to find possibly matching tails */
- if (len == 0)
- return 0;
- str->in_count++;
- str->in_len += len;
-
- node = str->root;
- c = s[len-1];
- for (depth = 0; depth <= len; depth++) {
- struct strbuf_child_entry search;
-
- /* match against current node */
- off = node->value_off + node->value_len - len;
- if (depth == len || (node->value_len >= len && memcmp(str->buf + off, s, len) == 0)) {
- str->dedup_len += len;
- str->dedup_count++;
- return off;
- }
-
- /* lookup child node */
- c = s[len - 1 - depth];
- search.c = c;
- child = bsearch(&search, node->children, node->children_count,
- sizeof(struct strbuf_child_entry),
- (__compar_fn_t) strbuf_children_cmp);
- if (!child)
- break;
- node = child->child;
- }
-
- /* add new string */
- buf_new = realloc(str->buf, str->len + len+1);
- if (!buf_new)
- return -ENOMEM;
- str->buf = buf_new;
- off = str->len;
- memcpy(str->buf + off, s, len);
- str->len += len;
- str->buf[str->len++] = '\0';
-
- /* new node */
- node_child = new0(struct strbuf_node, 1);
- if (!node_child)
- return -ENOMEM;
- node_child->value_off = off;
- node_child->value_len = len;
-
- /* extend array, add new entry, sort for bisection */
- child = realloc(node->children, (node->children_count + 1) * sizeof(struct strbuf_child_entry));
- if (!child) {
- free(node_child);
- return -ENOMEM;
- }
-
- str->nodes_count++;
-
- node->children = child;
- bubbleinsert(node, c, node_child);
-
- return off;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Kay Sievers <kay@vrfy.org>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdint.h>
-
-struct strbuf {
- char *buf;
- size_t len;
- struct strbuf_node *root;
-
- size_t nodes_count;
- size_t in_count;
- size_t in_len;
- size_t dedup_len;
- size_t dedup_count;
-};
-
-struct strbuf_node {
- size_t value_off;
- size_t value_len;
-
- struct strbuf_child_entry *children;
- uint8_t children_count;
-};
-
-struct strbuf_child_entry {
- uint8_t c;
- struct strbuf_node *child;
-};
-
-struct strbuf *strbuf_new(void);
-ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len);
-void strbuf_complete(struct strbuf *str);
-void strbuf_cleanup(struct strbuf *str);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Kay Sievers
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-/*
- * Concatenates/copies strings. In any case, terminates in all cases
- * with '\0' * and moves the @dest pointer forward to the added '\0'.
- * Returns the * remaining size, and 0 if the string was truncated.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include "strxcpyx.h"
-
-size_t strpcpy(char **dest, size_t size, const char *src) {
- size_t len;
-
- len = strlen(src);
- if (len >= size) {
- if (size > 1)
- *dest = mempcpy(*dest, src, size-1);
- size = 0;
- } else {
- if (len > 0) {
- *dest = mempcpy(*dest, src, len);
- size -= len;
- }
- }
- *dest[0] = '\0';
- return size;
-}
-
-size_t strpcpyf(char **dest, size_t size, const char *src, ...) {
- va_list va;
- int i;
-
- va_start(va, src);
- i = vsnprintf(*dest, size, src, va);
- if (i < (int)size) {
- *dest += i;
- size -= i;
- } else {
- *dest += size;
- size = 0;
- }
- va_end(va);
- *dest[0] = '\0';
- return size;
-}
-
-size_t strpcpyl(char **dest, size_t size, const char *src, ...) {
- va_list va;
-
- va_start(va, src);
- do {
- size = strpcpy(dest, size, src);
- src = va_arg(va, char *);
- } while (src != NULL);
- va_end(va);
- return size;
-}
-
-size_t strscpy(char *dest, size_t size, const char *src) {
- char *s;
-
- s = dest;
- return strpcpy(&s, size, src);
-}
-
-size_t strscpyl(char *dest, size_t size, const char *src, ...) {
- va_list va;
- char *s;
-
- va_start(va, src);
- s = dest;
- do {
- size = strpcpy(&s, size, src);
- src = va_arg(va, char *);
- } while (src != NULL);
- va_end(va);
-
- return size;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Harald Hoyer, Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/stat.h>
-#include <stdbool.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/mount.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include "util.h"
-#include "path-util.h"
-#include "switch-root.h"
-#include "mkdir.h"
-#include "base-filesystem.h"
-#include "missing.h"
-
-int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, unsigned long mountflags) {
-
- /* Don't try to unmount/move the old "/", there's no way to do it. */
- static const char move_mounts[] =
- "/dev\0"
- "/proc\0"
- "/sys\0"
- "/run\0";
-
- _cleanup_close_ int old_root_fd = -1;
- struct stat new_root_stat;
- bool old_root_remove;
- const char *i, *temporary_old_root;
-
- if (path_equal(new_root, "/"))
- return 0;
-
- temporary_old_root = strjoina(new_root, oldroot);
- mkdir_p_label(temporary_old_root, 0755);
-
- old_root_remove = in_initrd();
-
- if (stat(new_root, &new_root_stat) < 0)
- return log_error_errno(errno, "Failed to stat directory %s: %m", new_root);
-
- /* Work-around for kernel design: the kernel refuses switching
- * root if any file systems are mounted MS_SHARED. Hence
- * remount them MS_PRIVATE here as a work-around.
- *
- * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
- if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
- log_warning_errno(errno, "Failed to make \"/\" private mount: %m");
-
- NULSTR_FOREACH(i, move_mounts) {
- char new_mount[PATH_MAX];
- struct stat sb;
-
- snprintf(new_mount, sizeof(new_mount), "%s%s", new_root, i);
-
- mkdir_p_label(new_mount, 0755);
-
- if ((stat(new_mount, &sb) < 0) ||
- sb.st_dev != new_root_stat.st_dev) {
-
- /* Mount point seems to be mounted already or
- * stat failed. Unmount the old mount
- * point. */
- if (umount2(i, MNT_DETACH) < 0)
- log_warning_errno(errno, "Failed to unmount %s: %m", i);
- continue;
- }
-
- if (mount(i, new_mount, NULL, mountflags, NULL) < 0) {
- if (mountflags & MS_MOVE) {
- log_error_errno(errno, "Failed to move mount %s to %s, forcing unmount: %m", i, new_mount);
-
- if (umount2(i, MNT_FORCE) < 0)
- log_warning_errno(errno, "Failed to unmount %s: %m", i);
- }
- if (mountflags & MS_BIND)
- log_error_errno(errno, "Failed to bind mount %s to %s: %m", i, new_mount);
-
- }
- }
-
- /* Do not fail, if base_filesystem_create() fails. Not all
- * switch roots are like base_filesystem_create() wants them
- * to look like. They might even boot, if they are RO and
- * don't have the FS layout. Just ignore the error and
- * switch_root() nevertheless. */
- (void) base_filesystem_create(new_root);
-
- if (chdir(new_root) < 0)
- return log_error_errno(errno, "Failed to change directory to %s: %m", new_root);
-
- if (old_root_remove) {
- old_root_fd = open("/", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
- if (old_root_fd < 0)
- log_warning_errno(errno, "Failed to open root directory: %m");
- }
-
- /* We first try a pivot_root() so that we can umount the old
- * root dir. In many cases (i.e. where rootfs is /), that's
- * not possible however, and hence we simply overmount root */
- if (pivot_root(new_root, temporary_old_root) >= 0) {
-
- /* Immediately get rid of the old root, if detach_oldroot is set.
- * Since we are running off it we need to do this lazily. */
- if (detach_oldroot && umount2(oldroot, MNT_DETACH) < 0)
- log_error_errno(errno, "Failed to lazily umount old root dir %s, %s: %m",
- oldroot,
- errno == ENOENT ? "ignoring" : "leaving it around");
-
- } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0)
- return log_error_errno(errno, "Failed to mount moving %s to /: %m", new_root);
-
- if (chroot(".") < 0)
- return log_error_errno(errno, "Failed to change root: %m");
-
- if (chdir("/") < 0)
- return log_error_errno(errno, "Failed to change directory: %m");
-
- if (old_root_fd >= 0) {
- struct stat rb;
-
- if (fstat(old_root_fd, &rb) < 0)
- log_warning_errno(errno, "Failed to stat old root directory, leaving: %m");
- else {
- rm_rf_children(old_root_fd, false, false, &rb);
- old_root_fd = -1;
- }
- }
-
- return 0;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Harald Hoyer, Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, unsigned long mountflags);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <stdbool.h>
-#include <errno.h>
-#include <string.h>
-#include <stdio.h>
-#include <limits.h>
-#include <getopt.h>
-
-#include "log.h"
-#include "util.h"
-#include "fileio.h"
-#include "build.h"
-#include "sysctl-util.h"
-
-char *sysctl_normalize(char *s) {
- char *n;
-
- n = strpbrk(s, "/.");
- /* If the first separator is a slash, the path is
- * assumed to be normalized and slashes remain slashes
- * and dots remains dots. */
- if (!n || *n == '/')
- return s;
-
- /* Otherwise, dots become slashes and slashes become
- * dots. Fun. */
- while (n) {
- if (*n == '.')
- *n = '/';
- else
- *n = '.';
-
- n = strpbrk(n + 1, "/.");
- }
-
- return s;
-}
-
-int sysctl_write(const char *property, const char *value) {
- _cleanup_free_ char *p = NULL;
- char *n;
-
- log_debug("Setting '%s' to '%s'", property, value);
-
- p = new(char, strlen("/proc/sys/") + strlen(property) + 1);
- if (!p)
- return log_oom();
-
- n = stpcpy(p, "/proc/sys/");
- strcpy(n, property);
-
- return write_string_file(p, value);
-}
-
-int sysctl_read(const char *property, char **content) {
- _cleanup_free_ char *p = NULL;
- char *n;
-
- p = new(char, strlen("/proc/sys/") + strlen(property) + 1);
- if (!p)
- return log_oom();
-
- n = stpcpy(p, "/proc/sys/");
- strcpy(n, property);
-
- return read_full_file(p, content, NULL);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-char *sysctl_normalize(char *s);
-int sysctl_read(const char *property, char **value);
-int sysctl_write(const char *property, const char *value);
-
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include "libudev.h"
+#include <libudev.h>
#include "util.h"
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev*, udev_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_device*, udev_device_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_enumerate*, udev_enumerate_unref);
+// UNNEEDED DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_unref);
+// UNNEEDED DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_rules*, udev_rules_unref);
+// UNNEEDED DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl*, udev_ctrl_unref);
+// UNNEEDED DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_connection*, udev_ctrl_connection_unref);
+// UNNEEDED DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_msg*, udev_ctrl_msg_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref);
#define _cleanup_udev_unref_ _cleanup_(udev_unrefp)
#define _cleanup_udev_device_unref_ _cleanup_(udev_device_unrefp)
#define _cleanup_udev_enumerate_unref_ _cleanup_(udev_enumerate_unrefp)
+// UNNEEDED #define _cleanup_udev_event_unref_ _cleanup_(udev_event_unrefp)
+// UNNEEDED #define _cleanup_udev_rules_unref_ _cleanup_(udev_rules_unrefp)
+// UNNEEDED #define _cleanup_udev_ctrl_unref_ _cleanup_(udev_ctrl_unrefp)
+// UNNEEDED #define _cleanup_udev_ctrl_connection_unref_ _cleanup_(udev_ctrl_connection_unrefp)
+// UNNEEDED #define _cleanup_udev_ctrl_msg_unref_ _cleanup_(udev_ctrl_msg_unrefp)
#define _cleanup_udev_monitor_unref_ _cleanup_(udev_monitor_unrefp)
#define _cleanup_udev_list_cleanup_ _cleanup_(udev_list_cleanup)
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-#include "uid-range.h"
-
-static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) {
- assert(range);
-
- return range->start <= start + nr &&
- range->start + range->nr >= start;
-}
-
-static void uid_range_coalesce(UidRange **p, unsigned *n) {
- unsigned i, j;
-
- assert(p);
- assert(n);
-
- for (i = 0; i < *n; i++) {
- for (j = i + 1; j < *n; j++) {
- UidRange *x = (*p)+i, *y = (*p)+j;
-
- if (uid_range_intersect(x, y->start, y->nr)) {
- uid_t begin, end;
-
- begin = MIN(x->start, y->start);
- end = MAX(x->start + x->nr, y->start + y->nr);
-
- x->start = begin;
- x->nr = end - begin;
-
- if (*n > j+1)
- memmove(y, y+1, sizeof(UidRange) * (*n - j -1));
-
- (*n) --;
- j--;
- }
- }
- }
-
-}
-
-static int uid_range_compare(const void *a, const void *b) {
- const UidRange *x = a, *y = b;
-
- if (x->start < y->start)
- return -1;
- if (x->start > y->start)
- return 1;
-
- if (x->nr < y->nr)
- return -1;
- if (x->nr > y->nr)
- return 1;
-
- return 0;
-}
-
-int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) {
- bool found = false;
- UidRange *x;
- unsigned i;
-
- assert(p);
- assert(n);
-
- if (nr <= 0)
- return 0;
-
- for (i = 0; i < *n; i++) {
- x = (*p) + i;
- if (uid_range_intersect(x, start, nr)) {
- found = true;
- break;
- }
- }
-
- if (found) {
- uid_t begin, end;
-
- begin = MIN(x->start, start);
- end = MAX(x->start + x->nr, start + nr);
-
- x->start = begin;
- x->nr = end - begin;
- } else {
- UidRange *t;
-
- t = realloc(*p, sizeof(UidRange) * (*n + 1));
- if (!t)
- return -ENOMEM;
-
- *p = t;
- x = t + ((*n) ++);
-
- x->start = start;
- x->nr = nr;
- }
-
- qsort(*p, *n, sizeof(UidRange), uid_range_compare);
- uid_range_coalesce(p, n);
-
- return *n;
-}
-
-int uid_range_add_str(UidRange **p, unsigned *n, const char *s) {
- uid_t start, nr;
- const char *t;
- int r;
-
- assert(p);
- assert(n);
- assert(s);
-
- t = strchr(s, '-');
- if (t) {
- char *b;
- uid_t end;
-
- b = strndupa(s, t - s);
- r = parse_uid(b, &start);
- if (r < 0)
- return r;
-
- r = parse_uid(t+1, &end);
- if (r < 0)
- return r;
-
- if (end < start)
- return -EINVAL;
-
- nr = end - start + 1;
- } else {
- r = parse_uid(s, &start);
- if (r < 0)
- return r;
-
- nr = 1;
- }
-
- return uid_range_add(p, n, start, nr);
-}
-
-int uid_range_next_lower(const UidRange *p, unsigned n, uid_t *uid) {
- uid_t closest = UID_INVALID, candidate;
- unsigned i;
-
- assert(p);
- assert(uid);
-
- candidate = *uid - 1;
-
- for (i = 0; i < n; i++) {
- uid_t begin, end;
-
- begin = p[i].start;
- end = p[i].start + p[i].nr - 1;
-
- if (candidate >= begin && candidate <= end) {
- *uid = candidate;
- return 1;
- }
-
- if (end < candidate)
- closest = end;
- }
-
- if (closest == UID_INVALID)
- return -EBUSY;
-
- *uid = closest;
- return 1;
-}
-
-bool uid_range_contains(const UidRange *p, unsigned n, uid_t uid) {
- unsigned i;
-
- assert(p);
- assert(uid);
-
- for (i = 0; i < n; i++)
- if (uid >= p[i].start && uid < p[i].start + p[i].nr)
- return true;
-
- return false;
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <sys/types.h>
-
-typedef struct UidRange {
- uid_t start, nr;
-} UidRange;
-
-int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr);
-int uid_range_add_str(UidRange **p, unsigned *n, const char *s);
-
-int uid_range_next_lower(const UidRange *p, unsigned n, uid_t *uid);
-bool uid_range_contains(const UidRange *p, unsigned n, uid_t uid);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdint.h>
-
-static inline uint16_t unaligned_read_be16(const void *_u) {
- const uint8_t *u = _u;
-
- return (((uint16_t) u[0]) << 8) |
- ((uint16_t) u[1]);
-}
-
-static inline uint32_t unaligned_read_be32(const void *_u) {
- const uint8_t *u = _u;
-
- return (((uint32_t) unaligned_read_be16(u)) << 16) |
- ((uint32_t) unaligned_read_be16(u + 2));
-}
-
-static inline uint64_t unaligned_read_be64(const void *_u) {
- const uint8_t *u = _u;
-
- return (((uint64_t) unaligned_read_be32(u)) << 32) |
- ((uint64_t) unaligned_read_be32(u + 4));
-}
-
-static inline void unaligned_write_be16(void *_u, uint16_t a) {
- uint8_t *u = _u;
-
- u[0] = (uint8_t) (a >> 8);
- u[1] = (uint8_t) a;
-}
-
-static inline void unaligned_write_be32(void *_u, uint32_t a) {
- uint8_t *u = _u;
-
- unaligned_write_be16(u, (uint16_t) (a >> 16));
- unaligned_write_be16(u + 2, (uint16_t) a);
-}
-
-static inline void unaligned_write_be64(void *_u, uint64_t a) {
- uint8_t *u = _u;
-
- unaligned_write_be32(u, (uint32_t) (a >> 32));
- unaligned_write_be32(u + 4, (uint32_t) a);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <string.h>
-
-#include "path-util.h"
-#include "bus-label.h"
-#include "util.h"
-#include "unit-name.h"
-#include "def.h"
-#include "strv.h"
-
-#define VALID_CHARS \
- DIGITS LETTERS \
- ":-_.\\"
-
-static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
- [UNIT_SERVICE] = "service",
- [UNIT_SOCKET] = "socket",
- [UNIT_BUSNAME] = "busname",
- [UNIT_TARGET] = "target",
- [UNIT_SNAPSHOT] = "snapshot",
- [UNIT_DEVICE] = "device",
- [UNIT_MOUNT] = "mount",
- [UNIT_AUTOMOUNT] = "automount",
- [UNIT_SWAP] = "swap",
- [UNIT_TIMER] = "timer",
- [UNIT_PATH] = "path",
- [UNIT_SLICE] = "slice",
- [UNIT_SCOPE] = "scope"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
-
-static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
- [UNIT_STUB] = "stub",
- [UNIT_LOADED] = "loaded",
- [UNIT_NOT_FOUND] = "not-found",
- [UNIT_ERROR] = "error",
- [UNIT_MERGED] = "merged",
- [UNIT_MASKED] = "masked"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
-
-bool unit_name_is_valid(const char *n, enum template_valid template_ok) {
- const char *e, *i, *at;
-
- /* Valid formats:
- *
- * string@instance.suffix
- * string.suffix
- */
-
- assert(IN_SET(template_ok, TEMPLATE_VALID, TEMPLATE_INVALID));
-
- if (isempty(n))
- return false;
-
- if (strlen(n) >= UNIT_NAME_MAX)
- return false;
-
- e = strrchr(n, '.');
- if (!e || e == n)
- return false;
-
- if (unit_type_from_string(e + 1) < 0)
- return false;
-
- for (i = n, at = NULL; i < e; i++) {
-
- if (*i == '@' && !at)
- at = i;
-
- if (!strchr("@" VALID_CHARS, *i))
- return false;
- }
-
- if (at) {
- if (at == n)
- return false;
-
- if (template_ok != TEMPLATE_VALID && at+1 == e)
- return false;
- }
-
- return true;
-}
-
-bool unit_instance_is_valid(const char *i) {
-
- /* The max length depends on the length of the string, so we
- * don't really check this here. */
-
- if (isempty(i))
- return false;
-
- /* We allow additional @ in the instance string, we do not
- * allow them in the prefix! */
-
- return in_charset(i, "@" VALID_CHARS);
-}
-
-bool unit_prefix_is_valid(const char *p) {
-
- /* We don't allow additional @ in the instance string */
-
- if (isempty(p))
- return false;
-
- return in_charset(p, VALID_CHARS);
-}
-
-int unit_name_to_instance(const char *n, char **instance) {
- const char *p, *d;
- char *i;
-
- assert(n);
- assert(instance);
-
- /* Everything past the first @ and before the last . is the instance */
- p = strchr(n, '@');
- if (!p) {
- *instance = NULL;
- return 0;
- }
-
- d = strrchr(n, '.');
- if (!d)
- return -EINVAL;
- if (d < p)
- return -EINVAL;
-
- i = strndup(p+1, d-p-1);
- if (!i)
- return -ENOMEM;
-
- *instance = i;
- return 1;
-}
-
-char *unit_name_to_prefix_and_instance(const char *n) {
- const char *d;
-
- assert(n);
-
- assert_se(d = strrchr(n, '.'));
- return strndup(n, d - n);
-}
-
-char *unit_name_to_prefix(const char *n) {
- const char *p;
-
- assert(n);
-
- p = strchr(n, '@');
- if (p)
- return strndup(n, p - n);
-
- return unit_name_to_prefix_and_instance(n);
-}
-
-char *unit_name_change_suffix(const char *n, const char *suffix) {
- char *e, *r;
- size_t a, b;
-
- assert(n);
- assert(suffix);
- assert(suffix[0] == '.');
-
- assert_se(e = strrchr(n, '.'));
- a = e - n;
- b = strlen(suffix);
-
- r = new(char, a + b + 1);
- if (!r)
- return NULL;
-
- strcpy(mempcpy(r, n, a), suffix);
- return r;
-}
-
-char *unit_name_build(const char *prefix, const char *instance, const char *suffix) {
- assert(prefix);
- assert(suffix);
-
- if (!instance)
- return strappend(prefix, suffix);
-
- return strjoin(prefix, "@", instance, suffix, NULL);
-}
-
-static char *do_escape_char(char c, char *t) {
- assert(t);
-
- *(t++) = '\\';
- *(t++) = 'x';
- *(t++) = hexchar(c >> 4);
- *(t++) = hexchar(c);
-
- return t;
-}
-
-static char *do_escape(const char *f, char *t) {
- assert(f);
- assert(t);
-
- /* do not create units with a leading '.', like for "/.dotdir" mount points */
- if (*f == '.') {
- t = do_escape_char(*f, t);
- f++;
- }
-
- for (; *f; f++) {
- if (*f == '/')
- *(t++) = '-';
- else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f))
- t = do_escape_char(*f, t);
- else
- *(t++) = *f;
- }
-
- return t;
-}
-
-static char *do_escape_mangle(const char *f, enum unit_name_mangle allow_globs, char *t) {
- const char *valid_chars;
-
- assert(f);
- assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB));
- assert(t);
-
- /* We'll only escape the obvious characters here, to play
- * safe. */
-
- valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS;
-
- for (; *f; f++) {
- if (*f == '/')
- *(t++) = '-';
- else if (!strchr(valid_chars, *f))
- t = do_escape_char(*f, t);
- else
- *(t++) = *f;
- }
-
- return t;
-}
-
-char *unit_name_escape(const char *f) {
- char *r, *t;
-
- assert(f);
-
- r = new(char, strlen(f)*4+1);
- if (!r)
- return NULL;
-
- t = do_escape(f, r);
- *t = 0;
-
- return r;
-}
-
-char *unit_name_unescape(const char *f) {
- char *r, *t;
-
- assert(f);
-
- r = strdup(f);
- if (!r)
- return NULL;
-
- for (t = r; *f; f++) {
- if (*f == '-')
- *(t++) = '/';
- else if (*f == '\\') {
- int a, b;
-
- if (f[1] != 'x' ||
- (a = unhexchar(f[2])) < 0 ||
- (b = unhexchar(f[3])) < 0) {
- /* Invalid escape code, let's take it literal then */
- *(t++) = '\\';
- } else {
- *(t++) = (char) ((a << 4) | b);
- f += 3;
- }
- } else
- *(t++) = *f;
- }
-
- *t = 0;
-
- return r;
-}
-
-char *unit_name_path_escape(const char *f) {
- _cleanup_free_ char *p = NULL;
-
- assert(f);
-
- p = strdup(f);
- if (!p)
- return NULL;
-
- path_kill_slashes(p);
-
- if (STR_IN_SET(p, "/", ""))
- return strdup("-");
-
- return unit_name_escape(p[0] == '/' ? p + 1 : p);
-}
-
-char *unit_name_path_unescape(const char *f) {
- char *e, *w;
-
- assert(f);
-
- e = unit_name_unescape(f);
- if (!e)
- return NULL;
-
- if (e[0] != '/') {
- w = strappend("/", e);
- free(e);
- return w;
- }
-
- return e;
-}
-
-bool unit_name_is_template(const char *n) {
- const char *p, *e;
-
- assert(n);
-
- p = strchr(n, '@');
- if (!p)
- return false;
-
- e = strrchr(p+1, '.');
- if (!e)
- return false;
-
- return e == p + 1;
-}
-
-bool unit_name_is_instance(const char *n) {
- const char *p, *e;
-
- assert(n);
-
- p = strchr(n, '@');
- if (!p)
- return false;
-
- e = strrchr(p+1, '.');
- if (!e)
- return false;
-
- return e > p + 1;
-}
-
-char *unit_name_replace_instance(const char *f, const char *i) {
- const char *p, *e;
- char *r;
- size_t a, b;
-
- assert(f);
- assert(i);
-
- p = strchr(f, '@');
- if (!p)
- return strdup(f);
-
- e = strrchr(f, '.');
- if (!e)
- e = strchr(f, 0);
-
- a = p - f;
- b = strlen(i);
-
- r = new(char, a + 1 + b + strlen(e) + 1);
- if (!r)
- return NULL;
-
- strcpy(mempcpy(mempcpy(r, f, a + 1), i, b), e);
- return r;
-}
-
-char *unit_name_template(const char *f) {
- const char *p, *e;
- char *r;
- size_t a;
-
- assert(f);
-
- p = strchr(f, '@');
- if (!p)
- return strdup(f);
-
- e = strrchr(f, '.');
- if (!e)
- e = strchr(f, 0);
-
- a = p - f;
-
- r = new(char, a + 1 + strlen(e) + 1);
- if (!r)
- return NULL;
-
- strcpy(mempcpy(r, f, a + 1), e);
- return r;
-}
-
-char *unit_name_from_path(const char *path, const char *suffix) {
- _cleanup_free_ char *p = NULL;
-
- assert(path);
- assert(suffix);
-
- p = unit_name_path_escape(path);
- if (!p)
- return NULL;
-
- return strappend(p, suffix);
-}
-
-char *unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix) {
- _cleanup_free_ char *p = NULL;
-
- assert(prefix);
- assert(path);
- assert(suffix);
-
- p = unit_name_path_escape(path);
- if (!p)
- return NULL;
-
- return strjoin(prefix, "@", p, suffix, NULL);
-}
-
-char *unit_name_to_path(const char *name) {
- _cleanup_free_ char *w = NULL;
-
- assert(name);
-
- w = unit_name_to_prefix(name);
- if (!w)
- return NULL;
-
- return unit_name_path_unescape(w);
-}
-
-char *unit_dbus_path_from_name(const char *name) {
- _cleanup_free_ char *e = NULL;
-
- assert(name);
-
- e = bus_label_escape(name);
- if (!e)
- return NULL;
-
- return strappend("/org/freedesktop/systemd1/unit/", e);
-}
-
-int unit_name_from_dbus_path(const char *path, char **name) {
- const char *e;
- char *n;
-
- e = startswith(path, "/org/freedesktop/systemd1/unit/");
- if (!e)
- return -EINVAL;
-
- n = bus_label_unescape(e);
- if (!n)
- return -ENOMEM;
-
- *name = n;
- return 0;
-}
-
-/**
- * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
- * /blah/blah is converted to blah-blah.mount, anything else is left alone,
- * except that @suffix is appended if a valid unit suffix is not present.
- *
- * If @allow_globs, globs characters are preserved. Otherwise they are escaped.
- */
-char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix) {
- char *r, *t;
-
- assert(name);
- assert(suffix);
- assert(suffix[0] == '.');
-
- if (is_device_path(name))
- return unit_name_from_path(name, ".device");
-
- if (path_is_absolute(name))
- return unit_name_from_path(name, ".mount");
-
- r = new(char, strlen(name) * 4 + strlen(suffix) + 1);
- if (!r)
- return NULL;
-
- t = do_escape_mangle(name, allow_globs, r);
-
- if (unit_name_to_type(name) < 0)
- strcpy(t, suffix);
- else
- *t = 0;
-
- return r;
-}
-
-UnitType unit_name_to_type(const char *n) {
- const char *e;
-
- assert(n);
-
- e = strrchr(n, '.');
- if (!e)
- return _UNIT_TYPE_INVALID;
-
- return unit_type_from_string(e + 1);
-}
-
-int build_subslice(const char *slice, const char*name, char **subslice) {
- char *ret;
-
- assert(slice);
- assert(name);
- assert(subslice);
-
- if (streq(slice, "-.slice"))
- ret = strappend(name, ".slice");
- else {
- char *e;
-
- e = endswith(slice, ".slice");
- if (!e)
- return -EINVAL;
-
- ret = new(char, (e - slice) + 1 + strlen(name) + 6 + 1);
- if (!ret)
- return -ENOMEM;
-
- stpcpy(stpcpy(stpcpy(mempcpy(ret, slice, e - slice), "-"), name), ".slice");
- }
-
- *subslice = ret;
- return 0;
-}
-
-static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
- [UNIT_REQUIRES] = "Requires",
- [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
- [UNIT_REQUISITE] = "Requisite",
- [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
- [UNIT_WANTS] = "Wants",
- [UNIT_BINDS_TO] = "BindsTo",
- [UNIT_PART_OF] = "PartOf",
- [UNIT_REQUIRED_BY] = "RequiredBy",
- [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
- [UNIT_WANTED_BY] = "WantedBy",
- [UNIT_BOUND_BY] = "BoundBy",
- [UNIT_CONSISTS_OF] = "ConsistsOf",
- [UNIT_CONFLICTS] = "Conflicts",
- [UNIT_CONFLICTED_BY] = "ConflictedBy",
- [UNIT_BEFORE] = "Before",
- [UNIT_AFTER] = "After",
- [UNIT_ON_FAILURE] = "OnFailure",
- [UNIT_TRIGGERS] = "Triggers",
- [UNIT_TRIGGERED_BY] = "TriggeredBy",
- [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
- [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom",
- [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf",
- [UNIT_REFERENCES] = "References",
- [UNIT_REFERENCED_BY] = "ReferencedBy",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-#include "macro.h"
-
-#define UNIT_NAME_MAX 256
-
-typedef enum UnitType UnitType;
-typedef enum UnitLoadState UnitLoadState;
-typedef enum UnitDependency UnitDependency;
-
-enum UnitType {
- UNIT_SERVICE = 0,
- UNIT_SOCKET,
- UNIT_BUSNAME,
- UNIT_TARGET,
- UNIT_SNAPSHOT,
- UNIT_DEVICE,
- UNIT_MOUNT,
- UNIT_AUTOMOUNT,
- UNIT_SWAP,
- UNIT_TIMER,
- UNIT_PATH,
- UNIT_SLICE,
- UNIT_SCOPE,
- _UNIT_TYPE_MAX,
- _UNIT_TYPE_INVALID = -1
-};
-
-enum UnitLoadState {
- UNIT_STUB = 0,
- UNIT_LOADED,
- UNIT_NOT_FOUND,
- UNIT_ERROR,
- UNIT_MERGED,
- UNIT_MASKED,
- _UNIT_LOAD_STATE_MAX,
- _UNIT_LOAD_STATE_INVALID = -1
-};
-
-enum UnitDependency {
- /* Positive dependencies */
- UNIT_REQUIRES,
- UNIT_REQUIRES_OVERRIDABLE,
- UNIT_REQUISITE,
- UNIT_REQUISITE_OVERRIDABLE,
- UNIT_WANTS,
- UNIT_BINDS_TO,
- UNIT_PART_OF,
-
- /* Inverse of the above */
- UNIT_REQUIRED_BY, /* inverse of 'requires' and 'requisite' is 'required_by' */
- UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'requires_overridable' and 'requisite_overridable' is 'soft_required_by' */
- UNIT_WANTED_BY, /* inverse of 'wants' */
- UNIT_BOUND_BY, /* inverse of 'binds_to' */
- UNIT_CONSISTS_OF, /* inverse of 'part_of' */
-
- /* Negative dependencies */
- UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicted_by' */
- UNIT_CONFLICTED_BY,
-
- /* Order */
- UNIT_BEFORE, /* inverse of 'before' is 'after' and vice versa */
- UNIT_AFTER,
-
- /* On Failure */
- UNIT_ON_FAILURE,
-
- /* Triggers (i.e. a socket triggers a service) */
- UNIT_TRIGGERS,
- UNIT_TRIGGERED_BY,
-
- /* Propagate reloads */
- UNIT_PROPAGATES_RELOAD_TO,
- UNIT_RELOAD_PROPAGATED_FROM,
-
- /* Joins namespace of */
- UNIT_JOINS_NAMESPACE_OF,
-
- /* Reference information for GC logic */
- UNIT_REFERENCES, /* Inverse of 'references' is 'referenced_by' */
- UNIT_REFERENCED_BY,
-
- _UNIT_DEPENDENCY_MAX,
- _UNIT_DEPENDENCY_INVALID = -1
-};
-
-const char *unit_type_to_string(UnitType i) _const_;
-UnitType unit_type_from_string(const char *s) _pure_;
-
-const char *unit_load_state_to_string(UnitLoadState i) _const_;
-UnitLoadState unit_load_state_from_string(const char *s) _pure_;
-
-int unit_name_to_instance(const char *n, char **instance);
-char* unit_name_to_prefix(const char *n);
-char* unit_name_to_prefix_and_instance(const char *n);
-
-enum template_valid {
- TEMPLATE_INVALID,
- TEMPLATE_VALID,
-};
-
-bool unit_name_is_valid(const char *n, enum template_valid template_ok) _pure_;
-bool unit_prefix_is_valid(const char *p) _pure_;
-bool unit_instance_is_valid(const char *i) _pure_;
-
-UnitType unit_name_to_type(const char *n) _pure_;
-
-char *unit_name_change_suffix(const char *n, const char *suffix);
-
-char *unit_name_build(const char *prefix, const char *instance, const char *suffix);
-
-char *unit_name_escape(const char *f);
-char *unit_name_unescape(const char *f);
-char *unit_name_path_escape(const char *f);
-char *unit_name_path_unescape(const char *f);
-
-bool unit_name_is_template(const char *n) _pure_;
-bool unit_name_is_instance(const char *n) _pure_;
-
-char *unit_name_replace_instance(const char *f, const char *i);
-
-char *unit_name_template(const char *f);
-
-char *unit_name_from_path(const char *path, const char *suffix);
-char *unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix);
-char *unit_name_to_path(const char *name);
-
-char *unit_dbus_path_from_name(const char *name);
-int unit_name_from_dbus_path(const char *path, char **name);
-
-enum unit_name_mangle {
- MANGLE_NOGLOB,
- MANGLE_GLOB,
-};
-
-char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix);
-static inline char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
- return unit_name_mangle_with_suffix(name, allow_globs, ".service");
-}
-
-int build_subslice(const char *slice, const char*name, char **subslice);
-
-const char *unit_dependency_to_string(UnitDependency i) _const_;
-UnitDependency unit_dependency_from_string(const char *s) _pure_;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <utmpx.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/utsname.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <poll.h>
-
-#include "macro.h"
-#include "path-util.h"
-#include "utmp-wtmp.h"
-
-int utmp_get_runlevel(int *runlevel, int *previous) {
- struct utmpx *found, lookup = { .ut_type = RUN_LVL };
- int r;
- const char *e;
-
- assert(runlevel);
-
- /* If these values are set in the environment this takes
- * precedence. Presumably, sysvinit does this to work around a
- * race condition that would otherwise exist where we'd always
- * go to disk and hence might read runlevel data that might be
- * very new and does not apply to the current script being
- * executed. */
-
- e = getenv("RUNLEVEL");
- if (e && e[0] > 0) {
- *runlevel = e[0];
-
- if (previous) {
- /* $PREVLEVEL seems to be an Upstart thing */
-
- e = getenv("PREVLEVEL");
- if (e && e[0] > 0)
- *previous = e[0];
- else
- *previous = 0;
- }
-
- return 0;
- }
-
- if (utmpxname(_PATH_UTMPX) < 0)
- return -errno;
-
- setutxent();
-
- found = getutxid(&lookup);
- if (!found)
- r = -errno;
- else {
- int a, b;
-
- a = found->ut_pid & 0xFF;
- b = (found->ut_pid >> 8) & 0xFF;
-
- *runlevel = a;
- if (previous)
- *previous = b;
-
- r = 0;
- }
-
- endutxent();
-
- return r;
-}
-
-static void init_timestamp(struct utmpx *store, usec_t t) {
- assert(store);
-
- if (t <= 0)
- t = now(CLOCK_REALTIME);
-
- store->ut_tv.tv_sec = t / USEC_PER_SEC;
- store->ut_tv.tv_usec = t % USEC_PER_SEC;
-}
-
-static void init_entry(struct utmpx *store, usec_t t) {
- struct utsname uts = {};
-
- assert(store);
-
- init_timestamp(store, t);
-
- if (uname(&uts) >= 0)
- strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
-
- strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
- strncpy(store->ut_id, "~~", sizeof(store->ut_id));
-}
-
-static int write_entry_utmp(const struct utmpx *store) {
- int r;
-
- assert(store);
-
- /* utmp is similar to wtmp, but there is only one entry for
- * each entry type resp. user; i.e. basically a key/value
- * table. */
-
- if (utmpxname(_PATH_UTMPX) < 0)
- return -errno;
-
- setutxent();
-
- if (!pututxline(store))
- r = -errno;
- else
- r = 0;
-
- endutxent();
-
- return r;
-}
-
-static int write_entry_wtmp(const struct utmpx *store) {
- assert(store);
-
- /* wtmp is a simple append-only file where each entry is
- simply appended to the end; i.e. basically a log. */
-
- errno = 0;
- updwtmpx(_PATH_WTMPX, store);
- return -errno;
-}
-
-static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
- int r, s;
-
- r = write_entry_utmp(store_utmp);
- s = write_entry_wtmp(store_wtmp);
-
- if (r >= 0)
- r = s;
-
- /* If utmp/wtmp have been disabled, that's a good thing, hence
- * ignore the errors */
- if (r == -ENOENT)
- r = 0;
-
- return r;
-}
-
-static int write_entry_both(const struct utmpx *store) {
- return write_utmp_wtmp(store, store);
-}
-
-int utmp_put_shutdown(void) {
- struct utmpx store = {};
-
- init_entry(&store, 0);
-
- store.ut_type = RUN_LVL;
- strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
-
- return write_entry_both(&store);
-}
-
-int utmp_put_reboot(usec_t t) {
- struct utmpx store = {};
-
- init_entry(&store, t);
-
- store.ut_type = BOOT_TIME;
- strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
-
- return write_entry_both(&store);
-}
-
-_pure_ static const char *sanitize_id(const char *id) {
- size_t l;
-
- assert(id);
- l = strlen(id);
-
- if (l <= sizeof(((struct utmpx*) NULL)->ut_id))
- return id;
-
- return id + l - sizeof(((struct utmpx*) NULL)->ut_id);
-}
-
-int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line) {
- struct utmpx store = {
- .ut_type = INIT_PROCESS,
- .ut_pid = pid,
- .ut_session = sid,
- };
-
- assert(id);
-
- init_timestamp(&store, 0);
-
- /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
- strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
-
- if (line)
- strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
-
- return write_entry_both(&store);
-}
-
-int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
- struct utmpx lookup = {
- .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
- }, store, store_wtmp, *found;
-
- assert(id);
-
- setutxent();
-
- /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
- strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
-
- found = getutxid(&lookup);
- if (!found)
- return 0;
-
- if (found->ut_pid != pid)
- return 0;
-
- memcpy(&store, found, sizeof(store));
- store.ut_type = DEAD_PROCESS;
- store.ut_exit.e_termination = code;
- store.ut_exit.e_exit = status;
-
- zero(store.ut_user);
- zero(store.ut_host);
- zero(store.ut_tv);
-
- memcpy(&store_wtmp, &store, sizeof(store_wtmp));
- /* wtmp wants the current time */
- init_timestamp(&store_wtmp, 0);
-
- return write_utmp_wtmp(&store, &store_wtmp);
-}
-
-
-int utmp_put_runlevel(int runlevel, int previous) {
- struct utmpx store = {};
- int r;
-
- assert(runlevel > 0);
-
- if (previous <= 0) {
- /* Find the old runlevel automatically */
-
- r = utmp_get_runlevel(&previous, NULL);
- if (r < 0) {
- if (r != -ESRCH)
- return r;
-
- previous = 0;
- }
- }
-
- if (previous == runlevel)
- return 0;
-
- init_entry(&store, 0);
-
- store.ut_type = RUN_LVL;
- store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
- strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
-
- return write_entry_both(&store);
-}
-
-#define TIMEOUT_MSEC 50
-
-static int write_to_terminal(const char *tty, const char *message) {
- _cleanup_close_ int fd = -1;
- const char *p;
- size_t left;
- usec_t end;
-
- assert(tty);
- assert(message);
-
- fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0 || !isatty(fd))
- return -errno;
-
- p = message;
- left = strlen(message);
-
- end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC;
-
- while (left > 0) {
- ssize_t n;
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLOUT,
- };
- usec_t t;
- int k;
-
- t = now(CLOCK_MONOTONIC);
-
- if (t >= end)
- return -ETIME;
-
- k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
- if (k < 0)
- return -errno;
-
- if (k == 0)
- return -ETIME;
-
- n = write(fd, p, left);
- if (n < 0) {
- if (errno == EAGAIN)
- continue;
-
- return -errno;
- }
-
- assert((size_t) n <= left);
-
- p += n;
- left -= n;
- }
-
- return 0;
-}
-
-int utmp_wall(const char *message, const char *username, bool (*match_tty)(const char *tty)) {
- _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
- char date[FORMAT_TIMESTAMP_MAX];
- struct utmpx *u;
- int r;
-
- hn = gethostname_malloc();
- if (!hn)
- return -ENOMEM;
- if (!username) {
- un = getlogname_malloc();
- if (!un)
- return -ENOMEM;
- }
-
- getttyname_harder(STDIN_FILENO, &tty);
-
- if (asprintf(&text,
- "\a\r\n"
- "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
- "%s\r\n\r\n",
- un ?: username, hn,
- tty ? " on " : "", strempty(tty),
- format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
- message) < 0)
- return -ENOMEM;
-
- setutxent();
-
- r = 0;
-
- while ((u = getutxent())) {
- _cleanup_free_ char *buf = NULL;
- const char *path;
- int q;
-
- if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
- continue;
-
- /* this access is fine, because strlen("/dev/") << 32 (UT_LINESIZE) */
- if (path_startswith(u->ut_line, "/dev/"))
- path = u->ut_line;
- else {
- if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
- return -ENOMEM;
-
- path = buf;
- }
-
- if (!match_tty || match_tty(path)) {
- q = write_to_terminal(path, text);
- if (q < 0)
- r = q;
- }
- }
-
- return r;
-}
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ 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 <http://www.gnu.org/licenses/>.
+***/
+
+#include "util.h"
+
+#ifdef HAVE_UTMP
+int utmp_get_runlevel(int *runlevel, int *previous);
+
+int utmp_put_shutdown(void);
+int utmp_put_reboot(usec_t timestamp);
+int utmp_put_runlevel(int runlevel, int previous);
+
+int utmp_put_dead_process(const char *id, pid_t pid, int code, int status);
+int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user);
+
+int utmp_wall(
+ const char *message,
+ const char *username,
+ const char *origin_tty,
+ bool (*match_tty)(const char *tty, void *userdata),
+ void *userdata);
+
+#else /* HAVE_UTMP */
+
+static inline int utmp_get_runlevel(int *runlevel, int *previous) {
+ return -ESRCH;
+}
+static inline int utmp_put_shutdown(void) {
+ return 0;
+}
+static inline int utmp_put_reboot(usec_t timestamp) {
+ return 0;
+}
+static inline int utmp_put_runlevel(int runlevel, int previous) {
+ return 0;
+}
+static inline int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
+ return 0;
+}
+static inline int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
+ return 0;
+}
+static inline int utmp_wall(
+ const char *message,
+ const char *username,
+ const char *origin_tty,
+ bool (*match_tty)(const char *tty, void *userdata),
+ void *userdata) {
+ return 0;
+}
+
+#endif /* HAVE_UTMP */
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/ioctl.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <linux/watchdog.h>
-
-#include "watchdog.h"
-#include "log.h"
-
-static int watchdog_fd = -1;
-static usec_t watchdog_timeout = USEC_INFINITY;
-
-static int update_timeout(void) {
- int r;
-
- if (watchdog_fd < 0)
- return 0;
-
- if (watchdog_timeout == USEC_INFINITY)
- return 0;
- else if (watchdog_timeout == 0) {
- int flags;
-
- flags = WDIOS_DISABLECARD;
- r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
- if (r < 0)
- return log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
- } else {
- int sec, flags;
- char buf[FORMAT_TIMESPAN_MAX];
-
- sec = (int) ((watchdog_timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
- r = ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &sec);
- if (r < 0)
- return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec);
-
- watchdog_timeout = (usec_t) sec * USEC_PER_SEC;
- log_info("Set hardware watchdog to %s.", format_timespan(buf, sizeof(buf), watchdog_timeout, 0));
-
- flags = WDIOS_ENABLECARD;
- r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
- if (r < 0)
- return log_warning_errno(errno, "Failed to enable hardware watchdog: %m");
-
- r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
- if (r < 0)
- return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
- }
-
- return 0;
-}
-
-static int open_watchdog(void) {
- struct watchdog_info ident;
-
- if (watchdog_fd >= 0)
- return 0;
-
- watchdog_fd = open("/dev/watchdog", O_WRONLY|O_CLOEXEC);
- if (watchdog_fd < 0)
- return -errno;
-
- if (ioctl(watchdog_fd, WDIOC_GETSUPPORT, &ident) >= 0)
- log_info("Hardware watchdog '%s', version %x",
- ident.identity,
- ident.firmware_version);
-
- return update_timeout();
-}
-
-int watchdog_set_timeout(usec_t *usec) {
- int r;
-
- watchdog_timeout = *usec;
-
- /* If we didn't open the watchdog yet and didn't get any
- * explicit timeout value set, don't do anything */
- if (watchdog_fd < 0 && watchdog_timeout == USEC_INFINITY)
- return 0;
-
- if (watchdog_fd < 0)
- r = open_watchdog();
- else
- r = update_timeout();
-
- *usec = watchdog_timeout;
-
- return r;
-}
-
-int watchdog_ping(void) {
- int r;
-
- if (watchdog_fd < 0) {
- r = open_watchdog();
- if (r < 0)
- return r;
- }
-
- r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
- if (r < 0)
- return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
-
- return 0;
-}
-
-void watchdog_close(bool disarm) {
- int r;
-
- if (watchdog_fd < 0)
- return;
-
- if (disarm) {
- int flags;
-
- /* Explicitly disarm it */
- flags = WDIOS_DISABLECARD;
- r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
- if (r < 0)
- log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
-
- /* To be sure, use magic close logic, too */
- for (;;) {
- static const char v = 'V';
-
- if (write(watchdog_fd, &v, 1) > 0)
- break;
-
- if (errno != EINTR) {
- log_error_errno(errno, "Failed to disarm watchdog timer: %m");
- break;
- }
- }
- }
-
- watchdog_fd = safe_close(watchdog_fd);
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-
-int watchdog_set_timeout(usec_t *usec);
-int watchdog_ping(void);
-void watchdog_close(bool disarm);
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-
-#include "util.h"
-#include "xml.h"
-
-enum {
- STATE_NULL,
- STATE_TEXT,
- STATE_TAG,
- STATE_ATTRIBUTE,
-};
-
-static void inc_lines(unsigned *line, const char *s, size_t n) {
- const char *p = s;
-
- if (!line)
- return;
-
- for (;;) {
- const char *f;
-
- f = memchr(p, '\n', n);
- if (!f)
- return;
-
- n -= (f - p) + 1;
- p = f + 1;
- (*line)++;
- }
-}
-
-/* We don't actually do real XML here. We only read a simplistic
- * subset, that is a bit less strict that XML and lacks all the more
- * complex features, like entities, or namespaces. However, we do
- * support some HTML5-like simplifications */
-
-int xml_tokenize(const char **p, char **name, void **state, unsigned *line) {
- const char *c, *e, *b;
- char *ret;
- int t;
-
- assert(p);
- assert(*p);
- assert(name);
- assert(state);
-
- t = PTR_TO_INT(*state);
- c = *p;
-
- if (t == STATE_NULL) {
- if (line)
- *line = 1;
- t = STATE_TEXT;
- }
-
- for (;;) {
- if (*c == 0)
- return XML_END;
-
- switch (t) {
-
- case STATE_TEXT: {
- int x;
-
- e = strchrnul(c, '<');
- if (e > c) {
- /* More text... */
- ret = strndup(c, e - c);
- if (!ret)
- return -ENOMEM;
-
- inc_lines(line, c, e - c);
-
- *name = ret;
- *p = e;
- *state = INT_TO_PTR(STATE_TEXT);
-
- return XML_TEXT;
- }
-
- assert(*e == '<');
- b = c + 1;
-
- if (startswith(b, "!--")) {
- /* A comment */
- e = strstr(b + 3, "-->");
- if (!e)
- return -EINVAL;
-
- inc_lines(line, b, e + 3 - b);
-
- c = e + 3;
- continue;
- }
-
- if (*b == '?') {
- /* Processing instruction */
-
- e = strstr(b + 1, "?>");
- if (!e)
- return -EINVAL;
-
- inc_lines(line, b, e + 2 - b);
-
- c = e + 2;
- continue;
- }
-
- if (*b == '!') {
- /* DTD */
-
- e = strchr(b + 1, '>');
- if (!e)
- return -EINVAL;
-
- inc_lines(line, b, e + 1 - b);
-
- c = e + 1;
- continue;
- }
-
- if (*b == '/') {
- /* A closing tag */
- x = XML_TAG_CLOSE;
- b++;
- } else
- x = XML_TAG_OPEN;
-
- e = strpbrk(b, WHITESPACE "/>");
- if (!e)
- return -EINVAL;
-
- ret = strndup(b, e - b);
- if (!ret)
- return -ENOMEM;
-
- *name = ret;
- *p = e;
- *state = INT_TO_PTR(STATE_TAG);
-
- return x;
- }
-
- case STATE_TAG:
-
- b = c + strspn(c, WHITESPACE);
- if (*b == 0)
- return -EINVAL;
-
- inc_lines(line, c, b - c);
-
- e = b + strcspn(b, WHITESPACE "=/>");
- if (e > b) {
- /* An attribute */
-
- ret = strndup(b, e - b);
- if (!ret)
- return -ENOMEM;
-
- *name = ret;
- *p = e;
- *state = INT_TO_PTR(STATE_ATTRIBUTE);
-
- return XML_ATTRIBUTE_NAME;
- }
-
- if (startswith(b, "/>")) {
- /* An empty tag */
-
- *name = NULL; /* For empty tags we return a NULL name, the caller must be prepared for that */
- *p = b + 2;
- *state = INT_TO_PTR(STATE_TEXT);
-
- return XML_TAG_CLOSE_EMPTY;
- }
-
- if (*b != '>')
- return -EINVAL;
-
- c = b + 1;
- t = STATE_TEXT;
- continue;
-
- case STATE_ATTRIBUTE:
-
- if (*c == '=') {
- c++;
-
- if (*c == '\'' || *c == '\"') {
- /* Tag with a quoted value */
-
- e = strchr(c+1, *c);
- if (!e)
- return -EINVAL;
-
- inc_lines(line, c, e - c);
-
- ret = strndup(c+1, e - c - 1);
- if (!ret)
- return -ENOMEM;
-
- *name = ret;
- *p = e + 1;
- *state = INT_TO_PTR(STATE_TAG);
-
- return XML_ATTRIBUTE_VALUE;
-
- }
-
- /* Tag with a value without quotes */
-
- b = strpbrk(c, WHITESPACE ">");
- if (!b)
- b = c;
-
- ret = strndup(c, b - c);
- if (!ret)
- return -ENOMEM;
-
- *name = ret;
- *p = b;
- *state = INT_TO_PTR(STATE_TAG);
- return XML_ATTRIBUTE_VALUE;
- }
-
- t = STATE_TAG;
- continue;
- }
-
- }
-
- assert_not_reached("Bad state");
-}
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-enum {
- XML_END,
- XML_TEXT,
- XML_TAG_OPEN,
- XML_TAG_CLOSE,
- XML_TAG_CLOSE_EMPTY,
- XML_ATTRIBUTE_NAME,
- XML_ATTRIBUTE_VALUE,
-};
-
-int xml_tokenize(const char **p, char **name, void **state, unsigned *line);
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_CAPABILITY_MASK = 0xFFFFULL << 40
};
const char *signature;
const char *result;
sd_bus_message_handler_t handler;
+ size_t offset;
} method;
struct {
const char *member;
.x.start.element_size = sizeof(sd_bus_vtable), \
}
-#define SD_BUS_METHOD(_member, _signature, _result, _handler, _flags) \
+#define SD_BUS_METHOD_WITH_OFFSET(_member, _signature, _result, _handler, _offset, _flags) \
{ \
.type = _SD_BUS_VTABLE_METHOD, \
.flags = _flags, \
.x.method.signature = _signature, \
.x.method.result = _result, \
.x.method.handler = _handler, \
+ .x.method.offset = _offset, \
}
+#define SD_BUS_METHOD(_member, _signature, _result, _handler, _flags) \
+ SD_BUS_METHOD_WITH_OFFSET(_member, _signature, _result, _handler, 0, _flags)
#define SD_BUS_SIGNAL(_member, _signature, _flags) \
{ \
enum {
SD_BUS_CREDS_PID = 1ULL << 0,
SD_BUS_CREDS_TID = 1ULL << 1,
- SD_BUS_CREDS_UID = 1ULL << 2,
- SD_BUS_CREDS_EUID = 1ULL << 3,
- SD_BUS_CREDS_SUID = 1ULL << 4,
- SD_BUS_CREDS_FSUID = 1ULL << 5,
- SD_BUS_CREDS_GID = 1ULL << 6,
- SD_BUS_CREDS_EGID = 1ULL << 7,
- SD_BUS_CREDS_SGID = 1ULL << 8,
- SD_BUS_CREDS_FSGID = 1ULL << 9,
- SD_BUS_CREDS_SUPPLEMENTARY_GIDS = 1ULL << 10,
- SD_BUS_CREDS_COMM = 1ULL << 11,
- SD_BUS_CREDS_TID_COMM = 1ULL << 12,
- SD_BUS_CREDS_EXE = 1ULL << 13,
- SD_BUS_CREDS_CMDLINE = 1ULL << 14,
- SD_BUS_CREDS_CGROUP = 1ULL << 15,
- SD_BUS_CREDS_UNIT = 1ULL << 16,
- SD_BUS_CREDS_USER_UNIT = 1ULL << 17,
+ SD_BUS_CREDS_PPID = 1ULL << 2,
+ SD_BUS_CREDS_UID = 1ULL << 3,
+ SD_BUS_CREDS_EUID = 1ULL << 4,
+ SD_BUS_CREDS_SUID = 1ULL << 5,
+ SD_BUS_CREDS_FSUID = 1ULL << 6,
+ SD_BUS_CREDS_GID = 1ULL << 7,
+ SD_BUS_CREDS_EGID = 1ULL << 8,
+ SD_BUS_CREDS_SGID = 1ULL << 9,
+ SD_BUS_CREDS_FSGID = 1ULL << 10,
+ SD_BUS_CREDS_SUPPLEMENTARY_GIDS = 1ULL << 11,
+ SD_BUS_CREDS_COMM = 1ULL << 12,
+ SD_BUS_CREDS_TID_COMM = 1ULL << 13,
+ SD_BUS_CREDS_EXE = 1ULL << 14,
+ SD_BUS_CREDS_CMDLINE = 1ULL << 15,
+ SD_BUS_CREDS_CGROUP = 1ULL << 16,
+ SD_BUS_CREDS_UNIT = 1ULL << 17,
SD_BUS_CREDS_SLICE = 1ULL << 18,
- SD_BUS_CREDS_SESSION = 1ULL << 19,
- SD_BUS_CREDS_OWNER_UID = 1ULL << 20,
- SD_BUS_CREDS_EFFECTIVE_CAPS = 1ULL << 21,
- SD_BUS_CREDS_PERMITTED_CAPS = 1ULL << 22,
- SD_BUS_CREDS_INHERITABLE_CAPS = 1ULL << 23,
- SD_BUS_CREDS_BOUNDING_CAPS = 1ULL << 24,
- SD_BUS_CREDS_SELINUX_CONTEXT = 1ULL << 25,
- SD_BUS_CREDS_AUDIT_SESSION_ID = 1ULL << 26,
- SD_BUS_CREDS_AUDIT_LOGIN_UID = 1ULL << 27,
- SD_BUS_CREDS_UNIQUE_NAME = 1ULL << 28,
- SD_BUS_CREDS_WELL_KNOWN_NAMES = 1ULL << 29,
- SD_BUS_CREDS_DESCRIPTION = 1ULL << 30,
+ SD_BUS_CREDS_USER_UNIT = 1ULL << 19,
+ SD_BUS_CREDS_USER_SLICE = 1ULL << 20,
+ SD_BUS_CREDS_SESSION = 1ULL << 21,
+ SD_BUS_CREDS_OWNER_UID = 1ULL << 22,
+ SD_BUS_CREDS_EFFECTIVE_CAPS = 1ULL << 23,
+ SD_BUS_CREDS_PERMITTED_CAPS = 1ULL << 24,
+ SD_BUS_CREDS_INHERITABLE_CAPS = 1ULL << 25,
+ SD_BUS_CREDS_BOUNDING_CAPS = 1ULL << 26,
+ SD_BUS_CREDS_SELINUX_CONTEXT = 1ULL << 27,
+ SD_BUS_CREDS_AUDIT_SESSION_ID = 1ULL << 28,
+ SD_BUS_CREDS_AUDIT_LOGIN_UID = 1ULL << 29,
+ SD_BUS_CREDS_TTY = 1ULL << 30,
+ SD_BUS_CREDS_UNIQUE_NAME = 1ULL << 31,
+ SD_BUS_CREDS_WELL_KNOWN_NAMES = 1ULL << 32,
+ SD_BUS_CREDS_DESCRIPTION = 1ULL << 33,
SD_BUS_CREDS_AUGMENT = 1ULL << 63, /* special flag, if on sd-bus will augment creds struct, in a potentially race-full way. */
- _SD_BUS_CREDS_ALL = (1ULL << 32) -1,
+ _SD_BUS_CREDS_ALL = (1ULL << 34) -1,
};
enum {
/* Callbacks */
-typedef int (*sd_bus_message_handler_t)(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
+typedef int (*sd_bus_message_handler_t)(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
typedef int (*sd_bus_property_get_t) (sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *ret_error);
typedef int (*sd_bus_property_set_t) (sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *ret_error);
typedef int (*sd_bus_object_find_t) (sd_bus *bus, const char *path, const char *interface, void *userdata, void **ret_found, sd_bus_error *ret_error);
-typedef int (*sd_bus_node_enumerator_t) (sd_bus *bus, const char *path, void *userdata, char ***ret_nodes, sd_bus_error *ret_error);
+typedef int (*sd_bus_node_enumerator_t) (sd_bus *bus, const char *prefix, void *userdata, char ***ret_nodes, sd_bus_error *ret_error);
typedef int (*sd_bus_track_handler_t) (sd_bus_track *track, void *userdata);
#include "sd-bus-protocol.h"
int sd_bus_set_address(sd_bus *bus, const char *address);
int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd);
-int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]);
-int sd_bus_get_address(sd_bus *bus, const char **address);
-int sd_bus_set_bus_client(sd_bus *bus, int b);
-int sd_bus_is_bus_client(sd_bus *bus);
+// UNNEEDED int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]);
+// UNNEEDED int sd_bus_get_address(sd_bus *bus, const char **address);
+// UNNEEDED int sd_bus_set_bus_client(sd_bus *bus, int b);
+// UNNEEDED int sd_bus_is_bus_client(sd_bus *bus);
int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t bus_id);
-int sd_bus_is_server(sd_bus *bus);
-int sd_bus_set_anonymous(sd_bus *bus, int b);
-int sd_bus_is_anonymous(sd_bus *bus);
-int sd_bus_set_trusted(sd_bus *bus, int b);
-int sd_bus_is_trusted(sd_bus *bus);
-int sd_bus_set_monitor(sd_bus *bus, int b);
-int sd_bus_is_monitor(sd_bus *bus);
-int sd_bus_set_description(sd_bus *bus, const char *description);
-int sd_bus_get_description(sd_bus *bus, const char **description);
-int sd_bus_negotiate_fds(sd_bus *bus, int b);
-int sd_bus_can_send(sd_bus *bus, char type);
-int sd_bus_negotiate_timestamp(sd_bus *bus, int b);
+// UNNEEDED int sd_bus_is_server(sd_bus *bus);
+// UNNEEDED int sd_bus_set_anonymous(sd_bus *bus, int b);
+// UNNEEDED int sd_bus_is_anonymous(sd_bus *bus);
+// UNNEEDED int sd_bus_set_trusted(sd_bus *bus, int b);
+// UNNEEDED int sd_bus_is_trusted(sd_bus *bus);
+// UNNEEDED int sd_bus_set_monitor(sd_bus *bus, int b);
+// UNNEEDED int sd_bus_is_monitor(sd_bus *bus);
+// UNNEEDED int sd_bus_set_description(sd_bus *bus, const char *description);
+// UNNEEDED int sd_bus_get_description(sd_bus *bus, const char **description);
int sd_bus_negotiate_creds(sd_bus *bus, int b, uint64_t creds_mask);
-int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *creds_mask);
+// UNNEEDED int sd_bus_negotiate_timestamp(sd_bus *bus, int b);
+// UNNEEDED int sd_bus_negotiate_fds(sd_bus *bus, int b);
+int sd_bus_can_send(sd_bus *bus, char type);
+// UNNEEDED int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *creds_mask);
int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b);
-int sd_bus_get_allow_interactive_authorization(sd_bus *bus);
+// UNNEEDED int sd_bus_get_allow_interactive_authorization(sd_bus *bus);
int sd_bus_start(sd_bus *ret);
sd_bus *sd_bus_ref(sd_bus *bus);
sd_bus *sd_bus_unref(sd_bus *bus);
+sd_bus *sd_bus_flush_close_unref(sd_bus *bus);
-int sd_bus_is_open(sd_bus *bus);
+// UNNEEDED int sd_bus_is_open(sd_bus *bus);
-int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id);
-int sd_bus_get_scope(sd_bus *bus, const char **scope);
-int sd_bus_get_tid(sd_bus *bus, pid_t *tid);
+// UNNEEDED int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id);
+// UNNEEDED int sd_bus_get_scope(sd_bus *bus, const char **scope);
+// UNNEEDED int sd_bus_get_tid(sd_bus *bus, pid_t *tid);
int sd_bus_get_owner_creds(sd_bus *bus, uint64_t creds_mask, sd_bus_creds **ret);
int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *cookie);
int sd_bus_get_events(sd_bus *bus);
int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec);
int sd_bus_process(sd_bus *bus, sd_bus_message **r);
-int sd_bus_process_priority(sd_bus *bus, int64_t max_priority, sd_bus_message **r);
+// UNNEEDED int sd_bus_process_priority(sd_bus *bus, int64_t max_priority, sd_bus_message **r);
int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec);
int sd_bus_flush(sd_bus *bus);
-sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus);
+// UNNEEDED sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus);
sd_bus_message* sd_bus_get_current_message(sd_bus *bus);
sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus);
void* sd_bus_get_current_userdata(sd_bus *bus);
int sd_bus_add_object_vtable(sd_bus *bus, sd_bus_slot **slot, const char *path, const char *interface, const sd_bus_vtable *vtable, void *userdata);
int sd_bus_add_fallback_vtable(sd_bus *bus, sd_bus_slot **slot, const char *prefix, const char *interface, const sd_bus_vtable *vtable, sd_bus_object_find_t find, void *userdata);
int sd_bus_add_node_enumerator(sd_bus *bus, sd_bus_slot **slot, const char *path, sd_bus_node_enumerator_t callback, void *userdata);
-int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path);
+// UNNEEDED int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path);
/* Slot object */
sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot);
sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot);
-sd_bus* sd_bus_slot_get_bus(sd_bus_slot *slot);
-void *sd_bus_slot_get_userdata(sd_bus_slot *slot);
-void *sd_bus_slot_set_userdata(sd_bus_slot *slot, void *userdata);
-int sd_bus_slot_set_description(sd_bus_slot *slot, const char *description);
-int sd_bus_slot_get_description(sd_bus_slot *slot, char **description);
+// UNNEEDED sd_bus* sd_bus_slot_get_bus(sd_bus_slot *slot);
+// UNNEEDED void *sd_bus_slot_get_userdata(sd_bus_slot *slot);
+// UNNEEDED void *sd_bus_slot_set_userdata(sd_bus_slot *slot, void *userdata);
+// UNNEEDED int sd_bus_slot_set_description(sd_bus_slot *slot, const char *description);
+// UNNEEDED int sd_bus_slot_get_description(sd_bus_slot *slot, const char **description);
-sd_bus_message* sd_bus_slot_get_current_message(sd_bus_slot *slot);
-sd_bus_message_handler_t sd_bus_slot_get_current_handler(sd_bus_slot *bus);
-void *sd_bus_slot_get_current_userdata(sd_bus_slot *slot);
+// UNNEEDED sd_bus_message* sd_bus_slot_get_current_message(sd_bus_slot *slot);
+// UNNEEDED sd_bus_message_handler_t sd_bus_slot_get_current_handler(sd_bus_slot *bus);
+// UNNEEDED void *sd_bus_slot_get_current_userdata(sd_bus_slot *slot);
/* Message object */
int sd_bus_message_new_method_error(sd_bus_message *call, sd_bus_message **m, const sd_bus_error *e);
int sd_bus_message_new_method_errorf(sd_bus_message *call, sd_bus_message **m, const char *name, const char *format, ...) _sd_printf_(4, 5);
int sd_bus_message_new_method_errno(sd_bus_message *call, sd_bus_message **m, int error, const sd_bus_error *e);
-int sd_bus_message_new_method_errnof(sd_bus_message *call, sd_bus_message **m, int error, const char *format, ...) _sd_printf_(4, 5);
+// UNNEEDED int sd_bus_message_new_method_errnof(sd_bus_message *call, sd_bus_message **m, int error, const char *format, ...) _sd_printf_(4, 5);
sd_bus_message* sd_bus_message_ref(sd_bus_message *m);
sd_bus_message* sd_bus_message_unref(sd_bus_message *m);
-int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type);
-int sd_bus_message_get_cookie(sd_bus_message *m, uint64_t *cookie);
-int sd_bus_message_get_reply_cookie(sd_bus_message *m, uint64_t *cookie);
-int sd_bus_message_get_priority(sd_bus_message *m, int64_t *priority);
+// UNNEEDED int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type);
+// UNNEEDED int sd_bus_message_get_cookie(sd_bus_message *m, uint64_t *cookie);
+// UNNEEDED int sd_bus_message_get_reply_cookie(sd_bus_message *m, uint64_t *cookie);
+// UNNEEDED int sd_bus_message_get_priority(sd_bus_message *m, int64_t *priority);
-int sd_bus_message_get_expect_reply(sd_bus_message *m);
-int sd_bus_message_get_auto_start(sd_bus_message *m);
+// UNNEEDED int sd_bus_message_get_expect_reply(sd_bus_message *m);
+// UNNEEDED int sd_bus_message_get_auto_start(sd_bus_message *m);
int sd_bus_message_get_allow_interactive_authorization(sd_bus_message *m);
const char *sd_bus_message_get_signature(sd_bus_message *m, int complete);
const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m);
int sd_bus_message_get_errno(sd_bus_message *m);
-int sd_bus_message_get_monotonic_usec(sd_bus_message *m, uint64_t *usec);
-int sd_bus_message_get_realtime_usec(sd_bus_message *m, uint64_t *usec);
-int sd_bus_message_get_seqnum(sd_bus_message *m, uint64_t* seqnum);
+// UNNEEDED int sd_bus_message_get_monotonic_usec(sd_bus_message *m, uint64_t *usec);
+// UNNEEDED int sd_bus_message_get_realtime_usec(sd_bus_message *m, uint64_t *usec);
+// UNNEEDED int sd_bus_message_get_seqnum(sd_bus_message *m, uint64_t* seqnum);
sd_bus* sd_bus_message_get_bus(sd_bus_message *m);
sd_bus_creds *sd_bus_message_get_creds(sd_bus_message *m); /* do not unref the result */
-int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member);
+// UNNEEDED int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member);
int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member);
int sd_bus_message_is_method_error(sd_bus_message *m, const char *name);
-int sd_bus_message_is_empty(sd_bus_message *m);
-int sd_bus_message_has_signature(sd_bus_message *m, const char *signature);
+// UNNEEDED int sd_bus_message_is_empty(sd_bus_message *m);
+// UNNEEDED int sd_bus_message_has_signature(sd_bus_message *m, const char *signature);
-int sd_bus_message_set_expect_reply(sd_bus_message *m, int b);
+// UNNEEDED int sd_bus_message_set_expect_reply(sd_bus_message *m, int b);
int sd_bus_message_set_auto_start(sd_bus_message *m, int b);
-int sd_bus_message_set_allow_interactive_authorization(sd_bus_message *m, int b);
+// UNNEEDED int sd_bus_message_set_allow_interactive_authorization(sd_bus_message *m, int b);
int sd_bus_message_set_destination(sd_bus_message *m, const char *destination);
-int sd_bus_message_set_priority(sd_bus_message *m, int64_t priority);
+// UNNEEDED int sd_bus_message_set_priority(sd_bus_message *m, int64_t priority);
int sd_bus_message_append(sd_bus_message *m, const char *types, ...);
int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p);
int sd_bus_message_append_array(sd_bus_message *m, char type, const void *ptr, size_t size);
int sd_bus_message_append_array_space(sd_bus_message *m, char type, size_t size, void **ptr);
-int sd_bus_message_append_array_iovec(sd_bus_message *m, char type, const struct iovec *iov, unsigned n);
-int sd_bus_message_append_array_memfd(sd_bus_message *m, char type, int memfd, uint64_t offset, uint64_t size);
+// UNNEEDED int sd_bus_message_append_array_iovec(sd_bus_message *m, char type, const struct iovec *iov, unsigned n);
+// UNNEEDED int sd_bus_message_append_array_memfd(sd_bus_message *m, char type, int memfd, uint64_t offset, uint64_t size);
int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s);
-int sd_bus_message_append_string_iovec(sd_bus_message *m, const struct iovec *iov, unsigned n);
-int sd_bus_message_append_string_memfd(sd_bus_message *m, int memfd, uint64_t offset, uint64_t size);
+// UNNEEDED int sd_bus_message_append_string_iovec(sd_bus_message *m, const struct iovec *iov, unsigned n);
+// UNNEEDED int sd_bus_message_append_string_memfd(sd_bus_message *m, int memfd, uint64_t offset, uint64_t size);
int sd_bus_message_append_strv(sd_bus_message *m, char **l);
int sd_bus_message_open_container(sd_bus_message *m, char type, const char *contents);
int sd_bus_message_close_container(sd_bus_message *m);
int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents);
int sd_bus_message_exit_container(sd_bus_message *m);
int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents);
-int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents);
-int sd_bus_message_at_end(sd_bus_message *m, int complete);
+// UNNEEDED int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents);
+// UNNEEDED int sd_bus_message_at_end(sd_bus_message *m, int complete);
int sd_bus_message_rewind(sd_bus_message *m, int complete);
/* Bus management */
int sd_bus_release_name(sd_bus *bus, const char *name);
int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable); /* free the results */
int sd_bus_get_name_creds(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **creds); /* unref the result! */
-int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine);
+// UNNEEDED int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine);
/* Convenience calls */
int sd_bus_call_method(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, sd_bus_message **reply, const char *types, ...);
+// UNNEEDED int sd_bus_call_method_async(sd_bus *bus, sd_bus_slot **slot, const char *destination, const char *path, const char *interface, const char *member, sd_bus_message_handler_t callback, void *userdata, const char *types, ...);
int sd_bus_get_property(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, sd_bus_message **reply, const char *type);
-int sd_bus_get_property_trivial(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, char type, void *ret_ptr);
+// UNNEEDED int sd_bus_get_property_trivial(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, char type, void *ret_ptr);
int sd_bus_get_property_string(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, char **ret); /* free the result! */
-int sd_bus_get_property_strv(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, char ***ret); /* free the result! */
-int sd_bus_set_property(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, const char *ret_type, ...);
+// UNNEEDED int sd_bus_get_property_strv(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, char ***ret); /* free the result! */
+// UNNEEDED int sd_bus_set_property(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *ret_error, const char *type, ...);
int sd_bus_reply_method_return(sd_bus_message *call, const char *types, ...);
int sd_bus_reply_method_error(sd_bus_message *call, const sd_bus_error *e);
int sd_bus_reply_method_errorf(sd_bus_message *call, const char *name, const char *format, ...) _sd_printf_(3, 4);
int sd_bus_reply_method_errno(sd_bus_message *call, int error, const sd_bus_error *e);
-int sd_bus_reply_method_errnof(sd_bus_message *call, int error, const char *format, ...) _sd_printf_(3, 4);
+// UNNEEDED int sd_bus_reply_method_errnof(sd_bus_message *call, int error, const char *format, ...) _sd_printf_(3, 4);
int sd_bus_emit_signal(sd_bus *bus, const char *path, const char *interface, const char *member, const char *types, ...);
int sd_bus_emit_properties_changed(sd_bus *bus, const char *path, const char *interface, const char *name, ...) _sd_sentinel_;
int sd_bus_emit_object_added(sd_bus *bus, const char *path);
-int sd_bus_emit_object_removed(sd_bus *bus, const char *path);
+// UNNEEDED int sd_bus_emit_object_removed(sd_bus *bus, const char *path);
int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces);
int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) _sd_sentinel_;
int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces);
/* Credential handling */
-int sd_bus_creds_new_from_pid(sd_bus_creds **ret, pid_t pid, uint64_t creds_mask);
+// UNNEEDED int sd_bus_creds_new_from_pid(sd_bus_creds **ret, pid_t pid, uint64_t creds_mask);
sd_bus_creds *sd_bus_creds_ref(sd_bus_creds *c);
sd_bus_creds *sd_bus_creds_unref(sd_bus_creds *c);
-uint64_t sd_bus_creds_get_mask(const sd_bus_creds *c);
+// UNNEEDED uint64_t sd_bus_creds_get_mask(const sd_bus_creds *c);
+uint64_t sd_bus_creds_get_augmented_mask(const sd_bus_creds *c);
int sd_bus_creds_get_pid(sd_bus_creds *c, pid_t *pid);
+// UNNEEDED int sd_bus_creds_get_ppid(sd_bus_creds *c, pid_t *ppid);
int sd_bus_creds_get_tid(sd_bus_creds *c, pid_t *tid);
int sd_bus_creds_get_uid(sd_bus_creds *c, uid_t *uid);
int sd_bus_creds_get_euid(sd_bus_creds *c, uid_t *euid);
-int sd_bus_creds_get_suid(sd_bus_creds *c, uid_t *suid);
-int sd_bus_creds_get_fsuid(sd_bus_creds *c, uid_t *fsuid);
-int sd_bus_creds_get_gid(sd_bus_creds *c, gid_t *gid);
+// UNNEEDED int sd_bus_creds_get_suid(sd_bus_creds *c, uid_t *suid);
+// UNNEEDED int sd_bus_creds_get_fsuid(sd_bus_creds *c, uid_t *fsuid);
+// UNNEEDED int sd_bus_creds_get_gid(sd_bus_creds *c, gid_t *gid);
int sd_bus_creds_get_egid(sd_bus_creds *c, gid_t *egid);
-int sd_bus_creds_get_sgid(sd_bus_creds *c, gid_t *sgid);
-int sd_bus_creds_get_fsgid(sd_bus_creds *c, gid_t *fsgid);
-int sd_bus_creds_get_supplementary_gids(sd_bus_creds *c, const gid_t **gids);
-int sd_bus_creds_get_comm(sd_bus_creds *c, const char **comm);
-int sd_bus_creds_get_tid_comm(sd_bus_creds *c, const char **comm);
-int sd_bus_creds_get_exe(sd_bus_creds *c, const char **exe);
+// UNNEEDED int sd_bus_creds_get_sgid(sd_bus_creds *c, gid_t *sgid);
+// UNNEEDED int sd_bus_creds_get_fsgid(sd_bus_creds *c, gid_t *fsgid);
+// UNNEEDED int sd_bus_creds_get_supplementary_gids(sd_bus_creds *c, const gid_t **gids);
+// UNNEEDED int sd_bus_creds_get_comm(sd_bus_creds *c, const char **comm);
+// UNNEEDED int sd_bus_creds_get_tid_comm(sd_bus_creds *c, const char **comm);
+// UNNEEDED int sd_bus_creds_get_exe(sd_bus_creds *c, const char **exe);
int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline);
-int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **cgroup);
-int sd_bus_creds_get_unit(sd_bus_creds *c, const char **unit);
-int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **unit);
-int sd_bus_creds_get_slice(sd_bus_creds *c, const char **slice);
+// UNNEEDED int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **cgroup);
+// UNNEEDED int sd_bus_creds_get_unit(sd_bus_creds *c, const char **unit);
+// UNNEEDED int sd_bus_creds_get_slice(sd_bus_creds *c, const char **slice);
+// UNNEEDED int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **unit);
+// UNNEEDED int sd_bus_creds_get_user_slice(sd_bus_creds *c, const char **slice);
int sd_bus_creds_get_session(sd_bus_creds *c, const char **session);
int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *uid);
int sd_bus_creds_has_effective_cap(sd_bus_creds *c, int capability);
-int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability);
-int sd_bus_creds_has_inheritable_cap(sd_bus_creds *c, int capability);
-int sd_bus_creds_has_bounding_cap(sd_bus_creds *c, int capability);
+// UNNEEDED int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability);
+// UNNEEDED int sd_bus_creds_has_inheritable_cap(sd_bus_creds *c, int capability);
+// UNNEEDED int sd_bus_creds_has_bounding_cap(sd_bus_creds *c, int capability);
int sd_bus_creds_get_selinux_context(sd_bus_creds *c, const char **context);
int sd_bus_creds_get_audit_session_id(sd_bus_creds *c, uint32_t *sessionid);
int sd_bus_creds_get_audit_login_uid(sd_bus_creds *c, uid_t *loginuid);
-int sd_bus_creds_get_unique_name(sd_bus_creds *c, const char **name);
-int sd_bus_creds_get_well_known_names(sd_bus_creds *c, char ***names);
-int sd_bus_creds_get_description(sd_bus_creds *c, const char **name);
+int sd_bus_creds_get_tty(sd_bus_creds *c, const char **tty);
+// UNNEEDED int sd_bus_creds_get_unique_name(sd_bus_creds *c, const char **name);
+// UNNEEDED int sd_bus_creds_get_well_known_names(sd_bus_creds *c, char ***names);
+// UNNEEDED int sd_bus_creds_get_description(sd_bus_creds *c, const char **name);
/* Error structures */
/* Label escaping */
-int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path);
-int sd_bus_path_decode(const char *path, const char *prefix, char **ret_external_id);
+// UNNEEDED int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path);
+// UNNEEDED int sd_bus_path_decode(const char *path, const char *prefix, char **ret_external_id);
/* Tracking peers */
sd_bus_track* sd_bus_track_unref(sd_bus_track *track);
sd_bus* sd_bus_track_get_bus(sd_bus_track *track);
-void *sd_bus_track_get_userdata(sd_bus_track *track);
-void *sd_bus_track_set_userdata(sd_bus_track *track, void *userdata);
+// UNNEEDED void *sd_bus_track_get_userdata(sd_bus_track *track);
+// UNNEEDED void *sd_bus_track_set_userdata(sd_bus_track *track, void *userdata);
int sd_bus_track_add_sender(sd_bus_track *track, sd_bus_message *m);
int sd_bus_track_remove_sender(sd_bus_track *track, sd_bus_message *m);
See sd_listen_fds(3) for more information.
*/
-int sd_listen_fds(int unset_environment);
+// UNNEEDED int sd_listen_fds(int unset_environment);
/*
Helper call for identifying a passed file descriptor. Returns 1 if
See sd_is_fifo(3) for more information.
*/
-int sd_is_fifo(int fd, const char *path);
+// UNNEEDED int sd_is_fifo(int fd, const char *path);
/*
Helper call for identifying a passed file descriptor. Returns 1 if
See sd_is_special(3) for more information.
*/
-int sd_is_special(int fd, const char *path);
+// UNNEEDED int sd_is_special(int fd, const char *path);
/*
Helper call for identifying a passed file descriptor. Returns 1 if
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);
+// UNNEEDED 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
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);
+// UNNEEDED 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.
+*/
+// UNNEEDED int sd_is_mq(int fd, const char *path);
/*
Informs systemd about changed daemon state. This takes a number of
See sd_notifyf(3) for more information.
*/
-int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_(2,3);
+// UNNEEDED 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
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);
+// UNNEEDED 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
See sd_booted(3) for more information.
*/
-int sd_booted(void);
+// UNNEEDED int sd_booted(void);
/*
Returns > 0 if the service manager expects watchdog keep-alive
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosddevicehfoo
-#define foosddevicehfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-#include <stdint.h>
-
-#include "_sd-common.h"
-
-_SD_BEGIN_DECLARATIONS;
-
-typedef struct sd_device sd_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_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_is_initialized(sd_device *device, int *initialized);
-int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *usec);
-
-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_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);
-
-int sd_device_has_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_sysattr_value(sd_device *device, const char *sysattr, const char **_value);
-
-int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, char *value);
-
-_SD_END_DECLARATIONS;
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosddhcpclienthfoo
-#define foosddhcpclienthfoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <netinet/in.h>
-#include <net/ethernet.h>
-
-#include "sd-event.h"
-#include "sd-dhcp-lease.h"
-
-enum {
- DHCP_EVENT_STOP = 0,
- DHCP_EVENT_IP_ACQUIRE = 1,
- DHCP_EVENT_IP_CHANGE = 2,
- DHCP_EVENT_EXPIRED = 3,
- DHCP_EVENT_RENEW = 4,
-};
-
-typedef struct sd_dhcp_client sd_dhcp_client;
-
-typedef void (*sd_dhcp_client_cb_t)(sd_dhcp_client *client, int event,
- void *userdata);
-int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_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_index(sd_dhcp_client *client, int interface_index);
-int sd_dhcp_client_set_mac(sd_dhcp_client *client, const uint8_t *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);
-int sd_dhcp_client_get_client_id(sd_dhcp_client *client, uint8_t *type,
- const uint8_t **data, size_t *data_len);
-int sd_dhcp_client_set_mtu(sd_dhcp_client *client, uint32_t mtu);
-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_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret);
-
-int sd_dhcp_client_stop(sd_dhcp_client *client);
-int sd_dhcp_client_start(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);
-
-int sd_dhcp_client_new(sd_dhcp_client **ret);
-
-int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event, int priority);
-int sd_dhcp_client_detach_event(sd_dhcp_client *client);
-sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client);
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosddhcpleasehfoo
-#define foosddhcpleasehfoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2013 Intel Corporation. All rights reserved.
- Copyright (C) 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <netinet/in.h>
-#include <net/ethernet.h>
-
-typedef struct sd_dhcp_lease sd_dhcp_lease;
-struct 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);
-
-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_netmask(sd_dhcp_lease *lease, struct in_addr *addr);
-int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, 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_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_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_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_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routesgn);
-int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const uint8_t **client_id,
- size_t *client_id_len);
-
-int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file);
-int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file);
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosddhcpserverhfoo
-#define foosddhcpserverhfoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2013 Intel Corporation. All rights reserved.
- Copyright (C) 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <netinet/in.h>
-
-#include "sd-event.h"
-
-typedef struct sd_dhcp_server sd_dhcp_server;
-
-sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server);
-sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server);
-
-int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex);
-
-int sd_dhcp_server_attach_event(sd_dhcp_server *client, sd_event *event, int priority);
-int sd_dhcp_server_detach_event(sd_dhcp_server *client);
-sd_event *sd_dhcp_server_get_event(sd_dhcp_server *client);
-
-bool sd_dhcp_server_is_running(sd_dhcp_server *server);
-
-int sd_dhcp_server_start(sd_dhcp_server *server);
-int sd_dhcp_server_stop(sd_dhcp_server *server);
-
-int sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address, unsigned char prefixlen);
-int sd_dhcp_server_set_lease_pool(sd_dhcp_server *server, struct in_addr *start, size_t size);
-
-int sd_dhcp_server_forcerenew(sd_dhcp_server *server);
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosddhcp6clienthfoo
-#define foosddhcp6clienthfoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <net/ethernet.h>
-
-#include "sd-event.h"
-
-#include "sd-dhcp6-lease.h"
-
-enum {
- DHCP6_EVENT_STOP = 0,
- DHCP6_EVENT_RESEND_EXPIRE = 10,
- DHCP6_EVENT_RETRANS_MAX = 11,
- DHCP6_EVENT_IP_ACQUIRE = 12,
- DHCP6_EVENT_INFORMATION_REQUEST = 13,
-};
-
-typedef struct sd_dhcp6_client sd_dhcp6_client;
-
-typedef void (*sd_dhcp6_client_cb_t)(sd_dhcp6_client *client, int event,
- void *userdata);
-int sd_dhcp6_client_set_callback(sd_dhcp6_client *client,
- sd_dhcp6_client_cb_t cb, void *userdata);
-
-int sd_dhcp6_client_set_index(sd_dhcp6_client *client, int interface_index);
-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 type, uint8_t *duid,
- size_t duid_len);
-int sd_dhcp6_client_set_information_request(sd_dhcp6_client *client,
- bool enabled);
-int sd_dhcp6_client_get_information_request(sd_dhcp6_client *client,
- bool *enabled);
-int sd_dhcp6_client_set_request_option(sd_dhcp6_client *client,
- uint16_t option);
-
-int sd_dhcp6_client_get_lease(sd_dhcp6_client *client, sd_dhcp6_lease **ret);
-
-int sd_dhcp6_client_stop(sd_dhcp6_client *client);
-int sd_dhcp6_client_start(sd_dhcp6_client *client);
-int sd_dhcp6_client_attach_event(sd_dhcp6_client *client, sd_event *event,
- int priority);
-int sd_dhcp6_client_detach_event(sd_dhcp6_client *client);
-sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client);
-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);
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosddhcp6leasehfoo
-#define foosddhcp6leasehfoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2014 Tom Gundersen
- Copyright (C) 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <netinet/in.h>
-
-typedef struct sd_dhcp6_lease sd_dhcp6_lease;
-
-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);
-
-sd_dhcp6_lease *sd_dhcp6_lease_ref(sd_dhcp6_lease *lease);
-sd_dhcp6_lease *sd_dhcp6_lease_unref(sd_dhcp6_lease *lease);
-
-#endif
int sd_event_wait(sd_event *e, uint64_t timeout);
int sd_event_dispatch(sd_event *e);
int sd_event_run(sd_event *e, uint64_t timeout);
-int sd_event_loop(sd_event *e);
+// UNNEEDED int sd_event_loop(sd_event *e);
int sd_event_exit(sd_event *e, int code);
-int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec);
+// UNNEEDED int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec);
-int sd_event_get_fd(sd_event *e);
+// UNNEEDED int sd_event_get_fd(sd_event *e);
int sd_event_get_state(sd_event *e);
int sd_event_get_tid(sd_event *e, pid_t *tid);
int sd_event_get_exit_code(sd_event *e, int *code);
int sd_event_set_watchdog(sd_event *e, int b);
-int sd_event_get_watchdog(sd_event *e);
+// UNNEEDED int sd_event_get_watchdog(sd_event *e);
-sd_event_source* sd_event_source_ref(sd_event_source *s);
+// UNNEEDED sd_event_source* sd_event_source_ref(sd_event_source *s);
sd_event_source* sd_event_source_unref(sd_event_source *s);
sd_event *sd_event_source_get_event(sd_event_source *s);
-void* sd_event_source_get_userdata(sd_event_source *s);
-void* sd_event_source_set_userdata(sd_event_source *s, void *userdata);
+// UNNEEDED void* sd_event_source_get_userdata(sd_event_source *s);
+// UNNEEDED void* sd_event_source_set_userdata(sd_event_source *s, void *userdata);
int sd_event_source_set_description(sd_event_source *s, const char *description);
-int sd_event_source_get_description(sd_event_source *s, const char **description);
+// UNNEEDED int sd_event_source_get_description(sd_event_source *s, const char **description);
int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback);
-int sd_event_source_get_pending(sd_event_source *s);
-int sd_event_source_get_priority(sd_event_source *s, int64_t *priority);
+// UNNEEDED int sd_event_source_get_pending(sd_event_source *s);
+// UNNEEDED int sd_event_source_get_priority(sd_event_source *s, int64_t *priority);
int sd_event_source_set_priority(sd_event_source *s, int64_t priority);
-int sd_event_source_get_enabled(sd_event_source *s, int *enabled);
+// UNNEEDED int sd_event_source_get_enabled(sd_event_source *s, int *enabled);
int sd_event_source_set_enabled(sd_event_source *s, int enabled);
-int sd_event_source_get_io_fd(sd_event_source *s);
+// UNNEEDED int sd_event_source_get_io_fd(sd_event_source *s);
int sd_event_source_set_io_fd(sd_event_source *s, int fd);
-int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events);
+// UNNEEDED int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events);
int sd_event_source_set_io_events(sd_event_source *s, uint32_t events);
-int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
+// UNNEEDED int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
-int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec);
-int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);
-int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock);
-int sd_event_source_get_signal(sd_event_source *s);
-int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid);
+// UNNEEDED int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec);
+// UNNEEDED int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);
+// UNNEEDED int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock);
+// UNNEEDED int sd_event_source_get_signal(sd_event_source *s);
+// UNNEEDED int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid);
_SD_END_DECLARATIONS;
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdhwdbhfoo
-#define foosdhwdbhfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
- Copyright 2014 Tom Gundersen <teg@jklm.no>
-
- 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 <http://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_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 danling '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)
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdicmp6ndfoo
-#define foosdicmp6ndfoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <net/ethernet.h>
-
-#include "sd-event.h"
-
-enum {
- ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE = 0,
- ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT = 1,
- ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER = 2,
- ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED = 3,
- ICMP6_EVENT_ROUTER_ADVERTISMENT_PREFIX_EXPIRED = 4,
-};
-
-typedef struct sd_icmp6_nd sd_icmp6_nd;
-
-typedef void(*sd_icmp6_nd_callback_t)(sd_icmp6_nd *nd, int event,
- void *userdata);
-
-int sd_icmp6_nd_set_callback(sd_icmp6_nd *nd, sd_icmp6_nd_callback_t cb,
- void *userdata);
-int sd_icmp6_nd_set_index(sd_icmp6_nd *nd, int interface_index);
-int sd_icmp6_nd_set_mac(sd_icmp6_nd *nd, const struct ether_addr *mac_addr);
-
-int sd_icmp6_nd_attach_event(sd_icmp6_nd *nd, sd_event *event, int priority);
-int sd_icmp6_nd_detach_event(sd_icmp6_nd *nd);
-sd_event *sd_icmp6_nd_get_event(sd_icmp6_nd *nd);
-
-sd_icmp6_nd *sd_icmp6_nd_ref(sd_icmp6_nd *nd);
-sd_icmp6_nd *sd_icmp6_nd_unref(sd_icmp6_nd *nd);
-int sd_icmp6_nd_new(sd_icmp6_nd **ret);
-
-int sd_icmp6_prefix_match(struct in6_addr *prefix, uint8_t prefixlen,
- struct in6_addr *addr);
-
-int sd_icmp6_ra_get_mtu(sd_icmp6_nd *nd, uint32_t *mtu);
-int sd_icmp6_ra_get_prefixlen(sd_icmp6_nd *nd, const struct in6_addr *addr,
- uint8_t *prefixlen);
-int sd_icmp6_ra_get_expired_prefix(sd_icmp6_nd *nd, struct in6_addr **addr,
- uint8_t *prefixlen);
-
-int sd_icmp6_nd_stop(sd_icmp6_nd *nd);
-int sd_icmp6_router_solicitation_start(sd_icmp6_nd *nd);
-
-#define SD_ICMP6_ADDRESS_FORMAT_STR "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
-
-#define SD_ICMP6_ADDRESS_FORMAT_VAL(address) \
- be16toh((address).s6_addr16[0]), \
- be16toh((address).s6_addr16[1]), \
- be16toh((address).s6_addr16[2]), \
- be16toh((address).s6_addr16[3]), \
- be16toh((address).s6_addr16[4]), \
- be16toh((address).s6_addr16[5]), \
- be16toh((address).s6_addr16[6]), \
- be16toh((address).s6_addr16[7])
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdipv4llfoo
-#define foosdipv4llfoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <netinet/in.h>
-#include <net/ethernet.h>
-
-#include "sd-event.h"
-
-enum {
- IPV4LL_EVENT_STOP = 0,
- IPV4LL_EVENT_BIND = 1,
- IPV4LL_EVENT_CONFLICT = 2,
-};
-
-typedef struct sd_ipv4ll sd_ipv4ll;
-typedef void (*sd_ipv4ll_cb_t)(sd_ipv4ll *ll, int event, void *userdata);
-
-int sd_ipv4ll_detach_event(sd_ipv4ll *ll);
-int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int priority);
-int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address);
-int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_cb_t cb, void *userdata);
-int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr);
-int sd_ipv4ll_set_index(sd_ipv4ll *ll, int interface_index);
-int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint8_t seed[8]);
-bool sd_ipv4ll_is_running(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);
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdjournalhfoo
-#define foosdjournalhfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <inttypes.h>
-#include <sys/types.h>
-#include <stdarg.h>
-#include <sys/uio.h>
-#include <syslog.h>
-
-#include "sd-id128.h"
-#include "_sd-common.h"
-
-/* Journal APIs. See sd-journal(3) for more information. */
-
-_SD_BEGIN_DECLARATIONS;
-
-/* Write to daemon */
-int sd_journal_print(int priority, const char *format, ...) _sd_printf_(2, 3);
-int sd_journal_printv(int priority, const char *format, va_list ap) _sd_printf_(2, 0);
-int sd_journal_send(const char *format, ...) _sd_printf_(1, 0) _sd_sentinel_;
-int sd_journal_sendv(const struct iovec *iov, int n);
-int sd_journal_perror(const char *message);
-
-/* Used by the macros below. You probably don't want to call this directly. */
-int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_(5, 6);
-int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) _sd_printf_(5, 0);
-int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_(4, 0) _sd_sentinel_;
-int sd_journal_sendv_with_location(const char *file, const char *line, const char *func, const struct iovec *iov, int n);
-int sd_journal_perror_with_location(const char *file, const char *line, const char *func, const char *message);
-
-/* implicitly add code location to messages sent, if this is enabled */
-#ifndef SD_JOURNAL_SUPPRESS_LOCATION
-
-#define sd_journal_print(priority, ...) sd_journal_print_with_location(priority, "CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, __VA_ARGS__)
-#define sd_journal_printv(priority, format, ap) sd_journal_printv_with_location(priority, "CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, format, ap)
-#define sd_journal_send(...) sd_journal_send_with_location("CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, __VA_ARGS__)
-#define sd_journal_sendv(iovec, n) sd_journal_sendv_with_location("CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, iovec, n)
-#define sd_journal_perror(message) sd_journal_perror_with_location("CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, message)
-
-#endif
-
-int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix);
-
-/* Browse journal stream */
-
-typedef struct sd_journal sd_journal;
-
-/* Open flags */
-enum {
- SD_JOURNAL_LOCAL_ONLY = 1,
- SD_JOURNAL_RUNTIME_ONLY = 2,
- SD_JOURNAL_SYSTEM = 4,
- SD_JOURNAL_CURRENT_USER = 8,
-
- SD_JOURNAL_SYSTEM_ONLY = SD_JOURNAL_SYSTEM, /* deprecated name */
-};
-
-/* Wakeup event types */
-enum {
- SD_JOURNAL_NOP,
- SD_JOURNAL_APPEND,
- SD_JOURNAL_INVALIDATE
-};
-
-int sd_journal_open(sd_journal **ret, int flags);
-int sd_journal_open_directory(sd_journal **ret, const char *path, int flags);
-int sd_journal_open_files(sd_journal **ret, const char **paths, int flags);
-int sd_journal_open_container(sd_journal **ret, const char *machine, int flags);
-void sd_journal_close(sd_journal *j);
-
-int sd_journal_previous(sd_journal *j);
-int sd_journal_next(sd_journal *j);
-
-int sd_journal_previous_skip(sd_journal *j, uint64_t skip);
-int sd_journal_next_skip(sd_journal *j, uint64_t skip);
-
-int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret);
-int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id);
-
-int sd_journal_set_data_threshold(sd_journal *j, size_t sz);
-int sd_journal_get_data_threshold(sd_journal *j, size_t *sz);
-
-int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *l);
-int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *l);
-void sd_journal_restart_data(sd_journal *j);
-
-int sd_journal_add_match(sd_journal *j, const void *data, size_t size);
-int sd_journal_add_disjunction(sd_journal *j);
-int sd_journal_add_conjunction(sd_journal *j);
-void sd_journal_flush_matches(sd_journal *j);
-
-int sd_journal_seek_head(sd_journal *j);
-int sd_journal_seek_tail(sd_journal *j);
-int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec);
-int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);
-int sd_journal_seek_cursor(sd_journal *j, const char *cursor);
-
-int sd_journal_get_cursor(sd_journal *j, char **cursor);
-int sd_journal_test_cursor(sd_journal *j, const char *cursor);
-
-int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to);
-int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, const sd_id128_t boot_id, uint64_t *from, uint64_t *to);
-
-int sd_journal_get_usage(sd_journal *j, uint64_t *bytes);
-
-int sd_journal_query_unique(sd_journal *j, const char *field);
-int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l);
-void sd_journal_restart_unique(sd_journal *j);
-
-int sd_journal_get_fd(sd_journal *j);
-int sd_journal_get_events(sd_journal *j);
-int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec);
-int sd_journal_process(sd_journal *j);
-int sd_journal_wait(sd_journal *j, uint64_t timeout_usec);
-int sd_journal_reliable_fd(sd_journal *j);
-
-int sd_journal_get_catalog(sd_journal *j, char **text);
-int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **text);
-
-/* the inverse condition avoids ambiguity of danling 'else' after the macro */
-#define SD_JOURNAL_FOREACH(j) \
- if (sd_journal_seek_head(j) < 0) { } \
- else while (sd_journal_next(j) > 0)
-
-/* the inverse condition avoids ambiguity of danling 'else' after the macro */
-#define SD_JOURNAL_FOREACH_BACKWARDS(j) \
- if (sd_journal_seek_tail(j) < 0) { } \
- else while (sd_journal_previous(j) > 0)
-
-#define SD_JOURNAL_FOREACH_DATA(j, data, l) \
- for (sd_journal_restart_data(j); sd_journal_enumerate_data((j), &(data), &(l)) > 0; )
-
-#define SD_JOURNAL_FOREACH_UNIQUE(j, data, l) \
- for (sd_journal_restart_unique(j); sd_journal_enumerate_unique((j), &(data), &(l)) > 0; )
-
-_SD_END_DECLARATIONS;
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2014 Tom Gundersen
- Copyright (C) 2014 Susant Sahani
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#pragma once
-
-#include "sd-event.h"
-
-typedef struct sd_lldp sd_lldp;
-
-typedef void (*sd_lldp_cb_t)(sd_lldp *lldp, int event, void *userdata);
-
-enum {
- UPDATE_INFO = 10,
-};
-
-typedef enum LLDPPortStatus {
- LLDP_PORT_STATUS_NONE,
- LLDP_PORT_STATUS_ENABLED,
- LLDP_PORT_STATUS_DISABLED,
- _LLDP_PORT_STATUS_MAX,
- _LLDP_PORT_STATUS_INVALID = -1,
-} LLDPPortStatus;
-
-int sd_lldp_new(int ifindex, const char *ifname, const struct ether_addr *mac, sd_lldp **ret);
-void sd_lldp_free(sd_lldp *lldp);
-
-int sd_lldp_start(sd_lldp *lldp);
-int sd_lldp_stop(sd_lldp *lldp);
-
-int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int priority);
-int sd_lldp_detach_event(sd_lldp *lldp);
-
-int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_cb_t cb, void *userdata);
-int sd_lldp_save(sd_lldp *lldp, const char *file);
* return an error for system processes. */
int sd_pid_get_owner_uid(pid_t pid, uid_t *uid);
-/* Get systemd unit (i.e. service) name from PID, for system
+/* 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 unit (i.e. service) name from PID, for user
+/* 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 slice name from PID. */
-int sd_pid_get_slice(pid_t pid, char **slice);
+/* Get the control group from a PID, relative to the root of the
+ * hierarchy. */
+int sd_pid_get_cgroup(pid_t pid, char **cgroup);
-/* Similar to sd_pid_get_session(), but retrieves data about peer of
- * connected AF_UNIX socket */
+/* 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 peer of
- * connected AF_UNIX socket */
+/* 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 peer of
- * connected AF_UNIX socket */
+/* 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 peer of
- * connected AF_UNIX socket */
+/* 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_machine_name(), but retrieves data about peer
- * of connected AF_UNIX socket */
+/* 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 a connected AF_UNIX socket */
int sd_peer_get_machine_name(int fd, char **machine);
-/* Similar to sd_pid_get_slice(), but retrieves data about peer of
- * connected AF_UNIX socket */
-int sd_peer_get_slice(int fd, char **slice);
+/* Similar to sd_pid_get_cgroup(), but retrieves data about the peer
+ * of a connected AF_UNIX socket. */
+int sd_peer_get_cgroup(pid_t pid, char **cgroup);
/* Get state from UID. Possible states: offline, lingering, online, active, closing */
int sd_uid_get_state(uid_t uid, char **state);
#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_HIBERNATE_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,73)
-#define SD_MESSAGE_CONFIG_ERROR SD_ID128_MAKE(c7,72,d2,4e,9a,88,4c,be,b9,ea,12,62,5c,30,6c,01)
+#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_BOOTCHART SD_ID128_MAKE(9f,26,aa,56,2c,f4,40,c2,b1,6c,77,3d,04,79,b5,18)
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdnetworkhfoo
-#define foosdnetworkhfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
- Copyright 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-#include <inttypes.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 **state);
-
-/* Get DNS entries for all links. These are string representations of
- * IP addresses */
-int sd_network_get_dns(char ***dns);
-
-/* Get NTP entries for all links. These are domain names or string
- * representations of IP addresses */
-int sd_network_get_ntp(char ***ntp);
-
-/* Get the search/routing domains for all links. */
-int sd_network_get_domains(char ***domains);
-
-/* 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 **state);
-
-/* 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 **state);
-
-/* Get path to .network file applied to link */
-int sd_network_link_get_network_file(int ifindex, char **filename);
-
-/* Get DNS entries for a given link. These are string representations of
- * IP addresses */
-int sd_network_link_get_dns(int ifindex, char ***addr);
-
-/* 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 ***addr);
-
-/* 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 **llmnr);
-
-int sd_network_link_get_lldp(int ifindex, char **lldp);
-
-/* Get the DNS domain names for a given link. */
-int sd_network_link_get_domains(int ifindex, char ***domains);
-
-/* Get the CARRIERS to which current link is bound to. */
-int sd_network_link_get_carrier_bound_to(int ifindex, char ***carriers);
-
-/* Get the CARRIERS that are bound to current link. */
-int sd_network_link_get_carrier_bound_by(int ifindex, char ***carriers);
-
-/* Returns whether or not domains that don't match any link should be resolved
- * on this link. 1 for yes, 0 for no and negative value for error */
-int sd_network_link_get_wildcard_domain(int ifindex);
-
-/* 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 *timeout_usec);
-
-_SD_END_DECLARATIONS;
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdpppoefoo
-#define foosdpppoefoo
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2014 Tom Gundersen
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-#include <net/ethernet.h>
-
-#include "sd-event.h"
-
-#include "sparse-endian.h"
-
-enum {
- PPPOE_EVENT_RUNNING = 0,
- PPPOE_EVENT_STOPPED = 1,
-};
-
-typedef struct sd_pppoe sd_pppoe;
-typedef void (*sd_pppoe_cb_t)(sd_pppoe *ppp, int event, void *userdata);
-
-int sd_pppoe_detach_event(sd_pppoe *ppp);
-int sd_pppoe_attach_event(sd_pppoe *ppp, sd_event *event, int priority);
-int sd_pppoe_get_channel(sd_pppoe *ppp, int *channel);
-int sd_pppoe_set_callback(sd_pppoe *ppp, sd_pppoe_cb_t cb, void *userdata);
-int sd_pppoe_set_ifindex(sd_pppoe *ppp, int ifindex);
-int sd_pppoe_set_ifname(sd_pppoe *ppp, const char *ifname);
-int sd_pppoe_set_service_name(sd_pppoe *ppp, const char *service_name);
-int sd_pppoe_start(sd_pppoe *ppp);
-int sd_pppoe_stop(sd_pppoe *ppp);
-sd_pppoe *sd_pppoe_ref(sd_pppoe *ppp);
-sd_pppoe *sd_pppoe_unref(sd_pppoe *ppp);
-int sd_pppoe_new (sd_pppoe **ret);
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdresolvehfoo
-#define foosdresolvehfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2005-2014 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-
-#include "_sd-common.h"
-#include "sd-event.h"
-
-_SD_BEGIN_DECLARATIONS;
-
-/* An opaque sd-resolve session structure */
-typedef struct sd_resolve sd_resolve;
-
-/* An opaque sd-resolve query structure */
-typedef struct sd_resolve_query sd_resolve_query;
-
-/* A callback on completion */
-typedef int (*sd_resolve_getaddrinfo_handler_t)(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata);
-typedef int (*sd_resolve_getnameinfo_handler_t)(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata);
-typedef int (*sd_resolve_res_handler_t)(sd_resolve_query* q, int ret, unsigned char *answer, void *userdata);
-
-enum {
- SD_RESOLVE_GET_HOST = 1ULL,
- SD_RESOLVE_GET_SERVICE = 2ULL,
- SD_RESOLVE_GET_BOTH = 3ULL
-};
-
-int sd_resolve_default(sd_resolve **ret);
-
-/* Allocate a new sd-resolve session. */
-int sd_resolve_new(sd_resolve **ret);
-
-/* Free a sd-resolve session. This destroys all attached
- * sd_resolve_query objects automatically. */
-sd_resolve* sd_resolve_unref(sd_resolve *resolve);
-sd_resolve* sd_resolve_ref(sd_resolve *resolve);
-
-/* Return the UNIX file descriptor to poll() for events on. Use this
- * function to integrate sd-resolve with your custom main loop. */
-int sd_resolve_get_fd(sd_resolve *resolve);
-
-/* Return the poll() events (a combination of flags like POLLIN,
- * POLLOUT, ...) to check for. */
-int sd_resolve_get_events(sd_resolve *resolve);
-
-/* Return the poll() timeout to pass. Returns (uint64_t) -1 as
- * timeout if no timeout is needed. */
-int sd_resolve_get_timeout(sd_resolve *resolve, uint64_t *timeout_usec);
-
-/* Process pending responses. After this function is called, you can
- * get the next completed query object(s) using
- * sd_resolve_get_next(). */
-int sd_resolve_process(sd_resolve *resolve);
-
-/* Wait for a resolve event to complete. */
-int sd_resolve_wait(sd_resolve *resolve, uint64_t timeout_usec);
-
-int sd_resolve_get_tid(sd_resolve *resolve, pid_t *tid);
-
-int sd_resolve_attach_event(sd_resolve *resolve, sd_event *e, int priority);
-int sd_resolve_detach_event(sd_resolve *resolve);
-sd_event *sd_resolve_get_event(sd_resolve *resolve);
-
-/* Issue a name-to-address query on the specified session. The
- * arguments are compatible with those of libc's
- * getaddrinfo(3). The function returns a new query object. When the
- * query is completed, you may retrieve the results using
- * sd_resolve_getaddrinfo_done(). */
-int sd_resolve_getaddrinfo(sd_resolve *resolve, sd_resolve_query **q, const char *node, const char *service, const struct addrinfo *hints, sd_resolve_getaddrinfo_handler_t callback, void *userdata);
-
-/* Issue an address-to-name query on the specified session. The
- * arguments are compatible with those of libc's
- * getnameinfo(3). The function returns a new query object. When the
- * query is completed, you may retrieve the results using
- * sd_resolve_getnameinfo_done(). Set gethost (resp. getserv) to non-zero
- * if you want to query the hostname (resp. the service name). */
-int sd_resolve_getnameinfo(sd_resolve *resolve, sd_resolve_query **q, const struct sockaddr *sa, socklen_t salen, int flags, uint64_t get, sd_resolve_getnameinfo_handler_t callback, void *userdata);
-
-/* Issue a resolver query on the specified session. The arguments are
- * compatible with those of libc's res_query(3). The function returns a new
- * query object. When the query is completed, you may retrieve the results using
- * sd_resolve_res_done(). */
-int sd_resolve_res_query(sd_resolve *resolve, sd_resolve_query **q, const char *dname, int clazz, int type, sd_resolve_res_handler_t callback, void *userdata);
-
-/* Issue a resolver query on the specified session. The arguments are
- * compatible with those of libc's res_search(3). The function returns a new
- * query object. When the query is completed, you may retrieve the results using
- * sd_resolve_res_done(). */
-int sd_resolve_res_search(sd_resolve *resolve, sd_resolve_query **q, const char *dname, int clazz, int type, sd_resolve_res_handler_t callback, void *userdata);
-
-sd_resolve_query *sd_resolve_query_ref(sd_resolve_query* q);
-sd_resolve_query *sd_resolve_query_unref(sd_resolve_query* q);
-
-/* Returns non-zero when the query operation specified by q has been completed. */
-int sd_resolve_query_is_done(sd_resolve_query*q);
-
-void *sd_resolve_query_get_userdata(sd_resolve_query *q);
-void *sd_resolve_query_set_userdata(sd_resolve_query *q, void *userdata);
-
-sd_resolve *sd_resolve_query_get_resolve(sd_resolve_query *q);
-
-_SD_END_DECLARATIONS;
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdrtnlhfoo
-#define foosdrtnlhfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-#include <inttypes.h>
-#include <netinet/in.h>
-#include <netinet/ether.h>
-#include <linux/rtnetlink.h>
-#include <linux/neighbour.h>
-
-#include "sd-event.h"
-#include "_sd-common.h"
-
-_SD_BEGIN_DECLARATIONS;
-
-typedef struct sd_rtnl sd_rtnl;
-typedef struct sd_rtnl_message sd_rtnl_message;
-
-/* callback */
-
-typedef int (*sd_rtnl_message_handler_t)(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata);
-
-/* bus */
-int sd_rtnl_new_from_netlink(sd_rtnl **nl, int fd);
-int sd_rtnl_open(sd_rtnl **nl, unsigned n_groups, ...);
-int sd_rtnl_open_fd(sd_rtnl **nl, int fd, unsigned n_groups, ...);
-int sd_rtnl_inc_rcvbuf(const sd_rtnl *const rtnl, const int size);
-
-sd_rtnl *sd_rtnl_ref(sd_rtnl *nl);
-sd_rtnl *sd_rtnl_unref(sd_rtnl *nl);
-
-int sd_rtnl_send(sd_rtnl *nl, sd_rtnl_message *message, uint32_t *serial);
-int sd_rtnl_call_async(sd_rtnl *nl, sd_rtnl_message *message,
- sd_rtnl_message_handler_t callback,
- void *userdata, uint64_t usec, uint32_t *serial);
-int sd_rtnl_call_async_cancel(sd_rtnl *nl, uint32_t serial);
-int sd_rtnl_call(sd_rtnl *nl, sd_rtnl_message *message, uint64_t timeout,
- sd_rtnl_message **reply);
-
-
-int sd_rtnl_get_events(sd_rtnl *nl);
-int sd_rtnl_get_timeout(sd_rtnl *nl, uint64_t *timeout);
-int sd_rtnl_process(sd_rtnl *nl, sd_rtnl_message **ret);
-int sd_rtnl_wait(sd_rtnl *nl, uint64_t timeout);
-int sd_rtnl_flush(sd_rtnl *nl);
-
-int sd_rtnl_add_match(sd_rtnl *nl, uint16_t match, sd_rtnl_message_handler_t c, void *userdata);
-int sd_rtnl_remove_match(sd_rtnl *nl, uint16_t match, sd_rtnl_message_handler_t c, void *userdata);
-
-int sd_rtnl_attach_event(sd_rtnl *nl, sd_event *e, int priority);
-int sd_rtnl_detach_event(sd_rtnl *nl);
-
-/* messages */
-int sd_rtnl_message_new_link(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t msg_type, int index);
-int sd_rtnl_message_new_addr_update(sd_rtnl *rtnl, sd_rtnl_message **ret, int index, int family);
-int sd_rtnl_message_new_addr(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t msg_type, int index,
- int family);
-int sd_rtnl_message_new_route(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t nlmsg_type,
- int rtm_family, unsigned char rtm_protocol);
-int sd_rtnl_message_new_neigh(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t msg_type, int index, int nda_family);
-
-sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m);
-sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m);
-
-int sd_rtnl_message_request_dump(sd_rtnl_message *m, int dump);
-int sd_rtnl_message_is_error(sd_rtnl_message *m);
-int sd_rtnl_message_get_errno(sd_rtnl_message *m);
-int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type);
-int sd_rtnl_message_is_broadcast(sd_rtnl_message *m);
-
-int sd_rtnl_message_get_family(sd_rtnl_message *m, int *family);
-
-int sd_rtnl_message_addr_set_prefixlen(sd_rtnl_message *m, unsigned char prefixlen);
-int sd_rtnl_message_addr_set_scope(sd_rtnl_message *m, unsigned char scope);
-int sd_rtnl_message_addr_set_flags(sd_rtnl_message *m, unsigned char flags);
-int sd_rtnl_message_addr_get_family(sd_rtnl_message *m, int *family);
-int sd_rtnl_message_addr_get_prefixlen(sd_rtnl_message *m, unsigned char *prefixlen);
-int sd_rtnl_message_addr_get_scope(sd_rtnl_message *m, unsigned char *scope);
-int sd_rtnl_message_addr_get_flags(sd_rtnl_message *m, unsigned char *flags);
-int sd_rtnl_message_addr_get_ifindex(sd_rtnl_message *m, int *ifindex);
-
-int sd_rtnl_message_link_set_flags(sd_rtnl_message *m, unsigned flags, unsigned change);
-int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type);
-int sd_rtnl_message_link_set_family(sd_rtnl_message *m, unsigned family);
-int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex);
-int sd_rtnl_message_link_get_flags(sd_rtnl_message *m, unsigned *flags);
-int sd_rtnl_message_link_get_type(sd_rtnl_message *m, unsigned *type);
-
-int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char prefixlen);
-int sd_rtnl_message_route_set_src_prefixlen(sd_rtnl_message *m, unsigned char prefixlen);
-int sd_rtnl_message_route_set_scope(sd_rtnl_message *m, unsigned char scope);
-int sd_rtnl_message_route_get_family(sd_rtnl_message *m, int *family);
-int sd_rtnl_message_route_get_dst_prefixlen(sd_rtnl_message *m, unsigned char *dst_len);
-int sd_rtnl_message_route_get_src_prefixlen(sd_rtnl_message *m, unsigned char *src_len);
-
-int sd_rtnl_message_neigh_set_flags(sd_rtnl_message *m, uint8_t flags);
-int sd_rtnl_message_neigh_set_state(sd_rtnl_message *m, uint16_t state);
-int sd_rtnl_message_neigh_get_family(sd_rtnl_message *m, int *family);
-int sd_rtnl_message_neigh_get_ifindex(sd_rtnl_message *m, int *family);
-int sd_rtnl_message_neigh_get_state(sd_rtnl_message *m, uint16_t *state);
-int sd_rtnl_message_neigh_get_flags(sd_rtnl_message *m, uint8_t *flags);
-
-int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data);
-int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t data);
-int sd_rtnl_message_append_u16(sd_rtnl_message *m, unsigned short type, uint16_t data);
-int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t data);
-int sd_rtnl_message_append_in_addr(sd_rtnl_message *m, unsigned short type, const struct in_addr *data);
-int sd_rtnl_message_append_in6_addr(sd_rtnl_message *m, unsigned short type, const struct in6_addr *data);
-int sd_rtnl_message_append_ether_addr(sd_rtnl_message *m, unsigned short type, const struct ether_addr *data);
-int sd_rtnl_message_append_cache_info(sd_rtnl_message *m, unsigned short type, const struct ifa_cacheinfo *info);
-
-int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type);
-int sd_rtnl_message_open_container_union(sd_rtnl_message *m, unsigned short type, const char *key);
-int sd_rtnl_message_close_container(sd_rtnl_message *m);
-
-int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, const char **data);
-int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data);
-int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data);
-int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data);
-int sd_rtnl_message_read_ether_addr(sd_rtnl_message *m, unsigned short type, struct ether_addr *data);
-int sd_rtnl_message_read_cache_info(sd_rtnl_message *m, unsigned short type, struct ifa_cacheinfo *info);
-int sd_rtnl_message_read_in_addr(sd_rtnl_message *m, unsigned short type, struct in_addr *data);
-int sd_rtnl_message_read_in6_addr(sd_rtnl_message *m, unsigned short type, struct in6_addr *data);
-int sd_rtnl_message_enter_container(sd_rtnl_message *m, unsigned short type);
-int sd_rtnl_message_exit_container(sd_rtnl_message *m);
-
-int sd_rtnl_message_rewind(sd_rtnl_message *m);
-
-sd_rtnl_message *sd_rtnl_message_next(sd_rtnl_message *m);
-
-_SD_END_DECLARATIONS;
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdshutdownhfoo
-#define foosdshutdownhfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-
- 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 <http://www.gnu.org/licenses/>.
-***/
-
-/* Interface for scheduling and cancelling timed shutdowns. */
-
-#include <inttypes.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifndef _sd_packed_
-# define _sd_packed_ __attribute__((packed))
-#endif
-
-typedef enum sd_shutdown_mode {
- SD_SHUTDOWN_NONE = 0,
- SD_SHUTDOWN_REBOOT = 'r',
- SD_SHUTDOWN_POWEROFF = 'P',
- SD_SHUTDOWN_HALT = 'H',
- SD_SHUTDOWN_KEXEC = 'K'
-} sd_shutdown_mode_t;
-
-/* Calculate the size of the message as "offsetof(struct
- * sd_shutdown_command, wall_message) +
- * strlen(command.wall_message)" */
-struct sd_shutdown_command {
- /* Microseconds after the epoch 1970 UTC */
- uint64_t usec;
-
- /* H, P, r, i.e. the switches usually passed to
- * /usr/bin/shutdown to select whether to halt, power-off or
- * reboot the machine */
- sd_shutdown_mode_t mode:8;
-
- /* If non-zero, don't actually shut down, just pretend */
- unsigned dry_run:1;
-
- /* If non-zero, send our wall message */
- unsigned warn_wall:1;
-
- /* The wall message to send around. Leave empty for the
- * default wall message */
- char wall_message[];
-} _sd_packed_;
-
-/* The scheme is very simple:
- *
- * To schedule a shutdown, simply fill in and send a single
- * AF_UNIX/SOCK_DGRAM datagram with the structure above suffixed with
- * the wall message to the socket /run/systemd/shutdownd (leave an
- * empty wall message for the default shutdown message). To calculate
- * the size of the message, use "offsetof(struct sd_shutdown_command,
- * wall_message) + strlen(command.wall_message)".
- *
- * To cancel a shutdown, do the same, but send a fully zeroed-out
- * structure.
- *
- * To be notified about scheduled shutdowns, create an inotify watch
- * on /run/shutdown/. Whenever a file called "scheduled" appears, a
- * shutdown is scheduled. If it is removed, it is canceled. If it is
- * replaced, the scheduled shutdown has been changed. The file contains
- * a simple, environment-like block that contains information about
- * the scheduled shutdown:
- *
- * USEC=
- * encodes the time for the shutdown in usecs since the epoch UTC,
- * formatted as a numeric string.
- *
- * WARN_WALL=
- * is 1 if a wall message shall be sent
- *
- * DRY_RUN=
- * is 1 if a dry-run shutdown is scheduled
- *
- * MODE=
- * is the shutdown mode, one of "poweroff", "reboot", "halt", "kexec"
- *
- * WALL_MESSAGE=
- * is the wall message to use, with all special characters escaped in C-style.
- *
- * Note that some fields might be missing if they do not apply.
- *
- * Note that the file is first written to a temporary file and then
- * renamed, in order to provide atomic properties for readers: if the
- * file exists under the name "scheduled", it is guaranteed to be fully
- * written. A reader should ignore all files in that directory by any
- * other name.
- *
- * Scheduled shutdowns are only accepted from privileged processes,
- * but anyone may watch the directory and the file in it.
- */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
+++ /dev/null
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdutf8hfoo
-#define foosdutf8hfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Lennart Poettering
-
- 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 <http://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
# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
#
-# This file is part of systemd.
+# This file is part of elogind.
#
# Copyright 2012-2013 Zbigniew Jędrzejewski-Szmek
#
-# systemd is free software; you can redistribute it and/or modify it
+# elogind 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
+# elogind 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 <http://www.gnu.org/licenses/>.
+# along with elogind; If not, see <http://www.gnu.org/licenses/>.
import sys
import collections
from copy import deepcopy
TEMPLATE = '''\
-<refentry id="systemd.directives" conditional="HAVE_PYTHON">
+<refentry id="elogind.directives" conditional="HAVE_PYTHON">
<refentryinfo>
- <title>systemd.directives</title>
- <productname>systemd</productname>
+ <title>elogind.directives</title>
+ <productname>elogind</productname>
<authorgroup>
<author>
</refentryinfo>
<refmeta>
- <refentrytitle>systemd.directives</refentrytitle>
+ <refentrytitle>elogind.directives</refentrytitle>
<manvolnum>7</manvolnum>
</refmeta>
<refnamediv>
- <refname>systemd.directives</refname>
+ <refname>elogind.directives</refname>
<refpurpose>Index of configuration directives</refpurpose>
</refnamediv>
- <refsect1>
- <title>Unit directives</title>
-
- <para>Directives for configuring units, used in unit
- files.</para>
-
- <variablelist id='unit-directives' />
- </refsect1>
-
- <refsect1>
- <title>Options on the kernel command line</title>
-
- <para>Kernel boot options for configuring the behaviour of the
- systemd process.</para>
-
- <variablelist id='kernel-commandline-options' />
- </refsect1>
-
- <refsect1>
- <title>Environment variables</title>
-
- <para>Environment variables understood by the systemd
- manager and other programs.</para>
-
- <variablelist id='environment-variables' />
- </refsect1>
-
- <refsect1>
- <title>UDEV directives</title>
-
- <para>Directives for configuring systemd units through the
- udev database.</para>
-
- <variablelist id='udev-directives' />
- </refsect1>
-
- <refsect1>
- <title>Network directives</title>
-
- <para>Directives for configuring network links through the
- net-setup-link udev builtin and networks through
- systemd-networkd.</para>
-
- <variablelist id='network-directives' />
- </refsect1>
-
- <refsect1>
- <title>Journal fields</title>
-
- <para>Fields in the journal events with a well known meaning.</para>
-
- <variablelist id='journal-directives' />
- </refsect1>
-
<refsect1>
<title>PAM configuration directives</title>
</refsect1>
<refsect1>
- <title><filename>/etc/crypttab</filename> and
- <filename>/etc/fstab</filename> options</title>
-
- <para>Options which influence mounted filesystems and
- encrypted volumes.</para>
-
- <variablelist id='fstab-options' />
- </refsect1>
-
- <refsect1>
- <title>System manager directives</title>
-
- <para>Directives for configuring the behaviour of the
- systemd process.</para>
-
- <variablelist id='systemd-directives' />
- </refsect1>
-
- <refsect1>
- <title>bootchart.conf directives</title>
+ <title>elogind manager directives</title>
<para>Directives for configuring the behaviour of the
- systemd-bootchart process.</para>
+ elogind process.</para>
- <variablelist id='bootchart-directives' />
+ <variablelist id='elogind-directives' />
</refsect1>
<refsect1>
<title>command line options</title>
<para>Command-line options accepted by programs in the
- systemd suite.</para>
+ elogind suite.</para>
<variablelist id='options' />
</refsect1>
<refsect1>
<title>Constants</title>
- <para>Various constant used and/or defined by systemd.</para>
+ <para>Various constant used and/or defined by elogind.</para>
<variablelist id='constants' />
</refsect1>
# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
#
-# This file is part of systemd.
+# This file is part of elogind.
#
# Copyright 2012 Lennart Poettering
# Copyright 2013 Zbigniew Jędrzejewski-Szmek
#
-# systemd is free software; you can redistribute it and/or modify it
+# elogind 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
+# elogind 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 <http://www.gnu.org/licenses/>.
+# along with elogind; If not, see <http://www.gnu.org/licenses/>.
import collections
import sys
MDASH = ' — ' if sys.version_info.major >= 3 else ' -- '
TEMPLATE = '''\
-<refentry id="systemd.index" conditional="HAVE_PYTHON">
+<refentry id="elogind.index" conditional="HAVE_PYTHON">
<refentryinfo>
- <title>systemd.index</title>
- <productname>systemd</productname>
+ <title>elogind.index</title>
+ <productname>elogind</productname>
<authorgroup>
<author>
</refentryinfo>
<refmeta>
- <refentrytitle>systemd.index</refentrytitle>
+ <refentrytitle>elogind.index</refentrytitle>
<manvolnum>7</manvolnum>
</refmeta>
<refnamediv>
- <refname>systemd.index</refname>
- <refpurpose>List all manpages from the systemd project</refpurpose>
+ <refname>elogind.index</refname>
+ <refpurpose>List all manpages from the elogind project</refpurpose>
</refnamediv>
</refentry>
'''
<refsect1>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
+ <citerefentry><refentrytitle>elogind.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
</para>
<para id='counts' />
# Really, do not edit this file.
EXTRA_DIST += \\
- {files}
+ {dist_files}
'''
def man(page, number):
def mjoin(files):
return ' \\\n\t'.join(sorted(files) or '#')
-def make_makefile(rules, files):
+def make_makefile(rules, dist_files):
return HEADER + '\n'.join(
(CONDITIONAL if conditional else SECTION).format(
manpages=mjoin(set(rulegroup.values())),
if k != v),
conditional=conditional)
for conditional,rulegroup in sorted(rules.items())
- ) + FOOTER.format(files=mjoin(sorted(files)))
+ ) + FOOTER.format(dist_files=mjoin(sorted(dist_files)))
if __name__ == '__main__':
rules = create_rules(sys.argv[1:])
- files = (xml(file) for file in sys.argv[1:])
- print(make_makefile(rules, files), end='')
+ dist_files = (xml(file) for file in sys.argv[1:]
+ if not file.endswith(".directives.xml") and
+ not file.endswith(".index.xml"))
+ print(make_makefile(rules, dist_files), end='')