Change CLI options handling

This commit is contained in:
Alexey Avramov 2020-03-21 03:12:16 +09:00
parent c70b8242d0
commit 3e1acc3590
6 changed files with 103 additions and 79 deletions

View File

@ -17,14 +17,15 @@ script:
- nohang -h - nohang -h
- nohang -v - nohang -v
- nohang -cc /etc/nohang/nohang.conf - nohang --check --config /etc/nohang/nohang.conf
- nohang -cc /etc/nohang/nohang-desktop.conf - nohang --check --config /etc/nohang/nohang-desktop.conf
- nohang -cc nohang/test.conf - nohang --check --config nohang/test.conf
- sudo nohang -p - sudo nohang --config /etc/nohang/nohang.conf --tasks
- sudo nohang --config /etc/nohang/nohang-desktop.conf --tasks
- /bin/sleep 60 & - /bin/sleep 60 &
- sudo bash -c "nohang -c nohang/test.conf & tail /dev/zero & sleep 30 && pkill python3" - sudo bash -c "nohang -c nohang/test.conf --monitor & tail /dev/zero & sleep 30 && pkill python3"
- sudo cat /var/log/nohang/nohang.log - sudo cat /var/log/nohang/nohang.log
- sudo make uninstall - sudo make uninstall

View File

@ -133,8 +133,6 @@ $ cd nohang
$ sudo make install $ sudo make install
``` ```
`nohang` package comes with two systemd units that run with different config files: `nohang.conf` and `nohang-desktop.conf`.
To enable and start unit without GUI notifications: To enable and start unit without GUI notifications:
``` ```
$ sudo systemctl enable nohang $ sudo systemctl enable nohang
@ -158,20 +156,24 @@ $ sudo make uninstall
``` ```
./nohang -h ./nohang -h
usage: nohang [-h] [-v] [-m] [-p] [-c CONFIG] [-cc CONFIG] usage: nohang [-h|--help] [-v|--version] [-m|--memload]
[-c|--config CONFIG] [--check] [--monitor] [--tasks]
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
-v, --version print version -v, --version show version of installed package and exit
-m, --memload consume memory until 20 MiB remain free, and terminate -m, --memload consume memory until 20 MiB (MemAvailable + SwapFree)
the process remain free, and terminate the process
-p, --print-proc-table
print table of processes with their badness values
-c CONFIG, --config CONFIG -c CONFIG, --config CONFIG
path to the config file, default values: path to the config file. This should only be used
./nohang.conf, /etc/nohang/nohang.conf with one of the following options:
-cc CONFIG, --check-config CONFIG --monitor, --tasks, --check
check and print config --check check and show the configuration and exit. This should
only be used with -c/--config CONFIG option
--monitor start monitoring. This should only be used with
-c/--config CONFIG option
--tasks show tasks state and exit. This should only be used
with -c/--config CONFIG option
``` ```
## How to configure ## How to configure
@ -200,7 +202,7 @@ If testing occurs while `nohang` is running, these processes should be terminate
## Print table of processes with their badness values ## Print table of processes with their badness values
Run `sudo nohang -p` to see the table of prosesses with their badness values, oom_scores, names, UIDs etc. Run `sudo nohang -c/--config CONFIG --tasks` to see the table of prosesses with their badness values, oom_scores, names, UIDs etc.
<details> <details>
<summary>Output example</summary> <summary>Output example</summary>

View File

