github.com/iDigitalFlame/xmt@v0.5.4/cmd/zombie_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package cmd
    21  
    22  import (
    23  	"context"
    24  	"sync/atomic"
    25  )
    26  
    27  func (z *Zombie) wait() {
    28  	z.t.callback = z.callback
    29  	z.t.wait(z.x.i.ProcessID, z.x.i.ThreadID)
    30  }
    31  func (z *Zombie) callback() {
    32  	z.stopWith(z.t.exit, z.t.err)
    33  }
    34  
    35  // Start will attempt to start the Zombie and will return an errors that occur
    36  // while starting the Process.
    37  //
    38  // This function will return 'ErrEmptyCommand' if the 'Args' or 'Data'
    39  // parameters are empty and 'ErrAlreadyStarted' if attempting to
    40  // start a Zombie that already has been started previously.
    41  //
    42  // Always returns 'ErrNoWindows' on non-Windows devices.
    43  func (z *Zombie) Start() error {
    44  	if z.t.Running() {
    45  		return ErrAlreadyStarted
    46  	}
    47  	if len(z.Args) == 0 || len(z.Data) == 0 {
    48  		return ErrEmptyCommand
    49  	}
    50  	if z.ctx == nil {
    51  		z.ctx = context.Background()
    52  	}
    53  	z.ch, z.cancel = make(chan struct{}), func() {}
    54  	atomic.StoreUint32(&z.cookie, 0)
    55  	if err := z.x.start(z.ctx, &z.Process, true); err != nil {
    56  		return z.stopWith(exitStopped, err)
    57  	}
    58  	if err := z.t.Start(z.x.i.Process, z.Timeout, 0, z.Data); err != nil {
    59  		return z.stopWith(exitStopped, z.t.stopWith(exitStopped, err))
    60  	}
    61  	go z.wait()
    62  	return nil
    63  }