forked from gcwnow/mininit
-
Notifications
You must be signed in to change notification settings - Fork 8
/
loop.c
61 lines (49 loc) · 1.22 KB
/
loop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* SPDX-License-Identifier: BSD-2-Clause */
#include <fcntl.h>
#include <linux/loop.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "debug.h"
#include "loop.h"
// Note: klibc demands the third ioctl arg to be a void*.
int logetfree(void)
{
int fd = open("/dev/loop-control", O_RDWR);
if (fd < 0) {
WARNING("Failed to open '/dev/loop-control': %d\n", fd);
return -1;
}
int devnr = ioctl(fd, LOOP_CTL_GET_FREE, NULL);
if (devnr < 0) {
WARNING("Failed to acquire free loop device: %d\n", devnr);
} else {
DEBUG("Got free loop device: %d\n", devnr);
}
close(fd);
return devnr;
}
int losetup(const char *loop, const char *file)
{
DEBUG("Setting up loop: '%s' via '%s'\n", file, loop);
int filefd = open(file, O_RDONLY);
if (filefd < 0) {
ERROR("losetup: cannot open '%s': %d\n", file, filefd);
return -1;
}
int loopfd = open(loop, O_RDONLY);
if (loopfd < 0) {
ERROR("losetup: cannot open '%s': %d\n", loop, loopfd);
close(filefd);
return -1;
}
int res = ioctl(loopfd, LOOP_SET_FD, (void *)(intptr_t)filefd);
if (res < 0) {
ERROR("Cannot setup loop device '%s': %d\n", loop, res);
}
close(loopfd);
close(filefd);
return res;
}