Vidalia 0.3.1
TorService.h
Go to the documentation of this file.
1/*
2** This file is part of Vidalia, and is subject to the license terms in the
3** LICENSE file, found in the top level directory of this distribution. If
4** you did not receive the LICENSE file with this file, you may obtain it
5** from the Vidalia source package distributed by the Vidalia Project at
6** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7** including this file, may be copied, modified, propagated, or distributed
8** except according to the terms described in the LICENSE file.
9*/
10
11/*
12** \file torservice.h
13** \brief Starts, stops, installs, and uninstalls a Tor service (Win32).
14*/
15
16#ifndef _TORSERVICE_H
17#define _TORSERVICE_H
18
19#include <QObject>
20#include <QProcess>
21
22#include <windows.h>
23#define TOR_SERVICE_NAME "tor"
24#define TOR_SERVICE_DISP "Tor Win32 Service"
25#define TOR_SERVICE_DESC \
26 TEXT("Provides an anonymous Internet communication system.")
27#define TOR_SERVICE_ACCESS SERVICE_ALL_ACCESS
28#define SERVICE_ERROR 8
29
30/* NT service function prototypes. This code is adapted from Tor's
31 * nt_service_load_library() in main.c. See LICENSE for details on
32 * Tor's license. */
33typedef BOOL (WINAPI *ChangeServiceConfig2A_fn)(
34 SC_HANDLE hService,
35 DWORD dwInfoLevel,
36 LPVOID lpInfo);
37typedef BOOL (WINAPI *CloseServiceHandle_fn)(
38 SC_HANDLE hSCObject);
39typedef BOOL (WINAPI *ControlService_fn)(
40 SC_HANDLE hService,
41 DWORD dwControl,
42 LPSERVICE_STATUS lpServiceStatus);
43typedef SC_HANDLE (WINAPI *CreateServiceA_fn)(
44 SC_HANDLE hSCManager,
45 LPCTSTR lpServiceName,
46 LPCTSTR lpDisplayName,
47 DWORD dwDesiredAccess,
48 DWORD dwServiceType,
49 DWORD dwStartType,
50 DWORD dwErrorControl,
51 LPCTSTR lpBinaryPathName,
52 LPCTSTR lpLoadOrderGroup,
53 LPDWORD lpdwTagId,
54 LPCTSTR lpDependencies,
55 LPCTSTR lpServiceStartName,
56 LPCTSTR lpPassword);
57typedef BOOL (WINAPI *DeleteService_fn)(
58 SC_HANDLE hService);
59typedef SC_HANDLE (WINAPI *OpenSCManagerA_fn)(
60 LPCTSTR lpMachineName,
61 LPCTSTR lpDatabaseName,
62 DWORD dwDesiredAccess);
63typedef SC_HANDLE (WINAPI *OpenServiceA_fn)(
64 SC_HANDLE hSCManager,
65 LPCTSTR lpServiceName,
66 DWORD dwDesiredAccess);
67typedef BOOL (WINAPI *QueryServiceStatus_fn)(
68 SC_HANDLE hService,
69 LPSERVICE_STATUS lpServiceStatus);
70typedef BOOL (WINAPI *SetServiceStatus_fn)(SERVICE_STATUS_HANDLE,
71 LPSERVICE_STATUS);
72typedef BOOL (WINAPI *StartServiceA_fn)(
73 SC_HANDLE hService,
74 DWORD dwNumServiceArgs,
75 LPCTSTR* lpServiceArgVectors);
76
77/** Table of NT service related functions. */
79 bool loaded;
90};
91
92
93class TorService : public QObject
94{
95 Q_OBJECT
96
97public:
98 /** Returns if services are supported. */
99 static bool isSupported();
100 /** Dynamically loads NT service related functions from advapi32.dll. */
101 static bool loadServiceFunctions();
102
103 /** Default ctor. */
104 TorService(QObject* parent = 0);
105 /** Default dtor. */
106 ~TorService();
107
108 /** Returns true if the Tor service is installed. */
109 bool isInstalled();
110 /** Returns true if the Tor service is running. */
111 bool isRunning();
112 /** Starts the Tor service. Emits started on success. */
113 void start();
114 /** Stops the Tor service. Emits finished on success. */
115 bool stop();
116 /** Returns the exit code of the last Tor service that finished. */
117 int exitCode();
118 /** Returns the exit status of the last Tor service that finished. */
119 QProcess::ExitStatus exitStatus();
120 /** Installs the Tor service. */
121 bool install(const QString &torPath, const QString &torrc,
122 quint16 controlPort);
123 /** Removes the Tor service. */
124 bool remove();
125
126signals:
127 /** Called when the service gets started. */
128 void started();
129 /** Called when the service gets stopped. */
130 void finished(int exitCode, QProcess::ExitStatus);
131 /** Called when there is an error in starting the service. */
132 void startFailed(QString error);
133
134private:
135 /** Opens a handle to the Tor service. Returns NULL on error. */
136 SC_HANDLE openService();
137 /** Opens a handle to the service control manager. Returns NULL on error. */
138 static SC_HANDLE openSCM();
139 /** Closes the service <b>handle</b>. */
140 static void closeHandle(SC_HANDLE handle);
141 /** Gets the status of the Tor service. */
142 DWORD status();
143
144 /** Handle to the service control manager. */
145 SC_HANDLE _scm;
146 /** List of dynamically loaded NT service functions. */
148};
149
150#endif
151
SC_HANDLE(WINAPI * OpenServiceA_fn)(SC_HANDLE hSCManager, LPCTSTR lpServiceName, DWORD dwDesiredAccess)
Definition: TorService.h:63
SC_HANDLE(WINAPI * CreateServiceA_fn)(SC_HANDLE hSCManager, LPCTSTR lpServiceName, LPCTSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCTSTR lpBinaryPathName, LPCTSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR lpDependencies, LPCTSTR lpServiceStartName, LPCTSTR lpPassword)
Definition: TorService.h:43
BOOL(WINAPI * ChangeServiceConfig2A_fn)(SC_HANDLE hService, DWORD dwInfoLevel, LPVOID lpInfo)
Definition: TorService.h:33
BOOL(WINAPI * DeleteService_fn)(SC_HANDLE hService)
Definition: TorService.h:57
BOOL(WINAPI * StartServiceA_fn)(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCTSTR *lpServiceArgVectors)
Definition: TorService.h:72
SC_HANDLE(WINAPI * OpenSCManagerA_fn)(LPCTSTR lpMachineName, LPCTSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: TorService.h:59
BOOL(WINAPI * CloseServiceHandle_fn)(SC_HANDLE hSCObject)
Definition: TorService.h:37
BOOL(WINAPI * QueryServiceStatus_fn)(SC_HANDLE hService, LPSERVICE_STATUS lpServiceStatus)
Definition: TorService.h:67
BOOL(WINAPI * ControlService_fn)(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: TorService.h:39
BOOL(WINAPI * SetServiceStatus_fn)(SERVICE_STATUS_HANDLE, LPSERVICE_STATUS)
Definition: TorService.h:70
SC_HANDLE openService()
Definition: TorService.cpp:88
void finished(int exitCode, QProcess::ExitStatus)
bool stop()
Definition: TorService.cpp:168
bool isRunning()
Definition: TorService.cpp:130
bool remove()
Definition: TorService.cpp:280
int exitCode()
Definition: TorService.cpp:203
SC_HANDLE _scm
Definition: TorService.h:145
static ServiceFunctions _service_fns
Definition: TorService.h:147
static void closeHandle(SC_HANDLE handle)
Definition: TorService.cpp:110
TorService(QObject *parent=0)
Definition: TorService.cpp:34
DWORD status()
Definition: TorService.cpp:300
bool isInstalled()
Definition: TorService.cpp:119
QProcess::ExitStatus exitStatus()
Definition: TorService.cpp:225
void started()
static SC_HANDLE openSCM()
Definition: TorService.cpp:101
void startFailed(QString error)
void start()
Definition: TorService.cpp:137
bool install(const QString &torPath, const QString &torrc, quint16 controlPort)
Definition: TorService.cpp:239
static bool isSupported()
Definition: TorService.cpp:48
static bool loadServiceFunctions()
Definition: TorService.cpp:57
DebugMessage error(const QString &fmt)
Definition: tcglobal.cpp:40
SetServiceStatus_fn SetServiceStatus
Definition: TorService.h:88
OpenSCManagerA_fn OpenSCManagerA
Definition: TorService.h:85
StartServiceA_fn StartServiceA
Definition: TorService.h:89
QueryServiceStatus_fn QueryServiceStatus
Definition: TorService.h:87
CloseServiceHandle_fn CloseServiceHandle
Definition: TorService.h:81
OpenServiceA_fn OpenServiceA
Definition: TorService.h:86
ControlService_fn ControlService
Definition: TorService.h:82
ChangeServiceConfig2A_fn ChangeServiceConfig2A
Definition: TorService.h:80
CreateServiceA_fn CreateServiceA
Definition: TorService.h:83
DeleteService_fn DeleteService
Definition: TorService.h:84