]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/blkdev.c
setproctitle: fix out of boundary access
[thirdparty/util-linux.git] / lib / blkdev.c
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 */
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11 #include <stdint.h>
12
13 #ifdef HAVE_LINUX_FD_H
14 #include <linux/fd.h>
15 #endif
16
17 #ifdef HAVE_SYS_DISKLABEL_H
18 #include <sys/disklabel.h>
19 #endif
20
21 #ifdef HAVE_SYS_DISK_H
22 # include <sys/disk.h>
23 #endif
24
25 #ifndef EBADFD
26 # define EBADFD 77 /* File descriptor in bad state */
27 #endif
28
29 #include "blkdev.h"
30 #include "c.h"
31 #include "linux_version.h"
32 #include "fileutils.h"
33 #include "nls.h"
34
35 static long
36 blkdev_valid_offset (int fd, off_t offset) {
37 char ch;
38
39 if (lseek (fd, offset, 0) < 0)
40 return 0;
41 if (read (fd, &ch, 1) < 1)
42 return 0;
43 return 1;
44 }
45
46 int is_blkdev(int fd)
47 {
48 struct stat st;
49 return (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode));
50 }
51
52 off_t
53 blkdev_find_size (int fd) {
54 uintmax_t high, low = 0;
55
56 for (high = 1024; blkdev_valid_offset (fd, high); ) {
57 if (high == UINTMAX_MAX)
58 return -1;
59
60 low = high;
61
62 if (high >= UINTMAX_MAX/2)
63 high = UINTMAX_MAX;
64 else
65 high *= 2;
66 }
67
68 while (low < high - 1)
69 {
70 uintmax_t mid = (low + high) / 2;
71
72 if (blkdev_valid_offset (fd, mid))
73 low = mid;
74 else
75 high = mid;
76 }
77 blkdev_valid_offset (fd, 0);
78 return (low + 1);
79 }
80
81 /* get size in bytes */
82 int
83 blkdev_get_size(int fd, unsigned long long *bytes)
84 {
85 #ifdef DKIOCGETBLOCKCOUNT
86 /* Apple Darwin */
87 if (ioctl(fd, DKIOCGETBLOCKCOUNT, bytes) >= 0) {
88 *bytes <<= 9;
89 return 0;
90 }
91 #endif
92
93 #ifdef BLKGETSIZE64
94 if (ioctl(fd, BLKGETSIZE64, bytes) >= 0)
95 return 0;
96 #endif
97
98 #ifdef BLKGETSIZE
99 {
100 unsigned long size;
101
102 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
103 *bytes = ((unsigned long long)size << 9);
104 return 0;
105 }
106 }
107
108 #endif /* BLKGETSIZE */
109
110 #ifdef DIOCGMEDIASIZE
111 /* FreeBSD */
112 if (ioctl(fd, DIOCGMEDIASIZE, bytes) >= 0)
113 return 0;
114 #endif
115
116 #ifdef FDGETPRM
117 {
118 struct floppy_struct this_floppy;
119
120 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
121 *bytes = ((unsigned long long) this_floppy.size) << 9;
122 return 0;
123 }
124 }
125 #endif /* FDGETPRM */
126
127 #if defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO)
128 {
129 /*
130 * This code works for FreeBSD 4.11 i386, except for the full device
131 * (such as /dev/ad0). It doesn't work properly for newer FreeBSD
132 * though. FreeBSD >= 5.0 should be covered by the DIOCGMEDIASIZE
133 * above however.
134 *
135 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
136 * character) devices, so we need to check for S_ISCHR, too.
137 */
138 int part = -1;
139 struct disklabel lab;
140 struct partition *pp;
141 struct stat st;
142
143 if ((fstat(fd, &st) >= 0) &&
144 (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
145 part = st.st_rdev & 7;
146
147 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
148 pp = &lab.d_partitions[part];
149 if (pp->p_size) {
150 *bytes = pp->p_size << 9;
151 return 0;
152 }
153 }
154 }
155 #endif /* defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO) */
156
157 {
158 struct stat st;
159
160 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
161 *bytes = st.st_size;
162 return 0;
163 }
164 if (!S_ISBLK(st.st_mode))
165 return -1;
166 }
167
168 *bytes = blkdev_find_size(fd);
169 return 0;
170 }
171
172 /* get 512-byte sector count */
173 int
174 blkdev_get_sectors(int fd, unsigned long long *sectors)
175 {
176 unsigned long long bytes;
177
178 if (blkdev_get_size(fd, &bytes) == 0) {
179 *sectors = (bytes >> 9);
180 return 0;
181 }
182
183 return -1;
184 }
185
186 /*
187 * Get logical sector size.
188 *
189 * This is the smallest unit the storage device can
190 * address. It is typically 512 bytes.
191 */
192 #ifdef BLKSSZGET
193 int blkdev_get_sector_size(int fd, int *sector_size)
194 {
195 if (ioctl(fd, BLKSSZGET, sector_size) >= 0)
196 return 0;
197 return -1;
198 }
199 #else
200 int blkdev_get_sector_size(int fd __attribute__((__unused__)), int *sector_size)
201 {
202 *sector_size = DEFAULT_SECTOR_SIZE;
203 return 0;
204 }
205 #endif
206
207 /*
208 * Get physical block device size. The BLKPBSZGET is supported since Linux
209 * 2.6.32. For old kernels is probably the best to assume that physical sector
210 * size is the same as logical sector size.
211 *
212 * Example:
213 *
214 * rc = blkdev_get_physector_size(fd, &physec);
215 * if (rc || physec == 0) {
216 * rc = blkdev_get_sector_size(fd, &physec);
217 * if (rc)
218 * physec = DEFAULT_SECTOR_SIZE;
219 * }
220 */
221 #ifdef BLKPBSZGET
222 int blkdev_get_physector_size(int fd, int *sector_size)
223 {
224 if (ioctl(fd, BLKPBSZGET, &sector_size) >= 0)
225 return 0;
226 return -1;
227 }
228 #else
229 int blkdev_get_physector_size(int fd __attribute__((__unused__)), int *sector_size)
230 {
231 *sector_size = DEFAULT_SECTOR_SIZE;
232 return 0;
233 }
234 #endif
235
236 /*
237 * Return the alignment status of a device
238 */
239 #ifdef BLKALIGNOFF
240 int blkdev_is_misaligned(int fd)
241 {
242 int aligned;
243
244 if (ioctl(fd, BLKALIGNOFF, &aligned) < 0)
245 return 0; /* probably kernel < 2.6.32 */
246 /*
247 * Note that kernel returns -1 as alignment offset if no compatible
248 * sizes and alignments exist for stacked devices
249 */
250 return aligned != 0 ? 1 : 0;
251 }
252 #else
253 int blkdev_is_misaligned(int fd __attribute__((__unused__)))
254 {
255 return 0;
256 }
257 #endif
258
259 int open_blkdev_or_file(const struct stat *st, const char *name, const int oflag)
260 {
261 int fd;
262
263 if (S_ISBLK(st->st_mode)) {
264 fd = open(name, oflag | O_EXCL);
265 } else
266 fd = open(name, oflag);
267 if (-1 < fd && !is_same_inode(fd, st)) {
268 close(fd);
269 errno = EBADFD;
270 return -1;
271 }
272 if (-1 < fd && S_ISBLK(st->st_mode) && blkdev_is_misaligned(fd))
273 warnx(_("warning: %s is misaligned"), name);
274 return fd;
275 }
276
277 #ifdef CDROM_GET_CAPABILITY
278 int blkdev_is_cdrom(int fd)
279 {
280 int ret;
281
282 if ((ret = ioctl(fd, CDROM_GET_CAPABILITY, NULL)) < 0)
283 return 0;
284 else
285 return ret;
286 }
287 #else
288 int blkdev_is_cdrom(int fd __attribute__((__unused__)))
289 {
290 return 0;
291 }
292 #endif
293
294 /*
295 * Get kernel's interpretation of the device's geometry.
296 *
297 * Returns the heads and sectors - but not cylinders
298 * as it's truncated for disks with more than 65535 tracks.
299 *
300 * Note that this is deprecated in favor of LBA addressing.
301 */
302 #ifdef HDIO_GETGEO
303 int blkdev_get_geometry(int fd, unsigned int *h, unsigned int *s)
304 {
305 struct hd_geometry geometry;
306
307 if (ioctl(fd, HDIO_GETGEO, &geometry) == 0) {
308 *h = geometry.heads;
309 *s = geometry.sectors;
310 return 0;
311 }
312 #else
313 int blkdev_get_geometry(int fd __attribute__((__unused__)),
314 unsigned int *h, unsigned int *s)
315 {
316 *h = 0;
317 *s = 0;
318 #endif
319 return -1;
320 }
321
322 /*
323 * Convert scsi type to human readable string.
324 */
325 const char *blkdev_scsi_type_to_name(int type)
326 {
327 switch (type) {
328 case SCSI_TYPE_DISK:
329 return "disk";
330 case SCSI_TYPE_TAPE:
331 return "tape";
332 case SCSI_TYPE_PRINTER:
333 return "printer";
334 case SCSI_TYPE_PROCESSOR:
335 return "processor";
336 case SCSI_TYPE_WORM:
337 return "worm";
338 case SCSI_TYPE_ROM:
339 return "rom";
340 case SCSI_TYPE_SCANNER:
341 return "scanner";
342 case SCSI_TYPE_MOD:
343 return "mo-disk";
344 case SCSI_TYPE_MEDIUM_CHANGER:
345 return "changer";
346 case SCSI_TYPE_COMM:
347 return "comm";
348 case SCSI_TYPE_RAID:
349 return "raid";
350 case SCSI_TYPE_ENCLOSURE:
351 return "enclosure";
352 case SCSI_TYPE_RBC:
353 return "rbc";
354 case SCSI_TYPE_OSD:
355 return "osd";
356 case SCSI_TYPE_NO_LUN:
357 return "no-lun";
358 default:
359 break;
360 }
361 return NULL;
362 }
363
364 #ifdef TEST_PROGRAM_BLKDEV
365 #include <stdio.h>
366 #include <stdlib.h>
367 #include <fcntl.h>
368 int
369 main(int argc, char **argv)
370 {
371 unsigned long long bytes;
372 unsigned long long sectors;
373 int sector_size, phy_sector_size;
374 int fd;
375
376 if (argc != 2) {
377 fprintf(stderr, "usage: %s device\n", argv[0]);
378 exit(EXIT_FAILURE);
379 }
380
381 if ((fd = open(argv[1], O_RDONLY|O_CLOEXEC)) < 0)
382 err(EXIT_FAILURE, "open %s failed", argv[1]);
383
384 if (blkdev_get_size(fd, &bytes) < 0)
385 err(EXIT_FAILURE, "blkdev_get_size() failed");
386 if (blkdev_get_sectors(fd, &sectors) < 0)
387 err(EXIT_FAILURE, "blkdev_get_sectors() failed");
388 if (blkdev_get_sector_size(fd, &sector_size) < 0)
389 err(EXIT_FAILURE, "blkdev_get_sector_size() failed");
390 if (blkdev_get_physector_size(fd, &phy_sector_size) < 0)
391 err(EXIT_FAILURE, "blkdev_get_physector_size() failed");
392
393 printf(" bytes: %llu\n", bytes);
394 printf(" sectors: %llu\n", sectors);
395 printf(" sector size: %d\n", sector_size);
396 printf("phy-sector size: %d\n", phy_sector_size);
397
398 return EXIT_SUCCESS;
399 }
400 #endif /* TEST_PROGRAM_BLKDEV */