chiark / gitweb /
remove no longer needed includes
[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_utils.h"
33 #include "../../logging.h"
34 #include "volume_id/volume_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 set_str(char *to, const char *from, size_t count)
64 {
65         size_t i, j, len;
66
67         /* strip trailing whitespace */
68         len = strnlen(from, count);
69         while (len && isspace(from[len-1]))
70                 len--;
71
72         /* strip leading whitespace */
73         i = 0;
74         while (isspace(from[i]) && (i < len))
75                 i++;
76
77         j = 0;
78         while (i < len) {
79                 /* substitute multiple whitespace */
80                 if (isspace(from[i])) {
81                         while (isspace(from[i]))
82                                 i++;
83                         to[j++] = '_';
84                 }
85                 /* skip chars */
86                 if (from[i] == '/') {
87                         i++;
88                         continue;
89                 }
90                 to[j++] = from[i++];
91         }
92         to[j] = '\0';
93 }
94
95 int main(int argc, char *argv[])
96 {
97         const char help[] = "usage: vol_id [--export|-t|-l|-u] <device>\n"
98                             "       --export\n"
99                             "       -t filesystem type\n"
100                             "       -l filesystem label\n"
101                             "       -u filesystem uuid\n"
102                             "\n";
103         enum print_type {
104                 PRINT_EXPORT,
105                 PRINT_TYPE,
106                 PRINT_LABEL,
107                 PRINT_UUID,
108         } print = PRINT_EXPORT;
109         struct volume_id *vid = NULL;
110         static char name[VOLUME_ID_LABEL_SIZE];
111         int i;
112         uint64_t size;
113         const char *node = NULL;
114         int rc = 0;
115
116         logging_init("vol_id");
117
118         for (i = 1 ; i < argc; i++) {
119                 char *arg = argv[i];
120
121                 if (strcmp(arg, "--export") == 0) {
122                         print = PRINT_EXPORT;
123                 } else if (strcmp(arg, "-t") == 0) {
124                         print = PRINT_TYPE;
125                 } else if (strcmp(arg, "-l") == 0) {
126                         print = PRINT_LABEL;
127                 } else if (strcmp(arg, "-u") == 0) {
128                         print = PRINT_UUID;
129                 } else
130                         node = arg;
131         }
132         if (!node) {
133                 err("no node specified");
134                 fprintf(stderr, help);
135                 rc = 1;
136                 goto exit;
137         }
138
139         vid = volume_id_open_node(node);
140         if (vid == NULL) {
141                 fprintf(stderr, "%s: error open volume\n", node);
142                 rc = 2;
143                 goto exit;
144         }
145
146         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
147                 size = 0;
148         dbg("BLKGETSIZE64=%llu", size);
149
150         if (volume_id_probe_all(vid, 0, size) == 0)
151                 goto print;
152
153         if (print != PRINT_EXPORT)
154                 fprintf(stderr, "%s: unknown volume type\n", node);
155         rc = 3;
156         goto exit;
157
158 print:
159         set_str(name, vid->label, sizeof(vid->label));
160         replace_untrusted_chars(name);
161
162         switch (print) {
163         case PRINT_EXPORT:
164                 printf("ID_FS_USAGE=%s\n", vid->usage);
165                 printf("ID_FS_TYPE=%s\n", vid->type);
166                 printf("ID_FS_VERSION=%s\n", vid->type_version);
167                 printf("ID_FS_UUID=%s\n", vid->uuid);
168                 printf("ID_FS_LABEL=%s\n", vid->label);
169                 printf("ID_FS_LABEL_SAFE=%s\n", name);
170                 break;
171         case PRINT_TYPE:
172                 printf("%s\n", vid->type);
173                 break;
174         case PRINT_LABEL:
175                 if (name[0] == '\0' ||
176                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
177                         rc = 3;
178                         goto exit;
179                 }
180                 printf("%s\n", name);
181                 break;
182         case PRINT_UUID:
183                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
184                         rc = 4;
185                         goto exit;
186                 }
187                 printf("%s\n", vid->uuid);
188                 break;
189         }
190
191 exit:
192         if (vid != NULL)
193                 volume_id_close(vid);
194
195         logging_close();
196         return rc;
197 }