Document gotcha for --pidns
[untie.git] / cmdline.c
blobd4111e15f062b93169efc3636c0958f688b72ed9
1 /*
2 File autogenerated by gengetopt version 2.19.1
3 generated with the following command:
4 gengetopt -uCOMMAND --set-version=0.4
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9 */
11 /* If we use autoconf. */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "getopt.h"
22 #include "cmdline.h"
24 const char *gengetopt_args_info_purpose = "";
26 const char *gengetopt_args_info_usage = "Usage: untie [OPTIONS]... [COMMAND]...";
28 const char *gengetopt_args_info_description = "";
30 const char *gengetopt_args_info_help[] = {
31 " -h, --help Print help and exit",
32 " -V, --version Print version and exit",
33 " --mount New mount namespace (default=off)",
34 " --uname New uname (default=off)",
35 " --ipc New IPC context (default=off)",
36 " --userns New user namespace (default=off)",
37 " --pidns New PID namespace (default=off)",
38 " --netns New network namespace (default=off)",
39 " --mask=MASK Pass user defined flag",
40 " --chroot=DIRECTORY Chroot in the specified directory",
41 " --uid=UID Change to the specified UID",
42 " --username=USERNAME Change to the specified username",
43 " --gid=GID Add the specified GID",
44 " --groupname=GROUP Add the specified group",
45 " --daemonize Run as daemon (default=off)",
46 " --alarm=SECONDS Set an alarm after the specified delay",
47 " --nice=NICE Run as the specified nice level",
48 " --sched=SCHED Use the specified (possibly RT) scheduling policy",
49 " --schedprio=PRIO Set the priority according to the given scheduler",
50 " --timeout-term=SECONDS Delay before sending a SIGTERM to the process",
51 " --timeout-kill=SECONDS Delay before sending a SIGKILL to the process",
55 static
56 void clear_given (struct gengetopt_args_info *args_info);
57 static
58 void clear_args (struct gengetopt_args_info *args_info);
60 static int
61 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);
63 static int
64 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
66 struct int_list
68 int arg;
69 char *orig;
70 struct int_list * next;
73 struct string_list
75 char * arg;
76 char *orig;
77 struct string_list * next;
80 static char *
81 gengetopt_strdup (const char *s);
83 static
84 void clear_given (struct gengetopt_args_info *args_info)
86 args_info->help_given = 0 ;
87 args_info->version_given = 0 ;
88 args_info->mount_given = 0 ;
89 args_info->uname_given = 0 ;
90 args_info->ipc_given = 0 ;
91 args_info->userns_given = 0 ;
92 args_info->pidns_given = 0 ;
93 args_info->netns_given = 0 ;
94 args_info->mask_given = 0 ;
95 args_info->chroot_given = 0 ;
96 args_info->uid_given = 0 ;
97 args_info->username_given = 0 ;
98 args_info->gid_given = 0 ;
99 args_info->groupname_given = 0 ;
100 args_info->daemonize_given = 0 ;
101 args_info->alarm_given = 0 ;
102 args_info->nice_given = 0 ;
103 args_info->sched_given = 0 ;
104 args_info->schedprio_given = 0 ;
105 args_info->timeout_term_given = 0 ;
106 args_info->timeout_kill_given = 0 ;
109 static
110 void clear_args (struct gengetopt_args_info *args_info)
112 args_info->mount_flag = 0;
113 args_info->uname_flag = 0;
114 args_info->ipc_flag = 0;
115 args_info->userns_flag = 0;
116 args_info->pidns_flag = 0;
117 args_info->netns_flag = 0;
118 args_info->mask_arg = NULL;
119 args_info->mask_orig = NULL;
120 args_info->chroot_arg = NULL;
121 args_info->chroot_orig = NULL;
122 args_info->uid_orig = NULL;
123 args_info->username_arg = NULL;
124 args_info->username_orig = NULL;
125 args_info->gid_arg = NULL;
126 args_info->gid_orig = NULL;
127 args_info->groupname_arg = NULL;
128 args_info->groupname_orig = NULL;
129 args_info->daemonize_flag = 0;
130 args_info->alarm_orig = NULL;
131 args_info->nice_orig = NULL;
132 args_info->sched_arg = NULL;
133 args_info->sched_orig = NULL;
134 args_info->schedprio_orig = NULL;
135 args_info->timeout_term_orig = NULL;
136 args_info->timeout_kill_orig = NULL;
140 static
141 void init_args_info(struct gengetopt_args_info *args_info)
143 args_info->help_help = gengetopt_args_info_help[0] ;
144 args_info->version_help = gengetopt_args_info_help[1] ;
145 args_info->mount_help = gengetopt_args_info_help[2] ;
146 args_info->uname_help = gengetopt_args_info_help[3] ;
147 args_info->ipc_help = gengetopt_args_info_help[4] ;
148 args_info->userns_help = gengetopt_args_info_help[5] ;
149 args_info->pidns_help = gengetopt_args_info_help[6] ;
150 args_info->netns_help = gengetopt_args_info_help[7] ;
151 args_info->mask_help = gengetopt_args_info_help[8] ;
152 args_info->mask_min = -1;
153 args_info->mask_max = -1;
154 args_info->chroot_help = gengetopt_args_info_help[9] ;
155 args_info->uid_help = gengetopt_args_info_help[10] ;
156 args_info->username_help = gengetopt_args_info_help[11] ;
157 args_info->gid_help = gengetopt_args_info_help[12] ;
158 args_info->gid_min = -1;
159 args_info->gid_max = -1;
160 args_info->groupname_help = gengetopt_args_info_help[13] ;
161 args_info->groupname_min = -1;
162 args_info->groupname_max = -1;
163 args_info->daemonize_help = gengetopt_args_info_help[14] ;
164 args_info->alarm_help = gengetopt_args_info_help[15] ;
165 args_info->nice_help = gengetopt_args_info_help[16] ;
166 args_info->sched_help = gengetopt_args_info_help[17] ;
167 args_info->schedprio_help = gengetopt_args_info_help[18] ;
168 args_info->timeout_term_help = gengetopt_args_info_help[19] ;
169 args_info->timeout_kill_help = gengetopt_args_info_help[20] ;
173 void
174 cmdline_parser_print_version (void)
176 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
179 void
180 cmdline_parser_print_help (void)
182 int i = 0;
183 cmdline_parser_print_version ();
185 if (strlen(gengetopt_args_info_purpose) > 0)
186 printf("\n%s\n", gengetopt_args_info_purpose);
188 printf("\n%s\n\n", gengetopt_args_info_usage);
190 if (strlen(gengetopt_args_info_description) > 0)
191 printf("%s\n", gengetopt_args_info_description);
193 while (gengetopt_args_info_help[i])
194 printf("%s\n", gengetopt_args_info_help[i++]);
197 void
198 cmdline_parser_init (struct gengetopt_args_info *args_info)
200 clear_given (args_info);
201 clear_args (args_info);
202 init_args_info (args_info);
204 args_info->inputs = NULL;
205 args_info->inputs_num = 0;
208 static void
209 cmdline_parser_release (struct gengetopt_args_info *args_info)
212 unsigned int i;
213 if (args_info->mask_arg)
215 for (i = 0; i < args_info->mask_given; ++i)
217 if (args_info->mask_orig [i])
219 free (args_info->mask_orig [i]); /* free previous argument */
220 args_info->mask_orig [i] = 0;
223 free (args_info->mask_arg); /* free previous argument */
224 args_info->mask_arg = 0;
225 free (args_info->mask_orig); /* free previous argument */
226 args_info->mask_orig = 0;
228 if (args_info->chroot_arg)
230 free (args_info->chroot_arg); /* free previous argument */
231 args_info->chroot_arg = 0;
233 if (args_info->chroot_orig)
235 free (args_info->chroot_orig); /* free previous argument */
236 args_info->chroot_orig = 0;
238 if (args_info->uid_orig)
240 free (args_info->uid_orig); /* free previous argument */
241 args_info->uid_orig = 0;
243 if (args_info->username_arg)
245 free (args_info->username_arg); /* free previous argument */
246 args_info->username_arg = 0;
248 if (args_info->username_orig)
250 free (args_info->username_orig); /* free previous argument */
251 args_info->username_orig = 0;
253 if (args_info->gid_arg)
255 for (i = 0; i < args_info->gid_given; ++i)
257 if (args_info->gid_orig [i])
259 free (args_info->gid_orig [i]); /* free previous argument */
260 args_info->gid_orig [i] = 0;
263 free (args_info->gid_arg); /* free previous argument */
264 args_info->gid_arg = 0;
265 free (args_info->gid_orig); /* free previous argument */
266 args_info->gid_orig = 0;
268 if (args_info->groupname_arg)
270 for (i = 0; i < args_info->groupname_given; ++i)
272 if (args_info->groupname_arg [i])
274 free (args_info->groupname_arg [i]); /* free previous argument */
275 args_info->groupname_arg [i] = 0;
277 if (args_info->groupname_orig [i])
279 free (args_info->groupname_orig [i]); /* free previous argument */
280 args_info->groupname_orig [i] = 0;
283 if (args_info->groupname_arg [0])
284 free (args_info->groupname_arg [0]); /* free default string */
285 free (args_info->groupname_arg); /* free previous argument */
286 args_info->groupname_arg = 0;
287 free (args_info->groupname_orig); /* free previous argument */
288 args_info->groupname_orig = 0;
290 if (args_info->alarm_orig)
292 free (args_info->alarm_orig); /* free previous argument */
293 args_info->alarm_orig = 0;
295 if (args_info->nice_orig)
297 free (args_info->nice_orig); /* free previous argument */
298 args_info->nice_orig = 0;
300 if (args_info->sched_arg)
302 free (args_info->sched_arg); /* free previous argument */
303 args_info->sched_arg = 0;
305 if (args_info->sched_orig)
307 free (args_info->sched_orig); /* free previous argument */
308 args_info->sched_orig = 0;
310 if (args_info->schedprio_orig)
312 free (args_info->schedprio_orig); /* free previous argument */
313 args_info->schedprio_orig = 0;
315 if (args_info->timeout_term_orig)
317 free (args_info->timeout_term_orig); /* free previous argument */
318 args_info->timeout_term_orig = 0;
320 if (args_info->timeout_kill_orig)
322 free (args_info->timeout_kill_orig); /* free previous argument */
323 args_info->timeout_kill_orig = 0;
326 for (i = 0; i < args_info->inputs_num; ++i)
327 free (args_info->inputs [i]);
329 if (args_info->inputs_num)
330 free (args_info->inputs);
332 clear_given (args_info);
336 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
338 FILE *outfile;
339 int i = 0;
341 outfile = fopen(filename, "w");
343 if (!outfile)
345 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
346 return EXIT_FAILURE;
349 if (args_info->help_given) {
350 fprintf(outfile, "%s\n", "help");
352 if (args_info->version_given) {
353 fprintf(outfile, "%s\n", "version");
355 if (args_info->mount_given) {
356 fprintf(outfile, "%s\n", "mount");
358 if (args_info->uname_given) {
359 fprintf(outfile, "%s\n", "uname");
361 if (args_info->ipc_given) {
362 fprintf(outfile, "%s\n", "ipc");
364 if (args_info->userns_given) {
365 fprintf(outfile, "%s\n", "userns");
367 if (args_info->pidns_given) {
368 fprintf(outfile, "%s\n", "pidns");
370 if (args_info->netns_given) {
371 fprintf(outfile, "%s\n", "netns");
373 if (args_info->mask_orig)
375 for (i = 0; i < args_info->mask_given; ++i)
377 if (args_info->mask_orig [i])
379 fprintf(outfile, "%s=\"%s\"\n", "mask", args_info->mask_orig [i]);
383 if (args_info->chroot_given) {
384 if (args_info->chroot_orig) {
385 fprintf(outfile, "%s=\"%s\"\n", "chroot", args_info->chroot_orig);
386 } else {
387 fprintf(outfile, "%s\n", "chroot");
390 if (args_info->uid_given) {
391 if (args_info->uid_orig) {
392 fprintf(outfile, "%s=\"%s\"\n", "uid", args_info->uid_orig);
393 } else {
394 fprintf(outfile, "%s\n", "uid");
397 if (args_info->username_given) {
398 if (args_info->username_orig) {
399 fprintf(outfile, "%s=\"%s\"\n", "username", args_info->username_orig);
400 } else {
401 fprintf(outfile, "%s\n", "username");
404 if (args_info->gid_orig)
406 for (i = 0; i < args_info->gid_given; ++i)
408 if (args_info->gid_orig [i])
410 fprintf(outfile, "%s=\"%s\"\n", "gid", args_info->gid_orig [i]);
414 if (args_info->groupname_orig)
416 for (i = 0; i < args_info->groupname_given; ++i)
418 if (args_info->groupname_orig [i])
420 fprintf(outfile, "%s=\"%s\"\n", "groupname", args_info->groupname_orig [i]);
424 if (args_info->daemonize_given) {
425 fprintf(outfile, "%s\n", "daemonize");
427 if (args_info->alarm_given) {
428 if (args_info->alarm_orig) {
429 fprintf(outfile, "%s=\"%s\"\n", "alarm", args_info->alarm_orig);
430 } else {
431 fprintf(outfile, "%s\n", "alarm");
434 if (args_info->nice_given) {
435 if (args_info->nice_orig) {
436 fprintf(outfile, "%s=\"%s\"\n", "nice", args_info->nice_orig);
437 } else {
438 fprintf(outfile, "%s\n", "nice");
441 if (args_info->sched_given) {
442 if (args_info->sched_orig) {
443 fprintf(outfile, "%s=\"%s\"\n", "sched", args_info->sched_orig);
444 } else {
445 fprintf(outfile, "%s\n", "sched");
448 if (args_info->schedprio_given) {
449 if (args_info->schedprio_orig) {
450 fprintf(outfile, "%s=\"%s\"\n", "schedprio", args_info->schedprio_orig);
451 } else {
452 fprintf(outfile, "%s\n", "schedprio");
455 if (args_info->timeout_term_given) {
456 if (args_info->timeout_term_orig) {
457 fprintf(outfile, "%s=\"%s\"\n", "timeout-term", args_info->timeout_term_orig);
458 } else {
459 fprintf(outfile, "%s\n", "timeout-term");
462 if (args_info->timeout_kill_given) {
463 if (args_info->timeout_kill_orig) {
464 fprintf(outfile, "%s=\"%s\"\n", "timeout-kill", args_info->timeout_kill_orig);
465 } else {
466 fprintf(outfile, "%s\n", "timeout-kill");
470 fclose (outfile);
472 i = EXIT_SUCCESS;
473 return i;
476 void
477 cmdline_parser_free (struct gengetopt_args_info *args_info)
479 cmdline_parser_release (args_info);
483 /* gengetopt_strdup() */
484 /* strdup.c replacement of strdup, which is not standard */
485 char *
486 gengetopt_strdup (const char *s)
488 char *result = NULL;
489 if (!s)
490 return result;
492 result = (char*)malloc(strlen(s) + 1);
493 if (result == (char*)0)
494 return (char*)0;
495 strcpy(result, s);
496 return result;
499 static char *
500 get_multiple_arg_token(const char *arg)
502 char *tok, *ret;
503 size_t len, num_of_escape;
504 int i, j;
506 if (!arg)
507 return NULL;
509 tok = strchr (arg, ',');
510 num_of_escape = 0;
512 /* make sure it is not escaped */
513 while (tok)
515 if (*(tok-1) == '\\')
517 /* find the next one */
518 tok = strchr (tok+1, ',');
519 ++num_of_escape;
521 else
522 break;
525 if (tok)
526 len = (size_t)(tok - arg + 1);
527 else
528 len = strlen (arg) + 1;
530 len -= num_of_escape;
532 ret = (char *) malloc (len);
534 i = 0;
535 j = 0;
536 while (arg[i] && (j < len-1))
538 if (arg[i] == '\\')
539 ++i;
541 ret[j++] = arg[i++];
544 ret[len-1] = '\0';
546 return ret;
549 static char *
550 get_multiple_arg_token_next(const char *arg)
552 char *tok;
554 if (!arg)
555 return NULL;
557 tok = strchr (arg, ',');
559 /* make sure it is not escaped */
560 while (tok)
562 if (*(tok-1) == '\\')
564 /* find the next one */
565 tok = strchr (tok+1, ',');
567 else
568 break;
571 if (! tok || strlen(tok) == 1)
572 return 0;
574 return tok+1;
577 static int
578 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc);
581 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc)
583 int error = 0;
585 if (option_given && ! (min < 0 && max < 0))
587 if (min >= 0 && max >= 0)
589 if (min == max)
591 /* specific occurrences */
592 if (option_given != min)
594 fprintf (stderr, "%s: %s option occurrences must be %d\n",
595 prog_name, option_desc, min);
596 error = 1;
599 else if (option_given < min
600 || option_given > max)
602 /* range occurrences */
603 fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
604 prog_name, option_desc, min, max);
605 error = 1;
608 else if (min >= 0)
610 /* at least check */
611 if (option_given < min)
613 fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
614 prog_name, option_desc, min);
615 error = 1;
618 else if (max >= 0)
620 /* at most check */
621 if (option_given > max)
623 fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
624 prog_name, option_desc, max);
625 error = 1;
630 return error;
633 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
635 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
639 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
641 int result;
643 result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
645 if (result == EXIT_FAILURE)
647 cmdline_parser_free (args_info);
648 exit (EXIT_FAILURE);
651 return result;
655 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
657 int result = EXIT_SUCCESS;
659 if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
660 result = EXIT_FAILURE;
662 if (result == EXIT_FAILURE)
664 cmdline_parser_free (args_info);
665 exit (EXIT_FAILURE);
668 return result;
672 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
674 int error = 0;
676 /* checks for required options */
677 if (check_multiple_option_occurrences(prog_name, args_info->mask_given, args_info->mask_min, args_info->mask_max, "'--mask'"))
678 error = 1;
680 if (check_multiple_option_occurrences(prog_name, args_info->gid_given, args_info->gid_min, args_info->gid_max, "'--gid'"))
681 error = 1;
683 if (check_multiple_option_occurrences(prog_name, args_info->groupname_given, args_info->groupname_min, args_info->groupname_max, "'--groupname'"))
684 error = 1;
687 /* checks for dependences among options */
689 return error;
693 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
695 int c; /* Character of the parsed option. */
696 char *multi_token, *multi_next; /* for multiple options */
698 int i; /* Counter */
700 struct int_list * mask_list = NULL,* mask_new = NULL;
701 struct int_list * gid_list = NULL,* gid_new = NULL;
702 struct string_list * groupname_list = NULL,* groupname_new = NULL;
703 int error = 0;
704 struct gengetopt_args_info local_args_info;
706 if (initialize)
707 cmdline_parser_init (args_info);
709 cmdline_parser_init (&local_args_info);
711 optarg = 0;
712 optind = 0;
713 opterr = 1;
714 optopt = '?';
716 while (1)
718 int option_index = 0;
719 char *stop_char;
721 static struct option long_options[] = {
722 { "help", 0, NULL, 'h' },
723 { "version", 0, NULL, 'V' },
724 { "mount", 0, NULL, 0 },
725 { "uname", 0, NULL, 0 },
726 { "ipc", 0, NULL, 0 },
727 { "userns", 0, NULL, 0 },
728 { "pidns", 0, NULL, 0 },
729 { "netns", 0, NULL, 0 },
730 { "mask", 1, NULL, 0 },
731 { "chroot", 1, NULL, 0 },
732 { "uid", 1, NULL, 0 },
733 { "username", 1, NULL, 0 },
734 { "gid", 1, NULL, 0 },
735 { "groupname", 1, NULL, 0 },
736 { "daemonize", 0, NULL, 0 },
737 { "alarm", 1, NULL, 0 },
738 { "nice", 1, NULL, 0 },
739 { "sched", 1, NULL, 0 },
740 { "schedprio", 1, NULL, 0 },
741 { "timeout-term", 1, NULL, 0 },
742 { "timeout-kill", 1, NULL, 0 },
743 { NULL, 0, NULL, 0 }
746 stop_char = 0;
747 c = getopt_long (argc, argv, "hV", long_options, &option_index);
749 if (c == -1) break; /* Exit from `while (1)' loop. */
751 switch (c)
753 case 'h': /* Print help and exit. */
754 cmdline_parser_print_help ();
755 cmdline_parser_free (&local_args_info);
756 exit (EXIT_SUCCESS);
758 case 'V': /* Print version and exit. */
759 cmdline_parser_print_version ();
760 cmdline_parser_free (&local_args_info);
761 exit (EXIT_SUCCESS);
764 case 0: /* Long option with no short option */
765 /* New mount namespace. */
766 if (strcmp (long_options[option_index].name, "mount") == 0)
768 if (local_args_info.mount_given)
770 fprintf (stderr, "%s: `--mount' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
771 goto failure;
773 if (args_info->mount_given && ! override)
774 continue;
775 local_args_info.mount_given = 1;
776 args_info->mount_given = 1;
777 args_info->mount_flag = !(args_info->mount_flag);
779 /* New uname. */
780 else if (strcmp (long_options[option_index].name, "uname") == 0)
782 if (local_args_info.uname_given)
784 fprintf (stderr, "%s: `--uname' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
785 goto failure;
787 if (args_info->uname_given && ! override)
788 continue;
789 local_args_info.uname_given = 1;
790 args_info->uname_given = 1;
791 args_info->uname_flag = !(args_info->uname_flag);
793 /* New IPC context. */
794 else if (strcmp (long_options[option_index].name, "ipc") == 0)
796 if (local_args_info.ipc_given)
798 fprintf (stderr, "%s: `--ipc' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
799 goto failure;
801 if (args_info->ipc_given && ! override)
802 continue;
803 local_args_info.ipc_given = 1;
804 args_info->ipc_given = 1;
805 args_info->ipc_flag = !(args_info->ipc_flag);
807 /* New user namespace. */
808 else if (strcmp (long_options[option_index].name, "userns") == 0)
810 if (local_args_info.userns_given)
812 fprintf (stderr, "%s: `--userns' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
813 goto failure;
815 if (args_info->userns_given && ! override)
816 continue;
817 local_args_info.userns_given = 1;
818 args_info->userns_given = 1;
819 args_info->userns_flag = !(args_info->userns_flag);
821 /* New PID namespace. */
822 else if (strcmp (long_options[option_index].name, "pidns") == 0)
824 if (local_args_info.pidns_given)
826 fprintf (stderr, "%s: `--pidns' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
827 goto failure;
829 if (args_info->pidns_given && ! override)
830 continue;
831 local_args_info.pidns_given = 1;
832 args_info->pidns_given = 1;
833 args_info->pidns_flag = !(args_info->pidns_flag);
835 /* New network namespace. */
836 else if (strcmp (long_options[option_index].name, "netns") == 0)
838 if (local_args_info.netns_given)
840 fprintf (stderr, "%s: `--netns' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
841 goto failure;
843 if (args_info->netns_given && ! override)
844 continue;
845 local_args_info.netns_given = 1;
846 args_info->netns_given = 1;
847 args_info->netns_flag = !(args_info->netns_flag);
849 /* Pass user defined flag. */
850 else if (strcmp (long_options[option_index].name, "mask") == 0)
852 local_args_info.mask_given++;
854 multi_token = get_multiple_arg_token(optarg);
855 multi_next = get_multiple_arg_token_next (optarg);
857 while (1)
859 mask_new = (struct int_list *) malloc (sizeof (struct int_list));
860 mask_new->next = mask_list;
861 mask_list = mask_new;
862 mask_new->arg = strtol (multi_token, &stop_char, 0);
863 if (!(stop_char && *stop_char == '\0')) {
864 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], multi_token);
865 goto failure;
867 mask_new->orig = multi_token;
869 if (multi_next)
871 multi_token = get_multiple_arg_token(multi_next);
872 multi_next = get_multiple_arg_token_next (multi_next);
873 local_args_info.mask_given++;
875 else
876 break;
878 break;
880 /* Chroot in the specified directory. */
881 else if (strcmp (long_options[option_index].name, "chroot") == 0)
883 if (local_args_info.chroot_given)
885 fprintf (stderr, "%s: `--chroot' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
886 goto failure;
888 if (args_info->chroot_given && ! override)
889 continue;
890 local_args_info.chroot_given = 1;
891 args_info->chroot_given = 1;
892 if (args_info->chroot_arg)
893 free (args_info->chroot_arg); /* free previous string */
894 args_info->chroot_arg = gengetopt_strdup (optarg);
895 if (args_info->chroot_orig)
896 free (args_info->chroot_orig); /* free previous string */
897 args_info->chroot_orig = gengetopt_strdup (optarg);
899 /* Change to the specified UID. */
900 else if (strcmp (long_options[option_index].name, "uid") == 0)
902 if (local_args_info.uid_given)
904 fprintf (stderr, "%s: `--uid' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
905 goto failure;
907 if (args_info->uid_given && ! override)
908 continue;
909 local_args_info.uid_given = 1;
910 args_info->uid_given = 1;
911 args_info->uid_arg = strtol (optarg, &stop_char, 0);
912 if (!(stop_char && *stop_char == '\0')) {
913 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
914 goto failure;
916 if (args_info->uid_orig)
917 free (args_info->uid_orig); /* free previous string */
918 args_info->uid_orig = gengetopt_strdup (optarg);
920 /* Change to the specified username. */
921 else if (strcmp (long_options[option_index].name, "username") == 0)
923 if (local_args_info.username_given)
925 fprintf (stderr, "%s: `--username' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
926 goto failure;
928 if (args_info->username_given && ! override)
929 continue;
930 local_args_info.username_given = 1;
931 args_info->username_given = 1;
932 if (args_info->username_arg)
933 free (args_info->username_arg); /* free previous string */
934 args_info->username_arg = gengetopt_strdup (optarg);
935 if (args_info->username_orig)
936 free (args_info->username_orig); /* free previous string */
937 args_info->username_orig = gengetopt_strdup (optarg);
939 /* Add the specified GID. */
940 else if (strcmp (long_options[option_index].name, "gid") == 0)
942 local_args_info.gid_given++;
944 multi_token = get_multiple_arg_token(optarg);
945 multi_next = get_multiple_arg_token_next (optarg);
947 while (1)
949 gid_new = (struct int_list *) malloc (sizeof (struct int_list));
950 gid_new->next = gid_list;
951 gid_list = gid_new;
952 gid_new->arg = strtol (multi_token, &stop_char, 0);
953 if (!(stop_char && *stop_char == '\0')) {
954 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], multi_token);
955 goto failure;
957 gid_new->orig = multi_token;
959 if (multi_next)
961 multi_token = get_multiple_arg_token(multi_next);
962 multi_next = get_multiple_arg_token_next (multi_next);
963 local_args_info.gid_given++;
965 else
966 break;
968 break;
970 /* Add the specified group. */
971 else if (strcmp (long_options[option_index].name, "groupname") == 0)
973 local_args_info.groupname_given++;
975 multi_token = get_multiple_arg_token(optarg);
976 multi_next = get_multiple_arg_token_next (optarg);
978 while (1)
980 groupname_new = (struct string_list *) malloc (sizeof (struct string_list));
981 groupname_new->next = groupname_list;
982 groupname_list = groupname_new;
983 groupname_new->arg = gengetopt_strdup (multi_token);
984 groupname_new->orig = multi_token;
986 if (multi_next)
988 multi_token = get_multiple_arg_token(multi_next);
989 multi_next = get_multiple_arg_token_next (multi_next);
990 local_args_info.groupname_given++;
992 else
993 break;
995 break;
997 /* Run as daemon. */
998 else if (strcmp (long_options[option_index].name, "daemonize") == 0)
1000 if (local_args_info.daemonize_given)
1002 fprintf (stderr, "%s: `--daemonize' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1003 goto failure;
1005 if (args_info->daemonize_given && ! override)
1006 continue;
1007 local_args_info.daemonize_given = 1;
1008 args_info->daemonize_given = 1;
1009 args_info->daemonize_flag = !(args_info->daemonize_flag);
1011 /* Set an alarm after the specified delay. */
1012 else if (strcmp (long_options[option_index].name, "alarm") == 0)
1014 if (local_args_info.alarm_given)
1016 fprintf (stderr, "%s: `--alarm' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1017 goto failure;
1019 if (args_info->alarm_given && ! override)
1020 continue;
1021 local_args_info.alarm_given = 1;
1022 args_info->alarm_given = 1;
1023 args_info->alarm_arg = strtol (optarg, &stop_char, 0);
1024 if (!(stop_char && *stop_char == '\0')) {
1025 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1026 goto failure;
1028 if (args_info->alarm_orig)
1029 free (args_info->alarm_orig); /* free previous string */
1030 args_info->alarm_orig = gengetopt_strdup (optarg);
1032 /* Run as the specified nice level. */
1033 else if (strcmp (long_options[option_index].name, "nice") == 0)
1035 if (local_args_info.nice_given)
1037 fprintf (stderr, "%s: `--nice' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1038 goto failure;
1040 if (args_info->nice_given && ! override)
1041 continue;
1042 local_args_info.nice_given = 1;
1043 args_info->nice_given = 1;
1044 args_info->nice_arg = strtol (optarg, &stop_char, 0);
1045 if (!(stop_char && *stop_char == '\0')) {
1046 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1047 goto failure;
1049 if (args_info->nice_orig)
1050 free (args_info->nice_orig); /* free previous string */
1051 args_info->nice_orig = gengetopt_strdup (optarg);
1053 /* Use the specified (possibly RT) scheduling policy. */
1054 else if (strcmp (long_options[option_index].name, "sched") == 0)
1056 if (local_args_info.sched_given)
1058 fprintf (stderr, "%s: `--sched' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1059 goto failure;
1061 if (args_info->sched_given && ! override)
1062 continue;
1063 local_args_info.sched_given = 1;
1064 args_info->sched_given = 1;
1065 if (args_info->sched_arg)
1066 free (args_info->sched_arg); /* free previous string */
1067 args_info->sched_arg = gengetopt_strdup (optarg);
1068 if (args_info->sched_orig)
1069 free (args_info->sched_orig); /* free previous string */
1070 args_info->sched_orig = gengetopt_strdup (optarg);
1072 /* Set the priority according to the given scheduler. */
1073 else if (strcmp (long_options[option_index].name, "schedprio") == 0)
1075 if (local_args_info.schedprio_given)
1077 fprintf (stderr, "%s: `--schedprio' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1078 goto failure;
1080 if (args_info->schedprio_given && ! override)
1081 continue;
1082 local_args_info.schedprio_given = 1;
1083 args_info->schedprio_given = 1;
1084 args_info->schedprio_arg = strtol (optarg, &stop_char, 0);
1085 if (!(stop_char && *stop_char == '\0')) {
1086 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1087 goto failure;
1089 if (args_info->schedprio_orig)
1090 free (args_info->schedprio_orig); /* free previous string */
1091 args_info->schedprio_orig = gengetopt_strdup (optarg);
1093 /* Delay before sending a SIGTERM to the process. */
1094 else if (strcmp (long_options[option_index].name, "timeout-term") == 0)
1096 if (local_args_info.timeout_term_given)
1098 fprintf (stderr, "%s: `--timeout-term' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1099 goto failure;
1101 if (args_info->timeout_term_given && ! override)
1102 continue;
1103 local_args_info.timeout_term_given = 1;
1104 args_info->timeout_term_given = 1;
1105 args_info->timeout_term_arg = strtod (optarg, &stop_char);
1106 if (!(stop_char && *stop_char == '\0')) {
1107 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1108 goto failure;
1110 if (args_info->timeout_term_orig)
1111 free (args_info->timeout_term_orig); /* free previous string */
1112 args_info->timeout_term_orig = gengetopt_strdup (optarg);
1114 /* Delay before sending a SIGKILL to the process. */
1115 else if (strcmp (long_options[option_index].name, "timeout-kill") == 0)
1117 if (local_args_info.timeout_kill_given)
1119 fprintf (stderr, "%s: `--timeout-kill' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1120 goto failure;
1122 if (args_info->timeout_kill_given && ! override)
1123 continue;
1124 local_args_info.timeout_kill_given = 1;
1125 args_info->timeout_kill_given = 1;
1126 args_info->timeout_kill_arg = strtod (optarg, &stop_char);
1127 if (!(stop_char && *stop_char == '\0')) {
1128 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1129 goto failure;
1131 if (args_info->timeout_kill_orig)
1132 free (args_info->timeout_kill_orig); /* free previous string */
1133 args_info->timeout_kill_orig = gengetopt_strdup (optarg);
1136 break;
1137 case '?': /* Invalid option. */
1138 /* `getopt_long' already printed an error message. */
1139 goto failure;
1141 default: /* bug: option not considered. */
1142 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1143 abort ();
1144 } /* switch */
1145 } /* while */
1148 if (local_args_info.mask_given && mask_list)
1150 struct int_list *tmp;
1151 args_info->mask_arg = (int *) realloc (args_info->mask_arg, (args_info->mask_given + local_args_info.mask_given) * sizeof (int));
1152 args_info->mask_orig = (char **) realloc (args_info->mask_orig, (args_info->mask_given + local_args_info.mask_given) * sizeof (char *));
1153 for (i = (local_args_info.mask_given - 1); i >= 0; --i)
1155 tmp = mask_list;
1156 args_info->mask_arg [i + args_info->mask_given] = mask_list->arg;
1157 args_info->mask_orig [i + args_info->mask_given] = mask_list->orig;
1158 mask_list = mask_list->next;
1159 free (tmp);
1163 if (local_args_info.gid_given && gid_list)
1165 struct int_list *tmp;
1166 args_info->gid_arg = (int *) realloc (args_info->gid_arg, (args_info->gid_given + local_args_info.gid_given) * sizeof (int));
1167 args_info->gid_orig = (char **) realloc (args_info->gid_orig, (args_info->gid_given + local_args_info.gid_given) * sizeof (char *));
1168 for (i = (local_args_info.gid_given - 1); i >= 0; --i)
1170 tmp = gid_list;
1171 args_info->gid_arg [i + args_info->gid_given] = gid_list->arg;
1172 args_info->gid_orig [i + args_info->gid_given] = gid_list->orig;
1173 gid_list = gid_list->next;
1174 free (tmp);
1178 if (local_args_info.groupname_given && groupname_list)
1180 struct string_list *tmp;
1181 args_info->groupname_arg = (char * *) realloc (args_info->groupname_arg, (args_info->groupname_given + local_args_info.groupname_given) * sizeof (char *));
1182 args_info->groupname_orig = (char **) realloc (args_info->groupname_orig, (args_info->groupname_given + local_args_info.groupname_given) * sizeof (char *));
1183 for (i = (local_args_info.groupname_given - 1); i >= 0; --i)
1185 tmp = groupname_list;
1186 args_info->groupname_arg [i + args_info->groupname_given] = groupname_list->arg;
1187 args_info->groupname_orig [i + args_info->groupname_given] = groupname_list->orig;
1188 groupname_list = groupname_list->next;
1189 free (tmp);
1194 args_info->mask_given += local_args_info.mask_given;
1195 local_args_info.mask_given = 0;
1196 args_info->gid_given += local_args_info.gid_given;
1197 local_args_info.gid_given = 0;
1198 args_info->groupname_given += local_args_info.groupname_given;
1199 local_args_info.groupname_given = 0;
1201 if (check_required)
1203 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
1206 cmdline_parser_release (&local_args_info);
1208 if ( error )
1209 return (EXIT_FAILURE);
1211 if (optind < argc)
1213 int i = 0 ;
1214 int found_prog_name = 0;
1215 /* whether program name, i.e., argv[0], is in the remaining args
1216 (this may happen with some implementations of getopt,
1217 but surely not with the one included by gengetopt) */
1219 i = optind;
1220 while (i < argc)
1221 if (argv[i++] == argv[0]) {
1222 found_prog_name = 1;
1223 break;
1225 i = 0;
1227 args_info->inputs_num = argc - optind - found_prog_name;
1228 args_info->inputs =
1229 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1230 while (optind < argc)
1231 if (argv[optind++] != argv[0])
1232 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
1235 return 0;
1237 failure:
1238 if (mask_list)
1240 struct int_list *tmp;
1241 while (mask_list)
1243 tmp = mask_list;
1244 free (mask_list->orig);
1245 mask_list = mask_list->next;
1246 free (tmp);
1249 if (gid_list)
1251 struct int_list *tmp;
1252 while (gid_list)
1254 tmp = gid_list;
1255 free (gid_list->orig);
1256 gid_list = gid_list->next;
1257 free (tmp);
1260 if (groupname_list)
1262 struct string_list *tmp;
1263 while (groupname_list)
1265 tmp = groupname_list;
1266 free (groupname_list->arg);
1267 free (groupname_list->orig);
1268 groupname_list = groupname_list->next;
1269 free (tmp);
1273 cmdline_parser_release (&local_args_info);
1274 return (EXIT_FAILURE);