1 Star 0 Fork 1

mysnapcore / mysnapd

forked from tupelo-shen / mysnapd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
helpers.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
tupelo-shen 提交于 2022-11-08 21:39 . fix: usersession commit
// -*- 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"
"gitee.com/mysnapcore/mysnapd/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
}
Go
1
https://gitee.com/mysnapcore/mysnapd.git
git@gitee.com:mysnapcore/mysnapd.git
mysnapcore
mysnapd
mysnapd
v0.1.0

搜索帮助