/* Called for each key=value passed on the command line. This plugin * only accepts file=<filename>, which is required. */ static int file_config (const char *key, const char *value) { if (strcmp (key, "file") == 0) { /* See FILENAMES AND PATHS in nbdkit-plugin(3). */ filename = nbdkit_absolute_path (value); if (!filename) return -1; } else { nbdkit_error ("unknown parameter '%s'", key); return -1; } return 0; }
int main (int argc, char *argv[]) { int c; int option_index; int help = 0, version = 0; tls_init (); for (;;) { c = getopt_long (argc, argv, short_options, long_options, &option_index); if (c == -1) break; switch (c) { case 0: /* options which are long only */ if (strcmp (long_options[option_index].name, "dump-config") == 0) { dump_config (); exit (EXIT_SUCCESS); } else if (strcmp (long_options[option_index].name, "run") == 0) { run = optarg; foreground = 1; } else { fprintf (stderr, "%s: unknown long option: %s (%d)\n", program_name, long_options[option_index].name, option_index); exit (EXIT_FAILURE); } break; case 'f': foreground = 1; break; case 'g': group = optarg; break; case 'i': ipaddr = optarg; break; case 'n': newstyle = 1; break; case 'o': /* XXX When we add support for exportnames, we will need to * ensure that the user does not use -o + --export. */ newstyle = 0; break; case 'P': pidfile = nbdkit_absolute_path (optarg); if (pidfile == NULL) exit (EXIT_FAILURE); break; case 'p': port = optarg; break; case 'r': readonly = 1; break; case 's': listen_stdin = 1; break; case 'U': if (strcmp (optarg, "-") == 0) unixsocket = make_random_fifo (); else unixsocket = nbdkit_absolute_path (optarg); if (unixsocket == NULL) exit (EXIT_FAILURE); break; case 'u': user = optarg; break; case 'v': verbose = 1; break; case 'V': version = 1; break; case HELP_OPTION: help = 1; break; default: usage (); exit (EXIT_FAILURE); } } /* No extra parameters. */ if (optind >= argc) { if (help) { usage (); exit (EXIT_SUCCESS); } if (version) { display_version (); exit (EXIT_SUCCESS); } /* Otherwise this is an error. */ fprintf (stderr, "%s: no plugins given on the command line.\nRead nbdkit(1) for documentation.\n", program_name); exit (EXIT_FAILURE); } /* Remaining command line arguments define the plugins and plugin * configuration. If --help or --version was specified, we still * partially parse these in order that we can display the per-plugin * help/version information. In future (when the new protocol and * export names are permitted) we will allow multiple plugins to be * given, but at the moment only one plugin is allowed. */ while (optind < argc) { const char *filename = argv[optind]; char *p; open_plugin_so (filename); /* Find key=value configuration parameters for this plugin. */ ++optind; while (optind < argc && (p = strchr (argv[optind], '=')) != NULL) { if (help || version) continue; *p = '\0'; plugin_config (argv[optind], p+1); ++optind; } if (help) { usage (); printf ("\n%s:\n\n", filename); plugin_usage (); exit (EXIT_SUCCESS); } if (version) { display_version (); plugin_version (); exit (EXIT_SUCCESS); } plugin_config_complete (); /* If we supported export names, then we'd continue in the loop * here, but at the moment only one plugin may be used per server * so exit if there are any more. */ ++optind; if (optind < argc) { fprintf (stderr, "%s: this server only supports a single plugin\n", program_name); exit (EXIT_FAILURE); } } start_serving (); plugin_cleanup (); free (unixsocket); free (pidfile); if (random_fifo) { unlink (random_fifo); free (random_fifo); } if (random_fifo_dir) { rmdir (random_fifo_dir); free (random_fifo_dir); } exit (EXIT_SUCCESS); }