mount_linux.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package fuse
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. "sync"
  10. "syscall"
  11. )
  12. func handleFusermountStderr(errCh chan<- error) func(line string) (ignore bool) {
  13. return func(line string) (ignore bool) {
  14. if line == `fusermount: failed to open /etc/fuse.conf: Permission denied` {
  15. // Silence this particular message, it occurs way too
  16. // commonly and isn't very relevant to whether the mount
  17. // succeeds or not.
  18. return true
  19. }
  20. const (
  21. noMountpointPrefix = `fusermount: failed to access mountpoint `
  22. noMountpointSuffix = `: No such file or directory`
  23. )
  24. if strings.HasPrefix(line, noMountpointPrefix) && strings.HasSuffix(line, noMountpointSuffix) {
  25. // re-extract it from the error message in case some layer
  26. // changed the path
  27. mountpoint := line[len(noMountpointPrefix) : len(line)-len(noMountpointSuffix)]
  28. err := &MountpointDoesNotExistError{
  29. Path: mountpoint,
  30. }
  31. select {
  32. case errCh <- err:
  33. return true
  34. default:
  35. // not the first error; fall back to logging it
  36. return false
  37. }
  38. }
  39. return false
  40. }
  41. }
  42. // isBoringFusermountError returns whether the Wait error is
  43. // uninteresting; exit status 1 is.
  44. func isBoringFusermountError(err error) bool {
  45. if err, ok := err.(*exec.ExitError); ok && err.Exited() {
  46. if status, ok := err.Sys().(syscall.WaitStatus); ok && status.ExitStatus() == 1 {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. func mount(dir string, conf *mountConfig, ready chan<- struct{}, errp *error) (fusefd *os.File, err error) {
  53. // linux mount is never delayed
  54. close(ready)
  55. fds, err := syscall.Socketpair(syscall.AF_FILE, syscall.SOCK_STREAM, 0)
  56. if err != nil {
  57. return nil, fmt.Errorf("socketpair error: %v", err)
  58. }
  59. writeFile := os.NewFile(uintptr(fds[0]), "fusermount-child-writes")
  60. defer writeFile.Close()
  61. readFile := os.NewFile(uintptr(fds[1]), "fusermount-parent-reads")
  62. defer readFile.Close()
  63. cmd := exec.Command(
  64. "fusermount",
  65. "-o", conf.getOptions(),
  66. "--",
  67. dir,
  68. )
  69. cmd.Env = append(os.Environ(), "_FUSE_COMMFD=3")
  70. cmd.ExtraFiles = []*os.File{writeFile}
  71. var wg sync.WaitGroup
  72. stdout, err := cmd.StdoutPipe()
  73. if err != nil {
  74. return nil, fmt.Errorf("setting up fusermount stderr: %v", err)
  75. }
  76. stderr, err := cmd.StderrPipe()
  77. if err != nil {
  78. return nil, fmt.Errorf("setting up fusermount stderr: %v", err)
  79. }
  80. if err := cmd.Start(); err != nil {
  81. return nil, fmt.Errorf("fusermount: %v", err)
  82. }
  83. helperErrCh := make(chan error, 1)
  84. wg.Add(2)
  85. go lineLogger(&wg, "mount helper output", neverIgnoreLine, stdout)
  86. go lineLogger(&wg, "mount helper error", handleFusermountStderr(helperErrCh), stderr)
  87. wg.Wait()
  88. if err := cmd.Wait(); err != nil {
  89. // see if we have a better error to report
  90. select {
  91. case helperErr := <-helperErrCh:
  92. // log the Wait error if it's not what we expected
  93. if !isBoringFusermountError(err) {
  94. log.Printf("mount helper failed: %v", err)
  95. }
  96. // and now return what we grabbed from stderr as the real
  97. // error
  98. return nil, helperErr
  99. default:
  100. // nope, fall back to generic message
  101. }
  102. return nil, fmt.Errorf("fusermount: %v", err)
  103. }
  104. c, err := net.FileConn(readFile)
  105. if err != nil {
  106. return nil, fmt.Errorf("FileConn from fusermount socket: %v", err)
  107. }
  108. defer c.Close()
  109. uc, ok := c.(*net.UnixConn)
  110. if !ok {
  111. return nil, fmt.Errorf("unexpected FileConn type; expected UnixConn, got %T", c)
  112. }
  113. buf := make([]byte, 32) // expect 1 byte
  114. oob := make([]byte, 32) // expect 24 bytes
  115. _, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
  116. scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
  117. if err != nil {
  118. return nil, fmt.Errorf("ParseSocketControlMessage: %v", err)
  119. }
  120. if len(scms) != 1 {
  121. return nil, fmt.Errorf("expected 1 SocketControlMessage; got scms = %#v", scms)
  122. }
  123. scm := scms[0]
  124. gotFds, err := syscall.ParseUnixRights(&scm)
  125. if err != nil {
  126. return nil, fmt.Errorf("syscall.ParseUnixRights: %v", err)
  127. }
  128. if len(gotFds) != 1 {
  129. return nil, fmt.Errorf("wanted 1 fd; got %#v", gotFds)
  130. }
  131. f := os.NewFile(uintptr(gotFds[0]), "/dev/fuse")
  132. return f, nil
  133. }