net.exe: Add NET STOP.
[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                 if(!result) printf("Could not stop service %s\n", dependencies[counter].lpDisplayName);
48            }
49         }
50     }
51
52     if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
53     HeapFree(GetProcessHeap(), 0, dependencies);
54     return result;
55 }
56
57 static BOOL net_service(int operation, char *service_name)
58 {
59     SC_HANDLE SCManager, serviceHandle;
60     BOOL result = 0;
61     char service_display_name[4096];
62     DWORD buffer_size = sizeof(service_display_name);
63
64     SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
65     if(!SCManager)
66     {
67         printf("Couldn't get handle to SCManager.\n");
68         return FALSE;
69     }
70     serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
71     if(!serviceHandle)
72     {
73         printf("Couldn't get handle to service.\n");
74         CloseServiceHandle(SCManager);
75         return FALSE;
76     }
77
78
79     GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
80     if (!service_display_name[0]) strcpy(service_display_name, service_name);
81
82     switch(operation)
83     {
84     case NET_START:
85         printf("The %s service is starting.\n", service_display_name);
86         result = StartService(serviceHandle, 0, NULL);
87
88         printf("The %s service ", service_display_name);
89         if(!result) printf("failed to start.\n");
90         else printf("was started successfully.\n");
91         break;
92     case NET_STOP:
93         printf("The %s service is stopping.\n", service_display_name);
94         result = StopService(SCManager, serviceHandle);
95
96         printf("The %s service ", service_display_name);
97         if(!result) printf("failed to stop.\n");
98         else printf("was stopped successfully.\n");
99         break;
100     }
101
102     CloseServiceHandle(serviceHandle);
103     CloseServiceHandle(SCManager);
104     return result;
105 }
106
107 int main(int argc, char *argv[])
108 {
109
110     if (argc < 2)
111     {
112         printf("The syntax of this command is:\n\n");
113         printf("NET [ HELP | START | STOP ]\n");
114         return 1;
115     }
116
117     if(!strcasecmp(argv[1], "help"))
118     {
119         printf("The syntax of this command is:\n\n");
120         printf("NET HELP command\n    -or-\nNET command /HELP\n\n");
121         printf("   Commands available are:\n");
122         printf("   NET HELP    NET START    NET STOP\n");
123     }
124
125     if(!strcasecmp(argv[1], "start"))
126     {
127         if(argc < 3)
128         {
129             printf("Specify service name to start.\n");
130             return 1;
131         }
132
133         if(!net_service(NET_START, argv[2]))
134         {
135             return 1;
136         }
137         return 0;
138     }
139
140     if(!strcasecmp(argv[1], "stop"))
141     {
142         if(argc < 3)
143         {
144             printf("Specify service name to stop.\n");
145             return 1;
146         }
147
148         if(!net_service(NET_STOP, argv[2]))
149         {
150             return 1;
151         }
152         return 0;
153     }
154
155     return 0;
156 }