]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/__init__.py
tools: Remove umask 0
[waydroid.git] / tools / __init__.py
1 # Copyright 2021 Oliver Smith
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 # PYTHON_ARGCOMPLETE_OK
4 import sys
5 import logging
6 import os
7 import traceback
8 import dbus.mainloop.glib
9 import dbus
10 import dbus.exceptions
11
12 from . import actions
13 from . import config
14 from . import helpers
15 from .helpers import logging as tools_logging
16
17
18 def main():
19 def actionNeedRoot(action):
20 if os.geteuid() != 0:
21 raise RuntimeError(
22 "Action \"{}\" needs root access".format(action))
23
24 # Wrap everything to display nice error messages
25 args = None
26 try:
27 # Parse arguments, set up logging
28 args = helpers.arguments()
29 args.cache = {}
30 args.work = config.defaults["work"]
31 args.config = args.work + "/waydroid.cfg"
32 args.log = args.work + "/waydroid.log"
33 args.sudo_timer = True
34 args.timeout = 1800
35
36 if os.geteuid() == 0:
37 if not os.path.exists(args.work):
38 os.mkdir(args.work)
39 elif not os.path.exists(args.log):
40 args.log = "/tmp/tools.log"
41
42 tools_logging.init(args)
43
44 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
45 dbus.mainloop.glib.threads_init()
46 dbus_name_scope = None
47
48 if not actions.initializer.is_initialized(args) and \
49 args.action and args.action not in ("init", "first-launch", "log"):
50 if args.wait_for_init:
51 try:
52 dbus_name_scope = dbus.service.BusName("id.waydro.Container", dbus.SystemBus(), do_not_queue=True)
53 actions.wait_for_init(args)
54 except dbus.exceptions.NameExistsException:
55 print('ERROR: WayDroid service is already awaiting initialization')
56 return 1
57 else:
58 print('ERROR: WayDroid is not initialized, run "waydroid init"')
59 return 0
60
61 # Initialize or require config
62 if args.action == "init":
63 actionNeedRoot(args.action)
64 actions.init(args)
65 elif args.action == "upgrade":
66 actionNeedRoot(args.action)
67 actions.upgrade(args)
68 elif args.action == "session":
69 if args.subaction == "start":
70 actions.session_manager.start(args)
71 elif args.subaction == "stop":
72 actions.session_manager.stop(args)
73 else:
74 logging.info(
75 "Run waydroid {} -h for usage information.".format(args.action))
76 elif args.action == "container":
77 actionNeedRoot(args.action)
78 if args.subaction == "start":
79 if dbus_name_scope is None:
80 try:
81 dbus_name_scope = dbus.service.BusName("id.waydro.Container", dbus.SystemBus(), do_not_queue=True)
82 except dbus.exceptions.NameExistsException:
83 print('ERROR: WayDroid container service is already running')
84 return 1
85 actions.container_manager.start(args)
86 elif args.subaction == "stop":
87 actions.container_manager.stop(args)
88 elif args.subaction == "restart":
89 actions.container_manager.restart(args)
90 elif args.subaction == "freeze":
91 actions.container_manager.freeze(args)
92 elif args.subaction == "unfreeze":
93 actions.container_manager.unfreeze(args)
94 else:
95 logging.info(
96 "Run waydroid {} -h for usage information.".format(args.action))
97 elif args.action == "app":
98 if args.subaction == "install":
99 actions.app_manager.install(args)
100 elif args.subaction == "remove":
101 actions.app_manager.remove(args)
102 elif args.subaction == "launch":
103 actions.app_manager.launch(args)
104 elif args.subaction == "intent":
105 actions.app_manager.intent(args)
106 elif args.subaction == "list":
107 actions.app_manager.list(args)
108 else:
109 logging.info(
110 "Run waydroid {} -h for usage information.".format(args.action))
111 elif args.action == "prop":
112 if args.subaction == "get":
113 ret = helpers.props.get(args, args.key)
114 if ret:
115 print(ret)
116 elif args.subaction == "set":
117 helpers.props.set(args, args.key, args.value)
118 else:
119 logging.info(
120 "Run waydroid {} -h for usage information.".format(args.action))
121 elif args.action == "shell":
122 actionNeedRoot(args.action)
123 helpers.lxc.shell(args)
124 elif args.action == "logcat":
125 actionNeedRoot(args.action)
126 helpers.lxc.logcat(args)
127 elif args.action == "show-full-ui":
128 actions.app_manager.showFullUI(args)
129 elif args.action == "first-launch":
130 actions.remote_init_client(args)
131 if actions.initializer.is_initialized(args):
132 actions.app_manager.showFullUI(args)
133 elif args.action == "status":
134 actions.status.print_status(args)
135 elif args.action == "log":
136 if args.clear_log:
137 helpers.run.user(args, ["truncate", "-s", "0", args.log])
138 try:
139 helpers.run.user(
140 args, ["tail", "-n", args.lines, "-F", args.log], output="tui")
141 except KeyboardInterrupt:
142 pass
143 else:
144 logging.info("Run waydroid -h for usage information.")
145
146 #logging.info("Done")
147
148 except Exception as e:
149 # Dump log to stdout when args (and therefore logging) init failed
150 if not args:
151 logging.getLogger().setLevel(logging.DEBUG)
152
153 logging.info("ERROR: " + str(e))
154 logging.info("See also: <https://github.com/waydroid>")
155 logging.debug(traceback.format_exc())
156
157 # Hints about the log file (print to stdout only)
158 log_hint = "Run 'waydroid log' for details."
159 if not args or not os.path.exists(args.log):
160 log_hint += (" Alternatively you can use '--details-to-stdout' to"
161 " get more output, e.g. 'waydroid"
162 " --details-to-stdout init'.")
163 print(log_hint)
164 return 1
165
166
167 if __name__ == "__main__":
168 sys.exit(main())