...

Source file src/github.com/coreos/go-systemd/v22/examples/activation/udpconn.go

Documentation: github.com/coreos/go-systemd/v22/examples/activation

     1  // Copyright 2015 CoreOS, Inc.
     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  //go:build ignore
    16  // +build ignore
    17  
    18  // Activation example used by the activation unit tests.
    19  package main
    20  
    21  import (
    22  	"fmt"
    23  	"net"
    24  	"os"
    25  
    26  	"github.com/coreos/go-systemd/v22/activation"
    27  )
    28  
    29  func fixListenPid() {
    30  	if os.Getenv("FIX_LISTEN_PID") != "" {
    31  		// HACK: real systemd would set LISTEN_PID before exec'ing but
    32  		// this is too difficult in golang for the purpose of a test.
    33  		// Do not do this in real code.
    34  		os.Setenv("LISTEN_PID", fmt.Sprintf("%d", os.Getpid()))
    35  	}
    36  }
    37  
    38  func main() {
    39  	fixListenPid()
    40  
    41  	pc, err := activation.PacketConns()
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  
    46  	if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" || os.Getenv("LISTEN_FDNAMES") != "" {
    47  		panic("Can not unset envs")
    48  	}
    49  
    50  	udp1, ok := pc[0].(*net.UDPConn)
    51  	if !ok {
    52  		panic("packetConn 1 not UDP")
    53  	}
    54  	udp2, ok := pc[1].(*net.UDPConn)
    55  	if !ok {
    56  		panic("packetConn 2 not UDP")
    57  	}
    58  
    59  	_, addr1, err := udp1.ReadFromUDP(nil)
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  	_, addr2, err := udp2.ReadFromUDP(nil)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  
    68  	// Write out the expected strings to the two pipes
    69  	_, err = udp1.WriteToUDP([]byte("Hello world"), addr1)
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  	_, err = udp2.WriteToUDP([]byte("Goodbye world"), addr2)
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  
    78  	return
    79  }
    80  

View as plain text