chiark / gitweb /
2274550c197305e9d15d573a87ba65b4f7426bf2
[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         va_list args;
67
68         va_start(args, format);
69         log_message(priority, format, args);
70         va_end(args);
71 #endif
72         return;
73 }
74
75 static void set_str(char *to, const char *from, size_t count)
76 {
77         size_t i, j, len;
78
79         /* strip trailing whitespace */
80         len = strnlen(from, count);
81         while (len && isspace(from[len-1]))
82                 len--;
83
84         /* strip leading whitespace */
85         i = 0;
86         while (isspace(from[i]) && (i < len))
87                 i++;
88
89         j = 0;
90         while (i < len) {
91                 /* substitute multiple whitespace */
92                 if (isspace(from[i])) {
93                         while (isspace(from[i]))
94                                 i++;
95                         to[j++] = '_';
96                 }
97                 /* skip chars */
98                 if (from[i] == '/') {
99                         i++;
100                         continue;
101                 }
102                 to[j++] = from[i++];
103         }
104         to[j] = '\0';
105 }
106
107 int main(int argc, char *argv[])
108 {
109         const char help[] = "usage: vol_id [--export|-t|-l|-u] <device>\n"
110                             "       --export\n"
111                             "       -t filesystem type\n"
112                             "       -l filesystem label\n"
113                             "       -u filesystem uuid\n"
114                             "\n";
115         enum print_type {
116                 PRINT_EXPORT,
117                 PRINT_TYPE,
118                 PRINT_LABEL,
119                 PRINT_UUID,
120         } print = PRINT_EXPORT;
121         struct volume_id *vid = NULL;
122         static char name[VOLUME_ID_LABEL_SIZE];
123         int i;
124         uint64_t size;
125         const char *node = NULL;
126         uid_t nobody_uid;
127         gid_t nobody_gid;
128         int rc = 0;
129
130         logging_init("vol_id");
131
132         /* hook in our debug into libvolume_id */
133         volume_id_log_fn = vid_log;
134
135         for (i = 1 ; i < argc; i++) {
136                 char *arg = argv[i];
137
138                 if (strcmp(arg, "--export") == 0) {
139                         print = PRINT_EXPORT;
140                 } else if (strcmp(arg, "-t") == 0) {
141                         print = PRINT_TYPE;
142                 } else if (strcmp(arg, "-l") == 0) {
143                         print = PRINT_LABEL;
144                 } else if (strcmp(arg, "-u") == 0) {
145                         print = PRINT_UUID;
146                 } else
147                         node = arg;
148         }
149         if (!node) {
150                 err("no node specified");
151                 fprintf(stderr, help);
152                 rc = 1;
153                 goto exit;
154         }
155
156         vid = volume_id_open_node(node);
157         if (vid == NULL) {
158                 fprintf(stderr, "%s: error open volume\n", node);
159                 rc = 2;
160                 goto exit;
161         }
162
163         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
164                 size = 0;
165         dbg("BLKGETSIZE64=%llu", size);
166
167         /* drop all privileges */
168         nobody_uid = lookup_user("nobody");
169         nobody_gid = lookup_group("nogroup");
170         if (nobody_uid > 0 && nobody_gid > 0) {
171                 if (setgroups(0, NULL) != 0 ||
172                     setgid(nobody_gid) != 0 ||
173                     setuid(nobody_uid) != 0) {
174                         rc = 3;
175                         goto exit;
176                 }
177         }
178
179         if (volume_id_probe_all(vid, 0, size) == 0)
180                 goto print;
181
182         if (print != PRINT_EXPORT)
183                 fprintf(stderr, "%s: unknown volume type\n", node);
184         rc = 4;
185         goto exit;
186
187 print:
188         set_str(name, vid->label, sizeof(vid->label));
189         replace_untrusted_chars(name);
190
191         switch (print) {
192         case PRINT_EXPORT:
193                 printf("ID_FS_USAGE=%s\n", vid->usage);
194                 printf("ID_FS_TYPE=%s\n", vid->type);
195                 printf("ID_FS_VERSION=%s\n", vid->type_version);
196                 printf("ID_FS_UUID=%s\n", vid->uuid);
197                 printf("ID_FS_LABEL=%s\n", vid->label);
198                 printf("ID_FS_LABEL_SAFE=%s\n", name);
199                 break;
200         case PRINT_TYPE:
201                 printf("%s\n", vid->type);
202                 break;
203         case PRINT_LABEL:
204                 if (name[0] == '\0' ||
205                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
206                         rc = 3;
207                         goto exit;
208                 }
209                 printf("%s\n", name);
210                 break;
211         case PRINT_UUID:
212                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
213                         rc = 4;
214                         goto exit;
215                 }
216                 printf("%s\n", vid->uuid);
217                 break;
218         }
219
220 exit:
221         if (vid != NULL)
222                 volume_id_close(vid);
223
224         logging_close();
225         return rc;
226 }