Till now, cgrulesengd had a fixed configuration file at
/etc/cgrules.conf. Not exactly being sure why we made it static,
its quite straightforward to make it an arbitrary file. Do
so.

It is also a nice thing since the next patch makes it possible
to load multiple configuration files one after the other. So it
seems more logical to do it this way.

Signed-off-by: Dhaval Giani <dhaval.gi...@gmail.com>
---
 src/cgrules.c            |    8 ++++----
 src/daemon/cgrulesengd.c |   23 +++++++++++++++++++++--
 src/libcgroup-internal.h |    4 +++-
 3 files changed, 28 insertions(+), 7 deletions(-)

Index: libcg/src/daemon/cgrulesengd.c
===================================================================
--- libcg.orig/src/daemon/cgrulesengd.c
+++ libcg/src/daemon/cgrulesengd.c
@@ -960,7 +960,7 @@ int main(int argc, char *argv[])
        struct group *gr;
 
        /* Command line arguments */
-       const char *short_options = "hvqf:s::ndQu:g:";
+       const char *short_options = "hvqf:s::ndQu:g:c:";
        struct option long_options[] = {
                {"help", no_argument, NULL, 'h'},
                {"verbose", no_argument, NULL, 'v'},
@@ -972,9 +972,12 @@ int main(int argc, char *argv[])
                {"nolog", no_argument, NULL, 'Q'},
                {"socket-user", required_argument, NULL, 'u'},
                {"socket-group", required_argument, NULL, 'g'},
+               {"config-file", required_argument, NULL, 'c'},
                {NULL, 0, NULL, 0}
        };
 
+       cgrules_config_file = NULL;
+
        /* Make sure the user is root. */
        if (getuid() != 0) {
                fprintf(stderr, "Error: Only root can start/stop the control"
@@ -1059,6 +1062,13 @@ int main(int argc, char *argv[])
                        cgroup_dbg("Using socket group %s id %d\n",
                                        optarg, (int)socket_group);
                        break;
+               case 'c': /* config_file */
+                       cgrules_config_file = strdup(optarg);
+                       if (!cgrules_config_file) {
+                               ret = 4;
+                               goto finished;
+                       }
+                       break;
                default:
                        usage(stderr, "");
                        ret = 2;
@@ -1066,6 +1076,15 @@ int main(int argc, char *argv[])
                }
        }
 
+       if (!cgrules_config_file) {
+               cgrules_config_file = strdup(CGRULES_DEFAULT_CONFIG);
+               if (!cgrules_config_file) {
+                       fprintf(stderr, "Failed to set the correct"\
+                                                       " configuration\n");
+                       goto finished;
+               }
+       }
+
        /* Initialize libcgroup. */
        if ((ret = cgroup_init()) != 0) {
                fprintf(stderr, "Error: libcgroup initialization failed, %s\n",
@@ -1076,7 +1095,7 @@ int main(int argc, char *argv[])
        /* Ask libcgroup to load the configuration rules. */
        if ((ret = cgroup_init_rules_cache()) != 0) {
                fprintf(stderr, "Error: libcgroup failed to initialize rules"
-                               "cache from %s. %s\n", CGRULES_CONF_FILE,
+                               "cache from %s. %s\n", cgrules_config_file,
                                cgroup_strerror(ret));
                goto finished;
        }
Index: libcg/src/libcgroup-internal.h
===================================================================
--- libcg.orig/src/libcgroup-internal.h
+++ libcg/src/libcgroup-internal.h
@@ -46,7 +46,7 @@ __BEGIN_DECLS
 #define CGRULE_SUCCESS_STORE_PID       "SUCCESS_STORE_PID"
 
 
-#define CGRULES_CONF_FILE       "/etc/cgrules.conf"
+#define CGRULES_DEFAULT_CONFIG       "/etc/cgrules.conf"
 #define CGRULES_MAX_FIELDS_PER_LINE            3
 
 #define CGROUP_BUFFER_LEN (5 * FILENAME_MAX)
@@ -67,6 +67,8 @@ __BEGIN_DECLS
 #define max(x,y) ((y)<(x)?(x):(y))
 #define min(x,y) ((y)>(x)?(x):(y))
 
+char *cgrules_config_file;
+
 /* Check if cgroup_init has been called or not. */
 int cgroup_initialized;
 
Index: libcg/src/cgrules.c
===================================================================
--- libcg.orig/src/cgrules.c
+++ libcg/src/cgrules.c
@@ -207,10 +207,10 @@ static int cgroup_parse_rules(bool cache
 
        /* Open the configuration file. */
        pthread_rwlock_wrlock(&rl_lock);
-       fp = fopen(CGRULES_CONF_FILE, "re");
+       fp = fopen(cgrules_config_file, "re");
        if (!fp) {
                cgroup_dbg("Failed to open configuration file %s with"
-                               " error: %s\n", CGRULES_CONF_FILE,
+                               " error: %s\n", cgrules_config_file_file,
                                strerror(errno));
                last_errno = errno;
                ret = ECGOTHER;
@@ -841,11 +841,11 @@ int cgroup_reload_cached_rules(void)
        /* Return codes */
        int ret = 0;
 
-       cgroup_dbg("Reloading cached rules from %s.\n", CGRULES_CONF_FILE);
+       cgroup_dbg("Reloading cached rules from %s.\n", cgrules_config_file);
        ret = cgroup_parse_rules(true, CGRULE_INVALID, CGRULE_INVALID, NULL);
        if (ret) {
                cgroup_dbg("Error parsing configuration file \"%s\": %d.\n",
-                       CGRULES_CONF_FILE, ret);
+                       cgrules_config_file, ret);
                ret = ECGRULESPARSEFAIL;
                goto finished;
        }



------------------------------------------------------------------------------
Forrester recently released a report on the Return on Investment (ROI) of
Google Apps. They found a 300% ROI, 38%-56% cost savings, and break-even
within 7 months.  Over 3 million businesses have gone Google with Google Apps:
an online email calendar, and document program that's accessible from your 
browser. Read the Forrester report: http://p.sf.net/sfu/googleapps-sfnew
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to