proc_mnt.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C) 2005-2009 Junjiro Okajima
  3. *
  4. * This program, aufs is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <sys/types.h>
  19. #include <errno.h>
  20. #include <mntent.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "au_util.h"
  25. #define ProcMounts "/proc/self/mounts"
  26. static void copy_ent(struct mntent *dst, struct mntent *src)
  27. {
  28. free(dst->mnt_opts);
  29. free(dst->mnt_type);
  30. free(dst->mnt_dir);
  31. free(dst->mnt_fsname);
  32. dst->mnt_dir = NULL;
  33. dst->mnt_type = NULL;
  34. dst->mnt_opts = NULL;
  35. dst->mnt_fsname = strdup(src->mnt_fsname);
  36. if (dst->mnt_fsname)
  37. dst->mnt_dir = strdup(src->mnt_dir);
  38. if (dst->mnt_dir)
  39. dst->mnt_type = strdup(src->mnt_type);
  40. if (dst->mnt_type)
  41. dst->mnt_opts = strdup(src->mnt_opts);
  42. if (dst->mnt_opts) {
  43. dst->mnt_freq = src->mnt_freq;
  44. dst->mnt_passno = src->mnt_passno;
  45. } else
  46. AuFin("strdup");
  47. }
  48. int au_proc_getmntent(char *mntpnt, struct mntent *rent)
  49. {
  50. int found;
  51. struct mntent *p;
  52. FILE *fp;
  53. fp = setmntent(ProcMounts, "r");
  54. if (!fp)
  55. AuFin(ProcMounts);
  56. /* find the last one */
  57. memset(rent, 0, sizeof(*rent));
  58. found = 0;
  59. while ((p = getmntent(fp)))
  60. if (!strcmp(p->mnt_dir, mntpnt)) {
  61. Dpri("%s, %s, %s, %s, %d, %d\n",
  62. p->mnt_fsname, p->mnt_dir, p->mnt_type,
  63. p->mnt_opts, p->mnt_freq, p->mnt_passno);
  64. copy_ent(rent, p);
  65. found = 1;
  66. }
  67. endmntent(fp);
  68. if (!found) {
  69. errno = EINVAL;
  70. AuFin("%s", mntpnt);
  71. }
  72. return 0;
  73. }