chiark / gitweb /
9830929ef49baebc156f1ddfd8296705ba12163b
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * vol_id - read filesystem label and uuid
3  *
4  * Copyright (C) 2005-2006 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.h"
38 #include "lib/libvolume_id.h"
39
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
41
42 #ifdef USE_LOG
43 void log_message(int priority, const char *format, ...)
44 {
45         va_list args;
46         static int udev_log = -1;
47
48         if (udev_log == -1) {
49                 const char *value;
50
51                 value = getenv("UDEV_LOG");
52                 if (value)
53                         udev_log = log_priority(value);
54                 else
55                         udev_log = LOG_ERR;
56         }
57
58         if (priority > udev_log)
59                 return;
60
61         va_start(args, format);
62         vsyslog(priority, format, args);
63         va_end(args);
64 }
65 #endif
66
67 static void vid_log(int priority, const char *file, int line, const char *format, ...)
68 {
69 #ifdef USE_LOG
70         char log_str[1024];
71         va_list args;
72
73         va_start(args, format);
74         vsnprintf(log_str, sizeof(log_str), format, args);
75         log_str[sizeof(log_str)-1] = '\0';
76         log_message(priority, "%s:%i %s", file, line, log_str);
77         va_end(args);
78 #endif
79         return;
80 }
81
82 static void set_str(char *to, const char *from, size_t count)
83 {
84         size_t i, j, len;
85
86         /* strip trailing whitespace */
87         len = strnlen(from, count);
88         while (len && isspace(from[len-1]))
89                 len--;
90
91         /* strip leading whitespace */
92         i = 0;
93         while (isspace(from[i]) && (i < len))
94                 i++;
95
96         j = 0;
97         while (i < len) {
98                 /* substitute multiple whitespace */
99                 if (isspace(from[i])) {
100                         while (isspace(from[i]))
101                                 i++;
102                         to[j++] = '_';
103                 }
104                 /* skip chars */
105                 if (from[i] == '/') {
106                         i++;
107                         continue;
108                 }
109                 to[j++] = from[i++];
110         }
111         to[j] = '\0';
112 }
113
114 int main(int argc, char *argv[])
115 {
116         static const struct option options[] = {
117                 { "label", 0, NULL, 'l' },
118                 { "label-raw", 0, NULL, 'L' },
119                 { "uuid", 0, NULL, 'u' },
120                 { "type", 0, NULL, 't' },
121                 { "export", 0, NULL, 'x' },
122                 { "skip-raid", 0, NULL, 's' },
123                 { "probe-all", 0, NULL, 'a' },
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[128];
138         char uuid_safe[128];
139         uint64_t size;
140         int skip_raid = 0;
141         int probe_all = 0;
142         const char *node;
143         struct passwd *pw;
144         int fd;
145         const char *label, *uuid, *type, *type_version, *usage;
146         int retval;
147         int rc = 0;
148
149         logging_init("vol_id");
150
151         /* hook in our debug into libvolume_id */
152         volume_id_log_fn = vid_log;
153
154         while (1) {
155                 int option;
156
157                 option = getopt_long(argc, argv, "lLutxsah", options, NULL);
158                 if (option == -1)
159                         break;
160
161                 switch (option) {
162                 case 'l':
163                         print = PRINT_LABEL;
164                         break;
165                 case 'L':
166                         print = PRINT_LABEL_RAW;
167                         break;
168                 case 'u':
169                         print = PRINT_UUID;
170                         break;
171                 case 't':
172                         print = PRINT_TYPE;
173                         break;
174                 case 'x':
175                         print = PRINT_EXPORT;
176                         break;
177                 case 's':
178                         skip_raid = 1;
179                         break;
180                 case 'a':
181                         probe_all = 1;
182                         break;
183                 case 'h':
184                         printf("Usage: vol_id [options] <device>\n"
185                             " --export        export key/value pairs\n"
186                             " --type          filesystem type\n"
187                             " --label         filesystem label\n"
188                             " --label-raw     raw label\n"
189                             " --uuid          filesystem uuid\n"
190                             " --skip-raid     don't probe for raid\n"
191                             " --probe-all     find possibly conflicting signatures\n"
192                             " --help\n\n");
193                         goto exit;
194                 default:
195                         retval = 1;
196                         goto exit;
197                 }
198         }
199
200         node = argv[optind];
201         if (!node) {
202                 err("no device");
203                 fprintf(stderr, "no device\n");
204                 rc = 1;
205                 goto exit;
206         }
207
208         fd = open(node, O_RDONLY);
209         if (fd < 0) {
210                 fprintf(stderr, "%s: error opening volume\n", node);
211                 rc = 2;
212                 goto exit;
213         }
214
215         vid = volume_id_open_fd(fd);
216         if (vid == NULL) {
217                 rc = 2;
218                 goto exit;
219         }
220
221         if (ioctl(fd, BLKGETSIZE64, &size) != 0)
222                 size = 0;
223         dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
224
225         /* try to drop all privileges before reading disk content */
226         pw = getpwnam ("nobody");
227         if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
228                 dbg("dropping privileges to %u:%u",
229                     (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
230                 if (setgroups(0, NULL) != 0 ||
231                     setgid(pw->pw_gid) != 0 ||
232                     setuid(pw->pw_uid) != 0) {
233                         fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
234                         rc = 3;
235                         goto exit;
236                 }
237         }
238
239         if (probe_all) {
240                 if (volume_id_probe_linux_raid(vid, 0, size) == 0)
241                         if (volume_id_get_type(vid, &type))
242                                 printf("%s\n", type);
243                 if (volume_id_probe_ddf_raid(vid, 0, 0) == 0)
244                         if (volume_id_get_type(vid, &type))
245                                 printf("%s\n", type);
246                 if (volume_id_probe_intel_software_raid(vid, 0, size) == 0)
247                         if (volume_id_get_type(vid, &type))
248                                 printf("%s\n", type);
249                 if (volume_id_probe_lsi_mega_raid(vid, 0, size) == 0)
250                         if (volume_id_get_type(vid, &type))
251                                 printf("%s\n", type);
252                 if (volume_id_probe_via_raid(vid, 0, size) == 0)
253                         if (volume_id_get_type(vid, &type))
254                                 printf("%s\n", type);
255                 if (volume_id_probe_silicon_medley_raid(vid, 0, size) == 0)
256                         if (volume_id_get_type(vid, &type))
257                                 printf("%s\n", type);
258                 if (volume_id_probe_nvidia_raid(vid, 0, size) == 0)
259                         if (volume_id_get_type(vid, &type))
260                                 printf("%s\n", type);
261                 if (volume_id_probe_promise_fasttrack_raid(vid, 0, size) == 0)
262                         if (volume_id_get_type(vid, &type))
263                                 printf("%s\n", type);
264                 if (volume_id_probe_highpoint_45x_raid(vid, 0, size) == 0)
265                         if (volume_id_get_type(vid, &type))
266                                 printf("%s\n", type);
267                 if (volume_id_probe_adaptec_raid(vid, 0, size) == 0)
268                         if (volume_id_get_type(vid, &type))
269                                 printf("%s\n", type);
270                 if (volume_id_probe_jmicron_raid(vid, 0, size) == 0)
271                         if (volume_id_get_type(vid, &type))
272                                 printf("%s\n", type);
273                 if (volume_id_probe_vfat(vid, 0, 0) == 0)
274                         if (volume_id_get_type(vid, &type))
275                                 printf("%s\n", type);
276                 if (volume_id_probe_linux_swap(vid, 0, 0) == 0)
277                         if (volume_id_get_type(vid, &type))
278                                 printf("%s\n", type);
279                 if (volume_id_probe_luks(vid, 0, 0) == 0)
280                         if (volume_id_get_type(vid, &type))
281                                 printf("%s\n", type);
282                 if (volume_id_probe_xfs(vid, 0, 0) == 0)
283                         if (volume_id_get_type(vid, &type))
284                                 printf("%s\n", type);
285                 if (volume_id_probe_ext(vid, 0, 0) == 0)
286                         if (volume_id_get_type(vid, &type))
287                                 printf("%s\n", type);
288                 if (volume_id_probe_reiserfs(vid, 0, 0) == 0)
289                         if (volume_id_get_type(vid, &type))
290                                 printf("%s\n", type);
291                 if (volume_id_probe_jfs(vid, 0, 0) == 0)
292                         if (volume_id_get_type(vid, &type))
293                                 printf("%s\n", type);
294                 if (volume_id_probe_udf(vid, 0, 0) == 0)
295                         if (volume_id_get_type(vid, &type))
296                                 printf("%s\n", type);
297                 if (volume_id_probe_iso9660(vid, 0, 0) == 0)
298                         if (volume_id_get_type(vid, &type))
299                                 printf("%s\n", type);
300                 if (volume_id_probe_hfs_hfsplus(vid, 0, 0) == 0)
301                         if (volume_id_get_type(vid, &type))
302                                 printf("%s\n", type);
303                 if (volume_id_probe_ufs(vid, 0, 0) == 0)
304                         if (volume_id_get_type(vid, &type))
305                                 printf("%s\n", type);
306                 if (volume_id_probe_ntfs(vid, 0, 0)  == 0)
307                         if (volume_id_get_type(vid, &type))
308                                 printf("%s\n", type);
309                 if (volume_id_probe_cramfs(vid, 0, 0) == 0)
310                         if (volume_id_get_type(vid, &type))
311                                 printf("%s\n", type);
312                 if (volume_id_probe_romfs(vid, 0, 0) == 0)
313                         if (volume_id_get_type(vid, &type))
314                                 printf("%s\n", type);
315                 if (volume_id_probe_hpfs(vid, 0, 0) == 0)
316                         if (volume_id_get_type(vid, &type))
317                                 printf("%s\n", type);
318                 if (volume_id_probe_sysv(vid, 0, 0) == 0)
319                         if (volume_id_get_type(vid, &type))
320                                 printf("%s\n", type);
321                 if (volume_id_probe_minix(vid, 0, 0) == 0)
322                         if (volume_id_get_type(vid, &type))
323                                 printf("%s\n", type);
324                 if (volume_id_probe_ocfs1(vid, 0, 0) == 0)
325                         if (volume_id_get_type(vid, &type))
326                                 printf("%s\n", type);
327                 if (volume_id_probe_ocfs2(vid, 0, 0) == 0)
328                         if (volume_id_get_type(vid, &type))
329                                 printf("%s\n", type);
330                 if (volume_id_probe_vxfs(vid, 0, 0) == 0)
331                         if (volume_id_get_type(vid, &type))
332                                 printf("%s\n", type);
333                 if (volume_id_probe_squashfs(vid, 0, 0) == 0)
334                         if (volume_id_get_type(vid, &type))
335                                 printf("%s\n", type);
336                 if (volume_id_probe_netware(vid, 0, 0) == 0)
337                         if (volume_id_get_type(vid, &type))
338                                 printf("%s\n", type);
339                 if (volume_id_probe_gfs(vid, 0, 0) == 0)
340                         if (volume_id_get_type(vid, &type))
341                                 printf("%s\n", type);
342                 if (volume_id_probe_gfs2(vid, 0, 0) == 0)
343                         if (volume_id_get_type(vid, &type))
344                                 printf("%s\n", type);
345                 goto exit;
346         }
347
348         if (skip_raid)
349                 retval = volume_id_probe_filesystem(vid, 0, size);
350         else
351                 retval = volume_id_probe_all(vid, 0, size);
352         if (retval != 0) {
353                 fprintf(stderr, "%s: unknown volume type\n", node);
354                 rc = 4;
355                 goto exit;
356         }
357
358         if (!volume_id_get_label(vid, &label) ||
359             !volume_id_get_usage(vid, &usage) ||
360             !volume_id_get_type(vid, &type) ||
361             !volume_id_get_type_version(vid, &type_version) ||
362             !volume_id_get_uuid(vid, &uuid)) {
363                 rc = 4;
364                 goto exit;
365         }
366
367         set_str(label_safe, label, sizeof(label_safe));
368         replace_untrusted_chars(label_safe);
369         set_str(uuid_safe, uuid, sizeof(uuid_safe));
370         replace_untrusted_chars(uuid_safe);
371
372         switch (print) {
373         case PRINT_EXPORT:
374                 printf("ID_FS_USAGE=%s\n", usage);
375                 printf("ID_FS_TYPE=%s\n", type);
376                 printf("ID_FS_VERSION=%s\n", type_version);
377                 printf("ID_FS_UUID=%s\n", uuid);
378                 printf("ID_FS_UUID_SAFE=%s\n", uuid_safe);
379                 printf("ID_FS_LABEL=%s\n", label);
380                 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
381                 break;
382         case PRINT_TYPE:
383                 printf("%s\n", type);
384                 break;
385         case PRINT_LABEL:
386                 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
387                         rc = 3;
388                         goto exit;
389                 }
390                 printf("%s\n", label_safe);
391                 break;
392         case PRINT_UUID:
393                 if (uuid_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
394                         rc = 4;
395                         goto exit;
396                 }
397                 printf("%s\n", uuid_safe);
398                 break;
399         case PRINT_LABEL_RAW:
400                 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
401                         rc = 3;
402                         goto exit;
403                 }
404                 printf("%s\n", label);
405                 break;
406         }
407
408 exit:
409         if (vid != NULL)
410                 volume_id_close(vid);
411
412         logging_close();
413         return rc;
414 }