Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / utils / lustre_cfg.c
index aa8c1e3..261c5b2 100644 (file)
@@ -1,27 +1,44 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  Copyright (C) 2002 Cluster File Systems, Inc.
- *   Author: Peter J. Braam <braam@clusterfs.com>
- *   Author: Phil Schwan <phil@clusterfs.com>
- *   Author: Andreas Dilger <adilger@clusterfs.com>
- *   Author: Robert Read <rread@clusterfs.com>
+ * GPL HEADER START
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/utils/lustre_cfg.c
+ *
+ * Author: Peter J. Braam <braam@clusterfs.com>
+ * Author: Phil Schwan <phil@clusterfs.com>
+ * Author: Andreas Dilger <adilger@clusterfs.com>
+ * Author: Robert Read <rread@clusterfs.com>
  */
 
 #include <stdlib.h>
@@ -52,7 +69,7 @@
 
 #include "obdctl.h"
 #include <lnet/lnetctl.h>
-#include "parser.h"
+#include <libcfs/libcfsutil.h>
 #include <stdio.h>
 
 static char * lcfg_devname;
@@ -65,7 +82,7 @@ int lcfg_set_devname(char *name)
                 /* quietly strip the unnecessary '$' */
                 if (*name == '$' || *name == '%')
                         name++;
-                if (isdigit(*name)) { 
+                if (isdigit(*name)) {
                         /* We can't translate from dev # to name */
                         lcfg_devname = NULL;
                 } else {
@@ -73,7 +90,7 @@ int lcfg_set_devname(char *name)
                 }
         } else {
                 lcfg_devname = NULL;
-        } 
+        }
         return 0;
 }
 
@@ -137,8 +154,8 @@ int jt_lcfg_setup(int argc, char **argv)
 
         if (lcfg_devname == NULL) {
                 fprintf(stderr, "%s: please use 'device name' to set the "
-                        "device name for config commands.\n", 
-                        jt_cmdname(argv[0])); 
+                        "device name for config commands.\n",
+                        jt_cmdname(argv[0]));
                 return -EINVAL;
         }
 
@@ -169,8 +186,8 @@ int jt_obd_detach(int argc, char **argv)
 
         if (lcfg_devname == NULL) {
                 fprintf(stderr, "%s: please use 'device name' to set the "
-                        "device name for config commands.\n", 
-                        jt_cmdname(argv[0])); 
+                        "device name for config commands.\n",
+                        jt_cmdname(argv[0]));
                 return -EINVAL;
         }
 
@@ -201,8 +218,8 @@ int jt_obd_cleanup(int argc, char **argv)
 
         if (lcfg_devname == NULL) {
                 fprintf(stderr, "%s: please use 'device name' to set the "
-                        "device name for config commands.\n", 
-                        jt_cmdname(argv[0])); 
+                        "device name for config commands.\n",
+                        jt_cmdname(argv[0]));
                 return -EINVAL;
         }
 
@@ -239,8 +256,8 @@ int jt_obd_cleanup(int argc, char **argv)
         return rc;
 }
 
