Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.5 KiB

27 years ago
27 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /* $OpenBSD: readlabel.c,v 1.10 2006/10/02 12:01:40 krw Exp $ */
  2. /*
  3. * Copyright (c) 1996, Jason Downs. 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
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
  15. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
  18. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include <sys/types.h>
  27. #include <stdio.h>
  28. #include <err.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <paths.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <sys/dkio.h>
  35. #define DKTYPENAMES
  36. #include <sys/disklabel.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/param.h>
  39. #include <sys/stat.h>
  40. #include "util.h"
  41. /*
  42. * Try to get a disklabel for the specified device, and return mount_xxx
  43. * style filesystem type name for the specified partition.
  44. */
  45. char *
  46. readlabelfs(char *device, int verbose)
  47. {
  48. char rpath[MAXPATHLEN];
  49. struct disklabel dk;
  50. char part, *type;
  51. struct stat sbuf;
  52. int fd;
  53. /* Assuming device is of the form /dev/??p, build a raw partition. */
  54. if (stat(device, &sbuf) < 0) {
  55. if (verbose)
  56. warn("%s", device);
  57. return (NULL);
  58. }
  59. switch (sbuf.st_mode & S_IFMT) {
  60. case S_IFCHR:
  61. /* Ok... already a raw device. Hmm. */
  62. strncpy(rpath, device, sizeof(rpath));
  63. rpath[sizeof(rpath) - 1] = '\0';
  64. /* Change partition name. */
  65. part = rpath[strlen(rpath) - 1];
  66. rpath[strlen(rpath) - 1] = 'a' + getrawpartition();
  67. break;
  68. case S_IFBLK:
  69. if (strlen(device) > sizeof(_PATH_DEV) - 1) {
  70. snprintf(rpath, sizeof(rpath), "%sr%s", _PATH_DEV,
  71. &device[sizeof(_PATH_DEV) - 1]);
  72. /* Change partition name. */
  73. part = rpath[strlen(rpath) - 1];
  74. rpath[strlen(rpath) - 1] = 'a' + getrawpartition();
  75. break;
  76. }
  77. /* FALLTHROUGH */
  78. default:
  79. if (verbose)
  80. warnx("%s: not a device node", device);
  81. return (NULL);
  82. }
  83. /* If rpath doesn't exist, change that partition back. */
  84. fd = open(rpath, O_RDONLY);
  85. if (fd < 0) {
  86. if (errno == ENOENT) {
  87. rpath[strlen(rpath) - 1] = part;
  88. fd = open(rpath, O_RDONLY);
  89. if (fd < 0) {
  90. if (verbose)
  91. warn("%s", rpath);
  92. return (NULL);
  93. }
  94. } else {
  95. if (verbose)
  96. warn("%s", rpath);
  97. return (NULL);
  98. }
  99. }
  100. if (ioctl(fd, DIOCGDINFO, &dk) < 0) {
  101. if (verbose)
  102. warn("%s: couldn't read disklabel", rpath);
  103. close(fd);
  104. return (NULL);
  105. }
  106. close(fd);
  107. if (dk.d_partitions[part - 'a'].p_fstype >= FSMAXTYPES) {
  108. if (verbose)
  109. warnx("%s: bad filesystem type in label", rpath);
  110. return (NULL);
  111. }
  112. type = fstypesnames[dk.d_partitions[part - 'a'].p_fstype];
  113. return ((type[0] == '\0') ? NULL : type);
  114. }