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