r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[Samba/gebeck_regimport.git] / examples / libmsrpc / test / svcctl / svc.c
blobdb5fa27895395b8edd3fac5b0c8369890d68f347
1 /*Tests all of the svcctl calls (at least at time of writing)*/
3 #include "libmsrpc.h"
4 #include "test_util.h"
6 int main(int argc, char **argv) {
7 CacServerHandle *hnd = NULL;
8 TALLOC_CTX *mem_ctx = NULL;
11 struct SvcOpenScm sos;
12 struct SvcEnumServices es;
13 struct SvcOpenService os;
14 struct SvcGetStatus gs;
15 struct SvcStartService start;
16 struct SvcStopService stop;
17 struct SvcPauseService pause;
18 struct SvcContinueService res;
19 struct SvcGetDisplayName gdn;
20 struct SvcGetServiceConfig sgc;
22 POLICY_HND *svc_hnd = NULL;
24 fstring tmp;
25 fstring input;
27 int i;
29 mem_ctx = talloc_init("cac_samgroup");
31 hnd = cac_NewServerHandle(True);
33 cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
35 cac_parse_cmd_line(argc, argv, hnd);
37 if(!cac_Connect(hnd, NULL)) {
38 fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
39 exit(-1);
42 /*open a handle to the scm*/
43 ZERO_STRUCT(sos);
45 sos.in.access = SC_MANAGER_ALL_ACCESS;
47 if(!cac_SvcOpenScm(hnd, mem_ctx, &sos)) {
48 fprintf(stderr, "Could not open SCM. Error: %s\n", nt_errstr(hnd->status));
49 goto done;
52 printf("Opened SCM\n");
54 tmp[0] = 0x00;
55 while(tmp[0] != 'q') {
56 printf("\n");
57 printf("[e] Enum Services\n");
58 printf("[o] Open Service\n");
59 printf("[x] Close Service\n");
60 printf("[g] Get service status\n");
61 printf("[s] Start service\n");
62 printf("[t] Stop service\n");
63 printf("[p] Pause service\n");
64 printf("[r] Resume service\n");
65 printf("[c] Get service config\n");
67 printf("[d] Get display name\n");
69 printf("[q]uit\n\n");
70 printf("Enter option: ");
71 cactest_readline(stdin, tmp);
73 printf("\n");
75 switch(tmp[0]) {
76 case 'e': /*enum services*/
77 ZERO_STRUCT(es);
78 es.in.scm_hnd = sos.out.scm_hnd;
80 if(!cac_SvcEnumServices(hnd, mem_ctx, &es)) {
81 printf("Could not enumerate services. Error: %s\n", nt_errstr(hnd->status));
82 break;
85 for(i = 0; i < es.out.num_services; i++) {
86 print_cac_service(es.out.services[i]);
88 printf("Enumerated %d services:\n", es.out.num_services);
90 break;
92 case 'o': /*Open service*/
93 ZERO_STRUCT(os);
95 printf("Enter service name: ");
96 cactest_readline(stdin, tmp);
98 os.in.name = talloc_strdup(mem_ctx, tmp);
99 os.in.scm_hnd = sos.out.scm_hnd;
100 os.in.access = SERVICE_ALL_ACCESS;
102 if(!cac_SvcOpenService(hnd, mem_ctx, &os)) {
103 printf("Could not open service. Error: %s\n", nt_errstr(hnd->status));
104 break;
107 printf("Opened service.\n");
108 svc_hnd = os.out.svc_hnd;
110 break;
111 case 'x': /*close service*/
112 if(!svc_hnd) {
113 printf("Must open service first!\n");
114 break;
117 cac_SvcClose(hnd, mem_ctx, svc_hnd);
118 svc_hnd = NULL;
119 break;
120 case 'g': /*get svc status*/
122 if(!svc_hnd) {
123 printf("Must open service first!\n");
124 break;
127 ZERO_STRUCT(gs);
129 gs.in.svc_hnd = svc_hnd;
131 if(!cac_SvcGetStatus(hnd, mem_ctx, &gs)) {
132 printf("Could not get status. Error: %s\n", nt_errstr(hnd->status));
133 break;
136 print_service_status(gs.out.status);
137 break;
138 case 's': /*start service*/
139 if(!svc_hnd) {
140 printf("Must open service first!\n");
141 break;
144 ZERO_STRUCT(start);
146 start.in.svc_hnd = svc_hnd;
148 printf("Enter number of parameters: ");
149 scanf("%d", &start.in.num_parms);
151 start.in.parms = talloc_array(mem_ctx, char *, start.in.num_parms);
153 for(i = 0; i < start.in.num_parms; i++) {
154 printf("Parm %d: ", i);
155 cactest_readline(stdin, tmp);
156 start.in.parms[i] = talloc_strdup(mem_ctx, tmp);
159 printf("Timeout (seconds): ");
160 scanf("%d", &start.in.timeout);
162 if(!cac_SvcStartService(hnd, mem_ctx, &start)) {
163 printf("Could not start service. Error: %s\n", nt_errstr(hnd->status));
165 else {
166 printf("Started service.\n");
169 break;
170 case 't': /*stop service*/
171 if(!svc_hnd) {
172 printf("Must open service first!\n");
173 break;
176 ZERO_STRUCT(stop);
177 stop.in.svc_hnd = svc_hnd;
179 printf("Timeout (seconds): ");
180 scanf("%d", &stop.in.timeout);
182 if(!cac_SvcStopService(hnd, mem_ctx, &stop)) {
183 if(CAC_OP_FAILED(hnd->status)) {
184 printf("Error occured: %s\n", nt_errstr(hnd->status));
186 else {
187 printf("Service was not stopped within %d seconds.\n", stop.in.timeout);
188 print_service_status(stop.out.status);
191 else {
192 printf("Done.\n");
193 print_service_status(stop.out.status);
195 break;
196 case 'd': /*get display name*/
197 if(!svc_hnd) {
198 printf("Must open service first!\n");
199 break;
202 ZERO_STRUCT(gdn);
203 gdn.in.svc_hnd = svc_hnd;
205 if(!cac_SvcGetDisplayName(hnd, mem_ctx, &gdn)) {
206 printf("Could not get display name. Error: %s\n", nt_errstr(hnd->status));
208 else {
209 printf("\tDisplay Name: %s\n", gdn.out.display_name);
211 break;
213 case 'p': /*pause service*/
214 if(!svc_hnd) {
215 printf("Must open service first!\n");
216 break;
219 ZERO_STRUCT(pause);
220 pause.in.svc_hnd = svc_hnd;
222 printf("Timeout (seconds): ");
223 scanf("%d", &pause.in.timeout);
225 if(!cac_SvcPauseService(hnd, mem_ctx, &pause)) {
226 if(CAC_OP_FAILED(hnd->status)) {
227 printf("Error occured: %s\n", nt_errstr(hnd->status));
229 else {
230 printf("Service was not paused within %d seconds.\n", pause.in.timeout);
231 print_service_status(pause.out.status);
234 else {
235 printf("Done.\n");
236 print_service_status(pause.out.status);
239 break;
241 case 'r': /*resume service*/
242 if(!svc_hnd) {
243 printf("Must open service first!\n");
244 break;
247 ZERO_STRUCT(res);
248 res.in.svc_hnd = svc_hnd;
250 printf("Timeout (seconds): ");
251 scanf("%d", &res.in.timeout);
253 if(!cac_SvcContinueService(hnd, mem_ctx, &res)) {
254 if(CAC_OP_FAILED(hnd->status)) {
255 printf("Error occured: %s\n", nt_errstr(hnd->status));
257 else {
258 printf("Service was not resumed within %d seconds.\n", res.in.timeout);
259 print_service_status(res.out.status);
262 else {
263 printf("Done.\n");
264 print_service_status(res.out.status);
267 break;
269 case 'c': /*get service config*/
270 if(!svc_hnd) {
271 printf("Must open service first!\n");
272 break;
275 ZERO_STRUCT(sgc);
277 sgc.in.svc_hnd = svc_hnd;
279 if(!cac_SvcGetServiceConfig(hnd, mem_ctx, &sgc)) {
280 printf("Could not get service config. Error: %s\n", nt_errstr(hnd->status));
282 else {
283 print_service_config(&sgc.out.config);
285 break;
287 case 'q': /*quit*/
288 break;
289 default:
290 printf("Invalid command\n");
294 cac_SvcClose(hnd, mem_ctx, sos.out.scm_hnd);
296 done:
297 cac_FreeHandle(hnd);
299 talloc_destroy(mem_ctx);
301 return 0;