package samba import ( "errors" "fmt" "log" "os/exec" "os/user" "strings" "imuslab.com/arozos/mod/fileservers" ) /* Functions requested by the file server service router */ func (m *ShareManager) ServerToggle(enabled bool) error { return errors.New("not supported") } func (m *ShareManager) IsEnabled() bool { smbdRunning, err := checkSmbdRunning() if err != nil { log.Println("Unable to get smbd state: " + err.Error()) return false } return smbdRunning } func (m *ShareManager) GetEndpoints(userinfo *user.User) []*fileservers.Endpoint { eps := []*fileservers.Endpoint{} eps = append(eps, &fileservers.Endpoint{ ProtocolName: "//", Port: 0, Subpath: "/" + userinfo.Username, }) return eps } func checkSmbdRunning() (bool, error) { // Run the system command to check the smbd service status cmd := exec.Command("systemctl", "is-active", "--quiet", "smbd") err := cmd.Run() // If the command exits with status 0, smbd is running if err == nil { return true, nil } // If the command exits with a non-zero status, smbd is not running // We can check the error message to be sure it's not another issue if exitError, ok := err.(*exec.ExitError); ok { if strings.TrimSpace(string(exitError.Stderr)) == "" { return false, nil } return false, fmt.Errorf("error checking smbd status: %s", exitError.Stderr) } return false, fmt.Errorf("unexpected error checking smbd status: %v", err) }