| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "apps_plugin.h" |
| 4 | |
| 5 | bool pid_match_check(struct pid_stat *p, APPS_MATCH *match) { |
| 6 | if(!match->starts_with && !match->ends_with) { |
| 7 | // Exact match |
| 8 | if(match->pattern) { |
| 9 | if(simple_pattern_matches_string(match->pattern, p->comm)) |
| 10 | return true; |
| 11 | #if (PROCESSES_HAVE_COMM_AND_NAME == 1) |
| 12 | if(p->name && simple_pattern_matches_string(match->pattern, p->name)) |
| 13 | return true; |
| 14 | #endif |
| 15 | } |
| 16 | else { |
| 17 | if(string_equals_string_nocase(match->compare, p->comm) || |
| 18 | string_equals_string_nocase(match->compare, p->comm_orig)) |
| 19 | return true; |
| 20 | #if (PROCESSES_HAVE_COMM_AND_NAME == 1) |
| 21 | if(p->name && string_equals_string_nocase(match->compare, p->name)) |
| 22 | return true; |
| 23 | #endif |
| 24 | } |
| 25 | } |
| 26 | else if(match->starts_with && !match->ends_with) { |
| 27 | // Prefix match |
| 28 | if(match->pattern) { |
| 29 | if(simple_pattern_matches_string(match->pattern, p->comm)) |
| 30 | return true; |
| 31 | #if (PROCESSES_HAVE_COMM_AND_NAME == 1) |
| 32 | if(p->name && simple_pattern_matches_string(match->pattern, p->name)) |
| 33 | return true; |
| 34 | #endif |
| 35 | } |
| 36 | else { |
| 37 | if(string_starts_with_string_nocase(p->comm, match->compare) || |
| 38 | (p->comm != p->comm_orig && string_starts_with_string_nocase(p->comm_orig, match->compare))) |
| 39 | return true; |
| 40 | #if (PROCESSES_HAVE_COMM_AND_NAME == 1) |
| 41 | if(p->name && string_starts_with_string_nocase(p->name, match->compare)) |
| 42 | return true; |
| 43 | #endif |
| 44 | } |
| 45 | } |
| 46 | else if(!match->starts_with && match->ends_with) { |
| 47 | // Suffix match |
| 48 | if(match->pattern) { |
| 49 | if(simple_pattern_matches_string(match->pattern, p->comm)) |
| 50 | return true; |
| 51 | #if (PROCESSES_HAVE_COMM_AND_NAME == 1) |
| 52 | if(p->name && simple_pattern_matches_string(match->pattern, p->name)) |
| 53 | return true; |
| 54 | #endif |
| 55 | } |
| 56 | else { |
| 57 | if(string_ends_with_string_nocase(p->comm, match->compare) || |
| 58 | (p->comm != p->comm_orig && string_ends_with_string_nocase(p->comm_orig, match->compare))) |
| 59 | return true; |
| 60 | #if (PROCESSES_HAVE_COMM_AND_NAME == 1) |
| 61 | if(p->name && string_ends_with_string_nocase(p->name, match->compare)) |
| 62 | return true; |
| 63 | #endif |
| 64 | } |
| 65 | } |
| 66 | else if(match->starts_with && match->ends_with) { |
| 67 | // Substring match - search in cmdline and on Windows also in name |
| 68 | if(p->cmdline) { |
| 69 | if(match->pattern) { |
| 70 | if(simple_pattern_matches_string(match->pattern, p->cmdline)) |
| 71 | return true; |
| 72 | } |
| 73 | else { |
| 74 | if(strcasestr(string2str(p->cmdline), string2str(match->compare))) |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | #if (PROCESSES_HAVE_COMM_AND_NAME == 1) |
| 79 | // On Windows, also search in the name field for substring patterns |
| 80 | if(p->name) { |
| 81 | if(match->pattern) { |
| 82 | if(simple_pattern_matches_string(match->pattern, p->name)) |
| 83 | return true; |
| 84 | } |
| 85 | else { |
| 86 | if(strcasestr(string2str(p->name), string2str(match->compare))) |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | APPS_MATCH pid_match_create(const char *comm) { |
| 97 | APPS_MATCH m = { |
| 98 | .starts_with = false, |
| 99 | .ends_with = false, |
| 100 | .compare = NULL, |
| 101 | .pattern = NULL, |
| 102 | }; |
| 103 | |
| 104 | // copy comm to make changes to it |
| 105 | char *buf = strdupz(comm); |
| 106 | |
| 107 | trim_all(buf); |
| 108 | size_t len = strlen(buf); |
| 109 | |
| 110 | if(len && buf[len - 1] == '*') { |
| 111 | buf[--len] = '\0'; |
| 112 | m.starts_with = true; |
| 113 | } |
| 114 | |
| 115 | const char *nid = buf; |
| 116 | if (nid[0] == '*') { |
| 117 | m.ends_with = true; |
| 118 | nid++; |
| 119 | } |
| 120 | |
| 121 | m.compare = string_strdupz(nid); |
| 122 | |
| 123 | if(strchr(nid, '*')) |
| 124 | m.pattern = simple_pattern_create(comm, SIMPLE_PATTERN_NO_SEPARATORS, SIMPLE_PATTERN_EXACT, false); |
| 125 | |
| 126 | freez(buf); |
| 127 | return m; |
| 128 | } |
| 129 | |
| 130 | void pid_match_cleanup(APPS_MATCH *m) { |
| 131 | string_freez(m->compare); |
| 132 | simple_pattern_free(m->pattern); |
| 133 | } |