From 04248c9069d87bea8e3b7b400c541c97ff292ac2 Mon Sep 17 00:00:00 2001 From: liuy Date: Wed, 9 Sep 2009 01:06:35 +0000 Subject: [PATCH] Branch HEAD b=18775 i=adilger i=robert.read Improve lctl list/get/set_param: -introduce lctl list_param -handle the bad options -support more than one arguments -add '-F' option to show the indicator to the parameters, if '-N' specified in lctl get_param --- lustre/utils/lctl.c | 22 ++- lustre/utils/lustre_cfg.c | 383 +++++++++++++++++++++++++++++++++------------- lustre/utils/obdctl.h | 1 + 3 files changed, 294 insertions(+), 112 deletions(-) diff --git a/lustre/utils/lctl.c b/lustre/utils/lctl.c index 3c78810..e85190b 100644 --- a/lustre/utils/lctl.c +++ b/lustre/utils/lctl.c @@ -137,14 +137,24 @@ command_t cmdlist[] = { {"local_param", jt_lcfg_param, 0, "set a temporary, local param\n" "usage: local_param ...\n"}, {"get_param", jt_lcfg_getparam, 0, "get the Lustre or LNET parameter\n" - "usage: get_param [-n|-N] name.of.param\n" - "Get the value of Lustre or LNET parameter from the specified path\n" - "Use '-n' to disable printing of parameter name when printing value\n" - "Use '-N' to print only parameter names and not the values."}, + "usage: get_param [-n|-N|-F] \n" + "Get the value of Lustre or LNET parameter from the specified path.\n" + "The path can contain shell-style filename patterns.\n" + " -n Print only the value and not parameter name.\n" + " -N Print only matched parameter names and not the values.\n" + " (Especially useful when using patterns.)\n" + " -F When -N specified, add '/', '@' or '=' for directories,\n" + " symlinks and writeable files, respectively."}, {"set_param", jt_lcfg_setparam, 0, "set the Lustre or LNET parameter\n" - "usage: set_param [-n] name.of.param=value\n" + "usage: set_param [-n] \n" "Set the value of the Lustre or LNET parameter at the specified path\n" - "Use '-n' to disable printing of parameter name when setting value\n"}, + " -n Disable printing of the key name when printing values."}, + {"list_param", jt_lcfg_listparam, 0, + "list the Lustre or LNET parameter name\n" + "usage: list_param [-F] \n" + "List the name of Lustre or LNET parameter from the specified path.\n" + " -F Add '/', '@' or '=' for dirs, symlinks and writeable files,\n" + "respectively."}, /* Debug commands */ {"==== debugging control ====", jt_noop, 0, "debug"}, diff --git a/lustre/utils/lustre_cfg.c b/lustre/utils/lustre_cfg.c index 261c5b2..3d88065 100644 --- a/lustre/utils/lustre_cfg.c +++ b/lustre/utils/lustre_cfg.c @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -520,9 +521,15 @@ int jt_lcfg_mgsparam(int argc, char **argv) /* Display the path in the same format as sysctl * For eg. obdfilter.lustre-OST0000.stats */ -static char *display_name(char *filename) +static char *display_name(char *filename, int show_type) { char *tmp; + struct stat st; + + if (show_type) { + if (lstat(filename, &st) < 0) + return NULL; + } filename += strlen("/proc/"); if (strncmp(filename, "fs/", strlen("fs/")) == 0) @@ -538,6 +545,16 @@ static char *display_name(char *filename) while ((tmp = strchr(tmp, '/')) != NULL) *tmp = '.'; + /* append the indicator to entries */ + if (show_type) { + if (S_ISDIR(st.st_mode)) + strcat(filename, "/"); + else if (S_ISLNK(st.st_mode)) + strcat(filename, "@"); + else if (st.st_mode & S_IWUSR) + strcat(filename, "="); + } + return filename; } @@ -566,7 +583,7 @@ static char *globerrstr(int glob_rc) case GLOB_NOMATCH: return "Found no match"; } - return "Unknow error"; + return "Unknown error"; } static void clean_path(char *path) @@ -596,44 +613,135 @@ static void clean_path(char *path) } } -int jt_lcfg_getparam(int argc, char **argv) +struct param_opts { + int only_path; + int show_path; + int show_type; +}; + +static int listparam_cmdline(int argc, char **argv, struct param_opts *popt) +{ + int ch; + + popt->show_path = 1; + popt->only_path = 1; + popt->show_type = 0; + + while ((ch = getopt(argc, argv, "F")) != -1) { + switch (ch) { + case 'F': + popt->show_type = 1; + break; + default: + return -1; + } + } + + return optind; +} + +static int listparam_display(struct param_opts *popt, char *pattern) +{ + int rc; + int i; + glob_t glob_info; + char filename[PATH_MAX + 1]; /* extra 1 byte for file type */ + + rc = glob(pattern, GLOB_BRACE, NULL, &glob_info); + if (rc) { + fprintf(stderr, "error: list_param: %s: %s\n", + pattern, globerrstr(rc)); + return -ESRCH; + } + + for (i = 0; i < glob_info.gl_pathc; i++) { + char *valuename = NULL; + + strcpy(filename, glob_info.gl_pathv[i]); + valuename = display_name(filename, popt->show_type); + if (valuename) + printf("%s\n", valuename); + } + + globfree(&glob_info); + return rc; +} + +int jt_lcfg_listparam(int argc, char **argv) { int fp; - int rc = 0, i, show_path = 0, only_path = 0; + int rc = 0, i; + struct param_opts popt; char pattern[PATH_MAX]; - char *path, *buf; - glob_t glob_info; + char *path; - 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]; - } else { + rc = listparam_cmdline(argc, argv, &popt); + if (rc < 0 || rc >= argc) return CMD_HELP; + + for (i = rc; i < argc; i++) { + path = argv[i]; + + clean_path(path); + + /* If the entire path is specified as input */ + fp = open(path, O_RDONLY); + if (fp < 0) { + snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s", + path); + } else { + strcpy(pattern, path); + close(fp); + } + + rc = listparam_display(&popt, pattern); + if (rc < 0) + return rc; } - clean_path(path); + return 0; +} - /* If the entire path is specified as input */ - fp = open(path, O_RDONLY); - if (fp < 0) - snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s", - path); - else { - strcpy(pattern, path); - close(fp); +static int getparam_cmdline(int argc, char **argv, struct param_opts *popt) +{ + int ch; + + popt->show_path = 1; + popt->only_path = 0; + popt->show_type = 0; + + while ((ch = getopt(argc, argv, "nNF")) != -1) { + switch (ch) { + case 'N': + popt->only_path = 1; + break; + case 'n': + popt->show_path = 0; + case 'F': + popt->show_type = 1; + break; + default: + return -1; + } } + return optind; +} + +static int getparam_display(struct param_opts *popt, char *pattern) +{ + int rc; + int fd; + int i; + char *buf; + glob_t glob_info; + char filename[PATH_MAX + 1]; /* extra 1 byte for file type */ + rc = glob(pattern, GLOB_BRACE, NULL, &glob_info); if (rc) { - fprintf(stderr, "error : glob %s: %s \n", pattern, - globerrstr(rc)); - return rc; + fprintf(stderr, "error: get_param: %s: %s\n", + pattern, globerrstr(rc)); + return -ESRCH; } buf = malloc(CFS_PAGE_SIZE); @@ -641,39 +749,36 @@ int jt_lcfg_getparam(int argc, char **argv) char *valuename = NULL; memset(buf, 0, CFS_PAGE_SIZE); - if (show_path) { - char *filename; - filename = strdup(glob_info.gl_pathv[i]); - valuename = display_name(filename); - if (valuename && only_path) { - printf("%s\n", valuename); - continue; - } + /* As listparam_display is used to show param name (with type), + * here "if (only_path)" is ignored.*/ + if (popt->show_path) { + strcpy(filename, glob_info.gl_pathv[i]); + valuename = display_name(filename, 0); } /* Write the contents of file to stdout */ - fp = open(glob_info.gl_pathv[i], O_RDONLY); - if (fp < 0) { - fprintf(stderr, "error: %s: opening('%s') failed: %s\n", - jt_cmdname(argv[0]), glob_info.gl_pathv[i], - strerror(errno)); + fd = open(glob_info.gl_pathv[i], O_RDONLY); + if (fd < 0) { + fprintf(stderr, + "error: get_param: opening('%s') failed: %s\n", + glob_info.gl_pathv[i], strerror(errno)); continue; } do { - rc = read(fp, buf, CFS_PAGE_SIZE); + rc = read(fd, buf, CFS_PAGE_SIZE); if (rc == 0) break; if (rc < 0) { - fprintf(stderr, "error: %s: read('%s') " - "failed: %s\n", jt_cmdname(argv[0]), + fprintf(stderr, "error: get_param: " + "read('%s') failed: %s\n", glob_info.gl_pathv[i], strerror(errno)); break; } /* Print the output in the format path=value if the * value contains no new line character or cab be * occupied in a line, else print value on new line */ - if (valuename && show_path) { + if (valuename && popt->show_path) { int longbuf = strnchr(buf, rc - 1, '\n') != NULL || rc > 60; printf("%s=%s", valuename, longbuf ? "\n" : buf); @@ -684,13 +789,13 @@ int jt_lcfg_getparam(int argc, char **argv) } rc = write(fileno(stdout), buf, rc); if (rc < 0) { - fprintf(stderr, "error: %s: write to stdout " - "failed: %s\n", jt_cmdname(argv[0]), + fprintf(stderr, "error: get_param: " + "write to stdout failed: %s\n", strerror(errno)); break; } } while (1); - close(fp); + close(fd); } globfree(&glob_info); @@ -698,88 +803,154 @@ int jt_lcfg_getparam(int argc, char **argv) return rc; } -int jt_lcfg_setparam(int argc, char **argv) +int jt_lcfg_getparam(int argc, char **argv) { + int fp; int rc = 0, i; - int fp, show_path = 0; + struct param_opts popt; char pattern[PATH_MAX]; - char *path, *value; - glob_t glob_info; + char *path; - path = argv[1]; - if (argc == 4 && (strcmp(argv[1], "-n") == 0)) { - /* Format: lctl set_param -n param value */ - path = argv[2]; - value = argv[3]; - } else if (argc == 3) { - if (strcmp(argv[1], "-n") != 0) { - /* Format: lctl set_param param value */ - show_path = 1; - value = argv[2]; - } else if ((value = strchr(argv[2], '=')) != NULL) { - /* Format: lctl set_param -n param=value */ - path = argv[2]; - *value = '\0'; - value ++; + rc = getparam_cmdline(argc, argv, &popt); + if (rc < 0 || rc >= argc) + return CMD_HELP; + + for (i = rc; i < argc; i++) { + path = argv[i]; + + clean_path(path); + + /* If the entire path is specified as input */ + fp = open(path, O_RDONLY); + if (fp < 0) { + snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s", + path); } else { - fprintf(stderr, "error: %s Incorrect arguments." - "See Usage\n", - jt_cmdname(argv[0])); - return CMD_HELP; + strcpy(pattern, path); + close(fp); } - } else if (argc == 2 && ((value = strchr(argv[1], '=')) != NULL)) { - /* Format: lctl set_param param=value */ - show_path = 1; - *value = '\0'; - value++; - } else { - fprintf(stderr, "error: %s Incorrect arguments. See Usage\n", - jt_cmdname(argv[0])); - return CMD_HELP; + + if (popt.only_path) + rc = listparam_display(&popt, pattern); + else + rc = getparam_display(&popt, pattern); + if (rc < 0) + return rc; } - clean_path(path); + return 0; +} - fp = open(path, O_RDONLY); - if (fp < 0) - snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s", - path); - else { - strcpy(pattern, path); - close(fp); +static int setparam_cmdline(int argc, char **argv, struct param_opts *popt) +{ + int ch; + + popt->show_path = 1; + popt->only_path = 0; + popt->show_type = 0; + + while ((ch = getopt(argc, argv, "n")) != -1) { + switch (ch) { + case 'n': + popt->show_path = 0; + break; + default: + return -1; + } } + return optind; +} + +static int setparam_display(struct param_opts *popt, char *pattern, char *value) +{ + int rc; + int fd; + int i; + glob_t glob_info; + char filename[PATH_MAX + 1]; /* extra 1 byte for file type */ rc = glob(pattern, GLOB_BRACE, NULL, &glob_info); if (rc) { - fprintf(stderr, "error : glob %s: %s \n", pattern, - globerrstr(rc)); - return rc; + fprintf(stderr, "error: set_param: %s: %s\n", + pattern, globerrstr(rc)); + return -ESRCH; } for (i = 0; i < glob_info.gl_pathc; i++) { - if (show_path) { - char *valuename, *filename; - filename = strdup(glob_info.gl_pathv[i]); - valuename = display_name(filename); - printf("%s=%s\n", valuename, value); + char *valuename = NULL; + + if (popt->show_path) { + strcpy(filename, glob_info.gl_pathv[i]); + valuename = display_name(filename, 0); + if (valuename) + printf("%s=%s\n", valuename, value); } /* Write the new value to the file */ - fp = open(glob_info.gl_pathv[i], O_WRONLY); - if (fp > 0) { - rc = write(fp, value, strlen(value)); + fd = open(glob_info.gl_pathv[i], O_WRONLY); + if (fd > 0) { + rc = write(fd, value, strlen(value)); if (rc < 0) - fprintf(stderr, - "error writing to file %s\n", - glob_info.gl_pathv[i]); + fprintf(stderr, "error: set_param: " + "writing to file %s: %s\n", + glob_info.gl_pathv[i], strerror(errno)); else rc = 0; - close(fp); + close(fd); } else { - fprintf(stderr, "error: %s: %s opening %s\n", - jt_cmdname(argv[0]), strerror(rc = errno), - glob_info.gl_pathv[i]); + fprintf(stderr, "error: set_param: %s opening %s\n", + strerror(rc = errno), glob_info.gl_pathv[i]); } } globfree(&glob_info); return rc; } + +int jt_lcfg_setparam(int argc, char **argv) +{ + int fp; + int rc = 0, i; + struct param_opts popt; + char pattern[PATH_MAX]; + char *path = NULL, *value = NULL; + + rc = setparam_cmdline(argc, argv, &popt); + if (rc < 0 || rc >= argc) + return CMD_HELP; + + for (i = rc; i < argc; i++) { + if ((value = strchr(argv[i], '=')) != NULL) { + /* format: set_param a=b */ + *value = '\0'; + value ++; + path = argv[i]; + } else { + /* format: set_param a b */ + if (path == NULL) { + path = argv[i]; + continue; + } else { + value = argv[i]; + } + } + + clean_path(path); + + /* If the entire path is specified as input */ + fp = open(path, O_RDONLY); + if (fp < 0) { + snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s", + path); + } else { + strcpy(pattern, path); + close(fp); + } + + rc = setparam_display(&popt, pattern, value); + path = NULL; + value = NULL; + if (rc < 0) + return rc; + } + + return 0; +} diff --git a/lustre/utils/obdctl.h b/lustre/utils/obdctl.h index 0c69b12..b8bfd64 100644 --- a/lustre/utils/obdctl.h +++ b/lustre/utils/obdctl.h @@ -113,6 +113,7 @@ int jt_lcfg_param(int argc, char **argv); int jt_lcfg_mgsparam(int argc, char **argv); int jt_lcfg_getparam(int argc, char **argv); int jt_lcfg_setparam(int argc, char **argv); +int jt_lcfg_listparam(int argc, char **argv); int obd_add_uuid(char *uuid, lnet_nid_t nid); -- 1.8.3.1