{
const auto child_pid = OS::throwing_fork();
if (child_pid == 0) {
+
+ /* Avoid interactive mode, just in case. */
+ OS::close_stdin();
+
const auto mkfs = "/usr/sbin/mkfs.ext4"sv;
/* Would ideally be std::format instead of this ugly stack,
#include <optional>
#include <vector>
+#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <sys/smack.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
+#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
return child_pid;
}
+void OS::close_stdin()
+{
+ /* Some programs go into "interactive" modes by looking
+ * at various traits of the input stream. Apparently we
+ * can sometimes avoid this by redirecting to /dev/null.
+ * This is largely for use with the fork-exec pattern. */
+ const int fd = open("/dev/null", O_RDONLY);
+ if (fd == -1) {
+ close(STDIN_FILENO);
+ return;
+ }
+
+ const int r = dup2(fd, STDIN_FILENO);
+ if (r == -1)
+ close(STDIN_FILENO);
+
+ close(fd);
+}
+
void OS::throw_if_child_failed(int child_pid, std::string_view message_on_fail)
{
int status;