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 | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 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 | |
| 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include "libbb.h" |
| 29 | #include "loop.h" /* Pull in loop device support */ |
| 30 | |
| 31 | extern int del_loop(const char *device) |
| 32 | { |
| 33 | int fd; |
| 34 | |
| 35 | if ((fd = open(device, O_RDONLY)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | bb_perror_msg("%s", device); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | return (FALSE); |
| 38 | } |
| 39 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
Eric Andersen | 7b3edeb | 2003-05-02 16:25:01 +0000 | [diff] [blame] | 40 | close(fd); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | bb_perror_msg("ioctl: LOOP_CLR_FD"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 42 | return (FALSE); |
| 43 | } |
| 44 | close(fd); |
| 45 | return (TRUE); |
| 46 | } |
| 47 | |
| 48 | extern int set_loop(const char *device, const char *file, int offset, |
| 49 | int *loopro) |
| 50 | { |
| 51 | struct loop_info loopinfo; |
| 52 | int fd, ffd, mode; |
| 53 | |
| 54 | mode = *loopro ? O_RDONLY : O_RDWR; |
| 55 | if ((ffd = open(file, mode)) < 0 && !*loopro |
| 56 | && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | bb_perror_msg("%s", file); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 58 | return 1; |
| 59 | } |
| 60 | if ((fd = open(device, mode)) < 0) { |
| 61 | close(ffd); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | bb_perror_msg("%s", device); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 63 | return 1; |
| 64 | } |
| 65 | *loopro = (mode == O_RDONLY); |
| 66 | |
| 67 | memset(&loopinfo, 0, sizeof(loopinfo)); |
| 68 | safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); |
| 69 | |
| 70 | loopinfo.lo_offset = offset; |
| 71 | |
| 72 | loopinfo.lo_encrypt_key_size = 0; |
| 73 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | bb_perror_msg("ioctl: LOOP_SET_FD"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 75 | close(fd); |
| 76 | close(ffd); |
| 77 | return 1; |
| 78 | } |
| 79 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { |
| 80 | (void) ioctl(fd, LOOP_CLR_FD, 0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | bb_perror_msg("ioctl: LOOP_SET_STATUS"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 82 | close(fd); |
| 83 | close(ffd); |
| 84 | return 1; |
| 85 | } |
| 86 | close(fd); |
| 87 | close(ffd); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | extern char *find_unused_loop_device(void) |
| 92 | { |
| 93 | char dev[20]; |
| 94 | int i, fd; |
| 95 | struct stat statbuf; |
| 96 | struct loop_info loopinfo; |
| 97 | |
| 98 | for (i = 0; i <= 7; i++) { |
Eric Andersen | c7a3fb9 | 2002-03-20 15:25:25 +0000 | [diff] [blame] | 99 | sprintf(dev, LOOP_FORMAT, i); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 100 | if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { |
| 101 | if ((fd = open(dev, O_RDONLY)) >= 0) { |
| 102 | if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) { |
| 103 | if (errno == ENXIO) { /* probably free */ |
| 104 | close(fd); |
| 105 | return strdup(dev); |
| 106 | } |
| 107 | } |
| 108 | close(fd); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* END CODE */ |
| 117 | /* |
| 118 | Local Variables: |
| 119 | c-file-style: "linux" |
| 120 | c-basic-offset: 4 |
| 121 | tab-width: 4 |
| 122 | End: |
| 123 | */ |