Commit Graph

244 Commits

Author SHA1 Message Date
Lennart Poettering 1d84ad9445 util-lib: various improvements to kernel command line parsing
This improves kernel command line parsing in a number of ways:

a) An kernel option "foo_bar=xyz" is now considered equivalent to
   "foo-bar-xyz", i.e. when comparing kernel command line option names "-" and
   "_" are now considered equivalent (this only applies to the option names
   though, not the option values!). Most of our kernel options used "-" as word
   separator in kernel command line options so far, but some used "_". With
   this change, which was a source of confusion for users (well, at least of
   one user: myself, I just couldn't remember that it's systemd.debug-shell,
   not systemd.debug_shell). Considering both as equivalent is inspired how
   modern kernel module loading normalizes all kernel module names to use
   underscores now too.

b) All options previously using a dash for separating words in kernel command
   line options now use an underscore instead, in all documentation and in
   code. Since a) has been implemented this should not create any compatibility
   problems, but normalizes our documentation and our code.

c) All kernel command line options which take booleans (or are boolean-like)
   have been reworked so that "foobar" (without argument) is now equivalent to
   "foobar=1" (but not "foobar=0"), thus normalizing the handling of our
   boolean arguments. Specifically this means systemd.debug-shell and
   systemd_debug_shell=1 are now entirely equivalent.

d) All kernel command line options which take an argument, and where no
   argument is specified will now result in a log message. e.g. passing just
   "systemd.unit" will no result in a complain that it needs an argument. This
   is implemented in the proc_cmdline_missing_value() function.

e) There's now a call proc_cmdline_get_bool() similar to proc_cmdline_get_key()
   that parses booleans (following the logic explained in c).

f) The proc_cmdline_parse() call's boolean argument has been replaced by a new
   flags argument that takes a common set of bits with proc_cmdline_get_key().

g) All kernel command line APIs now begin with the same "proc_cmdline_" prefix.

