Wait for CRD daemon process to be killed before returning.

This should mean that "/etc/init.d/chrome-remote-desktop restart" will
stop the service and won't try to start it again until the daemon is
killed.

A timeout is used to make sure that system shutdown is not paused
indefinitely if the daemon is unkillable.

Note that, since the daemon is not a child process, os.waitpid() cannot
be used for this. The psutil Python module is used instead. Some other
parts of the script have been updated as well to use psutil methods.
This is slightly safer, as psutil has some PID-reuse detection built in.

BUG=334437

Review URL: https://codereview.chromium.org/335303002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278267 0039d316-1c4b-4281-b951-d872f2087c98
parent c6e22aa3
......@@ -500,14 +500,15 @@ class Desktop:
self.host_proc.stdin.close()
def get_daemon_pid():
def get_daemon_proc():
"""Checks if there is already an instance of this script running, and returns
its PID.
a psutil.Process instance for it.
Returns:
The process ID of the existing daemon process, or 0 if the daemon is not
running.
A Process instance for the existing daemon process, or None if the daemon
is not running.
"""
uid = os.getuid()
this_pid = os.getpid()
......@@ -535,11 +536,11 @@ def get_daemon_pid():
if len(cmdline) < 2:
continue
if cmdline[0] == sys.executable and cmdline[1] == sys.argv[0]:
return process.pid
return process
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
return 0
return None
def choose_x_session():
......@@ -977,8 +978,8 @@ Web Store: https://chrome.google.com/remotedesktop"""
# Check for a modal command-line option (start, stop, etc.)
if options.get_status:
pid = get_daemon_pid()
if pid != 0:
proc = get_daemon_proc()
if proc is not None:
print "STARTED"
elif is_supported_platform():
print "STOPPED"
......@@ -989,23 +990,28 @@ Web Store: https://chrome.google.com/remotedesktop"""
# TODO(sergeyu): Remove --check-running once NPAPI plugin and NM host are
# updated to always use get-status flag instead.
if options.check_running:
pid = get_daemon_pid()
return 0 if pid != 0 else 1
proc = get_daemon_proc()
return 1 if proc is None else 0
if options.stop:
pid = get_daemon_pid()
if pid == 0:
proc = get_daemon_proc()
if proc is None:
print "The daemon is not currently running"
else:
print "Killing process %s" % pid
os.kill(pid, signal.SIGTERM)
print "Killing process %s" % proc.pid
proc.terminate()
try:
proc.wait(timeout=30)
except psutil.TimeoutExpired:
print "Timed out trying to kill daemon process"
return 1
return 0
if options.reload:
pid = get_daemon_pid()
if pid == 0:
proc = get_daemon_proc()
if proc is None:
return 1
os.kill(pid, signal.SIGHUP)
proc.send_signal(signal.SIGHUP)
return 0
if options.add_user:
......@@ -1097,8 +1103,8 @@ Web Store: https://chrome.google.com/remotedesktop"""
# Determine whether a desktop is already active for the specified host
# host configuration.
pid = get_daemon_pid()
if pid != 0:
proc = get_daemon_proc()
if proc is not None:
# Debian policy requires that services should "start" cleanly and return 0
# if they are already running.
print "Service already running."
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment