github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/subsystem/service.go (about)

     1  // Copyright 2023 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package subsystem
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  type Service struct {
    11  	*Extractor
    12  	Revision  int
    13  	perName   map[string]*Subsystem
    14  	perParent map[*Subsystem][]*Subsystem
    15  }
    16  
    17  func MustMakeService(list []*Subsystem, revision int) *Service {
    18  	service, err := MakeService(list, revision)
    19  	if err != nil {
    20  		panic(fmt.Sprintf("service creation failed: %s", err))
    21  	}
    22  	return service
    23  }
    24  
    25  func MakeService(list []*Subsystem, revision int) (*Service, error) {
    26  	extractor := MakeExtractor(list)
    27  	perName := map[string]*Subsystem{}
    28  	perParent := map[*Subsystem][]*Subsystem{}
    29  	for _, item := range list {
    30  		if item.Name == "" {
    31  			return nil, fmt.Errorf("input contains a subsystem without a name")
    32  		}
    33  		if perName[item.Name] != nil {
    34  			return nil, fmt.Errorf("collision on %#v name", item.Name)
    35  		}
    36  		perName[item.Name] = item
    37  		for _, p := range item.Parents {
    38  			perParent[p] = append(perParent[p], item)
    39  		}
    40  	}
    41  	return &Service{
    42  		Extractor: extractor,
    43  		Revision:  revision,
    44  		perName:   perName,
    45  		perParent: perParent,
    46  	}, nil
    47  }
    48  
    49  func (s *Service) ByName(name string) *Subsystem {
    50  	return s.perName[name]
    51  }
    52  
    53  func (s *Service) List() []*Subsystem {
    54  	ret := []*Subsystem{}
    55  	for _, item := range s.perName {
    56  		ret = append(ret, item)
    57  	}
    58  	return ret
    59  }
    60  
    61  func (s *Service) Children(parent *Subsystem) []*Subsystem {
    62  	return append([]*Subsystem{}, s.perParent[parent]...)
    63  }