blob: 81d1f0ab6265cec357f85daba2e4eb43db0e0b22 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
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 Andersenaad1a882001-03-16 22:47:14 +000020 */
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"
Eric Andersenef8cd3b2004-02-06 07:16:36 +000029
30/* Grumble... The 2.6.x kernel breaks asm/posix_types.h
31 * so we get to try and cope as best we can... */
32#include <linux/version.h>
33#include <asm/posix_types.h>
34#if LINUX_VERSION_CODE >= 132608
35#define __bb_kernel_dev_t __kernel_old_dev_t
36#elif LINUX_VERSION_CODE >= 0x20600
37#define __bb_kernel_dev_t __kernel_dev_t
38#else
39#define __bb_kernel_dev_t unsigned short
40#endif
41
42/* Stuff stolen from linux/loop.h */
43#define LO_NAME_SIZE 64
44#define LO_KEY_SIZE 32
45#define LOOP_SET_FD 0x4C00
46#define LOOP_CLR_FD 0x4C01
47#define LOOP_SET_STATUS 0x4C02
48#define LOOP_GET_STATUS 0x4C03
49struct loop_info {
50 int lo_number;
51 __bb_kernel_dev_t lo_device;
52 unsigned long lo_inode;
53 __bb_kernel_dev_t lo_rdevice;
54 int lo_offset;
55 int lo_encrypt_type;
56 int lo_encrypt_key_size;
57 int lo_flags;
58 char lo_name[LO_NAME_SIZE];
59 unsigned char lo_encrypt_key[LO_KEY_SIZE];
60 unsigned long lo_init[2];
61 char reserved[4];
62};
Eric Andersenaad1a882001-03-16 22:47:14 +000063
64extern int del_loop(const char *device)
65{
66 int fd;
67
68 if ((fd = open(device, O_RDONLY)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 bb_perror_msg("%s", device);
Eric Andersenaad1a882001-03-16 22:47:14 +000070 return (FALSE);
71 }
72 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Eric Andersen7b3edeb2003-05-02 16:25:01 +000073 close(fd);
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_perror_msg("ioctl: LOOP_CLR_FD");
Eric Andersenaad1a882001-03-16 22:47:14 +000075 return (FALSE);
76 }
77 close(fd);
78 return (TRUE);
79}
80
81extern int set_loop(const char *device, const char *file, int offset,
82 int *loopro)
83{
84 struct loop_info loopinfo;
85 int fd, ffd, mode;
86
87 mode = *loopro ? O_RDONLY : O_RDWR;
88 if ((ffd = open(file, mode)) < 0 && !*loopro
89 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 bb_perror_msg("%s", file);
Eric Andersenaad1a882001-03-16 22:47:14 +000091 return 1;
92 }
93 if ((fd = open(device, mode)) < 0) {
94 close(ffd);
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 bb_perror_msg("%s", device);
Eric Andersenaad1a882001-03-16 22:47:14 +000096 return 1;
97 }
98 *loopro = (mode == O_RDONLY);
99
100 memset(&loopinfo, 0, sizeof(loopinfo));
101 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
102
103 loopinfo.lo_offset = offset;
104
105 loopinfo.lo_encrypt_key_size = 0;
106 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 bb_perror_msg("ioctl: LOOP_SET_FD");
Eric Andersenaad1a882001-03-16 22:47:14 +0000108 close(fd);
109 close(ffd);
110 return 1;
111 }
112 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
113 (void) ioctl(fd, LOOP_CLR_FD, 0);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 bb_perror_msg("ioctl: LOOP_SET_STATUS");
Eric Andersenaad1a882001-03-16 22:47:14 +0000115 close(fd);
116 close(ffd);
117 return 1;
118 }
119 close(fd);
120 close(ffd);
121 return 0;
122}
123
124extern char *find_unused_loop_device(void)
125{
126 char dev[20];
127 int i, fd;
128 struct stat statbuf;
129 struct loop_info loopinfo;
130
131 for (i = 0; i <= 7; i++) {
Eric Andersenc7a3fb92002-03-20 15:25:25 +0000132 sprintf(dev, LOOP_FORMAT, i);
Eric Andersenaad1a882001-03-16 22:47:14 +0000133 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
134 if ((fd = open(dev, O_RDONLY)) >= 0) {
135 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
136 if (errno == ENXIO) { /* probably free */
137 close(fd);
138 return strdup(dev);
139 }
140 }
141 close(fd);
142 }
143 }
144 }
145 return NULL;
146}
147
148
149/* END CODE */
150/*
151Local Variables:
152c-file-style: "linux"
153c-basic-offset: 4
154tab-width: 4
155End:
156*/