Skip to content

Commit

Permalink
Added support for Windows.
Browse files Browse the repository at this point in the history
This is done by splitting the parts using syslog and syscall
and then create simpler _windows.go versions.
  • Loading branch information
monicasarbu committed May 13, 2014
1 parent 453e510 commit 8c8fb75
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 36 deletions.
30 changes: 30 additions & 0 deletions droppriv_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"syscall"
)

func DropPrivileges() error {
var err error

if !_ConfigMeta.IsDefined("runoptions", "uid") {
// not found, no dropping privileges but no err
return nil
}

if !_ConfigMeta.IsDefined("runoptions", "gid") {
return MsgError("GID must be specified for dropping privileges")
}

INFO("Switching to user: %d.%d", _Config.RunOptions.Uid, _Config.RunOptions.Gid)

if err = syscall.Setgid(_Config.RunOptions.Gid); err != nil {
return MsgError("setgid: %s", err.Error())
}

if err = syscall.Setuid(_Config.RunOptions.Uid); err != nil {
return MsgError("setuid: %s", err.Error())
}

return nil
}
16 changes: 16 additions & 0 deletions droppriv_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"syscall"
)

func DropPrivileges() error {
var err error

if !_ConfigMeta.IsDefined("runoptions", "uid") {
// not found, no dropping privileges but no err
return nil
}

return MsgError("Dropping privileges is not supported on Windows")
}
3 changes: 1 addition & 2 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"log/syslog"
"testing"
"time"
//"fmt"
Expand Down Expand Up @@ -382,7 +381,7 @@ func TestHttpParser_splitResponse_midBody(t *testing.T) {
}

