Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
Allow to specify working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dasavick committed Mar 3, 2019
1 parent 68f8e70 commit 93ddb48
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
7 changes: 4 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,27 @@ func getUserInfoByName(userName string) (UserInfo, error) {
return UserInfo{userName, gid, uid}, nil
}

func executeCommand(userInfo UserInfo, args ...string) (string, error) {
func executeCommand(userInfo UserInfo, ignoreErrors bool, runDirectory string, args ...string) (string, error) {

baseCmd := args[0]
cmdArgs := args[1:]

cmd := exec.Command(baseCmd, cmdArgs...)
cmd.Dir = runDirectory
currentUser, _ := user.Current()
root := currentUser.Gid == "0"

if userInfo.name != currentUser.Username && !root {
return "", errors.New("Screen daemon requires root privileges to switch between users")
}

if root {
if userInfo.name != currentUser.Username && root {
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: userInfo.uid, Gid: userInfo.gid}
}

out, err := cmd.Output()
if err != nil {
if err != nil && !ignoreErrors {
return string(out), err
}

Expand Down
3 changes: 2 additions & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type ScreenInfo struct {
}

type ExecutionInfo struct {
Command string `ini:"command"`
Command string `ini:"command"`
RunDirectory string `ini:"run_directory"`
}

type DaemonInfo struct {
Expand Down
1 change: 1 addition & 0 deletions daemons/example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = Example Daemon
user = example

[Execution]
run_directory = /home/example
command = bash

[Daemon]
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {

cfg, err := loadDaemon(cfgPath)
if err != nil {
log.Fatal("Failed load ", cfgPath, err)
log.Print("Failed load ", cfgPath, err)
continue
}

Expand All @@ -55,17 +55,18 @@ func main() {
}

command := daemon.Command
runDirectory := daemon.RunDirectory
name := daemon.Name
user := daemon.User

screen, err := runScreen(user, name, command)
screen, err := runScreen(user, name, runDirectory, command)
if err != nil {
if err.Error() == "SCREEN_ALREADY_EXISTS" {
if firstRun {
log.Print("(", name, " on ", user, ") WARNING: skipped, screen already exists")
}
} else {
log.Fatal("(", name, " on ", user, ") FAILED: ", command, " [", err, "]")
log.Print("(", name, " on ", user, ") FAILED: ", command, " [", err, "]")
}
continue
}
Expand Down
18 changes: 7 additions & 11 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
"github.com/pkg/errors"
"strconv"
"strings"
"time"
)

type Screen struct {
id int
name string
}

func runScreen(userName string, screenName string, command string) (Screen, error) {
func runScreen(userName string, screenName string, runDirectory string, command string) (Screen, error) {

exists, err := doesScreenExists(userName, screenName)
if err != nil {
Expand All @@ -40,22 +41,23 @@ func runScreen(userName string, screenName string, command string) (Screen, erro
return Screen{}, errors.New("SCREEN_ALREADY_EXISTS")
}

args := []string{"screen", "-dmS", screenName}
args := []string{"screen", "-S", screenName, "-dm"}
args = append(args, strings.Split(command, " ")...)

userInfo, err := getUserInfoByName(userName)
if err != nil {
return Screen{}, err
}

_, err = executeCommand(userInfo, args...)
_, err = executeCommand(userInfo, false, runDirectory, args...)
if err != nil {
if err.Error() == "exit status 1" {
return Screen{}, errors.New("exit status 1: check command")
}
return Screen{}, err
}

time.Sleep(time.Second)
screen, err := getScreenByName(userName, screenName)
if err != nil {
if err.Error() == "SCREEN_NOT_FOUND" {
Expand All @@ -74,16 +76,10 @@ func getRunningScreens(userName string) ([]Screen, error) {
return []Screen{}, err
}

output, err := executeCommand(userInfo, "screen", "-ls")
if err != nil {
if !strings.HasPrefix(output, "No Sockets found in") {
return []Screen{}, err
}
return []Screen{}, nil
}

output, _ := executeCommand(userInfo, true, "", "screen", "-ls")
var screens []Screen
lines := strings.Split(output, "\n")

for _, line := range lines {

if !strings.HasPrefix(line, "\t") {
Expand Down

0 comments on commit 93ddb48

Please sign in to comment.