OLD | NEW |
| (Empty) |
1 // Copyright 2007-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Service-related utilities. | |
17 | |
18 #ifndef OMAHA_BASE_SERVICE_UTILS_H__ | |
19 #define OMAHA_BASE_SERVICE_UTILS_H__ | |
20 | |
21 #include <windows.h> | |
22 #include <atlstr.h> | |
23 #include "base/basictypes.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 // Utility functions for working with the SCM database. | |
28 class ScmDatabase { | |
29 public: | |
30 // Callback function type for EnumerateServices. This gets called for each | |
31 // service in the registry. | |
32 // | |
33 // @param callback_context Passed unchanged from the caller of | |
34 // EnumerateServices to the callback function. | |
35 // @param service_name The name of the service (not the display name but | |
36 // rather the canonical name, to be used with e.g. ::OpenService()). Note | |
37 // that because this function is based on enumerating the registry, it's | |
38 // possible that services that were recently deleted will show up in the | |
39 // enumeration; therefore, it should not be considered an error if you try | |
40 // to ::OpenService() on this name and it fails with a last error of | |
41 // ERROR_SERVICE_DOES_NOT_EXIST or possibly ERROR_INVALID_NAME (if | |
42 // somebody messed up the registry by hand). | |
43 // | |
44 // @return S_OK to continue enumeration, S_FALSE or a COM error code to | |
45 // stop enumeration. The return value will be propagated to the caller | |
46 // of Enumerate. | |
47 // | |
48 // @note The initial version of this function used EnumServicesStatusEx | |
49 // but it turns out the function is a fair bit flaky, not returning all | |
50 // recently created (e.g. created but never started) services. | |
51 typedef HRESULT(*EnumerateServicesCallback)(void* callback_context, | |
52 const wchar_t* service_name); | |
53 | |
54 // Calls 'callback' for each of the services in the registry. | |
55 // | |
56 // @param callback Callback function to call | |
57 // @param callback_context Passed unchanged to your callback function | |
58 // | |
59 // @return S_OK or a COM error code. | |
60 static HRESULT EnumerateServices(EnumerateServicesCallback callback, | |
61 void* callback_context); | |
62 | |
63 // Returns true iff the service passed in is in the indicated state. | |
64 // | |
65 // @param service An open handle to a service. The handle must have at | |
66 // least SERVICE_QUERY_CONFIG rights. | |
67 // @param state One of the SERVICE_XXX constants indicating the state of a | |
68 // service (e.g. SERVICE_DISABLED). | |
69 // | |
70 // @return True iff 'service' is in state 'state'. | |
71 static bool IsServiceStateEqual(SC_HANDLE service, DWORD state); | |
72 | |
73 // Returns true iff the service passed in has been marked deleted. | |
74 // | |
75 // @param service An open handle to a service. The handle must have at | |
76 // least SERVICE_QUERY_CONFIG and SERVICE_CHANGE_CONFIG rights. | |
77 // | |
78 // @return True iff 'service' has been marked deleted. | |
79 static bool IsServiceMarkedDeleted(SC_HANDLE service); | |
80 | |
81 private: | |
82 DISALLOW_EVIL_CONSTRUCTORS(ScmDatabase); | |
83 }; | |
84 | |
85 // Utility functions for the service's installation, overinstall etc. | |
86 class ServiceInstall { | |
87 public: | |
88 | |
89 // Generates a versioned service name based on the current system time. | |
90 static CString GenerateServiceName(const TCHAR* service_prefix); | |
91 | |
92 // Uninstalls all versions of the service other than the one that matches | |
93 // the service name passed in. Pass in NULL to uninstall everything. | |
94 static HRESULT UninstallServices(const TCHAR* service_prefix, | |
95 const TCHAR* exclude_service); | |
96 | |
97 static bool IsServiceInstalled(const TCHAR* service_name); | |
98 | |
99 // @return True if the current service can be installed without rebooting, | |
100 // false if a reboot is required before it can be installed. The cases | |
101 // where the current service can be installed without rebooting are: | |
102 // a) when no service exists with the current name | |
103 // b) when there is an existing service with the current name but it is | |
104 // not marked for deletion | |
105 static bool CanInstallWithoutReboot(); | |
106 | |
107 // Given a service name, stops it if it is already running. | |
108 static HRESULT StopService(const CString& service_name); | |
109 | |
110 protected: | |
111 // Context passed to the UninstallIfNotCurrent function; this is made a | |
112 // parameter so we can unit test the function without mucking with the | |
113 // "actual" services. | |
114 struct UninstallByPrefixParams { | |
115 CString prefix; // prefix of services we want to uninstall | |
116 CString unless_matches; // name of current service, to not touch | |
117 }; | |
118 | |
119 // Uninstalls a given service if it matches a given prefix but does not match | |
120 // a given full service name. | |
121 // | |
122 // This is an ScmDatabase::EnumerateServicesCallback function. | |
123 // | |
124 // @param context Pointer to an UninstallByPrefix structure. | |
125 static HRESULT UninstallByPrefix(void* context, const wchar_t* service_name); | |
126 | |
127 private: | |
128 DISALLOW_IMPLICIT_CONSTRUCTORS(ServiceInstall); | |
129 }; | |
130 | |
131 // Service utility functions for querying current state, and eventually more. | |
132 class ServiceUtils { | |
133 public: | |
134 static bool IsServiceRunning(const TCHAR* service_name); | |
135 static bool IsServiceDisabled(const TCHAR* service_name); | |
136 | |
137 private: | |
138 DISALLOW_IMPLICIT_CONSTRUCTORS(ServiceUtils); | |
139 }; | |
140 | |
141 } // namespace omaha | |
142 | |
143 #endif // OMAHA_BASE_SERVICE_UTILS_H__ | |
OLD | NEW |