kernel - Fix races created by a comedy of circumstansces (3)
[dragonfly.git] / sbin / gpt / show.c
blob7affc913f75a62fb76ff01b371745c065c0faf3d
1 /*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $
27 * $DragonFly: src/sbin/gpt/show.c,v 1.3 2007/06/17 08:34:59 dillon Exp $
30 #include <sys/types.h>
32 #include <err.h>
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "map.h"
40 #include "gpt.h"
42 static int show_label = 0;
43 static int show_uuid = 0;
45 static void
46 usage_show(void)
49 fprintf(stderr,
50 "usage: %s [-lu] device ...\n", getprogname());
51 exit(1);
54 static const char *
55 friendly(uuid_t *t)
57 static char *save_name1 /*= NULL*/;
58 static char *save_name2 /*= NULL*/;
60 if (show_uuid)
61 goto unfriendly;
63 uuid_addr_lookup(t, &save_name1, NULL);
64 if (save_name1)
65 return (save_name1);
67 unfriendly:
68 if (save_name2) {
69 free(save_name2);
70 save_name2 = NULL;
72 uuid_to_string(t, &save_name2, NULL);
73 return (save_name2);
76 static void
77 show(int fd __unused)
79 uuid_t type;
80 off_t start;
81 map_t *m, *p;
82 struct mbr *mbr;
83 struct gpt_ent *ent;
84 unsigned int i;
86 printf(" %*s", lbawidth, "start");
87 printf(" %*s", lbawidth, "size");
88 printf(" index contents\n");
90 m = map_first();
91 while (m != NULL) {
92 printf(" %*llu", lbawidth, (long long)m->map_start);
93 printf(" %*llu", lbawidth, (long long)m->map_size);
94 putchar(' ');
95 putchar(' ');
96 if (m->map_index != NOENTRY)
97 printf("%5d", m->map_index);
98 else
99 printf(" -");
100 putchar(' ');
101 putchar(' ');
102 switch (m->map_type) {
103 case MAP_TYPE_MBR:
104 if (m->map_start != 0)
105 printf("Extended ");
106 printf("MBR");
107 break;
108 case MAP_TYPE_PRI_GPT_HDR:
109 printf("Pri GPT header");
110 break;
111 case MAP_TYPE_SEC_GPT_HDR:
112 printf("Sec GPT header");
113 break;
114 case MAP_TYPE_PRI_GPT_TBL:
115 printf("Pri GPT table");
116 break;
117 case MAP_TYPE_SEC_GPT_TBL:
118 printf("Sec GPT table");
119 break;
120 case MAP_TYPE_MBR_PART:
121 p = m->map_data;
122 if (p->map_start != 0)
123 printf("Extended ");
124 printf("MBR part ");
125 mbr = p->map_data;
126 for (i = 0; i < 4; i++) {
127 start = le16toh(mbr->mbr_part[i].part_start_hi);
128 start = (start << 16) +
129 le16toh(mbr->mbr_part[i].part_start_lo);
130 if (m->map_start == p->map_start + start)
131 break;
133 printf("%d", mbr->mbr_part[i].part_typ);
134 break;
135 case MAP_TYPE_GPT_PART:
136 printf("GPT part ");
137 ent = m->map_data;
138 if (show_label) {
139 printf("- \"%s\"",
140 utf16_to_utf8(ent->ent_name));
141 } else {
142 le_uuid_dec(&ent->ent_type, &type);
143 printf("- %s", friendly(&type));
145 break;
146 case MAP_TYPE_PMBR:
147 printf("PMBR");
148 break;
150 putchar('\n');
151 m = m->map_next;
156 cmd_show(int argc, char *argv[])
158 int ch, fd;
160 while ((ch = getopt(argc, argv, "lu")) != -1) {
161 switch(ch) {
162 case 'l':
163 show_label = 1;
164 break;
165 case 'u':
166 show_uuid = 1;
167 break;
168 default:
169 usage_show();
173 if (argc == optind)
174 usage_show();
176 while (optind < argc) {
177 fd = gpt_open(argv[optind++]);
178 if (fd == -1) {
179 warn("unable to open device '%s'", device_name);
180 continue;
183 show(fd);
185 gpt_close(fd);
188 return (0);