Skip to content

Commit

Permalink
Add support for regexps in "name" field
Browse files Browse the repository at this point in the history
  • Loading branch information
uhbif19 committed Mar 20, 2024
1 parent bb52318 commit c7b9f88
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Support for cmdline in rules added. This is particularly useful for applications
```
This translates to: apply the rule to any process named `java`, that received `freenet.node.NodeStarter` as a command line argument. You can add more than one "cmdlines" in case you want to fine tune your rules.

Support for matching cmd by regexp:

```
{ "name": "ghc-.*", "name_is_regexp": true, "type": "compiler" }
```

# Old description

## Description
Expand Down
25 changes: 23 additions & 2 deletions ananicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ def __get_val(self, col):
return ""
return tmp[1].rstrip('"')

def __check_bool_field(self, value, field_name):
if type(value) == bool:
return value
elif value is None:
return False
else:
raise Failure(f"{field_name} must be boolean")

def __check_nice(self, nice):
if nice:
if not -20 <= nice <= 19:
Expand Down Expand Up @@ -634,6 +642,11 @@ def get_rule_info(self, line):
key = (name, cmdlines)

self.rules[key] = {
"name_is_regexp":
self.__check_bool_field(
line.get("name_is_regexp"),
"name_is_regexp",
),
"nice": self.__check_nice(line.get("nice")),
"ioclass": line.get("ioclass"),
"ionice": self.__check_ionice(line.get("ionice")),
Expand Down Expand Up @@ -713,9 +726,17 @@ def proc_map_update(self):
def get_tpid_rule(self, tpid: TPID):
rule_cmdlines = tpid.cmdline
for rule_name in [tpid.cmd, tpid.stat_name]:
for key in self.rules:
for key, rule_params in self.rules.items():
name,cmdlines = key
if name == rule_name:

# XXX: This field is always present
# Depending on `name_is_regexp`, `name` is checked differently
if rule_params['name_is_regexp']:
name_matches = name == rule_name
else:
name_matches = re.search(name, rule_name)

if name_matches:
if cmdlines:
for cl in cmdlines:
if cl not in rule_cmdlines:
Expand Down
4 changes: 4 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/bash
echo "Test"
sleep 100
echo "End sleep"

0 comments on commit c7b9f88

Please sign in to comment.