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

Commit 93ddb48

Browse files
committed
Allow to specify working directory
1 parent 68f8e70 commit 93ddb48

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

command.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,27 @@ func getUserInfoByName(userName string) (UserInfo, error) {
3030
return UserInfo{userName, gid, uid}, nil
3131
}
3232

33-
func executeCommand(userInfo UserInfo, args ...string) (string, error) {
33+
func executeCommand(userInfo UserInfo, ignoreErrors bool, runDirectory string, args ...string) (string, error) {
3434

3535
baseCmd := args[0]
3636
cmdArgs := args[1:]
3737

3838
cmd := exec.Command(baseCmd, cmdArgs...)
39+
cmd.Dir = runDirectory
3940
currentUser, _ := user.Current()
4041
root := currentUser.Gid == "0"
4142

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

46-
if root {
47+
if userInfo.name != currentUser.Username && root {
4748
cmd.SysProcAttr = &syscall.SysProcAttr{}
4849
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: userInfo.uid, Gid: userInfo.gid}
4950
}
5051

5152
out, err := cmd.Output()
52-
if err != nil {
53+
if err != nil && !ignoreErrors {
5354
return string(out), err
5455
}
5556

daemon.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ type ScreenInfo struct {
2828
}
2929

3030
type ExecutionInfo struct {
31-
Command string `ini:"command"`
31+
Command string `ini:"command"`
32+
RunDirectory string `ini:"run_directory"`
3233
}
3334

3435
type DaemonInfo struct {

daemons/example.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = Example Daemon
33
user = example
44

55
[Execution]
6+
run_directory = /home/example
67
command = bash
78

89
[Daemon]

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838

3939
cfg, err := loadDaemon(cfgPath)
4040
if err != nil {
41-
log.Fatal("Failed load ", cfgPath, err)
41+
log.Print("Failed load ", cfgPath, err)
4242
continue
4343
}
4444

@@ -55,17 +55,18 @@ func main() {
5555
}
5656

5757
command := daemon.Command
58+
runDirectory := daemon.RunDirectory
5859
name := daemon.Name
5960
user := daemon.User
6061

61-
screen, err := runScreen(user, name, command)
62+
screen, err := runScreen(user, name, runDirectory, command)
6263
if err != nil {
6364
if err.Error() == "SCREEN_ALREADY_EXISTS" {
6465
if firstRun {
6566
log.Print("(", name, " on ", user, ") WARNING: skipped, screen already exists")
6667
}
6768
} else {
68-
log.Fatal("(", name, " on ", user, ") FAILED: ", command, " [", err, "]")
69+
log.Print("(", name, " on ", user, ") FAILED: ", command, " [", err, "]")
6970
}
7071
continue
7172
}

screen.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import (
2222
"github.com/pkg/errors"
2323
"strconv"
2424
"strings"
25+
"time"
2526
)
2627

2728
type Screen struct {
2829
id int
2930
name string
3031
}
3132

32-
func runScreen(userName string, screenName string, command string) (Screen, error) {
33+
func runScreen(userName string, screenName string, runDirectory string, command string) (Screen, error) {
3334

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

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

4647
userInfo, err := getUserInfoByName(userName)
4748
if err != nil {
4849
return Screen{}, err
4950
}
5051

51-
_, err = executeCommand(userInfo, args...)
52+
_, err = executeCommand(userInfo, false, runDirectory, args...)
5253
if err != nil {
5354
if err.Error() == "exit status 1" {
5455
return Screen{}, errors.New("exit status 1: check command")
5556
}
5657
return Screen{}, err
5758
}
5859

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

77-
output, err := executeCommand(userInfo, "screen", "-ls")
78-
if err != nil {
79-
if !strings.HasPrefix(output, "No Sockets found in") {
80-
return []Screen{}, err
81-
}
82-
return []Screen{}, nil
83-
}
84-
79+
output, _ := executeCommand(userInfo, true, "", "screen", "-ls")
8580
var screens []Screen
8681
lines := strings.Split(output, "\n")
82+
8783
for _, line := range lines {
8884

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

0 commit comments

Comments
 (0)