...

Source file src/github.com/coreos/go-systemd/v22/examples/activation/activation.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  	"os"
    24  
    25  	"github.com/coreos/go-systemd/v22/activation"
    26  )
    27  
    28  func fixListenPid() {
    29  	if os.Getenv("FIX_LISTEN_PID") != "" {
    30  		// HACK: real systemd would set LISTEN_PID before exec'ing but
    31  		// this is too difficult in golang for the purpose of a test.
    32  		// Do not do this in real code.
    33  		os.Setenv("LISTEN_PID", fmt.Sprintf("%d", os.Getpid()))
    34  	}
    35  }
    36  
    37  func main() {
    38  	fixListenPid()
    39  
    40  	files := activation.Files(false)
    41  
    42  	if len(files) == 0 {
    43  		panic("No files")
    44  	}
    45  
    46  	if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" || os.Getenv("LISTEN_FDNAMES") == "" {
    47  		panic("Should not unset envs")
    48  	}
    49  
    50  	files = activation.Files(true)
    51  
    52  	if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" || os.Getenv("LISTEN_FDNAMES") != "" {
    53  		panic("Can not unset envs")
    54  	}
    55  
    56  	// Write out the expected strings to the two pipes
    57  	files[0].Write([]byte("Hello world: " + files[0].Name()))
    58  	files[1].Write([]byte("Goodbye world: " + files[1].Name()))
    59  
    60  	return
    61  }
    62  

View as plain text