+int jt_lcfg_listparam(int argc, char **argv)
+{
+ int rc = 0, i;
+ struct param_opts popt;
+ char pattern[PATH_MAX];
+ char *path;
+
+ rc = listparam_cmdline(argc, argv, &popt);
+ if (rc == argc && popt.recursive) {
+ rc--; /* we know at least "-R" is a parameter */
+ argv[rc] = "*";
+ } else if (rc < 0 || rc >= argc) {
+ return CMD_HELP;
+ }
+
+ for (i = rc; i < argc; i++) {
+ path = argv[i];
+
+ clean_path(path);
+
+ lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
+
+ rc = listparam_display(&popt, pattern);
+ if (rc < 0)
+ return rc;
+ }
+
+ return 0;
+}
+
+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;
+ popt->recursive = 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: get_param: %s: %s\n",
+ pattern, globerrstr(rc));
+ return -ESRCH;
+ }
+
+ buf = malloc(CFS_PAGE_SIZE);
+ for (i = 0; i < glob_info.gl_pathc; i++) {
+ char *valuename = NULL;
+
+ memset(buf, 0, CFS_PAGE_SIZE);
+ /* 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 */
+ 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(fd, buf, CFS_PAGE_SIZE);
+ if (rc == 0)
+ break;
+ if (rc < 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 && popt->show_path) {
+ int longbuf = strnchr(buf, rc - 1, '\n') != NULL
+ || rc > 60;
+ printf("%s=%s", valuename, longbuf ? "\n" : buf);
+ valuename = NULL;
+ if (!longbuf)
+ continue;
+ fflush(stdout);
+ }
+ rc = write(fileno(stdout), buf, rc);
+ if (rc < 0) {
+ fprintf(stderr, "error: get_param: "
+ "write to stdout failed: %s\n",
+ strerror(errno));
+ break;
+ }
+ } while (1);
+ close(fd);
+ }
+
+ globfree(&glob_info);
+ free(buf);
+ return rc;
+}
+
+int jt_lcfg_getparam(int argc, char **argv)
+{
+ int rc = 0, i;
+ struct param_opts popt;
+ char pattern[PATH_MAX];
+ char *path;
+
+ 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);
+
+ lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
+
+ if (popt.only_path)
+ rc = listparam_display(&popt, pattern);
+ else
+ rc = getparam_display(&popt, pattern);
+ if (rc < 0)
+ return rc;
+ }
+
+ return 0;
+}
+
+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;
+ popt->recursive = 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: set_param: %s: %s\n",
+ pattern, globerrstr(rc));
+ return -ESRCH;
+ }
+ for (i = 0; i < glob_info.gl_pathc; i++) {
+ 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 */
+ fd = open(glob_info.gl_pathv[i], O_WRONLY);
+ if (fd > 0) {
+ rc = write(fd, value, strlen(value));
+ if (rc < 0)
+ fprintf(stderr, "error: set_param: "
+ "writing to file %s: %s\n",
+ glob_info.gl_pathv[i], strerror(errno));
+ else
+ rc = 0;
+ close(fd);
+ } else {
+ 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 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);
+
+ lprocfs_param_pattern(argv[0], path, pattern, sizeof(pattern));
+
+ rc = setparam_display(&popt, pattern, value);
+ path = NULL;
+ value = NULL;
+ if (rc < 0)
+ return rc;
+ }
+
+ return 0;
+}