|
3 | 3 |
|
4 | 4 | #include "opk.h" |
5 | 5 |
|
| 6 | +#include <errno.h> |
| 7 | +#include <fcntl.h> |
6 | 8 | #include <getopt.h> |
7 | 9 | #include <limits.h> |
8 | 10 | #include <stdbool.h> |
|
11 | 13 | #include <string.h> |
12 | 14 | #include <sys/mount.h> |
13 | 15 | #include <sys/stat.h> |
| 16 | +#include <sys/types.h> |
14 | 17 | #include <sys/wait.h> |
15 | 18 | #include <unistd.h> |
16 | 19 |
|
| 20 | +#include <linux/loop.h> |
| 21 | + |
17 | 22 | #ifndef MY_NAME |
18 | 23 | #define MY_NAME "opkrun" |
19 | 24 | #endif |
@@ -232,6 +237,61 @@ static char *get_url(const char *file) |
232 | 237 | return tmp; |
233 | 238 | } |
234 | 239 |
|
| 240 | +static int logetfree(void) |
| 241 | +{ |
| 242 | + int fd = open("/dev/loop-control", O_RDWR); |
| 243 | + if (fd < 0) { |
| 244 | + fprintf(stderr, "Failed to open '/dev/loop-control': %d\n", fd); |
| 245 | + return -1; |
| 246 | + } |
| 247 | + |
| 248 | + int devnr = ioctl(fd, LOOP_CTL_GET_FREE, NULL); |
| 249 | + if (devnr < 0) { |
| 250 | + fprintf(stderr, "Failed to acquire free loop device: %d\n", devnr); |
| 251 | + } |
| 252 | + |
| 253 | + close(fd); |
| 254 | + return devnr; |
| 255 | +} |
| 256 | + |
| 257 | +static int losetup(const char *loop, const char *file) |
| 258 | +{ |
| 259 | + unsigned int i; |
| 260 | + int filefd, loopfd, ret; |
| 261 | + |
| 262 | + filefd = open(file, O_RDONLY); |
| 263 | + if (filefd < 0) { |
| 264 | + fprintf(stderr, "losetup: cannot open '%s': %d\n", file, filefd); |
| 265 | + return -1; |
| 266 | + } |
| 267 | + |
| 268 | + /* We try to open the loop device just a bit after it was created. |
| 269 | + * Give some time to udev so that it can set the proper rights. */ |
| 270 | + for (i = 0; i < 100; i++) { |
| 271 | + loopfd = open(loop, O_RDONLY); |
| 272 | + if (loopfd < 0 && errno == EACCES) |
| 273 | + usleep(10000); |
| 274 | + else |
| 275 | + break; |
| 276 | + } |
| 277 | + if (loopfd < 0) { |
| 278 | + fprintf(stderr, "losetup: cannot open '%s': %d\n", loop, loopfd); |
| 279 | + close(filefd); |
| 280 | + return loopfd; |
| 281 | + } |
| 282 | + |
| 283 | + ret = ioctl(loopfd, LOOP_SET_FD, (void *)(intptr_t)filefd); |
| 284 | + if (ret < 0) { |
| 285 | + fprintf(stderr, "Cannot setup loop device '%s': %d\n", loop, ret); |
| 286 | + close(loopfd); |
| 287 | + close(filefd); |
| 288 | + return ret; |
| 289 | + } |
| 290 | + |
| 291 | + close(filefd); |
| 292 | + return loopfd; |
| 293 | +} |
| 294 | + |
235 | 295 | int main(int argc, char **argv) |
236 | 296 | { |
237 | 297 | if (argc < 2) { |
@@ -328,10 +388,20 @@ int main(int argc, char **argv) |
328 | 388 | umount(params.mountpoint); |
329 | 389 | mkdir(params.mountpoint, 0755); |
330 | 390 |
|
331 | | - char buf[256]; |
332 | | - sprintf(buf, "mount -o loop,nodev,nosuid,ro \'%s\' \'%s\' >/dev/null 2>&1", |
333 | | - opk_name, params.mountpoint); |
334 | | - ret = system(buf); |
| 391 | + int devnr = logetfree(); |
| 392 | + if (devnr < 0) |
| 393 | + return devnr; |
| 394 | + |
| 395 | + char loop_dev[9 + 10 + 1]; |
| 396 | + sprintf(loop_dev, "/dev/loop%i", devnr); |
| 397 | + |
| 398 | + int loopfd = losetup(loop_dev, opk_name); |
| 399 | + if (loopfd < 0) { |
| 400 | + perror("Failed to losetup"); |
| 401 | + return loopfd; |
| 402 | + } |
| 403 | + |
| 404 | + ret = mount(loop_dev, params.mountpoint, "squashfs", MS_NODEV | MS_NOSUID | MS_RDONLY, 0); |
335 | 405 | if (ret < 0) { |
336 | 406 | perror("Unable to mount OPK"); |
337 | 407 | free(params.mountpoint); |
@@ -370,6 +440,9 @@ int main(int argc, char **argv) |
370 | 440 | rmdir(params.mountpoint); |
371 | 441 | free(params.mountpoint); |
372 | 442 |
|
| 443 | + ioctl(loopfd, LOOP_CLR_FD, (void *)0); |
| 444 | + close(loopfd); |
| 445 | + |
373 | 446 | for (char **ptr = args; *ptr; ptr++) |
374 | 447 | free(*ptr); |
375 | 448 |
|
|
0 commit comments