chiark / gitweb /
rename udev_volume_id to vol_id and add --export option
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * udev_volume_id - udev callout to read filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      sample udev rule for creation of a symlink with the filsystem uuid:
7  *      KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -u %N", SYMLINK="%c"
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sys/ioctl.h>
34
35 #include "../../udev_utils.h"
36 #include "../../logging.h"
37 #include "volume_id/volume_id.h"
38 #include "volume_id/dasd.h"
39
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
41
42 #ifdef USE_LOG
43 void log_message(int priority, const char *format, ...)
44 {
45         va_list args;
46         static int udev_log = -1;
47
48         if (udev_log == -1) {
49                 const char *value;
50
51                 value = getenv("UDEV_LOG");
52                 if (value)
53                         udev_log = log_priority(value);
54                 else
55                         udev_log = LOG_ERR;
56         }
57
58         if (priority > udev_log)
59                 return;
60
61         va_start(args, format);
62         vsyslog(priority, format, args);
63         va_end(args);
64 }
65 #endif
66
67 static void set_str(char *to, const unsigned char *from, int count)
68 {
69         int i, j;
70         int len;
71
72         len = strnlen(from, count);
73         while (isspace(from[len-1]))
74                 len--;
75
76         i = 0;
77         while (isspace(from[i]) && (i < len))
78                 i++;
79
80         j = 0;
81         while (i < len) {
82                 switch(from[i]) {
83                 case '/':
84                         break;
85                 case ' ':
86                         to[j++] = '_';
87                         break;
88                 default:
89                         to[j++] = from[i];
90                 }
91                 i++;
92         }
93         to[j] = '\0';
94 }
95
96 int main(int argc, char *argv[])
97 {
98         const char help[] = "usage: udev_volume_id [--export|-t|-l|-u] <device>\n"
99                             "       --export\n"
100                             "       -t filesystem type\n"
101                             "       -l filesystem label\n"
102                             "       -u filesystem uuid\n"
103                             "\n";
104         enum print_type {
105                 PRINT_EXPORT,
106                 PRINT_TYPE,
107                 PRINT_LABEL,
108                 PRINT_UUID,
109         } print = PRINT_EXPORT;
110         struct volume_id *vid = NULL;
111         static char name[VOLUME_ID_LABEL_SIZE];
112         int i;
113         unsigned long long size;
114         const char *node = NULL;
115         int rc = 0;
116
117         logging_init("udev_volume_id");
118
119         for (i = 1 ; i < argc; i++) {
120                 char *arg = argv[i];
121
122                 if (strcmp(arg, "--export") == 0) {
123                         print = PRINT_EXPORT;
124                 } else if (strcmp(arg, "-t") == 0) {
125                         print = PRINT_TYPE;
126                 } else if (strcmp(arg, "-l") == 0) {
127                         print = PRINT_LABEL;
128                 } else if (strcmp(arg, "-u") == 0) {
129                         print = PRINT_UUID;
130                 } else
131                         node = arg;
132         }
133         if (!node) {
134                 err("no node specified");
135                 fprintf(stderr, help);
136                 rc = 1;
137                 goto exit;
138         }
139
140         vid = volume_id_open_node(node);
141         if (vid == NULL) {
142                 fprintf(stderr, "error open volume\n");
143                 rc = 2;
144                 goto exit;
145         }
146
147         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
148                 size = 0;
149
150         if (volume_id_probe_all(vid, 0, size) == 0)
151                 goto print;
152
153         if (volume_id_probe_dasd(vid) == 0)
154                 goto print;
155
156         fprintf(stderr, "unknown volume type\n");
157         rc = 3;
158         goto exit;
159
160 print:
161         set_str(name, vid->label, sizeof(vid->label));
162
163         switch (print) {
164         case PRINT_EXPORT:
165                 printf("ID_FS_USAGE=%s\n", vid->usage);
166                 printf("ID_FS_TYPE=%s\n", vid->type);
167                 printf("ID_FS_VERSION=%s\n", vid->type_version);
168                 printf("ID_FS_UUID=%s\n", vid->uuid);
169                 printf("ID_FS_LABEL=%s\n", vid->label);
170                 printf("ID_FS_LABEL_SAFE=%s\n", name);
171                 break;
172         case PRINT_TYPE:
173                 printf("%s\n", vid->type);
174                 break;
175         case PRINT_LABEL:
176                 if (name[0] == '\0' ||
177                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
178                         rc = 3;
179                         goto exit;
180                 }
181                 printf("%s\n", name);
182                 break;
183         case PRINT_UUID:
184                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
185                         rc = 4;
186                         goto exit;
187                 }
188                 printf("%s\n", vid->uuid);
189                 break;
190         }
191
192 exit:
193         if (vid != NULL)
194                 volume_id_close(vid);
195
196         logging_close();
197         return rc;
198 }