1 Star 0 Fork 1

mysnapcore / mysnapd

forked from tupelo-shen / mysnapd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
locking.go 3.28 KB
一键复制 编辑 原始数据 按行查看 历史
tupelo-shen 提交于 2022-11-08 15:12 . fix: overlord commit
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 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 backend
import (
"gitee.com/mysnapcore/mysnapd/cmd/snaplock"
"gitee.com/mysnapcore/mysnapd/cmd/snaplock/runinhibit"
"gitee.com/mysnapcore/mysnapd/osutil"
"gitee.com/mysnapcore/mysnapd/snap"
)
func (b Backend) RunInhibitSnapForUnlink(info *snap.Info, hint runinhibit.Hint, decision func() error) (lock *osutil.FileLock, err error) {
// A process may be created after the soft refresh done upon
// the request to refresh a snap. If such process is alive by
// the time this code is reached the refresh process is stopped.
// Grab per-snap lock to prevent new processes from starting. This is
// sufficient to perform the check, even though individual processes
// may fork or exit, we will have per-security-tag information about
// what is running.
lock, err = snaplock.OpenLock(info.InstanceName())
if err != nil {
return nil, err
}
// Keep a copy of lock, so that we can close it in the function below.
// The regular lock variable is assigned to by return, due to the named
// return values.
lockToClose := lock
defer func() {
// If we have a lock but we are returning an error then unlock the lock
// by closing it.
if lockToClose != nil && err != nil {
lockToClose.Close()
}
}()
if err := lock.Lock(); err != nil {
return nil, err
}
//
if err := decision(); err != nil {
return nil, err
}
// Decision function did not fail so we can, while we still hold the snap
// lock, install the snap run inhibition hint, returning the snap lock to
// the caller.
//
// XXX: should we move this logic to the place that calls the "soft"
// check instead? Doing so would somewhat change the semantic of soft
// and hard checks, as it would effectively make hard check a no-op,
// but it might provide a nicer user experience.
if err := runinhibit.LockWithHint(info.InstanceName(), hint); err != nil {
return nil, err
}
return lock, nil
}
// WithSnapLock executes given action with the snap lock held.
//
// The lock is also used by snap-confine during pre-snap mount namespace
// initialization. Holding it allows to ensure mutual exclusion during the
// process of preparing a new snap app or hook processes. It does not prevent
// existing application or hook processes from forking.
//
// Note that this is not a method of the Backend type, so that it can be
// invoked from doInstall, which does not have access to a backend object.
func WithSnapLock(info *snap.Info, action func() error) error {
lock, err := snaplock.OpenLock(info.InstanceName())
if err != nil {
return err
}
// Closing the lock also unlocks it, if locked.
defer lock.Close()
if err := lock.Lock(); err != nil {
return err
}
return action()
}
Go
1
https://gitee.com/mysnapcore/mysnapd.git
git@gitee.com:mysnapcore/mysnapd.git
mysnapcore
mysnapd
mysnapd
v0.1.0

搜索帮助