5 // In here, we have the probing code that learns about scores, reads
6 // their configuration files, and does the heavy lifting for launching
7 // them, doing the privilege drop, etc.
17 "github.com/kuroneko/configureit"
20 type ScoreInfo struct {
24 InitialEnv map[string]string
28 Config *configureit.Config
31 type ScoreExecution struct {
37 func NewScoreInfo() (si *ScoreInfo) {
39 si.InitialEnv = make(map[string]string)
41 config := NewScoreInfoConfig()
42 si.updateFromConfig(config)
47 func NewScoreInfoConfig() (config *configureit.Config) {
48 config = configureit.New()
50 config.Add("interface", configureit.NewStringOption("env"))
51 config.Add("dir", configureit.NewStringOption(""))
52 config.Add("path", configureit.NewStringOption("/usr/bin:/bin"))
53 config.Add("user", configureit.NewUserOption(""))
58 func (si *ScoreInfo) updateFromConfig(config *configureit.Config) {
59 // propogate PATH overrides.
60 opt := config.Get("dir")
61 sopt, _ := opt.(*configureit.StringOption)
62 si.InitialEnv["PATH"] = sopt.Value
64 // set the interface type.
65 opt = config.Get("interface")
66 sopt, _ = opt.(*configureit.StringOption)
67 si.Interface = sopt.Value
69 // propogate initial Pwd
70 opt = config.Get("dir")
71 sopt, _ = opt.(*configureit.StringOption)
72 si.InitialPwd = sopt.Value
76 Scores map[string]*ScoreInfo
79 func ScoreConfigure(si *ScoreInfo, r io.Reader) {
80 config := NewScoreInfoConfig()
81 err := config.Read(r, 1)
82 o.MightFail(err, "Error Parsing Score Configuration for %s", si.Name)
83 si.updateFromConfig(config)
87 scoreDirectory := GetStringOpt("score directory")
89 dir, err := os.Open(scoreDirectory)
90 o.MightFail(err, "Couldn't open Score directory")
93 Scores = make(map[string]*ScoreInfo)
95 files, err := dir.Readdir(-1)
96 for i := range files {
97 // skip ., .. and other dotfiles.
98 if strings.HasPrefix(files[i].Name, ".") {
101 // emacs backup files. ignore these.
102 if strings.HasSuffix(files[i].Name, "~") || strings.HasPrefix(files[i].Name, "#") {
105 // .conf is reserved for score configurations.
106 if strings.HasSuffix(files[i].Name, ".conf") {
109 if !files[i].IsRegular() && !files[i].IsSymlink() {
113 // check for the executionable bit
114 if (files[i].Permission() & 0111) != 0 {
115 fullpath := path.Join(scoreDirectory, files[i].Name)
116 conffile := fullpath+".conf"
117 o.Warn("Considering %s as score", files[i].Name)
120 si.Name = files[i].Name
121 si.Executable = fullpath
123 conf, err := os.Open(conffile)
125 o.Warn("Parsing configuration for %s", fullpath)
126 ScoreConfigure(si, conf)
129 o.Warn("Couldn't open config file for %s, assuming defaults: %s", files[i].Name, err)
131 Scores[files[i].Name] = si