~sinzui/ubuntu/vivid/juju-core/vivid-1.24.6

« back to all changes in this revision

Viewing changes to src/github.com/coreos/go-systemd/examples/activation/activation.go

  • Committer: Curtis Hovey
  • Date: 2015-09-30 14:14:54 UTC
  • mfrom: (1.1.34)
  • Revision ID: curtis@hovey.name-20150930141454-o3ldf23dzyjio6c0
Backport of 1.24.6 from wily. (LP: #1500916)

Show diffs side-by-side

added added

removed removed

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