From: Dhaval Giani Date: Mon, 17 May 2010 16:15:59 +0000 (+0200) Subject: libcgroup: Introduce cgroup_basename and modify the functions to use it. X-Git-Tag: v0.36~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4af947b8722d49daded2aeb69e3436d29d83dc00;p=thirdparty%2Flibcgroup.git libcgroup: Introduce cgroup_basename and modify the functions to use it. basename() is not safe as it modifies the pointers from time to time. In order to prevent it, write a safe version of basename which uses basename internally. As a side effect, it also clears up the following warning, api.c: In function ‘cgroup_find_matching_rule’: api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ api.c:2201: warning: passing argument 1 of ‘__xpg_basename’ discards qualifiers from pointer target type /usr/include/libgen.h:35: note: expected ‘char *’ but argument is of type ‘const char *’ Signed-off-by: Dhaval Giani --- diff --git a/src/api.c b/src/api.c index 672243fe..2d4cd21e 100644 --- a/src/api.c +++ b/src/api.c @@ -180,6 +180,26 @@ static int cg_chown_recursive(char **path, uid_t owner, gid_t group) return ret; } +static char *cgroup_basename(const char *path) +{ + char *base; + char *tmp_string; + + tmp_string = strdup(path); + + if (!tmp_string) + return NULL; + + base = strdup(basename(tmp_string)); + + if (!base) + return NULL; + + free(tmp_string); + + return base; +} + static int cgroup_test_subsys_mounted(const char *name) { int i; @@ -494,6 +514,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, if (!matched) continue; if (len_procname) { + char *mproc_base; /* * If there is a rule based on process name, * it should be matched with mprocname. @@ -505,18 +526,16 @@ static int cgroup_parse_rules(bool cache, uid_t muid, continue; } - /* - * FIXME: basename() modifies the - * string and really shouldn't! - **/ - + mproc_base = cgroup_basename(mprocname); if (strcmp(mprocname, procname) && - strcmp(basename(mprocname), procname)) { + strcmp(mproc_base, procname)) { uid = CGRULE_INVALID; gid = CGRULE_INVALID; matched = false; + free(mproc_base); continue; } + free(mproc_base); } } @@ -2179,6 +2198,7 @@ static struct cgroup_rule *cgroup_find_matching_rule(uid_t uid, { /* Return value */ struct cgroup_rule *ret = rl.head; + char *base = NULL; pthread_rwlock_wrlock(&rl_lock); while (ret) { @@ -2194,17 +2214,20 @@ static struct cgroup_rule *cgroup_find_matching_rule(uid_t uid, break; if (!strcmp(ret->procname, procname)) break; - /* - * FIXME: basename() modifies the string and really shouldn't! - **/ - if (!strcmp(ret->procname, basename(procname))) + base = cgroup_basename(procname); + if (!strcmp(ret->procname, base)) /* Check a rule of basename. */ break; ret = ret->next; + free(base); + base = NULL; } pthread_rwlock_unlock(&rl_lock); + if (base) + free(base); + return ret; }