storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/service.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2016 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"os/exec"
    23  	"syscall"
    24  )
    25  
    26  // Type of service signals currently supported.
    27  type serviceSignal int
    28  
    29  const (
    30  	serviceRestart       serviceSignal = iota // Restarts the server.
    31  	serviceStop                               // Stops the server.
    32  	serviceReloadDynamic                      // Reload dynamic config values.
    33  	// Add new service requests here.
    34  )
    35  
    36  // Global service signal channel.
    37  var globalServiceSignalCh chan serviceSignal
    38  
    39  // GlobalServiceDoneCh - Global service done channel.
    40  var GlobalServiceDoneCh <-chan struct{}
    41  
    42  // GlobalContext context that is canceled when server is requested to shut down.
    43  var GlobalContext context.Context
    44  
    45  // cancelGlobalContext can be used to indicate server shutdown.
    46  var cancelGlobalContext context.CancelFunc
    47  
    48  func initGlobalContext() {
    49  	GlobalContext, cancelGlobalContext = context.WithCancel(context.Background())
    50  	GlobalServiceDoneCh = GlobalContext.Done()
    51  	globalServiceSignalCh = make(chan serviceSignal)
    52  }
    53  
    54  // restartProcess starts a new process passing it the active fd's. It
    55  // doesn't fork, but starts a new process using the same environment and
    56  // arguments as when it was originally started. This allows for a newly
    57  // deployed binary to be started. It returns the pid of the newly started
    58  // process when successful.
    59  func restartProcess() error {
    60  	// Use the original binary location. This works with symlinks such that if
    61  	// the file it points to has been changed we will use the updated symlink.
    62  	argv0, err := exec.LookPath(os.Args[0])
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	// Invokes the execve system call.
    68  	// Re-uses the same pid. This preserves the pid over multiple server-respawns.
    69  	return syscall.Exec(argv0, os.Args, os.Environ())
    70  }