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 | |
Rob Landley | 861f014 | 2004-12-09 23:12:00 +0000 | [diff] [blame] | 22 | #include <asm/posix_types.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include "libbb.h" |
Eric Andersen | ef8cd3b | 2004-02-06 07:16:36 +0000 | [diff] [blame] | 30 | |
| 31 | /* Grumble... The 2.6.x kernel breaks asm/posix_types.h |
| 32 | * so we get to try and cope as best we can... */ |
| 33 | #include <linux/version.h> |
Glenn L McGrath | 774e6cc | 2004-06-25 09:01:09 +0000 | [diff] [blame] | 34 | |
Eric Andersen | cf6ef05 | 2004-08-16 08:29:44 +0000 | [diff] [blame] | 35 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) |
Eric Andersen | 0722513 | 2004-08-16 08:36:28 +0000 | [diff] [blame] | 36 | #define __bb_kernel_dev_t __kernel_old_dev_t |
| 37 | #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
Glenn L McGrath | 774e6cc | 2004-06-25 09:01:09 +0000 | [diff] [blame] | 38 | #define __bb_kernel_dev_t __kernel_dev_t |
Eric Andersen | ef8cd3b | 2004-02-06 07:16:36 +0000 | [diff] [blame] | 39 | #else |
| 40 | #define __bb_kernel_dev_t unsigned short |
| 41 | #endif |
| 42 | |
| 43 | /* Stuff stolen from linux/loop.h */ |
| 44 | #define LO_NAME_SIZE 64 |
| 45 | #define LO_KEY_SIZE 32 |
| 46 | #define LOOP_SET_FD 0x4C00 |
| 47 | #define LOOP_CLR_FD 0x4C01 |
| 48 | #define LOOP_SET_STATUS 0x4C02 |
| 49 | #define LOOP_GET_STATUS 0x4C03 |
| 50 | struct loop_info { |
| 51 | int lo_number; |
| 52 | __bb_kernel_dev_t lo_device; |
| 53 | unsigned long lo_inode; |
| 54 | __bb_kernel_dev_t lo_rdevice; |
| 55 | int lo_offset; |
| 56 | int lo_encrypt_type; |
| 57 | int lo_encrypt_key_size; |
| 58 | int lo_flags; |
| 59 | char lo_name[LO_NAME_SIZE]; |
| 60 | unsigned char lo_encrypt_key[LO_KEY_SIZE]; |
| 61 | unsigned long lo_init[2]; |
| 62 | char reserved[4]; |
| 63 | }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 64 | |
| 65 | extern int del_loop(const char *device) |
| 66 | { |
| 67 | int fd; |
| 68 | |
| 69 | if ((fd = open(device, O_RDONLY)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | bb_perror_msg("%s", device); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 71 | return (FALSE); |
| 72 | } |
| 73 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
Eric Andersen | 7b3edeb | 2003-05-02 16:25:01 +0000 | [diff] [blame] | 74 | close(fd); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_perror_msg("ioctl: LOOP_CLR_FD"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 76 | return (FALSE); |
| 77 | } |
| 78 | close(fd); |
| 79 | return (TRUE); |
| 80 | } |
| 81 | |
| 82 | extern int set_loop(const char *device, const char *file, int offset, |
| 83 | int *loopro) |
| 84 | { |
| 85 | struct loop_info loopinfo; |
| 86 | int fd, ffd, mode; |
| 87 | |
| 88 | mode = *loopro ? O_RDONLY : O_RDWR; |
| 89 | if ((ffd = open(file, mode)) < 0 && !*loopro |
| 90 | && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | bb_perror_msg("%s", file); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 92 | return 1; |
| 93 | } |
| 94 | if ((fd = open(device, mode)) < 0) { |
| 95 | close(ffd); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | bb_perror_msg("%s", device); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 97 | return 1; |
| 98 | } |
| 99 | *loopro = (mode == O_RDONLY); |
| 100 | |
| 101 | memset(&loopinfo, 0, sizeof(loopinfo)); |
| 102 | safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); |
| 103 | |
| 104 | loopinfo.lo_offset = offset; |
| 105 | |
| 106 | loopinfo.lo_encrypt_key_size = 0; |
| 107 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | bb_perror_msg("ioctl: LOOP_SET_FD"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 109 | close(fd); |
| 110 | close(ffd); |
| 111 | return 1; |
| 112 | } |
| 113 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { |
| 114 | (void) ioctl(fd, LOOP_CLR_FD, 0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 115 | bb_perror_msg("ioctl: LOOP_SET_STATUS"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 116 | close(fd); |
| 117 | close(ffd); |
| 118 | return 1; |
| 119 | } |
| 120 | close(fd); |
| 121 | close(ffd); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | extern char *find_unused_loop_device(void) |
| 126 | { |
| 127 | char dev[20]; |
| 128 | int i, fd; |
| 129 | struct stat statbuf; |
| 130 | struct loop_info loopinfo; |
| 131 | |
| 132 | for (i = 0; i <= 7; i++) { |
Eric Andersen | c7a3fb9 | 2002-03-20 15:25:25 +0000 | [diff] [blame] | 133 | sprintf(dev, LOOP_FORMAT, i); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 134 | if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { |
| 135 | if ((fd = open(dev, O_RDONLY)) >= 0) { |
| 136 | if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) { |
| 137 | if (errno == ENXIO) { /* probably free */ |
| 138 | close(fd); |
| 139 | return strdup(dev); |
| 140 | } |
| 141 | } |
| 142 | close(fd); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /* END CODE */ |
| 151 | /* |
| 152 | Local Variables: |
| 153 | c-file-style: "linux" |
| 154 | c-basic-offset: 4 |
| 155 | tab-width: 4 |
| 156 | End: |
| 157 | */ |