chiark / gitweb /
volume_id: support for long-filename based labels
[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 static int all_probers(volume_id_probe_fn_t probe_fn,
115                        struct volume_id *id, uint64_t off, uint64_t size,
116                        void *data)
117 {
118         const char *type;
119
120         if (probe_fn(id, off, size) == 0)
121                 if (volume_id_get_type(id, &type))
122                         printf("%s\n", type);
123
124         return 0;
125 }
126
127 int main(int argc, char *argv[])
128 {
129         static const struct option options[] = {
130                 { "label", 0, NULL, 'l' },
131                 { "label-raw", 0, NULL, 'L' },
132                 { "uuid", 0, NULL, 'u' },
133                 { "type", 0, NULL, 't' },
134                 { "export", 0, NULL, 'x' },
135                 { "skip-raid", 0, NULL, 's' },
136                 { "probe-all", 0, NULL, 'a' },
137                 { "help", 0, NULL, 'h' },
138                 {}
139         };
140
141         enum print_type {
142                 PRINT_EXPORT,
143                 PRINT_TYPE,
144                 PRINT_LABEL,
145                 PRINT_UUID,
146                 PRINT_LABEL_RAW,
147         } print = PRINT_EXPORT;
148
149         struct volume_id *vid = NULL;
150         char label_safe[256];
151         char label_enc[256];
152         char uuid_enc[256];
153         uint64_t size;
154         int skip_raid = 0;
155         int probe_all = 0;
156         const char *node;
157         struct passwd *pw;
158         int fd;
159         const char *label, *uuid, *type, *type_version, *usage;
160         int retval;
161         int rc = 0;
162
163         logging_init("vol_id");
164
165         /* hook in our debug into libvolume_id */
166         volume_id_log_fn = vid_log;
167
168         while (1) {
169                 int option;
170
171                 option = getopt_long(argc, argv, "lLutxsah", options, NULL);
172                 if (option == -1)
173                         break;
174
175                 switch (option) {
176                 case 'l':
177                         print = PRINT_LABEL;
178                         break;
179                 case 'L':
180                         print = PRINT_LABEL_RAW;
181                         break;
182                 case 'u':
183                         print = PRINT_UUID;
184                         break;
185                 case 't':
186                         print = PRINT_TYPE;
187                         break;
188                 case 'x':
189                         print = PRINT_EXPORT;
190                         break;
191                 case 's':
192                         skip_raid = 1;
193                         break;
194                 case 'a':
195                         probe_all = 1;
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                             " --help\n\n");
207                         goto exit;
208                 default:
209                         retval = 1;
210                         goto exit;
211                 }
212         }
213
214         node = argv[optind];
215         if (!node) {
216                 err("no device");
217                 fprintf(stderr, "no device\n");
218                 rc = 1;
219                 goto exit;
220         }
221
222         fd = open(node, O_RDONLY);
223         if (fd < 0) {
224                 fprintf(stderr, "%s: error opening volume\n", node);
225                 rc = 2;
226                 goto exit;
227         }
228
229         vid = volume_id_open_fd(fd);
230         if (vid == NULL) {
231                 rc = 2;
232                 goto exit;
233         }
234
235         if (ioctl(fd, BLKGETSIZE64, &size) != 0)
236                 size = 0;
237         dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
238
239         /* try to drop all privileges before reading disk content */
240         pw = getpwnam ("nobody");
241         if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
242                 dbg("dropping privileges to %u:%u",
243                     (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
244                 if (setgroups(0, NULL) != 0 ||
245                     setgid(pw->pw_gid) != 0 ||
246                     setuid(pw->pw_uid) != 0) {
247                         fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
248                         rc = 3;
249                         goto exit;
250                 }
251         }
252
253         if (probe_all) {
254                 volume_id_all_probers(all_probers, vid, 0, size, NULL);
255                 goto exit;
256         }
257
258         if (skip_raid)
259                 retval = volume_id_probe_filesystem(vid, 0, size);
260         else
261                 retval = volume_id_probe_all(vid, 0, 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         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
324         logging_close();
325         return rc;
326 }