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 -v
- nohang -cc /etc/nohang/nohang.conf
- nohang -cc /etc/nohang/nohang-desktop.conf
- nohang -cc nohang/test.conf
- nohang --check --config /etc/nohang/nohang.conf
- nohang --check --config /etc/nohang/nohang-desktop.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 &
- 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 make uninstall

View File

@ -133,8 +133,6 @@ $ cd nohang
$ 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:
```
$ sudo systemctl enable nohang
@ -158,20 +156,24 @@ $ sudo make uninstall
```
./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:
-h, --help show this help message and exit
-v, --version print version
-m, --memload consume memory until 20 MiB remain free, and terminate
the process
-p, --print-proc-table
print table of processes with their badness values
-v, --version show version of installed package and exit
-m, --memload consume memory until 20 MiB (MemAvailable + SwapFree)
remain free, and terminate the process
-c CONFIG, --config CONFIG
path to the config file, default values:
./nohang.conf, /etc/nohang/nohang.conf
-cc CONFIG, --check-config CONFIG
check and print config
path to the config file. This should only be used
with one of the following options:
--monitor, --tasks, --check
--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
@ -200,7 +202,7 @@ If testing occurs while `nohang` is running, these processes should be terminate
## 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>
<summary>Output example</summary>

View File

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

View File

@ -5,7 +5,7 @@ Conflicts=nohang.service
After=system.slice
[Service]
ExecStart=${BINDIR}/nohang --config ${CONFDIR}/nohang/nohang-desktop.conf
ExecStart=${BINDIR}/nohang --config ${CONFDIR}/nohang/nohang-desktop.conf --monitor
SyslogIdentifier=nohang-desktop
KillMode=mixed
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
-v, --version print version
-v, --version show version of installed package and exit
-m, --memload consume memory until 20 MiB remain free, and terminate
the process
-p, --print-proc-table
print table of processes with their badness values
-m, --memload consume memory until 20 MiB (MemAvailable + SwapFree)
remain free, and terminate the process
-c CONFIG, --config CONFIG
path to the config file, default values:
./nohang.conf, /etc/nohang/nohang.conf
path to the config file. This should only be used
with one of the following options:
--monitor, --tasks, --check
-cc CONFIG, --check-config CONFIG
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
.SH HOW TO CONFIGURE

View File

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