2 * vol_id - read filesystem label and uuid
4 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
31 #include <sys/ioctl.h>
33 #include "../../udev.h"
34 #include "lib/libvolume_id.h"
36 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
39 void log_message(int priority, const char *format, ...)
42 static int udev_log = -1;
47 value = getenv("UDEV_LOG");
49 udev_log = log_priority(value);
54 if (priority > udev_log)
57 va_start(args, format);
58 vsyslog(priority, format, args);
63 static void vid_log(int priority, const char *file, int line, const char *format, ...)
69 va_start(args, format);
70 vsnprintf(log_str, sizeof(log_str), format, args);
71 log_str[sizeof(log_str)-1] = '\0';
72 log_message(priority, "%s:%i %s", file, line, log_str);
78 static void set_str(char *to, const char *from, size_t count)
82 /* strip trailing whitespace */
83 len = strnlen(from, count);
84 while (len && isspace(from[len-1]))
87 /* strip leading whitespace */
89 while (isspace(from[i]) && (i < len))
94 /* substitute multiple whitespace */
95 if (isspace(from[i])) {
96 while (isspace(from[i]))
101 if (from[i] == '/') {
110 int main(int argc, char *argv[])
112 const char help[] = "usage: vol_id [--export|-t|-l|-u] <device>\n"
114 " -t filesystem type\n"
115 " -l filesystem label\n"
116 " -u filesystem uuid\n"
123 } print = PRINT_EXPORT;
124 struct volume_id *vid = NULL;
125 static char name[VOLUME_ID_LABEL_SIZE];
128 const char *node = NULL;
133 logging_init("vol_id");
135 /* hook in our debug into libvolume_id */
136 volume_id_log_fn = vid_log;
138 for (i = 1 ; i < argc; i++) {
141 if (strcmp(arg, "--export") == 0) {
142 print = PRINT_EXPORT;
143 } else if (strcmp(arg, "-t") == 0) {
145 } else if (strcmp(arg, "-l") == 0) {
147 } else if (strcmp(arg, "-u") == 0) {
153 err("no node specified");
154 fprintf(stderr, help);
159 vid = volume_id_open_node(node);
161 fprintf(stderr, "%s: error open volume\n", node);
166 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
168 dbg("BLKGETSIZE64=%llu", size);
170 /* drop all privileges */
171 nobody_uid = lookup_user("nobody");
172 nobody_gid = lookup_group("nogroup");
173 if (nobody_uid > 0 && nobody_gid > 0) {
174 if (setgroups(0, NULL) != 0 ||
175 setgid(nobody_gid) != 0 ||
176 setuid(nobody_uid) != 0) {
182 if (volume_id_probe_all(vid, 0, size) == 0)
185 if (print != PRINT_EXPORT)
186 fprintf(stderr, "%s: unknown volume type\n", node);
191 set_str(name, vid->label, sizeof(vid->label));
192 replace_untrusted_chars(name);
196 printf("ID_FS_USAGE=%s\n", vid->usage);
197 printf("ID_FS_TYPE=%s\n", vid->type);
198 printf("ID_FS_VERSION=%s\n", vid->type_version);
199 printf("ID_FS_UUID=%s\n", vid->uuid);
200 printf("ID_FS_LABEL=%s\n", vid->label);
201 printf("ID_FS_LABEL_SAFE=%s\n", name);
204 printf("%s\n", vid->type);
207 if (name[0] == '\0' ||
208 (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
212 printf("%s\n", name);
215 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
219 printf("%s\n", vid->uuid);
225 volume_id_close(vid);