| 
									
										
										
										
											2016-09-25 08:39:20 +01:00
										 |  |  | // Package coremain contains the functions for starting CoreDNS.
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | package coremain
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"flag"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2022-02-14 08:24:21 -08:00
										 |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	"runtime"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							| 
									
										
										
										
											2016-08-29 14:39:24 +01:00
										 |  |  | 	caddy.DefaultConfigFile = "Corefile"
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 	caddy.Quiet = true // don't show init stuff from caddy
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	setVersion()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 14:39:24 +01:00
										 |  |  | 	flag.StringVar(&conf, "conf", "", "Corefile to load (default \""+caddy.DefaultConfigFile+"\")")
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	flag.BoolVar(&plugins, "plugins", false, "List installed plugins")
 | 
					
						
							|  |  |  | 	flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file")
 | 
					
						
							|  |  |  | 	flag.BoolVar(&version, "version", false, "Show version")
 | 
					
						
							| 
									
										
										
										
											2016-09-25 18:42:08 +01:00
										 |  |  | 	flag.BoolVar(&dnsserver.Quiet, "quiet", false, "Quiet mode (no initialization output)")
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	caddy.RegisterCaddyfileLoader("flag", caddy.LoaderFunc(confLoader))
 | 
					
						
							|  |  |  | 	caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(defaultLoader))
 | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	caddy.AppName = coreName
 | 
					
						
							| 
									
										
										
										
											2018-01-23 21:10:55 +01:00
										 |  |  | 	caddy.AppVersion = CoreVersion
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Run is CoreDNS's main() function.
 | 
					
						
							|  |  |  | func Run() {
 | 
					
						
							| 
									
										
										
										
											2018-05-03 01:57:38 -05:00
										 |  |  | 	caddy.TrapSignals()
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	flag.Parse()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 12:01:10 -07:00
										 |  |  | 	if len(flag.Args()) > 0 {
 | 
					
						
							|  |  |  | 		mustLogFatal(fmt.Errorf("extra command line arguments: %s", flag.Args()))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-24 14:30:01 +01:00
										 |  |  | 	log.SetOutput(os.Stdout)
 | 
					
						
							| 
									
										
										
										
											2018-10-31 21:32:23 +00:00
										 |  |  | 	log.SetFlags(0) // Set to 0 because we're doing our own time, with timezone
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if version {
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 		showVersion()
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 		os.Exit(0)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if plugins {
 | 
					
						
							|  |  |  | 		fmt.Println(caddy.DescribePlugins())
 | 
					
						
							|  |  |  | 		os.Exit(0)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 14:39:24 +01:00
										 |  |  | 	// Get Corefile input
 | 
					
						
							|  |  |  | 	corefile, err := caddy.LoadCaddyfile(serverType)
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		mustLogFatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Start your engines
 | 
					
						
							| 
									
										
										
										
											2016-08-29 14:39:24 +01:00
										 |  |  | 	instance, err := caddy.Start(corefile)
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		mustLogFatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-25 18:42:08 +01:00
										 |  |  | 	if !dnsserver.Quiet {
 | 
					
						
							|  |  |  | 		showVersion()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	// Twiddle your thumbs
 | 
					
						
							|  |  |  | 	instance.Wait()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // mustLogFatal wraps log.Fatal() in a way that ensures the
 | 
					
						
							|  |  |  | // output is always printed to stderr so the user can see it
 | 
					
						
							|  |  |  | // if the user is still there, even if the process log was not
 | 
					
						
							|  |  |  | // enabled. If this process is an upgrade, however, and the user
 | 
					
						
							|  |  |  | // might not be there anymore, this just logs to the process
 | 
					
						
							|  |  |  | // log and exits.
 | 
					
						
							|  |  |  | func mustLogFatal(args ...interface{}) {
 | 
					
						
							|  |  |  | 	if !caddy.IsUpgrade() {
 | 
					
						
							|  |  |  | 		log.SetOutput(os.Stderr)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	log.Fatal(args...)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // confLoader loads the Caddyfile using the -conf flag.
 | 
					
						
							|  |  |  | func confLoader(serverType string) (caddy.Input, error) {
 | 
					
						
							|  |  |  | 	if conf == "" {
 | 
					
						
							|  |  |  | 		return nil, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if conf == "stdin" {
 | 
					
						
							| 
									
										
										
										
											2017-06-28 17:05:54 +08:00
										 |  |  | 		return caddy.CaddyfileFromPipe(os.Stdin, serverType)
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 08:24:21 -08:00
										 |  |  | 	contents, err := os.ReadFile(filepath.Clean(conf))
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return caddy.CaddyfileInput{
 | 
					
						
							|  |  |  | 		Contents:       contents,
 | 
					
						
							|  |  |  | 		Filepath:       conf,
 | 
					
						
							|  |  |  | 		ServerTypeName: serverType,
 | 
					
						
							|  |  |  | 	}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 14:39:24 +01:00
										 |  |  | // defaultLoader loads the Corefile from the current working directory.
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | func defaultLoader(serverType string) (caddy.Input, error) {
 | 
					
						
							| 
									
										
										
										
											2021-10-13 15:30:31 +08:00
										 |  |  | 	contents, err := os.ReadFile(caddy.DefaultConfigFile)
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		if os.IsNotExist(err) {
 | 
					
						
							|  |  |  | 			return nil, nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return caddy.CaddyfileInput{
 | 
					
						
							|  |  |  | 		Contents:       contents,
 | 
					
						
							|  |  |  | 		Filepath:       caddy.DefaultConfigFile,
 | 
					
						
							|  |  |  | 		ServerTypeName: serverType,
 | 
					
						
							|  |  |  | 	}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-17 17:36:35 +03:00
										 |  |  | // showVersion prints the version that is starting.
 | 
					
						
							| 
									
										
										
										
											2016-09-17 21:24:39 +01:00
										 |  |  | func showVersion() {
 | 
					
						
							|  |  |  | 	fmt.Print(versionString())
 | 
					
						
							| 
									
										
										
										
											2017-08-12 12:59:28 -07:00
										 |  |  | 	fmt.Print(releaseString())
 | 
					
						
							| 
									
										
										
										
											2016-09-17 21:24:39 +01:00
										 |  |  | 	if devBuild && gitShortStat != "" {
 | 
					
						
							|  |  |  | 		fmt.Printf("%s\n%s\n", gitShortStat, gitFilesModified)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // versionString returns the CoreDNS version as a string.
 | 
					
						
							|  |  |  | func versionString() string {
 | 
					
						
							| 
									
										
										
										
											2019-10-17 17:36:35 +03:00
										 |  |  | 	return fmt.Sprintf("%s-%s\n", caddy.AppName, caddy.AppVersion)
 | 
					
						
							| 
									
										
										
										
											2017-08-12 12:59:28 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // releaseString returns the release information related to CoreDNS version:
 | 
					
						
							|  |  |  | // <OS>/<ARCH>, <go version>, <commit>
 | 
					
						
							|  |  |  | // e.g.,
 | 
					
						
							|  |  |  | // linux/amd64, go1.8.3, a6d2d7b5
 | 
					
						
							|  |  |  | func releaseString() string {
 | 
					
						
							| 
									
										
										
										
											2019-10-17 17:36:35 +03:00
										 |  |  | 	return fmt.Sprintf("%s/%s, %s, %s\n", runtime.GOOS, runtime.GOARCH, runtime.Version(), GitCommit)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 21:24:39 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | // setVersion figures out the version information
 | 
					
						
							|  |  |  | // based on variables set by -ldflags.
 | 
					
						
							|  |  |  | func setVersion() {
 | 
					
						
							|  |  |  | 	// A development build is one that's not at a tag or has uncommitted changes
 | 
					
						
							|  |  |  | 	devBuild = gitTag == "" || gitShortStat != ""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Only set the appVersion if -ldflags was used
 | 
					
						
							|  |  |  | 	if gitNearestTag != "" || gitTag != "" {
 | 
					
						
							|  |  |  | 		if devBuild && gitNearestTag != "" {
 | 
					
						
							| 
									
										
										
										
											2019-10-01 07:41:29 +01:00
										 |  |  | 			appVersion = fmt.Sprintf("%s (+%s %s)", strings.TrimPrefix(gitNearestTag, "v"), GitCommit, buildDate)
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | 		} else if gitTag != "" {
 | 
					
						
							|  |  |  | 			appVersion = strings.TrimPrefix(gitTag, "v")
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Flags that control program flow or startup
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	conf    string
 | 
					
						
							|  |  |  | 	version bool
 | 
					
						
							|  |  |  | 	plugins bool
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Build information obtained with the help of -ldflags
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	appVersion = "(untracked dev build)" // inferred at startup
 | 
					
						
							|  |  |  | 	devBuild   = true                    // inferred at startup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buildDate        string // date -u
 | 
					
						
							|  |  |  | 	gitTag           string // git describe --exact-match HEAD 2> /dev/null
 | 
					
						
							|  |  |  | 	gitNearestTag    string // git describe --abbrev=0 --tags HEAD
 | 
					
						
							|  |  |  | 	gitShortStat     string // git diff-index --shortstat
 | 
					
						
							|  |  |  | 	gitFilesModified string // git diff-index --name-only HEAD
 | 
					
						
							| 
									
										
										
										
											2018-02-23 15:02:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Gitcommit contains the commit where we built CoreDNS from.
 | 
					
						
							|  |  |  | 	GitCommit string
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:36:29 +01:00
										 |  |  | )
 |