Skip to content

Commit

Permalink
don't access stderr directly
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Jun 12, 2023
1 parent b5918aa commit 1dd5452
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 60 deletions.
8 changes: 3 additions & 5 deletions xpra/client/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
)
from xpra.util import (
flatten_dict, typedict, updict, parse_simple_dict, noerr, std,
repr_ellipsized, ellipsizer, nonl,
repr_ellipsized, ellipsizer, nonl, stderr_write,
envbool, envint, disconnect_is_an_error, dump_all_frames, csv, obsc,
SERVER_UPGRADE, CONNECTION_ERROR, AUTHENTICATION_FAILED,
)
Expand Down Expand Up @@ -257,8 +257,7 @@ def may_notify(self, nid, summary, body, *args, **kwargs):


def handle_deadly_signal(self, signum, _frame=None):
sys.stderr.write("\ngot deadly signal %s, exiting\n" % SIGNAMES.get(signum, signum))
sys.stderr.flush()
stderr_write("\ngot deadly signal %s, exiting\n" % SIGNAMES.get(signum, signum))
self.cleanup()
force_quit(128 + signum)

Expand All @@ -277,8 +276,7 @@ def install_signal_handlers(self):
def os_signal(signum, _frame=None):
if self.exit_code is None:
try:
sys.stderr.write("\n")
sys.stderr.flush()
stderr_write("\n")
log.info("client got signal %s", SIGNAMES.get(signum, signum))
except Exception:
pass
Expand Down
8 changes: 2 additions & 6 deletions xpra/client/gobject_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from xpra.util import (
u, net_utf8, nonl, sorted_nicely, print_nested_dict, envint, flatten_dict, typedict,
disconnect_is_an_error, ellipsizer, first_time, csv,
repr_ellipsized,
repr_ellipsized, stderr_write,
SERVER_EXIT, DONE,
)
from xpra.os_util import (
Expand All @@ -36,11 +36,7 @@


def errwrite(msg):
try:
sys.stderr.write(msg)
sys.stderr.flush()
except (OSError, AttributeError):
pass
stderr_write(msg)


class GObjectXpraClient(GObject.GObject, XpraClientBase):
Expand Down
6 changes: 2 additions & 4 deletions xpra/gtk_common/gobject_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

#legacy compatibility file

import sys
import signal

from xpra.util import dump_all_frames, dump_gc_frames
from xpra.util import dump_all_frames, dump_gc_frames, stderr_write
from xpra.os_util import SIGNAMES, POSIX, get_util_logger


Expand All @@ -24,8 +23,7 @@ def write_signal():
if commandtype is None:
return
try:
sys.stderr.write("\n")
sys.stderr.flush()
stderr_write("\n")
cstr = ""
if commandtype:
cstr = commandtype+" "
Expand Down
4 changes: 2 additions & 2 deletions xpra/net/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
restore_script_env, get_saved_env,
WIN32, OSX, POSIX,
)
from xpra.util import envint, envbool, envfloat, engs, noerr, csv
from xpra.util import envint, envbool, envfloat, engs, noerr, csv, stderr_write
from xpra.log import Logger, is_debug_enabled

#pylint: disable=import-outside-toplevel
Expand Down Expand Up @@ -537,7 +537,7 @@ def adddnscheckinfo(q):
f"Offending {keyname()} key in {host_keys_filename}",
f"ECDSA host key for {keyname()} has changed and you have requested strict checking.",
]
sys.stderr.write(os.linesep.join(qinfo))
stderr_write(os.linesep.join(qinfo))
transport.close()
raise InitExit(EXIT_SSH_KEY_FAILURE, "SSH Host key has changed")
if not confirm(qinfo):
Expand Down
4 changes: 2 additions & 2 deletions xpra/scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import shlex

from xpra.util import csv, sorted_nicely, remove_dupes
from xpra.util import csv, sorted_nicely, remove_dupes, stderr_write
from xpra.os_util import (
WIN32, OSX, POSIX,
osexpand, getuid, getgid, get_username_for_uid,
Expand All @@ -19,7 +19,7 @@
)

def warn(msg):
sys.stderr.write(msg+"\n")
stderr_write(msg+"\n")

