WIP
This commit is contained in:
185
cmds/cmdbase/service.go
Normal file
185
cmds/cmdbase/service.go
Normal file
@@ -0,0 +1,185 @@
|
||||
package cmdbase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/service"
|
||||
"github.com/safing/portmaster/service/mgr"
|
||||
)
|
||||
|
||||
var (
|
||||
RebootOnRestart bool
|
||||
PrintStackOnExit bool
|
||||
)
|
||||
|
||||
type SystemService interface {
|
||||
Run()
|
||||
IsService() bool
|
||||
RestartService() error
|
||||
}
|
||||
|
||||
type ServiceInstance interface {
|
||||
Ready() bool
|
||||
Start() error
|
||||
Stop() error
|
||||
Restart()
|
||||
Shutdown()
|
||||
Ctx() context.Context
|
||||
IsShuttingDown() bool
|
||||
ShuttingDown() <-chan struct{}
|
||||
ShutdownCtx() context.Context
|
||||
IsShutDown() bool
|
||||
ShutdownComplete() <-chan struct{}
|
||||
ExitCode() int
|
||||
ShouldRestartIsSet() bool
|
||||
CommandLineOperationIsSet() bool
|
||||
CommandLineOperationExecute() error
|
||||
}
|
||||
|
||||
var (
|
||||
SvcFactory func(*service.ServiceConfig) (ServiceInstance, error)
|
||||
SvcConfig *service.ServiceConfig
|
||||
)
|
||||
|
||||
func RunService(cmd *cobra.Command, args []string) {
|
||||
if SvcFactory == nil || SvcConfig == nil {
|
||||
fmt.Fprintln(os.Stderr, "internal error: service not set up in cmdbase")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Start logging.
|
||||
// Note: Must be created before the service instance, so that they use the right logger.
|
||||
err := log.Start(SvcConfig.LogLevel, SvcConfig.LogToStdout, SvcConfig.LogDir)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err.Error())
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
// Create instance.
|
||||
// Instance modules might request a cmdline execution of a function.
|
||||
var execCmdLine bool
|
||||
instance, err := SvcFactory(SvcConfig)
|
||||
switch {
|
||||
case err == nil:
|
||||
// Continue
|
||||
case errors.Is(err, mgr.ErrExecuteCmdLineOp):
|
||||
execCmdLine = true
|
||||
default:
|
||||
fmt.Printf("error creating an instance: %s\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
// Execute module command line operation, if requested or available.
|
||||
switch {
|
||||
case !execCmdLine:
|
||||
// Run service.
|
||||
case !instance.CommandLineOperationIsSet():
|
||||
fmt.Println("command line operation execution requested, but not set")
|
||||
os.Exit(3)
|
||||
default:
|
||||
// Run the function and exit.
|
||||
fmt.Println("executing cmdline op")
|
||||
err = instance.CommandLineOperationExecute()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "command line operation failed: %s\n", err)
|
||||
os.Exit(3)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// START
|
||||
|
||||
// Create system service.
|
||||
service := NewSystemService(instance)
|
||||
|
||||
// Start instance via system service manager.
|
||||
go func() {
|
||||
service.Run()
|
||||
}()
|
||||
|
||||
// SHUTDOWN
|
||||
|
||||
// Wait for shutdown to be started.
|
||||
<-instance.ShuttingDown()
|
||||
|
||||
// Wait for shutdown to be finished.
|
||||
select {
|
||||
case <-instance.ShutdownComplete():
|
||||
// Print stack on shutdown, if enabled.
|
||||
if PrintStackOnExit {
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON EXIT")
|
||||
}
|
||||
case <-time.After(3 * time.Minute):
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK - TAKING TOO LONG FOR SHUTDOWN")
|
||||
}
|
||||
|
||||
// Check if restart was triggered and send start service command if true.
|
||||
if instance.ShouldRestartIsSet() && service.IsService() {
|
||||
// Check if we should reboot instead.
|
||||
var rebooting bool
|
||||
if RebootOnRestart {
|
||||
// Trigger system reboot and record success.
|
||||
rebooting = triggerSystemReboot()
|
||||
if !rebooting {
|
||||
log.Warningf("updates: rebooting failed, only restarting service instead")
|
||||
}
|
||||
}
|
||||
|
||||
// Restart service if not rebooting.
|
||||
if !rebooting {
|
||||
if err := service.RestartService(); err != nil {
|
||||
slog.Error("failed to restart service", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stop logging.
|
||||
log.Shutdown()
|
||||
|
||||
// Give a small amount of time for everything to settle:
|
||||
// - All logs written.
|
||||
// - Restart command started, if needed.
|
||||
// - Windows service manager notified.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Exit
|
||||
os.Exit(instance.ExitCode())
|
||||
}
|
||||
|
||||
func printStackTo(writer io.Writer, msg string) {
|
||||
_, err := fmt.Fprintf(writer, "===== %s =====\n", msg)
|
||||
if err == nil {
|
||||
err = pprof.Lookup("goroutine").WriteTo(writer, 1)
|
||||
}
|
||||
if err != nil {
|
||||
slog.Error("failed to write stack trace", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func triggerSystemReboot() (success bool) {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
err := exec.Command("systemctl", "reboot").Run()
|
||||
if err != nil {
|
||||
log.Errorf("updates: triggering reboot with systemctl failed: %s", err)
|
||||
return false
|
||||
}
|
||||
default:
|
||||
log.Warningf("updates: rebooting is not support on %s", runtime.GOOS)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
128
cmds/cmdbase/service_linux.go
Normal file
128
cmds/cmdbase/service_linux.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package cmdbase
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
processInfo "github.com/shirou/gopsutil/process"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
)
|
||||
|
||||
type LinuxSystemService struct {
|
||||
instance ServiceInstance
|
||||
}
|
||||
|
||||
func NewSystemService(instance ServiceInstance) *LinuxSystemService {
|
||||
return &LinuxSystemService{instance: instance}
|
||||
}
|
||||
|
||||
func (s *LinuxSystemService) Run() {
|
||||
// Start instance.
|
||||
err := s.instance.Start()
|
||||
if err != nil {
|
||||
slog.Error("failed to start", "err", err)
|
||||
|
||||
// Print stack on start failure, if enabled.
|
||||
if PrintStackOnExit {
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON START FAILURE")
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Subscribe to signals.
|
||||
signalCh := make(chan os.Signal, 1)
|
||||
signal.Notify(
|
||||
signalCh,
|
||||
os.Interrupt,
|
||||
syscall.SIGHUP,
|
||||
syscall.SIGINT,
|
||||
syscall.SIGTERM,
|
||||
syscall.SIGQUIT,
|
||||
syscall.SIGUSR1,
|
||||
)
|
||||
|
||||
// Wait for shutdown signal.
|
||||
wait:
|
||||
for {
|
||||
select {
|
||||
case <-s.instance.ShuttingDown():
|
||||
break wait
|
||||
case sig := <-signalCh:
|
||||
// Only print and continue to wait if SIGUSR1
|
||||
if sig == syscall.SIGUSR1 {
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON REQUEST")
|
||||
continue wait
|
||||
} else {
|
||||
// Trigger shutdown.
|
||||
fmt.Printf(" <SIGNAL: %v>\n", sig) // CLI output.
|
||||
slog.Warn("received stop signal", "signal", sig)
|
||||
s.instance.Shutdown()
|
||||
break wait
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for shutdown to finish.
|
||||
|
||||
// Catch signals during shutdown.
|
||||
// Force exit after 5 interrupts.
|
||||
forceCnt := 5
|
||||
for {
|
||||
select {
|
||||
case <-s.instance.ShutdownComplete():
|
||||
return
|
||||
case sig := <-signalCh:
|
||||
if sig != syscall.SIGUSR1 {
|
||||
forceCnt--
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <SIGNAL: %s> again, but already shutting down - %d more to force\n", sig, forceCnt)
|
||||
} else {
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LinuxSystemService) RestartService() error {
|
||||
// Check if user defined custom command for restarting the service.
|
||||
restartCommand, exists := os.LookupEnv("PORTMASTER_RESTART_COMMAND")
|
||||
|
||||
// Run the service restart
|
||||
var cmd *exec.Cmd
|
||||
if exists && restartCommand != "" {
|
||||
slog.Debug("running custom restart command", "command", restartCommand)
|
||||
cmd = exec.Command("sh", "-c", restartCommand)
|
||||
} else {
|
||||
cmd = exec.Command("systemctl", "restart", "portmaster")
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("failed run restart command: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LinuxSystemService) IsService() bool {
|
||||
// Get own process ID
|
||||
pid := os.Getpid()
|
||||
|
||||
// Get parent process ID.
|
||||
currentProcess, err := processInfo.NewProcess(int32(pid)) //nolint:gosec
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
ppid, err := currentProcess.Ppid()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the parent process ID is 1 == init system
|
||||
return ppid == 1
|
||||
}
|
||||
237
cmds/cmdbase/service_windows.go
Normal file
237
cmds/cmdbase/service_windows.go
Normal file
@@ -0,0 +1,237 @@
|
||||
package cmdbase
|
||||
|
||||
// Based on the official Go examples from
|
||||
// https://github.com/golang/sys/blob/master/windows/svc/example
|
||||
// by The Go Authors.
|
||||
// Original LICENSE (sha256sum: 2d36597f7117c38b006835ae7f537487207d8ec407aa9d9980794b2030cbc067) can be found in vendor/pkg cache directory.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/windows/svc"
|
||||
"golang.org/x/sys/windows/svc/debug"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
)
|
||||
|
||||
const serviceName = "PortmasterCore"
|
||||
|
||||
type WindowsSystemService struct {
|
||||
instance ServiceInstance
|
||||
}
|
||||
|
||||
func NewSystemService(instance ServiceInstance) *WindowsSystemService {
|
||||
return &WindowsSystemService{instance: instance}
|
||||
}
|
||||
|
||||
func (s *WindowsSystemService) Run() {
|
||||
svcRun := svc.Run
|
||||
|
||||
// Check if we are running interactively.
|
||||
isService, err := svc.IsWindowsService()
|
||||
switch {
|
||||
case err != nil:
|
||||
slog.Warn("failed to determine if running interactively", "err", err)
|
||||
slog.Warn("continuing without service integration (no real service)")
|
||||
svcRun = debug.Run
|
||||
|
||||
case !isService:
|
||||
slog.Warn("running interactively, switching to debug execution (no real service)")
|
||||
svcRun = debug.Run
|
||||
}
|
||||
|
||||
// Run service client.
|
||||
err = svcRun(serviceName, s)
|
||||
if err != nil {
|
||||
slog.Error("service execution failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Execution continues in s.Execute().
|
||||
}
|
||||
|
||||
func (s *WindowsSystemService) Execute(args []string, changeRequests <-chan svc.ChangeRequest, changes chan<- svc.Status) (svcSpecificEC bool, exitCode uint32) {
|
||||
// Tell service manager we are starting.
|
||||
changes <- svc.Status{State: svc.StartPending}
|
||||
|
||||
// Start instance.
|
||||
err := s.instance.Start()
|
||||
if err != nil {
|
||||
fmt.Printf("failed to start: %s\n", err)
|
||||
|
||||
// Print stack on start failure, if enabled.
|
||||
if PrintStackOnExit {
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON START FAILURE")
|
||||
}
|
||||
|
||||
// Notify service manager we stopped again.
|
||||
changes <- svc.Status{State: svc.Stopped}
|
||||
|
||||
// Relay exit code to service manager.
|
||||
return false, 1
|
||||
}
|
||||
|
||||
// Tell service manager we are up and running!
|
||||
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
|
||||
|
||||
// Subscribe to signals.
|
||||
// Docs: https://pkg.go.dev/os/signal?GOOS=windows
|
||||
signalCh := make(chan os.Signal, 4)
|
||||
signal.Notify(
|
||||
signalCh,
|
||||
|
||||
// Windows ^C (Control-C) or ^BREAK (Control-Break).
|
||||
// Completely prevents kill.
|
||||
os.Interrupt,
|
||||
|
||||
// Windows CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT.
|
||||
// Does not prevent kill, but gives a little time to stop service.
|
||||
syscall.SIGTERM,
|
||||
)
|
||||
|
||||
// Wait for shutdown signal.
|
||||
waitSignal:
|
||||
for {
|
||||
select {
|
||||
case sig := <-signalCh:
|
||||
// Trigger shutdown.
|
||||
fmt.Printf(" <SIGNAL: %v>\n", sig) // CLI output.
|
||||
slog.Warn("received stop signal", "signal", sig)
|
||||
break waitSignal
|
||||
|
||||
case c := <-changeRequests:
|
||||
switch c.Cmd {
|
||||
case svc.Interrogate:
|
||||
changes <- c.CurrentStatus
|
||||
|
||||
case svc.Stop, svc.Shutdown:
|
||||
fmt.Printf(" <SERVICE CMD: %v>\n", serviceCmdName(c.Cmd)) // CLI output.
|
||||
slog.Warn("received service shutdown command", "cmd", c.Cmd)
|
||||
break waitSignal
|
||||
|
||||
default:
|
||||
slog.Error("unexpected service control request", "cmd", serviceCmdName(c.Cmd))
|
||||
}
|
||||
|
||||
case <-s.instance.ShuttingDown():
|
||||
break waitSignal
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for shutdown to finish.
|
||||
changes <- svc.Status{State: svc.StopPending}
|
||||
|
||||
// Catch signals during shutdown.
|
||||
// Force exit after 5 interrupts.
|
||||
forceCnt := 5
|
||||
waitShutdown:
|
||||
for {
|
||||
select {
|
||||
case <-s.instance.ShutdownComplete():
|
||||
break waitShutdown
|
||||
|
||||
case sig := <-signalCh:
|
||||
forceCnt--
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <SIGNAL: %s> but already shutting down - %d more to force\n", sig, forceCnt)
|
||||
} else {
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
case c := <-changeRequests:
|
||||
switch c.Cmd {
|
||||
case svc.Interrogate:
|
||||
changes <- c.CurrentStatus
|
||||
|
||||
case svc.Stop, svc.Shutdown:
|
||||
forceCnt--
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <SERVICE CMD: %v> but already shutting down - %d more to force\n", serviceCmdName(c.Cmd), forceCnt)
|
||||
} else {
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
default:
|
||||
slog.Error("unexpected service control request", "cmd", serviceCmdName(c.Cmd))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Notify service manager.
|
||||
changes <- svc.Status{State: svc.Stopped}
|
||||
|
||||
return false, 0
|
||||
}
|
||||
|
||||
func (s *WindowsSystemService) IsService() bool {
|
||||
isService, err := svc.IsWindowsService()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return isService
|
||||
}
|
||||
|
||||
func (s *WindowsSystemService) RestartService() error {
|
||||
// Script that wait for portmaster service status to change to stop
|
||||
// and then sends a start command for the same service.
|
||||
command := `
|
||||
$serviceName = "PortmasterCore"
|
||||
while ((Get-Service -Name $serviceName).Status -ne 'Stopped') {
|
||||
Start-Sleep -Seconds 1
|
||||
}
|
||||
sc.exe start $serviceName`
|
||||
|
||||
// Create the command to execute the PowerShell script
|
||||
cmd := exec.Command("powershell.exe", "-Command", command)
|
||||
// Start the command. The script will continue even after the parent process exits.
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func serviceCmdName(cmd svc.Cmd) string {
|
||||
switch cmd {
|
||||
case svc.Stop:
|
||||
return "Stop"
|
||||
case svc.Pause:
|
||||
return "Pause"
|
||||
case svc.Continue:
|
||||
return "Continue"
|
||||
case svc.Interrogate:
|
||||
return "Interrogate"
|
||||
case svc.Shutdown:
|
||||
return "Shutdown"
|
||||
case svc.ParamChange:
|
||||
return "ParamChange"
|
||||
case svc.NetBindAdd:
|
||||
return "NetBindAdd"
|
||||
case svc.NetBindRemove:
|
||||
return "NetBindRemove"
|
||||
case svc.NetBindEnable:
|
||||
return "NetBindEnable"
|
||||
case svc.NetBindDisable:
|
||||
return "NetBindDisable"
|
||||
case svc.DeviceEvent:
|
||||
return "DeviceEvent"
|
||||
case svc.HardwareProfileChange:
|
||||
return "HardwareProfileChange"
|
||||
case svc.PowerEvent:
|
||||
return "PowerEvent"
|
||||
case svc.SessionChange:
|
||||
return "SessionChange"
|
||||
case svc.PreShutdown:
|
||||
return "PreShutdown"
|
||||
default:
|
||||
return "Unknown Command"
|
||||
}
|
||||
}
|
||||
73
cmds/cmdbase/update.go
Normal file
73
cmds/cmdbase/update.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package cmdbase
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/base/notifications"
|
||||
"github.com/safing/portmaster/service"
|
||||
"github.com/safing/portmaster/service/updates"
|
||||
)
|
||||
|
||||
var UpdateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "Force an update of all components.",
|
||||
RunE: update,
|
||||
}
|
||||
|
||||
func update(cmd *cobra.Command, args []string) error {
|
||||
// Finalize config.
|
||||
err := SvcConfig.Init()
|
||||
if err != nil {
|
||||
return fmt.Errorf("internal configuration error: %w", err)
|
||||
}
|
||||
// Force logging to stdout.
|
||||
SvcConfig.LogToStdout = true
|
||||
|
||||
// Start logging.
|
||||
_ = log.Start(SvcConfig.LogLevel, SvcConfig.LogToStdout, SvcConfig.LogDir)
|
||||
defer log.Shutdown()
|
||||
|
||||
// Create updaters.
|
||||
instance := &updateDummyInstance{}
|
||||
binaryUpdateConfig, intelUpdateConfig, err := service.MakeUpdateConfigs(SvcConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("init updater config: %w", err)
|
||||
}
|
||||
binaryUpdates, err := updates.New(instance, "Binary Updater", *binaryUpdateConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("configure binary updates: %w", err)
|
||||
}
|
||||
intelUpdates, err := updates.New(instance, "Intel Updater", *intelUpdateConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("configure intel updates: %w", err)
|
||||
}
|
||||
|
||||
// Force update all.
|
||||
binErr := binaryUpdates.ForceUpdate()
|
||||
if binErr != nil {
|
||||
slog.Error("binary update failed", "err", binErr)
|
||||
}
|
||||
intelErr := intelUpdates.ForceUpdate()
|
||||
if intelErr != nil {
|
||||
slog.Error("intel update failed", "err", intelErr)
|
||||
}
|
||||
|
||||
// Return error.
|
||||
if binErr != nil {
|
||||
return fmt.Errorf("binary update failed: %w", binErr)
|
||||
}
|
||||
if intelErr != nil {
|
||||
return fmt.Errorf("intel update failed: %w", intelErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type updateDummyInstance struct{}
|
||||
|
||||
func (udi *updateDummyInstance) Restart() {}
|
||||
func (udi *updateDummyInstance) Shutdown() {}
|
||||
func (udi *updateDummyInstance) Notifications() *notifications.Notifications { return nil }
|
||||
20
cmds/cmdbase/version.go
Normal file
20
cmds/cmdbase/version.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package cmdbase
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/safing/portmaster/base/info"
|
||||
)
|
||||
|
||||
var VersionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Show version and related metadata.",
|
||||
RunE: Version,
|
||||
}
|
||||
|
||||
func Version(cmd *cobra.Command, args []string) error {
|
||||
fmt.Println(info.FullVersion())
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user