Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Eric Andersen | 1e4dc96 | 2005-01-04 20:37:55 +0000 | [diff] [blame] | 22 | #include <features.h> |
Eric Andersen | 1271dbb | 2005-01-03 05:50:01 +0000 | [diff] [blame] | 23 | #if defined (__GLIBC__) && !defined(__UCLIBC__) |
Eric Andersen | 1e4dc96 | 2005-01-04 20:37:55 +0000 | [diff] [blame] | 24 | #include <linux/posix_types.h> |
Eric Andersen | 1271dbb | 2005-01-03 05:50:01 +0000 | [diff] [blame] | 25 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include "libbb.h" |
Eric Andersen | ef8cd3b | 2004-02-06 07:16:36 +0000 | [diff] [blame] | 33 | |
| 34 | /* Grumble... The 2.6.x kernel breaks asm/posix_types.h |
| 35 | * so we get to try and cope as best we can... */ |
| 36 | #include <linux/version.h> |
Glenn L McGrath | 774e6cc | 2004-06-25 09:01:09 +0000 | [diff] [blame] | 37 | |
Eric Andersen | cf6ef05 | 2004-08-16 08:29:44 +0000 | [diff] [blame] | 38 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) |
Eric Andersen | 0722513 | 2004-08-16 08:36:28 +0000 | [diff] [blame] | 39 | #define __bb_kernel_dev_t __kernel_old_dev_t |
| 40 | #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
Glenn L McGrath | 774e6cc | 2004-06-25 09:01:09 +0000 | [diff] [blame] | 41 | #define __bb_kernel_dev_t __kernel_dev_t |
Eric Andersen | ef8cd3b | 2004-02-06 07:16:36 +0000 | [diff] [blame] | 42 | #else |
| 43 | #define __bb_kernel_dev_t unsigned short |
| 44 | #endif |
| 45 | |
| 46 | /* Stuff stolen from linux/loop.h */ |
| 47 | #define LO_NAME_SIZE 64 |
| 48 | #define LO_KEY_SIZE 32 |
| 49 | #define LOOP_SET_FD 0x4C00 |
| 50 | #define LOOP_CLR_FD 0x4C01 |
| 51 | #define LOOP_SET_STATUS 0x4C02 |
| 52 | #define LOOP_GET_STATUS 0x4C03 |
| 53 | struct loop_info { |
| 54 | int lo_number; |
| 55 | __bb_kernel_dev_t lo_device; |
| 56 | unsigned long lo_inode; |
| 57 | __bb_kernel_dev_t lo_rdevice; |
| 58 | int lo_offset; |
| 59 | int lo_encrypt_type; |
| 60 | int lo_encrypt_key_size; |
| 61 | int lo_flags; |
| 62 | char lo_name[LO_NAME_SIZE]; |
| 63 | unsigned char lo_encrypt_key[LO_KEY_SIZE]; |
| 64 | unsigned long lo_init[2]; |
| 65 | char reserved[4]; |
| 66 | }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 67 | |
| 68 | extern int del_loop(const char *device) |
| 69 | { |
| 70 | int fd; |
| 71 | |
| 72 | if ((fd = open(device, O_RDONLY)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | bb_perror_msg("%s", device); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 74 | return (FALSE); |
| 75 | } |
| 76 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
Eric Andersen | 7b3edeb | 2003-05-02 16:25:01 +0000 | [diff] [blame] | 77 | close(fd); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | bb_perror_msg("ioctl: LOOP_CLR_FD"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 79 | return (FALSE); |
| 80 | } |
| 81 | close(fd); |
| 82 | return (TRUE); |
| 83 | } |
| 84 | |
| 85 | extern int set_loop(const char *device, const char *file, int offset, |
| 86 | int *loopro) |
| 87 | { |
| 88 | struct loop_info loopinfo; |
| 89 | int fd, ffd, mode; |
| 90 | |
| 91 | mode = *loopro ? O_RDONLY : O_RDWR; |
| 92 | if ((ffd = open(file, mode)) < 0 && !*loopro |
| 93 | && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | bb_perror_msg("%s", file); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | if ((fd = open(device, mode)) < 0) { |
| 98 | close(ffd); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | bb_perror_msg("%s", device); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 100 | return 1; |
| 101 | } |
| 102 | *loopro = (mode == O_RDONLY); |
| 103 | |
| 104 | memset(&loopinfo, 0, sizeof(loopinfo)); |
| 105 | safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); |
| 106 | |
| 107 | loopinfo.lo_offset = offset; |
| 108 | |
| 109 | loopinfo.lo_encrypt_key_size = 0; |
| 110 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 111 | bb_perror_msg("ioctl: LOOP_SET_FD"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 112 | close(fd); |
| 113 | close(ffd); |
| 114 | return 1; |
| 115 | } |
| 116 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { |
| 117 | (void) ioctl(fd, LOOP_CLR_FD, 0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | bb_perror_msg("ioctl: LOOP_SET_STATUS"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 119 | close(fd); |
| 120 | close(ffd); |
| 121 | return 1; |
| 122 | } |
| 123 | close(fd); |
| 124 | close(ffd); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | extern char *find_unused_loop_device(void) |
| 129 | { |
| 130 | char dev[20]; |
| 131 | int i, fd; |
| 132 | struct stat statbuf; |
| 133 | struct loop_info loopinfo; |
| 134 | |
| 135 | for (i = 0; i <= 7; i++) { |
Eric Andersen | c7a3fb9 | 2002-03-20 15:25:25 +0000 | [diff] [blame] | 136 | sprintf(dev, LOOP_FORMAT, i); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 137 | if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { |
| 138 | if ((fd = open(dev, O_RDONLY)) >= 0) { |
| 139 | if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) { |
| 140 | if (errno == ENXIO) { /* probably free */ |
| 141 | close(fd); |
| 142 | return strdup(dev); |
| 143 | } |
| 144 | } |
| 145 | close(fd); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /* END CODE */ |
| 154 | /* |
| 155 | Local Variables: |
| 156 | c-file-style: "linux" |
| 157 | c-basic-offset: 4 |
| 158 | tab-width: 4 |
| 159 | End: |
| 160 | */ |