...

Source file src/github.com/godbus/dbus/v5/conn_other.go

Documentation: github.com/godbus/dbus/v5

     1  // +build !darwin
     2  
     3  package dbus
     4  
     5  import (
     6  	"bytes"
     7  	"errors"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"os/user"
    13  	"path"
    14  	"strings"
    15  )
    16  
    17  var execCommand = exec.Command
    18  
    19  func getSessionBusPlatformAddress() (string, error) {
    20  	cmd := execCommand("dbus-launch")
    21  	b, err := cmd.CombinedOutput()
    22  
    23  	if err != nil {
    24  		return "", err
    25  	}
    26  
    27  	i := bytes.IndexByte(b, '=')
    28  	j := bytes.IndexByte(b, '\n')
    29  
    30  	if i == -1 || j == -1 || i > j {
    31  		return "", errors.New("dbus: couldn't determine address of session bus")
    32  	}
    33  
    34  	env, addr := string(b[0:i]), string(b[i+1:j])
    35  	os.Setenv(env, addr)
    36  
    37  	return addr, nil
    38  }
    39  
    40  // tryDiscoverDbusSessionBusAddress tries to discover an existing dbus session
    41  // and return the value of its DBUS_SESSION_BUS_ADDRESS.
    42  // It tries different techniques employed by different operating systems,
    43  // returning the first valid address it finds, or an empty string.
    44  //
    45  // * /run/user/<uid>/bus           if this exists, it *is* the bus socket. present on
    46  //                                 Ubuntu 18.04
    47  // * /run/user/<uid>/dbus-session: if this exists, it can be parsed for the bus
    48  //                                 address. present on Ubuntu 16.04
    49  //
    50  // See https://dbus.freedesktop.org/doc/dbus-launch.1.html
    51  func tryDiscoverDbusSessionBusAddress() string {
    52  	if runtimeDirectory, err := getRuntimeDirectory(); err == nil {
    53  
    54  		if runUserBusFile := path.Join(runtimeDirectory, "bus"); fileExists(runUserBusFile) {
    55  			// if /run/user/<uid>/bus exists, that file itself
    56  			// *is* the unix socket, so return its path
    57  			return fmt.Sprintf("unix:path=%s", EscapeBusAddressValue(runUserBusFile))
    58  		}
    59  		if runUserSessionDbusFile := path.Join(runtimeDirectory, "dbus-session"); fileExists(runUserSessionDbusFile) {
    60  			// if /run/user/<uid>/dbus-session exists, it's a
    61  			// text file // containing the address of the socket, e.g.:
    62  			// DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-E1c73yNqrG
    63  
    64  			if f, err := ioutil.ReadFile(runUserSessionDbusFile); err == nil {
    65  				fileContent := string(f)
    66  
    67  				prefix := "DBUS_SESSION_BUS_ADDRESS="
    68  
    69  				if strings.HasPrefix(fileContent, prefix) {
    70  					address := strings.TrimRight(strings.TrimPrefix(fileContent, prefix), "\n\r")
    71  					return address
    72  				}
    73  			}
    74  		}
    75  	}
    76  	return ""
    77  }
    78  
    79  func getRuntimeDirectory() (string, error) {
    80  	if currentUser, err := user.Current(); err != nil {
    81  		return "", err
    82  	} else {
    83  		return fmt.Sprintf("/run/user/%s", currentUser.Uid), nil
    84  	}
    85  }
    86  
    87  func fileExists(filename string) bool {
    88  	_, err := os.Stat(filename)
    89  	return !os.IsNotExist(err)
    90  }
    91  

View as plain text