func TestHttpParser_RequestResponse(t *testing.T) {
LogInit(syslog.LOG_CRIT, "" /*toSyslog*/, false, []string{})
LogInit(LOG_CRIT, "" /*toSyslog*/, false, []string{})

data := []byte(
"GET / HTTP/1.1\r\n" +
Expand Down
19 changes: 17 additions & 2 deletions log.go → log_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import (
"runtime/debug"
)

type Priority int
const (
// Severity.

// From /usr/include/sys/syslog.h.
// These are the same on Linux, BSD, and OS X.
LOG_EMERG Priority = iota
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
)
type Logger struct {
toSyslog bool
level syslog.Priority
Expand Down Expand Up @@ -107,9 +122,9 @@ func openSyslog(level syslog.Priority, prefix string) *log.Logger {
return logger
}

func LogInit(level syslog.Priority, prefix string, toSyslog bool, debugSelectors []string) {
func LogInit(level Priority, prefix string, toSyslog bool, debugSelectors []string) {
_log.toSyslog = toSyslog
_log.level = level
_log.level = syslog.Priority(level)

_log.selectors = make(map[string]bool)
for _, selector := range debugSelectors {
Expand Down
97 changes: 97 additions & 0 deletions log_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"fmt"
"log"
"os"
"runtime/debug"
)
type Logger struct {
toSyslog bool
level Priority
selectors map[string]bool

logger *log.Logger
}

type Priority int
const (
// Severity.

// From /usr/include/sys/syslog.h.
// These are the same on Linux, BSD, and OS X.
LOG_EMERG Priority = iota
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
)

var _log Logger

func DEBUG(selector string, format string, v ...interface{}) {
if _log.level >= LOG_DEBUG {
selected := _log.selectors[selector]
if !selected {
return
}
_log.logger.Output(2, fmt.Sprintf("DBG "+format, v...))
}
}

func IS_DEBUG(selector string) bool {
return _log.selectors[selector]
}

func INFO(format string, v ...interface{}) {
if _log.level >= LOG_INFO {
_log.logger.Output(2, fmt.Sprintf("INFO "+format, v...))
}
}

func WARN(format string, v ...interface{}) {
if _log.level >= LOG_WARNING {
_log.logger.Output(2, fmt.Sprintf("WARN "+format, v...))
}
}

func ERR(format string, v ...interface{}) {
if _log.level >= LOG_ERR {
_log.logger.Output(2, fmt.Sprintf("ERR "+format, v...))
}
}

func CRIT(format string, v ...interface{}) {
if _log.level >= LOG_CRIT {
_log.logger.Output(2, fmt.Sprintf("CRIT "+format, v...))
}
}

func WTF(format string, v ...interface{}) {
if _log.level >= LOG_CRIT {
_log.logger.Output(2, fmt.Sprintf("CRIT "+format, v...))
}

// TODO: assert here when not in production mode
}

func RECOVER(msg string) {
if r := recover(); r != nil {
ERR("%s. Recovering, but please report this: %s.", msg, r)
ERR("Stacktrace: %s", debug.Stack())
}
}

func LogInit(level Priority, prefix string, toSyslog bool, debugSelectors []string) {
_log.level = level

_log.selectors = make(map[string]bool)
for _, selector := range debugSelectors {
_log.selectors[selector] = true
}

_log.logger = log.New(os.Stdout, prefix, log.Lshortfile)
}
28 changes: 1 addition & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package main
import (
"flag"
"fmt"
"log/syslog"
"strconv"
"strings"
"syscall"
"time"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -150,30 +148,6 @@ func decodePktEth(datalink int, pkt *pcap.Packet) {
FollowTcp(tcphdr, packet)
}

func DropPrivileges() error {
var err error

if !_ConfigMeta.IsDefined("runoptions", "uid") {
// not found, no dropping privileges but no err
return nil
}

if !_ConfigMeta.IsDefined("runoptions", "gid") {
return MsgError("GID must be specified for dropping privileges")
}

INFO("Switching to user: %d.%d", _Config.RunOptions.Uid, _Config.RunOptions.Gid)

if err = syscall.Setgid(_Config.RunOptions.Gid); err != nil {
return MsgError("setgid: %s", err.Error())
}

if err = syscall.Setuid(_Config.RunOptions.Uid); err != nil {
return MsgError("setuid: %s", err.Error())
}

return nil
}

func main() {

Expand All @@ -189,7 +163,7 @@ func main() {
flag.Parse()

debugSelectors := []string{}
logLevel := syslog.LOG_DEBUG
logLevel := LOG_DEBUG
if len(*debugSelectorsStr) > 0 {
debugSelectors = strings.Split(*debugSelectorsStr, ",")
}
Expand Down
5 changes: 2 additions & 3 deletions procs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io/ioutil"
"log/syslog"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -78,7 +77,7 @@ func AssertInt64ArraysAreEqual(t *testing.T, expected []int64, result []int64) b
}

func TestFindPidsByCmdlineGrep(t *testing.T) {
LogInit(syslog.LOG_DEBUG, "" /*toSyslog*/, false, []string{})
LogInit(LOG_DEBUG, "" /*toSyslog*/, false, []string{})
proc := []TestProcFile{
{Path: "/proc/1/cmdline", Contents: "/sbin/init"},
{Path: "/proc/1/cgroup", Contents: ""},
Expand Down Expand Up @@ -167,7 +166,7 @@ func TestRefreshPids(t *testing.T) {
}

func TestFindSocketsOfPid(t *testing.T) {
LogInit(syslog.LOG_DEBUG, "" /*toSyslog*/, false, []string{})
LogInit(LOG_DEBUG, "" /*toSyslog*/, false, []string{})

proc := []TestProcFile{
{Path: "/proc/766/fd/0", IsLink: true, Contents: "/dev/null"},
Expand Down
3 changes: 1 addition & 2 deletions publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"github.com/mattbaird/elastigo/api"
"github.com/mattbaird/elastigo/core"
"log/syslog"
"testing"
"time"
)
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestGetServerName(t *testing.T) {
t.Skip("Skipping topology tests in short mode, because they require Elasticsearch")
}

LogInit(syslog.LOG_DEBUG, "" /*!toSyslog*/, true, []string{})
LogInit(LOG_DEBUG, "" /*!toSyslog*/, true, []string{})
// TODO: delete old topology
api.Domain = "localhost"
api.Port = "9200"
Expand Down

0 comments on commit 8c8fb75

Please sign in to comment.