Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

120 lines (101 sloc) 2.568 kb
package main
import (
"exec"
"flag"
"fmt"
"io/ioutil"
"container/vector"
"os"
)
const (
etcrcd = "/etc/rc.d/"
varrun = "/var/run/daemons/"
)
var color = map[string]string{
"red": "\033[1;31m",
"green": "\033[1;32m",
"yellow": "\033[1;33m",
"blue": "\033[1;34m",
"magenta": "\033[1;35m",
"cyan": "\033[1;36m",
"white": "\033[1;37m",
"none": "\033[0m",
}
func parseDaemons() (*vector.StringVector, *vector.StringVector, os.Error) {
files, err := ioutil.ReadDir(etcrcd)
if err != nil {
return nil, nil, err
}
running := new(vector.StringVector)
stopped := new(vector.StringVector)
for _, file := range files {
if file.Name == "functions" || !file.IsRegular() {
continue
}
if _, err := os.Stat(fmt.Sprintf("%s%s", varrun, file.Name)); err == nil {
running.Push(file.Name)
} else {
stopped.Push(file.Name)
}
}
return running, stopped, nil
}
func modifyDaemon(argv []string) (int, os.Error) {
daemon := fmt.Sprintf("%s%s", etcrcd, argv[0])
p, err := exec.Run(daemon, argv, os.Environ(), etcrcd, 0, 1, 2)
w, err := p.Wait(0)
return w.ExitStatus(), err
}
func printDaemonStatus(daemon string, running bool) {
if running {
fmt.Printf("%s[RUNNING]\t%s%v%s\n", color["green"], color["blue"], daemon, color["none"])
} else {
fmt.Printf("%s[STOPPED]\t%s%v%s\n", color["red"], color["blue"], daemon, color["none"])
}
}
func findInVector(vector *vector.StringVector, needle string) bool {
for _, v := range *vector {
if v == needle {
return true
}
}
return false
}
func main() {
flag.Parse()
n := flag.NArg()
running, stopped, err := parseDaemons()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
if n > 0 {
if _, err := os.Stat(fmt.Sprintf("%s%s", etcrcd, flag.Arg(0))); err != nil {
fmt.Fprintf(os.Stderr, "%s%s%s is not a valid daemon\n", color["red"], flag.Arg(0), color["none"])
os.Exit(2)
}
}
switch {
case n == 0:
for _, v := range *stopped {
printDaemonStatus(v, false)
}
for _, v := range *running {
printDaemonStatus(v, true)
}
case n == 1:
if findInVector(running, flag.Arg(0)) {
printDaemonStatus(flag.Arg(0), true)
os.Exit(0)
}
if findInVector(stopped, flag.Arg(0)) {
printDaemonStatus(flag.Arg(0), false)
os.Exit(0)
}
case n > 1:
ret, err := modifyDaemon(flag.Args())
if ret > 0 || err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.String())
}
}
}
Jump to Line
Something went wrong with that request. Please try again.