1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
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.
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.
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
27 #include <sys/ioctl.h>
29 #include "loop.h" /* Pull in loop device support */
31 extern int del_loop(const char *device)
35 if ((fd = open(device, O_RDONLY)) < 0) {
36 bb_perror_msg("%s", device);
39 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
41 bb_perror_msg("ioctl: LOOP_CLR_FD");
48 extern int set_loop(const char *device, const char *file, int offset,
51 struct loop_info loopinfo;
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)) {
57 bb_perror_msg("%s", file);
60 if ((fd = open(device, mode)) < 0) {
62 bb_perror_msg("%s", device);
65 *loopro = (mode == O_RDONLY);
67 memset(&loopinfo, 0, sizeof(loopinfo));
68 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
70 loopinfo.lo_offset = offset;
72 loopinfo.lo_encrypt_key_size = 0;
73 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
74 bb_perror_msg("ioctl: LOOP_SET_FD");
79 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
80 (void) ioctl(fd, LOOP_CLR_FD, 0);
81 bb_perror_msg("ioctl: LOOP_SET_STATUS");
91 extern char *find_unused_loop_device(void)
96 struct loop_info loopinfo;
98 for (i = 0; i <= 7; i++) {
99 sprintf(dev, LOOP_FORMAT, i);
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 */
119 c-file-style: "linux"