mount_fs.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1992, 1993, 1994
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software donated to Berkeley by
  8. * Jan-Simon Pendry.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <sys/param.h>
  35. #include <sys/mount.h>
  36. #include <err.h>
  37. #include <getopt.h>
  38. #include <libgen.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include "extern.h"
  44. #include "mntopts.h"
  45. static struct mntopt mopts[] = {
  46. MOPT_STDOPTS,
  47. MOPT_END
  48. };
  49. static void
  50. usage(void)
  51. {
  52. (void)fprintf(stderr,
  53. "usage: mount [-t fstype] [-o options] target_fs mount_point\n");
  54. exit(1);
  55. }
  56. int
  57. mount_fs(const char *vfstype, int argc, char *argv[])
  58. {
  59. struct iovec *iov;
  60. int iovlen;
  61. int mntflags = 0;
  62. int ch;
  63. char *dev, *dir, mntpath[MAXPATHLEN];
  64. char fstype[32];
  65. char errmsg[255];
  66. char *p, *val;
  67. strlcpy(fstype, vfstype, sizeof(fstype));
  68. memset(errmsg, 0, sizeof(errmsg));
  69. getmnt_silent = 1;
  70. iov = NULL;
  71. iovlen = 0;
  72. optind = optreset = 1; /* Reset for parse of new argv. */
  73. while ((ch = getopt(argc, argv, "o:")) != -1) {
  74. switch(ch) {
  75. case 'o':
  76. getmntopts(optarg, mopts, &mntflags, 0);
  77. p = strchr(optarg, '=');
  78. val = NULL;
  79. if (p != NULL) {
  80. *p = '\0';
  81. val = p + 1;
  82. }
  83. build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
  84. break;
  85. case '?':
  86. default:
  87. usage();
  88. }
  89. }
  90. argc -= optind;
  91. argv += optind;
  92. if (argc != 2)
  93. usage();
  94. dev = argv[0];
  95. dir = argv[1];
  96. if (checkpath(dir, mntpath) != 0) {
  97. warn("%s", mntpath);
  98. return (1);
  99. }
  100. (void)rmslashes(dev, dev);
  101. build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
  102. build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
  103. build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
  104. build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
  105. if (nmount(iov, iovlen, mntflags) == -1) {
  106. if (*errmsg != '\0')
  107. warn("%s: %s", dev, errmsg);
  108. else
  109. warn("%s", dev);
  110. return (1);
  111. }
  112. return (0);
  113. }