github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tests/echo-socket-activated/echo-socket-activated.go (about)

     1  // Copyright 2015 The rkt Authors
     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"
    20  	"net"
    21  	"os"
    22  	"strconv"
    23  
    24  	"github.com/coreos/rkt/pkg/sys"
    25  )
    26  
    27  const (
    28  	SD_LISTEN_FDS_START = 3
    29  )
    30  
    31  func sdListenFDs(unsetEnvironment bool) (int, error) {
    32  	defer func() {
    33  		if unsetEnvironment {
    34  			os.Unsetenv("LISTEN_PID")
    35  			os.Unsetenv("LISTEN_FDS")
    36  		}
    37  	}()
    38  
    39  	e := os.Getenv("LISTEN_PID")
    40  	if e == "" {
    41  		return 0, nil
    42  	}
    43  
    44  	pid, err := strconv.Atoi(e)
    45  	if err != nil {
    46  		return -1, err
    47  	}
    48  
    49  	if os.Getpid() != pid {
    50  		return 0, nil
    51  	}
    52  
    53  	e = os.Getenv("LISTEN_FDS")
    54  	if e == "" {
    55  		return 0, nil
    56  	}
    57  
    58  	n, err := strconv.Atoi(e)
    59  	if err != nil {
    60  		return -1, err
    61  	}
    62  
    63  	for fd := SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START+n; fd++ {
    64  		if err := sys.CloseOnExec(fd, true); err != nil {
    65  			return -1, err
    66  		}
    67  	}
    68  
    69  	return n, nil
    70  }
    71  
    72  func main() {
    73  	var fd uintptr
    74  
    75  	n, err := sdListenFDs(false)
    76  	if err != nil {
    77  		fmt.Fprintf(os.Stderr, "Error getting socket-activated FDs: %v\n", err)
    78  		os.Exit(1)
    79  	}
    80  	switch {
    81  	case n == 1:
    82  		fd = SD_LISTEN_FDS_START
    83  	case n > 1:
    84  		fmt.Fprintf(os.Stderr, "Too many file descriptors received.\n")
    85  		os.Exit(1)
    86  	default:
    87  		fmt.Fprintf(os.Stderr, "Not socket activated.\n")
    88  		os.Exit(1)
    89  	}
    90  
    91  	f := os.NewFile(fd, "")
    92  	defer f.Close()
    93  
    94  	l, err := net.FileListener(f)
    95  	if err != nil {
    96  		fmt.Fprintf(os.Stderr, "FileListener: %v.\n", err)
    97  		os.Exit(1)
    98  	}
    99  
   100  	conn, err := l.Accept()
   101  	if err != nil {
   102  		fmt.Fprintf(os.Stderr, "Accept: %v.\n", err)
   103  		os.Exit(1)
   104  	}
   105  
   106  	buf := make([]byte, 1024)
   107  	for {
   108  		nr, err := conn.Read(buf)
   109  		if err != nil {
   110  			if err != io.EOF {
   111  				fmt.Fprintf(os.Stderr, "Read: %v.\n", err)
   112  				os.Exit(1)
   113  			}
   114  			break
   115  		}
   116  
   117  		data := buf[0:nr]
   118  		if _, err := conn.Write(data); err != nil {
   119  			fmt.Fprintf(os.Stderr, "Write: %v.\n", err)
   120  			os.Exit(1)
   121  		}
   122  	}
   123  }