chiark / gitweb /
vol_id: fix logging from libvolume_id's log function
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * vol_id - read filesystem label and uuid
3  *
4  * Copyright (C) 2005-2006 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 <grp.h>
31 #include <sys/ioctl.h>
32
33 #include "../../udev.h"
34 #include "lib/libvolume_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 vid_log(int priority, const char *file, int line, const char *format, ...)
64 {
65 #ifdef USE_LOG
66         char log_str[1024];
67         va_list args;
68
69         va_start(args, format);
70         vsnprintf(log_str, sizeof(log_str), format, args);
71         log_str[sizeof(log_str)-1] = '\0';
72         log_message(priority, "%s:%i %s", file, line, log_str);
73         va_end(args);
74 #endif
75         return;
76 }
77
78 static void set_str(char *to, const char *from, size_t count)
79 {
80         size_t i, j, len;
81
82         /* strip trailing whitespace */
83         len = strnlen(from, count);
84         while (len && isspace(from[len-1]))
85                 len--;
86
87         /* strip leading whitespace */
88         i = 0;
89         while (isspace(from[i]) && (i < len))
90                 i++;
91
92         j = 0;
93         while (i < len) {
94                 /* substitute multiple whitespace */
95                 if (isspace(from[i])) {
96                         while (isspace(from[i]))
97                                 i++;
98                         to[j++] = '_';
99                 }
100                 /* skip chars */
101                 if (from[i] == '/') {
102                         i++;
103                         continue;
104                 }
105                 to[j++] = from[i++];
106         }
107         to[j] = '\0';
108 }
109
110 int main(int argc, char *argv[])
111 {
112         const char help[] = "usage: vol_id [--export|-t|-l|-u] <device>\n"
113                             "       --export\n"
114                             "       -t filesystem type\n"
115                             "       -l filesystem label\n"
116                             "       -u filesystem uuid\n"
117                             "\n";
118         enum print_type {
119                 PRINT_EXPORT,
120                 PRINT_TYPE,
121                 PRINT_LABEL,
122                 PRINT_UUID,
123         } print = PRINT_EXPORT;
124         struct volume_id *vid = NULL;
125         static char name[VOLUME_ID_LABEL_SIZE];
126         int i;
127         uint64_t size;
128         const char *node = NULL;
129         uid_t nobody_uid;
130         gid_t nobody_gid;
131         int rc = 0;
132
133         logging_init("vol_id");
134
135         /* hook in our debug into libvolume_id */
136         volume_id_log_fn = vid_log;
137
138         for (i = 1 ; i < argc; i++) {
139                 char *arg = argv[i];
140
141                 if (strcmp(arg, "--export") == 0) {
142                         print = PRINT_EXPORT;
143                 } else if (strcmp(arg, "-t") == 0) {
144                         print = PRINT_TYPE;
145                 } else if (strcmp(arg, "-l") == 0) {
146                         print = PRINT_LABEL;
147                 } else if (strcmp(arg, "-u") == 0) {
148                         print = PRINT_UUID;
149                 } else
150                         node = arg;
151         }
152         if (!node) {
153                 err("no node specified");
154                 fprintf(stderr, help);
155                 rc = 1;
156                 goto exit;
157         }
158
159         vid = volume_id_open_node(node);
160         if (vid == NULL) {
161                 fprintf(stderr, "%s: error open volume\n", node);
162                 rc = 2;
163                 goto exit;
164         }
165
166         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
167                 size = 0;
168         dbg("BLKGETSIZE64=%llu", size);
169
170         /* drop all privileges */
171         nobody_uid = lookup_user("nobody");
172         nobody_gid = lookup_group("nogroup");
173         if (nobody_uid > 0 && nobody_gid > 0) {
174                 if (setgroups(0, NULL) != 0 ||
175                     setgid(nobody_gid) != 0 ||
176                     setuid(nobody_uid) != 0) {
177                         rc = 3;
178                         goto exit;
179                 }
180         }
181
182         if (volume_id_probe_all(vid, 0, size) == 0)
183                 goto print;
184
185         if (print != PRINT_EXPORT)
186                 fprintf(stderr, "%s: unknown volume type\n", node);
187         rc = 4;
188         goto exit;
189
190 print:
191         set_str(name, vid->label, sizeof(vid->label));
192         replace_untrusted_chars(name);
193
194         switch (print) {
195         case PRINT_EXPORT:
196                 printf("ID_FS_USAGE=%s\n", vid->usage);
197                 printf("ID_FS_TYPE=%s\n", vid->type);
198                 printf("ID_FS_VERSION=%s\n", vid->type_version);
199                 printf("ID_FS_UUID=%s\n", vid->uuid);
200                 printf("ID_FS_LABEL=%s\n", vid->label);
201                 printf("ID_FS_LABEL_SAFE=%s\n", name);
202                 break;
203         case PRINT_TYPE:
204                 printf("%s\n", vid->type);
205                 break;
206         case PRINT_LABEL:
207                 if (name[0] == '\0' ||
208                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
209                         rc = 3;
210                         goto exit;
211                 }
212                 printf("%s\n", name);
213                 break;
214         case PRINT_UUID:
215                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
216                         rc = 4;
217                         goto exit;
218                 }
219                 printf("%s\n", vid->uuid);
220                 break;
221         }
222
223 exit:
224         if (vid != NULL)
225                 volume_id_close(vid);
226
227         logging_close();
228         return rc;
229 }