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