chiark / gitweb /
[PATCH] Dialout group fix for capi devices in the gentoo rules file
[elogind.git] / extras / volume_id / udev_volume_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 level, const char *format, ...)
44 {
45         va_list args;
46
47         va_start(args, format);
48         vsyslog(level, format, args);
49         va_end(args);
50 }
51 #endif
52
53 extern int optind;
54
55 int main(int argc, char *argv[])
56 {
57         const char help[] = "usage: udev_volume_id [-t|-l|-u] <device>\n"
58                             "       -t filesystem type\n"
59                             "       -l filesystem label\n"
60                             "       -u filesystem uuid\n"
61                             "\n";
62         static const char short_options[] = "htlu";
63         struct volume_id *vid = NULL;
64         const char *device;
65         char print = 'a';
66         static char name[VOLUME_ID_LABEL_SIZE];
67         int len, i, j;
68         unsigned long long size;
69         int rc = 1;
70
71         logging_init("udev_volume_id");
72
73         while (1) {
74                 int option;
75
76                 option = getopt(argc, argv, short_options);
77                 if (option == -1)
78                         break;
79
80                 switch (option) {
81                 case 't':
82                         print = 't';
83                         continue;
84                 case 'l':
85                         print = 'l';
86                         continue;
87                 case 'u':
88                         print = 'u';
89                         continue;
90                 case 'h':
91                 case '?':
92                 default:
93                         printf(help);
94                         exit(1);
95                 }
96         }
97
98         device = argv[optind];
99         if (device == NULL) {
100                 printf(help);
101                 exit(1);
102         }
103
104         vid = volume_id_open_node(device);
105         if (vid == NULL) {
106                 printf("error open volume\n");
107                 goto exit;
108         }
109
110         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
111                 size = 0;
112
113         if (volume_id_probe_all(vid, 0, size) == 0)
114                 goto print;
115
116         if (volume_id_probe_dasd(vid) == 0)
117                 goto print;
118
119         printf("unknown volume type\n");
120         goto exit;
121
122
123 print:
124         len = strnlen(vid->label, VOLUME_ID_LABEL_SIZE);
125
126         /* remove trailing spaces */
127         while (len > 0 && isspace(vid->label[len-1]))
128                 len--;
129         name[len] = '\0';
130
131         /* substitute chars */
132         i = 0;
133         j = 0;
134         while (j < len) {
135                 switch(vid->label[j]) {
136                 case '/' :
137                         break;
138                 case ' ' :
139                         name[i++] = '_';
140                         break;
141                 default :
142                         name[i++] = vid->label[j];
143                 }
144                 j++;
145         }
146         name[i] = '\0';
147
148         switch (print) {
149         case 't':
150                 printf("%s\n", vid->type);
151                 break;
152         case 'l':
153                 if (name[0] == '\0' ||
154                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
155                         rc = 2;
156                         goto exit;
157                 }
158                 printf("%s\n", name);
159                 break;
160         case 'u':
161                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
162                         rc = 2;
163                         goto exit;
164                 }
165                 printf("%s\n", vid->uuid);
166                 break;
167         case 'a':
168                 printf("F:%s\n", vid->usage);
169                 printf("T:%s\n", vid->type);
170                 printf("V:%s\n", vid->type_version);
171                 printf("L:%s\n", vid->label);
172                 printf("N:%s\n", name);
173                 printf("U:%s\n", vid->uuid);
174         }
175         rc = 0;
176
177 exit:
178         if (vid != NULL)
179                 volume_id_close(vid);
180
181         logging_close();
182
183         exit(rc);
184 }