-static 
-int do_add_uuid(char * func, char *uuid, lnet_nid_t nid) 
+static
+int do_add_uuid(char * func, char *uuid, lnet_nid_t nid)
 {
         int rc;
         struct lustre_cfg_bufs bufs;
@@ -252,7 +269,7 @@ int do_add_uuid(char * func, char *uuid, lnet_nid_t nid)
 
         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
         lcfg->lcfg_nid = nid;
-        /* Poison NAL -- pre 1.4.6 will LASSERT on 0 NAL, this way it 
+        /* Poison NAL -- pre 1.4.6 will LASSERT on 0 NAL, this way it
            doesn't work without crashing (bz 10130) */
         lcfg->lcfg_nal = 0x5a;
 
@@ -275,8 +292,8 @@ int do_add_uuid(char * func, char *uuid, lnet_nid_t nid)
 int jt_lcfg_add_uuid(int argc, char **argv)
 {
         lnet_nid_t nid;
-        
-        if (argc != 3) {                
+
+        if (argc != 3) {
                 return CMD_HELP;
         }
 
@@ -308,7 +325,7 @@ int jt_lcfg_del_uuid(int argc, char **argv)
         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
         if (strcmp (argv[1], "_all_"))
                 lustre_cfg_bufs_set_string(&bufs, 1, argv[1]);
-        
+
         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
         lustre_cfg_free(lcfg);
@@ -320,9 +337,6 @@ int jt_lcfg_del_uuid(int argc, char **argv)
         return 0;
 }
 
-
-
-
 int jt_lcfg_del_mount_option(int argc, char **argv)
 {
         int rc;
@@ -365,7 +379,7 @@ int jt_lcfg_set_timeout(int argc, char **argv)
         lustre_cfg_bufs_reset(&bufs, lcfg_devname);
         lcfg = lustre_cfg_new(LCFG_SET_TIMEOUT, &bufs);
         lcfg->lcfg_num = atoi(argv[1]);
-        
+
         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
         //rc = lcfg_mgs_ioctl(argv[0], OBD_DEV_ID, lcfg);
 
@@ -377,8 +391,6 @@ int jt_lcfg_set_timeout(int argc, char **argv)
         return rc;
 }
 
-
-
 int jt_lcfg_add_conn(int argc, char **argv)
 {
         struct lustre_cfg_bufs bufs;
@@ -395,8 +407,8 @@ int jt_lcfg_add_conn(int argc, char **argv)
 
         if (lcfg_devname == NULL) {
                 fprintf(stderr, "%s: please use 'device name' to set the "
-                        "device name for config commands.\n", 
-                        jt_cmdname(argv[0])); 
+                        "device name for config commands.\n",
+                        jt_cmdname(argv[0]));
                 return -EINVAL;
         }
 
@@ -428,8 +440,8 @@ int jt_lcfg_del_conn(int argc, char **argv)
 
         if (lcfg_devname == NULL) {
                 fprintf(stderr, "%s: please use 'device name' to set the "
-                        "device name for config commands.\n", 
-                        jt_cmdname(argv[0])); 
+                        "device name for config commands.\n",
+                        jt_cmdname(argv[0]));
                 return -EINVAL;
         }
 
@@ -467,7 +479,7 @@ int jt_lcfg_param(int argc, char **argv)
         }
 
         lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
-        
+
         rc = lcfg_ioctl(argv[0], OBD_DEV_ID, lcfg);
         lustre_cfg_free(lcfg);
         if (rc < 0) {
@@ -502,7 +514,7 @@ int jt_lcfg_mgsparam(int argc, char **argv)
                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
                         strerror(rc = errno));
         }
-        
+
         return rc;
 }
 
@@ -530,6 +542,7 @@ static char *display_name(char *filename)
 }
 
 /* Find a character in a length limited string */
+/* BEWARE - kernel definition of strnchr has args in different order! */
 static char *strnchr(const char *p, char c, size_t n)
 {
        if (!p)
@@ -543,16 +556,60 @@ static char *strnchr(const char *p, char c, size_t n)
        return (0);
 }
 
