bug fixes + concurrent X11 threads

This commit updates the author.
It also contains the following changes:
- initializes Xlib support for concurrent threads
- fix minor device number calculation for large values
- fix crash if there are multiple pty slave folders

fix minor device number calculation for large values

Unfortunately the extraction of the minor device number from the tty_nr
was wrong.
The man page of procfs states that 'The minor device number is contained
[in the tty_nr] in the combination of bits 31 to 20 and 7 to 0' but it did not state
that you need to shift the bits together.
So I just missed that step.
This commit adds the correct placement of the bits to the calculation of the minor device number.

fix crash if there are multiple pty slave folders

If a system uses multiple pty slave folders it is possible that while
searching for the pty file a FileNotFoundError is raised which crashs
the program. In this case the correct behavior would be to look for the
pty file in the next folder. So this commit ignores the
FileNotFoundError for the call of the stat function.
master
seebye 1 year ago
parent 3780b8155e
commit 0745998c0d

@ -1,6 +1,6 @@
#!/usr/bin/env bash
# This is just an example how ueberzug can be used with fzf.
# Copyright (C) 2019 Nico Bäurer
# Copyright (C) 2019 seebye
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by

@ -1,6 +1,6 @@
#!/usr/bin/env bash
# This is just an example how ueberzug can be used.
# Copyright (C) 2019 Nico Bäurer
# Copyright (C) 2019 seebye
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by

@ -1,14 +1,36 @@
#include "python.h"
#include <X11/Xlib.h>
#include "util.h"
#include "display.h"
#include "window.h"
#include "Xshm.h"
static PyObject *
X_init_threads(PyObject *self) {
if (XInitThreads() == 0) {
raise(OSError, "Xlib concurrent threads initialization failed.");
}
Py_RETURN_NONE;
}
static PyMethodDef X_methods[] = {
{"init_threads", (PyCFunction)X_init_threads,
METH_NOARGS,
"Initializes Xlib support for concurrent threads."},
{NULL} /* Sentinel */
};
static PyModuleDef module = {
PyModuleDef_HEAD_INIT,
.m_name = "ueberzug.X",
.m_doc = "Modul which implements the interaction with the Xshm extension.",
.m_size = -1,
.m_methods = X_methods,
};

@ -4,4 +4,4 @@ __description__ ='ueberzug is a command line util which allows to display images
__url_repository__ = 'https://github.com/seebye/ueberzug'
__url_bug_reports__ = 'https://github.com/seebye/ueberzug/issues'
__url_project__ = __url_repository__
__author__ = 'Nico Bäurer'
__author__ = 'seebye'

@ -27,7 +27,7 @@ Layer options:
License:
ueberzug Copyright (C) 2018 Nico Bäurer
ueberzug Copyright (C) 2018 seebye
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.

@ -212,6 +212,7 @@ def main(options):
error_handler = error_processor_factory(parser_object)
view = View()
tools = Tools(image_loader, parser_object, error_handler)
X.init_threads()
display = X.Display()
window_factory = ui.CanvasWindow.Factory(display, view)
window_infos = xutil.get_parent_window_infos(display)

@ -4,7 +4,6 @@ import functools
MAX_PROCESS_NAME_LENGTH = 15
MINOR_DEVICE_NUMBER_MASK = 0b1111_1111_1111_0000_0000_0000_1111_1111
@functools.wraps(os.getpid)
@ -103,6 +102,29 @@ def get_parent_pid(pid: int):
return int(process_info['ppid'])
def calculate_minor_device_number(tty_nr: int):
"""Calculates the minor device number contained
in a tty_nr of a stat file of the procfs.
Args:
tty_nr (int):
a tty_nr of a stat file of the procfs
Returns:
int: the minor device number contained in the tty_nr
"""
TTY_NR_BITS = 32
FIRST_BITS = 8
SHIFTED_BITS = 12
FIRST_BITS_MASK = 0xFF
SHIFTED_BITS_MASK = 0xFFF00000
minor_device_number = (
(tty_nr & FIRST_BITS_MASK) +
((tty_nr & SHIFTED_BITS_MASK)
>> (TTY_NR_BITS - SHIFTED_BITS - FIRST_BITS)))
return minor_device_number
def get_pty_slave(pid: int):
"""Determines control device file
of the pty slave of the process with the given pid.
@ -123,12 +145,15 @@ def get_pty_slave(pid: int):
pty_slave_folders = get_pty_slave_folders()
process_info = get_info(pid)
tty_nr = int(process_info['tty_nr'])
minor_device_number = tty_nr & MINOR_DEVICE_NUMBER_MASK
minor_device_number = calculate_minor_device_number(tty_nr)
for folder in pty_slave_folders:
device_path = f'{folder}/{minor_device_number}'
if tty_nr == os.stat(device_path).st_rdev:
return device_path
try:
if tty_nr == os.stat(device_path).st_rdev:
return device_path
except FileNotFoundError:
pass
return None

Loading…
Cancel
Save