PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/10.4/fusefs/load/load_fusefs.c

http://macfuse.googlecode.com/
C | 147 lines | 90 code | 23 blank | 34 comment | 27 complexity | 56915ea140aaef0c4ae1c9770e12ce99 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. /*
  2. * Copyright (C) 2006-2008 Google. All Rights Reserved.
  3. * Amit Singh <singh@>
  4. */
  5. /*
  6. * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
  7. *
  8. * This file contains Original Code and/or Modifications of Original Code as
  9. * defined in and that are subject to the Apple Public Source License Version
  10. * 2.0 (the 'License'). You may not use this file except in compliance with
  11. * the License. Please obtain a copy of the License at
  12. * http://www.opensource.apple.com/apsl/ and read it before using this file.
  13. *
  14. * The Original Code and all software distributed under the License are
  15. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  16. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  17. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
  19. * the License for the specific language governing rights and limitations
  20. * under the License.
  21. */
  22. #include <stdio.h>
  23. #include <sys/errno.h>
  24. #include <sys/param.h>
  25. #include <sys/mount.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <unistd.h>
  29. #include <sys/sysctl.h>
  30. #include <grp.h>
  31. #include <string.h>
  32. #include <fuse_param.h>
  33. #include <fuse_version.h>
  34. int
  35. main(__unused int argc, __unused const char *argv[])
  36. {
  37. int pid = -1;
  38. int result = -1;
  39. union wait status;
  40. char version[MAXHOSTNAMELEN + 1] = { 0 };
  41. size_t version_len = MAXHOSTNAMELEN;
  42. size_t version_len_desired = 0;
  43. struct vfsconf vfc = { 0 };
  44. result = getvfsbyname(MACFUSE_FS_TYPE, &vfc);
  45. if (result) { /* MacFUSE is not already loaded */
  46. result = -1;
  47. goto need_loading;
  48. }
  49. /* some version of MacFUSE is already loaded; let us check it out */
  50. result = sysctlbyname(SYSCTL_MACFUSE_VERSION_NUMBER, version,
  51. &version_len, NULL, (size_t)0);
  52. if (result) {
  53. if (errno == ENOENT) {
  54. /* too old; doesn't even have the sysctl variable */
  55. goto need_unloading;
  56. }
  57. result = -1;
  58. goto out;
  59. }
  60. /* sysctlbyname() includes the trailing '\0' in version_len */
  61. version_len_desired = strlen(MACFUSE_VERSION) + 1;
  62. if ((version_len == version_len_desired) &&
  63. !strncmp(MACFUSE_VERSION, version, version_len)) {
  64. /* what's currently loaded is good */
  65. result = 0;
  66. goto out;
  67. }
  68. /* mismatched version; need to unload what's loaded */
  69. need_unloading:
  70. pid = fork();
  71. if (pid == 0) {
  72. result = execl(SYSTEM_KEXTUNLOAD, SYSTEM_KEXTUNLOAD, "-b",
  73. MACFUSE_BUNDLE_IDENTIFIER, NULL);
  74. /* We can only get here if the exec failed */
  75. goto out;
  76. }
  77. if (pid == -1) {
  78. result = errno;
  79. goto out;
  80. }
  81. /* Success! */
  82. if ((wait4(pid, (int *)&status, 0, NULL) == pid) && (WIFEXITED(status))) {
  83. result = status.w_retcode;
  84. } else {
  85. result = -1;
  86. }
  87. if (result != 0) {
  88. /* unloading failed */
  89. result = EBUSY;
  90. goto out;
  91. }
  92. /* unloading succeeded; now load the on-disk version */
  93. need_loading:
  94. pid = fork();
  95. if (pid == 0) {
  96. result = execl(SYSTEM_KEXTLOAD, SYSTEM_KEXTLOAD, MACFUSE_KEXT, NULL);
  97. /* We can only get here if the exec failed */
  98. goto out;
  99. }
  100. if (pid == -1) {
  101. result = errno;
  102. goto out;
  103. }
  104. /* Success! */
  105. if ((wait4(pid, (int *)&status, 0, NULL) == pid) && (WIFEXITED(status))) {
  106. result = status.w_retcode;
  107. } else {
  108. result = -1;
  109. }
  110. /* now do any kext-load-time settings we need to do as root */
  111. if (result == 0) {
  112. int admin_gid = 0;
  113. struct group *g = getgrnam(MACOSX_ADMIN_GROUP_NAME);
  114. if (!g) {
  115. goto out;
  116. }
  117. admin_gid = g->gr_gid;
  118. /* if this fails, we don't care */
  119. (void)sysctlbyname(SYSCTL_MACFUSE_TUNABLES_ADMIN, NULL, NULL,
  120. &admin_gid, sizeof(admin_gid));
  121. }
  122. out:
  123. _exit(result);
  124. }