-
-
Notifications
You must be signed in to change notification settings - Fork 508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix checkpid on windows #2031
Fix checkpid on windows #2031
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
_ "embed" | ||
"flag" | ||
|
@@ -473,7 +474,6 @@ func (s *serviceClient) updateStatus() error { | |
Stop: backoff.Stop, | ||
Clock: backoff.SystemClock, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
@@ -716,16 +716,44 @@ func openURL(url string) error { | |
|
||
// checkPIDFile exists and return error, or write new. | ||
func checkPIDFile() error { | ||
pidFile := path.Join(os.TempDir(), "wiretrustee-ui.pid") | ||
pidFile := path.Join(os.TempDir(), "netbird-ui.pid") | ||
if piddata, err := os.ReadFile(pidFile); err == nil { | ||
if pid, err := strconv.Atoi(string(piddata)); err == nil { | ||
if process, err := os.FindProcess(pid); err == nil { | ||
if err := process.Signal(syscall.Signal(0)); err == nil { | ||
return fmt.Errorf("process already exists: %d", pid) | ||
} | ||
if pidExists(pid) { | ||
return fmt.Errorf("process already exists: %d", pid) | ||
} | ||
} | ||
} | ||
|
||
return os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0o664) //nolint:gosec | ||
} | ||
|
||
func pidExists(pid int) bool { | ||
if runtime.GOOS != "windows" { | ||
if process, err := os.FindProcess(pid); err == nil { | ||
if err := process.Signal(syscall.Signal(0)); err == nil { | ||
return true | ||
} | ||
} | ||
} | ||
// Prepare the command | ||
cmd := exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid)) | ||
|
||
// Get the output | ||
output, err := cmd.Output() | ||
if err != nil { | ||
log.Tracef("Failed to list tasks: %v, output: %s", err, output) | ||
return false | ||
hurricanehrndz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Parse the output | ||
scanner := bufio.NewScanner(strings.NewReader(string(output))) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.Contains(line, fmt.Sprintf("%d", pid)) { | ||
return true | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If scanner.Err actually returns an error the overall return of the function would be false, the only benefit to handling scanner.Err is to log an issue with the scanning, but it is not like the log is written to disk or anything so ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but if there's an issue that would at least give us a hint when debugging the application from CLI |
||
|
||
return false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use a function similar to GetSystem32Command to avoid path issues?