Commit graph

74 commits

Author SHA1 Message Date
Lennart Poettering a8c9b7a0fc sd-login: let's also make sd-login understand ".host"
if sd-bus and machined grok it, then sd-login should grok it too.
2018-07-25 22:48:11 +02:00
Yu Watanabe 8f5dd8c094 tree-wide: drop empty comments 2018-06-29 11:00:30 +09:00
Lennart Poettering 0c69794138 tree-wide: remove Lennart's copyright lines
These lines are generally out-of-date, incomplete and unnecessary. With
SPDX and git repository much more accurate and fine grained information
about licensing and authorship is available, hence let's drop the
per-file copyright notice. Of course, removing copyright lines of others
is problematic, hence this commit only removes my own lines and leaves
all others untouched. It might be nicer if sooner or later those could
go away too, making git the only and accurate source of authorship
information.
2018-06-14 10:20:20 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Lennart Poettering 1a5a177eaf fileio: accept FILE* in addition to path in parse_env_file()
Most our other parsing functions do this, let's do this here too,
internally we accept that anyway. Also, the closely related
load_env_file() and load_env_file_pairs() also do this, so let's be
systematic.
2018-05-24 17:01:57 +02:00
Lennart Poettering da6053d0a7 tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.

Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.

So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.

This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:

1. strv_length()' return type becomes size_t

2. the unit file changes array size becomes size_t

3. DNS answer and query array sizes become size_t

Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:29:06 +02:00
Zbigniew Jędrzejewski-Szmek 11a1589223 tree-wide: drop license boilerplate
Files which are installed as-is (any .service and other unit files, .conf
files, .policy files, etc), are left as is. My assumption is that SPDX
identifiers are not yet that well known, so it's better to retain the
extended header to avoid any doubt.

I also kept any copyright lines. We can probably remove them, but it'd nice to
obtain explicit acks from all involved authors before doing that.
2018-04-06 18:58:55 +02:00
Yu Watanabe 1cc6c93a95 tree-wide: use TAKE_PTR() and TAKE_FD() macros 2018-04-05 14:26:26 +09:00
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
Alan Jenkins e6f44233c6 sd-login: test - fix failure when run from non-graphical seat
Observed when running from the console of a systemd nspawn container
(see failure below).

The value of r was tested, when r was last set by
sd_session_can_graphical().  This did not correspond to the value expected.

Fix the code, so we compare relevant values now.  Hopefully :).


Test failure
------------

/* Information printed is from the live system */
sd_pid_get_unit(0, …) → "session-13.scope"
sd_pid_get_user_unit(0, …) → "n/a"
sd_pid_get_slice(0, …) → "user-1000.slice"
sd_pid_get_session(0, …) → "13"
sd_pid_get_owner_uid(0, …) → 1000
sd_pid_get_cgroup(0, …) → "/user.slice/user-1000.slice/session-13.scope"
sd_uid_get_display(1000, …) → "13"
sd_uid_get_sessions(1000, …) → [2] "15 13"
sd_uid_get_seats(1000, …) → [1] "seat0"
sd_session_is_active("13") → yes
sd_session_is_remote("13") → no
sd_session_get_state("13") → "active"
sd_session_get_uid("13") → 1000
sd_session_get_type("13") → "tty"
sd_session_get_class("13") → "user"
sd_session_get_display("13") → "n/a"
sd_session_get_remote_user("13") → "n/a"
sd_session_get_remote_host("13") → "n/a"
sd_session_get_seat("13") → "seat0"
sd_session_can_multi_seat("seat0") → no
sd_session_can_tty("seat0") → no
sd_session_can_graphical("seat0") → no
sd_uid_get_state(1000, …) → active
Assertion '!!k == !!r' failed at ../src/libsystemd/sd-login/test-login.c:191, function test_login(). Aborting.
2017-08-08 16:55:15 +01:00
Alan Jenkins 5947643cc2 sd-login: test - fix function name in output 2017-08-08 16:55:15 +01:00
Zbigniew Jędrzejewski-Szmek 72cdb3e783 build-sys: drop automake support
v2:
- also mention m4
2017-07-18 10:04:44 -04: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
Tom Gundersen 0bf7d7cc88 logn: tests - don't compare signed with unsigned 2017-05-25 17:37:50 +02: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 ce737f46cd test-login: make the test non-manual
test-login.c is largely rewritten to use _cleanup_ and give more meaningful
messages (function names are used instead of creative terms like "active
session", so that when something unexpected is returned, it's much easier to
see what function is responsible).

The monitoring part is only activated if '-m' is passed on the command line.

It runs against the information from /run/systemd/ in the live system, but that
should be OK: logind/sd-login interface is supposed to be stable and both
backwards and forwards compatible.

If not running in a login session, some tests are skipped.

Those two changes together mean that it's possible to run test-login in the
test suite.

Tests for sd_pid_get_{unit,user_unit,slice} are added.
2017-05-18 22:11:58 -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