diff --git a/tools.c b/tools.c index 67723c0..d6229da 100644 --- a/tools.c +++ b/tools.c @@ -656,10 +656,12 @@ int string_array_length(char ** a) return i; } -int do_losetup(char * device, char * target) +/* setup loop device from file descriptor targfd */ +int do_losetup_fd(char *device, int targfd, const char *name) { - int i, loopfd, targfd; + int i, loopfd; struct loop_info loopInfo; + memset(&loopInfo, 0, sizeof(loopInfo)); my_insmod("loop", NULL); /* wait for udev's dust settles down */ @@ -670,19 +672,10 @@ int do_losetup(char * device, char * target) return -1; } - targfd = open( target, O_RDONLY ); - if ( targfd < 0 ) - { - log_message( "losetup: error opening %s: %s", target, strerror(errno) ); - close( loopfd ); - return -1; - } - if ( ioctl(loopfd, LOOP_SET_FD, targfd) < 0 ) { log_message( "losetup: error setting up loopback device: %s", strerror(errno) ); close( loopfd ); - close( targfd ); return -1; } /* Note: LOOP_SET_FD triggers change event. While processing it @@ -693,18 +686,19 @@ int do_losetup(char * device, char * target) */ udev_settle(); - memset(&loopInfo, 0, sizeof(loopInfo)); - strcpy(loopInfo.lo_name, target); + if (name) { + strcpy(loopInfo.lo_name, name); - if ( ioctl(loopfd, LOOP_SET_STATUS, &loopInfo) < 0 ) - log_message( "losetup: error setting up loopback device: %s", strerror(errno) ); + if ( ioctl(loopfd, LOOP_SET_STATUS, &loopInfo) < 0 ) + log_message( "losetup: error setting up loopback device: %s", strerror(errno) ); - /* Same here: LOOP_SET_STATUS triggers change event, give udev - * a chance to process it without concurrent access to the loop - * device. In other words, prevent the caller of this function - * from mounting the device while udev still running blkid on it - */ - udev_settle(); + /* Same here: LOOP_SET_STATUS triggers change event, give udev + * a chance to process it without concurrent access to the loop + * device. In other words, prevent the caller of this function + * from mounting the device while udev still running blkid on it + */ + udev_settle(); + } close( loopfd ); /* udev might be monitoring loopback device (with fanotify) and @@ -713,6 +707,20 @@ int do_losetup(char * device, char * target) * (which is going to mount the newly configured loopback device) */ udev_settle(); - close( targfd ); return 0; } + +int do_losetup(char * device, char * target) +{ + int ret = 0, targfd; + targfd = open( target, O_RDONLY ); + if ( targfd < 0 ) + { + log_message( "losetup: error opening %s: %s", target, strerror(errno) ); + return -1; + } + ret = do_losetup_fd(device, targfd, target); + close(targfd); + return ret; +} + diff --git a/tools.h b/tools.h index 4b2cf32..2f414d6 100644 --- a/tools.h +++ b/tools.h @@ -44,6 +44,19 @@ int pass_env(int); char ** list_directory(char * direct); int string_array_length(char ** a); int do_losetup(char * device, char * target); +/** + * Setup a loopback device backed by fd. + * + * @param device path to loopback device + * @param fd backing file descriptor + * @param name for pretty output from losetup, typically name of the backing file. + * @return 0 on success, otherwise -1 + * + * @note: should be used instead of do_losetup for anonymous files, + * i.e. ones created by memfd_create, open(O_TMPFILE), etc + */ +int do_losetup_fd(char *device, int fd, const char *name); + char * get_ramdisk_path(const char *); char * get_uid_file_path(const char *, const char *); enum return_type verify_ramdisk_digest(const char *filename, const char *sha256_hash);