]> git.ipfire.org Git - thirdparty/systemd.git/commit - src/basic/fd-util.c
fd-util: add new call rearrange_stdio()
authorLennart Poettering <lennart@poettering.net>
Wed, 28 Feb 2018 09:00:26 +0000 (10:00 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 2 Mar 2018 10:42:10 +0000 (11:42 +0100)
commitaa11e28bf2866cc8634b5706dfcd07f0e5579e17
treeb0e865d429c2c449c68ff41521a98a8c0e87b46e
parent9711b1adc737457aa7c44c45b8103e428b6bdd54
fd-util: add new call rearrange_stdio()

Quite often we need to set up a number of fds as stdin/stdout/stderr of
a process we are about to start. Add a generic implementation for a
routine doing that that takes care to do so properly:

1. Can handle the case where stdin/stdout/stderr where previously
   closed, and the fds to set as stdin/stdout/stderr hence likely in the
   0..2 range.  handling this properly is nasty, since we need to first
   move the fds out of this range in order to later move them back in, to
   make things fully robust.

2. Can optionally open /dev/null in case for one or more of the fds, in
   a smart way, sharing the open file if possible between multiple of
   the fds.

3. Guarantees that O_CLOEXEC is not set on the three fds, even if the fds
   already were in the 0..2 range and hence possibly weren't moved.
src/basic/fd-util.c
src/basic/fd-util.h
src/test/test-fd-util.c