OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # Virtual Me2Me implementation. This script runs and manages the processes | 6 # Virtual Me2Me implementation. This script runs and manages the processes |
7 # required for a Virtual Me2Me desktop, which are: X server, X desktop | 7 # required for a Virtual Me2Me desktop, which are: X server, X desktop |
8 # session, and Host process. | 8 # session, and Host process. |
9 # This script is intended to run continuously as a background daemon | 9 # This script is intended to run continuously as a background daemon |
10 # process, running under an ordinary (non-root) user account. | 10 # process, running under an ordinary (non-root) user account. |
11 | 11 |
12 import atexit | 12 import atexit |
13 import errno | 13 import errno |
14 import fcntl | 14 import fcntl |
15 import getpass | 15 import getpass |
16 import grp | 16 import grp |
17 import hashlib | 17 import hashlib |
18 import json | 18 import json |
19 import logging | 19 import logging |
20 import optparse | 20 import optparse |
21 import os | 21 import os |
22 import pipes | 22 import pipes |
23 import psutil | 23 import psutil |
| 24 import platform |
24 import signal | 25 import signal |
25 import socket | 26 import socket |
26 import subprocess | 27 import subprocess |
27 import sys | 28 import sys |
28 import tempfile | 29 import tempfile |
29 import time | 30 import time |
30 import uuid | 31 import uuid |
31 | 32 |
32 LOG_FILE_ENV_VAR = "CHROME_REMOTE_DESKTOP_LOG_FILE" | 33 LOG_FILE_ENV_VAR = "CHROME_REMOTE_DESKTOP_LOG_FILE" |
33 | 34 |
(...skipping 11 matching lines...) Expand all Loading... |
45 | 46 |
46 IS_INSTALLED = (os.path.basename(sys.argv[0]) != 'me2me_virtual_host.py') | 47 IS_INSTALLED = (os.path.basename(sys.argv[0]) != 'me2me_virtual_host.py') |
47 | 48 |
48 if IS_INSTALLED: | 49 if IS_INSTALLED: |
49 HOST_BINARY_NAME = "chrome-remote-desktop-host" | 50 HOST_BINARY_NAME = "chrome-remote-desktop-host" |
50 else: | 51 else: |
51 HOST_BINARY_NAME = "remoting_me2me_host" | 52 HOST_BINARY_NAME = "remoting_me2me_host" |
52 | 53 |
53 CHROME_REMOTING_GROUP_NAME = "chrome-remote-desktop" | 54 CHROME_REMOTING_GROUP_NAME = "chrome-remote-desktop" |
54 | 55 |
55 CONFIG_DIR = os.path.expanduser("~/.config/chrome-remote-desktop") | |
56 HOME_DIR = os.environ["HOME"] | 56 HOME_DIR = os.environ["HOME"] |
| 57 CONFIG_DIR = os.path.join(HOME_DIR, ".config/chrome-remote-desktop") |
| 58 SESSION_FILE_PATH = os.path.join(HOME_DIR, ".chrome-remote-desktop-session") |
57 | 59 |
58 X_LOCK_FILE_TEMPLATE = "/tmp/.X%d-lock" | 60 X_LOCK_FILE_TEMPLATE = "/tmp/.X%d-lock" |
59 FIRST_X_DISPLAY_NUMBER = 20 | 61 FIRST_X_DISPLAY_NUMBER = 20 |
60 | 62 |
61 # Amount of time to wait between relaunching processes. | 63 # Amount of time to wait between relaunching processes. |
62 SHORT_BACKOFF_TIME = 5 | 64 SHORT_BACKOFF_TIME = 5 |
63 LONG_BACKOFF_TIME = 60 | 65 LONG_BACKOFF_TIME = 60 |
64 | 66 |
65 # How long a process must run in order not to be counted against the restart | 67 # How long a process must run in order not to be counted against the restart |
66 # thresholds. | 68 # thresholds. |
67 MINIMUM_PROCESS_LIFETIME = 60 | 69 MINIMUM_PROCESS_LIFETIME = 60 |
68 | 70 |
69 # Thresholds for switching from fast- to slow-restart and for giving up | 71 # Thresholds for switching from fast- to slow-restart and for giving up |
70 # trying to restart entirely. | 72 # trying to restart entirely. |
71 SHORT_BACKOFF_THRESHOLD = 5 | 73 SHORT_BACKOFF_THRESHOLD = 5 |
72 MAX_LAUNCH_FAILURES = SHORT_BACKOFF_THRESHOLD + 10 | 74 MAX_LAUNCH_FAILURES = SHORT_BACKOFF_THRESHOLD + 10 |
73 | 75 |
74 # Globals needed by the atexit cleanup() handler. | 76 # Globals needed by the atexit cleanup() handler. |
75 g_desktops = [] | 77 g_desktops = [] |
76 g_host_hash = hashlib.md5(socket.gethostname()).hexdigest() | 78 g_host_hash = hashlib.md5(socket.gethostname()).hexdigest() |
77 | 79 |
| 80 def is_supported_platform(): |
| 81 # Always assume that the system is supported if the config directory or |
| 82 # session file exist. |
| 83 if os.path.isdir(CONFIG_DIR) or os.path.isfile(SESSION_FILE_PATH): |
| 84 return True |
| 85 |
| 86 # The host has been tested only on Ubuntu. |
| 87 distribution = platform.linux_distribution() |
| 88 return (distribution[0]).lower() == 'ubuntu' |
| 89 |
78 class Config: | 90 class Config: |
79 def __init__(self, path): | 91 def __init__(self, path): |
80 self.path = path | 92 self.path = path |
81 self.data = {} | 93 self.data = {} |
82 self.changed = False | 94 self.changed = False |
83 | 95 |
84 def load(self): | 96 def load(self): |
85 """Loads the config from file. | 97 """Loads the config from file. |
86 | 98 |
87 Raises: | 99 Raises: |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 A string containing the command to run, or a list of strings containing | 507 A string containing the command to run, or a list of strings containing |
496 the executable program and its arguments, which is suitable for passing as | 508 the executable program and its arguments, which is suitable for passing as |
497 the first parameter of subprocess.Popen(). If a suitable session cannot | 509 the first parameter of subprocess.Popen(). If a suitable session cannot |
498 be found, returns None. | 510 be found, returns None. |
499 """ | 511 """ |
500 # If the session wrapper script (see below) is given a specific session as an | 512 # If the session wrapper script (see below) is given a specific session as an |
501 # argument (such as ubuntu-2d on Ubuntu 12.04), the wrapper will run that | 513 # argument (such as ubuntu-2d on Ubuntu 12.04), the wrapper will run that |
502 # session instead of looking for custom .xsession files in the home directory. | 514 # session instead of looking for custom .xsession files in the home directory. |
503 # So it's necessary to test for these files here. | 515 # So it's necessary to test for these files here. |
504 XSESSION_FILES = [ | 516 XSESSION_FILES = [ |
505 "~/.chrome-remote-desktop-session", | 517 SESSION_FILE_PATH, |
506 "~/.xsession", | 518 "~/.xsession", |
507 "~/.Xsession" ] | 519 "~/.Xsession" ] |
508 for startup_file in XSESSION_FILES: | 520 for startup_file in XSESSION_FILES: |
509 startup_file = os.path.expanduser(startup_file) | 521 startup_file = os.path.expanduser(startup_file) |
510 if os.path.exists(startup_file): | 522 if os.path.exists(startup_file): |
511 # Use the same logic that a Debian system typically uses with ~/.xsession | 523 # Use the same logic that a Debian system typically uses with ~/.xsession |
512 # (see /etc/X11/Xsession.d/50x11-common_determine-startup), to determine | 524 # (see /etc/X11/Xsession.d/50x11-common_determine-startup), to determine |
513 # exactly how to run this file. | 525 # exactly how to run this file. |
514 if os.access(startup_file, os.X_OK): | 526 if os.access(startup_file, os.X_OK): |
515 # "/bin/sh -c" is smart about how to execute the session script and | 527 # "/bin/sh -c" is smart about how to execute the session script and |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
895 "available (if the Xvfb server supports this).") | 907 "available (if the Xvfb server supports this).") |
896 parser.add_option("-f", "--foreground", dest="foreground", default=False, | 908 parser.add_option("-f", "--foreground", dest="foreground", default=False, |
897 action="store_true", | 909 action="store_true", |
898 help="Don't run as a background daemon.") | 910 help="Don't run as a background daemon.") |
899 parser.add_option("", "--start", dest="start", default=False, | 911 parser.add_option("", "--start", dest="start", default=False, |
900 action="store_true", | 912 action="store_true", |
901 help="Start the host.") | 913 help="Start the host.") |
902 parser.add_option("-k", "--stop", dest="stop", default=False, | 914 parser.add_option("-k", "--stop", dest="stop", default=False, |
903 action="store_true", | 915 action="store_true", |
904 help="Stop the daemon currently running.") | 916 help="Stop the daemon currently running.") |
| 917 parser.add_option("", "--get-status", dest="get_status", default=False, |
| 918 action="store_true", |
| 919 help="Prints host status") |
905 parser.add_option("", "--check-running", dest="check_running", default=False, | 920 parser.add_option("", "--check-running", dest="check_running", default=False, |
906 action="store_true", | 921 action="store_true", |
907 help="Return 0 if the daemon is running, or 1 otherwise.") | 922 help="Return 0 if the daemon is running, or 1 otherwise.") |
908 parser.add_option("", "--config", dest="config", action="store", | 923 parser.add_option("", "--config", dest="config", action="store", |
909 help="Use the specified configuration file.") | 924 help="Use the specified configuration file.") |
910 parser.add_option("", "--reload", dest="reload", default=False, | 925 parser.add_option("", "--reload", dest="reload", default=False, |
911 action="store_true", | 926 action="store_true", |
912 help="Signal currently running host to reload the config.") | 927 help="Signal currently running host to reload the config.") |
913 parser.add_option("", "--add-user", dest="add_user", default=False, | 928 parser.add_option("", "--add-user", dest="add_user", default=False, |
914 action="store_true", | 929 action="store_true", |
915 help="Add current user to the chrome-remote-desktop group.") | 930 help="Add current user to the chrome-remote-desktop group.") |
916 parser.add_option("", "--host-version", dest="host_version", default=False, | 931 parser.add_option("", "--host-version", dest="host_version", default=False, |
917 action="store_true", | 932 action="store_true", |
918 help="Prints version of the host.") | 933 help="Prints version of the host.") |
919 (options, args) = parser.parse_args() | 934 (options, args) = parser.parse_args() |
920 | 935 |
921 # Determine the filename of the host configuration and PID files. | 936 # Determine the filename of the host configuration and PID files. |
922 if not options.config: | 937 if not options.config: |
923 options.config = os.path.join(CONFIG_DIR, "host#%s.json" % g_host_hash) | 938 options.config = os.path.join(CONFIG_DIR, "host#%s.json" % g_host_hash) |
924 | 939 |
925 # Check for a modal command-line option (start, stop, etc.) | 940 # Check for a modal command-line option (start, stop, etc.) |
| 941 |
| 942 if options.get_status: |
| 943 pid = get_daemon_pid() |
| 944 if pid != 0: |
| 945 print "STARTED" |
| 946 elif is_supported_platform(): |
| 947 print "STOPPED" |
| 948 else: |
| 949 print "NOT_IMPLEMENTED" |
| 950 return 0 |
| 951 |
| 952 # TODO(sergeyu): Remove --check-running once NPAPI plugin and NM host are |
| 953 # updated to always use get-status flag instead. |
926 if options.check_running: | 954 if options.check_running: |
927 pid = get_daemon_pid() | 955 pid = get_daemon_pid() |
928 return 0 if pid != 0 else 1 | 956 return 0 if pid != 0 else 1 |
929 | 957 |
930 if options.stop: | 958 if options.stop: |
931 pid = get_daemon_pid() | 959 pid = get_daemon_pid() |
932 if pid == 0: | 960 if pid == 0: |
933 print "The daemon is not currently running" | 961 print "The daemon is not currently running" |
934 else: | 962 else: |
935 print "Killing process %s" % pid | 963 print "Killing process %s" % pid |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1161 else: | 1189 else: |
1162 logging.info("Host exited with status %s." % os.WEXITSTATUS(status)) | 1190 logging.info("Host exited with status %s." % os.WEXITSTATUS(status)) |
1163 elif os.WIFSIGNALED(status): | 1191 elif os.WIFSIGNALED(status): |
1164 logging.info("Host terminated by signal %s." % os.WTERMSIG(status)) | 1192 logging.info("Host terminated by signal %s." % os.WTERMSIG(status)) |
1165 | 1193 |
1166 | 1194 |
1167 if __name__ == "__main__": | 1195 if __name__ == "__main__": |
1168 logging.basicConfig(level=logging.DEBUG, | 1196 logging.basicConfig(level=logging.DEBUG, |
1169 format="%(asctime)s:%(levelname)s:%(message)s") | 1197 format="%(asctime)s:%(levelname)s:%(message)s") |
1170 sys.exit(main()) | 1198 sys.exit(main()) |
OLD | NEW |