New creates a new Upgrader. Files are passed from the parent and may be empty. Only the first call to this function will succeed. May return ErrNotSupported.
(opts Options)
| 57 | // |
| 58 | // Only the first call to this function will succeed. May return ErrNotSupported. |
| 59 | func New(opts Options) (upg *Upgrader, err error) { |
| 60 | stdEnvMu.Lock() |
| 61 | defer stdEnvMu.Unlock() |
| 62 | |
| 63 | if !isSupportedOS() { |
| 64 | return nil, fmt.Errorf("%w", ErrNotSupported) |
| 65 | } |
| 66 | |
| 67 | if stdEnvUpgrader != nil { |
| 68 | return nil, errors.New("tableflip: only a single Upgrader allowed") |
| 69 | } |
| 70 | |
| 71 | upg, err = newUpgrader(stdEnv, opts) |
| 72 | // Store a reference to upg in a private global variable, to prevent |
| 73 | // it from being GC'ed and exitFd being closed prematurely. |
| 74 | stdEnvUpgrader = upg |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | func newUpgrader(env *env, opts Options) (*Upgrader, error) { |
| 79 | if initialWD == "" { |