chiark / gitweb /
replace libsysfs
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * vol_id - udev callout to read filesystem label and uuid
3  *
4  * Copyright (C) 2005 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 <sys/ioctl.h>
31
32 #include "../../udev.h"
33 #include "libvolume_id/volume_id.h"
34
35 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
36
37 #ifdef USE_LOG
38 void log_message(int priority, const char *format, ...)
39 {
40         va_list args;
41         static int udev_log = -1;
42
43         if (udev_log == -1) {
44                 const char *value;
45
46                 value = getenv("UDEV_LOG");
47                 if (value)
48                         udev_log = log_priority(value);
49                 else
50                         udev_log = LOG_ERR;
51         }
52
53         if (priority > udev_log)
54                 return;
55
56         va_start(args, format);
57         vsyslog(priority, format, args);
58         va_end(args);
59 }
60 #endif
61
62 static void set_str(char *to, const char *from, size_t count)
63 {
64         size_t i, j, len;
65
66         /* strip trailing whitespace */
67         len = strnlen(from, count);
68         while (len && isspace(from[len-1]))
69                 len--;
70
71         /* strip leading whitespace */
72         i = 0;
73         while (isspace(from[i]) && (i < len))
74                 i++;
75
76         j = 0;
77         while (i < len) {
78                 /* substitute multiple whitespace */
79                 if (isspace(from[i])) {
80                         while (isspace(from[i]))
81                                 i++;
82                         to[j++] = '_';
83                 }
84                 /* skip chars */
85                 if (from[i] == '/') {
86                         i++;
87                         continue;
88                 }
89                 to[j++] = from[i++];
90         }
91         to[j] = '\0';
92 }
93
94 int main(int argc, char *argv[])
95 {
96         const char help[] = "usage: vol_id [--export|-t|-l|-u] <device>\n"
97                             "       --export\n"
98                             "       -t filesystem type\n"
99                             "       -l filesystem label\n"
100                             "       -u filesystem uuid\n"
101                             "\n";
102         enum print_type {
103                 PRINT_EXPORT,
104                 PRINT_TYPE,
105                 PRINT_LABEL,
106                 PRINT_UUID,
107         } print = PRINT_EXPORT;
108         struct volume_id *vid = NULL;
109         static char name[VOLUME_ID_LABEL_SIZE];
110         int i;
111         uint64_t size;
112         const char *node = NULL;
113         int rc = 0;
114
115         logging_init("vol_id");
116
117         for (i = 1 ; i < argc; i++) {
118                 char *arg = argv[i];
119
120                 if (strcmp(arg, "--export") == 0) {
121                         print = PRINT_EXPORT;
122                 } else if (strcmp(arg, "-t") == 0) {
123                         print = PRINT_TYPE;
124                 } else if (strcmp(arg, "-l") == 0) {
125                         print = PRINT_LABEL;
126                 } else if (strcmp(arg, "-u") == 0) {
127                         print = PRINT_UUID;
128                 } else
129                         node = arg;
130         }
131         if (!node) {
132                 err("no node specified");
133                 fprintf(stderr, help);
134                 rc = 1;
135                 goto exit;
136         }
137
138         vid = volume_id_open_node(node);
139         if (vid == NULL) {
140                 fprintf(stderr, "%s: error open volume\n", node);
141                 rc = 2;
142                 goto exit;
143         }
144
145         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
146                 size = 0;
147         dbg("BLKGETSIZE64=%llu", size);
148
149         if (volume_id_probe_all(vid, 0, size) == 0)
150                 goto print;
151
152         if (print != PRINT_EXPORT)
153                 fprintf(stderr, "%s: unknown volume type\n", node);
154         rc = 3;
155         goto exit;
156
157 print:
158         set_str(name, vid->label, sizeof(vid->label));
159         replace_untrusted_chars(name);
160
161         switch (print) {
162         case PRINT_EXPORT:
163                 printf("ID_FS_USAGE=%s\n", vid->usage);
164                 printf("ID_FS_TYPE=%s\n", vid->type);
165                 printf("ID_FS_VERSION=%s\n", vid->type_version);
166                 printf("ID_FS_UUID=%s\n", vid->uuid);
167                 printf("ID_FS_LABEL=%s\n", vid->label);
168                 printf("ID_FS_LABEL_SAFE=%s\n", name);
169                 break;
170         case PRINT_TYPE:
171                 printf("%s\n", vid->type);
172                 break;
173         case PRINT_LABEL:
174                 if (name[0] == '\0' ||
175                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
176                         rc = 3;
177                         goto exit;
178                 }
179                 printf("%s\n", name);
180                 break;
181         case PRINT_UUID:
182                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
183                         rc = 4;
184                         goto exit;
185                 }
186                 printf("%s\n", vid->uuid);
187                 break;
188         }
189
190 exit:
191         if (vid != NULL)
192                 volume_id_close(vid);
193
194         logging_close();
195         return rc;
196 }