From 0bc89a7290453f4a496358e90d807c3616518273 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 19 Dec 2011 17:01:07 -0500 Subject: make stdin read from /dev/null for all cloud-init programs (LP: #903993) the cloud-init programs are never intended to run interactively. Some programs were being run via subprocess, and would notice that their input was attached to a terminal (/dev/console). As a result, they they would try to prompt the user for input (apt-add-repository) This change simply re-opens standard input as /dev/null so any subprocesses will not end up blocking on input. --- cloudinit/util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'cloudinit') diff --git a/cloudinit/util.py b/cloudinit/util.py index dc461f6c..de95ec79 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -26,6 +26,7 @@ import urllib import logging import re import socket +import sys import time import traceback import urlparse @@ -523,3 +524,16 @@ def search_for_mirror(candidates): raise return None + +def close_stdin(): + """ + reopen stdin as /dev/null so even subprocesses or other os level things get + /dev/null as input. + + if _CLOUD_INIT_SAVE_STDIN is set in environment to a non empty or '0' value + then input will not be closed (only useful potentially for debugging). + """ + if os.environ.get("_CLOUD_INIT_SAVE_STDIN") in ("", "0", False): + return + with open(os.devnull) as fp: + os.dup2(fp.fileno(), sys.stdin.fileno()) -- cgit v1.2.3