96
96
char * find_unused_loop_device (void)
98
/* Just creating a device, say in /tmp, is probably a bad idea -
99
people might have problems with backup or so.
100
So, we just try /dev/loop[0-7]. */
102
char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
103
int i, j, fd, somedev = 0, someloop = 0, permission = 0;
105
struct loop_info loopinfo;
107
for (j = 0; j < SIZE(loop_formats); j++) {
108
for(i = 0; i < 256; i++) {
109
sprintf(dev, loop_formats[j], i);
110
if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
112
fd = open (dev, O_RDONLY);
114
if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
115
someloop++; /* in use */
116
else if (errno == ENXIO) {
118
return xstrdup(dev);/* probably free */
121
} else if (errno == EACCES)
124
continue;/* continue trying as long as devices exist */
101
fd = open("/dev/loop-control", O_RDWR);
103
error("%s: could not open /dev/loop-control. Maybe this kernel "
105
" about the loop device? (If so, recompile or "
106
"`modprobe loop'.)", progname);
131
error("%s: could not find any device /dev/loop#", progname);
132
else if (!someloop && permission)
133
error("%s: no permission to look at /dev/loop#", progname);
136
"%s: Could not find any loop device. Maybe this kernel "
138
" about the loop device? (If so, recompile or "
139
"`modprobe loop'.)", progname);
109
rc = ioctl(fd, LOOP_CTL_GET_FREE, 0);
141
112
error("%s: could not find any free loop device", progname);
116
sprintf(dev, "/dev/loop%d", rc);