LibreOffice Module setup_native (master) 1
restartindexingservice.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20/*
21 After installation of the OOo filter for the indexing service
22 it is necessary to restart the indexing service in order to
23 activate the filter. This is the most reliable way to get the
24 indexing service working. We only restart the service if it is
25 already running. If we have insufficient privileges to restart
26 the service we do nothing.
27*/
28
29#define WIN32_LEAN_AND_MEAN
30#include <windows.h>
31#include <msiquery.h>
32
33const wchar_t * const INDEXING_SERVICE_NAME = L"cisvc";
34
35static bool StopIndexingService(SC_HANDLE hService)
36{
37 SERVICE_STATUS status;
38
39 if (ControlService(hService, SERVICE_CONTROL_STOP, &status))
40 {
41 // Check the status until the service is no longer stop pending.
42 if (QueryServiceStatus(hService, &status))
43 {
44 DWORD startTime = GetTickCount();
45 DWORD oldCheckPoint = status.dwCheckPoint;
46
47 while (status.dwCurrentState == SERVICE_STOP_PENDING)
48 {
49 // Do not wait longer than the wait hint. A good interval is
50 // one tenth the wait hint, but no less than 1 second and no
51 // more than 10 seconds.
52 DWORD waitTime = status.dwWaitHint / 10;
53
54 if (waitTime < 1000)
55 waitTime = 1000;
56 else if (waitTime > 10000)
57 waitTime = 10000;
58
59 Sleep(waitTime);
60
61 // Check the status again.
62 if (!QueryServiceStatus(hService, &status) ||
63 (status.dwCurrentState == SERVICE_STOPPED))
64 break;
65
66 if (status.dwCheckPoint > oldCheckPoint)
67 {
68 startTime = GetTickCount();
69 oldCheckPoint = status.dwCheckPoint;
70 }
71 else if ((GetTickCount() - startTime) > status.dwWaitHint)
72 {
73 break; // service doesn't react anymore
74 }
75 }
76 }
77 }
78 return (status.dwCurrentState == SERVICE_STOPPED);
79}
80
81static void StartIndexingService(SC_HANDLE hService)
82{
83 if (StartServiceW(hService, 0, nullptr))
84 {
85 SERVICE_STATUS status;
86
87 // Check the status until the service is no longer stop pending.
88 if (QueryServiceStatus(hService, &status))
89 {
90 DWORD startTime = GetTickCount();
91 DWORD oldCheckPoint = status.dwCheckPoint;
92
93 while (status.dwCurrentState == SERVICE_START_PENDING)
94 {
95 // Do not wait longer than the wait hint. A good interval is
96 // one tenth the wait hint, but no less than 1 second and no
97 // more than 10 seconds.
98 DWORD waitTime = status.dwWaitHint / 10;
99
100 if (waitTime < 1000)
101 waitTime = 1000;
102 else if (waitTime > 10000)
103 waitTime = 10000;
104
105 Sleep(waitTime);
106
107 // Check the status again.
108 if (!QueryServiceStatus(hService, &status) ||
109 (status.dwCurrentState == SERVICE_STOPPED))
110 break;
111
112 if (status.dwCheckPoint > oldCheckPoint)
113 {
114 startTime = GetTickCount();
115 oldCheckPoint = status.dwCheckPoint;
116 }
117 else if ((GetTickCount() - startTime) > status.dwWaitHint)
118 {
119 // service doesn't react anymore
120 break;
121 }
122 }
123 }
124 }
125}
126
127extern "C" __declspec(dllexport) UINT __stdcall RestartIndexingService(MSIHANDLE)
128{
129 SC_HANDLE hSCManager = OpenSCManagerW(
130 nullptr, // local machine
131 nullptr, // ServicesActive database
132 SC_MANAGER_ALL_ACCESS);
133
134 if (hSCManager != nullptr)
135 {
136 SC_HANDLE hIndexingService = OpenServiceW(
137 hSCManager, INDEXING_SERVICE_NAME, SERVICE_QUERY_STATUS | SERVICE_START | SERVICE_STOP);
138
139 if (hIndexingService)
140 {
141 SERVICE_STATUS status;
142 ZeroMemory(&status, sizeof(status));
143
144 if (QueryServiceStatus(hIndexingService, &status) &&
145 (status.dwCurrentState == SERVICE_RUNNING))
146 {
147 if (StopIndexingService(hIndexingService))
148 StartIndexingService(hIndexingService);
149 }
150 CloseServiceHandle(hIndexingService);
151 }
152 CloseServiceHandle(hSCManager);
153 }
154 return ERROR_SUCCESS;
155}
156
157/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void StartIndexingService(SC_HANDLE hService)
const wchar_t *const INDEXING_SERVICE_NAME
static bool StopIndexingService(SC_HANDLE hService)
__declspec(dllexport) UINT __stdcall RestartIndexingService(MSIHANDLE)