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