chiark / gitweb /
fd-util: add new call rearrange_stdio()
authorLennart Poettering <lennart@poettering.net>
Wed, 28 Feb 2018 09:00:26 +0000 (10:00 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:59:13 +0000 (07:59 +0200)
commit2c0aa1402b7bbccc25331c1f82103b3490d88026
tree96a94235738e8c29289dd61f0c618e8406aeddc0
parent684be08b883424bd6e352e588c15345c78d592a7
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