1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS net command
4  * FILE:            base/applications/network/net/cmdStart.c
5  * PURPOSE:
6  *
7  * PROGRAMMERS:     Magnus Olsen (greatlord@reactos.org)
8  */
9 
10 #include "net.h"
11 
12 /* Enumerate all running services */
13 static
14 INT
15 EnumerateRunningServices(VOID)
16 {
17     SC_HANDLE hManager = NULL;
18     SC_HANDLE hService = NULL;
19     DWORD dwBufferSize = 0;
20     DWORD dwServiceCount;
21     DWORD dwResumeHandle = 0;
22     LPENUM_SERVICE_STATUSW lpServiceBuffer = NULL;
23     INT i;
24     INT nError = 0;
25     DWORD dwError = ERROR_SUCCESS;
26 
27     hManager = OpenSCManagerW(NULL,
28                               SERVICES_ACTIVE_DATABASE,
29                               SC_MANAGER_ENUMERATE_SERVICE);
30     if (hManager == NULL)
31     {
32         dwError = GetLastError();
33         nError = 1;
34         goto done;
35     }
36 
37     EnumServicesStatusW(hManager,
38                         SERVICE_WIN32,
39                         SERVICE_ACTIVE,
40                         NULL,
41                         0,
42                         &dwBufferSize,
43                         &dwServiceCount,
44                         &dwResumeHandle);
45 
46     if (dwBufferSize != 0)
47     {
48         lpServiceBuffer = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
49         if (lpServiceBuffer != NULL)
50         {
51             if (EnumServicesStatusW(hManager,
52                                     SERVICE_WIN32,
53                                     SERVICE_ACTIVE,
54                                     lpServiceBuffer,
55                                     dwBufferSize,
56                                     &dwBufferSize,
57                                     &dwServiceCount,
58                                     &dwResumeHandle))
59             {
60                 ConPuts(StdOut, L"The following services have been started:\n\n");
61 
62                 for (i = 0; i < dwServiceCount; i++)
63                 {
64                     ConPrintf(StdOut, L"  %s\n", lpServiceBuffer[i].lpDisplayName);
65                 }
66             }
67 
68             HeapFree(GetProcessHeap(), 0, lpServiceBuffer);
69         }
70     }
71 
72 done:
73     if (hService != NULL)
74         CloseServiceHandle(hService);
75 
76     if (hManager != NULL)
77         CloseServiceHandle(hManager);
78 
79      if (dwError != ERROR_SUCCESS)
80     {
81         /* FIXME: Print proper error message */
82         ConPrintf(StdErr, L"Error: %lu\n", dwError);
83     }
84 
85     return nError;
86 }
87 
88 /* Start the service argv[2] */
89 static
90 INT
91 StartOneService(INT argc, WCHAR **argv)
92 {
93     SC_HANDLE hManager = NULL;
94     SC_HANDLE hService = NULL;
95     LPCWSTR *lpArgVectors = NULL;
96     DWORD dwError = ERROR_SUCCESS;
97     INT nError = 0;
98     INT i;
99 
100     hManager = OpenSCManagerW(NULL,
101                               SERVICES_ACTIVE_DATABASE,
102                               SC_MANAGER_ENUMERATE_SERVICE);
103     if (hManager == NULL)
104     {
105         dwError = GetLastError();
106         nError = 1;
107         goto done;
108     }
109 
110     hService = OpenServiceW(hManager,
111                             argv[2],
112                             SERVICE_START);
113     if (hService == NULL)
114     {
115         dwError = GetLastError();
116         nError = 1;
117         goto done;
118     }
119 
120     lpArgVectors = HeapAlloc(GetProcessHeap(),
121                              0,
122                              (argc - 2) * sizeof(LPCWSTR));
123     if (lpArgVectors == NULL)
124     {
125         dwError = GetLastError();
126         nError = 1;
127         goto done;
128     }
129 
130     for (i = 2; i < argc; i++)
131     {
132         lpArgVectors[i - 2] = argv[i];
133     }
134 
135     if (!StartServiceW(hService,
136                        (DWORD)argc - 2,
137                        lpArgVectors))
138     {
139         dwError = GetLastError();
140         nError = 1;
141     }
142 
143 done:
144     if (lpArgVectors != NULL)
145         HeapFree(GetProcessHeap(), 0, (LPVOID)lpArgVectors);
146 
147     if (hService != NULL)
148         CloseServiceHandle(hService);
149 
150     if (hManager != NULL)
151         CloseServiceHandle(hManager);
152 
153     if (dwError != ERROR_SUCCESS)
154     {
155         /* FIXME: Print proper error message */
156         ConPrintf(StdErr, L"Error: %lu\n", dwError);
157     }
158 
159     return nError;
160 }
161 
162 INT
163 cmdStart(INT argc, WCHAR **argv)
164 {
165     INT i;
166 
167     if (argc == 2)
168     {
169         return EnumerateRunningServices();
170     }
171 
172     for (i = 2; i < argc; i++)
173     {
174         if (_wcsicmp(argv[i], L"/help") == 0)
175         {
176             PrintMessageString(4381);
177             ConPuts(StdOut, L"\n");
178             PrintNetMessage(MSG_START_SYNTAX);
179             PrintNetMessage(MSG_START_HELP);
180             return 1;
181         }
182     }
183 
184     return StartOneService(argc, argv);
185 }
186