github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/utils/syslog/service.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package syslog
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/service"
    12  )
    13  
    14  const svcName = "rsyslog"
    15  
    16  // These are patched out during tests.
    17  var (
    18  	getEuid = func() int {
    19  		return os.Geteuid()
    20  	}
    21  	restart = func(name string) error {
    22  		return service.Restart(name)
    23  	}
    24  )
    25  
    26  // Restart restarts the "rsyslog" service using the local host's
    27  // currently running init system (e.g. upstart, systemd).  If the caller
    28  // is not a superuser then Restart returns an error.
    29  func Restart() error {
    30  	if getEuid() == 0 {
    31  		if err := restart(svcName); err != nil {
    32  			return errors.Annotatef(err, "failed to restart service %q", svcName)
    33  		}
    34  		return nil
    35  	}
    36  	return errors.Errorf("must be root")
    37  }