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