+static char *globerrstr(int glob_rc)
+{
+        switch(glob_rc) {
+        case GLOB_NOSPACE:
+                return "Out of memory";
+        case GLOB_ABORTED:
+                return "Read error";
+        case GLOB_NOMATCH:
+                return "Found no match";
+        }
+        return "Unknow error";
+}
+
+static void clean_path(char *path)
+{
+        char *tmp;
+
+        /* If the input is in form Eg. obdfilter.*.stats */
+        if (strchr(path, '.')) {
+                tmp = path;
+                while (*tmp != '\0') {
+                        if ((*tmp == '.') &&
+                            (tmp != path) && (*(tmp - 1) != '\\'))
+                                *tmp = '/';
+                        tmp ++;
+                }
+        }
+        /* get rid of '\', glob doesn't like it */
+        if ((tmp = strrchr(path, '\\')) != NULL) {
+                char *tail = path + strlen(path);
+                while (tmp != path) {
+                        if (*tmp == '\\') {
+                                memmove(tmp, tmp + 1, tail - tmp);
+                                --tail;
+                        }
+                        --tmp;
+                }
+        }
+}
+
 int jt_lcfg_getparam(int argc, char **argv)
 {
         int fp;
-        int rc = 0, i, show_path = 0;
+        int rc = 0, i, show_path = 0, only_path = 0;
         char pattern[PATH_MAX];
-        char *path, *tmp, *buf;
+        char *path, *buf;
         glob_t glob_info;
 
-        if (argc == 3 && strcmp(argv[1], "-n") == 0) {
+        if (argc == 3 && (strcmp(argv[1], "-n") == 0 || strcmp(argv[1], "-N") == 0)) {
                 path = argv[2];
+                if (strcmp(argv[1], "-N") == 0) {
+                        only_path = 1;
+                        show_path = 1;
+                }
         } else if (argc == 2) {
                 show_path = 1;
                 path = argv[1];
@@ -560,15 +617,7 @@ int jt_lcfg_getparam(int argc, char **argv)
                 return CMD_HELP;
         }
 
-        /* If the input is in form Eg. obdfilter.*.stats */
-        if (strchr(path, '.')) {
-                tmp = path;
-                while (*tmp != '\0') {
-                        if (*tmp == '.')
-                                *tmp = '/';
-                        tmp ++;
-                }
-        }
+        clean_path(path);
 
         /* If the entire path is specified as input */
         fp = open(path, O_RDONLY);
@@ -582,7 +631,8 @@ int jt_lcfg_getparam(int argc, char **argv)
 
         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
         if (rc) {
-                fprintf(stderr, "error : glob %s: %s \n", pattern,strerror(rc));
+                fprintf(stderr, "error : glob %s: %s \n", pattern,
+                        globerrstr(rc));
                 return rc;
         }
 
@@ -595,6 +645,10 @@ int jt_lcfg_getparam(int argc, char **argv)
                         char *filename;
                         filename = strdup(glob_info.gl_pathv[i]);
                         valuename = display_name(filename);
+                        if (valuename && only_path) {
+                                printf("%s\n", valuename);
+                                continue;
+                        }
                 }
 
                 /* Write the contents of file to stdout */
@@ -644,13 +698,12 @@ int jt_lcfg_getparam(int argc, char **argv)
         return rc;
 }
 
-
 int jt_lcfg_setparam(int argc, char **argv)
 {
         int rc = 0, i;
         int fp, show_path = 0;
         char pattern[PATH_MAX];
-        char *path, *value, *tmp;
+        char *path, *value;
         glob_t glob_info;
 
         path = argv[1];
@@ -685,15 +738,7 @@ int jt_lcfg_setparam(int argc, char **argv)
                 return CMD_HELP;
         }
 
-        /* If the input is in form Eg. obdfilter.*.stats */
-        if (strchr(path, '.')) {
-                tmp = path;
-                while (*tmp != '\0') {
-                        if (*tmp == '.')
-                                *tmp = '/';
-                        tmp ++;
-                }
-        }
+        clean_path(path);
 
         fp = open(path, O_RDONLY);
         if (fp < 0)
@@ -706,7 +751,8 @@ int jt_lcfg_setparam(int argc, char **argv)
 
         rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
         if (rc) {
-                fprintf(stderr, "error : glob %s: %s \n", pattern,strerror(rc));
+                fprintf(stderr, "error : glob %s: %s \n", pattern,
+                        globerrstr(rc));
                 return rc;
         }
         for (i = 0; i  < glob_info.gl_pathc; i++) {