Skip to content

Commit

Permalink
gofmt
Browse files Browse the repository at this point in the history
Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
  • Loading branch information
n1hility committed Sep 14, 2023
1 parent 1a1ee26 commit de5e47b
Show file tree
Hide file tree
Showing 15 changed files with 594 additions and 594 deletions.
38 changes: 19 additions & 19 deletions pkg/winquit/channels_windows.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
package winquit

import (
"os"
"syscall"
"os"
"syscall"
)

type baseChannelType interface {
getKey() any
notifyNonBlocking()
notifyBlocking()
getKey() any
notifyNonBlocking()
notifyBlocking()
}

type boolChannelType struct {
channel chan bool
channel chan bool
}

func (b *boolChannelType) getKey() any {
return b.channel
return b.channel
}

func (b *boolChannelType) notifyNonBlocking() {
select {
case b.channel <- true:
default:
}
select {
case b.channel <- true:
default:
}
}

func (s *boolChannelType) notifyBlocking() {
s.channel <- true
s.channel <- true
}

type sigChannelType struct {
channel chan os.Signal
channel chan os.Signal
}

func (s *sigChannelType) getKey() any {
return s.channel
return s.channel
}

func (s *sigChannelType) notifyNonBlocking() {
select {
case s.channel <- syscall.SIGTERM:
default:
}
select {
case s.channel <- syscall.SIGTERM:
default:
}
}

func (s *sigChannelType) notifyBlocking() {
s.channel <- syscall.SIGTERM
s.channel <- syscall.SIGTERM
}
6 changes: 3 additions & 3 deletions pkg/winquit/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package winquit

import (
"time"
"time"
)

