1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package userd
import (
"fmt"
"github.com/godbus/dbus/v5"
"github.com/snapcore/snapd/sandbox/cgroup"
)
var snapFromSender = snapFromSenderImpl
func snapFromSenderImpl(conn *dbus.Conn, sender dbus.Sender) (string, error) {
pid, err := connectionPid(conn, sender)
if err != nil {
return "", fmt.Errorf("cannot get connection pid: %v", err)
}
snap, err := cgroup.SnapNameFromPid(pid)
if err != nil {
return "", fmt.Errorf("cannot find snap for connection: %v", err)
}
// Check that the sender is still connected to the bus: if it
// has disconnected between the GetConnectionUnixProcessID
// call and when we poked around in /proc, then it is possible
// that the process ID was reused.
if !nameHasOwner(conn, sender) {
return "", fmt.Errorf("sender is no longer connected to the bus")
}
return snap, nil
}
func connectionPid(conn *dbus.Conn, sender dbus.Sender) (pid int, err error) {
call := conn.BusObject().Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, sender)
if call.Err != nil {
return 0, call.Err
}
call.Store(&pid)
return pid, nil
}
func nameHasOwner(conn *dbus.Conn, sender dbus.Sender) bool {
call := conn.BusObject().Call("org.freedesktop.DBus.NameHasOwner", 0, sender)
if call.Err != nil {
return false
}
var hasOwner bool
call.Store(&hasOwner)
return hasOwner
}
|