def nodebug(*_args):
#can be overriden
Expand Down
17 changes: 8 additions & 9 deletions xpra/scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from xpra.util import (
csv, envbool, envint, nonl, pver, engs,
noerr, sorted_nicely, typedict,
stderr_write,
DEFAULT_PORTS,
)
from xpra.exit_codes import (
Expand Down Expand Up @@ -81,8 +82,7 @@ def nox():

def werr(*msg):
for x in msg:
noerr(sys.stderr.write, "%s\n" % (x,))
noerr(sys.stderr.flush)
stderr_write("%s\n" % (x,))

def add_process(*args, **kwargs):
from xpra.child_reaper import getChildReaper
Expand All @@ -96,10 +96,10 @@ def main(script_file, cmdline):
start_mem_watcher(ml)

if sys.flags.optimize>0: # pragma: no cover
sys.stderr.write("************************************************************\n")
sys.stderr.write(f"Warning: the python optimize flag is set to {sys.flags.optimize}\n")
sys.stderr.write(" xpra is very likely to crash\n")
sys.stderr.write("************************************************************\n")
stderr_write("************************************************************\n")
stderr_write(f"Warning: the python optimize flag is set to {sys.flags.optimize}\n")
stderr_write(" xpra is very likely to crash\n")
stderr_write("************************************************************\n")
time.sleep(5)

from xpra.platform import clean as platform_clean, command_error, command_info
Expand Down Expand Up @@ -2239,8 +2239,7 @@ def do_run_glcheck(opts, show=False) -> dict:
if is_debug_enabled("opengl"):
log("do_run_glcheck(..)", exc_info=True)
if use_tty():
noerr(sys.stderr.write, "error=%s\n" % nonl(e))
noerr(sys.stderr.flush)
stderr_write("error=%s\n" % nonl(e))
return {
"success" : False,
"message" : str(e).replace("\n", " "),
Expand Down Expand Up @@ -2656,7 +2655,7 @@ def run_proxy(error_cb, opts, script_file, cmdline, args, mode, defaults):
if display_name:
state = dotxpra.get_display_state(display_name)
if state!=DotXpra.DEAD:
sys.stderr.write(f"found existing display {display_name} : {state}\n")
stderr_write(f"found existing display {display_name} : {state}\n")
if state!=DotXpra.LIVE:
#strip defaults, only keep extra ones:
for x in ("start", "start-child",
Expand Down
21 changes: 4 additions & 17 deletions xpra/scripts/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

#pylint: disable=import-outside-toplevel

import sys
import shlex
import os.path
import optparse

from xpra.version_util import full_version_str
from xpra.util import envbool, csv, parse_simple_dict, DEFAULT_PORT, DEFAULT_PORTS
from xpra.util import envbool, csv, parse_simple_dict, DEFAULT_PORT, DEFAULT_PORTS, stderr_write
from xpra.os_util import WIN32, OSX, POSIX, get_user_uuid
from xpra.scripts.config import (
OPTION_TYPES, TRUE_OPTIONS,
Expand Down Expand Up @@ -57,30 +56,18 @@ def sound_option(v):
return bool_or(vl, "disabled", "on", "off", "disabled")


def _stderr_write(msg):
#use this function to print warnings
#we must write to stderr to prevent
#the output from interfering when running as proxy over ssh
#(which uses stdin / stdout as communication channel)
try:
sys.stderr.write(msg+"\n")
sys.stderr.flush()
return True
except OSError:
return False

def info(msg):
if not _stderr_write(msg) and POSIX:
if not stderr_write(msg) and POSIX:
import syslog
syslog.syslog(syslog.LOG_INFO, msg)

def warn(msg):
if not _stderr_write(msg) and POSIX:
if not stderr_write(msg) and POSIX:
import syslog
syslog.syslog(syslog.LOG_WARNING, msg)

def error(msg):
if not _stderr_write(msg) and POSIX:
if not stderr_write(msg) and POSIX:
import syslog
syslog.syslog(syslog.LOG_ERR, msg)

Expand Down
6 changes: 3 additions & 3 deletions xpra/server/auth/gss_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sys

from xpra.util import typedict
from xpra.util import typedict, stderr_write
from xpra.server.auth.sys_auth_base import SysAuthenticatorBase, log, parse_uid, parse_gid
from xpra.net.digest import get_salt, get_digests, gendigest

Expand Down Expand Up @@ -63,8 +63,8 @@ def main(argv):
from xpra.platform import program_context
with program_context("GSS-Auth", "GSS-Authentication"):
if len(argv)!=3:
sys.stderr.write("%s invalid arguments\n" % argv[0])
sys.stderr.write("usage: %s username token\n" % argv[0])
stderr_write("%s invalid arguments\n" % argv[0])
stderr_write("usage: %s username token\n" % argv[0])
return 1
username = argv[1]
token = argv[2]
Expand Down
6 changes: 3 additions & 3 deletions xpra/server/auth/kerberos_password_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from xpra.server.auth.sys_auth_base import SysAuthenticatorBase, xor, log, parse_uid, parse_gid
from xpra.net.digest import get_salt, get_digests, gendigest
from xpra.util import typedict
from xpra.util import typedict, stderr_write
from xpra.os_util import WIN32


Expand Down Expand Up @@ -65,8 +65,8 @@ def main(argv):
from xpra.platform import program_context
with program_context("Kerberos-Password-Auth", "Kerberos-Password-Authentication"):
if len(argv) not in (3,4,5):
sys.stderr.write("%s invalid arguments\n" % argv[0])
sys.stderr.write("usage: %s username password [service [realm]]\n" % argv[0])
stderr_write("%s invalid arguments\n" % argv[0])
stderr_write("usage: %s username password [service [realm]]\n" % argv[0])
return 1
username = argv[1]
password = argv[2]
Expand Down
6 changes: 3 additions & 3 deletions xpra/server/auth/kerberos_token_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from xpra.server.auth.sys_auth_base import SysAuthenticatorBase, xor, log, parse_uid, parse_gid
from xpra.net.digest import get_salt, get_digests, gendigest
from xpra.util import typedict
from xpra.util import typedict, stderr_write
from xpra.os_util import WIN32


Expand Down Expand Up @@ -79,8 +79,8 @@ def main(argv):
from xpra.platform import program_context
with program_context("Kerberos-Token-Auth", "Kerberos Token Authentication"):
if len(argv)!=3:
sys.stderr.write("%s invalid arguments\n" % argv[0])
sys.stderr.write("usage: %s username token\n" % argv[0])
stderr_write("%s invalid arguments\n" % argv[0])
stderr_write("usage: %s username token\n" % argv[0])
return 1
username = argv[1]
token = argv[2]
Expand Down
6 changes: 3 additions & 3 deletions xpra/server/auth/ldap3_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import sys

from xpra.util import obsc, typedict
from xpra.util import obsc, typedict, stderr_write
from xpra.server.auth.sys_auth_base import SysAuthenticatorBase, log, parse_uid, parse_gid
from xpra.log import enable_debug_for, is_debug_enabled
assert log #tests will disable logging from here
Expand Down Expand Up @@ -112,8 +112,8 @@ def main(argv):
enable_debug_for("auth")
argv.remove(x)
if len(argv) not in (3,4,5,6):
sys.stderr.write("%s invalid arguments\n" % argv[0])
sys.stderr.write("usage: %s username password [host] [port] [tls]\n" % argv[0])
stderr_write("%s invalid arguments\n" % argv[0])
stderr_write("usage: %s username password [host] [port] [tls]\n" % argv[0])
return 1
username = argv[1]
password = argv[2]
Expand Down
6 changes: 3 additions & 3 deletions xpra/server/auth/ldap_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
import socket

from xpra.util import envint, obsc, typedict
from xpra.util import envint, obsc, typedict, stderr_write
from xpra.os_util import bytestostr
from xpra.server.auth.sys_auth_base import SysAuthenticatorBase, log, parse_uid, parse_gid
from xpra.log import is_debug_enabled, enable_debug_for
Expand Down Expand Up @@ -133,8 +133,8 @@ def main(argv):
enable_debug_for("auth")
argv.remove(x)
if len(argv) not in (3,4,5,6,7):
sys.stderr.write("%s invalid arguments\n" % argv[0])
sys.stderr.write("usage: %s username password [host] [port] [tls] [username_format]\n" % argv[0])
stderr_write("%s invalid arguments\n" % argv[0])
stderr_write("usage: %s username password [host] [port] [tls] [username_format]\n" % argv[0])
return 1
username = argv[1]
password = argv[2]
Expand Down
11 changes: 11 additions & 0 deletions xpra/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ def noerr(fn, *args):
except Exception:
return None

def stderr_write(msg:str) -> bool:
stderr = sys.stderr
if stderr:
try:
noerr(stderr.write, msg+"\n")
noerr(stderr.flush)
return True
except (OSError, AttributeError):
pass
return False


def net_utf8(value):
"""
Expand Down

0 comments on commit 1dd5452

Please sign in to comment.