intmountimg(constchar *img, constchar *dir) { // Get a new loopfile for losetup. int loopctlfd = open("/dev/loop-control", O_RDWR | O_CLOEXEC); // It takes the same effect as `losetup -f`. int devnr = ioctl(loopctlfd, LOOP_CTL_GET_FREE); close(loopctlfd); char loopfile[PATH_MAX] = { '\0' }; sprintf(loopfile, "/dev/loop%d", devnr); int loopfd = open(loopfile, O_RDWR | O_CLOEXEC); if (loopfd < 0) { // On Android, loopfile is in /dev/block. sprintf(loopfile, "/dev/block/loop%d", devnr); loopfd = open(loopfile, O_RDWR | O_CLOEXEC); if (loopfd < 0) { error("\033[31mError: losetup error!\n"); } } // It takes the same efferct as `losetup` command. int imgfd = open(img, O_RDWR | O_CLOEXEC); ioctl(loopfd, LOOP_SET_FD, imgfd); close(loopfd); close(imgfd); trymount(loopfile, dir); }