-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathwrapper.go
More file actions
40 lines (34 loc) · 1.35 KB
/
wrapper.go
File metadata and controls
40 lines (34 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package lambda
import (
"log"
"os"
)
const awsLambdaExecWrapper = "AWS_LAMBDA_EXEC_WRAPPER"
// execAWSLambdaExecWrapper applies the AWS_LAMBDA_EXEC_WRAPPER environment variable.
// If AWS_LAMBDA_EXEC_WRAPPER is defined, replace the current process by spawning
// it with the current process' arguments (including the program name). If the call
// to syscall.Exec fails, this aborts the process with a fatal error.
func execAWSLambdaExecWrapper(
getenv func(key string) string,
sysExec func(argv0 string, argv []string, envv []string) error,
callbacks []func(),
) {
wrapper := getenv(awsLambdaExecWrapper)
if wrapper == "" {
return
}
// Execute the provided callbacks before re-starting the process...
for _, callback := range callbacks {
callback()
}
// The AWS_LAMBDA_EXEC_WRAPPER variable is blanked before replacing the process
// in order to avoid endlessly restarting the process.
env := append(os.Environ(), awsLambdaExecWrapper+"=")
if err := sysExec(wrapper, append([]string{wrapper}, os.Args...), env); err != nil {
log.Fatalf("failed to sysExec() %s=%s: %v", awsLambdaExecWrapper, wrapper, err)
}
}