github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/client/allocdir/alloc_dir_unix.go (about)

     1  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     2  
     3  // Functions shared between linux/darwin.
     4  package allocdir
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"os/user"
    10  	"path/filepath"
    11  	"strconv"
    12  
    13  	"golang.org/x/sys/unix"
    14  )
    15  
    16  var (
    17  	// SharedAllocContainerPath is the path inside container for mounted
    18  	// directory shared across tasks in a task group.
    19  	SharedAllocContainerPath = filepath.Join("/", SharedAllocName)
    20  
    21  	// TaskLocalContainer is the path inside a container for mounted directory
    22  	// for local storage.
    23  	TaskLocalContainerPath = filepath.Join("/", TaskLocal)
    24  
    25  	// TaskSecretsContainerPath is the path inside a container for mounted
    26  	// secrets directory
    27  	TaskSecretsContainerPath = filepath.Join("/", TaskSecrets)
    28  )
    29  
    30  func (d *AllocDir) linkOrCopy(src, dst string, perm os.FileMode) error {
    31  	// Attempt to hardlink.
    32  	if err := os.Link(src, dst); err == nil {
    33  		return nil
    34  	}
    35  
    36  	return fileCopy(src, dst, perm)
    37  }
    38  
    39  func (d *AllocDir) dropDirPermissions(path string) error {
    40  	// Can't do anything if not root.
    41  	if unix.Geteuid() != 0 {
    42  		return nil
    43  	}
    44  
    45  	u, err := user.Lookup("nobody")
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	uid, err := getUid(u)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	gid, err := getGid(u)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	if err := os.Chown(path, uid, gid); err != nil {
    61  		return fmt.Errorf("Couldn't change owner/group of %v to (uid: %v, gid: %v): %v", path, uid, gid, err)
    62  	}
    63  
    64  	if err := os.Chmod(path, 0777); err != nil {
    65  		return fmt.Errorf("Chmod(%v) failed: %v", path, err)
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func getUid(u *user.User) (int, error) {
    72  	uid, err := strconv.Atoi(u.Uid)
    73  	if err != nil {
    74  		return 0, fmt.Errorf("Unable to convert Uid to an int: %v", err)
    75  	}
    76  
    77  	return uid, nil
    78  }
    79  
    80  func getGid(u *user.User) (int, error) {
    81  	gid, err := strconv.Atoi(u.Gid)
    82  	if err != nil {
    83  		return 0, fmt.Errorf("Unable to convert Gid to an int: %v", err)
    84  	}
    85  
    86  	return gid, nil
    87  }