chiark / gitweb /
udevadm: add --version --help options to man page, hide them as commands
[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 it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <getopt.h>
34 #include <fcntl.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
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         log_fn(NULL, 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         struct udev *udev;
114         static const struct option options[] = {
115                 { "label", 0, NULL, 'l' },
116                 { "label-raw", 0, NULL, 'L' },
117                 { "uuid", 0, NULL, 'u' },
118                 { "type", 0, NULL, 't' },
119                 { "export", 0, NULL, 'x' },
120                 { "skip-raid", 0, NULL, 's' },
121                 { "probe-all", 0, NULL, 'a' },
122                 { "offset", 2, NULL, 'o' },
123                 { "debug", 0, NULL, 'd' },
124                 { "help", 0, NULL, 'h' },
125                 {}
126         };
127
128         enum print_type {
129                 PRINT_EXPORT,
130                 PRINT_TYPE,
131                 PRINT_LABEL,
132                 PRINT_UUID,
133                 PRINT_LABEL_RAW,
134         } print = PRINT_EXPORT;
135
136         struct volume_id *vid = NULL;
137         char label_safe[256];
138         char label_enc[256];
139         char uuid_enc[256];
140         uint64_t size;
141         int skip_raid = 0;
142         int probe_all = 0;
143         uint64_t offset = 0;
144         const char *node;
145         int fd;
146         const char *label, *uuid, *type, *type_version, *usage;
147         int retval;
148         int rc = 0;
149
150         udev = udev_new();
151         if (udev == NULL)
152                 goto exit;
153         logging_init("vol_id");
154         udev_set_log_fn(udev, log_fn);
155
156         /* hook in our debug into libvolume_id */
157         volume_id_log_fn = vid_log;
158
159         while (1) {
160                 int option;
161
162                 option = getopt_long(argc, argv, "lLutxsaodh", options, NULL);
163                 if (option == -1)
164                         break;
165
166                 switch (option) {
167                 case 'd':
168                         debug = 1;
169                         if (udev_get_log_priority(udev) < LOG_INFO)
170                                 udev_set_log_priority(udev, LOG_INFO);
171                         break;
172                 case 'l':
173                         print = PRINT_LABEL;
174                         break;
175                 case 'L':
176                         print = PRINT_LABEL_RAW;
177                         break;
178                 case 'u':
179                         print = PRINT_UUID;
180                         break;
181                 case 't':
182                         print = PRINT_TYPE;
183                         break;
184                 case 'x':
185                         print = PRINT_EXPORT;
186                         break;
187                 case 's':
188                         skip_raid = 1;
189                         break;
190                 case 'a':
191                         probe_all = 1;
192                         break;
193                 case 'o':
194                         if (optarg[0] != '\0')
195                                 offset = strtoull(optarg, NULL, 0);
196                         break;
197                 case 'h':
198                         printf("Usage: vol_id [options] <device>\n"
199                             " --export         export key/value pairs\n"
200                             " --type           filesystem type\n"
201                             " --label          filesystem label\n"
202                             " --label-raw      raw label\n"
203                             " --uuid           filesystem uuid\n"
204                             " --skip-raid      don't probe for raid\n"
205                             " --probe-all      find possibly conflicting signatures\n"
206                             " --offset=<bytes> probe at the given offset\n"
207                             " --debug          print debug output to stderr\n"
208                             " --help\n\n");
209                         goto exit;
210                 default:
211                         retval = 1;
212                         goto exit;
213                 }
214         }
215
216         node = argv[optind];
217         if (!node) {
218                 err(udev, "no device\n");
219                 fprintf(stderr, "no device\n");
220                 rc = 1;
221                 goto exit;
222         }
223
224         fd = open(node, O_RDONLY);
225         if (fd < 0) {
226                 fprintf(stderr, "%s: error opening volume\n", node);
227                 rc = 2;
228                 goto exit;
229         }
230
231         vid = volume_id_open_fd(fd);
232         if (vid == NULL) {
233                 rc = 2;
234                 goto exit;
235         }
236
237         if (ioctl(fd, BLKGETSIZE64, &size) != 0)
238                 size = 0;
239         info(udev, "BLKGETSIZE64=%llu (%lluGB)\n", (unsigned long long)size, (unsigned long long)size >> 30);
240
241         /* try to drop all privileges before reading disk content */
242         if (getuid() == 0) {
243                 struct passwd *pw;
244
245                 pw = getpwnam("nobody");
246                 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
247                         if (setgroups(0, NULL) != 0 ||
248                             setgid(pw->pw_gid) != 0 ||
249                             setuid(pw->pw_uid) != 0)
250                                 info(udev, "unable to drop privileges: %s\n\n", strerror(errno));
251                 }
252         }
253
254         if (probe_all) {
255                 volume_id_all_probers(all_probers, vid, offset, size, NULL);
256                 goto exit;
257         }
258
259         if (skip_raid)
260                 retval = volume_id_probe_filesystem(vid, offset, size);
261         else
262                 retval = volume_id_probe_all(vid, offset, size);
263         if (retval != 0) {
264                 fprintf(stderr, "%s: unknown volume type\n", node);
265                 rc = 4;
266                 goto exit;
267         }
268
269         if (!volume_id_get_label(vid, &label) ||
270             !volume_id_get_usage(vid, &usage) ||
271             !volume_id_get_type(vid, &type) ||
272             !volume_id_get_type_version(vid, &type_version) ||
273             !volume_id_get_uuid(vid, &uuid)) {
274                 rc = 4;
275                 goto exit;
276         }
277
278         set_str(label_safe, label, sizeof(label_safe));
279         replace_chars(label_safe, ALLOWED_CHARS_INPUT);
280
281         volume_id_encode_string(label, label_enc, sizeof(label_enc));
282         volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
283
284         switch (print) {
285         case PRINT_EXPORT:
286                 printf("ID_FS_USAGE=%s\n", usage);
287                 printf("ID_FS_TYPE=%s\n", type);
288                 printf("ID_FS_VERSION=%s\n", type_version);
289                 printf("ID_FS_UUID=%s\n", uuid);
290                 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
291                 printf("ID_FS_LABEL=%s\n", label);
292                 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
293                 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
294                 break;
295         case PRINT_TYPE:
296                 printf("%s\n", type);
297                 break;
298         case PRINT_LABEL:
299                 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
300                         rc = 3;
301                         goto exit;
302                 }
303                 printf("%s\n", label_safe);
304                 break;
305         case PRINT_UUID:
306                 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
307                         rc = 4;
308                         goto exit;
309                 }
310                 printf("%s\n", uuid_enc);
311                 break;
312         case PRINT_LABEL_RAW:
313                 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
314                         rc = 3;
315                         goto exit;
316                 }
317                 printf("%s\n", label);
318                 break;
319         }
320
321 exit:
322         if (vid != NULL)
323                 volume_id_close(vid);
324         udev_unref(udev);
325         logging_close();
326         return rc;
327 }