h) There are now tests for much of this. Yay!
2016-12-21 19:09:08 +01:00
Daniel Wagner a92cf7840f udevd: check correct return value of fcntl() (#4758)
This looks like a copy&paste error from the code block above.
2016-11-28 18:24:26 +01:00
Zbigniew Jędrzejewski-Szmek f97b34a629 Rename formats-util.h to format-util.h
We don't have plural in the name of any other -util files and this
inconsistency trips me up every time I try to type this file name
from memory. "formats-util" is even hard to pronounce.
2016-11-07 10:15:08 -05:00
Zbigniew Jędrzejewski-Szmek d7f69e16f1 tree-wide: make parse_proc_cmdline() strip "rd." prefix automatically
This stripping is contolled by a new boolean parameter. When the parameter
is true, it means that the caller does not care about the distinction between
initrd and real root, and wants to act on both rd-dot-prefixed and unprefixed
parameters in the initramfs, and only on the unprefixed parameters in real
root. If the parameter is false, behaviour is the same as before.

Changes by caller:
log.c (systemd.log_*):      changed to accept rd-dot-prefix params
pid1:                       no change, custom logic
cryptsetup-generator:       no change, still accepts rd-dot-prefix params
debug-generator:            no change, does not accept rd-dot-prefix params
fsck:                       changed to accept rd-dot-prefix params
fstab-generator:            no change, custom logic
gpt-auto-generator:         no change, custom logic
hibernate-resume-generator: no change, does not accept rd-dot-prefix params
journald:                   changed to accept rd-dot-prefix params
modules-load:               no change, still accepts rd-dot-prefix params
quote-check:                no change, does not accept rd-dot-prefix params
udevd:                      no change, still accepts rd-dot-prefix params

I added support for "rd." params in the three cases where I think it's
useful: logging, fsck options, journald forwarding options.
2016-10-22 16:08:55 -04:00
Zbigniew Jędrzejewski-Szmek 92e724670f udev: change kernel commandline option parsing
- do not crash if an option without value is specified on the kernel command
  line, e.g. "udev.log-priority" :P
- simplify the code a bit
- warn about unknown "udev.*" options — this should make it easier to spot
  typos and reduce user confusion
2016-10-22 14:42:12 -04:00
Zbigniew Jędrzejewski-Szmek 96287a4916 tree-wide: allow state to be passed through to parse_proc_cmdline_item
No functional change.
2016-10-22 14:24:52 -04:00
0xAX c76cf844d6 tree-wide: pass return value of make_null_stdio() to warning instead of errno (#4328)
as @poettering suggested in the #4320
2016-10-10 19:51:33 +02:00
0xAX 9fc932bff1 tree-wide: print warning in a failure case of make_null_stdio() (#4320)
The make_null_stdio() may fail. Let's check its result and print
warning message instead of keeping silence.
2016-10-09 22:55:24 -04:00
Michal Sekletar 1ef72b55ba udev: inform systemd how many workers we can potentially spawn (#4036) 2016-08-25 08:18:42 +02:00
Lennart Poettering 1d98fef17d core: when forcibly killing/aborting left-over unit processes log about it
Let's lot at LOG_NOTICE about any processes that we are going to
SIGKILL/SIGABRT because clean termination of them didn't work.

This turns the various boolean flag parameters to cg_kill(), cg_migrate() and
related calls into a single binary flags parameter, simply because the function
now gained even more parameters and the parameter listed shouldn't get too
long.

Logging for killing processes is done either when the kill signal is SIGABRT or
SIGKILL, or on explicit request if KILL_TERMINATE_AND_LOG instead of LOG_TERMINATE
is passed. This isn't used yet in this patch, but is made use of in a later
patch.
2016-07-20 14:35:15 +02:00
Franck Bui e95764ec91 udevd: try close ctrl connection blocking socket only once when spawning a new worker (#3387) 2016-05-30 16:35:34 +02:00
Zbigniew Jędrzejewski-Szmek 045e00cf16 udevd: use (void) to silence coverity
CID #1351429.
2016-04-08 21:08:18 -04:00
Zbigniew Jędrzejewski-Szmek c3dacc8bbf selinux: always try to load the full selinux db
https://github.com/systemd/systemd/pull/2508#issuecomment-190901170
Maybe fixes https://bugzilla.redhat.com/show_bug.cgi?id=1308771.
2016-03-01 20:39:30 -05:00
Lennart Poettering 6d2353394f udev: fix cg_unified() return code checking
Fixes fall-out from 8b3aa503c1.

Fixes: #2635
2016-02-16 15:30:04 +01:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Zbigniew Jędrzejewski-Szmek efa1606eec udevd: do not clean up fds in main
fds will also be closed during manager cleanup in run, leading
to an error when we try to close them again. It is now possible
to "leak" the fds on error, but it's an unlikely event and we
will exit immediately anyway.

Fixes #2418.
2016-01-23 11:45:22 -05:00
Zbigniew Jędrzejewski-Szmek e5d7bce12d udevd: initialize fds to -1 and close them before exiting
Little change in practice, because the program will exit soon
afterwards, but the standard style of closing all fds is now followed.
Also gets rid of gcc warning about fd_ctrl and fd_uevent being
unitialized.
2016-01-18 15:21:28 -05:00
Zbigniew Jędrzejewski-Szmek 3f65d73149 udev: add emacs header line
Otherwise emacs wants to use 2-space indentation and other
attrocities.
2015-12-07 00:45:08 -05:00
Lennart Poettering 4afd3348c7 tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.

With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.

The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).

This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.

Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:

       #define _cleanup_(function) __attribute__((cleanup(function)))

Or similar, to make the gcc feature easier to use.

Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.

See #2008.
2015-11-27 19:19:36 +01:00
Lennart Poettering 4a0b58c4a3 tree-wide: use right cast macros for UIDs, GIDs and PIDs 2015-11-17 00:52:10 +01:00
Michal Schmidt 709f6e46a3 treewide: use the negative error codes returned by our functions
Our functions return negative error codes.
Do not rely on errno being set after calling our own functions.
2015-11-05 13:44:06 +01:00
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering 8f328d36c9 socket-util: move CMSG_FOREACH() from macro.h to socket-util.h 2015-10-27 13:25:57 +01:00
Lennart Poettering ee104e11e3 user-util: move UID/GID related macros from macro.h to user-util.h 2015-10-27 13:25:57 +01:00
Lennart Poettering 4e731273ed util-lib: move /proc/cmdline parsing code to proc-cmdline.[ch] 2015-10-27 13:25:57 +01:00
Lennart Poettering f4f15635ec util-lib: move a number of fs operations into fs-util.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering 6bedfcbb29 util-lib: split string parsing related calls from util.[ch] into parse-util.[ch] 2015-10-27 13:25:55 +01:00
Lennart Poettering c004493cde util-lib: split out IO related calls to io-util.[ch] 2015-10-26 01:24:38 +01:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +01:00
Lennart Poettering 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Lennart Poettering 618234a525 basic: split out cpu set specific APIs into cpu-set-util.[ch] 2015-09-30 22:26:16 +02:00
Lennart Poettering ece174c543 tree-wide: drop {} from one-line if blocks
Patch via coccinelle.
2015-09-09 08:20:20 +02:00
David Herrmann e859aa9e99 udev: ignore ENOEXEC from cgroup lookup
The recent cgroup-rework changed the error code for un-mounted cgroupfs to
ENOEXEC. Make sure udev ignores it just like ENOENT and does not spill
warnings on the screen.
2015-09-03 15:19:15 +02:00
Lennart Poettering 38a03f06a7 sd-event: make sure sd_event_now() cannot fail
Previously, if the event loop never ran before sd_event_now() would
fail. With this change it will instead fall back to invoking now(). This
way, the function cannot fail anymore, except for programming error when
invoking it with wrong parameters.

This takes into account the fact that many callers did not handle the
error condition correctly, and if the callers did, then they kept simply
invoking now() as fall back on their own. Hence let's shorten the code
using this call, and make things more robust, and let's just fall back
to now() internally.

Whether now() is used or the cache timestamp may still be detected via
the return value of sd_event_now(). If > 0 is returned, then the fall
back to now() was used, if == 0 is returned, then the cached value was
returned.

This patch also simplifies many of the invocations of sd_event_now():
the manual fall back to now() can be removed. Also, in cases where the
call is invoked withing void functions we can now protect the invocation
via assert_se(), acknowledging the fact that the call cannot fail
anymore except for programming errors with the parameters.

This change is inspired by #841.
2015-08-03 17:34:49 +02:00
Daniel Mack 7121b2152a Merge pull request #702 from ldzhong/fix
udev: fix parameter process
2015-07-24 11:59:30 +02:00
Lidong Zhong e14b6f211c udev: fix parameter process 2015-07-24 16:39:55 +08:00
Michal Schmidt 3567afa5b4 udev: unify reporting of invalid cmdline keys
This way it does not need distinct string literals and it also preserves
the "rd." prefix.
2015-07-21 20:07:34 +02:00
Michal Schmidt e00f5bddde udev: fix crash with invalid udev.log-priority
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1245293
2015-07-21 20:07:34 +02:00
Daniel Mack ad118bda15 tree-wide: fix write_string_file() user that should not create files
The latest consolidation cleanup of write_string_file() revealed some users
of that helper which should have used write_string_file_no_create() in the
past but didn't. Basically, all existing users that write to files in /sys
and /proc should not expect to write to a file which is not yet existant.
2015-07-06 19:27:20 -04:00
Daniel Mack 4c1fc3e404 fileio: consolidate write_string_file*()
Merge write_string_file(), write_string_file_no_create() and
write_string_file_atomic() into write_string_file() and provide a flags mask
that allows combinations of atomic writing, newline appending and automatic
file creation. Change all users accordingly.
2015-07-06 19:19:25 -04:00
Daniel Mack b6b3475533 Merge pull request #463 from dvdhrm/udev-run
udev: destroy manager before cleaning environment
2015-07-01 13:42:16 -04:00
David Herrmann 077fc5e260 udev: destroy manager before cleaning environment
Due to our _cleanup_ usage for the udev manager, it will be destroyed
after the "exit:" label has finished. Therefore, it is the last
destruction done in main(). This has two side-effects:
  - mac_selinux is destroyed before the udev manager is, possible causing
    use-after-free if the manager-cleanup accesses selinux data
  - log_close() is called *before* the manager is destroyed, possibly
    re-opening the log if you use --debug (and thus not re-applying the
    --debug option)

Avoid this by moving the manager-handling into a new function called
run(). This function will be left before we enter the "exit:" label in
main(), hence, the manager object will be destroyed early.
2015-07-01 19:30:24 +02:00
Kay Sievers 78d3e041a5 udevd: force --debug mode to stderr
https://github.com/systemd/systemd/issues/462
2015-07-01 19:20:59 +02:00
Tom Gundersen b6aab8ef9c udev: worker - check return value of udev_monitor_enable_receiving()
Fixes CID#1297430.
2015-06-23 17:20:12 +02:00
Tom Gundersen 11b9fb15be udevd: suppress warning if we don't find cgroup
This is expected on non-systemd systems, so just log it at debug level.

This fixes issue #309.
2015-06-22 16:55:49 +02:00
Thomas Hindoe Paaboel Andersen 118786182b udved: remove dead code
Leftover from 6af5e6a4c9
2015-06-17 20:33:25 +02:00
Tom Gundersen 40e749b59b udevd: daemon - connect /dev/null to std{in,out,err} in debug mode
This is essentially a revert of 5c67cf2 and fixes issue #190.
2015-06-17 18:07:20 +02:00
David Herrmann ab7854df73 udev: don't close FDs before dropping them from epoll
Make sure we never close fds before we drop their related event-source.
This will cause horrible disruptions if the fd-num is re-used by someone
else. Under normal conditions, this should not cause any problems as the
close() will drop the fd from the epoll-set automatically. However, this
changes if you have any child processes with a copy of that fd.

This fixes issue #163.

Background:
        If you create an epoll-set via epoll_create() (lets call it 'EFD')
        you can add file-descriptors to it to watch for events. Whenever
        you call EPOLL_CTL_ADD on a file-descriptor you want to watch, the
        kernel looks up the attached "struct file" pointer, that this FD
        refers to. This combination of the FD-number and the "struct file"
        pointer is used as key to link it into the epoll-set (EFD).

        This means, if you duplicate your file-descriptor, you can watch
        this file-descriptor, too (because the duplicate will have a
        different FD-number, hence, the combination of FD-number and
        "struct file" is different as before).

        If you want to stop watching an FD, you use EPOLL_CTL_DEL and pass
        the FD to the kernel. The kernel again looks up your
        file-descriptor in your FD-table to find the linked "struct file".
        This FD-number and "struct file" combination is then dropped from
        the epoll-set (EFD).

        Last, but not least: If you close a file-descriptor that is linked
        to an epoll-set, the kernel does *NOTHING* regarding the
        epoll-set. This is a vital observation! Because this means, your
        epoll_wait() calls will still return the metadata you used to
        watch/subscribe your file-descriptor to events.
        There is one exception to this rule: If the file-descriptor that
        you just close()ed was the last FD that referred to the underlying
        "struct file", then _all_ epoll-set watches/subscriptions are
        destroyed. Hence, if you never dup()ed your FD, then a simple
        close() will also unsubscribe it from any epoll-set.

        With this in mind, lets look at fork():
                Assume you have an epoll-set (EFD) and a bunch of FDs
                subscribed to events on that EFD. If you now call fork(),
                the new process gets a copy of your file-descriptor table.
                This means, the whole table is copied and the "struct
                file" reference of each FD is increased by 1. It is
                important to notice that the FD-numbers in the child are
                exactly the same as in the parent (eg., FD #5 in the child
                refers to the same "struct file" as FD #5 in the parent).

                This means, if the child calls EPOLL_CTL_DEL on an FD, the
                kernel will look up the linked "struct file" and drop the
                FD-number and "struct file" combination from the epoll-set
                (EFD). However, this will effectively drop the
                subscription that was installed by the parent.

                To sum up: even though the child gets a duplicate of the
                EFD and all FDs, the subscriptions in the EFD are *NOT*
                duplicated!

Now, with this in mind, lets look at what udevd does:
        Udevd has a bunch of file-descriptors that it watches in its
        sd-event main-loop. Whenever a uevent is received, the event is
        dispatched on its workers. If no suitable worker is present, a new
        worker is fork()ed to handle the event. Inside of this worker, we
        try to free all resources we inherited. However, the fork() call
        is done from a call-stack that is never rewinded. Therefore, this
        call stack might own references that it drops once it is left.
        Those references we cannot deduce from the fork()'ed process;
        effectively causing us to leak objects in the worker (eg., the
        call to sd_event_dispatch() that dispatched our uevent owns a
        reference to the sd_event object it used; and drops it again once
        the function is left).

        (Another example is udev_monitor_ref() for each 'worker' that is
         also inherited by all children; thus keeping the udev-monitor and
         the uevent-fd alive in all children (which is the real cause for
         bug #163))

        (The extreme variant is sd_event_source_unref(), which explicitly
         keeps event-sources alive, if they're currently dispatched,
         knowing that the dispatcher will free the event once done. But
         if the dispatcher is in the parent, the child will never ever
         free that object, thus leaking it)

        This is usually not an issue. However, if such an object has a
        file-descriptor embedded, this FD is left open and never closed in
        the child.

In manager_exit(), if we now destroy an object (i.e., close its embedded
file-descriptor) before we destroy its related sd_event_source, then
sd-event will not be able to drop the FD from the epoll-set (EFD). This
is, because the FD is no longer valid at the time we call EPOLL_CTL_DEL.
Hence, the kernel cannot figure out the linked "struct file" and thus
cannot remove the FD-number plus "struct file" combination; effectively
leaving the subscription in the epoll-set.
Since we leak the uevent-fd in the children, they retain a copy of the FD
pointing to the same "struct file". Thus, the EFD-subscription are not
automatically removed by close() (as described above). Therefore, the main
daemon will still get its metadata back on epoll_watch() whenever an event
occurs (even though it already freed the metadata). This then causes the
free-after-use bug described in #163.

This patch fixes the order in which we destruct objects and related
sd-event-sources. Some open questions remain:

 * Why does source_io_unregister() not warn on EPOLL_CTL_DEL failures?
   This really needs to be turned into an assert_return().

 * udevd really should not leak file-descriptors into its children. Fixing
   this would *not* have prevented this bug, though (since the child-setup
   is still async).
   It's non-trivial to fix this, though. The stack-context of the caller
   cannot be rewinded, so we cannot figure out temporary refs. Maybe it's
   time to exec() the udev-workers?

 * Why does the kernel not copy FD-subscriptions across fork()?
   Or at least drop subscriptions if you close() your FD (it uses the
   FD-number as key, so it better subscribe to it)?
   Or it better used
         FD+"struct file_table*"+"struct file*"
   as key to not allow the childen to share the subscription table..
   *sigh*
   Seems like we have to live with that API forever.
2015-06-17 00:31:57 +02:00
Lennart Poettering 72c0a2c255 everywhere: port everything to sigprocmask_many() and friends
This ports a lot of manual code over to sigprocmask_many() and friends.

Also, we now consistly check for sigprocmask() failures with
assert_se(), since the call cannot realistically fail unless there's a
programming error.

Also encloses a few sd_event_add_signal() calls with (void) when we
ignore the return values for it knowingly.
2015-06-15 20:13:23 +02:00
Tom Gundersen 1c4baffc18 sd-netlink: rename from sd-rtnl 2015-06-13 19:52:54 +02:00