/domainjoin/libdomainjoin/src/djservicemgr.c

https://github.com/BeyondTrust/pbis-open · C · 223 lines · 151 code · 48 blank · 24 comment · 15 complexity · 2257d6b78cabf0069ffb3c3a67cc4c77 MD5 · raw file

  1. /*
  2. * Copyright © BeyondTrust Software 2004 - 2019
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * BEYONDTRUST MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING TERMS AS
  18. * WELL. IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT WITH
  19. * BEYONDTRUST, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE TERMS OF THAT
  20. * SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE APACHE LICENSE,
  21. * NOTWITHSTANDING THE ABOVE NOTICE. IF YOU HAVE QUESTIONS, OR WISH TO REQUEST
  22. * A COPY OF THE ALTERNATE LICENSING TERMS OFFERED BY BEYONDTRUST, PLEASE CONTACT
  23. * BEYONDTRUST AT beyondtrust.com/contact
  24. */
  25. #include <lwerror.h>
  26. #include <lwmem.h>
  27. #include <lwstr.h>
  28. #include <lwsm/lwsm.h>
  29. #define BAIL_ON_ERROR(dwError) if (dwError) { goto error; }
  30. DWORD
  31. DJGetServiceStatus(
  32. PCSTR pszServiceName,
  33. PBOOLEAN pbStarted
  34. )
  35. {
  36. DWORD dwError = 0;
  37. PWSTR pwszServiceName = NULL;
  38. LW_SERVICE_HANDLE hHandle = NULL;
  39. LW_SERVICE_STATUS status = {0};
  40. BOOLEAN bStarted = FALSE;
  41. dwError = LwMbsToWc16s(pszServiceName, &pwszServiceName);
  42. BAIL_ON_ERROR(dwError);
  43. dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle);
  44. BAIL_ON_ERROR(dwError);
  45. dwError = LwSmQueryServiceStatus(hHandle, &status);
  46. BAIL_ON_ERROR(dwError);
  47. switch (status.state)
  48. {
  49. case LW_SERVICE_STATE_RUNNING:
  50. bStarted = TRUE;
  51. break;
  52. default:
  53. break;
  54. }
  55. *pbStarted = bStarted;
  56. cleanup:
  57. LW_SAFE_FREE_MEMORY(pwszServiceName);
  58. if (hHandle)
  59. {
  60. LwSmReleaseServiceHandle(hHandle);
  61. }
  62. return dwError;
  63. error:
  64. goto cleanup;
  65. }
  66. DWORD
  67. DJStartService(
  68. PCSTR pszServiceName
  69. )
  70. {
  71. DWORD dwError = 0;
  72. PWSTR pwszServiceName = NULL;
  73. LW_SERVICE_HANDLE hHandle = NULL;
  74. LW_SERVICE_HANDLE hDepHandle = NULL;
  75. LW_SERVICE_STATUS status = {0};
  76. PWSTR* ppwszDependencies = NULL;
  77. PSTR pszTemp = NULL;
  78. size_t i = 0;
  79. dwError = LwMbsToWc16s(pszServiceName, &pwszServiceName);
  80. BAIL_ON_ERROR(dwError);
  81. dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle);
  82. BAIL_ON_ERROR(dwError);
  83. dwError = LwSmQueryServiceDependencyClosure(hHandle, &ppwszDependencies);
  84. BAIL_ON_ERROR(dwError);
  85. for (i = 0; ppwszDependencies[i]; i++)
  86. {
  87. dwError = LwSmAcquireServiceHandle(ppwszDependencies[i], &hDepHandle);
  88. BAIL_ON_ERROR(dwError);
  89. dwError = LwSmQueryServiceStatus(hDepHandle, &status);
  90. BAIL_ON_ERROR(dwError);
  91. if (status.state != LW_SERVICE_STATE_RUNNING)
  92. {
  93. dwError = LwSmStartService(hDepHandle);
  94. BAIL_ON_ERROR(dwError);
  95. }
  96. dwError = LwSmReleaseServiceHandle(hDepHandle);
  97. hDepHandle = NULL;
  98. BAIL_ON_ERROR(dwError);
  99. }
  100. dwError = LwSmStartService(hHandle);
  101. BAIL_ON_ERROR(dwError);
  102. cleanup:
  103. LW_SAFE_FREE_MEMORY(pwszServiceName);
  104. LW_SAFE_FREE_MEMORY(pszTemp);
  105. if (ppwszDependencies)
  106. {
  107. LwSmFreeServiceNameList(ppwszDependencies);
  108. }
  109. if (hHandle)
  110. {
  111. LwSmReleaseServiceHandle(hHandle);
  112. }
  113. if (hDepHandle)
  114. {
  115. LwSmReleaseServiceHandle(hDepHandle);
  116. }
  117. return dwError;
  118. error:
  119. goto cleanup;
  120. }
  121. DWORD
  122. DJStopService(
  123. PCSTR pszServiceName
  124. )
  125. {
  126. DWORD dwError = 0;
  127. PWSTR pwszServiceName = NULL;
  128. LW_SERVICE_HANDLE hHandle = NULL;
  129. LW_SERVICE_HANDLE hDepHandle = NULL;
  130. LW_SERVICE_STATUS status = {0};
  131. PWSTR* ppwszDependencies = NULL;
  132. PSTR pszTemp = NULL;
  133. size_t i = 0;
  134. dwError = LwMbsToWc16s(pszServiceName, &pwszServiceName);
  135. BAIL_ON_ERROR(dwError);
  136. dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle);
  137. BAIL_ON_ERROR(dwError);
  138. dwError = LwSmQueryServiceReverseDependencyClosure(hHandle, &ppwszDependencies);
  139. BAIL_ON_ERROR(dwError);
  140. for (i = 0; ppwszDependencies[i]; i++)
  141. {
  142. dwError = LwSmAcquireServiceHandle(ppwszDependencies[i], &hDepHandle);
  143. BAIL_ON_ERROR(dwError);
  144. dwError = LwSmQueryServiceStatus(hDepHandle, &status);
  145. BAIL_ON_ERROR(dwError);
  146. if (status.state != LW_SERVICE_STATE_STOPPED)
  147. {
  148. dwError = LwSmStopService(hDepHandle);
  149. BAIL_ON_ERROR(dwError);
  150. }
  151. dwError = LwSmReleaseServiceHandle(hDepHandle);
  152. hDepHandle = NULL;
  153. BAIL_ON_ERROR(dwError);
  154. }
  155. dwError = LwSmStopService(hHandle);
  156. BAIL_ON_ERROR(dwError);
  157. cleanup:
  158. LW_SAFE_FREE_MEMORY(pwszServiceName);
  159. LW_SAFE_FREE_MEMORY(pszTemp);
  160. if (ppwszDependencies)
  161. {
  162. LwSmFreeServiceNameList(ppwszDependencies);
  163. }
  164. if (hHandle)
  165. {
  166. LwSmReleaseServiceHandle(hHandle);
  167. }
  168. if (hDepHandle)
  169. {
  170. LwSmReleaseServiceHandle(hDepHandle);
  171. }
  172. return dwError;
  173. error:
  174. goto cleanup;
  175. }