@ -2682,20 +2682,24 @@ v_dict = dict()
start_time = monotonic() start_time = monotonic()
help_mess = """usage: nohang [-h] [-v] [-p] [-c CONFIG] [-cc CONFIG] help_mess = """usage: nohang [-h|--help] [-v|--version] [-m|--memload]
[-c|--config CONFIG] [--check] [--monitor] [--tasks]
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
-v, --version print version -v, --version show version of installed package and exit
-m, --memload consume memory until 20 MiB remain free, and terminate -m, --memload consume memory until 20 MiB (MemAvailable + SwapFree)
the process remain free, and terminate the process
-p, --print-proc-table
print table of processes with their badness values
-c CONFIG, --config CONFIG -c CONFIG, --config CONFIG
path to the config file, default values: path to the config file. This should only be used
./nohang.conf, /etc/nohang/nohang.conf with one of the following options:
-cc CONFIG, --check-config CONFIG --monitor, --tasks, --check
check and print config""" --check check and show the configuration and exit. This should
only be used with -c/--config CONFIG option
--monitor start monitoring. This should only be used with
-c/--config CONFIG option
--tasks show tasks state and exit. This should only be used
with -c/--config CONFIG option"""
SC_CLK_TCK = os.sysconf(os.sysconf_names['SC_CLK_TCK']) SC_CLK_TCK = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
@ -2724,12 +2728,6 @@ else:
root = False root = False
if os.path.exists('./nohang_notify_helper'):
notify_helper_path = './nohang_notify_helper'
else:
notify_helper_path = 'nohang_notify_helper'
last_action_dict = dict() last_action_dict = dict()
last_action_dict['t'] = monotonic() last_action_dict['t'] = monotonic()
@ -2753,45 +2751,64 @@ print_proc_table_flag = False
check_config_flag = False check_config_flag = False
if os.path.exists('./nohang.conf'): a = argv[1:]
config = os.getcwd() + '/nohang.conf' la = len(a)
else: if la == 0:
config = '/etc/nohang/nohang.conf' print('ERROR: invalid input: missing CLI options\n')
print(help_mess)
exit(1)
if len(argv) == 1: if la == 1:
pass if a[0] == '-h' or a[0] == '--help':
elif len(argv) == 2:
if argv[1] == '--help' or argv[1] == '-h':
print(help_mess) print(help_mess)
exit() exit()
if argv[1] == '--memload' or argv[1] == '-m': if a[0] == '-v' or a[0] == '--version':
memload()
exit()
elif argv[1] == '--check-config' or argv[1] == '-cc':
check_config_flag = True
elif argv[1] == '--version' or argv[1] == '-v':
print_version() print_version()
elif argv[1] == '--print-proc-table' or argv[1] == '-p': if a[0] == '-m' or a[0] == '--memload':
print_proc_table_flag = True memload()
if os.path.exists('./nohang.conf'): print('ERROR: invalid input\n')
config = os.getcwd() + '/nohang.conf' print(help_mess)
else:
config = '/etc/nohang/nohang.conf'
else:
errprint('Unknown option: {}'.format(argv[1]))
exit(1) exit(1)
elif len(argv) == 3: if la == 2:
if argv[1] == '--config' or argv[1] == '-c': print('ERROR: invalid input\n')
config = argv[2] print(help_mess)
elif argv[1] == '--check-config' or argv[1] == '-cc': exit(1)
config = argv[2] if la == 3:
if '-c' in a or '--config' in a:
if '--monitor' in a or '--check' in a or '--tasks' in a:
try:
aaa = a.index('-c')
except ValueError:
pass
try:
aaa = a.index('--config')
except ValueError:
pass
try:
config = a[aaa + 1]
except IndexError:
print('ERROR: invalid input\n')
print(help_mess)
exit(1)
if (config == '--check' or config == '--monitor' or
config == '--tasks:'):
print('ERROR: invalid input\n')
print(help_mess)
exit(1)
if '--check' in a:
check_config_flag = True check_config_flag = True
if '--tasks' in a:
print_proc_table_flag = True
else: else:
errprint('Unknown option: {}'.format(argv[1])) print('ERROR: invalid input\n')
print(help_mess)
exit(1) exit(1)
else: else:
errprint('Invalid CLI input: too many options') print('ERROR: invalid input\n')
print(help_mess)
exit(1)
if la > 3:
print('ERROR: invalid CLI input: too many options\n')
print(help_mess)
exit(1) exit(1)

View File

@ -5,7 +5,7 @@ Conflicts=nohang.service
After=system.slice After=system.slice
[Service] [Service]
ExecStart=${BINDIR}/nohang --config ${CONFDIR}/nohang/nohang-desktop.conf ExecStart=${BINDIR}/nohang --config ${CONFDIR}/nohang/nohang-desktop.conf --monitor
SyslogIdentifier=nohang-desktop SyslogIdentifier=nohang-desktop
KillMode=mixed KillMode=mixed
Restart=always Restart=always

View File

@ -36,20 +36,24 @@ Nohang is a highly configurable daemon for Linux which is able to correctly prev
-h, --help show this help message and exit -h, --help show this help message and exit
-v, --version print version -v, --version show version of installed package and exit
-m, --memload consume memory until 20 MiB remain free, and terminate -m, --memload consume memory until 20 MiB (MemAvailable + SwapFree)
the process remain free, and terminate the process
-p, --print-proc-table
print table of processes with their badness values
-c CONFIG, --config CONFIG -c CONFIG, --config CONFIG
path to the config file, default values: path to the config file. This should only be used
./nohang.conf, /etc/nohang/nohang.conf with one of the following options:
--monitor, --tasks, --check
-cc CONFIG, --check-config CONFIG --check check and show the configuration and exit. This should
check and print config only be used with -c/--config CONFIG option
--monitor start monitoring. This should only be used with
-c/--config CONFIG option
--tasks show tasks state and exit. This should only be used
with -c/--config CONFIG option
.SH HOW TO CONFIGURE .SH HOW TO CONFIGURE

View File

@ -5,7 +5,7 @@ Conflicts=nohang-desktop.service
After=system.slice After=system.slice
[Service] [Service]
ExecStart=${BINDIR}/nohang --config ${CONFDIR}/nohang/nohang.conf ExecStart=${BINDIR}/nohang --config ${CONFDIR}/nohang/nohang.conf --monitor
SyslogIdentifier=nohang SyslogIdentifier=nohang
KillMode=mixed KillMode=mixed
Restart=always Restart=always