Commit Graph

58 Commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
Lennart Poettering 4cbbc2a2a4 sd-login: make use of _cleanup_close_ where possible 2018-02-26 18:45:45 +01:00
Lennart Poettering 665dfe9318 io-util: make flush_fd() return how many bytes where flushed
This is useful so that callers know whether anything at all and how much
was flushed.

This patches through users of this functions to ensure that the return
values > 0 which may be returned now are not propagated in public APIs.

Also, users that ignore the return value are changed to do so explicitly
now.
2018-01-05 13:55:08 +01:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Djalal Harouni df6d1a29b8 Merge pull request #6311 from keszybz/memleak
Fix trivial memleak in sd-login code
2017-07-09 17:58:52 +02:00
Yu, Li-Yu 4c4bc54697 sd_uid_get_state: do not return -ENOENT if state is "offline" (#6302) 2017-07-08 18:59:29 -04:00
Zbigniew Jędrzejewski-Szmek 964806347b sd-login: fix memleak when output argument is NULL 2017-07-08 18:04:44 -04:00
Yu Watanabe 76ed21e1e6 sd-login: sd_get_machine_names(): do not return -EINVAL when output parameter is NULL
Other functions in sd-login generally allow the output parameter to be NULL, in
which case only the number of items that would be stored in the array is returned.
Be nice and do the same here.
2017-05-31 19:22:38 -04:00
Yu Watanabe 2b5e92673d sd-login: treat missing /run/systemd/{seats,sessions,users} the same as empty
C.f. 0543105b0f.
This makes if /run/systemd/{seats,sessions,users} are missing, then
sd_get_seats(), sd_get_sessions() and sd_get_uids() return 0, that is,
an empty list, instead of -ENOENT.
2017-05-31 19:01:13 -04:00
Zbigniew Jędrzejewski-Szmek bc9e9af137 sd-login: translate -ENOMEDIUM to -ENODATA
The -ENOMEDIUM return value was introduced in v232-1001-g2977724b09,
('core: make hybrid cgroup unified mode keep compat /sys/fs/cgroup/systemd hierarchy'),
and would be returned by cg_pid_get_path_shifted(), but the documented and
expected return value is -ENODATA. Let's just catch ENXIO/ENOMEDIUM and translate
it to ENODATA in all cases.

Complements 171f8f591f, fixes #6012.
2017-05-30 21:17:46 -04:00
Zbigniew Jędrzejewski-Szmek 0543105b0f sd-login: fix querying machines when machined is not running
We should not leak the internal error from missing directory and treat
that case the same as no machines.
2017-05-19 07:52:00 -04:00
Zbigniew Jędrzejewski-Szmek 171f8f591f sd-login,test-login: return -ENODATA from sd_pid_get_unit too
After all, we might be running on a non-systemd system.
2017-05-18 22:32:19 -04:00
Zbigniew Jędrzejewski-Szmek d440fb97ba sd-login: fix return value of sd_pid_get_user_unit
E.g. "/user.slice/user-1000.slice/session-15.scope" would cause -ENXIO to be
returned.
2017-05-18 22:11:58 -04:00
Zbigniew Jędrzejewski-Szmek 19d64d10c0 sd-login: fix return value of sd_pid_get_session
We'd return -ENXIO, even thoug -ENODATA is documented.
2017-05-18 21:59:59 -04:00
Zbigniew Jędrzejewski-Szmek cc6182e854 sd-login: always return two arrays of same length from sd_seat_get_sessions
sd_seat_get_sessions returns two arrays, that in principle should always match:
the session names and corresponding uids. The second array could be shorter only
if parsing or uid conversion fails. But in that case there is no way to tell
*which* uid is wrong, so they are *all* useless. It's better to simplify things and
just return an error if parsing fails.
2017-05-18 21:59:09 -04:00
Zbigniew Jędrzejewski-Szmek d3cfab3148 sd-login: read list of uids of sessions from UIDS not ACTIVE_SESSIONS
As described by Luke Shumaker:

  sd_seat_get_sessions looks at /run/systemd/seats/${seat_name}:SESSIONS to get
  the list of sessions (which I believe is correct), and at
  /run/systemd/seats/${seat_name}:ACTIVE_SESSIONS for the list of users (which
  I believe is incorrect); I believe that it should look at the UIDS field for
  the list of users. As far as I can tell, the ACTIVE_SESSIONS field is never
  even present in the seats file. I also believe that this has been broken
  since the function was first committed almost 6 years ago.

Fixes #5743.
2017-05-18 21:37:41 -04:00
Reverend Homer 8fb3f00997 tree-wide: replace all readdir cycles with FOREACH_DIRENT{,_ALL} (#4853) 2016-12-09 10:04:30 +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
Michael Biebl 595bfe7df2 Various fixes for typos found by lintian (#3705) 2016-07-12 12:52:11 +02: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 b326715278 tree-wide: check if errno is greater than zero (2)
Compare errno with zero in a way that tells gcc that
(if the condition is true) errno is positive.
2016-01-13 15:10:17 -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 6ad623a3f7 parse-util: introduce parse_ifindex() and make use of it everywhere
We have enough places where we parse an ifindex, hence introduce a
proper parsing function for it, that verifies all parameters.
2015-11-03 00:02:00 +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 f4f15635ec util-lib: move a number of fs operations into fs-util.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering a09561746f util-lib: introduce dirent-util.[ch] for directory entry calls
Also, move a couple of more path-related functions to path-util.c.
2015-10-27 13:25:56 +01:00
Lennart Poettering bb15fafe9c util: move filename_is_valid() and path_is_safe() to path-util.[ch] 2015-10-27 13:25:55 +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 2583fbea8e socket-util: move remaining socket-related calls from util.[ch] to socket-util.[ch] 2015-10-26 01:24:39 +01:00
Lennart Poettering b1d4f8e154 util-lib: split out user/group/uid/gid calls into user-util.[ch] 2015-10-26 01:24:38 +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 4f5dd3943b util: split out escaping code into escape.[ch]
This really deserves its own file, given how much code this is now.
2015-10-24 23:04:42 +02:00
David Herrmann 4211d5bd13 sd-login: fix sd_seat_get_active() to return ENODATA
This seems to be an oversight from:
    707b66c663

We have to return ENODATA instead of ENOENT if a requested entry is
non-present. Also fix the call-site in udev to check for these errors.
2015-09-08 14:03:22 +02:00
Lennart Poettering f5aaf57562 sd-login: add new sd_pid_get_cgroup() API
This adds a new sd_pid_get_cgroup() call to sd-login which may be used
to query the control path of a process. This is useful for programs when
making use of delegation units, in order to figure out which subtree has
been delegated.

In light of the unified control group hierarchy this is finally safe to
do, hence let's add a proper API for it, to make it easier to use this.
2015-09-05 14:56:23 +02:00
Lennart Poettering 707b66c663 sd-login: rework error handling
Makre sure we always return sensible errors for the various, following
the same rules, and document them in a comment in sd-login.c. Also,
update all relevant man pages accordingly.
2015-09-04 09:07:31 +02:00
Lennart Poettering a1f686daf5 util: add new uid_is_valid() call
This simply factors out the uid validation checks from parse_uid() and
uses them everywhere. This simply verifies that the passed UID is
neither 64bit -1 nor 32bit -1.
2015-09-04 09:07:30 +02:00
Lennart Poettering fc60d8153c sd-login: improve error handling
let's return ENXIO whenever we don't know something rather than ENOENT.

ENOENT suggests this was really about a file or directory, while ENXIO
is a more generic "not found" indicator.
2015-09-01 17:20:56 +02:00
Lennart Poettering b9a8d25081 machined: validate machine names at more places
When enumerating machines from /run, and when accepting machine names
for operations, be more strict and always validate.

Note that these checks are strictly speaking unnecessary, since
enumeration happens only on the trusted /run...
2015-08-24 22:46:45 +02:00
Lennart Poettering 25300b5a1f util: make machine_name_is_valid() a macro and move it to hostname-util.h
As it turns out machine_name_is_valid() does the exact same thing as
hostname_is_valid() these days, as it just invoked that and checked the
name length was < 64. However, hostname_is_valid() checks the length
against HOST_NAME_MAX anyway (which is 64 on Linux), hence any
additional check is redundant.

We hence replace machine_name_is_valid() by a macro that simply maps it
to hostname_is_valid() but sets the allow_trailing_dot parameter to
false. We also move this this call to hostname-util.h, to the same place
as the hostname_is_valid() declaration.
2015-08-24 22:46:45 +02:00
Lennart Poettering 8ac43fee1a tree-wide: generate EBADF when we get invalid fds
This is a follow-up to #907, and makes the same change for all our other
public APIs.
2015-08-14 18:23:16 +02:00
Kay Sievers a095315b3c build-sys: split internal basic/ library from shared/
basic/      can be used by everything
            cannot use anything outside of basic/

libsystemd/ can use basic/
            cannot use shared/

shared/     can use libsystemd/
2015-06-11 10:52:46 +02:00
Lennart Poettering 329ac4bc54 sd-bus,sd-login: add api for querying the slice within the the user systemd instance of a process
units are organized in slice trees, not only for the system instance,
but also for user systemd instances, expose this properly.
2015-04-30 11:58:06 +02:00
Ronny Chevalier 6482f6269c shared: add formats-util.h 2015-04-10 23:54:48 +02:00
Lennart Poettering 527b7a421f util: rework cunescape(), improve error handling
Change cunescape() to return a normal error code, so that we can
distuingish OOM errors from parse errors.

This also adds a flags parameter to control whether "relaxed" or normal
parsing shall be done. If set no parse failures are generated, and the
only reason why cunescape() can fail is OOM.
2015-04-07 15:42:25 +02:00
Harald Hoyer a7f7d1bde4 fix gcc warnings about uninitialized variables
like:

src/shared/install.c: In function ‘unit_file_lookup_state’:
src/shared/install.c:1861:16: warning: ‘r’ may be used uninitialized in
this function [-Wmaybe-uninitialized]
         return r < 0 ? r : state;
                ^
src/shared/install.c:1796:13: note: ‘r’ was declared here
         int r;
             ^
2015-03-27 14:57:38 +01:00
Thomas Hindoe Paaboel Andersen 0a6f50c0af include <poll.h> instead of <sys/poll.h>
include-what-you-use automatically does this and it makes finding
unnecessary harder to spot. The only content of poll.h is a include
of sys/poll.h so should be harmless.
2015-02-12 20:47:38 +01:00
Lennart Poettering 63c372cb9d util: rework strappenda(), and rename it strjoina()
After all it is now much more like strjoin() than strappend(). At the
same time, add support for NULL sentinels, even if they are normally not
necessary.
2015-02-03 02:05:59 +01:00
David Herrmann c72d5456e2 login: add public sd_session_get_desktop() API
The desktop brand is stored as DESKTOP variable for sessions. It can be
set arbitrarily by the session owner and identifies the desktop
environment that is running on that session.
2014-09-22 14:27:02 +02:00