blob: bd837c3b033805ef8184011a59ddd9029f21c175 [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
Eric Andersen1271dbb2005-01-03 05:50:01 +000022#if defined (__GLIBC__) && !defined(__UCLIBC__)
Rob Landley861f0142004-12-09 23:12:00 +000023#include <asm/posix_types.h>
Eric Andersen1271dbb2005-01-03 05:50:01 +000024#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000025#include <stdio.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/ioctl.h>
31#include "libbb.h"
Eric Andersenef8cd3b2004-02-06 07:16:36 +000032
33/* Grumble... The 2.6.x kernel breaks asm/posix_types.h
34 * so we get to try and cope as best we can... */
35#include <linux/version.h>
Glenn L McGrath774e6cc2004-06-25 09:01:09 +000036
Eric Andersencf6ef052004-08-16 08:29:44 +000037#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
Eric Andersen07225132004-08-16 08:36:28 +000038#define __bb_kernel_dev_t __kernel_old_dev_t
39#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
Glenn L McGrath774e6cc2004-06-25 09:01:09 +000040#define __bb_kernel_dev_t __kernel_dev_t
Eric Andersenef8cd3b2004-02-06 07:16:36 +000041#else
42#define __bb_kernel_dev_t unsigned short
43#endif
44
45/* Stuff stolen from linux/loop.h */
46#define LO_NAME_SIZE 64
47#define LO_KEY_SIZE 32
48#define LOOP_SET_FD 0x4C00
49#define LOOP_CLR_FD 0x4C01
50#define LOOP_SET_STATUS 0x4C02
51#define LOOP_GET_STATUS 0x4C03
52struct loop_info {
53 int lo_number;
54 __bb_kernel_dev_t lo_device;
55 unsigned long lo_inode;
56 __bb_kernel_dev_t lo_rdevice;
57 int lo_offset;
58 int lo_encrypt_type;
59 int lo_encrypt_key_size;
60 int lo_flags;
61 char lo_name[LO_NAME_SIZE];
62 unsigned char lo_encrypt_key[LO_KEY_SIZE];
63 unsigned long lo_init[2];
64 char reserved[4];
65};
Eric Andersenaad1a882001-03-16 22:47:14 +000066
67extern int del_loop(const char *device)
68{
69 int fd;
70
71 if ((fd = open(device, O_RDONLY)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 bb_perror_msg("%s", device);
Eric Andersenaad1a882001-03-16 22:47:14 +000073 return (FALSE);
74 }
75 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Eric Andersen7b3edeb2003-05-02 16:25:01 +000076 close(fd);
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 bb_perror_msg("ioctl: LOOP_CLR_FD");
Eric Andersenaad1a882001-03-16 22:47:14 +000078 return (FALSE);
79 }
80 close(fd);
81 return (TRUE);
82}
83
84extern int set_loop(const char *device, const char *file, int offset,
85 int *loopro)
86{
87 struct loop_info loopinfo;
88 int fd, ffd, mode;
89
90 mode = *loopro ? O_RDONLY : O_RDWR;
91 if ((ffd = open(file, mode)) < 0 && !*loopro
92 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 bb_perror_msg("%s", file);
Eric Andersenaad1a882001-03-16 22:47:14 +000094 return 1;
95 }
96 if ((fd = open(device, mode)) < 0) {
97 close(ffd);
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 bb_perror_msg("%s", device);
Eric Andersenaad1a882001-03-16 22:47:14 +000099 return 1;
100 }
101 *loopro = (mode == O_RDONLY);
102
103 memset(&loopinfo, 0, sizeof(loopinfo));
104 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
105
106 loopinfo.lo_offset = offset;
107
108 loopinfo.lo_encrypt_key_size = 0;
109 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 bb_perror_msg("ioctl: LOOP_SET_FD");
Eric Andersenaad1a882001-03-16 22:47:14 +0000111 close(fd);
112 close(ffd);
113 return 1;
114 }
115 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
116 (void) ioctl(fd, LOOP_CLR_FD, 0);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 bb_perror_msg("ioctl: LOOP_SET_STATUS");
Eric Andersenaad1a882001-03-16 22:47:14 +0000118 close(fd);
119 close(ffd);
120 return 1;
121 }
122 close(fd);
123 close(ffd);
124 return 0;
125}
126
127extern char *find_unused_loop_device(void)
128{
129 char dev[20];
130 int i, fd;
131 struct stat statbuf;
132 struct loop_info loopinfo;
133
134 for (i = 0; i <= 7; i++) {
Eric Andersenc7a3fb92002-03-20 15:25:25 +0000135 sprintf(dev, LOOP_FORMAT, i);
Eric Andersenaad1a882001-03-16 22:47:14 +0000136 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
137 if ((fd = open(dev, O_RDONLY)) >= 0) {
138 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
139 if (errno == ENXIO) { /* probably free */
140 close(fd);
141 return strdup(dev);
142 }
143 }
144 close(fd);
145 }
146 }
147 }
148 return NULL;
149}
150
151
152/* END CODE */
153/*
154Local Variables:
155c-file-style: "linux"
156c-basic-offset: 4
157tab-width: 4
158End:
159*/