chiark / gitweb /
b4db5adcd86210dcebcdd6e4ca414f92a8bffa42
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * vol_id - read filesystem label and uuid
3  *
4  * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <getopt.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <sys/ioctl.h>
36
37 #include "../../udev/udev.h"
38 #include "lib/libvolume_id.h"
39
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
41
42 static int debug;
43 struct udev *udev_ctx;
44
45 static void log_fn(struct udev *udev, int priority,
46                    const char *file, int line, const char *fn,
47                    const char *format, va_list args)
48 {
49         if (debug) {
50                 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
51                 vfprintf(stderr, format, args);
52         } else {
53                 vsyslog(priority, format, args);
54         }
55 }
56
57 static void vid_log(int priority, const char *file, int line, const char *format, ...)
58 {
59         va_list args;
60
61         if (priority > udev_get_log_priority(udev_ctx))
62                 return;
63         va_start(args, format);
64         log_fn(udev_ctx, priority, file, line, NULL, format, args);
65         va_end(args);
66         return;
67 }
68
69 static void set_str(char *to, const char *from, size_t count)
70 {
71         size_t i, j, len;
72
73         /* strip trailing whitespace */
74         len = strnlen(from, count);
75         while (len && isspace(from[len-1]))
76                 len--;
77
78         /* strip leading whitespace */
79         i = 0;
80         while (isspace(from[i]) && (i < len))
81                 i++;
82
83         j = 0;
84         while (i < len) {
85                 /* substitute multiple whitespace */
86                 if (isspace(from[i])) {
87                         while (isspace(from[i]))
88                                 i++;
89                         to[j++] = '_';
90                 }
91                 /* skip chars */
92                 if (from[i] == '/') {
93                         i++;
94                         continue;
95                 }
96                 to[j++] = from[i++];
97         }
98         to[j] = '\0';
99 }
100
101 static int all_probers(volume_id_probe_fn_t probe_fn,
102                        struct volume_id *id, uint64_t off, uint64_t size,
103                        void *data)
104 {
105         const char *type;
106
107         if (probe_fn(id, off, size) == 0)
108                 if (volume_id_get_type(id, &type))
109                         printf("%s\n", type);
110
111         return 0;
112 }
113
114 int main(int argc, char *argv[])
115 {
116         static const struct option options[] = {
117                 { "label", no_argument, NULL, 'l' },
118                 { "label-raw", no_argument, NULL, 'L' },
119                 { "uuid", no_argument, NULL, 'u' },
120                 { "type", no_argument, NULL, 't' },
121                 { "export", no_argument, NULL, 'x' },
122                 { "skip-raid", no_argument, NULL, 's' },
123                 { "size", required_argument, NULL, 'S' },
124                 { "probe-all", no_argument, NULL, 'a' },
125                 { "offset", optional_argument, NULL, 'o' },
126                 { "debug", no_argument, NULL, 'd' },
127                 { "help", no_argument, NULL, 'h' },
128                 {}
129         };
130
131         enum print_type {
132                 PRINT_EXPORT,
133                 PRINT_TYPE,
134                 PRINT_LABEL,
135                 PRINT_UUID,
136                 PRINT_LABEL_RAW,
137         } print = PRINT_EXPORT;
138
139         struct volume_id *vid = NULL;
140         char label_safe[256];
141         char label_enc[256];
142         char uuid_safe[256];
143         char uuid_enc[256];
144         char type_enc[256];
145         char type_version_enc[256];
146         uint64_t size = 0;
147         int skip_raid = 0;
148         int probe_all = 0;
149         uint64_t offset = 0;
150         const char *node;
151         int fd;
152         const char *label, *uuid, *type, *type_version, *usage;
153         int retval;
154         int rc = 0;
155
156         udev_ctx = udev_new();
157         if (udev_ctx == NULL)
158                 goto exit;
159         logging_init("vol_id");
160         udev_set_log_fn(udev_ctx, log_fn);
161
162         /* hook in our debug into libvolume_id */
163         volume_id_log_fn = vid_log;
164
165         while (1) {
166                 int option;
167
168                 option = getopt_long(argc, argv, "lLutxsS:aodh", options, NULL);
169                 if (option == -1)
170                         break;
171
172                 switch (option) {
173                 case 'd':
174                         debug = 1;
175                         if (udev_get_log_priority(udev_ctx) < LOG_INFO)
176                                 udev_set_log_priority(udev_ctx, LOG_INFO);
177                         break;
178                 case 'l':
179                         print = PRINT_LABEL;
180                         break;
181                 case 'L':
182                         print = PRINT_LABEL_RAW;
183                         break;
184                 case 'u':
185                         print = PRINT_UUID;
186                         break;
187                 case 't':
188                         print = PRINT_TYPE;
189                         break;
190                 case 'x':
191                         print = PRINT_EXPORT;
192                         break;
193                 case 's':
194                         skip_raid = 1;
195                         break;
196                 case 'a':
197                         probe_all = 1;
198                         break;
199                 case 'S':
200                         if (optarg[0] != '\0')
201                                 size = strtoull(optarg, NULL, 0);
202                         break;
203                 case 'o':
204                         if (optarg[0] != '\0')
205                                 offset = strtoull(optarg, NULL, 0);
206                         break;
207                 case 'h':
208                         printf("Usage: vol_id [options] <device>\n"
209                             " --export         export key/value pairs\n"
210                             " --type           filesystem type\n"
211                             " --label          filesystem label\n"
212                             " --label-raw      raw label\n"
213                             " --uuid           filesystem uuid\n"
214                             " --skip-raid      don't probe for raid\n"
215                             " --probe-all      find possibly conflicting signatures\n"
216                             " --offset=<bytes> probe at the given offset\n"
217                             " --size=<bytes>   overwrite device size\n"
218                             " --debug          print debug output to stderr\n"
219                             " --help\n\n");
220                         goto exit;
221                 default:
222                         retval = 1;
223                         goto exit;
224                 }
225         }
226
227         node = argv[optind];
228         if (!node) {
229                 err(udev_ctx, "no device\n");
230                 fprintf(stderr, "no device\n");
231                 rc = 1;
232                 goto exit;
233         }
234
235         fd = open(node, O_RDONLY);
236         if (fd < 0) {
237                 fprintf(stderr, "%s: error opening volume\n", node);
238                 rc = 2;
239                 goto exit;
240         }
241
242         vid = volume_id_open_fd(fd);
243         if (vid == NULL) {
244                 rc = 2;
245                 goto exit;
246         }
247
248         if (size == 0) {
249                 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
250                         size = 0;
251                 info(udev_ctx, "BLKGETSIZE64=%" PRIu64 " (%" PRIu64 "GB)\n", size, size >> 30);
252         }
253
254         /* try to drop all privileges before reading disk content */
255         if (getuid() == 0) {
256                 struct passwd *pw;
257
258                 pw = getpwnam("nobody");
259                 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
260                         if (setgroups(0, NULL) != 0 ||
261                             setgid(pw->pw_gid) != 0 ||
262                             setuid(pw->pw_uid) != 0)
263                                 info(udev_ctx, "unable to drop privileges: %s\n\n", strerror(errno));
264                 }
265         }
266
267         if (probe_all) {
268                 volume_id_all_probers(all_probers, vid, offset, size, NULL);
269                 goto exit;
270         }
271
272         if (skip_raid)
273                 retval = volume_id_probe_filesystem(vid, offset, size);
274         else
275                 retval = volume_id_probe_all(vid, offset, size);
276         if (retval != 0) {
277                 fprintf(stderr, "%s: unknown volume type\n", node);
278                 rc = 4;
279                 goto exit;
280         }
281
282         if (!volume_id_get_label(vid, &label) ||
283             !volume_id_get_usage(vid, &usage) ||
284             !volume_id_get_type(vid, &type) ||
285             !volume_id_get_type_version(vid, &type_version) ||
286             !volume_id_get_uuid(vid, &uuid)) {
287                 rc = 4;
288                 goto exit;
289         }
290
291         set_str(label_safe, label, sizeof(label_safe));
292         util_replace_chars(label_safe, ALLOWED_CHARS_INPUT);
293         volume_id_encode_string(label, label_enc, sizeof(label_enc));
294
295         set_str(uuid_safe, uuid, sizeof(uuid_safe));
296         util_replace_chars(uuid_safe, ALLOWED_CHARS_INPUT);
297         volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
298
299         volume_id_encode_string(type, type_enc, sizeof(type_enc));
300         volume_id_encode_string(type_version, type_version_enc, sizeof(type_version_enc));
301
302         switch (print) {
303         case PRINT_EXPORT:
304                 printf("ID_FS_USAGE=%s\n", usage);
305                 printf("ID_FS_TYPE=%s\n", type_enc);
306                 printf("ID_FS_VERSION=%s\n", type_version_enc);
307                 printf("ID_FS_UUID=%s\n", uuid_safe);
308                 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
309                 printf("ID_FS_LABEL=%s\n", label_safe);
310                 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
311                 break;
312         case PRINT_TYPE:
313                 printf("%s\n", type);
314                 break;
315         case PRINT_LABEL:
316                 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
317                         rc = 3;
318                         goto exit;
319                 }
320                 printf("%s\n", label_safe);
321                 break;
322         case PRINT_UUID:
323                 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
324                         rc = 4;
325                         goto exit;
326                 }
327                 printf("%s\n", uuid_enc);
328                 break;
329         case PRINT_LABEL_RAW:
330                 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
331                         rc = 3;
332                         goto exit;
333                 }
334                 printf("%s\n", label);
335                 break;
336         }
337
338 exit:
339         if (vid != NULL)
340                 volume_id_close(vid);
341         udev_unref(udev_ctx);
342         logging_close();
343         return rc;
344 }