mdmfs.c (d69f5dee040f105728160e6e1569a880c1fbf4aa) mdmfs.c (94ddc5afe95a42ea10f16fc1747e2a87feb01ca5)
1/*
2 * Copyright (c) 2001 Dima Dorfman.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 72 unchanged lines hidden (view full) ---

81static void do_mount(const char *, const char *);
82static void do_mtptsetup(const char *, struct mtpt_info *);
83static void do_newfs(const char *);
84static void extract_ugid(const char *, struct mtpt_info *);
85static int run(int *, const char *, ...) __printflike(2, 3);
86static void usage(void);
87
88int
1/*
2 * Copyright (c) 2001 Dima Dorfman.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 72 unchanged lines hidden (view full) ---

81static void do_mount(const char *, const char *);
82static void do_mtptsetup(const char *, struct mtpt_info *);
83static void do_newfs(const char *);
84static void extract_ugid(const char *, struct mtpt_info *);
85static int run(int *, const char *, ...) __printflike(2, 3);
86static void usage(void);
87
88int
89main(int ac, char **av)
89main(int argc, char **argv)
90{
91 struct mtpt_info mi; /* Mountpoint info. */
92 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
93 *mount_arg;
94 enum md_types mdtype; /* The type of our memory disk. */
95 bool have_mdtype;
96 bool detach, softdep, autounit;
97 char *mtpoint, *unitstr;

--- 12 unchanged lines hidden (view full) ---

