Commit Graph

39 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
Zbigniew Jędrzejewski-Szmek e0b6d3cabe
Merge pull request #7816 from poettering/chase-pid
Make MAINPID= and PIDFile= handling more restrictive (and other stuff)
2018-01-15 14:14:34 +04:00
Lennart Poettering 9e1d021ee3 sd-dameon: also sent ucred when our UID differs from EUID
Let's be explicit, and always send the messages from our UID and never
our EUID. Previously this behaviour was conditionalized only on whether
the PID was specified, which made this non-obvious.
2018-01-11 15:12:16 +01:00
Lennart Poettering dccca82b1a log: minimize includes in log.h
log.h really should only include the bare minimum of other headers, as
it is really pulled into pretty much everything else and already in
itself one of the most basic pieces of code we have.

Let's hence drop inclusion of:

1. sd-id128.h because it's entirely unneeded in current log.h
2. errno.h, dito.
3. sys/signalfd.h which we can replace by a simple struct forward
   declaration
4. process-util.h which was needed for getpid_cached() which we now hide
   in a funciton log_emergency_level() instead, which nicely abstracts
   the details away.
5. sys/socket.h which was needed for struct iovec, but a simple struct
   forward declaration suffices for that too.

Ultimately this actually makes our source tree larger (since users of
the functionality above must now include it themselves, log.h won't do
that for them), but I think it helps to untangle our web of includes a
tiny bit.

