@@ -297,7 +297,7 @@ func (m *sysvinitManager) BuildCommand(action string, config *ServiceConfig) ([]
297297 "sh" ,
298298 "-c" ,
299299 fmt .Sprintf ("if ls /etc/rc*.d/S*%s >/dev/null 2>&1; then echo 'enabled'; else echo 'disabled'; fi" , service )}, nil
300- case "is-active" :
300+ case "is-active" , "status" :
301301 return []string {
302302 "sh" ,
303303 "-c" ,
@@ -335,23 +335,6 @@ func (m *sysvinitManager) ParseStatus(output string, config *ServiceConfig, stat
335335 return result , err
336336 }
337337 return false , fmt .Errorf ("unsupported status type: %s" , statusType )
338- // service := config.ServiceName[m.name]
339- // serviceRegex := regexp.MustCompile(fmt.Sprintf(`\b%s\b`, regexp.QuoteMeta(service)))
340- // lines := strings.Split(output, "\n")
341- // for _, line := range lines {
342- // if serviceRegex.MatchString(line) {
343- // if strings.Contains(line, "not found") {
344- // return false, nil
345- // }
346- // result, err := m.baseManager.ParseStatus(line, config, statusType)
347- // if err != nil {
348- // return false, err
349- // }
350- // return result, nil
351- // }
352- // }
353-
354- // return false, nil
355338}
356339
357340func (m * sysvinitManager ) FindServices (keyword string ) ([]string , error ) {
@@ -373,10 +356,12 @@ type openrcManager struct{ baseManager }
373356
374357func newOpenrcManager () ServiceManager {
375358 return & openrcManager {baseManager {
376- name : "openrc" ,
377- cmdTool : "rc-service" ,
378- activeRegex : regexp .MustCompile (`(?i)^\s*status:\s+(started|running|active)\s*$` ),
379- enabledRegex : regexp .MustCompile (`(?i)^[^\|]+\|\s*(default|enabled)\b.*$` ),
359+ name : "openrc" ,
360+ cmdTool : "rc-service" ,
361+ // activeRegex: regexp.MustCompile(`(?i)^\s*status:\s+(started|running|active)\s*$`),
362+ // enabledRegex: regexp.MustCompile(`(?i)^[^\|]+\|\s*(default|enabled)\b.*$`),
363+ activeRegex : regexp .MustCompile (`(?i)(?:^|\s)\b(running|active)\b(?:$|\s)` ),
364+ enabledRegex : regexp .MustCompile (`(?i)(?:^|\s)\b(enabled)\b(?:$|\s)` ),
380365 }}
381366}
382367func (m * openrcManager ) IsAvailable () bool {
@@ -386,25 +371,39 @@ func (m *openrcManager) IsAvailable() bool {
386371
387372func (m * openrcManager ) ServiceExists (config * ServiceConfig ) (bool , error ) {
388373 return m .commonServiceExists (config , func (name string ) (bool , error ) {
389- ctx , cancel := context . WithTimeout ( context . Background (), serviceCheckTimeout )
390- defer cancel ()
391- out , err := executeCommand ( ctx , m . cmdTool , "-l" )
392- if err != nil {
393- return false , fmt .Errorf ("rc-service -l failed: %w" , err )
374+ _ , err := os . Stat ( filepath . Join ( "/etc/init.d" , name ) )
375+ if os . IsNotExist ( err ) {
376+ return false , nil
377+ } else if err != nil {
378+ return false , fmt .Errorf ("stat /etc/init.d/%s failed: %w" , name , err )
394379 }
395- return bytes . Contains ( out , [] byte ( name )) , nil
380+ return true , nil
396381 })
397382}
398383
399384func (m * openrcManager ) BuildCommand (action string , config * ServiceConfig ) ([]string , error ) {
400385 cmdArgs := m .buildBaseCommand ()
401386 service := config .ServiceName [m .name ]
402- if action == "is-enabled" {
403- cmdArgs = []string {"rc-update" , "check" , service }
387+ switch action {
388+ case "is-enabled" :
389+ return []string {
390+ "sh" ,
391+ "-c" ,
392+ fmt .Sprintf ("if ls /etc/runlevels/default/%s >/dev/null 2>&1; then echo 'enabled'; else echo 'disabled'; fi" , service )}, nil
393+ case "is-active" , "status" :
394+ return []string {
395+ "sh" ,
396+ "-c" ,
397+ fmt .Sprintf ("if service %s status >/dev/null 2>&1; then echo 'active'; else echo 'inactive'; fi" , service ),
398+ }, nil
399+ case "enable" :
400+ return []string {"rc-update" , "add" , service , "default" }, nil
401+ case "disable" :
402+ return []string {"rc-update" , "del" , service , "default" }, nil
403+ default :
404+ cmdArgs = append (cmdArgs , service , action )
404405 return cmdArgs , nil
405406 }
406- cmdArgs = append (cmdArgs , service , action )
407- return cmdArgs , nil
408407}
409408
410409func (m * openrcManager ) ParseStatus (output string , config * ServiceConfig , statusType string ) (bool , error ) {
@@ -418,19 +417,15 @@ func (m *openrcManager) ParseStatus(output string, config *ServiceConfig, status
418417 return result , nil
419418}
420419func (m * openrcManager ) FindServices (keyword string ) ([]string , error ) {
421- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
422- defer cancel ()
423-
424- out , err := executeCommand (ctx , m .cmdTool , "-l" )
420+ files , err := os .ReadDir ("/etc/init.d/" )
425421 if err != nil {
426- return nil , fmt .Errorf ("failed to list openrc services : %w" , err )
422+ return nil , fmt .Errorf ("failed to read init.d directory : %w" , err )
427423 }
428-
424+ keyword = strings . ToLower ( keyword )
429425 var services []string
430- lines := strings .Split (string (out ), "\n " )
431- for _ , line := range lines {
432- if strings .Contains (line , keyword ) {
433- services = append (services , strings .TrimSpace (line ))
426+ for _ , file := range files {
427+ if strings .Contains (file .Name (), keyword ) {
428+ services = append (services , file .Name ())
434429 }
435430 }
436431 return services , nil
0 commit comments