github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/overlord/snapstate/dbus.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package snapstate
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/overlord/state"
    26  	"github.com/snapcore/snapd/snap"
    27  )
    28  
    29  func getActivatableDBusServices(info *snap.Info) (session, system map[string]bool) {
    30  	session = make(map[string]bool)
    31  	system = make(map[string]bool)
    32  	for _, app := range info.Apps {
    33  		for _, slot := range app.ActivatesOn {
    34  			busName, ok := slot.Attrs["name"].(string)
    35  			// Should not fail for info that has passed
    36  			// validation
    37  			if !ok {
    38  				continue
    39  			}
    40  			switch app.DaemonScope {
    41  			case snap.SystemDaemon:
    42  				system[busName] = true
    43  			case snap.UserDaemon:
    44  				session[busName] = true
    45  			}
    46  		}
    47  	}
    48  	return session, system
    49  }
    50  
    51  func checkDBusServiceConflicts(st *state.State, info *snap.Info) error {
    52  	sessionServices, systemServices := getActivatableDBusServices(info)
    53  
    54  	// If there are no activatable services, we're done
    55  	if len(sessionServices) == 0 && len(systemServices) == 0 {
    56  		return nil
    57  	}
    58  
    59  	stateMap, err := All(st)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	for instanceName, snapst := range stateMap {
    64  		if instanceName == info.InstanceName() {
    65  			continue
    66  		}
    67  
    68  		otherInfo, err := snapst.CurrentInfo()
    69  		if err != nil {
    70  			return err
    71  		}
    72  
    73  		otherSessionServices, otherSystemServices := getActivatableDBusServices(otherInfo)
    74  		for svc := range sessionServices {
    75  			if otherSessionServices[svc] {
    76  				return fmt.Errorf("snap %q requesting to activate on session bus name %q conflicts with snap %q use", info.InstanceName(), svc, instanceName)
    77  			}
    78  		}
    79  		for svc := range systemServices {
    80  			if otherSystemServices[svc] {
    81  				return fmt.Errorf("snap %q requesting to activate on system bus name %q conflicts with snap %q use", info.InstanceName(), svc, instanceName)
    82  			}
    83  		}
    84  	}
    85  	return nil
    86  }