net.exe: Add missing CloseServiceHandle().
[wine] / programs / net / net.c
1 /*
2  * Copyright 2007 Tim Schwartz
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <windows.h>
22
23 #define NET_START 0001
24 #define NET_STOP  0002
25
26 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
27 {
28     LPENUM_SERVICE_STATUS dependencies = NULL;
29     DWORD buffer_size = 0;
30     DWORD count = 0, counter;
31     BOOL result;
32     SC_HANDLE dependent_serviceHandle;
33     SERVICE_STATUS_PROCESS ssp;
34
35     result = EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
36
37     if(!result && (GetLastError() == ERROR_MORE_DATA))
38     {
39         dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
40         if(EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
41         {
42             for(counter = 0; counter < count; counter++)
43             {
44                 printf("Stopping dependent service: %s\n", dependencies[counter].lpDisplayName);
45                 dependent_serviceHandle = OpenService(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
46                 if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
47                 CloseServiceHandle(dependent_serviceHandle);
48                 if(!result) printf("Could not stop service %s\n", dependencies[counter].lpDisplayName);
49            }
50         }
51     }
52
53     if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
54     HeapFree(GetProcessHeap(), 0, dependencies);
55     return result;
56 }
57
58 static BOOL net_service(int operation, char *service_name)
59 {
60     SC_HANDLE SCManager, serviceHandle;
61     BOOL result = 0;
62     char service_display_name[4096];
63     DWORD buffer_size = sizeof(service_display_name);
64
65     SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
66     if(!SCManager)
67     {
68         printf("Couldn't get handle to SCManager.\n");
69         return FALSE;
70     }
71     serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
72     if(!serviceHandle)
73     {
74         printf("Couldn't get handle to service.\n");
75         CloseServiceHandle(SCManager);
76         return FALSE;
77     }
78
79
80     GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
81     if (!service_display_name[0]) strcpy(service_display_name, service_name);
82
83     switch(operation)
84     {
85     case NET_START:
86         printf("The %s service is starting.\n", service_display_name);
87         result = StartService(serviceHandle, 0, NULL);
88
89         printf("The %s service ", service_display_name);
90         if(!result) printf("failed to start.\n");
91         else printf("was started successfully.\n");
92         break;
93     case NET_STOP:
94         printf("The %s service is stopping.\n", service_display_name);
95         result = StopService(SCManager, serviceHandle);
96
97         printf("The %s service ", service_display_name);
98         if(!result) printf("failed to stop.\n");
99         else printf("was stopped successfully.\n");
100         break;
101     }
102
103     CloseServiceHandle(serviceHandle);
104     CloseServiceHandle(SCManager);
105     return result;
106 }
107
108 int main(int argc, char *argv[])
109 {
110
111     if (argc < 2)
112     {
113         printf("The syntax of this command is:\n\n");
114         printf("NET [ HELP | START | STOP ]\n");
115         return 1;
116     }
117
118     if(!strcasecmp(argv[1], "help"))
119     {
120         printf("The syntax of this command is:\n\n");
121         printf("NET HELP command\n    -or-\nNET command /HELP\n\n");
122         printf("   Commands available are:\n");
123         printf("   NET HELP    NET START    NET STOP\n");
124     }
125
126     if(!strcasecmp(argv[1], "start"))
127     {
128         if(argc < 3)
129         {
130             printf("Specify service name to start.\n");
131             return 1;
132         }
133
134         if(!net_service(NET_START, argv[2]))
135         {
136             return 1;
137         }
138         return 0;
139     }
140
141     if(!strcasecmp(argv[1], "stop"))
142     {
143         if(argc < 3)
144         {
145             printf("Specify service name to stop.\n");
146             return 1;
147         }
148
149         if(!net_service(NET_STOP, argv[2]))
150         {
151             return 1;
152         }
153         return 0;
154     }
155
156     return 0;
157 }