chiark / gitweb /
usb_id: use libudev
[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 <sys/ioctl.h>
35
36 #include "../../udev/udev.h"
37 #include "lib/libvolume_id.h"
38
39 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
40
41 static int debug;
42 struct udev *udev_ctx;
43
44 static void log_fn(struct udev *udev, int priority,
45                    const char *file, int line, const char *fn,
46                    const char *format, va_list args)
47 {
48         if (debug) {
49                 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
50                 vfprintf(stderr, format, args);
51         } else {
52                 vsyslog(priority, format, args);
53         }
54 }
55
56 static void vid_log(int priority, const char *file, int line, const char *format, ...)
57 {
58         va_list args;
59
60         va_start(args, format);
61         udev_log(udev_ctx, priority, file, line, NULL, format, args);
62         va_end(args);
63         return;
64 }
65
66 static void set_str(char *to, const char *from, size_t count)
67 {
68         size_t i, j, len;
69
70         /* strip trailing whitespace */
71         len = strnlen(from, count);
72         while (len && isspace(from[len-1]))
73                 len--;
74
75         /* strip leading whitespace */
76         i = 0;
77         while (isspace(from[i]) && (i < len))
78                 i++;
79
80         j = 0;
81         while (i < len) {
82                 /* substitute multiple whitespace */
83                 if (isspace(from[i])) {
84                         while (isspace(from[i]))
85                                 i++;
86                         to[j++] = '_';
87                 }
88                 /* skip chars */
89                 if (from[i] == '/') {
90                         i++;
91                         continue;
92                 }
93                 to[j++] = from[i++];
94         }
95         to[j] = '\0';
96 }
97
98 static int all_probers(volume_id_probe_fn_t probe_fn,
99                        struct volume_id *id, uint64_t off, uint64_t size,
100                        void *data)
101 {
102         const char *type;
103
104         if (probe_fn(id, off, size) == 0)
105                 if (volume_id_get_type(id, &type))
106                         printf("%s\n", type);
107
108         return 0;
109 }
110
111 int main(int argc, char *argv[])
112 {
113         static const struct option options[] = {
114                 { "label", no_argument, NULL, 'l' },
115                 { "label-raw", no_argument, NULL, 'L' },
116                 { "uuid", no_argument, NULL, 'u' },
117                 { "type", no_argument, NULL, 't' },
118                 { "export", no_argument, NULL, 'x' },
119                 { "skip-raid", no_argument, NULL, 's' },
120                 { "probe-all", no_argument, NULL, 'a' },
121                 { "offset", optional_argument, NULL, 'o' },
122                 { "debug", no_argument, NULL, 'd' },
123                 { "help", no_argument, NULL, 'h' },
124                 {}
125         };
126
127         enum print_type {
128                 PRINT_EXPORT,
129                 PRINT_TYPE,
130                 PRINT_LABEL,
131                 PRINT_UUID,
132                 PRINT_LABEL_RAW,
133         } print = PRINT_EXPORT;
134
135         struct volume_id *vid = NULL;
136         char label_safe[256];
137         char label_enc[256];
138         char uuid_enc[256];
139         uint64_t size;
140         int skip_raid = 0;
141         int probe_all = 0;
142         uint64_t offset = 0;
143         const char *node;
144         int fd;
145         const char *label, *uuid, *type, *type_version, *usage;
146         int retval;
147         int rc = 0;
148
149         udev_ctx = udev_new();
150         if (udev_ctx == NULL)
151                 goto exit;
152         logging_init("vol_id");
153         udev_set_log_fn(udev_ctx, log_fn);
154
155         /* hook in our debug into libvolume_id */
156         volume_id_log_fn = vid_log;
157
158         while (1) {
159                 int option;
160
161                 option = getopt_long(argc, argv, "lLutxsaodh", options, NULL);
162                 if (option == -1)
163                         break;
164
165                 switch (option) {
166                 case 'd':
167                         debug = 1;
168                         if (udev_get_log_priority(udev_ctx) < LOG_INFO)
169                                 udev_set_log_priority(udev_ctx, LOG_INFO);
170                         break;
171                 case 'l':
172                         print = PRINT_LABEL;
173                         break;
174                 case 'L':
175                         print = PRINT_LABEL_RAW;
176                         break;
177                 case 'u':
178                         print = PRINT_UUID;
179                         break;
180                 case 't':
181                         print = PRINT_TYPE;
182                         break;
183                 case 'x':
184                         print = PRINT_EXPORT;
185                         break;
186                 case 's':
187                         skip_raid = 1;
188                         break;
189                 case 'a':
190                         probe_all = 1;
191                         break;
192                 case 'o':
193                         if (optarg[0] != '\0')
194                                 offset = strtoull(optarg, NULL, 0);
195                         break;
196                 case 'h':
197                         printf("Usage: vol_id [options] <device>\n"
198                             " --export         export key/value pairs\n"
199                             " --type           filesystem type\n"
200                             " --label          filesystem label\n"
201                             " --label-raw      raw label\n"
202                             " --uuid           filesystem uuid\n"
203                             " --skip-raid      don't probe for raid\n"
204                             " --probe-all      find possibly conflicting signatures\n"
205                             " --offset=<bytes> probe at the given offset\n"
206                             " --debug          print debug output to stderr\n"
207                             " --help\n\n");
208                         goto exit;
209                 default:
210                         retval = 1;
211                         goto exit;
212                 }
213         }
214
215         node = argv[optind];
216         if (!node) {
217                 err(udev_ctx, "no device\n");
218                 fprintf(stderr, "no device\n");
219                 rc = 1;
220                 goto exit;
221         }
222
223         fd = open(node, O_RDONLY);
224         if (fd < 0) {
225                 fprintf(stderr, "%s: error opening volume\n", node);
226                 rc = 2;
227                 goto exit;
228         }
229
230         vid = volume_id_open_fd(fd);
231         if (vid == NULL) {
232                 rc = 2;
233                 goto exit;
234         }
235
236         if (ioctl(fd, BLKGETSIZE64, &size) != 0)
237                 size = 0;
238         info(udev_ctx, "BLKGETSIZE64=%llu (%lluGB)\n", (unsigned long long)size, (unsigned long long)size >> 30);
239
240         /* try to drop all privileges before reading disk content */
241         if (getuid() == 0) {
242                 struct passwd *pw;
243
244                 pw = getpwnam("nobody");
245                 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
246                         if (setgroups(0, NULL) != 0 ||
247                             setgid(pw->pw_gid) != 0 ||
248                             setuid(pw->pw_uid) != 0)
249                                 info(udev_ctx, "unable to drop privileges: %s\n\n", strerror(errno));
250                 }
251         }
252
253         if (probe_all) {
254                 volume_id_all_probers(all_probers, vid, offset, size, NULL);
255                 goto exit;
256         }
257
258         if (skip_raid)
259                 retval = volume_id_probe_filesystem(vid, offset, size);
260         else
261                 retval = volume_id_probe_all(vid, offset, size);
262         if (retval != 0) {
263                 fprintf(stderr, "%s: unknown volume type\n", node);
264                 rc = 4;
265                 goto exit;
266         }
267
268         if (!volume_id_get_label(vid, &label) ||
269             !volume_id_get_usage(vid, &usage) ||
270             !volume_id_get_type(vid, &type) ||
271             !volume_id_get_type_version(vid, &type_version) ||
272             !volume_id_get_uuid(vid, &uuid)) {
273                 rc = 4;
274                 goto exit;
275         }
276
277         set_str(label_safe, label, sizeof(label_safe));
278         util_replace_chars(label_safe, ALLOWED_CHARS_INPUT);
279
280         volume_id_encode_string(label, label_enc, sizeof(label_enc));
281         volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
282
283         switch (print) {
284         case PRINT_EXPORT:
285                 printf("ID_FS_USAGE=%s\n", usage);
286                 printf("ID_FS_TYPE=%s\n", type);
287                 printf("ID_FS_VERSION=%s\n", type_version);
288                 printf("ID_FS_UUID=%s\n", uuid);
289                 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
290                 printf("ID_FS_LABEL=%s\n", label);
291                 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
292                 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
293                 break;
294         case PRINT_TYPE:
295                 printf("%s\n", type);
296                 break;
297         case PRINT_LABEL:
298                 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
299                         rc = 3;
300                         goto exit;
301                 }
302                 printf("%s\n", label_safe);
303                 break;
304         case PRINT_UUID:
305                 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
306                         rc = 4;
307                         goto exit;
308                 }
309                 printf("%s\n", uuid_enc);
310                 break;
311         case PRINT_LABEL_RAW:
312                 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
313                         rc = 3;
314                         goto exit;
315                 }
316                 printf("%s\n", label);
317                 break;
318         }
319
320 exit:
321         if (vid != NULL)
322                 volume_id_close(vid);
323         udev_unref(udev_ctx);
324         logging_close();
325         return rc;
326 }