loop.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <features.h>
  22. #if defined (__GLIBC__) && !defined(__UCLIBC__)
  23. #include <linux/posix_types.h>
  24. #endif
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/ioctl.h>
  31. #include "libbb.h"
  32. /* Grumble... The 2.6.x kernel breaks asm/posix_types.h
  33. * so we get to try and cope as best we can... */
  34. #include <linux/version.h>
  35. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  36. #define __bb_kernel_dev_t __kernel_old_dev_t
  37. #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  38. #define __bb_kernel_dev_t __kernel_dev_t
  39. #else
  40. #define __bb_kernel_dev_t unsigned short
  41. #endif
  42. /* Stuff stolen from linux/loop.h */
  43. #define LO_NAME_SIZE 64
  44. #define LO_KEY_SIZE 32
  45. #define LOOP_SET_FD 0x4C00
  46. #define LOOP_CLR_FD 0x4C01
  47. #define LOOP_SET_STATUS 0x4C02
  48. #define LOOP_GET_STATUS 0x4C03
  49. struct loop_info {
  50. int lo_number;
  51. __bb_kernel_dev_t lo_device;
  52. unsigned long lo_inode;
  53. __bb_kernel_dev_t lo_rdevice;
  54. int lo_offset;
  55. int lo_encrypt_type;
  56. int lo_encrypt_key_size;
  57. int lo_flags;
  58. char lo_name[LO_NAME_SIZE];
  59. unsigned char lo_encrypt_key[LO_KEY_SIZE];
  60. unsigned long lo_init[2];
  61. char reserved[4];
  62. };
  63. extern int del_loop(const char *device)
  64. {
  65. int fd;
  66. if ((fd = open(device, O_RDONLY)) < 0) {
  67. bb_perror_msg("%s", device);
  68. return (FALSE);
  69. }
  70. if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
  71. close(fd);
  72. bb_perror_msg("ioctl: LOOP_CLR_FD");
  73. return (FALSE);
  74. }
  75. close(fd);
  76. return (TRUE);
  77. }
  78. extern int set_loop(const char *device, const char *file, int offset,
  79. int *loopro)
  80. {
  81. struct loop_info loopinfo;
  82. int fd, ffd, mode;
  83. mode = *loopro ? O_RDONLY : O_RDWR;
  84. if ((ffd = open(file, mode)) < 0 && !*loopro
  85. && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
  86. bb_perror_msg("%s", file);
  87. return 1;
  88. }
  89. if ((fd = open(device, mode)) < 0) {
  90. close(ffd);
  91. bb_perror_msg("%s", device);
  92. return 1;
  93. }
  94. *loopro = (mode == O_RDONLY);
  95. memset(&loopinfo, 0, sizeof(loopinfo));
  96. safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
  97. loopinfo.lo_offset = offset;
  98. loopinfo.lo_encrypt_key_size = 0;
  99. if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
  100. bb_perror_msg("ioctl: LOOP_SET_FD");
  101. close(fd);
  102. close(ffd);
  103. return 1;
  104. }
  105. if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
  106. (void) ioctl(fd, LOOP_CLR_FD, 0);
  107. bb_perror_msg("ioctl: LOOP_SET_STATUS");
  108. close(fd);
  109. close(ffd);
  110. return 1;
  111. }
  112. close(fd);
  113. close(ffd);
  114. return 0;
  115. }
  116. extern char *find_unused_loop_device(void)
  117. {
  118. char dev[20];
  119. int i, fd;
  120. struct stat statbuf;
  121. struct loop_info loopinfo;
  122. for (i = 0; i <= 7; i++) {
  123. sprintf(dev, LOOP_FORMAT, i);
  124. if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
  125. if ((fd = open(dev, O_RDONLY)) >= 0) {
  126. if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
  127. if (errno == ENXIO) { /* probably free */
  128. close(fd);
  129. return strdup(dev);
  130. }
  131. }
  132. close(fd);
  133. }
  134. }
  135. }
  136. return NULL;
  137. }
  138. /* END CODE */
  139. /*
  140. Local Variables:
  141. c-file-style: "linux"
  142. c-basic-offset: 4
  143. tab-width: 4
  144. End:
  145. */