github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/tao_launch/ctty_darwin.go (about)

     1  // Copyright (c) 2014, Google, Inc.  All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"strconv"
    22  	"strings"
    23  	"syscall"
    24  
    25  	//"github.com/golang/crypto/ssh/terminal"
    26  	"golang.org/x/crypto/ssh/terminal"
    27  )
    28  
    29  func isCtty(fd int) bool {
    30  	// One would hope there was a simple way to figure out what our controlling
    31  	// tty is. Or at least check if stdin is coming from our controlling tty (as
    32  	// opposed to just any old terminal). Alas, the infuriating morass that
    33  	// passes for job control provides no such ability. Of no help:
    34  	// stat(/dev/tty), readlink(/dev/fd/0), open(/dev/stdin), open(/dev/tty),
    35  	// cat(/proc/self/fdinfo/0), stat(/proc/self/fd/0), ioctl, tty_ioctl, TIOC*,
    36  	// anything to do with sid, pgrp, pgid, /dev/console, the tty command, $TTY,
    37  	// $SSH_TTY. Since I am on a mission, I delved into the source for /bin/ps
    38  	// to discover /proc/self/stat contains the major/minor numbers for the
    39  	// controlling tty. And stat(stdin).Rdev provides the same info. If they
    40  	// differ, I'm going to conclude -- oh so tentatively -- that stdin is NOT
    41  	// our controlling tty. If they match, or anything goes wrong, we will
    42  	// assume that stdin, if it is a terminal, is our ctty.
    43  	if !terminal.IsTerminal(fd) {
    44  		return false
    45  	}
    46  	var s syscall.Stat_t
    47  	err := syscall.Fstat(fd, &s)
    48  	if err != nil {
    49  		fmt.Fprintf(noise, "[warning: fstat(%d) failed: %v]\n", fd, err)
    50  		return true
    51  	}
    52  	name := "/proc/self/stat"
    53  	f, err := os.Open(name)
    54  	if err != nil {
    55  		fmt.Fprintf(noise, "[warning: open(%q) failed: %v]\n", name, err)
    56  		return true
    57  	}
    58  	b, err := ioutil.ReadAll(f)
    59  	if err != nil {
    60  		fmt.Fprintf(noise, "[warning: read(%q) failed: %v]\n", name, err)
    61  		return true
    62  	}
    63  	a := strings.Split(string(b), " ")
    64  	tty_nr := 6
    65  	if len(a) <= tty_nr {
    66  		fmt.Fprintf(noise, "[warning: read(%q) borked: only %d fields]\n", name, len(a))
    67  		return true
    68  	}
    69  	ctty, err := strconv.Atoi(a[tty_nr])
    70  	if err != nil {
    71  		fmt.Fprintf(noise, "[warning: read(%q) borked: tty_nr = %v]\n", name, a[tty_nr])
    72  		return true
    73  	}
    74  	if int32(ctty) != s.Rdev {
    75  		fmt.Fprintf(noise, "[warning: stdin is a tty, but not ctty]\n")
    76  		return false
    77  	}
    78  	return true
    79  }