1 /*Tests all of the svcctl calls (at least at time of writing)*/
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;
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));
42 /*open a handle to the scm*/
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));
52 printf("Opened SCM\n");
55 while(tmp[0] != 'q') {
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");
70 printf("Enter option: ");
71 cactest_readline(stdin, tmp);
76 case 'e': /*enum services*/
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));
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);
92 case 'o': /*Open service*/
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));
107 printf("Opened service.\n");
108 svc_hnd = os.out.svc_hnd;
111 case 'x': /*close service*/
113 printf("Must open service first!\n");
117 cac_SvcClose(hnd, mem_ctx, svc_hnd);
120 case 'g': /*get svc status*/
123 printf("Must open service first!\n");
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));
136 print_service_status(gs.out.status);
138 case 's': /*start service*/
140 printf("Must open service first!\n");
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));
166 printf("Started service.\n");
170 case 't': /*stop service*/
172 printf("Must open service first!\n");
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));
187 printf("Service was not stopped within %d seconds.\n", stop.in.timeout);
188 print_service_status(stop.out.status);
193 print_service_status(stop.out.status);
196 case 'd': /*get display name*/
198 printf("Must open service first!\n");
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));
209 printf("\tDisplay Name: %s\n", gdn.out.display_name);
213 case 'p': /*pause service*/
215 printf("Must open service first!\n");
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));
230 printf("Service was not paused within %d seconds.\n", pause.in.timeout);
231 print_service_status(pause.out.status);
236 print_service_status(pause.out.status);
241 case 'r': /*resume service*/
243 printf("Must open service first!\n");
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));
258 printf("Service was not resumed within %d seconds.\n", res.in.timeout);
259 print_service_status(res.out.status);
264 print_service_status(res.out.status);
269 case 'c': /*get service config*/
271 printf("Must open service first!\n");
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));
283 print_service_config(&sgc.out.config);
290 printf("Invalid command\n");
294 cac_SvcClose(hnd, mem_ctx, sos.out.scm_hnd);
299 talloc_destroy(mem_ctx);