// RequestQuit sends a Windows quit notification to the specified process id.
Expand All @@ -17,7 +17,7 @@ import (
// Callers must have appropriate security permissions, otherwise an error will
// be returned. See the notes in the package documentation for more details.
func RequestQuit(pid int) error {
return requestQuit(pid)
return requestQuit(pid)
}

// QuitProcess first sends a Windows quit notification to the specified process id,
Expand All @@ -27,5 +27,5 @@ func RequestQuit(pid int) error {
// Callers must have appropriate security permissions, otherwise an error will
// be returned. See the notes in the package documentation for more details.
func QuitProcess(pid int, waitNicely time.Duration) error {
return quitProcess(pid, waitNicely)
return quitProcess(pid, waitNicely)
}
8 changes: 4 additions & 4 deletions pkg/winquit/client_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
package winquit

import (
"fmt"
"time"
"fmt"
"time"
)

func requestQuit(pid int) error {
return fmt.Errorf("not implemented on non-Windows")
return fmt.Errorf("not implemented on non-Windows")
}

func quitProcess(pid int, waitNicely time.Duration) error {
return fmt.Errorf("not implemented on non-Windows")
return fmt.Errorf("not implemented on non-Windows")
}
58 changes: 29 additions & 29 deletions pkg/winquit/client_windows.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
package winquit

import (
"os"
"time"
"os"
"time"

"github.com/containers/winquit/pkg/winquit/win32"
"github.com/sirupsen/logrus"
"github.com/containers/winquit/pkg/winquit/win32"
"github.com/sirupsen/logrus"
)

func requestQuit(pid int) error {
threads, err := win32.GetProcThreads(uint32(pid))
if err != nil {
return err
}
threads, err := win32.GetProcThreads(uint32(pid))
if err != nil {
return err
}

for _, thread := range threads {
logrus.Debugf("Closing windows on thread %d", thread)
win32.CloseThreadWindows(uint32(thread))
}
for _, thread := range threads {
logrus.Debugf("Closing windows on thread %d", thread)
win32.CloseThreadWindows(uint32(thread))
}

return nil
return nil
}

func quitProcess(pid int, waitNicely time.Duration) error {
_ = RequestQuit(pid)
_ = RequestQuit(pid)

proc, err := os.FindProcess(pid)
if err != nil {
return nil
}
proc, err := os.FindProcess(pid)
if err != nil {
return nil
}

done := make(chan bool)
done := make(chan bool)

go func() {
proc.Wait()
done <- true
}()
go func() {
proc.Wait()
done <- true
}()

select {
case <-done:
return nil
case <-time.After(waitNicely):
}
select {
case <-done:
return nil
case <-time.After(waitNicely):
}

return proc.Kill()
return proc.Kill()
}
76 changes: 38 additions & 38 deletions pkg/winquit/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,64 @@
// The following example demonstrates usage of NotifyOnQuit() to wait for a
// windows quit event before shutting down:
//
// func server() {
// fmt.Println("Starting server")
// func server() {
// fmt.Println("Starting server")
//
// // Create a channel, and register it
// done := make(chan bool, 1)
// winquit.NotifyOnQuit(done)
// // Create a channel, and register it
// done := make(chan bool, 1)
// winquit.NotifyOnQuit(done)
//
// // Wait until we receive a quit event
// <-done
// // Wait until we receive a quit event
// <-done
//
// fmt.Println("Shutting down")
// // Perform cleanup tasks
// }
// fmt.Println("Shutting down")
// // Perform cleanup tasks
// }
//
// # Blended signal example
//
// The following example demonstrates usage of SimulateSigTermOnQuit() in
// concert with signal.Notify():
//
// func server() {
// fmt.Println("Starting server")
// func server() {
// fmt.Println("Starting server")
//
// // Create a channel, and register it
// done := make(chan os.Signal, 1)
// // Create a channel, and register it
// done := make(chan os.Signal, 1)
//
// // Wait on console interrupt events
// signal.Notify(done, syscall.SIGINT)
// // Wait on console interrupt events
// signal.Notify(done, syscall.SIGINT)
//
// // Simulate SIGTERM when a quit occurs
// winquit.SimulateSigTermOnQuit(done)
// // Simulate SIGTERM when a quit occurs
// winquit.SimulateSigTermOnQuit(done)
//
// // Wait until we receive a signal or quit event
// <-done
// // Wait until we receive a signal or quit event
// <-done
//
// fmt.Println("Shutting down")
// // Perform cleanup tasks
// }
// fmt.Println("Shutting down")
// // Perform cleanup tasks
// }
//
// # Client example
//
// The following example demonstrates how an application can ask or
// force other windows programs to quit:
//
// func client() {
// // Ask nicely for program "one" to quit. This request may not
// // be honored if its a console application, or if the program
// // is hung
// if err := winquit.RequestQuit(pidOne); err != nil {
// fmt.Printf("error sending quit request, %s", err.Error())
// }
//
// // Force program "two" to quit, but give it 20 seconds to
// // perform any cleanup tasks and quit on it's own
// timeout := time.Second * 20
// if err := winquit.QuitProcess(pidTwo, timeout); err != nil {
// fmt.Printf("error killing process, %s", err.Error())
// }
// }
// func client() {
// // Ask nicely for program "one" to quit. This request may not
// // be honored if its a console application, or if the program
// // is hung
// if err := winquit.RequestQuit(pidOne); err != nil {
// fmt.Printf("error sending quit request, %s", err.Error())
// }
//
// // Force program "two" to quit, but give it 20 seconds to
// // perform any cleanup tasks and quit on it's own
// timeout := time.Second * 20
// if err := winquit.QuitProcess(pidTwo, timeout); err != nil {
// fmt.Printf("error killing process, %s", err.Error())
// }
// }
//
// # How it works
//
Expand Down
6 changes: 3 additions & 3 deletions pkg/winquit/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package winquit

import (
"os"
"os"
)

// NotifyOnQuit relays a Windows quit notification to the boolean done channel.
Expand All @@ -20,7 +20,7 @@ import (
// If this function is called after a Windows quit notification has occurred, it
// will immediately deliver a "true" value.
func NotifyOnQuit(done chan bool) {
notifyOnQuit(done)
notifyOnQuit(done)
}

// SimulateSigTermOnQuit relays a Windows quit notification following the same
Expand All @@ -30,5 +30,5 @@ func NotifyOnQuit(done chan bool) {
// This function allows for the reuse of the same underlying channel used with
// in a separate os.signal.Notify method call.
func SimulateSigTermOnQuit(handler chan os.Signal) {
simulateSigTermOnQuit(handler)
simulateSigTermOnQuit(handler)
}
2 changes: 1 addition & 1 deletion pkg/winquit/server_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package winquit

import (
"os"
"os"
)

func notifyOnQuit(done chan bool) {
Expand Down
Loading

0 comments on commit de5e47b

Please sign in to comment.