📄 ntmain.c
字号:
} else {
errmsg = nt_strerror(GetLastError());
printf("StartService() failed : %s\n", errmsg);
LocalFree(errmsg);
}
return -1;
}
/** Stop the Tor service. Return 0 if the service is stopped or was not
* previously running. Return -1 on error. */
static int
nt_service_stop(SC_HANDLE hService)
{
/** Wait at most 10 seconds for the service to stop. */
#define MAX_SERVICE_WAIT_TIME 10
int wait_time;
char *errmsg = NULL;
nt_service_loadlibrary();
service_fns.QueryServiceStatus_fn(hService, &service_status);
if (service_status.dwCurrentState == SERVICE_STOPPED) {
printf("Service is already stopped\n");
return 0;
}
if (service_fns.ControlService_fn(hService, SERVICE_CONTROL_STOP,
&service_status)) {
wait_time = 0;
while (service_fns.QueryServiceStatus_fn(hService, &service_status) &&
(service_status.dwCurrentState != SERVICE_STOPPED) &&
(wait_time < MAX_SERVICE_WAIT_TIME)) {
Sleep(1000);
wait_time++;
}
if (service_status.dwCurrentState == SERVICE_STOPPED) {
printf("Service stopped successfully\n");
return 0;
} else if (wait_time == MAX_SERVICE_WAIT_TIME) {
printf("Service did not stop within %d seconds.\n", wait_time);
} else {
errmsg = nt_strerror(GetLastError());
printf("QueryServiceStatus() failed : %s\n",errmsg);
LocalFree(errmsg);
}
} else {
errmsg = nt_strerror(GetLastError());
printf("ControlService() failed : %s\n", errmsg);
LocalFree(errmsg);
}
return -1;
}
/** Build a formatted command line used for the NT service. Return a
* pointer to the formatted string on success, or NULL on failure. Set
* *<b>using_default_torrc</b> to true if we're going to use the default
* location to torrc, or 1 if an option was specified on the command line.
*/
static char *
nt_service_command_line(int *using_default_torrc)
{
TCHAR tor_exe[MAX_PATH+1];
char *command, *options=NULL;
smartlist_t *sl;
int i, cmdlen;
*using_default_torrc = 1;
/* Get the location of tor.exe */
if (0 == GetModuleFileName(NULL, tor_exe, MAX_PATH))
return NULL;
/* Get the service arguments */
sl = smartlist_create();
for (i = 1; i < backup_argc; ++i) {
if (!strcmp(backup_argv[i], "--options") ||
!strcmp(backup_argv[i], "-options")) {
while (++i < backup_argc) {
if (!strcmp(backup_argv[i], "-f"))
*using_default_torrc = 0;
smartlist_add(sl, backup_argv[i]);
}
}
}
if (smartlist_len(sl))
options = smartlist_join_strings(sl,"\" \"",0,NULL);
smartlist_free(sl);
/* Allocate a string for the NT service command line */
cmdlen = strlen(tor_exe) + (options?strlen(options):0) + 32;
command = tor_malloc(cmdlen);
/* Format the service command */
if (options) {
if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service \"%s\"",
tor_exe, options)<0) {
tor_free(command); /* sets command to NULL. */
}
} else { /* ! options */
if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service", tor_exe)<0) {
tor_free(command); /* sets command to NULL. */
}
}
tor_free(options);
return command;
}
/** Creates a Tor NT service, set to start on boot. The service will be
* started if installation succeeds. Returns 0 on success, or -1 on
* failure. */
static int
nt_service_install(int argc, char **argv)
{
/* Notes about developing NT services:
*
* 1. Don't count on your CWD. If an absolute path is not given, the
* fopen() function goes wrong.
* 2. The parameters given to the nt_service_body() function differ
* from those given to main() function.
*/
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
SERVICE_DESCRIPTION sdBuff;
char *command;
char *errmsg;
const char *user_acct = GENSRV_USERACCT;
const char *password = "";
int i;
OSVERSIONINFOEX info;
SID_NAME_USE sidUse;
DWORD sidLen = 0, domainLen = 0;
int is_win2k_or_worse = 0;
int using_default_torrc = 0;
nt_service_loadlibrary();
/* Open the service control manager so we can create a new service */
if ((hSCManager = nt_service_open_scm()) == NULL)
return -1;
/* Build the command line used for the service */
if ((command = nt_service_command_line(&using_default_torrc)) == NULL) {
printf("Unable to build service command line.\n");
service_fns.CloseServiceHandle_fn(hSCManager);
return -1;
}
for (i=1; i < argc; ++i) {
if (!strcmp(argv[i], "--user") && i+1<argc) {
user_acct = argv[i+1];
++i;
}
if (!strcmp(argv[i], "--password") && i+1<argc) {
password = argv[i+1];
++i;
}
}
/* Compute our version and see whether we're running win2k or earlier. */
memset(&info, 0, sizeof(info));
info.dwOSVersionInfoSize = sizeof(info);
if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
printf("Call to GetVersionEx failed.\n");
is_win2k_or_worse = 1;
} else {
if (info.dwMajorVersion < 5 ||
(info.dwMajorVersion == 5 && info.dwMinorVersion == 0))
is_win2k_or_worse = 1;
}
if (user_acct == GENSRV_USERACCT) {
if (is_win2k_or_worse) {
/* On Win2k, there is no LocalService account, so we actually need to
* fall back on NULL (the system account). */
printf("Running on Win2K or earlier, so the LocalService account "
"doesn't exist. Falling back to SYSTEM account.\n");
user_acct = NULL;
} else {
/* Genericity is apparently _so_ last year in Redmond, where some
* accounts are accounts that you can look up, and some accounts
* are magic and undetectable via the security subsystem. See
* http://msdn2.microsoft.com/en-us/library/ms684188.aspx
*/
printf("Running on a Post-Win2K OS, so we'll assume that the "
"LocalService account exists.\n");
}
} else if (0 && service_fns.LookupAccountNameA_fn(NULL, // On this system
user_acct,
NULL, &sidLen, // Don't care about the SID
NULL, &domainLen, // Don't care about the domain
&sidUse) == 0) {
/* XXXX For some reason, the above test segfaults. Fix that. */
printf("User \"%s\" doesn't seem to exist.\n", user_acct);
return -1;
} else {
printf("Will try to install service as user \"%s\".\n", user_acct);
}
/* XXXX This warning could be better about explaining how to resolve the
* situation. */
if (using_default_torrc)
printf("IMPORTANT NOTE:\n"
" The Tor service will run under the account \"%s\". This means\n"
" that Tor will look for its configuration file under that\n"
" account's Application Data directory, which is probably not\n"
" the same as yours.\n", user_acct?user_acct:"<local system>");
/* Create the Tor service, set to auto-start on boot */
if ((hService = service_fns.CreateServiceA_fn(hSCManager, GENSRV_SERVICENAME,
GENSRV_DISPLAYNAME,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
command, NULL, NULL, NULL,
user_acct, password)) == NULL) {
errmsg = nt_strerror(GetLastError());
printf("CreateService() failed : %s\n", errmsg);
service_fns.CloseServiceHandle_fn(hSCManager);
LocalFree(errmsg);
tor_free(command);
return -1;
}
printf("Done with CreateService.\n");
/* Set the service's description */
sdBuff.lpDescription = (char*)GENSRV_DESCRIPTION;
service_fns.ChangeServiceConfig2A_fn(hService, SERVICE_CONFIG_DESCRIPTION,
&sdBuff);
printf("Service installed successfully\n");
/* Start the service initially */
nt_service_start(hService);
service_fns.CloseServiceHandle_fn(hService);
service_fns.CloseServiceHandle_fn(hSCManager);
tor_free(command);
return 0;
}
/** Removes the Tor NT service. Returns 0 if the service was successfully
* removed, or -1 on error. */
static int
nt_service_remove(void)
{
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
char *errmsg;
nt_service_loadlibrary();
if ((hSCManager = nt_service_open_scm()) == NULL)
return -1;
if ((hService = nt_service_open(hSCManager)) == NULL) {
service_fns.CloseServiceHandle_fn(hSCManager);
return -1;
}
nt_service_stop(hService);
if (service_fns.DeleteService_fn(hService) == FALSE) {
errmsg = nt_strerror(GetLastError());
printf("DeleteService() failed : %s\n", errmsg);
LocalFree(errmsg);
service_fns.CloseServiceHandle_fn(hService);
service_fns.CloseServiceHandle_fn(hSCManager);
return -1;
}
service_fns.CloseServiceHandle_fn(hService);
service_fns.CloseServiceHandle_fn(hSCManager);
printf("Service removed successfully\n");
return 0;
}
/** Starts the Tor service. Returns 0 on success, or -1 on error. */
static int
nt_service_cmd_start(void)
{
SC_HANDLE hSCManager;
SC_HANDLE hService;
int start;
if ((hSCManager = nt_service_open_scm()) == NULL)
return -1;
if ((hService = nt_service_open(hSCManager)) == NULL) {
service_fns.CloseServiceHandle_fn(hSCManager);
return -1;
}
start = nt_service_start(hService);
service_fns.CloseServiceHandle_fn(hService);
service_fns.CloseServiceHandle_fn(hSCManager);
return start;
}
/** Stops the Tor service. Returns 0 on success, or -1 on error. */
static int
nt_service_cmd_stop(void)
{
SC_HANDLE hSCManager;
SC_HANDLE hService;
int stop;
if ((hSCManager = nt_service_open_scm()) == NULL)
return -1;
if ((hService = nt_service_open(hSCManager)) == NULL) {
service_fns.CloseServiceHandle_fn(hSCManager);
return -1;
}
stop = nt_service_stop(hService);
service_fns.CloseServiceHandle_fn(hService);
service_fns.CloseServiceHandle_fn(hSCManager);
return stop;
}
/** Given a Win32 error code, this attempts to make Windows
* return a human-readable error message. The char* returned
* is allocated by Windows, but should be freed with LocalFree()
* when finished with it. */
static char*
nt_strerror(uint32_t errnum)
{
char *msgbuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&msgbuf, 0, NULL);
return msgbuf;
}
int
nt_service_parse_options(int argc, char **argv, int *should_exit)
{
backup_argv = argv;
backup_argc = argc;
*should_exit = 0;
if ((argc >= 3) &&
(!strcmp(argv[1], "-service") || !strcmp(argv[1], "--service"))) {
nt_service_loadlibrary();
if (!strcmp(argv[2], "install"))
return nt_service_install(argc, argv);
if (!strcmp(argv[2], "remove"))
return nt_service_remove();
if (!strcmp(argv[2], "start"))
return nt_service_cmd_start();
if (!strcmp(argv[2], "stop"))
return nt_service_cmd_stop();
printf("Unrecognized service command '%s'\n", argv[2]);
*should_exit = 1;
return 1;
}
if (argc >= 2) {
if (!strcmp(argv[1], "-nt-service") || !strcmp(argv[1], "--nt-service")) {
nt_service_loadlibrary();
nt_service_main();
*should_exit = 1;
return 0;
}
// These values have been deprecated since 0.1.1.2-alpha; we've warned
// about them since 0.1.2.7-alpha.
if (!strcmp(argv[1], "-install") || !strcmp(argv[1], "--install")) {
nt_service_loadlibrary();
fprintf(stderr,
"The %s option is deprecated; use \"--service install\" instead.",
argv[1]);
*should_exit = 1;
return nt_service_install(argc, argv);
}
if (!strcmp(argv[1], "-remove") || !strcmp(argv[1], "--remove")) {
nt_service_loadlibrary();
fprintf(stderr,
"The %s option is deprecated; use \"--service remove\" instead.",
argv[1]);
*should_exit = 1;
return nt_service_remove();
}
}
*should_exit = 0;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -