Permalink
Browse files

use glob matching in simplerules

use the glib glob matching instead of substring match for tests on
strings. this seems more natural for tests of filenames.
  • Loading branch information...
1 parent 0803859 commit d53debf9518dd88ef83fca5d8ee3381254369eef @poelzi committed Mar 19, 2011
Showing with 96 additions and 58 deletions.
  1. +13 −0 conf/simple.conf
  2. +8 −8 conf/simple.d/audio.conf
  3. +3 −3 conf/simple.d/games.conf
  4. +2 −2 conf/simple.d/idle.conf
  5. +10 −8 conf/simple.d/video.conf
  6. +4 −0 conf/ulatencyd.conf
  7. +56 −37 modules/simplerules.c
View
@@ -4,9 +4,22 @@
# [match] [flagename] [[attribute]=[value]|...]
#
# match:
+# if not regular expressions is specified, a glob style pattern is used.
+# The g_pattern_match* functions match a string against a pattern containing
+# '*' and '?' wildcards with similar semantics as the standard glob() function:
+# '*' matches an arbitrary, possibly empty, string,
+# '?' matches an arbitrary character.
+#
+# Note that in contrast to glob(), the '/' character can be matched by the
+# wildcards, there are no '[...]' character ranges and '*' and '?'
+# can not be escaped to include them literally in a pattern.
+#
+# see:
+# http://library.gnome.org/devel/glib/2.26/glib-Glob-style-pattern-matching.html
#
# /full/path path starting with / are absolute exe paths
# basename matches the last part of the command
+# cmd:[globmatch] matches the cmdline with glob pattern
# re_exe:[regexp] regular expression used on the path of the executable
# re_cmd:[regexp] regular expression on full command line
# re_base:[regexp] regular expression on basename
@@ -1,10 +1,10 @@
# audio players
-/usr/bin/amarok user.media
-/usr/bin/clementine user.media
-exaile user.media
-/usr/bin/listen user.media
-/usr/bin/mpd user.media
-/usr/bin/parole user.media
-/usr/bin/rhythmbox user.media
-/usr/bin/xmms2d user.media
+amarok user.media
+clementine user.media
+cmd:python*exaile.py* user.media
+listen user.media
+mpd user.media
+parole user.media
+rhythmbox user.media
+xmms2d user.media
@@ -1,3 +1,3 @@
-re_exe:/usr/games/.* user.game inherit=1
-re_exe:/usr/local/games/.* user.game inherit=1
-re_exe:/opt/games/.* user.game inherit=1
+/usr/games/* user.game inherit=1
+/usr/local/games/* user.game inherit=1
+/opt/games/* user.game inherit=1
@@ -1,2 +1,2 @@
-/usr/bin/boinc daemon.idle inherit=1
-/usr/local/bin/boinc daemon.idle inherit=1
+boinc daemon.idle inherit=1
+boinc daemon.idle inherit=1
@@ -1,11 +1,13 @@
# video players
-re_exe:/usr/bin/(g?)mplayer.* user.media
-/usr/bin/xine user.media
-re_exe:/usr/bin/(c?)vlc user.media
+?mplayer* user.media
+mplayer* user.media
+xine user.media
+?vlc user.media
+vlc user.media
-/usr/bin/dragon user.media
-/usr/bin/totem user.media
-/usr/bin/kplayer user.media
-/usr/bin/kmplayer user.media
-/usr/bin/kaffeine user.media
+dragon user.media
+totem user.media
+kplayer user.media
+kmplayer user.media
+kaffeine user.media
View
@@ -77,3 +77,7 @@ scheduler=cfq
[xwatch]
# poll xserver very n milliseconds
poll_interval=500
+
+[simplerules]
+# enables debug logging for simplerules
+debug=false
View
@@ -36,17 +36,23 @@
#include <fnmatch.h>
int simplerules_id;
+int simplerules_debug;
struct simple_rule {
- gid_t gid;
- uid_t uid;
- char *cmdline;
- char *exe;
- char *basename;
- GRegex *re_exe;
- GRegex *re_cmd;
- GRegex *re_basename;
- u_flag *template;
+ gid_t gid;
+ uid_t uid;
+/* char *cmdline;
+ char *exe;
+ char *basename;
+*/
+ char *pattern;
+ GPatternSpec *glob_exe;
+ GPatternSpec *glob_basename;
+ GPatternSpec *glob_cmd;
+ GRegex *re_exe;
+ GRegex *re_cmd;
+ GRegex *re_basename;
+ u_flag *template;
};
@@ -66,6 +72,9 @@ enum {
LIST_END
};
+#define simple_debug(...) \
+ if(simplerules_debug) g_debug(__VA_ARGS__);
+
int parse_line(char *line, int lineno) {
char **chunks = NULL;
GError *error = NULL;
@@ -98,7 +107,10 @@ int parse_line(char *line, int lineno) {
rule = g_slice_new0(struct simple_rule);
if(chunks[0][0] == '/') {
- rule->exe = g_strdup(chunks[0]);
+ rule->glob_exe = g_pattern_spec_new(chunks[0]);
+
+ } else if(!strncmp(chunks[0], "cmd:", 4)) {
+ rule->glob_cmd = g_pattern_spec_new(chunks[0]+4);
} else if(!strncmp(chunks[0], "re_exe:", 7)) {
rule->re_exe = g_regex_new(chunks[0] + 7, G_REGEX_OPTIMIZE, 0, &error);
@@ -119,8 +131,9 @@ int parse_line(char *line, int lineno) {
goto error;
}
} else {
- rule->basename = g_strdup(chunks[0]);
+ rule->glob_basename = g_pattern_spec_new(chunks[0]);
}
+ rule->pattern = g_strdup(chunks[0]);
rule->template = g_slice_new0(u_flag);
rule->template->name = g_strdup(chunks[1]);
@@ -270,49 +283,54 @@ void read_rules(void) {
int rule_applies(u_proc *proc, struct simple_rule *rule) {
// u_proc_ensure(proc, EXE, TRUE);
// printf("add proc %d to %s\n", proc->pid, proc->exe);
-
- if(rule->cmdline) {
- if(u_proc_ensure(proc, CMDLINE, FALSE) &&
- !strncmp(proc->cmdline_match, rule->cmdline, strlen(rule->cmdline))) {
-// printf("cmdline %s %s\n", proc->cmdline_match, rule->cmdline);
+ gboolean match = FALSE;
+ if(rule->glob_cmd) {
+ if(u_proc_ensure(proc, CMDLINE, FALSE) && proc->cmdline_match) {
+ match = g_pattern_match_string(rule->glob_cmd, proc->cmdline_match);
+ simple_debug("match pid:%d cmdline glob:'%s' cmdline:'%s' = %d", proc->pid, rule->pattern, proc->cmdline_match, match)
+ if(match)
return TRUE;
- }
+ }
}
- if(rule->basename) {
- if(u_proc_ensure(proc, CMDLINE, FALSE) &&
- proc->cmdfile &&
- !strncmp(proc->cmdfile, rule->basename, strlen(rule->basename))) {
-// printf("cmdfile %s %s\n", proc->cmdfile, rule->basename);
+ if(rule->glob_basename) {
+ if(u_proc_ensure(proc, CMDLINE, FALSE) && proc->cmdfile) {
+ match = g_pattern_match_string(rule->glob_basename, proc->cmdfile);
+ simple_debug("match pid:%d basename glob:'%s' basename:'%s' = %d", proc->pid, rule->pattern, proc->cmdfile, match)
+ if(match)
return TRUE;
}
}
- if(rule->exe) {
- if(u_proc_ensure(proc, EXE, FALSE) &&
- !strcmp(proc->exe, rule->exe)) {
-// printf("exe %s %s\n", proc->exe, rule->exe);
+ if(rule->glob_exe) {
+ if(u_proc_ensure(proc, EXE, FALSE) && proc->exe) {
+ match = g_pattern_match_string(rule->glob_exe, proc->exe);
+ simple_debug("match pid:%d exe glob:'%s' exe:'%s' = %d", proc->pid, rule->pattern, proc->exe, match)
+ if(match)
return TRUE;
}
}
if(rule->re_exe) {
- if(u_proc_ensure(proc, EXE, FALSE) &&
- g_regex_match(rule->re_exe, proc->exe, 0, NULL)) {
-// printf("re_exe %s %p\n", proc->exe, rule->re_exe);
+ if(u_proc_ensure(proc, EXE, FALSE) && proc->exe) {
+ match = g_regex_match(rule->re_exe, proc->exe, 0, NULL);
+ simple_debug("match pid:%d cmdline re:'%s' exe:'%s' = %d", proc->pid, rule->pattern, proc->cmdline_match, match)
+ if(match)
return TRUE;
}
}
if(rule->re_cmd) {
- if(u_proc_ensure(proc, CMDLINE, FALSE) &&
- g_regex_match(rule->re_cmd, proc->cmdline_match, 0, NULL)) {
-// printf("re_cmd %s %p\n", proc->cmdline_match, rule->re_cmd);
+ if(u_proc_ensure(proc, CMDLINE, FALSE) && proc->cmdline) {
+ match = g_regex_match(rule->re_cmd, proc->cmdline_match, 0, NULL);
+ simple_debug("match pid:%d cmdline re:'%s' cmdline:'%s' = %d", proc->pid, rule->pattern, proc->cmdline_match, match)
+ if(match)
return TRUE;
- }
+ }
}
if(rule->re_basename) {
- if(u_proc_ensure(proc, CMDLINE, FALSE) &&
- g_regex_match(rule->re_basename, proc->cmdfile, 0, NULL)) {
-// printf("re_base %s %p\n", proc->exe, rule->re_basename);
+ if(u_proc_ensure(proc, CMDLINE, FALSE) && proc->cmdfile) {
+ match = g_regex_match(rule->re_basename, proc->cmdfile, 0, NULL);
+ simple_debug("match pid:%d cmdline re:'%s' basename:'%s' = %d", proc->pid, rule->pattern, proc->cmdline_match, match)
+ if(match)
return TRUE;
- }
+ }
}
return FALSE;
}
@@ -355,6 +373,7 @@ int simplerules_init() {
int i = 0;
simplerules_id = get_plugin_id();
u_filter *filter;
+ simplerules_debug = g_key_file_get_boolean(config_data, "simplerules", "debug", NULL);
// target_rules = NULL;
read_rules();
// if(target_rules) {

0 comments on commit d53debf

Please sign in to comment.