gitee.com/quant1x/gox@v1.21.2/daemon/helper_windows.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by
     3  // license that can be found in the LICENSE file.
     4  
     5  package daemon
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"gitee.com/quant1x/gox/util/homedir"
    11  	"golang.org/x/sys/windows/registry"
    12  )
    13  
    14  // SystemError contains error description and corresponded action helper to fix it
    15  type SystemError struct {
    16  	Title       string
    17  	Description string
    18  	Action      string
    19  }
    20  
    21  var (
    22  	// WinErrCode - List of system errors from Microsoft source:
    23  	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms681385(v=vs.85).aspx
    24  	WinErrCode = map[int]SystemError{
    25  		5: SystemError{
    26  			Title:       "ERROR_ACCESS_DENIED",
    27  			Description: "Access denied.",
    28  			Action:      "Administrator access is needed to install a service.",
    29  		},
    30  		1051: SystemError{
    31  			Title:       "ERROR_DEPENDENT_SERVICES_RUNNING",
    32  			Description: "A stop control has been sent to a service that other running services are dependent on.",
    33  		},
    34  		1052: SystemError{
    35  			Title:       "ERROR_INVALID_SERVICE_CONTROL",
    36  			Description: "The requested control is not valid for this service.",
    37  		},
    38  		1053: SystemError{
    39  			Title:       "ERROR_SERVICE_REQUEST_TIMEOUT",
    40  			Description: "The service did not respond to the start or control request in a timely fashion.",
    41  		},
    42  		1054: SystemError{
    43  			Title:       "ERROR_SERVICE_NO_THREAD",
    44  			Description: "A thread could not be created for the service.",
    45  		},
    46  		1055: SystemError{
    47  			Title:       "ERROR_SERVICE_DATABASE_LOCKED",
    48  			Description: "The service database is locked.",
    49  		},
    50  		1056: SystemError{
    51  			Title:       "ERROR_SERVICE_ALREADY_RUNNING",
    52  			Description: "An instance of the service is already running.",
    53  		},
    54  		1057: SystemError{
    55  			Title:       "ERROR_INVALID_SERVICE_ACCOUNT",
    56  			Description: "The account name is invalid or does not exist, or the password is invalid for the account name specified.",
    57  		},
    58  		1058: SystemError{
    59  			Title:       "ERROR_SERVICE_DISABLED",
    60  			Description: "The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.",
    61  		},
    62  		1060: SystemError{
    63  			Title:       "ERROR_SERVICE_DOES_NOT_EXIST",
    64  			Description: "The specified service does not exist as an installed service.",
    65  		},
    66  		1061: SystemError{
    67  			Title:       "ERROR_SERVICE_CANNOT_ACCEPT_CTRL",
    68  			Description: "The service cannot accept control messages at this time.",
    69  		},
    70  		1062: SystemError{
    71  			Title:       "ERROR_SERVICE_NOT_ACTIVE",
    72  			Description: "The service has not been started.",
    73  		},
    74  		1063: SystemError{
    75  			Title:       "ERROR_FAILED_SERVICE_CONTROLLER_CONNECT",
    76  			Description: "The service process could not connect to the service controller.",
    77  		},
    78  		1064: SystemError{
    79  			Title:       "ERROR_EXCEPTION_IN_SERVICE",
    80  			Description: "An exception occurred in the service when handling the control request.",
    81  		},
    82  		1066: SystemError{
    83  			Title:       "ERROR_SERVICE_SPECIFIC_ERROR",
    84  			Description: "The service has returned a service-specific error code.",
    85  		},
    86  		1068: SystemError{
    87  			Title:       "ERROR_SERVICE_DEPENDENCY_FAIL",
    88  			Description: "The dependency service or group failed to start.",
    89  		},
    90  		1069: SystemError{
    91  			Title:       "ERROR_SERVICE_LOGON_FAILED",
    92  			Description: "The service did not start due to a logon failure.",
    93  		},
    94  		1070: SystemError{
    95  			Title:       "ERROR_SERVICE_START_HANG",
    96  			Description: "After starting, the service hung in a start-pending state.",
    97  		},
    98  		1071: SystemError{
    99  			Title:       "ERROR_INVALID_SERVICE_LOCK",
   100  			Description: "The specified service database lock is invalid.",
   101  		},
   102  		1072: SystemError{
   103  			Title:       "ERROR_SERVICE_MARKED_FOR_DELETE",
   104  			Description: "The specified service has been marked for deletion.",
   105  		},
   106  		1073: SystemError{
   107  			Title:       "ERROR_SERVICE_EXISTS",
   108  			Description: "The specified service already exists.",
   109  		},
   110  		1075: SystemError{
   111  			Title:       "ERROR_SERVICE_DEPENDENCY_DELETED",
   112  			Description: "The dependency service does not exist or has been marked for deletion.",
   113  		},
   114  		1077: SystemError{
   115  			Title:       "ERROR_SERVICE_NEVER_STARTED",
   116  			Description: "No attempts to start the service have been made since the last boot.",
   117  		},
   118  		1078: SystemError{
   119  			Title:       "ERROR_DUPLICATE_SERVICE_NAME",
   120  			Description: "The name is already in use as either a service name or a service display name.",
   121  		},
   122  		1079: SystemError{
   123  			Title:       "ERROR_DIFFERENT_SERVICE_ACCOUNT",
   124  			Description: "The account specified for this service is different from the account specified for other services running in the same process.",
   125  		},
   126  		1083: SystemError{
   127  			Title:       "ERROR_SERVICE_NOT_IN_EXE",
   128  			Description: "The executable program that this service is configured to run in does not implement the service.",
   129  		},
   130  		1084: SystemError{
   131  			Title:       "ERROR_NOT_SAFEBOOT_SERVICE",
   132  			Description: "This service cannot be started in Safe Mode.",
   133  		},
   134  	}
   135  )
   136  
   137  func initWindows() {
   138  	fmt.Printf("check Windows Environment...")
   139  	environment := `SYSTEM\ControlSet001\Control\Session Manager\Environment`
   140  	key, err := registry.OpenKey(registry.LOCAL_MACHINE, environment, registry.ALL_ACCESS)
   141  	if err != nil {
   142  		fmt.Println(err)
   143  		return
   144  	}
   145  	homePath, _, err := key.GetStringValue(homedir.EnvGoxHome)
   146  	if err == nil {
   147  		fmt.Println(homedir.EnvGoxHome, "=", homePath)
   148  		return
   149  	}
   150  	homePath, _ = homedir.Dir()
   151  	fmt.Printf("set Windows Environment...%s=%s, ", homedir.EnvGoxHome, homePath)
   152  	err = key.SetStringValue(homedir.EnvGoxHome, homePath)
   153  	if err != nil {
   154  		fmt.Println(err)
   155  	}
   156  	fmt.Println("SUCCESS")
   157  }