(Background: I'd like to isolate the generic bits of src/basic/ enough
so that we can do a git submodule import into casync for it)
2018-01-11 14:44:31 +01:00
Lennart Poettering dfde7e8c5b sd-daemon: use sockaddr_port() helper 2017-12-26 13:36:07 +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
Yu Watanabe 945c2931bb libsystemd: use IN_SET macro 2017-09-28 17:37:59 +09:00
Lennart Poettering df0ff12775 tree-wide: make use of getpid_cached() wherever we can
This moves pretty much all uses of getpid() over to getpid_raw(). I
didn't specifically check whether the optimization is worth it for each
replacement, but in order to keep things simple and systematic I
switched over everything at once.
2017-07-20 20:27:24 +02:00
Zbigniew Jędrzejewski-Szmek f6f372d2f4 Add sd_is_socket_sockaddr (#4885)
Fixes #1188.
2016-12-14 11:51:26 +01:00
Lennart Poettering 8e38570ebe tree-wide: htonl() is weird, let's use htobe32() instead (#3538)
Super-important change, yeah!
2016-06-15 01:26:01 +02:00
Lennart Poettering fc2fffe770 tree-wide: introduce new SOCKADDR_UN_LEN() macro, and use it everywhere
The macro determines the right length of a AF_UNIX "struct sockaddr_un" to pass to
connect() or bind(). It automatically figures out if the socket refers to an
abstract namespace socket, or a socket in the file system, and properly handles
the full length of the path field.

This macro is not only safer, but also simpler to use, than the usual
offsetof() + strlen() logic.
2016-05-05 22:24:36 +02:00
Torstein Husebø 96d4901149 treewide: fix typos and then/that use 2016-02-24 11:56:11 +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
Lennart Poettering b3d2548baf Merge pull request #1768 from vcaputo/sd-daemon-listen-fds-overflow-bis
sd-daemon: fix potential LISTEN_FDS overflow in sd_listen_fds()
2015-11-04 11:55:40 +01:00
Vito Caputo 046c93f8db sd-daemon: fix potential LISTEN_FDS overflow in sd_listen_fds() 2015-11-03 11:13:26 -08:00
Lennart Poettering caffe412c7 sd-daemon: explicitly filter out -1 when parsing watchdog timeout
We already filter out 0, and as -1 is usually special (meaning infinity,
as in USEC_INFINITY) we should better not accept it either. Better safe
than sorry...
2015-11-03 17:45:12 +01:00
Lennart Poettering 638b56cd3c sd-daemon: verify NOTIFY_SOCKET path length
Better generate a real error then simply connect to the wrong socket.
2015-11-01 21:49:19 +01:00
Lennart Poettering a47806fafa sd-daemon: increase sd_notify() socket buffer size
Let's make sure we don't start blocking on sd_notify() earlier than
necessary, let's bump the socket buffer sizes to 8M.

We already do something similar for our logging socket buffers, hence
apply a similar bump here.
2015-10-31 19:09:20 +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 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 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
Daniel Mack 40f4423892 sd-daemon: wipe out memory before using CMSG_NXTHDR()
CMSG_NXTHDR() checks for cmsg->cmsg_len *after* it increased the pointer.
While this makes sense for parsing received messages, that's a pitfall
for code crafting messages with this macro.

Wipe out the allocated memory to fix this.
2015-10-12 15:15:25 +02:00
Lennart Poettering c463a6f1c4 sd-daemon: simply code simplification
No change in behaviour, just make the code more obvious.
2015-10-09 17:20:32 +02:00
Lennart Poettering 8dd4c05b54 core: add support for naming file descriptors passed using socket activation
This adds support for naming file descriptors passed using socket
activation. The names are passed in a new $LISTEN_FDNAMES= environment
variable, that matches the existign $LISTEN_FDS= one and contains a
colon-separated list of names.

This also adds support for naming fds submitted to the per-service fd
store using FDNAME= in the sd_notify() message.

This also adds a new FileDescriptorName= setting for socket unit files
to set the name for fds created by socket units.

This also adds a new call sd_listen_fds_with_names(), that is similar to
sd_listen_fds(), but also returns the names of the fds.

systemd-activate gained the new --fdname= switch to specify a name for
testing socket activation.

This is based on #1247 by Maciej Wereski.

Fixes #1247.
2015-10-06 11:52:48 +02:00
Zbigniew Jędrzejewski-Szmek 0260d1d542 sd-daemon: fix sd_is_mq for non-mq fds
mq_getattr returns -1/EBADF for file descriptors which are not mq.
But we should return 0 in this case.

We first check that fd is a valid fd, so we can assume that if
mq_getattr returns EBADF, it is simply a non-mq fd. There is a slight
race, but there doesn't seem to be a nice way to fix it.
2015-09-30 14:23:13 -04:00
Lennart Poettering 31021ba085 sd-daemon: Simplify sd_booted() check
We can just use access() to check whether /run/system/system/ is a
directory, no need to involve stat(). The trick is to suffix the path
name with a dash.
2015-09-29 21:55:51 +02:00
Maciej Wereski a5bd3c32ab sd_pid_notify_with_fds: fix computing msg_controllen
CMSG_SPACE(0) may return value other than 0. This caused sendmsg to fail
with EINVAL, when have_pid or n_fds was 0.
2015-09-09 14:28:43 +02:00
Zbigniew Jędrzejewski-Szmek e680380168 sd-daemon: return EBADF for invalid fd numbers
This matches what open(2) and other system functions do.
2015-08-07 11:30:18 -04:00
Zbigniew Jędrzejewski-Szmek 64144440a5 sd-daemon: simplify sd_pid_notify_with_fds
Coverity was complaining that CMSG_NXTHDR is used without
checking the return value. In this case it cannot fail, but
it is a good excuse to simplify the function a bit.

CID #1261726.
2015-03-13 23:42:17 -04:00
Lennart Poettering d4a144fadf sd-daemon: replace VLA with alloca(), to make llvm happy
https://bugs.freedesktop.org/show_bug.cgi?id=89379
2015-03-02 20:55:38 +01:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00
Lennart Poettering a354329f72 core: add new logic for services to store file descriptors in PID 1
With this change it is possible to send file descriptors to PID 1, via
sd_pid_notify_with_fds() which PID 1 will store individually for each
service, and pass via the usual fd passing logic on next invocation.
This is useful for enable daemon reload schemes where daemons serialize
their state to /run, push their fds into PID 1 and terminate, restoring
their state on next start from the data in /run and passed in from PID
1.

The fds are kept by PID 1 as long as no POLLHUP or POLLERR is seen on
them, and the service they belong to are either not dead or failed, or
have a job queued.
2015-01-06 03:16:39 +01:00
Zbigniew Jędrzejewski-Szmek a9becdd65b sd-daemon,man: ignore missing $WATCHDOG_PID
Systemd 209 started setting $WATCHDOG_PID, and sd-daemon watch was
modified to check for this variable. This means that
sd_watchdog_enabled() stopped working with previous versions of
systemd. But sd-event is a public library and API and we must keep it
working even when a program compiled with a newer version of the
libary is used on a system running an older version of the manager.

getenv() and unsetenv() are fairly expensive calls, so optimize
sd_watchdog_enabled() by not calling them when unnecessary.

man: centralize the description of $WATCHDOG_PID and $WATCHDOG_USEC in
the sd_watchdog_enabled manpage. It is better not to repeat the same
stuff in two places.
2014-10-23 00:17:18 -04:00
Lennart Poettering 3cb4674019 libsystemd: make use of our common sockaddr_union everywhere 2014-07-10 21:16:40 +02:00
Lennart Poettering be8f4e9e8e sd-daemon: introduce sd_pid_notify() and sd_pid_notifyf()
sd_pid_notify() operates like sd_notify(), however operates on a
different PID (for example the parent PID of a process).

Make use of this in systemd-notify, so that message are sent from the
PID specified with --pid= rather than the usually shortlived PID of
systemd-notify itself.

This should increase the likelyhood that PID 1 can identify the cgroup
that the notification message was sent from properly.
2014-06-05 17:05:18 +02:00
Lennart Poettering 56dc9aec21 sd-daemon: fix incorrect variable access 2014-03-24 23:54:14 +01:00
Kay Sievers 0ebee88184 build-sys: merge libsystemd-daemon into libsystemd 2014-02-19 01:09:19 +01:00
Renamed from src/libsystemd-daemon/sd-daemon.c (Browse further)