chiark / gitweb /
7c6fc4050d6fb70ec5cd76d2c3257d6f911a124e
[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 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 extern int optind;
68
69 int main(int argc, char *argv[])
70 {
71         const char help[] = "usage: udev_volume_id [-t|-l|-u] <device>\n"
72                             "       -t filesystem type\n"
73                             "       -l filesystem label\n"
74                             "       -u filesystem uuid\n"
75                             "\n";
76         static const char short_options[] = "htlu";
77         struct volume_id *vid = NULL;
78         const char *device;
79         char print = 'a';
80         static char name[VOLUME_ID_LABEL_SIZE];
81         int len, i, j;
82         unsigned long long size;
83         int rc = 1;
84
85         logging_init("udev_volume_id");
86
87         while (1) {
88                 int option;
89
90                 option = getopt(argc, argv, short_options);
91                 if (option == -1)
92                         break;
93
94                 switch (option) {
95                 case 't':
96                         print = 't';
97                         continue;
98                 case 'l':
99                         print = 'l';
100                         continue;
101                 case 'u':
102                         print = 'u';
103                         continue;
104                 case 'h':
105                 case '?':
106                 default:
107                         printf(help);
108                         exit(1);
109                 }
110         }
111
112         device = argv[optind];
113         if (device == NULL) {
114                 printf(help);
115                 exit(1);
116         }
117
118         vid = volume_id_open_node(device);
119         if (vid == NULL) {
120                 printf("error open volume\n");
121                 goto exit;
122         }
123
124         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
125                 size = 0;
126
127         if (volume_id_probe_all(vid, 0, size) == 0)
128                 goto print;
129
130         if (volume_id_probe_dasd(vid) == 0)
131                 goto print;
132
133         printf("unknown volume type\n");
134         goto exit;
135
136
137 print:
138         len = strnlen(vid->label, VOLUME_ID_LABEL_SIZE);
139
140         /* remove trailing spaces */
141         while (len > 0 && isspace(vid->label[len-1]))
142                 len--;
143         name[len] = '\0';
144
145         /* substitute chars */
146         i = 0;
147         j = 0;
148         while (j < len) {
149                 switch(vid->label[j]) {
150                 case '/' :
151                         break;
152                 case ' ' :
153                         name[i++] = '_';
154                         break;
155                 default :
156                         name[i++] = vid->label[j];
157                 }
158                 j++;
159         }
160         name[i] = '\0';
161
162         switch (print) {
163         case 't':
164                 printf("%s\n", vid->type);
165                 break;
166         case 'l':
167                 if (name[0] == '\0' ||
168                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
169                         rc = 2;
170                         goto exit;
171                 }
172                 printf("%s\n", name);
173                 break;
174         case 'u':
175                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
176                         rc = 2;
177                         goto exit;
178                 }
179                 printf("%s\n", vid->uuid);
180                 break;
181         case 'a':
182                 printf("F:%s\n", vid->usage);
183                 printf("T:%s\n", vid->type);
184                 printf("V:%s\n", vid->type_version);
185                 printf("L:%s\n", vid->label);
186                 printf("N:%s\n", name);
187                 printf("U:%s\n", vid->uuid);
188         }
189         rc = 0;
190
191 exit:
192         if (vid != NULL)
193                 volume_id_close(vid);
194
195         logging_close();
196
197         exit(rc);
198 }