2022-04-01 20:28:51 +00:00
|
|
|
// Package execuser is used to run applications from a high privilege user (root on Unix,
|
|
|
|
|
// SYSTEM service on Windows) as the current login user.
|
|
|
|
|
package execuser
|
|
|
|
|
|
2024-12-05 15:44:16 +00:00
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2024-12-05 15:02:03 +00:00
|
|
|
|
2022-04-01 20:28:51 +00:00
|
|
|
type eopts struct {
|
|
|
|
|
env [][2]string
|
2023-02-23 17:48:40 +00:00
|
|
|
args [][2]string
|
2022-04-01 20:28:51 +00:00
|
|
|
stderrPath string //nolint:structcheck,unused
|
2024-12-05 15:44:16 +00:00
|
|
|
timeout time.Duration
|
2025-03-20 14:49:23 +00:00
|
|
|
user string
|
2022-04-01 20:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Option allows configuring the application.
|
|
|
|
|
type Option func(*eopts)
|
|
|
|
|
|
|
|
|
|
// WithEnv sets environment variables for the application.
|
|
|
|
|
func WithEnv(name, value string) Option {
|
|
|
|
|
return func(a *eopts) {
|
|
|
|
|
a.env = append(a.env, [2]string{name, value})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 17:48:40 +00:00
|
|
|
// WithArg sets command line arguments for the application.
|
|
|
|
|
func WithArg(name, value string) Option {
|
|
|
|
|
return func(a *eopts) {
|
|
|
|
|
a.args = append(a.args, [2]string{name, value})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 15:44:16 +00:00
|
|
|
// WithTimeout sets the timeout for the application. Currently only supported on Linux.
|
|
|
|
|
func WithTimeout(duration time.Duration) Option {
|
|
|
|
|
return func(a *eopts) {
|
|
|
|
|
a.timeout = duration
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 14:49:23 +00:00
|
|
|
// WithUser sets the user to run the application as. Currently only supported on MacOS.
|
|
|
|
|
func WithUser(user string) Option {
|
|
|
|
|
return func(a *eopts) {
|
|
|
|
|
a.user = user
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:28:51 +00:00
|
|
|
// Run runs an application as the current login user.
|
|
|
|
|
// It assumes the caller is running with high privileges (root on Unix, SYSTEM on Windows).
|
|
|
|
|
//
|
|
|
|
|
// It returns after starting the child process.
|
2024-10-31 19:24:42 +00:00
|
|
|
func Run(path string, opts ...Option) (lastLogs string, err error) {
|
2022-04-01 20:28:51 +00:00
|
|
|
var o eopts
|
|
|
|
|
for _, fn := range opts {
|
|
|
|
|
fn(&o)
|
|
|
|
|
}
|
|
|
|
|
return run(path, o)
|
|
|
|
|
}
|
2024-11-20 16:44:40 +00:00
|
|
|
|
|
|
|
|
// RunWithOutput runs an application as the current login user and returns its output.
|
|
|
|
|
// It assumes the caller is running with high privileges (root on UNIX).
|
|
|
|
|
//
|
|
|
|
|
// It blocks until the child process exits.
|
|
|
|
|
// Non ExitError errors return with a -1 exitCode.
|
|
|
|
|
func RunWithOutput(path string, opts ...Option) (output []byte, exitCode int, err error) {
|
|
|
|
|
var o eopts
|
|
|
|
|
for _, fn := range opts {
|
|
|
|
|
fn(&o)
|
|
|
|
|
}
|
|
|
|
|
return runWithOutput(path, o)
|
|
|
|
|
}
|