110 * respective programs without modification. I.e., we may not
111 * receive any command-line options which will caused them to
112 * be modified.
113 */
114 mdconfig_arg = strdup("");
115 newfs_arg = strdup("");
116 mount_arg = strdup("");
117
90{
91 struct mtpt_info mi; /* Mountpoint info. */
92 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
93 *mount_arg;
94 enum md_types mdtype; /* The type of our memory disk. */
95 bool have_mdtype;
96 bool detach, softdep, autounit;
97 char *mtpoint, *unitstr;

--- 12 unchanged lines hidden (view full) ---

110 * respective programs without modification. I.e., we may not
111 * receive any command-line options which will caused them to
112 * be modified.
113 */
114 mdconfig_arg = strdup("");
115 newfs_arg = strdup("");
116 mount_arg = strdup("");
117
118 while ((ch = getopt(ac, av,
118 while ((ch = getopt(argc, argv,
119 "a:b:c:Dd:e:F:f:hi:LMm:Nn:O:o:p:Ss:t:w:X")) != -1)
120 switch (ch) {
121 case 'a':
122 argappend(&newfs_arg, "-a %s", optarg);
123 break;
124 case 'b':
125 argappend(&newfs_arg, "-b %s", optarg);
126 break;

--- 66 unchanged lines hidden (view full) ---

193 extract_ugid(optarg, &mi);
194 break;
195 case 'X':
196 debug = true;
197 break;
198 default:
199 usage();
200 }
119 "a:b:c:Dd:e:F:f:hi:LMm:Nn:O:o:p:Ss:t:w:X")) != -1)
120 switch (ch) {
121 case 'a':
122 argappend(&newfs_arg, "-a %s", optarg);
123 break;
124 case 'b':
125 argappend(&newfs_arg, "-b %s", optarg);
126 break;

--- 66 unchanged lines hidden (view full) ---

193 extract_ugid(optarg, &mi);
194 break;
195 case 'X':
196 debug = true;
197 break;
198 default:
199 usage();
200 }
201 ac -= optind;
202 av += optind;
203 if (ac < 2)
201 argc -= optind;
202 argv += optind;
203 if (argc < 2)
204 usage();
205
206 /* Derive 'unit' (global). */
204 usage();
205
206 /* Derive 'unit' (global). */
207 unitstr = av[0];
207 unitstr = argv[0];
208 if (strncmp(unitstr, "/dev/", 5) == 0)
209 unitstr += 5;
210 if (strncmp(unitstr, mdname, mdnamelen) == 0)
211 unitstr += mdnamelen;
212 if (*unitstr == '\0') {
213 autounit = true;
214 unit = -1;
215 } else {
216 unit = strtoul(unitstr, &p, 10);
217 if ((unsigned)unit == ULONG_MAX || *p != '\0')
218 errx(1, "bad device unit: %s", unitstr);
219 }
220
208 if (strncmp(unitstr, "/dev/", 5) == 0)
209 unitstr += 5;
210 if (strncmp(unitstr, mdname, mdnamelen) == 0)
211 unitstr += mdnamelen;
212 if (*unitstr == '\0') {
213 autounit = true;
214 unit = -1;
215 } else {
216 unit = strtoul(unitstr, &p, 10);
217 if ((unsigned)unit == ULONG_MAX || *p != '\0')
218 errx(1, "bad device unit: %s", unitstr);
219 }
220
221 mtpoint = av[1];
221 mtpoint = argv[1];
222 if (!have_mdtype)
223 mdtype = MD_SWAP;
224 if (softdep)
225 argappend(&newfs_arg, "-U");
226
227 /* Do the work. */
228 if (detach && !autounit)
229 do_mdconfig_detach();

--- 305 unchanged lines hidden (view full) ---

535 * program to run. The return value is the return code of the process
536 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
537 * the program spawned (i.e., you can read from ofd and get the output
538 * of the program).
539 */
540static int
541run(int *ofd, const char *cmdline, ...)
542{
222 if (!have_mdtype)
223 mdtype = MD_SWAP;
224 if (softdep)
225 argappend(&newfs_arg, "-U");
226
227 /* Do the work. */
228 if (detach && !autounit)
229 do_mdconfig_detach();

--- 305 unchanged lines hidden (view full) ---

535 * program to run. The return value is the return code of the process
536 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
537 * the program spawned (i.e., you can read from ofd and get the output
538 * of the program).
539 */
540static int
541run(int *ofd, const char *cmdline, ...)
542{
543 char **av, **avp; /* Result of splitting 'cmd'. */
544 int ac;
543 char **argv, **argvp; /* Result of splitting 'cmd'. */
544 int argc;
545 char *cmd; /* Expansion of 'cmdline'. */
546 int pid, status; /* Child info. */
547 int pfd[2]; /* Pipe to the child. */
548 int nfd; /* Null (/dev/null) file descriptor. */
549 bool dup2dn; /* Dup /dev/null to stdout? */
550 va_list ap;
551 char *p;
552 int rv, i;
553
554 dup2dn = true;
555 va_start(ap, cmdline);
556 rv = vasprintf(&cmd, cmdline, ap);
557 if (rv == -1)
558 err(1, "vasprintf");
559 va_end(ap);
560
545 char *cmd; /* Expansion of 'cmdline'. */
546 int pid, status; /* Child info. */
547 int pfd[2]; /* Pipe to the child. */
548 int nfd; /* Null (/dev/null) file descriptor. */
549 bool dup2dn; /* Dup /dev/null to stdout? */
550 va_list ap;
551 char *p;
552 int rv, i;
553
554 dup2dn = true;
555 va_start(ap, cmdline);
556 rv = vasprintf(&cmd, cmdline, ap);
557 if (rv == -1)
558 err(1, "vasprintf");
559 va_end(ap);
560
561 /* Split up 'cmd' into 'av' for use with execve. */
562 for (ac = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
563 ac++; /* 'ac' generation loop. */
564 av = (char **)malloc(sizeof(*av) * (ac + 1));
565 assert(av != NULL);
566 for (p = cmd, avp = av; (*avp = strsep(&p, " ")) != NULL;)
567 if (**av != '\0')
568 if (++avp >= &av[ac]) {
569 *avp = NULL;
561 /* Split up 'cmd' into 'argv' for use with execve. */
562 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
563 argc++; /* 'argc' generation loop. */
564 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
565 assert(argv != NULL);
566 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
567 if (**argv != '\0')
568 if (++argvp >= &argv[argc]) {
569 *argvp = NULL;
570 break;
571 }
570 break;
571 }
572 assert(*av);
572 assert(*argv);
573
574 /* Make sure the above loop works as expected. */
575 if (debug) {
576 /*
577 * We can't, but should, use debugprintf here. First,
578 * it appends a trailing newline to the output, and
579 * second it prepends "DEBUG: " to the output. The
580 * former is a problem for this would-be first call,
581 * and the latter for the would-be call inside the
582 * loop.
583 */
584 (void)fprintf(stderr, "DEBUG: running:");
585 /* Should be equivilent to 'cmd' (before strsep, of course). */
573
574 /* Make sure the above loop works as expected. */
575 if (debug) {
576 /*
577 * We can't, but should, use debugprintf here. First,
578 * it appends a trailing newline to the output, and
579 * second it prepends "DEBUG: " to the output. The
580 * former is a problem for this would-be first call,
581 * and the latter for the would-be call inside the
582 * loop.
583 */
584 (void)fprintf(stderr, "DEBUG: running:");
585 /* Should be equivilent to 'cmd' (before strsep, of course). */
586 for (i = 0; av[i] != NULL; i++)
587 (void)fprintf(stderr, " %s", av[i]);
586 for (i = 0; argv[i] != NULL; i++)
587 (void)fprintf(stderr, " %s", argv[i]);
588 (void)fprintf(stderr, "\n");
589 }
590
591 /* Create a pipe if necessary and fork the helper program. */
592 if (ofd != NULL) {
593 if (pipe(&pfd[0]) == -1)
594 err(1, "pipe");
595 *ofd = pfd[0];

--- 16 unchanged lines hidden (view full) ---

612 err(1, "dup2");
613 if (dup2dn)
614 if (dup2(nfd, STDOUT_FILENO) < 0)
615 err(1, "dup2");
616 if (dup2(nfd, STDERR_FILENO) < 0)
617 err(1, "dup2");
618 }
619
588 (void)fprintf(stderr, "\n");
589 }
590
591 /* Create a pipe if necessary and fork the helper program. */
592 if (ofd != NULL) {
593 if (pipe(&pfd[0]) == -1)
594 err(1, "pipe");
595 *ofd = pfd[0];

--- 16 unchanged lines hidden (view full) ---

612 err(1, "dup2");
613 if (dup2dn)
614 if (dup2(nfd, STDOUT_FILENO) < 0)
615 err(1, "dup2");
616 if (dup2(nfd, STDERR_FILENO) < 0)
617 err(1, "dup2");
618 }
619
620 (void)execv(av[0], av);
621 warn("exec: %s", av[0]);
620 (void)execv(argv[0], argv);
621 warn("exec: %s", argv[0]);
622 _exit(-1);
623 case -1:
624 err(1, "fork");
625 }
626
627 free(cmd);
622 _exit(-1);
623 case -1:
624 err(1, "fork");
625 }
626
627 free(cmd);
628 free(av);
628 free(argv);
629 while (waitpid(pid, &status, 0) != pid)
630 ;
631 return (WEXITSTATUS(status));
632}
633
634static void
635usage(void)
636{
637
638 fprintf(stderr,
639"usage: %s [-DLMNSX] [-a maxcontig] [-b block-size] [-c cylinders]\n"
640"\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
641"\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
642"\t[-o mount-options] [-p permissions] [-s size] [-w user:group]\n"
643"\tmd-device mount-point\n", getprogname());
644 exit(1);
645}
629 while (waitpid(pid, &status, 0) != pid)
630 ;
631 return (WEXITSTATUS(status));
632}
633
634static void
635usage(void)
636{
637
638 fprintf(stderr,
639"usage: %s [-DLMNSX] [-a maxcontig] [-b block-size] [-c cylinders]\n"
640"\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
641"\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
642"\t[-o mount-options] [-p permissions] [-s size] [-w user:group]\n"
643"\tmd-device mount-point\n", getprogname());
644 exit(1);
645}